Single Source Shortest Path

Source: Internet
Author: User

1, problem description

Given the weighted graph G = (v,e), the right of each edge is a nonnegative real number. In addition, a vertex in V is given, called the source. Now you want to calculate the shortest -path length from the source to all the other vertices . The length of the road here refers to the sum of the right side of the road. This problem is often referred to as the single source shortest path problem.

2. Dijkstra algorithm

Dijkstra algorithm is a greedy algorithm for Jianhanyuan shortest path problem.
The basic idea is to set up the vertex set S and continue to make greedy choices to augment this set. A vertex belongs to the set S when and only if the shortest path length from the source to the vertex is known. Initially, S contains only the source. Set U is a vertex of G, the path from source to u and only through the vertices of s in the middle is called a special path from source to U , and the shortest special path length corresponding to each vertex is recorded with array Dist. The Dijkstra algorithm removes the vertex u with the shortest special path length from the V-s each time, adds u to S, and makes the necessary modifications to the array dist. Once s contains all the vertices in V, Dist records the shortest path length from the source to all other vertices.

The Dijkstra algorithm can be described as follows, where the input weighted graph is g= (v,e), v={1,2,..., n}, and vertex V is the source. C is a two-dimensional array, C[i][j] represents the right side (I,J). When (I,J) does not belong to E, C[i][j] is a large number. Dist[i] represents the shortest special path length currently from source to vertex i. When making greedy choices in the Dijkstra algorithm, it is actually considered that when S is added u, there may be a new special way to the vertex, if this new special road is first through the old s to reach the vertex u, and then from U through an edge directly to vertex I, then the shortest length of this road is dist[ U]+c[u][i]. If dist[u]+c[u][i]<dist[i], you need to update the value of Dist[i] . The steps are as follows:

(1) using the weighted adjacency matrix C to represent the weighted graph, c[i][j] represents the weighted value on the arc <vi,vj>. Sets S to the set of endpoints for the known shortest path, with an empty initial state. The initial value of the current shortest path length from the source point v through S to the remaining point VI on the graph is: dist[i]=c[v][i], vi belongs to v.
(2) Choose Vu, make dist[u]=min{dist[i] | VI belongs to V-S},VJ is the shortest shortest path end point. Make s=s u {u}.

(3) Modify the current shortest path length from V to set v-s one vertex vi: If dist[u]+c[u][j]< dist[j], modify dist[j]= dist[u]+c[u][j].
(4) Repeat operation (2), (3) total n-1 times.

The algorithm is implemented as follows

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace Std;
#define N 5
#define M 10000

Template<class type>
void Dijkstra (int n,int v,type dist[],int prev[],type c[][n+1])
{
BOOL S[n+1];
for (int i1=1; i1<=n; i1++)
{
DIST[I1] = C[v][i1];
S[I1] = false;

if (dist[i1] = = M)
{
PREV[I1] = 0;
}
Else
{
PREV[I1] = v;
}
}

DIST[V] = 0;
S[V] = true;

for (int i=1; i<n; i++)
{
int temp = M;
int u = v;


for (int k=1; k<=n; k++)
{
if ((!s[k]) && (dist[k]<temp))
{
U = k;
temp = Dist[k];
}
}
S[u] = true;


for (int j=1; j<=n; j + +)
{
if ((!s[j]) && (c[u][j]<m))
{
Type newdist = Dist[u] + c[u][j];
if (Newdist < dist[j])
{
DIST[J] = newdist;
PREV[J] = u;
}
}
}
}
}


void Traceback (int v,int i,int prev[])
{
if (v = = i)
{
cout<<i;
Return
}
Traceback (V,prev[i],prev);
cout<< "<<i;"
}

int main ()
{
int v = 1;
int dist[n+1],prev[n+1],c[n+1][n+1];

cout<< "The matrix of the weighted graph is:" <<endl;
for (int i=1; i<=n; i++)
{
for (int j=1; j<=n; j + +)
{
cin>>c[i][j];
}
cout<<endl;
}

Dijkstra (N,V,DIST,PREV,C);

for (int j=2; j<=n; j + +)
{
cout<< the shortest path length of "source point 1 to" <<j<< "is:" <<dist[j]<< ", its path is";
Traceback (1,j,prev);
cout<<endl;
}

return 0;
}

Single Source 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.