Data Structures and Algorithms
Data Structures and Algorithms
(CS2005)
Dr. Biri Arun
CSE, NIT Rourkela.
References:
1. Data Structures and Algorithm Analysis in C by Mark Allen Weiss.
2. An Introduction to Data Structures with Applications by Jean-Paul Trembly and Paul G. Sorenson.
3. Data Structures with C by Seymour Lipschutz.
How are students (Objects of Real World) represented in
the Teacher's Register?
Roll Number Name Date
1 John
2 Michael
3 Lily
Non-Magnetized Bits
of Magnetic Tape
Secondary memory of a Computer
S S N N N
N N S S S
0 1 1 1 1 0 1
↓ ↑ ↑ ↑ ↑ ↓ ↑
• So, Arabic numbers can be converted into Binary
numbers and can be conveniently represented &
stored in Computers!!!!!
• First, a real world entity is modeled using Arabic number
system
• Second, the model is converted into an Abstract Data
Type.
• That is, the model is converted into a Binary number.
• Abstract Data Type (ADT) is collection of a set of
values and a set of operations, without
emphasizing/considering/focusing the representation and
implementation details of the set of values & operations.
• Representation and implementation details of the set
of values & operations are hidden from the users.
• An user must be able to use an ADT without knowing
anything of its implementation details.
Height
Arabic Number Binary Number System, without
of a
System implementation details
Person
Height
of a (1.75m)10 (00001.110000)2
Person
(1.11)2
Data Representation Process using Computer
Real
World Mathematical ADT Data Type
Object Model
Height Binary
Arabic Number Binary Number System,
of a Number
System with implementation details
Person System
Height
of a (1.75m)10 (00001.110000)2 (001.110)2
Person
(1.11)2 (001.110)2
• Data Type is collection of a set of values and a set of
operations, with the representation and implementation
details of the set of values & operations.
• Following details are considered in Data Type:
• How many bits should be used to represent integer,
fractions & characters,
• Whether zero charge should represent bit 0 or otherwise,
• Whether N-S magnetization should represent bit 0 or
otherwise,
• Order in which a sequence of bytes should be stored in
memory, etc.
• Data Type gives a definite structure (at Hardware &
Compiler level) to the data.
• Data Types:
• Built-in/elementary/primitive data types.
• Ex. Integer, Floating-point, Char, Boolean, Void.
• They are provided by all programming languages.
• User-defined data types.
• Built using Elementary Data Types,
• Ex. arrays, pointers, strings, structures, union etc.
• What is Data Structure?
The most significant bit (MSB) is used for sign and rest are
for magnitude
Drawbacks:
• Zero has two representation;
• 0 =0000 =(+000) and 0 =1111 =(-111)
3. Integers can be stored in Two’s complement form
32−1 32−1
• long int = 32 bits; Range = -(2 -1) to (2 -1).
• long long int =64 bits; Range = -(264−1 -1) to (264−1 -1).
• Floating Point Data Type
• It consist of the set of fractional values
{...-3.0...-2.9999…-1.0...0.0…,0.99….2.0…3.0…}
• It consist of the set of operations {arithmetic, relational,
logical, etc.}
• Physical representation/storage:
• Floating-Point Number (FPN):
• Ex.
• - (2.5)10 = - (010.100)2 = - (1.010)2 x2(1)
Base Exponent
Sign Mantissa/Significand
• -(2.5)10 = -(0010.1)2 = -(1.010)2x2(1)
• Bias =127
• 8 bits are used to represent true exponent, then the
number of true exponents = 0 to (28-1) i.e.,0 to 255.
Bias
• Bias (B) is used for encoding.
• True exponent (TE) is encoded using the bias (B) to
obtain a Biased exponent (BE).
• Ex.
• if TE = 0, then BE = TE + B = 0 + 127 = 127.
• if TE = 1, then BE = TE + B = 1 + 127 = 128.
• if TE = -1, then BE = TE + B = -1 + 127 = 126.
• if TE = 127, then BE = TE + B = 127 + 127 = 254.
• if TE = -126, then BE = TE + B = -126 + 127 = -1.
Encoded Biased Exponents
True Exponents
0 … 127 … 255
Binary
00000000 … 01111111 … 11111111 Representation
True Exponents
Used for
representing the Used for representing
Number Zero the Number Infinity
• True exponent (TE) is obtained by subtracting the Bias
(B) from the Biased exponent (BE).
• TE = BE – B.
• Ex.
• if BE = 127, then TE = BE - B = 127 - 127 = 0.
• if BE = 254, then TE = BE - B = 254 - 127 = 127.
•
• Ex.
• -(2.5)10 = -(0010.1)2 = -(1.010)2x2(1)
• = -(1.010)2x2(1+127)
• = -(1.010)2x2(128)
Sign (1 bit) Exponent (8 bits) Fraction (23 bits)
- 128 010
1 10000000 01000000000000000000000000
Single Precision Floating Point Representation
• Single precision floating point representation for
Number Zero:
• (0)10 = +(0)10 = -(0)10
• Representation for +(0)10
0 00000000 00000000000000000000000000
• Representation for -(0)10
1 00000000 00000000000000000000000000
• Single precision floating point representation for
Number Infinity (∞):
• Representation for +(∞)10
0 11111111 00000000000000000000000000
• Representation for -(∞)10
1 11111111 00000000000000000000000000
• Single precision floating point representation for Not a
Number (NaN):
• NaN = Non-Real numbers
= {+∞ − ∞; -∞ + ∞; ±∞% ±∞; ±0% ±0;
±∞x0}
• Minimum Value:
• Sign = -
• Min. Exponent = 2-126 = 1.1754944E-38
• Min. Value that can be represented by the Fractional part
of Min. Significand = (00000000000000000000000)2 (23
Zero’s)
• So, Min. Significand = (1. 00000000000000000000000)2
• = (1.0)10
• Therefore, Min. Float Value = (-)Min. Significand * Min.
Exponent
• =(1.0)10 * 1.1754944E-38 = 1.2E-38.
• Bias =1023
• Boolean Data Type:
• Set of Values = {0,1}
• Set of Operations = {Logical operations, etc.}
• Size = 8 bits.
• Size = 32 bits.
• Array:
• Set of Values = set of values of a single data type.
• Set of Operations = operation on index [i], etc.
Memory Storage
• Array storage:
Row i=0
Row i=1
Row i=2
Col Col
j=0 j=4
struct student
{
char Name[20];
int Roll_No;
char Course[10];
float CGPA;
};
struct student
{
char Name[20];
int Roll_No;
char Course[10];
float CGPA;
}student_1, student_2;
• Accessing the elements of a structure variable:
stud[1].Name = “John”;
stud[1].Roll_No = 11254;
stud[1].Course = “Data_Structure”;
stud[1].CGPA = 7.5;
• Passing structures to a function:
• Passing the elements of a structure to a function
• Func1(student_1.Name);
• Func2(student_1.Roll_No);
• Passing the address of the elements of a structure to a
function
• Func1(student_1.Name);
• Func2(&student_1.Roll_No);
• Passing the whole structure to a function by value
• Func1(struct student s); //declaration
• Func1(student_1); //calling by value
• Passing the whole structure to a function by reference
struct student student_1; //object of a structure
struct student *p; //pointer to a structure
p = &student_1;
Func1(p); //calling by reference
• Union:
• It is similar to structure, but it stores only one data
element at any particular time.
• Size of union = size of its largest element.
union student
{
char Name[20]; //size = 20 bytes
int Roll_No; //size = 1 byte
char Course[10]; //size = 10 bytes
float CGPA; //size = 4 bytes
}student_1;
• Enumeration:
• It is a set of name integer constants.
• Ex.
• enum day = {Sunday, Monday, Tuesday};
• = { 0, 1 , 2 }(integer constants)
• enum day weekday, holiday;
• weekday =Monday;
• holiday = Sunday;
• typedef:
• It is a used to give a new name to built-in and derived
data types.
• Giving new names increases readability and error
correction.
• Ex.
typedef int Roll_no;
Roll_no class_roll_no;
class_roll_no = 12;
• Ex.
typedef struct student;
student student1, student2;
student1.Name = “John”;
student1.Roll = 20;
Real World Object
Measurement
Analog Data
Analog to Digital Conversion
Computer Program
Computer Program
Constant
Problem size =n
Including Excluding
Time taken= f(n)
Problem size =n
• Big O is used to represent the maximum time taken by an
algorithm to solve a computational problem.
• For f(n) = O(g(n)) to be true, f(n) ≤ cg(n) must hold for +ve
c and n0.
• => f(n) ≤ cg(n).
• => 5n+2 ≤ cn…..eqn.-1.
• => when n=1, then we have 7 ≤ c => c = 7.
• => when n <1, then LHS>RHS, so n0 = 1.
• => so, for c = 7 and n0 =1, the above equation is true
𝒇(𝒏) 𝟓𝒏 + 𝟐
lim = lim = 𝟓 𝝐 [0, ∞)
𝒏→∞ 𝒈(𝒏) 𝒏→∞ 𝒏
• For f(n) = O(g(n)) to be true, f(n) ≤ cg(n) must hold for +ve
c and n0.
• => f(n) ≤ cg(n).
• => 5n+2 ≤ cn2 ...eqn.-2.
• => when n=1, then we have 7 ≤ c. => c = 7.
• => for n =1, then LHS ≤ RHS, so n0 = 1.
• *Note: there could be any other values for c and n0.
• => so, for c = 7 and n0 =1, the above equation is true
• => Therefore, f(n) = O(g(n)) for c = 7 and n0 =1
𝒇(𝒏) 𝟓𝒏 + 𝟐
lim = lim 𝟐
= 0 𝝐 [0, ∞)
𝒏→∞ 𝒈(𝒏) 𝒏→∞ 𝒏
• Likewise, f(n) = O(n3), f(n) = O(n4) are true for some c & n0.
c1g(n)
f(n)
n0
Problem size = n
• Big O notation does not specify the nature of upper bound.
𝒇(𝒏) 𝟓𝒏 + 𝟐
lim = lim 𝟐
=0
𝒏→∞ 𝒈(𝒏) 𝒏→∞ 𝒏
• Further, f(n) = o(n2), f(n) = o(n3), f(n) = o(n4) are also true
c2g(n3)
Time taken= f(n)
c1g(n2)
Loose Upper Bounds
f(n)
n0 n 0
Problem size = n
• iii). Omega notation denoted as Ω:
• It is used to denote the set of functions that act as lower
bound for f(n).
• Let f(n) & g(n) be functions defined on positive integers.
Excluding Including
Problem size =n
• f(n) = Ω(g(n)) implies that f(n) is a member of the set
Ω(g(n)) and is lower bounded by cg(n).
• Omega Ω is used to represent the minimum time taken by
an algorithm to solve a computational problem.
• For f(n) = Ω(g(n)) to be true, cg(n) ≤ f(n) must hold for +ve
c and n0.
• => cg(n) ≤ f(n).
• => cn ≤ 5n+2
• => when c=5, then LHS < RHS for all +ve n0
• => f(n) = Ω(g(n))
• => f(n) = Ω(n)
𝒇(𝒏) 𝟓𝒏 + 𝟐
lim = lim = 𝟓 𝝐 (0, ∞]
𝒏→∞ 𝒈(𝒏) 𝒏→∞ 𝒏
c1g(n)
c2g(n)
n0 n0
Problem size = n
• Omega Ω notation does not specify the nature of lower
bound.
n0
Problem size =n
• v). Theta notation denoted as Ɵ:
• It is used to denote the set of functions are between a tight
upper and a tight lower bound for f(n).
• Let f(n) & g(n) be functions defined on positive integers.
Excluding Excluding
Time taken= f(n)
Problem size =n
• f(n) = Ɵ(g(n)) implies that f(n) is a member of the set
Ɵ(g(n)) and is sandwiched between c1g(n) & c2g(n).
𝒇(𝒏) 𝟓𝒏 + 𝟐
lim = lim = 𝟓 𝝐 (0, ∞)
𝒏→∞ 𝒈(𝒏) 𝒏→∞ 𝒏
• Searching operation:
i). Search Time for an element, T(n) = O(log(n)), when the
list is sorted. It is called binary search.
ii). Search Time for an element, T(n) = O(n) when the
list is not sorted.
• Limitation of Array implementation of List Data
Structure:
i). Size of the list has to be known in advance. This not
practical in many situations
DSA
Marks 87.0 77.0 45.0 65.0 X
Header Node
• Header Node contains information about the Singly
Linked List.
87.0 568 87.0 212 87.0 002 87.0 0
Address of Address of Address of Address of
node-1: node-2: node-3: node-4:
100 568 212 002
100
Address of Header:
999
Internal representation of a linked list
Declaration of a Node in C language:
struct node
{
float Data;
struct node *next;
};
Data next
/*create Header*/
node *Header;
// Header will have junk value
• 1. Creating an empty linked list:
Header = NULL;
• 1. Creating an empty linked list using a function:
8 7 5
65 24 39 X 80 X
8 7 5
Add of Add of Add of Add of
Node-1: Node-2: Node-3: New Node:
9999 88 77 55
3. Inserting an element into an Non-Empty Linked List
• 3. Inserting elements into an non-empty linked list:
1 8 7 5
22 0 65 24 39 80 X
8 7 5
New Node 10
8 7 15
65 24 39 80 X
8 7 05
8
65 80 X
8
Ptr2 = *header;
Ptr1 = (*header)->next;
6. Deleting an element at the End of Linked List
Header Free the space
Ptr2 Ptr1 for the deleted
Node
8 7 5
65 24 39 X 80 X
8 7 5
while(ptr1->next != NULL)
{ ptr2 = ptr1;
ptr1 = ptr1->next;}
6. Deleting an element at the End of Linked List
• 6. Deleting an elements from the end of a linked list:
void DeleteInListEnd(node **header, int data)
{ node *ptr1,*ptr2;
//when there is only one node in the list
if((*header)->next==(node *)NULL)
{
ptr1=*header;
*header=NULL;
free(ptr1);
}
• 6. Deleting an elements from the end of a linked list:
//when there is more than one node in the list
else
{ ptr2 = *header;
ptr1= (*header)->next;
while(ptr1->next != NULL)
{ ptr2 = ptr1;
ptr1 = ptr1->next;}
ptr2->next = NULL;
free(ptr1);
}}
Header
8 7 5
65 X 24 39 80 X
8 7 5
Ptr1
8 75 80 X
65 24
8 75
5
39
5
• 8. Deleting an elements at Specific location (at 39) of a
linked list:
if(Header!=NULL)
Header Ptr {ptr=Header;
Header=ptr->next;
Free(ptr);
Ptr=Header;}
8 7 5
65 24 39 80 X X
8 7 5
8 7 5
65 24 39 80 X
8 7 5
8 7 5 3
65 X 24 39 46 80 X X
8 7 5 3
Ptr2=Header; if(ptr1!=NULL)
Ptr1=Ptr2->next; if(ptr1==NULL)
{ptr3=ptr2;
Ptr3=NULL; {Header=ptr2;}
Ptr2=ptr1;
Ptr2->next=NULL; Ptr1=ptr1->next;
• Reversing a linked list: Ptr2->next=ptr3;}
Circular Linked List
• One of the limitations of a Singly Linked List is that one
can not access the previous elements of the list.
Header
8 7 5 3 1
65 24 39 46 80
8 7 5 3 1
X 20 40 60 X
new->next = ptr->next;
(ptr->next)->prev = new;
ptr->next = new;
new->prev = ptr;
Inserting an element at Specific location (after 40) of
Doubly Linked List
Other operations on Doubly linked List:
• CreateDLL( ).
• Insert Element at Beginning of DLL ( ).
• Traverse a DLL ( ).
• Delete at the End of DLL( ).
• Delete at Start of DLL( ).
• Delete at Specific Location of DLL( ).
• Delete the Entire DLL( ).
• Search for an element in DLL( ).
Header data data data
STACK 18 Top
55 Top
74 Top
• Pop Operation:
• It is the operation to delete data element from the top of
the stack using a pointer variable called top.
STACK 18 Top
55 Top
74 Top
• Push and Pop Operation takes place only at one end of
the Stack which is pointed by the top pointer variable.
• Other Operations are:
• CreateEmptyStack( ),
• IsStackEmpty( ),
• GetTopofStack( ),
• IsStackFull( ),
• 1. CreateEmptyStack( ):
• This function Creates an Empty Stack.
STACK
Top
• 2. IsStackEmpty( ):
• This function checks if an Stack is Empty.
STACK
Top
• 3. GetTopofStack( ):
• This function gives the data element pointed by the
pointer variable top.
STACK 18 Top
55
74
• 4. IsStackFull( ):
• This function checks if an Stack is full or not.
Top
STACK 18
55
74
Stack Implementation
• 1. Array Implementation
• 2. Linked List Implementation
1. Array Implementation of Stack
Data Structure
1. 1. CreateEmptyStack( )
Top = -1;
STACK
Top = -1;
STACK
22
22 33 44
33
22 Top = Top+1;
Array[Top];
1. 2. Push( )
22 33 44
33
22 Top = Top-1;=>Top = 2-1 = 1
1. 3. Pop( )
22 33 44
33
22 Top_Data = Array[Top];
1. 4. GetTopofStack( )
int GetTopofStack(int *Top, int head[ ])
{
int data;
data = head[*Top];
return (data);
}
1. 5. IsStackFull( )
int IsStackFull(int *Top, int maxsize)
{
int true =1, false = 0;
if(*Top = = (maxsize-1))
return(true);
else
return(false);
}
1. 5. IsStackEmpty( )
int IsStackEmpty(int *Top)
{
int true =1, false = 0;
if(*Top = = -1)
return(true);
else
return(false);
}
2. Linked List Implementation of
Stack Data Structure
Top
STACK
Node-1
44 Node-3 Node-2
44 33 22 X
33
22
STACK 2. 1. CreateEmptyStack( )
Top = NULL
2. 1. CreateEmptyStack( )
Node-1
22 X
22
STACK 2. 2. Push( ) Top
Node-1
44 Node-3 Node-2
44 33 22 X
33
22
2. 2. Push( )
void Push(node **Top, int data)
{
node *ptr;
//Create a new node
ptr=(node *)malloc(sizeof(node));
ptr->data=data;
ptr->next=*Top;
*Top=ptr;
}
STACK 2. 3. Pop( ) Top
Node-1
44 Node-3 Node-2
44 33 22 X
33
22
Ptr
2. 3. Pop( )
1. Parentheses Checker
2. Conversion of infix expression to postfix expression
3. Evaluation of postfix expression
4. Conversion of infix expression to prefix expression
5. Evaluation of prefix expression
6. Recursion
1. Parentheses Checker
• Parentheses are used in Programming Languages and
Mathematical Expression.
• Ex. {{( )}}, (((a-b)+2)+(c-d))*e/3.
• In a valid expression, there are equal number of left &
right parentheses.
• Every right parentheses is preceded by its
corresponding left parentheses.
• Example of Valid Expression:
• (((( )))), {{( )}}, {{( )[ ]( ) }{ }}
• Example of Invalid Expression:
• (((( ))), {{( )}, {{( ) [ ( ] }{ })
1. Parentheses Checker
• Ex. { a*(b-c) }
STACK
13 + - Left Right
• Ex.
• 5+1-3+4-1 = ((((5+1)-3)+4)-1) = (((6-3)+4)-1) = ((3+4)-1)
= (7-1) = 6
2. Infix Expression to Postfix Expression Conversion
Precedence Operator Group Associativity
Highest=16 ()[] Left Right
14 * / % Left Right
• Ex.
• 5*3/2 = ((5*3)/2) = (15/2) = 7.5
• 5/3*2 = ((5/3)*2) = (1.666*2) = 3.332
2. Infix Expression to Postfix Expression Conversion
Precedence Operator Group Associativity
Highest=16 ()[] Left Right
• Ex.
• 5-3*2/2 = (5-((3*2)/2)) = (5-(6/2)) = (5-3) = 2
• 5-2+4/2 = (5-2+(4/2)) = (5-2 + 2) = (3+2) = 5
2. Infix Expression to Postfix Expression Conversion
• Ex. • Ex.
• ((5-2)+6)/((3-2)*4) • 7*4/(6-4)-4
• => (3+6)/((3-2)*4) • =>7*4/2-4
• => 9/((3-2)*4) • =>28/2-4
• => 9/(1*4) = 9/4 = 2.25 • =>14-4 =10
• Parentheses can be used to alter precedence of operators
• This makes the evaluation algorithms of such expressions
difficult.
• So, infix expressions are converted into post or prefix
expressions for ease of evaluation.
2. Infix Expression to Postfix Expression Conversion
• In Postfix Expression, an operator is placed after its two
operands.
• Ex.
• Ex.
• A + B - C => A + B - C
• A+B => AB+
• => AB+ - C => AB+ - C
• B+A => BA+
• => AB+C-
• A-B => AB-
• B-A => BA- • Ex.
• A/B => AB/ • A * B / C => A * B / C
• A*B => AB* • => AB* / C => AB* / C
• => AB*C/
2. Infix Expression to Postfix Expression Conversion
• Ex.
• A + B * C => A + B * C
• => A + BC* => A + BC*
• => ABC*+
• Ex.
• A * (B + C) - D => A * (B + C) - D
• => A * BC+ - D => A * BC+ - D
• => ABC+* - D => ABC+* - D
• => ABC+*D-
**Postfix Expression does not contain Parentheses
2. Infix Expression to Postfix Expression Conversion
Symbol Priority
*/ 3
+- 2
( 1
Infix to Postfix Conversion Algorithm:
1. Assign priorities to all the operators.
2. Add symbol ‘#’ at the end of the infix expression.
3. Scan the infix expression, character by character, from left to right until the end.
4. {If the character is an Operand, Then add it to the postfix expression.
5. If the character is a Left Parentheses ‘(’, Then push it into Stack.
6. If the character is an Operator, Then,
If (Priority of Operator) > (Priority of the Character in the Top of Stack),
{Then, push the operator into Stack.}
Else,
{While ((Priority of Operator) <= (Priority of the Operator in the Top of Stack))
{Pop operators from the stack and add it to the postfix expression.}
Push the operator into Stack.}
7. If the character is a Right Parentheses ‘)’, Then pop an operator from the Stack
and add it to postfix expression, until a Left Parentheses ‘(’ is encountered.
8. Pop the Left Parentheses, but do not added it to the postfix expression.
9. If the character is ‘#’, Then pop all the operators from the stack and add them to the
postfix expression and Stop.}
• Stack is used for Postfix Conversion
• A * (B + C) - D ==> A * (B + C) – D #
A*(B+C)–D#
+
STACK
( Postfix Expression
*- ABC + * D -
2. Infix Expression to Postfix Expression Conversion
735 Result =
14 = 2 * 79
29
14 9 =14 - 5
3. Infix Expression to Prefix Expression Conversion
• In Prefix Expression, an operator is placed before its two
operands.
• Ex.
• Ex.
• A + B - C => A + B - C
• A+B => +AB
• => +AB - C => +AB - C
• B+A => +A+
• => -+ABC
• A-B => -AB
• B-A => -BA • Ex.
• A/B => /AB • A * B / C => A * B / C
• A*B => *AB • => *AB / C => *AB / C
• => /*ABC
3. Infix Expression to Prefix Expression Conversion
• Ex.
• A + B * C => A + B * C
• => A + *BC => A + *BC
• => +*ABC
• Ex.
• A * (B + C) - D => A * (B + C) - D
• => A * +BC - D => A * +BC - D
• => *A+BC - D => *A+BC - D
• => -*A+BCD
**Prefix Expression does not contain Parentheses
3. Infix Expression to Prefix Expression Conversion
Symbol Priority
*/ 3
+- 2
) 0
1.
Infix to Prefix Conversion
Assign priorities to all the operators.
Algorithm:
2. Add symbol ‘#’ at the Beginning of the infix expression E.
3. Scan the infix expression, character by character, from Right to Left until the end.
4. Ch = get_next_char(E).
5. While (ch!=‘#’)
6. { if(ch = = ‘)’, then push (ch) into Operator Stack.
7. if(ch = =‘(’, then
8. do { ch = pop( ) from the Operator Stack.
9. push(ch) into the Display Stack.
10. } while (ch!=‘)’.
11. If(ch = = operator)
12. {if(priority of ch >= priority of character on the Top of Operator Stack)
13. { push(ch) into Operator Stack}
14. else
15. {do{ch-2 = pop( ) from the Operator Stack.
16. push(ch-2) into the Display Stack.
17. }While (priority of ch < priority of character on the Top of the Operator Stack)}
18. push(ch) into Operator Stack.}}
Infix to Prefix Conversion Algorithm:
Operator Stack
A
Display Stack
+
B + Prefix Expression
C *)
D - - * A + B C D
3. Infix to Prefix Expression Conversion
9
Stack
2
3 7=3+4
47
14 14 = 2 * 7
95 9 = 14 - 5
4. Function Call
void main( )
{
int x=22; main( ) Activation Record or
x=22 Frame for main( )
printf(“%d”,x);
return; Heap
}
Global Variables
22 void main( )
{printf(“%d”,x);
return;}
4. Function Call
1
2 1
3 2 31
A B C
Three Elements
4.1 Recursion
1
2 3
A B C
4.1 Recursion
1 2 3
A B C
4.1 Recursion
2
1 3
A B C
4.1 Recursion
1
2
3
A B C
4.1 Recursion
1
2
A B C
Two Elements
4.1 Recursion
2 1
A B C
1. A→B
4.1 Recursion
1 2
A B C
2. A → C
4.1 Recursion
1
2
A B C
3. B → C
4.1 Recursion
1
A B C
One Element
4.1 Recursion
1
A B C
A→C
B→C
1
A B C
1
A B C
1
A B C
1
B A C
1
B A C
1
B C A
Function ( 1, A, B, C )
First Parameter = No. of elements to be Shifted
Second Parameter is the Stack from which elements is
to be Poped.
Third Parameter is the Stack that is used for helping
the shifting operation.
Fourth Parameter is the Stack that is to be used for
Pushing the elements.
4.1 Recursion
Function ( 1, A, B, C )
A→C
=>implies that one element has to be poped from Stack A
and then pushed into Stack C.
=>implies that Stack B is used for helping the Shifting.
4.1 Recursion
Function ( 1, A, C, B )
A→B
4.1 Recursion
Function ( 1, B, A, C )
B→C
Function ( 2, A, B, C )
=>implies that two element has to be poped from Stack A
and then pushed into Stack C.
=>implies that Stack B is used for helping the Shifting.
Function ( 2, A, B, C )
1
2
A B C
Function ( 2, A, B, C )
A→B
1
2
A B C
Function ( 2, A, B, C )
A→B
1
2
A C B
Function ( 2, A, B, C )
Function ( 1, A, C, B )
A→B
2 1
A C B
Function ( 2, A, B, C )
Function ( 1, A, C, B )
A→B
A→C
2 1
A C B
Function ( 2, A, B, C )
Function ( 1, A, C, B )
A→B
A→C
2 1
A B C
Function ( 2, A, B, C )
Function ( 1, A, C, B ) Function ( 1, A, B, C )
A→B A→C
1 2
A B C
Function ( 2, A, B, C )
Function ( 1, A, C, B ) Function ( 1, A, B, C )
A→B A→C
B→C
1 2
A B C
Function ( 2, A, B, C )
Function ( 1, A, C, B ) Function ( 1, A, B, C )
A→B A→C
B→C
2
1
C
B A
Function ( 2, A, B, C )
1 3
Function ( 1, A, C, B ) 2 Function ( 1, B, A, C )
Function ( 1, A, B, C )
A→B B→C
A→C
1
2
C
B A
TowerHonoi(n, A, B, C)
{
If(n==1), then
{ A→C. /*pop( ) from A & push( ) into C*/
return.
}
Else
{TowerHonoi((n-1), A, C, B).
TowerHonoi(1, A, B, C).
TowerHonoi((n-1), B, A, C).
}
return.
}
TH(3,A,B,C) 1
2
TH(2,A,C,B) TH(2,B,A,C)
TH(1,A,B,C)
3 4 5
TH(1,A,B,C) TH(1,A,C,B) TH(1,C,A,B)
A→C
TH(3,A,B,C)
7
TH(2,A,C,B) TH(2,B,A,C)
TH(1,A,B,C)
8 9 10
TH(1,B,C,A) TH(1,B,A,C) TH(1,A,B,C)
1 2 3
Front
Queue Data Structure
Dequeue Operation
Rear
1 2 3
Front
Front Front
Front
Queue Data Structure
Circular Queue Operation
RearRearRear
Rear
Rear
6 7 4 5
Front
Front Front
Front
Queue Data Structure
• Operations on Queue:
• CreateEmptyQ( ),
• EnQueue( ),
• DeQueue( ),
• IsQEmpty( ),
• IsQFull( ),
• GetRear( ),
• GetFront( ),
• MakeEmpty( ).
Queue Data Structure
1 2 3
Front
Queue Data Structure
• EnQueue( )
{
if (IsQFull)
print “can’t insert”
else
{ rear = rear + 1;
queue[rear]=data;
Qsize = Qsize + 1;}
}
Queue Data Structure
• int DeQueue( )
{ int x;
if(IsQEmpty( ))
print “ Can’t delete”
Else
{front = front+1;
x = queue[front];
Qsize = Qsize - 1;}
return(x);
}
Queue Data Structure
• IsQEmpty( )
{if(front == rear) //if (Qsize = = 0)
return 1;
Else
return 0;
}
Queue Data Structure
• IsQFull( )
{if(rear == max-1)
return 1;
Else
return 0;
}
Queue Data Structure
• Int GetRear( )
{if(IsQEmpty( ))
print “Queue is Empty”
Else
return (queue[rear]);
}
Queue Data Structure
• Int GetFront( )
{if(IsQEmpty( ))
print “Queue is Empty”
Else
return (queue[front]);
}
Queue Data Structure
A[6] A[2]
A[5]
A[3]
A[4]
Maxsize of Q = n
Circular Queue
Rear
Empty
Empty
A[0] A[1] A[2] A[3] A[4]
1 2 3 4 5
MaxSize of Q = 5
Front Qsize = 2
No. of Empty Space = 3
Rear Circular Queue
Empty
1 2 3 4 5
MaxSize of Q = 5
Front Qsize = 2
No. of Empty Space = 3
Rear Circular Queue
Empty
1 2 3 4 5
MaxSize of Q = 5
Front Qsize = 0
No. of Empty Space = 5
Circular Queue
Rear Empty
6 2 3 4 5 If (front == (n-1))
front = front mod (n-1)
i.e., G = ( V, E )
B
C
Set V = {A, B, C, D, E}
Set E = {(AB), (AC), (AE), (BC), (CD), (DA), (ED)}
Graph
E
Node or Vertex, E
Edge or Arch,
• Represented by a pair of vertices, (DE).
D
• Undirected edge is represented by a pair
of unordered vertices, (DE) = (ED).
A E
A E
Directed Graphs
Graph
D B
D Isolated Node
G = ( V, E )
V ={ E, D}
E={ɸ}
D
Graph
• Nodes of an edge are called adjacent nodes or neighbors.
• Ex. For edge (DE), nodes E & E are adjacent nodes.
• Out-degree of a node, E, of a directed
graph, is the number of edges originating
from the node, E. E
• Ex.OD( E ) =1
• In-degree of a node, E, is the number of
F
edges terminating at node E. Ex. ID( E )=1 D
• Degree of a node, E, is the sum of its out-
degree and in-degree. Ex. D( E ) = 1+1=2. B
A
Graph
• In an undirected graph, the degree of a node, E, is the
number of edges incident to the node, E.
• Ex. D ( E ) = 2.
• Ex. D (A) = 2
E
A
F
D
C
B A
Graph
• A Path in a directed graph, is any sequence of edges, such
that the terminal node of any edge in the sequence is the
initial node of the edge, if any, appearing next in the
sequence. E
E
E
F
F
F D
D
D
A
A
A
Path with non-
Cyclic Path distinct edges
Simple Path
Graph
• A Path is represented as given below:
• Path=((Vi1,Vi2), (Vi2,Vi3), (Vi3,Vi4),……..(Vin-2,Vin-1), (Vin-1,Vin)) or
• Path=(Vi1,Vi2,Vi3,Vi4……..Vin-2,Vin-1,Vin)
• P1=((D,E))
• P2=((D,E),(E,F))
• P3=((D,A),(A,E),(E,F),(F,D),(D,A),(A,E),(E,F))
A
Graph
• A Path of a digraph, with distinct edges is called a simple
path.
• A Path in which all nodes are distinct is called an
elementary path.
• If there exists a path from node A to node B, then there must be an
elementary path from A to B.
• A Path which originates and ends in the same
E
node is called a cycle.
• A cycle in which a node(except the D
F
D F
A Complete Graph
Graph
• A Simple digraph is a digraph without parallel edges.
• Simple digraph which does not have any cycle is called
Directed Acyclic Graph(DAG).
E
F
D
A
Representation of Graphs
• 1. Adjacency Matrix.
• 2. Adjacency List.
Representation of Graphs
1. Adjacency Matrix:
1 if vi is adjacent to vj,
aij =
0 otherwise.
Representation of Graphs
1. Adjacency Matrix for DAG:
C
Adjacent Nodes
A B C D
A 0 0 0 0 D
B 1 0 1 0 B
C 0 0 0 1
D 1 0 0 0 A
Nodes
Representation of Graphs
1. Adjacency Matrix:
C
Adjacent Nodes
A B C D
A 1 0 0 0 D
B 1 0 1 1 B
C 0 0 0 1
D 1 0 0 0 A
Nodes
Representation of Graphs
1. Adjacency Matrix for undirected graph:
C
Adjacent Nodes
A B C D
A 0 1 0 1 D
B 1 0 1 1 B
C 0 1 0 1
D 1 1 1 0 A
Nodes
Adjacency Matrix for undirected graph is Symmetric, aij = aji
Representation of Graphs
1. Adjacency Matrix for a Weighted Graph:
C
Adjacent Nodes 7
A B C D 2
A 0 0 0 0 D
B 5 0 2 0 B
C 0 0 0 7 5 1
D 1 0 0 0 A
Nodes
Powers of Adjacency Matrix: V4
V1 V2 V3 V4
V1 0 1 0 1
V2 1 0 0 0
V3
V3 1 1 0 1 V1
V4 0 1 0 0
V2
A = aij
• Adjacency matrix A = aij shows the number of path, with
path length =1, that exits from node Vi to Vj.
Powers of Adjacency Matrix: V4
V1 V2 V3 V4
V1 1 1 0 0
V2 0 1 0 1
V3
V3 1 2 0 1 V1
V4 1 0 0 0
V2
A2 = aij (2)
V2 1 1 0 0
V3
V3 2 2 0 1 V1
V4 0 1 0 1
V2
A3 = aij (3)
V2 1 1 0 1
V3
V3 2 3 0 2 V1
V4 1 1 0 0
V2
A4 = aij (4)
3 3 0 2 V2
Bn = 6 8 0 5 V3
2 3 0 1 V4
Path Matrix (Reachability Matrix):
Let G = ( V, E ) be a simple digraph with n nodes.
Then its path matrix P is defined as:
• A ˄ A = A(2),
=> If aij(2) =1, then there is at least one path of length two
from node Vi to Vj, else there is no path.
• A ˄ A(r-1) = A(r)
=> If aij(r) =1, then there is at least one path of length r from
node Vi to Vj, else there is no path.
Computation of Path Matrix using Warshall Algorithm:
k=1
V4
V1 V2 V3 V4
V1 1 1 0 0
V2 0 1 0 1
V3
V3 1 1 0 1 V1
V4 1 0 0 0
V2
V2 1 1 0 0
V3
V3 1 1 0 1 V1
V4 0 1 0 1
V2
V2 1 1 0 1
V3
V3 1 1 0 1 V1
V4 1 1 0 0
V2
V2 1 1 0 1
V3
V3 1 1 0 1 V1
V4 1 1 0 1
V2
A B C X
C
B A C D X
C A B D X
D
B C X
Representation of Graphs B
A C 7 X 5
7 C
B A 2 X
C
B 4 X
D
B 1 C 5 X
Graphs Traversals
Visited Status D
A
A B C D
01 01 01 01
C
Print B A D C
1. Breadth First Search(BFS):
Print B
Level-0
B A D E C F G H
A D
Level-1
E C F
Level-2
G
H
Level-3
1. Breadth First Search(BFS) Algorithm:
D
E A
C
1. Breadth First Search(BFS) Algorithm:
Queue
B E A D C
From Nodes
# B B A A
Visited Nodes
B
01 01 01 01 01
A B C D E
2. B to A => B->A = 1
3. B to D => B->A->D = 2
4. B to C => B->A->C = 2 C
2. Depth First Search(DFS):
B
D
DC A
BA
Stack
C
Visited Status
1 0 10 1 0 1 0
Print B D C A
A B C D
2. Depth First Search(DFS):
B
Stack D
E A
Visited Status
F C
Print
B D C F G A E G
2. Depth First Search(DFS) Algorithm:
1. Set the visited status of each node of the graph to 0.
2. Push the starting node into Stack and set its visited status
to1.
3. Pop the node, N, located at the top of stack and print the
data of the node.
4. Repeat Step-5 & Step-6 until Stack is Empty
5. Push all unvisited adjacent nodes of the node, N, into the
stack and set its visited status to1.
6. Pop the node located at the top of stack as N & Print its
data.
7. End.
2. Depth First Search(DFS) Algorithm:
E
B C
1. A B D C E F G
2. B A D C E F G
2. Depth First Search(DFS) Algorithm: Start
Topological Sorting of DAG:
D
A F G
E A 0 1
B C B 0 1
C 0 1
B D 0 1
A E 0 1
C F 1
E 0
GE G 0 1
D
FC F
DAB G B A C E D F G
Disjoint Set Data Structure
• Set is a collection of distinct elements.
• S1 = {1, 2, 3, 4}. 1 4
2 3
*1 *2 *3 *4
*5 *6 *7 *8
*1 *2 *3 *4
*5 *6 *7 *8
*1 *2 *3 *4
*5 *6 *7 *8
*1 2 *3 *4
*5 *6 *7 *8
*1 2 *3 *4
*5 *6 *7 *8
*1 2 *3 4
*5 6 *7 8
Disjoint Set Data Structure
• Unionset(2,4) => Unionset(findset(2),findset(4))
• => Unionset(1,3);
*1 2 *3 4
*1 *5 6 *7 8
3
2
4
Disjoint Set Data Structure
• Unionset(3,6) => Unionset(findset(3),findset(6))
• => Unionset(1,5);
*1
*5 6 *7 8
3
2
• Since the size of set *1 is bigger than that of set *5, make
the representative of set *1 as the representative of the
new set obtained after unionset operation.
Disjoint Set Data Structure
• Unionset(3,6) => Unionset(findset(3),findset(6))
• => Unionset(1,5);
*7 8
*1
3 5
2
4 6
Array Implementation
L = {1,2,3,4,5,6,7,8} Makeset(L):
Representative -1 -1 -1 -1 -1 -1 -1 -1
S => 1 2 3 4 5 6 7 8
=> A set is represented by an index of an array.
=> Minus sign(-) indicates that the associated index is the
representative of the set.
1 indicates the size of the set.
Any +ve number in the array indicates the parent of the
index
Array Implementation
L = {1,2,3,4,5,6,7,8} Makeset(L):
Representative -1 -1 -1 -1 -1 -1 -1 -1
S => 1 2 3 4 5 6 7 8
*1 *2 *3 *4
*5 *6 *7 *8
Array Implementation
Unionset(1, 2); Makeset(L):
Representative -2 1 -1 -1 -1 -1 -1 -1
S => 1 2 3 4 5 6 7 8
*1 2 *3 *4
*5 *6 *7 *8
Unionset(2, 3) => Unionset(findset(2), findset(3))
=> Unionset(1,3)
Representative -2 1 -1 -1 -1 -1 -1 -1
S => 1 2 3 4 5 6 7 8
*1 2 *3 *4
*5 *6 *7 *8
Unionset(2, 3) => Unionset(findset(2), findset(3))
=> Unionset(1,3)
Representative -3 1 1 -1 -1 -1 -1 -1
S => 1 2 3 4 5 6 7 8
*1 2 3 *4
*5 *6 *7 *8
Unionset(4, 5); Unionset(6,7); Unionset(4,8)
Representative -3 1 1 -1 -1
-2
-3 4 -1
-2 -1
6 -1
4
S => 1 2 3 4 5 6 7 8
*1 2 3 *4
*5
5 *6 *77 *8
8
Unionset(4,6);
Representative -3 1 1 -1 -1
-5
-2
-3 4 -1
-2
4 -1
6 -1
4
S => 1 2 3 4 5 6 7 8
*4
*1
6
*6
3 5 8
2
7
Unionset(3,6)
Representative -3
4 1 1 -1 -1
-2
-5
-3
-8 4 -1
-2
4 -1
6 -1
4
S => 1 2 3 4 5 6 7 8
*4
*1
1
6
*6
3 5 8
2
7
findset(7); findset(2)
Representative -3
4 1 1 -1 -1
-2
-5
-3
-8 4 -1
-2
4 -1
6 -1
4
S => 1 2 3 4 5 6 7 8
*4
1 6
5 8
7
2 3
Better findset( ) by path compression
Representative -3
4 4 4 -1 -1
-2
-5
-3
-8 4 -1
-2
4 -1
4 -1
4
S => 1 2 3 4 5 6 7 8
*4
1 6
5 8
7
2 3
int findset (int x)
Findset( )
{
If (array[x]<0)
return x;
Else
{ array[x]=findset(array[x]);
return array[x];
}
} Time complexity = O(log m), where
m is the number of union operations
done prior to findset( ).
int findset (int x) Findset( )
{ int rep,y=x;
//find the representative node of node x
Do{
rep=x;
x= array[x];
}While (x>0);
X=rep;
//make all the nodes in the path of x to have the representative node of x as their
representative node
While (array[y]<x)
{
rep = y;
y = array[rep];
array[rep] = x;
} return x;
}
int unionset (int x, int y) = unionset(findset(m),findset(n))
{ Unionset( )
if( x!=y)
{ if(array[x] < array[y]) //size comparison of sets (-8<-6)
{ array[x] += array[y];
array[y] = x;}
else { array[y] += array[x];
array[x] = y;}
if(array[x] == array[y])
{ array[x] += array[y];
array[y] = x;}
return 1; //union done
}
else
return 0; //union not possible
Time complexity = O(1)
}
=> A sequence of ‘f’ findset( ) and ‘m’ unionset( ) operations
takes Ɵ(m + fα(f+m, m)).
α is an extremely slow growing function
α is never greater than 4 for any value of ‘f’ & ‘m’.
A D Detecting Cycles in a Graph
B C
A Graph
unionset(1,2);
A D
-1 -1 -1 -1
B C
1 2 3 4
A B C D
Finding cycles in a Graph
A D
B C
A Graph unionset(1,2);
A D
-2 1 -1 -1
B C 1 2 3 4
A B C D
Finding cycles in a Graph
A D
B C
A Graph unionset(2,3);
A D
-2 1 -1 -1
B C 1 2 3 4
A B C D
Finding cycles in a Graph
A D
B C
A Graph unionset(2,3);
A D
-3 1 1 -1
B C 1 2 3 4
A B C D
Finding cycles in a Graph
A D
B C
A Graph unionset(3,4);
A D
-3 1 1 -1
B C 1 2 3 4
A B C D
Finding cycles in a Graph
A D
B C
A Graph unionset(3,4);
A D
-4 1 1 1
B C 1 2 3 4
A B C D
Finding cycles in a Graph
A D
B C unionset(4,1) =unionset(1,1);
returns 0. So cycle detected.
A Graph
A D
-4 1 1 1
B C 1 2 3 4
A B C D
Minimum Spanning Tree
• A Tree is a connected graph without any cycle.
• A tree can be constructed from a graph.
• A tree is a subset of a graph.
A D A D A D
B C B C B C
A Graph
A D
C
B
C
Minimum Spanning Tree
• A Spanning Tree of a graph is a tree that contains all the
nodes of the graph.
A D A D
A D
B C B C
B C
A Graph A D
B C
Minimum Spanning Tree
• Minimum Spanning tree is a spanning tree whose sum of
the weights of all of its edges is minimum.
• Algorithms for MST
• Kruskal’s Algorithm
• Prim’s Algorithm
A
1 D
6 E
3 5
3 1 2
B C F
1 4
Kruskal’s Algorithm
A
1 D
6 E Sort the edges in increasing order
3 5 2 • AD=1
3 1
• BC=1
B C F
1 4 • DC=1
If (Unionset(A,D)) • EF=2
Then add AD Makeset(G)
to result • AB=3
A D E • BD=3
• CF=4
• CE=5
B C F
• DE=6
Minimum Spanning Tree
A
1 D
6 E Sort the edges in increasing order
3
1
5
2 • AD=1
3
B
• BC=1
C F
1 4 • DC=1
• EF=2
1
A D E • AB=3
2 • BD=3
1
B
• CF=4
C F
4 • CE=5
1
• DE=6
Kruskal’s Algorithm
MST-Kruskal(G)
{ mst=empty;
makeset(V); //make disjoin sets using V //O(V)
sort(E); //sort the edges of Graph //O(Elog(E))
for each edge (A,B) from the sorted list of edges //O(E)
{ if (unionset(A,B)) //checking for cycle //~ O(1)
add edge(A,B) to mst; //O(1)
}
return mst;
}
Kruskal’s Algorithm
• If the weights of edges are distinct then MST is unique
• Otherwise there can be many MST.
• Time complexity = O(Elog(E))
• Space Complexity = O(V+E)
Priority Queue
• It is a queue in which each element has a priority.
• Elements with higher priority are processed before the
elements with lower priority.
• If the priorities of the elements are sorted, then enqueue
takes O(n) and dequeue takes O(1).
• If the priorities are not sorted, then enqueue takes O(1)
and dequeue takes O(n).
• The above two implementations are not desirable.
• It can be efficiently implemented using graphs(heap).
Root
Priority Queue
20 Parent/Child 20
45 79
15 19 Leaf
5 7 55 60 87 99
10 9
2 15 3 19
20 15 19
1 2 3
Max Heap
• Dequeue always takes place at root node, i = 1.
• Enqueue always takes place at the rear, that is, at the leaf
node that occurs after the last element of the array.
1 20
2 15 3 19
Max Heap
• Enqueue
1 25
20
30
Q 30
20
25 25
20 25
30
1 2 3
Max Heap
• Enqueue
2 20 25
30
3
Q 30
25 20 25
30
1 2 3
Max Heap
• Dequeue
1 30
18
2 20
3 25
4
18
30
Max Heap
• Dequeue
1 18
25
2 20
3 18
25
Max Heap
Movedown(int i)
• Dequeue {
Dequeue( ) While((2*i )<= n)
{ {Lchild=2*i;
If Q is not Empty Rchild=(2*i+1);
{swap(1, rear); //O(1) If(Q[Lchild]>Q[i])
rear--; largest=Lchild;
Movedown(1); //log(n) Else largest=Rchild;
Swap(i,largest);
}
i=largest;
} }
*n=number of elements in Q }
Prim’s Algorithm
A
1 D
6 EE
3 5
3 1 2
B C F
1 4
P=NIL P=NIL P=NIL MST={ }
K= 0 K= ∞ K= ∞ For each vertex,v, of G
1 6 {Key[v]=∞;
A D E Parent[v]=NIL;
Root
3 5 }
3 1 2 Key[root]=0;
Min-Q=V;
B C F
While(Min-Q is not Empty)
1 4 {u=dequeue(Min-Q);
P=NIL P=NIL P=NIL
If(Parent(u)!=NIL)
K= ∞ K= ∞ K= ∞
MST=Union(MST,(u,Parent(u)));
For each adjacent vertex, v, of vertex u
MST={ } if v is in Min-Q & Weight(u,v)<Key(v)
{Parent[v]=u;
Min-Q ={A,B,C,D,E,F} Key[v]=Weight(u,v);}
}
End
MST={ }
P=NIL P=A
P=NIL P=NIL For each vertex,v, of G
Root K= 0 K= 1∞ K= ∞ {Key[v]=∞;
A
1 D
6 E
Parent[v]=NIL;
}
3 5 Key[root]=0;
3 1 2 Min-Q=V;
B
While(Min-Q is not Empty)
C F
{u=dequeue(Min-Q);
1 4 If(Parent(u)!=NIL)
P=A
P=NIL P=NIL P=NIL
K= 3∞ K= ∞
MST=Union(MST,(u,Parent(u)));
K= ∞
For each adjacent vertex, v, of vertex u
if v is in Min-Q & Weight(u,v)<Key(v)
MST={ } {Parent[v]=u;
Key[v]=Weight(u,v);}
Min-Q ={B,C,D,E,F}
={A,B,C,D,E,F}
={D,B,C,E,F} }
End
MST={ }
P=NIL P=A P=D
P=NIL For each vertex,v, of G
Root K= 0 K= 1 K= 6∞ {Key[v]=∞;
Parent[v]=NIL;
A
1 DD
6 E }
Key[root]=0;
3 5
3 1 2 Min-Q=V;
While(Min-Q is not Empty)
B C F {u=dequeue(Min-Q);
P=A
1 4 If(Parent(u)!=NIL)
P=D
P=NIL P=NIL MST=Union(MST,(u,Parent(u)));
K= 3 K= ∞
1 K= ∞ For each adjacent vertex, v, of vertex u
if v is in Min-Q & Weight(u,v)<Key(v)
{Parent[v]=u;
MST={{A,D}}
MST={ } Key[v]=Weight(u,v);}
Min-Q
Min-Q ={B,C,E,F}
={C,B,E,F}
={D,B,C,E,F} }
End
MST={ }
P=NIL P=A P=D
P=C
P=NIL For each vertex,v, of G
Root K= 0 K= 1 K= 65∞ {Key[v]=∞;
A
1 DD
6 E
Parent[v]=NIL;
}
3 5 Key[root]=0;
3 1 2 Min-Q=V;
B C F While(Min-Q is not Empty)
1 {u=dequeue(Min-Q);
P=C
P=A P=D
P=NIL 4 P=C
P=NIL If(Parent(u)!=NIL)
K= 3
1 K= ∞
1 K= 4∞ MST=Union(MST,(u,Parent(u)));
For each adjacent vertex, v, of vertex u
if v is in Min-Q & Weight(u,v)<Key(v)
MST={{A,D}}
MST={{A,D},{D,C}} {Parent[v]=u;
Key[v]=Weight(u,v);}
Min-Q ={B,E,F}
Min-Q ={C,B,E,F}
={B,F,E} }
End
MST={ }
P=NIL P=A P=D
P=C
P=NIL For each vertex,v, of G
Root K= 0 K= 1 K= 65∞ {Key[v]=∞;
A
1 DD
6 E
Parent[v]=NIL;
}
3 5 Key[root]=0;
3 1 2 Min-Q=V;
B C F While(Min-Q is not Empty)
1 {u=dequeue(Min-Q);
P=C
P=A P=D
P=NIL 4 P=C
P=NIL If(Parent(u)!=NIL)
K= 3
1 K= ∞
1 K= 4∞ MST=Union(MST,(u,Parent(u)));
For each adjacent vertex, v, of vertex u
if v is in Min-Q & Weight(u,v)<Key(v)
MST={{A,D},{D,C},{C,B}}
MST={{A,D},{D,C}} {Parent[v]=u;
Key[v]=Weight(u,v);}
Min-Q ={B,F,E}
={F,E} }
End
MST={ }
P=NIL P=A P=D
P=F
P=C
P=NIL For each vertex,v, of G
Root K= 0 K= 1 K= 65∞
2 {Key[v]=∞;
Parent[v]=NIL;
A
1 DD
6 E }
Key[root]=0;
3 5
3 1 2 Min-Q=V;
While(Min-Q is not Empty)
B C F {u=dequeue(Min-Q);
P=C
P=A
1 4 If(Parent(u)!=NIL)
P=D
P=NIL P=C
P=NIL MST=Union(MST,(u,Parent(u)));
K= 3
1 K= ∞
1 K= 4∞ For each adjacent vertex, v, of vertex u
if v is in Min-Q & Weight(u,v)<Key(v)
MST={{A,D},{D,C},{C,B}}
MST={{A,D},{D,C},{C,B},{C,F }} {Parent[v]=u;
Key[v]=Weight(u,v);}
Min-Q ={F,E}
={E} }
End
MST={ }
For each vertex,v, of G
P=NIL P=A P=D
P=F
P=C
P=NIL
Root K= 0 {Key[v]=∞;
K= 1 K= 5∞
62
Parent[v]=NIL;
A
1 DD
6 EE }
Key[root]=0;
3 5
3 1 2 Min-Q=V;
While(Min-Q is not Empty)
B C F {u=dequeue(Min-Q);
P=C
P=A
1 4 P=C If(Parent(u)!=NIL)
P=NIL
P=D P=NIL MST=Union(MST,(u,Parent(u)));
K= 3
1 K= 1∞
K= K= 4∞ For each adjacent vertex, v, of vertex u
if v is in Min-Q & Weight(u,v)<Key(v)
{Parent[v]=u;
Key[v]=Weight(u,v);}
MST={{A,D},{D,C},{C,B},{C,F }} },{F,E}}
}
End
Min-Q ={E}
={ }
MST={ }
For each vertex,v, of G //O(V)
{Key[v]=∞; Time Complexity
Parent[v]=NIL; = O(Vlog(V)+Elog(V))
}
Key[root]=0; = O(V+E(log(V))
Min-Q=V; //Vlog(V) = O(Elog(V))
While(Min-Q is not Empty)//V
{ u=dequeue(Min-Q); //log(V) Space Complexity
If(Parent(u)!=NIL) = O(2V+V+E)
MST=Union(MST,(u,Parent(u))); //1
For each adjacent-vertex, v, of vertex u //E = O(V+E)
{if v is in Min-Q & Weight(u,v)<Key(v) //1
{Parent[v]=u; //1
Key[v]=Weight(u,v);} //log(V)
}
End
Single Source Shortest Paths
3 4
δ=0 δ=5 6 δ=11 δ= -∞
S 5 D
8 G
C
-3 δ= ∞
7 2
2 I
3 δ= ∞
E F H 3
δ= -∞ -6 J
δ= -∞ -8 δ= ∞
Single Source Shortest Paths
• Paths from S to C
• {S,C}= 5
• {S,C,D,C}=5+6-3 = 8 (a cyclic path with +ve weight)
• {S,C,D,C,D,C}=5+6-3+6-3 = 11
• Infinite paths, so, δ = 5
• Paths from S to E
• {S,E}= 2
• {S,E,F,E}=2+3-6= -1 (a cyclic path with -ve weight)
• {S,E,F,E,F,E}=2+3-6+3-6= - 4
• Infinite paths, so, so, δ = -∞
Relaxation
Relax(u,v,w) Relax(u,v,2)
{ if (d[u]+w(u,v)) < d[v]) if (5+2) < 9)
{ d[v] = d[u]+w(u,v); {
p[v]=u; d[v] = 7
} p[v] = u;
} }
Initialization
Initialize-Single-Source(G, source)
{
For each vertex, v𝜖𝑉
{ d[v]=∞;
p[v]=NIL;
}
d[source]=0;
}
* Time complexity =O(V)
Single Source Shortest Path Algorithms
1. Bellman-Ford algorithm
2. Dijkstra’s algorithm
Bellman-Ford algorithm
P=A
5 P = NIL Bellman-Ford(G,w,s)
d=6 d=∞ {Initialize-single-source(G,s); //O(V)
P = NIL -2
6 B D For i=1 to (V-1) //O(V)
d=0 -3
8 -4
{ For each edge(u,v) v𝜖𝐸 //O(E)
A 2
7 Relax(u,v,w);}
Source
7 C E For each edge(u,v) v𝜖𝐸 //O(E)
PP= =NIL
A 9 P = NIL {If ((d[u]+w(u,v)) < d[v]
dd == ∞
7 d=∞
Return false;}
(A.d + W(A,C)) < C.d Return True;
}
Order of Processing the Edges:
(B,D), (B,C), (B,E), (D,B), (C,D), (C,E), (E,D), (E,A), (A,B), (A,C)
i=2 Bellman-Ford algorithm
P=A
5 PP==NIL
B Bellman-Ford(G,w,s)
d=6 dd == 11
∞ {Initialize-single-source(G,s); //O(V)
P = NIL -2
6 B D For i=1 to (V-1) //O(V)
d=0 -3
8 -4
{ For each edge(u,v) v𝜖𝐸 //O(E)
A 2
7 Relax(u,v,w);}
Source
7 C E For each edge(u,v) v𝜖𝐸 //O(E)
P=A 9 P = NIL {If ((d[u]+w(u,v)) < d[v]
d=7 d=∞
Return false;}
(B.d + W(B,D)) < D.d Return True;
}
Order of Processing the Edges:
(B,D), (B,C), (B,E), (D,B), (C,D), (C,E), (E,D), (E,A), (A,B), (A,C)
i=2 Bellman-Ford algorithm
P=A
5 P=B Bellman-Ford(G,w,s)
d=6 d = 11 {Initialize-single-source(G,s); //O(V)
P = NIL -2
6 B D For i=1 to (V-1) //O(V)
d=0 -3
8 -4
{ For each edge(u,v) v𝜖𝐸 //O(E)
A 2
7 Relax(u,v,w);}
Source
7 C E For each edge(u,v) v𝜖𝐸 //O(E)
P=A 9 P = NIL {If ((d[u]+w(u,v)) < d[v]
d=7 d=∞
Return false;}
(B.d + W(B,C)) < C.d Return True;
}
Order of Processing the Edges:
(B,D), (B,C), (B,E), (D,B), (C,D), (C,E), (E,D), (E,A), (A,B), (A,C)
i=2 Bellman-Ford algorithm
P=A
5 P=B Bellman-Ford(G,w,s)
d=6 d = 11 {Initialize-single-source(G,s); //O(V)
P = NIL -2
6 B D For i=1 to (V-1) //O(V)
d=0 -3
8 -4
{ For each edge(u,v) v𝜖𝐸 //O(E)
A 2
7 Relax(u,v,w);}
Source
7 C E For each edge(u,v) v𝜖𝐸 //O(E)
P=A 9 PP==NIL
B {If ((d[u]+w(u,v)) < d[v]
d=7 dd == ∞
2
Return false;}
(B.d + W(B,E)) < E.d Return True;
}
Order of Processing the Edges:
(B,D), (B,C), (B,E), (D,B), (C,D), (C,E), (E,D), (E,A), (A,B), (A,C)
i=2 Bellman-Ford algorithm
P=A
5 P=B Bellman-Ford(G,w,s)
d=6 d = 11 {Initialize-single-source(G,s); //O(V)
P = NIL -2
6 B D For i=1 to (V-1) //O(V)
d=0 -3
8 -4
{ For each edge(u,v) v𝜖𝐸 //O(E)
A 2
7 Relax(u,v,w);}
Source
7 C E For each edge(u,v) v𝜖𝐸 //O(E)
P=A 9 P=B {If ((d[u]+w(u,v)) < d[v]
d=7 d=2
Return false;}
(D.d + W(D,B)) < B.d Return True;
}
Order of Processing the Edges:
(B,D), (B,C), (B,E), (D,B), (C,D), (C,E), (E,D), (E,A), (A,B), (A,C)
i=2 Bellman-Ford algorithm
P=A
5 B
P=C Bellman-Ford(G,w,s)
d=6 dd== 11
4 {Initialize-single-source(G,s); //O(V)
P = NIL -2
6 B D For i=1 to (V-1) //O(V)
d=0 -3
8 -4
{ For each edge(u,v) v𝜖𝐸 //O(E)
A 2
7 Relax(u,v,w);}
Source
7 C E For each edge(u,v) v𝜖𝐸 //O(E)
P=A 9 P=B {If ((d[u]+w(u,v)) < d[v]
d=7 d=2
Return false;}
(C.d + W(C,D)) < D.d Return True;
}
Order of Processing the Edges:
(B,D), (B,C), (B,E), (D,B), (C,D), (C,E), (E,D), (E,A), (A,B), (A,C)
i=2 Bellman-Ford algorithm
P=A
5 P=C Bellman-Ford(G,w,s)
d=6 d=4 {Initialize-single-source(G,s); //O(V)
P = NIL -2
6 B D For i=1 to (V-1) //O(V)
d=0 -3
8 -4
{ For each edge(u,v) v𝜖𝐸 //O(E)
A 2
7 Relax(u,v,w);}
Source
7 C E For each edge(u,v) v𝜖𝐸 //O(E)
P=A 9 P=B {If ((d[u]+w(u,v)) < d[v]
d=7 d=2
Return false;}
(C.d + W(C,E)) < E.d Return True;
}
Order of Processing the Edges:
(B,D), (B,C), (B,E), (D,B), (C,D), (C,E), (E,D), (E,A), (A,B), (A,C)
i=2 Bellman-Ford algorithm
P=A
5 P=C Bellman-Ford(G,w,s)
d=6 d=4 {Initialize-single-source(G,s); //O(V)
P = NIL -2
6 B D For i=1 to (V-1) //O(V)
d=0 -3
8 -4
{ For each edge(u,v) v𝜖𝐸 //O(E)
A 2
7 Relax(u,v,w);}
Source
7 C E For each edge(u,v) v𝜖𝐸 //O(E)
P=A 9 P=B {If ((d[u]+w(u,v)) < d[v]
d=7 d=2
Return false;}
(E.d + W(E,D)) < D.d Return True;
}
Order of Processing the Edges:
(B,D), (B,C), (B,E), (D,B), (C,D), (C,E), (E,D), (E,A), (A,B), (A,C)
i=2 Bellman-Ford algorithm
P=A
5 P=C Bellman-Ford(G,w,s)
d=6 d=4 {Initialize-single-source(G,s); //O(V)
P = NIL -2
6 B D For i=1 to (V-1) //O(V)
d=0 -3
8 -4
{ For each edge(u,v) v𝜖𝐸 //O(E)
A 2
7 Relax(u,v,w);}
Source
7 C E For each edge(u,v) v𝜖𝐸 //O(E)
P=A 9 P=B {If ((d[u]+w(u,v)) < d[v]
d=7 d=2
Return false;}
(E.d + W(E,A)) < A.d Return True;
}
Order of Processing the Edges:
(B,D), (B,C), (B,E), (D,B), (C,D), (C,E), (E,D), (E,A), (A,B), (A,C)
i=2 Bellman-Ford algorithm
P=A
5 P=C Bellman-Ford(G,w,s)
d=6 d=4 {Initialize-single-source(G,s); //O(V)
P = NIL -2
6 B D For i=1 to (V-1) //O(V)
d=0 -3
8 -4
{ For each edge(u,v) v𝜖𝐸 //O(E)
A 2
7 Relax(u,v,w);}
Source
7 C E For each edge(u,v) v𝜖𝐸 //O(E)
P=A 9 P=B {If ((d[u]+w(u,v)) < d[v]
d=7 d=2
Return false;}
(A.d + W(A,B)) < B.d Return True;
}
Order of Processing the Edges:
(B,D), (B,C), (B,E), (D,B), (C,D), (C,E), (E,D), (E,A), (A,B), (A,C)
i=2 Bellman-Ford algorithm
P=A
5 P=C Bellman-Ford(G,w,s)
d=6 d=4 {Initialize-single-source(G,s); //O(V)
P = NIL -2
6 B D For i=1 to (V-1) //O(V)
d=0 -3
8 -4
{ For each edge(u,v) v𝜖𝐸 //O(E)
A 2
7 Relax(u,v,w);}
Source
7 C E For each edge(u,v) v𝜖𝐸 //O(E)
P=A 9 P=B {If ((d[u]+w(u,v)) < d[v]
d=7 d=2
Return false;}
(A.d + W(A,C)) < C.d Return True;
}
Order of Processing the Edges:
(B,D), (B,C), (B,E), (D,B), (C,D), (C,E), (E,D), (E,A), (A,B), (A,C)
i=3 Bellman-Ford algorithm
P=D
5 P=C Bellman-Ford(G,w,s)
d=2 d=4 {Initialize-single-source(G,s); //O(V)
P = NIL -2
6 B D For i=1 to (V-1) //O(V)
d=0 -3
8 -4
{ For each edge(u,v) v𝜖𝐸 //O(E)
A 2
7 Relax(u,v,w);}
Source
7 C E For each edge(u,v) v𝜖𝐸 //O(E)
P=A 9 P=B {If ((d[u]+w(u,v)) < d[v]
d=7 d=2
Return false;}
Return True;
}
Order of Processing the Edges:
(B,D), (B,C), (B,E), (D,B), (C,D), (C,E), (E,D), (E,A), (A,B), (A,C)
i=4 Bellman-Ford algorithm
P=D
5 P=C Bellman-Ford(G,w,s)
d=2 d=4 {Initialize-single-source(G,s); //O(V)
P = NIL -2
6 B D For i=1 to (V-1) //O(V)
d=0 -3
8 -4
{ For each edge(u,v) v𝜖𝐸 //O(E)
A 2
7 Relax(u,v,w);}
Source
7 C E For each edge(u,v) v𝜖𝐸 //O(E)
P=A 9 P=B {If ((d[u]+w(u,v)) < d[v]
d=7 d = -2
Return false;}
Return True;
}
Order of Processing the Edges:
(B,D), (B,C), (B,E), (D,B), (C,D), (C,E), (E,D), (E,A), (A,B), (A,C)
For each Edge Bellman-Ford algorithm
5 Bellman-Ford(G,w,s)
P=D P=C
{Initialize-single-source(G,s); //O(V)
d=2 d=4
P = NIL -2 For i=1 to (V-1) //O(V)
d=0 6 B -3
D { For each edge(u,v) v𝜖𝐸 //O(E)
8 -4
A 7 Relax(u,v,w);}
Source 2
For each edge(u,v) v𝜖𝐸 //O(E)
7 C E
P=A
{If ((d[u]+w(u,v)) < d[v]
9 P=B
d=7 d = -2 Return false;}
Return True;
}
Order of Processing the Edges: //TC=O(VE), SC=O(V+E)
(B,D), (B,C), (B,E), (D,B), (C,D), (C,E), (E,D), (E,A), (A,B), (A,C)
Bellman-Ford algorithm
Bellman-Ford(G,w,s)
{Initialize-single-source(G,s); //O(V)
For i=1 to (V-1) //O(V)
{ For each edge(u,v) v𝜖𝐸 //O(E)
Relax(u,v,w);} //O(1)
For each edge(u,v) v𝜖𝐸 //O(E)
{If ((d[u]+w(u,v)) < d[v]
Return false;}
Return True;
}
//TC = O(VE+E) = O(VE),
SC = O(2V+E) = O(V+E)
Bellman-Ford algorithm
Home Work
4 Find the shortest path from
Source A B node A to all other nodes?
5
5 -14
D C Order of Processing the Edges:
3
(C,B), (D,C), (A,D), (A,B), (D,B)
Bellman-Ford algorithm
Algorithm to print the shortest Path
Print_Path(G,s,v)
{
If v == s
Print s;
Elseif p[v] == NIL
Print “No path from source to vertex”;
Else Print_Path(G,s,p[v]);
Print v;
}
Dijkstra’s algorithm
Min-Q
Min-Q=={B,D}
{D} If(B.d + W(B,C)) <
If(B.d + W(B,D)) < D.dC.d
P=C
1 P=B Dijkstra(G,w,s)
d=8 d=9 {
P = NIL Initialize-single-source(G,s); //O(V)
d=0
10 B D SP={empty}; //shortest path set
3 9
A 2 4 Min-Q=V; //O(V)
Source 6 While(Min-Q is not Empty) //O(V)
7 { u=dequeue(Min-Q); //O(log(V))
5
C E SP=Union(SP,u); //O(1)
2 P=C
For each adjacent vertex,v, of vertex u //O(E)
P=A
d=5 d=7
Relax(u,v,w); //O(log(v))
}
}
SP=={A,C,E,B}
SP {A,C,E,B,D}
Min-Q=={D}
Min-Q { } If(D.d + W(D,E)) < D.d
Dijkstra(G,w,s)
{
Initialize-single-source(G,s); //O(V)
SP={empty}; //shortest path set
Min-Q=V; //O(V)
While(Min-Q is not Empty) //O(V)
{ u=dequeue(Min-Q); //O(log(V))
SP=Union(SP,u); //O(1)
For each adjacent vertex,v, of vertex u //O(E)
Relax(u,v,w); //O(log(v))
}
}
// TC = O(Vlog(V) + Elog(V)) = O((V+E)log(V)) = O(Elog(V))
// SC = O(2V+2V) = O(V)
All Pairs Shortest Paths
k
2 3
i j
8
Path = {i,j};
If (dist[i,k]+dist[k,j]) < dist[i,j])
dist[i,j] = dist[i,k]+dist[k,j]); where k = 1 to V
update the path as {i,k,j};
If (k=0),
D[i,j] = W[i,j];
2
3 4 D(k=0) = 1 2 3 4 5
1 0
8
1 3 2
3
2 4
-4 1
7 -5 5
6 Pj(k=0) = 1 2 3 4 5
5 4 1 Nil
If (i = = j) OR W[i,j] = = ∞), 2
Pj[i,j] = Nil; 3
Pj[i,j] = i; 5
If (k=0),
D[i,j] = W[i,j];
2
3 4 D(k=0) = 1 2 3 4 5
1 0 3 8 ∞ -4
8
1 3 2 ∞ 0 ∞ 1 7
3 ∞ 4 0 ∞ ∞
2 4 2 ∞ -5 0 ∞
-4 1 ∞ ∞ ∞ 6 0
7 -5 5
6 Pj(k=0) = 1 2 3 4 5
5 4 1 Nil 1 1 Nil 1
i=1 0 3 8 ∞ -4
8
1 3 2 ∞ 0 ∞ 1 7
3 ∞ 4 0 ∞ ∞
2 4 2 ∞ -5 0 ∞
-4 1 ∞ ∞ ∞ 6 0
7 -5 5
5
6
4 Pj(k=1) = 1 2 3 4 5
1 Nil 1 1 Nil 1
2 Nil Nil Nil 2 2
3 Nil 3 Nil Nil Nil
4 4 Nil 4 Nil Nil
5 Nil Nil Nil 5 Nil
If (D[i,k]+D[k,j]) < D[i,j]),
D[i,j] = D[i,k]+D[k,j]); If (k >= 1), Pj[i,j] = Pj[k,j];
2
3 4 D(k=1) = j=1 2 3 4 5
i=1 0 3 8 ∞ -4
8
1 3 2 ∞ 0 ∞ 1 7
3 ∞ 4 0 ∞ ∞
2 4 2 ∞ -5 0 ∞
-4 1 ∞ ∞ ∞ 6 0
7 -5 5
6 Pj(k=0) = 1 2 3 4 5
5 4 1 Nil 1 1 Nil 1
2 Nil Nil Nil 2 2
3 Nil 3 Nil Nil Nil
4 4 Nil 4 Nil Nil
5 Nil Nil Nil 5 Nil
If (D[i,k]+D[k,j]) < D[i,j]),
D[i,j] = D[i,k]+D[k,j]); If (k >= 1), Pj[i,j] = Pj[k,j];
2
3 4 D(k=1) = j=1 2 3 4 5
i=1 0 3 8 ∞ -4
8
1 3 2 ∞ 0 ∞ 1 7
3 ∞ 4 0 ∞ ∞
2 4 2 ∞ -5 0 ∞
-4 1 ∞ ∞ ∞ 6 0
7 -5 5
6 Pj(k=0) = 1 2 3 4 5
5 4 1 Nil 1 1 Nil 1
2 Nil Nil Nil 2 2
3 Nil 3 Nil Nil Nil
4 4 Nil 4 Nil Nil
5 Nil Nil Nil 5 Nil
If (D[i,k]+D[k,j]) < D[i,j]),
D[i,j] = D[i,k]+D[k,j]); If (k >= 1), Pj[i,j] = Pj[k,j];
2
3 4 D(k=1) = j=1 2 3 4 5
i=1 0 3 8 ∞ -4
8
1 3 2 ∞ 0 ∞ 1 7
3 ∞ 4 0 ∞ ∞
2 4 2 ∞ -5 0 ∞
-4 1 ∞ ∞ ∞ 6 0
7 -5 5
6 Pj(k=0) = 1 2 3 4 5
5 4 1 Nil 1 1 Nil 1
2 Nil Nil Nil 2 2
3 Nil 3 Nil Nil Nil
4 4 Nil 4 Nil Nil
5 Nil Nil Nil 5 Nil
If (D[i,k]+D[k,j]) < D[i,j]),
D[i,j] = D[i,k]+D[k,j]); If (k >= 1), Pj[i,j] = Pj[k,j];
2
3 4 D(k=1) = j=1 2 3 4 5
i=1 0 3 8 ∞ -4
8
1 3 2 ∞ 0 ∞ 1 7
3 ∞ 4 0 ∞ ∞
2 4 2 5 -5 0 ∞
-4 1 ∞ ∞ ∞ 6 0
7 -5 5
6 Pj(k=0) = 1 2 3 4 5
5 4 1 Nil 1 1 Nil 1
2 Nil Nil Nil 2 2
3 Nil 3 Nil Nil Nil
4 4 1 4 Nil Nil
5 Nil Nil Nil 5 Nil
If (D[i,k]+D[k,j]) < D[i,j]),
D[i,j] = D[i,k]+D[k,j]); If (k >= 1), Pj[i,j] = Pj[k,j];
2
3 4 D(k=1) = j=1 2 3 4 5
i=1 0 3 8 ∞ -4
8
1 3 2 ∞ 0 ∞ 1 7
3 ∞ 4 0 ∞ ∞
2 4 2 5 -5 0 -2
-4 1 ∞ ∞ ∞ 6 0
7 -5 5
6 Pj(k=1) = 1 2 3 4 5
5 4 1 Nil 1 1 Nil 1
2 Nil Nil Nil 2 2
3 Nil 3 Nil Nil Nil
4 4 1 4 Nil 1
5 Nil Nil Nil 5 Nil
If (D[i,k]+D[k,j]) < D[i,j]),
D[i,j] = D[i,k]+D[k,j]); If (k >= 1), Pj[i,j] = Pj[k,j];
2
3 4 D(k=1) = j=1 2 3 4 5
i=1 0 3 8 ∞ -4
8
1 3 2 ∞ 0 ∞ 1 7
3 ∞ 4 0 ∞ ∞
2 4 2 5 -5 0 -2
-4 1 ∞ ∞ ∞ 6 0
7 -5 5
6 Pj(k=1) = 1 2 3 4 5
5 4 1 Nil 1 1 Nil 1
2 Nil Nil Nil 2 2
3 Nil 3 Nil Nil Nil
4 4 1 4 Nil 1
5 Nil Nil Nil 5 Nil
If (D[i,k]+D[k,j]) < D[i,j]),
D[i,j] = D[i,k]+D[k,j]); If (k >= 1), Pj[i,j] = Pj[k,j];
2
3 4 D(k=2) = j=1 2 3 4 5
i=1 0 3 8 4 -4
8
1 3 2 ∞ 0 ∞ 1 7
3 ∞ 4 0 5 11
2 4 2 5 -5 0 -2
-4 1 ∞ ∞ ∞ 6 0
7 -5 5
6 Pj(k=2) = 1 2 3 4 5
5 4 1 Nil 1 1 2 1
2 Nil Nil Nil 2 2
3 Nil 3 Nil 2 2
4 4 1 4 Nil 1
5 Nil Nil Nil 5 Nil
If (D[i,k]+D[k,j]) < D[i,j]),
D[i,j] = D[i,k]+D[k,j]); If (k >= 1), Pj[i,j] = Pj[k,j];
2
3 4 D(k=3) = j=1 2 3 4 5
i=1 0 3 8 4 -4
8
1 3 2 ∞ 0 ∞ 1 7
3 ∞ 4 0 5 11
2 4 2 -1 -5 0 -2
-4 1 ∞ ∞ ∞ 6 0
7 -5 5
6 Pj(k=3) = 1 2 3 4 5
5 4 1 Nil 1 1 2 1
2 Nil Nil Nil 2 2
3 Nil 3 Nil 2 2
4 4 3 4 Nil 1
5 Nil Nil Nil 5 Nil
If (D[i,k]+D[k,j]) < D[i,j]),
D[i,j] = D[i,k]+D[k,j]); If (k >= 1), Pj[i,j] = Pj[k,j];
2
3 4 D(k=4) = j=1 2 3 4 5
i=1 0 3 -1 4 -4
8
1 3 2 3 0 -4 1 -1
3 7 4 0 5 3
2 4 2 -1 -5 0 -2
-4 1 8 5 1 6 0
7 -5 5
6 Pj(k=4) = 1 2 3 4 5
5 4 1 Nil 1 4 2 1
2 4 Nil 4 2 1
3 4 3 Nil 2 1
4 4 3 4 Nil 1
5 4 3 4 5 Nil
If (D[i,k]+D[k,j]) < D[i,j]),
D[i,j] = D[i,k]+D[k,j]); If (k >= 1), Pj[i,j] = Pj[k,j];
2
3 4 D(k=5) = j=1 2 3 4 5
i=1 0 1 -3 2 -4
8
1 3 2 3 0 -4 1 -1
3 7 4 0 5 3
2 4 2 -1 -5 0 -2
-4 1 8 5 1 6 0
7 -5 5
6 Pj(k=5) = j=1 2 3 4 5
5 4 i=1 Nil 3 4 5 1
2 4 Nil 4 2 1
3 4 3 Nil 2 1
4 4 3 4 Nil 1
5 4 3 4 5 Nil
All Pairs Shortest Paths
Floyd-Warshall Algorithm
Floyd-Warshall(W) Time
{
// Initialization Complexity:
For i=1 to V //O(V)
For j=1 to V //O(V) =O(V2+V3)
{D[i,j] = W[i,j]; =O(V3)
If (i = = j) OR W[i,j] = = ∞) //O(1)
Pj[i,j] = Nil; //O(1)
If (i ≠ j) AND W[i,j] < ∞) //O(1) Space
Pj[i,j] = i;} //O(1)
//Computation of All Pair Shortest Path Complexity:
for k=1 to V //O(V)
for i=1 to V //O(V) =O(V2 +V2)
for j=1 to V //O(V) =O(V2)
{If (D[i,k]+D[k,j]) < D[i,j]), //O(1)
D[i,j] = D[i,k]+D[k,j]); //O(1)
If (k >= 1), Pj[i,j] = Pj[k,j]; //O(1)
}
}
Tree Data Structure
Sibling
Nodes
Root Node Right Sub-
Left Sub- A Tree
Tree
B C
D E F G
For a Rooted Tree,T, with root, r:
• Depth of a vertex, v:
• Depth(v)=length of path from root, r, to vertex, v.
• Height of a vertex, v:
• Height(v)=length of the longest path from vertex, v, to
a leaf.
• Depth of the Tree,T:
• Depth(T)=Depth of the deepest vertex.
• Height of the Tree, T:
• Height(T)=Height( r );
For a Rooted Tree,T, with root, r:
• Level of a vertex, v:
• Level(v)=Height(T) – Depth(v)
Depth=0 Height=2
Book
Depth=1
Height=1
Chap-1 Chap-2 Chap-3
Depth=2 Height=0
Section-1.1 Section-1.2
Level=2
Book
Level=1
Level=0
Section-1.1 Section-1.2
A
B
C
B C
D E G
Ternary Tree
• Its a tree in which each node can have either 0, 1, 2 or 3
child nodes.
A
B C
D E F G
m-ary Tree
• Its a tree in which each node can have either 0,1,2,3…m
child nodes.
B C
D E .............. . Z G
Full m-ary Tree
• Its a m-ary tree in which each non-leaf/internal node has
exactly m child nodes.
• If m=2, then it’s a full binary tree
A
B C
D E
• For a Full m-ary tree, T, with ‘n’ nodes:
• Height(T) = floor(logm(n)).
B C Level=1
D E Level=0
• For a Full m-ary tree, T, with ‘n’ nodes:
(𝑛−1)Τ
• No. of internal vertices, i, = 𝑚
𝑚−1 𝑛+1Τ
• No. of leaf nodes, L, = 𝑚
• No. of nodes, n, = mi+1
• L=(mi-1)+1
(𝑚𝐿−1)
• n= ൗ(𝑚−1)
(𝐿−1)
• i= ൗ(𝑚−1)
Complete m-ary Tree
• Its a m-ary tree in which each depth is filled from left to
right and the next depth can only be filled when the current
depth is completely filled.
• If m=2, then its complete binary tree
A Level=2
C Level=1
B
D E F Level=0
Full and Complete m-ary Tree
A Level=2
C Level=1
B
D E F G Level=0
Balanced m-ary Tree
• Its a m-ary tree in which all the leaf nodes are at level 0 0r
1.
• If m=2, then its balanced binary tree
A Level=2
C Level=1
B
D E Level=0
• For a full & complete m-ary tree, T, with ‘n’ nodes:
• Height(T) = floor(logm(n)).
B C Level=1
D E F G Level=0
• For a full & complete m-ary tree, T, with height ‘h’:
• No. of leaf nodes = mh.
B C Level=1
D E F G Level=0
• For a full & complete m-ary tree:
• The number of nodes(NN) at level, L, = m(Height(T) - L).
• The number of nodes at Depth, D, = m(D).
• If m=2;
Level=2 A NN=2(2-2)=1
Level=1 NN=2(2-1)=2
B C
Level=0
D E F G NN=2(2-0)=4
Tree Data Structure
Set of Values:
• Domain Specific.
Set of Operations:
1. Insert
2. Delete
3. Search/Traversals
Implementation
1. Array
2. Linked List
Binary Tree
Implementation
1. Array
2. Linked List
Array implementation of Binary Tree
Root, i = 1 A
Left Child =2i
Right Child =2i+1
Parent =i/2 B C
D E F G
0 1 2 3 4 5 6 7
A B C D E F G
Array implementation of Binary Tree
Root, i = 1
Left Child =2i A
Right Child =2i+1
Parent =i/2
B C
D G
0 1 2 3 4 5 6 7
A B C D G
Linked List implementation of Binary Tree
Data A
LPtr RPtr
B C
A
B C
Null Null Null Null
Tree Traversals
B C
Pre-Order Traversal: A B C
1. Pre-Order Tree Traversal
1. Visit Root node
A
2. Visit Left Child/Sub-Tree
3. Visit Right Child/Sub-Tree
B C
D E F G
Pre-Order Traversal: A B D E C F G
Non-Recursive Pre-Order Tree Traversal using Stack
CreateEmptyStack(S); Root A 1
Push(S,Null);
ptr = Root;
While (ptr ≠ Null) B 2 C 3
{ print(ptr->data;
If(ptr->right ≠ Null) D E F G
Push(S,ptr->right); Null 4 5 6 7
If(ptr->left ≠ Null)
Ptr = ptr->left; Stack, S
Else Pre-Order Traversal:
Ptr = Pop(S) ptr = 1
}
Non-Recursive Pre-Order Tree Traversal using Stack
CreateEmptyStack(S); Root A 1
Push(S,Null);
ptr = Root;
While (ptr ≠ Null) B 2 C 3
{ print(ptr->data);
If(ptr->right ≠ Null) 3 D E F G
Push(S,ptr->right); Null 4 5 6 7
If(ptr->left ≠ Null)
Ptr = ptr->left; Stack, S
Pre-Order Traversal:
Else
Ptr = Pop(S) ptr
ptr==12 A
}
Non-Recursive Pre-Order Tree Traversal using Stack
CreateEmptyStack(S); Root A 1
Push(S,Null);
ptr = Root;
While (ptr ≠ Null) B 2 C 3
{ print(ptr->data; 5
If(ptr->right ≠ Null) 3 D E F G
Push(S,ptr->right); Null 4 5 6 7
If(ptr->left ≠ Null)
Ptr = ptr->left; Stack, S
Pre-Order Traversal:
Else
ptr = 2
Ptr = Pop(S) A B
} ptr = 4
Non-Recursive Pre-Order Tree Traversal using Stack
CreateEmptyStack(S); Root A 1
Push(S,Null);
ptr = Root;
While (ptr ≠ Null) B 2 C 3
{ print(ptr->data; 5
If(ptr->right ≠ Null) 3 D E F G
Push(S,ptr->right); Null 4 5 6 7
If(ptr->left ≠ Null)
Ptr = ptr->left; Stack, S
Pre-Order Traversal:
Else
ptr = 4
Ptr = Pop(S) A B D
} ptr = 5
Non-Recursive Pre-Order Tree Traversal using Stack
CreateEmptyStack(S); Root A 1
Push(S,Null);
ptr = Root;
While (ptr ≠ Null) B 2 C 3
{ print(ptr->data;
If(ptr->right ≠ Null) 3 D E F G
Push(S,ptr->right); Null 4 5 6 7
If(ptr->left ≠ Null)
Ptr = ptr->left; Stack, S
Pre-Order Traversal:
Else
ptr = 5
Ptr = Pop(S) A B D E
} ptr = 3
Non-Recursive Pre-Order Tree Traversal using Stack
CreateEmptyStack(S); Root A 1
Push(S,Null);
ptr = Root;
While (ptr ≠ Null) B 2 C 3
{ print(ptr->data;
If(ptr->right ≠ Null) 7 D E F G
Push(S,ptr->right); Null 4 5 6 7
If(ptr->left ≠ Null)
Ptr = ptr->left; Stack, S
Pre-Order Traversal:
Else
ptr = 3
Ptr = Pop(S) A B D E C
} ptr = 6
Non-Recursive Pre-Order Tree Traversal using Stack
CreateEmptyStack(S); Root A 1
Push(S,Null);
ptr = Root;
While (ptr ≠ Null) B 2 C 3
{ print(ptr->data;
If(ptr->right ≠ Null) 7 D E F G
Push(S,ptr->right); Null 4 5 6 7
If(ptr->left ≠ Null)
Ptr = ptr->left; Stack, S
Pre-Order Traversal:
Else
ptr = 6
Ptr = Pop(S) A B D E C F
} ptr = 7
Non-Recursive Pre-Order Tree Traversal using Stack
CreateEmptyStack(S); Root A 1
Push(S,Null);
ptr = Root;
While (ptr ≠ Null) B 2 C 3
{ print(ptr->data);
If(ptr->right ≠ Null) D E F G
Push(S,ptr->right); Null 4 5 6 7
If(ptr->left ≠ Null)
Ptr = ptr->left; Stack, S
Pre-Order Traversal:
Else
ptr = 7
Ptr = Pop(S) A B D E C F G
} ptr = Null
Recursive Pre-Order Tree Traversal
Root A 1
B C
In-Order Traversal: B A C
2. In-Order Tree Traversal
A
1. Visit Left Child/Sub-Tree
2. Visit Root node B C
3. Visit Right Child/Sub-Tree
D E F G
In-Order Traversal: D B E A F C G
CreateEmptyStack(S);
Push(S,Null);
Non-Recursive In-Order
Ptr=Root;
Done=false;
Tree Traversal
While(done == false)
{While(ptr ≠ null) Root A 1
{Push(S, ptr);
ptr = ptr->left; 2 3
4
} B C
ptr = Pop(S);
2
flag = true;
D E F G
While( (ptr ≠ null) and (flag ==true))
{ print(ptr->data);
1
4 5 6 7
if(ptr->right ≠ null)
{ ptr = ptr->right;
Null
flag = false;
} Stack, S
else
ptr = Pop(S);
In-Order Traversal:
} ptr = 1
If( ptr = = Null)
Done = true; D
} ptr = 4
CreateEmptyStack(S);
Push(S,Null);
Non-Recursive In-Order
Ptr=Root;
Done=false;
Tree Traversal
While(done == false)
{While(ptr ≠ null) Root A 1
{Push(S, ptr);
ptr = ptr->left; 2 3
} B C
ptr = Pop(S);
2
flag = true;
D E F G
While( (ptr ≠ null) and (flag ==true))
{ print(ptr->data);
1
4 5 6 7
if(ptr->right ≠ null)
{ ptr = ptr->right;
Null
flag = false;
} Stack, S
else
ptr = Pop(S);
In-Order Traversal:
} ptr==54
ptr
If( ptr = = Null)
Done = true; D B
} ptr = 2
CreateEmptyStack(S);
Push(S,Null);
Non-Recursive In-Order
Ptr=Root;
Done=false;
Tree Traversal
While(done == false)
{While(ptr ≠ null) Root A 1
{Push(S, ptr);
ptr = ptr->left; 2 3
} B C
ptr = Pop(S); 5
flag = true;
D E F G
While( (ptr ≠ null) and (flag ==true))
{ print(ptr->data);
1
4 5 6 7
if(ptr->right ≠ null)
{ ptr = ptr->right;
Null
flag = false;
} Stack, S
else
ptr = Pop(S);
In-Order Traversal:
} ptr = 5
If( ptr = = Null)
Done = true; D B
} ptr = Null
CreateEmptyStack(S);
Push(S,Null);
Non-Recursive In-Order
Ptr=Root;
Done=false;
Tree Traversal
While(done == false)
{While(ptr ≠ null) Root A 1
{Push(S, ptr);
ptr = ptr->left; 2 3
} B C
ptr = Pop(S); 5
flag = true;
D E F G
While( (ptr ≠ null) and (flag ==true))
{ print(ptr->data);
1
4 5 6 7
if(ptr->right ≠ null)
{ ptr = ptr->right;
Null
flag = false;
} Stack, S
else
ptr = Pop(S);
In-Order Traversal:
} ptr = Null
If( ptr = = Null)
Done = true; D B E
} ptr = 5
CreateEmptyStack(S);
Push(S,Null);
Non-Recursive In-Order
Ptr=Root;
Done=false;
Tree Traversal
While(done == false)
{While(ptr ≠ null) Root A 1
{Push(S, ptr);
ptr = ptr->left; 2 3
} B C
ptr = Pop(S);
flag = true;
D E F G
While( (ptr ≠ null) and (flag ==true))
{ print(ptr->data);
1
4 5 6 7
if(ptr->right ≠ null)
{ ptr = ptr->right;
Null
flag = false;
} Stack, S
else
ptr = Pop(S);
In-Order Traversal:
} ptr = 5
If( ptr = = Null)
Done = true; D B E
} ptr = 1
CreateEmptyStack(S);
Push(S,Null);
Non-Recursive In-Order
Ptr=Root;
Done=false;
Tree Traversal
While(done == false)
{While(ptr ≠ null) Root A 1
{Push(S, ptr);
ptr = ptr->left; 2 3
} B C
ptr = Pop(S);
flag = true;
D E F G
While( (ptr ≠ null) and (flag ==true))
{ print(ptr->data); 4 5 6 7
if(ptr->right ≠ null)
{ ptr = ptr->right;
Null
flag = false;
} Stack, S
else
ptr = Pop(S);
In-Order Traversal:
} ptr = 1
If( ptr = = Null)
Done = true; D B E A
} ptr = 3
CreateEmptyStack(S);
Push(S,Null);
Non-Recursive In-Order
Ptr=Root;
Done=false;
Tree Traversal
While(done == false)
{While(ptr ≠ null) Root A 1
{Push(S, ptr);
ptr = ptr->left; 2 3
} B C
ptr = Pop(S);
flag = true;
D E F G
While( (ptr ≠ null) and (flag ==true))
{ print(ptr->data);
3
4 5 6 7
if(ptr->right ≠ null)
{ ptr = ptr->right;
Null
flag = false;
} Stack, S
else
ptr = Pop(S);
In-Order Traversal:
} ptr = 3
If( ptr = = Null)
Done = true; D B E A
} ptr = 6
CreateEmptyStack(S);
Push(S,Null);
Non-Recursive In-Order
Ptr=Root;
Done=false;
Tree Traversal
While(done == false)
{While(ptr ≠ null) Root A 1
{Push(S, ptr);
ptr = ptr->left; 2 3
} B C
ptr = Pop(S); 6
flag = true;
D E F G
While( (ptr ≠ null) and (flag ==true))
{ print(ptr->data);
3
4 5 6 7
if(ptr->right ≠ null)
{ ptr = ptr->right;
Null
flag = false;
} Stack, S
else
ptr = Pop(S);
In-Order Traversal:
} ptr = 6
If( ptr = = Null)
Done = true; D B E A
} ptr = 6
CreateEmptyStack(S);
Push(S,Null);
Non-Recursive In-Order
Ptr=Root;
Done=false;
Tree Traversal
While(done == false)
{While(ptr ≠ null) Root A 1
{Push(S, ptr);
ptr = ptr->left; 2 3
} B C
ptr = Pop(S);
flag = true;
D E F G
While( (ptr ≠ null) and (flag ==true))
{ print(ptr->data);
3
4 5 6 7
if(ptr->right ≠ null)
{ ptr = ptr->right;
Null
flag = false;
} Stack, S
else
ptr = Pop(S);
In-Order Traversal:
} ptr = 6
If( ptr = = Null)
Done = true; D B E A F C
} ptr = 3
CreateEmptyStack(S);
Push(S,Null);
Non-Recursive In-Order
Ptr=Root;
Done=false;
Tree Traversal
While(done == false)
{While(ptr ≠ null) Root A 1
{Push(S, ptr);
ptr = ptr->left; 2 3
} B C
ptr = Pop(S);
flag = true;
D E F G
While( (ptr ≠ null) and (flag ==true))
{ print(ptr->data);
7
4 5 6 7
if(ptr->right ≠ null)
{ ptr = ptr->right;
Null
flag = false;
} Stack, S
else
ptr = Pop(S);
In-Order Traversal:
} ptr = 3
If( ptr = = Null)
Done = true; D B E A F C
} ptr = 7
CreateEmptyStack(S);
Push(S,Null);
Non-Recursive In-Order
Ptr=Root;
Done=false;
Tree Traversal
While(done == false)
{While(ptr ≠ null) Root A 1
{Push(S, ptr);
ptr = ptr->left; 2 3
} B C
ptr = Pop(S);
flag = true;
D E F G
While( (ptr ≠ null) and (flag ==true))
{ print(ptr->data);
7
4 5 6 7
if(ptr->right ≠ null)
{ ptr = ptr->right;
Null
flag = false;
} Stack, S
else
ptr = Pop(S);
In-Order Traversal:
} ptr = 7
If( ptr = = Null)
Done = true; D B E A F C G
} ptr = Null
Recursive In-Order Tree Traversal
Void InOrder(Root)
{
If(Root ≠ Null)
Root A 1
{
2
InOrder(Root->left); C
3
B
Print(Root->data);
InOrder(Right->right); D E F G
4
} 5 6 7
}
3. Post-Order Tree Traversal
A
1. Visit Left Child/Sub-Tree
2. Visit Right Child/Sub-Tree B C
3. Visit Root node
D E F G
Post-Order Traversal: D E B F G C A
Non-Recursive Post-Order Tree Traversal using Stack
//Initialization //Traversal //Printing
CreateEmptyStack(S1); S1.Push1(ptr); ptr = S2.Pop2( );
CreateEmptyStack(S2); ptr = S1.Pop1( ); While(ptr ≠ Null)
Char flag; While(ptr ≠ Null) {
S1.Push1(Null); {S2.Push2(ptr); Print(ptr->data);
S2.Push2(Null); if(ptr->left ≠ Null) ptr = S2.Pop2( );
Ptr=root; S1.Push1(ptr->left); }
if(ptr->right ≠ Null)
S1.Push1(ptr->right);
ptr = S1.Pop1( );
}
}
Non-Recursive Post-Order Tree Traversal using Stack
//Initialization Root A 1
CreateEmptyStack(S1);
CreateEmptyStack(S2);
Char flag;
B 2 C 3
S1.Push1(Null);
S2.Push2(Null); D E F G
Ptr=root; Null 4 5 6 7
Null
S1 S2
ptr = 1
Non-Recursive Post-Order Tree Traversal using Stack
//Traversal Root A 1
S1.Push1(ptr);
ptr = S1.Pop1( );
While(ptr ≠ Null) B 2 C 3
{S2.Push2(ptr);
if(ptr->left ≠ Null) F G
D E
S1.Push1(ptr->left); 1
if(ptr->right ≠ Null) Null Null 4 5 6 7
S1.Push1(ptr->right);
ptr = S1.Pop1( ); S1 S2
}
} ptr = 1
Non-Recursive Post-Order Tree Traversal using Stack
//Traversal Root A 1
S1.Push1(ptr);
ptr = S1.Pop1( );
While(ptr ≠ Null) B 2 C 3
{S2.Push2(ptr);
if(ptr->left ≠ Null) F G
1 D E
S1.Push1(ptr->left);
if(ptr->right ≠ Null) Null Null 4 5 6 7
S1.Push1(ptr->right);
ptr = S1.Pop1( ); S1 S2
}
} ptr = 1
Non-Recursive Post-Order Tree Traversal using Stack
//Traversal Root A 1
S1.Push1(ptr);
ptr = S1.Pop1( );
While(ptr ≠ Null) B 2 C 3
{S2.Push2(ptr);
if(ptr->left ≠ Null) 3
D E F G
S1.Push1(ptr->left); 2 1
if(ptr->right ≠ Null) Null Null 4 5 6 7
S1.Push1(ptr->right);
ptr = S1.Pop1( ); S1 S2
}
} ptr = 1
Non-Recursive Post-Order Tree Traversal using Stack
//Traversal Root A 1
S1.Push1(ptr);
ptr = S1.Pop1( );
While(ptr ≠ Null) B 2 C 3
{S2.Push2(ptr);
if(ptr->left ≠ Null) F G
1 D E
S1.Push1(ptr->left); 2
if(ptr->right ≠ Null) Null Null 4 5 6 7
S1.Push1(ptr->right);
ptr = S1.Pop1( ); S1 S2
}
} ptr = 3
Non-Recursive Post-Order Tree Traversal using Stack
//Traversal Root A 1
S1.Push1(ptr);
ptr = S1.Pop1( );
While(ptr ≠ Null) B 2 C 3
{S2.Push2(ptr);
if(ptr->left ≠ Null)
3
D E F G
S1.Push1(ptr->left); 2 1
if(ptr->right ≠ Null) Null Null 4 5 6 7
S1.Push1(ptr->right);
ptr = S1.Pop1( ); S1 S2
}
} ptr = 3
Non-Recursive Post-Order Tree Traversal using Stack
//Traversal Root A 1
S1.Push1(ptr);
ptr = S1.Pop1( );
While(ptr ≠ Null) 7 B 2 C 3
{S2.Push2(ptr);
if(ptr->left ≠ Null) 6 3
D E F G
S1.Push1(ptr->left); 2 1
if(ptr->right ≠ Null) Null Null 4 5 6 7
S1.Push1(ptr->right);
ptr = S1.Pop1( ); S1 S2
}
} ptr = 3
Non-Recursive Post-Order Tree Traversal using Stack
//Traversal Root A 1
S1.Push1(ptr);
ptr = S1.Pop1( );
While(ptr ≠ Null) B 2 C 3
{S2.Push2(ptr);
if(ptr->left ≠ Null) 6 3
D E F G
S1.Push1(ptr->left); 2 1
if(ptr->right ≠ Null) Null Null 4 5 6 7
S1.Push1(ptr->right);
ptr = S1.Pop1( ); S1 S2
}
} ptr = 7
Non-Recursive Post-Order Tree Traversal using Stack
//Traversal Root A 1
S1.Push1(ptr);
ptr = S1.Pop1( );
While(ptr ≠ Null) 7 B 2 C 3
{S2.Push2(ptr);
if(ptr->left ≠ Null) 6 3
D E F G
S1.Push1(ptr->left); 2 1
if(ptr->right ≠ Null) Null Null 4 5 6 7
S1.Push1(ptr->right);
ptr = S1.Pop1( ); S1 S2
}
} ptr = 7
Non-Recursive Post-Order Tree Traversal using Stack
//Traversal Root A 1
S1.Push1(ptr);
ptr = S1.Pop1( );
While(ptr ≠ Null) 7 B 2 C 3
{S2.Push2(ptr);
if(ptr->left ≠ Null)
3
D E F G
S1.Push1(ptr->left); 2 1
if(ptr->right ≠ Null) Null Null 4 5 6 7
S1.Push1(ptr->right);
ptr = S1.Pop1( ); S1 S2
}
} ptr = 6
Non-Recursive Post-Order Tree Traversal using Stack
//Traversal Root A 1
S1.Push1(ptr);
ptr = S1.Pop1( );
While(ptr ≠ Null) B 2 C 3
{S2.Push2(ptr);
if(ptr->left ≠ Null) 6 F G
D E
S1.Push1(ptr->left); 7
if(ptr->right ≠ Null) 4 5 6 7
S1.Push1(ptr->right); 3
ptr = S1.Pop1( ); 2 1
} Null Null ptr = 6
}
S1 S2
Non-Recursive Post-Order Tree Traversal using Stack
//Traversal Root A 1
S1.Push1(ptr);
ptr = S1.Pop1( );
While(ptr ≠ Null) B 2 C 3
{S2.Push2(ptr);
if(ptr->left ≠ Null) 2
6 D E F G
S1.Push1(ptr->left);
if(ptr->right ≠ Null) 7 4 5 6 7
S1.Push1(ptr->right); 3
ptr = S1.Pop1( ); 1
} Null Null ptr = 2
}
S1 S2
Non-Recursive Post-Order Tree Traversal using Stack
//Traversal Root A 1
S1.Push1(ptr);
ptr = S1.Pop1( );
While(ptr ≠ Null) B 2 C 3
{S2.Push2(ptr);
if(ptr->left ≠ Null) 2
6 D E F G
S1.Push1(ptr->left);
if(ptr->right ≠ Null) 7 4 5 6 7
S1.Push1(ptr->right);
5 3
ptr = S1.Pop1( ); 4 1
} Null Null ptr = 2
}
S1 S2
Non-Recursive Post-Order Tree Traversal using Stack
//Traversal Root A 1
S1.Push1(ptr);
ptr = S1.Pop1( );
While(ptr ≠ Null)
5
B 2 C 3
{S2.Push2(ptr);
if(ptr->left ≠ Null) 2
6 D E F G
S1.Push1(ptr->left);
if(ptr->right ≠ Null) 7 4 5 6 7
S1.Push1(ptr->right); 3
ptr = S1.Pop1( ); 4 1
} Null Null ptr = 5
}
S1 S2
Non-Recursive Post-Order Tree Traversal using Stack
//Traversal Root A 1
S1.Push1(ptr);
ptr = S1.Pop1( );
4
While(ptr ≠ Null)
5
B 2 C 3
{S2.Push2(ptr);
if(ptr->left ≠ Null) 2
6 D E F G
S1.Push1(ptr->left);
if(ptr->right ≠ Null) 7 4 5 6 7
S1.Push1(ptr->right); 3
ptr = S1.Pop1( ); 1
} Null Null ptr = 4
}
S1 S2
Non-Recursive Post-Order Tree Traversal using Stack
//Traversal Root A 1
S1.Push1(ptr);
ptr = S1.Pop1( );
4
While(ptr ≠ Null)
5
B 2 C 3
{S2.Push2(ptr);
if(ptr->left ≠ Null) 2
6 D E F G
S1.Push1(ptr->left);
if(ptr->right ≠ Null) 7 4 5 6 7
S1.Push1(ptr->right); 3
ptr = S1.Pop1( ); 1
} Null
} ptr = Null
S1 S2
Non-Recursive Post-Order Tree Traversal using Stack
//Printing 4 Root A 1
ptr = S2.Pop2( ); 5
While(ptr ≠ Null)
{
2
B 2 C 3
6
Print(ptr->data); 7
ptr = S2.Pop2( ); D E F G
3
} 1 4 5 6 7
Null
D
A E B F G C A
Recursive Post-Order Tree Traversal
Stack
A B
*
A B
Infix Expression: A*B-C/D Expression Tree
Postfix Expression: AB*CD/-#
Stack
* C D
A B
Infix Expression: A*B-C/D Expression Tree
Postfix Expression: AB*CD/-#
Stack
* C D
/
A B C D
Infix Expression: A*B-C/D Expression Tree
Postfix Expression: AB*CD/-#
Stack
* / -
A B C D
* /
A B C D
Stack
Expression Tree
A B C D
Stack Evaluation of Expression Tree
EvaluateExpressionTree(root)
{ if(root->data == Operand)
- return (Operand);
else
* / { operator = root->data;
R1=EvaluateExpressionTree(root->left);
A B C D R2=EvaluateExpressionTree(root->right);
root->data =Operation(R1,R2,operator);
}
}
Binary Search Tree (BST)
It is a Binary Tree in which the following conditions hold:
• The value of the left child node is always less than the
value of its parent node.
• The value of the right child node is always greater than or
equal to the value of its parent node.
• The left and right subtree are also BSTs.
Binary Search Tree (BST)
50
40 60
2 45 55 65
55
Inserting a data element into a BST
50,60,55,20,45,40,65
Root
50
20 60
55
Inserting a data element into a BST
50,60,55,20,45,40,65
Root
50
60
20
45 55
Inserting a data element into a BST
50,60,55,20,45,40,65
Root
50
60
20
45 55
40
Inserting a data element into a BST
50,60,55,20,45,40,65
Root
50
60
20
45 55 65
40
Deleting a node from a BST
• Three Cases
1. deleting a leaf node
2. deleting an intermediate node with one
child/subtree
3. deleting an intermediate node with both
children/subtree
Deleting a node from a BST
1. deleting a leaf node
Find the Parent
Root
50
10 20
60
10 45 55 65
40
2. deleting an intermediate node with one child/subtree
2.1. intermediate node with left child/subtree only.
Find the Parent
Root
50
45 45
60
20 55 65
10
40
2. deleting an intermediate node with one child/subtree
2.2. intermediate node with right child/subtree only.
Find the Parent
Root
50
10 10
60
40 55 65
20
45
3. deleting an intermediate node with both child/subtree
• Find the Inorder
60 50 Successor(IOS) of 60.
• Find the parent of the IOS.
40 60
• If IOS is a leaf node
62
55 75
88
65
62
62 69
3. deleting an intermediate node with both child/subtree
• Find the Inorder
45 Successor(IOS) of 40.
58 • Find the parent of the IOS.
• If IOS is an intermediate node
40 60
22 45
45
48
55 65
42 48
48
50
49
55
Algorithm for node deletion from BST
15
45
7 48
6 50
4 55
BF = (1-1)=0 BF = (1-1)=0
40 60
A Balanced BST
AVL Trees
• An AVL Tree is a BST, with Balance Factor as -1, 0 or 1.
48
BF = (1-2) = -1 BF = (2-2) = 0
48
40 60 40 60
22 45
65 65
55 55
48 BF = (2-1) = 1
40 60
22 45
AVL Trees
• Insertion and Deletion Operations on AVL Trees can make
it unbalanced.
• When an AVL Tree is not balanced, rotation operations are
carried out to balance it.
• Types of Rotations:
• Single Rotations
• Right (RR) rotation
• Left (LL) rotation
• Double Rotations
• Left-Right (LR) rotation
• Right-Left (RL) rotation
• Right rotation
2 10
58
21 0 0
40 60
10
22
0
15
2 0
40
40 60
1
22
0 If the Tree is Unbalanced then
15
Identify the Nearest Ancestor (with (BF ≠ -1, 0, 1) AND
(BF >1)) of the most recently inserted Node.
0 0
22 60
0 0
15
40
-10
60
0
74
BST is Unbalanced
• Left rotation • Rotate the sub-tree towards Left
at the position of NA.
-2
58
-1
60
0
74
60
0
0
-1 • Left Rotation is performed when a
58
74 node is inserted as Right Child.
0
59
0 2. L:
60 Left rotate about the Nearest
Ancestor (58) of the most
The BST is Unbalanced recently inserted Node (59).
• Left-Right (LR) rotation
1. R:
0 Right rotate about the Parent
59
Node (60) of the most recently
0 0
inserted Node (59).
58 60
2. L:
Left rotate about the Nearest
Ancestor (58) of the most
The BST is Balanced recently inserted Node (59).
• Right-Left (RL) rotation
21 If the Tree is Unbalanced then
74
Identify the Nearest Ancestor
74
(with BF ≠ -1, 0, 1) of the most
01
recently inserted Node.
60
0
65
Unbalanced BST
• Delete 45
2
-1
40
30
0
0 1
30
40
20
0 0
20
0
35
35
Unbalanced BST
• Delete 55
2
0
40
30
1
0 0
30
40
20
0
20
Unbalanced BST
Deleting 60 Do First Single Rotation about
the Child Node with BF = -1.
2
58 2
58
-1 0
40 59 1 0
0 0 45 59
22 45 0 0
0 40 48
0
42 0 0
48
22 42
• For BST,
• Number of pointers, m = 2.
• Number of data/key = (2-1) = 1.
Multi-Way Search Trees
Pointer Pointer
to LST Data to RST P0 D0/K0 P1
BST node Structure
• For BST,
• Number of pointers(order), m = 2.
• Number of data/key = (2-1) = 1.
Multi-Way Search Trees
P1 K1 P2 K2 P3 K3 P4 K4 P5
Node Structure of M-Way Search Tree
• Number of pointers, m = 5.
• Number of data/key = (m-1)= (5-1) = 4.
• K1< K2 < K3 < K4
Multi-Way Search Trees
P1 K1 P2 K2 P3 K3 P4 K4 P5
Node Structure of M-Way Search Tree
• Key Values pointed to by P1 is less than K1
• Key Values pointed to by P2 is less than K2
• Key Values pointed to by P3 is less than K3
• Key Values pointed to by P4 is less than K4
Multi-Way Search Trees
P1 K1 P2 K2 P3 K3 P4 K4 P5
Node Structure of M-Way Search Tree
• Key Values pointed to by P2 is greater than K1
• Key Values pointed to by P3 is greater than K2
• Key Values pointed to by P4 is greater than K3
• Key Values pointed to by P5 is greater than K4
Multi-Way Search Trees
P1 K1 P2 K2 P3 K3 P4 K4 P5
Node Structure of M-Way Search Tree
# 6 # 8 # # 32 # 45 # # 99 # 112 #
# 22 # 25 #
# 78 # 80 #
Multi-Way Search Trees
• Order, m=3 P1 60 P2 85 P3
P11 20 # 30 #
P111 16 # 18 #
# 6 # 8 #
P1 45 P2
# 18 # 27 # # 36 # 39 # # 46 # 47 P23 # 67 # 72 #
# 30 # 31 #
# 54 # 59 # 61 #
• Order, m = 4 B-Trees
• Searching,
31, 61, 18 P1 45 P2
# 18 # 27 # # 36 # 39 # # 46 # 47 P23 # 67 # 72 #
# 30 # 31 #
# 54 # 59 # 61 #
• Inserting an Element:
B-Trees
• All insertions takes place at leaf nodes.
• Procedure:
1. Find the leaf node to insert the data.
2. If the leaf node is not full, then insert the data at appropriate
location in the leaf node.
3. If the leaf node is full, then
4. Arrange the data of the above node in ascending order and
find its median.
5. Push the median data into it’s parent node and split the node
into two nodes.
6. If the parent node is full, then go to step-4.
• Order, m = 5 B-Trees
p1 18 p2 45 p3 72 p4 # #
# 7 # 11 # # # # #
# 21 # 27 # 36 # 42 #
# 54 # 63 # # # # #
# 81 # 89 # 90 # # #
• Inserting, 8, 9, 39, 4
• Order, m = 5 B-Trees
p1 18 p2 45 p3 72 p4 # #
# 7 # 8 # 11 # # #
# 21 # 27 # 36 # 42 #
# 54 # 63 # # # # #
# 81 # 89 # 90 # # #
• Inserting, 8, 9, 39, 4
• Order, m = 5 B-Trees
p1 18 p2 45 p3 72 p4 # #
# 7 # 8 # 9 # 11 #
# 21 # 27 # 36 # 42 #
# 54 # 63 # # # # #
• Inserting, 8, 9, 39, 4 # 81 # 89 # 90 # # #
• Node is Full, So
• Arrange the data in ascending order, 21,27,36,39,42
• Median = 36
• Order, m = 5 B-Trees
p1 18 p2 36 p3 45 p4 72 p5
# 7 # 8 # 9 # 11 #
# 21 # 27 # # # # #
# 39 # 42 # # # # #
# 54 # 63 # # # # #
# 81 # 89 # 90 # # #
• Inserting, 8, 9, 39, 4
• Order, m = 5 B-Trees
p1 18 p2 36 p3 45 p4 72 p5
# 7 # 8 # 9 # 11 #
•
# 21 # 27 # # # # #
Node is Full, So
• Arrange the
# data
39 # in42 ascending
# # # # order,
# 4,7,8,9,11
• Median = 8, Push into parent and split the node
# 63
•
54 # # # # # #
Parent node is also full
• Arrange the data in ascending# order, 81 #
8,18,36,45,72
89 # 90 # # #
•• Inserting,
Median 8, 9,push
= 36, 39, 4into parent and split the node
• Order, m = 5 B-Trees
p1 36 p2 # # # # # #
# 39 # 42 # # # # #
# 4 # 7 # # # # #
# 54 # 63 # # # # #
# 9 # 11 # # # # #
# 81 # 89 # 90 # # #
# 21 # 27 # # # # #
• Inserting, 8, 9, 39, 4
B-Trees
• Deleting an Element:
• All deletions takes place at leaf nodes.
• Procedure:
1. Find the leaf node to delete the data.
2. If the leaf node contains more than [ceil(m/2)-1] data, then
delete the data from the appropriate location in the leaf node.
3. Else-If the leaf node contains less than [ceil(m/2)-1] data,
then,
(i). If the left sibling has more than [ceil(m/2)-1] data, then
push its largest element into its parent node and pull down
the intervening data from the parent node to the leaf node.
B-Trees
(ii). Else-If the right sibling has more than [ceil(m/2)-1]
data, then push its smallest element into its parent node and
pull down the intervening data from the parent node to the
leaf node.
4. Else-If, both left and right siblings contain exactly [ceil(m/2)-
1] then create a new leaf node by combining them with the
intervening data of the parent node.
5. If pulling the intervening data from the parent node leaves it
with less than [ceil(m/2)-1] data, then propagate the process
upwards.
B-Trees
6. To delete an data in an internal node, promote the successor or
predecessor of the data to occupy the position of the deleted data.
• Order, m = 5 B-Trees
p1 108 p2 # # # # # #
# 111 # 114 # # # # #
# 36 # 45 # # # # #
# 151 # 180 # # # # #
# 72 # 79 # # # # #
# 243 # 256 # 333 # 450 #
# 90 # 93 # 101 # # #
• Deleting, 93
• Order, m = 5 B-Trees
p1 108 p2 # # # # # #
# 111 # 114 # # # # #
# 36 # 45 # # # # #
# 151 # 180 # # # # #
# 72 # 79 # # # # #
# 243 # 256 # 333 # 450 #
# 90 # 101 # # # # #
# 111 # 114 # # # # #
# 36 # 45 # # # # #
# 151 # 180 # # # # #
# 72 # 79 # # # # #
# 256 # 333 # 450 # # #
# 90 # 101 # # # # #
# 111 # 114 # # # # #
# 36 # 45 # # # # #
# 151 # 243 # # # # #
# 72 # 79 # # # # #
# 333 # 450 # # # # #
# 90 # 101 # # # # #
# 36 # 45 # 63 # 79 # # 151 # 243 # # # # #
# 111 # 114 # # # # #
# 5 # 10 # 15 # 20 p11 p21 25 # 55 # 60 # 65 #
• Insertion B+ Trees
• Order, m = 5 p1 25 p2 50 p3 75 p4 # #
# 5 # 10 # 15 # 20 # # 50 # 55 # 60 # 65 #
# 25 # 30 # # # # # # 75 # 80 # # # # #
• Insert 28
• Order, m = 5 B+ Trees
p1 25 p2 50 p3 75 p4 # #
# 5 # 10 # 15 # 20 # # 50 # 55 # 60 # 65 #
# 25 # 28 # 30 # # # # 75 # 80 # # # # #
• Insert 28, 70
• Order, m = 5 B+ Trees
p1 25 p2 50 p3 60 p4 75 p5
# 5 # 10 # 15 # 20 # # 50 # 55 # 60 # 65 #
# 25 # 28 # 30 # # # # 60 # 65 # 70 # # #
# 75 # 80 # # # # #
• Insert 28, 70
• Deletion B+ Trees
• Order, m = 5
p1 17 p2 # # # # # #
# 19 # 20 # 22 # # #
# 2 # 3 # # # # #
# 24 # 27 # 29 # # #
# 5 # 7 # 8 # # #
# 30 # 33 # 34 # 39 #
# 13 # 16 # # # # #
• Deleting, 19
• Deletion B+ Trees
• Order, m = 5
p1 17 p2 # # # # # #
# 19 # 20 # 22 # # #
# 2 # 3 # # # # #
# 24 # 27 # 29 # # #
# 5 # 7 # 8 # # #
# 30 # 33 # 34 # 39 #
# 13 # 16 # # # # #
• Deleting, 19, 20
• Deletion B+ Trees
• Order, m = 5
p1 17 p2 # # # # # #
# 22 # 24 # # # # #
# 2 # 3 # # # # #
# 27 # 29 # # # # #
# 5 # 7 # 8 # # #
# 30 # 33 # 34 # 39 #
# 13 # 16 # # # # #
# 2 # 3 # # # # # # 17 # 22 # 27 # 29 #
# 5 # 7 # 8 # # # # 30 # 33 # 34 # 39 #
# 13 # 16 # # # # #
• Procedure:
• Pass-1: compare(A[1] and A[2]) and do the needful, then
compare(A[2] and A[3]) and do the needful and so on…finally,
compare(A[n-1] and A[n]).
• Pass-2: compare(A[1] and A[2]) and do the needful, then
compare(A[2] and A[3]) and do the needful and so on…finally,
compare(A[n-2] and A[n-1]).
• Pass-(n-1): compare(A[1] and A[2]) and do the needful.
1. Bubble Sort
1 2 3 4
90 3 44 2
Array
• Example: Sort in Ascending Order:
• Pass-1:
• compare(A[1] and A[2])=> A[1]=3 and A[2]=90.
• compare(A[2] and A[3]) => A[2]=44 and A[3]=90.
• compare(A[3] and A[4]) => A[3]=2 and A[4]=90.
1 2 3 4
3 44 2 90
1. Bubble Sort
1 2 3 4
3 44 2 90
Array
1 2 3 4
2 3 44 90
1. Bubble Sort
• Algorithm:
• Step-1: repeat step-2 for(i=1 to n-1)
• Step-2: {repeat for(j= 1 to n-i)
• {if(A[j] > A[j+1])
• swap(A[j] and A[j+1)
• }
• }
• Step-3: End
• Time Complexity: O(n2).
Array
2. Selection Sort
1 2 3 4 5 6 7 8 9 10
90 3 44 2 22 53 67 44 21 8
• Procedure:
• Pass-1:
• Find the location of the smallest element in the array and then
swap A[location] and A[0].
• Pass-2: c
• Find the location of the smallest element in the array, from A[1]
to A[n] and then swap A[location] and A[1].
• Pass-(n-1):
• Find the location of the smallest element in the array, from A[n-1]
to A[n] and then swap A[location] and A[n-1].
2. Selection Sort
Array 1 2 3 4
90 3 444 2
• Procedure:
• Divide the array into sorted set and unsorted set
• Initialize A[1] to be the only element in the sorted set and rest in
the unsorted set.
• Pass-1:
• Pick the first element of the unsorted set i.e., A[2] and insert it in
its proper location in the sorted set by comparing first with the
last elements of the sorted set and then the next last element and
so on.
3. Insertion Sort
• Procedure:
• Pass-2:
• Pick the first element of the unsorted set i.e., A[3] and insert it in
its proper location in the sorted set by comparing first with the
last elements of the sorted set and then the next last element and
so on.
• Pass-(n-1):
• Pick the first element of the unsorted set i.e., A[n] and insert it in
its proper location in the sorted set by comparing first with the
last elements of the sorted set and then the next last element and
so on.
3. Insertion Sort
1 2 3 4
Array
90 3 44 2
• Example:
• A[1]=90 is the only element in sorted set and rest are in unsorted
set.
• Pass-1:
• Let temp=A[2] and compare temp with A[1]
• Compare, temp < A[1], Since true So, Swap them
• => A[1]=3 and A[2]=90
• There are no more elements in the sorted set to be compared, so
stop comparison. 1 2 3 4
• Now A[1] and A[2] are in sorted set. 3 90 44 2
3. Insertion Sort
Array 1 2 3 4
3 90 44 2
• Example:
• Pass-2:
• Let temp=A[3] and compare it with A[2]
• Compare, temp < A[2], Since true So, => A[3]=A[2]
• => A[3]=90
• Compare, temp < A[1], Since false So, A[2]=temp
• => A[2]=44 & Stop comparison.
• Now A[1], A[2] and A[3] are in sorted set.
1 2 3 4
3 44 90 2
3. Insertion Sort
1 2 3 4
3 44 90 2
• Example:
• Pass-3:
• Let temp=A[4] and compare it with A[3]
• Compare, temp < A[3], Since true So, => A[4]=A[3]=> A[4]=90
• Compare, temp < A[2], Since true, So,=> A[3]=A[2]=>A[3]=44
• Compare, temp < A[1], Since true, So,=> A[1]=temp=>A[1]=2
• Stop comparison.
• Now A[1], A[2],A[3] and A[4] are in sorted set. 1 2 3 4
2 3 44 90
• Algorithm:
3. Insertion Sort
Step-1: Repeat Step-2 to Step-5 for(k=2 to n)
Step-2: {set temp=A[k]
Step-3: set j=(k-1)
Step-4: Repeat While(temp<= A[j])
{set A[j+1] = A[j]
set j=j-1
}
set A[j+1]=temp
}
Step-5: End
Time Complexity = O(n2).
4. Merge Sort
• Procedure:
• If the array has only one element, then it is sorted
• Otherwise, divide the array into two sub-arrays of equal size
• Use merge-sort algorithm to recursively sort the two sub-arrays
• Merge the two sub-arrays to form a single sorted list.
Example: 4. Merge Sort
1 2 3 4 5 6 7 8
Array 90 3 44 2 22 53 67 45
Phase-1:
1 2 3 4 1 2 3 4
90 3 44 2 22 53 67 45
1 2 1 2 1 2 1 2
90 3 44 2 22 53 67 45
1 1 1 1 1 1 1 1
90 3 44 2 22 53 67 45
Example: 4. Merge Sort
1 2 3 4 5 6 7 8
2 3 22 44 45 53 67 90
Phase-2:
1 2 3 4 1 2 3 4
2 3 44 90 22 45 53 67
1 2 1 2 1 2 1 2
3 90 2 44 22 53 45 67
1 1 1 1 1 1 1 1
90 3 44 2 22 53 67 45
4. Merge Sort
Merge Algorithm:
A is an Array and p, q, r are indices of the array, such that p <= q < r.
Sub-array A[p…q] and A[q+1,…..r are in sorted order
MERGE(A,p,q,r)
{ n1=q-p+1 //no. of elements in sub-array 1
n2=r-q // no. of elements in sub-array 2
Let L[1…n1+1] and R[1…n2+1] be new arrays
for(i=1 to n1)
L[i]=A[p+i-1]
for(j=1 to n2)
R[j]=A[q+j]
L[n1+1]=∞
R[n2+1]=∞
4. Merge Sort
Merge Algorithm:
i=1 and j=1
for(k = p to r)
{if(L[i]<=R[j])
{ A[k]=L[i]
i=i+1}
else
{A[k]=R[j]
j=j+1
}
}
4. Merge Sort
Merge-Sort Algorithm:
MERGE-SORT(A, p, r)
{ if(p < r)
{ q = floor((p+r)/2)
MERGE-SORT(A, p, q)
MERGE-SORT(A, q+1, r)
MERGE(A, p, q, r)
}
}
i=1, j=2,
6. Quick-Sort
p=1, r =3,
Example:
1 2 3 4 5 6 7 8
2 1 3 4 7 5 6 8
j=2,
i=2,
6. Quick-Sort
p=1, r =3,
Example:
1 2 3 4 5 6 7 8
2 1 3 4 7 5 6 8
i=0, j=1,
6. Quick-Sort
p=1, r =3,
Example:
1 2 3 4 5 6 7 8
2 1 3 4 7 5 6 8
i=0, j=1,
6. Quick-Sort
p=1, r =3,
Example:
1 2 3 4 5 6 7 8
2 1 3 4 7 5 6 8
i=0, j=1,
6. Quick-Sort
p=1, r =3,
Example:
1 2 3 4 5 6 7 8
1 2 3 4 7 5 6 8
i=0, j=1,
6. Quick-Sort
p=1, r =3,
Example:
1 2 3 4 5 6 7 8
1 2 3 4 7 5 6 8
i=0, j=1,
QUICK-SORT(A, p, r)
{ if(p < r)
{ q = PARTITION(A, p, r)
QUICK-SORT(A, p, q-1)
QUICK-SORT(A, q+1, r)
}
}
Randomized_Partition(A, p, r)
{ i=Random(p,r)
exchange A[r] with A[i]
return PARTITION(A, p, r)
}
7. Randomized Quick-Sort
Algorithm:
RANDOMIZED_QUICK-SORT(A, p, r)
{ if(p < r)
{ q = Randomized_Partition (A, p, r)
RANDOMIZED_QUICK-SORT(A, p, q-1)
RANDOMIZED_QUICK-SORT(A, q+1, r)
}
}
• It assumes that each of the elements of the Array is an integer in the range 0
to k.
• For each element ‘x’ it determines the number of elements less than ‘x’.
• This information is then used to place the element ‘x’ directly into its
position in the output array.
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Frequency Array 0 0 0 0 0 0
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Frequency Array
2 0 2 3 0 1
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Position Array F 2 0 2 3 0 1
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Position Array F 2 2 2 3 0 1
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Position Array F 2 2 4 3 0 1
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Position Array F 2 2 4 7 0 1
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Position Array F 2 2 4 7 7 1
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Position Array F 2 2 4 7 7 8
1 2 3 4 5 6 7 8
Output Array B
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Position Array F 2 2 4 7 7 8
1 2 3 4 5 6 7 8
Output Array B 3
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Position Array F 2 2 4 7 7 8
1 2 3 4 5 6 7 8
Output Array B 3
0 1 2 3 4 5
Position Array F 2 2 4 6 7 8
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Position Array F 2 2 4 6 7 8
1 2 3 4 5 6 7 8
Output Array B 0 3
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Position Array F 2 2 4 6 7 8
1 2 3 4 5 6 7 8
Output Array B 0 3
0 1 2 3 4 5
Position Array F 1 2 4 6 7 8
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Position Array F 1 2 4 6 7 8
1 2 3 4 5 6 7 8
Output Array B 0 3 3
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Position Array F 1 2 4 6 7 8
1 2 3 4 5 6 7 8
Output Array B 0 3 3
0 1 2 3 4 5
Position Array F 1 2 4 5 7 8
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Position Array F 1 2 4 5 7 8
1 2 3 4 5 6 7 8
Output Array B 0 2 3 3
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Position Array F 1 2 4 5 7 8
1 2 3 4 5 6 7 8
Output Array B 0 2 3 3
0 1 2 3 4 5
Position Array F 1 2 3 5 7 8
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Position Array F 1 2 3 5 7 8
1 2 3 4 5 6 7 8
Output Array B 0 0 2 3 3
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Position Array F 1 2 3 5 7 8
1 2 3 4 5 6 7 8
Output Array B 0 0 2 3 3
0 1 2 3 4 5
Position Array F 0 2 3 5 7 8
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Position Array F 0 2 3 5 7 8
1 2 3 4 5 6 7 8
Output Array B 0 0 2 3 3 3
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Position Array F 0 2 3 5 7 8
1 2 3 4 5 6 7 8
Output Array B 0 0 3 2 3 3 3 3
0 1 2 3 4 5
Position Array F 0 2 3 4 7 8
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Position Array F 0 2 3 4 7 8
1 2 3 4 5 6 7 8
Output Array B 0 0 2 3 3 3 5
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Position Array F 0 2 3 4 7 8
1 2 3 4 5 6 7 8
Output Array B 0 0 3 2 3 3 3 5
0 1 2 3 4 5
Position Array F 0 2 3 4 7 7
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Position Array F 0 2 3 4 7 7
1 2 3 4 5 6 7 8
Output Array B 0 0 2 2 3 3 3 5
8. Counting Sort
Example:
1 2 3 4 5 6 7 8
Input Array A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Position Array F 0 2 3 4 7 7
1 2 3 4 5 6 7 8
Output Array B 0 0 2 2 3 3 3 5
Algorithm:
8. Counting Sort
COUNTING-SORT(A, B, k)
{ create a new array F[0…k]
for(i= 0 to k)
F[i] = 0
for(j= 1 to A.length)
F(A[j]] = C[A[j]]+1
for(i= 1 to k)
F[i] = F[i] + F[i-1]
for(j= A.length downto 1)
{B[F[A[j]]] = A[j]
C[A[j]] = C[A[j]]-1
} Time complexity = Ɵ(k+n)
}
9. Radix Sort
• Integers are sorted according to the least significant and then according to
next higher significant digits and so on.
Radix-Sort(A, d)
{ //d=no. of digits in the highest input data
for (i= 1 to d)
use a stable sort(bubble, insertion, counting) to sort array A on digit i.
}
Input Buffer
Input File
3 1 2 6 5 4 17 9 8 15 12 11 14 13 10
Output
Buffer
Input Buffer
External Disk
11. Two Way External Sorting
• Divide the input file into blocks of size 3,
• So that each block can fit into each input buffer RAM
(Capacity = 9 numbers)
Input Buffer
Input File
3 1 2 6 5 4 17 9 8 15 12 11 14 13 10
Output
Buffer
Input Buffer
External Disk
11. Two Way External Sorting
• Pass-0:
RAM
(Capacity = 9 numbers)
Input Buffer
Input File
3 1 2 6 5 4 17 9 8 15 12 11 14 13 10
Output
Buffer
Input Buffer
External Disk
11. Two Way External Sorting
• Pass-0:
RAM
(Capacity = 9 numbers)
Input Buffer
Input File
3 1 2 6 5 4 17 9 8 15 12 11 14 13 10
Output
Buffer
Input Buffer
External Disk
11. Two Way External Sorting
• Pass-0:
RAM
(Capacity = 9 numbers)
Input Buffer
3 1 2
Input File
3 1 2 6 5 4 17 9 8 15 12 11 14 13 10
Output
Buffer
Input Buffer
External Disk
11. Two Way External Sorting
• Pass-0:
RAM
(Capacity = 9 numbers)
Input Buffer
1 2 3
Input File
3 1 2 6 5 4 17 9 8 15 12 11 14 13 10
Output
Buffer
Input Buffer
External Disk
11. Two Way External Sorting
• Pass-0:
RAM
(Capacity = 9 numbers)
Input Buffer
1 2 3
Input File
3 1 2 6 5 4 17 9 8 15 12 11 14 13 10
Temporary File-1 Output
Buffer
Input Buffer
External Disk
11. Two Way External Sorting
• Pass-0:
RAM
(Capacity = 9 numbers)
Input Buffer
1 2 3
Input File
3 1 2 6 5 4 17 9 8 15 12 11 14 13 10
Temporary File-1 Output
1 2 3 Buffer
Input Buffer
External Disk
11. Two Way External Sorting
• Pass-0:
RAM
(Capacity = 9 numbers)
Input Buffer
10 13 14
Input File
3 1 2 6 5 4 17 9 8 15 12 11 14 13 10
Temporary File-1 Output
1 2 3 4 5 6 8 9 17 11 12 15 10 13 14 Buffer
Input Buffer
External Disk
• Pass-1: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-1
3 2 1
1 2 3 4 5 6 8 9 17 11 12 15 10 13 14
External Disk
• Pass-1: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-1
1 2 3 4 5 6 8 9 17 11 12 15 10 13 14
1 2 3
External Disk
• Pass-1: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-1
1 2 3 4 5 6 8 9 17 11 12 15 10 13 14
1 2 3
External Disk
• Pass-1: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-1
1 2 3 4 5 6 8 9 17 11 12 15 10 13 14
4 5 6
External Disk
• Pass-1: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-1
17 9 8
1 2 3 4 5 6 8 9 17 11 12 15 10 13 14
Output
6 5 4 Buffer
1 2 3 4 5 6
Input Buffer
Temporary File-2
External Disk
• Pass-1: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-1
1 2 3 4 5 6 8 9 17 11 12 15 10 13 14
Output
Buffer
1 2 3 4 5 6 8 9 11 12 15 17
Input Buffer
Temporary File-2
External Disk
• Pass-1: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-1
1 2 3 4 5 6 8 9 17 11 12 15 10 13 14
Output
Buffer
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
Input Buffer
Temporary File-2
External Disk
• Pass-2: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-2
3 2 1
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
External Disk
• Pass-2: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-2
6 5 4
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
1 2 3
External Disk
• Pass-2: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-2
6 5 4
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
External Disk
• Pass-2: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-2
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
4 5 6
External Disk
• Pass-2: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-2
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
External Disk
• Pass-2: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-2
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
8 9 11
External Disk
• Pass-2: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-2
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
12 15 17
External Disk
• Pass-2: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-2
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
External Disk
• Pass-2: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-2
14 13 10
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
External Disk
• Pass-2: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-2
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
10 13 14
External Disk
• Pass-2: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-2
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
10 13 14
External Disk
• Pass-3: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-3
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
Output
Buffer
Input Buffer
External Disk
• Pass-3: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-3
3 2 1
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
External Disk
• Pass-3: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-3
6 5 4
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
1 2 3
External Disk
• Pass-3: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-3
6 5 4
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
External Disk
• Pass-3: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-3
11 9 8
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
4 5 6
External Disk
• Pass-3: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-3
11 9 8
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
8 9 10
External Disk
• Pass-3: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-3
11 9 8
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
External Disk
• Pass-3: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-3
11 9 8
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
11
External Disk
• Pass-3: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-3
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
11
External Disk
• Pass-3: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-3
17 15 12
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
11
External Disk
• Pass-3: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-3
17 15 12
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
11 12 13
External Disk
• Pass-3: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-3
17 15 12
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
External Disk
• Pass-3: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-3
17 15 12
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
14 15 17
External Disk
• Pass-3: 11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
Temporary File-3
1 2 3 4 5 6 8 9 11 12 15 17 10 13 14
External Disk
11. Two Way External Sorting
RAM
(Capacity = 9 numbers)
Input Buffer
File
1 2 3 4 5 6 8 9 10 11 12 13 14 15 17
Output
Buffer
Input Buffer
External Disk
11. Two Way External Sorting
• The amount of data that can be held in the input buffers of RAM is called
page
• If there are P pages in a file, then
• No. of passes = ceil(log2(P))+1
• In pass-0, a block of data of size equal to a page is read from external
storage into a single input buffer of RAM, sorted and then written back to
the external storage.
• In pass-1, two blocks of data, each of size equal to two pages is read
simultaneously from external storage into a two input buffer of RAM,
sorted and then written back to the external storage.
• So total read and write operations = 2P(ceil(log2(P))+1)
• So, time complexity = O(Plog2(P))
11. Two Way External Sorting
• External merge-sort is better than merge-sort, since,
• O(Plog2(P)) << O(Nlog2(N))
• Since, P << N
12. General External Merge Sort
Input Buffers
IB-1
IB-2
I B – (B-1)
External Disk
Main Memory
Searching
Unsorted List
• Time Complexity = O(n)
2. Binary Search
1 2 3 4 5 6 7 8 9
99 76 53 34 28 20 14 8 5
Sorted List
• Search Data = 5
• Time Complexity = O(n)
2. Binary Search
1 2 3 4 5 6 7 8 9
99 76 53 34 28 20 14 8 5
Sorted List
• Search Data = 5
• Time Complexity = O(n)
2. Binary Search
1 2 3 4 5 6 7 8 9
99 76 53 34 28 20 14 8 5
Sorted List
• Search Data = 5
• Time Complexity = O(n)
2. Binary Search
1 2 3 4 5 6 7 8 9
99 76 53 34 28 20 14 8 5
Sorted List
• Search Data = 5
• Time Complexity = O(log(n))
3. Interpolation/Extrapolation Search
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
0 2 3 6 7 10 13 16 18 22 27 33 45 50 80 104
low hi
Sorted List
• Search Data = 50
• When the search data is towards higher index of the array, the search
should begin towards higher index, not in the middle.
3. Interpolation/Extrapolation Search
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
0 2 3 6 7 10 13 16 18 22 27 33 45 50 80 104
low hi
Sorted List
• Search Data = 3
• When the search data is towards lower index of the array, the search
should begin towards lower index, not in the middle.
3. Interpolation/Extrapolation Search
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 6 7 10 13 16 18 22 27 33 45 50 80 104
low hi
Sorted List
• Search Data, x = 2.
pos = low + ceil(( high - low )*( (x – a[low]) / ( a[high] – a[low] )))
Example:
Pos = 1 + ceil((16-1)*((2-1)/(104-1)))
= 1 + ceil(15*(1/103)) = 1 + ceil(15*0.0097) = 1 + 1 = 2.
If A[pos] = = x => search data found!!!
If A[pos] > x then hi = pos-1
Else low = pos+1
3. Interpolation/Extrapolation Search
• Time Complexity = O(log(log(n))) if the data is uniformly distributed
• = O(n) otherwise
4. Jump Search
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 6 7 10 13 16 18 22 27 33 45 50 80 104
• Tree Search
• Fibonacci Search