printing all the edges of a graph in an adjacency matrix in python

Question:

How do you print the all the edges of a graph with a given adjacency matrix in python? for example, if 0 is adjacent to 3 and 8, it should print:
0 3
0 8
without repetition
I’ve been using Bfs but i don’t know how to update the queue and current element.

This is my code so far

A =  [[0, 1, 0, 0, 0, 1], 
      [1, 0, 0, 0, 0, 1], 
      [0, 0, 0, 1, 1, 0], 
      [0, 0, 0, 0, 1, 0],
      [0, 0, 0, 0, 0, 0],
      [1, 0, 0, 0, 0, 0]]

def edges(A):
    visited = [False] * len(A)
    queue = []

    s = [0][0]
    queue.append(s)
    visited[s] = True

    while len(queue) > 0:
        s = queue.pop(0)
        print(s)
        for i in range(len(A)):
            print(i)

            for j in range(len(A[0])):
                if A[i][j] == 1 and visited[s]== False:

                    queue.append([i][j])

                    visited[s] = True

print(edges(A))
Asked By: niko

||

Answers:

If I understood correctly, and given that your example matrix A is asymmetric, you could do:

A =  [[0, 1, 0, 0, 0, 1],
      [1, 0, 0, 0, 0, 1],
      [0, 0, 0, 1, 1, 0],
      [0, 0, 0, 0, 1, 0],
      [0, 0, 0, 0, 0, 0],
      [1, 0, 0, 0, 0, 0]]

def edges(adj):

    for i, neighbors in enumerate(adj):
        for j, v in enumerate(neighbors):
            if v:
                yield (i, j)


for edge in edges(A):
    print(edge)

Output

(0, 1)
(0, 5)
(1, 0)
(1, 5)
(2, 3)
(2, 4)
(3, 4)
(5, 0)
Answered By: Dani Mesejo

A simple way would be to iterate over the adjacency matrix, and build a list of tuples with indices where a connection exists:

[(i,j) for i,l in enumerate(A) for j,v in enumerate(l) if v]
# [(0, 1), (0, 5), (1, 0), (1, 5), (2, 3), (2, 4), (3, 4), (5, 0)]

However, you can easily do this with networkx. You can create a graph from the adjacency matrix using from_numpy_matrix, and print a list with the edges using edges:

A =  np.array([[0, 1, 0, 0, 0, 1], 
               [1, 0, 0, 0, 0, 1], 
               [0, 0, 0, 1, 1, 0], 
               [0, 0, 0, 0, 1, 0],
               [0, 0, 0, 0, 0, 0],
               [1, 0, 0, 0, 0, 0]])

import networkx as nx
g = nx.from_numpy_matrix(A, create_using=nx.DiGraph)
g.edges()
# OutEdgeView([(0, 1), (0, 5), (1, 0), (1, 5), (2, 3), (2, 4), (3, 4), (5, 0)])
Answered By: yatu

You could convert the matrix to an adjacency list, then print out the nodes and connecting edges:

A = [
    [0, 1, 0, 0, 0, 1],
    [1, 0, 0, 0, 0, 1],
    [0, 0, 0, 1, 1, 0],
    [0, 0, 0, 0, 1, 0],
    [0, 0, 0, 0, 0, 0],
    [1, 0, 0, 0, 0, 0],
]


def matrix_to_list(matrix):
    """Convert adjacency matrix to adjacency list"""
    graph = {}
    for i, node in enumerate(matrix):
        adj = []
        for j, connected in enumerate(node):
            if connected:
                adj.append(j)
        graph[i] = adj
    return graph


adjacency_list = matrix_to_list(A)
print(adjacency_list)
# {0: [1, 5], 1: [0, 5], 2: [3, 4], 3: [4], 4: [], 5: [0]}


connected_edges = [
    (node, edge) for node, edges in adjacency_list.items() for edge in edges
]
print(connected_edges)
# [(0, 1), (0, 5), (1, 0), (1, 5), (2, 3), (2, 4), (3, 4), (5, 0)]
Answered By: RoadRunner

Create a meshgrid the size of the adjacency matrix:

nodes = np.arange(len(A))
c_grid, r_grid = np.meshgrid(nodes, nodes)
np.stack([r_grid, c_grid, A])

Now you have a cube of dimensions [3, len(nodes), len(nodes)], which you can slice according to A:

almost_edges = coords_adj_mat[:, adj_mat]
edges = almost_edges[:2]
Answered By: Danny Zilberg