1

Given two adjacency matrices:

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

How to find their intersection , and their Union?

--> The edge with the highest value will be taken as the chosen resulting graph edge.

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
Sook Yee Lim
  • 101
  • 1
  • 9

1 Answers1

1

Intersection:

The intersection of two adjacency matrices in the adjacency matrix where two nodes are connected in each of the original matrices.

In this case, you must choose the edge with the highest value.

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

output:

[[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]]

Union:

Union is a little bit more involved, but can be obtained using 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))

output:

[[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]]
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • How do I modify this so that if one of the graph has a 0(vertice does not have edge) but the other one doesn't9vertice has edge)it just skips it instead of appending 0? – Sook Yee Lim Sep 29 '17 at 04:37
  • It does exactly what you ask for: try it with : `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]]`, the result is `[[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]]` – Reblochon Masque Sep 29 '17 at 04:46
  • Your codes run correctly but I just wanted to know how to modify it so that In a list of list, if one list has all 0's I want to remove that list and also remove the item with that index from all lists?For example: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]] Your codes will ouput: [[0, 19, 1, 0], [19, 0, 6, 0], [1, 6, 0, 0], [0, 0, 0, 0]] I want to remove the last list and also the last number in each list so it becomes [[0,19,1],[19,0,6],[1,6,0] .Thanks in advance. – Sook Yee Lim Sep 29 '17 at 05:24
  • Hi again I just want to know if there is any way to modify it so it becomes union instead?For the graph 1 and graph 2 example in the question the output becomes:[[0, 19, 2, 0, 12,0],, [19, 0, 0, 0, 0,0], [2, 0, 0, 0, 2,2],...and so on or do I have to recode a new one – Sook Yee Lim Sep 29 '17 at 12:10
  • Okay, I edited the answer to add the union of two adjacency matrices - the computation is a little bit more involved, due to the fact that `zip` ends when thre shortest sequence is exhausted. – Reblochon Masque Sep 29 '17 at 12:46