Add script calcul_rang.py

This commit is contained in:
flyingscorpio@clevo 2022-03-07 16:20:29 +01:00
parent 4702397f21
commit 2d2cc2eca3

51
theorie-graphes/calcul_rang.py Executable file
View 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()