{"id":16774066,"url":"https://github.com/marcelklehr/toposort","last_synced_at":"2025-05-15T08:11:20.960Z","repository":{"id":56716170,"uuid":"6506814","full_name":"marcelklehr/toposort","owner":"marcelklehr","description":"Topologically sort directed acyclic graphs (such as dependency lists) in javascript","archived":false,"fork":false,"pushed_at":"2023-07-10T21:24:28.000Z","size":173,"stargazers_count":303,"open_issues_count":4,"forks_count":38,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-05-01T05:01:42.533Z","etag":null,"topics":["directed-edges","edges","javascript","toposort","vertices"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/marcelklehr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"License","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2012-11-02T13:57:40.000Z","updated_at":"2025-03-08T10:47:20.000Z","dependencies_parsed_at":"2023-02-11T07:00:36.336Z","dependency_job_id":"13d10081-65e5-4287-86dd-d5cd4952a5bf","html_url":"https://github.com/marcelklehr/toposort","commit_stats":{"total_commits":82,"total_committers":7,"mean_commits":"11.714285714285714","dds":"0.24390243902439024","last_synced_commit":"3e3d72d1b48196ab0e87348d142ef23788a5bb67"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcelklehr%2Ftoposort","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcelklehr%2Ftoposort/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcelklehr%2Ftoposort/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcelklehr%2Ftoposort/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marcelklehr","download_url":"https://codeload.github.com/marcelklehr/toposort/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253497501,"owners_count":21917697,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["directed-edges","edges","javascript","toposort","vertices"],"created_at":"2024-10-13T06:47:55.208Z","updated_at":"2025-05-15T08:11:20.938Z","avatar_url":"https://github.com/marcelklehr.png","language":"JavaScript","funding_links":[],"categories":["Packages"],"sub_categories":[],"readme":"# Toposort\n\nSort directed acyclic graphs\n\n[![Build Status](https://travis-ci.org/marcelklehr/toposort.png)](https://travis-ci.org/marcelklehr/toposort)\n\n## Installation\n\n`npm install toposort` or `component install marcelklehr/toposort`  \n\nthen in your code:\n\n```js\ntoposort = require('toposort')\n```\n\n## Usage\nWe want to sort the following graph.\n\n![graph](https://cdn.rawgit.com/marcelklehr/toposort/8b14e9fd/graph.svg)\n\n```js\n// First, we define our edges.\nvar graph = [\n  ['put on your shoes', 'tie your shoes']\n, ['put on your shirt', 'put on your jacket']\n, ['put on your shorts', 'put on your jacket']\n, ['put on your shorts', 'put on your shoes']\n]\n\n\n// Now, sort the vertices topologically, to reveal a legal execution order.\ntoposort(graph)\n// [ 'put on your shirt'\n// , 'put on your shorts'\n// , 'put on your jacket'\n// , 'put on your shoes'\n// , 'tie your shoes' ]\n```\n\n(Note that there is no defined order for graph parts that are not connected\n -- you could also put on your jacket after having tied your shoes...)\n\n### Sorting dependencies\nIt is usually more convenient to specify *dependencies* instead of \"sequences\".\n```js\n// This time, edges represent dependencies.\nvar graph = [\n  ['tie your shoes', 'put on your shoes']\n, ['put on your jacket', 'put on your shirt']\n, ['put on your shoes', 'put on your shorts']\n, ['put on your jacket', 'put on your shorts']\n]\n\ntoposort(graph) \n// [ 'tie your shoes'\n// , 'put on your shoes'\n// , 'put on your jacket'\n// , 'put on your shirt'\n// , 'put on your shorts' ]\n\n// Now, reversing the list will reveal a legal execution order.\ntoposort(graph).reverse() \n// [ 'put on your shorts'\n// , 'put on your shirt'\n// , 'put on your jacket'\n// , 'put on your shoes'\n// , 'tie your shoes' ]\n```\n\n## API\n\n### toposort(edges)\n\n+ edges {Array} An array of directed edges describing a graph. An edge looks like this: `[node1, node2]` (vertices needn't be strings but can be of any type).\n\nReturns: {Array} a list of vertices, sorted from \"start\" to \"end\"\n\nThrows an error if there are any cycles in the graph.\n\n### toposort.array(nodes, edges)\n\n+ nodes {Array} An array of nodes\n+ edges {Array} An array of directed edges. You don't need to mention all `nodes` here.\n\nThis is a convenience method that allows you to define nodes that may or may not be connected to any other nodes. The ordering of unconnected nodes is not defined.\n\nReturns: {Array} a list of vertices, sorted from \"start\" to \"end\"\n\nThrows an error if there are any cycles in the graph.\n\n## Tests\n\nRun the tests with `node test.js`.\n\n## Legal\n\nMIT License\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcelklehr%2Ftoposort","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarcelklehr%2Ftoposort","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcelklehr%2Ftoposort/lists"}