Diff for Directed Acyclic Graphs

前端 未结 2 1906
故里飘歌
故里飘歌 2021-01-31 09:47

I\'m looking for an algorithm which can diff two Directed Acyclic Graphs(DAGs). That is, I\'d like an algorithm which produces a sequence of deletions and insertion

相关标签:
2条回答
  • 2021-01-31 10:46

    How would your specific data representation show that edges c and x in your DAG 2 example terminate in the same vertex?

    If we assume Wikipedia's general definitions of "directed graph", "vertex", and "edge", there is no such thing as an "unlabeled vertex" because without labeling them, there would be no way to describe edges, according to the definition there.

    As is, it seems to me your question is impossible to answer. Please provide (1) a simple example of the input provided to the algorithm — a data structure describing each graph as a collection of vertices and edges — and the expected output in a similar way, and (2) a consistent way to distinguish if an edge or vertex in the first DAG is equivalent to one in the second DAG, implying no difference in that aspect of the graphs.

    Perhaps your question is actually mostly about how to determine the labels for the vertices in each DAG in the input and how to best correlate them. Or, alternatively, perhaps labels are just a convenience to describe each graph and the question is actually seeking the minimal set of changes to describe a transformation of the structure of one graph to another.

    That said, edges and vertices in a traditional, mathematical, definition of a graph are atomic. Each vertex or edge either exists or does not exist in any one graph, making the concept of a diff somewhat meaningless, or otherwise trivial to build, if we assume that an identical label for any specific vertex or edge represents the exact same vertex or edge in both graphs.

    Such a trivial algorithm would basically just enumerate each vertex and edge in the two DAGs and add the appropriate operations to the diff, choosing only from the following operations:

    add vertex v
    remove vertex v
    add edge e
    remove edge e
    switch direction for edge e
    
    0 讨论(0)
  • 2021-01-31 10:47

    This might be a bit too late but just for fun: Both of your DAGs can be expressed as matrices, with row index indicating the "from" vertex, and the column index indicating the "to" vertex, and the corresponding cell labeled with edge id. You can give vertex unique and random ids.

    The next part is a bit tricky, because only your edges have meaningful label that maps from DAG1 to DAG2. Suppose you have a set of edges E* that are the intersect of labeled edges from DAG1 and DAG2, you will need to perform a series of row shift (move up or down) or column shift (move left or right) so position of all edges in E* in DAG1 and DAG2 maps to each other. Note that for a DAG represented in Matrix, shifting position of entire row or entire column still makes the representation equivalent.

    The remaining operation would be to rename the vertex according to the mapped matrices, compare the two matrices, and identify the new edges and new vertex required (and edges and vertices that can be removed.

    0 讨论(0)
提交回复
热议问题