Application of Breadth-First Search to see if a directed graph is Strongly Connected
- Vertex
u
andv
are mutually reachable if there is a path fromu
tov
and also a path fromv
tou
- A directed graph is strongly connected if every pair of nodes is mutually reachable
- Run BFS from any vertex
- Make Grev, a new graph with edge directions reversed
- Run BFS on Grev
- Return true iff every vertex is reachable in both BFS on G and BFS on Grev
- Make a
new StrongConnectivity()
object - Create a graph as an
ArrayList<ArrayList<Integer>>
Adjacency List- Each
graph.get(u)
is anArrayList<Integer>
of vertices representing edges going Fromu
- Vertex names are simply increasing integer values, no need for human-readable vertex ID's like
S
orT
- Graph adjacency list must have all edges represented
- If there are no outgoing edges from a vertex, add an empty
ArrayList
graph2.add(new ArrayList<Integer>());
- Each
- Call
isStronglyConnected()
and pass in a graph
boolean isGraph1StronglyConnected = strongConnectivityFinder.isStronglyConnected(graph1);
isStronglyConnected()
always chooses vertex0
to start BFS, but that's completely arbitrary