Graph Theory37 sections · 1633 units
Open in Course

Planet Cycles - Walkthrough

(Example execution)

Graph: f(1)=2,f(2)=3,f(3)=1,f(4)=2f(1) = 2, f(2) = 3, f(3) = 1, f(4) = 2. You need to find the answer for each node. The answer is the total path length until returning to a cycle. Start DFS at 11: path [1,2,3][1, 2, 3], next is 11 (in path). Cycle detected with length 33. Assign 33 to nodes 11, 22, 33.

All cycle nodes get the cycle length as their answer. Start DFS at 44: path [4][4], next is 22 (already processed with answer 33). Node 22 has answer 33. Node 44 is 11 step from the cycle, so answer is 1+3=41 + 3 = 4. Tail nodes add their distance.