Skip to content
DeepForgeby svx
All problems
EasyGraph Algorithmsgraph-344

Matrix to Edge List

Problem

Convert an undirected adjacency matrix into a sorted edge list.

matrix is a square 0/1 matrix and the graph is simple, so only the upper triangle matters. Emit each edge once as [u, v] with u < v and sort the result. A matrix with all zero entries yields an empty list.

Hint

Scan u < v only; iterating the full matrix would list every edge twice.

Test cases

case 1
in: [[[0,1],[1,0]]]
expected: [[0,1]]
case 2
in: [[[0,1,0],[1,0,1],[0,1,0]]]
expected: [[0,1],[1,2]]
case 3
in: [[[0,0],[0,0]]]
expected: []
case 4
in: [[[0,1,1],[1,0,1],[1,1,0]]]
expected: [[0,1],[0,2],[1,2]]
case 5
in: [[[0]]]
expected: []
Starter codePython
def matrix_to_edge_list(matrix):
    # Your code here
    pass

5 test cases · run your code against them in the editor