Add script calcul_rang.py
This commit is contained in:
parent
4702397f21
commit
2d2cc2eca3
1 changed files with 51 additions and 0 deletions
51
theorie-graphes/calcul_rang.py
Executable file
51
theorie-graphes/calcul_rang.py
Executable file
|
@ -0,0 +1,51 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
"""Algorithme de calcul de rang"""
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Algorithme itératif de calcul de rang sur une matrice d'adjacence"""
|
||||
|
||||
rangs = []
|
||||
|
||||
matrice = [
|
||||
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1],
|
||||
[0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
||||
[1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
|
||||
[0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
]
|
||||
|
||||
for _ in range(len(matrice)):
|
||||
d_entrant = [0] * len(matrice)
|
||||
|
||||
for line in matrice:
|
||||
for i, row in enumerate(line):
|
||||
d_entrant[i] += row
|
||||
|
||||
to_remove = [i for i, d in enumerate(d_entrant) if d == 0]
|
||||
if not to_remove:
|
||||
break
|
||||
|
||||
for i in reversed(to_remove):
|
||||
matrice.pop(i)
|
||||
for i in reversed(to_remove):
|
||||
for line in matrice:
|
||||
line.pop(i)
|
||||
|
||||
rangs.append(to_remove)
|
||||
|
||||
for rang, sommets in enumerate(rangs):
|
||||
print(f"Rang {rang} :")
|
||||
print(f" {sommets}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in a new issue