Graph Tree Definitions

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Graph:

A graph G consists of two sets V and E. The set V is a finite,

non empty set of vertices {v1 ,v2 ,v3 ,…….vk} and E is a finite set of pairs
of vertices {e1,e2,……..,en} where each ej is associated with (vi,vj)
unordered, these pairs are called edges. The notations V(G) and E(G)
represent the set of vertices and edges, respectively, of graph G. We
also write G=(V,E) to represent a graph.

Undirected graph :
A graph in which the pair of vertices representing any edge is
unordered is called undirected graph. Thus the pair (vi,vj) and (vj,vi)
represent the same edge.
Examples :

Directed Graph :
A graph in which each edge is represented by a directed pair
<vi,vj>; vi is the head and vj is the tail of the edge.
Example:

Degree: The degree of the vertex u is written as d(u), it is the number of


edges incident on u . if d(u)=0 i.e., if u does not belong to any edge, then
u is called an isolated vertex or isolated node.

Here,
d(v3)=2 and d(v2)=1 and d(v1)=3
d(v4)=2

Indegree:
In case of directed graphs, among the edges containing a vertex vi,
the number of edges pointing towards vi is called Indegree din of the
vertex vi.

Outdegree:
In case of directed graphs, among the edges containing a vertex vi,
the number of edges pointing away from vi is called outdegree dout of the
vertex vi.
here din (v1)=1,dout(v1)=2
din (v2)=1,dout(v2)=0
din (v3)=2,dout(v3)=0
din (v4)=1,dout(v4)=1

Adjacency matrix:
Let G=(V,E) be a graph with n vertices, n>=1. The adjacency
matrix of G is a two dimensional n×n array, say a, with the property that
a(i,j)=1 iff the edge (i,j) is in E(G). The element a(i,j)=0 if there is no such
edge in G.

corresponding adjacency matrices:

The adjacency matrix of the graph does depend on the ordering of


the nodes of G i.e., a different ordering of the nodes may result in
different adjacent matrices. However, the matrices resulting from
reverse or opposite ordering are closely related in that one can be
obtained from the other by simply interchanging rows and columns.

Corresponding adjacency matrix is:

G3
Here we can observe that G2 and G3 are transpose to one another.
If G is an undirected graph, then the adjacency matrix A of G will
be a symmetric matrix, i.e., one in whish aij=aji for every i and j. This
follows from the fact that each undirected edge (u,v) corresponds to the
two directed edges <u,v> and <v,u>.
Path :
A path is a collection of nodes beginning with vertex v i and ending
with vertex vj. There may be repetition of vertices.

Closed path:
If the beginning and ending vertices of the path are same, it is
called closed path.
Examples: 1) v1 → v2 → v4 → v1 → v3
2) v3 → v1 → v2 → v4 → v1
v1 v2
Graph G1

v3 v4
Example: For graph G1 above, v3 → v1 → v4 → v2 → v1 → v3 is a closed
path.

Open path:
If the beginning and ending vertices of the path are different, it is
called open path.
Example: For graph G1 above v3 → v1 → v4 → v2 → v1 is a open path.

Simple path:
A path in which there is no repetition of vertices is called Simple
Path.
Example: For graph G1 above v3 → v1 → v4 → v2 is a simple path.

Simple closed path:


A simple path that starts and ends with the same vertex is called
simple closed path.
Example: For graph G1 above v1 → v4 → v2 → v1 is a simple closed
path.

Connected Graph and Disconnected Graph :


If there exists a path between any pair of vertices in a graph then
the graph is called connected graph, if not it is called disconnected
graph.
Graph G1 Graph G2

In the graph G1, vertex v3 is not connected with any other vertices.
So, it is a disconnected Graph.
In the graph G2, all the vertices are connected. So it is a connected
Graph.
If there are ‘n’ vertices, then the minimum no. of edges required to
form a connected graph is ‘n-1’.
Tree : An undirected connected graph with n vertices and (n-1) edges
is called a tree.

Fig:- a tree with 9 nodes (i.e. vertices) and 8 edges

Rooted Tree : A tree with one of the node as distinguished node is


called a rooted tree and that distinguished node is called a root.
Fig:- a rooted tree with 9 nodes (i.e. vertices) and 8 edges

Binary Tree : :- A binary tree is a rooted tree in which each vertex


has at most two children and each child of a vertex is designated as its
left or right child. The left child and right child are either empty or two
disjoint binary trees called the left subtree and right subtree.

Fig.- Binary Tree

Representation of Binary Tree in memory:


