Skip to content
DeepForgeby svx
All problems
EasyGraph Algorithmsgraph-186

Hypergraph Incidence Matrix

Problem

Build the incidence matrix of a hypergraph with n nodes and the given hyperedges.

hyperedges is a list of vertex lists. Return an n by m matrix where entry [v][j] is 1 when node v belongs to hyperedge j, counting repeated mentions only once.

Hint

A hyperedge can contain any number of nodes, and duplicates do not change membership.

Test cases

case 1
in: [3,[[0,1],[1,2],[0,2]]]
expected: [[1,0,1],[1,1,0],[0,1,1]]
case 2
in: [2,[]]
expected: [[],[]]
case 3
in: [2,[[0,0,1]]]
expected: [[1],[1]]
Starter codePython
def hypergraph_incidence(n, hyperedges):
    # Your code here
    pass

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