Documente Academic
Documente Profesional
Documente Cultură
i
Lecture 7: Sorting
Selection sort and its analysis
Bubble sort and its analysis
Radix sort
Lecture 8: Sorting and Recursion
Insertion sort
Recursive functions
Lecture 9: Quick Sort
Splitting mechanism
Quick sort algorithm
Quick sort program
Lecture 10: Structures
Defining and using structures
Array of structures
Copying structure variables
Nested structures
Passing structure elements
Passing structures
Lecture 11: Structures and Polynomials
Polynomial representation
Passing array of structures
General operations on polynomials
Lecture 12: Two Dimensional Arrays
Using two dimensional arrays
Saddle point of an array
Lecture 13: Sparse Matrices
Sparse matrices
Passing arrays
3 tuple conversion
Lecture 14: Transpose of Sparse Matrices
Transpose of a sparse matrix
Lecture 15: Addition of Sparse Matrices
Addition of sparse matrices
Lecture 16: Multiplication of Sparse Matrices
Matrix multiplication
ii
Multiplication of sparse matrices
Lecture 17: Storage
Storage - two dimensional arrays
3 dimensional arrays
4 dimensional arrays
n - dimensional arrays
Lecture 18: Dynamic memory allocation
Limitations of arrays
Dynamic memory allocation
Steps involved in memory allocation
Lecture 19: Linked Lists
Memory leaks
Linked lists
Creation of linked lists
Lecture 20: Operations on Linked Lists I
Appending a new node to a link list
Adding a new node at the beginning of a linked list
Adding a new node at a specified position in a linked list
Deleting a node from a linked list
Deleting all the nodes in a list
Lecture 21: Operations on Linked Lists II
Arranging the elements in a linked list in ascending order
Sorting the elements in a linked list
Lecture 22: Operations on Linked Lists - III
Reversing a linked list
Merging two linked lists
Lecture 23: Operations on Linked Lists - IV
Concatenating two linked lists
Addition of polynomials using linked lists
Lecture 24: Circular Linked Lists
Circular linked lists
Adding a new node to a circular list
Deleting an existing node from a circular list
Counting the nodes of a circular linked list
Displaying the nodes in a circular linked list
iii
Lecture 25: Doubly Linked Lists
Doubly linked lists
Common operations on a doubly linked list
Lecture 26: Sparse Matrices as Linked Lists I
Representing sparse matrices in the form of linked lists
Lecture 27: Sparse Matrices as Linked Lists II
Add new node to linked Lists
Display contents of Linked List
Delete all nodes in the Linked List
Program to implement sparse matrices as linked lists
Lecture 28: Recursive LL
Recursive count( )
Recursive compare( )
Recursive copy( )
Recursive append( )
Lecture 29: Linked Lists Using Unions
What are unions
Declaring unions
Using unions
Generalized linked lists
Lecture 30: Generalized Linked Lists
Copying generalized linked lists
Depth of generalized linked lists
Comparing generalized linked lists
Lecture 31: Stacks
Stack and its utilities
Program to implement stack as an array
Lecture 32: Stack as a Linked List
Program to implement stack as a linked list
Lecture 33: Stack Expressions
Different form of expressions
Lecture 34: Stack Operations
Infix to Postfix conversion
Lecture 35: Postfix
Evaluation of Postfix form
iv
Lecture 36: Infix To Prefix
Infix to Prefix conversion
Lecture 37: Postfix To Prefix
Evaluation of Prefix form
Postfix to Prefix conversion
Lecture 38: Postfix To Infix
Postfix to Infix conversion
Lecture 39: More Conversions
Prefix to Postfix conversion
Prefix to Infix conversion
Lecture 40: Queue I
Queue
Program to implement queue as an array
Lecture 41: Queue II
Limitations of queue
Circular queue
Lecture 42: Deque and Priority Queue
Deque
Program to implement deque
Priority queue
Lecture 43: Trees
What are trees
Tree terminology
Strictly and complete trees
Lecture 44: Tree Traversal
Preorder, Postorder and Inorder traversal
Reconstruction of trees
Lecture 45: Binary Search Trees I
Representing trees using arrays
Linked representation of trees
Binary search trees
Lecture 46: Binary Search Trees II
Traversing of trees using Inorder, Preorder and Postorder
Non recursive Inorder traversal of trees
Non recursive Preorder traversal of trees
v
Non recursive Postorder traversal of trees
Comparison of two trees
Lecture 47: Binary Tree
Successor and predecessor
Insertion of a node in a binary search tree
Deleting an existing node from a binary search tree
Searching a node in a binary search tree
Lecture 48: Threaded Binary Tree
Threaded binary trees
Representation of threaded binary trees
Inserting a node in a threaded binary tree
Traversing the threaded binary tree in inorder
Lecture 49: Heap
Heap
Creation of a heap
Heap sort
Lecture 50: AVL Trees and B Tree
Program to create a heap
AVL tree
2 - 3 tree
B tree
Lecture 51: Graphs
Graphs
Terminology associated with graphs
Representation of graphs
Adjacency list
Lecture 52: Adjacency Multilist
Adjacency matrices
Adjacency multilist
Orthogonal representation of graphs
Lecture 53: Depth First Search
Depth first search
Program to implement depth first search algorithm
Lecture 54: Breadth First Search
Breadth first search
vi
Program to implement breadth first search algorithm
Lecture 55: Spanning Tree
What is a spanning tree
Cost of spanning tree
Kruskal’s algorithm to determine minimum cost spanning tree
Lecture 56: Dijkstra’s Algorithm
Dijkstra’s algorithm to determine minimum cost spanning tree
AOV network
Lecture 57: Memory Management I
Memory management
First fit algorithm
Best fit algorithm
Lecture 58: Memory Management II
Next fit algorithm
Lecture 59: Garbage Collection
Garbage Collection
Marking algorithm
Compaction
Lecture 60: File Organization
Files
File Organization
vii
C Revision - I
Yashavant Kanetkar
Objectives
The importance of data structures
The data types in C
What are instructions and its types
The decision control instruction
Working of logical operators
Different types of loops
Difference between break, continue and exit
Case control instruction
Data Structures
What are they
Way of organizing data and opns. performed on it
Where are they used
- Car Parking
- File Storage
- Dictionary
- Shortest route
- Searching Law Books Ô¿²¹«¿¹»
- Sorting
- Device Drivers
- Evaluation of expression
- Networking
C Instructions
Type Declaration Instructions
int i, j ; char ch, dh ; float a, b ;
Arithmetic Instructions
c = 5 / 9.0 * ( f - 32 ) ;
+ - * / % - Arithmetic operators
Input / Output Instructions
printf( ), scanf( )
Control Instructions
Control the sequence of execution of instructions
Types - Sequence - Default
- Decision
- Repetition
- Case
while ( …. ) for ( .. ; .. ;… ) do
{ { {
if ( cond1 ) if ( cond1 ) if ( cond1 )
break ; break ; break ;
if ( cond2 ) if ( cond2 ) if ( cond2 )
continue ; continue ; continue ;
} } } while ( …. ) ;
Objectives
Operations performed on an array
What are algorithms
Important features of algorithms
Conventions to follow while writing an algorithm
Linear search and Binary search
Algorithm
Method of accomplishing a task in a finite number of steps
Origin - Persian Mathematician - Abu Jaffer Al-Khowarizmi
Aka - Recipe, Method, Technique, Procedure, Routine
Important Features:
Linear Search
main( )
{
int a[ ] = { 11, 2, 9, 13, 57, 25, 17, 1, 90, 3 } ;
int i, num ;
printf ( "Enter no. to search: " ) ;
scanf ( "%d", &num ) ;
for ( i = 0 ; i <= 9 ; i++ )
{
if ( a[ i ] == num )
break ;
}
if ( i == 10 )
printf ( "No such number in array" ) ;
else
printf ( "Number is at position %d", i ) ;
}
Binary Search l m u
main( ) 1 2 3 9 11 13 17 25 57 90
{
int a[ ] = { 1, 2, 3, 9, 11, 13, 17, 25, 57, 90 } ;
int l = 0, u = 9, m, num ;
printf ( "Enter number to search: " ) ;
scanf ( "%d", &num ) ;
57
while ( l <= u )
{
m=(l+u)/2;
if ( a[m] == num )
{
printf ( "No. is at position %d ", m ) ; exit( ) ;
}
a[m] > num ? ( u = m - 1 ) : ( l = m + 1 ) ;
}
printf ( "Element is not present in the array." ) ;
}
Objectives
Analyzing an algorithm
Rate of increase for growth rate with respect to
Big Oh notation
Analysis Of Algorithms
Determining
What To Count
Multiplication of Matrices- Number of multiplications
Searching - Number of comparisons
Sorting - Number of comparisons
Count chars in a file
Fibonacci Series
1 fibonacci ( ) Line no. No. of execution
2 {
3 old = 1 ;
4 new = 1 ;
5 n = 20 ;
6 for ( i = 1 ; i < n ; i + + )
7 {
8 a = old + new ;
9 printf ( "%d ", a ) ;
10 old = new ;
11 new = a ;
12 }
13 }
Ignoring the const 5 & 2
complexity -> O( n )
Rate of Increase
n log n n log n n2 n3 2n
1 0.0 0.0 1.0 1.0 2.0
2 1.0 2.0 4.0 8.0 4.0
5 2.3 11.6 25.0 125.0 32.0
10 3.3 33.2 100.0 1000.0 1024.0
15 3.9 58.6 225.0 3375.0 32768.0
20 4.3 86.4 400.0 8000.0 1048576.0
30 4.9 147.2 900.0 27000.0 1073741824.0
40 5.3 212.9 1600.0 64000.0 1099511627776.0
50 5.6 282.2 2500.0 125000.0 1125899906842620.0
O( 1 ) - const., O( n ) - linear, O( n2 ) - quadratic, O( n3 ) - cubic
O( 2n ) - exponential
O( log n ) is faster than O( n )
O( n log n ) is faster than O( n2 ) but not as good as O( n )
Exercise
For which range of values would the algorithm whose order
of magnitude is n3 be better than an algorithm who order of
magnitude is 2n
Range Better
n n3 2n
1 1.0 2.0
2 8.0 4.0
5 125.0 32.0
6 216.0 64.0
7 343.0 128.0
8 512.0 256.0
9 729.0
10 1000.0 1024.0
15 3375.0 32768.0
20 8000.0 1048576.0
30 27000.0 1073741824.0
40 64000.0 1099511627776.0
50 125000.0 1125899906842620.0
Objectives
Tools to calculate time complexity
Cases to be considered while analyzing
algorithms
Analysis of Linear search and Binary search
Classification Of Growth
Rate of growth is dominated by largest term in an equation
Neglect terms that grow more slowly
Leftover is known as order of the algorithm
Algos. are grouped into 3 categories based on their order
Big Omega - (f)
If g( x ) ( f ), g( n ) >= cf( n ) for all n >= n0 (c = const.)
Represents class of funcns that grow at least as fast as f
Big Oh - O( f )
If g( x ) O( f ), g( n ) <= cf( n ) for all n >= n0 (c = const.)
Represents class of funcns that grow no faster than f
Big Theta - ( f )
( f ) = ( f ) O( f )
Represents class of funcns that grow as fast as f
Average Case
N
1 i
A( N ) = +N
Probability
N+1 * i=1
N
1 1
i
A( N ) =
N+1
i=1
+ N+1 *N
1 N(N+1) N
A( N ) = +N+1
N+1 * 2
N N
+1_
N 1
A( N ) = + N+1 =
2 2 N+1
N+2
A( N )
2
1
(As n gets very large, becomes almost 0)
N+1
Objectives
Hashing Techniques
Division method
Mid – Square method
Folding method
Digit Analysis method
Linear and quadratic probing
Hashing Functions
Division
Mid - Square
Folding
Digit Analysis
Mid - Square
Identifiers - A = 1, ..., Z = 26, 0 = 27, 1 = 28, ..., 9 = 36
Find octal equivalent of each character
Square the octal equivalent
Use middle bits of square as hash value
Table size = 2r, r is no. of middle bits
X X1 (X1) 2
A 01 1
B 02 4
... ... ...
Y 31 1701
Z 32 2000
0 33 2101
1 34 2204
A1 134 20420
A2 135 20711
CAT 030124 125620
Folding
Distribute digits in multiple partitions
Excluding last make all partitions equal
Add partition values to get hash value
No. Of
Partition
Digits
1 1
2 2
3 1, 2 CAT 030124
4 1, 1, 2
5 2, 2, 1 0 3 0 1 24 28
6 1, 1, 1, 1, 2
7 3, 3, 1
8 3, 3, 2
9 2, 2, 2, 2, 1
... ...
More Hashing
Hash table contains buckets
Each buckets may contain several slots
Each slot can hold one record
Collision - when two identifiers hash into same bucket
Overflow - when new identifier is hashed into a full bucket
If number of slots = 1 then Collision = Overflow
Collision handling techniques:
Linear probing -
search the bucket ( f ( x ) + i ) % b, for 0 <= i <= b - 1
Quadratic probing -
search the bucket f ( x )
( f ( x ) + i2 ) % b
( f ( x ) - i2 ) % b, for 1 <= i <= ( b - 1) / 2
Linear Probing
4028 2133 1098 7915 6749 5141 3138 f ( x ) = no. % 10
key = ( f( x ) + i ) % b
f ( int no, int *arr, int b )
{ 0 6749
int i, j, initialpos ; 1 5141
j = no % 10 ; intialpos = j ; 2 3138
for ( i = 1 ; arr[ j ] != no && arr[ j ] != 0 ; i + + ) 3 2133
{ 4
j = ( no % 10 + i ) % b ;
if ( j == initialpos ) 5 7915
{ 6
printf ( "Array full" ) ; return ; 7
} 8 4028
} 9 1098
arr [ j ] = no ;
}
f ( x ) = no. % 10 0
key = f( x ) 1 5141
= f( x ) + i2 ) % b 2 3138
= f( x ) - i2 ) % b 3 2133
for 1 <= i <= ( b -1 ) / 2 4
5 7915
6
7 6748
8 4028
9 1098
Objectives
main( )
Selection Sort
{ 17 6 13 12 2 i j
int a[ ] = { 17, 6, 13,12, 2 } ; 6 17 13 12 2 0-1
int i, j, t ; 6 17 13 12 2 0-2
for ( i = 0 ; i <= 3 ; i + + ) 6 17 13 12 2 0-3
{ 2 17 13 12 6 0-4
for ( j = i + 1 ; j <= 4 ; j + + ) 2 13 17 12 6 1-2
{ 2 12 17 13 6 1-3
if ( a[ i ] >a[ j ] )
2 6 17 13 12 1-4
{
t = a[ i ] ; a[ i ] = a[ j ] ; 2 6 13 17 12 2-3
a[ j ] = t ; 2 6 12 17 13 2-4
}
}
2 6 12 13 17 3 - 4
}
for ( i = 0 ; i <= 4 ; i + + )
printf ( "%d", a[ i ] ) ;
}
Program
main( ) initq ( int arr[ 10 ][ 3 ] )
{ {
int a[ ] = { 9, 47, 21, 32, 5, int i, j ;
13, 27, 4, 54, 76 } ; for ( i = 0 ; i < 10 ; i + + )
int arr[ 10 ][ 3 ], k, dig ; {
dig = 1 ; for ( j = 0 ; j < 3 ; j + + )
for ( k = 0 ; k <= 1 ; k + + ) arr[ i ][ j ] = 0 ;
{ }
initq ( arr ) ; }
radix ( a, arr, 10, dig ) ;
combine ( a, arr ) ;
dig *= 10 ;
}
for ( i = 0 ; i < 10 ; i + + )
printf ( "%d", a [ i ] ) ;
}
Contd...
...Contd.
radix ( int a[ ], int arr[ ][ 3], combine ( int *a, int arr[ ][ 3 ] )
int n, int dig ) {
{ int i, j, x = 0 ;
int i, j, key ; for ( i = 0 ; i < 10 ; i + + )
for ( i = 0 ; i < n ; i + + ) {
{ for ( j = 0 ; j < 3 ; j + + )
key = ( a [ i ] / dig ) % 10 ; {
for ( j = 0 ; j < 3 ; j + + ) if ( arr[ i ][ j ] ! = 0 )
{
{
a[ x ] = arr[ i ][ j ] ;
if ( arr[ key ][ j ] == 0 )
x++;
{
}
arr[ key ][ j ] = arr[ i ] ;
}
break ;
}
}
}
}
}
}
Objectives
Insertion Sort
Recursive Functions
main( )
More General
31698
{
int num, sum ;
d5
printf ( ”Enter a number” ) ;
scanf ( ”%d”, &num ) ; 485
sum = sumdig ( num ) ; d3
d3
printf ( ”%d”, sum ) ;
}
sumdig ( int n ) n s d
{ 327 0 7
int d ; int s = 0 ; 32 7 2
while ( n != 0 ) 3 9 3
{ 0 12
d = n % 10 ;
n = n / 10 ; s = s + d ;
}
return ( s ) ;
}
main( )
{
int num, sum ;
printf ( ”Enter a number” ) ;
scanf ( ”%d”, &num ) ;
sum = rsum ( num ) ;
printf ( ”%d”, sum ) ;
}
rsum ( int n )
{ 4!
int d ; int s ;
if ( n != 0 ) 4*3*2*1
{ 4*3!
d = n % 10 ; n = n / 10 ;
s = d + rsum ( n ) ; 3*2!
}
else 2*1!
return ( 0 ) ;
} return (s);
Recursive Factorial
main( ) Recursion Tips
{
int num, fact ;
Make recursive call in an if
printf ( ”Enter no.” ) ;
scanf ( ”%d”, &num ) ; else block is escape route
fact = refact ( num ) ; else contains end cond. logic
printf ( ”%d”, fact ) ;
return may not be present
}
refact ( int n )
{
int p ;
if ( n != 0 )
p = n * refact ( n - 1 ) ;
else
return ( 1 ) ;
return ( p ) ;
}
Objectives
11 2 9 13 57 25 17 1 90 3 Split Array
11 2 9 13 57 25 17 1 90 3
11 2 9 13 57 25 17 1 90 3
11 2 9 13 57 25 17 1 90 3
11 2 9 3 57 25 17 1 90 13
11 2 9 3 57 25 17 1 90 13
11 2 9 3 1 25 17 57 90 13
11 2 9 3 1 25 17 57 90 13
1 2 9 3 11 25 17 57 90 13
..Contd 11 2 9 13 57 25 17 1 90 3 p - L to R
q - R to L
int split ( int a[ ], int lower, int upper ) if ( p < q )
{ {
int i, p, q, t ; t = a[ p ] ;
p = lower + 1 ; a[ p ] = a[ q ] ;
q = upper ; a[ q ] = t ;
i = a[ lower ] ; }
}
while ( p <= q ) t = a[ lower ] ;
{ a[ lower ] = a[ q ] ;
while ( a[ p ] < i ) a[ q ] = t ;
p++; return q ;
while ( a[ q ] > i ) }
q--;
11 2 9 3 1 25 17 57 90 13
1 2 9 3 11 25 17 57 90 13
Problem
Show the steps for sorting the following numbers using
Quick Sort Algorithm
Contd… q p
43 23 36 11 65 58 94 74 99 87
11 23 36 43 65 58 94 74 99 87
p q p q
11 23 36 43 65 58 94 74 99 87
pq p q
11 23 36 43 65 58 94 74 99 87
q p p q
11 23 36 43 65 58 94 74 99 87
pq
11 23 36 43 65 58 94 74 99 87
q p
65 58 94 74 99 87
58 65 94 74 99 87
Complexity
Algorithm Worst Case Best Case
Bubble sort O (n2) O (n2)
Selection Sort O (n2) O (n2)
Quick Sort O (n2) log2 n
Insertion sort O (n2) n–1
Binary Tree Sort O (n2) O (n log n)
Heap Sort O (n log n) O (n log n)
Merge Sort O (n log n) O (n log n)
Objectives
Handling Data
main( )
{
char n[ ] = { ’A’, ’X’, ’Y’, ’\0’ } ;
int a[ ] = { 23, 27, 28 } ;
float s[ ] = { 4000.50, 5000.00, 6000.75 } ;
int i ;
for ( i = 0 ; i <= 2 ; i ++ )
printf ( "%c %d %f", n[ i ], a[ i ], s[ i ] ) ;
}
main( )
{
Array of Structures
struct employee
{
char n ;
int a ;
float s ;
};
struct employee e[ ] = {
{ ’A’, 23, 4000.50 } ,
{ ’X’, 27, 5000.00 } ,
{ ’Y’, 28, 6000.75 }
};
int i ;
for ( i = 0 ; i <= 2 ; i++ )
printf ( "%c %d %f", e[ i ] .n, e[ i ].a, e[ i ].s ) ;
}
Terminology
Structure
Keyword struct employee name/tag
{
char n ; Structure
int a ; elements/
members
float s ;
};
struct employee e1, e2, e[ 10 ] ;
Structure
variables Array of
structures
struct employee e[ 3 ] = { .. } ;
A 23 400.50 X 27 500.00 Y 28 600.75
401 408 415
Array of Structures
A 23 400.50 X 27 500.00 Y 28 600.75
401 408 415
struct employee e[ ] = { ... } ;
Ptr. to structure
char *p ;
struct employee *q ;
struct employee (*r )[ 3 ] ; Ptr. to array of structures
e1
Copying Rahul 23 400.50
main( ) e2
{ Rahul 23 400.50
struct emp
{ e3
char n[20] ;
int a ; Rahul 23 400.50
float s ;
};
struct emp e1 = { "Rahul", 23, 4000.50 } ;
piecemeal
copying struct emp e2, e3 ;
e2.n = e1.n ; strcpy ( e2.n, e1.n) ;
e2.a = e1.a ;
e2.s = e1.s ; copying at
one shot
e3 = e1 ;
printf ( "%s %d %f", e3.n, e3.a, e3.s ) ;
}
Passing Structures b
Basic 425 135.00
main( )
{ 501 521 523
struct book
{
char n[20] ; int nop ; float pr ;
};
struct book b = { "Basic", 425, 135.00 } ;
display1 ( b ) ; show1 ( &b ) ;
}
display1 (struct book bb )
{
printf ("%s %d %f", bb.n, bb.nop, bb.pr ) ;
}
show1 ( struct book *bb )
{
printf ( "%s %d %f", ( * bb ) .n,( * bb ) .nop, ( * bb ) .pr );
printf ( "%s %d %f", bb -> n, bb -> nop, bb -> pr ) ;
}
Objectives
main( )
Two Dimensional Array
{
int a[ ][ 5 ] = {
{ 2, 6, 1, 8, 4 } ,
optional { 1, 2, 5, 6, 8 },
optional
compulsory { 7, 9, 8, 7, 21 },
{ 4, 5, 6, 8, 10 } int b[ ][1][2][3]
};
int i, j ;
printf ( "%d", a[ 2 ][ 4 ] ) ; compulsory
printf ( "%d %d", sizeof ( a ), a ) ;
for ( i = 0 ; i <= 3 ; i + + )
{
for ( j = 0 ; j <= 4 ; j + + )
printf ( "%d", a [ i ][ j ] ) ;
printf ( ”\n” ) ;
}
}
Problem
Given 2 matrices A & B of the order ( n x n )
Upper triangle = 0, Lower triangle = Non-zero
Generate C of n x ( n + 1 ) t o accommodate A and B
A C B C
a[ 4 ][ 4 ]
0, 0 0, 0 0, 0 0, 1
1 0 0 0 1, 0 1, 0 1, 0 0, 2
5 2 0 0 2, 0 2, 0 2, 0 0, 3
6 7 3 0 3, 0 3, 0 3, 0 0, 4
1 11 15 16 18
8 9 10 4 1, 1 1, 1 1, 1 1, 2
5 2 12 17 19
2, 1 2, 1 2, 1 1, 3
6 7 3 13 20
11 0 0 0 3, 1 3, 1 3, 1 1, 4
8 9 10 4 14
15 12 0 0 2, 2 2, 2 2, 2 2, 3
16 17 13 0 c[ 4 ][ 5 ] 3, 2 3, 2 3, 2 2, 4
18 19 20 14 3, 3 3, 3 3, 3 3, 4
b[ 4 ][ 4 ]
main( ) A C B C
{ 0, 0 0, 0 0, 0 0, 1
int a[ ][ 4 ] = {
1, 0, 0, 0, 1, 0 1, 0 1, 0 0, 2
5, 2, 0, 0, 2, 0 2, 0 2, 0 0, 3
6, 7, 3, 0, 3, 0 3, 0 3, 0 0, 4
8, 9, 10, 4 1, 1 1, 1 1, 1 1, 2
};
int b[ ][ 4 ] = { 2, 1 2, 1 2, 1 1, 3
11, 0, 0, 0, 3, 1 3, 1 3, 1 1, 4
15, 12, 0, 0, 2, 2 2, 2 2, 2 2, 3
16, 17, 13, 0, 3, 2 3, 2 3, 2 2, 4
18, 19, 20, 14
}; 3, 3 3, 3 3, 3 3, 4
int c[ 4 ][ 5 ], i, j ; for ( i = 0 ; i <= 3 ; i ++ )
for ( j = 0 ; j <= 3 ; j++ ) {
{ for ( j = i + 1 ; j <= 4 ; j++ )
for ( i = j ; i <= 3 ; i++ ) c[ i ][ j ] = b[ j - 1 ][ i ] ;
c[ i ][ j ] = a[ i ][ j ] ; }
} Cont…
for ( i = 0 ; i <= 3 ; i ++ )
{
for ( j = 0 ; j <= 4 ; j++ )
printf ( "%d ", c[ i ][ j ] ) ;
printf ( "\n" ) ;
}
}
Determine
Determine values of a[ i ][ j ] and b[ i ][ j ] from matrix c
Determine a[ i ][ j ] Determine b[ i ][ j ]
if ( j > i ) if ( j > i )
element = 0 ; element = 0 ;
else else
element = c[ i ] [ j ] ; element = c[ j ] [ i + 1 ] ;
a[ 3 ][ 2 ] = c[ 3 ][ 2 ] b[ 3 ][ 2 ] = c[ 2 ][ 4 ]
1 0 0 0 11 0 0 0 1 11 15 16 18
5 2 0 0 15 12 0 0 5 2 12 17 19
6 7 3 0 16 17 13 0 6 7 3 13 20
8 9 10 4 18 19 20 14 8 9 10 4 14
Problem
A magic square of 4 rows x 4 columns contains different
elements. Write a function to verify whether the sum of
each individual column elements, sum of each individual
row elements and sum of diagonal elements is equal or
not.
16 3 2 13
5 10 11 8
9 6 7 12 34
4 15 14 1
Objectives
Sparse matrices
How to pass arrays
Performing 3 tuple conversion
Sparse Matrices
r c Non-zero
15 0 0 22 0 -15 6 6 8
0 11 3 0 0 0 0 0 15
0 0 0 -6 0 0 0 3 22
0 0 0 0 0 0 0 5 -15
91 0 0 0 0 0 1 1 11
0 0 28 0 0 0 1 2 3
2 3 -6
4 0 91
3-Tuple Form 5 2 28
3-Tuple Conversion
# include <alloc.h> r c Non-zero
main( )
{ 3 4 6
int a[ 3 ][ 4 ] = { 0 0 4
4, 0, 0, 1,
2, 0, 0, 9, 0 3 1
6, 1, 0, 0 1 0 2
}; 1 3 9
int *ta ; 2 0 6
ta = create ( a, 3, 4 ) ; 2 1 1
display ( ta ) ;
free ( ta ) ;
}
Cont…
p
3 4 6 0 0 4 0 3 1 1 0 2 1 3 9 2 0 6 2 1 1
Objectives
Transpose
4 0 0 3 4 3 0 11 12
3 0 0 1 0 0 0 0 0
0 0 2 5 0 0 2 0 0
11 0 0 0 3 1 5 0 1
12 0 0 1
4 5 9
5 4 9
0 0 4 0 0 4
0 3 3 0 1 3 Hint
1 0 3 0 3 11
1 3 1 Search for 0, 1, 2, etc. in
2 2 2 0 4 12 first column of matrix A
2 3 5 2 2 2 and then set matrix B
3 0 11 3 0 3
4 0 12 3 1 1
4 3 1 3 2 5
3 4 1
Problem
Write a program to verify whether transpose of a give
sparse matrix is same as the original sparse matrix.
Objectives
Addition of SM
4 0 0 3 7 0 0 0 11 0 0 3
3 0 0 1 0 2 1 0 3 2 1 1
0 0 2 5 + 0 5 0 2 = 0 5 2 2
11 0 0 0 0 0 0 0 11 0 0 0
12 0 0 1 1 0 0 0 13 0 0 1
5 4 9 5 4 6 5 4 12
0 0 11
0 0 4 0 0 7 0 3 3
0 3 3 1 1 2 1 0 3
1 0 3 + 1 2 1 = 1 1 2
1 3 1 2 1 5 1 2 1
2 2 2 2 3 2 1 3 1
2 3 5 4 0 1 2 1 5
3 0 11 2 2 2
4 0 12 2 3 2
4 3 1 3 0 11
4 0 13
4 3 1
Addition of SM
# include <alloc.h> int *ta, *tb, *tc ;
main( ) ta = create ( a, 5, 4 ) ;
{ tb = create ( b, 5, 4 ) ;
int a[ 5 ][ 4 ] = { tc = add ( ta, tb ) ;
4, 0, 0, 3, display ( tc ) ;
3, 0, 0, 1, free ( ta ) ;
0, 0, 2, 5, free ( tb ) ;
11, 0, 0, 0, free ( tc ) ;
12, 0, 0, 1 }
};
5 4 9 5 4 6
int b[ 5 ][ 4 ] = {
0 0 4 0 0 7
7, 0, 0, 0, 0 3 3 1 1 2
0, 2, 1, 0, 1 0 3 + 1 2 1
0, 5, 0, 2, 1 3 1 2 1 5
0, 0, 0, 0 2 2 2 2 3 2
1, 0, 0, 0 2 3 5 4 0 1
}; .. .. ..
Cont…
p[ 0 ] = s1[ 0 ] ;
p[ 1 ] = s1[ 1 ] ;
p[ 2 ] = max ;
return p ;
} // end of add( )
5 4 9 5 4 6 5 4 12
0 0 4 0 0 7 0 0 11
0 3 3 1 1 2 0 3 3
1 0 3 + 1 2 1 = 1 0 3
1 3 1 2 1 5 1 1 2
2 2 2 2 3 2 1 2 1
1 3 1
2 3 5 4 0 1 2 1 5
3 0 11 2 2 2
4 0 12 2 3 2
4 3 1 .. ..Cont…
..
Objectives
Matrix multiplication
Multiplication of sparse matrices
Matrix Multiplication
4 0 0 1 1 0 4 3 ik k j i k kj
2 0 0 9 x 2 0 = 2 27 00 00 00 01
6 1 0 0 0 0 8 0 01 10 01 11
0 3 02 20 02 21
3x 4 4x 2 3x 2 03 30 03 31
10 00 10 01
for ( i = 0 ; i < 3 ; i + + )
{ 11 10 11 11
for ( j = 0 ; j < 2 ; j + + ) 12 20 12 21
{ 13 30 13 31
s=0;
for ( k = 0 ; k < 4 ; k + + ) 20 00 20 01
s = s + a[ i ][ k ] * b[ k ][ j ] ; 21 10 21 11
22 20 22 21
c[ i ][ j ] = s ;
23 30 23 31
}
}
int* mul ( int *x, int *y ) s_in_x ( int *p, int j, int colx )
{ {
/* Existing Code */ int i ;
for ( i = 0 ; i < x[ 0 ] ; i++ ) for ( i = 1 ; i <= p[ 2 ] ; i++ )
{ {
for ( j = 0 ; j < y[ 1 ] ; j++ ) if ( p[ i * 3 + 1 ] == j &&
{ p[ i * 3 ] == colx )
px = s_in_x ( x, i ) ; return i ;
if ( px != -1 ) } col X = row Y
{ return -1 ;
py = s_in_y ( y, j, }
3 4 6 4 2 3
x[ px * 3+1 ] ) ;
0 0 4 0 0 1
0 3 1 1 0 2
4 0 0 1 1 0 1 0 2 3 1 3
2 0 0 9 2 0 1 3 9
6 1 0 0 0 0 2 0 6
0 3 2 1 1
Objectives
int a[ 3 ][ 4 ] ; int a[ m ][ n ]
a23 aij
Col major
a00 a10 a20 a01 a11 a21 a02 a12 a22 a03 a13 a23
int a[ 3 ][ 4 ] ; int a[ m ][ n ]
a23 aij
u1
u3
Row major
a[ i ][ j ][ k ]
Column major
a[ i ][ j ][ k ]
4-D Array
a[ 1…u1, 1…u2, 1…u3, 1…u4 ]
Row major
a[ i ][ j ][ k ][ l ]
Column major
a[ i ][ j ][ k ][ l ]
n-Dimensional Array
a[ 1…u1, 1…u2, 1…u3, …, un ]
+ ( i1 – 1 ) * u2u3u4…un + ( i1 – 1 ) * u2u3u4…un
+ ( i2 – 1 ) * u3u4…un + ( i2 – 1 ) * u3u4…un
+ ( i3 – 1 ) * u4u5…un + ( i3 – 1 ) * u4u5…un
+ ( i4 – 1 ) * u5u6…un + ( i4 – 1 ) * u5u6…un
+ +
. .
. .
. .
+ ( i n - 1 – 1 ) * un + ( i n – 1 ) * un - 1
+(in–1) +( in-1–1 )
Col major
+(6–3)*((4–2+1)*(6–3+1)*(9–6+1))
+(2–2)*((6–3+1)*(9–6+1))
+(8–6)*(6–3+1)
+ 3– 3
Problem
Find the location of an element A[ 7 ][ 8 ][ 2 ][ 5 ] relative to
the first element of the array A[ 6:8 ][ 7:10 ][ 2:5 ][ 5:8 ]
Objectives
Limitations on arrays
Dynamic memory allocation
How the memory allocation is done
Arrays
No Flexibility
main( )
{
int m1, m2, m3, i, per [ 10 ] ;
for ( i = 0 ; i <= 9 ; i++ )
{
printf ( "Enter marks" ) ;
scanf ( "%d %d %d", &m1, &m2, &m3 ) ;
per [ i ] = ( m1 + m2 + m3 ) / 3 ;
printf ( "%d", per [ i ] ) ;
}
}
Memory Allocation
Array malloc( )
Static Dynamic
int x, y ;
main( ) Allocation
{
int j ; float k ; int *p ;
p = ( int * ) malloc ( 20 ) ; Compiler
}
Symbol table entries for variables
- Name, Scope, Length
- Offset from DS (global), SS (local)
Instructions for local variables
Call to malloc( )
Creates OBJ and discards Symbol Table
Our object code + Library’s object code Linker
Create EXE file format (Header to help loader)
If too many globals - Loader fails Loader
Fatal
If too many locals - Stack overflow
If too much dynamic demand - Heap full
Best... NULL
55 63 28 45 60
data link
NULL
Linked List
struct node
{
int data ;
struct node *link ;
};
Objectives
Linked List p q r s
main( )
{
struct node 55 63 28 45 N
{
int data ;
struct node *link ;
};
struct node *p , *q, *r, *s ;
malloc ( sizeof ( struct node ) ) ;
q = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
r= ...
s= ...
p data = 55 ;q data = 63 ;r data = 28 ;s data = 45
p link = q ; q link = r ; r link = s ; s link = NULL ;
Cont...
45 16 25 N 90 N
t t t
struct node
{
char name[ 20 ] ;
int age ;
struct node *link ;
};
t t
Objectives
How to perform various operations on linked
lists
How to append a new node to a link list
How to add a new node at the beginning of a
linked list
How to add a new node at a specified position
in a linked list
How to delete a node from a linked list
How to delete all nodes in the list
#include "alloc.h"
struct node
LL Operations
{ addatbeg ( &p, 7 ) ;
int data ; addatbeg ( &p, 58 ) ;
struct node *link ; display ( p ) ;
}; addafter ( p, 7, 0 ) ;
main( ) addafter ( p, 2, 1 ) ;
addafter ( p, 5, 99 ) ;
{
struct node *p ; display ( p ) ;
c = count ( p ) ;
p = NULL ; printf ( "\nNo. of eles. %d", c ) ;
int c ;
del ( &p, 30 ) ;
append ( &p, 14 ) ; del ( &p, 10 ) ;
append ( &p, 30 ) ;
append ( &p, 25 ) ; display ( p ) ;
append ( &p, 17 ) ; c = count ( p ) ;
printf ( "\nNo. of eles. %d”, c ) ;
display ( p ) ; deleteall ( &p ) ;
}
14 30 25 N 17 N
Add At Beginning
addatbeg ( struct node **q, int n )
{
struct node *t ;
t = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
t -> data = n ;
t -> link = *q ;
*q = t ;
}
t *q
7 14 30 25 N
New node
14 30 25 N
14 30 2 N
Objectives
2 4 5 6 N
Cont… New node
Cont… *q t
2 4 6 N
else
{ 5
while ( t != NULL )
{ r
if ( ( t -> data <= n && t -> link -> data > n ) ||
( t -> data <= n && t -> link == NULL ) )
{
r -> link = t -> link ; t -> link = r ;
return ;
}
t = t -> link ;
} *q t r
}
}
2 4 5 N 6 N
Sorting
#include "alloc.h" display ( p ) ;
struct node ssort ( p ) ;
{ display ( p ) ;
int data ; deleteall ( p ) ;
struct node *link ;
}
};
main( )
{
struct node *p = NULL ;
append ( &p, 17 ) ;
append ( &p, 6 ) ;
append ( &p, 13 ) ;
append ( &p, 12 ) ;
append ( &p, 2 ) ;
Objectives
Reversing LL
*q
7 43 3 23 5 N
7 N 43 3 23 5
7 43 3 23 5 N
*q
Merging LL
f
9 12 14 17 79 N
s
12 17 24 64 87 N
9 12 12 14 17
17 24 64 79 87 N
merge ( struct node *p, struct node *q, struct node **s )
{ Empty List
struct node *z ;
*s z
while ( p != NULL && q != NULL )
{
if ( *s == NULL )
z = *s = ( struct node * ) malloc (
sizeof ( struct node ) ) ;
else
{
z -> link = ( struct node *) malloc (
sizeof ( struct node ) ) ;
z = z -> link ;
} Non-Empty List
*s z
14
9 12 14 N 12 17 24 N
Objectives
Concatenation Of LL
#include "alloc.h" append ( &s, 7 ) ;
struct node append ( &s, 13 ) ;
{ append ( &s, 2 ) ;
int data ; append ( &s, 84 ) ;
struct node *link ;
printf ( "\nSecond LL: " ) ;
};
display ( second ) ;
main( )
{ concat ( &f, &s ) ;
struct node *f, *s ; printf ( "\nConcatenated LL: " ) ;
f = s = NULL ; display ( f ) ;
append ( &f, 1 ) ; deleteall ( f ) ;
append ( &f, 41 ) ; deleteall ( s ) ;
append ( &f, 3 ) ; }
append ( &f, 9 ) ;
printf ( "\nFirst LL: " ) ;
display ( f ) ;
9 12 14 N 12 17 24 N
padd ( struct pnode *p, struct pnode *q, struct pnode **s )
{
struct pnode *z ;
while ( p != NULL && q != NULL )
{
if ( *s == NULL )
z = *s = ( struct pnode * ) malloc (
sizeof ( struct pnode ) ) ;
else
{
z -> link = ( struct pnode * ) malloc (
sizeof ( struct pnode ) ) ;
z = z -> link ;
}
while ( p != NULL )
{
z -> link = ( struct pnode * ) malloc (
sizeof ( struct pnode ) ) ;
z = z -> link ;
*z = *p ;
p = p -> link ;
}
while ( q != NULL )
{
z -> link = ( struct pnode * ) malloc (
sizeof ( struct pnode ) ) ;
z = z -> link ;
*z = *q ;
q = q -> link ;
}
z -> link = NULL ;
} // padd
Objectives
What are circular linked lists
How to perform common operations on a circular
linked list
How to add a new node to a circular list
How to delete an existing node from a circular lis
How to count the nodes of the circular linked list
How to display the nodes in the circular linked lis
12 17 24 64 87 N
12 17 24 64 87
64 12 17 24
Cont…
64 12 17 24
*q t r
12 17 24 88
64 12 17 24
prev
q t t
64 12 17 24
Objectives
11 2 14 17 N
N 11 2 14 17 N
struct dnode
{
struct dnode *prev ;
int data ;
struct dnode *next ;
}
N 11 N 2 14 17 N
N 2 N 14 11 N
Cont…
Cont… else
{
t -> prev -> next = t -> next ;
if ( t -> next != NULL )
t -> next -> prev = t -> prev ;
} Node to be Deleted = 2
free ( t ) ;
return ; t *q
} // if
t = t -> next ; N 2 N 14 11 N
} // while
printf ( "\nNo. not found." ) ;
} // d_delete Node to be Deleted = 11
*q t
N 2 14 11 17 N
Objectives
SM Represented As LL
2 0 7 3 3 3
3-Tuple
0 0 2
11 0 0
0 2 7
0 0 0 1 0 11
3 3 0 N 1 2 N
0 0 0 2 0 2 7
N N
1 1 0 11
N N
N 2 N
Objectives
struct node
Program
{ 3 3 0
int row, col, val ;
struct node *d, *r ;
}; 0 0 0 2
struct rcnode
{
int no ; 1 1 0 11
struct rcnode *p1 ; N N
struct node *p2 ;
};
N 2 N
struct spmat
{
struct rcnode *fr ;
int rows, cols ;
struct rcnode *fc ;
};
Cont…
for ( i = 0 ; i < ptup[ 1 ] ; i++ )
{
if ( i == 0 )
t = p -> fc = ( struct rcnode * ) malloc (
sizeof ( struct rcnode ) ) ;
else
{
t -> p1 = ( struct rcnode * ) malloc ( sizeof (
struct rcnode ) ) ;
t = t -> p1 ;
}
t -> no = i ; t -> p2 = NULL ;
}
t -> p1 = NULL ;
} // initsmll
p t t t
3 3 N 0 N 1 N 2 N
Cont…
Cont…
3 3 0 N 1 2 N
t1 p
0 0 0 2 0 2 7
rh N N N
else
{
while ( t1 -> r != NULL )
t1 = t1 -> r ;
t1 -> r = p ;
}
p -> r = NULL ;
Cont…
Cont… Define ch
ch = q.fc ; ch
for ( i = 0 ; i < p -> col ; i++ ) 3 3 0
ch = ch -> p1 ; p
t1 = ch -> p2 ;
0 0 0 2
if ( t1 == NULL )
N
ch -> p2 = p ;
else
ch
{
while ( t1 -> d != NULL ) 3 3 0
t1 = t1 -> d ; t1
t1 -> d = p ; 0 0 0 2
} N
p -> d = NULL ;
} // addspll p
1 0 2 7
N N
t = q.fr ;
3 3 0
while ( t != NULL )
{ p
p = t -> p2 ; t
0 0 0 2
while ( p != NULL ) N N
{
printf ( "%d %d %d\n", p -> row, p -> col, p -> val ) ;
p = p -> r ;
}
t = t -> p1 ;
}
}
Objectives
Recursive count( )
#include "alloc.h" int count ( struct node *q )
struct node {
{ int i ;
int data ; if ( q != NULL )
struct node *link ; {
}; i = 1 + count ( q -> link ) ;
main( ) return ( i ) ;
{ }
struct node *p ; else
int c ; return ( 0 ) ;
p = NULL ; }
append ( &p, 41 ) ;
append ( &p, 2 ) ;
append ( &p, 23 ) ; q q q q
c = count ( p ) ;
printf ( ”Ele. = %d", c ) ;
41 2 23 N
}
append ( &f, 1 ) ;
append ( &f, 2 ) ;
append ( &f, 3 ) ;
41 2 N 41 2 N
Recursive copy( )
#include "alloc.h" append ( &f, 51 ) ;
struct node append ( &f, 29 ) ;
{ append ( &f, 7 ) ;
int data ; display ( f ) ;
struct node *link ; copy ( f, &s ) ;
};
display ( s ) ;
main( )
deleteall ( &f ) ;
{
deleteall ( &s ) ;
struct node *f, *s ;
}
f = s = NULL ;
append ( &f, 17 ) ;
append ( &f, 42 ) ;
append ( &f, 3 ) ;
append ( &f, 64 ) ;
12 17 24 N
12 N 17 N 24 N
*s *s *s *s
Recursive append( )
#include "alloc.h"
append ( &f, 51 ) ;
struct node append ( &f, 29 ) ;
{ append ( &f, 7 ) ;
int data ;
struct node *link ;
}; display ( p ) ;
deleteall ( &p ) ;
main( ) }
{
struct node *p ;
p = NULL ;
append ( &f, 17 ) ;
append ( &f, 42 ) ;
append ( &f, 3 ) ;
append ( &f, 64 ) ;
Empty List
*s
append ( struct node **s, int n )
{
if ( *s == NULL ) 24 N
{
*s = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
( *s ) -> data = n ;
( *s ) -> link = NULL ;
}
else
append ( &( ( *s ) -> link ), n ) ;
} Non-Empty List
*s *s
24 N 17 N
Objectives
Problem
x7 + 2 x6 + 4 x4 + 5 x2
1 7 2 6 4 4 5 2 N
Solution I
1 10 3 2 2 8 3 2 3 8 2 2
1 4 4 1 6 3 4 1 2 0 1 1 N
Each node
contains a term
z 0 2 1 N
var exp link pd exp link
y 0 3 2 N
struct glist
{ x 0 3 8 N
int flag ;
union varcoeffpd
x 0 1 10 2 8 N
{
char var ;
int coeff ; coeff exp link
struct glist *pd ;
} vcp ; y 0 4 2 1 N
int exp ;
struct glist *link ;
}; x 0 1 4 6 3 N
Exercise - I
2a4b6c3 + 5b3c3 + 9a2b2c - 3ab 2c + abc
( 2a4b6 + 5b3 ) c3 + ( ( 9a2 - 3a ) b2 + ( a ) b ) c
Objectives
p p p p=N
Copy
a 0 5 3 1 N
# define VAR 0
# define COF 1
# define PDL 2 p b 0 2 9 1 8 N
copy ( struct glist *p, struct glist **q )
{ *q *q *q *q *q
if ( p == NULL )
*q = NULL ; a 0 5 3 1 N
else
{ b 0 2 9 1 8 N
*q = ( struct glist * ) malloc ( sizeof ( struct glist ) ) ;
**q = *p ;
if ( ( *q ) -> flag == PDL )
copy ( p -> vcp.pd, &( ( *q ) -> vcp.pd ) ) ;
copy ( p -> link, &( ( *q ) -> link ) ) ;
}
}
Compare
# define DIFF 0
# define SAME 1
int compare ( struct glist *p, struct glist *q )
{
if ( ( p == NULL ) && ( q == NULL ) )
return SAME ;
p a 0 5 3 1 N
if ( p == NULL || q == NULL ) b 0 2 9 1 8
return DIFF ;
if ( p -> exp != q -> exp )
return DIFF ;
q a 0 5 3 1 N
case COF :
if ( p -> coeff != q -> coeff )
return DIFF ;
break ;
case PDL :
int i i = compare ( p -> vcp.pd, q -> vcp.pd ) ;
if ( i == DIFF )
return DIFF ;
} Contd...
a 0 5 3 1 N
b 0 2 9 1 8 N
a 0 5 2 1 N
b 0 2 9 1 8 N
Objectives
Stack and its utilities
Stack as an Array
Stack
Top 20
Top 10 10
Top
Empty Push 10 Push 20
Top 20
10 Top 10
Top
Pop Pop Empty
#define NULL 0
#define MAX 10
Program
struct stack c = count ( s ) ;
{ printf ( "Count = %d", c ) ;
int arr[ MAX ] ; displaystack ( s ) ;
int top ; }
};
main( )
{
struct stack s ; int i, c ;
s.top = -1 ;
push ( &s, 11 ) ; top 7
push ( &s, 23 ) ; top 16
push ( &s, - 8 ) ; top -8
push ( &s, 16 ) ;
top 23
push ( &s, 7 ) ;
top 11
i = pop ( &s ) ;
top = -1
printf ( "%d", i ) ;
Push Operation
push ( struct stack *p, int item ) top 21
{
4
if ( p -> top == MAX - 1 )
{ 9
printf ( "Stack is full." ) ; 13
return ; 2
} 7
p -> top + + ; 16
p -> arr[ p ->top ] = item ; -8
} 23
top 11
top = -1
Objectives
Stack as a Linked List
Push Pop
top 55 top 55
top 63 top 63
top 28 top 28
top 45 N top 45 N
Push Operation
int push ( struct node **s, int item )
{
struct node *q ;
q = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
q -> data = item ;
q -> link = *s ; Stack
*s = q ;
} *s top
q 28
*s top
45 N
q
Pop Operation
int pop ( struct node **s ) Pop
{
struct node *q ; int item ; *s
63 top
q
if ( *s == NULL )
{
printf ( "Stack is empty" ) ; 28
return NULL ;
}
45 N
q = *s ;
item = q -> data ; *s = N top = N
*s = q -> link ;
free ( q ) ;
return ( item ) ;
}
Objectives
Expressions of different forms
Expressions
A$ B * C- D + E /F/ ( G +H ) Infix
AB$C*D-EF/GH+/+ Postfix
+-*$ABCD//EF+GH Prefix
...Contd.
In To Post
A$ B *C - D +E /F/ ( G+ H)
Objectives
Infix to Postfix Conversion
Program
#include "string.h" printf ( "\nEnter Infix form: " ) ;
#include "ctype.h" gets ( infix ) ;
#define MAX 50 convert ( infix, &p ) ;
struct post printf ( "Postfix expr. " ) ;
{ printf ( "%s ", p.expr ) ;
char stack[ MAX ] ; }
int top ;
char expr[ MAX ] ; Infix
}; A$ B * C- D + E /F/ ( G +H )
main( )
{ Postfix
struct post p ; AB$C*D-EF/GH+/+
char infix[ MAX ] ;
p.top = -1 ;
Contd...
Objectives
Evaluation of Postfix form
Evaluate Postfix
Scan from L to R. Repeat step 1- 2
Token Operation
operand Add to stack
operator Pop stack into n1
Pop stack into n2
Perform n3 = n2 operator n1
Push n3
Pop stack to obtain result
Program
# include "math.h"
# include "ctype.h "
main( )
{
char expr[ MAX ] ;
int val ;
Objectives
Infix to Prefix Conversion
A$ B * C- D + E /F/ ( G +H )
Infix To Prefix
A+(B* C-(D/E$F)*G )*H
Objectives
Evaluation of Prefix form
Postfix to Prefix Conversion
Evaluate Prefix
Scan from R to L. Repeat step 1- 2
Token Operation
operand Add to stack
operator Pop stack into n1
Pop stack into n2
Perform n3 = n1 operator n2
Push n3
Pop stack to obtain result
...Contd.
AB$C*D-EF/GH+/+
Tok. Stack
H - * $ A B C D, / E F, G, H
+ - * $ A B C D, / E F, + G H
/ - * $ A B C D, / / E F + G H
+ +-*$ABCD//EF+GH
#include "string.h"
#define MAX 50
Program
struct postfix Postfix
{ AB$C*D-EF/GH+/+
char stack[ MAX ][ MAX ] ;
int top ; Prefix
}; +-*$ABCD//EF+GH
main( )
{
struct postfix q ;
char expr[ MAX ] ;
q.top = -1 ;
printf ( "Enter postfix expr: " ) ;
gets ( expr ) ;
convert ( expr, &q ) ;
printf ( "\nPrefix expr: " ) ;
printf ( "%s", &q.stack[ 0 ][ 0 ] ) ;
}
pop( )
char * pop ( struct postfix *p )
{
char *pstr ;
if ( p -> top == -1 )
{
printf ( "\nStack is empty." ) ;
return 0 ;
}
pstr = p -> stack[ p -> top ] ;
p -> top - - ;
return pstr ;
}
Objectives
Prefix to Postfix Conversion
Prefix to Infix Conversion
+-*$ABCD//EF+GH
Tok. Stack
$ E F / G H + /, D, C, A B $
* E F / G H + /, D, A B $ C *
- E F / G H + /, A B $ C * D -
+ AB$C*D-EF/GH+/+
Pre To Post
+A*-*BC*/D$EFGH
Tok. Stack
H H
G H, G
F H, G, F
E H, G, F, E
$ H, G, E F $
D H, G, E F $, D
/ H, G, D E F $ /
* H, D E F $ / G *
C H, D E F $ / G * , C
B H, D E F $ / G * , C, B
* H, D E F $ / G * , B C *
- H, B C * D E F $ / G * -
* BC*DEF$ /G*-H*
A BC*DEF$ /G*-H*,A
+ ABC*DEF$/G*-H*+
...Contd.
+-*$ABCD//EF+GH
Tok. Stack
$ E / F / G + H, D, C, A $ B
* E / F / G + H, D, A $ B * C
- E / F / G + H, A $ B * C - D
+ A$B*C-D+E/F/G+H
Pre To In
+A*-*BC*/D$EFGH
Tok. Stack
H H
G H, G
F H, G, F
E H, G, F, E
$ H, G, E $ F
D H, G, E $ F, D
/ H, G, D / E $ F
* H, D / E $ F * G
C H, D / E $ F * G, C
B H, D / E $ F * G, C, B
* H, D / E $ F * G, B * C
- H, B * C - D / E $ F * G
* B*C-D/E$ F* G* H
A B * C - D / E $ F * G * H, A
+ A+ B*C-D/E$ F* G* H
Summary
Conversion Scan Order Token Operation
In - Post L-R operand Add to expression
operator Priority
In - Pre R-L ( Add to stack / delete
) Add to stack / delete
Post - Pre L-R operand Push to stack
Post - In L-R operator Pop stack into s1
Pop stack into s2
Pre - Post R-L
Construct expression
Pre - In R-L Push expression on stack
In Post s1 B Pre s1 A
A$B AB$ s2 A $AB s2 B
Objectives
Queue
Queue
WM_PAINT WM_KEYDOWN
WM_CREATE WM_MOUSEMOVE
Message queue
Retrieved Messages
Dispatched Messages
Call Appropriate Handler
16 - 8 23 11 7
16 - 8 23 11 7 13
16 - 8 23 11 7 13
Program
#define MAX 10
#define NULL 0 i = delq ( &q ) ;
printf ( "\nItem = %d", i ) ;
struct queue
{ }
int arr[ MAX ] ;
int f, r ;
};
main( )
{
struct queue q ; int i ;
q.f = q.r = -1 ;
addq ( &q, 23 ) ;
addq ( &q, 9 ) ;
addq ( &q, 11 ) ;
addq ( &q, -10 ) ;
addq( )
addq ( struct queue *p, int item )
{
if ( p -> r == MAX - 1 )
{
printf ( "\nQueue is full." ) ;
return ;
}
p -> r++ ;
p -> arr[ p -> r ] = item ;
if ( p -> f == -1 ) f = -1
p -> f = 0 ; r = -1
} 23
f r
23 9 11 -10 7 13 2 - 8 22 3
23 9 11 -10 7
f r f = -1
r = -1
23 9 11 -10 7
Objectives
Limitations of Queue
Circular Queues
Problems
f r
5 -1 34 12 53 61 9 23 - 8 42
-1 34 12 53 61 9 23 - 8 42 42
7 -1 34 12 53 61 9 23 - 8 42
addq( )
addq ( struct queue *p, int item )
{
if ( ( p -> r == MAX - 1 && p -> f == 0 ) ||
( p -> r + 1 == p -> f ) )
{
printf ( "\nQueue is full." ) ; f = -1
return ; r = -1
} -8
if ( p -> r == MAX - 1 )
p -> r = 0 ; f r
else
p -> r + + ; 9 42 34 12 53
p -> arr[ p -> r ] = item ;
f r
if ( p -> f == -1 )
p -> f = 0 ;
- 8 42 34 12 53
}
Objectives
Deque
Priority Queue
Deque
f r
5 -1 34 12 53 61 9 23 - 8 42
f r
5 -1 34 12 53 61 9 23 - 8 42
f r
5 -1 34 12 53 61 9 23 - 8 42
Priority Queue
Job Priority f r f r f r
2 2
9 4 2 9 2 9 2 8
8 2
3 3
f r
6 1
1 4
7 5 9 3 2 8
4 4
0 4 f r
5 2
9 3 2 8 6
Objectives
Trees
Trees terminology
Types of Trees
Data structures
Linear Non - Linear
Arrays Trees
Linked Lists Graphs
Stacks
Queues
A T1 T2 T3
A @ 99
ST1 ST2
K Z
D $ & 45 55
D S G C ^ 35
G M N H
C
H O P ~ 0 25
H #
15
A A - Root node
B, D, F - Nodes of left sub-tree
B C C, E, G, H - Nodes of right sub-tree
F, G, H - Leaf nodes
D E
A is Father of B & C
F G H E is right child C
G is right descendant of A
F is left child of D
F is left descendant of A
A is ancestor of D
G & H are siblings, D and E aren’t
B C
D E
F G H
B C B C
D E D E F G
F G
Objectives
Traversal of Trees
Reconstruction of Trees
Traversal
A
B C
D E
F G
2 7
8 9
4 1 6
Reconstruction - I In - 4, 7, 2, 8, 5, 1, 6, 9, 3
Pre - 1, 2, 4, 7, 5, 8, 3, 6, 9
1
1
1 In: 4, 7 In: 8, 5 1
Pre: 4, 7 Pre: 5, 8
2 3 2 3
4 5 In: 6, 9 4 5 6
Pre: 6, 9
7 8 7 8 9
Exercise In - 2, 4, 8, 3, 7, 1, 9, 6
Pre - 3, 2, 8, 4, 7, 9, 1, 6
3
3
In: 4, 8
3 Pre: 8, 4 3
2 7 2 7
8 In: 1, 9, 6 8 9
Pre: 9, 1, 6
4 4 1 6
Exercise In - D, B, F, E, G, A, C
Post - D, F, G, E, B, C, A
A
In: D, B, F, E, G In: C
Post: D, F, G, E, B Post: C B C
A
D In: F, E, G
Post: F, G, E
B C
D E
F G
5 1 9
9
17
3
3 17
2
Pre: 2, 4 5 1
5 1 Post: 4, 2
4
8 2 2 Pre: 13, 10
Post: 10, 13
2 13 19 25
10
Objectives
How to represent trees using arrays and linked
lists
What are binary search trees
Array Representation
3 - Arrays
0 1 2 3 4 5 6 7 8 9
A arr A B C D E F G '\0' '\0' H
lc 1 3 5 -1 9 -1 -1 -1 -1 -1
B C
rc 2 4 6 -1 -1 -1 -1 -1 -1 -1
E 1 - Array 2 ( depth + 2 ) - 1
D F G
0 1 2 3 4 5 6 7 8 9
arr A B C D E F G '\0' '\0' H
H 10 11 12 13 14 15 16 17 18 19
'\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0'
lc = 2 pos + 1
20 21 22 23 24 25 26 27 28 29 30
rc = 2 pos + 2
'\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0'
3 rc 2 4 6 - 1 - 1 - 1 12 - 1 - 1 - 1 - 1 - 1 - 1
2 7
8 9
4 1 6
Linked Representation
A
B C
N D N E N N F N N G N
N H N struct btnode
{
struct btnode *lc ;
char data ;
struct btnode *rc ;
};
L Y
A U W Z
D X
Objectives
A program to traverse the trees
Traversal of trees using inorder, preorder and
postorder
Non recursive traversal of trees
Comparing two trees
Program
# include "alloc.h" printf ( "\nIn-order: " ) ;
struct btnode inorder ( bt ) ;
{
struct btnode *lc ; printf ( "\nPre-order: " ) ;
int data ; preorder ( bt ) ;
struct btnode *rc ;
}; printf ( "\nPost-order: " ) ;
main( ) postorder ( bt ) ;
{
struct btnode *bt ; deleteall ( bt ) ;
}
bt = NULL ;
insert ( &bt, 10 ) ;
insert ( &bt, 6);
insert ( &bt, 8);
insert ( &bt, 15 ) ;
insert ( &bt, 20 ) ;
Objectives
How to perform insertion on binary search trees
How to delete an existing node from a binary
search tree
How to search a node in a binary search tree
if ( *pt == NULL )
7 14
{
printf ( "\nTree is empty" ) ;
return ;
5 10 12
}
p = c = NULL ;
found = search ( pt, num, &p, &c ) ; 9
# define TRUE 1
if ( found == FALSE ) # define FALSE 0
{
printf ( "\nData not found" ) ;
return ;
} Contd...
Contd...
...Contd 11
if ( c -> lc == NULL && c -> rc == NULL )
t = NULL ; 10 12
struct btnode *t
if ( c -> lc == NULL && c -> rc != NULL ) 9
t = c -> rc ; p
if ( c -> lc != NULL && c -> rc == NULL )
t = c -> lc ; cp 11
p
if ( p -> lc == c )
p -> lc = t ;
11 c c 7 t 14
else
p -> rc = t ;
10
free ( c ) ; t 14 5 10 12
}
9
12 9
Objectives
What are threaded binary trees
How threaded binary trees are represented
How to insert a node in a threaded binary tree
How to traverse the threaded binary tree in
inorder
Links
Threads
11
struct thtree
{
8 16 struct thtree *lc ;
int haslt ;
int data ;
3 10 int hasrt ;
struct thtree *rc ;
};
6
Left - Predecessor or head
Right - Successor or head
F 11 F
F 8 F T 16 T
T 3 F T 10 T
T 6 T
17
Exercise
In: 10, 13, 17, 20, 27, 38, 81
10 38 Post: 13, 10, 20, 27, 81, 38, 17
13 27 81
20
Program
# include "alloc.h" insert ( &th_head, 8 ) ;
# define TRUE 1 insert ( &th_head, 16 ) ;
# define FALSE 0 insert ( &th_head, 3 ) ;
struct thtree insert ( &th_head, 6 ) ;
{ insert ( &th_head, 10 ) ;
struct thtree *lc ; printf ( "Threaded BT:\n" ) ;
int haslt ; inorder ( th_head ) ;
int data ; }
int hasrt ;
struct thtree *rc ;
};
main( )
{
struct thtree *th_head ;
th_head = NULL ;
insert ( &th_head, 11 ) ;
...Contd
p = ( *h ) -> lc ; Non-Empty
struct thtree *p *h
while ( p != *h )
{ - 9999
if ( p -> data > num ) p
{
if ( p -> haslt == FALSE )
p = p -> lc ; z 11
else
{ 8
z -> lc = p -> lc ;
z -> rc = p ;
p -> lc = z ;
p -> haslt = FALSE ;
return ;
}
} Contd...
...Contd
if ( p -> data < num ) Non-Empty
{ *h
if ( p -> hasrt == FALSE )
p = p -> rc ; - 9999
else p
{
z -> rc = p -> rc ;
z -> lc = p ; p 11
p -> rc = z ;
p -> hasrt = FALSE ; 8
return ; z
}
}
} 10
}
Objectives
What is a heap
How to construct a heap
How to perform the heap sort
Heap
90 3
57 25 15 12
13 11 9 17 25 37 19
1 2 3 44 52
2 9 13 9
13 57 25 17 2 57 25 17
1 90 3 1 90 3
13 57
11 9 13 25
2 57 25 17 2 11 9 17
1 90 3 1 90 3 Contd...
...Contd 57 57
13 25 13 25
2 11 9 17 90 11 9 17
1 90 3 1 2 3
57 90
90 25 57 25
13 11 9 17 13 11 9 17
1 2 3 1 2 3
Exercise 13
4
4 27
13 27
5 8
21 14 8
21 14 5 8
33
33 27
27
4 13 21 13
21 14 5 8 4 14 5 8
33 33 Contd...
21 13 21 13
4 14 5 8 33 14 5 8
33 4
27 33
33 13 27 13
21 14 5 8 21 14 5 8
4 4
13 25
57 25
3 11 9 17
13 11 9 17
1 2 3 1 2
57 13 25 3 11 9 17 1 2 90
90 57 25 13 11 9 17 1 2 3 Contd...
...Contd
57 25
13 25 13 17
3 11 9 17 3 11 9 2
1 2 1
25 13 17 3 11 9 2 1 57 90
57 13 25 3 11 9 17 1 2 90
Contd...
25 17
13 17 13 9
3 11 9 2 3 11 1 2
17 13 9 3 11 1 2 25 57 90
25 13 17 3 11 9 2 1 57 90
Contd...
...Contd
17 13
13 9 11 9
3 11 1 2 3 2 1
13 11 9 3 2 1 17 25 57 90
17 13 9 3 11 1 2 25 57 90
Contd...
...Contd
13 11
11 9 3 9
3 2 1 1 2
11 3 9 1 2 13 17 25 57 90
13 11 9 3 2 1 17 25 57 90
Contd...
3 9 3 2
1 2 1
9 3 2 1 11 13 17 25 57 90
11 3 9 1 2 13 17 25 57 90
3
3 1 2 9 11 13 17 25 57 90
1 2
Contd...
...Contd
3
3 1 2 9 11 13 17 25 57 90
1 2
2
2 1 3 9 11 13 17 25 57 90
1
1 1 2 3 9 11 13 17 25 57 90
1 2 3 9 11 13 17 25 57 90
Objectives
What are AVL trees
What are 2 - 3 trees
What are B - trees
Create Heap
# define MAX 10
main( )
{
int arr[ ] = { 11, 2, 9, 13, 57, 25, 17, 1, 90, 3 } ;
int i ;
createheap ( arr, MAX ) ;
printf ( "Heap: " ) ;
for ( i = 0 ; i < MAX ; i + + )
printf ( "%d\t", arr[ i ] ) ;
}
for ( i = 1 ; i < n ; i + + )
{ 13 57 25 17
val = a[ i ] ;
c=i;
p=(c-1)/2; 1 90 3
AVL Tree
1
Adelson - Velskii - Landis
13
Balanced Binary Tree
-1 1
5 19 Bal. Factor = ht. of left ST -
-1 ht. of right ST
0 0
3 9 16 struct AVL
{
0 struct AVL *lc ;
11 int balfact ;
int data ;
struct AVL *rc ;
};
2 - 3 Tree
20
6 15 24
3 5 9 17 22 27 33
Max. 2 values, min. 1 value
Non - leaf node must have max. 3 children, min. 2
children
All the leaf nodes should appear on the same level
Value(s) at left child < value(s) at middle child
Value(s) at middle child < value(s) at right child
If i and j are 2 values on same node then i < j
- all values on left child < i
- all values on middle child > i & < j
- all values on right child > j
Order - 4
2 13 22 32 37 40 47 51
Objectives
What are graphs
The terminology associated with graphs
Representation of graphs
Adjacency list
Graphs 1
1
2 3 2
4
3
2
2 3
2 3
3 Strongly connected
components
Graph Representation
1 1
2 3
2
Store only
4 upper triangular
1 2 3 4 3
1 0 1 0 1 1 2 3
2 1 0 1 1 1 0 1 0
3 0 1 0 0 2 1 0 1
4 1 1 0 0 3 0 0 1
Adjacency Matrices
Adjacency List
V1 2 4 N
1
V2 1 3 4 N
2 3
V3 2 N
4
V4 1 2 N
1
V1 2 N
2
V2 1 3 N
V3 3 N
3
V1 2 3 N 2 3
V2 1 4 5 N 4 5 6 7
V3 1 6 7 N
8
V4 2 8 N 1 2 3 4 5 6 7 8
1 0 1 1 0 0 0 0 0
V5 2 8 N 2 1 0 0 1 1 0 0 0
3 1 0 0 0 0 1 1 0
V6 3 8 N 4 0 1 0 0 0 0 0 1
5 0 1 0 0 0 0 0 1
V7 3 8 N 6 0 0 1 0 0 0 0 1
7 0 0 1 0 0 0 0 1
V8 4 5 6 7 N 8 0 0 0 1 1 1 1 0
Objectives
Adjacency matrices
Adjacency multilist
Orthogonal representation of graphs
Adjacency Multilist
1 V1 2 3 4 N
V2 1 3 4 N
2 3
V3 1 2 4 N
4 V4 1 2 3 N
1 V1 E1 1 2 E2 E4
E1 E2
E3 V2 E2 1 3 E3 E4 V1: E1-> E2 -> E3
2
E4
3 E3 1 4 N E V2: E1-> E4 -> E5
5
V3
E5 E6 E4 2 3 E5 E6 V3: E2-> E4 -> E6
4 V4
E5 2 4 N E V4: E3-> E5 -> E6
6
E6 3 4 N N
2 V3 2 N
E4 E3
Next link Next link
3 m tail head for tail for head
V1h E1 1 2 N E4 V1h : E2
V1t V1t : E1
V2h E2 2 1 E3 N V2h : E1 -> E4
V2t
E3 2 3 E2 N V2t : E2 -> E3
V3h V3h : E3
V3t E4 3 2 N E1 V : E4
3t
Exercise
1 6
2 5
3 4
Orthogonal Representation
1 2 3
1
1 0 1 0
2 1 0 1
2
3 0 0 0
col. link row link
3 tail head for head for head
head node 1 2 3
1 1 2 N N
2 2 1 N 2 3 N N
3 N
2 4
Exercise
Give all graphical representations for the following graph
1 1 2 3 4 5
2
1 0 1 1 0 0
3 5 2 1 0 0 0 0
4 3 0 1 0 0 0
4 0 0 1 0 1
5 0 1 0 1 0
Adjacency Matrices
Adjacency List
1 2 V1 2 3 N
V2 1 N
3 5
4 V3 2 N
V4 3 5 N
V5 2 4 N
1 E1
E3
Adjacency Multilist
E2 2
E4
E8
3 E7 5
V1h E1 1 2 E2 E4
E5 4 V1t
E6 E2 1 3 E1 E5
V2h
V1h: E3 V2t E3 2 1 N N
V1t : E1 -> E2 V3h
E4 3 2 N E7
V2h: E1 -> E4 -> E7 V3t
V2t : E3 V4h E5 4 3 E6 E2
V3h: E2 -> E5 V4t
E6 4 5 E5 N
V3t : E4 V5h
V4h: E8 V5t E7 5 2 E8 E1
V4t : E5 - > E6
V5h: E6 E8 5 4 E7 N
V5t : E7 -> E8
Orthogonal
1 1 2 3 4 5
2 1 0 1 1 0 0
2 1 0 0 0 0
3 5 3 0 1 0 0 0
4 0 0 1 0 1
4 5 0 1 0 1 0
head node 1 2 3 4 5
1 1 2 1 3 N
2 2 1 N
3 3 2 N
4 4 3 N 4 5N N
5 5 2 N 5 4 N N
Objectives
Study Depth First Search
V3 1 6 7 N 1
V4 2 8 N 2 3
V5 2 8 N
4 5 6 7
V6 3 8 N
V7 3 8 N 8
V8 4 5 6 7 N
V2 1 3 8 N
1 2 3
V3 2 4 8 N
V4 3 7 N 7 8
V5 6 7 N
6 5 4
V6 5 N
V7 4 5 8 N
V8 1 2 3 7 N
Program
# include "alloc.h" v1 = add ( 1 ) ;
#define MAX 8 v2 = add ( 4 ) ;
struct node v3 = add ( 5 ) ;
{ arr[ 1 ] = v1 ;
int data ; v1 -> next = v2 ;
v2 -> next = v3 ;
struct node *next ;
}; v1 = add ( 1 ) ;
struct node * add ( int ) ; v2 = add ( 6 ) ;
main( ) v3 = add ( 7 ) ;
{ arr[ 2 ] = v1 ;
struct node *arr[ MAX ] ; v1 -> next = v2 ;
struct node *v1, *v2, *v3 ; v2 -> next = v3 ;
v1 = add ( 2 ) ; v1 = add ( 2 ) ;
v2 = add ( 3 ) ; v2 = add ( 8 ) ;
arr[ 0 ] = v1 ; arr[ 3 ] = v1 ;
v1 -> next = v2 ; v1 -> next = v2 ; Contd...
...Contd
v1 = add ( 2 ) ; arr[ 7 ] = v1 ;
v2 = add ( 8 ) ; v1 -> next = v2 ;
arr[ 4 ] = v1 ; v2 -> next = v3 ;
v1 -> next = v2 ; v3 -> next = v4 ;
v1 = add ( 3 ) ; struct node *v4
v2 = add ( 8 ) ;
arr[ 5 ] = v1 ; dfs ( 1, arr, visited ) ;
v1 -> next = v2 ; int visited[ MAX ] = { 0 } ;
v1 = add ( 3 ) ;
v2 = add ( 8 ) ; for ( i = 0 ; i < MAX ; i++ )
arr[ 6 ] = v1 ; del ( arr[ i ] ) ; int i
v1 -> next = v2 ; }
v1 = add ( 4 ) ;
v2 = add ( 5 ) ;
v3 = add ( 6 ) ;
v4 = add ( 7 ) ;
5 N
del( )
del ( struct node *n )
{
struct node *t ;
while ( n != NULL )
n n t t = NULL
{
t = n -> next ; 2 3 N
arr[ 0 ]
free ( n ) ;
n=t;
} arr[ 1 ] 1 4 5 N
}
arr[ 2 ] 1 6 7 N
arr[ 3 ] 2 8 N
arr[ 4 ] 2 8 N
Objectives
Study Breadth First Search
V1 2 3 N 2 3
V2 1 4 5 N 4 5 6 7
V3 1 6 7 N
8
V4 2 8 N 1 2 3 4 5 6 7 8
7 8
V3 2 4 8 N
V4 3 7 N
6 5 4
V5 6 7 N
V6 5 N 1 2 8 3 7 4 5 6
V7 4 5 8 N
V8 1 2 3 7 N
8 2 8 N
arr[ 3 ]
pq -> r + + ;
pq -> q[ pq -> r ] = vertex ;
if ( pq -> f == - 1 )
pq -> f = 0 ;
}
Objectives
Spanning Tree
Cost of Spanning Tree
Kruskal’s Algorithm to determine minimum cost
Spanning Tree
Spanning Tree
Undirected tree
Contains edges necessary to connect all vertices
1 1 1
2 5 3 2 5 3 2 5 3
4 4 4
Spanning Trees
1 1
2 5 3 2 5 3
4 4
DFS ST BFS ST
2 3
3
4 4 4
1 4 1 4 1 4 1 4
1 2 7 1 7
2 3 2 3 2 3 2 3
3 3 3 3
4
1 4
Minimum Cost
2 1 Spanning Tree
2 3
1 4
4
1 4 4-2 2 2 1 Included
2 1 2 3
2 3 4
3-2 3 1 4 Rejected
Minimum Cost 2 1
Spanning Tree 4-1 4 2 3 Included
3
40 D 35 E
14
A
E B C
Contd...
E
E
A A
10
B 9 B C
C
20 D D
14
Min. E
Cost ST E
Objectives
Dijkstra’s Algorithm to determine minimum cost
Spanning Tree
AOV Network
Topological Order
AOE Network
Dijkstra’s Algorithm
7
4
1 4
2
5 7 1
2 3
3
1 2 3 4 1 2 3 4
1 7 5 1 11 12 - -
2 7 2 2 21 - - 24
3 3 3 - 32 - -
4 4 1 4 41 - 43 -
Contd...
2 3
3
1 2 3 4 1 2 3 4
1 7 5 1 11 12 - -
2 7 2 2 21 - - 24
3 3 3 - 32 - -
4 4 1 4 41 - 43 -
1 2 3 4 1 2 3 4
1 7 5 1 11 12 - -
2 7 12 2 2 21 212 - 24
3 3 3 - 32 - -
4 4 9 1 4 41 412 43 -
...Contd 7
4
1 4
2
5 7 1
2 3
1 2 3 4 3
1 2 3 4
1 7 5 1 11 12 - -
2 7 12 2 2 21 212 - 24
3 3 3 - 32 - -
4 4 9 1 4 41 412 43 -
1 2 3 4 1 2 3 4
1 7 5 7 1 11 12 - 124
2 7 12 2 2 21 212 - 24
3 10 3 5 3 321 32 - 324
4 4 9 1 11 4 41 412 43 4124
...Contd 7
4
1 4
2
5 7 1
2 3
3
1 2 3 4 1 2 3 4
1 7 5 7 1 11 12 - 124
2 7 12 2 2 21 212 - 24
3 10 3 5 3 321 32 - 324
4 4 9 1 11 4 41 412 43 4124
1 2 3 4 1 2 3 4
1 7 5 7 1 11 12 - 124
2 7 12 2 2 21 212 - 24
3 10 3 5 3 321 32 - 324
4 4 4 1 6 4 41 432 43 4324
2 3
3
1 2 3 4 1 2 3 4
1 7 5 7 1 11 12 - 124
2 7 12 2 2 21 212 - 24
3 10 3 5 3 321 32 - 324
4 4 4 1 6 4 41 432 43 4324
1 2 3 4 1 2 3 4
1 7 5 8 7 1 11 12 1243 124
2 6 6 3 2 2 241 2432 243 24
3 9 3 6 5 3 3241 32 3243 324
4 4 4 1 6 4 41 432 43 4324
20 Exercise
8
E F
15 6 E F G E F G
G
7 E 8 15 E - EF EG
F 20 6 F FE - FG
G 7 G GE - -
E F G E F G
E 8 15 E - EF EG
F 20 28 6 F FE FEF FG
G 7 15 22 G GE GEF GEG
Contd...
...Contd
20
8
E F
15 6
G
7
E F G E F G
E 8 15 E - EF EG
F 20 28 6 F FE FEF FG
G 7 15 22 G GE GEF GEG
E F G E F G
E 28 8 14 E EFE EF EFG
F 20 28 6 F FE FEF FG
G 7 15 21 G GE GEF GEFG
Contd...
E F G E F G
E 21 8 14 E EFGE EF EFG
F 13 21 6 F FGE FGEF FG
G 7 15 21 G GE GEF GEFG
Sub. No.
S1
Prereq.
None
Sub. No.
S8
Prereq.
S4
AOV N/W
S2 S1, S14 S9 S3 Activity On Vertex Network
S3 S1, S14 S10 S3, S4
S11 S10 V - Activity / Task
S4 S1, S13 E - Precedence Relation
S12 S11
S5 S15 S13 None
S6 S3 S14 S13
S7 S3, S4, S10 S15 S14
S13 S14 S15 S5
S4 S8 S2 S9
S1 S3 S6
Precedence
Activity
S7
S10
S11 S12
Exercise - I
Vertex Prerequisite V2
V1
V2
V3
V1 V3 V5
V4
V5
V6
V6
V4
Not Feasible
Objectives
Study Memory Management
Study First Fit Algorithm
Study Best Fit Algorithm
Memory Management
P1 P2 P3 P4 P5 Free
0 10000 5000
20000 30000 0
av q p
0 10000 0
20000 30000 0
firstfit ( *av, n, *p )
{ 0 10000 5000
20000 30000
p = q = link ( av ) ;
while ( p != 0 )
{ n = 15000
if ( size ( p ) >= n )
{ q=p;
setsize ( p, size ( p ) - n ) ; p = link ( p ) ;
if ( size ( p ) == 0 ) }
setlink ( q, link ( p ) ) ; }
else
p = p + size ( p ) ;
break ;
}
Objectives
Study Next Fit Algorithm
Best Fit
Disadv. of firstfit Algo.
Tendency to create too many small nodes
Search always starts at beginning of available list
av q p
0 50000 0
10000 30000 0
Next Fit
llink tag = 0 size rlink tag = 1 size
0 0 0
nextfit ( *av, n, p )
q = llink ( p ) ; r = rlink ( p ) ;
{
p = rlink ( av ) ; alpha = 50 ; setlink ( rlink ( q ), r ) ;
while ( 1 ) setlink ( link ( r ), q) ;
{ settag ( p, 1 ) ;
if ( size ( p ) >= n ) settag ( p + size ( p ) - 1, 1) ;
{ av = llink ( p ) ;
diff = ( size ( p ) - n ) ; }
if ( diff < alpha )
{
Contd...
Objectives
Study Garbage Collection and Compaction
Garbage Collection
Method of detecting & reclaiming free nodes
Done in two phases:
Marking Phase - marks all nodes that are
accessible from an external
pointer ( nodes in use )
Collection Phase - free all nodes that
are not marked
f 1 t t N
Atomic Node
f 4 f 3
marker( )
{
for each pointer x such that x != NULL
{
x -> mark = t ;
if ( x -> tag == t )
mark1 ( x ) ;
}
}
mark1 ( *x )
{
Algorithm Of Mark
top = 0 ; xp qp q
push ( stack, top, x ) ;
while ( top > 0 )
t t t t t
{
pop ( stack, top, p ) ;
while ( 1 ) t f 1 t t t
{
q = p -> link ;
p t f 4 q
if ( q != NULL )
{ if ( p -> mark == t
if ( q -> tag == t && || p -> tag == f )
q -> mark == f )
break ;
push ( stack, top, q ) ;
q -> mark = t ; p -> mark = t ;
} }
p = p -> dlink ; p -> mark = t ;
if ( p == NULL ) }
break ; }
GC & Compaction
size m link1 300 200
a 300 1500 200
nadd kr link2 T
1900
F
1000 1300
400 200 100
400 1000 200 1500 100
T 1900 T 1500 F
1500 1900 2100
Step - II
300 400 500
300 300
700 400 500 T 0
0 T 1200
800 F 300 800
1200
0 300 700
600 700
600 T 300
700 700
800 300
700 F
1200 1800
i=0;
while ( i < m )
{
if ( mark ( i ) )
{
link1 ( i ) = newadd ( link1 ( i ) ) ;
link2 ( i ) = newadd ( link2 ( i ) ) ;
}
i = i + size ( i ) ;
}
Step - III
300 500 600
300 T 300 500 0 600 300
0 800 300 T 800 800 T 300
0 300 800
400 700
400 700
F F
1200 1600
i=0; p=0;
while ( i < m )
{
if ( mark ( i ) )
{
for ( j = i ; j < i + size ( i ) - 1 ; j + +, p + + )
mem[ p ] = mem [ j ] ;
}
i = i + size ( i ) ;
}
}
Objectives
Study File Organization
Files
Contains records
Records contain one or more fields
Each field represents an item of information
Purpose of file organization - Easy Retrieval and Updation
Retrieval using Primary or Secondary keys
Mode of Retrieval - Real time and Batched
Mode of Update - Real time or Batched
Linked Organization:
Add. of next record is kept in current record
Easy insertion and deletion
Inefficient - As search is sequential
To improve efficiency - Range based index
struct emp
{
Ind. Seq. Access
int id ;
char name[ 20 ] ;
int age ;
float sal ;
};