Calculation of "node closure" of a graph with deletion

For a directed graph, the goal is to combine the node with the nodes it points to and come up with a minimum number of these [let's give a name] super nodes.

A trap is when you combine nodes that you cannot use again. [first node, as well as all merged nodes are all members of one super node]

A greedy approach would be to select the node with the maximum degree and combine that node with the nodes it points to, and delete all of them. Do this every time with nodes that are not yet removed from the graph.

O (V) is greedy, but this does not necessarily infer the minimum numbers of super nodes. So what is the best algorithm for this?

+3
source share
2 answers

twenty! quite large, more than 2 ^ 61. Fortunately, there is a better way to solve small problems: (EDIT) dynamic programming. While preserving the optimal solutions for each subtask, we pay some memory in order to get very big time savings.

Here is sample code in Python. When implementing the code below in another language, you probably want to assign vertices 0, ..., n-1 and implement sets as bit vectors.

# find a smallest node closure of G
# G is a graph in adjacency-list format: G[v] is the set of neighbors of v
def node_closure(G):
    # maps subsets of vertices to their smallest node closure
    smallest = {frozenset(): []}
    def find_smallest(S):
        if S in smallest:
            return smallest[S]
        else:
            candidates = [[v] + find_smallest(S - frozenset([v]) - G[v]) for v in S]
            return min(candidates, key=len)
    return find_smallest(frozenset(G))

This problem has a reduction in NP hardness from the installed coating, which retains objective value. This means that if P = NP, the best guarantee you can get for an algorithm with polynomial time is that it always produces a solution that does not exceed O(log n)times the optimal one.

x1, ..., xm - , , S1, ..., Sn - , , {x1, ..., xm}. , , x1, ..., xm, S1, ..., Sn, R, R Si i, j, Si xj, xj Si. node : node , , R; node, , , xj, .

( , ! - .)

+1

-. . " ", " ", , .

        root     dependent
                  B
           A  <
                  C

                  E
           D  <
                  F

. 0: .

. , , , , .

: , , , . , .

"", "" "", , node, , (. . 1). , , node, .

    A B C
     \|/
      D
     /|\
    E F G

. 1: A-D , .

"" :

  • node ( , ).

, , :

  • node "". , , . , "". , "" , , , .

, . , :

  • node . "" . "" , node , node . , .

, , , .

+2

Source: https://habr.com/ru/post/1736637/


All Articles