Skip to content
DeepForgeby svx
All problems
EasyGraph Algorithmsgraph-140

Effective Resistance on a Tree

Problem

Compute the effective resistance between two nodes of a tree whose edges are unit resistors.

On a tree there is a unique path between any two nodes, so the resistance is simply the number of edges on it. Return 0 when u equals v. edges is a list of [u, v] pairs.

Hint

Series resistors add along the unique tree path.

Test cases

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

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