Skip to content

Commit bcb2a33

Browse files
committed
[Refactor] Graph now uses dict instead of defaultdict and supports all types as vertex names.
1 parent cc30d03 commit bcb2a33

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

structures/graph.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from collections import defaultdict
1+
from collections import defaultdict, deque
22

33
class GraphNode():
44

@@ -18,7 +18,7 @@ class Graph():
1818
"""
1919

2020
def __init__(self, verticies):
21-
self.graph = defaultdict(list)
21+
self.graph = {}
2222
self.verticies = verticies
2323

2424
def add_edge(self, source, destination):
@@ -33,7 +33,16 @@ def add_edge(self, source, destination):
3333
destination: the destination vertex
3434
3535
"""
36-
self.graph[source].append(destination)
36+
if len(self.graph) > self.verticies:
37+
raise IndexError("Too many verticies in graph.")
38+
39+
if source in self.graph:
40+
self.graph[source].append(destination)
41+
else:
42+
self.graph[source] = [destination]
43+
44+
if destination not in self.graph:
45+
self.graph[destination] = []
3746

3847
def has_cycle(self):
3948
"""
@@ -96,21 +105,21 @@ def topological_sort(self):
96105
97106
"""
98107
visited = set()
99-
stack = []
108+
stack = deque()
100109

101110
def dfs(vertex):
102111
visited.add(vertex)
103-
for i in self.graph[vertex]:
104-
if i not in visited:
105-
dfs(i)
112+
for j in self.graph[vertex]:
113+
if j not in visited:
114+
dfs(j)
106115

107-
stack.append(vertex)
108-
109-
for i in range(self.verticies):
110-
if i not in visited:
111-
dfs(i)
116+
stack.appendleft(vertex)
117+
118+
for key, _ in self.graph.items():
119+
if key not in visited:
120+
dfs(key)
112121

113-
return stack[::-1]
122+
return list(stack)
114123

115124
class WeightedGraphNode():
116125

tests/test_graph_cycles.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33

44
class TestCycle(unittest.TestCase):
55

6+
def test_non_int(self):
7+
8+
graph = Graph(4)
9+
10+
graph.add_edge('Tampa','Colorado')
11+
graph.add_edge('Colorado','Las Vegas')
12+
graph.add_edge('Las Vegas', 'Seattle')
13+
graph.add_edge('Seattle','Anchorage')
14+
15+
self.assertEqual(['Tampa', 'Colorado', 'Las Vegas', 'Seattle', 'Anchorage'], graph.topological_sort())
16+
617
def test_cycles_true(self):
718

819
graph = Graph(4)
@@ -59,4 +70,12 @@ def test_cycles_false(self):
5970
graph.add_edge(1,2)
6071
graph.add_edge(2,3)
6172

62-
self.assertEqual(False, graph.has_cycle())
73+
self.assertEqual(False, graph.has_cycle())
74+
75+
def test_too_many_verticies_error(self):
76+
77+
graph = Graph(1)
78+
79+
graph.add_edge(0,1)
80+
with self.assertRaises(IndexError):
81+
graph.add_edge(1,2)

tests/test_topological_sort.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_topological_sort(self):
1515

1616
graph.add_edge(3,1)
1717

18-
self.assertEqual([5, 4, 2, 3, 1, 0], graph.topological_sort())
18+
self.assertEqual([4, 5, 0, 2, 3, 1], graph.topological_sort())
1919

2020

2121

0 commit comments

Comments
 (0)