|
1 | | -from Stack import Stack |
| 1 | +from stack import Stack |
2 | 2 |
|
3 | 3 | class Graph: |
4 | 4 | def __init__(self): |
5 | 5 | self.vertices: list = [] |
6 | | - self.adjacencyList: dict = {} |
| 6 | + self.distance: dict = {} |
7 | 7 | self.prev: dict = {} |
| 8 | + self.adjacencyList: dict = {} |
8 | 9 | self.colors: dict = {} |
| 10 | + self.entry: dict = {} |
| 11 | + self.exit: dict = {} |
| 12 | + self.time: int = 0 |
9 | 13 | self.explored: list = [] |
10 | 14 |
|
11 | | - def add_vertex(self, label): |
| 15 | + def addVertex(self, label: str): |
12 | 16 | self.vertices.append(label) |
13 | | - self.adjacencyList[label] = [] |
| 17 | + self.distance[label] = 0 |
14 | 18 | self.prev[label] = None |
| 19 | + self.adjacencyList[label]: list = [] |
15 | 20 | self.colors[label] = "white" |
16 | 21 |
|
17 | | - def add_edge(self, label1, label2): |
| 22 | + def addEdge(self, label1: str, label2: str): |
18 | 23 | self.adjacencyList[label1].append(label2) |
19 | | - # self.adjacencyList[label2].append(label1) directed acyclic graph |
20 | | - |
21 | | - |
22 | | - def dfs(self, label, prev = None): |
23 | | - if self.colors[label] != "white": |
24 | | - return |
25 | | - self.colors[label] = "gray" |
26 | | - self.prev[label] = prev |
27 | | - for neighbour in self.adjacencyList[label]: |
28 | | - self.dfs(neighbour, label) |
29 | | - self.colors[label] = "black" |
30 | | - |
31 | | - def print_path(self, end_label)-> str: |
32 | | - if self.prev[end_label] is None: |
33 | | - return end_label |
| 24 | + #self.adjacencyList[label2].append(label1) |
| 25 | + |
| 26 | + def dfs(self, start: str): |
| 27 | + print("Visiting ", start) |
| 28 | + self.colors[start] = "gray" |
| 29 | + self.entry[start] = self.time |
| 30 | + self.time += 1 |
| 31 | + for neighbour in self.adjacencyList[start]: |
| 32 | + if self.colors[neighbour] == "white": |
| 33 | + self.distance[neighbour] = self.distance[start] + 1 |
| 34 | + self.prev[neighbour] = start |
| 35 | + self.dfs(neighbour) |
| 36 | + self.colors[start] = "black" |
| 37 | + self.exit[start] = self.time |
| 38 | + self.time + 1 |
| 39 | + self.explored.append(start) |
| 40 | + |
| 41 | + def printPath(self, label: str)-> str: |
| 42 | + if self.prev[label] is None: |
| 43 | + return label |
34 | 44 | else: |
35 | | - return self.print_path(self.prev[end_label]) + " -> " + end_label |
36 | | - |
37 | | - def get_neighbour(self, label)-> str: |
38 | | - count: int = 0 |
39 | | - found: bool = False |
40 | | - while count < len(self.adjacencyList[label]) and not found: |
41 | | - if self.colors[self.adjacencyList[label][count]] == "white": |
42 | | - found = True |
43 | | - else: |
44 | | - count += 1 |
45 | | - if found: |
46 | | - return self.adjacencyList[label][count] |
47 | | - else: |
48 | | - return None |
49 | | - |
50 | | - def toString(self): |
51 | | - result = "" |
52 | | - for vertex in self.vertices: |
53 | | - result += ("Neigbours of " + vertex + ": ") |
54 | | - for neighbour in self.adjacencyList[vertex]: |
55 | | - result += (neighbour + ", ") |
56 | | - result += "\n" |
57 | | - return result |
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 | | - |
| 45 | + return self.printPath(self.prev[label]) + " -> " + label |
83 | 46 |
|
0 commit comments