-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.py
More file actions
66 lines (61 loc) · 2.17 KB
/
Copy pathgraph.py
File metadata and controls
66 lines (61 loc) · 2.17 KB
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
class Graph:
def __init__(self):
self.vertices ={}
def add_vertex(self, vertex_id):
self.vertices[vertex_id] = set()
def add_edge(self, v1, v2):
if v1 in self.vertices and v2 in self.vertices:
self.vertices[v1].add(v2)
self.vertices[v2].add(v1)
else
raise indexError("That vertex does not exist:")
# could also print the response
def add_directed_edge(self, v1, v2):
if v1 in self.vertices and v2 in self.vertices:
self.vertices[v1].add(v2)
else
raise indexError("That vertex does not exist:")
def debthFirstSearch(adjList, node_id):
print(node_id)
for child_node in adjList[node_id]:
debthFirstSearch(adjList, child_node) # recursive call
def debthFirstSearchVisted(adjList, node_id, visited):
# time complexity is O(n**2) = quadratic
# because fir each ieration (n), you will check the visited (n) = N^2
# space complexity is
print(node_id)
visited.append(node_id)
for child_node in adjList[node_id]:
if child_node not in visited:
debthFirstSearchVisted(adjList, child_node, visited) # recursive call
def dft(adjList, node_id):
# time complexity is O(n) = linear
# space complexity is
nodes[node_id].color = "black"
# print(node_id)
# visited.append(node_id)
for child_node in adjList[node_id]:
if nodes[node_id].color = "white":
debthFirstSearchVisted(adjList, child_node, visited) # recursive call
def breathFirstSearch(adjList, node_id):
print(node_id)
frontier = []
frontier.append(node_id)
while len(frontier) > 0:
n = frontier.pop
print(n)
for next_node in adjList[n]:
frontier.append(next_node)
def breathFirstSearchVisited(adjList, node_id):
# this traversal will be O(n) = linear implimentation
print(node_id)
frontier = []
frontier.append(node_id)
visited = [] # this will helps with graph as well as
while len(frontier) > 0:
n = frontier.pop
if n not in visited
print(n)
visited.append(n)
for next_node in adjList[n]:
frontier.append(next_node)