Data Structures and Algorithm
Data Structures and Algorithm
II EEE
=======================================================================
EE 36
Aim: To master the design 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 cursorbased 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 III :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 Kruskal's algorithms Depth-first traversal biconnectivity 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
1. M. A. Weiss, Data Structures and Algorithm Analysis in C, Pearson Education Asia, 2002.
2. ISRD Group, Data Structures using C, Tata McGraw-Hill Publishing Company Ltd., 2006.
REFERENCES
1. A. V. Aho, J. E. Hopcroft, and J. D. Ullman, Data Structures and Algorithms, Pearson Education, 1983.
2. R. F. Gilberg, B. A. Forouzan, Data Structures: A Pseudocode approach with C, Second Edition, Thomson
India Edition, 2005.
3. Sara Baase and A. Van Gelder, Computer Algorithms, Third Edition, Pearson Education, 2000.
4. T. H. Cormen, C. E. Leiserson, R. L. Rivest, and C. Stein, "Introduction to
algorithms", Second Edition, Prentice Hall of India Ltd, 2001.
=======================================================================
@ Einstein College Of Engineering
[1/159]
Queues
Graphs
Lists
Tables
Non-Primitive Data Structure : is one that data items are not operated closest
to machine level instruction.
2.1.
sequence
order.
Eg: Arrays, Lists, Stacks and Queues.
Operations performed on any linear structure:
1.
2.
3.
4.
5.
=======================================================================
@ Einstein College Of Engineering
[2/159]
presence.
Eg : Trees, Graphs.
Linear Data Structure
NonLinearDataStructures
2. List
1. Graph
a. AdjacencyList
a. Array
b. AdjacencyMatrix
i. One Dimensional
c. SpanningTree
ii. Multi-Dimensional
2. Tree
iii. Dynamic Array
a. MWayTree
iv. Matrix
i. BTree
1. Sparse Matrix
1. 234Tree
b. Linked List
2. B+Tree
i. Single Linked List
b. BinaryTree
i. BinarySearchTree
ii. Double Linked List
ii. SelfBalancingBinary
iii. Circular Linked List
SearchTree
c. Ordered List
1. AVLTree
i. Stack
2. RedBlackTree
ii. Queue
3. SplayTree
1. Circular Queue
iii. Heap
2. Priority Queue
1. MinHeap
iii. Deque
2. MaxHeap
3. BinaryHeap
3. Dictionary (Associative Array)
iv. ParseTree
a. Hash Table
Disadvantages
Array
Quick inserts
Slow search
Slow deletes
Fixed size
Slow inserts
Slow deletes
Fixed size
Stack
Queue
=======================================================================
@ Einstein College Of Engineering
[3/159]
Quick inserts
Slow search
Quick deletes
Binary Tree
Quick
Quick
inserts
Quick
deletes
Quick search
Tree
Quick inserts
Complex to implement
Quick deletes
(Tree always remains balanced)
2-3-4 Tree
Quick search
Complex to implement
Quick inserts
Quick deletes
(Tree always remains balanced)
(Similar trees good for disk storage)
Hash Table
Heap
Quick inserts
Quick deletes
Access to largest item
Graph
Some
algorithms
are
slow
and
very
complex
Associative array
Set
Stack
Queue
Tree
=======================================================================
@ Einstein College Of Engineering
[4/159]
A value is stored in an element of the array with the statement of the form,
Data[i] = X ; Where I is the valid index in the array
And X is the element
Extraction :
=======================================================================
@ Einstein College Of Engineering
[5/159]
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-1.
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.
The elements of an array A may be denoted by the subscript notation (or bracket
notation),
The number K in A[K] is called a subscript or an index and A[K] is called a subscripted
variable.
In other words, the number of subscripts gives the dimension of that array.
Two-dimensional Arrays :
9
A two-dimensional mXn array A is a collection of m*n data elements such that each
element is specified by a pair of integers (such as I, J), called subscripts, with the property
that,
0I<m
and
0J<n
The element of A with first subscript i and second subscript j will be denoted by,
A[i,j] or A[i][j] (c language)
There is a standard way of drawing a two-dimensional mXn array A where the elements of
A form a rectangular array with m rows and n columns and where the element A[i][j]
appears in row i and column j.
Rows
Columns
0
A[0][0]
A[0][1]
A[0][2]
A[1][0]
A[1][1]
A[1][2]
A[2][0]
A[2][1]
A[2][2]
=======================================================================
@ Einstein College Of Engineering
[6/159]
the
implementation.
This
generalization
of
operations
with
unspecified
A normal variable:
An Array variable:
int b;
int a[4];
2. Declaration of Operations
a[0]
a[1]
homogeneous elements. It is an
example for linear data structure.
b=6;
a[2]
a[3]
a[2]=6;
array index
int a, b, c, d, e;
Suppose you need to fined average of 100 numbers. What will you do? You have to declare 100
variables. For example:
int a, b, c, d, e, f, g, h, i, j, k, l, m, n... etc.,
An easier way is to declare an array of 100 integers:
int a[100];
The General Syntax is:
datatype array_name [size];
Example:
Subscript
int a[5];
=======================================================================
@ Einstein College Of Engineering
[7/159]
The array name will hold the address of the first element. It is called as BASE
ADDRESS of that array. The base address cant be modified during execution, because it
is static. It means that the increment /decrement operation would not work on the base
address.
Consider the first element is stored in the address of 1020. It will look like this,
1020
1022
12
0
9
1
1024
14
2
5
3
1026 1028
1
4
=======================================================================
@ Einstein College Of Engineering
[8/159]
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.
Disadvantages:
Wastage of memory space is possible. For example: Storing only 10 elements in a 100 size
array. Here, remaining 90 elements space is waste because these spaces cant be used by
other programs till this program completes its execution.
optional
struct tag
{
Dont forget the
Semicolon here
data-type member 1;
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 member1, meber2, , member m are individual member declarations.
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.
=======================================================================
@ Einstein College Of Engineering
[9/159]
=======================================================================
@ Einstein College Of Engineering
[10/159]
struct student
{
int regno;
char name[20];
char dept[10];
int year;
};
s1
s2
sN
The size of each of these variables is 34 bytes because the size of the student datatype is 34
bytes. And the memory will be allocated for each variable as follows:
34 bytes
2 bytes
Address
6100
20 bytes
6102
10 bytes
34 bytes
2 bytes
6132
6122
s1
2 bytes
name
regno
Address
1002
20 bytes
1004
10 bytes
dept
2 bytes
year
1034
1024
s2
name
regno
INITIALIZING STRUCTURE VARIABLES:
dept
year
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
commas.
The general form is,
storage-class struct tag variable = {value1, 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/159]
OR
This is an int
array. So each
location can be
accessed only
with help of
address only. So
the subscripts
are needed
int total;
float avg;
char result[5];
}stu;
This is a char
array but it is
used as string.
So no need to
worry about the
individual
location and their
addresses
stu
name array (size 20 bytes)
2190 2192
roll
name
2
3
mark
2224
total
avg
2228
result
stu.mark[3]
=======================================================================
@ Einstein College Of Engineering
[12/159]
struct bill
{
int cno;
char name[20];
float amt;
struct date
{
int day;
int month;
int year;
}billdate, paydate;
struct date
{
int day;
int month;
int year;
};
struct bill
OR
{
int cno;
}b1, b2;
char name[20];
float amt;
struct date billdate;
struct date paydate;
}b1, b2;
The second structure bill now contains another structure, date, as one of its members. The
structure may look like as follows:
b1 (size of the variable is 38 bytes)
billdate (size 6 bytes)
2190 2192
2212
2216
2218
2220
2224
2226
b1
cno
name
amt
This can be
accessed by b1.cno
PROCESSING STRUCTURES:
day
month year
This can be
accessed by
b1.billdate.day
day
month
year
This can be
accessed by
b1.paydate.year
=======================================================================
@ Einstein College Of Engineering
[13/159]
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
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
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
{
int regno;
char name[20];
char dept[10];
int year;
};
This is structure
variable
This is structure
pointer variable
=======================================================================
@ Einstein College Of Engineering
[14/159]
34 bytes
2 bytes
This is
structure
pointer
variable.
Address
6100
20 bytes
10 bytes
6102
2 bytes
6132
6122
stu1
regno
name
dept
year
1008
6100
sptr
Address of the
structure variable
stu1
Address of the
pointer variable
sptr.
2 bytes
Access to members of the structure is shown below:
: %s\n, sptr->name);
printf(Department Name
printf(Year of Study
: %s\n, sptr->dept);
: %d\n, sptr->year);
1.4.2 STACK :
A stack is an ordered list in which all insertions and deletions are made at one end, called
the top. stacks are sometimes referred to as Last In First Out (LIFO) lists
Stacks have some useful terminology associated with them:
LIFO Refers to the last in, first out behavior of the stack
=======================================================================
@ Einstein College Of Engineering
[15/159]
2.
pop() :
Removes the top element from the stack and returns that top element. An error
occurs if the stack is empty.
peek():
Returns the top element and an error occurs if the stack is empty.
=======================================================================
@ Einstein College Of Engineering
[16/159]
Stack
item /
element
6
Stack
PUSH
8
4
top
top
operation
6
8
4
top
6
8
4
item /
element
POP
operation
Stack
top
8
4
Stack
=======================================================================
@ Einstein College Of Engineering
[17/159]
Returns the item at the top of the stack but does not delete it.
item /
element
6
8
4
top
PEEK
6
top
operation
Stack
6
8
4
Stack
Algorithm:
PEEK(STACK, TOP)
BEGIN
/* Check, Stack is empty? */
if (TOP == -1) then
print Underflow and return 0.
else
item = STACK[TOP] / * stores the top element into a local variable */
return item
END
Implementation in C using array:
/* here, the variables stack, and top are global variables */
int pop ( )
{
if (top == -1)
{
printf(Stack is Underflow);
return (0);
}
else
=======================================================================
@ Einstein College Of Engineering
[18/159]
A+(B*C)
2. Infix notation
(A+B)*C
equivalent
Postfix notation
ABC*+
=======================================================================
@ Einstein College Of Engineering
[19/159]
Prefix
Postfix
a+b
+ab
ab+
a+b*c
+a*bc
abc*+
(a + b) * (c - d)
*+ab-cd
ab+cd-*
b*b-4*a*c
40 - 3 * 5 + 1
($)
Next Highest
Lowest
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
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.
=======================================================================
@ Einstein College Of Engineering
[20/159]
In each step, the stack is the main tool that is used to accomplish the given task.
(1)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]
{
Opnd1:=pop(opndstk);
Opnd2:=pop(opndnstk);
Value := result of applying symb to opnd1 & opnd2
Push(opndstk,value);
}
Result := pop (opndstk);
Example:
623+-382/+*2$3+
Symbol
6
Operand 1 (A)
Operand 2 (B)
Value (A B)
STACK
6
=======================================================================
@ Einstein College Of Engineering
[21/159]
6, 2
6, 2, 3
6, 5
1, 3
1, 3, 8
1, 3, 8, 2
1, 3, 4
1, 7
7, 2
49
49
49, 3
49
52
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 then popping off address and returning to it
return value can be pushed onto stack before returning, popped off by caller
(3) expression parsing
e.g.
matching
brackets:
...
...
...
...(
...
...]
...
...
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. Steps:
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.
(i) POP the stack.
=======================================================================
@ Einstein College Of Engineering
[22/159]
if ( symb is an operand )
add symb to the Postfix String
4. else
{
5.
6.
} /* end while * /
enqueue
(Insertion)
=======================================================================
@ Einstein College Of Engineering
[23/159]
dequeue
Rear
Front
(Deletion)
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 addq (item : items);
{add item to the queue q}
begin
if rear=n then queuefull
else begin
rear :=rear+1;
q[rear]:=item;
end;
end;{of addq}
2. Deletion in a queue
procedure deleteq (var item : items);
{delete from the front of q and put into item}
begin
if front = rear then queueempty
else begin
front := front+1
item := q[front];
=======================================================================
@ Einstein College Of Engineering
[24/159]
scheduling
processing of GUI events
printing request
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.
rear
front
Algorithm Circular Queue Insert
Void CQInsert ( int queue[ ], front, rear, item)
{
if ( front = = 0 )
front = front +1;
if ( ( ( rear = maxsize ) && ( front = = 1 ) ) || ( ( rear ! = 0 ) && ( front = rear +1)))
{
printf( queue overflow );
if( rear = = maxsize )
rear = 1;
else
rear = rear + 1;
q [ rear ] = item;
=======================================================================
@ Einstein College Of Engineering
[25/159]
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.
NODE
INFO LINK
=======================================================================
@ Einstein College Of Engineering
[26/159]
The linked list consists of series of nodes, which are not necessarily adjacent in memory.
A list is a dynamic data structure i.e. the number of nodes on a list may vary
dramatically as elements are inserted and removed.
The pointer of the last node contains a special value, called the null pointer, which is any
invalid address. This null pointer signals the end of list.
The list with no nodes on it is called the empty list or null list.
Example: The linked list with 4 nodes.
START
OR
HEAD
=======================================================================
@ Einstein College Of Engineering
[27/159]
=======================================================================
@ Einstein College Of Engineering
[28/159]
=======================================================================
@ Einstein College Of Engineering
[29/159]
7060
7060
INFO RIGHT
2140
NULL
2140
7060
4020
4020
2140
end
NULL
4020
O(1)
O(n)
O(1)
O(1)
O(1)
Persistent
No
Singly yes
Locality
Great Bad
Array
Static memory
Insertion and deletion required
to modify the existing element
location
Linked list
Dynamic memory
Insertion and deletion are made
easy.
memory as on block.
=======================================================================
@ Einstein College Of Engineering
[30/159]
=======================================================================
@ Einstein College Of Engineering
[31/159]
=======================================================================
@ Einstein College Of Engineering
[32/159]
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
an exception or do some other automatic error handling in their 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 a pointer to a block of memory in the
heap a pointer which some time earlier was obtained via a call to malloc().
Linked List implementation
In C: typically individual cells dynamically allocated containing a pointer to the next cell.
Advantages:
Space used adapts to size usually results 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 linked)
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]
=======================================================================
@ Einstein College Of Engineering
[34/159]
DATA
ADDRESS
: This operation is used to remove an item from the top of the stack.
3. PEEK
2. Prefix:
=======================================================================
@ Einstein College Of Engineering
[35/159]
ABC^D-+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?
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.
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
c) Two-way or doubly linked lists
d) Circular doubly linked lists
19.What are the Applications of linked list?
a) Implementation of Stack
b) Implementation of Queue
c) Implementation of Tree
d) 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]
Parsing It is a logic that breaks into independent pieces for further processing
j) Backtracking
=======================================================================
@ Einstein College Of Engineering
[37/159]
A simple tree.
A tree consists of a finite set of elements, called nodes, and a finite set of directed
lines, called branches, that connect the nodes.
The number of branches associated with a node is the degree of the node.
o
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.
A node 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.
Root
Level 0
Branch
Internal Nodes
Level 1
Level 2
Leaf
C
H
=======================================================================
I
J
@ Einstein College Of Engineering
[38/159]
Level 3
Siblings
Parents
A, B, C, G
Children
B, F, G, C, H, I, J, D, E
Siblings
{ B, F, G }, { D, E }, { H, I, J }
Leaves
F, D, E, H, I, J
Length
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.
A
B
Left Subtree
C
Right Subtree
The height of the binary trees can be mathematically predicted. The maximum height
of the binary tree which has N nodes, Hmax = N
A tree with a maximum height is rare. It occurs when the entire tree is built in one
direction. The minimum height of the tree, Hmin is determined by,
Hmin = Log2 N + 1
=======================================================================
@ Einstein College Of Engineering
[39/159]
Given a height of the binary tree, H, the minimum and maximum number of nodes in the
tree are given as,
Nmin = H
and,
Nmax = 2H - 1
If the height of the tree is less, then it is easier to locate any desired node in the tree.
If HL represents the height of the left subtree and HR represents the height of the right
subtree then Balance factor, B = HL HR
A tree is balanced if its balance factor is 0 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.
A
B
C
D
A tree is considered nearly complete if it has the minimum height for its nodes and all
nodes in the last level are found on the left.
A
B
A
C
A
C
has no children; or
=======================================================================
@ Einstein College Of Engineering
[40/159]
node[p].father
respectively.
The operations,
isleft(p) can be implemented in terms of the operation left(p)
isright(p) can be implemented in terms of the operation right(p)
Example: -
=======================================================================
@ Einstein College Of Engineering
[41/159]
Fig (a)
Fig (b)
E
The above representation appears to be good for complete binary trees and
wasteful for many other binary trees. In addition, the insertion or deletion of nodes from the
middle of a tree requires the insertion of many nodes to reflect the change in level number of
these nodes.
Linked Representation: The problems of sequential representation can be easily overcome through the use of a
linked representation. Each node will have three fields LCHILD, DATA and RCHILD as represented
below
LCHILD
DATA
RCHILD
=======================================================================
@ Einstein College Of Engineering
[42/159]
Fig (a)
Fig (b)
In most applications it is adequate. But this structure make it difficult to determine
the parent of a node since this leads only to the forward movement of the links.
Using the linked implementation, we may declare,
stuct nodetype
{
int info;
struct nodetype *left;
struct nodetype *right;
struct nodetype *father;
};
typedef struct nodetype *NODEPTR;
This representation is called dynamic node representation.
pinfo = x;
pleft = NULL;
pright=NULL;
return(p);
}
=======================================================================
@ Einstein College Of Engineering
[43/159]
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
B
C
The binary tree format can be adopted by changing the meaning of the left and right
pointers. There are two relationships in binary tree,
o
Parent to child
Sibling to sibling
Using these relationships, the general tree can be implemented as binary tree.
Algorithm
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 a branch for each sibling to its right
sibling.
3. Remove all unconnected branches from the parent to its children
A
=======================================================================
E
F
@ Einstein College Of Engineering B [44/159]
C
B
I
A
B
C
B
E
F
G
H
A binary tree traversal requires that each node of the tree be processed once and only
once in a predetermined sequence.
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.
In a breadth-first traversal, the processing proceeds horizontally form the root to all its
children, then to its childrens 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]
( Recursive procedure )
1. Inorder Traversal
Steps :
3.
Algorithm
C Coding
void inorder_traversal ( NODE * T)
{
if( T ! = NULL)
{
Begin
inorder_traversal(T->lchild);
inorder_traversal(T->rchild);
Inorder_traversal ( left subtree ( T ) )
Print ( info ( T ) )
/ * process node */
}
}
=======================================================================
@ Einstein College Of Engineering
[46/159]
C
The Output is : C B D A E F
2. Preorder Traversal
Steps :
Algorithm
if( T ! = NULL)
{
Begin
preorder_traversal(T->lchild);
Begin
preorder_traversal(T->rchild);
Print ( info ( T ) ) / * process node * /
Output is : A B C D E F
3. Postorder Traversal
Steps :
Algorithm
C function
=======================================================================
@ Einstein College Of Engineering
[47/159]
if( T ! = NULL)
{
Begin
postorder_traversal(T->lchild);
postorder_traversal(T->rchile);
Begin
}
}
The Output is : C D B F E A
Non Recursive algorithm:
Inorder_Traversal
=======================================================================
@ Einstein College Of Engineering
[48/159]
p=pop(s);
printf(%d \n, pinfo);
p =pright;
}
}while( !empty (s) || p! = NULL );
}
Non Recursive algorithm:
Preorder_Traversal
=======================================================================
@ Einstein College Of Engineering
[49/159]
if(pright!=NULL)
push(s,pright);
p=pleft;
}
else
p=pop(s);
}while( ! empty(s) || p! = NULL )
}
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
Binary tree is useful data structure when two-way decisions must be made at each point in
a process. For example find the all duplicates in a list of numbers. One way doing this is each
number compare with its precede it. However, it involves a large number of comparisons.
The number of comparisons can be reduced by using a binary tree.
Step 1: from root, each successive number in the list is then compared to
the number in the root.
Step 2: If it matches, we have a duplicate.
Step 3: If it is smaller, we examine the left subtree.
=======================================================================
@ Einstein College Of Engineering
[50/159]
=======================================================================
@ Einstein College Of Engineering
[51/159]
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
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()
Insert(elt)
Search(elt,x)
FindMin()
FindMax()
Ordered Output()
=======================================================================
@ Einstein College Of Engineering
[52/159]
}
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 (elt<key) then
InsertBST(elt, Tlchild)
Else if(elt>key) then
InsertBST(elt, Trchild)
Else
element is already exist
return T
End
C coding to Insert element into a BST
struct node
{
int info;
struct node *lchild;
struct node *rchild;
};
typedef stuct node NODE;
NODE *InsertBST(int elt, NODE *T)
{
if(T = = NULL)
{
T = (NODE *)malloc(sizeof(NODE));
if ( T = = NULL)
printf ( No memory error);
else
{
tinfo = elt;
tlchild = NULL;
trchild = NULL;
}
}
=======================================================================
@ Einstein College Of Engineering
[53/159]
=======================================================================
@ Einstein College Of Engineering
[54/159]
=======================================================================
@ Einstein College Of Engineering
[55/159]
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]
=======================================================================
@ Einstein College Of Engineering
[57/159]
+
*
a
b
ab+cde+**
Step 1:
The first two symbols are operands, so create one-node tree and push pointers of them onto a stack.
Step 2:
Next symbol is read, i.e + operator. Now the symbol is operator, so two previous pointers to trees
in the stack are popped, and a new tree is formed and its pointer is pushed on to the stack. The popped
pointers are placed as left child and right child in the new tree, as a result two subtrees are merged.
=======================================================================
@ Einstein College Of Engineering
[58/159]
+
e
c
b
a
Step 4:
Next symbol is read, i.e + operator. Now the symbol is operator, so two previous pointers to trees
in the stack are popped, and a new tree is formed and its pointer is pushed on to the stack. The popped
pointers are placed as left child and right child in the new tree, as a result two subtrees are merged.
+
+
c
a
Step 5:
Next symbol is read, i.e * operator. The symbol is operator, so two previous pointers to trees in the stack
are popped, and a new tree is formed and its pointer is pushed on to the stack. The popped pointers are
placed as left child and right child in the new tree, as a result two subtrees are merged.
+
a
c
d
=======================================================================
@ Einstein College Of Engineering
[59/159]
+
*
+
+
a
c
d
Atlast we made an expression tree. In this tree we can make in-order , pre-order and post- order traversal
like a binary tree.
2.3.2 Algorithm & Implementation of Expression Tree
Algorithm:
1. Start the program.
2. Create two stacks.
a.
b.
=======================================================================
@ Einstein College Of Engineering
[60/159]
b.
c.
b.
c.
b.
c.
node newnode,item,temp;
char str[50];
int i,k,p,s,flag=0;
printf("ENTER THE EXPRESSION ");
scanf("%s",str);
printf("\n%s",str);
for(i=0;str[i]!='\0';i++)
{
if(isalnum(str[i]))
{
newnode=create();
newnode->info=str[i];
newnode->lchild=NULL;
newnode->rchild=NULL;
item=newnode;
push_num(item);
}
=======================================================================
@ Einstein College Of Engineering
[61/159]
}
printf("\nThe prefix expression is\n ");
preorder(root);
printf("\nThe infix exp is\n ");
inorder(root);
printf("\nThe postfix expression is\n ");
postorder(root);
evaluate();
=======================================================================
@ Einstein College Of Engineering
[62/159]
op[++ot]=item;
push_num(node item)
{
num[++nt]=item;
}
char pop_op()
{
if(ot!=-1)
return(op[ot--]);
else
return(0);
}
node pop_num()
{
if(nt!=-1)
return(num[nt--]);
else
return(NULL);
}
int prec(char data)
{
switch(data)
{
case '(':return(1);
break;
case '+':
case '-':return(2);
break;
case '*':
case '/':return(3);
break;
case '^':return(4);
break;
case ')':return(5);
break;
}
}
inorder(node temp)
{
if(temp!=NULL)
{
inorder(temp->lchild);
printf("%c ",temp->info);
inorder(temp->rchild);
}
}
preorder(node temp)
{
if(temp!=NULL)
{
printf("%c ",temp->info);
preorder(temp->lchild);
preorder(temp->rchild);
}
}
=======================================================================
@ Einstein College Of Engineering
[63/159]
=======================================================================
@ Einstein College Of Engineering
[64/159]
10
BF = 1
(1-0 = 1)
8
BF = 0
BF = -1
(1-2 = -1)
14 BF = 1
8
BF = 0
(-1-(-1) = 0)
BF = 0 10
=======================================================================
@ Einstein College Of Engineering
[65/159]
The left child (K1) of the root (K2) is promoted as root node.
The root (K2) is promoted as the right child of the new root(K1).
K2
K1
K1
Z
K2
X
Before Rotation
Routine to perform single rotation with left
After Rotation
i.
ii.
Perform a rotation between node (K2) and its left child (K1)
iii.
=======================================================================
@ Einstein College Of Engineering
[66/159]
8
BF=0 5
BF=0 3
BF=1
8
BF=1 5
10 BF=0
BF=1 3
7 BF=0
Balanced Tree
Now insert the value 1 it becomes unbalanced
BF=2
10 BF=0
7 BF=0
BF=0 5
BF=1 3
BF=0 1
BF=0
10 BF=0
BF=0 7
Its(K2) left child(Y) is converted to right child of the previous root node(K1).
The previous root node(K1) is considered as the left child of new root node(K2).
ii.
Perform a rotation between node (K1) and its right child (K2)
K2
K1
K1
K2
Z
Y
Before Rotation
Y
After Rotation
=======================================================================
@ Einstein College Of Engineering
[67/159]
6 BF=-2
6 BF=-1
BF=0
BF=0
10 BF=-1
10 BF=0
11 BF=-1
BF=0 8
11 BF=0
BF=0 8
10
Balanced Tree
Now insert the value 13 it becomes unbalanced
due to the insertion of new node in the Right Subtree
BF=0 10
11 BF=-1
BF=1 6
BF=0 4
BF=0
13
BF=0
K3
K3
K1
K2
K2
A
B
Before Rotation
K1
K2
D
C
K3
K1
=======================================================================
@ Einstein College Of Engineering
[68/159]
Apply single Right rotation at K1, as a result K2 becomes the left child of the root node K3 and K1
becomes the left child of K2.
ii.
Apply single Left rotation at the root node K3, as a result K2 becomes the root node and K1
becomes the left child of it and K3 becomes right child of it. Now the tree is balanced.
20 BF=2
20 BF=1
BF=0 10
BF=0 5
BF=-1 10
30 BF=0
BF=0 5
15 BF=0
30 BF=0
15 BF=0
BF=0 12
Balanced Tree
Now insert the value 12& 18 the tree becomes unbalanced
18 BF=0
BF=0
20 BF=2
BF=1 15
BF=0 10
5 BF=0
30 BF=0
BF=0
15
20 BF=0
10
18 BF=0
12 BF=0
BF=0
12
18
30
2. Double Rotation with Right(Insertion in the Left Subtree of the Right SubtreeRight-Left Rotation)
Apply single Left rotation at K3, as a result K2 becomes the right child of the root node K1 and K3
becomes the right child of K2.
ii.
Apply single Right rotation at the root node K1, as a result K2 becomes the root node and K1
becomes the left child of it and K3 becomes right child of it. Now the tree is balanced.
=======================================================================
@ Einstein College Of Engineering
[69/159]
K1
K2
K3
K2
K2
K3
A
C
K3
K1
Before Rotation
After Left Rotation
static Position DoubleRotateWithRight (Position K1)
{
/* Rotate between K2 and K3*/
KRight = SingleRotateWithLeft (K1Right);
/* Rotate between K2 and K1*/
return SingleRotateWithRight (K1);
}
Example:
10 BF=-2
10 BF=1
17 BF=0
BF=0 12
17 BF=0
BF=0 12
15 BF=1
BF=0 8
15 BF=0
BF=0 8
Balanced Tree
Now insert the value 11& 14 the tree becomes unbalanced
14 BF=0
BF=0 11
10
BF=0
BF=-2
BF=0 12
12 BF=-1
8
BF=0 11
BF=0 14
15 BF=0
BF=0 10
BF=0 8
15 BF=0
11
14
17
BF = 0
17 BF=0
=======================================================================
@ Einstein College Of Engineering
[70/159]
=======================================================================
@ Einstein College Of Engineering
[71/159]
20 BF = 0
20 BF = 1
20 BF = 1
BF = 0 10
10 BF = 0
Step 4:(Insert the value 50)
40 BF = 0
20 BF =-1
20 BF =-2
40 BF =-1
BF = 0 10
BF = 0 10
40 BF =-2
50 BF = 0
50 BF =-1
20 BF =-1
BF = 0 10
90 BF = 0
50 BF =0
Now the tree is Balanced
BF =0 40
90 BF =0
=======================================================================
@ Einstein College Of Engineering
[72/159]
20 BF =-2
50 BF =1
BF = 0 10
BF =1 40
90 BF =0
30 BF = 0
After Left Rotation:
40 BF =0
20 BF =-2
BF =0 30
40 BF =-1
90 BF = 0
30
10
50 BF =-1
50 BF =-1
BF = 0 20
40 BF =-1
BF = 0 10
90
50 BF =-2
BF = 0 20
90 BF =1
30
10
50.
After Right Rotation:
60 BF = 0
40 BF =0
40 BF =-1
50 BF =-2
BF = 0 20
10
30
60 BF =0
BF = 0 20
60 BF =-1
10
30
40 BF =0
90 BF = 0
Step 8: (Insert the value 70)
90
50
60 BF =0
BF = 0 20
10
30
90
50
70
=======================================================================
@ Einstein College Of Engineering
[73/159]
K1
< K1
K2
. Km-1
K1<K2
Km-1
Advantages:
There are limitations on size of the collection that can be maintained in the main memory.
When data is maintained in the disk, it is 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 non leaf nodes store up to M-1 keys to guide the searching, key i represents the smallest key
in subtree i+1.
All non leaf nodes (expect the root) have between M/2 level and M children.
All leaves are at the same depth and have between L/2 and L children for some L.
=======================================================================
@ Einstein College Of Engineering
[74/159]
1
23
12
1
2
10
18
12
15
32
18
20
23
28
30
46
32
38
64
46
50
82
64
65
82
83
84
Note:
Interior nodes (non leaves) are represented as ellipses, with 2 pieces of data. Dashed ----- lines
indicates a node with only 2 children.
Leaves are drawn in boxes, which contains the data in ordered way.
22:-
16:-
8,11,12
41:58
16,17
22,23,31
41,52
58, 59, 61
22:-
16:-
8,11,12
41:58
16,17,18
22,23,31
41,52
58, 59, 61
=======================================================================
@ Einstein College Of Engineering
[75/159]
1, 8, 11, 12
This node violates the condition, since any leaf node can hold only two or three keys. Therefore
divide the node into two nodes, with 2 keys in each node
22:-
16:-
1, 8
41:58
16,17,18
11, 12
22,23,31
Insert 19
41,52
58, 59, 61
22:-
16:-
41:58
P
1, 8
11, 12
16, 17
18,19
22,23,31
41,52
58, 59, 61
The internal node P have 4 leaf nodes but only 3 children per node is allowed therefore split the node P is
into 2 internal node with two children.
16:22
11:-
1, 8
41:58
18:-
11, 12
16, 17
18,19
22,23,31
41,52
58, 59, 61
Insert 28:
The leaf node
22,23,28,31
has 4 keys, which is not allowed. Therefore split into 2 node and introduce a
16:22
11:-
1, 8
11, 12
41:58
18:-
16, 17
41:58
18,19
22,23
28, 31
41,52
58, 59, 61
=======================================================================
@ Einstein College Of Engineering
[76/159]
11:-
1, 8
18:-
11, 12
18,19
16, 17
58:-
28:-
22,23
28, 31
41,52
58, 59, 61
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:16:-
41:-
11:-
1, 8
18:-
11, 12
18,19
16, 17
58:-
28:-
22,23
41,52
28, 31
58, 59, 61
B-Tree Deletion:
25 -
4 10
40 -
11 12
30 -
50 -
P
1To Delete
2 32: 4
10 11 12
25
30
40
50
Since P has 3 childrens(leaf nodes), 2 can be simply deleted and P be left with just 2 childrens.
2 3
Deletion
1
To Delete 5:
Deletion
4
When the node has only one child, borrow from left or right sibling, without violating the condition.
Left sibling has least value and the right sibling has greatest value.
=======================================================================
@ Einstein College Of Engineering
[77/159]
40 -
12 -
10 10
30 -
11
12
25
50 30
40
50
To Delete 10:
P cant borrow from any of its sibling. So, move the child to left sibling and delete P.
25 11
3
1
40 -
4
3
12 4
11
30 12
25
50 30
40
50
To Delete 30:
This is invalid
25
When the node has only one child, borrow from left or right sibling, without violating the condition.
Left sibling has least value and the right sibling has greatest value.
40 50
25 40
50
This is also invalid because the root having only one child. So
borrow from left or right sibling without violating the rule.
So remove the root and make the grant parent as new root.
25 3 4
1
40
50 -
12 4
11
12
40 40
50
=======================================================================
@ Einstein College Of Engineering
[78/159]
Database implementation
In a 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 queue, run it until either it finishes or its time
limit is up, and placing it at the and of the queue if it doesnt finish.
This strategy is generally not approximate, because very short jobs will soon to take a 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 still 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:
It is a collection of ordered elements that provides fast access to the minimum or maximum element.
Basic Operations performed by priority queue are:
1. Insert operation
2. Deletemin operation
Insert operation is the equivalent of queues Enqueue operation.
Deletemin operation is the priority queue equivalent of the queues Dequeue operation.
Deltemin(H)
Priority Queue H
Insert(H)
3.3.3 Implementation:
There are three ways for implementing priority queue. They are:
1.
Linked list
2.
3. Binary Heap
Linked list:
A simple linked list implementation of priority queue requires O(1) time to perform the insertion at
the front and O(N) time to delete the minimum element.
Binary Search Tree:
This implementation gives an average running time of O(log N) for both insertion and deletemin
operations.
=======================================================================
@ Einstein College Of Engineering
[79/159]
Deletemin operation makes tree imbalance, by making the right subtree heavy as we are repeatedly
removing the node from the left subtree.
40
[0]
60
[2]
20
[1]
A complete binary tree of height
10
h[3]
has
30 50
[4] 2h[5]
between
and
90
[6]nodes
-1
h+1
number of nodes it have must be 2 or 3). This implies that the height of complete binary tree is log N which
is clearly O(log N).
An array implementation of complete binary tree is as follows:
40
20
60
10
30
50
90
[0] [1]
[2]
[3]
[4]
[5]
[6]
ii)
the right child is in the cell after the left child 2i+2
the parent node is [(i+1)/2] = = 0.5 = app 1 i.e it refers the root node 40
ii)
the left child is 2i +1 = (2*0)+1 = 1 i.e it refers the position of the element 20
iii) the right child is 2i+2 = (2*0)+2 = 2 i.e it refers the position of the element 60
If i = 1,
i)
the parent node is [(i+1)/2] = 2/2 = 1 i.e it refers the root node 20
ii)
the left child is 2i+1 = (2*1)+1 = 3 i.e it refers the position of the element 10
iii) the right child is 2i+2 = (2*1)+2 = 4 i.e it refers the position of the element 30
If i = 2,
i)
the parent node is [(i+1)/2] = 3/2 = 1.5 = app 2 i.e it refers the root node 60
ii)
the left child is 2i+1 = (2*2)+1 = 5 i.e it refers the position of the element 50
=======================================================================
@ Einstein College Of Engineering
[80/159]
30
12
8
10
20
25
25
20
30
10
12
In a MaxHeap, for every node X, the key in the parent of X is greater than (or equal to) the key in X,
with the exception of the root(which has no parent). [figure 2]
Routine for the declaration of priority Queue
Struct heap
{
int capacity;
int size;
ElementType *Element;
};
3.3.4.2 Basic 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.
Otherwise we slide the element is in the holes parent node in to 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
Step1: Create a hole in the next available location, since otherwise the tree will not be complete.
=======================================================================
@ Einstein College Of Engineering
[81/159]
13
16
21
16
21
----
31
24
26 32
65
31
24
68
19
19
68
26 32
65
Step2: Check the inserting element 14 with the holes parent node 31.
If the inserting element is greater than the holes parent node element, then the element will be
inserted in to the hole.
If the inserting element is lesser than the holes parent node element (14<31), we have to slide
the holes parent node element in to the hole. Thus, the bubbling the hole up towards the root
happen.
13
16
21
24
26 32
65
68
19
32
Step3: Repeat the step 2 check the inserting element 14 with the holes parent node 21.
If the inserting element is greater than the holes parent node element, then the element will be
inserted in to the hole.
If the inserting element is lesser than the holes parent node element (14<21). We have to slide the
holes parent node element in to the hole. Thus the bubbling the hole up towards the root happens.
13
16
21
24
65
26 32
19
68
31
Step4: Inserting the element 14 in to the hole will not validate heap order Property. Therefore you can
insert the element 14 in to he hole.
=======================================================================
@ Einstein College Of Engineering
[82/159]
16
14
21
24
26 32
65
19
68
31
This general strategy is known as a perculate up; the new element is perculated up the Heap until
the correct location is found.
Perculation in the Insert routine by performing repeated swaps until the correct order was
established.
The time to do the insertion could be O(log N).
Routine to insert in to a binary heap
void Insert (Element Type X, PriorityQueue H)
{
int i;
if (IsFull(H))
{
Error (priority Queue is full);
return;
}
for (i=++Hsize; HElement [i/2]>X; i/2)
{
HElement[i] = HElement[i/2];
}
HElement[i]= X;
}
DeleteMin:
Finding the minimum is easy, it is hard to move. When the minimum is removed a hole is created at
the root.
Since the Heap now becomes one smaller, it follows that the last element X in the Heap must move
somewhere in the Heap.
We slide the smaller of the holes children into the hole, thus pushing the hole down one level.
=======================================================================
@ Einstein College Of Engineering
[83/159]
16
14
21
24
26 32
65
21
24
68
19
31
16
14
Delete 13
----------------
DeleteMin
65
26 32
31
The last value cannot be placed in the hole, because this would violate heap order.
Therefore, we place the smaller child 14 in the hole, sliding the hole down one level.
14
14
16
21
24
26 32
65
68
19
68
19
Place 21 into
the hole
----------------
24
68
19
26 32
65
31
16
21
31
Now slide the hole down by inserting the value 31 in to the hole.
14
14
16
21
31
24
65
19
16
21
68
26 32
31
24
65
19
68
26 32
=======================================================================
@ Einstein College Of Engineering
[84/159]
The hash table or hash map is a data structure that associates key with value.
Hashing is used for faster access of elements and records from the collection of tables and files.
Hashing is a technique used for performing insertions, deletions and search operations in constant
average time of (1).
Hashing is applied where the array size is large and time taken for searching an element is more.
It works by transforming the key using a hash function into a hash, a member which is used to index
into an array to locate the desired location where the values should be.
Hash table supports the efficient addition of new entries and the time spent searching for the
required data is independent of the number of items stored.
In hashing an ideal hash table data structure is nearly an array of fixed size, containing the key.
The key is a string or integer associate with a value. Each key is mapped in to some number in the
range 0 to TableSize -1 and placed in the appropriate cell.
The mapping is called hash function, which should be simple to compute and should ensure that any
two distinct keys get different cells.
0
1
2
3
=======================================================================
@ Einstein College Of Engineering
[85/159]
5
6
7
8
9
3.4.2 Hash function:
Hash function is a key to address transformation which acts upon a given key to compute the relative
position of the key in an array.
The mapping of each key into some number ranged from 0 to TableSize-1 is known as Hashing.
Ideally, the hash function is used to determine the location of any record given its key value.
Simple to compute
Hash
Function
Key
Address
The hash function in other words, maps a key value to an address in the table and the domain of the
hash function is non-negative integers in the range 0 to the size of the table.
If x is the key and h is the hash function, then h(x) is the address for the key to be stored.
10
25
33
47
64
88
39
The following figure shows the Storage of the key values in the above table using the hash function value
as index.
Key (x)
10
1
2
=======================================================================
@ Einstein College Of Engineering
[86/159]
33
64
25
6
7
47
88
39
Suppose we want to store valu 25, we need to apply the hash function first.
In this case, it is 25 mod 10 = 5 . 5 is used as the address for storing 25
For instant suppose the table size is 10007. The given input key can have maximum of 8 character
are few characters long.
Since a char as an integer value (i.e) always atmost 127 (i.e) ASCII code can represent in 7 bits
therefore 27=128.
The hash function can 1st assume the values between 0 to 1016 (i.e) 127x8. Hence it cannot provide
equal distribution.
In this hash function we can assume at least 2 characters + null terminator. The value 27 represents
number of English alphabets 26+a blank space. Therefore 729 is 272.
This function examines only the first 3 char, if these are random and the table size is 10007, (i.e)
English is not random. Therefore 263=17576 possible combination of 3 char ignoring blank space.
=======================================================================
@ Einstein College Of Engineering
[87/159]
When a memory location filled if another value of the same memory location comes then there
occurs collision.
When an element is inserted it hashes to the same value as an already inserted element, then it
produces collision and need to be resolved.
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 list).
Each link list contains the entire element whose keys hash to the same index.
Collision diagram:
10
0
1
Key (x)
10
10 % 10 = 0
25
25 % 10 = 5
33
33 % 10 = 3
47
47 % 10 = 7
65
65 % 10 = 5
83
83 % 10 = 3
30
30 % 10 = 0
2
33
3
4
25
5
6
47
Hash Table
Each linked list contain all the elements whose keys to the same index.
When an overflow occurs this 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 new keys.
=======================================================================
@ Einstein College Of Engineering
[88/159]
33
83
25
65
4
5
6
7
47
8
9
=======================================================================
@ Einstein College Of Engineering
[89/159]
=======================================================================
@ Einstein College Of Engineering
[90/159]
ii)
The elements having the same memory address will be in the same chain and hence leads to faster
searching.
iii) Doesnt require a prior knowledge of the number of elements that are to be stored in the hash
table (i.e.) dynamic allocation is done.
iv) It helps to get a uniform and perfect collision resolution hashing.
Disadvantages:
i)
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
implementation of a second data structure
Open addressing hashing is an alternating 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/159]
with F(0) = 0.
The function F is the collision resolution strategy. This technique is generally used where storage space
is large.
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 strategic.
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 is referred as linear probing.
In linear probing for the ith probe, the position to be tried is, (h(k) + i) mod Table_Size, where f is a
linear function of i, F(i)=i. This amounts to trying cells sequentially in search of an empty cell.
Example for Linear Probing:
Insert the keys 89, 18, 49, 58, 69 into a hash table using in the same hash function as before and
the collision resolving strategies. F(i)=i.
Solution:
The first collision occurs when 49 is inserted. It is put in the next available index namely 0 which is
open.
The key 58 collides with 18, 89, 49 afterwards it found an empty cell at the index 1.
If the table is big enough, a free cell can be always be found, but the time to do so can get quite
large.
Even if the table is relatively empty, blocks of occupied cells start forming. This is known as primary
clustering means that any key hashes into the cluster will require several attempts to resolve the
collision and then it will add to the cluster.
Empty Table
0
1
2
After 89
After 18
After 49
After 58
After 69
49
49
49
58
58
69
3
4
5
6
=======================================================================
@ Einstein College Of Engineering
[92/159]
89
18
18
18
18
89
89
89
89
ii)
Check the remaining locations of the table one after the other till an empty
location is reached. Wrap around on the table can be used. When we reach
the end of the table, start looking again from the beginning.
=======================================================================
@ Einstein College Of Engineering
[93/159]
lists.
Algorithms to find a path in a graph to find the connected components of an undirected graph, and to
find a spanning tree of a connected undirected graph.
4.1 DEFINITIONS: A graph G= (V, E) is 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 (i,j), where i and j are the
two vertices joined by E.
4.1.1 Graph Display: A graph generally represented as figure in which the vertices represented by circles
and the edges by lines. Examples, of graphs are as follows:
Some of the edges in this figure are oriented (that they have arrow heads) while other are not. An
edge with an orientation is a directed while an edge with no orientations is an undirected edge.
The
undirected edges (i,j) and (j,i) are the same; the directed edge (i,j) is different from the directed edge(j,i)
the former being oriented from I to j and the latter from j to i.
Example: -
1
2
3
Undirected Graph G1
Directed graph G2
V ( G1 ) = { 1, 2, 3, 4}; E ( G1 ) = { ( 1, 2 ), ( 1, 3 ), ( 1, 4 ), ( 2, 3 ), ( 2, 4 ), (3, 4 ) }
V ( G2 ) = { 1, 2, 3 };
The edges of a directed graph are drawn with an arrow from the tail to the head.
For undirected graph:
1
2
n(n 1)
= 3 edges
2
=======================================================================
@ Einstein College Of Engineering
[94/159]
1
3
n2 = 9 edges
n(n 1)
2
1
2
(3,2), (3,4)}
Vertices = {1,2,3,4}
Edges = {(1,2), (1,4), (2,4), (3,1),
1
2
Vertices = {1,2,3,4}
Edges = {(1,2), (1,4), (1,3),
3
Note:
i)
ii)
4.1.4 Subgraph:
A subgraph of graph G = (V,E) is a graph G=(V,E) such that V V and E E.
=======================================================================
@ Einstein College Of Engineering
[95/159]
1
2
4
3
1
2
4
2
4
3
G1
G1
G1
4.1.5 Symmetric Digraph:
A symmetric digraph is a directed graph such that for every edge vw there is also the reverse edge
wv.
Example:
A
B
=======================================================================
@ Einstein College Of Engineering
[96/159]
A
Path
from A to D is 2
length
Note:
Path
Example:
B
C
D
A
B
C
4.1.13 Cycle:
Cycle in a directed graph is a simple cycle in which no vertex is repeated except that the first and last
are identical.
Example:
Cycle
= A B C A
=======================================================================
@ Einstein College Of Engineering
[97/159]
0i<k
Example:
A
B
Cycle = ABCA
B
(b)
A
Undirected acyclic graph (or) undirected tree.
B
4.1.15 Connected component:
A
15
B
10
5
25
D
20
C
4.1.17 Degree
The number of edges incident on a vertex determines its degree. The degree of the vertex V is written as
degree (V).
The in degree of the vertex V, is the number of edges entering into the vertex V.
Similarly the out degree of the vertex V is the number of edges exiting from that vertex V.
=======================================================================
@ Einstein College Of Engineering
[98/159]
G=(V,E)
be
graph
with
n=|V|,
number
of
vertices,
m=|E|,
number
of
Edges
and
A
B
C
D
0
1
1
0
0
0
0C
1
0
1
1
1
1
Adjacency Matrix
The adjacency matrix is maintained in the array arr[4][4]. Here the entry of matrix arr[0][1] = 1, which
represents there is an edge in the graph from node A to B. Similarly arr[2][0]= 0, which represents there
is no edge from node C to node A.
=======================================================================
@ Einstein College Of Engineering
[99/159]
C
D
0
1
1
0
1
0
1C
1
0
1
1
1
1
Adjacency Matrix
The adjacent matrix for an undirected graph will be a symmetric matrix. This implies that for every I and
j, A[i][j] = A[j][i] in an undirected graph.
3. Adjacency matrix for Weighted graph:
3
A
9
8
D
6
5
0
3
2
0
C
8
4 7
0 5
6 0
Adjacency Matrix
The space needed to represent a graph using its adjacency matrix is n2 bits.
Disadvantages:
1. Takes O(n2) space to represents the graph
2. It takes O(n2) time to solve the most of the problems.
4.2.2 Adjacency List Representation:
A graph containing n 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 singly 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.
Eventhough adjacency list is a difficult way of representing graphs, a 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 of all the
nodes in the graph and second list will maintain a list of adjacent nodes for each node.
=======================================================================
@ Einstein College Of Engineering
[100/159]
Vertex 2
Vertex 3
Vertex 4
A
C
D
B
Vertex 2
Vertex 3
Vertex 4
random access to the adjacency list for any particular vertex. For an undirected graph with n vertices and m
edges this representation requires n head nodes and 2m list nodes. The storage required is then log n + log
m for the list nodes and O ( log n ) for head nodes because 2 fields in each node.
The structure of header node is:
The total number of edges in G may, therefore can be determined by counting the number of nodes
on its adjacency list. In order to determine in-degree of a vertex easily, we can maintain one more list called
inverse-adjacency lists for each vertex. Each list will contain a node for each vertex adjacent to the vertex it
represents.
Disadvantages:
It takes O(n) time to determine whether there is an arc from vertex i to vertex j. Since there can be O(n)
vertices on the adjacency list for vertex i.
=======================================================================
@ Einstein College Of Engineering
[101/159]
V1
V2
Link1 for V1
Link2 for V2
Here M is a one bit mark field that may be used to indicate 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 a matrix with n rows and m
columns. The matrix is formed by storing: 1 in its ith row and jth column corresponding to the matrix, if
there exists as a ith vertex, connected to one end of the jth edge and a 0 if there is no jth vertex connected to
an end of the jth edge of the graph, such a matrix is referred as an incidence matrix.
IncMat[i][j] = 1, if there is an edge Ej from Vi.
IncMat[i][j] = 0, otherwise
Example 1:
V1
V2
V3
E1
E2
V4
E3
V1
V2
V3
V4
Disadvantage of Incidence matrix:
1.
E2
0
1
0
1
E3
0
0
1
1
Eventhough incidence matrix is an easy way of representing graphs, a graph with considerable
number of nodes could use a large amount of memory.
2. Most of the incidence matrices will be sparse matrices only( a matrix with lot of zeros in it).
4.3 APPLICATIONS OF GRAPHS:
The problems using graph representation are useful in many fields for different operations. Some
examples are:
4.3.1 Airline route map-undirected graph
The vertices are cities, a line connects two cities if and only if there is a nonstop flight between them
in both directions.
A
C
D
A
=======================================================================
@ Einstein College Of Engineering
[102/159]
Directed graph:
Start
Read n
Sum=0
i=1
3
4
While
i<=n
No
Print sum
5
Yes
Sum=sum+i
6
7
8
8
Stop
i=i+1
a) A star network
(undirected graph)
b) A ring network
(directed graph)
=======================================================================
@ Einstein College Of Engineering
[103/159]
Find any vertex with no incoming edges (i.e) indegree=0. If such vertex found, print the
vertex and remove it along with its edges from the graph.
(ii)
Assuming that the Indegree array is initialized and the graph is read into an adjacency list.
(ii)
The function FindNewVerrtexOfIndegreeZero scans the indegree array, to find a vertex with
indegree 0, that has not already been assigned a topological number.
a). It returns 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.
(iii)
NumVertex = 3
=======================================================================
@ Einstein College Of Engineering
[104/159]
=======================================================================
@ Einstein College Of Engineering
[105/159]
B
E
NumVertex = 5
=======================================================================
@ Einstein College Of Engineering
[106/159]
Enqueue
B,C
Dequeue
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 [B] = 1, Indegree [C] = 1, Indegree [D] = 3,
Indegree [E]= 2
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 of its adjacent vertex `B' & `C'
Hence, Indegree [B] = 0 & Indegree [C] = 0
Now, Enqueue the vertex `B' AND C as its indegree becomes zero. Then dequeue the vertex B.
Step 4
Dequeue the vertex `B' from Q and decrement the indegree's of its adjacent vertex `C' and `D'.
Hence, Indegree [C] = 0 & Indegree [D] = 1
Now, Enqueue the vertex `C' as its indegree falls to zero.
Step 5
Dequeue the vertex `C' from 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 zero.
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(|E| + |V|). where E represents the Edges & V represents the
vertices of the graph.
4.4.3 Implementation of Topological sorting technique:
(Refer the lab program manual and write only the corresponding functions)
=======================================================================
@ Einstein College Of Engineering
[107/159]
Starting vertex may be determined by the problem 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 adjacent to it and initiate a DFS from w. The
search terminates when no unvisited vertex can be reached from any of the initiated vertices.
If path exists from one node to another node walk across the edge exploring the edge.
If path does not exist from one specific node to any other node, return to the previous node
where we have been before backtracking.
Algorithm:
Procedure DFS(u)
VISITED (u) 1
for each vertex w adjacent to v do
if VISITED ( w ) = 0 then call DFS ( w )
end
end DFS
Example:
a)
D
B
F
b)
c)
E
D
E
D
A
B
A
B
F
e)
A
B
F
G
E
f)
A
B
F
G
E
2. Pull a node from the beginning of the queue and examine it.
If 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.
Procedure BFS
//Breadth first search of G is carried out beginning at vertex v.
visited are
marked as VISITED(i) = 1.The graph G and array VISITED are global and VISITED is
initialized to zero //
VISITED (v) 1
Initialize Q to be empty
// Q is queue //
=======================================================================
@ Einstein College Of Engineering
[109/159]
// add w to queue //
// mark w as VISITED//
visited[v] = 1;
/*mark v as visited : 1 */
ADDQ(Q,v);
while(!QMPTYQ(Q))
{
v = DELQ(Q);
/*Dequeue v*/
w = FIRSTADJ(G,v);
while(w != -1)
{
if(visited[w] == 0)
{
VISIT(w);
ADDQ(Q,w);
visited[w] = 1;
}
W = NEXTADJ(G,v);
}
}
}
Main Algorithm of apply Breadth-first search to graph G=(V,E)
void TRAVEL_BFS(VLink G[], int visited[], int n)
{
int i;
for(i = 0; i < n; i ++)
{
visited[i] = 0;
}
for(i = 0; i < n; i ++)
if(visited[i] == 0)
=======================================================================
@ Einstein College Of Engineering
[110/159]
A
B
F
b)
G
E
D
A
B
F
c)
E
D
A
B
F
d)
A
B
F
FIFO queue.
3. Search is done parallely in all possible direction.
time.
=======================================================================
@ Einstein College Of Engineering
[111/159]
C
i =1
i , i +1
This is referred as weighted path length. The unweighted path length is the number of the edges on the
path, namely N - 1.
Two types of shortest path problems, exist namely,
1.
=======================================================================
@ Einstein College Of Engineering
[112/159]
V1
V3
V2
V5
V4
V6
V7
=======================================================================
@ Einstein College Of Engineering
[113/159]
V3 Dequeued
Initial State
Known
dv
Pv
Known
dv
Pv
V1
V3
V2
V3
V4
V5
V6
V3
V7
Queue
Vertex
V3
V 1 , V6
V1 Dequeued
V6 Dequeued
Known
dv
Pv
Known
dv
Pv
V1
V3
V3
V2
V1
V1
V3
V4
V2
V1
V5
V6
V3
V3
V7
Queue
Vertex
V 6 , V2, V4
V 2 , V4
V2 Dequeued
V4 Dequeued
Known
dv
Pv
Known
dv
Pv
V1
V3
V3
V2
V1
V1
V3
V4
V1
V1
V5
V2
V2
V6
V3
V3
V7
V4
Queue
Vertex
V 4, V5
V 5 , V7
V5 Dequeued
V7 Dequeued
Known
dv
Pv
Known
dv
Pv
V1
V3
V3
V2
V1
V1
V3
V4
V1
V1
V5
V2
V2
V6
V3
V3
=======================================================================
@ Einstein College Of Engineering
[114/159]
Queue
V4
V7
V4
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 is calculated as follows:
T[W].Dist = T[V].Dist + 1
T[W].path = V
When V3 is dequeued, known is set to 1 for that vertex and the distance of its adjacent vertices V1
and V6 are updated if INFINITY. Path length of V1 is calculated as;
T[V1].Dist = T[V3].Dist + 1 = 0 + 1 = 1
And its actual path is also calculated as,
T[V1].path = V3
Similarly,
T[V6].Dist = T[V3].Dist + 1 = 0 + 1 = 1
T[V6].path = V3
Similarly, when the other vertices are dequeued, the table is updated as shown above. The shortest distance
from the source vertex V3 to all other vertex is listed below;
V3 V1 is 1
V3 V2 is 2
V3 V4 is 2
V3 V5 is 3
V3 V6 is 1
V3 V7 is 3
Analysis:
The running time of this algorithm is O(|E|+|V|) 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:
0 A
0 A
1 B
1 C
=======================================================================
@ Einstein College Of Engineering
[115/159]
A Dequeued
Initial State
Known
dv
Pv
Known
dv
Pv
Queue
B, C
0 A
1 B
1 C
D
2
Vertex
B Dequeued
C Dequeued
D Dequeued
Known
dv
Pv
Known
dv
Pv
Known
dv
Pv
Queue
C, D
Empty
The shortest distance from the source vertex A to all other vertex is listed below;
A B is 1
A C is 1
A D is 2
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.
Dijkstra'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 dv among all the unknown vertices, and
declares that as the shortest path from S to V and mark it to be known. We should set dw = dv + Cvw, if the
new value for dw would be an improvement.
Algorithm(Informal)
1. Create a table with known, Pv, dv 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]
3. Select a vertex V which has smallest dv among all unknown vertices and sets the shortest path from
S to V as known.
4. The adjacent vertices W to V is located and dw is set as dw = dv + Cvw, if the new sum is less than
the existing dw. That is, in every stage dw is updated only if there is an improvement in the path
distance.
5.
Repeat step 3 and 4, until all vertices are classified under known.
Example 1 :
A
4
6
8
Known
dv
Pv
Comments
0
0
Known
dv
Pv
Comments
Known
dv
Pv
Comments
=======================================================================
@ Einstein College Of Engineering
[117/159]
than dv through D.
Known
dv
Pv
Comments
Known
dv
Pv
Comments
Known
dv
Pv
Comments
A is 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 C
and D are classified under known.
=======================================================================
@ Einstein College Of Engineering
[118/159]
A
2
C
Solution:
2
1
2
D
=======================================================================
@ Einstein College Of Engineering
[119/159]
2
C
Vertex
2
D
Initial State
known
dv
Pv
0
0
0
Initial Configuration
Step 2:
Vertex `A' is choose as source vertex and is declared as known vertex.
Then the adjacent vertices of `A' is found and its distance are updated as follows :
T [B] .Dist = Min [T[B].Dist, T[A] .Dist + Ca,b]
= Min [
, 0 + 2]
=2
T [d] .Dist = Min [T[d].Dist, T[a] .Dist + Ca,d]
= Min [
, 0 + 1]
=1
2
C
2
1
Vertex
known
dv
Pv
2
D
1
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,
C
2
, 1 + 1] = 2
2
D
1
Vertex
known
dv
Pv
=======================================================================
@ Einstein College Of Engineering
[120/159]
Vertex
known
dv
Pv
2
D
C
2
2
C
Vertex
known
dv
Pv
2
D
A
5
Connected Graph G
D
3
Cost = 7
5
C
D
1
C
Cost = 8
5
B
1
2
Cost = 8
5
B
Cost = 9
=======================================================================
@ Einstein College Of Engineering
[121/159]
1
B
Minimum Spanning Tree
D
1
Cost = 5
Cost = 5
One node is picked as a root node(u) from the given connected graph.
ii)
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.
iii) The prims algorithm table is constructed with three parameters. They are;
dv
Pv
iv) After selecting the vertex v, update rule is applied for each unknown w adjacent to v. The rule is
dw = min(dw, Cw,v), i.e, if more than one path exist between v to w, then dw is updated with
minimum cost.
SKETCH OF PRIM'S ALGORITHM
void Prim (Graph G)
{
MSTTREE T;
Vertex u, v;
Set of vertices V;
Set of tree vertices U;
T = NULL;
/* Initialization of Tree begins with the vertex `1' */
U = {1}
while (U # V)
=======================================================================
@ Einstein College Of Engineering
[122/159]
Prims algorithm runs on undirected graphs. So when coding it, remember to put every edge in two
adjacency lists. The running time is O(|V|2) without heaps, which is optimal for dense graphs, and O(|E| log
|V|) using binary heaps.
Example 1: For the following graph construct MST using prims algorithm.
2
1
3
C
B
2
D
=======================================================================
@ Einstein College Of Engineering
[123/159]
2
1
Initial State
Vertex
2
D
Undirected Graph G
known
dv
Pv
0
0
0
INITIAL CONFIGURATION
Step 2:
Here, `A' is taken as source vertex and marked as visited. Then the distance of its adjacent vertex is
updated as follows :
T[B].dist = Min (T[B].Dist, Ca,b)
= Min (
, 2)
= Min (
=2
0 A
3
, 1)
=1
B 2
D
1
, 3)
= Min (
=3
Vertex
dv
Pv
Step 3:
Next, vertex `D' with minimum distance is marked as visited and the distance of its unknown adjacent
vertex is updated.
T[B].Dist = Min [T[B].Dist, Cd,b]
= Min (2, 2)
=2
T[C].dist = Min [T[C].Dist, Cd,c]
= Min (3,1)
=1
=======================================================================
@ Einstein College Of Engineering
[124/159]
Vertex
B
1
C
3
known
dv
Pv
Step 4:
Next, the vertex with minimum cost `C' is marked as visited and the distance of its unknown adjacent
vertex is updated.
B 2
A
1
C
D
1
Vertex
dv
Pv
Step 5:
Since, there is no unknown vertex adjacent to `C', there is no updation in the distance. Finally, the
vertex `B' which is not visited is marked.
Vertex
0 A
1
B
3
C
1
dv
Pv
1
B
C
1
=======================================================================
@ Einstein College Of Engineering
[125/159]
V1
1
4
V3
V2
V4
2
8
10
V5
V6
6
V7
V3
10
3
7
V4
V5
V6
6
V7
Step 2:
Vertex
V2
V1
0 V1
4
V3
V5
V4
V6
V7
known
dv
Pv
V1
V2
V3
V4
V5
V6
V7
Vertex
V2
Initial State
0
0
0
0
0
0
dv
Pv
V1
V2
V1
V3
V1
V4
V1
V5
V6
V7
0
0
0
Here Vi is marked as visited and then the distance of its adjacent vertex is updated as follows.
T[V2].dist = Min [T[V2].dist, Cv1, v2]
= Min [
= Min [
, 2] = 2.
, 1] = 1.
=======================================================================
@ Einstein College Of Engineering
[126/159]
V1
V2
V3
,1] = 4
Vertex
3
7
V4
= Min[
V5
V6
V7
dv
Pv
V1
V2
V1
V3
V4
V4
V1
V5
V4
V6
V4
V7
V4
Here V4 is marked as visited and then the distance of its adjacent vertex is updated.
Vertex
Step 4:
V1
V2
V3
10
V5
V4
V6
V7
known
dv
Pv
V1
V2
V1
V3
V4
V4
V1
V5
V4
V6
V4
V7
V4
Here V2 is marked as visited and then the distance of its adjacent vertex is updated as follows;
T[V4].dist = Min [T[V4].dist, Cv2, v4] = Min [1, 3] = 1.
T[V5].dist = Min [T[V5].dist, Cv2, v5] = Min [7, 10] = 7
Step 5:
V1
V2
2
Vertex
known
dv
Pv
V1
V2
V1
V3
V4
V4
V1
V5
V4
V6
V3
V7
V4
1
2
V3
2
V5
V4
5
V6
5
V7
4
=======================================================================
@ Einstein College Of Engineering
[127/159]
V1
V2
Vertex
1
2
2 V3
V5 6
V4
4
1
1 V6
6
V7 4
dv
Pv
V1
V2
V1
V3
V4
V4
V1
V5
V7
V6
V7
V7
V4
Here V7 is marked as visited and then the distance of its adjacent vertex is updated as follows;
T[V6].dist = Min [T[V6].dist, Cv7,v6] = Min [5, 1] = 1
T[V6].dist = Min [T[V6].dist, Cv7,v5] = Min (7, 6) = 6
Step 7:
V1
V2
Vertex
known
dv
Pv
V1
V2
V1
V3
V4
V4
V1
V5
V7
V6
V7
V7
V4
1
2
2 V3
V5 6
V4
4
1
1 V6
6
V7 4
Here V6 is marked as visited and then the distance of its adjacent vertex is updated.
Step 8:
V1
V2
Vertex
1
2 V3
V5 6
V4
4
1 V6
6
V7 4
dv
Pv
V1
V2
V1
V3
V4
V4
V1
V5
V7
V6
V7
V7
V4
=======================================================================
@ Einstein College Of Engineering
[128/159]
V1
V2
1
Minimum Spanning Tree
V3
V5
V4
4
Undirected graph.
V6
6
V7
=======================================================================
@ Einstein College Of Engineering
[129/159]
The edges are build into a minheap structure and each vertex is considered as a single node
tree.
ii)
iii) The vertices u and v are searched in the spanning tree set S and if the returned sets are not
same then (u,v) is added to the set S (union operation is performed), with the constraint that
adding (u,v) will not create a cycle in spanning tree set S.
iv) Repeat step (ii) & (iii), until a spanning tree is constructed with |v|-1 edges.
Algorithm:
void Kruskal(Graph G)
{
int EdgesAccepted;
Disjset S;
PriorityQueue H;
Vertex U,V;
SetType Uset, Vset;
Edge E;
Initialize(S);
ReadGraphIntoHeapArray(G,H);
BuildHeap(H);
=======================================================================
@ Einstein College Of Engineering
[130/159]
/* E = (U,V)*/
Uset = Find(U,S);
Vset = Find(V,S);
if(Uset!=Vset)
{
/* Accep the Edge*/
EdgesAccepted++;
SetUnion(S,Uset,Vset);
}
}
}
Analysis of the Kruskals Algorithm:
The worst case running time of this algorithm is O(|E| log |E|), which is dominated by heap
operations. Notice that since |E| = O(|V|2), this running time is actually O(|E| log |V|). In practice, the
algorithm is mush faster than this time bound would indicate.
Example:
Given G = (V, E, W)
V2
4
V3
V1
V4
V6
10
4
1
V5
6
V7
Step 1:
V1
V2
V1
V2
1
V3
V5
V4
V6
V7
V3
V6
V7
Step 2:
V1
V1
V5
V4
V2
V2
1
V3
V5
V4
=======================================================================
@
V3Einstein College Of
V5[131/159]
V4 Engineering
V6
V7
Step 3:
V1
V2
1
V3
V1
V2
1
V3
V5
V4
V5
V4
4
V6
V7
V6
V7
V1
V2
1
V3
V5
V4
4
V6
6
V7
The final Execution table for finding MST using Kruskals algorithm is as follows:
Edge
Weight
Action
(V1, V4)
Accepted
(V6, V7)
Accepted
(V1, V2)
Accepted
(V3, V4)
Accepted
(V2, V4)
Rejected
(V1, V3)
Rejected
(V4, V7)
Accepted
(V3, V6)
Rejected
(V5, V7)
Accepted
4.8 BICONNECTIVITY
=======================================================================
@ Einstein College Of Engineering
[132/159]
A
20
B
10
C
D
30
F
Here the removal of `C' vertex will disconnect G from the
graph.
=======================================================================
@ Einstein College Of Engineering
[133/159]
lllly removal of `D' vertex will disconnect E & F from the graph. Therefore `C' & `D' are articulation
points.
The graph is not biconnected, if it has articulation points.
Depth first search provides a linear time algorithm to find all articulation points in a connected graph.
Steps to find Articulation Points :
Step 1 : Perform Depth first search, starting at any vertex
Step 2 : Number the vertex as they are visited, as Num (v).
Step 3 : Compute the lowest numbered vertex for every vertex v in the Depth first spanning tree,
which we call as low (w), that is reachable from v by taking zero or more tree edges and
then possibly one back edge. By definition, Low(v) is the minimum of
(i) Num (v)
(ii) The lowest Num (w) among all back edges (v, w)
(iii) The lowest Low (w) among all tree edges (v, w)
Step 4 :
(i) The root is an articulation if and only if it has more than two children.
(ii) Any vertex v other than root is an articulation point if and only if v has same child w
such that Low (w) Num (v), The time taken to compute this algorithm an a graph is
Note:
For any edge (v, w) we can tell whether it is a tree edge or back edge merely by checking Num (v) and
Num (w). If Num (w) > Num (v) then the edge (v, w) is a back edge.
A (1/1)
Tree edge(V,W)
V
Num(V) = 1
B (2/1)
C (3/1)
Num(W) = 2
D (4/1)
Backedge(w,V)
E
F
G (7/7)
(5/4)
(6/4)
Depth First Tree For Fig (4.8.1) With Num and Low.
ROUTINE TO COMPUTE LOW AND TEST FOR ARTICULATION POINTS
=======================================================================
@ Einstein College Of Engineering
[134/159]
=======================================================================
@ Einstein College Of Engineering
[135/159]
Example:
F
D
Now we stuck in C.
F
D
=======================================================================
D
@ Einstein College Of Engineering
[136/159]
B
G
time O(e+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;
C
E
We consider the vertex A as the root. From vertex A we have three ways, so we resolve the tie
using alphabet order, so we select vertex B.
From B algorithm proceeds to C, then to D, then to E and finally to F which proves to be a dead
end.
So the algorithm backtracks from F to E, then to D and then to C, which provides alternative to
pursue.
Going from C to E which is useless, and the algorithm has to backtrack from E to C and then to
B. From there it goes to the vertices F, E, C & D, from which it return to A, yielding the
Hamiltonian Circuit A, B, F, E, C, D, A.
0
1
2
C
A
9
F
6
D 3
E
E 10
=======================================================================
@ Einstein College Of Engineering
[137/159]
7
8
F
D
E 4
C 11
Dead End
They are shortsighted in their 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
problems.
=======================================================================
@ Einstein College Of Engineering
[138/159]
Nickels (5 cents)
Pennies (1 cent)
The problem is to make a change of a given amount using the smallest possible number of coins.
The informal algorithm is:
Formal algorithm for make change for n units using the least possible number of coins:
Make-Change (n)
C {100, 25, 10 , 5, 1};
// constant
Sol {};
Sum 0
2. Rejected items
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.
=======================================================================
@ Einstein College Of Engineering
[139/159]
fraction in the expression has a numerator equal to 1 and a denominator that is a positive integer, and all
the denominators differ from each other.
The sum of an expression of this type is a positive rational number a/b; for instance the Egyptian fraction
above sums to 43/48. Every positive rational number can be represented by an Egyptian fraction.
A greedy algorithm, for finding such a representation, can add at each stage to the sum of the largest
unit fraction which does not cause the sum to exceed the fraction.
Ex:
5.1.3 Map Coloring:The map coloring problem asks to assign colors to the different regions, where adjacent
regions are not allowed to have the same color. There is no general algorithm to assign minimal number of
colors, yet each map has an assignment which uses no more than four colors.
The greedy approach, repeatedly choose a new color and assign it to as many regions as possible.
5.1.4 Shortest Path Algorithm:
Establish the shortest path between a single source node and all of the other nodes in a graph.
Greedy algorithm: Add an edge that connects a new node to those already selected. The path from the
source to the new node should be the shortest among all the paths to nodes that have not been selected yet.
Greedy Approach:
We need an array to hold the shortest distance to each node, and a visited set.
We take neighboring node; get the direct distance from it to the root node.
Next we have to check if it is less than the entry in the array or if it not less than the entry in the
array or if the array has null value, then we store it in the array.
From that node, put the neighboring node in the visited set. Then we visit every other node it is
connected to and also calculate the distance from it to the root node.
If it is less than the entry in the array or if the array has null value, then we store it in the array.
After we are finished with that node we go to the next connected node, and so on.
At the end we will have an array of values representing the shortest distance from the starting
node to every other node in the graph.
2
10
4
20
30
50
20
3
3
=======================================================================
@ Einstein College Of Engineering
[140/159]
// Initialize S
dist[i] = cost[v,i];
}
S[v] = true;
dist[v] = 0.0;
// Put v in S
for num = 2 to n do
{
// Determine n-1 paths from u. Choose u from among those vertices not in S such that dist[u] is minimum;
S[u] = true;
// Put
u in S
for (each w adjacent to u with S[w] = false) do
if (dist[w] > (dist[u] + cost[u,w])) then
// Update distances
Fractional knapsack problem by Greedy Algorithm 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 is never removed.
2.
=======================================================================
@ Einstein College Of Engineering
[141/159]
subject to
w x
1i n
i i
and 0 xi 1, 1 i n
4. The profits and weights are positive numbers.
Algorithm:
Algorithm GreedyKnapsack(m,n)
// p[1:n] and w[1:n] contain the profits and weights respectively of the n objects
ordered
//such that p[i]/w[i] p[i+1]/w[i+1]. m is the knapsack size and x[1:n] is the
solution vector.
{
for i = 1 to n do x[i] = 0.0;
// Initialize x
U = m;
for i =1 to n do
{
if (w[i] > U) then break;
x[i] = 1.0;
U = U w[i];
}
if(in) then x[i] = U/w[i];
}
Procedure
Procedure GreedyFractionalKnapsack(w, v, W)
FOR i = 1 yo n do
x[i] = 0
weight=0
while(weight<W)
do i = best remaining item
If weight + w[i] = W
then x[i] = 1
weight = weight + w[i]
else
x[i] = (w- weight) / w[i]
weight = W
return x
End;
If the items are already sorted in the descending order of vi/wi, then the while-loop takes a time in
O(n).
=======================================================================
@ Einstein College Of Engineering
[142/159]
If we keep the items in heap with largest vi/wi at the root. Then creating the heap takes O(n) time.
The optimal solution to this problem is to sort by the value of the item in decreasing order. Then pick up
the most valuable item which also has a least weight.
First, if its weight is less than the total weight that can be carried, then deduct the total weight that
can be carried by the weight of the item just pick.
The second item to pick is the most valuable item among those remaining.
5.2 Divide and Conquer
Breaking the problem into several sub-programs that are similar to the original problem but smaller
in size.
2.
3.
For an given array of n elements, the basic idea of binary search is that for a given element we
probe the middle element of the array.
We continue in either lower or upper segment of the array, depending on the outcome of the probe
until we reached the required element.
Algorithm
1. Let A[1n] be an array of non-decreasing sorted order.
2. Let q be the query point.
3. The problem consists of finding q in the array. If q is not in A, then find the position where
q might be inserted.
4. Formally, find the index i such that
5. Look for q either in the first half or in the second half of the array A.
6. Compare q to an element in the middle, n/2 of the array. Let k = n/2.
7. If q < A[k], then search in the A[1..k].
8. Otherwise search A[k+1n] for q.
Recursive call to the function and the procedure
/* Recursive procedure for Binary search*/
BinarySearch(A[0n-1], value, low, high)
{
=======================================================================
@ Einstein College Of Engineering
[143/159]
// not found
// found
}
We can eliminate the recursion by initializing the low = 0 and high = n-1
/* Non-recursive procedure for Binary search */
BinarySearch(A[0.n-1], value)
{
low = 0, high = n-1;
while (low <= high)
{
mid = (low + high) /2
if (A[mid] > value)
high = mid -1
else if (A[mid < value)
low = mid +1
else
return mid
// found
}
return -1
// not found
}
5.2.2 Merge Sort
DIVIDE: Partition the n- element sequence to be sorted into two subsequences of n/2 elements each.
COMBINE: Merge the two sorted subsequences of size n/2 each to produce the sorted sequence
consisting of n elements.
Overall process
1.
2.
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]
T (n) = 0 if n=1
whose solution is T(n) = O (n log n).
We process the execution of elements in the array so that for some j, all the records with keys less
than v appears in A[1], A[2] .. A[j] and all those with keys v or greater than v appear in
A[j+1] ..A[n]
4. Apply quick sort to the newly formed array A[1] A[j] and A[j+1] A[n].
5. 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.
For randomly chosen pivots, the expected running of the algorithm satisfies recurrence equation.
T(N) = T(i) + T(N-j-i) + CN
The running time of quick sort is equal to the running time of the two recursive calls plus the linear
time spent in the partition.
Algorithm:
void quicksort(int s[], int l, int h)
{
int p; /* index of partition */
if ((h - l) > 0) {
p = partition(s, l, h);
quicksort(s, l, p - 1);
quicksort(s, p + 1, h);
}
=======================================================================
@ Einstein College Of Engineering
[145/159]
The given problem has two N by N matrices A and B and we have to compute C = AxB.
C11 C12
A11 A12 B11 B12
=
C22
A21 A22 B21 B22
21
The divide and conquer method for performing the multiplication is as shown below:
P1 = (A11 + A22) (B11 + B22)
P2 = (A21+A22)B11
P3 = A11(B12-B22)
P4 = A22(B12-B22)
P5 = (A11+A12)B22
P6 = (A21-A11) (B11+B12)
P7 = (A12-A22) (B21 + B22)
=======================================================================
@ Einstein College Of Engineering
[146/159]
if n =1
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.
Speed it up by only solving each subproblem once and remembering the solution later use.
Principle of Optimality
The dynamic programming relies on a principle of optimality, which states that an optimal
sequence of decisions or choices has the property that whatever the initial state and
decision are, the remaining decisions must constitute an optimal decision sequence with
regard to the state resulting from the first decision.
Thus, the essential difference between the greedy method and dynamic programming is that in the
greedy method only one decision sequence is ever generated. In dynamic programming, many
decision sequences may be generated. However, sequences containing suboptimal subsequences
cannot be optimal(it the principle of optimality holds) and so will not be generated.
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.
=======================================================================
@ Einstein College Of Engineering
[147/159]
The problem statement is that a thief robbing a store and can carry a maximal weight of W into their
knapsack.
There are n items and ith item weight wi and is worth vi dollars.
What items should thief take? There are two versions of problem;
1. Fractional knapsack problem which dealed in greedy algorithm.
2. 0 1 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 a fraction of an item. 0-1 knapsack problem has
the following properties:
1. Exhibit no greedy choice property.
2. Exhibit optimal substructure property.
3. Only dynamic programming algorithm exists.
Step1: 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 original 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 smaller 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 3 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
maximum (combined) computing time of any subset of files (1,2..i)ined) size at most w.If we can compute
all the entries of this array, then the array entry V[n.W] will contain the maximum computing time of files
that can fit into the storage, that is, the solution to our problem.
Step 2: Recursively define the value of an optimal solution in terms of solutions to smaller problems.
Initial Settings: Set
=======================================================================
@ Einstein College Of Engineering
[148/159]
=======================================================================
@ Einstein College Of Engineering
[149/159]
A bottom-up approach computes f(0), f(1), f(2), f(3), f(4), f(5) in the listed order.
Greedy Approach VS Dynamic Programming (DP)
Greedy and Dynamic Programming are methods for solving optimization problems.
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]
DP provides efficient 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.
Go straight
Go left
Go right
One or more sequences of choices may (or may not) lead to a solution
b) Coloring a map
You wish to color a map with not more than four colors red, yellow, green, blue
One or more sequences of choices may (or may not) lead to a solution
c) Solving a puzzle
In this puzzle, all holes but one are filled with white pegs
=======================================================================
@ Einstein College Of Engineering
[151/159]
One or more sequences of choices may (or may not) lead to a solution
Terminology I
A tree is composed of nodes
To explore node N:
1. If N is a goal node, return success
2. If N is a leaf node, return failure
3. For each child C of N,
3.1. Explore C
3.1.1. If C was successful, return success
4. Return failure
d) 8-Queen Problem:
In the Eight Queens problem the challenge is to place eight queens pieces from the game of Chess on a
chessboard so that no queen piece 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 failed attempts there cant be any solution!.
However there are in fact ninety-two valid solutions to the problem although many of those ninetytwo are symmetrical mirrors. All of the 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 eight 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]
a b c d e f
g h
8
7
6
5
4
3
2
1
a b c d e f
8
7
6
5
4
3
2
1
8
7
6
5
4
3
2
1
a b c d e f g h
Unique solution 1
a b c d e f
a b c d e f
8
7
6
5
4
3
2
1
a b c d e f
8
7
6
5
4
3
2
1
a b c d e f
8
7
6
5
4
3
2
1
a b c d e f
8 8
7 7
6 6
8
7
6
5
4
3
2
1
a b c d e f g h
Unique solution 3
g h
8
7
6
5
4
3
2
1
a b c d e f g h
Unique solution 5
g h
g h
8
7
6
5
4
3
2
1
g h
8
7
6
5
4
3
2
1
a b c d e f g h
Unique solution 4
8
7
6
a b c d e f
a b c d e f g h
Unique solution 2
g h
8
7
6
5
4
3
2
1
g h
8
7
6
5
4
3
2
1
a b c d e f g h
Unique solution 6
g h
a b c d e f
8
7
6
8
7
6
g h
8
7
6
=======================================================================
@ Einstein College Of Engineering
[153/159]
g h
8
7
6
5
4
3
2
1
a b c d e f
8
7
6
5
4
3
2
1
g h
8
7
6
5
4
3
2
1
a b c d e f g h
Unique solution 10
a b c d e f
8
7
6
5
4
3
2
1
a b c d e f g h
Unique solution 11
g h
8
7
6
5
4
3
2
1
8
7
6
5
4
3
2
1
a b c d e f g h
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[18]
count 0
queen[0] 0
repeat until queen[0] < 8 do
if is_row_good(0) = YES
queen[0] queen[0] + 1
queen[1] 0
repeat until queen[1] < 8 do
if is_row_good(0) = YES
queen[1] queen[1] + 1
queen[2] 0
repeat until queen[2] < 8 do
if is_row_good(0) = YES
queen[2] queen[2] + 1
queen[3] 0
repeat until queen[3] < 8 do
if is_row_good(0) = YES
queen[3] queen[3] + 1
queen[4] 0
=======================================================================
@ Einstein College Of Engineering
[154/159]
=======================================================================
@ Einstein College Of Engineering
[155/159]
=======================================================================
@ Einstein College Of Engineering
[156/159]
The first set of constraints ensures that for each i exactly two variables corresponding to edges
incident with i are chosen. Since each edge has two endpoints, this implies that exactly n variables are
allowed to take the value 1. The second set of constraints consists of the subtour elimination constraints.
Each of these states for a specific subset S of V that the number of edges connecting vertices in S has to be
less than |S| thereby ruling out that these form a subtour. Unfortunately there are exponentially many of
these constraints. The given constraints determine the set of feasible solutions S. One obvious way of
relaxing this to a set of potential solutions is to relax (i.e. discard) the subtour elimination constrains. The set
of potential solutions P is then the family of all sets of subtours such that each i belongs to exactly one of the
subtours in each set in the family, cf.
Another possibility is decribed, which in a B&B context turns out to be more appropriate. A
subproblem of a given symmetric TSP is constructed by deciding for a subset A of the edges of G that these
must be included in the tour to be constructed, while for another subset B the edges are excluded from the
tour. Exclusion of an edge (i; j) is usually modeled by setting cij to 1, whereas the inclusion of an edge can
be handled in various ways as e.g. graph contraction. The number of feasible solutions to the problem is
(n1)! /2, which for n = 50 is appr. 3x1062
5. 7 THE ANALYSIS OF ALGORITHMS
The analysis of algorithms is made considering both qualitative and quantitative aspects to get a solution
that is economical in the use of computing and human resources which improves the performance of an
algorithm.
A good algorithm usually posses the following qualities and capabilities:
1. 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]
notations.
This notation is used to describe the best case running time of algorithms and concerned with very
large values of N.
Definition : - T(N) = omega(f(N)), if there are positive constants C and no such that T(N) CF(N) when N no
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 C1, C2 and no such that
T(N) = O(F(N)) and T(N) =
Definition : - T(N) =
=======================================================================
@ Einstein College Of Engineering
[158/159]
i+1
if i < n return i
else return - 1
Here, the best - case efficiency is 0(1) where the first element is itself the search element and the worst case efficiency is 0(n) where the last element is the search element or the search element may not be found
in the given array.
=======================================================================
@ Einstein College Of Engineering
[159/159]