FE36-Data Structure and Algorithms IEEE
EE 36 DATA STRUCTURES AND ALGORITHMS
(Common to EEE, EIE 8 ICE)
‘Aim: To master the desion and applications of linear, tree, and graph structures. To understand various
algorithm design and analysis techniques.
UNIT I :LINEAR STRUCTURES
[Abstract Data Types (ADT) ~ List ADT ~ array-based implementation ~ linked list Implementation ~ cursor~
based linked lists ~ doubly-linked lists ~ applications of lists Stack ADT - Queue ADT ~ circular queue
Implementation Applications of stacks and queues
UNIT II: TREE STRUCTURES
Need for non-linear structures ~ Tree ADT - tree traversals - left child right sibling data structures for
‘general trees ~ Binary Tree ADT ~ expression trees ~ applications of trees binary search tree ADT
UNIT IIT :BALANCED SEARCH TREES AND INDEXING
[AVL trees ~ Binary Heaps ~ B-Tree ~ Hashing ~ Separate chaining ~ open addressing ~Linear probing
UNIT IV: GRAPHS
Definitions ~ Topological sort ~ breadth-first traversal - shortest-path algorithms ~ minimum spanning tree ~
Prim’s and Kruska's algorithms ~ Depth-first traversal ~ bleannectivity ~ euler circuits ~ applications of
graphs
UNIT V :ALGORITHM DESIGN AND ANALYSIS
Greedy algorithms ~ Divide and conquer ~ Dynamic programming ~ backtracking ~ branch and bound ~
Randomized algorithms ~ algorithm analysis ~ asymptotic notations ~ recurrences ~ NP-complete problems
TOTAL: 45 PERIODS
TEXT BOOKS
A.M. A. Weiss, "Data Structures and Algorithm Analysis in C*, Pearson Education Asia, 2002.
2. ISRD Group, "Data Structures using C’, Tata McGraw-Hill Publishing Company Ltd., 2006.
REFERENCES
1.A.V. Aho, 3. E. Hoperoft, and 3. D. ullman, “Data Structures and Algorithms”, Pearson Education, 1983,
2. R. F. Gilberg, B. A. Forouzan, “Data Structures: A Pseudocode approach with C", Second Edition, Thomson
India Edition, 2005.
3. Sara Baase and A. Van Gelder, “Computer Algorithms", Third Edition, Pearson Education, 2000.
4, T.H, Cormen, C. E, Lelserson, R. L. Rivest, and C. Stein, “Introduction to
algorithms", Second Edition, Prentice Hall of India Ltd, 2001
@ Einstein College Of Engineering [1/159]EE36-Data Structure and Algorithms EEE
4.4 Introduction:
What Is data structure?
“The way information is organized in the memory of a computer Is called a data
structure”.
(or)
A data structure Is a way of organizing data that considers not only the items stored, but
also their relationship to each other. Advance knowledge about the relationship between data
items allows designing of efficient algorithms for the manipulation of data,
Definition of data structures
+ Many algorithms require that we use a proper representation of data to achieve efficiency.
* This representation and the operations that are allowed for it are called data structures.
+ Each data structure allows insertion, access, deletion etc.
Why do we need data structures?
* Data structures allow us to achieve an important goal: component reuse
* Once each data structure has been implemented once, it can be used over and over
again in various applications.
Common data structures are
+ Stacke + Queues + Liste
eTrees + Graphs + Tables
4.2 Classification Of Data Structure:
Based on how the data Items or operated it will classified into
1. Primitive Data Stucture : Is one the dati items are operated closest to the
machine level instruction.
Eg : int, char and double.
2. _Non-Primitive Data Structure : is one that data items are not operated closest
to machine level instruction,
2.4, Linear Data Structure : In which the data items are stored in sequence
order.
Eg: Arrays, Lists, Stacks and Quoues.
Operations performed on any linear structure:
1. Traversal ~ Processing each element in the list
2. Search ~ Finding the location of the element with a given value.
3. Insertion ~ Adding a new element to the list.
4. Deletion - Removing an element from the list.
5. Sorting ~ Arranging the elements in some type of order.
@ Einstein College Of Engineering [2/159]FE36-Data Structure and Algorithms
IEEE
6. Merging ~ Combining two lists into a single list.
2.2,
Linear Data Structure
Non Linear Data Structure :
presence.
Eg: Trees, Graphs.
In which the order of data items Is not
Non-Linear Data Structures
2. List 1. Graph
a. Array a. Adjacency List
i. One Dimensional b. Adjacency Matrix
li, Multi-Dimensionat ¢. Spanning Tree
iii, Dynamic Array 2. Tree
a. M-Way Tree
iv, Matrix: «. BeTnoe
1. Sparse Matrix 1. 23-4 Tree
b. Linked List 2. B+ Tree
L. Single Linked List b. Binary Tree
ji, Double Linked List
iil, Circular Linked List
c. Ordered List
1. Stack
li, Queue
1. Circular Queue
2. Priority Queue
lil, Deque
3. Dictionary (Associative Array)
a. Hash Table
i, Binary Search Tree
ii, Self-Balancing Binary
Search Tree
1. AVL Tree
2. Red-Black Tree
3. Splay Tree
iii, Heap
1. Min Heap
2. Max Heap
3. Binary Heap
iv. Parse Tree
‘An example of several common data s Characteristics of Data Structures
Data Structure |Advantages Disadvantages
‘array Quick inserts ‘Slow search
Fast access if index known Slow deletes
Fixed size
‘Ordered Array [Faster search than unsorted array Slow inserts
Slow deletes
Fixed size
‘stack Last-in, first-out acces Slow access to other items
Queue ‘Slow access to other items
First-in, first-out access
@ Einstein College Of Engineering
[3/159]FE36-Data Structure and Algorithms
IEEE
Linked List (Quick inserts ‘Slow search
Quick deletes
Binary Tree Quick search [Deletion algorithm is complex
Quick Inserts
Quick deletes
(if the tree remains balanced)
Red-Black Quick search ‘Complex to implement
Tree Quick inserts
Quick deletes
(Tree always remains balanced)
2-3-4Tree (Quick search ‘Complex to implement
Quick inserts
Quick deletes
(Tree always remains balanced)
(Simitar trees good for disk storage)
Hash Table [Very fast access if key is known Slow deletes
Quick inserts Access slow if key Is not known
Inefficient memory usage
Heap Quick inserts ‘Slow access to other items
Quick deletes
‘Access to largest item
Graph ‘Best models real-world situations |Some algorithms are slow and very
complex
4.3 Abstract Data Types
Abstract data type (ADT) Is a specification of a set of data and the set of operations that
‘can be performed on the data.
examples
= Associative array
= Set
© Stack
@ Einstein College Of Engineering
[4/159]FE36-Data Structure and Algorithms IEEE
Uses of Al
1. It helps to efficiently develop well designed program
2. Facilitates the decomposition of the complex task of developing a software system into a
number of simpler subtasks
3. Helps to reduce the number of things the programmer has to keep in mind at any time
4, Breaking down a complex task into a number of earlier subtasks also simplifies testing and
debucaing
Algorithm:
Definition: An algorithm is a finite set of instructions which, if followed, accomplish a
particular task. In addition every algorithm must satisfy the following criteria
1. input: there are zero or more quantities which are externally supplied;
2. output: at least one quantity Is produced;
3. definiteness: each instruction must be clear and unambiguous;
4, finiteness: if we trace out the instructions of an algorithm, then for all cases the
algorithm will terminate after a finite number of steps;
4.4 Linear Data Structures
A data structure is sald to be /inear if its elements form a sequence or a linear list.
Bramples:
arrays
inked Lists
Stacks, Queues
1.4.4 Arrays
% The very common linear structure Is array. Since arrays are usually easy to traverse,
search and sort, they are frequently used to store relatively permanent collections of data.
An array isa list ofa finite number n of homogeneous data elements (J.e., data elements
of the same type) such that:
2) The elements of the array are referenced respectively by an index consisting of
i consecutive numbers.
b) The elements of the array are stored respectively In successive memory
locations.
Operations of Array:
4 Two basic operations in an array are storing and retrieving (extraction)
Storing: A value is stored in an element of the array with the statement of the form,
Data[l] = X ; Where Is the valid Index in the array
And X Js the element
Extraction: Refers to getting the value of an element stored in an array.
@ Einstein College Of Engineering 15/159]FE36-Data Structure and Algorithms IEEE
X = Data [ i ], Where I is the valid index of the array and X Is the element.
Array Representation:
The number n of elements Is called the length or size of the array. If not explicitly stated
we will assume that the index starts from 0 and end with n~
In general, the length (range) or the number of data elements of the array can be obtained
from the index by the formula,
Length = UB-LB +1
Where UB Is the largest index, called the Upper Bound, and LB is the smallest index, called
Lower Bound, of the array.
If LB = 0 and UB = 4 then the length is,
Length = 4-0+1=5
‘The elements of an array A may be denoted by the subscript notation (or bracket
notation), ALO], ALL], AZ], » ALN]
The number K in A[K] Is called a subscript or an index and A[K] is called a subscripted
variable.
‘Subscripts allow any element of A to be referenced by its relative position in A.
If each element in the array Is referenced by a single subscript, It is called single
dimensional array.
In other words. the number of subscripts gives the dimension of that array.
Two-dimensional Arrays :
A two-dimensional mXn array A is a collection of mi*n data elements such that each
element is specified by a pair of integers (such as I, 3), called subscripts, with the property
that, Ost
1020 + 0% 1020 (locates the 1020)
al1] means a + 19 1020 + 1 * size of datatype 1020 + 2 > 1022[for ‘int’ size is 2 byte]
a[2] means a + 29 1020 + 2 * size of datatype > 1020 + 4 1024
n[1] means a +39 1070 +1 sire of dntntyne + 1020 1 6 > 1026
al4] means a + 49 1020 + 4 * size of datatype > 1020 + 8 9 1028,
Array indexing helps to manipulate the index using a for loop. Because of that retrieval of element
from an array is very easy. For example, the following code initializes all of the values in the
array to 0:
int a[5]; /* Array dedaration */
inti;
/* Initializing Array Elements to 0 */
for (i=0; i<5; i++)
ati] = 0;
/* print array *
printf("Elements in the array are..\n");
for (120; 1 < 5; I++)
printf("%6d\n", afi);
Note 1 (mathematics) A matrix most of whose entries are zeros.
@ Einstein College Of Engineering [8/159]FE36-Data Structure and Algorithms IEEE
Advantages:
+ Reduces memory access time, because all the elements are stored sequentially. By
Incrementing the index, It Is possible to access all the elements in an array.
‘= Reduces no, of variables in a program.
«+ Easy to use for the programmers.
Disadvantages:
‘+ Wastage of memory space Is possible, For example: Storing only 10 elements in 2 100 size
array. Here, remaining 90 elements space is waste because these spaces can't be used by
other programs till this program completes its execution.
‘+ Storing heterogeneous elements are not possible.
= Array bound checking Is not available in °C’. So, manually we have to do that.
Note: Memory representation of 1, 2 and multi-dimensional array refer class notes
1.4.4.1 Structures
Struct: Declares a structure, an object consisting of multiple data items that may be of
different types.
DEFINING A StruCTURE:
Syntax:
struct tag
-, data-type member 1;
‘forget the
‘Semicolon here _} data+type member 2;
data-type member m;
Here, struct is the required keyword; tag (optional) Is a name that identifies structures of this
type; and membert, meber2, ... member m are individual member deciarations.
+The individual members can be ordinary variables, pointers, arrays, or other structures.
+ A storage class cannot be assigned to an individual member, and individual members can
not be initialized within a structure type declaration.
DECLARING STRUCTURE VARIABLES?
Once the composition of the structure haa been defined, individual structure-type variablee
can be declared as follows:
storage-class struct tag variable, varibale2, .., varlable ny
where storage-class is an optional storage class specifier, struct is @ required keyword, tag
is the name that appeared in the structure declaration and variablet, variable2, ..., varlable n are
structure variables of type tag.
Example:
@ Einstein College Of Engineering [9/159]FE36-Data Structure and Algorithms IEEE
struct student
t
int regno;
char name[20];
char dept[10];
int year;
a
Here, regno, name, dept and year are the members of the student structure, And this is
the definition of the datatype. So, no memory will be allocated at this stage. The memory will be
allocated after the declaration only. Structure variables can be declared as following methods:
1a) Normal way of declaration
struct student s4, s2;
bb) It Is possible to combine the declaration of the structure composition with that of the structure
variables, as shown below:
struct student
t
int regno;
char name[20];
char doptl1o},
int year;
381, 52;
) If we are going to declare all the necessary structure variables at definition time then we can
create them without the tag, as shown below:
struct
t
int regno;
char name[20];
char dept[10];
int year
} st, s2
Since there Is no tag name, additional variables can not be generated other than thie location. Le.
can't create new variables with this structure In the local functions. If we want we have to
redefine the structure variable once again.
d) If we use the typedef in front of the struct keyword then the tag name alone can be used in
other places whenever you want to use the student data type.
typedef struct student
t
@ Einstein College Of Engineering [10/159]FE36-Data Structure and Algorithms IEEE
int regno;
char name[20];
char dept[10];
int year;
} ;student s1, 52; /* here the struct keyword is not needed because of typedef */
‘struct student
(Int regno;
char name{20);
char deprfi0}:
int year;
sN
[°
The size of each of these variables is 34 bytes because the size of the student datatype is 34
bytes. And the memory will be allocated for each variable as follows:
sa bytes
t
Address 6100 _ [6102 sabes 10122
al
‘asress. 1002
2
regno, name dept year
INITIALIZING STRUCTURE VARIABLES!
The members of a structure variable can be assigned initial values in much the same
manner as the elements of an array. The initial values must appear in the order in which they will
be assigned to their corresponding structure members, enclosed In braces and separated by
The general form is,
storage-class struct tag variable = {valuet, value2, ....value n};
A structure variable, like an array, can be initialized only if its storage class is either
external or static.
Example:
static struct student s1 = { 340, “Kumara Vel”, “CSE”, 3};
@ Einstein College Of Engineering [11/189]FE36-Data Structure and Algorithms IEEE
static struct student 52 = {533, "Sankarl”, “CSE”, 4};
STORING VALUES INTO THE MEMBERS OF THE STRUCTURE VARIABLES:
a) Values may be stored by assignment operation.
si.regno = 500;
strepy(s1.name, “Surya”
strepy(si.dept, "CSE”);
si.year =
b) also the scanf statement may be used to give values through the keyboard,
scanf("%d", &s4.regno);
scanf("%s“, st.name);
scanf("%s”, s1.dept);
scanf("%d", &S4.year); oR
scanf("%d%s%s%d", &S4.regno, S1.name, st.dept, &st.year);
[ARRAYS IN THE STRUCTURE:
‘The derived data types like array can be included in the structure as a member.
Example:
struct student
t
int com
char name[20];
int marks[5]; 1 Thiseanin c
int totaly | ioncsnie | 1 dyeats 13
float avg; | Sone | 1 Siete 1
char result[5); | gece | imei 11
dstu; Lsemeet it | | mgenner 1
In memory It would be stored as given below:
stu (size is 43 bytes}
tug. -
fname array (size —20 bytes) mark array (size — $f)
7
2100 2102, 12 214 2216 2218 2220 $2022 2024
rol “ham 2D oy a fear ave Tesut
mak,
Toaccess hslocaton we |
1 neodto ure, i
t
i
@ Einstein College Of Engineering [12189]EE36-Data Structure and Algorithms EEE
Nesrep SrucTURES:
A structure variable may be defined as a member of another structure. In such situations, the
declaration of the embedded structure must appear before the declaration of the outer structure,
Example:
struct date ret
t int no;
Int day: char name[20};
Float amt;
int month; ‘struct date
int yeary t
, int day;
» int month;
struct bill int year;
t OR Jbitidate, paydates
Int eno; Jb, b2;
char name[20];
float amt;
struct date billdate;
struct date paydate;
}b4, bz
The second structure bill now contains another structure, date, as one of ite members. The
structure may look like as follows:
bildate (size-O bytes) | _paydate (size —0 bytes)
ot
2100 2192 2212 foo 2018 220042002008
a
eno name amt day month year day month
This can be
Thiecan be
accessed by bf.cno accessed by This can be
bt billdate.day accossed by
. |. Dt-paydate.year
PROCESSING STRUCTURE
Consider the following structure:
struct student
t
int regno;
char name[20];
char dept[10];
struct date
@ Einstein College Of Engineering [13/189]FE36-Data Structure and Algorithms IEEE
int day;
Int monti
int year;
}bday;
int marks[51;
Int year:
st;
‘The members of a structure are usualy processed individually, as separate entities.
Therefore, we must be able to access the individual structure members. A structure member can
be accessed by writing _structure_variable.member;
where variable refers to the name of a structure-type variable, and member refers to the
name of a member within the structure. The period (.) separates the variable name from the
member name. It is a member of the highest precedence group, and its associativity is left to
right.
Example —_s4.regno, si.name, s4.dept, si.year
Annested structure member can be accessed by writing
structure_variable.member.submember;
Examele —sdbday.day, stubday:month, o4.bday.year
where member refers to the name of the member within the outer structure, and submember
refers to the name of the member within the embedded structure. similarly, if a structure Is an
array, then an individual array element can be accessed by writing
structure-variable.member[expression];
Example: s4.mark[0], s4.mark[41], s4.mark[2], s1.mark[3], st.mark[4]
POINTERS To STRUCTURES:
‘The address of a given structure variable can be obtained by using the & operator. Pointers
to structures, like all other pointer variables may be assigned addresses. The following statements
Illustrate this concept.
example:
struct student
c
int regno;
char name[20]; This is structures
char dept{10}; vanable
int yeary ” hi is truce
» , pointer variable
struct student stu, *sptr;”
@ Einstein College Of Engineering [14/189]FE36-Data Structure and Algorithms IEEE
sptr = &stu; 34 bytes
2 bytes
: eee a
non .
/ Thisis ‘ addoss e109 _'6102 e122 Ietg2_!
structure
pointer; stut
variable, /
regno);
printf(“student Name 1 %s\n", sptr->name);
printf(“Department Name : %s\n", sptr->dept);
printf(“Year of Study 1 %d\n", sptr->year);
1.4.2 STACK:
“) stack Is an ordered list in which all ingortions and deletions are made at one end, called
the top”. stacks are sometimes referred to as Last In First Out (LIFO) lists
Stacks have some useful terminology associated with them:
+ Push To add an element to the stack
+ Pop To remove an element from the stock
‘+ Peek To look at elements in the stack without removing them
+ LIFO Refers to the last in, first out behavior of the stack
+ FILO Equivalent to LIFO
STACK (DATA STRUCTURE)
Simple representation of a stack
Given a stack S=(a[1],a[2],.......a[n]) thea we say that at is the bottom most element and
element a[i]) Is on top of element a[i-1], 1 Stack r clemen | Stack
PUSH | * top — 16
top: 8 8
4 4
2, Deleting an element from a stack, ( called POP operations )
Deleting or Removing element from the TOP of the stack Is called POP operations.
Check Condition:
TOP = 0, then STACK EMPTY
Deletion in stack ( POP Operation )
procedure delete(var item : items);
{
remove top element from the stack stack and put it in the item}
= 0 then stackempty;
stack(top);
top := top-t;
end; (of delete)
item!
element [6
‘7.6 ————_ pop
z operation
4 top- 8
4
Stack
stack
/* here, the variables stack, and top are global variables */
int pop ( )
{
if (top
t
4)
printf(“Stack is Underflow");
@ Einstein College Of Engineering [17/189]FE36-Data Structure and Algorithms IEEE
return (0);
I
else
{
return (stack[top--]);
I
3
3. Peek Operation:
v- Returns the item at the top of the stack but does not delete it.
This can also result in underflow if the stack is empty,
em /
element [G
tor—4_6 PEEK top.
8 ‘operation 8
4 4
Stack Stack
PEEK(STACK, TOP)
BEGIN
/* Check, Stack Is empty? */
IF (TOP == -1) then
print “Underflow” and return 0.
else
tem = STACK[TOP] / * stores the top element into a local variable */
return item /* returns the top element to the user */
END
Implementation in C using array:
/* here, the variables stack, and top are global variables */
int pop ()
C
if (top
{
1)
printf("Stack is Underfiow");
return (0);
@ Einstein College Of Engineering [18/159]FE36-Data Structure and Algorithms IEEE
return (stack{top]);
+
}
Applications of Stack
1. It Is very useful to evaluate arithmetic expressions. (Postfix Expressions)
2. Infix to Postfix Transformation
3. It Is useful during the execution of recursive programs
4, A Stack Is useful for designing the compiler in operating system to store local variables
Inside a function block.
5. A stack (memory stack) can be used in function calls including recursion.
6. Reversing Data
7. Reverse a List
8. Convert Decimal to Binary
9. Parsing ~ It is a logic that breaks into independent pieces for further processing
10. Backtracking
Examples :
1. Infix notation A+(B¥C)— equivalent Postfix notation ABC*+
2. Infix notation (A1B)*C_—_—Equivalent Postfix netatien AB+C*
Expression evaluation and syntax parsing
Calculators employing reverse Polish notation (also known as postfix notation Juse a
stack structure to hold values.
Expressions can be represented in prefix, postfix or infix notations. Conversion from one
form of the expression to another form needs a stack. Many compilers use a stack for parsing the
syntax of expressions, program blocks etc. before translating into low level code. Most of the
programming languages are context-free lanquages allowing them to be parsed with stack based
machines. Note that natural languages are context sensitive languages and stacks alone are not
enough to interpret their meaning.
Inflx, Prefix and Postfix Notation:
We are accustomed to write arithmetic expressions with the operation between the two
operands: a+b or ¢/d. If we write abe, however, we have to apply precedence rules to avoid
the ambiguous evaluation (add first or multiply first?).
‘There's no real reason to put the operation between the variables or values. They can just
as well precede or follow the operands. You should note the advantage of prefix and postfix: the
need for precedence rules and parentheses are eliminated.
@ Einstein College Of Engineering [19/189]FE36-Data Structure and Algorithms IEEE
Infix Prefix | Postfix
arb ab abe
a+bec +atbe abets
(a+b) * (c-4) *+ab-cd ab+cd-*
b*b-4*are
40-3" 541
Examples of use: ( application of stack )
Arithmetic Expressions: Polish Notation
> An arithmetic expression will have operands and operators.
» Operator precedence listed below:
Highest 8)
Next Highest (*) and (1)
Lowest : (+) and (-)
> For most common arithmetic operations, the operator symbol is placed in between its two
operands. This Is called infix notation.
Example: A+B, E*F
> Parentheses can be used to group the operations.
Example: (A + B) *C
» Accordingly, the order of the operators and operands in an arithmetic expression does not
uniquely determine the order in which the operations are to be performed,
> Polish notation refers to the notation in which the operator symbol Is placed before its two
operands. This Is called prefix notation.
Example: +AB, *EF
> The fundamental property of polish notation is that the order in which the operations are to
be performed is completely determined by the positions of the operators and operands in
the expression.
» Accordingly, one never needs parentheses when writing expressions In Polish notation.
> Reverse Polish Notation refers to the analogous notation in which the operator symbol is.
placed after its two operands. This is called postfix notation.
Example: AB+, EF*
> Here also the parentheses are not needed to determine the order of the operations,
> The computer usually evaluates an arithmetic expression written in infix notation in two
steps,
1. It converts the expression to postfix notation.
@ Einstein College Of Engineering [20/159]FE36-Data Structure and Algorithms IEEE
2, It evaluates the postfix expression.
» In each step, the stack is the main tool that is used to accomplish the given task.
(2)Question : ( Postfix evaluation )
How to evaluate a mathematical expression using a stack The algorithm for Evaluating a
postfix expression ?
+ Initialise an empty stack
+ While token remain in the input stream
~ Read next token
~ If token is a number, push it into the stack
~ Else, if token is an operator, pop top two tokens off the stack, apply the operator,
and push the answer back into the stack
+ Pop the answer off the stack.
Algorithm postfixexpression
Initialize a stack, opndstk to be empty.
{
scan the Input string reading one element at a time into symb
}
While ( not end of input string )
{
Symb := next input character;
If symb Is an operand Then
push (opndstk,symb)
Else
[symbol Is an operator]
{
Opndt:=pop(opndstk);
(Opnd2:=pop(opndnstk);
Value := result of applying symb to opndt & opnd2
Push(opndstk, value);
y
Result = pop (opndstk);
Example:
6234+-382/4+%*283+
‘Symbol | Operand 4 (A) | Operand 2 (B)| Value (A @B) | STACK
6 6
@ Einstein College Of Engineering [21/189]FE36-Data Structure and Algorithms IEEE
2 62
3 62,3
+ z 3 3 65
= 5 T t
3 13
e Tae
z 13,82
7 2 7 a4
+ a 7 7
= 7 7
2 72
5 7 z 49 8
3 a3
+ a 3 32 52
‘The Final value in the STACK Is 52, This is the answer for the given expression.
(2) run time stack for function calls ( write factorial number calculation procedure)
push local data and return address onto stack
return by popping off local data and ten popping off address and returning to it
retum value can be pushed onto stack before returning, popped off by caller
(3) expression parsing
e.g. matching brackets: [ Coe Coe DD Ded oe) J
push left ones, pop off and compare with right ones
4) Infix To Postfix Conversion
Infix expressions are often translated into postfix form in which the operators appear after
their operands. Stey
1, Initialize an empty stack.
2. ‘Scan the Infix Expression from left to right.
3. If the scannned character Is an operand, add it to the Postfix Expression.
4, If the scanned character is an operator and if the stack is empty, then push the character to
stack.
5. If the scanned character Is an operator and the stack is not empty, Then
(a) Compare the precedence of the character with the operator on the top of the stack.
(b) While operator at top of stack has higher precedence over the scanned character &
stack is not empty.
(1) POP the stack.
@ Einstein College Of Engineering [22/189]FE36-Data Structure and Algorithms IEEE
(ii) Add the Popped character to Postfix String.
((¢) Push the scanned character to stack.
6. Repeat the steps 3-5 till all the characters
7. While stack is not empty,
(a) Add operator in top of stack
(b) Pop the stack.
8. Return the Postfix string.
Algorithm Infix to Postfix conversion ( without parenthesis)
1. Opstk = the empty stack:
2. while ( not end of input )
{
symb = next input character;
3. if (symb Is an operand )
add symb to the Postfix String
4, else
{
5. While( ! empty (opstk) && prec ( stacktop ( opstk), symb) )
C
topsymb = pop ( opstk )
add topsymb to the Postfix String;
} / * end of while */
Push(opstk, symb);
} /* end else */
6. } /* end while * /
7. While( ! empty ( opstk ) )
{
topsymb = pop (opstk)
add topsymb to the Postfix String
} /* end of while */
8, Return the Postfix String.
1.4.3 QUEUE:
“A queue Is an ordered list in which all insertions at one end called REAR and deletions are
made at another end called FRONT”, queues are sometimes referred to as First In First Out
(FIFO) lists. enqueue
Gnsertion)
lence
@ Einstein
dequene
(Deletion)
Front RearFE36-Data Structure and Algorithms IEEE
Example
1. The people waiting In line at a bank cash counter form a queue.
2. In computer. the jobs waiting in line to use the processor for execution. This queue Is
called Job Queue.
Operations of Queue
‘There are two basic queue operations. They are,
Enqueue ~ Inserts an item / element at the rear end of the queue. An error occurs if the queue is
full,
Dequeue - Removes an item / element from the front end of the queue, and returns it to the
user. An error occurs if the queue is empty.
1, Addition into a queue
Procedure addg (item : items);
{add Item to the queue q}
begin
if rear=n then queuefull
else begin
rear :=rear+1;
alrear]:=item;
end;
end; {of addq)
Deletion in a queue
Procedure deleteg (var item : items);
{delete fru Ure front of y aud pul ity ikem}
begin
if front = rear then queueempty
else begin
front :
front+1
Item := q[front];
@ Einstein College Of Engineering [24/189]FE36-Data Structure and Algorithms IEEE
end;
end
Uses of Queues ( Application of queue )
Queues remember things in first-in-first-out (FIFO) order. Good for fair (First come first
served) ordering of actions.
Examples of use: (Application of stack )
le scheduling
processing of GUI events
printing request
2» simulation
orders the events
models real life queues (e.g. supermarkets checkout, phone calls on hold)
Circular Queue:
Location of queue are viewed in a circular form. The first location is viewed after the last
one.
Overflow occurs when all the locations are filled.
LA
front
Algorithm Circular Queue Insert
Void cQtnsert ( int queuel J, front, rear, item)
{
if (front = = 0)
front = front +1;
1 (( (rear = maxsize ) 88 ( front = = 1)) || ( (rear! = 0.) && ( front = rear +1)))
c
printf(* queue overflow *);
if( rear = = maxsize )
rear = 1;
else
rear = rear +1;
q [rear] = item;
@ Einstein College Of Engineering [25/159]FE36-Data Structure and Algorithms IEEE
Algorithm Circular Queue Delete
int CQDelete ( queue [ ], front, rear )
{
If (front = = 0)
printf (" queue underflow *);
else
{
Item = queue [ front ];
If(front = = rear)
{
front = 0; rear = 0;
+
else if ( front = = maxsize )
{
front = 1;
1
else
front = front + 1;
+
return item;
+
1.4.4 Linked List
Some demerits of array, leads us to use linked list to store the list of items. They are,
1. Its relatively expensive to insert and delete elements in an array.
2. Array usually occupies a block of memory space, one cannot simply double or triple
the size of an array when additional space Is required. (For this reason, arrays are
called “dense lists” ond arc said to be “static” data structures.)
% A linked list, or on
the linear order is given by means of pointers. That Is, each node Is divided into two
parts:
¥_ The first part contains the information of the element i.e, INFO or DATA,
¥. The second part contains the link field, which contains the address of the next
node in the list.
/-way list, is a linear collection of data elements, called nodes, where
Nope
@ Einstein College Of Engineering [26/159]FE36-Data Structure and Algorithms IEEE
4 The linked list consists of series of nodes, which are not necessarily adjacent in memory.
A list is a dynamic data structure ie. the number of nodes on a list may vary
dramatically as elements are inserted and removed.
% The pointer of the last node contains a special value, called the null pointer, which is any
Invalid address. This null pointer signals the end of list.
The list with no nodes on it Is called the empty list or null list.
Example: The linked list with 4 nodes.
staRr
oe fet} Hol eel He
HEAD
‘Types of Linked Lists:
fa) Linear Singly Linked List
b) Circular Linked List
¢) Two-way or doubly linked lists
) Circular doubly linked lists
Advantages of Linked List
1. Linked List Is dynamic data structure; the size of a list can grow or shrink during the
program execution. So, maximum size need not be known in advance.
2. The Linked List does not waste memory
3. It is not necessary to specify the size of the list, as in the case of arrays.
4. Linked List provides the flexibility in allowing the items to be rearranged.
What are the pitfalls encountered in single linked list?
1. A singly linked list allows traversal of the Ist in only one direction.
2. Deleting a node from a list requires keeping track of the previous node, that is, the node
whose link points to the node to be deleted.
3. If the link In any node gets corrupted, the remaining nodes of the list become unusable.
Linearly-linked List:
Is a collection of elements called Nodes. Each node consist of two fields, namely data field
to hold the values and link(next ) field points to the next node in the list.
It consists of a sequence of nodes, each containing arbitrary data fields and one or two
references ("links") pointing to the next and/or previous nodes.
A linked list is a self-referential datatype (or) data structure because it contains a
pointer or link to another data of the same type.
Linked lists permit insertion and removal of nodes at any point in the list in constant time,
but do not allow random access.
@ Einstein College Of Engineering [27/189]FE36-Data Structure and Algorithms IEEE
Several different types of linked list exist: singly-linked lists, doubly-linked lists, and
circularly-linked lists. One of the biggest advantages of linked lists Is that nodes may have
multiple pointers to other nodes, allowing the same nodes to simultaneously appear in different
orders in several linked lists
Singly-linked li
‘The simplest kind of linked list Is a singly-linked list (or slist for short), which has one
link per node. This link points to the next node in the list, or to a null value or empty list if it Is the
final node.
12 » 99 > 37 >]
A singly linked list containing three integer values
inked list:
‘A more sophisticated kind of linked list is a doubly-linked list or two-way linked list.
Each node has two links: one points to the previous node, or points to a pull value or empty list If
It Is the first node; and one points to the next, or points to a null value or empty list if it is the
final node.
head node ‘prey dala ‘next
rma] | |2] OL |s6) OL as] an
kL a eb
An example of a doubly linked list.
Circularly-linked list:
In a circularly-linked list, the first and final nodes are linked together. This can be done
for both singly and doubly linked lists. To traverse a circular linked list, you begin at any node and
follow the list in either direction until you return to the original node, Viewed another way,
Circularly-linked lists can be seen as having no beginning or end. This type of list is most useful for
managing buffers for data ingest, and in cases where you have one object in a list and wish to see
all other objects in the list.
‘The pointer pointing to the whole list is usually called the end pointer.
Singly-circularly-linked list:
In a singly-circularly-linked list, each node has one link, similar to an ordinary singly-
linked list, except that the next link of the last node points back to the first node
As in a singly-linked list, new nodes can only be efficiently inserted after a node we already
have a reference to. For this reason, it's usual to retain a reference to only the last element in a
singly-circularly-linked list, as this allows quick insertion at the beginning, and also allows access
to the first node through the last node's next pointer.
@ Einstein College Of Engineering [28/159]FE36-Data Structure and Algorithms IEEE
+ Note that there is no NULL terminating pointer
4 Choice of head node Is arbitrary
Atal pointer serves no purpose
‘What purpose(s) does the head pointer serve?
Doubly-circularly-linked list
In a doubly-circularly-tinked list, each node has two links, similar to a doubly-linked
list, except that the previous link of the first node points to the last node and the next link of the
last node points to the first node. As in doubly-linked lists, Insertions and removals can be done at
any point with access to any nearby node,
Sentinel nodes
Linked lists sometimes have a special dummy or sentinel node at the beginning and/or at the end
of the list, which is not used to store data.
Basic Operations on Linked Lists
1. Insertion
a. At first
b. At last
c. Ata given location (at middle)
Deletion
a. First Node
b. Last Node
c. Node in given location or having given data item,
Initial Condition
HEAD = NULL;
/* Address of the first node in the list Is stored in HEAD. Initially there is no node in the list. So,
HEAD Is initialized to NULL (No address) */
What are the Applications of linked list?
To Implement of Stack, Queue, Tree, Graph etc.,
Used by the Memory Manager
@ Einstein College Of Engineering [29/189]FE36-Data Structure and Algorithms IEEE
To maintain Free-Storage List
Doubly Linked Lists (or) Two - Way Lists
There are some problems in using the Single linked list. They are
1. A singly linked list allows traversal of the Ist in only one direction. (Forward only)
2. Deleting a node from a list requires keeping track of the previous node, that Is, the node
Whose link points to the node to be deleted.
‘These major drawbacks can be avoided by using the double linked list. The doubly linked list is a
linear collection of data elements, called nodes, where each node is divided into three parts.
They are:
1. A pointer fleld LEFT which contains the address of the preceding node in the list
2. An information field INFO which contains the data of the Node
3. A pointer fleld RIGHT which contains the address of the next node in the list
Example:
LEFT INFO RIGHT
has 080) 2140
frano} ts] 7 [oaole—*frono] 7
Linked lists vs. arrays
faao] 7 [uu 120
Array Linked list
Indexing 0(1) O(n)
Inserting / Deleting at end ofa) (2)
Inserting / Deleting in middle (with iterator) O(n) (1)
Persistent No Singly yes
Locality Great Bad
array Linked list
Static memory Dynamic memory
Insertion and deletion required
to modify the existing element
easy,
Incertion and deletion are made
location
Elements stored as contiguous Element stored as Non-
memory as on block. contiguous memory as pointers
Accessing element is fast Accessing element is slow
SINGLY LINKED LISTS
@ Einstein College Of Engineering [30/159]FE36-Data Structure and Algorithms IEEE
1. Insertion of a Node in the Beginning of a List
Step 1 : Allocate memory for a node and assign its address to the variable * New"
Step 2 : Assign the element in the data field of the new node.
Step 3; Make the next field of the new node as the beginning of the existing list.
Step 4: Make the new node as the Head of the list after insertion.
Algorithm InsertBegin ( Head. Elt )
[ Adding the element elt in the beginning of the I'st pointed by Head]
1, new € getnode ( NODE )
data (new) € elt
next (new) © Head
Head © new
return Head
Insertion of a Node at the End of a Singly Linked List
Step 1; Allocate memory for a node and assign its address to the variable * New"
Step 2: Assign the element in the data field of the naw node.
Step 3: Make the next field of the new node as NULL This Is because the new node
will be the end of the resultant list.
Step 4: If the existing list is empty, call this new node ae the liet, Elee, get the
address of the last node in the list by traversing from the beginning pointer.
Step 5: Make the next field of the last node point to the new node.
yaen
Algorithm Insertend ( Head, Elt )
[ Adding the element elt at the end of the list]
1, new € getnode ( NODE )
2. data (new) € elt
3. next (new ) © NULL
4. If (Head == NULL) Then
Head € new
Return Head
Else
Temp € Head
5. While ( next ( temp ) # NULL)
temp € next ( temp )
6. next (temp ) € new
7. return Head.
Applications of linked lists
@ Einstein College Of Engineering [31/189]FE36-Data Structure and Algorithms IEEE
Linked lists are used as a building block for many other data structures, such as stacks,
‘queues and their variations.
1. Polynomial ADT:
A polynomial can be represented with primitive data structures. For example, a polynomial
represented as aix* aix*! +... + 20 can be represented as a linked list. Each node Is a structure
with two values: a, and i. Thus, the length of the list will be k. The first node will have (a, k), the
second node will have (a:.1, k-1) etc, The last node will be (ao, 0).
The polynomial 3x° + 7x” + 5 can be represented in a list as follows: (3,9) --> (7,3) ==> (5,0)
where each pair of integers represent a node, and the arrow represents a link to its neighbouring
node.
Derivatives of polynomials can be easily computed by proceeding node by node. In our
previous example the list after computing the derivative would represented as follows: (27,8) -->
(21,2). The specific polynomial ADT will define various operations, such as multiplication, addition,
subtraction, derivative, Integration etc. A polynomial ADT can be useful for symbolic computation
as well
2. Large Integer ADT:
Large integers can also be implemented with primitive data structures. To conform to our
previous example, consider a large integer represented as a linked list. If we represent the integer
as successive powers of 10, where the power of 10 increments by 3 and the coefficient is a three
digit number, we can make computations on such numbers easier. For example, we can represent
a very large number as follows:
513(10°) + 899(10%) + 722(10°).
Using this notation, the number can be represented as follows:
(513) --> (899) --> (722).
The first number represents the coefficient of the 10° term, the next number represents the
coefficient of the 10° term and so on. The arrows represent links to adjacent nodes.
The specific ADT will define operations on this representation, such as addition, subtraction,
multiplication, division, comparison, copy etc.
An array allocates memory for all its elements lumped together as one block of memory. In
contrast, a linked list allocates space for each element separately in its own block of memory
called 2 ode". The list gets Is overall structure by using pointers to
connect all its nodes together like the links in a chai
“data” field to store whatever element type the list holds for its
client, and a “next” field which is a pointer used to link one node to the next node.
Inked list element” or
Each node contains two flelds:
@ Einstein College Of Engineering [32/189]FE36-Data Structure and Algorithms IEEE
Each node is allocated in the heap with a call to malloc(), so the node memory continues to
exist until It Is explicitly deallocated with a call to free(). The front of the list Is a pointer to the
first node. Here Is what a list containing the numbers 1, 2, and 3 might look like...
‘malfoc() _malloc() is a system function which allocates a block of memory in the "heap" and
returns a pointer to the new block. The prototype for malloc() and other heap functions are in
stdlib.h. The argument to malloc() Is the integer size of the block in bytes. Unlike local ("stack")
variables. heap memory Is not automatically deallocated when the creating function exits.
malloc() returns NULL if it cannot fulfill the request. (extra for experts) You may check for the
NULL case with assert() If you wish just to be safe. Most modern programming systems will throw
lan exception or do some other automatic error handling in thelr memory allocator, so It Is
becoming less common that source code needs to explicitly check for allocation failures.
free() free() Is the opposite of malloc(). Call free() on a block of heap memory to indicate to the
system that you are done with It. The argument to free() is @ pointer to a block of memory in the
heap — a pointer which some time earlier was obtained via a call to malioc().
Linked List implementation
In C: typically individual cells dynamically allocated containing a pointer to the next cell.
Advantages:
Space used adapts to size + usually resuts in better space usage than sequential despite
storing a pointer in each cell
speed improvements for some operations
Disadvantages:
* speed reductions for some operations
Doubly Linked list
Allows efficient backwards traversal takes longer to insert and delete (but the same
complexity) takes more space for the extra pointer (unless we use the for trick to save space at the
cost of time)
Circular list (head and tail inked)
‘Two Marks
1, Limitations of arrays
e) Arrays have a fixed dimension. Once the size of an array is decided it cannot be increased
or decreased during execution.
f) Array elements are always stored in contiguous memory locations. So it need contiguous
locations otherwise memory will not be allocated to the arrays
@ Einstein College Of Engineering [33/159]FE36-Data Structure and Algorithms IEEE
9) Operations like insertion of a new element in an array or deletion of an existing element
from the array are pretty tedious. This is because during insertion or deletion each element
after the specified position has to be shifted one position to the right or one position to the
left.
2, Define Data Structure,
‘A data structure Is a way of organizing data that considers not only the items stored, but also
thelr relationship to each other. Advance knowledae about the relationship between data items
allows designing of efficient algorithms for the manipulation of data,
3. Why do we need data structures?
4 Data structures allow us to achieve an important goal: component reuse
% Once each data structure has been implemented once, It can be used over and over again
In various applications.
4, Simple Classification of Data Structure.
The data structure can be classified Into two types as follows:
a) Linear Data Structures ~ All the elements are formed in a sequence or maintain a
linear ordering
i. Arrays
Hi. Linked Lists
iii, Stacks
lv. Queues
b) Non-Linear Data Structures - All the elements are distributed on a plane i.e. these
have no such sequence of elements as in case of linear data structure,
1. Trees
li, Graphs
li, Sets
5. List the operations performed in the Linear Data Structure
a) Traversal - Processing each element in the list
b) Search - Finding the location of the element with a given value,
©) Insertion / Storing ~ Adding a new element to the list.
d) Deletion - Removing an element from the list.
€) Sorting Arranging the cle ments In some type of order.
f) Merging ~ Combining two lists into a single list.
6. Explain Linked List
4 A linked list is a list of elements in which the elements of the list can be placed
anywhere in memory, and these elements are linked with each other using an explicit
link field, that is by storing the address of the next element in the link field of the
previous element.
@ Einstein College Of Engineering [34/189]FE36-Data Structure and Algorithms IEEE
A linked list is @ self-referential date type because it contains a pointer or link to
another data of the same type. This permit insertion and removal of nodes at any point
in the list in constant time, but do not allow random access,
What is a node?
Each element structure in a slinked list called node, containing two fields one is data and
another Is address of next data.
Advantages of Linked List
5. Linked List is dynamic data structure; the size of a list can grow or shrink during the
DATA [ ADDRESS
program execution. So, maximum size need not be known in advance.
6. The Linked List does not waste memory
7. Itis not necessary to specify the size of the list, as in the case of arrays.
8. Linked List provides the flexibility in allowing the items to be rearranged.
What are the pitfalls encountered in single linked list?
4. A singly linked list allows traversal of the list in only one direction.
5. Deleting a node from a list requires keeping track of the previous node, that is, the node
whose link points to the node to be deleted.
6. Ifthe link in any node gets corrupted, the remaining nodes of the list become unusable.
10.Define Stack
4 Stack io a lincar data structure and Is an ordered collection of homogeneous data
‘elements, where the insertion and deletion operations takes place at one end called top
of the stack.
4 A stack data structure exhibits the LIFO (Last In First Out) property.
411.What are operations allowed in a Stack?
4. PUSH : This operation Is used to add an Item into the top of the stack,
2. POP: This operation is used to remove an Item from the top of the stack.
3. PEEK : This operation is used to display the top item in the stack.
412.List the notations used to represent the arithmetic expressions.
1. Inf operator
Ex A+B
2. Prefix: operator
(2130 called as polish notation) Ex: +AD
3. Postfix: operator
Ex: ABS
13.Rules for converting an Infix notation to postfix form
1. Assume, the fully parenthesized version of the Infix expression
2. Move all operator, so that they replace their corresponding right part of parenthesis
3. Remove all parenthesis
@ Einstein College Of Engineering [35/159]FE36-Data Structure and Algorithms IEEE
Example: ((A+((B*C)-D))*(E-(A/C))) > ABCD-+EAC/-*
14,Define Queue
Queue Is an ordered collection of homogeneous data elements, in which the element insertion
and deletion takes place at two ends called front and rear. The elements are ordered In linear
fashion and inserted at REAR end and deleted FRONT end. This exhibits FIFO (First In First
Out) property.
15.Applications of Queue
Applications of queue as a data structure are more common.
a) Within a computer system there may be queues of tasks waiting for the line printer, or for
access to disk storage, or in a time-sharing system for use of the CPU,
b) Within a single program, there may be multiple requests to be kept in a queue, or one task
may create other tasks, which must be done in turn by keeping them in a queue.
16.What is the need of Circular Queue?
> Queue implemented using an array suffers from one limitation i.e. there is a possibility that
the queue is reported as full (since rear has reached the end of the array), even though in
actuality there might be empty slots at the beginning of the queue. To overcome this
limitation circular queue is needed.
» Now the queue would be reported as full only when all the slots in the array stand
occupied.
17.What is deque?
4 The word deque Is a short form of double-ended queue and defines a data structure
in which items can be added or deleted at either the front or rear end, but no changes
‘can be made elsewhere in the list.
4 Thus a deque is a generalization of both a stack and a queue.
18.Types of Linked Lists:
a) Linear Singly Linked List
b) Circular Linked List
) Two-way or doubly linked lists
4) Circular doubly linked lists
19.What are the Applications of linked list?
a) Implementation of Stack
b) Implementation of Queue
)_ Implementation of Tree
4d) Implementation of Graph
20.Applications of Stack
a) It is very useful to evaluate arithmetic expressions. (Postfix Expressions)
b) Infix to Postfix Transformation
@ Einstein College Of Engineering [36/159]FE36-Data Structure and Algorithms IEEE
©) Itis useful during the execution of recursive programs
d) A Stack Is useful for designing the compiler in operating system to store local variables,
Inside a function block.
) A stack can be used in function calls including recursion.
f) Reversing Data
g) Reverse a List
hh) Convert Decimal to Binary
1) Parsing - It is a logic that breaks into Independent pieces for further processing
J) Backtracking
UNIT IT: TREE STRUCTURES
Need for non-linear structures - Tree ADT ~ tree traversals - left child right sibling data structures
for general trees ~ Binary Tree ADT ~ expression trees ~ applications of trees — binary search tree
ADT
unit 11
TREES
@ Einstein College Of Engineering [37/189]FE36-Data Structure and Algorithms IEEE
2.4 The ADT tree
A tree Is a finite set of elements or nodes. If the set is non-empty, one of the nodes is
distinguished as the root node, while the remaining (possibly empty) set of nodes are grouped
into subsets, each of which is itself a tree, This hlerarchical relationship is described by referring
to each such subtree as a child of the root, while the root is referred to as the parent of each
subtree. If a tree consists of a single node, that node Is called a leaf node.
(i)
o 0 0
FOOWO
Assimple tree,
2.4.4 Basle Tee Concepls
4 A tree consists of a finite set of elements, called ‘nodes’, and a finite set of directed
lines, called ‘branches’, that connect the nodes.
4 The number of branches associated with a node is the degree of the node.
When the branch Is directed toward a node, it Is an indegree branch; when the
branch is directed away from the node, it is an outdegree branch.
© The sum of indegree and outdegree branches is the degree of the node.
© The indegree of the root is by definition is zero.
A leaf \s any node with an outdegree of zero.
4 Anode that is not a root or a leaf is known as an internal node.
Anode is a parent if it has successor nodes - that Is, if it has an outdegree greater
than zero,
Conversely, a node with a predecessor is a child. A child node has an indegree of one.
‘Two or more nodes with the same parent are siblings.FE36-Data Structure and Algorithms IEEE
Parents : ABCG
Children : BFG,C.H,LID,E
Siblings : €B,F,G}, {DE}, CH 13)
Leaves : FDEH LI
Length 4
A path Is a sequence of nodes In which each node Is adjacent to the next one.
% Every node in the tree can be reached by following a unique path starting from the root.
The level of a node is its distance from the root. Because the root has a zero distance
from itself, the root is at level 0, The children of the root are at the level 1.
The height or length of the tree is the level of the leaf in the longest path from the root
plus 1. By definition, the height of an empty tree Is -1.
A tree may be divided into subtrees. A subtree is any connected structure below the
root,
The first node in a subtree Is known as the root of the subtree and Is used to name the
subtree,
Binary Trees
@ A binary tree is a tree in which no node can have more than two subtrees,
% ‘These subtrees are designated as the left subtree and right subtree.
Each subtree is a binary tree itself
Right Subiree
The height of the j trees can be mfithematically predicted. The maximum height
of the binary tree which has N nodes, Hinax = N-
A tree with a maximum height Is rare. Tt occurs when the entire tree Is bullt in one
direction. The minimum height of the tree, Hmin is determined by,
Hmin =| Log, N | +1
@ Einstein College Of Engineering [39/159]FE36-Data Structure and Algorithms IEEE
+
Given a height of the binary tree, H, the minimum and maximum number of nodes in the
tree are given as, Nein =H and,
Novas = 2-4
If the height of the tree is less, then itis easier to locate any desired node in the tree.
To determine whether tree is balanced, the balance factor should be calculated.
If Hy represents the height of the left subtree and Ha represents the height of the right
subtree then Balance factor. B = Hi ~ Ha
A tree is balanced if its balance factor is and its subtrees are also balanced.
A binary tree is balanced if the height of its subtrees differs by no more than one and its
subtrees are also balanced.
A complete tree has the maximum number of entries for Its height.
The maximum number is reached when the least level Is full. The maximum number is
reached when the last level is full
©
Complete Trees (at levels 0, 1, and 2)
A tree Is considered nearly complete If it has the minimum height for its nodes and all
nodes in the last level are found on the left.
Nearly Complete Trees (at level 2)
2.4.2 Binary Tree Representation
A binary tree is a tree which is either empty, or one in which every node:
hhas no children; or
has just a left child; or
hhas Just a right child; or
has both a left and a right child
@ Einstein College Of Engineering [40/159]FE36-Data Structure and Algorithms IEEE
‘A Complete binary tree of depth K is a binary tree of depth K having 2k ~ 1 nodes.
Level 1
Level 2
Level 3
A very simple representation for such binary tree results from sequentially numberina the
nodes, starting with nodes on level 4 then those on level 2 and so on, Nodes on any level are
numbered from left to right as shown in the above picture. This numbering scheme gives us the
definition of a complete binary tree. A binary tree with n nodes and of depth K Is complete If its
nodes correspond to the nodes which are numbered one to n in the full binary tree of depth K.
Array Representation:
Each node contains Info, left, right and father fields, The left, right and father fields of a
node point to the node's left son, right son and father respectively.
Using the array implementation, we may declare
+#define NUMNODES 100
struct nodetype
{
int info:
Int left;
Int right;
int father;
he
struct nodetype node[NUMNODES];
This representation Is called linked array representation.
Under this representation,
Info(p) would be implemented by reference node[p].info,
left(p) would be implemented by reference node[p].left,
Fight(p) would be implemented by reference node[p].right,
father(p) would be implemented by reference _node[p].father respectively.
The operations,
isleft(p) can be implemented in terms of the operation left(p)
isright(p) can be implemented in terms of the operation right(p)
Example: -
@ Einstein (_Jege Of Engineering [41/189]EE36-Data Structure and Algorithms EEE
Fig (a) Fig (b)
‘The above trees can be represented In memory sequentially as follows
A
B
c
E
The above representation appears to be good for complete binary trees and
wastetul tor many other binary trees. In addition, the insertion or deletion of nodes from the
middle of a tree requires the insertion of many nodes to reflect the change in level number of
these nodes.
Linked Representation: -
‘The problems of sequential representation can be easily overcome through the use of a
linked representation. Each node will have three fields LCHILD, DATA and RCHILD as represented
below
LCHILD DATA RCHILD
@ Einstein College Of Engineering [42/189]EE36-Data Structure and Algorithms EEE
] I LE
Y ]
Fia (a) Fia (b)
In most applications it s adequate. But this structure make it difficult to determine
the parent ofa node since ths leads only to the forward movernent of the links,
Using the linked implementation, we may decare,
stuct nodetype
¢
int info;
struct nodetype *left;
struct nodetype *right;
struct nodetype *father;
d;
typedef struct nodetype *NODEPTR;
‘hie reprecentation lo called dynamic node representation. Under the reprecentation,
info(p) would be implemented by reference p>info,
left(p) would be implemented by reference pleft,
right(p) would be implemented by reference p->right,
father(p) would be implemented by reference p> father
Primitive Operation On Binarv Trees
(4) maketreeQ function
Which allocates a node and sets it as the root of a single-node binary tree, may be written
as follows;
NODEPTR maketree()
int x;
(
NODEMTR pi
P = getnode(); /* getnode() function get a available node */
pinto = x;
pleft = NULL;
p>right=NULL;
return(e):
3
@ Einstein College Of Engineering 43/159]FE36-Data Structure and Algorithms IEEE
(2) setleft(p.x) function Which sets a node with contents x as the left son of node(p)
setleft(p,x)
NODEPTR
int x;
{
if{(p = = NULL)
printf(*insertion not made"):
else if ( p>Heft ! = NULL)
printf("invalid insertion ");
else
poleft = maketree (x);
+
Conversion of a General Tree to Binary Tree
General Tree:
A General Tree is a tree in which each node can have an unlimited out degree.
Each node may have as many children as is necessary to satisfy Its requirements.
Example: Directory Structure
a
CTO!
S COO
Its considered easy to represent binary trees in programs than It is to represent general
trees. So, the general trees can be represented in binary tree format.
‘Changing general tree to Binary tree:
The binary tree format can be adopted by changing the meaning of the left and right
pointers. There are two relationships in binary tree,
© Parent to child
© Sibling to sibling
Using these relationships, the general tree can be implemented as binary tree.
Algorithen
1. Identify the branch from the parent to its first or leftmost child. These branches from each
parent become left pointers in the binary tree
2. Connect siblings, starting with the leftmost child, using @ branch for each sibling to its right
sibling
3. Remove all unconnected branches from the parent to its childrenFE36-Data Structure and Algorithms IEEE
(a) General Tree
Seo J
Step 1: Identify all leftmost children
(B) THE RESULTING BINARY TREE
2.1.3 Binary Tree Traversals
A binary tree traversal requires that each node of the tree be processed once and only
conce in a predetermined sequence.
% The two general approaches to the traversal sequence are,
© Depth first traversal
© Breadth first traversal
In depth first traversal, the processing proceeds along a path from the root through one
child to the most distant descendent of that first child before processing a second child.
In other words, in the depth first traversal, all the descendants of a child are processed
before going to the next child.
Ina breadth-first traversal, the processing proceeds horizontally form the root to all its
children, then to its children’s children, and so forth until all nodes have been processed.
In other words, in breadth traversal, each level is completely processed before the next
level is started.
a) Depth-First Traversal
@ Einstein College Of Engineering 45/159]FE36-Data Structure and Algorithms
IEEE
‘There are basically three ways of binary tree traversals. They are:
1, Pre Order Traversal
2. In Order Traversal
3, Post Order Traversal
In C, each node is defined as a structure of the following form:
struct node
4
int info;
struct node *Ichild;
struct node *rchild;
t
typedef struct node NODE;
Binary Tree Traversals ( Recursive procedure )
1. Inorder Traversal
Steps: 1, Traverse left subtree in inorder
2. Process root node
3. Traverse right subtree in inorder
Algorithm
Algorithm inoder traversal (Bin-Tree T)
Begin
If ( not empty (T) ) then
Begin
Inorder_traversal ( left subtree (T) )
Print ( info(T)) / * process node */
Inorder_traversal ( right subtree ( T) )
End
End
Coding
veld inorder traversal ( NODE * T)
{
f(T | = NULL)
{
inorder_traversal(T->Ichild);
Printf("%d \t", T->info);
Inorder_traversal(T->rchild);
3FE36-Data Structure and Algorithms IEEE
The Output is: C3 BSD 3ASE DSF
2. Preorder Traversal
Steps: 1. Process root node
2. Traverse left subtree in preorder
3. Traverse right subtree In preorder
Algorithm function
Void preorder_traversal ( NODE * T)
{
Algorithm preorder traversal (Bin-Tree T) f(T! = NULL)
{
Begin printf(“4ed \t", T->info);
If ( not empty (T) ) then preorder traversal(T->Ichild);
Begin preorder_traversal(T->rehild);
Print ( info (T))/ * process node * / }
Preoder traversal (left subtree (T ) ) 3
Tnorder traversal ( right subtree ( T ) )
End
End
Steps : 1. Traverse left subtree in postorder
2. Traverse right subtree in postorder
3. process root node
Algorithm function
@ Einstein College Of Engineering [47/189]FE36-Data Structure and Algorithms IEEE
Postorder Traversal Void postorder_traversal ( NODE * T)
{
Algorithm postorder traversal (Bin-Tree T) f(T! = NULL)
t
Begin postorder_traversal(T->Ichild);
If ( not empty (T) ) then postorder_traversal(T->rehile);
Begin brintf(“9d \t", T->info):
Postorder_traversal ( left subtree (T ) ) }
Postorder_traversal ( right subtree T)) }
Print ( Info (T)) / * process node */
End
End
The Output is: C 3D SBSFIEDA
Non - Recursive algorithm: Inorder_Traversal
define MAXSTACK 100
Inorder_traversal (tree)
NODEPTR tree;
{
struct stack
{
Int top;
NODEPTR Item[MAXSTACK];
isi
NODEPTR p;
stop = -1;
p= tree;
do
{/* travel down left branches as far as possible, saving
@ Einstein College Of Engineering [48/159]FE36-Data Structure and Algorithms IEEE
pointers to nodes passed */
while(p!=NULL)
{
push(s,p);
p= p> lett;
+
1 check if finished */
if ( tempty (s) )
{ | * atthis point the left subtree is empty */
p=pop(s);
printf("%d \n”, p>info); /* visit the root */
=p>right; /* traverse right subtree */
+
}while( lempty (s) || p! = NULL);
Non - Recursive algorithm: Preorder_Traversal
define MAXSTACK 100
preorder traversal (tree)
{
NODEPTR tree;
{
struct stack
q
Int top;
NODEPTR item[MAXSTACK];
3s;
NODEPTR p;
s.top = -1;
p= tree;
do
{
/* travel down left branches as far as possible, saving
pointers to nodes passed */
If(p!=NULL)
{
@ Einstein College Of Engineering 49/189]FE36-Data Structure and Algorithms IEEE
printf("9%6d \n", p>info); /* visit the root */
If(p>right!=NULL)
Push(s,p>right); _/* push the right subtree on to the stack */
p=pleft;,
+
else
p=pon(s):
Dwhile( ! empty(s) |] pt
}
2.2 Binary Search Tree
Binary tree that all elements in the left subtree of a node n are less than
the contents of n, and all elements in the right subtree of n are greater than or equal to
the contents of n.
Uses : used in sorting and searching
Figure Binary search tree
Applications of Binary Trees (find the duplication element from the list)
Binary tree is useful data structure when two-way decisions must be made at each point in
‘a process. ror example find the all duplicates in a list uf numbers, One way doing Unis is eae
number compare with it’s precede it. However, it involves a large number of comparisons.
‘The number of comparisons can be reduced by using a binaty tree.
Step 1: from root, each successive number in the list Is then compared to
the number in the root.
Step 2: If it matches, we have a duplicate.
Step 3: If it is smaller, we examine the left subtree.
@ Einstein College Of Engineering [50/159]FE36-Data Structure and Algorithms IEEE
Step 4: If itis larger, we examine the right subtree.
Step 5: If the subtree Is empty, the number is not a duplicate and is placed
Into 2 new node at that passion in the tree,
Step 6: If the subtree is nonempty, we compare the number of the contents
of root of the subtree and entire process is repeated till all node
completed.
(/* read the first number and insert it into a sinlae-node binary tree */
scanf("Y%d" &number);
tree = maketree (number);
while(there are numbers left In the input)
{
scanf("%d", &number);
= tree;
p=q
while (number | = Info(p) && q ! = NULL)
{
a
If (number < info (p) )
a= left (p);
cle
= right (p);
+
if(number = = info(p) )
printf(* 9d %s", number, “is a duplicate”);
else if ( number < info(p) )
setleft( p, number );
else
setright(p, number);
+
(2) Application of Blanry Tree - ( Sort the number in Ascending Order)
If a binary search tree Is traversed in Inorder(left,root,right) and the contents of each node
are printed as the node Is visited, the numbers are printed in accending order.
@ Einstein College Of Engineering 51/189]FE36-Data Structure and Algorithms IEEE
Figure Bory ech
For convince the above binary search tree if it is traversed Inorder manner the result order Is,
30, 46, 50, 58, 60, 70, 77 and 80 Is ascending order sequence.
(3) Application Binary Tree ~ ( Expression Tree)
Representing an expression containing operands and binary operators by a strictly binary
tree, The root of the strictly binary tree contains an operator that Is to be applied to the results of
evaluating the expressions represented by the left and right subtrees, A node representing an
operator is @ nonleaf, whereas a node representing an operand in a leaf.
BINARY SEARCH TREE OPERATIONS:
‘The basic operation on 2 binary search tree(BST) include, creating @ BST, inserting an
‘element into BST, deleting an element from BST, searching an element and printing element of
BST in ascending order.
‘The ADT specification of a BST:
ADT BST
{
Create BST() + Create an empty BST;
Insert(elt) : Insert elt into the BST;
Search(elt,x) 1 Search for the presence of element elt and
Set x=elt, return true if elt Is found, else
return false.
FindMin() 2 Find minimum element;
FindMax() : Find maximum element;
Ordered Output() + Output elements of BST in ascending order;
@ Einstein College Of Engineering [52/189]FE36-Data Structure and Algorithms IEEE
Delete(ett.x) 2 Delete elt and set x = elt;
+
Inserting an element into Binary Search Tree
Algorithm InsertBST(int elt, NODE *T)
[elt is the element to be inserted and T is the pointer to the root of the tree]
If (T= = NULL) then
Create a one-node tree and return
Else if (eltIchild)
Else if(ett>key) then
InsertBST(elt, T-rchild)
Else
* element is already exist
return T
End
€ coding to Insert element into a BST
struct node
{
int infos
struct node *Ichild;
struct node *rchil
ui
typedef stuct node NODE;
NODE *InsertaST(int elt, NODE *T)
{
I(T = = NULL)
{
T = (NODE *)malloc(sizeof(NODE));
if(T = = NULL)
printf (" No memory error”);
olse
{
info = elt;
t3Ichild = NULL;
torchild = NULL;
+
t
@ Einstein College Of Engineering [53/159]FE36-Data Structure and Algorithms IEEE
else if ( elt < Tinfo)
tIchild = InsertasT(elt, t>Ichild);
else if ( elt > T>info)
trchild = InsertBST( elt, t>rchild);
return T;
+
‘Searching an element in BST
Searching an element in BST is similar to insertion operation, but they only return the
pointer to the node that contains the key value or if element is not, a NULL Is returt
Searching start from the root of the tree;
If the search key value Is less than that In root, then the search Is left subtree;
If the search key value Is greater than that in root, then the search is right subtree;
This searching should continue till the node with the search key value or null pointer(end of
the branch) is reached.
In case null pointer(null left/righ chile) is reached, it is an indication of the absence of the
node.
Algorithm SearchBST(int elt, NODE *T)
[elt Is the clement to be inserted and T Is the pointer to the root of the tree]
1, If (T= = NULL) then
Return NULL
2. If (eltkey) then /* elt is greater than the key in root */
return SerachBST(elt, T>rchild)
Else
return T
End
NODE * SearchBST(int elt, NODE *T)
{
f(T = = NULL)
return NULL;
If ( elt < T>info)
return SearchBST(elt, t->Ichild);
else if ( elt > Tinfo)
return SearchBST( elt, t>rchild);
@ Einstein College Of Engineering [54/159]FE36-Data Structure and Algorithms IEEE
return T;
Finding Minimum Element in a BST.
Minimum element lies as the left most node in the left most branch starting from the root.
To reach the node with minimum value, we need to traverse the tree from root along the left
branch til we get a node with a null / empty left subtree.
Algorithm FindMin(NODE * T)
1. If Tree Is null then
return NULL;
2. If Ichild(Tree) is null then
return tree
else
return FindMin(T-Ichild)
3. End
NODE * FindMin( NODE *T )
{
f(T = = NULL)
return NULL;
If (TIchild = = NULL)
return tree;
else
return FindMin(Tree>Ichild);
t
Finding Maximum Element ina BST
Maximum element lies as the right most node in the right most branch starting from the
root. To reach the node with maximum value, we need to traverse the tree from root along the
right branch till we get a node with a null / empty right subtree.
Algorithm FrindMax(NODE * T)
1. If Tree is null then
return NULL;
2. If rchild(Tree) Is null then
return tree
else
return FindMax(T->rchild)
@ Einstein College Of Engineering [55/159]FE36-Data Structure and Algorithms IEEE
3. End
NODE * FindMax( NODE *T )
{
if(T = = NULL)
return NULL;
If ( Trchild = = NULL)
return tree;
else
return FindMax(Tree->rchild);
DELETING AN ELEMENT IN A BST
‘The node to be deleted can fall into any one of the following categories;
1. Node may not have any children (ie, its a leaf node)
2. Node may have only on child ( either left / right child)
3. Node may have two children ( both left and right)
Algorithm DelcteBST(int elt, NODE * T)
4, If Tree is null then
print “Element is not found”
2. If elt is less than info(Tree) then
locate element in left subtree and delete it
else if elt is greater than info(Tree) then
locate element in right subtree and delete it
else if (both left and right child are not NULL) then /* node with two children */
begin
Locate minimum element in the right subtree
Replace elt by this value
Delete min element in right subtree and move the remaining tree as its
right child
end
else
If leftsubtree Is Null then
/* has only right subtree or both subtree Null */
replace node by its rchild
else
@ Einstein College Of Engineering [56/159]FE36-Data Structure and Algorithms IEEE
if right subtree is Null then
replace node by its left chila\
end
free memory allocated to min node
end
return Tree
End
NODE *DeleteBST(int elt, NODE * T)
{
NODE * minelt;
I(T = = NULL)
printf(” element not found \n");
else If ( elt < T>info)
TIchild = DeleteBST(elt, T>Ichild);
else if ( elt > Tinfo)
Torchild = DeleteBsT(elt, Trchild);
else
I(T child && T > rchild)
{
‘min€it = FindMin(T>rchild);
TSInfo = minélt info;
‘Torchild = DeleteBST(T> info, T>rchild);
+
else
{
minelt = Tree;
if (T>tchila
T
else if ( T>rchild
T= Tlehild;
Free (minEit);
+
return T;
+
@ Einstein College Of Engineering [57/159]FE36-Data Structure and Algorithms IEEE
2.3 Expression tree:
Expression Tree Is a binary tree in which the leaf nodes are operands and the interior nodes are
operators. Like binary tree, expression tree can also be travesed by inorder, preorder and postorder traversa
For ex. Expression tree for a+b¥c Is as follows:
2.3.1 Algorithm for Constructing an Expression Tree with an example:
Let us consider postfix expression given as an input for constructing an expression tree by
performing the following steps:
1. Read one symbol at a time from the postfix expression.
2. Check whether the symbol is an operand or operator.
(2) If the symbol is an operand, create a one - node tree and push a pointer on to the stack
(b) If the symbol Is an operator pop two pointers from the stack namely T, and Ts and form a new
‘ree with root as the operator and Tz as a let child and T; as @ right child. A pointer to this new
tree Is then pushed onto the stack
Comision Uae agua, leper
Step 4:
The first two symbols are operands, so create one-node tree and push pointers of them onto a stack.
©
Next symbol Is read, Le “+” operator. Now the symbol is operator, so two previous pointers to trees
in the stack are popped, and 2 new tree is formed and its pointer is pushed on to the stack. The popped
Step 2:
pointers are placed as left child and right child in the new tree, as a result two subtrees are merged,
@ Einstein College Of Engineering [58/159]FE36-Data Structure and Algorithms IEEE
Step 3:
Next symbols are operands Le., ¢,d,and e, they are read. For each operand create one-node tree
‘and push pointers of the corresponding tree onto the stack,
| titi
Step 4:
Next symbol Is read, Le “+” operator. Now the symbol is operator, so two previous pointers to trees
In the stack are popped, and 4 new tree Is formed and its pointer Is pushed on to the stack, The popped
pointers are placed as left child and right child in the now tree, as a result two subtrees are merged.
[i \
OQ |
bo
OO OO
Next symbol Is read, Le “*' operator. The symbol Is operator, so two previous pointers to trees in the stack
are popped, and a new tree is formed and its pointer Is pushed on to the stack. The popped pointers are
placed as left child and right child In the new tree, as a result two subtrees are merged
fy,
@ Einstein College Of Engineering [59/159]FE36-Data Structure and Algorithms IEEE
Step 6:
Final symbol is read, Le ‘*’ operator. The symbol Is operator, so two previous pointers to trees in the
stack are popped, and a new tree Is formed and Its pointer Is pushed on to the stack. The popped pointers
are placed as left child and right child in the new tree, as a result two subtrees are merged. So a pointer to
the final tree is left on the stack.
[Atlast we made an expression tree, In this tree we can make In-order , pre-order and post- order traversal
like @ binary tree.
2.3.2 Algorithm & Implementation of Expression Tree
Algorithm:
1. Start the program:
2. Create two stacks.
a. Operator stack- for operators.
b. Operand Stack- for tree node built so far
3. From the infix Expression, Process one token at a time.
4. Each time we see an operand, we create 2 new SinaryNode for the operand, and push pointer to the
operand stack.
5. Each time we see an operator, compare the precedence of this input operator with the precedence of
operator in top of operator stack. If the Precedence is high, push the input operator on to the stack.
Otherwise pop the operators from the until the stack operators precedence is low.
66. When we pop an operator from the operator stack, we create a new BinaryNode for it and we pop two
‘operands from the operand stack, we pop the first one and make it the right child of a operator node
‘and pop the second one and make it the left child. Then we push the operator node pointer onto the
operand stack.
7. At the end, the tree node on the top of the operand stack Is the root of the expression tree for the
siven infix expression,
8. For in order traversal
@ Einstein College Of Engineering [60/159]FE36-Data Structure and Algorithms IEEE
a. Visit Left sub tree
b. Visit Root node
c. Visit Right sub tree,
9. For preorder traversal
a. Visit Root node
b. Visit Left sub tree
Visit Right sub tree
10. For post onder traverse
2. Visit Right sub tree
b. Visit Left sub tree
c. Visit Root nade.
11. Stop the program,
2.3.2 Implementation of Expression tree and make in-order, pre-order and post-order traversal
#include
Struct tree
{
char info;
struct tree *rchild;
struct tree "child;
h
int pree(char data);
typeder struct tree * node;
‘char pop_op():
node pop_num();
void push_op(char item);
node create()
)__Tetuennodeymatotstzerinode);
inode num[20],root=NULL;
char op[20},oprt,ev{20];
Int nt=-1,0t=-1,et=-1;
main()
{
node newnode,tem,temp;
char str{50];
Int ik,p,5,flag=0;
printi("ENTER THE EXPRESSION ");
scanf(%s", str);
printt(\rica" tr)
Forti=0;str[Ii="\Oii++)
{
(salnum(ste[t))
newnode=create();
hewnode->info=st{i];
ewnode->lehild=NULL;
newnode->rchild=NULL;
item=newnode;
push_nurn(item);
@ Einstein College Of Engineering [61/159]FE36-Data Structure and Algorithms IEEE
else
t
Ineott=-1)
p=precoplot]);
fee(strtH)
5)
while(k!=1)
{
oprt=pop_op();
newnode-createl);
ewnode->In
nevmode->rchi
rewnode-> Ichi
11 Mf(root--NULL)
root=newnode:
/[ else if{(newnode->rchild==root)| | (newnede->lehild
1/ root=newnode;
ush_num(root);
k=prec(oplot]);
y
oprt—pop on);
else if(k==1)
push. op( sti
{
ifck>p)
uch onto;
{
Iiéke=p)
{
coprt=pop_op(),
newnode=create();
root=newnade;
else If(newnode->rchikl==root)] (newnade->Ichil
root=newnode;
push_num(nownode);
ush_op(stii]):
11 k=precoplotl)s
y
x
,
?
)
printi("\nThe prefix expression is\n ");
preorder(root);
printf("\nThe infix exp s\n"),
Inorder(roat);
printf("\nThe postfix expression Is\n
postorder(root);
evaluate();
+
void push_op(char item)
@ Einstein College Of Engineering [62/159]FE36-Data Structure and Algorithms IEEE
opl++ot]=item;
+
ush_num(node item)
{
umt-+-Fat
+
char pop_op()
in(ott=-1),
returnoptot-D;
return(0);
y
node pop_num()
{
ii(nt=-1)
retuen(num[nt-);
else
return(NULL);
y
int pree(char data)
switch (data)
{
case '(sreturn(1);
break;
return(2);
break;
case '/:return(3);
break;
case 'A'sreturn(4);
break;
case 'Y:return(5);
break;
+
inorder
{
rode temp)
If(temp!=NULL)
{
Inorder(temp->ichild);
printt("c ",temp->info);
Inorder(temp->rehild);
+
preorder(node temp)
Hf(temp!=NULL)
{
printf ("9% *,temp->info);
preorder(temp->Ichild);
preorder(temp->rchild);
y
@ Einstein College Of Engineering [63/159]FE36-Data Structure and Algorithms
IEEE
postorder(node temp)
Hf(temp!=NULL)
{ postorder(terp->lhl;
postorder(temp->rehild);
printt("¥oc *,temp->info)
ev[+#et]=temp->info;
y
+
cevaluate()
{
Int iJ=-1,,b,ch(20];
for(t=0;evUI}!=\0's14-+)
{
'#(lsainum(evtiy))
ch{++il=evLi)-48;
else
«
b=chtth
anchi-1;
switch(evtl)
‘
se “Hsch[-f]-0 4;
breaks
case “:chl--J]=a-b;
break;
case "*!2ch[—j]=2%b;
break:
case '/ichl--i]=a/b5
break;
+
»
y
printi("\nValue = 9%d",ch{0});
@ Einstein College Of Engineering [64/159]FE36-Data Structure and Algorithms IEEE
UNIT IIT : BALANCED SEARCH TREES AND INDEXING
[AVL trees ~ Binary Heaps ~ B-Tree ~ Hashing ~ Separate chaining ~ open addressing -Linear probing
unrr nr
BALANCED SEARCH TREES&INDEXING
lanced Tree: An empty tree Is height-balanced if Tis a non empty binary tree with T, and Ty as
its left and eight sub trees respectively, then T is a height ~balanced if
1. T and Taare helaht-balancea
2. [hy=hq] ¢ 1 where hy and hy are the heights of T, and Ta respectively
The definition of a helght balanced binary tree requires that every sub tree also be height balanced. The
balance factor of the nodes decides if the tree is balanced or not.
nce Factor: The balance factor BF(T) of a node Tis a binary tree is defined to be
ar
he
where hy and hy respectively are the heights of the left and right sub trees of T.
For any node Tis an AVL tree BF(T) = -1, 0, of 4. IF itis not in among these 3 values then the tree
has to be rebalanced by performing the appropriate rotation
Balanced Search Tree )
‘Adelson ~ Volskil ~and Landis introduced @ binary tree structure that Is balanced with respect to
heights of sub trees. The order of retrieval, Insertion and deletion is only O (log n). This tree structure is
called AVL tree.
Av AVL lies fs a sjnaial Lm: uf binary live that is always “partially” balanced. Ihe criteria that is
used to determine the “level” of "balanced-tree” Is the difference between the heights of sub trees of a root
Inthe tree. The “height of tree is the “number levels" in the tree. To be more formal, the height of a tree is
defined as follows:
1. The helght of a tree with no elements fs 0
2. The height of a tree with 1 element is 1
3. ‘The height of tree with > 1 element is equal to 1 + height ofits tallest sub tree
‘An AVL tree Is a binary tree in which the difference between the height of the right and left sub trees
co the root node is never more than one,
BF
(-0=1)
BF=0
(1-€1)=0)
3.4.4 Insertion of a node:
We can insert a new node Into an AVL tree by first using binary tree insertion algorithm, comparing
the key of the new node with that In the root, and Inserting the new node into the left or right sub tree as
appropriate.
@ Einstein College Of Engineering [65/159]FE36-Data Structure and Algorithms IEEE
It often turns out that the new nade can be inserted without changing the height of a sub tree, in
which case neither the height not the balance of the root will be changed. Even when the height of a sub
tree does increase, it may be the shorter subtree that has grown, so only the balance factor of the root will
change.
3.1.2 Rotations:
When a new node has been inserted into the taller subtree of the root and Its helaht has Increased,
50 thot now one subtree hos height 2 more thon the other, and the tree no longer sotiafiea the AVL
requirements. We must now rebulld part of the tree to restore its balance.
The rebalancing can be carried out using four diferent kinds of rotations: LL, RR, LR & RL. LL and
RR are symmetric as are LR and RL. There rotations are characterized by the nearest ancestor, A, of the
Inserted node, ¥, whose balance factor become + 2. The following rotations type is obtained:
LL: New node ¥ is inserted in the Left Subtree of the Left Subtree of A > Single Rotation with let
LR: Is inserted in the right subtree of the Left Subtree of A > Double Rotation with Left
RR: Vis inserted in the right subtree of the right subtroe of A > Single Rotation with Right
AL: Y Is inserted in the left subtree of the right subtree of A > Double Rotation with Right
Single rotation:
1. Single Rotation with left. - Left Rotation(Left of Left)
> The left child (Kt) of the root (2) is promoted as root node.
>The root (Ka) lo promoted ae the right child of the new root( let.
> The right child of the K1 Is converted to the left child of K2
®
Q& @ ~ 1D
® AN 6S
fore Rotation After Rotation
ingle rotation with left
|L_ This function can be called only if K2 has a left child
|i. Perform a rotation between node (K2) and its left child (Ki)
Routine to perform
Ii Update height, then return new root.
tatic Pocition SingloRotatowithLoft (Pocition K2)
{
Position Kt;
K1-k2>Ieft;
K29Left = KLSRight;
K19Right =K2;
K2-Sheight= Max (height(K2Ieft), height (K23RIght))+1;
Ki Sheight= Max (height(K1>left), (K2->height))+1;
@ Einstein College Of Engineering [66/159]FE36-Data Structure and Algorithms IEEE
return K1;/*New Root*/
y
Example;
Consider the following tree which is balanced.
Balanced Tree
Now insert the value ‘t" it becomes unbalanced
due to insertion of the new node in the Left Subtree
of the Left Subtree. So we have to make single left rotation
in the root node.
‘After Left Rotation
le Rotation with Right - Right Rotation(Right of Right)
The right child( 2) of the root(K1) Is promoted as root node.
1s(K2) left child(Y) is converted to right child of the previous root node(k1)
> The previous root nade(K1) is considered as the left child of new root node(K2).
(Algorithm) Routine to Perform Single Rotation with Right:
|. This function can be called only If KI has a right child.
|i, Perform a rotation between node (Kt) and its right child (K2)
il, Update height, then return new root
©
Q ®
© ZX
Before Rotation After Rotation
static Position SingleRotatewithRight (Position K1)
{
Position 2;
@ Einstein College Of Engineering [67/159]FE36-Data Structure and Algorithms IEEE
K2=K19Right ;
KISRIght = K2>!eMt;
K2Ieft =K1;
K2->height= Max (height(k21eft), height (K2Right))+1;
Ki >height= Max (helght(K-Heft), (K->hefght))+1;
return K2;
Now insert the value "13" It becomes unbalanced
due to the insertion of new node In the Right Subtree Before Right Rotation
of the Right Subtree. So we have to make single Right rotation
In the root nade.
After Right Rotation
Double Rotation:
1. Double Rotation with Left (Insertion in the Right Subtree of the Left Subtree > Left-Right Rotation)
_ ©) ©
AA BS A> & ‘a
J 2 CG >
Ad KX L0 2\/\ L\
Before Rotation After Right Rotation After Left Rotation
@ Einstein College Of Engineering [68/159]FE36-Data Structure and Algorithms IEEE
Routine to perform double rotation with right and then Left
L Apply single Right rotation at ky, as a result Ke becomes the left child of the root node Ks and Ky
becomes the left child of Ks,
|. Apply single Left rotation at the root node Ks, a8 a result Ky becomes the root node and Ki
becomes the left child of it and K, becomes right child of it. Now the tree Is balanced.
static Position DoubleRotateWithLeft (Position K3)
{
/* Rotate between Iki ond K2*/
Ky 2Left = SingleRotatewithright (Ky3Left);
(P* Rotate between K2 and K3*/
return SingleRotatewithtett (K3);
y
Example:
Consider the following tree which Is balanced.
ar-o(
balanced Tree © Baro
tow seit he value '12'8 18 the tree becomes unbobnced _—_—Bafore Right Rotation
{due to the insertion of the new node in the Right Subtree
of the Left Subtree. So we have to make double rotation
first with right and then with Left
Aftar Right Rotation & Refare aft Rotation After Left Rotation
2. Double Rotation with Right(Insertion in the Left Subtree of the Right Subtree MRight-Left Rotation)
Routine to perform double rotation with right and then left
Apply single Left rotation at K, as a result Ke becomes the right child of the root node Ki and Ks
becomes the right child of Ke.
|i. Apply single Right rotation at the root node Ki, as a result
becomes the left child of it and Ks becomes right child of it. Now the tree is balanced
becomes the root node and Ks
@ Einstein College Of Engineering [69/159]FE36-Data Structure and Algorithms IEEE
Before Rotation After Left Rotation After Right Rotation
static Position DoubleRotateWithRight (Position K1)
{
F* Rotate between K2 and K3*/
KRight = SingleRotatewithLeft (Ky >RIght);
/* Rotate between K2 and K14/
return SingleRotateWithRight (K1);
y
Example;
Balanced Tree
Now ingert the value ‘11'& "14" the trae becomes unbalanced
due to the insertion of the new node in the Left Subtree
of the Right Subtree. So we have to make double rotation first
with Left and then with Right.
‘After Right Rotation
@ Einstein College Of Engineering 70/159]FE36-Data Structure and Algorithms IEEE
Routine to find height of a node:
static Int height (Position P)
return -1;
return P>height;
y
Declaration for AVL tree
Struct AviNade
{
DataType data;
AviTree Left;
AviTree Right;
int height;
h
Algorithm to insert a node in the AVL Tree:
AulTree Insort (DataType X, AvITree T)
NULL)
Fatalerror(“out of space);
else
{
y
>
else
lf (X data)
4
To Loft = Insert(x,T> Lett);
IF (neight(T->Left) - height(TSRIght)= =2)
<
IF (Xdata)
T-singleRotateWithLeft(T);
else
T=DoubleRotatewithLeR(D);
@ Einstein College Of Engineering 171/189]FE36-Data Structure and Algorithms IEEE
>
else
HoT adata)
{
TO Right=Insert(X,T>RIaht)
iF (neight(T->Right)-height(T Left)
‘
If (> T>Right 3data)
T=SingleRotateWithRiaht(T);
else
T-DoubleRotatawithRight(1);
y
>
felse X is in tree already. We will do nething*/;
T>height =Max(height(T>Lett), helght(T9RIgNt)) +1;
return T;
y
Example:
Let us consider how to balance a tree while inserting the numbers 20, 10, 40, 50, 90, 30, 60, 70.
Stop Ai(tngert the value 20) «Gop 21 (Inert the value 10) Step 3: (Ineart the value 40)
@) BF=0 (20) BF=1 (20) BF=1
@) BF=0 BF=0(0) 9) BF=0
Now the tree is unbalanced due to
‘the insertion of node in the Right
‘subtree of the Right subtree. So
‘we have to make single Right
Now the tree is Balanced
@ Einstein College Of Engineering 172/189]FE36-Data Structure and Algorithms IEEE
[Row the tae is uni
Irseton of node0" nthe Left subtree
ft the Right subtree. So we have ta
‘make Double rotation frst with Lefton
‘the node *50’ and then with Right on
the node 20! i
unbalanced
1 J
due to the insertion of node *60" in the Left
subtree of the Right subtree. So we have to
‘mako Double rotation fst with Left on the
‘node °90' and then with Right on the node
i
Now the tree is Balanced
‘The tree is Balanced
@ Einstein College Of Engineering 73/159]FE36-Data Structure and Algorithms IEEE
3.2 M- ary search tree B- tree
‘An Mary search tree allows. M-way branching. As branching Increases, depth decreases. Where as a
complete binary tree has helght that 1s roughly equal to log:N, a complete M-ary tree has height that is
roughly lon
Example:
jtmost m-1 keys and m
ubtrese
KI 2 Each node consists of
ceceeeeeenneseeen |
Advantages:
There are limitations on size of the collection that can be maintained in the main memory.
When data is maintained in the disk, itis Important to reduce the number of disc access.
Many search tree performs this task, by reducing the number of levels,
3.2.1 B tree:
It is a balanced M-ary search tree. A B-tree of order M Is an M-ary tree, with the following properties are
+ The data items are stored at levels,
+ The non leaf nodes store up to M-1 keys to guide the searching, key I represents the smallest key
in subtree 141
+ The root Is either a leaf or has between 2 and M children.
+ All non leaf nodes (expect the root) have between M/2 level and M children.
+ Allleaves are at the same depth and have between L/2 and L children for some L.
Description about B-tr
‘Each node occupies a disk block.
‘# nterior node stores only the keys which will guide the searching,
MIs determined by size of disk block, size of pointers and number of keys.
‘Leaf nodes are different from interior nodes, Le objects (Data) are stored in leaf nodes only
‘Each leat can have'L’ objects arranged in sorted order, depends on size of object and disk block:
@ Einstein College Of Engineering [74/189]FE36-Data Structure and Algorithms
IEEE
All the leaf nodes can be logically organized as one single data file and all other nodes can be
organized as a single index files.
# Index file provides a hierarchical index into the database file
#8-Tree are mostly used for database implementation
By using B-Troe efficient search can be done
Example: 1 2 3 4
23
pein Tel
23 32] [a6 64 82]
28 38] {so 65 33]
30| 34]
Note:
> Leaf node must have at least 2 objects
> Betree of order 4 is known as 2-2-4 tree.
> Betree of order 3 Is known as 2-3 tree,
> Interior nodes (non leaves) are represented as ellipses, with 2 pleces of data, Dashed '-----' lines
Indicates @ node with only 2 children.
> Leaves are drawn in boxes, which contains the data In ordered way.
22:
16 41:58
[1112 16.17 2.33.3] A152 58.59. 1
Insertion at 2-3 tr
To insert the element find the position of insertion in the leaf level
rnsert 18"
22
16. 41:58
[e112 1617.15] 22.23.31 A S2 58, 59, 61
@ Einstein College Of Engineering 75/159]FE36-Data Structure and Algorithms IEEE
‘The key "18" is added to leaf level without causing any violations.
Insert "1"
When "1's inserted, the leaf node has totally 4 keys. that ts [1 8, 11.12
% This node violates the condition, since any leaf node can
divi
hold only two or three keys, Therefore
the node into two nodes, with 2 keys in each node
Fe
16: 41:58
18 2] ferris) $2233] 152 58,59. 6
Insert "19" 22
41:58
1) fei] D815) 23337 A S2 58. 59. 61
The Internal node P have WTeaf nodes but only 3 children per node is allowed therefore split the nade P is,
Into 2 Internal node with two children.
16:22 >
Bb GAR Bo ee fs) weal
Insert 2
The leaf node E225,28,5i}s 4 keys, which is not allowed. Therefore split into 2 node and introduce a
new internal leaf nodes:
16:22,
1 1s 41-58
DM hike Bs] es a2) Ral
Here the internal nodeCqizse>hes 4 children it violates the rule. So we have to spit into 2 children
@ Einstein College Of Engineering [76/159]FE36-Data Structure and Algorithms IEEE
58,59. 6
Hence the root has 4 internal nodes in the next level, which violates the rule. Therefore divide the root
into 2 nodes which increases the height by 1
22:-
Al
58
i] Pokey By 22.23] Gas) [4is2) 8.59.61
B-Tiee Del
Example: 2-3 tree with [-1. 25
ct)
30] -] 30] —
fips
PS
9 O2G0do © @2@ @
Since P has 3 children‘s(leaf nodes), 2’ can be simply deleted and P be left with just 2 children’s,
3.-
z5
Deletion
revue:
zr
Deletion >
@ @® 4
Loft sibling has least value and the right sibling has greatest value.
% Therefore add '4’to the right sibling
@ Einstein College Of Engineering 177/189]FE36-Data Structure and Algorithms IEEE
-]?= [io EI] a0] = 30] =
TreTererTe
To Delete "10":
Pant horrow from any of its sibling, So, move the child to left sibling and delete P.
25[ -
[y- 40] -
OO® O © & .@ @
To Delete ‘20
% When the node has only one child, borrow from left or right sibling, without violating the condition.
Left sibling has least value and the right sibling has greatest value
% Therefore add ‘25' to the right sibling
invalid
*
[40]50|
borrow from left or right si
Qs) Ga) (0)
% So remove the fast arid make the grant parent as new root.
BD WW@®
Algorithm for Searching an element
Step1: Get the searching element x
‘Step2: Compare x with key of root node
Step3: Find the subtree
@ Einstein College Of Engineering [78/159]FE36-Data Structure and Algorithms IEEE
Stepd: Return the element ifit exists in the subtree otherwise return false.
Suppose there are n objects then the tree will have at least 1+1og3n levels and no more that
| +logan. Therefore any access can be perform in O(log n) times.
‘Application:
Database implementation
> Indexing on non primary key fields are also possible.
3.3 Priority Queue:
3.3.1 Need for priority queu:
© Ina multi user environment, the operating system scheduler must decide which of several processes
to run only for a fixed period for time.
© For that we can use the algorithm of QUEUE, where Jobs are initially placed at the end of the queue,
The scheduler will repeatedly take the first job on the quoue, run it until either it finishes or its time
limit fs up, and placing # at the and of the queue if it doesn’t finish.
% This strategy is generally not approximate, because very short Jobs will soon to take @ long time
because of the wait involved to run.
= Generally, it Is Important that short jobs finish as fast as possible, so these jobs should have
precedence over jobs that have already been running
Further more, some jobs that are not short are stil very important and should also have precedence.
This particular application seems to require a special kind of queue, known as a PRIORITY QUEUE.
3.3.2 Model:
Priority Queue:
Its a collection of ordered elements that provides fast access to the minimum or maximum element
Basic Operations performed by prlority queue are:
4. Insert operation
2. Deletemin operation
Insert operation fs the equivalent of queue’s Enqueue operation,
Deletemin operation Is the priority queue equivalent of the queue's Dequeue operation.
Deltemin(H) Priotity Queve H Insert(H)
3.3.3 Implementation:
There are three ways for implementing priority queue. They are:
1. Linked list
2. Binary Search tree
3. Binary Heap
Linked fi
A simple linked list implementation of priority queue requires (1) time to perform the insertion at
the front and O(N) time to delete the minimum element.
Binary Search Tree
‘% This Implementation glves an average running time of O(log N) for both insertion and deletemin
operations,
@ Einstein College Of Engineering [79/189]FE36-Data Structure and Algorithms IEEE
% Deletemin operation makes tree imbalance, by making the right subtree heavy as we are repeatedly
removing the node from the left subtree
3.3.4 Binary Heap:
3.3.4.1 Properties:
The efficient way of implementing priority queue Is binary heap. Binary heap is merely referred as
heaps. Heaps have two properties namel
1, Structure property
2. Heap order Property
Structure property:
A heap Is binary tree that is fully completely fled, with the possible exception of the bottom level,
hich Is filed from left to right. Such a tree Is known as a complete binary tree.
‘A complete binary tree of height fS]os betwl], 21Shd 2°*1Sloodes (Ke i the tree has height h=2, then
number of nodes it have must be 2 or 3). This implies that the height of complete binary tree is log N which
ie clearly O(loa HD.
‘an array implementation of complete binary tree Is as ‘olows
fo) (1) 2) (3) EG) [6
For any element in array position i
1) the left child is 21-4 1
li). the right child is in the cell after the left child 2142
lil) the parent is in position [(141)/2]
Example:
Ifi=0,
1) the parent node ts [(141)/2] = v8 = 0.5 = app 1 he it refers the root node ‘40
i) the left child Is 21-41 ~ (2*0)#1~1 Let refers the position of the element '20'
iD) the right child Is 2142 = (2*0)+2 = 2 Lt rofers the position of the element ‘60"
wi=4,
1) the parent nade ts [(i+1)/2] = 2/2 = 1 Le it refers the root node "20"
li) the loft child ts 2142 = (2*1)41
iD) the right child Is 2142 = (2*1)+2 = 4 Let rofers the position of the element "30"
Le it refers the position of the element '10"
1) the parent node ts [(1+1)/21 = 3/3
i) the left child fe 2t+1 = (282) 42
1.5 = app 2 Le it refers the root node "60"
Le It refers the position of the element ‘50"
@ Einstein College Of Engineering [80/159]FE36-Data Structure and Algorithms IEEE
lil) the right child is 214
Binary heaps is represented as array it doesn't require pointers and also the operations required to
traverse the tree are extremely simple and fast. But the only disadvantage is to specify the maximum heap
(2*2)+2 = 6 Le It refers the position of the element ‘90°
size in advance.
Heap Order Property
In a Heap, for every node X, the key in the parent of X is smaller than (or equal to) the key in X,
with the exception of the reot{which has no parent).
The Ileap order property varies for the two different types of heaps,
1) Minkeap
2) MaxHeap
In a MinHeap, for every node X, the key in the parent of X Is smaller than (or equal to) the key In X,
with the exception of the root{which has no parent). [figure 1]
a GO),
( (2 0) a)
© O88 ® ®@ OY ©
In a MaxHeap, for every node X, the key in the parent of X Is greater than (or equal to) the key in X,
with the exception of the root(which has no parent). (gure 2]
Routine for the declaration of priority Queue
struct heap
{
Int capacity;
int size;
ElementType *Element;
heap operations
Insertion
To insert an element X into the heap, create a hole in the next available location, since otherwise the
tree will not be complete.
% IF X can be placed in the hole without validating the heap order property, then we can insert
% Otherwice we slide the clement is In the holes parent node in ta the hole, thus bubbling the hole up
towards the root. We continue this property until the element X can be placed in the hole.
To insert "14"
Stept: Create a hole in the next available location, since otherwise the tree will not be complete.
@ Einstein College Of Engineering [81/159]FE36-Data Structure and Algorithms IEEE
a8 O
Step2: Check the Inserting element 14” with the holes parent node ‘31.
® If the inserting element is greater than the holes parent nade element, then the element will be
inserted in to the hole,
4 If the inserting element is lesser than the holes parent nade element (14<31), we have to slide
the holes parent node element in to the hole. Thus, the bubbling the hole up towards the root
happen.
Step3: Repeat the step 2 check the inserting element ‘14’ with the holes parent node '21’
‘If the Inserting oloment Is greater than the holes parent node element, then the element will be
Inserted in to the hole.
If the inserting element is lesser than the holes parent node element (14<21). We have to slide the
holes parent node element in to the hole. Thus the bubbling the hole up towards the root happens.
Step4: Inserting the element "14" In to the hole will not validate heap order Property. Therefore you can
insert the element ‘14’ in to he hole
@ Einstein College Of Engineering [62/159]FE36-Data Structure and Algorithms IEEE
This general strategy Is known as @ perculate up; the new element is perculated up the Heap until
the correct location is found.
Porculation In the Insert routine by performing repeated swaps until the correct order was:
established
The time to do the insertion could be O(lo9 N).
Routine to insert in to a binary heap
void Insert (Element Type X, PriorityQueue H)
t
ints
sr uoeunH))
<
Error (‘priority Queue ts Full");
return;
y
4H size; H9Element [i/2]>%; 1/2)
{
H3Element{i] = H-9element{i/2];
+
H2 Elementi
>
DeleteMin:
Finding the minimum is any, ite hard tm mowe. When the minimum is removed a hole is created at
the root
Since the Heap now becomes one smaller, it follows that the last element X In the Heap must move
somewhere in the Heap.
If X can be placed in the hole, then we are done.
We slide the smaller of the hole's children into the hole, thus pushing the hole dawn one level.
We repeat this step until X can he placed in the hole.
Final
eee
Delete the Inst leaf
@ Einstein College Of Engineering [83/159]Deletemin
The last value cannot be placed in the hole, because this would violate heap order.
% Therefore, we place the smaller child ‘14" in the hole, sliding the hole down one level
Place "24
Routine to DeleteMin in the Binary Heap
Element type deletemin (Priority Queue H)
«
Int I chil;
ElementType MinElement, Lastélement;
Wsempty(+)
<
Error ("PRIORITY QUEUE is empty");
return H9Element{0];
+
Minglement = #3Element{1];
@ Einstein College Of Engineering [84/159]FE36-Data Structure and Algorithms IEEE
LastElement = H->Element{H95ize-
for (IH1;##2<=H-size; child)
4
/*Finding Smaller Child*/
chlld=1*2;
Ii(child!=Hsize S&H-Element{child)>H->Element[child+2])
child +
percolate one level/
If (LastElement>H-3Element{ child];
else
break:
+
H>element[i] = LastElement;
return MinElement;
y
‘Application Priority Queues:
1. Selection Prablem
2. Event Simulation
3.4 Hashing
% Hashing loa key to address transformation technique.
‘& The hash table or hash map is a data structure that associates key with value.
& The Implementation of hash tables Is frequently called hashing,
% Hashing Is used for faster access of elements and records from the collection of tables and fies,
% Hashing Is a technique used for performing insertions, deletions and search operations in constant
average time of (1).
'® Hashing is applied where the array size is large and time taken for searching an element Is more.
It works by transforming the key using a hash function into @ hash, a member which is used to index
Into an array to locate the desired location where the values should be.
% Hash table supports the efficient addition of new entries and the time spent searching for the
required data Is independent of the number of items stored.
3.4.1 Hash tabl
= In hashing an Ideal hash table data structure Is nearly an array of fixed size, containing the key.
& The key Isa string or Integer associate with a value. Each key Is mapped in to some number in the
range 0 to TableSize -1 and placed in the appropriate cell
'% The mapping is called hash function, which should be simple to compute and should ensure that any
two distinct keys get diferent cells.
°
1
2
3
@ Einstein College Of Engineering [85/159]FE36-Data Structure and Algorithms IEEE
3.4.2 Hash function:
++ Ilosh function Is @ key to address transformation which acta upen a given key te compute the relative
position of the key in an array.
& The mapping of each key into some number ranged from 0 to TableSizo~1 Is known as Hashing.
Ideally, the hash function is used to determine the location of any record given its key value
‘The hash function should have the following properties
+ Simple to compute
‘+ Must distribute the data evenly.
+ Generates lower number of calli
+ Reduce storage requirement.
& Hashing is a method to transform a key to an address. The transformation involves application of @
function to the key value, as shown below.
Hash
unction
Key —————» [> Address
The hach function in other words, mape @ key value to an address in the table and the damain of the
hash function Is non-negative Integers in the range 0 to the size of the table.
This the key and h fs the hash function, then h(x) is the address for the key to be stored
‘Simple Hash function:
Consider a simple hash function, h(x) = x med 10. The following table shows list of key values and
the corresponding hash function values.
Key (x) Hash Function Value h(x)
10 °
25 5
33 3
ar 7
64 a
88 8
39 8
The following figure shows the Storage of the key values In the above table using the hash function value
as Index.
Key (x) Hash Function Value h(x)
o 10
1
2
@ Einstein College Of Engineering [86/159]FE36-Data Structure and Algorithms IEEE
aa
64
25
a7
88
30
Suppose we want to store vali 29, we need to epply the hash funetion first.
In this case, it ig 25 mod 10 = 5. 5 fs used as the address for storing 25
& The simple hash function is HashValue = key % TableSize.
Routine for simple Hash Function
Typedef unsigned int index;
index Hash (const int*key, int TableSize)
{
index HashVal
Whilec*key !=07)
HashValt=*key ++;
return HashVal 9% Tablesize ;
y
For instant suppose the table size is 10007. The given input key can have maximum of 8 character
are few characters lena.
Since a char as an Integer value (\.e) always atmost 127 (\.e) ASCII code can represent In 7 bits
therefore 2’= 128.
The hash function can 1* assume the values between 0 to 1016 ([.e) 127x@. Hence it cannot provide
‘equal distribution
Routine for Second Hash function:
Typedef unsigned int index;
index Hash (const char*key, int TableSize)
{
return (key[0]427*kay[1]+720*key[2])% Tablesize;
y
% In this hash function we can assume at least 2 characters + null terminator. The value 27 represents
umber of English alphabets 26-+a blank space. Therefore 729 is 27°
‘% This function examines only the first 3 char, If these are random and the table size Is 10007, (1.0)
English is not random. Therefore 26°=17576 possible combination of 3 char ignoring blank space.
Routine for Third Hash function:
Typedef unsigned int index;
Index Hash (const char*key,int Tablesize)
{
Index Hashval=0;
@ Einstein College Of Engineering [87/159]FE36-Data Structure and Algorithms IEEE
While(*key=09)
Hashval=(Hash<<5)+*key++;
return HashVal % TableSize;
y
3.4.3 Collision:
When a memory location filled if another value of the same memory location comes then there
occurs collision.
4 When on element is Inserted It hashes to the 2ame value a2 an already inaerted clement, then tt
produces collision and need to be resolved
ion resolving methods:
Separate chaining (or) External Hashing
Open addressing (or) Closed Hashing
1. Separate chai
19 (or) external hashing:
© Separate chaining Is a collision resolution technique, in which we can keep the list of all elements
that hash to same value. This is called as separate chaining because each hash table element is a
separate chain (linked lst)
Each link list contains the entire element whose keys hash to the same index.
Collision diagram:
7 :
Kor (x) Hoo Funtton Ylus Bon 2
10 10 % 10-0 — 33, 3
2s 25% 10-5 4
3 22% 10-3 2 5
° 7% 107 6
os osm 10-5 a 7
sam 10-3 8
x 20% 10-0 °
Separate chaining is an open hashing techniaue Hash Table
Each linked list contain all the elements whose keys to the same index,
AA pointer field is added to each record location.
\When an overflow occurs thls pointer Is set to point the overflow blocks making a linked list
In this method, the table can never overflow, since the linked lists are only extended upon the arrival
of now keys.
% The following figure shows separate chaining hash table,
eee ae
@ Einstein College Of Engineering [88/159]FE36-Data Structure and Algorithms IEEE
List_Node 9 Structure Is the same as the linked list declaration
Hash Table > Structure contains an array of linked lists, which are dynamically allocated when the table Is
Initialize
‘The_List 3 Pointer to a pointer to 2 List_Node structure
‘Type declaration for open hash table
typedef struct List_Node *Nade_ptr;
struct List_Node
«
Element_Type element;
Node Ptr next;
x
typedef Node_Ptr List;
typedef Node_Ptr Position;
PLIST *the_list will be an array of lists, allocated later */
/* The lists will use headers, allocated later */
struct Hash_Tbl
{
Unsigned int Table Size;
LUst *The_Lists;
%
typedef struct Hash_Tbl *Hash_Table;
ization routine for open hash table
Hash Table Initialize Tablo( unsigned int Teble_Size )
t
Hash_Table Hi;
Inti
If(Table_Size < MIN_TABLE_SIZE )
«
cerror(*Table size too small");
@ Einstein College Of Engineering [89/159]FE36-Data Structure and Algorithms IEEE
>
Find Function:
return NULL;
y
P* Allocate table */
Hash_Table) malloc ( sizeof (struct Hash_Tbl));
NULL)
fatal_error(*Out of spacell!");
H->Table_size = next_prime(Table_Size );
1° Allocote list pointers */
H->The_Lists = (position *) malloc( sizeof (LIST) * H-> Table_Size);
if( H->The_Lists == NULL)
fatal_error(*Out of space!
F* Allocate list headers */
for(i=0; icH->Table Size; i++)
t
nc
H->The Listsfi] = (LIST) malloc( sizeof (struct List_Node) );
M( H->The_Listsfi] == NULL)
fatal_error("Out of spacell!");
else
H->The_Lists[->next = NULL;
return 5
Use the hash function to determine which list to traverse
‘Traverse the list in normal manner.
Return the position where the item Is found.
The call Find(Key, H) will return a pointer to the cell containing key.
If Element Type is a string, comparison and assignment must be done with
stromp & strepy respectively.
Find routine for open hash table
Position Find( Element_Type Key, Hash Table # )
{
Position p;
List
L = H->The_Lists[ Hash( Key, H->Table_Size) ];
P= L>next;
hile( (P 1= NULL) && (P->element != Key) )
P* Probably need strempl! */
P= P->next;
return P;
@ Einstein College Of Engineering [90/159]FE36-Data Structure and Algorithms IEEE
Insert Function:
1. Go to the position by the hash function for the item x
2. Traverse the list to see If X exists already.
3. It not, insert a new node at the rear ofthe list:
Insert routine for open hash table
Void Insert( Element Type Key, Hash_Table H )
{
Position Pos, New Cell;
(Position) malloc(sizeof(struct List_Node));
if( New_Cell =~ NULL)
fatal_error("Out of spacell!");
else
{
H->The_Listsl Hash( Key, H->Table Size ) J;
New_Cell->next = L->next;
New_Cell->element = Key; /* Probably need stropy!! */
L >nont ~ New Cell;
>
,
?
Advantages:
i) More number of elements can be inserted as it uses array of linked lst.
11) The elements having the same memory address will be in the same chain and hence loads to faster
searching.
Ii) Doesn't require a prior knowledge of the number of elements that are to be stored in the hash
table ((.0.) dynamic allocation is dene.
|v) It helps to get a uniform and perfect collision resolution hashing.
Disadvantages:
|) The elements are not evenly distributed. Some hash index may have more elements and some
may not have anything.
Ii) It requires pointers which require more memory space. This leads to slow the algorithm down a bit
because of the time required to allocate the new cells, and also essentially requires the
Implomentation of second data structure
2, Open addressing or Closed Hashi
® Open addressing hashing Is an altemating technique for resolving collisions with linked list. In this
system if a collision occurs, alternative cells are tried until an empty cell is found,
@ Einstein College Of Engineering [91/189]FE36-Data Structure and Algorithms IEEE
& The cell hel), hil), Are tried in succession, where hy(x) = (Hash (X) + F(D)) mod Table_stze
with FO) = 0.
@ The function F Is the collision resolution strategy. This technique Is generally used where storage space
's large.
4 Arrays are used here as hash tables.
Definition: The technique of finding the availability of another suitable empty location in the hash table when
the calculated hash address Is already occupied Is known as open Addressing. There are three common
collisions resolving strateate
1. Linear probing
2. Quadratic Probing
3. Double hashing
Linear probing:
‘Probing Is the process of a placing In next available empty position in the hash table. The Linear
probing method searches for next suitable position in a linear manner(next by next). Since this
‘method searches for the empty position in a linear way, this method ts referred as linear probing,
& In linear probing for the i" probe, the position to be teed is, (h(k) + |) mod Table_Size, where "fis @
linear function of i, F(i)=I. This amounts to trying cells sequentially In search of an empty cell
Example for Linear Probin;
Insert the keys 89, 18, 49, 58, 69 into a hash table using in the same hash function as before and
the collision recelving otrateates. F(N)~I.
Solution:
% In this e.9 initially 89 Is inserted at index 9", Then 18 is inserted at index 8.
& The frst collision occurs when 49 Is inserted. It Is put in the next available index namely *0" which is
open.
& The key 58 collides with 18, 89, 49 afterwards it found an empty cell at the index 1
Similarly collision 69 Is handled.
If the table is big enough, a free cell can be always be found, but the time to do so can get quite
large.
Even If the table is relatively empty, blocks of occupied cells start forming. This #s known as primary
clustering means that any key hashes into the cluster will require several attempts to resolve the
calision and then it will add to the cluster
Empty Table After 89 After 18 Aer a5 Titer 56 Titer 69
49. 49 49
58 58
69
@ Einstein College Of Engineering [92/189]FE36-Data Structure and Algorithms IEEE
TRigarithm for Inear probing
1. Apply hash function on the key value and get the address of the location.
2. Ifthe location is free, then
1) Store the key value at this location, else
li) Check the remaining tocations ot the table one atter the otner tit an empty
location Is reached, Wiap around on the table can be used. When we reach
the end of the table, start looking again from the beginning.
lil) Store the key In this empty location.
a. End
‘Advantages of li
F probing
1. Ik does not require pointers.
2. It is very simpler to implement.
Disadvantages of linear probing:
1. It forms clusters, which degrades the performance of the hash table for sorting and ret
data,
2. If any collision occur when the hash table becomes half full, itis difficult to find an empty location
In the hash table and hence the insertion process takes a longer time.
@ Einstein College Of Engineering [93/159]FE36-Data Structure and Algorithms IEEE
UNIT IV: GRAPHS
Definitions ~ Topological sort ~ breadth-first traversal - shortest-path algorithms ~ minimum spanning tree ~
Prim's and Kruskal's algorithms ~ Depth-first traversal ~ biconnectivity ~ euler circults - applications of
graphs
unit 1v
Representation of Graph
‘+ Graph terminology including these terms: Vertex, edge, adjacent, incident, degree, cycle, path,
connected component, and spanning tree.
+ Three types of graphs : Undirected, directed, and welghted
+ Common graph representations: adjacency matrix, packed ~ adjacency lists, and linked ~ adjacency
lists,
‘+ Standard graph search methods: breadth-first and depth-first search
‘+ Algorithms to find a path in a graph to find the connected components of an un:
find a spanning tree of a connected undirected raph.
+ Specifying an abstract data type as an abstract class,
4.1 DEFINITIONS: A graph
= (V, E) fa an ordered pair of finite sets V and E, The elements of V are called
as vertices are also called as nodes and points. The elements of E are called edges are also called arcs and.
lines. Each edge in E joins two different vertices of V and Is denoted by the tuple (1), where | and j are the
two vertices joined by E.
4.1.4 Graph Display: A groph generally represented as figure in which the vertices represented by circles
‘and the edace by linea. Examplea, of araphe are ae fellows:
‘Some of the edges in this figure are oriented (that they have arrow heads) while other are not. An
edge with an orientation Is @ directed while an edg® with no orientations is an undirected edge. The
undiracted edges (ij) and (J) are the same; the directed edge (i,)) Is different from the directed edgeU,!)
the former being oriented from I to j and the latter from j toi
=e
eso
©
Unairected Graph G1 Directed graph G2
V (G1) = €4,2,3, 4% ECGLY={(4,2), (4,3), (4,4), (2,3), (249,84) F
V(G2)= {4,23} E(62)=(<1,2>,<2,1>,<2,3>4
The edges of a directed graph are drawn with en arrow from the tall to the head.
For undirected graph:
n(n—))
@ Einstein College Of Engineering [94/189]FE36-Data Structure and Algorithms IEEE
For directed graph
Gi
The no.0f possible edges in an undirected graph is “and ine directed graph is
9 edges
4.1.2 Directed Graph
A directed graph or di-araph Is a pair G = (V,E) where V Is @ set whose elements are called
vertices(nodes) and E is a set of ordered pairs of elements of V called edges or directed edges or arcs. For
directed edges(v,¥) in E, 'V' Is its tall and ‘w' is Its head. (v,w) Is represented in the diagrams as the arrow,
ahem ey
KO » vette 238
Edges = {(1,2), (1,4), (2,4), (3,1),
(3,2), (3,4) Gy
A.a.2 Unairocted Graph
‘An undiracted graph is 4 pair G = (V,E), where V is a sot whose elements are called verticas(nodes)
land E Is a set of unordered pairs of distinct elements of V called edges or undirected edges. Each edge may
be considered as a subset of V containing two elements, {v,w} denotes an undirected edge, represented as
v-w Le simply vw . Of course vw = wy for undiracted graph.
vertices
12,34)
Edges ~ {(1,2), (14), (1,3),
1) ANE sot cannot have duplicate elements
i) An edge that connects a vertex to itself is not possible,
4.1.4 Subgraph:
‘A subgraph of graph G ~ (VE) Is a graph G’=(V,E?) such that V'CV and ECE.
@ Einstein College Of Engineering [95/159]FE36-Data Structure and Algorithms IEEE
Graph G1
cr
4.1.5 Symmetric
graph:
‘A symmetric digraph 1s a directed graph such that for every edge vw there Is also the reverse edge
Example; (a)
4.1.6 symmetric Undiracted graph:
Every undirected graph fs a symmetric digraph by Interpreting each undirected edge as a pair of
rected edges in opponte eretone =)
4.1.7 Complete graph:
‘A complete graph is a graph normally undirected with an edge between each pair of vertices,
SZ
Pst
4.1.8 Incident edge:
‘The edge ww is said to be incident, ifit is incident on the vertices v and w, and vice versa.
4.1.9 Adjacency Relation:
‘Adjacency relation in digraph ts represented by vAw ( w Is adfacent to v ) if and only If vw Is in E.
FG is undirected araph, the adjacency relation (A) is symmetric (Le. wAv if and only f vAw)
@ Einstein College Of Engineering [96/159]FE36-Data Structure and Algorithms IEEE
4.1.10 Path:
A path from v to w in a graph G = (V,£) is a sequence of edges, VeVi, ViV2;.-VkiMay Where ¥=Vo and
fo The lenath of the path Is k.
‘Therefore, Length of a path
example
a
from Ato Dis 2
lo.of edges traversed
Path length
a
length from vertex V to itself is zero
Poth
4.1.11 Connected graph:
‘An undirected graph is connected if and only if, for each pair of vertices v and w, there Is a path from
esa @
OH
@ 1
S—2® J BH &
4.1.12 Strongly Connected Graph:
A directed graph is strongly connected if and only if, for each pair of vertices v and wi, there is a path
from v to w. Le, strong connectivity means travelling the one-way streets in thelr correct direction from
anywhere to anywhere.
Example: )
K—2 ov
@ Einstein College Of Engineering [97/189]FE36-Data Structure and Algorithms IEEE
‘The definition of cycle is same for undirected graph also, but there Is a requirement that if any edge appears
more than once it always with the same orientation. Le using the path definition If v,— x and wax ~ y for
O
son
‘The space needed to represent a graph using its adjacency matrix isn bits
Disadvantages:
1. Takes 0(
space to represents the graph
2, It takes O(n) ume to golve the most of the problems
4.2.2 Adjacency List Representati
® A graph containing ni vertices and m edges can be represented using a linked list, referred to as
adjacency list. The number of vertices in the graph forms a sinaly linked list.
In this representation the n rows of the adjacency matrix are represented as-n linked lists.
® Each vertex have a separate linked list, with nodes equal to the number of edges connected from the
corresponding vertex. The nodes in list i represent the vertices that are adjacent from vertex I
% Each node has atleast two fields: VERTEX and LINK. VERTEX flelds contain the indices of the
vortices adjacent to vertex I.
Advantages:
® Eventhough adjacency list Is a difficult way of representing graphs, @ graph with large number of
nodes will use small amount of memory.
% In adjacency list representation of graph, we will maintain two lists. First list will keep track ofall the
nodes in the graph and second list will maintain a list of adjacent nodes for each node
1. Adjacency lists for Directed graph:
@ Einstein College Of Engineering [100/159]FE36-Data Structure and Algorithms IEEE
Adjacency list for the above graph is as follows:
Verte 2 yO —py
Veriee 3 q by
verter 3} —[5}—
2. adjacency lists for Undirected graph:
Let us consider the following undirected graph,
The adjacency list for the above graph is as falows:
vertex [7 [BR Ae DY
¥
Vertex 2 B
¥
Vertex 3 C
+
Vertex 4 DI
In this representation each list had a head node, The head nodes are sequential providing easy
random access ta the adjacency list for any particular vertex. For an undirected graph with n vertices and m
‘edges this representation requires n head nodes and 2m list nodes. The storage required Is then log n + log
1m for the list nodes and 0 ( log n } for head nodes because 2 fields in each node.
‘The structure of header node ts:
[Name ofthe node
(he next node bender
‘nde it
‘The total number of edges in G may, therefore can be determined by counting the number of nodes
‘on its adjacency list. In order to determine in-dearee of a vertex easily, we can maintain one more list called
Inverse-adjacency lists for each vertex. Each list will contain a node for each vertex adjacent to the vertex It
represents.
isadvantages:
1 takes O(n) time to determine whether there is an arc from vertex | to vertex J. Since there can be O(n)
vertices on the adjacency list for vertex I
@ Einstein College Of Engineering [101/159]FE36-Data Structure and Algorithms IEEE
4.2.3 Adjacency Multilists Representati
In adjacency list representation of an undirected graph each edge (Vi, Vj) Is represent by two entries, one
‘on the list for V, and other on the List for Vj. Using Multiists each edge will be in exactly one node, but this
rode will be in two lists. .e the adjacency lists for each of the two nodes It Is Incident to the node structural
now becomes
™ ve ve Linke for Vi —Link2 for Va
Here M is @ one bit mark field that may be used to indkate whether or not the edge has been examined.
4.2.4 Incidence Matrix Representation:
A graph containing n vertices and m edges can be represented by 8 matrix with n rows and m
columns. The matrix 1s formed by storing: 1" in its f row and J* column corresponding to the matrix, if
there exists as a I vertex, connected to one end of the j* edge and a 0'if there is no j* vertex connected to
an end of the" edge ofthe graph, such @ mate is referred as an tncidence matrix.
IneMat{t}[) ~ 1, If there is an edge €, from V,
IncMat{i]j] = 0, otherwise
Example 1: Let us consider the following undirected graph,
& E &
ve 1 ° °
Ve o 1 °
Vs 0 ° 1
Vy 1 1 1
Disadvantage of IncWdence matrix:
1. Eventhough incidence matrix Is an easy way of representing graphs, a graph with considerable
number of nodes could use a large amount of memory.
2. Most of the incidence matrices wil
be sparse matrices only( a matrix with lot of zeros in i)
4.3 APPLICATIONS OF GRAPH:
The problems using graph representation are useful in many fields for different operations. Some
examples are:
4.3.1 Airline route map-undirected graph
in both directions.
‘graph between four cities ~ {A, 8, C, DY
@ Einstein College Of Engineering [102/159]FE36-Data Structure and Algorithms IEEE
4.3.2 An electrical circuit:
The vertices could be diodes, transistors, capacitors, Switches and so on. Two vertices are connected
by a line if there is a wire connecting them,
4.3.3 Flowcharts ~ Directed graph
The vertices are the flowchart boxes; the connecting arrows are the flowchart arrows.
Flowchart: Directed graph:
1
2 [iain
> pas
+©O+O+O
GO+
@+O'
4.3.4 Computer Networks:
The vertices are computers. The lines (uncirected graph) or arrows (directed graph) are the
‘communication links.
Example;
a) A star network b) A ring network
(undirected graph) (directed graph)
4.4 TOPOLOGICAL SORT
A topological sort is @ linear ordering of vertices In a Directed Acyclle Graph(DAG) such that if there
1s a path from V\ to Vy, then V, appears after V; In the linear ordering,
@ Einstein College Of Engineering [103/159]FE36-Data Structure and Algorithms IEEE
Topological ordering is not possible, if the graph has a cycle, since for two vertices v and w on the
cycle, v precedes w and w precedes v.
4.4.1 Simple Topological sort:
The Strategy:
() Find any vertex with no incoming edges (\.e) Indegree-0. If such vertex found, print the
vertex and remove it along with its edges from the graph,
(i)—_-Repeot step (1) for the rest of the areph.
‘The steps to perform simple topological sort is:
() Assuming that the Indegree array is intlalized and the graph is read into an adjacency list.
(i) The function FindNewverrtexofindegreeZero scans the Indegree array, to find a vertex with
Indegree 0, that has not already been assigned a topological number.
a). Itreturns NotVertex if no such vertex exists, that indicates that the graph has a cycle,
b). If vertex (v) Is returned, the topological number is assigned to v, then the indegree of
vertices(w) adjacent to vertex (v) are decremented,
Cit) Repeat step 2 for the rest of the graph
Algorithm for
void Topsort(Graph )
ple Topological sort:
{
int counter;
vertex Vs Wi
for{counter ~ 0; counter
?
Example @—)
2)
* Indegree is initialized in an array
+ Numvertex
3
+ Graph is stored in an adjacency list.
@ Einstein College Of Engineering [104/159]FE36-Data Structure and Algorithms IEEE
Step 1: Find a vertex with Indegree 0 (i.e) a vertex with no incoming edges. Therefore,
V= Als returned
Step 2: Topological Number is assigned to vertex A.
TopNum[a] = 0
The indegree adjacent vertices of V are decremented, (L.e) B,C
Now tncegree[t] = 0
Indagree[e} = 1
Step 0: Step (1) Is repented.
Find a vertex with indegree 0 ({.e) a vertex with no Incoming edges. Therefore,
V= Bis returned
Step 4: Step (2) Is repeated
Topological Number Is assigned to vertex B.
TopNum(B] = 1
The indegree adjacent vertices of V are decremented, (i.e) C
Now Indegreefe
Step 5: Step (1) is repeated
Find 2 vertex with indegree 0 (i.e) 2 vertex
tno incoming edges. Therefore,
v= C returned
Step 6 Step (2) Is repeated
Topological Number is assigned to vertex
TopNumiel~ 2
Step 7: Loop variable counter |< NumVertex so the Iteration process for finding the vertex was
terminated.
Step 8: The vertices are printed as per the topological number ordering,
le, A, B, C > hence the topological order is derived for the given DAG.
Analysis
In this algorithm FindNewVerrtexofindegreezero Is a simple sequential scan of Indegree array, each
call takes (OVI) time. Since there are |V] such calls, re running time of the algorithm Is (OIVI*).
Drawback of this algorithm:
++ IF the graph Is sparse, only a few vertices have their Indegree to be updated during iteration. But this
algorithm carries @ sequential search of all the vertices.
‘To Overcome Thi
‘A queue Is to be maintained for vertices with Indegree 0 and not yet processed.
4.4.2 To implement the topological sort with queue representation, perform the following steps.
Step 1 : - Find the indegree for every vortex.
Step 2 : - Place the vertices whose indegree Is *0'on the empty queue.
Step 3
= Dequaue the vertex V and decrement the Indegree's ofall its adjacent vertices.
Step 4: ~ Enqueue the vertex on the queue, if ts Indegree falls to zero.
Step 5 : ~ Repeat from step 3 until the queue becomes empty
@ Einstein College Of Engineering [105/159]FE36-Data Structure and Algorithms IEEE
Step 6 : - The topological ordering is the order in which the vertices dequeued.
Routine to perform Topological Sort using queue representation
7 Assume that the graph Is read Into an adjacency matrix and that the indegrees are computed for every
vertices and placed in an array (Le. Indegree 1) */
void Topsort (Graph 6)
{
Queue Q;
Int counter —
vertex V, W;
Q = CreateQueue (NumVertex);
Makeempty (Q);
for each vertex V
if (indegree [V] = = 0)
Enqueue (V, Q);
tile (1 IsEmpty (Q))
{
v= Dequeue (Q);
TopNum [VJ = + + counter;
for each W adjacent to V
If (~Indegree [W] = = 0)
Enaucue (W, 0);
+
IF (counter ! = NumiVertex)
Error (* Graph has a cycle");
DisposeQueue (Q); /* Free the Memory */
y
Note :
Enqueue (V, Q) implies to insert a vertex V into the queue Q
Dequeue () implies to delete a vertex from the queue Q.
‘TopNum [VJ Indicates an array to place the topological numbering,
Illustration with an example:
= Numverte)
% Indegree Is intialised In an array
& Graph is stored as an adjacency list
@ Einstein College Of Engineering [106/159]FE36-Data Structure and Algorithms IEEE
VERTEX _Indegree before Dequeue
202 3 4 5
A 0 0 © 0 o
8 1 0 0 0 0
© 1 0 0 0 0
. 302 41 0 0
e 2 2 12 4 0
Cnaueue A Dor
Dequeue AB C D E
Step 1
Number of 1's present in each column of adjacency matrix represents the Indegree of the corresponding
vertex.
From above figure Indegree [A] = 0, Indegree [8] = 1, Indegree [C]
Indegree [E]= 2
4, Indegree (D]
Step 2
Enqueue the vertex, whose indegree is "0"
Vertex A’ is 0, so place it on the queue and then dequeued.
step 3
Dequeue the vertex °A' from the queue and decrement the indegree's ofits adjacent vertex "B! & °C’
Hence, Indegree [8] = 0 & Indegree [C] = 0
Now, Enqucue the vertex “B' AND 'C’ ag Ite Indearce becomes zero. Then dequeue the vertex °S"
Step 4
Dequeue the vertex °8! from Q and decrement the indegree's ofits adjacent vertex °C! and °D!
Hence, Indegree [C] = 0 & Indegree [D] = 1
Now, Enqueue the vertex °C! as its indegree falls to ze°0.
Step 5
Dequeue the vertex °C" fram Q and decrement the indegree's of Its adjacent vertex °D’.
Hence, Indegree [D] = 0
Now, Enqueue the vertex "D" as its indegree falls to 2er.
step 6
Dequeue the vertex *D"
step 7
‘As the queue becomes empty, topological ordering is performed, which is nothing but, the order in which
the vertices are dequeued.
Analysis
The running time of this algorithm Is O{IE| + [V|). where E represents the Edges & V represents the
vertices of the graph.
4.4.3 Implementation of Topoloy
(Refer the lab program manual and write only the corresponding functions)
I sorting technique:
@ Einstein College Of Engineering [107/159]FE36-Data Structure and Algorithms IEEE
4.5 GRAPH TRAVERSALS.
Graph traversal fs defined as visiting all the vertices of the graph G ~ (V, E), where V Is the set of
vertices and E Is the set of edges.
Two different types of traversal strategies are:
4. Depth First Search ( DFS )
2. Brendth First Search ( BFS )
4.5.1 Depth First Search ( DFS ) :
+ DIG Is otherwise known as Depth first Troversel(DFT), ita kind of tree traveraal.
DFS technique Is as follows:
+ Starting vertex may be determined by the problom or chosen arbitrarily. Visit the start vertex
¥v, next an unvisited vertex w adjacent to v Is selected and a DFS from w Is initiated. When a
vertex u Is reached such that all the adjacent vertices have been visited, we back up to the last
vertex visited which has an unvisited vertex w adjecent to It and initiate @ DFS from w. The
search terminates when no unvisited vertex can be reached from any of the initiated vertices.
1+ If path exists from one nade to another node walk across the edge ~ exploring the edge.
4+ IF path does not exist from ane specific node to any other node, return to the previous node
where we have been before ~ backtracking.
The theme of DFS Is to explore if possible, otherwise backtrack,
Algorithm:
Procedure DFS(u)
VISITED (u) 1
for each vertex w adjacent to v do
If VISITED ( w ) = 0 then call DFS ( wr)
end
end DFS,
*)
Ge +b A directed graph G=(V.E) is given, where
A.B,C,D.E,F,G}
TG) + For simplicity, assume the start vertex is A
© and exploration is done in alphabetical order.
4 From start vertex A explores to B, now AB is
explored edge.
¢ From vertex B either C or D to be explored,
‘but Cis explored as per the alphabetical order.
her lore from C, therefFE36-Data Structure and Algorithms IEEE
Since C is a dead end, backtrack to B, from
there explore to D
*
From D it is possible @ explore A, this would
complete a cycle, but trees should not have
cycles,
“Again backtrack to B, from there backtrack to
A. explore the path to F.
“& From F it is possible to traverse either A or C.
‘but both the nodes are discovered nodes. So F
is also a dead end.
¢ From the above diagram it is possible to say
that G and E are never traversed.
4.5.2 Breadth First Search:
Starting at vertex v and marking it as visited. BFS performs simultaneous explorations starting from a
common point and epreading out independently.
Breadth firs search differs from depth first search in that all unvisited vertices adjacent to v are visited
next. Then unvisited vertices adjacent to these vertices are visited and so on. A breadth first search
beginning at vertex vi of the graph in above figure would first visit root node and then one by one in the
‘same level and passes to other level
BFS Technique step:
41. Put the ending node (the root node) in the queue.
2. Pull node from the beginning of the queue and examine it
© I the searched element is found in this node, quit the search and return a result.
Otherwise push all the (so-Far-unexamined) successors (the direct child nodes) of this node
Into the end of the queue, if there are any.
3. If the queue is empty, every node on the graph has been examined ~ quit the search and return
“not found”.
4. Repeat from Step 2.
Procedure BFS.
[Jereadth fist search of G is carried out beginning at vertex v. All the vertices
visited are
marked as VISITED(I) = 1.The graph G and array VISITED are global and VISITED Is
initialized to zero /)
VISITED (v) 4
Initialize Q to be empty 11 Qs queue //
@ Einstein College Of Engineering [109/159]FE36-Data Structure and Algorithms IEEE
Loop
for all vertices w adjacent to v do
IFVISITED(w) J] add w to queue //
then [ call ADDO(w, Q); VISITED() — 11
end /] mark w as VISITED//
iF Q is empty then return
call DELETE ( v, @)
forever
end BFS
Implementation:
void BFS(VLink GU], int v)
«
Int w; VISIT(W); ‘Prvisit vertex vy
visiteatv] ~ 1; Pemark vas visited = 1 */
ADDY);
whilesQMPT¥Q(Q))
«
v= DELQ(Q); /*Dequeve v*/
WFIRSTADI(G,v); [Find first neighbor, return -2 if no neighbor*/
wiile(w 1= -1)
f
Ifevisited{w] ~~ 0)
4
vIsIT(w) /r¥isit vertex v7
ADDQ(Qw);—_/*ENqueue current visited vertex w*/
Visited{w] = 1; /*mark w as visited*/
JEXTADK(G,v);/*Find next neighbor, return -1 If no neighbor*/
void TRAVEL_BFS(VLink GU], int visited{], int n)
£
Int;
for(i = 031
Applications of Graph Traversal: ~
41. Finding the components of a graph and
2. Finding a spanning tree of a connected graph
3. Finding the shortest path between two nodes u and v (in an unweighted graph)
Example:
A directed graph G=(V.E) is given, where
V-(A.B.C.D.EF.G)}
Assume the start vertex is A.
Explore all paths from vertex A
»)
From A the explored edges ate AF, AB
°
Explore all paths from vertex B and F.
From B the explored edges are BD, BC
From F the explored edges are FA, FC.
The dashed lines shows the edges that were
explored but went to vertices that were
previously discovered (Le FA, FO)
From D the explored edge is DA.
But A is already exist in the discovered
vertices list. So we will say that the edge DA
is checked rather than explored.
** There is no backtracking In BFS and E and G are unreachable.
4.5.3 DFS vs BFS.
Depth first search Breadth first search
41. Backtracking Is possible from a dead end 1. Backtracking is not possible
2. Vertices from which exploration is incomplete are 2, The vertices to be explored are organized as a
processed in a LIFO order. FIFO queue.
3. Search Is done in one particular direction at the 3. Search is done parailoly n all possible direction.
ume.
@ Einstein College Of Engineering [111/159]FE36-Data Structure and Algorithms IEEE
4.6 SHORTEST PATH ALGORITHM
The Shortest path algorithm determines the minimum cost of the path from source to every other vertex.
The cost of the path Vi, Vay Va Is
This ts referred as weighted path length. The unweighted path lenath is the number of the edges on the
path, namely N- 1
‘Two types of shortest path problems, exist namely,
1. Single source shortest path problem
The single source shortest path algorithm fings the minimum cost from single source vertex to all
other vertices. For a given graph G=(V,E), the shortest path Is calculated from a distinguished vertex.
S to every other vertex in G, Dijkstra's algorithm Is used to solve this problem which follows the
greedy technique.
2. All pairs shortest path problem
Al pairs shortest path problem finds the shortest path for @ given graph G=(V,E) of every pair of
vertices in G. To solve this problem dynamic programming technique known as floyd's algorithm is,
used.
These algorithms are applicable to both directed and undirected weighted graphs provided that they do not
contain 2 cycle of negative length. Here we will single source shortest path problem for unweighted and
weighted graph.
4.6.1 Single Source Shortest Path
Given an Input graph G ~ (V, E) and a distingulshed vertex S, find the shortest path from S to every
‘other vertex in G, This problem could be applied to both weighted and unweighted graph.
4.6.1.4 Unweighted Shortest Path
In unweighted shortest path all the edges are assigned 2 weight of "1" for each vertex. The following
three pieces of information is maintained.
known
Specifies whether the vertex is processed or not, Tt Is set to “1! after it Is processed, otherwise 0"
Initially all vertices are marked unknown. (1.2) "0"
4
‘Specifies the distance from the source “s', Initially all vertices are unreachable except for's', whose path
lenath is “0!
Be
Specifies the bookkeeping varlable which will allow us to print. The actual path (i.e) the vertex which
makes the changes In dv.
To implement the unweighted shortest path, perform the following steps :
Step 1: Create a queue, where the size of the queue is equivalent to the number of vertices in the
‘raph.
Step 2: - Empty the queue.
Step 3: - Arbitrarily assign the source nade as "S' and Enqueue *S'
@ Einstein College Of Engineering [112/159]FE36-Data Structure and Algorithms IEEE
Step 4: - Dequeue the vertex °S' from queue and assign the value of that vertex to be known and
then find its adjacency vertices. Also update the three parameters known, dy and Py.
Step 5 :- Ifthe distance of the adjacent vertices Is equal to infnity then change the distance
‘of that vertex as the distance of its source vertex increment by * 1" and Enqueue
the vertex,
Step 6 :- Repeat from step 2, until the queue becomes empty.
ROUTINE FOR UNWEIGHTED SHORTEST PATH
void Unweighted (Table 7)
{
Queue Q;
Vertex V, W
Q = Createqueue (Numvertex);
Maketmpty (Q);
J Enqueue the start vertex s */
Engueue (s,Q);
while (! IsEmpty (Q))
{
V = Dequeue (Q);
V = Dequeue (Q);
TIV]. Known = True; /* Not needed anymore/
for cach W adiacont to
IF (TDW), Dist = = INFINITY)
«
IW] . Dist = TIV] . Dist +1;
TIW] . path = Vj;
Enqueue (W, Q);
»
Disposequeue (Q) ; /* Free the memory */
Illustrations with an example
@ Einstein College Of Engineering —_—_—[113/159]FE36-Data Structure and Algorithms
IEEE
Vertex
Queue
Vertex
vs
Ve
Vs
vs
Vs
Ve
vy
‘oucue
vs
Ve
vs
Ve
Vs
Ve
vy
Queue
vw
ve
Ve
ve
Vs
Ve
Known
Known
1
Known
Initial State
4, Pm
a o
2 °
° °
© °
2 °
2 °
vs
Vy Dequeued
4 »
1 Vs
2 vs
o °
2 ve
°
2 Vs
~ o
Vor Vie Va
4 Py
1 Vs
2 vs
o o
2
3
1
© o
Vs, Ve
Vs Dequeued
a By
1
2
° °
2 vs
3 Ve
1 vs
known
Known
1
V3 Dequeued
ge
Bo
Vo Ve
V, Dequeued
om
»
vs
ys
vs
vs
@ Einstein College Of Engineering
[114/159]FE36-Data Structure and Algorithms IEEE
vs ° 3 ve 1 a Ve
Queue vy Empty
In general, when the vertex 'V’ Is dequeued and the distance of its adjacency vertex 'W' is infinitive then
distance and path of W's calculated as follows:
TDW).Dist = TIVI.Dist + 4
TDW).path = v
When Vp fs dequeued, known is set to 1’ for that vertex and the distance of its adjacent vertices Vs
‘ond Ve ore updated If INCINITY, Path length of Vs Is coleulated a9;
T[V{].Dist = Tvs] Dist +1 =0F1=4
[And its actual path is also calculated as,
Thvi].path
similarly,
T[Ve]-Dist = TVs].Dist +1 =041=4
TIVelipath = Vy
Similarly, when the other vertices are dequeued, the table is updated as shown above. The shortest distance
from the source vertex V, to all other vertex is listed below;
Vs 9 Vs is 1
Vs 9 Ve is2
Vs > V4 is 2
Vs 3Vsis3
VF Valet
Vs > Vy is 3
Analysis: The running time of this algorithm Is O(|EI+IVI) if adjacency list is used to store and rread
the graph,
Illustrations with an example 2:
Find the shortest path for the following graph with 'A’ as source vertex.
Solution: Source vertex "A’ is initially assigned a path length °o!
9
@ Einstein College Of Engineering [115/159]FE36-Data Structure and Algorithms IEEE
Initial State A Dequeued
Vertex
Known dy Py Known dy %
A ° ° 1 o o
8 ° © ° ° 1 A
c ° 2 ° ° 1 A
> ° » ° o 2 °
Queue A Be
[After finding all vertices whose path lenath from ‘A’ iat’
8 Deaueued © Dequeued Dequeued
vertex mown dy PR Known dy P, Known yy
A 1 ° ° 1 o ° 1 a)
8 1 1 A 1 1 A 4 1 A
© ° 1 A 1 4 a 4 1 A
> ° 2 B ° 2 8 1 2 8
Queue oe > empty
‘The shortest distance from the source vertex A to all other vertex is listed below;
AD Bist
APCiot
A> DIs2
4.6.1.2 Weighted Shortest Paths - Dijkstra's Algorithm
The general method to solve the single source shortest path problem Is known as Dijkstra’s algorithm,
‘This is applied to the weighted graph G.
Dikstra’s algorithm is the prime example of Greedy technique, which generally solve a problem in stages
by doing what appears to be the best thing at each stage, This algorithm proceeds in stages, just like the
unweighted shortest path algorithm.
‘At each stage, it selects a vertex v, which has the smallest dy among all the unknown vertices, and
aciarns that ae tha shartact path fram Stn Wand mark it to be known. We should set da = dy + Crm, if the
new value for dy would be an improvement.
Algorithm(Informal)
1. Create a table with known, P,, dy parameters, where the size of the table equivalent to the number of
vertices (0 to N-1)
known
‘Specifies whether the vertex Is processed or not. It is set to “1' after it is processed, otherwise
° 0’. Initially all vertices are marked unknown. (i.e) "0".
@ Einstein College Of Engineering [116/159]FE36-Data Structure and Algorithms IEEE
a
Specifies the distance from the source ‘', initially all vertices are unreachable except for 's’
whose path length is *
PB,
Specifies the bookkeeping variable which will allow us to print. The actual path (I.e) the vertex
which makes the changes in dv.
Read graph from the adjacency list representation.
9. Select 0 vertex V which has smallest dy among ell unknown vertices and acta the shorteat path from
Sto Vas known,
4. The adjacent vertices W to V is located and dw Is set a5 dy = dy + Cy, If the new sum Is lass than
the existing dw. That Is, in every stage dw is updated only if there fs an improvement in the path
distance.
5. Repeat step 3 and 4, until all vertices are classified under known.
Exemple 1:
‘The directed graph
Initial configuration of the table
Vertex Known dy Py Comments.
o o a
8 o © o
Als chosen as source vertex, since the
c ° © °
edge AD has minimum value.
Db o © o
E ° © °
After A is declared known
Vertex Known dy Py comments,
A 1 ° °
8 ° ow °
The verthess aujacenil Lu A fe C antl D.
© o 2 A
Therefore, dy, Py of C and D is updated.
Db o 1 A
E o » °
‘After D Is declared known
Vertex Known 4, % Comments,
A 1 ° 0 The vertices adjacent to D Is 8 and C.
8 ° 6 D The vertex B, through D is included
@ Einstein College Of Engineering [117/159]FE36-Data Structure and Algorithms IEEE
c ° 2 A Thed,, Py values of vertex C remains
1 a A same, since the d, through A fs less
° ~ ° than d, through D.
After C is declared known
Vertex Known 4 m Comments:
A 1 o o
8 ° 6 >
‘ ‘ : . The vertex E is updated, since itis
adjacent to C.
D 1 1 A
E ° 8 c
After 8 is declared known
Vertex Known 4 m Comments:
A 1 o o
8 1 6 >
ce 1 2 A The vertex B is selected through D.
D 1 a A
E ° 8 c
After E is declared known
Vertex Known a m comments,
A 1 o o
5 1 6 >
c 1 2 A The vertex E Is selected through c.
> 1 1 A
E 1 8 c
Initial values of known, d,, Py are assigned in a table,
Als selected as source vertex, since AD=1 (minimum cost function), classified as known,
From A, the vertices C and D are updated with the given distance passing through A. The vertices ©
and D are classified under known.
% The vertex B Is set to known, with distance 6, passing through D
% To reach E, the paths with weights
A-c-E=8
A-p-c-E=-10
® The path with low cost is selected, and E is set as known.
ROUTINE FOR DIJKSTRA’S ALGORITHM
‘Void Dykstra (Graph G, Table T)
{
inti:
vertex V, W;
Read Graph (G, 7) /* Read graph from adjacency list */
/* Table Initialization */
@ Einstein College Of Engineering —_—[118/159]FE36-Data Structure and Algorithms IEEE
for (i= 0; 1 < Numvertex; i++)
{
T [i]. known = False;
T [i]. Dist = tnfinity:
T [i]. path = NotA vertex;
y
7 [start]. dist
fora)
{
V = Smallest unknown distance vertex;
ww
break ;
TIV]. known = True;
Not A vertex)
for each W adjacent to V
FL TDW). known)
t
T[W]. Dist
TDWI. path
in [TW]. Dist, TIV]. Dist + Cyw]
y
Analysis:
This algorithm works for the welghted graph without negative cost. If any edge has negative cost,
the algorithm could produce the wrong answer
‘The running time depends on how the table Is manipulated. If we use the evident algorithm of scanning
down the table to find the minimum d,, each phase wil take O(|V) time to find the minimum. Thus O(IVI?)
‘ume will be spent for finding the minimum over the course of the algorithm.
The time for updating dy Is constant per update, and there Is at most one update per edge for a total of
O(IED. Thus, the total running time is OCIEI+IVI?) = OC1V1?)-
If the graph is dense, with 1E| = ECIVI?), this algorithm Is not only simple but essentially optimal, since It
runs In time linear In the number of edges. If the graph has negative edge cost, then Dijkstra’s algorithm
does not work
Example 2: Find the shortest path for the following weighted graph.
Solution:
@ Einstein College Of Engineering [119/159]FE36-Data Structure and Algorithms IEEE
Step 4: Vertex Initial State
T [8] Dist ~ Min [T[B].Dist, T[A] .Dist + Ca]
Min[ © ,0+2)
= Min [SO , 0+ 4)
'O-O
2 Vertex known 4, Py
(¢} >) c ° ~ °
cy 1
After ‘A’ Is declared Known
After ‘A’ is declared known
Step 3:
Now, select the vertex with minimum distance, which Is not known and mark that vertex as visited.
Here "D' Is the next minimum distance vertex. The adjacent vertex to “D!is °C, therefore, the distance of C
Is updated a follows, T[C].Dist = Min [T[C].Dist, TID]-Dist + Ce,<]
= Min [09,1 + 1] =?
vertex known a Py
1 ° °
8 ° 2 a
c ° 2 D
> 1 1 a
Is declared Known
After ‘D’ is declared known
Step 4:
The next minimum vertex is°B" and mark it as visited.
@ Einstein College Of Engineering [120/159]FE36-Data Structure and Algorithms IEEE
° @ 2 (®) 2 Vetex known 4, Pe
2 P c o 2 Dd
OPO, ivan
° (3) 2 vertex known a, Pp
® Sok ft
O® octane,
2 1 1 After‘C’ is declared Known, then the algorithm terminates
4,7 MINIMUM 6PANNING TREE
A tree Is a connected graph with no cycles. A spanning tree Is a subgraph of G that has the same set of
vertices of G and Is a tree,
A minimum spanning tree of a weighted connected graph G ts its spanning tree of the smallest weight,
where the weight of a tree Is defined as the sum of the weights on all its edges. The total number of edges in
Minimum Spanning Tree (MST) is [V[-1 where V is the number of vertices,
‘A minimum spanning tree exists if and only @ G ts connected. For any spanning Tree T, if an edge E that
is not in T is added, a cycle is created. The removal of any edge on the cycle reinstates the spanning tree
property.
@ Einstein College Of Engineering [121/159]FE36-Data Structure and Algorithms IEEE
OD ©—®
OO ©
Minimum Spanning Tree > Cost = 5
Spanning Tree with minimum cost ls considcred a9 Minimum Spanning Tree
‘Types of Algorithm to find MST:
1. Prim’s Algorithm:
2. Keuskal’s Algorithm
47.4 Pri
Algorithm,
Prim’s algorithm Is one of the methods to compute a minimum spanning tree which uses 2 greedy
technique. This algorithm begins with a set U initialised to {1}.
In each stage, one node is picked as the root, and we add an edge, and thus an associated vertex, to the
tree, until all the vertices are present in the tree with [V[-1 edges.
[At each stop, It finds a shortest edge (u,v) such that the cost of (u, v) Is the smallest among all edges,
where u Is in Minimum Spanning Tree and V Is not in Minimum Spanning Tree.
Algorithm (Informal)
1) One node Ie picked ae a root nede(u) from the given connected graph.
i) At each stage choose a new vertex v from u, by considering an edge (u,v) with minimum cost
‘among all edges from u, where u Is already in the tree and v is not in the tree.
1) The prim’s algorithm table is constructed with three parameters. They are;
& Known - Vertex Is added In the tree or not.
4 ~ Weight of the shortest arc connecting v to a known vertex.
ev ~ last vertex which causes a change In dy.
|) Aftar selecting the vertex v, update rule Is applied for each unknown w adjacent to v. The rule Is
min(dw, Cus)
minimum cost.
SKETCH OF PRIM'S ALGORITHM
void Prim (Graph G)
«
dw
.e, if more than one path exist between v to w, then dw Is updated with
MSTTREE T;
Vertex u,v;
Set of vertices V;
‘Set of tree vertices U;
NULL:
/* Inttialzation of Tree begins with the vertex "1" */
u=ay
while (U # v)
@ Einstein College Of Engineering [122/159]FE36-Data Structure and Algorithms IEEE
Let (u,v) be a lowest cost such that u Is In U and v isin VU;
T=TU CW
usuuMy:
y
ROUTINE FOR PRIMS ALGORITHM
vold Prims (Table 7)
t
vertex V, W;
/* Table intialization */
for (I= 0; 1 < Numvertex ; I++)
{
TU]. known ~ False:
TLi]. Dist = Infinity;
TLL. path = 0;
y
for (55
{
Lat V be the start vertex with the smallest distance
TIL. dist ~ 0;
TIV]. known ~ True;
for each W adjacent to V
IF TIW] - Known)
{
TDW].Dist = in
(TLW]. Dist, Cow):
TLW}.path = V5
y
y
>
‘Analysis: The implementation of prim’s algorithm Is virtually Identical to that of Dikkstra’s algorithm.
Prim’s algorithm runs on undirected graphs. So when coding it, remember to put every edge in two
adjacency lists. The running time Is O(IVI?) without heaps, which is optimal for dense graphs, and O(IE| log
IVD using binary heaps.
Example 1: For the following graph construct MST using prim's algorithm.
la
@ Einstein College Of Engineering [123/159]FE36-Data Structure and Algorithms IEEE
vertex Initial State
known 4, »,
A ° ° °
8 ° x °
c ° wn °
> ° wn °
Undirected Graph &
INITIAL CONFIGURATION
Step 2:
Here, °A' fg taken as source vertex and marked as visited. Then the distance ofits adjacent vertex Is
updated as follows :
T[B).dist = Min (T[BLDist, C,
s)— TID].dist = Min [ID}Dist, Cy] TIC.dist
in [TECI.Dist, Ca}
= Min ( 20, 2) = Min (00, 4) = Min (op + 3)
2 =3
°@) vertex After ‘A’ Is marked visited
: known 4, »
A 1 ° °
8 ° 2 A
c ° 3 A
> ° 1 A
Step 3:
Next, vertex "' with minimum distance is marked as visited and the distance of its unknown adjacent
vertex is updated,
T[B] Dist = Min (T[B]-Dist, Cy]
in (2, 2)
T[Cl.dist = Min [TICI.DIst, Cae]
Min (3,4)
-1
@ Einstein College Of Engineering [124/159]FE36-Data Structure and Algorithms WEEE
0
2 vertex ‘After D' Is marked visited
@ won ae ®
1
soe
°
°
1
Step 4:
Next, the vertex with minimum cost ‘C's marked as visited and the distance of Its unknown adjacent
vertex is updated
4 Vertex Aeris marked sted
@ ‘oom ah ’
:
0
0) 1
3 1 1
1
coer
pore
Step 5:
Since, there Is no unknown vertex adjacent to °C, there Is no updation in the distance. Finally, the
vortex "o! which Is not visited Is marked.
Vertex After °B’ Is marked visited Algorthm
° 2 > terminates,
known 4, %
1 °
1 2
@—o) : :
5 1 1 1
&)
©
pore
‘The final MST using prim’s algorithm is as follows:
‘The minimum cost of this spanning Tree Is 4 [le Cay 4 Coa + Ca
oe
@ Einstein College Of Engineering _—_—[125/159]FE36-Data Structure and Algorithms WEEE
Example 2: For the following graph construct MST using prim's algorithm.
vy} Va
0
7 3D
6
v
vertex Intl state
known %
vu 0 ° °
va ° ~ °
v3 0 ” 0
va ° ” °
vs 0 ~ 0
ve 0 ~ 0
w ° wo °
Vertex ter Vi is deciared known
know B
vi 1 0 3
ve 0 2 “i
v3 0 4 vi
vs 0 1 “i
vs ° ” °
@ @ e| we ° ” °
~ vw o aD o
&. =
TIVz]-dist = Min (TIVs]-dist, Cave] = Min, 21= 2
Min [TIVe}-dist, Cy.) = Min [eye
1
Theis
@ Einstein College Of Engineering [126/159]FE36-Data Structure and Algorithms WEEE
TIVsLcist = Min ETE dst, Cra] = Min a0 A) = 4
step a: 7
@ Vertex ter Vis deciared known
oom
ut 1 ° 0
@-—- v2 o 2 vi
2) “3 0 2
“a 1 1 ow
“5 0 7M
w 0 sow
Here 'V4" Is marked as visited and then the distance ofits adjacent vertex is updated.
Veter ter Vas deed tnown
‘Step 4: known dq BR
“oo ee
_ % 0 2
0
re
8 4
Hor V2' mrad 26 vied and then the dtnce os ajacent vertex upeted 0 flows;
‘TlV.].dist = Min [T[V4].dist, Cyo,y4] = Min [1, 3] = 4.
‘T[Vs].dist = Min [T[Vs]-dist, Cyz, ys] = Min [7, 10] - 7
steoss , 7]
p VY: Vertex After V3 is declared known
on
wor GG
rr
0
i rn
@ Einstein College Of Engineering _—_—[127/159]FE36-Data Structure and Algorithms WEEE
Here °V3" is marked as visited and then the distance of its adjacent vertex is updated as Follows;
TIVel-dist = Min [TIVs]-dist, Crave] = Min (8, 5] = 5
Step 6: 0; 2 vertex After V7 Is declared known
A ‘known: a, Pe,
N “ 1 0 0
2) Ws wv) 6 va 1 2 va
t v4 1 1 vi
At v5 o 6 vw
1@)—L1_ Ga ve 0 1 vw
w 1 4 “a
Here ‘V7’ s marked a5 visited and then the dtence of ts adjacent vertex is updated 2 fllows;
‘T[V¢]-dist = Min [T[V¢]-dist, Cyzye] = Min [5, 1] = 1
‘TlVo]-dist = Min [T[Ve]-dist, Cys) lin (7, 6)
step 7:
9 2 Vv: 2 vertex after V6 is declared known
known ®
“i 1 0 0
© va 1 2 vi
va 1 2 va
va 1 1 vi
vs 0 5 w
ve 1 1 w
w 1 4 va
Here ‘Vo’ is marked a8 vsited and then the dtance ofits adjacent vertex is updated
Step 8:
Vertex After VS is decared known
known %
“i 1 0 8
va 1 2 vi
20) W)5) vs 1 2 va
vs 1 1 vi
6 v5 1 5 wr
ve 1 1 w
w 1 4 “a
@ Einstein College Of Engineering _—_—[128/159]FE36-Data Structure and Algorithms WEEE
Here 'V5' Is marked as visited and then the distance of ts adjacent vertex is updated.
‘The final MST is;
Minimum Spanning Tree
Cost is 16 for the given
Undirected graph,
Implementation of Prim’s Algorithm:
#include
Finelude
void main()
{
float Icost{8],a18)[81;
Int clost(8],1/kamin,ns
clrser();
priner(“enter the no. of vertices");
printf("input weighted matrix");
for(i=1;1<=n3l++)
As=nge4)
96 a1);
i<=njie4)
{
leost{i}=al1 100;
clost{i}-1;
?
printi(*minimum cost spanning tree edges ares\n");
for(|-2;1<=n;+ +1)
t
min-leost(2];
ke2;
forti-3;3<—n;++)
'f(\cost{j}
printi("%a\ d\n", clost(k));
leosttk
forti=2ii<=ni+ +)
M((aLe]{s} Num (v) then the edge (v, w) Is a back edge.
‘Tree edge(V.W)
w
‘Num(V) = 1
ROUTINE TO COMPUTE LOW AND TEST FOR ARTICULATION POINTS
@ Einstein College Of Engineering _—_—[134/159]FE36-Data Structure and Algorithms WEEE
void Assigntow (Vertex V)
{
Vertex W;
Low [V] = Num [V]; /* Rule 1 */
for each W adjacent to V
{
If (Num (W] > Num [V]) /* forward edge */
«
Assian Low (Ws
Xf (Low [W]> = Num [])
Print ("% V is an articulation pt \n", V);
Lowlv] = Min (Low [V1], Low{Vl); /* Rule 34/
>
Ele
1 (parent [Vv]
Low (VI
= W) /* Back edge */
Min (Low [V], Num [WI)); /* Rule 2*/
y
Low can be computed by performing a postorder traversal of the depth - first spanning tree. (ie)
Low (F) = Min (Num (F), Num (0))
/* since there 1s no sree edge & only one back cage */
in (6, 4) = 4
Low (F) = 4
Low (E) = Min (Num (E), Low (FY)
7 there Is no back edge */
= min (5,4) = 4
Low (D) = Min (Num (D), Low (E), Num (A))
Min (4,4,1) = 4
tow (0)
Low (G) = Min (Num (G)) = 7 /* Since there Is no tree edge & back edge */
Low (C) = Min (Num (C), Low (D), Low (G))
Min (3,4,7) = 4
tow (©) =
UW Low (B) ~ Min (Munn (D), Low (CD)
= Min (2,1) = 1
Low (A) = Min (Num (A), Low (B))
= Min 1,1) = 3
Low (A) = 4.
From fig (4.8) It Is clear that Low (G) > Num (C) (le) 7 > 3 /* If Low (W) Num (V)*/ the *V" Is an articulation
pt Therefore °C! is an articulation point.
@ Einstein College Of Engineering _—_—[135/159]FE36-Data Structure and Algorithms WEEE
WY Low (E) = Num (D), Hence D Is an articulation point,
4.8.4 Euler Circs
‘Visiting all the edges only one with different start and end vertex is a path, otherwise itis a circu.
1F the condition "Each vertex has an even degree and the graph is connected", is satisfled, then the
(raph have an Euler circuit
‘DFS considers each edge once and all vertices while Euler visit ther.
DFS visits a vertex only once, but Euler circuit may visit more than once.
+ Suppose we make DFS visit edges In order that it considers, derives a Euler circuit.
Example: aw
B
Suppose we start from C, we would go C, A, F.C
6
Now we stuck In ©
Now backtrack to F and process F, D, G, F. Wenow stuck in F.
@ Einstein CollegeFE36-Data Structure and Algorithms WEEE
Splice the paths CAFC and FDGF, we get C,A, F, D, F, C.
% Now backtrack to G and process G, E, B, A, D, B, G
Splice the path with previous, we gat:
CAF, D, GE, B, A, D, B, GF, C
‘As a result no more edges are left unvisited and we have obtained an Euler crcult, having a running
time Ofe#n).
4.8.5 Hamiltonian Circuit Problem:
& A Hamiltonian path is a path in an undirected graph which visits each vertex exactly once and also
returns to the starting vertex.
% Determining whether such paths and cycles exist in graphs Is the Hamiltonian path problem which Is
NP-complete.
‘These paths and cycles are named after William Rowan Hamiltonian.
Consider a Graph G;
% The problem of finding Hamiltonian cycle is solved by backtracking approach.
'% We consider the vertex ‘A’ as the root. From vertex "A’ we have three ways, so we resolve the tie
Using alphabet order, so we select vortex'B'.
& From'B’ algorithm proceeds to C’, then to 'D’, then to “E’ and finally to"F” which proves to be @ dead
end,
% So the algorithm backtracks from ‘F” to", then to
pursue,
and then to 'C’, which provides alternative to
Going from ‘C’ to which Is useless, and the algorithm has to backtrack from ‘E* to 'C’ and then to
°B'. From there it goes to the vertices 'F,‘E, ‘C’ &'D’, from which It return to "A, yielding the
Homiltunion Cea A, B, FE, GD, A.FE36-Data Structure and Algorithms WEEE
UNIT V :ALGORITHM DESIGN AND ANALYSIS
Greedy algorithms ~ Divide and conquer ~ Dynamic programming ~ backtracking ~ branch and bound —
Randomized algorithms ~ algorithm analysis ~ asymptotic notations - recurrences ~ NP-complete problems
unit V
5.1 Greedy Algorithm
Greedy algorithms work in phases. In each phase, a decision Is made that appears to be good, without
regard for future consequences(cost). This means that some Joca/ optimum ts chosen. This "take what you
can get now" strategy Is the source of the name for this dass of algorithms. When the algorithm terminates,
‘we hope that the local optimum Is equal to the global optimum. If this Is the case, then the algorithm Is.
correct; omerwise, che algonthm nas produced a suboptimal solution. 1f the absolute best answer Is not
required, then simple greedy algorithms are sometimes used to generate approximate answers, rather than
using the more complicated algorithms generally required to generate an exact answer
To make a greedy algorithm, we have to concentrate on two sets:
1. One for candidate chosen
2. Another for candidates that are nat chosen
‘Add the candidate we chose to the chosen set if we have a solution, else we Iterate through loop and get
the next optimal candidate,
+ Greedy algorithms are simple and straightforward.
‘+ ‘They are shortsighted in thelr approach in the sense that they take decisions on the basis,
of Information at hand without worrying about the effect these decisions may have in the
future
+ They are easy to Implement, invent and efficient. They are used to solve optimization
robles
5.1.4 Greedy approach for Making Change Problem:
Greedy algorithm works by making the decision that seems most promising at any moment. It never
reconsiders this decision, whatever situation may arise later. Consider the problem of *Making Change”:
Coins available are:
+ Dollars (100 cents)
Quarters (25 cents)
@ Einstein College Of Engineering _—_—[138/159]FE36-Data Structure and Algorithms WEEE
Dimes (10 cents)
+ Nickels (5 cents)
+ Pennies (1 cent)
“The problem Is to make a change of a given amourt using the smallest possible number of coins.
The informal algorithm is:
+ Start with nothing
+ At every stage without passing the civen amount,
+ Add the largest to the coins already chosen
Formal algorithm for make change for n units using the least possible number of coins:
Make-Change (n)
C€ (100, 25, 10, 5, 195 Uf constant
sole OF // Set that will hold the solution set
sum €0 // sum of item in solution set
WHILE sum not =
x = largest item in set C such that sum +x =n
AF no such item THEN
RETURN "No Solution”
SE S{value of x}
‘Sue sum +x
RETURN S
ena;
‘The problem asks to provide a change for a specified amount, using @ minimal number of coins. A greedy
algorithm may at each stage employ the criteria of providing the largest available coin which is not greater
than the amount still owed
To construct the solution In an optimal way, let us see how the greedy algorithm works:
+ Algorithen contains two sets; 1. Chosen items 2. Rejected items
+ Consists of four functions:
© A function that checks whether chosen set of tems provides a solution.
© Aunetion that checks the feastolity of a set.
© The selection function tells which of the candidate is the most promising
(© An objective function, which does not appear explicitly, gives the value of a solution.
‘The greedy algorithm Is shown below:
procedure GreedyMaking Change
jally Une set uf clavate fa etna
i.e solution set
At each step
Item will be added in a solution set by using selection function.
IF the set would be longer be feasible
reject them under consideration and Is never consider again
ELSE IF set is still feasible THEN
@ Einstein College Of Engineering _—_—[139/159]FE36-Data Structure and Algorithms WEEE
‘add the current ITEM,
End; // Procedure GreedyMaking Change
5.1.2 Eoyptian Fractions:
Ayryga
An Egyptian fraction is the sum of distinct unit fractions, such as 2" 3 ' 16, That Is, each
{taction in the expression has a gumerator equal to 1 and a denominator that is a positive integer, and all
Ure desninatins ier foun eau olen
‘The sum of an expression of this type Is @ positive rational number a/b; for instance the Egyptian fraction
above sums to 42/48. Every postive rational number can be represented by an Egyptian fraction.
{A greedy algorithm, for nding such a representation, can add at each stage to the sum of the largest
unit fraction which does not cause the sum to exceed the fraction.
cn
110 =
1
Bx: tet
5.1.2 Map Coloring:the map coloring problem asks to assign colors to the different reglons, where adjacent
regions are not allowed to have the same color. There is no general algorithm to assign minimal number of
colors, yet each map has an assignment which uses no more than four coors
The greedy approach, repeatedly choose a new color and assign it to as many regions as possible
5.1.4 Shortest Path Algorithm:
Cotablish the shortest path between a single source node and all ofthe other nodes in 9 graph
Greedy algorthm: Add an edge that connects a new node to those already selected. The path from the
source tothe new node should be the shortest among al the paths to nodes that have not been selected yet.
Greedy Approach:
4 Weneed an array to hold the shortest distance to each node, and a visited set
4 We take neighboring node; get the direct distance from i to the root node.
& Next we have to check Ii is fess than the entry in the array or if it not less than the entry inthe
array oF if the arvay has null value, then we store itn the array.
+ From that node, put the neighboring node in the visited set. Then we visit every other node itis
connected to and also calculate the distance from It to the root node
If itis less than the entry inthe array ot ifthe array has nal value, then we store iti the array
® After we are finished with that node we go to the next connected node, and so on.
4 At the end we will have an array of values representing the shortest distance from the staring
code to ovary othor node inthe graph.
4 This algorithm is also known as Difkstra’s algorithm.
@ Einstein College Of Engineering [140/159]FE36-Data Structure and Algorithms WEEE
Algorithm ShortestPaths(v, cost, dist, n)
J/ distli], 1) (dist{ul + cost{u,w])) then J Update distances
dist{w] = dist{ul + cost{u.w];
>
,
5.4.5 Greedy approach for Knapsack Problem:
Description: Given a set of items each with a cost and a value, determine the number of each item to
Include in a collection so that the total cost does not exceed some given cost and the total value is as large
as possible.
We are given n objects and a knapsack. Object | has @ weight w; and the knapsack has @ capacity m.
‘The problom Is that a thiof robbing a store and can carry @ maximal weight of w into their knapsack. What
items should thief take?
‘There are two version of problem:
1. Fractional knapsack problem by Greedy Algoritim which solves the problem by putting items into the
knapsack one-by-one. This approach Is greedy because once an item has been put into the knapsack,
It ie never removed.
2. 0~ 1 knapsack problem by Dynamic programming algorithm
We con apply yreealy aupnuauts Ly sulve i
The Greedy solution to the Fractional Knapsack preblem is;
L.lf a fraction x, 0 U) then break;
1.0;
U=U-witls
>
li(in) then x{i] = U/W;
>
Procedure
Procedure GroadyFractionaiknapsack(w, v, W)
FORI=1yondo
xf
weie!
while(wetghtvatue)
return BinarySearch(A, value, low, mid-S)
else if(agmid] < value)
return binarySearch(A, value, mid-+4, high)
else
return mid Y found
2
We can eliminate the recursion by Initializing the low = 0 and high = 0-4
/* Non-recursive procedure for Binary search *f
BinarySearch(A[0...n-L}, value)
‘
low = 0, high = 0-1;
white (low <= high)
t
‘mid = (low + high) /2
if (Almid} > value)
high = mid -1
else ir carmia < varue)
ow = mid +1
else
return mid Ui found
}
return 1 Uf not found
o
5.2.2 Merge Sort _- Divide and conquer algorithm.
+ DIVIDE: Partition the n- element sequence to be sorted into two subsequences of n/2 elements each
+ CONQUER: Sort the two subsequences recursively using the mergesort.
+ COMBINE: Merge the two sorted subsequences of size n/2 each to produce the sorted sequence
consisting of n elements,
Overall pres
1. Split the original list into two halves.
2. Sort each half using merge sort
3. Merge the two sorted halves together Into a single sorted list.
procedure mergesort(first, last, array)
mid = (first + last)/2
@ Einstein College Of Engineering _—_—[144/159]FE36-Data Structure and Algorithms WEEE
mergsort(frst, mid, array)
mergsort(mid-+1, last, array)
rejoin_two_halves(smid, array)
end mergesort
‘The time complexity of the algorithm satisfies the recurrence equation
+ T(n) = 21(n/2) + O(a) > Fars
+ T(n)= 09 ito=t
whose solution is T(n) = 0 (n log n),
5.2.3 Quick Sort ~ Divide and conquer algorithm
Here recombine step requires no additional work.
1, The essence of quick sort fs to sort an array All] ...ALn] by picking some key value 'v'in the array as
pivot element around which to rearrange the elements in the array.
2. The pivot Is near the median key value in the array, s0 that itis preceded by about half the keys and
followed by about half.
3. We process the execution of elements in the stray so that for some j, all the records with keys less
than ‘v’ appears in A[3], A[2] .... ALi] and all those with keys ‘v’ or greater than ‘v’ appear in
A+] At]
4. Apply quick sort to the newly formed array ALI]... ALi] and A[j+1] .... Afni]
Since all the keys in the first group precede all the keys in the second group, thus the entire array be
sorted.
6. Sets of cardinality greater than one are partitioned into two subsets, and then the algorithm is,
recursively invoked on the subsets.
‘The partitioning uses a key from the set as pivot
‘8. Values smaller than the pivot are sent one subset, and values greater than the pivot are sent to the
other subset.
9. For randomly chosen pivots, the expected running of the algorithm satisfies recurrence equation.
T(N) = TG) + (NH) + CN
+ The running time of quick sort is equal to the running time of the two recursive calls plus the linear
tme spent in the partition.
+ The pivot selection takes constant time only,
Algorithm:
void quicksort(int s[], int I, nth)
t
int p; /* index of partition */
FQh=D> 0)¢
= partition(s, |, h);
quicksort(s, |, p- 1);
guicksort(s, p + 1, h)s
@ Einstein College Of Engineering [145/159]FE36-Data Structure and Algorithms WEEE
y
Int partition(int sf], nt J nth)
{
inti;
int p; /* pivot element index */
Int frsthigh; /* divider position for pivot element */
firsthig
for (I= chs be)
(SE < stp)
swap(sft], &stfirsthigh));
firsthigh++;
>
swap(8s[p], &s{firsthigh);
return{firsthigh);
y
void swap(int *a, int *b)
{
Int x:
tas tb
b= x
y
Analysis of Quick sort:
Worst case > TIN) = O(N2)
Best case > T(N) = O(N log N)
Average case > T(N) = O(N loa N)
5.2.4 Matrix Multiplication ~ Divide and Conquer Algorithm
The given problem has two N by N matrices A and 8 and we have to compute C = AXxB,
a “| = | “i B, Be |
tne dae arb SiaSerernatipeind ft nfticanon as shown blow
Pa — (aaa + azz) (D4 + 022)
2 = (A21+A22)B11
9 = A13(B12-B22)
4 — a22(B12-B22)
5 = (A11+A12)822
Po = (A21-A11) (B114812)
7 = (A12-A22) (B21 + 822)
@ Einstein College Of Engineering _—_—[146/159]FE36-Data Structure and Algorithms WEEE
C11 = Pi + Pa PS +7
12 = P34 PS
cai =p2+Pa
22 = Pi + Pa~ 2 + Pe
Tn) = 71(0/2) + en > naa
Tiny =
T(n) = O(nles 7)
o(n2.8t)
5.4 Dynamic programming
+ Dynamic programming is a method of solving problems exhibiting the properties of overlapping
subproblems and optimal substructure which takes less time.
+ By this way a solutions may be viewed as a result of a sequence of decisions.
+ Dynamic programming takes advantage for the duplication and arranges to solve each subproblem
only once, saving the solution for later use.
+The idea of dynamic programming is to avoid calculating the same stuff twice.
+ A-dynamic programming is a bottom-up approach. It means;
1 Start with the smallest sub problems.
2. Combining thelr solutions, obtain the solutions to subproblems of increasing size.
3. Until arrive at the solution of the original preblem,
basic idea of aynamic programming
+ Start with an inefficient recursive algorithm,
+ Speed it up by only solving each subproblem once and remembering the solution later use.
+ May rearrange the order of subproblem computation to be more convenient.
Principle of Optimality
+ The dynamic programming relies on a principle of optimality, which states that an optimal
sequence of decisions or choices has the property that whatever the initial state and
decision are, the remaining decisions must constitute an optimal decision sequence with
regard to the state resulting from the first decision.
+ Thus, the essential difference between the greedy method and dynamic programming is that in the
greedy method only one decision sequence is ever generated. In dynamic programming, many
decision sequences may be generated. However, sequences containing suboptimal subsequences
Lonut be uptinral(il Une principhe uf uptinality helde) and 20 will nt be generates.
+ Ex, in matrix chain multiplication problem
+ The principle can be related as follows: the optimal solution to a problem is a combination of optimal
solutions to some of its subproblems,
+The difficulty in turning the principle of optimality into an algorithm is that It Is not usually obvious
Which subproblems are relevant to the problem under consideration,
5.4.1 0-1 knapsack problem
@ Einstein College Of Engineering _—_—[147/159]FE36-Data Structure and Algorithms WEEE
+ The problem statement is that a thief robbing a store and can carry @ maximal weight of W into thelr
knapsack.
+ There are n items and i* item weight w; and is worth v; dollars.
+ What items should thief take? There are two versions of problem;
1. Fractional knapsack problem which dealed in greedy algorithm,
2.04 knapsack problem:
The setup is same, but the items may not be broken into smaller pieces, so the thief decide either to
take an item or to leave it(binary choice), but may not take @ fraction of an item. 0-1 knapsack problem has
the following properties:
4. Exhibit no greedy choice property
2. Exhibit optimal substructure property.
3. Only dynamic programming algorithm exists.
‘Step: Structure: Characterize the structure of an optimal solution,
= Decompose the problem into smaller problems, and find a relation between the structure of the
‘optimal solution of the ariginal problem and the solutions of the smaller problems.
‘Step2: Principle of Optimality: Recursively define the value of an optimal solution.
= Express the solution of the original problem in terms of optimal solutions for smallor problems.
‘Step 3: Bottom-up computation: Compute the value of an optimal solution in a bottom-up fashion by
Using a table structure,
‘Step 4: Construction of optimal solution: Construct an optimal solution from computed information.
‘steps 5 and 4 may often be combined.
Remarks on the Dynamic Programming Approach
Steps 1-3 form the basis of a dynamic-programming solution to a problem,
Step 4 can be omitted if only the value of an optimal solution is required.
Developing a DP Algorithm for Knapsack
‘Step 1: Decompose the problem into smaller problems,
We construct an array V10."0.1V] For 1 < ¢ w, then vj-+V[i—1, ww) =
‘so the lemma is correct in any case.
The Dynamic Programming Algorithm]
KrapSeck(o, 1,
‘ 010) ¥[0,u] =
it(wfi] <
Visa] = max(v [i yuh ofl + VEE ~ 150 ~ wll
ese
Vficw] = VE ~ tou
return Vin WI;
Time complexity: Clearly, O(nW’)
Constructing the Optimal Solution
The algorithm for computing VIJ,W] described In the previous slide does not keep record of which
subset of items gives the optimal solution,
To compute the actual subset, we can add an auxiliary boolean array keepliiw] which is 1 if we
decide to take the file in V{I,W] and 0 otherwise.
@ Einstein College Of Engineering _—_—[149/159]FE36-Data Structure and Algorithms
[The Complete Algorithm for the Knapsack Problem]
KnapSack(v, w,n,W)
{
{or (w
to W) VIO, w] =
Lion)
Oto Ww)
w) and (fi) + VUE 1, w — wll] > VIe— 1,0)
VU,u) = oli] + VEE = 1,00 = wlll;
keepfi,w] = 1)
aise
Vii,w) = Vit;
keepli,o] = 0;
kau:
{or (j = 1 downto 1)
Hf (keepfi, 1
{
output
K=K-vfil
reuen Vin 1
5.4.2 Eibonnaci Numbers
1a ={ {0-949 ifi>1
‘A top-down approach of computing, say, £(5) is inefficient do to repeated subcomputations.
1)
Ay 1)
/Q) 4Q) @ i)
#@) 70) 70) 0) FQ) 0)
FO) 10)
‘A bottom up approach computes F(0), f(4), f(2), (2), fA), (5) In the leted ordar.
Greedy Approach VS Dynamic Programming (DP)
+ Greedy and Dynamic Programming are methods for solving optimization problems.
+ Greedy algorithms are usually more efficient than DP solutions.
+ However, often you need to use dynamic programming since the optimal solution cannot be
guaranteed by a greedy algorithm.
@ Einstein College Of Engineering [150/159]FE36-Data Structure and Algorithms WEEE
+ DP provides efficent solutions for some problems for which a brute force approach would be very
slow.
+ To use Dynamic Programming we need only show that the principle of optimality applies to the
problem,
5.5 Backtracking Algorithm:
Backtracking is © general algorithm for finding all (or some) solutions to some computational
problem, that incrementally builds cendidates to the solutions, and abandons each partial candidate ¢
(rbacktracks") as soon as it determines that ¢ cannot possibly be comploted to @ valid solution.
The classic textbook example of the use of backtracking Is the eight queens puzzle, that asks for all
arrangements of eight queens on a standard chessboard so that no queen attacks any other. In the common
backtracking approach, the partial candidates are arrangements of k queens in the frst k rows of the board,
all in different rows and columns. Any partial solution that contains two mutually attacking queens can be
abandoned, since It cannot possibly be completed to a valid solution.
acktracking can be applied only for problems which admit the concept of a “partial candidate
solution” and a relatively quick test of whether it can possibly be completed to a valid solution. It Is useless,
or example, for locating @ given value in an unordered table. When It Is applicable, however, backtracking Is
‘often much faster than brute force enumeration of all complete candidates, since It can eliminate a large
number of candidates with a single test.
18) Solving a maze
= Given 3 maze, find a path trom stare to nnish
= At each intersection, you have to decide between three or fewer choices:
Go straight
= Go lett
= Go right
You don’t have enough information to choose correctly
Each choice leads to another set of choices
One or more sequences of choices may (or may not) lead to a solution
Many types of maze problem can be solved with backtracking
b) Coloring a map
‘You wish to color a map with not more than four colors red, yellow, green, blue
‘Adjacent countries must be in different colors
You don’t have enough information to choose colors
aul ose Kenly ly cna sel uf clivives
(One oF more sequences of cholces may (or may not) lead to a solution
|= Many coloring problems can be solved with backtracking
¢) Solving puzzle
In this puzzle, all holes but one are filled with white pegs
= You can jump over one peg with another
Jumped pags are removed
@ Einstein College Of Engineering _—_—[151/159]FE36-Data Structure and Algorithms WEEE
The object is to remove all but the last peg
‘You don’t have enough information to Jump correctly
Each choice leads to another set of choices
ne oF more sequences of chotces may (or may not) lead to a solution
|| Many kinds of puzzle can be solved with backtracking
Terminology 1
‘A toe is composed of nodes
‘The backtracking algorithm
Backtracking |s really quite simple--we “explore” each node, as follows:
= 10 “explore” node nz
1. TFN Is a goal node, return “success”
2. If Nis a leaf node, return “failure”
3. For each child C of N,
3.1. Explore ©
3.1.1. IFC was success, return “success”
4, Return “allure”
4) 8-Queen Problem:
In the Eight Queens problem the challenge is to place eight queens pleces from the game of Chess on a
‘chessboard so that no queen plece Is threatening another queen on the board. In the game of chess the
‘queen Is a powerful piece and has the ability to attack any other playing piece positioned anywhere else on
the same row, column, or diagonals. This makes the challenge quite tricky for humans who often declare
after several falled attempts *..there can’t be any solution!”
However Unete ane i faut
Ly-tmy valid sulations to the problem although many of those nincty
‘two are symmetrical mirrors, All ofthe solutions can be found using a recursive backtracking algorithm. The
algorithm works by placing queens on various positions, adding one at a time until either eight queens have
been placed on the chess board or less than elght queens are on the board but there are no more safe
positions left on the board. When the latter situation is reached the algorithm backtracks and tries another
layout of queens.
@ Einstein College Of Engineering [152/159]FE36-Data Structure and Algorithms WEEE
NOTES: A queen can attack horizontally, vertically, and on both diagonals, so its pretty hard to place
several queens on one board so that they don’t attack each other.
a
“\™
‘The nm Queens problem:
Given Is a board of n by n squares. Is It possible to place m queens (that behave exactly ike chess queens)
fon this board, without having any one of them attack any other queen?
Solutions to the eight queens puzzle
‘The eight queens puzzle has 92 distinct solutions. 1f solutions that differ only by symmetry operations
(rotations an¢ reflections) of the board are counted as one, the puzzle has 12 unique (or fundamental)
solutions, which are presented below:
abedefeh
abedefeh abe de fe h
SeGbaawe
abedefgh abedefgh abedefgh
Unique solution 1 Unique solution 2 ‘Unique solution 3
abedefg¢h abedefeh abedefeph
Seo Sas ae
ee 1 ee 1
abedefgh abcodefgh abedefgh
Unique solution 4 Unique sotution Unique solution 6
abedefeh abedefeh abedefeh
@ Einstein College Of Engineering —_[153/159]FE36-Data Structure and Algorithms WEEE
1
abedefgh abecdefgh abedefgeh
Unique solution 7 Unique sofution & Unique solution 9
abedef
1
abedefgh abedefgh abedefgh
Unique solution 10 ‘Unique solution 11
Unique solution 12
Procedure BacktrackingQueenProblem
queen|0, 0, 0, 0, 0, 0, 0, 0 | { has 8 entries, all have value 0 }
Int EightQueen() return array[1..8]
count € 0
queen(o] € 0
repeat until queen[0] <8 do
IFis_row_good(0) = YES
queen[0]é queen[0] + 1
queent1] € 0
repeat until queen 1] < 8 do
Iis_row_good(o) = YES
queen{1]€ queen] + 1
queen[2] € 0
repeat until queen{2] < 8 do
18 row_goodty) = Yes
queen[2}¢ queen[2] + 1
queen(3] € 0
repeat until queen[3] < 8 do
IF is_row_good(o) = YES
queen(3}€ queen({3] +1
queen(4] € 0
@Einstein College Of Engineering _—_[184/159]FE36-Data Structure and Algorithms WEEE
repeat until queen[4] <8 do
IF 1s_row_good(o) = YES
queen(4]€ queen(4] + 1
queen{5] € 0
repeat until queen{5] < 8 do
if fe_row_good(0) ~ YES
queen[5}€ queen(5] + 1
queen(6] © 0
repeat until queen[6] < 8 do
IF ts_row_gooa(o) = YES
queen(6]€ queen[6] + 1
queent7] <0
repeat until queen[7] < 8 do
IF is_row_good(a) = YES
queen[7}€ queen[7] + 1
‘count € count +1
End; //Procedure
5.6 Branch and Bound algorithm
Branch and Bound algorithms are similar to backtracking algorithms; however we are looking for an
‘opumal solution. Lost 1s usually involved; we want to find the lowest cost solution. This algorithm usually
‘grows in a breadth first search manner,
All permutations of 2 decision are examined, and we follow on the one that has the potential to give the
lowest cost or highest valued solutions, i it falls we consider the other decisions.
‘Optimal Solution for TSP using Branch and Bound
‘The Travelling Salesman Problem (TSP) 's an NP-hard problem in combinatorial optimization
studied in operations research and theoretical computer science. Given a list of cities and thelr pairwise
distances, the task is to find a shortest possible tour that visits each city exactly once,
The problem was first formulated as a mathematical problem in 1930 and is one of the most intensively
studied problems In optimization. 1t Is used as a benchmark for many optimization methods. Even though the
problem is comoutationally difficult. @ large number of heuristics and exact methods are known, so that some
instances with tens of thousands of cities can be solved
The TSP has several applications even in its purest formulation, such as plannina, logistics, and the
manufacture of sniciochips. Slightly modified, it appears as a sub-problem in many areas, such as DNA
‘sequencing. In these applications, the concept city represents, for example, customers, soldering points, or
DNA fragments, and the concept distance represents travelling times or cost, or a similarity measure
between DNA fragments. In many applications, addtional constraints such as limited resources or time
windows make the problem considerably harder.
@ Einstein College Of Engineering _—_—[185/159]FE36-Data Structure and Algorithms WEEE
In the theory of computational complexity, the decision version of the TSP belongs to the class of NP-
somplete problems. Thus, itis likely that the worst case sunning time for any algorithm for the TSP increases
exponentially with the number of cities
The Symmetric Travelling Salesman problem:
‘A map over the Danish island Bornholm is given together with a distance table showing the distances,
between major citles/tourist attra
points, is to find a tour of minimum length starting and ending in the same city, and visiting each other city
‘exactly once, Such a tour Is called a Hamilton cycle. The problem Is called the symmetric Travelling Salesman.
problem (TSP) since the table of distances fs symmetric. In general a symmetric TSP is given by @ symmetric
1s. The problem of a biking tourist, who wants to visit all these major
1x fr matrix D of non-negative distances, and the goal is to find 2 Hamilton tour of minimum length. In
terms of graphs, we consider a complete undirected graph with n vertices Kn and non-negative lengths
assigned to the edges, and the goal is to determine @ Hamilton tour of minimum length. The problem may
also be stated mathematically by using decision variables to describe which edges are to be included in the
tour. We introduce 0-1 variables xij ; 1 _/ < jn, and interpret the value 0 (1 resp.) to mean *not in tour”
(Cin tour" resp.) The problem is then
min > dysy
sch that
Sant Sawn ic tn
Dwc| vczcv
By 10N KEM np
@ Einstein College Of Engineering [156/159]FE36-Data Structure and Algorithms WEEE
Figure 4: A potential, but not feasible solution to the biking tourist's problem
The first set of constraints ensures that for each i exactly two variables corresponding to edges
incident with 7 are chosen. Since each edge has two endpoints, this implies that exactly n variables are
allowed to take the value 1. The second set of constraints consists of the subtour elimination constraints.
Each of these states for a specific subset $ of V that the number of edges connecting vertices in S has to be
less than [S| thereby ruling out that these form @ subtour. Unfortunately there are exponentially many of
Unse cunsbiainls, The yivest wanstiaiuls delsiesive the set of feasible solutions S. One obvious way of
relaxing this to a set of potential solutions is to relax (Le. discard) the subtour elimination constrains. The set
‘of potential solutions P is then the family of all sets of subtours such that each i belongs to exactly one of the
ssubtours in each set in the family, of.
Another possibility Is decribed, which In a B&B context turns out to be more appropriate. A,
subproblem of given symmetric TSP is constructed by deciding for a subset A of the edges of G that these
must be included in the tour to be constructed, while for another subset 8 the edges are excluded from the
tour, Exclusion of an edge (i; /) is usually modeled by setting ci to 1, whereas the inclusion of an edge can
be handled in various ways as e.g. graph contraction. The number of feasible solutions to the problem is
(0-1)! 72, which for n= 50 fs appr. 3x10
5. 7 THE ANALYSIS OF ALGORITHMS
‘The analysis of algorithms fs made considering both qualitative and quantitative aspects to get a solution
thot Io cconomlcal in the use of computing and human rasources which Impraves the perfermance of an
algorithm,
‘A good algorithm usually posses the following qualities and capabilities:
41. They are simple but powerful and general solutions.
2. They are user friendly
3, They can be easily updated.
4, They are correct.
@ Einstein College Of Engineering _—[157/159]FE36-Data Structure and Algorithms WEEE
‘They are able to be understood on a number of levels.
They are economical in the use of computer “ime, storage and peripherals.
‘They are well documented
‘They are independent to run on a particular computer.
9, They can be used as subprocedures for other problems.
10. The solution is pleasing and satisfying to its designer.
‘Computational Complexity
‘The computational complexity can be measured in terms of space and time required by an algorithm,
‘Space Complexity
‘The space complexity of an algorithm is the amount of memory It needs to run the algorithm,
Time Complexity
‘The time complexity of an algorithm is the amount of time it needs to run the algorithm. The time taken
by the program is the sum of the compile time and run time. To make a quantitative measure of an
algorithm's performance, the computational model must capture the essence of the computation while at the
‘same time it must be divorced from any programming language.
Big Oh (0) ~ Upper Bound:
uN)
5.8 Asymtotic Notation
Asymtotie notations are method used to estimate and represent the efficiency of an algorithm using
simple formula. This can be useful for seperating algorithms that leads to different amounts of work for large
inputs.
Comparing or classify functions that Ignores constant factors and small Inputs Is called as asymtotic growth
rate oF asymtotic order or simply the order of functions. Complexity of an algorithm is usually represented in
0,0, , notations.
Big - oh notation (0)
This is @ standard notation that has been developed to represent functions which bound the
computing time for algorithms and it Is used to defe the worst case running time of an algorithm and
concerned with very large values of W.
Definition : - T(N) = O({(N)}, if there are positive constants C and ng such that TIN) Cf(N) when N- ny
BIG - OMEGA NoTATION (Q)
This notation is used to describe the hest case running time of algorithms and concerned with very
large values of W.
Rofinition + ~ TC) = amega({(N)), if thera are positive constants C and ns such that TIN) CF(N) when Na
BIG - THETA NOTATION
‘This notation is used to describe the average case running time of algorithms and concerned with very large
values of n.
Definition : -T(N) = theta (F(N)), If there are positive constants C,, Cand ng such that
TON) = OCF(ND) and TEN) = (FN) for all Ning
Definition = ~ TOW
(F(N)), if there are positive constants C;, Cz and ng such that
@ Einstein College Of Engineering _—_—[18/159]FE36-Data Structure and Algorithms WEEE
TEN) = OCF(N)) and T(N) = (F(NN)) for all Nn,
Little - oh Notation (0)
‘This notation is used to describe the worstcase analysis of algorithms and concerned with small values of n,
Basic Asymptotic Efficiency Classes
‘Computing Time Name
0(1) constant
O(log n) Logarithmic function
2(n) Linear
0(n") quadratic,
0(n*) cubic
0(2") exponential
O(nlogn) n - Jog - n Logarithmic
nl factorial
WORST - CASE, BEST - CASE AND AVERAGE - CASE EFFICIENCIES
Worst - Case - Efficiency
‘The worst - case efficiency of an algorithm is its efficency for the worst - case Input of size n, which Is an
Input of size n for which the algorithm runs the longest among all possible Inputs of that size
Best - Case Efficiency
The best - case efficiency of an algorithm Is its efficiency for the best case input of size n, which is an input of
size n for which the algorithm runs the fastest among all possible inputs of that size.
Average - Case eMiciency
The average - case efficiency of an algorithm Is its efficiency for the random input of size n, which makes
‘some assumptions about possible inputs of size n
For example, let us consider sequential search
ALGORITHM
SequentialSearch (AfO..1-1],K)
{Input : An array A[O..0-1] and a search key k.
UJ Output : Returns the index of the first element of A that matches R or -1 if there are no matching
‘elements.
while 1