-
Notifications
You must be signed in to change notification settings - Fork 0
/
S_Topol_var1.py
49 lines (48 loc) · 1.27 KB
/
S_Topol_var1.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
'''
Sortare topologica var 1
folosind BF si o lista cu gradele interne ale nodurilor
incep de la nodurile cu gradul intern 0
graf orientat
nu merge pe graf cu circuite
'''
n=6
m=7
#doar de la a la b
la={1: [2, 5],
2: [],
3: [], # am scos muchia (3,5) ca sa nu mai fie circuit
4: [6],
5: [2, 4],
6: [3]
}
#calculez gradul intern pt fiecare varf
#de cate ori apare un nod in liste
grad_intern=[0]*(n+1)
for i in range(1, n+1):
for key in la.keys():
for nod in la[key]:
if i==nod:
grad_intern[i]=grad_intern[i]+1
#alg de sortare topologica
sortate=[]
from collections import deque
c=deque([])
#adaug in coada nodurule care au gradul 0
for i in range (1,n+1):
if grad_intern[i]==0:
c.append(i)
nod=0
while len(c)>0:
i=c.popleft()
sortate.append(i)
for j in la[i]: #pt fiecare muchie de la i la j
nod=j
grad_intern[j]=grad_intern[j]-1 #scad gradul
if grad_intern[j]==0: #daca e 0 il adaug in coada si pe el
c.append(j)
#detectare nod in care incepe si se termina un circuit
#nodul la care se blocheaza
if len(sortate)!=n:
print ("graf cu circuit \nnodul la care avem circuit: ", nod)
else:
print(*sortate)