public final class Trees extends Object
v
is called parent of a vertex w
if v
and w
are
adjacent (i.e. connected by an edge) and the unique path between w
and the root contains
v
. Note that each vertex except the root has exactly one parent.
w
is called child of a vertex v
if v
is the parent of
w
. A vertex may have several children.
n
children.v
is a node with out-degree (i.e., the number of edges having v
as a target) zero if
the input is a directed rooted tree, and a node with degree (i.e., the number of edges incident to
v
) one, otherwise.
T
is a subgraph of T
which is also a tree.u
and v
in a
tree graph is the shared ancestor of u
and v
that is located farthest from the root.
Modifier and Type | Method and Description |
---|---|
static void |
collectSubtree(Node root,
NodeList nodes)
Collects all nodes of the subtree starting from the given root node.
|
static EdgeList |
directTree(Graph tree)
Converts the given tree to a directed rooted tree with the given node as root element by reversing some edges.
|
static EdgeList |
directTree(Graph tree,
Node root)
Converts the given tree to a directed rooted tree with the given node as root element by reversing some edges.
|
static Node |
getCenterRoot(Graph tree)
Returns the center node of an undirected tree.
|
static NodeList |
getLeafNodes(Graph tree,
boolean directedRootedTree)
Returns all leaf nodes of the given tree.
|
static Node |
getNearestCommonAncestor(Graph tree,
Node root,
boolean rootedDownward,
NodeList nodes)
Returns the nearest common ancestor of a subset of nodes within a directed rooted tree.
|
static Node |
getRoot(Graph tree)
Returns a possible root for the given (undirected) tree.
|
static void |
getSubTreeDepths(Graph tree,
INodeMap subtreeDepthMap)
Returns the depths of each subtree of a rooted directed tree.
|
static void |
getSubTreeSizes(Graph tree,
INodeMap subtreeSizeMap)
Returns the size (number of nodes) of each subtree of a rooted directed tree.
|
static EdgeList[] |
getTreeEdges(Graph graph)
Returns an array of
EdgeList objects each containing edges that belong to a maximal directed subtree of the
given graph. |
static EdgeList[] |
getTreeEdges(Graph graph,
NodeList[] treeNodes)
Returns an array of
EdgeList objects each containing edges that belong to a maximal directed subtree of the
given graph. |
static NodeList[] |
getTreeNodes(Graph graph)
Returns an array of
NodeList objects each containing nodes that belong to a maximal directed subtree of the
given graph. |
static NodeList[] |
getUndirectedTreeNodes(Graph graph)
Returns an array of
NodeList objects each containing nodes that belong to a maximal undirected subtree of the
given graph. |
static Node |
getWeightedCenterNode(Graph tree)
Finds a node used by the greatest number of all (undirected) paths interconnecting all nodes with each other.
|
static Node |
getWeightedCenterNode(Graph tree,
INodeMap intWeight)
Finds a node used by the greatest number of all (undirected) paths interconnecting all nodes with each other.
|
static boolean |
isForest(Graph graph)
Checks whether or not the given graph is a forest, that is, a graph whose connected components are directed rooted
trees.
|
static boolean |
isForest(Graph graph,
boolean directedRootedTree)
Checks whether or not the given graph is a forest.
|
static boolean |
isNaryTree(Graph graph,
int n)
Checks whether or not the given graph is a directed rooted tree in which each node has a maximum of
n children. |
static boolean |
isRootedTree(Graph graph)
Checks whether or not the given graph is a directed rooted tree.
|
static boolean |
isTree(Graph graph)
Checks whether or not the given graph is an undirected tree.
|
public static final void collectSubtree(Node root, NodeList nodes)
root
- the given root nodenodes
- the NodeList
that will be filled with the nodes of the subtreepublic static final EdgeList directTree(Graph tree)
A list of all reversed edges will be returned by this method.
public static final EdgeList directTree(Graph tree, Node root)
A list of all reversed edges will be returned by this method.
public static final Node getCenterRoot(Graph tree)
The center node has the property of inducing a minimum depth tree when being used as the root of that tree.
public static final NodeList getLeafNodes(Graph tree, boolean directedRootedTree)
A leaf node is a node with outdegree == 0
if the input is a directed rooted tree, and a node with
degree == 1
, otherwise.
tree
- the given treedirectedRootedTree
- true
if the algorithm should consider the tree as directed, false
otherwiseNodeList
that contains all leaf nodes of the given treepublic static final Node getNearestCommonAncestor(Graph tree, Node root, boolean rootedDownward, NodeList nodes)
It is not part of the given subset.
tree
.O(tree.N())
tree
- the given directed rooted treeroot
- the root of the treerootedDownward
- true
if the tree is directed from the root to the leaves, false
otherwisenodes
- the subset of nodesnearest common ancestor
of the given subset of nodespublic static final Node getRoot(Graph tree)
More precisely:
getWeightedCenterNode(Graph, INodeMap)
.
indegree == 0
(or outdegree == 0
) is returned.public static final void getSubTreeDepths(Graph tree, INodeMap subtreeDepthMap)
tree
- a rooted directed tree graphsubtreeDepthMap
- the INodeMap
that will be filled during the execution with the depth of the subtree rooted at each nodepublic static final void getSubTreeSizes(Graph tree, INodeMap subtreeSizeMap)
tree
- a rooted directed tree graphsubtreeSizeMap
- the INodeMap
that will be filled during the execution with the size of the subtree rooted at each nodepublic static final EdgeList[] getTreeEdges(Graph graph)
EdgeList
objects each containing edges that belong to a maximal directed subtree of the
given graph.
This method can also be applied to the result obtained by
getUndirectedTreeNodes(Graph)
. In this case, the subtrees are considered to be undirected.
graph
- the given graphEdgeList
objects each containing edges that belong to a maximal subtreepublic static final EdgeList[] getTreeEdges(Graph graph, NodeList[] treeNodes)
EdgeList
objects each containing edges that belong to a maximal directed subtree of the
given graph.
This method can also be applied to the result obtained by
getUndirectedTreeNodes(Graph)
. In this case, the subtrees are considered to be undirected.
graph
- the given graphtreeNodes
- an array of NodeList
s previously calculated by getTreeNodes(Graph)
EdgeList
objects each containing edges that belong to a maximal subtreepublic static final NodeList[] getTreeNodes(Graph graph)
NodeList
objects each containing nodes that belong to a maximal directed subtree of the
given graph.
For each list of tree nodes, the first node element is the root of a tree. On each such root, all outgoing edges connect to nodes in the subtree and each in-degree of the root is at least two.
graph
- the given graphNodeList
objects each containing nodes that belong to a maximal directed subtreepublic static final NodeList[] getUndirectedTreeNodes(Graph graph)
NodeList
objects each containing nodes that belong to a maximal undirected subtree of the
given graph.
For each list of tree nodes, the first node is the only node of the subtree that may be incident to non-tree edges.
graph
- the given graphNodeList
objects each containing nodes that belong to a maximal undirected subtreepublic static final Node getWeightedCenterNode(Graph tree)
The number of paths per node are stored in the given INodeMap
.
public static final Node getWeightedCenterNode(Graph tree, INodeMap intWeight)
The number of paths per node are stored in the given INodeMap
.
public static final boolean isForest(Graph graph)
graph
- the given graphtrue
if the given graph is a forest, false
otherwisepublic static final boolean isForest(Graph graph, boolean directedRootedTree)
If
directedRootedTree == true
, each component has to be a directed rooted tree. Otherwise, each component has
to be an undirected tree.
isForest(graph, true) => isForest(graph, false)
graph
- the given graphdirectedRootedTree
- true
if the algorithm should check for directed rooted trees, false
otherwisetrue
if the given graph is a forest, false
otherwisepublic static final boolean isNaryTree(Graph graph, int n)
n
children.graph
- the given graphn
- the allowed maximum of childrentrue
if the given graph is a n-ary tree, false
otherwisepublic static final boolean isRootedTree(Graph graph)
isRootedTree(graph) => isTree(graph)
graph
- the given graphtrue
if the given graph is a directed rooted tree, false
otherwisepublic static final boolean isTree(Graph graph)
isRootedTree(graph) => isTree(graph)
.graph
- the given graphtrue
if the given graph is an undirected tree, false
otherwise