{"id":17767281,"url":"https://github.com/jamiebuilds/graph-sequencer","last_synced_at":"2025-03-15T13:31:14.003Z","repository":{"id":57253165,"uuid":"113151213","full_name":"jamiebuilds/graph-sequencer","owner":"jamiebuilds","description":"Sort items in a graph using a topological sort while resolving cycles with priority groups","archived":false,"fork":false,"pushed_at":"2020-09-04T01:07:36.000Z","size":110,"stargazers_count":28,"open_issues_count":7,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-12T21:02:44.791Z","etag":null,"topics":["adjacency-lists","cycles","graph","priority-list","sequencing","sorting"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jamiebuilds.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}},"created_at":"2017-12-05T07:59:01.000Z","updated_at":"2024-02-04T12:16:15.000Z","dependencies_parsed_at":"2022-08-31T22:11:21.485Z","dependency_job_id":null,"html_url":"https://github.com/jamiebuilds/graph-sequencer","commit_stats":null,"previous_names":["thejameskyle/graph-sequencer"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamiebuilds%2Fgraph-sequencer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamiebuilds%2Fgraph-sequencer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamiebuilds%2Fgraph-sequencer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamiebuilds%2Fgraph-sequencer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jamiebuilds","download_url":"https://codeload.github.com/jamiebuilds/graph-sequencer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243735838,"owners_count":20339536,"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":["adjacency-lists","cycles","graph","priority-list","sequencing","sorting"],"created_at":"2024-10-26T20:45:47.906Z","updated_at":"2025-03-15T13:31:13.687Z","avatar_url":"https://github.com/jamiebuilds.png","language":"JavaScript","readme":"# graph-sequencer\n\n\u003e Sort items in a graph using a topological sort while resolving cycles with\n\u003e priority groups.\n\nSay you have some sort of graph of dependencies: (using an adjacency list)\n\n```js\nlet graph = new Map([\n  [\"task-a\", [\"task-d\"]], // task-a depends on task-d\n  [\"task-b\", [\"task-d\", \"task-a\"]],\n  [\"task-c\", [\"task-d\"]],\n  [\"task-d\", [\"task-a\"]],\n]);\n```\n\nYou could run a topological sort on these items, but you'd still end up with\ncycles:\n\n```\ntask-a -\u003e task-d -\u003e task-a\n```\n\nTo resolve this you pass \"priority groups\" to `graph-sequencer`:\n\n```js\nlet groups = [\n  [\"task-d\"], // higher priority\n  [\"task-a\", \"task-b\", \"task-c\"], // lower priority\n];\n```\n\nThe result will be a chunked list of items sorted topologically and by the\npriority groups:\n\n```js\nlet chunks = [\n  [\"task-d\"],\n  [\"task-a\", \"task-c\"],\n  [\"task-b\"]\n];\n```\n\nYou can then run all these items in order with maximum concurrency:\n\n```js\nfor (let chunk of chunks) {\n  await Promise.all(chunk.map(task =\u003e exec(task)));\n}\n```\n\nHowever, even with priority groups you can still accidentally create cycles of\ndependencies in your graph.\n\n`graph-sequencer` will return a list of the unresolved cycles:\n\n```js\nlet cycles = [\n  [\"task-a\", \"task-b\"] // task-a depends on task-b which depends on task-a\n];\n```\n\nHowever, `graph-sequencer` will still return an \"unsafe\" set of chunks. When it\ncomes across a cycle, it will add another chunk with the item with the fewest\ndependencies remaining which will often break cycles.\n\n\nAll together that looks like this:\n\n```js\nconst graphSequencer = require('graph-sequencer');\n\ngraphSequencer({\n  graph: new Map([\n    [\"task-a\", [\"task-d\"]], // task-a depends on task-d\n    [\"task-b\", [\"task-d\", \"task-a\"]],\n    [\"task-c\", [\"task-d\"]],\n    [\"task-d\", [\"task-a\"]],\n  ]);\n  groups: [\n    [\"task-d\"], // higher priority\n    [\"task-a\", \"task-b\", \"task-c\"], // lower priority\n  ],\n})\n// {\n//   safe: true,\n//   chunks: [[\"task-d\"], [\"task-a\", \"task-c\"], [\"task-b\"]],\n//   cycles: [],\n// }\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamiebuilds%2Fgraph-sequencer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjamiebuilds%2Fgraph-sequencer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamiebuilds%2Fgraph-sequencer/lists"}