Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // input: graph G
- // output: labeling of edges and partition of the vertices of G
- LabelAllConnectedComponents(G):
- // initialize all vertices and edges as unexplored (label is 0)
- for all u ∈ G.vertices()
- setLabel(u, UNEXPLORED)
- for all e ∈ G.edges()
- setLabel(e, UNEXPLORED)
- // call BFS on every unlabeled vertex, which results in
- // calling BFS once for each connected component
- componentID = 1
- for all v ∈ G.vertices()
- if getLabel(v) == 0:
- BFS(G, v, componentID++)
- // standard breadth-first-search algorithm that works on one component
- BFS(G, s, componentID):
- L[0] = new empty sequence
- insert vertex s at the end of L[0]
- setLabel(s, componentID)
- i = 0
- while L[i] is not empty:
- L[i+1] = new empty sequence
- for all v ∈ L[i].elements()
- for all e ∈ G.incidentEdges(v)
- if getLabel(e) == UNEXPLORED
- w ← opposite(v,e)
- if getLabel(w) == UNEXPLORED
- setLabel(e, DISCOVERY)
- setLabel(w, componentID)
- L[i+1].insertLast(w)
- else
- setLabel(e, CROSS)
- i = i+1
Add Comment
Please, Sign In to add comment