-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tris.py
102 lines (83 loc) · 2.04 KB
/
Tris.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
Created on Dec 2020
@author: robotane
"""
def tri_topo(graphe):
"""
Realise le tri topologique du graphe de maniere iterative
Parameters
----------
graphe : list
Matrice d'adjacence du graphe.
Returns
-------
tri : dict
Dictionnaire contenant les sommets range selon le tri topologique, les
valeurs etant les sommet et les cles l'ordre du sommet dans le tri.
"""
# Initialisation
E = list()
comp = dict()
tri = list()
rang = 0
long = len(graphe)
for s in range(long):
comp[s] = 0
for t in range(long):
if graphe[t][s] == 1:
comp[s] += 1
if comp[s] == 0:
E.append(s)
# Traitement
while E:
s = E.pop()
tri.append(s + 1)
rang += 1
for t in range(long):
if graphe[s][t] == 1:
comp[t] -= 1
if comp[t] == 0:
E.append(t)
return tri
def norme(graphe):
"""
Calcule la norme de tous les sommets du graphe
Parameters
----------
graphe : list
Matrice d'adjacence du graphe.
Returns
-------
nor : dict
Dictionaire dont les cles sont les sommets et les valeurs les normes
de ces sommets.
"""
# Initialisation
E = list()
comp = dict()
nor = dict()
long = len(graphe)
for s in range(long):
comp[s] = 0
nor[s] = 0
for t in range(long):
if graphe[t][s] == 1:
comp[s] += 1
if comp[s] == 0:
E.append(s)
# Traitement
while E:
s = E.pop()
for t in range(long):
if (graphe[t][s] == 1) and (nor[t] >= nor[s]):
nor[s] = nor[t] + 1
for t in range(long):
if graphe[s][t] == 1:
comp[t] -= 1
if comp[t] == 0:
E.append(t)
# Affichage du resultat
for k, v in nor.items():
# print(f"|{k}| = {v}")
print(f"|{k + 1}| = {v}")
return nor