CH - 6 & 7 Graphs and Trees
CH - 6 & 7 Graphs and Trees
CH - 6 & 7 Graphs and Trees
1
Tree
A Tree is also a mathematical non-linear data structure, but it organizes
the data in the context of hierarchy, i.e. the data is represented by a
node and the successive data is denoted by the nodes just below it
which is termed as the child node.
Trees are used to arrange the data in a sorted order because they take a
hierarchical structure between data elements.
A Tree arranges the data into branches that relate the information and
properties.
Formally, a graph is a pair of sets (V, E), where V is the set of vertices
and E is the set of edges, connecting the pairs of vertices. Take a look at
the following graph −
here, V = {a, b, c, d, e}
E = {ab, ac, bd, cd, de}
In computer programming, a Graph is a non-linear data structure which
consists of vertices (also known as nodes) and edges to show the relation
between data.
The nodes on a graph are the circles or points, while the edges are the
line segments or arcs.
Graphs are basically the mathematical non-linear data structure that are
used for representation of different kinds of physical structures.
In a graph, a node can have any number of edges. But, it does not have a
unique node like a root.
The most effective rule depends on the type of graph and the problem at hand
so it’s not possible to make truly general statements.
Since many data structures in computer science have an underlying graph that
is a tree, it’s worth spending some effort to understand better the strategies to
explore such a graph in order to find paths or nodes that satisfy the conditions
of the problem at hand.
In many cases, the problem can be reduced to a statement such as “find the path
between node A and B such that a certain quantity is optimized”.
The quantity may be for example the number of edges (i.e. the length of the
path), or the sum of a certain feature (possibly binary) associated to each node.
Two main approaches are called Depth-First Search (DFS) and Breadth-First
Search (BFS).
Depth First Traversal
Depth First Search (DFS) algorithm traverses a graph in a depthward
motion and uses a stack to remember to get the next vertex to start a
search, when a dead end occurs in any iteration.