(1) Array Representation : An array (say T[1-----n]) can be used
to store the nodes of a binary tree, the maximum no. of node a binary
tree can have is the size of the array.
Another array (say status[1-----n]) is used for status i.e. to check whether
there is a node or not in the binary tree. The element of status array is
either 1 (if node exists at corresponding location in binary tree) or 0(if
node does not exist at corresponding location in binary tree).

Example : Let us consider the Binary tree shown in figure below,

Array representation of this tree is,


Index 1 2 3 4 5 6
T 10 23 18
Status 1 0 1 0 0 1
Now, suppose root node is at index 1 in array T, then in successive
memory location left and right child are stored and so on. In this way we
have two rules for array representation of a Binary Tree.

(i) Rule for Children : If root is at index i then left child is at index
2i and right child is at index 2i+1.

Fig.- representing rule for children


(ii) Rule for Parent : If left child is at index j (and obviously right
child is at index j+1) then, parent will be at index  j / 2 ( i.e.
floor of j/2).

Fig.- representing rule for parent

Now let us see an algorithm for making a binary tree. In this algorithm
the terms used are,
x = value of data which is to be inserted,
P = location of parent
T[1----n] = array of size n to store data of nodes
status[1----n] = array of size n to store status (1 or 0) of node

Algorithm :
Make Tree(x, T[1----n], status[1-----n])
for i1 to n
status [i]0
T[1]x
Status[1]1

Set Left ((P,x), T[1----n], status[1----n])


If(2P>n) then “invalid insertion”.
else If (status[2P]=1) then “can’t insert” (i.e. element exists at this
position).
else
T[2P]  x
status[2P]1

Set Right ((P,x), T[1----n], status[1----n])


If(2P+1>n) then “invalid insertion”.
else If (status[2P+1]=1) then “can’t insert” (i.e. element exists at this
position).
else
T[2P+1]  x
status[2P+1]1
(2) Linked Representation : Binary Tree can be represented
using Linked list, in which a node consist of three fields as follows-

(i) Info (i.e. data at that node)


(ii) Left child
(iii) Right child

Lchild Info Rchild


Fig.- linked representation of a node in binary tree

If a node does not have a left or right child then it contains NULL in that
particular field.
NULL Info Rchild
Fig.- if left child doesn’t exist

Lchild Info NULL


Fig.- if right child doesn’t exist
NULL Info NULL
Fig.- if node doesn’t have any child

Algorithm :
Make Tree (x)
P get node()
Info(P)x
Left(P)NULL
Right(P)NULL

Set Left (P,x)


If (left(P) ≠ NULL) then “can’t insert”. (i.e. element exists at this
position).
else
qget node()
Info(q)x
Left(P)q
Left(q)NULL
Right(q)NULL

Set Right(P,x)
If (right(P)≠NULL) then “can’t insert”. (i.e. element exists at this position).
else
qget node()
Info(q)x
Right(P)q
Left(q)NULL
Right(q)NULL
Binary Search Tree. A binary tree in which all the elements to the
left of the node are smaller and all the elements to the right of the node
are larger is called a binary search tree(BST).

Fig.- Binary Search Tree


Algorithm: - Here is an algorithm for making binary search tree with no
repeated elements.

Make Tree (num)


p1, q1
while(q<n && status(q)=1 && num ≠ T(p))
{
pq
if(num<info(q)) then q2p else q2p+1
}
if(q>n) then “overflow”
else
if (num=info(p)) then “num is duplicate”
else if (num<info(p)) then set left (p, num)
else set right (p, num)

Almost Complete Binary Tree :- An almost complete binary


tree is a tree in which each node that has a right child also has a left
child. Having a left child does not require for a node to have a right child.
In other words we can say an almost complete binary tree is a tree
where for a right child, there is always a left child, but for a left child
there may not be a right child.
Note :- A left child is prior to the right child in almost complete binary
tree.

Fig.- Almost complete binary tree with 12 vertices


Hence, in an almost complete binary tree with depth k, the minimum
number of elements are,
20 +21 +22 +23 +------+2k-1+ 1
and maximum number of elements are,
20 +21 +22 +23 +------+2k

i.e. if n is the no. of elements in an almost complete binary tree, then


20 +21 +22 +23 +------+2k-1+ 1 ≤ n ≤ 20 +21 +22 +23 +------+2k
or, 2k ≤ n ≤ 2k+1 - 1
or, 2k ≤ n ≤ C 2k+1
or, k ∈ θ(log2n)

You might also like