{"id":13709983,"url":"https://github.com/mitchellh/zig-graph","last_synced_at":"2025-09-01T09:33:39.208Z","repository":{"id":66228478,"uuid":"439460222","full_name":"mitchellh/zig-graph","owner":"mitchellh","description":"Directed graph data structure for Zig","archived":false,"fork":false,"pushed_at":"2022-09-13T21:01:06.000Z","size":39,"stargazers_count":107,"open_issues_count":1,"forks_count":7,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-08-31T20:53:59.921Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Zig","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/mitchellh.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}},"created_at":"2021-12-17T21:06:40.000Z","updated_at":"2025-08-06T23:20:17.000Z","dependencies_parsed_at":"2023-04-22T03:39:20.047Z","dependency_job_id":null,"html_url":"https://github.com/mitchellh/zig-graph","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mitchellh/zig-graph","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitchellh%2Fzig-graph","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitchellh%2Fzig-graph/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitchellh%2Fzig-graph/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitchellh%2Fzig-graph/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mitchellh","download_url":"https://codeload.github.com/mitchellh/zig-graph/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitchellh%2Fzig-graph/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273100194,"owners_count":25045697,"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-09-01T02:00:09.058Z","response_time":120,"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":[],"created_at":"2024-08-02T23:00:49.821Z","updated_at":"2025-09-01T09:33:39.183Z","avatar_url":"https://github.com/mitchellh.png","language":"Zig","readme":"# zig-graph\n\nA Zig library for directed graph data structures and associated algorithms.\nThis library can be used for acyclic and cyclic graphs and unweighted and weighted\nedges. This library requires Zig 0.9+.\n\n**Warning: This is literally the first piece of Zig code I've ever written\nin my life.** I'm using this project as a way to learn how to do things in\nZig, what is idiomatic, what isn't, etc. Feedback is very welcome on how\nI can improve and I expect to alter the library a bit as I do so.  There is\nalso a lot of room for improvement in performance by various measures.\n\n## Features\n\n* Directed edges\n* Cycle detection\n* Strongly connected components\n* Cheap edge reversal\n* Depth-first traversal\n\n#### TODO\n\n* Vertex iterator\n* Edge iterator\n* Dijkstra for single-source shortest path w/ edge-weighting\n* Kahn for topological sorting\n* Shortest path given a topological sort\n* String marshaling for easier debugging\n* \"Unmanaged\" graph so allocator can be sent to each op\n\n## Example\n\n```zig\nconst std = @import(\"std\");\nconst graph = @import(\"graph\");\n\npub fn main() void {\n\t// Create a directed graph type for strings.\n\tconst Graph = graph.DirectedGraph([]const u8, std.hash_map.StringContext);\n\n\t// Initialize using some allocator\n\tvar g = Graph.init(std.debug.global_allocator);\n\tdefer g.deinit();\n\n\t// Add some vertices\n\ttry g.add(\"A\");\n\ttry g.add(\"B\");\n\ttry g.add(\"C\");\n\n\t// Add some edges with weights. For unweighted edges just make all\n\t// weights the same value.\n\ttry g.addEdge(\"A\", \"B\", 5);\n\ttry g.addEdge(\"A\", \"C\", 2);\n\ttry g.addEdge(\"B\", \"C\", 2);\n\ttry g.addEdge(\"C\", \"B\", 3);\n\n\t// We can detect cycles\n\tif (g.cycles()) |cycles| {\n\t\tdefer cycles.deinit();\n\t\tstd.log.info(\"there are {d} cycles\", .{cycles.count()});\n\t\treturn;\n\t}\n\n\t// We can do a depth-first search through iteration.\n\tvar dfsIter = try g.dfsIterator(\"B\");\n\twhile (dfsIter.next()) |id| {\n\t\tstd.log.info(\"{}\", .{g.lookup(id).?});\n\t}\n\tdfsIter.deinit();\n\n\t// We can easily reverse the graph if we want.\n\tconst reversed = g.reverse();\n\n\t// ... and more\n}\n```\n","funding_links":[],"categories":["Libraries","Zig","Language Essentials"],"sub_categories":["Data Structure and Algorithm"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmitchellh%2Fzig-graph","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmitchellh%2Fzig-graph","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmitchellh%2Fzig-graph/lists"}