Skip to content
DeepForgeby svx
All problems
MediumLinear Algebrala-258

Incidence Matrix Build

Problem

Build the oriented incidence matrix B of an undirected graph: rows are vertices and columns are edges.

For edge e = (a, b), set B[a][e] = -1 and B[b][e] = 1, and leave all other entries 0.

Hint

Each column has exactly one +1 and one -1, so the all-ones vector is always in its left null space.

Test cases

case 1
in: [3,[[0,1],[1,2]]]
expected: [[-1,0],[1,-1],[0,1]]
case 2
in: [2,[[0,1]]]
expected: [[-1],[1]]
case 3
in: [3,[[0,1],[0,2],[1,2]]]
expected: [[-1,-1,0],[1,0,-1],[0,1,1]]
case 4
in: [3,[[0,2]]]
expected: [[-1],[0],[1]]
Starter codePython
def incidence_matrix(num_vertices, edges):
    # Your code here
    pass

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