{"id":23080599,"url":"https://github.com/chen0040/js-graph-algorithms","last_synced_at":"2025-04-04T23:07:56.788Z","repository":{"id":57283153,"uuid":"92423638","full_name":"chen0040/js-graph-algorithms","owner":"chen0040","description":"Package provides javascript implementation of algorithms for graph processing","archived":false,"fork":false,"pushed_at":"2023-08-09T19:51:17.000Z","size":1566,"stargazers_count":150,"open_issues_count":9,"forks_count":40,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-28T22:12:24.494Z","etag":null,"topics":["bellman-ford-algorithm","breadth-first-search","connected-components","depth-first-search","dijkstra","dijkstra-shortest-path","kruskal-algorithm","maxflow","mincut","minimum-spanning-trees","prim-algorithm","strongly-connected-components","topological-sort"],"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/chen0040.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":"2017-05-25T16:39:36.000Z","updated_at":"2025-01-19T08:49:07.000Z","dependencies_parsed_at":"2024-06-18T13:41:20.099Z","dependency_job_id":"b3dec418-2912-4cb7-88be-ff787635354b","html_url":"https://github.com/chen0040/js-graph-algorithms","commit_stats":{"total_commits":49,"total_committers":2,"mean_commits":24.5,"dds":0.08163265306122447,"last_synced_commit":"01b523553f2169923185cd0c39a434c5792e9458"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chen0040%2Fjs-graph-algorithms","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chen0040%2Fjs-graph-algorithms/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chen0040%2Fjs-graph-algorithms/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chen0040%2Fjs-graph-algorithms/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chen0040","download_url":"https://codeload.github.com/chen0040/js-graph-algorithms/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247261604,"owners_count":20910108,"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":["bellman-ford-algorithm","breadth-first-search","connected-components","depth-first-search","dijkstra","dijkstra-shortest-path","kruskal-algorithm","maxflow","mincut","minimum-spanning-trees","prim-algorithm","strongly-connected-components","topological-sort"],"created_at":"2024-12-16T13:15:24.117Z","updated_at":"2025-04-04T23:07:56.766Z","avatar_url":"https://github.com/chen0040.png","language":"JavaScript","readme":"# js-graph-algorithms\nPackage provides javascript implementation of algorithms for graph processing\n\n[![Build Status](https://travis-ci.org/chen0040/js-graph-algorithms.svg?branch=master)](https://travis-ci.org/chen0040/js-graph-algorithms) [![Coverage Status](https://coveralls.io/repos/github/chen0040/js-graph-algorithms/badge.svg?branch=master)](https://coveralls.io/github/chen0040/js-graph-algorithms?branch=master) \n\n# Features\n\n* Depth First Search (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-dfs.html))\n* Breadth First Search\n* Connected Components for undirected graph (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-connected-components.html))\n* Topoloical Sort (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-topo-sort.html))\n* Strongly Connected Components for directed graph (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-strongly-connected-components.html))\n* Minimum Spanning Tree for weighted graph (Kruskal, Prim Lazy, Prim Eager) (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-kruskal.html))\n* Shortest Paths (Dijkstra, Bellman-Ford, Topological Sort on DAG) (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-dijkstra.html))\n* MaxFlow-MinCut (Ford-Fulkerson) (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-ford-fulkerson.html))\n\n# Install\n\n```bash\nnpm install js-graph-algorithms\n```\n\n# Usage\n\n### Create an undirected unweighted graph\n\nThe sample code below shows how to create a undirected and unweighted graph (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-graph.html)):\n\n```javascript\nvar jsgraphs = require('js-graph-algorithms');\n\nvar g = new jsgraphs.Graph(6); // 6 is the number vertices in the graph\ng.addEdge(0, 5); // add undirected edge connecting vertex 0 to vertex 5\ng.addEdge(2, 4);\ng.addEdge(2, 3);\ng.addEdge(1, 2);\ng.addEdge(0, 1);\ng.addEdge(3, 4);\ng.addEdge(3, 5);\ng.addEdge(0, 2);\n\ng.node(2).label = 'Hello'; // assigned 'Hello' as label for node 2\ng.edge(0, 2).label = 'World'; // edge between 0 and 2\n\nconsole.log(g.V); // display 6, which is the number of vertices in g\nconsole.log(g.adj(0)); // display [5, 1, 2], which is the adjacent list to vertex 0\n```\n\n### Create directed unweighted graph\n\nThe sample code below shows how to create a direted and unweighted graph (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-digraph.html)):\n\n```javascript\nvar jsgraphs = require('js-graph-algorithms');\n\nvar g = new jsgraphs.DiGraph(13); // 13 is the number vertices in the graph\ng.addEdge(4,  2); // add directed edge from 4 to 2\ng.addEdge(2,  3);\ng.addEdge(3,  2);\ng.addEdge(6,  0);\ng.addEdge(0,  1);\ng.addEdge(2,  0);\ng.addEdge(11, 12);\ng.addEdge(12,  9);\ng.addEdge(9, 10);\ng.addEdge(9, 11);\ng.addEdge(7,  9);\ng.addEdge(10, 12);\ng.addEdge(11,  4);\ng.addEdge(4,  3);\ng.addEdge(3,  5);\ng.addEdge(6,  8);\ng.addEdge(8,  6);\ng.addEdge(5,  4);\ng.addEdge(0,  5);\ng.addEdge(6,  4);\ng.addEdge(6,  9);\ng.addEdge(7,  6);\n\ng.node(2).label = 'Hello'; // assign 'Hello' as label for node 2\ng.edge(0, 5).label = 'World'; // edge from 0 to 5\n\nconsole.log(g.V); // display 13, which is the number of vertices in g\nconsole.log(g.adj(0)); // display the adjacency list which are vertices directed from vertex 0\n```\n\n### Create undirected weighted graph\n\nThe sample code below shows show to create undirected weighted graph (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-weighted-graph.html)):\n\n\n```javascript\nvar jsgraphs = require('js-graph-algorithms');\nvar g = new jsgraphs.WeightedGraph(8); // 8 is the number vertices in the graph\ng.addEdge(new jsgraphs.Edge(0, 7, 0.16));\ng.addEdge(new jsgraphs.Edge(2, 3, 0.17));\ng.addEdge(new jsgraphs.Edge(1, 7, 0.19));\ng.addEdge(new jsgraphs.Edge(0, 2, 0.26));\ng.addEdge(new jsgraphs.Edge(5, 7, 0.28));\ng.addEdge(new jsgraphs.Edge(1, 3, 0.29));\ng.addEdge(new jsgraphs.Edge(1, 5, 0.32));\ng.addEdge(new jsgraphs.Edge(2, 7, 0.34));\ng.addEdge(new jsgraphs.Edge(4, 5, 0.35));\ng.addEdge(new jsgraphs.Edge(1, 2, 0.36));\ng.addEdge(new jsgraphs.Edge(4, 7, 0.37));\ng.addEdge(new jsgraphs.Edge(0, 4, 0.38));\ng.addEdge(new jsgraphs.Edge(6, 2, 0.4));\ng.addEdge(new jsgraphs.Edge(3, 6, 0.52));\ng.addEdge(new jsgraphs.Edge(6, 0, 0.58));\ng.addEdge(new jsgraphs.Edge(6, 4, 0.93));\n\ng.node(2).label = 'Hello'; // assign 'Hello' as label for node 2\ng.edge(4, 5).label = 'World'; // edge between node 4 and 5\n\nconsole.log(g.V); // display 13, which is the number of vertices in g\nconsole.log(g.adj(0)); // display the adjacency list which are undirected edges connected to vertex 0\n```\n\n### Create directed weighted graph\n\nThe sample code below shows show to create directed weighted graph (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-weighted-digraph.html)):\n\n```javascript\nvar jsgraphs = require('js-graph-algorithms');\nvar g = new jsgraphs.WeightedDiGraph(8); // 8 is the number vertices in the graph\ng.addEdge(new jsgraphs.Edge(0, 7, 0.16));\ng.addEdge(new jsgraphs.Edge(2, 3, 0.17));\ng.addEdge(new jsgraphs.Edge(1, 7, 0.19));\ng.addEdge(new jsgraphs.Edge(0, 2, 0.26));\ng.addEdge(new jsgraphs.Edge(5, 7, 0.28));\ng.addEdge(new jsgraphs.Edge(1, 3, 0.29));\ng.addEdge(new jsgraphs.Edge(1, 5, 0.32));\ng.addEdge(new jsgraphs.Edge(2, 7, 0.34));\ng.addEdge(new jsgraphs.Edge(4, 5, 0.35));\ng.addEdge(new jsgraphs.Edge(1, 2, 0.36));\ng.addEdge(new jsgraphs.Edge(4, 7, 0.37));\ng.addEdge(new jsgraphs.Edge(0, 4, 0.38));\ng.addEdge(new jsgraphs.Edge(6, 2, 0.4));\ng.addEdge(new jsgraphs.Edge(3, 6, 0.52));\ng.addEdge(new jsgraphs.Edge(6, 0, 0.58));\ng.addEdge(new jsgraphs.Edge(6, 4, 0.93));\n\ng.node(2).label = 'Hello';\ng.edge(4, 5).label = 'World'; // edge from node 4 to node 5\n\nconsole.log(g.V); // display 13, which is the number of vertices in g\nconsole.log(g.adj(0)); // display the adjacency list which are directed edges from vertex 0\n```\n\n### Depth First Search\n\nThe sample code below show how to perform depth first search of an undirected graph (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-dfs.html)):\n\n```javascript\nvar jsgraphs = require('js-graph-algorithms');\n\nvar g = new jsgraphs.Graph(6);\ng.addEdge(0, 5);\ng.addEdge(2, 4);\ng.addEdge(2, 3);\ng.addEdge(1, 2);\ng.addEdge(0, 1);\ng.addEdge(3, 4);\ng.addEdge(3, 5);\ng.addEdge(0, 2);\nvar s = 0;\nvar dfs = new jsgraphs.DepthFirstSearch(g, s);\n\n\nfor(var v=0; v \u003c g.V; ++v) {\n if(dfs.hasPathTo(v)) {\n    console.log(s + \" is connected to \" + v);\n    console.log(\"path: \" + dfs.pathTo(v));\n } else {\n     console.log('No path from ' + s + ' to ' + v);\n }\n} \n```\n\n### Connected Components\n\nThe sample code below show how to obtain the number of connected components in an undirected graph  (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-connected-components.html)):\n\n```javascript\nvar jsgraphs = require('js-graph-algorithms');\n\nvar g = new jsgraphs.Graph(13);\ng.addEdge(0, 5);\ng.addEdge(4, 3);\ng.addEdge(0, 1);\ng.addEdge(9, 12);\ng.addEdge(6, 4);\ng.addEdge(5, 4);\ng.addEdge(0, 2);\ng.addEdge(11, 12);\ng.addEdge(9,10);\ng.addEdge(0, 6);\ng.addEdge(7, 8);\ng.addEdge(9, 11);\ng.addEdge(5, 3); \n\nvar cc = new jsgraphs.ConnectedComponents(g);\nconsole.log(cc.componentCount()); // display 3\nfor (var v = 0; v \u003c g.V; ++v) {\n    console.log('id[' + v + ']: ' + cc.componentId(v));\n}\n```\n\n### Topological Sort\n\nThe sample code below show how to obtain the reverse post order of a topological sort in a directed acyclic graph (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-topo-sort.html)):\n\n```javascript\nvar jsgraphs = require('js-graph-algorithms');\n\nvar dag = new jsgraphs.DiGraph(7); // must be directed acyclic graph\n\ndag.addEdge(0, 5);\ndag.addEdge(0, 2);\ndag.addEdge(0, 1);\ndag.addEdge(3, 6);\ndag.addEdge(3, 5);\ndag.addEdge(3, 4);\ndag.addEdge(5, 4);\ndag.addEdge(6, 4);\ndag.addEdge(6, 0);\ndag.addEdge(3, 2);\ndag.addEdge(1, 4);\n\nvar ts = new jsgraphs.TopologicalSort(dag);\n\nvar order = ts.order();\nconsole.log(order); // display array which is the topological sort order\n\n```\n\n### Strongly Connected Components for Directed Graph\n\nThe sample code below show how to obtain the strongly connected components from a directed graph (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-strongly-connected-components.html)):\n\n```javascript\nvar jsgraphs = require('js-graph-algorithms');\n\nvar graph = new jsgraphs.DiGraph(13);\ngraph.addEdge(4, 2);\ngraph.addEdge(2, 3);\ngraph.addEdge(3, 2);\ngraph.addEdge(6, 0);\ngraph.addEdge(0, 1);\ngraph.addEdge(2, 0);\ngraph.addEdge(11, 12);\ngraph.addEdge(12, 9);\ngraph.addEdge(9, 10);\ngraph.addEdge(9, 11);\ngraph.addEdge(8, 9);\ngraph.addEdge(10, 12);\ngraph.addEdge(11, 4);\ngraph.addEdge(4, 3);\ngraph.addEdge(3, 5);\ngraph.addEdge(7, 8);\ngraph.addEdge(8, 7);\ngraph.addEdge(5, 4);\ngraph.addEdge(0, 5);\ngraph.addEdge(6, 4);\ngraph.addEdge(6, 9);\ngraph.addEdge(7, 6);\nvar scc = new jsgraphs.StronglyConnectedComponents(graph);\nconsole.log(scc.componentCount()); // display 5\nfor (var v = 0; v \u003c graph.V; ++v) {\n    console.log('id[' + v + ']: ' + scc.componentId(v));\n}\n```\n\n### Use Kruskal algorithm to find the minimum spanning tree of a weighted graph\n\nThe sample code below show how to obtain the minimum spanning tree from a weighted graph using Kruskal algorithm (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-kruskal.html)):\n\n```javascript\nvar jsgraphs = require('js-graph-algorithms');\nvar g = new jsgraphs.WeightedGraph(8);\n\ng.addEdge(new jsgraphs.Edge(0, 7, 0.16));\ng.addEdge(new jsgraphs.Edge(2, 3, 0.17));\ng.addEdge(new jsgraphs.Edge(1, 7, 0.19));\ng.addEdge(new jsgraphs.Edge(0, 2, 0.26));\ng.addEdge(new jsgraphs.Edge(5, 7, 0.28));\ng.addEdge(new jsgraphs.Edge(1, 3, 0.29));\ng.addEdge(new jsgraphs.Edge(1, 5, 0.32));\ng.addEdge(new jsgraphs.Edge(2, 7, 0.34));\ng.addEdge(new jsgraphs.Edge(4, 5, 0.35));\ng.addEdge(new jsgraphs.Edge(1, 2, 0.36));\ng.addEdge(new jsgraphs.Edge(4, 7, 0.37));\ng.addEdge(new jsgraphs.Edge(0, 4, 0.38));\ng.addEdge(new jsgraphs.Edge(6, 2, 0.4));\ng.addEdge(new jsgraphs.Edge(3, 6, 0.52));\ng.addEdge(new jsgraphs.Edge(6, 0, 0.58));\ng.addEdge(new jsgraphs.Edge(6, 4, 0.93));\n\nvar kruskal = new jsgraphs.KruskalMST(g); \nvar mst = kruskal.mst;\nfor(var i=0; i \u003c mst.length; ++i) {\n    var e = mst[i];\n    var v = e.either();\n    var w = e.other(v);\n    console.log('(' + v + ', ' + w + '): ' + e.weight);\n}\n```\n\n### Use Lazy Prim algorithm to find the minimum spanning tree of a weighted graph\n\nThe sample code below show how to obtain the minimum spanning tree from a weighted graph using Lazy Prim algorithm (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-lazy-prim.html)):\n\n```javascript\nvar jsgraphs = require('js-graph-algorithms');\nvar g = new jsgraphs.WeightedGraph(8);\n\ng.addEdge(new jsgraphs.Edge(0, 7, 0.16));\ng.addEdge(new jsgraphs.Edge(2, 3, 0.17));\ng.addEdge(new jsgraphs.Edge(1, 7, 0.19));\ng.addEdge(new jsgraphs.Edge(0, 2, 0.26));\ng.addEdge(new jsgraphs.Edge(5, 7, 0.28));\ng.addEdge(new jsgraphs.Edge(1, 3, 0.29));\ng.addEdge(new jsgraphs.Edge(1, 5, 0.32));\ng.addEdge(new jsgraphs.Edge(2, 7, 0.34));\ng.addEdge(new jsgraphs.Edge(4, 5, 0.35));\ng.addEdge(new jsgraphs.Edge(1, 2, 0.36));\ng.addEdge(new jsgraphs.Edge(4, 7, 0.37));\ng.addEdge(new jsgraphs.Edge(0, 4, 0.38));\ng.addEdge(new jsgraphs.Edge(6, 2, 0.4));\ng.addEdge(new jsgraphs.Edge(3, 6, 0.52));\ng.addEdge(new jsgraphs.Edge(6, 0, 0.58));\ng.addEdge(new jsgraphs.Edge(6, 4, 0.93));\n\nvar prim = new jsgraphs.LazyPrimMST(g); \nvar mst = prim.mst;\nfor(var i=0; i \u003c mst.length; ++i) {\n    var e = mst[i];\n    var v = e.either();\n    var w = e.other(v);\n    console.log('(' + v + ', ' + w + '): ' + e.weight);\n}\n```\n\n### Use Eager Prim algorithm to find the minimum spanning tree of a weighted graph\n\nThe sample code below show how to obtain the minimum spanning tree from a weighted graph using Eager Prim algorithm (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-eager-prim.html)):\n\n```javascript\nvar jsgraphs = require('js-graph-algorithms');\nvar g = new jsgraphs.WeightedGraph(8);\n\ng.addEdge(new jsgraphs.Edge(0, 7, 0.16));\ng.addEdge(new jsgraphs.Edge(2, 3, 0.17));\ng.addEdge(new jsgraphs.Edge(1, 7, 0.19));\ng.addEdge(new jsgraphs.Edge(0, 2, 0.26));\ng.addEdge(new jsgraphs.Edge(5, 7, 0.28));\ng.addEdge(new jsgraphs.Edge(1, 3, 0.29));\ng.addEdge(new jsgraphs.Edge(1, 5, 0.32));\ng.addEdge(new jsgraphs.Edge(2, 7, 0.34));\ng.addEdge(new jsgraphs.Edge(4, 5, 0.35));\ng.addEdge(new jsgraphs.Edge(1, 2, 0.36));\ng.addEdge(new jsgraphs.Edge(4, 7, 0.37));\ng.addEdge(new jsgraphs.Edge(0, 4, 0.38));\ng.addEdge(new jsgraphs.Edge(6, 2, 0.4));\ng.addEdge(new jsgraphs.Edge(3, 6, 0.52));\ng.addEdge(new jsgraphs.Edge(6, 0, 0.58));\ng.addEdge(new jsgraphs.Edge(6, 4, 0.93));\n\nvar prim = new jsgraphs.EagerPrimMST(g); \nvar mst = prim.mst;\nfor(var i=0; i \u003c mst.length; ++i) {\n    var e = mst[i];\n    var v = e.either();\n    var w = e.other(v);\n    console.log('(' + v + ', ' + w + '): ' + e.weight);\n}\n```\n\n### Find the shortest paths using Dijkstra\n\nThe sample code below show how to obtain the shortest paths from a starting point 0 on a weighted directed graph using Dijkstra (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-dijkstra.html)):\n\n```javascript\nvar jsgraphs = require('js-graph-algorithms');\nvar g = new jsgraphs.WeightedDiGraph(8);\ng.addEdge(new jsgraphs.Edge(0, 1, 5.0));\ng.addEdge(new jsgraphs.Edge(0, 4, 9.0));\ng.addEdge(new jsgraphs.Edge(0, 7, 8.0));\ng.addEdge(new jsgraphs.Edge(1, 2, 12.0));\ng.addEdge(new jsgraphs.Edge(1, 3, 15.0));\ng.addEdge(new jsgraphs.Edge(1, 7, 4.0));\ng.addEdge(new jsgraphs.Edge(2, 3, 3.0));\ng.addEdge(new jsgraphs.Edge(2, 6, 11.0));\ng.addEdge(new jsgraphs.Edge(3, 6, 9.0));\ng.addEdge(new jsgraphs.Edge(4, 5, 5.0));\ng.addEdge(new jsgraphs.Edge(4, 6, 20.0));\ng.addEdge(new jsgraphs.Edge(4, 7, 5.0));\ng.addEdge(new jsgraphs.Edge(5, 2, 1.0));\ng.addEdge(new jsgraphs.Edge(5, 6, 13.0));\ng.addEdge(new jsgraphs.Edge(7, 5, 6.0));\ng.addEdge(new jsgraphs.Edge(7, 2, 7.0));  \n\n\nvar dijkstra = new jsgraphs.Dijkstra(g, 0);\n\nfor(var v = 1; v \u003c g.V; ++v){\n    if(dijkstra.hasPathTo(v)){\n        var path = dijkstra.pathTo(v);\n        console.log('=====path from 0 to ' + v + ' start==========');\n        for(var i = 0; i \u003c path.length; ++i) {\n            var e = path[i];\n            console.log(e.from() + ' =\u003e ' + e.to() + ': ' + e.weight);\n        }\n        console.log('=====path from 0 to ' + v + ' end==========');\n        console.log('=====distance: '  + dijkstra.distanceTo(v) + '=========');\n    }\n}\n```\n\n### Find the shortest paths using Bellman-Ford\n\nThe sample code below show how to obtain the shortest paths from a starting point 0 on a weighted directed graph using Bellman-Ford:\n\n```javascript\nvar jsgraphs = require('js-graph-algorithms');\nvar g = new jsgraphs.WeightedDiGraph(8);\ng.addEdge(new jsgraphs.Edge(0, 1, 5.0));\ng.addEdge(new jsgraphs.Edge(0, 4, 9.0));\ng.addEdge(new jsgraphs.Edge(0, 7, 8.0));\ng.addEdge(new jsgraphs.Edge(1, 2, 12.0));\ng.addEdge(new jsgraphs.Edge(1, 3, 15.0));\ng.addEdge(new jsgraphs.Edge(1, 7, 4.0));\ng.addEdge(new jsgraphs.Edge(2, 3, 3.0));\ng.addEdge(new jsgraphs.Edge(2, 6, 11.0));\ng.addEdge(new jsgraphs.Edge(3, 6, 9.0));\ng.addEdge(new jsgraphs.Edge(4, 5, 5.0));\ng.addEdge(new jsgraphs.Edge(4, 6, 20.0));\ng.addEdge(new jsgraphs.Edge(4, 7, 5.0));\ng.addEdge(new jsgraphs.Edge(5, 2, 1.0));\ng.addEdge(new jsgraphs.Edge(5, 6, 13.0));\ng.addEdge(new jsgraphs.Edge(7, 5, 6.0));\ng.addEdge(new jsgraphs.Edge(7, 2, 7.0));  \n\n\nvar bf = new jsgraphs.BellmanFord(g, 0);\n\nfor(var v = 1; v \u003c g.V; ++v){\n    if(bf.hasPathTo(v)){\n        var path = bf.pathTo(v);\n        console.log('=====path from 0 to ' + v + ' start==========');\n        for(var i = 0; i \u003c path.length; ++i) {\n            var e = path[i];\n            console.log(e.from() + ' =\u003e ' + e.to() + ': ' + e.weight);\n        }\n        console.log('=====path from 0 to ' + v + ' end==========');\n        console.log('=====distance: '  + bf.distanceTo(v) + '=========');\n    }\n}\n```\n\n### Find the shortest paths using Topological Sort Shortest Paths\n\nThe sample code below show how to obtain the shortest paths from a starting point 0 on a weighted directed acylic graph using Topological Sort:\n\n```javascript\nvar jsgraphs = require('js-graph-algorithms');\nvar g = new jsgraphs.WeightedDiGraph(8);\ng.addEdge(new jsgraphs.Edge(0, 1, 5.0));\ng.addEdge(new jsgraphs.Edge(0, 4, 9.0));\ng.addEdge(new jsgraphs.Edge(0, 7, 8.0));\ng.addEdge(new jsgraphs.Edge(1, 2, 12.0));\ng.addEdge(new jsgraphs.Edge(1, 3, 15.0));\ng.addEdge(new jsgraphs.Edge(1, 7, 4.0));\ng.addEdge(new jsgraphs.Edge(2, 3, 3.0));\ng.addEdge(new jsgraphs.Edge(2, 6, 11.0));\ng.addEdge(new jsgraphs.Edge(3, 6, 9.0));\ng.addEdge(new jsgraphs.Edge(4, 5, 5.0));\ng.addEdge(new jsgraphs.Edge(4, 6, 20.0));\ng.addEdge(new jsgraphs.Edge(4, 7, 5.0));\ng.addEdge(new jsgraphs.Edge(5, 2, 1.0));\ng.addEdge(new jsgraphs.Edge(5, 6, 13.0));\ng.addEdge(new jsgraphs.Edge(7, 5, 6.0));\ng.addEdge(new jsgraphs.Edge(7, 2, 7.0));  \n\n\nvar bf = new jsgraphs.TopologicalSortShortestPaths(g, 0);\n\nfor(var v = 1; v \u003c g.V; ++v){\n    if(bf.hasPathTo(v)){\n        var path = bf.pathTo(v);\n        console.log('=====path from 0 to ' + v + ' start==========');\n        for(var i = 0; i \u003c path.length; ++i) {\n            var e = path[i];\n            console.log(e.from() + ' =\u003e ' + e.to() + ': ' + e.weight);\n        }\n        console.log('=====path from 0 to ' + v + ' end==========');\n        console.log('=====distance: '  + bf.distanceTo(v) + '=========');\n    }\n}\n```\n\n### Find the MaxFlow-MinCut using Ford-Fulkerson algorithm\n\nThe sample code below show how to obtain the MaxFlow-MinCut of a directed weighted graph using ford-fulkerson algorithm (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-ford-fulkerson.html)):\n\n```javascript\nvar jsgraphs = require('js-graph-algorithms');\nvar g = new jsgraphs.FlowNetwork(8);\ng.addEdge(new jsgraphs.FlowEdge(0, 1, 10));\ng.addEdge(new jsgraphs.FlowEdge(0, 2, 5));\ng.addEdge(new jsgraphs.FlowEdge(0, 3, 15));\ng.addEdge(new jsgraphs.FlowEdge(1, 4, 9));\ng.addEdge(new jsgraphs.FlowEdge(1, 5, 15));\ng.addEdge(new jsgraphs.FlowEdge(1, 2, 4));\ng.addEdge(new jsgraphs.FlowEdge(2, 5, 8));\ng.addEdge(new jsgraphs.FlowEdge(2, 3, 4));\ng.addEdge(new jsgraphs.FlowEdge(3, 6, 16));\ng.addEdge(new jsgraphs.FlowEdge(4, 5, 15));\ng.addEdge(new jsgraphs.FlowEdge(4, 7, 10));\ng.addEdge(new jsgraphs.FlowEdge(5, 7, 10));\ng.addEdge(new jsgraphs.FlowEdge(5, 6, 15));\ng.addEdge(new jsgraphs.FlowEdge(6, 2, 6));\ng.addEdge(new jsgraphs.FlowEdge(6, 7, 10)); \n\ng.node(2).label = 'Hello';\ng.edge(0, 1).label = 'World';\n\nvar source = 0;\nvar target = 7;\nvar ff = new jsgraphs.FordFulkerson(g, source, target);\nconsole.log('max-flow: ' + ff.value);\n\nvar minCut = ff.minCut(g);\n\nfor(var i = 0; i \u003c minCut.length; ++i) {\n    var e = minCut[i];\n    console.log('min-cut: (' + e.from() + \", \" + e.to() + ')');\n}\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchen0040%2Fjs-graph-algorithms","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchen0040%2Fjs-graph-algorithms","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchen0040%2Fjs-graph-algorithms/lists"}