efrei/theorie-graphes/calcul_rang.py

57 lines
1.4 KiB
Python
Raw Normal View History

2022-03-07 16:20:29 +01:00
#!/usr/bin/env python3
"""Algorithme de calcul de rang"""
2022-06-25 00:44:06 +02:00
import pprint
2022-03-07 16:20:29 +01:00
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],
]
2022-06-25 00:44:06 +02:00
print("Matrice :")
pprint.pprint(matrice)
2022-03-07 16:20:29 +01:00
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()