图归约

时间:2018-09-13 12:14:41

标签: python-3.x graph networkx

我一直在研究一段代码来简化图形。问题是有一些我要删除的分支。一旦删除分支,就可以合并或不合并节点,具体取决于分支加入的节点之间的路径数。

也许下面的示例说明了我想要的东西:

enter image description here

我的代码如下:

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()

在给定分支索引的情况下,我认为应该有一种更简单的直接方法来从第一个图形中获取最后一个图形,但是我没有头绪。

1 个答案:

答案 0 :(得分:1)

我认为这或多或少是您想要的。我将链中的所有节点(2级的连接节点)合并到一个超节点中。我返回新图和将超节点映射到收缩节点的字典。

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))

这将返回您的示例:

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]