Shortest path JavaScript for graphs

Source: Internet
Author: User

The shortest path of a node to another node in the graph can be applied to many realistic problems. When you perform a breadth-first search, the shortest path from one vertex to another connected vertex is automatically found. For example, finding the shortest path from a to D first looks for a single-sided path from a to D, then finds the path of two edges, and so on.

So on the basis of the original, we need an array to hold all the edges from one vertex to the next. Name is Edgeto. The new breadth-first search code is as follows:

function BFs (s) {//breadth-First search for (Var i=0;i<this.vertices;i++) {//due to a depth first search, the identity bit is reset this.marked[i]=false;} var Queue=[];this.marked[s]=true;queue.push (s), while (queue.length>0) {var v=queue.shift (), if (v!=undefined) { document.write ("Access node:" +v+ ' <br> ');} for (Var i=0;i<this.adj[v].length;i++) {var w=this.adj[v];//find the adjacent sub-list for the selected node for (Var j=0;j<w.length;j++) {if (! This.marked[w[j]] {this.edgeto[w[j]]=v;//the corresponding node into the edge array this.marked[w[j]]=true;//in turn accesses its adjacent sub-list Queue.push (W[j]);// Push child list to queue}}}}

Next we need a function to show the path to the different vertices in the diagram. The Pathto function is used to store all vertices that have a common edge on the specified vertex. The code is as follows:

function Pathto (v) {//Find shortest path var source=0;var path=[];for (var i=v;i!=source;i=this.edgeto[i]) {//Find Path.push in adjacent edge array (i );} Path.push (source);//Add the starting node to the shortest path array return path; function Hashpathto (v) {return this.marked[v];} The experiment code is as follows://Experiment G=new Graph (5); G.addedge (0,1); G.addedge (0,2); G.addedge (1,3); G.addedge (2,4); G.bfs (0); var vertex=4;var Paths=g.pathto (vertex); while (paths.length>0) {//path loop to find if (paths.length>1) {document.write (Paths.pop () + "-") ;} Else{document.write (Paths.pop ());}} Access node: 0//Access node: 1//Access node: 2//Access node: 3//Access node: 4//0-2-4

The complete code for the representation and shortest path of the graph is as follows:

<! DOCTYPE html>




Shortest path JavaScript for graphs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.