Implementation of Shortest Path algorithm

Source: Internet
Author: User

Shortest Path algorithm

1. Dijkstra algorithm

The goal is to solve the shortest path from the fixed starting point to the remaining points

The steps are as follows:

    1. Preparation: Build two-bit matrix edge,edge[i][j] to store the weights of i->j, if I==j is edge[i][j]=0, if I and J are not direct, then Edge[i][j]=max_int
    2. Build array dis[], where dis[i] means actually point start->i between the weights, constantly updated, get the smallest weights
    3. Select the closest direct point to the start, (note that the non-direct point will pass through the middle of the jump point, the indirect arrival, the first consideration must be to start the nearest point to jump)
    4. Judge Dis[i] and dis[The nearest point index]+edge[from start Index][i] the size of the nearest point, update dis[i]
    5. Repeat 3-4 (note mark to prevent repeated calculations)
#include <iostream>using namespacestd;Const intMax_int = ~ (1<< to);Const intMin_int = (1<< to);intMain () {intN,m,s;//N is the number of nodes, M is the number of edges and S is the start node.    intT1,T2,T3;//T1 is the start node, T2 are the end node and T3 is the weight between T1 and T2cout<<"Please input the number of node (n), edges (m) and start node (s):"<<Endl; CIN>>n>>m>>s; intedge[n+1][n+1];//Store the edges for the N nodes    intdis[n+1], is_visited[n+1];//Dis[k] Store the min distance between S and k,\Is_visited Store the status of the node (whether it isvisited or not)//Init the edge[][] with the Max_int     for(intI=1; i<=n; i++){         for(intj =1; J <= N; J + +)            if(i==j) Edge[i][j] =0; ElseEDGE[I][J] =Max_int; }    //Input the Edge datacout<<"Please input the Edge data:t1 (Start node), T2 (end node), t3 (weight)"<<Endl;  for(intI=1; i<=m; i++) {cin>>t1>>t2>>T3; EDGE[T1][T2]=T3; }    /** init the is_visited[] with 0 * init the dis[]*/     for(intI=1; i<=n; i++) {Is_visited[i]=0; Dis[i]=Edge[s][i]; } Is_visited[s]=1; //The Dijkstra algorithm     for(intI=1; i<=n; i++){        intU//Store The Min value index in dis[] which are not visited        intmin = Max_int;//Store the Min value in dis[] which are not visited         for(intj=1; j<=n; J + +){            if(is_visited[j]==0&& dis[j]<min) {min=Dis[j]; U=J; }} Is_visited[u]=1;  for(intk=1; k<=n; k++){            //The first layer of judgment prevents dis[u]+edge[u][k] from crossing            if(Edge[u][k] <max_int) {                if(is_visited[k]==0&& dis[k]>dis[u]+Edge[u][k]) {Dis[k]= Dis[u] +Edge[u][k]; cout<<u<<" "<<k<<" "<<dis[k]<<Endl; }            }        }    }    //Print The result     for(intI=1; i<=n; i++) {cout<<"The min weight between"<<s<<" and"<<i<<"is :"<<dis[i]<<Endl; }}

2. Floyd algorithm

The goal is to solve any two-point shortest path, the core idea is to pass any number of nodes to relay, check whether the path is the shortest

 for (k=1; k<=n;k++)//        transfer via K  for (i=1; i<=n;i++)              for (j=1; j<=n;j++)                 if (e[i][j]>e[i][k]+e[k][j])                     e[i][j]=e[i][k]+e[k][j];

Implementation of Shortest Path algorithm

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.