Dijkstra (Dijkstra) algorithm to find the shortest path

Source: Internet
Author: User

I'm using an adjacency matrix to store the graph.

The code is as follows:

voidGraph:: Dijkstra () {structdijnode{intindex; intdistance; Vector<string>path; BOOLfounded;//determine if you have found ...    }; Dijnode*shorestpaths=NewDijnode[mgraph.vexnum]; stringStartPoint; intStartIndex; cout<<"Please enter a single source point:"<<Endl; CIN>>StartPoint;    Locate (Startpoint,startindex); //initializing an arrayshorestpaths[0]={startindex,0,vector<string>{startpoint},true};  for(intI=1; i<mgraph.vexnum;i++) {Shorestpaths[i]=dijnode{i,none,vector<string>{""},false}; }    //Start Running     for(intI=1; i<mgraph.vexnum;i++) {cout<<startIndex<<Endl; //If this point has not determined the shortest path and there is a path from the current point and is shorter than the current, then it is good to consider         for(intI=0; i<mgraph.vexnum;i++){            if(I!=startindex&&mgraph.arcs[startindex][i].adj+shorestpaths[startindex].distance<shorestpaths[i]. Distance&&shorestpaths[i]. founded==false) {Shorestpaths[i].path=Shorestpaths[startindex].path;                Shorestpaths[i].path.push_back (Mgraph.vexs[i]); Shorestpaths[i].distance=shorestpaths[startindex].distance+Mgraph.arcs[startindex][i].adj; }        }        //Select a fixed point that currently has the shortest distance and does not find the shortest path as StartPoint        intmin=-1;  for(intI=0; i<mgraph.vexnum;i++) {//Select the first min value that meets the requirements            if(Shorestpaths[i]. founded==false) {MIN=i;  Break; }        }         for(intI=0; i<mgraph.vexnum;i++){            if(Shorestpaths[i]. founded==false&&shorestPaths[i].distance<shorestpaths[min].distance) {MIN=i; }} Shorestpaths[min]. Founded=true; StartIndex=MIN; }    //result Output     for(intI=1; i<mgraph.vexnum;i++) {cout<<"from single source point"<<mgraph.vexs[shorestpaths[0].index]<<"to the"<<mgraph.vexs[shorestPaths[i].index]<<"the shortest path is:";  for(intj=0; J<shorestpaths[i].path.size ()-1; j + +) {cout<<shorestPaths[i].path[j]<<" -"; } cout<<shorestpaths[i].path[shorestpaths[i].path.size ()-1]<<"path Length:"<<shorestPaths[i].distance<<Endl; }}

Dijkstra (Dijkstra) algorithm to find the shortest path

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.