Package com.jgalgo
Class Trees
- java.lang.Object
-
- com.jgalgo.Trees
-
public class Trees extends Object
Static methods class for tree graphs.- Author:
- Barak Ugav
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanisForest(Graph g)Check if a graph is a forest.static booleanisTree(Graph g)Check if an undirected graph is a tree.static booleanisTree(Graph g, int root)Check if a graph is a tree rooted as some vertex.
-
-
-
Method Detail
-
isTree
public static boolean isTree(Graph g)
Check if an undirected graph is a tree.An undirected graph is a tree if its connected and contains no cycle, therefore \(n-1\) edges.
This method runs in linear time.
- Parameters:
g- a graph- Returns:
trueif the graph is a tree, elsefalse- Throws:
IllegalArgumentException- ifgis a directed graph
-
isTree
public static boolean isTree(Graph g, int root)
Check if a graph is a tree rooted as some vertex.For undirected graphs, a graph which is a tree rooted at some vertex can be rooted at any other vertex and will always be a tree. For directed graphs however this is not true. A directed graph might be a tree rooted at some vertex, but will no be connected if we root it at another vertex.
This method runs in linear time.
- Parameters:
g- a graphroot- a root vertex- Returns:
trueif the graph is a tree rooted atroot, elsefalse.
-
isForest
public static boolean isForest(Graph g)
Check if a graph is a forest.A forest is a graph which can be divided into trees, which is equivalent to saying a graph with no cycles.
This method runs in linear time.
- Parameters:
g- a graph- Returns:
trueif the graph is a forest, elsefalse
-
-