Bonjour,

En cour nous avons écrit une fonction permettant de contracter/simplifier un graphe avec la bibliothèque Networkx.
Ce code suivant fonctionn epour tous mes camarades excepté pour moi qui code sur ma machine perso:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
 
def contract(A):
    B = A
    modif = True
    while modif:
        modif = False
        for som in B.nodes():
            if B.degree(som) == 2:
                B.add_edges_from([B.neighbors(som)])
                B.remove_node(som)
                modif = True
    return B
Quand j'execute mon main:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
 
G = MultiGraph()
 
G.add_nodes_from([1, 2, 3, 4])
G.add_edges_from([(1, 2),
                  (2, 3),
                  (3, 4)])
 
G = contract(G)
J'obtient ceci:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
 
/usr/bin/python3.6 /home/thomas/Code/Graph_Theory/Session_4/src/test.py
Traceback (most recent call last):
  File "/home/thomas/Code/Graph_Theory/Session_4/src/test.py", line 10, in <module>
    G = contract(G)
  File "/home/thomas/Code/Graph_Theory/Session_4/src/functions.py", line 252, in contract
    B.add_edges_from([B.neighbors(som)])
  File "/home/thomas/.local/lib/python3.6/site-packages/networkx/classes/multigraph.py", line 516, in add_edges_from
    ne = len(e)
TypeError: object of type 'dict_keyiterator' has no len()
 
Process finished with exit code 1
Pourriez vous m'aider svp, cela me rend fou de ne pas comprendre pourquoi ce la ne fonctionnne pas pour moi.