{"id":26784220,"url":"https://github.com/nelsonbn/algorithms-data-structures-kahn","last_synced_at":"2025-03-29T10:18:47.918Z","repository":{"id":283852107,"uuid":"953100267","full_name":"NelsonBN/algorithms-data-structures-kahn","owner":"NelsonBN","description":"Algorithms and Data Structures - Kahn","archived":false,"fork":false,"pushed_at":"2025-03-22T15:16:16.000Z","size":3,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-22T16:24:57.359Z","etag":null,"topics":["algorithms","algorithms-and-data-structures","data-structures","graphs"],"latest_commit_sha":null,"homepage":"","language":"Python","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/NelsonBN.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-22T15:15:16.000Z","updated_at":"2025-03-22T15:37:04.000Z","dependencies_parsed_at":"2025-03-22T16:35:03.894Z","dependency_job_id":null,"html_url":"https://github.com/NelsonBN/algorithms-data-structures-kahn","commit_stats":null,"previous_names":["nelsonbn/algorithms-data-structures-kahn"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NelsonBN%2Falgorithms-data-structures-kahn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NelsonBN%2Falgorithms-data-structures-kahn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NelsonBN%2Falgorithms-data-structures-kahn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NelsonBN%2Falgorithms-data-structures-kahn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NelsonBN","download_url":"https://codeload.github.com/NelsonBN/algorithms-data-structures-kahn/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246168108,"owners_count":20734391,"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":["algorithms","algorithms-and-data-structures","data-structures","graphs"],"created_at":"2025-03-29T10:18:47.397Z","updated_at":"2025-03-29T10:18:47.909Z","avatar_url":"https://github.com/NelsonBN.png","language":"Python","readme":"# Algorithms and Data Structures - Kahn\n\nThe Kahn's algorithm is a method for **topological sorting** of a **directed acyclic graph (DAG)**. It works by iteratively removing nodes with no incoming edges and adding them to the sorted order. The process continues until all nodes are removed or a cycle is detected.\n\n\n## Characteristics\n- Time complexity: O(V + E) - Because the algorithm processes each vertex and edge exactly once.\n  - V = number of vertices\n  - E = number of edges\n- Space complexity: O(V) - The algorithm use a list to store the in-degree of each vertex and a queue to store the vertices with in-degree 0.\n\n\n\n## Demos\n\n### Using Numbered Vertices\n\n```mermaid\ngraph LR\n  5((5)) --\u003e 2\n  5 --\u003e 0((0))\n  4((4)) --\u003e 0\n  4 --\u003e 1((1))\n  2((2)) --\u003e 3\n  3((3)) --\u003e 1\n```\n\n[Implementation](./src/01-kahn-using-numbered-vertices.py)\n\n\n### Using Labelled Vertices\n\n**DAG**:\n```mermaid\ngraph LR\n  A((A)) --\u003e B((B))\n  B --\u003e C\n  B --\u003e D\n  C((C)) --\u003e E\n  D((D)) --\u003e E\n  D --\u003e F\n  E((E)) --\u003e G((G))\n  F((F)) --\u003e G\n```\n**Cycle**:\n```mermaid\ngraph LR\n  A((A)) --\u003e B\n  B((B)) --\u003e C\n  C((C)) --\u003e A\n  D((D)) --\u003e B\n  D --\u003e C\n  E((E)) --\u003e C\n  E --\u003e D\n\n  linkStyle 0,1,2 stroke:#f00\n```\n\n[Implementation](./src/02-kahn-using-labelled-vertices.py)\n\n\n### Detect if has more than one topological sorting\n\n```mermaid\ngraph LR\n  A((A)) --\u003e B\n  B((B)) --\u003e C((C))\n```\n```mermaid\ngraph LR\n  A((A)) --\u003e B\n  A --\u003e C\n\n  B((B)) --\u003e C\n  B --\u003e D((D))\n  B --\u003e E\n\n  C((C)) --\u003e E((E))\n  C --\u003e F((F))\n```\n\n[Implementation](./src/03-kahn-detecting-multiple-topological-sorting.py)\n\n\n\n## References\n- [Topological Sorting with DFS](https://github.com/NelsonBN/algorithms-data-structures-topological-sorting-dfs)\n- [Other Algorithms \u0026 Data Structures](https://github.com/NelsonBN/algorithms-data-structures)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnelsonbn%2Falgorithms-data-structures-kahn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnelsonbn%2Falgorithms-data-structures-kahn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnelsonbn%2Falgorithms-data-structures-kahn/lists"}