{"id":20409678,"url":"https://github.com/sleekpanther/topological-ordering","last_synced_at":"2025-08-23T06:33:17.330Z","repository":{"id":115108645,"uuid":"98943326","full_name":"SleekPanther/topological-ordering","owner":"SleekPanther","description":"Finds a Topological Ordering of vertices in a Directed Acyclic Graph","archived":false,"fork":false,"pushed_at":"2017-08-03T11:31:42.000Z","size":267,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-23T06:32:39.115Z","etag":null,"topics":["algorithm-design","algorithms","graph","graph-algorithms","graphs","incoming","incoming-edge","noah","noah-patullo","noahpatullo","order","pattullo","pattulo","patullo","patulo","sort","topological","topological-order","topological-ordering","topological-sort"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SleekPanther.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-08-01T00:57:00.000Z","updated_at":"2017-08-03T12:29:16.000Z","dependencies_parsed_at":null,"dependency_job_id":"feeb08c3-2cb6-404d-9c6d-040793543c3d","html_url":"https://github.com/SleekPanther/topological-ordering","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/SleekPanther/topological-ordering","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SleekPanther%2Ftopological-ordering","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SleekPanther%2Ftopological-ordering/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SleekPanther%2Ftopological-ordering/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SleekPanther%2Ftopological-ordering/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SleekPanther","download_url":"https://codeload.github.com/SleekPanther/topological-ordering/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SleekPanther%2Ftopological-ordering/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271745678,"owners_count":24813521,"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","status":"online","status_checked_at":"2025-08-23T02:00:09.327Z","response_time":69,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["algorithm-design","algorithms","graph","graph-algorithms","graphs","incoming","incoming-edge","noah","noah-patullo","noahpatullo","order","pattullo","pattulo","patullo","patulo","sort","topological","topological-order","topological-ordering","topological-sort"],"created_at":"2024-11-15T05:42:55.992Z","updated_at":"2025-08-23T06:33:17.301Z","avatar_url":"https://github.com/SleekPanther.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Topological Ordering\nFinds a Topological Ordering of vertices in a Directed Acyclic Graph\n\n## Problem Statement\n**Topological Ordering:** arranges nodes as v\u003csub\u003e1\u003c/sub\u003e, v\u003csub\u003e2\u003c/sub\u003e ..., v\u003csub\u003en\u003c/sub\u003e so that for every edge (v\u003csub\u003ei\u003c/sub\u003e, v\u003csub\u003ej\u003c/sub\u003e), i \u003c j\n- Must be a Directed Graph\n- Must NOT contain a cycle\n\n### Graph 1\n\u003cimg src=\"images/graph1.png\" width=\"400\"\u003e\n\n### Graph 1 Topological Ordering\n\u003cimg src=\"images/graph1-topological-ordering.png\" width=\"550\"\u003e\n\n**Can can be more than 1 topologcal ordering. Algorithm finds 1 if 1 exists**\n\n\n## Algorithm\nA DAG must have a node with **no incoming edges**  \nPick a node `v` with no incoming edges  \nPrint `v`  \nCalculate a topological ordering on G – {`v`}  \nEnds when all nodes are included in the ordering\n\nKeep track of 2 things:\n- `count[w]` = number of incoming edges\n- `S` = set of nodes with no incoming edges\n\nSet-Up\n- Scan through the graph to initialize `count[]` and `S`\n- **O(m + n)**\n\nFinding the Ordering\n- Remove v from S\n- Decrement `count[w]` for all edges from `v` to` w`  \nadd `w` to `S` if `count[w]` hits `0`\n- **O(1) per edge**\n\n\n## Runtime\n**O(m + n)**\n\n## Usage\n**Detects cycles if graph is not a DAG and prints an error message**  \n**Node names are integers for simplicity of the code \u0026 start at `0`**\n\n- Create a graph as an adjacency list  \n`ArrayList\u003cArrayList\u003cInteger\u003e\u003e graph1 = new ArrayList\u003cArrayList\u003cInteger\u003e\u003e();`\n  - Add rows for each vertex. `graph1.get(u)` is a list of nodes representing edges FROM `u`\n  - Orphan nodes are allowed (i.e. a node with no incoming or outgoing edges). It doesn't have to be the last node in the graph, but is probably easier if it is. Just make sure no edges go to that node. (e.g. if node `5` is an orphan, then `5` must not appear in any of the rows of the adjacency list)\n  - Nodes with no outgoing edges just need an empty `ArrayList`  \n  `graph1.add(new ArrayList\u003cInteger\u003e());`\n- Run `TopologicalOrdering.findTopologicalOrdering(graph1);`\n\n### Graph 2\n\u003cimg src=\"images/graph2.png\" width=\"400\"\u003e\n\n### Graph 2 Topological Ordering\n\u003cimg src=\"images/graph2-topological-ordering.png\" width=\"550\"\u003e\n\n### Graph 3 (No topological ordering)\n\u003cimg src=\"images/graph3.png\" width=\"200\"\u003e\nContains a cycle, \u0026 no nodes with no incoming edges\n\n### Graph 4 (No topological ordering)\n\u003cimg src=\"images/graph4.png\" width=\"350\"\u003e\nContains a cycle\n\n\n## References\n- [Kevin Wayne Slides](https://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/03Graphs.pdf#page=45)\n- [University of Washington slides](https://homes.cs.washington.edu/~jrl/421slides/lec5.pdf)\n- [Rajesh Rao Slides](https://courses.cs.washington.edu/courses/cse326/03wi/lectures/RaoLect20.pdf)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsleekpanther%2Ftopological-ordering","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsleekpanther%2Ftopological-ordering","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsleekpanther%2Ftopological-ordering/lists"}