{"id":22192336,"url":"https://github.com/mzusin/mz-graph","last_synced_at":"2025-08-03T13:13:17.824Z","repository":{"id":206736743,"uuid":"717577497","full_name":"mzusin/mz-graph","owner":"mzusin","description":"Typescript implementation of graphs.","archived":false,"fork":false,"pushed_at":"2024-06-09T14:32:27.000Z","size":191,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-08T19:31:44.185Z","etag":null,"topics":["adjacency-list","adjacency-matrix","bfs","cycle-detection","dfs","dijkstra","dijkstra-algorithm","disjoint-set-union","dsu","graph","matrix","union-find"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/mzusin.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":"2023-11-11T22:22:12.000Z","updated_at":"2024-06-09T14:32:30.000Z","dependencies_parsed_at":"2023-11-12T00:19:48.895Z","dependency_job_id":"b51c2a82-381a-459e-a269-5989a84287a2","html_url":"https://github.com/mzusin/mz-graph","commit_stats":null,"previous_names":["mzusin/mz-graph"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mzusin/mz-graph","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mzusin%2Fmz-graph","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mzusin%2Fmz-graph/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mzusin%2Fmz-graph/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mzusin%2Fmz-graph/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mzusin","download_url":"https://codeload.github.com/mzusin/mz-graph/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mzusin%2Fmz-graph/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268550264,"owners_count":24268336,"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-03T02:00:12.545Z","response_time":2577,"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":["adjacency-list","adjacency-matrix","bfs","cycle-detection","dfs","dijkstra","dijkstra-algorithm","disjoint-set-union","dsu","graph","matrix","union-find"],"created_at":"2024-12-02T12:22:01.994Z","updated_at":"2025-08-03T13:13:17.779Z","avatar_url":"https://github.com/mzusin.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Graphs\n\nTypescript implementation of graphs.\n\n**Adjacency List Representation**\n\n```ts\nexport type Label = string|number;\n\nexport interface IVertex\u003cT\u003e {\n    label: Label;\n    edgeWeight: T;\n}\n\nexport type AdjacencyList\u003cT\u003e = Map\u003cLabel, IVertex\u003cT\u003e[]\u003e;\nexport interface IGraph\u003cT\u003e {\n    addVertex: (label: Label) =\u003e void;\n    getVertex: (label: Label) =\u003e IVertex\u003cT\u003e[]|null;\n    hasVertex: (label: Label) =\u003e boolean;\n    addEdge: (source: Label, destination: Label, edgeWeight?: T) =\u003e void;\n    printGraph: () =\u003e void;\n\n    bfs: (callback: (label: Label) =\u003e void, startLabel?: Label) =\u003e void;\n    dfs: (callback: (label: Label) =\u003e void, startLabel?: Label) =\u003e void;\n    dfsRecursive: (callback: (label: Label, startLabel?: Label) =\u003e void) =\u003e void;\n\n    hasCycle: () =\u003e boolean;\n    findShortestPathDijkstra: (startLabel: Label) =\u003e Map\u003cLabel, number\u003e;\n}\n\nexport interface IAdjacencyListOptions\u003cT\u003e {\n    isDirected?: boolean;\n    initial?: { [key: Label]: IVertex\u003cT\u003e[] };\n}\n\nexport const graph: \u003cT\u003e(options: IAdjacencyListOptions\u003cT\u003e) =\u003e IGraph\u003cT\u003e;\n```\n\nUsage example:\n\n```ts\nconst myGraph: IGraph\u003cnumber\u003e = graph\u003cnumber\u003e({\n    isDirected: true,\n    initial: {\n        A: [{ label: 'B', edgeWeight: 10 }],\n        B: [{ label: 'C', edgeWeight: 20 }],\n        C: [],\n    }\n});\n\n// or\n\nconst myGraph: IGraph\u003cnumber\u003e = graph\u003cnumber\u003e({\n    isDirected: false\n});\n\n// add/get a vertex\nmyGraph.addVertex('A'); // or use a number myGraph.addVertex(10);\nconsole.log(myGraph.getVertex('A'));\n\n// add an edge\nmyGraph.addVertex('B');\nmyGraph.addVertex('C');\nmyGraph.addEdge('A', 'B', 10);\nmyGraph.addEdge('B', 'C');\n\n// print the graph\nmyGraph.printGraph();\n\nmyGraph.bfs((label: Label) =\u003e {\n    console.log(label);\n});\n\nmyGraph.dfs((label: Label) =\u003e {\n    console.log(label);\n});\n\nmyGraph.dfsRecursive((label: Label) =\u003e {\n    console.log(label);\n});\n\nconsole.log(myGraph.hasCycle()); // true or false\n\nconst distances = myGraph.findShortestPathDijkstra('A');\n```\n\n**Adjacency Matrix Representation**\n\n```ts\nexport type AdjacencyMatrix\u003cT\u003e = T[][];\n\nexport interface IMatrix\u003cT\u003e {\n    getMatrix: () =\u003e AdjacencyMatrix\u003cT\u003e;\n    addEdge: (source: number, destination: number, weight: T) =\u003e void;\n    printGraph: () =\u003e void;\n    bfs: (callback: (row: number, col: number, value: T) =\u003e void) =\u003e void;\n    dfs: (callback: (row: number, col: number, value: T) =\u003e void) =\u003e void;\n}\n\nexport interface IAdjacencyMatrixOptions\u003cT\u003e {\n    isDirected?: boolean;\n    rowsCount?: number;\n    columnsCount?: number;\n    defaultValue?: T;\n    initial?: T[][];\n}\n\nexport const matrix: \u003cT\u003e(options: IAdjacencyMatrixOptions\u003cT\u003e) =\u003e IMatrix\u003cT\u003e;\n```\n\nUsage example:\n\n```ts\nconst myGraph: IMatrix\u003cnumber\u003e = matrix\u003cnumber\u003e({\n    initial: [\n        [2, 1],\n        [1, 2],\n    ]\n});\n\n// or\n\n// create a matrix 2x2 with default value 0\nconst myGraph: IMatrix\u003cnumber\u003e = matrix\u003cnumber\u003e({\n    isDirected: false,\n    rowsCount: 2,\n    columnsCount: 2,\n    defaultValue: 0,\n});\n\n// add edge, row = 0, col = 1, weight = 5\nmyGraph.addEdge(0, 1, 5);\n\n// get matrix\nconst res = myGraph.getMatrix();\n\n// print the matrix\nmyGraph.printGraph();\n\nmyGraph.bfs((row: number, col: number, value: number) =\u003e {\n    console.log(row, col, value);\n});\n\nmyGraph.dfs((row: number, col: number, value: number) =\u003e {\n    console.log(row, col, value);\n});\n```\n\n**Union-Find**\n```ts\nconst uf: IUnionFind = unionFind(5);\n\nexpect(uf.union(0, 1)).toBe(true);\nexpect(uf.find(0)).toBe(0);\nexpect(uf.find(1)).toBe(0);\n\nexpect(uf.union(1, 2)).toBe(true);\nexpect(uf.find(2)).toBe(0);\n\nexpect(uf.union(3, 4)).toBe(true);\nexpect(uf.find(3)).toBe(3);\nexpect(uf.find(4)).toBe(3);\n\nexpect(uf.union(2, 4)).toBe(true);\nexpect(uf.find(0)).toBe(0);\nexpect(uf.find(1)).toBe(0);\nexpect(uf.find(2)).toBe(0);\nexpect(uf.find(3)).toBe(0);\nexpect(uf.find(4)).toBe(0);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmzusin%2Fmz-graph","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmzusin%2Fmz-graph","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmzusin%2Fmz-graph/lists"}