From 2d2cc2eca3060fb4940b0435b4063bad1ec491e4 Mon Sep 17 00:00:00 2001 From: "flyingscorpio@clevo" Date: Mon, 7 Mar 2022 16:20:29 +0100 Subject: [PATCH] Add script calcul_rang.py --- theorie-graphes/calcul_rang.py | 51 ++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100755 theorie-graphes/calcul_rang.py diff --git a/theorie-graphes/calcul_rang.py b/theorie-graphes/calcul_rang.py new file mode 100755 index 0000000..9ebda86 --- /dev/null +++ b/theorie-graphes/calcul_rang.py @@ -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()