-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDoesPathExistInDirectedGraphBFS.cs
191 lines (163 loc) · 5.47 KB
/
DoesPathExistInDirectedGraphBFS.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
189
190
191
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Linq;
// Does route exist between two nodes in a directed graph?
// Implemented using breadth first search.
//
// Compiled: Visual Studio 2013
namespace DoesPathExistInDirectedGraphBFS
{
public class Edge<TKey, TVal>
{
public Node<TKey, TVal> To { get; private set; }
public Node<TKey, TVal> From { get; private set; }
public Edge(Node<TKey, TVal> to, Node<TKey, TVal> from)
{
this.To = to;
this.From = from;
}
}
public class Node<TKey, TVal>
{
public TKey Key { get; private set; }
public TVal Value { get; private set; }
private IList<Edge<TKey, TVal>> edges;
public IEnumerable<Edge<TKey, TVal>> Edges
{
get { return edges; }
}
public Node(TKey key, TVal value)
{
this.Key = key;
this.Value = value;
this.edges = new List<Edge<TKey, TVal>>();
}
public void AddConnection(Node<TKey, TVal> to)
{
this.edges.Add(new Edge<TKey,TVal>(to, this));
}
}
public class Graph<TKey, TValue> where TKey : IEquatable<TKey>
{
// Max number of nodes to visit while searching
private const int MAX_SEARCH = 1000;
private IList<Node<TKey, TValue>> nodes;
public IEnumerable<Node<TKey, TValue>> Nodes
{
get { return nodes; }
}
public Graph()
{
this.nodes = new List<Node<TKey, TValue>>();
}
public void AddNode(TKey key, TValue value)
{
AddNode(new Node<TKey, TValue>(key, value));
}
public void AddNode(Node<TKey, TValue> nodeToAdd)
{
this.nodes.Add(nodeToAdd);
}
public Node<TKey, TValue> Search(TKey key)
{
// TODO: O(n) linear search - could be improved.
foreach (var node in nodes)
if (node.Key.Equals(key))
return node;
// Node wasn't found
return null;
}
public void AddConnection(TKey to, TKey from)
{
var toNode = Search(to);
var fromNode = Search(from);
if (toNode == null || fromNode == null)
throw new Exception("Cannot add connection to non-existent node(s).");
fromNode.AddConnection(toNode);
}
public bool DoesPathExist(TKey to, TKey from)
{
var toNode = Search(to);
var fromNode = Search(from);
if (toNode == null || fromNode == null)
return false;
return DoesPathExist(toNode, fromNode);
}
/// <summary>
/// Performs a breadth first search to find target node.
/// Time Complexity: O(Nodes ^ Max Depth)
/// </summary>
public bool DoesPathExist(Node<TKey, TValue> to, Node<TKey, TValue> from)
{
var visited = new HashSet<TKey>();
var nodesToVisit = new Queue<Node<TKey, TValue>>();
nodesToVisit.Enqueue(from);
while (nodesToVisit.Any() & visited.Count < MAX_SEARCH)
{
var current = nodesToVisit.Dequeue();
visited.Add(current.Key);
// Check if current node is target
if (current.Key.Equals(to.Key))
return true;
// Enqueue current node's unvisited neighbors
foreach (var edge in current.Edges)
{
var neighbor = edge.To;
if (!visited.Contains(neighbor.Key))
nodesToVisit.Enqueue(neighbor);
}
}
return false; // Didn't find a matching node.
}
}
[TestClass]
public class GraphTests
{
[TestMethod]
public void DoesPathExist_WhenBothNodesDoNotExist_ExpectFalse()
{
var sut = new Graph<int,int>();
Assert.IsFalse(sut.DoesPathExist(1, 2));
}
[TestMethod]
public void DoesPathExist_WhenSingleNodesDoNotExist_ExpectFalse()
{
var sut = new Graph<int,string>();
sut.AddNode(1, "A");
sut.AddNode(2, "B");
Assert.IsFalse(sut.DoesPathExist(1, 3));
}
[TestMethod]
public void DoesPathExist_WhenPathToItself_ExpectTrue()
{
var sut = new Graph<int,string>();
sut.AddNode(1, "A");
Assert.IsTrue(sut.DoesPathExist(1, 1));
}
[TestMethod]
public void DoesPathExist_WhenNoConnection_ExpectFalse()
{
var sut = MakeFourNodeGraphWithTwoConnections();
Assert.IsFalse(sut.DoesPathExist(4, 1));
}
[TestMethod]
public void DoesPathExist_WhenConnection_ExpectTrue()
{
var sut = MakeFourNodeGraphWithTwoConnections();
sut.AddConnection(4, 3);
Assert.IsTrue(sut.DoesPathExist(4, 1));
}
private static Graph<int, string> MakeFourNodeGraphWithTwoConnections()
{
var graph = new Graph<int, string>();
graph.AddNode(1, "A");
graph.AddNode(2, "B");
graph.AddNode(3, "C");
graph.AddNode(4, "D");
graph.AddConnection(2, 1);
graph.AddConnection(3, 2);
return graph;
}
}
}