Module 5
Module 5
GRAPHS
Abstract data type
• The vertices are sometimes also referred to as nodes and the edges are
lines or arcs that connect any two nodes in the graph.
Indegree of a Vertex
It is the number of arcs entering the vertex. For example, let us consider the above graph. Indegree of
vertex B is 1.
Outdegree of Vertex
It is the number of arcs leaving the vertex. For example, let us consider the above graph. Outdegree of
vertex B is 3.
Graph representation
• Connected components
• Spanning trees
• Biconnected components
DFS(Depth First Search)
• Visit all the vertices up to the deepest level and continue.
• DFS uses stack for its implementation.
Procedure
1.Select any vertex as starting vertex and insert it on to the top of stack and
make status as visited.
2.While stack is not empty we have to perform the following 2 steps
i)select any unvisited adjacent vertex of the corresponding top vertex and
push into on the stack &make the status as visited.
ii)if there is no unvisited vertex then perform backtracking and then pop
element from the stack.
void dfs(int source) {
int v, top = -1;
s[++top] = 1;
b[source] = 1;
for (v = 1; v <= n; v++)
{ if (a[source][v] == 1 && b[v] ==
0)
{
printf("\n%d -> %d", source, v);
dfs(v); }
DFS
Algorithm
1.Push starting vertex & print
2.while(stack not empty)
{
i.P=TOP()
ii.push(print)only one adjacent vertex of P
If no valid vertex available,POP()
}
Breadth first search(BFS)
a.delete a vertex from the queue & display it(we are visiting that vertex).
b. Insert all the unvisited adjacent vertices of that vertex(deleted vertex) into the
printf("\n%d", i); } } }}
AVL Trees
• AVL trees was developed to overcome the drawbacks of binary search
trees.
• In case of left skewed BST and right skewed BST to search an element
it takes more amount of time.
• If there are 5 nodes than 5 comparisons needed.
• AVL tree is known as high balanced tree.
• Height of AVL tree is O(logn).
• Balance Factor (k) = height (left(k)) - height (right(k))
AVL TREES
• AVL Tree is invented by GM Adelson - Velsky and EM
Landis in 1962. The tree is named AVL in honor of its
inventors.
• AVL Tree can be defined as height balanced binary
search tree in which each node is associated with a
balance factor which is calculated by subtracting the
height of its right sub-tree from that of its left sub-tree.
• Tree is said to be balanced if balance factor of each
node is in between -1 to 1, otherwise, the tree will be
unbalanced and need to be balanced.
• Balance Factor (k) = height (left(k)) - height (right(k))
AVL TREES(Contd..)
• If balance factor of any node is 1, it means that the left
sub-tree is one level higher than the right sub-tree.
• If balance factor of any node is 0, it means that the left
sub-tree and right sub-tree contain equal height.
• If balance factor of any node is -1, it means that the left
sub-tree is one level lower than the right sub-tree.
• An AVL tree is given in the following figure. We can see
that, balance factor associated with each node is in
between -1 and +1. therefore, it is an example of AVL
tree.
• We have to calculate balance factor of each node.
• Balance factor=height of LST-height of RST.
• If a balance factor of a node is -1,0,+1 that that node is balanced.
• If we get other than these than it is unbalanced.
• In order to make unbalanced to balanced we need to perform some
rotations on the tree
• Balance Factor (k) = height (left(k)) - height (right(k))
AVL Rotations
In above example, node A has balance factor -2 because a node C is inserted in the
right subtree of A right subtree. We perform the RR rotation on the edge below A.
2. LL Rotation
In above example, node C has balance factor 2 because a node A is inserted in the left
subtree of C left subtree. We perform the LL rotation on the edge below A.
3. LR Rotation
• Double rotations are bit tougher than single rotation which has already
explained above. LR rotation = RR rotation + LL rotation, i.e., first
RR rotation is performed on subtree and then LL rotation is performed
on full tree, by full tree we mean the first node from the path of
inserted node whose balance factor is other than -1, 0, or 1.
RL Rotation
• In all the paths of the tree ,there should be same number of BLACK
coloured nodes.
• In a Red-Black Tree, every new node must be inserted with the color
RED. The insertion operation in Red Black Tree is similar to insertion
operation in Binary Search Tree. But it is inserted with a color
property. After every insertion operation, we need to check all the
properties of Red-Black Tree. If all the properties are satisfied then we
go to next operation otherwise we perform the following operation to
make it Red Black Tree.
• 1. Recolor
• 2. Rotation
• 3. Rotation followed by Recolor
The insertion operation in Red Black tree
• The Red-Black tree is used because the AVL tree requires many rotations
when the tree is large, whereas the Red-Black tree requires a maximum of
two rotations to balance the tree.
• The main difference between the AVL tree and the Red-Black tree is that
the AVL tree is strictly balanced, while the Red-Black tree is not
completely height-balanced. So, the AVL tree is more balanced than the
Red-Black tree.
B-Trees
• What are B-trees?
• B-tree is a self balancing search tree(like AVL and Red-Black trees),it
• is assumed that everything is in memory.
• B trees are used when the entire data cannot be stored in memory.
• Disk access time is very high compared to main memory access time.
• The main idea of using B-tree is to reduce the number of disk
accesses.
• B-tree is a special type of self-balancing search tree in
which each node can contain more than one key and
can have more than two children. It is a generalized
form of the binary search tree.
Properties of B trees
• All B-tree of order m can have at most m-1 keys and m children.