Python networkx : edge contraction
Asked Answered
V

2

6

I have a NetworkX graph. I would like to know how to do edge contraction between multiple nodes.

For example, if I wanted to contract X, Y and Z:

         _ node A _
       _/    |     \_
node X --- node Y --- node Z

Would become

           node A 
             |     
           node XYZ (or whatever X/Y/Z)

Graph creation is not the problem. It works. I want to reduce the graph by merging nodes that have the same "meanings": nodes that I call "end lvl" (node name length is equal to 7) and that are linked together.

I have found the condensation function in NetworkX so I tried to use it:

# edge contraction for same nodes
# for each node, get the links to other nodes "end lvl"
# if there is such a link, it means that these node are
# the sames
#
# copy graph
I = G
for n,d in G.nodes(data=True):
    if n in I.nodes():
        if len(n) == 7:
            # list of nodes adjacent to n : filter only "end lvl" nodes
            neighbors = [ node for node in I.neighbors(n) if len(node) == 7 ]
            nodes_to_merges = neighbors.append(n)
            I = nx.condensation(I,scc=nodes_to_merges)

The thing I got when I convert to JSON is:

{"directed": true, "graph": [], "nodes": [{"id": 0}], "links": [], "multigraph": false}

There is a problem as you can see...

Reference to functions are here.

Virginavirginal answered 26/3, 2013 at 15:28 Comment(5)
One solution would be to do it manually using a dict representation (to_dict_of_dicts).Oxbridge
Ok, i am not familiar with graphs but i should improve my question, as @zodiac asked me. Here it comes.Virginavirginal
what do the nodes(), neighbours() and condensation() functions do? what is nx?Faceplate
nx is python networkx : networkx.github.com. Nodes() return the nodes for a graph. Neighbours(x) return the neighbors node of a node x. Condensation of G is the graph with each of the strongly connected components or nodes contracted into a single node.Virginavirginal
you whole graph seems to be strongly connected to me, hence the output only has one node. don't use it to contract arbitrary nodesFaceplate
A
4

How about:

add_node(XYZ)
add_edge(XYZ, A)
for edge incident on (X, Y, Z):
    v = nodes in edge not in (X, Y, Z, A)
    if v:
       remove_edge(edge)
       add_edge(v, XYZ)
for node in (X, Y, Z):
    remove_node(node)
Aceae answered 26/3, 2013 at 15:38 Comment(2)
I rewrote your pseudocode in python and with networkx functions. I made what i wanted. Thanks.Virginavirginal
Note that the above will erase any attribute information from the edges and nodes.Exit
F
0

Instead of trying to use nx.condensation (which is meant for contracting strongly connected components, not groups of nodes in general) use these functions:

https://networkx.org/documentation/stable/reference/classes/graph.html#adding-and-removing-nodes-and-edges

to remove all but one of the collapsed nodes and rewire their nodes.

Faceplate answered 27/3, 2013 at 9:13 Comment(2)
The web does not existLurlene
I found what I think must be the current version of that link and added it. Hope it's right.Ionogen

© 2022 - 2024 — McMap. All rights reserved.