Skip to content
DeepForgeby svx
All problems
MediumGraph Algorithmsgraph-327

Feedback Edge Set Size

Problem

Compute how many undirected edges must be deleted to make the graph acyclic.

A forest with c components and n nodes has n - c edges, so the answer is m - (n - c), where m is the number of edges. Count the components with union-find first. Return 0 for an empty graph.

Hint

Every component with k nodes and k - 1 edges is a tree; extra edges are the culprits.

Test cases

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

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