-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortestPath.cs
More file actions
42 lines (38 loc) · 986 Bytes
/
Copy pathShortestPath.cs
File metadata and controls
42 lines (38 loc) · 986 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Collections.Generic;
using DA.Graphs;
namespace DA.Algorithms.TreeAlgorithms
{
static class ShortestPath
{
public static void FindShortestPath (Graph graph, int source)
{
int currentIndex;
int count = graph.Count;
int[] distance = new int[count];
int[] path = new int[count];
Queue<int> queue = new Queue<int> ();
for (int i = 0; i < count; i++)
{
distance[i] = -1;
}
queue.Enqueue (source);
distance[source] = 0;
while (queue.Count > 0)
{
currentIndex = queue.Dequeue ();
Graph.Node currentNode = graph.GetNode (currentIndex);
while (currentNode != null)
{
if (distance[currentNode.destination] == -1)
{
distance[currentNode.destination] = distance[currentIndex] + 1;
path[currentNode.destination] = currentIndex;
queue.Enqueue (currentNode.destination);
}
currentNode = currentNode.next;
}
}
}
}
}