-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_algorithms.py
More file actions
39 lines (34 loc) · 991 Bytes
/
Copy pathsearch_algorithms.py
File metadata and controls
39 lines (34 loc) · 991 Bytes
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
from graph import *
from graph_io import *
from list_and_stack import *
import random as rd
def connected_BFS(G: Graph):
queue = doubly_linked_list()
start = G.vertices[0]
queue.append(start)
visited = [start]
for i, node in enumerate(queue):
vertex = node.data
for neighbour in vertex.neighbours:
if neighbour not in visited:
queue.append(neighbour)
visited.append(neighbour)
# vertex.label = i
connected = i == len(G.vertices)-1
return connected
def connected_DFS(G: Graph):
s = stack()
start = G.vertices[0]
s.push(start)
visited = []
i = 0
while not s.isEmpty():
vertex = s.pop()
if vertex not in visited:
visited.append(vertex)
# vertex.label = vertex.degree
for neighbour in vertex.neighbours:
s.push(neighbour)
i += 1
connected = i == len(G.vertices)
return G, connected