Dijkstra Shortest Path algorithm (C # implementation)

Source: Internet
Author: User

Learning the shortest path algorithm, want to use C # language implementation, calculate their own summary improve, also for beginners to provide help gossip not to say, first state the idea of Dijkstra algorithm

1, sets a set of S, used to hold the shortest path of the vertex, a set of U, used to hold the vertex without determining the shortest path, a set of distance, representing the starting point to the point at the moment of the shortest distance,

Sets the pre, which represents the previous reference point when the point is taken to the current path, the initial reference point subscript is 0, and the set isfor indicates whether the shortest path is determined.

2. First add the subscript 0 of the starting V0 to the set S, then mark the Isfor[0]=true, and mark the subscript of the starting point as the subscript 123456 of the v1,v2,v3,v4,v5,v6 into U, traverse the set U, The distance from 0 to each vertex is added to the distance (without a directly connected edge, which is represented by the large number of 2048);

3. Traverse the distance, find the minimum distance, add the subscript of the vertex to S, and set the value of the subscript corresponding to Isfor to true, and find the distance between the subscript as the starting point to each edge, if the new starting point is to the rest of the distance from the vertex of the U set and the distance from the previous start to the new start < Distance the distance from the data, set the distance value of the position to the sum of the distance of the rest of the vertices in the U set and the distance from the previous starting point to the new starting point, and then change the reference subscript for the corresponding pre of the vertex to the subscript for the previous starting point; then repeat 3;

Attached code:

Using System;
Using System.Collections;
Using System.Text;
Namespace Dijkstramethod
{
Class Program
{
V1 to V7 adjacency matrix
Static int[,] Metro = new int[7,7] {
{0, 3, 7, 5,2048,2048,2048},
{3, 0, 2,2048, 6,2048,2048},
{7, 2, 0, 3, 3,2048,2048},
{5,2048, 3, 0,2048, 2, 8},
{2048, 6, 3,2048, 0,2048, 2},
{2048,2048,2048, 2,2048, 0, 2},
{2048,2048,2048, 8, 2, 2, 0}};
static int row = 7;
ArrayList S = new ArrayList (row),//s stores the subscript of the vertex that determines the shortest path
ArrayList U = new ArrayList (row), subscript for a vertex in//u where the path is not determined
int[] Distance = new int[7];//to hold data per query
int[] prev = new int[7];//to store the subscript of the previous nearest vertex
bool[] isfor = new Bool[7] {false, False, False, False, False, false};
<summary>
The implementation part of the Dijkstra algorithm
</summary>
<param name= "Start" ></param>
void Findway (int Start)
{
S.add (Start);
Isfor[start] = true;
for (int i=0;i<row;i++)
{
if (I!=start)
U.add (i);
}
for (int i=0;i<row;i++) {
Distance[i] = Metro[start, I];
Prev[i] = 0;
}
int Count = U.count;
while (count>0)
{
int min_index = (int) u[0];//assumes that the first number in U stores the subscript of the smallest number
foreach (int r in U)
{
if (Distance[r] < distance[min_index]&&! ISFOR[R])
Min_index = R;
}
S.add (min_index);//s Join this nearest point
Isfor[min_index]=true;
U.remove (min_index); Remove this point from the//u;
foreach (int r in U)
{
Find the next row of adjacency matrices, how distance from the previous starting point is smaller than the array storage, change the new distance and starting point, and then the distance from the new start to the edge
if (Distance[r] > Distance[min_index] + metro[min_index, R])
{
DISTANCE[R] = Distance[min_index] + metro[min_index, R];
Prev[r] = Min_index;
}
Else
{
DISTANCE[R] = Distance[r];
}
}
Count = U.count;
}
}
<summary>
Show the generated data.
</summary>
void display ()
{
for (int i=0;i<row;i++)
{
Console.Write ("V1 to v{0} The shortest path is →v1", i);
int prepoint = Prev[i];
string s = "";
StringBuilder sb = new StringBuilder (10);
while (prepoint> 0)
{
s = (prepoint + 1) +s;
Prepoint = Prev[prepoint];
}
for (int j = 0; J < s.length;j++)
{
Sb. Append ("-V"). Append (S[j]);
}
Console.Write (sb.) ToString ());
Console.Write ("-v{0}", i);
Console.WriteLine (": {0}", Distance[i]);

}
}
static void Main (string[] args)
{
Program P = new program ();
P.findway (0);
P.display ();
Console.readkey ();
}
}
}

Dijkstra Shortest Path algorithm (C # implementation)

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.