-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadjacent matrix.py
More file actions
53 lines (36 loc) · 808 Bytes
/
Copy pathadjacent matrix.py
File metadata and controls
53 lines (36 loc) · 808 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
40
41
42
43
44
45
46
47
def bfs(a):
q=[]
i=0
visited=[0]*len(a)
q.append(i)
visited[i]=1
t1 = False
p=[-1]*len(a)
p[0]=0
while(len(q)>0):
d=q.pop(0)
print(d,visited)
i=d
temp=i
for j in range(len(a)):
if(a[i][j]==1) and (visited[j]==0):
visited[j]=1
q.append(j)
p[j]=d
print(p)
elif(a[i][j]==1) and (p[i]!=j):
return True
return False
d=int(input("Enter the number of vertex"))
a=[]
for i in range(d):
a.append([0]*d)
s=int(input("Enter the no graph vertices:"))
for i in range(s):
temp1,temp2=input().split()
temp1=int(temp1)
temp2=int(temp2)
a[temp1][temp2]=1
a[temp2][temp1]=1
print(a)
print(bfs(a))