Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Tree or Connected acyclic graph
Trees are graphs that do not contain even a single cycle. They represent hierarchical structure in a graphical form. Trees belong to the simplest class of graphs. Despite their simplicity, they have a rich structure.
Trees provide a range of useful applications as simple as a family tree to as complex as trees in data structures of computer science.
Tree
A connected acyclic graph is called a tree. In other words, a connected graph with no cycles is called a tree.
The edges of a tree are known as branches. Elements of trees are called their nodes. The nodes without child nodes are called leaf nodes.
A tree with 'n' vertices has 'n−1' edges. If it has one more edge extra than 'n−1', then the extra edge should obviously have to pair up with two vertices which leads to forming a cycle. Then, it becomes a cyclic graph which is a violation for the tree graph.
Example 1
The graph shown here is a tree because it has no cycles and it is connected. It has four vertices and three edges, i.e., for 'n' vertices 'n−1' edges as mentioned in the definition ?
Note − Every tree has at least two vertices of degree one.
Example 2
In the above example, vertices 'a' and 'd' have degree one. The other two vertices 'b' and 'c' have degree two. For not forming a cycle, there should be at least two vertices with degree one (leaf nodes).
Forest
A disconnected acyclic graph is called a forest. In other words, a disjoint collection of trees is called a forest.
Example
The following graph is a single disconnected graph with no cycles, hence it is a forest ?
Spanning Trees
Let G be a connected graph, then a sub-graph H of G is called a spanning tree of G if −
- H is a tree (connected and acyclic)
- H contains all vertices of G
A spanning tree T of an undirected graph G is a subgraph that includes all vertices of G with the minimum number of edges (n−1) needed to keep the graph connected.
Example
In the above example, G is a connected graph with 5 edges. H is a sub-graph of G that contains all four vertices but only 3 edges (n−1), has no cycles, and is connected. Hence H is a spanning tree of G.
Conclusion
A tree is a connected acyclic graph with n−1 edges for n vertices. A disconnected collection of trees is called a forest. A spanning tree of a connected graph includes all vertices with the minimum edges needed to maintain connectivity without any cycles.
