-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.py
More file actions
68 lines (57 loc) · 1.78 KB
/
Copy pathBFS.py
File metadata and controls
68 lines (57 loc) · 1.78 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
import igraph as ig
from igraph import *
import matplotlib.pyplot as plt
def breadth_first_search(graph, start_vertex):
queue = []
visited = set()
queue.append(start_vertex)
visited.add(start_vertex)
traversal_edge=[]
while queue:
traverse=Graph()
current_vertex = queue.pop(0)
#print(current_vertex)
try:
neighbors = graph[current_vertex]
except KeyError:
pass
for neighbor in neighbors:
# names=[0]
if neighbor not in visited:
# names+=[neighbor]
traversal_edge+=[(current_vertex,neighbor)]
print(traversal_edge)
traverse=ig.Graph(edges=traversal_edge)
# traverse.vs['name']=names
queue.append(neighbor)
visited.add(neighbor)
traverse.layout(layout='auto')
# visual_style = {}
# visual_style["vertex_label"] = traverse.vs["name"]
fig, ax = plt.subplots()
ig.plot(traverse, target=ax)
plt.show()
#graph creation
vertex=[0,1,2,3,4]
edges = [(0, 1), (0, 2), (0, 3), (1, 3), (1, 2), (3, 4), (2, 4)]
g=Graph()
g= ig.Graph(edges=edges)
g.vs['name']=vertex
el=g.get_edgelist()
edge_dict={}
for edge in edges:
parent,child = edge
if parent not in edge_dict:
edge_dict[parent] = []
edge_dict[parent].append(child)
print(edge_dict)
start_vertex = 0
print("Breadth-First Search starting from vertex", start_vertex)
breadth_first_search(edge_dict, start_vertex)
# #plotting
# g.layout(layout='auto')
# visual_style = {} #visual attributes of the graph
# visual_style["vertex_label"] = g.vs["name"]
# fig, ax = plt.subplots()
# ig.plot(g, target=ax,**visual_style)
# plt.show()