Skip to content

Commit dd5b95a

Browse files
committed
Topological sorting
1 parent 3ad08d3 commit dd5b95a

File tree

5 files changed

+81
-11
lines changed

5 files changed

+81
-11
lines changed

Ch07. Graphs and Graph Algorithms/topological-sorting/graph.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ def __init__(self):
1111
self.no_incoming_edges: dict = {} # to avoid looking through whole list
1212
self.explored: list = []
1313

14-
def addVertex(self, label: str):
14+
def add_vertex(self, label: str):
1515
self.vertices.append(label)
1616
self.adjacencyList[label]: list = []
1717
self.colors[label] = "white"
1818
self.distance[label] = 0
1919
self.previous[label] = None
2020
self.no_incoming_edges[label] = label
2121

22-
def addEdge(self, label1: str, label2: str):
22+
def add_edge(self, label1: str, label2: str):
2323
self.adjacencyList[label1].append(label2)
2424
if label2 in self.no_incoming_edges:
25-
del self.no_incoming_edges[label2] # remove vertex, it has incoming edge
25+
del self.no_incoming_edges[label2] # remove vertex, it has incoming edge
2626

2727
def topsort(self):
2828
# perform depth first search on vertices with no incoming edges
@@ -45,9 +45,9 @@ def dfs(self, start: str):
4545
self.colors[start] = "black"
4646
self.explored.append(start)
4747

48-
def showPath(self, label: str)->str:
48+
def show_path(self, label: str) -> str:
4949
if self.previous[label] is None:
5050
return label
5151
else:
52-
return self.showPath(self.previous[label]) + " -> " + label
52+
return self.show_path(self.previous[label]) + " -> " + label
5353

Ch07. Graphs and Graph Algorithms/topological-sorting/main.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
vertices = ["a", "b", "c", "d", "e", "f"]
66
for vertex in vertices:
7-
graph.addVertex(vertex)
7+
graph.add_vertex(vertex)
88

99

10-
graph.addEdge("a", "c")
11-
graph.addEdge("c", "d")
12-
graph.addEdge("c", "f")
13-
graph.addEdge("b", "c")
14-
graph.addEdge("b", "e")
10+
graph.add_edge("a", "c")
11+
graph.add_edge("c", "d")
12+
graph.add_edge("c", "f")
13+
graph.add_edge("b", "c")
14+
graph.add_edge("b", "e")
1515

1616
graph.topsort()
1717

graphs/topological-sorting/graph.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class Graph:
2+
def __init__(self):
3+
self.vertices: list = []
4+
self.adjacencyList: dict = {}
5+
self.colors: dict = {}
6+
self.previous: dict = {}
7+
self.distance: dict = {}
8+
self.time: int = 0
9+
self.entry: dict = {}
10+
self.exit: dict = {}
11+
self.no_incoming_edges: dict = {} # to avoid looking through whole list
12+
self.explored: list = []
13+
14+
def add_vertex(self, label: str):
15+
self.vertices.append(label)
16+
self.adjacencyList[label]: list = []
17+
self.colors[label] = "white"
18+
self.distance[label] = 0
19+
self.previous[label] = None
20+
self.no_incoming_edges[label] = label
21+
22+
def add_edge(self, label1: str, label2: str):
23+
self.adjacencyList[label1].append(label2)
24+
if label2 in self.no_incoming_edges:
25+
del self.no_incoming_edges[label2] # remove vertex, it has incoming edge
26+
27+
def topsort(self):
28+
# perform depth first search on vertices with no incoming edges
29+
for label in self.no_incoming_edges:
30+
self.dfs(label)
31+
self.explored.reverse()
32+
print(self.explored)
33+
34+
def dfs(self, start: str):
35+
self.time += 1
36+
self.entry[start] = self.time
37+
self.colors[start] = "gray"
38+
for neighbour in self.adjacencyList[start]:
39+
if self.colors[neighbour] == "white":
40+
self.previous[neighbour] = start
41+
self.distance[neighbour] = self.distance[neighbour] + 1
42+
self.dfs(neighbour)
43+
self.time += 1
44+
self.exit[start] = self.time
45+
self.colors[start] = "black"
46+
self.explored.append(start)
47+
48+
def show_path(self, label: str) -> str:
49+
if self.previous[label] is None:
50+
return label
51+
else:
52+
return self.show_path(self.previous[label]) + " -> " + label
53+

graphs/topological-sorting/main.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from graph import Graph
2+
3+
graph = Graph()
4+
5+
vertices = ["a", "b", "c", "d", "e", "f"]
6+
for vertex in vertices:
7+
graph.add_vertex(vertex)
8+
9+
10+
graph.add_edge("a", "c")
11+
graph.add_edge("c", "d")
12+
graph.add_edge("c", "f")
13+
graph.add_edge("b", "c")
14+
graph.add_edge("b", "e")
15+
16+
graph.topsort()
17+

0 commit comments

Comments
 (0)