-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBellmanFordShortestPath.cs
More file actions
40 lines (37 loc) · 1.31 KB
/
Copy pathBellmanFordShortestPath.cs
File metadata and controls
40 lines (37 loc) · 1.31 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DA.Algorithms.TreeAlgorithms
{
internal static class BellmanFordShortestPath
{
public static void FindShortestPath (Graphs.Graph graph, int source)
{
int count = graph.Count;
int [] distance = new int [count];
int [] path = new int [count];
for (int i = 0; i < count; i++)
distance [i] = int.MaxValue;
distance [source] = 0;
for (int i = 0; i < count - 1; i++)
{
for (int j = 0; j < count; j++)
{
Graphs.Graph.Node currentNode = graph.GetNode (j);
while (currentNode != null)
{
int newDistance = distance [j] + currentNode.cost;
if (distance[currentNode.destination] > newDistance)
{
distance [currentNode.destination] = newDistance;
path [currentNode.destination] = j;
}
currentNode = currentNode.next;
}
}
}
}
}
}