-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.cs
188 lines (156 loc) · 5.1 KB
/
Graph.cs
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Djikstra
{
public class Graph
{
public List<Node> Nodes { get; set; }
public List<Edge> Edges { get; set; }
public Node FindNode(string Name)
{
foreach (Node n in Nodes)
{
if (n.Name == Name)
{
return n;
}
}
return null;
}
public Node FindNode(Node node)
{
return FindNode(node.Name);
}
public List<Node> GetNodesExceptGivenOne(Node n)
{
if (Nodes == null)
{
throw new Exception("Error occured since Nodes are null");
}
List<Node> result = new List<Node>();
foreach (Node node in Nodes)
{
if (node != n)
{
result.Add(node);
}
}
return result;
}
public void IntializeEdges()
{
if (Nodes == null)
{
throw new Exception("Nodes cannot be null when edges have to be initialized");
}
foreach (Node n in Nodes)
{
foreach (Node neighbor in n.Neighbors)
{
Edge edge = new Edge(n, neighbor);
Edges.Add(edge);
n.Edges.Add(edge);
neighbor.Edges.Add(edge);
}
}
}
// Currently prints traversed nodes to console
public void PerformBreadthFirstTraversal()
{
Queue<Node> queueNodes = new Queue<Node>();
queueNodes.Enqueue(Nodes[0]);
Nodes[0].IsVisited = true;
while (queueNodes.Count > 0)
{
Node currentNode = queueNodes.Dequeue();
System.Console.WriteLine(currentNode.Name);
foreach (Node n in currentNode.Neighbors)
{
if (n.IsVisited == false)
{
queueNodes.Enqueue(n);
n.IsVisited = true;
}
}
}
}
void UpdateGraphWithDistancesFromNode(Node source)
{
PriorityQueue<Node> unVisitedNodes = new PriorityQueue<Node>(Nodes); // Time to create a min heap - O(n)
// Does this update the value of 'source' in Nodes ?
source.Distance = 0;
while (!unVisitedNodes.Empty()) // O(n)
{
Node current = unVisitedNodes.Peek();
if (current.Distance == Constants.INFINITY)
{
break;
}
foreach (Node neighbor in current.Neighbors) // O(nm)
{
if (unVisitedNodes.Contains(neighbor))
{
int tentative = 0;
Edge edge = FindEdge(current, neighbor); // O(nml)
tentative = current.Distance + edge.Cost;
if (tentative < neighbor.Distance)
{
neighbor.Distance = tentative;
neighbor.Previous = current;
}
}
}
unVisitedNodes.Dequeue();
}
}
List<Node> FindShortestPath(Node destination)
{
if (destination == null)
{
throw new Exception("Error occured: Destination is null");
}
Node current = destination.Previous;
Stack<Node> resultReverse = new Stack<Node>();
resultReverse.Push(destination);
while (current != null)
{
resultReverse.Push(current);
current = current.Previous;
}
List<Node> result = new List<Node>();
while (resultReverse.Count > 0)
{
result.Add(resultReverse.Pop());
}
return result;
}
// Time complexity ?
public List<Node> FindShortestPath(Node source, Node destination)
{
if (FindNode(source) == null)
{
throw new Exception("Source node is not present in the graph");
}
if (FindNode(destination) == null)
{
throw new Exception("destination node is not present in graph");
}
UpdateGraphWithDistancesFromNode(source);
return FindShortestPath(destination);
}
public Edge FindEdge(Node startNode, Node endNode)
{
foreach (Edge edge in Edges)
{
if ((edge.StartNode.Name == startNode.Name) && (edge.EndNode.Name == endNode.Name))
{
return edge;
}
}
return null;
}
}
}