Notes Dsa
Notes Dsa
Notes Dsa
com
TOTAL : 45 PERIODS
TEXT BOOKS
1. M. A. Weiss, “Data Structures and Algorithm Analysis in C”, Pearson Education Asia, 2002.
2. ISRD Group, “Data Structures using C”, Tata McGraw-Hill Publishing Company Ltd., 2006.
REFERENCES
1. A. V. Aho, J. E. Hopcroft, and J. D. Ullman, “Data Structures and Algorithms”, Pearson Education, 1983.
2. R. F. Gilberg, B. A. Forouzan, “Data Structures: A Pseudocode approach with C”, Second Edition, Thomson
India Edition, 2005.
3. Sara Baase and A. Van Gelder, “Computer Algorithms”, Third Edition, Pearson Education, 2000.
4. T. H. Cormen, C. E. Leiserson, R. L. Rivest, and C. Stein, "Introduction to
algorithms", Second Edition, Prentice Hall of India Ltd, 2001.
=======================================================================
@ Einstein College Of Engineering [1/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [2/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [3/159]
www.vidyarthiplus.com
www.eeeuniversity.com
Graph Best models real-world situations Some algorithms are slow and very
complex
Examples
• Associative array
• Set
• Stack
• Queue
• Tree
=======================================================================
@ Einstein College Of Engineering [4/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [5/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [6/159]
www.vidyarthiplus.com
www.eeeuniversity.com
6
example for linear data structure.
into “b” with the a[2]
statement
An array lets you declare and work b=6; a[3]
with a collection of values of the same
type (homogeneous). For example, you You place a value
might want to create a collection of five into “a” with the
statement like:
integers. One way to do it would be to
a[2]=6;
declare five integers directly: array index
int a, b, c, d, e;
Suppose you need to fined average of 100 numbers. What will you do? You have to declare 100
variables. For example:
int a, b, c, d, e, f, g, h, i, j, k, l, m, n... etc.,
An easier way is to declare an array of 100 integers:
int a[100];
The General Syntax is:
datatype array_name [size];
Example: Subscript
int a[5];
=======================================================================
@ Einstein College Of Engineering [7/159]
www.vidyarthiplus.com
www.eeeuniversity.com
/* print array */
printf("Elements in the array are…\n");
for (i=0; i < 5; i++)
printf("%d\n",a[i]);
Note : (mathematics) A matrix most of whose entries are zeros.
=======================================================================
@ Einstein College Of Engineering [8/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [9/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [10/159]
www.vidyarthiplus.com
www.eeeuniversity.com
struct student
{ int regno;
char name[20];
char dept[10];
int year;
};
s1 s2 sN
The size of each of these variables is 34 bytes because the size of the student datatype is 34
bytes. And the memory will be allocated for each variable as follows:
34 bytes
s1
2 bytes 20 bytes 10 bytes 2 bytes
regno name dept year
s2
=======================================================================
@ Einstein College Of Engineering [11/159]
www.vidyarthiplus.com
www.eeeuniversity.com
name array (size – 20 bytes) mark array (size – 10 bytes) result (size – 5 bytes)
2190 2192 2212 2214 2216 2218 2220 2222 2224 2228
=======================================================================
@ Einstein College Of Engineering [12/159]
www.vidyarthiplus.com
www.eeeuniversity.com
b1
cno name amt day month year day month year
This can be This can be
accessed by b1.cno accessed by This can be
b1.billdate.day accessed by
b1.paydate.year
PROCESSING STRUCTURES:
Consider the following structure:
struct student
{
int regno;
char name[20];
char dept[10];
struct date
=======================================================================
@ Einstein College Of Engineering [13/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [14/159]
www.vidyarthiplus.com
www.eeeuniversity.com
1008
Address of the
Address of the pointer variable
sptr 6100 structure variable sptr. To access this location
stu1
using structure pointer
2 bytes variable (sptr),
sptr->dept
Access to members of the structure is shown below: should be used
printf(“Student Registration Number : %d\n”, sptr->regno);
printf(“Student Name : %s\n”, sptr->name);
printf(“Department Name : %s\n”, sptr->dept);
printf(“Year of Study : %d\n”, sptr->year);
1.4.2 STACK :
“A stack is an ordered list in which all insertions and deletions are made at one end, called
the top”. stacks are sometimes referred to as Last In First Out (LIFO) lists
Stacks have some useful terminology associated with them:
• Push To add an element to the stack
• Pop To remove an element from the stock
• Peek To look at elements in the stack without removing them
• LIFO Refers to the last in, first out behavior of the stack
• FILO Equivalent to LIFO
STACK (DATA STRUCTURE)
=======================================================================
@ Einstein College Of Engineering [15/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [16/159]
www.vidyarthiplus.com
www.eeeuniversity.com
PUSH top 6
operation
top 8 8
4 4
2. Deleting an element from a stack. ( called POP operations )
Deleting or Removing element from the TOP of the stack is called POP operations.
Check Condition:
top 6 POP
8 operation
top 8
4
4
Stack
Implementation in C using array: Stack
/* here, the variables stack, and top are global variables */
int pop ( )
{
if (top == -1)
{
printf(“Stack is Underflow”);
=======================================================================
@ Einstein College Of Engineering [17/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [18/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [19/159]
www.vidyarthiplus.com
www.eeeuniversity.com
(a + b) * (c - d) *+ab-cd ab+cd-*
b*b-4*a*c
40 - 3 * 5 + 1
=======================================================================
@ Einstein College Of Engineering [20/159]
www.vidyarthiplus.com
www.eeeuniversity.com
Algorithm postfixexpression
Initialize a stack, opndstk to be empty.
{
scan the input string reading one element at a time into symb
}
While ( not end of input string )
{
Symb := next input character;
If symb is an operand Then
push (opndstk,symb)
Else
[symbol is an operator]
{
Opnd1:=pop(opndstk);
Opnd2:=pop(opndnstk);
Value := result of applying symb to opnd1 & opnd2
Push(opndstk,value);
}
Result := pop (opndstk);
Example:
623+-382/+*2$3+
Symbol Operand 1 (A) Operand 2 (B) Value (A ⊗ B) STACK
6 6
=======================================================================
@ Einstein College Of Engineering [21/159]
www.vidyarthiplus.com
www.eeeuniversity.com
(2) run time stack for function calls ( write factorial number calculation procedure)
push local data and return address onto stack
return by popping off local data and then popping off address and returning to it
return value can be pushed onto stack before returning, popped off by caller
=======================================================================
@ Einstein College Of Engineering [22/159]
www.vidyarthiplus.com
www.eeeuniversity.com
1.4.3 QUEUE :
“A queue is an ordered list in which all insertions at one end called REAR and deletions are
made at another end called FRONT”. queues are sometimes referred to as First In First Out
(FIFO) lists. enqueue
(Insertion)
=======================================================================
@ Einstein College Of Engineering [23/159]
dequeue Rear
Front
(Deletion)
www.vidyarthiplus.com
www.eeeuniversity.com
Example
1. The people waiting in line at a bank cash counter form a queue.
2. In computer, the jobs waiting in line to use the processor for execution. This queue is
called Job Queue.
Operations Of Queue
There are two basic queue operations. They are,
Enqueue – Inserts an item / element at the rear end of the queue. An error occurs if the queue is
full.
Dequeue – Removes an item / element from the front end of the queue, and returns it to the
user. An error occurs if the queue is empty.
2. Deletion in a queue
=======================================================================
@ Einstein College Of Engineering [24/159]
www.vidyarthiplus.com
www.eeeuniversity.com
Circular Queue :
Location of queue are viewed in a circular form. The first location is viewed after the last
one.
Overflow occurs when all the locations are filled.
rear
front
Algorithm Circular Queue Insert
Void CQInsert ( int queue[ ], front, rear, item)
{
if ( front = = 0 )
front = front +1;
if ( ( ( rear = maxsize ) && ( front = = 1 ) ) || ( ( rear ! = 0 ) && ( front = rear +1)))
{
printf( “ queue overflow “);
if( rear = = maxsize )
rear = 1;
else
rear = rear + 1;
q [ rear ] = item;
=======================================================================
@ Einstein College Of Engineering [25/159]
www.vidyarthiplus.com
www.eeeuniversity.com
www.vidyarthiplus.com
www.eeeuniversity.com
The linked list consists of series of nodes, which are not necessarily adjacent in memory.
A list is a dynamic data structure i.e. the number of nodes on a list may vary
dramatically as elements are inserted and removed.
The pointer of the last node contains a special value, called the null pointer, which is any
invalid address. This null pointer signals the end of list.
The list with no nodes on it is called the empty list or null list.
Example: The linked list with 4 nodes.
START
OR 7 5 8 9
HEAD
=======================================================================
@ Einstein College Of Engineering [27/159]
www.vidyarthiplus.com
www.eeeuniversity.com
Circularly-linked list:
In a circularly-linked list, the first and final nodes are linked together. This can be done
for both singly and doubly linked lists. To traverse a circular linked list, you begin at any node and
follow the list in either direction until you return to the original node. Viewed another way,
circularly-linked lists can be seen as having no beginning or end. This type of list is most useful for
managing buffers for data ingest, and in cases where you have one object in a list and wish to see
all other objects in the list.
The pointer pointing to the whole list is usually called the end pointer.
Singly-circularly-linked list:
In a singly-circularly-linked list, each node has one link, similar to an ordinary singly-
linked list, except that the next link of the last node points back to the first node.
As in a singly-linked list, new nodes can only be efficiently inserted after a node we already
have a reference to. For this reason, it's usual to retain a reference to only the last element in a
singly-circularly-linked list, as this allows quick insertion at the beginning, and also allows access
to the first node through the last node's next pointer.
=======================================================================
@ Einstein College Of Engineering [28/159]
www.vidyarthiplus.com
www.eeeuniversity.com
Doubly-circularly-linked list
Sentinel nodes
Linked lists sometimes have a special dummy or sentinel node at the beginning and/or at the end
of the list, which is not used to store data.
Basic Operations on Linked Lists
1. Insertion
a. At first
b. At last
c. At a given location (At middle)
2. Deletion
a. First Node
b. Last Node
c. Node in given location or having given data item
Initial Condition
HEAD = NULL;
/* Address of the first node in the list is stored in HEAD. Initially there is no node in the list. So,
HEAD is initialized to NULL (No address) */
What are the Applications of linked list?
To implement of Stack, Queue, Tree, Graph etc.,
Used by the Memory Manager
=======================================================================
@ Einstein College Of Engineering [29/159]
www.vidyarthiplus.com
www.eeeuniversity.com
Example:
LEFT INFO RIGHT
head 7060 2140 4020 end
7060 NULL 7 2140 7060 7 4020 2140 7 NULL 4020
=======================================================================
@ Einstein College Of Engineering [30/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [31/159]
www.vidyarthiplus.com
www.eeeuniversity.com
1. Polynomial ADT:
A polynomial can be represented with primitive data structures. For example, a polynomial
represented as akxk ak-1xk-1 + ... + a0 can be represented as a linked list. Each node is a structure
with two values: ai and i. Thus, the length of the list will be k. The first node will have (ak, k), the
second node will have (ak-1, k-1) etc. The last node will be (a0, 0).
The polynomial 3x9 + 7x3 + 5 can be represented in a list as follows: (3,9) --> (7,3) --> (5,0)
where each pair of integers represent a node, and the arrow represents a link to its neighbouring
node.
Derivatives of polynomials can be easily computed by proceeding node by node. In our
previous example the list after computing the derivative would represented as follows: (27,8) -->
(21,2). The specific polynomial ADT will define various operations, such as multiplication, addition,
subtraction, derivative, integration etc. A polynomial ADT can be useful for symbolic computation
as well.
Large integers can also be implemented with primitive data structures. To conform to our
previous example, consider a large integer represented as a linked list. If we represent the integer
as successive powers of 10, where the power of 10 increments by 3 and the coefficient is a three
digit number, we can make computations on such numbers easier. For example, we can represent
a very large number as follows:
513(106) + 899(103) + 722(100).
Using this notation, the number can be represented as follows:
(513) --> (899) --> (722).
The first number represents the coefficient of the 106 term, the next number represents the
coefficient of the 103 term and so on. The arrows represent links to adjacent nodes.
The specific ADT will define operations on this representation, such as addition, subtraction,
multiplication, division, comparison, copy etc.
An array allocates memory for all its elements lumped together as one block of memory. In
contrast, a linked list allocates space for each element separately in its own block of memory
called a "linked list element" or "node". The list gets is overall structure by using pointers to
connect all its nodes together like the links in a chain.
Each node contains two fields: a "data" field to store whatever element type the list holds for its
client, and a "next" field which is a pointer used to link one node to the next node.
=======================================================================
@ Einstein College Of Engineering [32/159]
www.vidyarthiplus.com
www.eeeuniversity.com
Two Marks
1. Limitations of arrays
e) Arrays have a fixed dimension. Once the size of an array is decided it cannot be increased
or decreased during execution.
f) Array elements are always stored in contiguous memory locations. So it need contiguous
locations otherwise memory will not be allocated to the arrays
=======================================================================
@ Einstein College Of Engineering [33/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [34/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [35/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [36/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [37/159]
www.vidyarthiplus.com
www.eeeuniversity.com
A simple tree.
Root
Level 0 Branch
A
Internal Nodes
Level 1
B F G
Level 2 Leaf
=======================================================================
C H I J
@ Einstein College Of Engineering [38/159]
Level 3
D E Siblings
www.vidyarthiplus.com
www.eeeuniversity.com
Parents : A, B, C, G
Children : B, F, G, C, H, I, J, D, E
Siblings : { B, F, G }, { D, E }, { H, I, J }
Leaves : F, D, E, H, I, J
Length : 4
A path is a sequence of nodes in which each node is adjacent to the next one.
Every node in the tree can be reached by following a unique path starting from the root.
The level of a node is its distance from the root. Because the root has a zero distance
from itself, the root is at level 0. The children of the root are at the level 1.
The height or length of the tree is the level of the leaf in the longest path from the root
plus 1. By definition, the height of an empty tree is -1.
A tree may be divided into subtrees. A subtree is any connected structure below the
root.
The first node in a subtree is known as the root of the subtree and is used to name the
subtree.
Binary Trees
A binary tree is a tree in which no node can have more than two subtrees.
These subtrees are designated as the left subtree and right subtree.
Each subtree is a binary tree itself.
A
B C
The height of the binary trees can be mathematically predicted. The maximum height
of the binary tree which has N nodes, Hmax = N
A tree with a maximum height is rare. It occurs when the entire tree is built in one
direction. The minimum height of the tree, Hmin is determined by,
Hmin = ⎣ Log2 N ⎦ + 1
=======================================================================
@ Einstein College Of Engineering [39/159]
www.vidyarthiplus.com
www.eeeuniversity.com
A
A A
B C
B C
D E F G
Complete Trees (at levels 0, 1, and 2)
A tree is considered nearly complete if it has the minimum height for its nodes and all
nodes in the last level are found on the left.
A A A
B C B C B C
D D E D E F
=======================================================================
@ Einstein College Of Engineering [40/159]
www.vidyarthiplus.com
www.eeeuniversity.com
Level 2
Level 3
A very simple representation for such binary tree results from sequentially numbering the
nodes, starting with nodes on level 1 then those on level 2 and so on. Nodes on any level are
numbered from left to right as shown in the above picture. This numbering scheme gives us the
definition of a complete binary tree. A binary tree with n nodes and of depth K is complete if its
nodes correspond to the nodes which are numbered one to n in the full binary tree of depth K.
Array Representation:
Each node contains info, left, right and father fields. The left, right and father fields of a
node point to the node’s left son, right son and father respectively.
Using the array implementation, we may declare
#define NUMNODES 100
struct nodetype
{
int info;
int left;
int right;
int father;
};
struct nodetype node[NUMNODES];
This representation is called linked array representation.
Under this representation,
info(p) would be implemented by reference node[p].info,
left(p) would be implemented by reference node[p].left,
right(p) would be implemented by reference node[p].right,
father(p) would be implemented by reference node[p].father respectively.
The operations,
isleft(p) can be implemented in terms of the operation left(p)
isright(p) can be implemented in terms of the operation right(p)
Example: -
=======================================================================
@ Einstein College Of Engineering [41/159]
www.vidyarthiplus.com
www.eeeuniversity.com
E
The above representation appears to be good for complete binary trees and
wasteful for many other binary trees. In addition, the insertion or deletion of nodes from the
middle of a tree requires the insertion of many nodes to reflect the change in level number of
these nodes.
Linked Representation: -
The problems of sequential representation can be easily overcome through the use of a
linked representation. Each node will have three fields LCHILD, DATA and RCHILD as represented
below
=======================================================================
@ Einstein College Of Engineering [42/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [43/159]
www.vidyarthiplus.com
www.eeeuniversity.com
B F G
C H I J
=======================================================================
@ Einstein College Of Engineering B [44/159]
E F
C G H I
www.vidyarthiplus.com
www.eeeuniversity.com
B E F B E F
C D G H I C D G H I
B
B E F
C E
C D G H I
Step 3: Delete unneeded branches D F
a) Depth-First Traversal
=======================================================================
@ Einstein College Of Engineering [45/159]
www.vidyarthiplus.com
www.eeeuniversity.com
B E
=======================================================================
@ Einstein College Of Engineering [46/159]
C D F
www.vidyarthiplus.com
www.eeeuniversity.com
The Output is : C Æ B Æ D ÆA ÆE Æ F
2. Preorder Traversal
Steps : 1. Process root node
2. Traverse left subtree in preorder
3. Traverse right subtree in preorder
Algorithm C function
void preorder_traversal ( NODE * T)
{
Algorithm preorder traversal (Bin-Tree T) if( T ! = NULL)
{
Begin printf(“%d \t”, T->info);
If ( not empty (T) ) then preorder_traversal(T->lchild);
Begin preorder_traversal(T->rchild);
Print ( info ( T ) ) / * process node * / }
Preoder traversal (left subtree ( T ) ) }
Inorder traversal ( right subtree ( T ) )
End
End
A
B E
C D F
Output is : A ÆB Æ C Æ D Æ E Æ F
3. Postorder Traversal
Steps : 1. Traverse left subtree in postorder
2. Traverse right subtree in postorder
3. process root node
Algorithm C function
=======================================================================
@ Einstein College Of Engineering [47/159]
www.vidyarthiplus.com
www.eeeuniversity.com
B E
C D F
The Output is : C Æ D Æ B Æ F Æ E Æ A
Non – Recursive algorithm: Inorder_Traversal
#define MAXSTACK 100
inorder_traversal (tree)
NODEPTR tree;
{
struct stack
{
int top;
NODEPTR item[MAXSTACK];
}s;
NODEPTR p;
s.top = -1;
p = tree;
do
{/* travel down left branches as far as possible, saving
=======================================================================
@ Einstein College Of Engineering [48/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [49/159]
www.vidyarthiplus.com
www.eeeuniversity.com
Applications of Binary Trees ( find the duplication element from the list)
Binary tree is useful data structure when two-way decisions must be made at each point in
a process. For example find the all duplicates in a list of numbers. One way doing this is each
number compare with it’s precede it. However, it involves a large number of comparisons.
The number of comparisons can be reduced by using a binary tree.
Step 1: from root, each successive number in the list is then compared to
the number in the root.
Step 2: If it matches, we have a duplicate.
Step 3: If it is smaller, we examine the left subtree.
=======================================================================
@ Einstein College Of Engineering [50/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [51/159]
www.vidyarthiplus.com
www.eeeuniversity.com
For convince the above binary search tree if it is traversed inorder manner the result order is,
30, 46, 50, 58, 60, 70, 77 and 80 is ascending order sequence.
=======================================================================
@ Einstein College Of Engineering [52/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [53/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [54/159]
www.vidyarthiplus.com
www.eeeuniversity.com
if ( TÆlchild = = NULL)
return tree;
else
return FindMin(TreeÆlchild);
}
Finding Maximum Element in a BST
Maximum element lies as the right most node in the right most branch starting from the
root. To reach the node with maximum value, we need to traverse the tree from root along the
right branch till we get a node with a null / empty right subtree.
Algorithm FindMax(NODE * T)
1. If Tree is null then
return NULL;
2. if rchild(Tree) is null then
return tree
else
return FindMax(TÆrchild)
=======================================================================
@ Einstein College Of Engineering [55/159]
www.vidyarthiplus.com
www.eeeuniversity.com
if ( TÆrchild = = NULL)
return tree;
else
return FindMax(TreeÆrchild);
}
=======================================================================
@ Einstein College Of Engineering [56/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [57/159]
www.vidyarthiplus.com
www.eeeuniversity.com
a *
b c
a b
Step 2:
Next symbol is read, i.e ‘+’ operator. Now the symbol is operator, so two previous pointers to trees
in the stack are popped, and a new tree is formed and its pointer is pushed on to the stack. The popped
pointers are placed as left child and right child in the new tree, as a result two subtrees are merged.
a b
=======================================================================
@ Einstein College Of Engineering [58/159]
www.vidyarthiplus.com
www.eeeuniversity.com
+
d e
c
a b
Step 4:
Next symbol is read, i.e ‘+’ operator. Now the symbol is operator, so two previous pointers to trees
in the stack are popped, and a new tree is formed and its pointer is pushed on to the stack. The popped
pointers are placed as left child and right child in the new tree, as a result two subtrees are merged.
+
+
c
a b d e
Step 5:
Next symbol is read, i.e ‘*’ operator. The symbol is operator, so two previous pointers to trees in the stack
are popped, and a new tree is formed and its pointer is pushed on to the stack. The popped pointers are
placed as left child and right child in the new tree, as a result two subtrees are merged.
*
+
+
a b c
d e
=======================================================================
@ Einstein College Of Engineering [59/159]
www.vidyarthiplus.com
www.eeeuniversity.com
*
+
+
c
a b
e
d
Atlast we made an expression tree. In this tree we can make in-order , pre-order and post- order traversal
like a binary tree.
=======================================================================
@ Einstein College Of Engineering [60/159]
www.vidyarthiplus.com
www.eeeuniversity.com
2.3.3 Implementation of Expression tree and make in-order, pre-order and post-order traversal
#include<stdio.h>
struct tree
{
char info;
struct tree *rchild;
struct tree *lchild;
};
char pop_op();
node pop_num();
void push_op(char item);
node create()
{
return((node)malloc(sizeof(node)));
}
node num[20],root=NULL;
char op[20],oprt,ev[20];
int nt=-1,ot=-1,et=-1;
main()
{
node newnode,item,temp;
char str[50];
int i,k,p,s,flag=0;
printf("ENTER THE EXPRESSION ");
scanf("%s",str);
printf("\n%s",str);
for(i=0;str[i]!='\0';i++)
{
if(isalnum(str[i]))
{
newnode=create();
newnode->info=str[i];
newnode->lchild=NULL;
newnode->rchild=NULL;
item=newnode;
push_num(item);
}
=======================================================================
@ Einstein College Of Engineering [61/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [62/159]
www.vidyarthiplus.com
www.eeeuniversity.com
push_num(node item)
{
num[++nt]=item;
}
char pop_op()
{
if(ot!=-1)
return(op[ot--]);
else
return(0);
}
node pop_num()
{
if(nt!=-1)
return(num[nt--]);
else
return(NULL);
}
inorder(node temp)
{
if(temp!=NULL)
{
inorder(temp->lchild);
printf("%c ",temp->info);
inorder(temp->rchild);
}
}
preorder(node temp)
{
if(temp!=NULL)
{
printf("%c ",temp->info);
preorder(temp->lchild);
preorder(temp->rchild);
}
}
=======================================================================
@ Einstein College Of Engineering [63/159]
www.vidyarthiplus.com
www.eeeuniversity.com
postorder(node temp)
{
if(temp!=NULL)
{
postorder(temp->lchild);
postorder(temp->rchild);
printf("%c ",temp->info);
ev[++et]=temp->info;
}
}
evaluate()
{
int i,j=-1,a,b,ch[20];
for(i=0;ev[i]!='\0';i++)
{
if(isalnum(ev[i]))
ch[++j]=ev[i]-48;
else
{
b=ch[j];
a=ch[j-1];
switch(ev[i])
{
case '+':ch[--j]=a+b;
break;
case '-':ch[--j]=a-b;
break;
case '*':ch[--j]=a*b;
break;
case '/':ch[--j]=a/b;
break;
}
}
}
printf("\nValue = %d",ch[0]);
}
=======================================================================
@ Einstein College Of Engineering [64/159]
www.vidyarthiplus.com
www.eeeuniversity.com
UNIT III
BALANCED SEARCH TREES&INDEXING
Height- Balanced Tree: An empty tree is height-balanced if T is a non empty binary tree with TL and TR as
its left and eight sub trees respectively, then T is a height –balanced if
1. TL and TR are height-balanced
2. | hL – hR | ≤ 1 where hL and hR are the heights of TL and TR respectively.
The definition of a height balanced binary tree requires that every sub tree also be height balanced. The
balance factor of the nodes decides if the tree is balanced or not.
Balance Factor: The balance factor BF(T) of a node T is a binary tree is defined to be
B.F = hL – hR
where hL and hR respectively are the heights of the left and right sub trees of T.
For any node T is an AVL tree BF(T) = -1, 0, or 1. If it is not in among these 3 values then the tree
has to be rebalanced by performing the appropriate rotation.
BF = 1 BF = -1
10 8
(1-0 = 1) (1-2 = -1)
BF = 0 6 14 BF = 1
8
BF = 0 BF = 0 10
(-1-(-1) = 0)
3.1.1 Insertion of a node:
We can insert a new node into an AVL tree by first using binary tree insertion algorithm, comparing
the key of the new node with that in the root, and inserting the new node into the left or right sub tree as
appropriate.
=======================================================================
@ Einstein College Of Engineering [65/159]
www.vidyarthiplus.com
www.eeeuniversity.com
3.1.2 Rotations:
When a new node has been inserted into the taller subtree of the root and its height has increased,
so that now one subtree has height 2 more than the other, and the tree no longer satisfies the AVL
requirements. We must now rebuild part of the tree to restore its balance.
The rebalancing can be carried out using four different kinds of rotations: LL, RR, LR & RL. LL and
RR are symmetric as are LR and RL. There rotations are characterized by the nearest ancestor, A, of the
inserted node, Y, whose balance factor become ± 2. The following rotations type is obtained:
LL: New node Y is inserted in the Left Subtree of the Left Subtree of A Æ Single Rotation with left.
LR: Y is inserted in the right subtree of the Left Subtree of A Æ Double Rotation with Left
RR: Y is inserted in the right subtree of the right subtree of A Æ Single Rotation with Right
RL: Y is inserted in the left subtree of the right subtree of A Æ Double Rotation with Right
Single rotation:
1. Single Rotation with left. - Left Rotation(Left of Left)
¾ The left child (K1) of the root (K2) is promoted as root node.
¾ The root (K2) is promoted as the right child of the new root(K1).
¾ The right child of the K1 is converted to the left child of K2
K2 K1
Æ
K1 Z K2
X
Y Y
X Z
Before Rotation After Rotation
Routine to perform single rotation with left
i. This function can be called only if K2 has a left child.
ii. Perform a rotation between node (K2) and its left child (K1)
iii. Update height, then return new root.
static Position SingleRotateWithLeft (Position K2)
{
Position K1;
K1=K2Æleft;
K2ÆLeft = K1ÆRight;
K1ÆRight =K2;
K2Æheight= Max (height(K2Æleft), height (K2ÆRight))+1;
K1Æheight= Max (height(K1Æleft), (K2Æheight))+1;
=======================================================================
@ Einstein College Of Engineering [66/159]
www.vidyarthiplus.com
www.eeeuniversity.com
8 BF=1 8 BF=2
BF=0 5 BF=1 5
10 BF=0 10 BF=0
BF=1 3 8 BF=0
BF=0 1
BF=0 7 10 BF=0
After Left Rotation
K2
K1
Æ
K2 K1
X
Z
Y X Y
Z
Before Rotation After Rotation
=======================================================================
@ Einstein College Of Engineering [67/159]
www.vidyarthiplus.com
www.eeeuniversity.com
BF=0 4 10 BF=0
BF=0 8 11 BF=-1
BF=0 8 11 BF=0
Balanced Tree 10
Now insert the value ‘13’ it becomes unbalanced
due to the insertion of new node in the Right Subtree Before Right Rotation
of the Right Subtree. So we have to make single Right rotation
in the root node.
BF=0 10
BF=1 6 11 BF=-1
BF=0 4 13
8 BF=0
BF=0
After Right Rotation
Double Rotation:
1. Double Rotation with Left (Insertion in the Right Subtree of the Left Subtree Æ Left-Right Rotation)
K3 K3 K2
K1 K2
D Æ
D
Æ
K1 K3
A K2 K1
C
A B C D
B C A B
Before Rotation After Right Rotation After Left Rotation
=======================================================================
@ Einstein College Of Engineering [68/159]
www.vidyarthiplus.com
www.eeeuniversity.com
20 BF=1 BF=-1 10
30 BF=0
BF=0 10
30 BF=0 BF=0 5 15 BF=0
BF=0 5 15 BF=0
BF=0 12 18 BF=0
Balanced Tree
Now insert the value ‘12’& ‘18’ the tree becomes unbalanced Before Right Rotation
due to the insertion of the new node in the Right Subtree
of the Left Subtree. So we have to make double rotation
first with right and then with Left.
20 BF=2 BF=0
15
BF=1 15
30 BF=0
BF=0
10 20 BF=0
BF=0 10 18 BF=0
BF=0 5 12 18 30
5 BF=0 12 BF=0
After Right Rotation & Before Left Rotation After Left Rotation
2. Double Rotation with Right(Insertion in the Left Subtree of the Right SubtreeÆRight-Left Rotation)
Routine to perform double rotation with right and then left
i. Apply single Left rotation at K3, as a result K2 becomes the right child of the root node K1 and K3
becomes the right child of K2.
ii. Apply single Right rotation at the root node K1, as a result K2 becomes the root node and K1
becomes the left child of it and K3 becomes right child of it. Now the tree is balanced.
=======================================================================
@ Einstein College Of Engineering [69/159]
www.vidyarthiplus.com
www.eeeuniversity.com
BF=0 8 15 BF=1
BF=0 8 15 BF=0
BF=0 12 17 BF=0
BF=0 12 17 BF=0
BF=-2 BF=0 12
10
BF=0
8 12 BF=-1 BF=0 10
Æ 15 BF=0
BF=0 11 15 BF=0
BF=0 8 11 14 17 BF = 0
BF=0 14 17 BF=0
After Left Rotation & Before Right Rotation After Right Rotation
=======================================================================
@ Einstein College Of Engineering [70/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [71/159]
www.vidyarthiplus.com
www.eeeuniversity.com
20 BF = 0 20 BF = 1 20 BF = 1
10 BF = 0 BF = 0 10 40 BF = 0
Step 4:(Insert the value 50) Step 5: (Insert the value 90)
20 BF =-1
90 BF = 0
BF = 0 10 50 BF =0
Now the tree is Balanced
BF =0 40 90 BF =0
=======================================================================
@ Einstein College Of Engineering [72/159]
www.vidyarthiplus.com
www.eeeuniversity.com
30 BF = 0
After Left Rotation: After Right Rotation:
40 BF =0
20 BF =-2
BF = 0 20 50 BF =-1
BF = 0 10 40 BF =-1
10 30 90
BF =0 30 50 BF =-1
Now the tree is Balanced
Step 7: (Insert the value 60) 90 BF = 0 Now the tree at the node ‘50’is unbalanced
40 BF =-1 due to the insertion of node ‘60’ in the Left
subtree of the Right subtree. So we have to
60 BF = 0 40 BF =0
After Left Rotation:
40 BF =-1
BF = 0 20 60 BF =0
BF = 0 20 50 BF =-2
10 30 50 90
90 BF = 0 40 BF =0
10 30 50 90
=======================================================================
@ Einstein College Of Engineering [73/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [74/159]
www.vidyarthiplus.com
www.eeeuniversity.com
12 18 32 46 64 82
1 12 18 23 32 46 64 82
2 15 20 28 38 50 65 83
10 30 84
Note:
¾ Leaf node must have at least l/2 objects
22:-
16:- 41:58
16:- 41:58
=======================================================================
@ Einstein College Of Engineering [75/159]
www.vidyarthiplus.com
www.eeeuniversity.com
22:-
16:- 41:58
16:- 41:58
P
16:22
Here the internal node 41:58 has 4 children it violates the rule. So we have to spit it into 2 children.
=======================================================================
@ Einstein College Of Engineering [76/159]
www.vidyarthiplus.com
www.eeeuniversity.com
16:22
16:- 41:-
B-Tree Deletion:
Example: 2-3 tree with l=1 25 -
4 10 40 -
2 3 5 - 11 12 30 - 50 -
P
1To Delete
2 3‘2’: 4 5 10 11 12 25 30 40 50
Since P has 3 children’s(leaf nodes), ‘2’ can be simply deleted and P be left with just 2 children’s.
2 3 3 -
Deletion
1 2 3 1 3
To Delete ‘5’:
5 - ? -
Deletion
4 5 4
When the node has only one child, borrow from left or right sibling, without violating the condition.
Left sibling has least value and the right sibling has greatest value.
Therefore add ‘4’ to the right sibling.
=======================================================================
@ Einstein College Of Engineering [77/159]
www.vidyarthiplus.com
www.eeeuniversity.com
25 -
4 11 40 -
3 - P 10 - 12 - 30 - 50 -
1 3 4 10 11 12 25 30 40 50
To Delete ‘10’:
P can’t borrow from any of its sibling. So, move the child to left sibling and delete P.
25 -
11 - 40 -
3 4 12 - 30 - 50 -
1 3 4 11 12 25 30 40 50
To Delete ‘30’:
? -
This is invalid
25
When the node has only one child, borrow from left or right sibling, without violating the condition.
Left sibling has least value and the right sibling has greatest value.
Therefore add ‘25’ to the right sibling.
40 50
This is also invalid because the root having only one child. So
borrow from left or right sibling without violating the rule.
25 40 50
So remove the root and make the grant parent as new root.
25 -
3 4 12 - 50 -
1 40 4 11 12 40 40 50
Algorithm for Searching an element
Step1: Get the searching element x
Step2: Compare x with key of root node
Step3: Find the subtree
=======================================================================
@ Einstein College Of Engineering [78/159]
www.vidyarthiplus.com
www.eeeuniversity.com
Deltemin(H) Insert(H)
Priority Queue H
3.3.3 Implementation:
There are three ways for implementing priority queue. They are:
1. Linked list
2. Binary Search tree
3. Binary Heap
Linked list:
A simple linked list implementation of priority queue requires O(1) time to perform the insertion at
the front and O(N) time to delete the minimum element.
Binary Search Tree:
This implementation gives an average running time of O(log N) for both insertion and deletemin
operations.
=======================================================================
@ Einstein College Of Engineering [79/159]
www.vidyarthiplus.com
www.eeeuniversity.com
40
[0]
20 60
[1] [2]
10 30 50 90
A complete binary tree of height h[3]
has [4] 2h[5]
between and 2h+1 [6]nodes
-1 (i.e if the tree has height h=1, then
number of nodes it have must be 2 or 3). This implies that the height of complete binary tree is log N which
is clearly O(log N).
An array implementation of complete binary tree is as follows:
40 20 60 10 30 50 90
[0] [1] [2] [3] [4] [5] [6]
For any element in array position i:
i) the left child is 2i + 1
ii) the right child is in the cell after the left child 2i+2
iii) the parent is in position [(i+1)/2]
Example:
If i = 0,
i) the parent node is [(i+1)/2] = ½ = 0.5 = app 1 i.e it refers the root node ‘40’
ii) the left child is 2i +1 = (2*0)+1 = 1 i.e it refers the position of the element ‘20’
iii) the right child is 2i+2 = (2*0)+2 = 2 i.e it refers the position of the element ‘60’
If i = 1,
i) the parent node is [(i+1)/2] = 2/2 = 1 i.e it refers the root node ‘20’
ii) the left child is 2i+1 = (2*1)+1 = 3 i.e it refers the position of the element ‘10’
iii) the right child is 2i+2 = (2*1)+2 = 4 i.e it refers the position of the element ‘30’
If i = 2,
i) the parent node is [(i+1)/2] = 3/2 = 1.5 = app 2 i.e it refers the root node ‘60’
ii) the left child is 2i+1 = (2*2)+1 = 5 i.e it refers the position of the element ‘50’
=======================================================================
@ Einstein College Of Engineering [80/159]
www.vidyarthiplus.com
www.eeeuniversity.com
4 30
8 12 20 25
10 20 25 30 4 8 10 12
In a MaxHeap, for every node X, the key in the parent of X is greater than (or equal to) the key in X,
with the exception of the root(which has no parent). [figure 2]
=======================================================================
@ Einstein College Of Engineering [81/159]
www.vidyarthiplus.com
www.eeeuniversity.com
21 16 21 16
----Æ
24 31 19 68 24 31 19 68
65 26 32 65 26 32
Step2: Check the inserting element ‘14’ with the holes parent node ‘31’.
If the inserting element is greater than the holes parent node element, then the element will be
inserted in to the hole.
If the inserting element is lesser than the holes parent node element (14<31), we have to slide
the holes parent node element in to the hole. Thus, the bubbling the hole up towards the root
happen.
13
21 16
24 19 68
65 26 32 32
Step3: Repeat the step 2 check the inserting element ‘14’ with the holes parent node ‘21’.
If the inserting element is greater than the holes parent node element, then the element will be
inserted in to the hole.
If the inserting element is lesser than the holes parent node element (14<21). We have to slide the
holes parent node element in to the hole. Thus the bubbling the hole up towards the root happens.
13
16
24 21 19 68
65 26 32 31
Step4: Inserting the element ‘14’ in to the hole will not validate heap order Property. Therefore you can
insert the element ’14’ in to he hole.
=======================================================================
@ Einstein College Of Engineering [82/159]
www.vidyarthiplus.com
www.eeeuniversity.com
14 16
24 21 19 68
65 26 32 31
This general strategy is known as a perculate up; the new element is perculated up the Heap until
the correct location is found.
Perculation in the Insert routine by performing repeated swaps until the correct order was
established.
The time to do the insertion could be O(log N).
int i;
if (IsFull(H))
{
Error (“priority Queue is full”);
return;
}
for (i=++HÆsize; HÆElement [i/2]>X; i/2)
{
HÆElement[i] = HÆElement[i/2];
}
HÆElement[i]= X;
}
DeleteMin:
Finding the minimum is easy, it is hard to move. When the minimum is removed a hole is created at
the root.
Since the Heap now becomes one smaller, it follows that the last element X in the Heap must move
somewhere in the Heap.
If X can be placed in the hole, then we are done.
We slide the smaller of the hole’s children into the hole, thus pushing the hole down one level.
We repeat this step until X can be placed in the hole.
Finally, Delete the last leaf.
=======================================================================
@ Einstein College Of Engineering [83/159]
www.vidyarthiplus.com
www.eeeuniversity.com
14 16 Delete ‘13’ 14 16
----------------Æ
DeleteMin
24 21 19 68 24 21 19 68
65 26 32 31 65 26 32 31
The last value cannot be placed in the hole, because this would violate heap order.
Therefore, we place the smaller child ‘14’ in the hole, sliding the hole down one level.
14 14
16 21 16
Place ‘21’ into
the hole
24 21 ----------------Æ 24
19 68 19 68
65 26 32 31 65 26 32 31
Now slide the hole down by inserting the value ‘31’ in to the hole.
14 14
21 16 21 16
Delete the last
leaf
24 31 19 68 ----------------Æ 24 31 19 68
65 26 32 65 26 32
=======================================================================
@ Einstein College Of Engineering [84/159]
www.vidyarthiplus.com
www.eeeuniversity.com
3.4 Hashing
Hashing is a key to address transformation technique.
The hash table or hash map is a data structure that associates key with value.
The implementation of hash tables is frequently called hashing.
Hashing is used for faster access of elements and records from the collection of tables and files.
Hashing is a technique used for performing insertions, deletions and search operations in constant
average time of (1).
Hashing is applied where the array size is large and time taken for searching an element is more.
It works by transforming the key using a hash function into a hash, a member which is used to index
into an array to locate the desired location where the values should be.
Hash table supports the efficient addition of new entries and the time spent searching for the
required data is independent of the number of items stored.
3.4.1 Hash table:
In hashing an ideal hash table data structure is nearly an array of fixed size, containing the key.
The key is a string or integer associate with a value. Each key is mapped in to some number in the
range 0 to TableSize -1 and placed in the appropriate cell.
The mapping is called hash function, which should be simple to compute and should ensure that any
two distinct keys get different cells.
0
1
2
3 A
=======================================================================
@ Einstein College Of Engineering [85/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [86/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [87/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [88/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [89/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [90/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [91/159]
www.vidyarthiplus.com
www.eeeuniversity.com
Linear probing:
Probing is the process of a placing in next available empty position in the hash table. The Linear
probing method searches for next suitable position in a linear manner(next by next). Since this
method searches for the empty position in a linear way, this method is referred as linear probing.
In linear probing for the ith probe, the position to be tried is, (h(k) + i) mod Table_Size, where ‘f’ is a
linear function of i, F(i)=i. This amounts to trying cells sequentially in search of an empty cell.
Example for Linear Probing:
Insert the keys 89, 18, 49, 58, 69 into a hash table using in the same hash function as before and
the collision resolving strategies. F(i)=i.
Solution:
In this e.g initially 89 is inserted at index ‘9’. Then 18 is inserted at index 8.
The first collision occurs when 49 is inserted. It is put in the next available index namely ‘0’ which is
open.
The key 58 collides with 18, 89, 49 afterwards it found an empty cell at the index 1.
Similarly collision 69 is handled.
If the table is big enough, a free cell can be always be found, but the time to do so can get quite
large.
Even if the table is relatively empty, blocks of occupied cells start forming. This is known as primary
clustering means that any key hashes into the cluster will require several attempts to resolve the
collision and then it will add to the cluster.
=======================================================================
@ Einstein College Of Engineering [92/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [93/159]
www.vidyarthiplus.com
www.eeeuniversity.com
1 1
2 4 2
3
3
Undirected Graph G1 Directed graph G2
V ( G1 ) = { 1, 2, 3, 4}; E ( G1 ) = { ( 1, 2 ), ( 1, 3 ), ( 1, 4 ), ( 2, 3 ), ( 2, 4 ), (3, 4 ) }
V ( G2 ) = { 1, 2, 3 }; E ( G2 ) = { < 1, 2 >, < 2, 1 >, < 2, 3 > }
The edges of a directed graph are drawn with an arrow from the tail to the head.
For undirected graph:
1
n(n − 1)
2 3 = 3 edges
2
=======================================================================
@ Einstein College Of Engineering [94/159]
www.vidyarthiplus.com
www.eeeuniversity.com
3
2 n2 = 9 edges
n(n − 1)
The no.of possible edges in an undirected graph is and in a directed graph is n2.
2
4.1.2 Directed Graph:
A directed graph or di-graph is a pair G = (V,E) where V is a set whose elements are called
vertices(nodes) and E is a set of ordered pairs of elements of V called edges or directed edges or arcs. For
directed edges(v,w) in E, ‘v’ is its tail and ‘w’ is its head. (v,w) is represented in the diagrams as the arrow,
vÆw, i.e simply vw.
1
2 Vertices = {1,2,3,4}
4
Edges = {(1,2), (1,4), (2,4), (3,1),
(3,2), (3,4)}
3
Vertices = {1,2,3,4}
2 4 Edges = {(1,2), (1,4), (1,3),
(2,4), (2,3), (3,4)}
Note:
i) An E set cannot have duplicate elements.
ii) An edge that connects a vertex to itself is not possible.
4.1.4 Subgraph:
A subgraph of graph G = (V,E) is a graph G’=(V’,E’) such that V’ ⊆ V and E’ ⊆ E.
=======================================================================
@ Einstein College Of Engineering [95/159]
www.vidyarthiplus.com
www.eeeuniversity.com
2 4
2 4 3
3
G1’ G1’
G1’
4.1.5 Symmetric Digraph:
A symmetric digraph is a directed graph such that for every edge vw there is also the reverse edge
wv.
Example:
A
B
4.1.6 Symmetric Undirected graph:
Every undirected graph is a symmetric digraph by interpreting each undirected edge as a pair of
directed edges in opposite directions.
A
B C
4.1.7 Complete graph:
A complete graph is a graph normally undirected with an edge between each pair of vertices.
Example
A B
C D
=======================================================================
@ Einstein College Of Engineering [96/159]
www.vidyarthiplus.com
www.eeeuniversity.com
Note:
C D
Path
length from vertex V to itself is zero.
B C
C D G
D E F
A B
4.1.13 Cycle:
Cycle in a directed graph is a simple cycle in which no vertex is repeated except that the first and last
are identical.
Example:
A
B C Cycle
= AÆ B Æ C Æ A
=======================================================================
@ Einstein College Of Engineering [97/159]
www.vidyarthiplus.com
www.eeeuniversity.com
(b)
A
Undirected acyclic graph (or) undirected tree.
B C
5
15 20
B C A—D = 10, represents weight of the edge.
25
4.1.17 Degree
The number of edges incident on a vertex determines its degree. The degree of the vertex V is written as
degree (V).
The in degree of the vertex V, is the number of edges entering into the vertex V.
Similarly the out degree of the vertex V is the number of edges exiting from that vertex V.
=======================================================================
@ Einstein College Of Engineering [98/159]
www.vidyarthiplus.com
www.eeeuniversity.com
A B A B
A ⎡0 1 0C 1⎤
⎢1 0 1 1 ⎥⎥
B
⎢
D C C ⎢0 0 0 1⎥
⎢ ⎥
⎣1 0 1 0⎦
D
Adjacency Matrix
The adjacency matrix is maintained in the array arr[4][4]. Here the entry of matrix arr[0][1] = 1, which
represents there is an edge in the graph from node A to B. Similarly arr[2][0]= 0, which represents there
is no edge from node C to node A.
=======================================================================
@ Einstein College Of Engineering [99/159]
www.vidyarthiplus.com
www.eeeuniversity.com
A A B
A ⎡0 1 1C 1⎤
⎢1 0 1 1 ⎥⎥
C D
B
⎢
C ⎢1 1 0 1⎥
⎢ ⎥
⎣1 0 1 0⎦
D
B Adjacency Matrix
The adjacent matrix for an undirected graph will be a symmetric matrix. This implies that for every I and
j, A[i][j] = A[j][i] in an undirected graph.
3. Adjacency matrix for Weighted graph:
3 A B
A ⎡0 2 ∞
C
8⎤
A B ⎢3
2 B
⎢ 0 4 7 ⎥⎥
9 8 7 4 C ⎢∞ ∞ 0 5⎥
⎢ ⎥
D 6 C D
⎣9 ∞ 6 0⎦
Adjacency Matrix
5
The space needed to represent a graph using its adjacency matrix is n2 bits.
Disadvantages:
1. Takes O(n2) space to represents the graph
2. It takes O(n2) time to solve the most of the problems.
A B
D C
=======================================================================
@ Einstein College Of Engineering [100/159]
www.vidyarthiplus.com
www.eeeuniversity.com
Vertex 2 B A C D
Vertex 3 C D
Vertex 4 D A C
2. Adjacency lists for Undirected graph:
Let us consider the following undirected graph,
C D
B
The adjacency list for the above graph is as follows:
Vertex 1 A B C D
Vertex 2 B A C D
Vertex 3 C A B D
Vertex 4 D A B D
In this representation each list had a head node. The head nodes are sequential providing easy
random access to the adjacency list for any particular vertex. For an undirected graph with n vertices and m
edges this representation requires n head nodes and 2m list nodes. The storage required is then log n + log
m for the list nodes and O ( log n ) for head nodes because 2 fields in each node.
The structure of header node is:
Name of the node
Pointer which points to
the first node adjacent
to header node
Pointer which points to
the next node in header
node list
The total number of edges in G may, therefore can be determined by counting the number of nodes
on its adjacency list. In order to determine in-degree of a vertex easily, we can maintain one more list called
inverse-adjacency lists for each vertex. Each list will contain a node for each vertex adjacent to the vertex it
represents.
Disadvantages:
It takes O(n) time to determine whether there is an arc from vertex i to vertex j. Since there can be O(n)
vertices on the adjacency list for vertex i.
=======================================================================
@ Einstein College Of Engineering [101/159]
www.vidyarthiplus.com
www.eeeuniversity.com
V1 E1
V2 E2 V4
E3
V3
The incidence matrix for the above graph is as follows:
E1 E2 E3
V1 1 0 0
V2 0 1 0
V3 0 0 1
V4 1 1 1
Disadvantage of Incidence matrix:
1. Eventhough incidence matrix is an easy way of representing graphs, a graph with considerable
number of nodes could use a large amount of memory.
2. Most of the incidence matrices will be sparse matrices only( a matrix with lot of zeros in it).
C D
A
B graph between four cities = {A, B, C, D}
=======================================================================
@ Einstein College Of Engineering [102/159]
www.vidyarthiplus.com
www.eeeuniversity.com
1 Start 1
2 Read n 2
Sum=0 3
3
i=1
4
7
4 While No 5
i<=n 7
8
Print sum 6
5 Yes
8
Sum=sum+i
Stop
6
i=i+1
=======================================================================
@ Einstein College Of Engineering [103/159]
www.vidyarthiplus.com
www.eeeuniversity.com
C
• NumVertex = 3
• Indegree is initialized in an array
• Graph is stored in an adjacency list.
=======================================================================
@ Einstein College Of Engineering [104/159]
www.vidyarthiplus.com
www.eeeuniversity.com
4.4.2 To implement the topological sort with queue representation, perform the following steps.
Step 1 : - Find the indegree for every vertex.
Step 2 : - Place the vertices whose indegree is `0' on the empty queue.
Step 3 : - Dequeue the vertex V and decrement the indegree's of all its adjacent vertices.
Step 4 : - Enqueue the vertex on the queue, if its indegree falls to zero.
Step 5 : - Repeat from step 3 until the queue becomes empty.
=======================================================================
@ Einstein College Of Engineering [105/159]
www.vidyarthiplus.com
www.eeeuniversity.com
A B
C D
NumVertex = 5
Indegree is intialised in an array
Graph is stored as an adjacency list
=======================================================================
@ Einstein College Of Engineering [106/159]
www.vidyarthiplus.com
www.eeeuniversity.com
Analysis
The running time of this algorithm is O(|E| + |V|). where E represents the Edges & V represents the
vertices of the graph.
=======================================================================
@ Einstein College Of Engineering [107/159]
www.vidyarthiplus.com
www.eeeuniversity.com
b) A D
A D
c)
B
From vertex B either C or D to be explored,
G
but C is explored as per the alphabetical order.
F C E There is nowhere to explore from C, therefore
=======================================================================
C is a dead end.
@ Einstein College Of Engineering [108/159]
www.vidyarthiplus.com
www.eeeuniversity.com
F C E
e)
A D From D it is possible to explore A, this would
complete a cycle, but trees should not have
B cycles.
G
Again backtrack to B, from there backtrack to
F C E A, explore the path to F.
f)
A D From F it is possible to traverse either A or C,
but both the nodes are discovered nodes. So F
B is also a dead end.
G From the above diagram it is possible to say
that G and E are never traversed.
F C E
4.5.2 Breadth First Search:
Starting at vertex v and marking it as visited. BFS performs simultaneous explorations starting from a
common point and spreading out independently.
Breadth firs search differs from depth first search in that all unvisited vertices adjacent to v are visited
next. Then unvisited vertices adjacent to these vertices are visited and so on. A breadth first search
beginning at vertex v1 of the graph in above figure would first visit root node and then one by one in the
same level and passes to other level.
BFS Technique steps:
1. Put the ending node (the root node) in the queue.
2. Pull a node from the beginning of the queue and examine it.
o If the searched element is found in this node, quit the search and return a result.
o Otherwise push all the (so-far-unexamined) successors (the direct child nodes) of this node
into the end of the queue, if there are any.
3. If the queue is empty, every node on the graph has been examined -- quit the search and return
"not found".
4. Repeat from Step 2.
Procedure BFS
//Breadth first search of G is carried out beginning at vertex v. All the vertices
visited are
marked as VISITED(i) = 1.The graph G and array VISITED are global and VISITED is
initialized to zero //
VISITED (v) ← 1
Initialize Q to be empty // Q is queue //
=======================================================================
@ Einstein College Of Engineering [109/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [110/159]
www.vidyarthiplus.com
www.eeeuniversity.com
F C E
c) D Explore all paths from vertex B and F.
A
From B the explored edges are BD, BC.
B From F the explored edges are FA, FC.
G The dashed lines shows the edges that were
explored but went to vertices that were
F C E previously discovered.(i.e FA, FC)
d)
A D From D the explored edge is DA.
But A is already exist in the discovered
B
G vertices list. So we will say that the edge DA
is checked rather than explored.
F C E
** There is no backtracking in BFS and E and G are unreachable.
4.5.3 DFS vs BFS
Depth first search Breadth first search
1. Backtracking is possible from a dead end 1. Backtracking is not possible.
2. Vertices from which exploration is incomplete are 2. The vertices to be explored are organized as a
processed in a LIFO order. FIFO queue.
3. Search is done in one particular direction at the 3. Search is done parallely in all possible direction.
time.
=======================================================================
@ Einstein College Of Engineering [111/159]
www.vidyarthiplus.com
www.eeeuniversity.com
∑C
i =1
i , i +1 .
This is referred as weighted path length. The unweighted path length is the number of the edges on the
path, namely N - 1.
Two types of shortest path problems, exist namely,
1. Single source shortest path problem
The single source shortest path algorithm finds the minimum cost from single source vertex to all
other vertices. For a given graph G=(V,E), the shortest path is calculated from a distinguished vertex
S to every other vertex in G. Dijkstra's algorithm is used to solve this problem which follows the
greedy technique.
2. All pairs shortest path problem
All pairs shortest path problem finds the shortest path for a given graph G=(V,E) of every pair of
vertices in G. To solve this problem dynamic programming technique known as floyd's algorithm is
used.
These algorithms are applicable to both directed and undirected weighted graphs provided that they do not
contain a cycle of negative length. Here we will single source shortest path problem for unweighted and
weighted graph.
=======================================================================
@ Einstein College Of Engineering [112/159]
www.vidyarthiplus.com
www.eeeuniversity.com
V1 V2
V3 V4 V5
V6 V7
=======================================================================
@ Einstein College Of Engineering [113/159]
www.vidyarthiplus.com
www.eeeuniversity.com
V1 Dequeued V6 Dequeued
Vertex
Known dv Pv Known dv Pv
V1 1 1 V3 1 1 V3
V2 0 2 V1 0 2 V1
V3 1 0 0 1 0 0
V4 0 2 V2 0 2 V1
V5 0 ∞ 0 0 ∞ 0
V6 0 2 V3 1 1 V3
V7 0 ∞ 0 0 ∞ 0
Queue V 6 , V2, V4 V 2 , V4
V2 Dequeued V4 Dequeued
Vertex
Known dv Pv Known dv Pv
V1 1 1 V3 1 1 V3
V2 1 2 V1 1 2 V1
V3 1 0 0 1 0 0
V4 0 2 V1 1 2 V1
V5 0 3 V2 0 3 V2
V6 1 1 V3 1 1 V3
V7 0 ∞ 0 0 3 V4
Queue V 4, V5 V 5 , V7
V5 Dequeued V7 Dequeued
Vertex
Known dv Pv Known dv Pv
V1 1 1 V3 1 1 V3
V2 1 2 V1 1 2 V1
V3 1 0 0 1 0 0
V4 1 2 V1 1 2 V1
V5 1 3 V2 1 3 V2
V6 1 1 V3 1 1 V3
=======================================================================
@ Einstein College Of Engineering [114/159]
www.vidyarthiplus.com
www.eeeuniversity.com
A B
C D
0 A B 0 A 1 B
Æ
C D 1 C D
=======================================================================
@ Einstein College Of Engineering [115/159]
www.vidyarthiplus.com
www.eeeuniversity.com
0 A 1 B
1 C D
2
B Dequeued C Dequeued D Dequeued
Vertex
Known dv Pv Known dv Pv Known dv Pv
A 1 0 0 1 0 0 1 0 0
B 1 1 A 1 1 A 1 1 A
C 0 1 A 1 1 A 1 1 A
D 0 2 B 0 2 B 1 2 B
Queue C, D D Empty
The shortest distance from the source vertex A to all other vertex is listed below;
A Æ B is 1
A Æ C is 1
A Æ D is 2
=======================================================================
@ Einstein College Of Engineering [116/159]
www.vidyarthiplus.com
www.eeeuniversity.com
B 6 D 3 C
8 7 6
E
The directed graph G=(V, E, W)
Initial configuration of the table
Vertex Known dv Pv Comments
A 0 0 0
B 0 ∞ 0
A is chosen as source vertex, since the
C 0 ∞ 0
edge AD has minimum value.
D 0 ∞ 0
E 0 ∞ 0
After A is declared known
Vertex Known dv Pv Comments
A 1 0 0
B 0 ∞ 0
The vertices adjacent to A is C and D.
C 0 2 A
Therefore, dv, Pv of C and D is updated.
D 0 1 A
E 0 ∞ 0
After D is declared known
Vertex Known dv Pv Comments
A 1 0 0 The vertices adjacent to D is B and C.
B 0 6 D The vertex B, through D is included.
=======================================================================
@ Einstein College Of Engineering [117/159]
www.vidyarthiplus.com
www.eeeuniversity.com
E 0 ∞ 0 than dv through D.
=======================================================================
@ Einstein College Of Engineering [118/159]
www.vidyarthiplus.com
www.eeeuniversity.com
A 2 B ∞
0
2 1
2
C
1
D
∞
∞
Solution:
=======================================================================
@ Einstein College Of Engineering [119/159]
www.vidyarthiplus.com
www.eeeuniversity.com
C D C 0 ∞ 0
∞ 1 ∞ D 0 ∞ 0
Initial Configuration
Step 2:
Vertex `A' is choose as source vertex and is declared as known vertex.
Then the adjacent vertices of `A' is found and its distance are updated as follows :
T [B] .Dist = Min [T[B].Dist, T[A] .Dist + Ca,b]
= Min [ ∞ , 0 + 2]
=2
T [d] .Dist = Min [T[d].Dist, T[a] .Dist + Ca,d]
= Min [ ∞ , 0 + 1]
=1
0 A 2 B 2 Vertex known dv Pv
A 1 0 0
1
2 2 B 0 2 A
C D C 0 ∞ 0
∞ 1
1 D 0 1 A
After ‘A’ is declared Known
After ‘A’ is declared known
Step 3:
Now, select the vertex with minimum distance, which is not known and mark that vertex as visited.
Here `D' is the next minimum distance vertex. The adjacent vertex to `D' is `C', therefore, the distance of C
is updated a follows, T[C].Dist = Min [T[C].Dist, T[D].Dist + Cd, c]
= Min [ ∞ , 1 + 1] = 2
A 2 B 2
0 Vertex known dv Pv
A 1 0 0
2 2 B 0 2 A
1 C 0 2 D
C D
D 1 1 A
2 1
1 After ‘D’ is declared Known
After ‘D’ is declared known
Step 4:
The next minimum vertex is ‘B’ and mark it as visited.
=======================================================================
@ Einstein College Of Engineering [120/159]
www.vidyarthiplus.com
www.eeeuniversity.com
0 A 2 B Vertex known dv Pv
2
A 1 0 0
1 B 1 2 A
2 2 C 0 2 D
C D D 1 1 A
2 1 After ‘B’ is declared Known
1
0 2 2 Vertex known dv Pv
A B
A 1 0 0
1 B 1 2 A
2 2 C 1 2 D
C D D 1 1 A
1 After ‘C’ is declared Known, then the algorithm terminates
2 1
After ‘C’ is declared known and algorithm terminates
4.7 MINIMUM SPANNING TREE
A tree is a connected graph with no cycles. A spanning tree is a subgraph of G that has the same set of
vertices of G and is a tree.
A minimum spanning tree of a weighted connected graph G is its spanning tree of the smallest weight,
where the weight of a tree is defined as the sum of the weights on all its edges. The total number of edges in
Minimum Spanning Tree (MST) is |V|-1 where V is the number of vertices.
2 G is connected. For any spanning Tree T, if an edge E that
A minimum spanning tree exists if and only if
is not in T is added, a cycle is created. The removal of any edge on the cycle reinstates the spanning tree
property.
A D
3
5 1 1
B C
2
Connected Graph G
A 2 D A 2 D A D A 2 D
3 5 1 5 1 5
B 2 C B C B C B C
2 2
Cost = 7 Cost = 8 Cost = 8 Cost = 9
=======================================================================
@ Einstein College Of Engineering [121/159]
www.vidyarthiplus.com
www.eeeuniversity.com
2 A 2 D
A D
1 1
B 2 C B 2 C
Minimum Spanning Tree Æ Cost = 5 Cost = 5
Spanning Tree with minimum cost is considered as Minimum Spanning Tree
Types of Algorithm to find MST:
1. Prim’s Algorithm
2. Kruskal’s Algorithm
=======================================================================
@ Einstein College Of Engineering [122/159]
www.vidyarthiplus.com
www.eeeuniversity.com
2
A B
1 2
3
C 1 D
=======================================================================
@ Einstein College Of Engineering [123/159]
www.vidyarthiplus.com
www.eeeuniversity.com
3 1 2 A 0 0 0
B 0 ∞ 0
D∞
∞ C 1 C 0 ∞ 0
Undirected Graph G
D 0 ∞ 0
INITIAL CONFIGURATION
Step 2:
Here, `A' is taken as source vertex and marked as visited. Then the distance of its adjacent vertex is
updated as follows :
T[B].dist = Min (T[B].Dist, Ca,b) T[D].dist = Min [T[D].Dist, Ca,b] T[C].dist = Min [T[C].Dist, Ca,b]
= Min ( ∞, 2) = Min ( ∞ , 1) = Min ( ∞ , 3)
=2 =1 =3
Step 3:
Next, vertex `D' with minimum distance is marked as visited and the distance of its unknown adjacent
vertex is updated.
T[B].Dist = Min [T[B].Dist, Cd,b]
= Min (2, 2)
=2
T[C].dist = Min [T[C].Dist, Cd,c]
= Min (3,1)
=1
=======================================================================
@ Einstein College Of Engineering [124/159]
www.vidyarthiplus.com
www.eeeuniversity.com
C D C 0 1 D
3 1 1 D 1 1 A
Step 4:
Next, the vertex with minimum cost `C' is marked as visited and the distance of its unknown adjacent
vertex is updated.
0 Vertex After ‘C’ is marked visited
A B 2 known dv Pv
A 1 0 0
1
B 0 2 A
C D C 1 1 D
3 1 1 D 1 1 A
Step 5:
Since, there is no unknown vertex adjacent to `C', there is no updation in the distance. Finally, the
vertex `B' which is not visited is marked.
Vertex After ‘B’ is marked visited Algorithm
0 A 2 2 terminates.
D
known dv Pv
1 A 1 0 0
B 1 2 A
B C C 1 1 D
3 1 1
D 1 1 A
A 2 D
1
B C
1
=======================================================================
@ Einstein College Of Engineering [125/159]
www.vidyarthiplus.com
www.eeeuniversity.com
Example 2: For the following graph construct MST using prim’s algorithm.
2
V1 V2
1 3 10
4
7
V3 V4 V5
2
5 8 4 6
V6 V7
1
7 V2 0 ∞ 0
V3 V5
∞ 2
V4
∞ V3 0 ∞ 0
5 8 4 6 V4 0 ∞ 0
V5 0 ∞ 0
V6 V7
1 V6 0 ∞ 0
∞ ∞ V7 0 ∞ 0
V3 V4 V5 V3 0 4 V1
V4 0 1 V1
V5 0 ∞ 0
V6 V7
∞ V6 0 ∞ 0
∞ V7 0 ∞ 0
∞ ∞
Here ‘Vi’ is marked as visited and then the distance of its adjacent vertex is updated as follows.
T[V2].dist = Min [T[V2].dist, Cv1, v2] = Min [ , 2] = 2.
T[V4].dist = Min [T[V4].dist, Cv1, v4] = Min [ ∞ , 1] = 1.
=======================================================================
@ Einstein College Of Engineering [126/159]
www.vidyarthiplus.com
www.eeeuniversity.com
V6 V7 V7 0 4 V4
8 4
Here ‘V2’ is marked as visited and then the distance of its adjacent vertex is updated as follows;
T[V4].dist = Min [T[V4].dist, Cv2, v4] = Min [1, 3] = 1.
T[V5].dist = Min [T[V5].dist, Cv2, v5] = Min [7, 10] = 7
Step 5:
0 2 2
V1 V2 Vertex After V3 is declared known
1 known dv Pv
V1 1 0 0
V3 2 V4 V5 V2 1 2 V1
2 7 V3 1 2 V4
5
V4 1 1 V1
V5 0 7 V4
V6 V7
V6 0 5 V3
4
5 V7 0 4 V4
=======================================================================
@ Einstein College Of Engineering [127/159]
www.vidyarthiplus.com
www.eeeuniversity.com
Step 6:
0 2 2 Vertex After V7 is declared known
V1 V2
known dv Pv
1 V1 1 0 0
V2 1 2 V1
2
2 V3 V4 V5 6 V3 1 2 V4
4 V4 1 1 V1
6 V5 0 6 V7
1 V6 1 V7 4 V6 0 1 V7
V7 1 4 V4
Here ‘V7’ is marked as visited and then the distance of its adjacent vertex is updated as follows;
T[V6].dist = Min [T[V6].dist, Cv7,v6] = Min [5, 1] = 1
T[V6].dist = Min [T[V6].dist, Cv7,v5] = Min (7, 6) = 6
Step 7:
0 2 2
V1 V2 Vertex After V6 is declared known
1 known dv Pv
V1 1 0 0
2
2 V3 V4 V5 6 V2 1 2 V1
V3 1 2 V4
4
6 V4 1 1 V1
1 V5 0 6 V7
1 V6 V7 4
V6 1 1 V7
V7 1 4 V4
Here ‘V6’ is marked as visited and then the distance of its adjacent vertex is updated.
Step 8:
1 V6 1 V7 4 V6 1 1 V7
V7 1 4 V4
=======================================================================
@ Einstein College Of Engineering [128/159]
www.vidyarthiplus.com
www.eeeuniversity.com
Here ‘V5’ is marked as visited and then the distance of its adjacent vertex is updated.
=======================================================================
@ Einstein College Of Engineering [129/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [130/159]
www.vidyarthiplus.com
www.eeeuniversity.com
V6 V7
1
Step 1:
V1 V2 V1 V2
1
V3 V4 V5 V3 V4 V5
V6 V7 V6 V7
Step 2:
2
V1 V2
V1 V2 1
1
V3 V4 V5
=======================================================================
@V3Einstein College Of
V4 Engineering V5[131/159]
V6 1 V7
www.vidyarthiplus.com
www.eeeuniversity.com
Step 3:
2 2
V1 V2 V1 V2
1 1
V3 2 V4 V5 V3 2 V4 V5
4
V6 1 V7 V6 1 V7
2
V1 V2
1
V3 2 V4 V5
4
6
V6 1 V7
The final Execution table for finding MST using Kruskal’s algorithm is as follows:
Edge Weight Action
(V1, V4) 1 Accepted
(V6, V7) 1 Accepted
(V1, V2) 2 Accepted
(V3, V4) 2 Accepted
(V2, V4) 3 Rejected
(V1, V3) 4 Rejected
(V4, V7) 4 Accepted
(V3, V6) 5 Rejected
(V5, V7) 6 Accepted
4.8 BICONNECTIVITY
=======================================================================
@ Einstein College Of Engineering [132/159]
www.vidyarthiplus.com
www.eeeuniversity.com
B 20
C 10 30
D E
4.8.3 Explanation with an Example: Figure 4.8.1
A B
C D F
G E
A B
C D F
G graph.
E
Removal of vertex `D'
A B
=======================================================================
@ Einstein College Of Engineering [133/159]
C D F
www.vidyarthiplus.com
www.eeeuniversity.com
lllly removal of `D' vertex will disconnect E & F from the graph. Therefore `C' & `D' are articulation
points.
The graph is not biconnected, if it has articulation points.
Depth first search provides a linear time algorithm to find all articulation points in a connected graph.
Steps to find Articulation Points :
Step 1 : Perform Depth first search, starting at any vertex
Step 2 : Number the vertex as they are visited, as Num (v).
Step 3 : Compute the lowest numbered vertex for every vertex v in the Depth first spanning tree,
which we call as low (w), that is reachable from v by taking zero or more tree edges and
then possibly one back edge. By definition, Low(v) is the minimum of
(i) Num (v)
(ii) The lowest Num (w) among all back edges (v, w)
(iii) The lowest Low (w) among all tree edges (v, w)
Step 4 : (i) The root is an articulation if and only if it has more than two children.
(ii) Any vertex v other than root is an articulation point if and only if v has same child w
such that Low (w) Num (v), The time taken to compute this algorithm an a graph is
Note:
For any edge (v, w) we can tell whether it is a tree edge or back edge merely by checking Num (v) and
Num (w). If Num (w) > Num (v) then the edge (v, w) is a back edge.
B (2/1)
V W
Num(W) = 2 C (3/1)
Num(V) = 1
D (4/1) G (7/7)
Backedge(w,V)
E (5/4)
F (6/4)
Depth First Tree For Fig (4.8.1) With Num and Low.
ROUTINE TO COMPUTE LOW AND TEST FOR ARTICULATION POINTS
=======================================================================
@ Einstein College Of Engineering [134/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [135/159]
www.vidyarthiplus.com
www.eeeuniversity.com
Example:
C
A F
D
B G
E
Suppose we start from C, we would go C, A, F, C
Now we stuck in C.
C
A F
D
B G
E
Now backtrack to F and process F, D, G, F. We now stuck in F.
A F
=======================================================================
D
@ Einstein College Of Engineering [136/159]
B G
www.vidyarthiplus.com
www.eeeuniversity.com
C F
D E
The problem of finding Hamiltonian cycle is solved by backtracking approach.
We consider the vertex ‘A’ as the root. From vertex ‘A’ we have three ways, so we resolve the tie
using alphabet order, so we select vertex ‘B’.
From ‘B’ algorithm proceeds to ‘C’, then to ‘D’, then to ‘E’ and finally to ‘F’ which proves to be a dead
end.
So the algorithm backtracks from ‘F’ to ‘E’, then to ‘D’ and then to ‘C’, which provides alternative to
pursue.
Going from ‘C’ to ‘E’ which is useless, and the algorithm has to backtrack from ‘E’ to ‘C’ and then to
‘B’. From there it goes to the vertices ‘F’, ‘E’, ‘C’ & ‘D’, from which it return to ‘A’, yielding the
Hamiltonian Circuit A, B, F, E, C, D, A.
0 A
1
B
9
2
F
C
6
D 3 E
E 10
=======================================================================
@ Einstein College Of Engineering
7 [137/159]
8
D F
E 4
Dead End C 11
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [138/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [139/159]
www.vidyarthiplus.com
www.eeeuniversity.com
An Egyptian fraction is the sum of distinct unit fractions, such as . That is, each
fraction in the expression has a numerator equal to 1 and a denominator that is a positive integer, and all
the denominators differ from each other.
The sum of an expression of this type is a positive rational number a/b; for instance the Egyptian fraction
above sums to 43/48. Every positive rational number can be represented by an Egyptian fraction.
A greedy algorithm, for finding such a representation, can add at each stage to the sum of the largest
unit fraction which does not cause the sum to exceed the fraction.
Ex:
5.1.3 Map Coloring:The map coloring problem asks to assign colors to the different regions, where adjacent
regions are not allowed to have the same color. There is no general algorithm to assign minimal number of
colors, yet each map has an assignment which uses no more than four colors.
The greedy approach, repeatedly choose a new color and assign it to as many regions as possible.
5.1.4 Shortest Path Algorithm:
Establish the shortest path between a single source node and all of the other nodes in a graph.
Greedy algorithm: Add an edge that connects a new node to those already selected. The path from the
source to the new node should be the shortest among all the paths to nodes that have not been selected yet.
Greedy Approach:
We need an array to hold the shortest distance to each node, and a visited set.
We take neighboring node; get the direct distance from it to the root node.
Next we have to check if it is less than the entry in the array or if it not less than the entry in the
array or if the array has null value, then we store it in the array.
From that node, put the neighboring node in the visited set. Then we visit every other node it is
connected to and also calculate the distance from it to the root node.
If it is less than the entry in the array or if the array has null value, then we store it in the array.
After we are finished with that node we go to the next connected node, and so on.
At the end we will have an array of values representing the shortest distance from the starting
node to every other node in the graph.
This algorithm is also known as Dijkstra’s algorithm.
10
2
6 2
10
50
1 4
1 4
20 30
3 20
3
=======================================================================
@ Einstein College Of Engineering [140/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [141/159]
www.vidyarthiplus.com
www.eeeuniversity.com
subject to ∑w x
1≤i ≤ n
i i ≤m
and 0 ≤ xi ≤ 1, 1 ≤ i ≤ n
4. The profits and weights are positive numbers.
Algorithm:
Algorithm GreedyKnapsack(m,n)
// p[1:n] and w[1:n] contain the profits and weights respectively of the n objects
ordered
//such that p[i]/w[i] ≥ p[i+1]/w[i+1]. m is the knapsack size and x[1:n] is the
solution vector.
{
for i = 1 to n do x[i] = 0.0; // Initialize x
U = m;
for i =1 to n do
{
if (w[i] > U) then break;
x[i] = 1.0;
U = U – w[i];
}
if(i≤n) then x[i] = U/w[i];
}
Procedure
Procedure GreedyFractionalKnapsack(w, v, W)
FOR i = 1 yo n do
x[i] = 0
weight=0
while(weight<W)
do i = best remaining item
If weight + w[i] = W
then x[i] = 1
weight = weight + w[i]
else
x[i] = (w- weight) / w[i]
weight = W
return x
End;
• If the items are already sorted in the descending order of vi/wi, then the while-loop takes a time in
O(n).
=======================================================================
@ Einstein College Of Engineering [142/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [143/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [144/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [145/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [146/159]
www.vidyarthiplus.com
www.eeeuniversity.com
Principle of Optimality
• The dynamic programming relies on a principle of optimality, which states that an optimal
sequence of decisions or choices has the property that whatever the initial state and
decision are, the remaining decisions must constitute an optimal decision sequence with
regard to the state resulting from the first decision.
• Thus, the essential difference between the greedy method and dynamic programming is that in the
greedy method only one decision sequence is ever generated. In dynamic programming, many
decision sequences may be generated. However, sequences containing suboptimal subsequences
cannot be optimal(it the principle of optimality holds) and so will not be generated.
• Ex, in matrix chain multiplication problem
• The principle can be related as follows: the optimal solution to a problem is a combination of optimal
solutions to some of its subproblems.
• The difficulty in turning the principle of optimality into an algorithm is that it is not usually obvious
which subproblems are relevant to the problem under consideration.
5.4.1 0-1 knapsack problem
=======================================================================
@ Einstein College Of Engineering [147/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [148/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [149/159]
www.vidyarthiplus.com
www.eeeuniversity.com
A bottom-up approach computes f(0), f(1), f(2), f(3), f(4), f(5) in the listed order.
=======================================================================
@ Einstein College Of Engineering [150/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [151/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [152/159]
www.vidyarthiplus.com
www.eeeuniversity.com
The eight queens puzzle has 92 distinct solutions. If solutions that differ only by symmetry operations
(rotations and reflections) of the board are counted as one, the puzzle has 12 unique (or fundamental)
solutions, which are presented below:
a b c d e f g h a b c d e f g h a b c d e f g h
8 8 8 8 8 8
7 7 7 7 7 7
6 6 6 6 6 6
5 5 5 5 5 5
4 4 4 4 4 4
3 3 3 3 3 3
2 2 2 2 2 2
1 1 1 1 1 1
a b c d e f g h a b c d e f g h a b c d e f g h
Unique solution 1 Unique solution 2 Unique solution 3
a b c d e f g h a b c d e f g h a b c d e f g h
8 8 8 8 8 8
7 7 7 7 7 7
6 6 6 6 6 6
5 5 5 5 5 5
4 4 4 4 4 4
3 3 3 3 3 3
2 2 2 2 2 2
1 1 1 1 1 1
a b c d e f g h a b c d e f g h a b c d e f g h
Unique solution 4 Unique solution 5 Unique solution 6
a b c d e f g h a b c d e f g h a b c d e f g h
8 8 8 8 8 8
7 7 7 7 7 7
6 6 6 6 6 6
=======================================================================
@ Einstein College Of Engineering [153/159]
www.vidyarthiplus.com
www.eeeuniversity.com
Procedure BacktrackingQueenProblem
queen[0, 0, 0, 0, 0, 0, 0, 0 ] { has 8 entries, all have value 0 }
int EightQueen() return array[1…8]
count Å 0
queen[0] Å 0
repeat until queen[0] < 8 do
if is_row_good(0) = YES
queen[0]Å queen[0] + 1
queen[1] Å 0
repeat until queen[1] < 8 do
if is_row_good(0) = YES
queen[1]Å queen[1] + 1
queen[2] Å 0
repeat until queen[2] < 8 do
if is_row_good(0) = YES
queen[2]Å queen[2] + 1
queen[3] Å 0
repeat until queen[3] < 8 do
if is_row_good(0) = YES
queen[3]Å queen[3] + 1
queen[4] Å 0
=======================================================================
@ Einstein College Of Engineering [154/159]
www.vidyarthiplus.com
www.eeeuniversity.com
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [156/159]
www.vidyarthiplus.com
www.eeeuniversity.com
The first set of constraints ensures that for each i exactly two variables corresponding to edges
incident with i are chosen. Since each edge has two endpoints, this implies that exactly n variables are
allowed to take the value 1. The second set of constraints consists of the subtour elimination constraints.
Each of these states for a specific subset S of V that the number of edges connecting vertices in S has to be
less than |S| thereby ruling out that these form a subtour. Unfortunately there are exponentially many of
these constraints. The given constraints determine the set of feasible solutions S. One obvious way of
relaxing this to a set of potential solutions is to relax (i.e. discard) the subtour elimination constrains. The set
of potential solutions P is then the family of all sets of subtours such that each i belongs to exactly one of the
subtours in each set in the family, cf.
Another possibility is decribed, which in a B&B context turns out to be more appropriate. A
subproblem of a given symmetric TSP is constructed by deciding for a subset A of the edges of G that these
must be included in the tour to be constructed, while for another subset B the edges are excluded from the
tour. Exclusion of an edge (i; j) is usually modeled by setting cij to 1, whereas the inclusion of an edge can
be handled in various ways as e.g. graph contraction. The number of feasible solutions to the problem is
(n−1)! /2, which for n = 50 is appr. 3x1062
=======================================================================
@ Einstein College Of Engineering [157/159]
www.vidyarthiplus.com
www.eeeuniversity.com
Definition : - T(N) = (F(N)), if there are positive constants C1, C2 and no such that
=======================================================================
@ Einstein College Of Engineering [158/159]
www.vidyarthiplus.com
www.eeeuniversity.com
=======================================================================
@ Einstein College Of Engineering [159/159]
www.vidyarthiplus.com