next up previous
Next: Up: Previous:

Application

Finding the connected components of a graph.



Connected-Components(Graph)
$\;\;\;\;\;$foreach v in vertices(Graph)
$\;\;\;\;\;$ $\;\;\;\;\;$Make-Set(v)
$\;\;\;\;\;$foreach e in edges(Graph)
$\;\;\;\;\;$ $\;\;\;\;\;$(u,v) = e
$\;\;\;\;\;$ $\;\;\;\;\;$if Find-Set(u) $\neq$ Find-Set(v)
$\;\;\;\;\;$ $\;\;\;\;\;$then Union(u,v)



Same-Component(u,v)
$\;\;\;\;\;$if Find-Set(u) = Find-Set(v)
$\;\;\;\;\;$then return True
$\;\;\;\;\;$else return False

Example


next up previous
Next: Up: Previous: