52 lines
1.4 KiB
Python
52 lines
1.4 KiB
Python
|
#!/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()
|