Search code examples
pythonnetworkxgraph-theorybreadth-first-search

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


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

Solution

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