Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graph reduction

I have been working on an piece of code to reduce a graph. The problem is that there are some branches that I want to remove. Once I remove a branch I can merge the nodes or not, depending on the number of paths between the nodes the branch joined.

Maybe the following example illustrates what I want:

enter image description here

The code I have is the following:

from networkx import DiGraph, all_simple_paths, draw
from matplotlib import pyplot as plt

# data preparation
branches = [(2, 1),
            (3, 2),
            (4, 3),
            (4, 13),
            (7, 6),
            (6, 5),
            (5, 4),
            (8, 7),
            (9, 8),
            (9, 10),
            (10, 11),
            (11, 12),
            (12, 1),
            (13, 9)]

branches_to_remove_idx = [11, 10, 9, 8, 6, 5, 3, 2, 0]
ft_dict = dict()
graph = DiGraph()

for i, br in enumerate(branches):
    graph.add_edge(br[0], br[1])
    ft_dict[i] = (br[0], br[1])

# Processing -----------------------------------------------------
for idx in branches_to_remove_idx:

    # get the nodes that define the edge to remove
    f, t = ft_dict[idx]

    # get the number of paths from 'f' to 't'
    n_paths = len(list(all_simple_paths(graph, f, t)))

    if n_paths == 1:
        # remove branch and merge the nodes 'f' and 't'
        #
        #       This is what I have no clue how to do
        #
        pass

    else:
        # remove the branch and that's it
        graph.remove_edge(f, t)
        print('Simple removal of', f, t)

# -----------------------------------------------------------------

draw(graph, with_labels=True)
plt.show()

I feel that there should be a simpler direct way to obtain the last figure from the first, given the branch indices, but I have no clue.

like image 620
Santi Peñate-Vera Avatar asked Sep 13 '18 12:09

Santi Peñate-Vera


1 Answers

I think this is more or less what you want. I am merging all nodes that are in chains (connected nodes of degree 2) into one hypernode. I return the the new graph and a dictionary mapping the hypernode to the contracted nodes.

import networkx as nx

def contract(g):
    """
    Contract chains of neighbouring vertices with degree 2 into one hypernode.

    Arguments:
    ----------
    g -- networkx.Graph instance

    Returns:
    --------
    h -- networkx.Graph instance
        the contracted graph

    hypernode_to_nodes -- dict: int hypernode -> [v1, v2, ..., vn]
        dictionary mapping hypernodes to nodes

    """

    # create subgraph of all nodes with degree 2
    is_chain = [node for node, degree in g.degree_iter() if degree == 2]
    chains = g.subgraph(is_chain)

    # contract connected components (which should be chains of variable length) into single node
    components = list(nx.components.connected_component_subgraphs(chains))
    hypernode = max(g.nodes()) +1
    hypernodes = []
    hyperedges = []
    hypernode_to_nodes = dict()
    false_alarms = []
    for component in components:
        if component.number_of_nodes() > 1:

            hypernodes.append(hypernode)
            vs = [node for node in component.nodes()]
            hypernode_to_nodes[hypernode] = vs

            # create new edges from the neighbours of the chain ends to the hypernode
            component_edges = [e for e in component.edges()]
            for v, w in [e for e in g.edges(vs) if not ((e in component_edges) or (e[::-1] in component_edges))]:
                if v in component:
                    hyperedges.append([hypernode, w])
                else:
                    hyperedges.append([v, hypernode])

            hypernode += 1

        else: # nothing to collapse as there is only a single node in component:
            false_alarms.extend([node for node in component.nodes()])

    # initialise new graph with all other nodes
    not_chain = [node for node in g.nodes() if not node in is_chain]
    h = g.subgraph(not_chain + false_alarms)
    h.add_nodes_from(hypernodes)
    h.add_edges_from(hyperedges)

    return h, hypernode_to_nodes


edges = [(2, 1),
         (3, 2),
         (4, 3),
         (4, 13),
         (7, 6),
         (6, 5),
         (5, 4),
         (8, 7),
         (9, 8),
         (9, 10),
         (10, 11),
         (11, 12),
         (12, 1),
         (13, 9)]

g = nx.Graph(edges)

h, hypernode_to_nodes = contract(g)

print("Edges in contracted graph:")
print(h.edges())
print('')
print("Hypernodes:")
for hypernode, nodes in hypernode_to_nodes.items():
    print("{} : {}".format(hypernode, nodes))

This returns for your example:

Edges in contracted graph:
[(9, 13), (9, 14), (9, 15), (4, 13), (4, 14), (4, 15)]

Hypernodes:
14 : [1, 2, 3, 10, 11, 12]
15 : [8, 5, 6, 7]
like image 78
Paul Brodersen Avatar answered Sep 29 '22 11:09

Paul Brodersen