【问题标题】:Finding the Intersection and Union of two graphs given their adjacency matrices?在给定邻接矩阵的情况下找到两个图的交集和并集?
【发布时间】:2017-09-29 00:17:18
【问题描述】:

给定两个邻接矩阵:

graph1 = [[0, 1, 2, 1, 9], [1, 0, 0, 6, 0], [2, 0, 0, 15, 2], [1, 6, 15, 0, 7], [9, 0, 2, 7, 0]]
graph2 = [[0, 19, 1, 0, 12, 0], [19, 0, 2, 0, 0, 0], [1, 2, 0, 0, 2, 0], [0, 0, 0, 0, 3, 5], [12, 0, 2, 3, 0, 2], [0, 0, 0, 5, 2, 0]]

如何找到他们的intersection,以及他们的Union

--> 具有最高值的边将作为选择的结果图边。

【问题讨论】:

    标签: python graph-theory adjacency-matrix set-intersection set-union


    【解决方案1】:

    路口:

    邻接矩阵中两个邻接矩阵的交集,其中两个节点在每个原始矩阵中连接。

    在这种情况下,您必须选择具有最高值的边。

    def graph_intersection(graph1, graph2):
        """calculates the intersection of two graphs represented by their adjacency matrices
        the edges with highest weights are retained.
        :graph1: List of Lists representing the adjacency matrix of a graph
                 graph1 is not mutated by the function
        :graph2: List of Lists representing the adjacency matrix of a graph
                 graph2 is not mutated by the function
        :returns: a newly constructed List of Lists representing the intersection of graph1 and graph2
        """
        intersection = []
        for g1, g2 in zip(graph1, graph2):
            line = []
            for e1, e2 in zip(g1, g2):
                line.append(max(e1, e2) if e1 and e2 else 0)
            intersection.append(line)
        return intersection
    
    print(graph_intersection(graph1, graph2))
    

    输出:

    [[0, 19, 2, 0, 12], [19, 0, 0, 0, 0], [2, 0, 0, 0, 2], [0, 0, 0, 0, 7], [12, 0, 2, 7, 0]]
    

    联合:

    联合有点复杂,但可以使用itertools.zip_longest获得:

    import itertools
    
    def graph_union(graph1, graph2):
        """calculates the union of two graphs represented by their adjacency matrices
        the edges with highest weights are retained.
        :graph1: List of Lists representing the adjacency matrix of a graph
                 graph1 is not mutated by the function
        :graph2: List of Lists representing the adjacency matrix of a graph
                 graph2 is not mutated by the function
        :returns: a newly constructed List of Lists representing the union of graph1 and graph2
        """
        union = []
        for g1, g2 in itertools.zip_longest(graph1, graph2):
            line = []
            g1 = g1 if g1 is not None else (0,)
            g2 = g2 if g2 is not None else (0,)
            for e1, e2 in itertools.zip_longest(g1, g2):
                e1 = e1 if e1 is not None else 0
                e2 = e2 if e2 is not None else 0
                line.append(max(e1, e2))
            union.append(line)
        return union
    
    graph1 = [[0, 1, 2, 1, 9], [1, 0, 0, 6, 0], [2, 0, 0, 15, 2], [1, 6, 15, 0, 7], [9, 0, 2, 7, 0]] 
    graph2 = [[0, 19, 1, 0, 12, 0], [19, 0, 2, 0, 0, 0], [1, 2, 0, 0, 2, 0], [0, 0, 0, 0, 3, 5], [12, 0, 2, 3, 0, 2], [0, 0, 0, 5, 2, 0]]
    
    print(graph_intersection(graph1, graph2))
    print(graph_union(graph1, graph2))
    

    输出:

    [[0, 19, 2, 1, 12, 0], [19, 0, 2, 6, 0, 0], [2, 2, 0, 15, 2, 0], [1, 6, 15, 0, 7, 5], [12, 0, 2, 7, 0, 2], [0, 0, 0, 5, 2, 0]]
    

    【讨论】:

    • 我该如何修改它,以便如果一个图形有一个 0(顶点没有边)但另一个没有 9vertice 有边)它只是跳过它而不是附加 0?
    • 它完全符合您的要求:尝试使用:graph1 = [[0, 1, 2, 1, 9], [0, 0, 0, 0, 0], [2, 0, 0, 15, 2], [1, 6, 15, 0, 7], [9, 0, 2, 7, 0]] graph2 = [[0, 19, 1, 0, 12, 0], [19, 0, 2, 0, 0, 0], [1, 2, 0, 0, 2, 0], [0, 0, 0, 0, 3, 5], [12, 0, 2, 3, 0, 2], [0, 0, 0, 5, 2, 0]],结果为[[0, 19, 2, 0, 12], [0, 0, 0, 0, 0], [2, 0, 0, 0, 2], [0, 0, 0, 0, 7], [12, 0, 2, 7, 0]]
    • 您的代码运行正确,但我只是想知道如何修改它,以便在列表列表中,如果一个列表全为 0,我想删除该列表并删除具有该索引的项目从所有列表中?例如:graph1 = [[0,19,1,0],[19,0,2,0],[1,2,0,0],[0,0,0,0]] graph2 = [[0,1,1,9],[1,0,6,0],[1,6,0,7],[9,0,7,0]] 您的代码将输出: [[ 0, 19, 1, 0], [19, 0, 6, 0], [1, 6, 0, 0], [0, 0, 0, 0]] 我想删除最后一个列表和最后一个每个列表中的数字,因此它变为 [[0,19,1],[19,0,6],[1,6,0] 。提前致谢。
    • 您好,我只是想知道是否有任何方法可以修改它,使其变为联合?对于问题中的图 1 和图 2 示例,输出变为:[[0, 19, 2, 0, 12,0],, [19, 0, 0, 0, 0,0], [2, 0, 0, 0, 2,2],...等等还是我必须重新编码一个新的
    • 好的,我编辑了答案以添加两个邻接矩阵的并集 - 由于zip 在最短序列用完时结束,因此计算有点复杂。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多