Commit f1b4622a authored by sbalev's avatar sbalev

A simple method to determine if a graph is (weakly) connected.

parent 6935b59d
......@@ -1710,4 +1710,23 @@ public class Toolkit extends
result.add(graph.<T> getEdge(i));
return result;
* Determines if a graph is (weakly) connected.
* @param graph A graph.
* @return {@code true} if the graph is connected.
* @complexity O({@code m + n}) where {@code m} is the number of edges and {@code n} is the number of nodes.
public static boolean isConnected(Graph graph) {
if (graph.getNodeCount() == 0)
return true;
Iterator<Node> it = graph.getNode(0).getBreadthFirstIterator(false);
int visited = 0;
while (it.hasNext()) {;
return visited == graph.getNodeCount();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment