-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL1_B4.py
65 lines (60 loc) · 1.33 KB
/
L1_B4.py
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
'''
DRUMURI MINIME FOLOSIND BF
determinarea mai multor lanturi minime intre doua varfuri u si v
folosesc dictionar de tati
graf neorientat
folosesc BF
'''
#de la a la b si de la b la a
lista={1: [2, 4],
2: [1, 3, 5],
3: [2, 6, 8, 5],
4: [1, 6],
5: [2, 3, 8],
6: [3, 4, 7],
7: [6, 8],
8: [3, 7, 5]}
n=8
m=11
#u si v date de la tastatura
print("dati cele doua noduri: ")
u=int(input())
v=int(input())
viz=[0]*(n+1)
tata={}
for i in range (1,n+1):
tata[i]=[]
d=[None]*(n+1)
parcurg=[]
def BF(s):
from collections import deque
q=deque([])
q.append(s)
viz[s]=1
d[s]=0
while len(q)>0:
x=q.popleft()
parcurg.append(x)
for j in lista[x]:
if viz[j]==0:
q.append(j)
viz[j]=1
tata[j].append(x)
d[j]=d[x]+1
elif d[j]==d[x]+1:
tata[j].append(x)
BF(u)
c=[]
def LANT(s):
c.append(s)
if s==u:
while len(c)>0 and len(tata[c[-1]])<=1:
print(c.pop(), end=" ")
if len(c)>0:
c.reverse()
print(*c)
c.reverse()
print()
for t in tata[s]:
LANT(t)
LANT(v)