Shortest Path in graph theory ------- Dijkstra Algorithm

Source: Internet
Author: User

Dijkstra algorithm (the edge weight of a single-source shortest path is non-negative), which is defined as a fixed vertex as the source point, to find the shortest path from the source point to other vertices.

I:

Set S indicates the vertex that has been added to the shortest path, and set T indicates the vertex that has not been added to the shortest path.

 

II:

In order to find the shortest short path from the Source Vertex v0 to each vertex vi, three arrays need to be set, namely, S [n], path [n], dist [n];

 

1. S [n]: S [I] = 0 indicates that vi is added to the set S, and S [I] = 1 indicates that the vertex has been added to the set S. Initially, S [v0] = 1, and the rest are 0.

2. path [n]: path [I] indicates the sequence number of the first vertex of vi in the path v0 --> vi.

3. dist [n]: dist [I] indicates the length of the currently found v0 --> vi Shortest Path. In the initial stage, dist [I] = Edge [v0] [I]. edge [n] [n] indicates the adjacent matrix of the graph.

 

III:

This algorithm requires three steps:

 

1: Find S [I] In diat [n]! = 1, and dist [I] is the smallest vertex u.

2: Modify S [u] to 1, indicating that u is added to the set S.

3: Modify the dist and path values of each vk in T. When u -- vk has an Edge and Edge [u] [k] <INF (INF is infinite) when dist [u] + Edge [u] [k] <dist [k], dist [k] = dist [u] + Edge [u] [k], path [k] = u.

 

 

IV:

Main Code:

 

A directed graph is used as an example:

# Include <stdio. h>
# Include <string. h>
# Define MAXN 20
# Define INF 1000000
Int n;
Int Edge [MAXN] [MAXN];
Int S [MAXN];
Int dist [MAXN];
Int path [MAXN];
Void Dijkstra (int v0)
{
Int I, j, k;
For (I = 0; I <n; I ++) // Key Step 1 for initializing s, dist, and path
{
Dist [I] = Edge [v0] [I];
S [I] = 0;
If (I! = V0 & dist [I] <INF)
Path [I] = v0;
Else
Path [I] =-1;
}
S [v0] = 1; dist [v0] = 0; // at first, only the Source Vertex is in the S collection.
For (I = 0; I <n-1; I ++) // find the smallest path vertex u. Key Step 2
{
Int min = INF, u = v0;
For (j = 0; j <n; j ++)
{
If (! S [j] & dist [j] <min)
{
U = j;
Min = dist [j];
}
}
S [u] = 1; // indicates the vertex of the smallest path found. Pay special attention to it.
For (k = 0; k <n; k ++) // Key Step 3: Modify the dist and path values of vertices without the minimum path
{
If (! S [k] & Edge [u] [k] <INF & dist [u] + Edge [u] [k] <dist [k])
{
Dist [k] = dist [u] + Edge [u] [k];
Path [k] = u;
}
}
}
}
Int main ()
{
Int I, j;
Int u, v, w;
Scanf ("% d", & n );
While (1)
{
Scanf ("% d", & u, & v, & w );
If (u =-1 & v =-1 & w =-1)
Break;
Edge [u] [v] = w;
}
For (I = 0; I <n; I ++)
{
For (j = 0; j <n; j ++)
{
If (I = j)
Edge [I] [j] = 0;
Else if (Edge [I] [j] = 0)
Edge [I] [j] = INF;
}
}
Dijkstra (0 );
Int shortest [MAXN]; // to record the path
For (I = 1; I <n; I ++)
{
Printf ("% d \ t", dist [I]);
Memset (shortest, 0, sizeof (shortest ));
Int k = 0;
Shortest [k] = I;
While (path [shortest [k]! = 0)
{
K ++;
Shortest [k] = path [shortest [k-1];
}
K ++;
Shortest [k] = 0;
For (j = k; j> 0; j --)
Printf ("% d->", shortest [j]);
Printf ("% d \ n", shortest [0]);
}
Return 0;
}


Author: No_Retreats

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.