Data Structures and Algorithms Overview
Data Structures and Algorithms Overview
1
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Reference Books
• A. V. Aho, J. E. Hopcroft, J. D. Ullman, “Data Structures and
Algorithm”, Pearson.
• D. Samanta, “Classic Data Structure”, PHI.
• S. Lipscutz, “Data Structures with C”, TMH.
• R. Kruse, C.L. Tondo, B. Leung, S. Mogalla, “Data Structures and
Program Design in C”, Pearson.
• D. E. Knuth, “The Art of Computer Programming”, Addison-Wesley
• S. Chattopadhyay, D.G. Dastidar, M. Chattopadhyay, “Data
Structures through C Language”, BPB Publications.
2
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Introduction
3
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Algorithms
• A finite set of instructions executed in
sequence in finite time
• Algorithm for finding GCD
step 1: read two positive integers x and y
step 2: divide x by y to get remainder r and quotient q
step 3: if r is zero go to step 7
step 4: assign y to x
step 5: assign r to y
step 6: go to step 2
step 7: y is the required GCD, print y
step 8: stop
4
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Algorithms
• Properties of an algorithm
– Input
– Output
– Finiteness
• For all input data, the algorithm must terminate after a
finite number of steps
– Definiteness
– Clear and unambiguous steps
– Effectiveness
• Steps must be very basic
5
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Algorithms
• Expressing an algorithm
– Natural language, flowchart, programming languages
• Designing an algorithm
– Innovative exercise, no methodology to automatically generate
algorithms
– Use of new techniques and strategies for good algorithms
• Depends heavily on the organization of data
• Analyzing an algorithm
– Validation
– Complexity evaluation
• Both time and space
6
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Abstract Data Type
• Data type defines a set of values and
permitted operations
• Built-in data types are not enough for most
applications
– Create new data types in terms of structure
• Operations applicable to structure variables can not be
specified
– Concept of abstract data type (ADT) introduced
• A mathematical model with collection of operations
defined on that model
7
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Abstract Data Type
• Define a new data type “SET”
• Operations on ADT “SET”
– assign (SET A, SET B)
– SET union (SET A, SET B)
– SET Intersection (SET A, SET B)
– int cardinality (SET A)
• No limit on the number of operations
• Implementation aspect is not considered
8
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Data Structure
• A scheme to organize data
– Stack, queue, tree, graph etc.
• Affects the performance of a program for
different tasks
• Choice of data structure depends on:
– Nature of data
– Processes to be performed on the data
9
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Analysis of Algorithms
10
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Analysis of Algorithms
• Used to compare number of algorithms and
choose the best one
• Typically, two quantitative metrics:
– Space complexity
• Run time storage requirement
– Time complexity
• Time required to complete execution
– Usually depends on input size, i.e., time complexity is a
function of input size
11
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Analysis of Algorithms
• Types of analysis
– Best Case
• Lower bound on cost
• Determined by “easiest” input
• Provides a goal for all inputs
– Worst Case
• Upper bound on cost
• Determined by most “difficult” input
• Provides a guarantee for all inputs
– Average Case
• Expected cost for random input
• Provides a way to predict performance
12
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Analysis of Algorithms
• Time complexity
– Very difficult to compute exact time
• Several factors influence execution time which are
outside the domain of programmers
– Programs are translated to machine code
13
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Analysis of Algorithms
• Big-Oh notation
– T(n)=O( f(n) ) if T(n) ≤ c f(n) ie for sufficiently
for some constant, c > 0, and n ≥ n0 large n
15
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Analysis of Algorithms
• T is O(g) is transitive
– If T is O(g) and g is O(h) then T is O(h)
• Product of upper bounds is upper bound for the product
– If f is O(g) and h is O(r) then fh is O(gr)
• Two additional notations
W( g(n) )
T(n) ≥ c g(n) g(n) is a lower
for some constant, c, and n > n0 bound for T
( g(n) ), for some constants, c1,c2 and n > n0
c2g(n) ≥ T(n) ≥ c1 g(n) Best and worst case complexities are
same
16
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Analysis of Algorithms
• Simple statement
S=p+q
– Time Complexity is O(1)
• Simple loops
for(i=0;i<n;i++) { s=p+q; }
– Time complexity is n O(1) or O(n)
• Nested loops
for(i=0;i<n;i++)
This part is
for(j=0;j<n;j++) { s=p+q; }
O(n)
– Time Complexity is n O(n) or O(n )
2
17
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Analysis of Algorithms
• Loop index doesn’t vary linearly
h = 1;
while ( h <= n ) {
s;
h = 2 * h;
}
– h takes values 1, 2, 4, … until it exceeds n
– There are 1 + log2n iterations
– Complexity O(log n)
18
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Analysis of Algorithms
• Loop index depends on outer loop index
for(j=0;j<n;j++)
for(k=0;k<j;k++){
s;
}
– Inner loop executed
• 1, 2, 3, …., n times
n(n+1)
S i =
i=1 2
Complexity O(n2)
19
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Analysis of Algorithms
• Common computing times are
O(1) < O(log n) < O(n) < O(n log n) < O(n2) < O(2n)
• log n
• Logarithmic algorithm, cuts down the problem to smaller one
• n
• Linear algorithm
• n log n
• Breaks the large problem into sub-problems, solve sub-problems
independently, combine the results
• 2n
• Exponential running time, not suitable for practical use
20
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
The LIST ADT
21
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
LIST ADT
• Ordered sequence of data items called
elements
– A1, A2, A3, …,AN is a list of size N
• Size of an empty list is 0
• First element is A1 called “head”
• Last element is AN called “tail”
Operations ?
22
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
LIST ADT
• Operations
– PrintList
– Search
– FindKth
– Insert
– Delete
– Reverse
– Sorting
– MakeEmpty
23
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
LIST ADT
• Example:
the elements of a list are
34, 12, 52, 16, 12
– Search (52) 3
– Insert (20, 3) 34, 12, 20, 52, 16, 12
– Delete (52) 34, 12, 20, 16, 12
– FindKth (3) 20
24
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
LIST ADT
• You can see the difference between arrays and
lists when you delete items.
25
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Array Implementation of LIST
• Need to define a size for array
– High overestimate (waste of space)
FindKth
Next O(1)
Previous
26
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Array Implementation of LIST
int search (int a[], int n, int val) Best Case: O(1)
{ Average Case:
int i=0; number of comparisons
for (i=0; i<n;i++) could be 1,2,..,n depending
if (a[i] = = val) If a match is found come on the position of val.
break; out of the for loop =(1+2+…+n)/n
if (i = = n) =(n+1)/2
return -1; Val not found in a[ ] =O(n)
else Worst Case: O(n)
return i; Return the index of
val in a[ ]
}
27
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Array Implementation of LIST
iii ii i
5 2 1 6 9 4
Initial array; arrow shows
shifting of elements
5 2 1 8 6 9 4
Initial array; arrow shows
shifting of elements
29
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Array Implementation of LIST
1 2 3 4 5 6 7 8 9
Arrows indicate
required exchange
void reverse (int a [], int n)
{
int i, temp;
for ( i = 0; i < n/2 ; i++ )
{
temp = a [i];
a [i] = a [n-1-i];
a [n-1-i] = a [i];
}
}
30
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Polynomials using Arrays
• Polynomial, P(x) of degree n is:
P(x) = a0 + a1x + a2x2 + …….. + anxn
• Treat Polynomials as ADT
– Operations
• Initialization
• Copy
• Add
• Multiply
• Evaluate
31
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Polynomials using Arrays
• Representation:
typedef struct poly
{
float coeff [1000];
int degree;
} poly
32
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Polynomials using Arrays
• Store only non-zero terms aixi
• Define the terms:
typedef struct term
{
float coeff;
int expo;
} term;
• Now, define the polynomial:
typedef struct poly
{
term a[ 1000];
int no_of_terms;
}poly;
33
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Polynomials using Arrays
• Polynomial 1 + 4x2 + 2x8 is stored as:
1,0 4,2 2,8 ……
34
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Polynomials using Arrays
• Addition of polynomials:
1,0 4,2 2,8 …… 3,1 5,2 6,7 ……
1,0
1,0 3,1
35
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Polynomials using Arrays
• Addition of polynomials:
1,0 4,2 2,8 …… 3,1 5,2 6,7 ……
36
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Polynomials using Arrays
• Addition of polynomials:
1,0 4,2 2,8 …… 3,1 5,2 6,7 ……
37
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Polynomials using Arrays
for ( i = 0, j = 0, k=0; ( i < P1no_of_terms) && ( j < P2no_of_terms); k++)
{
if (P1 a [i].expo == P2 a[j].expo)
{
P3a[k].coeff = P1a[i].coeff + P2a[j].coeff;
P3a[k].expo = P1a[i].expo;
For remaining terms in P1 and P2
i++; j++;
} if (i < P1no_of_terms)
else if (P1 a [i].expo < P2 a[j].expo) for (l = i; l < P1no_of_terms; l++, k++)
{ {
copy the term in P1 to P3; i++; copy the terms in P1 to P3;
} }
else else
{ for (l = j; l < P2no_of_terms; l++, k++)
copy the term in P2 to P3; j++; {
} copy the terms in P2 to P3;
} }
Complexity: O(m+n), m and n are degree of the polynomials P3no_of_terms = k;
38
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Polynomials using Arrays
• Very large numbers can not be stored in
variables of type “int” or “long”!!!!
– 80 digit number is greater than the maximum
value in “long”
39
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Polynomials using Arrays
• An integer of n digits is a polynomial of degree
n-1:
P(x)= S aixi for x=10, 0 ai
i=0,n-1
• 2000020000000800009000000001 can be
represented as:
40
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Sparse Matrix
• Most of the elements are zero in a matrix
0 0 1 0
2 0 5 0
0 0 0 3
0 4 0 0
42
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Sparse Matrix
• Define the sparse matrix:
typedef struct sparsemat
{
int no_of_nonzero_elements;
int no_of_rows, no_of_cols;
element a [100];
}sparsemat;
43
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Sparse Matrix
• Saving in Space
– Space required to store m x n matrix of integers is
m x n x (size of an integer)
– Space required in ordered LIST
3 x p x (size of an integer), p is size of array
– LIST is advantageous if
3xp<mxn i.e., p < m x n/3
44
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Sparse Matrix
• Finding number of non-zero elements in each
column:
for ( i = 0; i < sno_of_cols; i ++)
Number of comparisons = p x n
{ In worst case p = O(mn)
count = 0; So, complexity is O (mn2)
45
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Sparse Matrix
• Use of programming trick helps to reduce the
complexity
• Use an array column so that column [i] stores
number of elements in ith column
for ( i = 0; i < sno_of_nonzero_elements; i ++)
{
j = sa[i].col; Complexity is now O(mn)
col [j] = col [j] + 1;
}
46
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Multi-dimensional Array
• Computer memory is one dimensional
– Multi-dimensional arrays are represented as one
dimensional array
e.g., int a[5][10][4][10][5] may be stored as b[10000]
A difficult question:
Which element in “b” contains a particular
element of “a”??
0 1 ….. n-1 n ….. 2*n-1 ….. i*n ….. i*n+n-1 ….. (m-1)*n ….. m*n-1
i j * ui im 1
m2 m 1
j 0
i j 1
50
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Linked Lists
51
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Limitations of Arrays
• Simple, Fast
but
• Must specify size at construction time
• Construct an array with space for n
– n = twice your estimate of largest collection
• Actual size is much less than n, wastage of space
• Tomorrow you’ll need n+1, overflow
• Shifting of elements during insertion and deletion
–More flexible system?
52
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Linked Lists
• Flexible space use
– Dynamically allocate space for each element as needed
• Series of nodes
– Each node of the list contains
• the data item
• a pointer to the next node
• Avoids the linear cost of insertion and deletion !
53
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Linked Lists
• Define a node
typedef Struct node
{
int val;
struct node *next; Recursive type definition
} node;
} header
Val Next Val Next
last
55
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Linked Lists
• Printing content of a list is simple…
Void printlist (node *header)
{
node *temp=header;
while (temp != NULL)
{
printf (“%d”, tempval);
temp = temp next;
}
}
56
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Linked Lists
• Insert a node in a linked list
Val Next Val Next Val Next Val Next
header
target
Val Next
temp
Val Next
temp
i
ii
57
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Linked Lists
void insert (node * header, int i, node *t) {
int k; node *temp = header;
if ( i == 0)
{
tnext = temp; Insert at the first position
header = t;
return;
}
for ( k = 1; ( k < i ) && ( temp != NULL ); k++)
temp = tempnext; Move to the target node in question
If ( ( temp == NULL ) && i > 0) return;
tnext = tempnext;
tempnext = t;
return; }
58
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Linked Lists
• Delete a node from linked list
59
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Linked Lists
void delete ( node * header, int k) {
int i; node *temp = header, *target;
If ( k == 0 )
{
header = headernext; free ( temp ); return;
}
target = tempnext;
for ( i = 1; i < k && target != NULL; i ++)
{
temp = target; target = targetnext;
}
if ( target == NULL && i > 0) return;
tempnext = targetnext; targetnext = NULL;
free (target); return; }
60
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Linked Lists
• Printing a linked list in reverse order
Void printreverse ( node *header ) {
node * target, *temp = NULL; Node n is printed after visiting n nodes
Node n-1 is printed after visiting n-1 nodes
if ( header == NULL) return; So, complexity=n(n+1)/2 = O(n2)
while ( temp != header)
{ target
target = header; header Val Next Val Next Val Next Val Next
target = targetnext;
printf ( “%d”, targetval ); After one
temp = target;
iteration of outer
while loop
} target
}
Val Next Val Next Val Next Val Next
header
temp
61
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Linked Lists
• Reverse a linked list
prev curr future
Val Next Val Next Val Next Val Next
header
62
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Linked Lists
node * reverse ( node * header) {
node *prev = NULL, *future, *curr = header;
future = currnext;
while ( currnext != NULL)
{
currnext = prev;
prev = curr;
curr = future;
future = futurenext;
}
currnext = prev;
return ( curr ); }
63
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Doubly Linked Lists
64
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Doubly Linked Lists
• Define a node:
typedef struct node
{
int val;
struct node *prev;
struct node *next;
} node;
65
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Doubly Linked Lists
• Insert a node after target node:
Void insert ( node *header, node *new, node *target)
{
newnext = targetnext; Set the pointers of
newprev = target; node “new”
targetnextprev = new;
targetnext = new;
}
66
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Doubly Linked Lists
• Delete a node pointed to by target:
void delete( node *header, node *target) {
if ( target != header )
targetprevnext = targetnext;
else
{
header = targetnext; Deleting the first
headerprev = NULL; node
}
if ( targetnext != NULL ) Deleting the last node
targetnextprev = targetprev;
free ( target ); }
67
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Doubly Linked Lists
• Reverse a doubly linked list:
void reverse ( node *header) {
node *last = header, *start = header;
while ( lastnext != NULL) last = lastnext; Move to the last node
while ( start != last )
{
swap ( startval , lastval);
start = startnext;
if ( start == last) break; Check for even number of nodes
last = lastprev;
}
}
68
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Circular Linked Lists
70
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Josephus Problem
• “n” children arranged a in a circle
– Children are numbered in clockwise fashion
• Choose a lucky number “m”
• Start counting from child 1 in clockwise fashion
– The mth child is eliminated
• Start the next round from the child next to the
eliminated child
– Continue until you are left with one child who is the
winner
71
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Josephus Problem
• Define a child:
typedef struct child
{
int position;
struct child *nextchild;
} child;
72
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Josephus Problem
int findwinner ( int n, int m)
{
create a circular linked list with n children;
while ( the list contain more than one child)
{
set a counter to zero;
go to the next child and increment the counter as long as it is less
than m;
delete the current child;
}
get the position of the only child in the list;
return the position; }
73
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Polynomials
• The polynomial P(x) = 3x6 + 5x3 - 4x can be
represented as:
1 -4 Next 3 5 Next 6 3 Next
header
74
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Stacks
75
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
What is a Stack
• Special form of linear list
• Principle: Last In First Out
– the last element inserted is the first one to be
removed
• Like a plate stacker
76
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Stack Applications
• Real life
– Pile of books
– Plate trays
• More applications related to computer science
– Recursive function calls
– Evaluating expressions
77
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Stack Operations
• construct a stack (usually empty)
• check if it is empty
• Push: add an element to the top
• Top: retrieve the top element
• Pop: remove the top element
78
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Last In First Out
E top
D top D D
C top C C C
B top B B B B
A top A A A A A
79
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Array Implementation of Stack
• Allocate an array of some size (pre-defined)
– MAX_STACK_SIZE elements in stack
– Bottom stack element stored at position 0
– Last element in the stack is the top
• Define the stack:
typedef struct stack
{
int a [MAX_STACK_SIZE];
int top;
} stack;
80
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Array Implementation of Stack
• Push Operation
void push ( stack *s, int item)
{
if ( stop == MAX_STACK_SIZE - 1 ) return; Stack is full
else
{
stop = stop + 1; Increment the top
}
}
81
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Array Implementation of Stack
• Pop Operation
int pop ( stack *s, int *x )
{
if ( stop == -1) return 0; Stack is empty
else
{
*x = sa[stop]; Remove topmost element
stop = stop - 1; Decrement the top
return 1;
}
}
82
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Linked List Implementation
• Define the nodes:
typedef struct node
{
int item;
struct node *next;
} node;
84
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Linked List Implementation
• Pop Operation
int pop ( stack *s, int *x )
{
node *temp;
if ( smytop == NULL ) Empty stack
{ x = NULL; return 0; }
86
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Function Calls
Consider events when a function begins
execution
• Stack frame is created
• Copy of stack frame pushed onto run-time stack
• Arguments copied into parameter spaces
• Control transferred to starting address of body of
function
87
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Function Calls
When function terminates
• Run-time stack popped
– Removes stack frame of terminated function
– exposes stack frame of previously executing function
88
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Function Calls
function f( int x, int y) {
int a;
if ( term_cond ) return …;
a = ….;
return g( a );
}
function g( int z ) {
int p, q;
p = …. ; q = …. ;
return f(p,q);
}
Context
for execution of f
89
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Evaluation of Arithmetic Expression
INFIX POSTFIX PREFIX
A+B A B + + A B
A*B+C A B * C + + * A B C
A * (B + C) A B C + * * A + B C
A - (B - (C - D)) A B C D--- -A-B-C D
A-B-C-D A B-C-D- ---A B C D
• Advantage:
expressions can be written without parentheses
90
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Evaluation of Arithmetic Expression
• Evaluating Postfix Expression
– "By hand" (Underlining technique):
• Scan the expression from left to right
to find an operator. 2 3 4 + 5 6 - - *
2 3 4 + 5 6 - - *
• Locate ("underline") the last two
preceding operands and combine 2 7 5 6 - - *
them using this operator. 2 7 5 6 - - *
• Repeat until the end of the 2 7 -1 - *
expression is reached. 2 7 -1 - *
2 8 *
2 8 *
16
91
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Evaluation of Arithmetic Expression
• Evaluating Postfix Expression
1. Initialize an empty stack
2. Repeat the following until the end of the expression is
encountered
1. Get the next element from the expression
2. Operand – push onto stack
Operator – do the following
1. Pop 2 values from stack
2. Apply operator to the two values
3. Push resulting value back onto stack
3. When end of expression encountered, value of
expression is the (only) number left in stack
92
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Evaluation of Arithmetic Expression
• Evaluating Postfix
Expression
93
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Evaluation of Arithmetic Expression
• Infix to Postfix Conversion
– By hand: "Fully parenthesize-move-erase" method:
• Fully parenthesize the expression.
• Replace each right parenthesis by the corresponding operator.
• Erase all left parentheses.
A * B + C ((A * B) + C) A * (B + C) (A * (B + C) )
((A B * C + (A (B C + *
A B * C + A B C + *
94
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Evaluation of Arithmetic Expression
1. Initialize an empty stack of operators Operator In-Stack Input
2. While !end of expression Priority Priority
a) Get next input "token" from infix expression +, - 1 1
b) If token is … *, / 2 2
^ 3 4
i. operand display it
( 0 4
ii. operator
if operator has higher priority than top of stack
push token onto stack
else
pop and display top of stack
repeat comparison of token with top of stack
iii. "(" : push onto stack
iv. ")" : pop and display stack elements until
"(" occurs, do not display it
3. When end of infix reached, pop and display stack items until empty
95
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Evaluation of Arithmetic Expression
• (A+B)^(C-D)^E
+ top + top
( top ( top ( ( top
top ^
A A AB AB+ AB+
- top - top
( top ( top ( ( ^ top
^ ^ ^ ^ ^ top ^
AB+ AB+C AB+C AB+CD AB+CD- AB+CD-
^ top
^ ^ top
top
AB+CD-E AB+CD-E^ AB+CD-E^^
96
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Queues
97
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
What is a queue
• Stores a set of elements in a particular order
• Stack principle: FIRST IN FIRST OUT
• = FIFO
• It means: the first element inserted is the first one to
be removed
• Example
99
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
First In First Out
Front Rear
Front Rear
A B
Front Rear
A B C
Front Rear
B C
Front Rear
100
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Job Scheduling
101
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Queue Operations
• Create a queue
• Check if a queue is full or not
• Check if a queue is empty or not
• Add an element to a queue (enqueue)
• Remove an element from queue (dequeue)
102
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Array Implementation of Queue
• As with the array-based stack implementation, the
array is of fixed size
– A queue of maximum N elements
• Slightly more complicated
– Need to maintain track of both front and rear
• Define the queue:
typedef struct queue
{
int a[MAX_SIZE];
int rear, front;
} queue;
103
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Array Implementation of Queue
• Enqueue Operation
int enqueue (queue *q, int data)
{
if (qrear == MAX_SIZE – 1) Queue is full
return 0;
else
{
qrear == qrear + 1; Increment the rear
qa[qrear] = data; Insert the element
return 1;
}
}
104
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Array Implementation of Queue
• Dequeue Operation
int dequeue (queue *q, int *data)
{
if (qrear == qfront)
{
Queue is empty
qfront = -1;
qrear = -1;
*data = NULL;
return 0;
}
else
{
qfront = qfront + 1; Increment the front
*data = qa[qfront]; Remove the element
return 1;
}
}
105
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Circular Queue
EMPTY QUEUE
[2] [3] [2] [3]
J2 J3
front = 0 front = 0
rear = 0 rear = 3
106
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Circular Queue
Leave one empty space when queue is full. Why?
FULL QUEUE FULL QUEUE
[2] [3] [2] [3]
J2 J3 J8 J9
J5 J6 J5
front =0 front =4
rear = 5 rear =3
How to test when queue is empty?
How to test when queue is full?
107
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Circular Queue
• Enqueue Operation
108
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Circular Queue
• Dequeue Operation
109
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Linked List Implementation
• Define the nodes
Val Next Val Next Val Next Val Next
front
rear
typedef struct node
{
int item;
struct node *next;
} node;
• Define the rear and front
typedef struct queue
{
node *rear;
node *front;
}
110
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Linked List Implementation
• Enqueue Operation
void enqueue (queue *q, int data)
{
node *temp = (node *) malloc (size of (queue));
tempitem = data; New node
tempnext = NULL; created
if (qfront == NULL)
Queue is empty
{
qfront = temp;
qrear = temp;
return;
}
qrearnext = temp; Insert at the rear
qrear = temp; New node becomes rear
}
111
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Linked List Implementation
• Dequeue Operation
int dequeue (queue *q, int *data)
{
node *temp;
if (qfront == NULL)
{
data = NULL; Queue is empty
return 0;
}
temp = qfront;
*data = tempitem;
qfront = qfrontnext;
if (qrear == temp) Only node in
qrear = NULL; the Queue
free (temp);
return 1;
}
112
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Trees
113
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Introduction
• Non-linear data structure
• Depicts hierarchical relationship between the nodes
– Parent-child is typical relation
President-CEO Root
Production Sales
Manager Manager
Personnel
Manager
114
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Another Example
• Unix / Windows file structure
115
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Definition
A tree T is a finite set of one or more nodes
such that:
There is a specially designated node called
the root.
The remaining nodes are partitioned into n>=0
disjoint sets T1, ..., Tn, where each of these sets is
a tree.
We call T1, ..., Tn the subtrees of the root.
116
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Terminology
Degree of a node: number of Level
nodes connected of that node
– The node with degree 1 is a leaf or A 1
terminal node.
Children of the same parent are
siblings. B C D 2
Ancestors of a node: all the nodes
along the path from the root to
the node.
E F G H I J 3
Level: level of a node is one more
than its parent. Root node has a K L M
level 1. 4
Height of a tree: maximum level
of any node
117
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Binary Tree
A special class of trees: max number of child for
each node is 2.
Recursive definition: A binary tree is a finite set
of nodes that is either empty or consists of a root
and two disjoint binary trees called the left
subtree and the right subtree.
Any tree can be transformed into binary tree.
by left child-right sibling representation
Total number of binary trees possible with n
nodes is 2nCn - 2nCn-1
118
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Examples
A A
A
B B
C B C
D
D E F G
E
H I
119
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Examples
Left Child - Right Sibling A
A B
E C
B C D
F G D
K
E F G H I J
L H
K L M
M I
General Tree
Binary Tree J
120
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Properties
The maximum number of nodes on level i
of a binary tree is 2i-1, i>=1.
The maximum number of nodes in a
binary tree of depth h is 2h-1, h>=1.
Prove by induction
2
h
i 1
2 1 h
In other words
i 1 h=log(n+1)
121
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Properties
For any nonempty binary tree, T, if n0 is the
number of leaf nodes and n2 the number of
nodes with 2 children, then n0=n2+1
proof:
Let n and B are the total number of nodes & branches in T.
Let n0, n1, n2 represent the nodes with no children, single
child, and two children respectively.
n= n0+n1+n2, B+1=n, B=n1+2n2 ==>
n1+2n2+1= n,
n1+2n2+1= n0+n1+n2 ==> n0=n2+1
122
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Full Binary Tree
• If all non-leaf nodes of a binary tress have exactly
two non-empty children and all leaf nodes are at
the same level.
• A full binary tree of depth h is a binary tree of
depth h having 2h -1 nodes, h>=1.
A
B C
D E F G
H I J K L M N O
123
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Complete Binary Tree
• A full binary tree or Full up A
to level h-1 and if any node
at level h-1 has one child, B C
H I J K L M N O
B C B C
D E F G D E F G
H I H I J
124
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Complete Binary Tree
• A full binary tree or Full up
to level h-1 and if any node A
at level h-1 has one child,
B C
that must be a left child.
D E
H I
A
D E F G
H I K
125
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Binary Tree Representation
If a complete binary tree with n nodes is
represented sequentially, then for
any node with index i, 1<=i<=n, we have:
parent(i) is at i/2 if i!=1. If i=1, i is at the root and has no parent.
leftChild(i) is at 2i if 2i<=n. If 2i>n, then i has no left child.
rightChild(i) is at 2i+1 if 2i +1 <=n. If 2i +1 >n, then i has no
right child.
Relationships between labels
1
of children and parent:
1 2 3
i
2 3
2i 2i+1
4 5 6 7
126
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Binary Tree Representation
A [1] A
[1] A
[2] B
Wastage of space [2] B
[3] -- [3] C
B
[4] C [4] D
[5] -- [5] E
C [6] -- A [6] F
[7] G
[7] -- H
[8]
D [8] D B C
[9] I
[9] --
. . D E F G
E
[16] E
H I
127
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Linked Representation
typedef struct btnode
{
int data;
btnode *lchild, *rchild;
}btnode;
data
left data right
left right
128
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Creation of Binary Tree
p=(btnode*)malloc(sizeof(btnode));
main () pdata=3;
{ plchild=prchild=NULL;
qlchild=p;
btnode *Root=NULL; q=p;
btnode *p, *q;
q=Root;
p=(btnode*)malloc(sizeof(btnode)); p=(btnode*)malloc(sizeof(btnode));
pdata=1; pdata=4;
plchild=prchild=NULL;
plchild=prchild=NULL; qrchild=p;
Root=p; q=p; }
p=(btnode*)malloc(sizeof(btnode)); 1
pdata=2;
plchild=prchild=NULL;
2 3
qlchild=p; q=p;
4
129
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Binary Tree Traversal
Traversal is the process of visiting every node
once
Let l, R, and r denotes moving left, visiting
the node, and moving right.
Six possible combinations of traversal
lRr, lrR, Rlr, Rrl, rRl, rlR
Adopt convention that we traverse left before
right, only 3 traversals remain
lRr, lrR, Rlr
inorder, postorder, preorder
130
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Binary Tree Traversal
Inorder Traversal
1. Traverse left subtree
2. Visit the root
3. Traverse right subtree
• Inorder: A
HDIBEAFCG
• Postorder: B C
HIDEBFGCA D E F G
• Preorder:
ABDHIECFG H I
132
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Binary Tree Traversal
+
Inorder
A/B*C*D+E
* E infix expression
* D postorder
AB/C*D*E+
postfix expression
/ C
preorder
A B +**/ABCDE
prefix expression
133
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Binary Tree Traversal
void inorder(btnode *Root)
{
if (Root) {
inorder(Root->lchild);
printf(“%d”,Root->data);
inorder(Root->rchild);
}
}
134
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Binary Tree Traversal
void postorder(btnode *Root)
{
if (Root) {
postorder(Root->lchild);
postorder(Root->rchild);
printf(“%d”,Root->data);
}
}
135
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Binary Tree Traversal
void preorder(btnode *Root)
{
if (Root) {
printf(“%d”,Root->data);
preorder(Root->lchild);
preorder(Root->rchild);
}
}
136
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Reconstruction of Binary Tree
• It is impossible to reconstruct binary tree from
inorder or preorder or postorder traversals
alone.
• However, if inorder and preorder traversals
are given, a unique binary tree can be
reconstructed
137
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Reconstruction of Binary Tree
A
B C
D E F G
Inorder: D B E A F C G
Preorder: A B D E C F G
138
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Reconstruction of Binary Tree
A
A
B C
I.T: D B E I.T: F C G
P.T: B D E P.T: C F G
I.T: D I.T: E I.T: F I.T: G
P.T: D P.T: E P.T: F P.T: G
B C
D E F G
139
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Binary Search Trees
• Definition
– The keys in a nonempty left subtree (right
subtree) are smaller (larger) than the key in
the root of subtree.
– The left and right subtrees are also binary
search trees. K
140
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Binary Search Trees
• Binary Search Trees (BST) are a type of
Binary Trees with a special organization
of data.
141
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Examples and Counter Examples
8 5
5 11 4 8
2 6 10 12 1 7 11
4 7 9 14 3
13
142
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Examples and Counter Examples
8
5 11
2 4 10 18
4 15 20
21
143
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Inorder Traversal
10
5 15
2 9 20
7 17 30
25791015172030
5 15
2 9 20
7 17 30
146
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Searching in a BST
bstnode *search(int key, bstnode * root)
{
if (root == NULL) return root; 8
else if (key < rootdata)
5 11
return search(key, rootlchild);
else if (key > rootdata) 2 6 10 12
return find(key, rootrchild);
else 4 7 9 14
return root;
13
}
147
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Insertion into a BST
based on comparisons of the new item and
values of nodes in the BST
starting at the root probe down the tree till
you find a node whose left or right pointer is
empty and is a logical place for the new value
In other words, all inserts take place at a leaf
or at a leaflike node – a node that has only
one null subtree.
148
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Insertion into a BST
30 30 30
40 5 40 5 40
5
2 2 80 2 35 80
Insert 80 Insert 35
149
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Insertion into a BST
void insert(bstnode * newnode, bstnode * root)
{
if (rootdata > newnodedata)
{
if (rootlchild == NULL)
rootlchild=newnode;
else
insert( newnode, rootlchild );
}
else
{
if (rootrchild == NULL)
rootrchild=newnode;
else
insert( newnode, rootrchild );
}
}
150
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Insertion into a BST
The order of supplying the data determines where it
is placed in the BST , which determines the shape of
the BST
Create BSTs from the same set of data presented
each time in a different order:
a) 17 4 14 19 15 7 9 3 16 10
b) 9 10 17 4 3 7 14 16 15 19
151
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Inorder Successor
10
bstnode * successor(bstnode * n)
{
5 15
bstnode *iosuccessor;
if (nrchild == NULL)
iosuccessor= ???; 2 9 20
else
{ 7 17 30
iosuccessor=nrchild;
while (iosuccessorlchild != NULL)
iosuccessor=iosuccessorlchild;
}
return iosuccessor; How many children can the
} inorder successor of a node have?
152
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Inorder Predecessor
10
bstnode * predecessor(bstnode * n)
{
5 15
bstnode *iopredecessor;
if (nlchild == NULL)
iopredecessor= ???; 2 9 20
else
{ 7 17 30
iopredecessor=nlchild;
while (iopredecessorrchild != NULL)
iopredecessor=iopredecessorrchild;
}
return iopredecessor; How many children can the inorder
} predecessor of a node have?
153
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from a BST
• Following are the possible cases when we delete
a node:
– The node to be deleted has no children.
• Set the respective pointer of its parent to NULL.
– The node to be deleted has only a right subtree.
• Attach respective pointer of node’s parent to right subtree.
– The node to be deleted has only a left subtree.
• Attach respective pointer of node’s parent to left subtree.
– The node to be deleted has two subtrees.
• Replace node’s data with data in inorder successor
(predecessor) and delete the inorder successor
(predecessor).
154
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from a BST
Case 1: deleting a node with 2 EMPTY SUBTREES
8 8
Delete 9
5 11 5 11
2 6 10 12 2 6 10 12
4 7 9 14 4 7 14
13 13
155
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from a BST
Case 2: deleting a node with only RIGHT SUBTREES
8 8
Delete 6
5 11 5 11
2 6 10 12 2 7 10 12
4 7 9 14 4 9 14
13 13
156
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from a BST
Case 3: deleting a node with only LEFT SUBTREES
8 8
Delete 6
4 11 4 11
2 6 10 12 2 5 10 12
3 5 9 14 3 9 14
13 13
157
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from a BST
Case 4: deleting a node with 2 NON-EMPTY SUBTREES
8 9
Delete 8
5 11 5 11
2 6 10 12 2 6 10 12
4 7 9 14 4 7 9 14
13 13
158
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from a BST
bstnode * delete (bstnode * root, bstnode * n, bstnode * parent)
{
bstnode *iosuccessor, *retval;
if (nlchild != NULL && nrchild != NULL)
{ This function takes node to be deleted, its parent
successor (n, &parent, &iosuccessor); iosuccessor
as argument. When the function terminates
contains the inorder successor of n,
ndata = iosuccessordata; parent becomes the parent of iosuccessor
159
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from a BST
Check to see whether n belongs
if (parentlchild == n) to the left subtree of parent
parentlchild=nrchild;
else
parentrchild=nrchild;
retval=root;
}
else
retval=nrchild; Deleting root
}
if (nrchild == NULL)
{
if (parent != NULL)
160
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from a BST
{
if (parentlchild == n)
parentlchild=nlchild;
else
parentrchild=nlchild;
retval=root;
}
else
retval=nlchild; Deleting root
}
return retval;
}
161
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Analysis of BST Operations
The complexity of operations search,
insert and delete in BST is O(h) , where
h= O(log n), the height of BST.
162
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Height Balanced or AVL Trees
• Unbalanced Binary Search Trees are bad. Worst
case: operations take O(n).
• Height Balanced or AVL (Adelson-Velskii &
Landi) trees maintain balance.
– For each node in BST, height of left subtree and height
of right subtree differ by a maximum of 1.
A A
h
h-2
h-1
163
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Height Balanced or AVL Trees
typedef struct avlnode
{
int data;
int balancefactor;
bstnode *lchild, *rchild;
}avlnode;
164
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Examples of AVL trees
10 10
5 20 5 20
3 3 43
1
2
1 3
165
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Insertion into an AVL Tree
Insert 1 in the following AVL tree
12
8 16
4 10 14
2 6
166
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Insertion into an AVL Tree
After inserting 1 +2
12
+2
8 16
4 10 14
2 6
1
167
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Insertion into an AVL Tree
• Insertion of a node into AVL tree may result in
imbalance.
• To ensure balance condition, after insertion of a
new node, back up the path from the inserted
node to root and calculate balance factor for
each node.
– If the balance condition does not hold in a
certain node, we do one of the following
rotations:
• Single rotation
• Double rotation
168
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Insertion into an AVL Tree
hP=hQ=hR k1 k1
k2 k2
R P
P Q Q R
169
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Insertion into an AVL Tree
Case 1: Single Rotation
avlnode* rotateright (avlnode *y)
{
avlnode *x;
hA=hB+1 x=ylchild;
ylchild=xrchild;
hB=hC xrchild=y;
k2 k1 }
return x;
k1 k2
C
B A B C
A
170
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Insertion into an AVL Tree
Case 4: Single Rotation
avlnode* rotateleft (avlnode *y)
{
avlnode *x;
x=yrchild; hA=hB
yrchild=xlchild;
xlchild=y; hC=hB+1
return x; k2 k1
}
k1 k2
A
A B C B
C
171
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Insertion into an AVL Tree
Case 2 & 3 (inside case): Single Rotation does not work
k2 k1
k1 k2
C A
A C
B B
hB=hA+1
hA=hC
172
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Insertion into an AVL Tree
Case 2 & 3 (inside case): Double Rotation
+2 +2
k3 k3
-1 +1
k1 k2
k2 k1
D D
A C
B C A B
hA=hB=hC=hD
173
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Insertion into an AVL Tree
Case 2 & 3 (inside case): Double Rotation
k2
k1 k3
A B C D
174
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Insertion into an AVL Tree
Double Rotation
k3 k2
k1 k1 k3
k2
D A B C D
A
B C
hA=hB=hC=hD
175
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Insertion into an AVL Tree
Double Rotation
k2 k1
k1 k3 k3
k2
A B C D A
D
B C
hA=hB=hC=hD
176
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Insertion into an AVL Tree
11 11
8 20 4 20
4 16 27 3 8 16 27
3
177
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Insertion into an AVL Tree
11 11
8 20 5 20
4 16 27 4 8 16 27
5
178
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from AVL Tree
• Deleting a node from an AVL Tree is similar to
that of deleting a node from a binary search
tree. However, it may unbalance the tree.
• Starting from the deleted node, check all the
nodes in the path up to the root for the first
unbalance node.
– Use appropriate single or double rotation.
– May need to continue searching for unbalanced
nodes all the way to the root.
179
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from AVL Tree
• Deletion:
– Case 1: if X is a leaf, delete X
– Case 2: if X has 1 child, use it to replace X
– Case 3: if X has 2 children, replace X with its
inorder predecessor (and recursively
delete it)
• Rebalancing
180
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from AVL Tree
Delete 55
60
20 70
10 40 65 85
5 15 30 50 80 90
55
181
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from AVL Tree
Delete 55
60
20 70
10 40 65 85
5 15 30 50 80 90
55
182
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from AVL Tree
Delete 50
60
20 70
10 40 65 85
5 15 30 50 80 90
55
183
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from AVL Tree
Delete 50
60
20 70
10 40 65 85
5 15 30 50 80 90
55
184
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from AVL Tree
Delete 60
60
20 70
10 40 65 85
5 15 30 50 prev 80 90
55
185
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from AVL Tree
Delete 60
55
20 70
10 40 65 85
5 15 30 50 80 90
186
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from AVL Tree
Delete 55
55
20 prev 70
10 40 65 85
5 15 30 50 80 90
187
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from AVL Tree
Delete 55
50
20 70
10 40 65 85
5 15 30 80 90
188
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from AVL Tree
Delete 50
50
20 prev 70
10 40 65 85
5 15 30 80 90
189
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from AVL Tree
Delete 50
40
20 70
10 30 65 85
5 15 80 90
190
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from AVL Tree
Delete 40
40
20 prev 70
10 30 65 85
5 15 80 90
191
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from AVL Tree
Delete 40
30
20 70
10 Case ? 65 85
5 15 80 90
192
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Delete a Node from AVL Tree
After Rebalancing
30
10 70
5 20 65 85
15 80 90
193
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Graph Algorithms
194
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Definitions
• A graph G = (V,E) is composed of:
V: Finite, non-empty set of vertices
E: set of edges connecting the vertices in V
= Subset of VxV
• An edge e = (u,v) is a pair of vertices.
a b
V= {a, b, c, d, e}
c
E= {(a,b), (a,c), (a,d), (b,e),
(c,d), (c,e), (d,e)}
d e
195
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Definitions
• An undirected graph is one in which the pair of
vertices in a edge is unordered, (v0, v1) = (v1,v0)
• A directed graph is one in which each edge is a
directed pair of vertices, <v0, v1> != <v1,v0>
• If (v0, v1) is an edge in an undirected graph,
– v0 and v1 are adjacent
– The edge (v0, v1) is incident on vertices v0 and v1
• If <v0, v1> is an edge in a directed graph
– v0 is adjacent to v1, and v1 is adjacent from v0
196
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Definitions
The degree of a vertex is the number of edges
incident to that vertex
For directed graph,
the in-degree of a vertex v is the number of
edges that have v as the end vertex
the out-degree of a vertex v is the number of
edges that have v as the start vertex
if di is the degree of a vertex i in a graph G
with n vertices and e edges, the number of
edges is
n 1
e ( di) / 2
0
197
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Definitions
3 0
0 2
1 2
3 1 2 3 3 3
3 3 4 5 6
3
1 1 1 1
0 in:1, out: 1
1 in: 1, out: 2
2 in: 1, out: 0
198
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Definitions
• path: sequence of
3 2
adjacent.
a b a b
c c
d e d e
abedc bedc
199
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Definitions
• simple path: no repeated vertices
• cycle: simple path, except that the last vertex
is the same as the first vertex
a b a b
bec adca
c c
d e d e
200
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Definitions
• connected graph: any two vertices are connected by some path
201
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Definitions
• A weighted graph associates weights with the
edges
– e.g., a road map: edges might be weighted with
distance
10
7
8 8
6
6
10
202
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Definitions
• We will typically express running times in
terms of |E| and |V|
– If |E| |V|2 the graph is dense
– If |E| |V| the graph is sparse
203
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Graph Representation
• Adjacency Matrix
• Adjacency Lists
• Incidence Matrix
204
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Adjacency Matrix
Let G=(V,E) be a graph with n vertices.
The adjacency matrix of G is a two-dimensional
nxn array, say adj_mat
If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
If there is no such edge in E(G), adj_mat[i][j]=0
205
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Adjacency Matrix
0 1
a
1 2 2 d 4
3 b c
A 0 1 2 3
3
0 0 1 1 1 A 1 2 3 4
1 1 0 1 1 1 0 1 1 0
2 1 1 0 1 2 0 0 1 0
3 1 1 1 0 3 0 0 0 0
4 0 0 1 0
206
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Adjacency Matrix
• From the adjacency matrix, to determine the connection
of vertices is easy
– The degree of a vertex i is the number of 1’s in ith row
– For a directed graph, the number of 1’s in ith row is the
out_degree, while the number of 1’s in ith column in_degree.
• Time: to list all vertices adjacent to u: O(V).
• Time: to determine if (u, v) E: O(1).
• Space: O(V2).
– Not memory efficient for large graphs.
• Parallel edges cannot be represented
• Can store weights instead of bits for weighted graph.
207
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Adjacency Lists
• Adjacency list: for each vertex v V, store a
list of vertices adjacent to v
0
0
1 2
3 0 1
1 0 2 1
0 1 2 3
2
1 0 2 3
2 0 1 3 2
3 0 1 2
208
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Adjacency Lists
typedef struct adjvertex
{
int vertex;
struct node *next;
}adjvertex;
211
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Incidence Matrix
0
1 2
3
213
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Graph Traversal
• Depth First Search
– Once a possible path is found, continue the search
until the end of the path
– Think of a Stack
• Breadth First Search
– Start several paths at a time, and advance in each
one step at a time
– Think of a Queue
214
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
• We start at vertex s, and mark s “visited”. Next
we label s as our current vertex called u.
• Now we travel along an arbitrary edge (u, v).
• If edge (u, v) leads us to an already visited
vertex v we return to u.
• If vertex v is unvisited, we move to v, mark v
“visited”, set v as our current vertex, and
repeat the previous steps.
215
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
void DFS (int start)
{
int v;
adjvertex *adj;
visited [start] = 1;
printf (“%d”, start);
adj = gadjlist [start];
while (adj ! = NULL)
{
v = adjvertex;
if (! visited [v])
DFS (v);
adj = adj next;
}
} Total running time: O(V+E)
216
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
B C G
Adjacency Lists
D E
A: F C B G
B: A
C: A
F D: F E
E: G F D
F: A E D
G: E A
217
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
B C G
D E
F newly
discovered
F
Undiscovered
visit(A)
Marked
(A, F) (A, C) (A, B) (A, G)
Active
Stack
Finished
218
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
A already
marked B C G
D E
visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
visit(A)
Marked
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack
219
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
B C G
E newly
D EE
discovered
visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
visit(A)
Marked
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack
220
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
G newly
discovered
B C G
visit(E)
D E
(E, G) (E, F) (E, D)
visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
visit(A)
Marked
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack
221
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
visit(G)
E already B C G
marked (G, E) (G, A)
visit(E)
D E
(E, G) (E, F) (E, D)
visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
visit(A)
Marked
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack
222
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
visit(G)
A already B C G
marked (G, E) (G, A)
visit(E)
D E
(E, G) (E, F) (E, D)
visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
visit(A)
Marked
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack
223
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
Finished G
visit(G)
B C G (G, E) (G, A)
visit(E)
D E
(E, G) (E, F) (E, D)
visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
visit(A)
Marked
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack
224
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
F already B C G
marked
visit(E)
D E
(E, G) (E, F) (E, D)
visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
visit(A)
Marked
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack
225
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
B C G
D newly visit(E)
D E
discovered (E, G) (E, F) (E, D)
visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
visit(A)
Marked
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack
226
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
F already visit(D)
B C G
marked (D, F) (D, E)
visit(E)
D E
(E, G) (E, F) (E, D)
visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished Stack
227
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
E already visit(D)
B C G
marked (D, F) (D, E)
visit(E)
D E
(E, G) (E, F) (E, D)
visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished Stack
228
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
Finished D
visit(D)
B C G
(D, F) (D, E)
visit(E)
D E
(E, G) (E, F) (E, D)
visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished Stack
229
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
Finished E
B C G
visit(E)
D E
(E, G) (E, F) (E, D)
visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
visit(A)
Marked
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack
230
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
D already B C G
marked
D E
visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
visit(A)
Marked
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack
231
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
B C G
Finished F
D E
visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
visit(A)
Marked
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack
232
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
C newly
discovered
B C G
D E
F
Undiscovered
visit(A)
Marked
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack
233
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
A already B C G
marked
D E
visit(C)
F
(C, A)
Undiscovered
visit(A)
Marked
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack
234
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
B C G
Finished C
D E
visit(C)
F
(C, A)
Undiscovered
visit(A)
Marked
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack
235
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
B newly
BB C G
discovered
D E
F
Undiscovered
visit(A)
Marked
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack
236
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
A already B C G
marked
D E
visit(B)
F
(B, A)
Undiscovered
visit(A)
Marked
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack
237
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
B C G
Finished B
D E
visit(B)
F
(B, A)
Undiscovered
visit(A)
Marked
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack
238
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
G already B C G
finished
D E
F
Undiscovered
visit(A)
Marked
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack
239
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
B C G
D E
Finished A
F
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished Stack
240
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
B C G
D E
F
Undiscovered
Marked
Active
Finished
241
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
• Breadth-First Search (BFS) traverses a graph, and in doing so
defines a tree with several useful properties.
• The starting vertex s has level 0, and, as in DFS, defines that
point as an “anchor.”
• In the first round, all of the edges that are only one edge away
from the anchor are visited.
• These vertices are placed into level 1;
• In the second round, all the new edges from level 1 that can
be reached are visited and placed in level 2.
• This continues until every vertex has been assigned a level.
• The label of any vertex v corresponds to the length of the
shortest path from s to v.
242
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
void BFS (int start) {
int v, result; queue q; adjvertex *adj;
visited [start] = 1; enqueue (start, &q);
while ((result = dequeue (&v, &q)) != -1) { Nodes dequeued
printf (“%d”, v); adj = gadjlist [v];
while (adj != NULL) {
if (! visited [adjvertex])
{
visited [adjvertex] = 1; Nodes enqueued
enqueue (adjvertex, &q);
exactly once
}
adj = adjnext;
} } }
Total running time: O(V+E)
243
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
2 4 8
s 5 7
3 6 9
244
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered
Top of queue Queue: s
Finished
245
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1
Current node = s 2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered
Top of queue Queue:
Finished
246
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1
Current node = s 2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered
Top of queue Queue: 2
Finished
247
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1
Current node = s 2 4 8
0 s 5 7
1
3 6 9
Undiscovered
Discovered
Top of queue Queue: 2 3
Finished
248
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1
Current node = s 2 4 8
0 s 5 7
1
3 6 9
Undiscovered
Discovered
Top of queue Queue: 2 3 5
Finished
249
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2
Current node = 2 2 4 8
0 s 5 7
1
3 6 9
Undiscovered
Discovered
Top of queue Queue: 3 5
Finished
250
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2
Current node = 2 2 4 8
5 already discovered:
0 s 5 7
don't enqueue
1
3 6 9
Undiscovered
Discovered
Top of queue Queue: 3 5 4
Finished
251
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2
Current node = 2 2 4 8
0 s 5 7
1
3 6 9
Undiscovered
Discovered
Top of queue Queue: 2 3 5 4
Finished
252
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2
Current node = 3 2 4 8
0 s 5 7
1
3 6 9
Undiscovered
Discovered
Top of queue Queue: 5 4
Finished
253
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2
Current node = 3 2 4 8
0 s 5 7
1
3 6 9
1 2
Undiscovered
Discovered
Top of queue Queue: 5 4
Finished
254
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2
Current node = 3 2 4 8
0 s 5 7
1
3 6 9
1 2
Undiscovered
Discovered
Top of queue Queue: 5 4 6
Finished
255
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2
Current node = 5 2 4 8
0 s 5 7
1
3 6 9
1 2
Undiscovered
Discovered
Top of queue Queue: 4 6
Finished
256
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2
Current node = 5 2 4 8
0 s 5 7
1
3 6 9
1 2
Undiscovered
Discovered
Top of queue Queue: 4 6
Finished
257
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2
Current node = 4 2 4 8
0 s 5 7
1
3 6 9
1 2
Undiscovered
Discovered
Top of queue Queue: 6
Finished
258
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
Current node = 4 2 4 8
0 s 5 7
1
3 6 9
1 2
Undiscovered
Discovered
Top of queue Queue: 6
Finished
259
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
Current node = 4 2 4 8
0 s 5 7
1
3 6 9
1 2
Undiscovered
Discovered
Top of queue Queue: 6 8
Finished
260
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
Current node = 6 2 4 8
0 s 5 7
1 3
3 6 9
1 2
Undiscovered
Discovered
Top of queue Queue: 8
Finished
261
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
Current node = 6 2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered
Top of queue Queue: 8 7
Finished
262
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
Current node = 6 2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered
Top of queue Queue: 8 7 9
Finished
263
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
Current node = 6 2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered
Top of queue Queue: 7 9
Finished
264
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
Current node = 7 2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered
Top of queue Queue: 9
Finished
265
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
Current node = 7 2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered
Top of queue Queue: 9
Finished
266
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
Current node = 7 2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered
Top of queue Queue: 9
Finished
267
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
Current node = 7 2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered
Top of queue Queue: 9
Finished
268
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
Current node = 9 2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered
Top of queue Queue:
Finished
269
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
Current node = 9 2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered
Top of queue Queue:
Finished
270
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
Current node = 9 2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered
Top of queue Queue:
Finished
271
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
Current node = 9 2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered
Top of queue Queue:
Finished
272
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
BFS Tree
273 273
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Single Source Shortest Path Problem
274
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Dijkstra’s Algorithm
solution to the single-source shortest path problem in
graph theory.
277
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Dijkstra’s Algorithm
278
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Dijkstra’s Algorithm
279
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Dijkstra’s Algorithm
280
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Dijkstra’s Algorithm
281
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Dijkstra’s Algorithm
282
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Dijkstra’s Algorithm
283
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Dijkstra’s Algorithm
284
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Dijkstra’s Algorithm
285
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Dijkstra’s Algorithm
286
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Spanning Trees
or or or
287
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Minimum Spanning Trees
The Minimum Spanning Tree for a given graph is the Spanning Tree of
minimum cost for that graph.
7 7 7 7
7 7
288
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Algorithms for Obtaining the Minimum Spanning Tree
• Kruskal's Algorithm
• Prim's Algorithm
289
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Kruskal's Algorithm
290
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Kruskal's Algorithm
The steps are:
Every step will join two trees in the forest together, so that at the
end, there will only be one tree in T.
291
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph
B 4 C
4
2 1
A 4 E
1 F
D 2 3
10
G 5
5 6 3
4
I
H
2 3
J
292
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
A 4 B A 1 D
B 4 C B 4 D
B 4 C B 10 J C 2 E
4
2 1
C 1 F D 5 H
A 4 E
1 F
2 D 6 J E 2 G
D 3
10
G 5
F 3 G F 5 I
5 6 3
4
I G 3 I G 4 J
H
2 3
J H 2 J I 3 J
293
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Sort Edges A 1 D C 1 F
C 2 E E 2 G
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
294
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Add Edge A 1 D C 1 F
C 2 E E 2 G
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
295
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Add Edge A 1 D C 1 F
C 2 E E 2 G
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
296
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Add Edge A 1 D C 1 F
C 2 E E 2 G
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
297
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Add Edge A 1 D C 1 F
C 2 E E 2 G
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
298
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Add Edge A 1 D C 1 F
C 2 E E 2 G
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
299
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Cycle
Don’t Add Edge A 1 D C 1 F
C 2 E E 2 G
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
300
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Add Edge A 1 D C 1 F
C 2 E E 2 G
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
301
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Add Edge A 1 D C 1 F
C 2 E E 2 G
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
302
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Add Edge A 1 D C 1 F
C 2 E E 2 G
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
303
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Cycle
Don’t Add Edge A 1 D C 1 F
C 2 E E 2 G
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
304
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Add Edge A 1 D C 1 F
C 2 E E 2 G
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
305
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Minimum Spanning Tree Example Graph
B 4 C 4
B C
4 4
2 1 2 1
A E A 4
1 F E F
1
D 2 2
D 3
10
G G 5
3 5 6 3
4
I I
H H
2 3 3
J 2 J
306
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Analysis of Kruskal's Algorithm
307
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Prim's Algorithm
308
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Prim's Algorithm
The steps are:
1. The new graph is constructed - with one node from the old
graph.
2. While new graph has fewer than n nodes,
2.1. Find the node from the old graph with the smallest
connecting edge to the new graph,
2.2. Add it to the new graph 2 Complexity: O(n )
In every step one node is added, so that at the end we will have
one graph with all the nodes and it will be a minimum spanning
tree of the original graph.
309
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph
B 4 C
4
2 1
A 4 E
1 F
D 2 3
10
G 5
5 6 3
4
I
H
2 3
J
310
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph New Graph
B 4 C 4
B C
4 4
2 1 2 1
A 4 E A 4
1 F E F
1
D 2 3 2
D 3
10 10
G 5 5
G
5 6 3 5 6 3
4 4
I I
H H
2 3 3
J 2 J
311
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph New Graph
B 4 C 4
B C
4 4
2 1 2 1
A 4 E A 4
1 F E F
1
D 2 3 2
D 3
10 10
G 5 5
G
5 6 3 5 6 3
4 4
I I
H H
2 3 3
J 2 J
312
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph New Graph
B 4 C 4
B C
4 4
2 1 2 1
A 4 E A 4
1 F E F
1
D 2 3 2
D 3
10 10
G 5 5
G
5 6 3 5 6 3
4 4
I I
H H
2 3 3
J 2 J
313
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph New Graph
B 4 C 4
B C
4 4
2 1 2 1
A 4 E A 4
1 F E F
1
D 2 3 2
D 3
10 10
G 5 5
G
5 6 3 5 6 3
4 4
I I
H H
2 3 3
J 2 J
314
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph New Graph
B 4 C 4
B C
4 4
2 1 2 1
A 4 E A 4
1 F E F
1
D 2 3 2
D 3
10 10
G 5 5
G
5 6 3 5 6 3
4 4
I I
H H
2 3 3
J 2 J
315
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph New Graph
B 4 C 4
B C
4 4
2 1 2 1
A 4 E A 4
1 F E F
1
D 2 3 2
D 3
10 10
G 5 5
G
5 6 3 5 6 3
4 4
I I
H H
2 3 3
J 2 J
316
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph New Graph
B 4 C 4
B C
4 4
2 1 2 1
A 4 E A 4
1 F E F
1
D 2 3 2
D 3
10 10
G 5 5
G
5 6 3 5 6 3
4 4
I I
H H
2 3 3
J 2 J
317
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph New Graph
B 4 C 4
B C
4 4
2 1 2 1
A 4 E A 4
1 F E F
1
D 2 3 2
D 3
10 10
G 5 5
G
5 6 3 5 6 3
4 4
I I
H H
2 3 3
J 2 J
318
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph New Graph
B 4 C 4
B C
4 4
2 1 2 1
A 4 E A 4
1 F E F
1
D 2 3 2
D 3
10 10
G 5 5
G
5 6 3 5 6 3
4 4
I I
H H
2 3 3
J 2 J
319
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph New Graph
B 4 C 4
B C
4 4
2 1 2 1
A 4 E A 4
1 F E F
1
D 2 3 2
D 3
10 10
G 5 5
G
5 6 3 5 6 3
4 4
I I
H H
2 3 3
J 2 J
320
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph Minimum Spanning Tree
B 4 C 4
B C
4 4
2 1 2 1
A 4 E A
1 F E F
1
D 2 3 2
D
10
G 5
G
5 6 3 3
4
I I
H H
2 3 3
J 2 J
321
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
322
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Any Doubt ?
• Please feel free to
write to me:
bhaskargit@[Link]
323
Bhaskar Sardar, Information Technology Department, Jadavpur University, India