JNTUA Advanced Data Structures & Algorithms Notes R20
JNTUA Advanced Data Structures & Algorithms Notes R20
me/jntua
UNIT –I
Introduction to Algorithms:
Algorithms, Pseudocode for expressing algorithms, Performance Analysis-Space
complexity, Time complexity, Asymptotic Notation- Big oh, Omega, Theta notation and
Little oh notation, Polynomial Vs Exponential Algorithms, Average, Best and Worst Case
Complexities, Analysing Recursive Programs.
An Algorithm is any well-defined computational procedure that takes some value or set of
values as Input and produces a set of values or some value as output. Thus algorithm is a
sequence of computational steps that transforms the input into the output.
Formal Definition:
Algorithm Specification:
Algorithm can be described in three ways.
1. Natural language like English:
1
www.android.universityupdates.in | www.universityupdates.in |
When this way is choused care should be taken, we should ensure that each & every
statement is definite.
2. Graphic representation called flowchart:
This method will work well when the algorithm is small& simple.
3. Pseudo-code Method:
In this method, we should typically describe algorithms as program, which resembles
language like Pascal & algol.
While Loop:
While < condition > do
{
<statement-1>
.
.
.
<statement-n> }
2
www.android.universityupdates.in | www.universityupdates.in |
For Loop:
For variable: = value-1 to value-2 step step do
{
<statement-1>
.
.
.
<statement-n>
}
repeat-until:
repeat
<statement-1>
.
.
.
<statement-n>
until<condition>
As an example, the following algorithm fields & returns the maximum of ‘n’ given
numbers:
1. algorithm Max(A,n)
2. // A is an array of size n
3
www.android.universityupdates.in | www.universityupdates.in |
3. {
4. Result := A[1];
5. for I:= 2 to n do
6. if A[I] > Result then
7. Result :=A[I];
8. return Result;
9. }
In this algorithm (named Max), A & n are procedure parameters. Result & I are Local
variables.
Selection Sort:
1. For i:= 1 to n do
2. {
3. Examine a[I] to a[n] and suppose the smallest element is at a[j];
4. Interchange a[I] and a[j];
5. }
4
www.android.universityupdates.in | www.universityupdates.in |
Selection Sort:
Selection Sort begins by finding the least element in the list. This element is
moved to the front. Then the least element among the remaining element is found out and put
into second position. This procedure is repeated till the entire list has been studied.
1 is selected , 1,5,4,3,2
2 is selected, 1,2,4,3,5
3 is selected, 1,2,3,4,5
4 is selected, 1,2,3,4,5
Proof:
We first note that any I, say I=q, following the execution of lines 6 to 9,it is the
case that a[q] Þ a[r],q<r<=n.
Also observe that when ‘i’ becomes greater than q, a[1:q] is unchanged. Hence,
following the last execution of these lines (i.e. I=n).We have a[1] <= a[2]
<=……a[n].
We observe this point that the upper limit of the for loop in the line 4 can be
changed to n-1 without damaging the correctness of the algorithm.
Algorithm:
5
www.android.universityupdates.in | www.universityupdates.in |
PERFORMANCE ANALYSIS:
1. Space Complexity:
The space complexity of an algorithm is the amount of money it needs to run to
compilation.
2. Time Complexity:
The time complexity of an algorithm is the amount of computer time it needs to run to
compilation.
Space Complexity:
Algorithm abc(a,b,c)
{
return a+b++*c+(a+b-c)/(a+b) +4.0;
}
The Space needed by each of these algorithms is seen to be the sum of the following
component.
1.A fixed part that is independent of the characteristics (eg:number,size)of the inputs and
outputs.
The part typically includes the instruction space (ie. Space for the code), space for
simple variable and fixed-size component variables (also called aggregate) space for
constants, and so on.
variable part that consists of the space needed by component variables whose size is
dependent on the particular problem instance being solved, the space needed by
referenced variables (to the extent that is depends on instance characteristics), and
the recursion stack space.
a. The space requirement s(p) of any algorithm p may therefore be written as,
{
s=0.0;
for I=1 to n do
s= s+a[I];
6
www.android.universityupdates.in | www.universityupdates.in |
return s;
}
The problem instances for this algorithm are characterized by n,the number of elements
to be summed. The space needed d by ‘n’ is one word, since it is of type integer.
The space needed by ‘a’a is the space needed by variables of tyepe array of floating point
numbers.
This is atleast ‘n’ words, since ‘a’ must be large enough to hold the ‘n’ elements to be
summed.
So,we obtain Ssum(n)>=(n+s) [ n for a[ ],one each for n,I a& s]
Time Complexity:
The time T(p) taken by a program P is the sum of the compile time and the run
time(execution time)
The compile time does not depend on the instance characteristics. Also we may assume
that a compiled program will be run several times without recompilation .This rum time is
denoted by tp(instance characteristics).
The number of steps any problem statemn t is assigned depends on the kind of statement.
Assignment statements 1 steps. [Which does not involve any calls to other
algorithms]
Interactive statement such as for, while & repeat-until Control part of the statement.
This is done so that each time a statement in the original program is executes count
is incremented by the step count of that statement.
Algorithm:
Algorithm sum(a,n)
{
s= 0.0;
count = count+1;
for I=1 to n do
7
www.android.universityupdates.in | www.universityupdates.in |
{
count =count+1;
s=s+a[I];
count=count+1;
}
count=count+1;
count=count+1;
return s;
}
If the count is zero to start with, then it will be 2n+3 on termination. So each
invocation of sum execute a total of 2n+3 steps.
2. The second method to determine the step count of an algorithm is to build a table
in which we list the total number of steps contributes by each statement.
First determine the number of steps per execution (s/e) of the statement and the total
number of times (ie., frequency) each statement is executed.
By combining these two quantities, the total contribution of all statements, the
step count for the entire algorithm is obtained.
Statement S/e Frequency Total
1. Algorithm Sum(a,n) 0 - 0
2.{ 0 - 0
3. S=0.0; 1 1 1
5. s=s+a[I]; 1 n n
6. return s; 1 1 1
7. } 0 - 0
Total 2n+3
ASYMPTOTIC NOTATIONS
There are different kinds of mathematical notations used to represent time complexity.
These are called Asymptotic notations. They are as follows:
1. Big oh(O) notation
2. Omega(Ω) notation
3. Theta(ɵ) notation
8
www.android.universityupdates.in | www.universityupdates.in |
The function f(n) = O(g(n)) if and only if there exists positive constants c and n0
such that f(n)≤c*g(n) for all n , n ≥ n0.
Example:
If f(n)=3n+2 then prove that f(n) = O(n)
Let f(n) =3n+2, c=4, g(n) =n
if n=1 3n+2 ≤ 4n
3(1)+2 ≤ 4(1)
3+2 ≤ 4
5 ≤ 4 (F)
if n=2 3n+2≤4n
3(2)+2 ≤ 4(2)
8 ≤ 8 (T)
3n+2 ≤ 4n for all n ≥ 2
This is in the form of f(n) ≤ c*g(n) for all n ≥ n0, where c=4, n0 =2
2. Omega(Ω) notation:
Big oh(O) notation is used to represent lowerbound of algorithm runtime.
The function f(n) = Ω(g(n)) if and only if there exists positive constants c and n0
such that f(n) ≥ c*g(n) for all n , n ≥ n0.
9
www.android.universityupdates.in | www.universityupdates.in |
Example
f(n)=3n+2 then prove that f(n) = Ω(g(n))
Let f(n) =3n+2, c=3, g(n) =n
if n=1 3n+2 ≥ 3n
3(1)+2 ≥ 3(1)
5 ≥ 3 (T)
3n+2 ≥ 4n for all n ≥ 1
This is in the form of f(n) ≥ c*g(n) for all n ≥ n0, where c=3, n0 =1
3. Theta(ɵ) notation:
Theta(ɵ) notation is used to represent the running time between upper bound and
lower bound.
Example:
f(n)=3n+2 then Prove that f(n) = θ(g(n))
Lower bound = 3n+2 ≥ 3n for all n ≥ 1
c1=3,g(n)=n,n0=1
Upper Bound = 3n+2 ≤ 4n for all n ≥ 2
1
www.android.universityupdates.in | www.universityupdates.in |
1
www.android.universityupdates.in | www.universityupdates.in |
Algorithms which have exponential time complexity grow much faster than
polynomial algorithms.
The difference you are probably looking for happens to be where the variable is in the
equation that expresses the run time. Equations that show a polynomial time complexity
have variables in the bases of their terms.
Examples: n3 + 2n2 + 1. Notice n is in the base, NOT the exponent.
In exponential equations, the variable is in the exponent.
Examples: 2n. As said before, exponential time grows much faster. If n is equal to 1000 (a
reasonable input for an algorithm), then notice 10003 is 1 billion, and 21000 is simply huge!
For a reference, there are about 280 hydrogen atoms in the sun, this is much more than 1
billion.
AVERAGE, BEST AND WORST CASE COMPLEXITIES
Best case: This analysis constrains on the input, other than size. Resulting in the fasters
possible run time
Worst case: This analysis constrains on the input, other than size. Resulting in the fasters
possible run time
Average case: This type of analysis results in average running time over every type of input.
Complexity: Complexity refers to the rate at which the storage time grows as a function of
the problem size.
1
www.android.universityupdates.in | www.universityupdates.in |
Merge Sort
T(N) = 2 T(N/2) + CN
Base Condition: T(1) = 1
Recursive Algorithm: Finding min and max in an array
T(N) = 2 T(N/2) + 2
Base Condition: T(1) = 0 and T(2) = 1
Quick Sort
T(N) = T(i) + T(N-i-1) + CN
The time taken by quick sort depends upon the distribution of the input array and partition
strategy. T(i) and T(N-i-1) are two smaller subproblems after the partition where i is the
number of elements that are smaller than the pivot. CN is the time complexity of the partition
process where C is a constant. .
Worst Case: This is a case of the unbalanced partition where the partition process always
picks the greatest or smallest element as a pivot(Think!).For the recurrence relation of the
worst case scenario, we can put i = 0 in the above equation.
T(N) = T(0) + T(N-1) + CN
which is equivalent to
T(N) = T(N-1) + CN
Best Case: This is a case of the balanced partition where the partition process always picks
the middle element as pivot. For the recurrence relation of the worst case scenario, put i =
N/2 in the above equation.
T(N) = T(N/2) + T(N/2-1) + CN
which is equivalent to
T(N) = 2T(N/2) + CN
Average Case: For average case analysis, we need to consider all possible permutation of
input and time taken by each permutation.
T(N) = (for i = 0 to N-1) ∑ ( T(i) + T(N-i-1) ) / N
Note: This looks mathematically complex but we can find several other intuitive ways to
analyse the average case of quick sort.
1
www.android.universityupdates.in | www.universityupdates.in |
Step 2: Add the time complexities of the sub-problems and the total number of basic
operations performed at that stage of recursion.
Step3: Set up a recurrence relation, with a correct base condition, for the number of times the
basic operation is executed.
14
www.android.universityupdates.in | www.universityupdates.in |
Step4: Solve the recurrence or, at least, ascertain the order of growth of its solution. There
are several ways to analyse the recurrence relation but we are discussing here two popular
approaches of solving recurrences:
Method 1: Recursion Tree Method
Method 2: Master Theorem
1
www.android.universityupdates.in | www.universityupdates.in |
Example 1
T(N) = T(N/2) + C
The above recurrence relation is of binary search. Comparing this with master theorem, we
get a = 1, b = 2 and k = 0 because f(N) = C = C(N^0)
Example 2
T(N) = 2*T(N/2) + CN
The above recurrence relation is of merge sort. Comparing this with master theorem,a = 2, b
= 2 and f(N) = CN. Comparing left and right sides of f(N), we get k = 1.
logb(a) = log2(2) = 1 = K
1
www.android.universityupdates.in | www.universityupdates.in |
PART-A (2 Marks)
2. What is an algorithm?
Ans. An algorithm is a finite set of instructions that, if followed, accomplishes a particular
task.
7. Define the asymptotic notation “Big Oh” (O) ,“Omega” ( Ω ) and “theta” (ɵ)
Ans. Big Oh(O) :The function f(n) = O(g(n)) iff there exist positive constants C and no such
that f(n) ≤ C * g(n) for all n, n ≥ n0.
Omega ( Ω ) :The function f(n) =Ω(g(n)) iff there exist positive constant C and no such that
f(n) ≥ C * g(n) for all n, n ≥ n0.
theta(ɵ) :The function f(n) = ɵ (g(n)) iff there exist positive constant C1, C2, and no such that
C1 *g(n) ≤ f(n) ≤ C2* g(n) for all n, n ≥ n0.
1
www.android.universityupdates.in | www.universityupdates.in |
1. Write the merge sort algorithm. Find out the best, worst and average cases of this
algorithm. Sort the following numbers using merge sort:
5. Define the term algorithm and state the criteria the algorithm should satisfy.
6. If f(n)=5n2 + 6n + 4, then prove that f(n) is O(n2).
7. Use step count method and analyze the time complexity when two n×n matrices are
added.
8. Describe the role of space complexity and time complexity of a program ?
9. Discuss various the asymptotic notations used for best case average case and worst
case analysis of algorithms.
18
www.android.universityupdates.in | www.universityupdates.in |
UNIT –II
Binary Search Trees: Definition and Operations, AVL Trees: Definition and Operations,
Applications.
B Trees: Definition and Operations.
INTRODUCTION
In a binary tree, every node can have a maximum of two children but there is no need to
maintain the order of nodes basing on their values. In a binary tree, the elements are arranged
in the order they arrive at the tree from top to bottom and left to right.
To enhance the performance of binary tree, we use a special type of binary tree known
as Binary Search Tree. Binary search tree mainly focuses on the search operation in a binary
tree. Binary search tree can be defined as follows...
Binary Search Tree is a binary tree in which every node contains only smaller values
in its left subtree and only larger values in its right subtree.
In a binary search tree, all the nodes in the left subtree of any node contains smaller values
and all the nodes in the right subtree of any node contains larger values as shown in the
following figure...
19
www.android.universityupdates.in | www.universityupdates.in |
Example
The following tree is a Binary Search Tree. In this tree, left subtree of every node contains
nodes with smaller values and right subtree of every node contains larger values.
Every binary search tree is a binary tree but every binary tree need not to be binary
search tree.
1. Searching become very efficient in a binary search tree since, we get a hint at each
step, about which sub-tree contains the desired element.
2. The binary search tree is considered as efficient data structure in compare to arrays
and linked lists. In searching process, it removes half sub-tree at every step. Searching
for an element in a binary search tree takes o(log2n) time. In worst case, the time it
takes to search an element is 0(n).
3. It also speed up the insertion and deletion operations as compare to that in array and
linked list.
2
www.android.universityupdates.in | www.universityupdates.in |
Example1:
Create the binary search tree using the following data elements.
43, 10, 79, 90, 12, 54, 11, 9, 50
1. Insert 43 into the tree as the root of the tree.
2. Read the next element, if it is lesser than the root node element, insert it as the root of
the left sub-tree.
The process of creating BST by using the given elements, is shown in the image
below.
Example2
10,12,5,4,20,8,7,15 and 13
21
www.android.universityupdates.in | www.universityupdates.in |
Searching means finding or locating some specific element or node within a data structure.
However, searching for some specific node in binary search tree is pretty easy due to the fact
that, element in BST are stored in a particular order.
2
www.android.universityupdates.in | www.universityupdates.in |
Algorithm:
o Step 2: END
Insert function is used to add a new element in a binary search tree at appropriate location.
Insert function is to be designed in such a way that, it must node violate the property of
binary search tree at each value.
23
www.android.universityupdates.in | www.universityupdates.in |
Delete function is used to delete the specified node from a binary search tree. However, we
must delete a node from a binary search tree in such a way, that the property of binary search
tree doesn't violate.
There are three situations of deleting a node from binary search tree.
a) The node to be deleted is a leaf node
It is the simplest case, in this case, replace the leaf node with the NULL and simple free the
allocated space.
In the following image, we are deleting the node 85, since the node is a leaf node, therefore
the node will be replaced with NULL and allocated space will be freed.
2
www.android.universityupdates.in | www.universityupdates.in |
In this case, replace the node with its child and delete the child node, which now contains the
value which is to be deleted. Simply replace it with the NULL and free the allocated space.
In the following image, the node 12 is to be deleted. It has only one child. The node will be
replaced with its child node and the replaced node 12 (which is now leaf node) will simply be
deleted.
In the following image, the node 50 is to be deleted which is the root node of the tree. The in-
order traversal of the tree given below.
replace 50 with its in-order successor 52. Now, 50 will be moved to the leaf of the tree, which
will simply be deleted.
2
www.android.universityupdates.in | www.universityupdates.in |
AVL TREES
AVL tree is a height-balanced binary search tree. That means, an AVL tree is also a
binary search tree but it is a balanced tree. A binary tree is said to be balanced if, the
difference between the heights of left and right subtrees of every node in the tree is either -1,
0 or +1. In other words, a binary tree is said to be balanced if the height of left and right
children of every node differ by either -1, 0 or +1. In an AVL tree, every node maintains an
extra information known as balance factor. The AVL tree was introduced in the year 1962
by G.M. Adelson-Velsky and E.M. Landis.
An AVL tree is defined as follows...
An AVL tree is a balanced binary search tree. In an AVL tree, balance factor of every
node is either -1, 0 or +1.
Balance factor of a node is the difference between the heights of the left and right subtrees of
that node. The balance factor of a node is calculated either height of left subtree - height of
2
www.android.universityupdates.in | www.universityupdates.in |
right subtree (OR) height of right subtree - height of left subtree. In the following
explanation, we calculate as follows...
The above tree is a binary search tree and every node is satisfying balance factor condition.
So this tree is said to be an AVL tree.
Every AVL Tree is a binary search tree but every Binary Search Tree need not be
AVL tree.
In AVL tree, after performing operations like insertion and deletion we need to check
the balance factor of every node in the tree. If every node satisfies the balance factor
condition then we conclude the operation otherwise we must make it balanced. Whenever the
tree becomes imbalanced due to any operation we use rotation operations to make the tree
balanced.
Rotation operations are used to make the tree balanced.
ThereRotation
are four rotations and they
is the process are classified
of moving nodesinto two to
either types.
left or to right to make the tree
balanced.
2
www.android.universityupdates.in | www.universityupdates.in |
In LL Rotation, every node moves one position to left from the current position. To
understand LL Rotation, let us consider the following insertion operation in AVL Tree...
In RR Rotation, every node moves one position to right from the current position. To
understand RR Rotation, let us consider the following insertion operation in AVL Tree...
The LR Rotation is a sequence of single left rotation followed by a single right rotation. In
LR Rotation, at first, every node moves one position to the left and one position to right from
the current position. To understand LR Rotation, let us consider the following insertion
operation in AVL Tree...
2
www.android.universityupdates.in | www.universityupdates.in |
The RL Rotation is sequence of single right rotation followed by single left rotation. In RL
Rotation, at first every node moves one position to right and one position to left from the
current position. To understand RL Rotation, let us consider the following insertion operation
in AVL Tree...
1. Search
2. Insertion
3. Deletion
In an AVL tree, the search operation is performed with O(log n) time complexity. The search
operation in the AVL tree is similar to the search operation in a Binary search tree. We use
the following steps to search an element in AVL tree...
2
www.android.universityupdates.in | www.universityupdates.in |
In an AVL tree, the insertion operation is performed with O(log n) time complexity. In AVL
Tree, a new node is always inserted as a leaf node. The insertion operation is performed as
follows...
Step 1 - Insert the new element into the tree using Binary Search Tree insertion logic.
Step 2 - After insertion, check the Balance Factor of every node.
Step 3 - If the Balance Factor of every node is 0 or 1 or -1 then go for next
operation.
Step 4 - If the Balance Factor of any node is other than 0 or 1 or -1 then that tree is
said to be imbalanced. In this case, perform suitable Rotation to make it balanced and
go for next operation.
3
www.android.universityupdates.in | www.universityupdates.in |
31
www.android.universityupdates.in | www.universityupdates.in |
The deletion operation in AVL Tree is similar to deletion operation in BST. But after every
deletion operation, we need to check with the Balance Factor condition. If the tree is balanced
after deletion go for next operation otherwise perform suitable rotation to make the tree
Balanced.
The two types of rotations are L rotation and R rotation. Here, we will discuss R rotations.
L rotations are the mirror images of them.
If the node which is to be deleted is present in the left sub-tree of the critical node, then L
rotation needs to be applied else if, the node which is to be deleted is present in the right sub-
tree of the critical node, the R rotation will be applied.
Let us consider that, A is the critical node and B is the root node of its left sub-tree. If node
X, present in the right sub-tree of A, is to be deleted, then there can be three different
situations:
If the node B has 0 balance factor, and the balance factor of node A disturbed upon deleting
the node X, then the tree will be rebalanced by rotating tree using R0 rotation.
The critical node A is moved to its right and the node B becomes the root of the tree with T1
as its left sub-tree. The sub-trees T2 and T3 becomes the left and right sub-tree of the node A.
the process involved in R0 rotation is shown in the following image.
3
www.android.universityupdates.in | www.universityupdates.in |
Example:
Delete the node 30 from the AVL tree shown in the following image.
Solution
In this case, the node B has balance factor 0, therefore the tree will be
rotated by using R0 rotation as shown in the following image. The node
B(10) becomes the root, while the node A is moved to its right. The right
child of node B will now become the left child of node A.
33
www.android.universityupdates.in | www.universityupdates.in |
Example
Delete Node 55 from the AVL tree shown in the following image.
Solution :
Deleting 55 from the AVL Tree disturbs the balance factor of the node 50 i.e. node A which
becomes the critical node. This is the condition of R1 rotation in which, the node A will be
moved to its right (shown in the image below). The right of B is now become the left of A
(i.e. 45).
34
www.android.universityupdates.in | www.universityupdates.in |
R-1 rotation is to be performed if the node B has balance factor -1. This case is treated in the
same way as LR rotation. In this case, the node C, which is the right child of node B,
becomes the root node of the tree with B and A as its left and right children respectively.
The sub-trees T1, T2 becomes the left and right sub-trees of B whereas, T3, T4 become the
left and right sub-trees of A. The process involved in R-1 rotation is shown in the following
image.
3
www.android.universityupdates.in | www.universityupdates.in |
Example
Delete the node 60 from the AVL tree shown in the following image.
Solution:
in this case, node B has balance factor -1. Deleting the node 60, disturbs the balance factor of
the node 50 therefore, it needs to be R-1 rotated. The node C i.e. 45 becomes the root of the
tree with the node B(40) and A(50) as its left and right child.
Applications of A VL Trees
3
www.android.universityupdates.in | www.universityupdates.in |
B Tree
B Tree is a specialized m-way tree that can be widely used for disk access. A B-Tree of order
m can have at most m-1 keys and m children. One of the main reason of using B tree is its
capability to store large number of keys in a single node and large key values by keeping the
height of the tree relatively small.
A B tree of order m contains all the properties of an M way tree. In addition, it contains the
following properties.
It is not necessary that, all the nodes contain the same number of children but, each node
must have m/2 number of nodes.
While performing some operations on B Tree, any property of B Tree may violate such as
number of minimum children a node can have. To maintain the properties of B Tree, the tree
may split or join.
Operations of B Trees
1.Searching
2.Insertion
3.Deletion
3
www.android.universityupdates.in | www.universityupdates.in |
i) Searching :
Searching in B Trees is similar to that in Binary search tree. For example, if we search for an
item 49 in the following B Tree. The process will something like following :
1. Compare item 49 with root node 78. since 49 < 78 hence, move to its left sub-tree.
2. Since, 40<49<56, traverse right sub-tree of 40.
3. 49>45, move to right. Compare 49.
4. match found, return.
Searching in a B tree depends upon the height of the tree. The search algorithm takes O(log
n) time to search any element in a B tree.
ii) Inserting
Insertions are done at the leaf node level. The following algorithm needs to be followed in
order to insert an item into B Tree.
1. Traverse the B Tree in order to find the appropriate leaf node at which the node can
be inserted.
2. If the leaf node contain less than m-1 keys then insert the element in the increasing
order.
3. Else, if the leaf node contains m-1 keys, then follow the following steps.
o Insert the new element in the increasing order of elements.
o Split the node into the two nodes at the median.
o Push the median element upto its parent node.
o If the parent node also contain m-1 number of keys, then split it too by
following the same steps.
3
www.android.universityupdates.in | www.universityupdates.in |
Example:
Insert the node 8 into the B Tree of order 5 shown in the following image.
The node, now contain 5 keys which is greater than (5 -1 = 4 ) keys. Therefore split the node
from the median i.e. 8 and push it up to its parent node shown as follows.
iii) Deletion
Deletion is also performed at the leaf nodes. The node which is to be deleted can either be a
leaf node or an internal node. Following algorithm needs to be followed in order to delete a
node from a B tree.
39
www.android.universityupdates.in | www.universityupdates.in |
4. If neither of the sibling contain more than m/2 elements then create a new leaf node
by joining two leaf nodes and the intervening element of the parent node.
5. If parent is left with less than m/2 nodes then, apply the above process on the parent
too.
If the the node which is to be deleted is an internal node, then replace the node with its in-
order successor or predecessor. Since, successor or predecessor will always be on the leaf
node hence, the process will be similar as the node is being deleted from the leaf node.
Example 1
Delete the node 53 from the B Tree of order 5 shown in the following figure.
4
www.android.universityupdates.in | www.universityupdates.in |
Now, 57 is the only element which is left in the node, the minimum number of elements that
must be present in a B tree of order 5, is 2. it is less than that, the elements in its left and right
sub-tree are also not sufficient therefore, merge it with the left sibling and intervening
element of parent i.e. 49.
The final B tree is shown as follows.
Application of B tree
B tree is used to index the data and provides fast access to the actual data stored on the disks
since, the access to value stored in a large database that is stored on a disk is a very time
consuming process.
Searching an un-indexed and unsorted database containing n key values needs O(n) running
time in worst case. However, if we use B Tree to index this database, it will be searched in
O(log n) time in worst case.
4
www.android.universityupdates.in | www.universityupdates.in |
PART-A (2 Marks)
4
www.android.universityupdates.in | www.universityupdates.in |
4
www.android.universityupdates.in | www.universityupdates.in |
1. Explain the AVL tree insertion and deletion with suitable example.
2. Describe the algorithms used to perform single and double rotation on AVL tree.
3. Explain about B+ trees with suitable algorithm.
4. How to insert and delete an element into a binary search tree and write down the code
for the insertion routine with an example.
5. What are binary search tree? Write an algorithm for deleting a node in a binary search
tree.
6. Create a binary search tree for the following numbers start from an empty binary
search tree. 45,26,10,60,70,30,40 Delete keys 10,60 and 45 one after the other and
show the trees at each
44
www.android.universityupdates.in | www.universityupdates.in |
UNIT –III
Red-Black Trees, Splay Trees, Applications. Hash Tables: Introduction, Hash Structure, Hash
functions, Linear Open Addressing, Chaining and Applications.
Example
Following is a Red-Black Tree which is created by inserting numbers from 1 to 9.
The above tree is a Red-Black tree where every node is satisfying all the properties of Red-Black
Tree.
Every Red Black Tree is a binary search tree but every Binary Search Tree need not be Red
Black tree.
If all the properties are satisfied then we go to next operation otherwise we perform the following
operation to make it Red Black Tree.
1. Recolor
2. Rotation
3. Rotation followed by Recolor
The insertion operation in Red Black tree is performed using the following steps...
Step 1 - Check whether tree is Empty.
Step 2 - If tree is Empty then insert the newNode as Root node with color Black and exit from
the operation.
Step 3 - If tree is not Empty then insert the newNode as leaf node with color Red.
Step 4 - If the parent of newNode is Black then exit from the operation.
Step 5 - If the parent of newNode is Red then check the color of parentnode's sibling of newNode.
Step 6 - If it is colored Black or NULL then make suitable Rotation and Recolor it.
Step 7 - If it is colored Red then perform Recolor. Repeat the same until tree becomes Red Black Tree.
The deletion operation in Red-Black Tree is similar to deletion operation in BST. But after every deletion
operation, we need to check with the Red-Black Tree properties. If any of the properties are violated then
make suitable operations like Recolor, Rotation and Rotation followed by Recolor to make it Red-
Black .
4
www.android.universityupdates.in | www.universityupdates.in |
47
www.android.universityupdates.in | www.universityupdates.in |
Splay tree is another variant of a binary search tree. In a splay tree, recently accessed element is
placed at the root of the tree. A splay tree is defined as follows...
Splay Tree is a self - adjusted Binary Search Tree in which every operation on element
rearranges the tree so that the element is placed at the root position of the tree.
In a splay tree, every operation is performed at the root of the tree. All the operations in splay tree are
involved with a common operation called "Splaying".
Splaying an element, is the process of bringing it to the root position by performing suitable rotation
operations. In a splay tree, splaying an element rearranges all the elements in the tree so that splayed
element is placed at the root of the tree. By splaying elements we bring more frequently used
elements closer to the root of the tree so that any operation on those elements is performed quickly.
That means the splaying operation automatically brings more frequently used elements. .
Every operation on splay tree performs the splaying operation. For example, the insertion operation
first inserts the new element using the binary search tree insertion process, then the newly inserted
element is splayed so that it is placed at the root of the tree. The search operation in a splay tree is
nothing but searching the element using binary search process .In splay tree, to splay any element we
use the following rotation operations...
Example
Zig Rotation
The Zig Rotation in splay tree is similar to the single right rotation in AVL Tree rotations. In
zig rotation, every node moves one position to the right from its current position.
4
www.android.universityupdates.in | www.universityupdates.in |
Zag Rotation
The Zag Rotation in splay tree is similar to the single left rotation in AVL Tree rotations. In zag
rotation, every node moves one position to the left from its current position. Consider the following
example...
Zig-Zig Rotation
The Zig-Zig Rotation in splay tree is a double zig rotation. In zig-zig rotation, every node moves
two positions to the right from its current position. Consider the following example...
4
www.android.universityupdates.in | www.universityupdates.in |
Zag-Zag Rotation
The Zag-Zag Rotation in splay tree is a double zag rotation. In zag-zag rotation, every
node moves two positions to the left from its current position. Consider the following example...
Zig-Zag Rotation
The Zig-Zag Rotation in splay tree is a sequence of zig rotation followed by zag rotation.
In zig-zag rotation, every node moves one position to the right followed by one position to the
left from its current position. Consider the following example...
Zag-Zig Rotation
The Zag-Zig Rotation in splay tree is a sequence of zag rotation followed by zig rotation. In zag-zig
rotation, every node moves one position to the left followed by one position to the right from its current
position.
Consider the following example...
5
www.android.universityupdates.in | www.universityupdates.in |
Every Splay tree must be a binary search tree but it is need not to be balanced tree.
Insertion Operation in Splay Tree
The insertion operation in Splay tree is performed using following steps...
Step 1 - Check whether tree is Empty.
Step 2 - If tree is Empty then insert the newNode as Root node and exit from the operation.
Step 3 - If tree is not Empty then insert the newNode as leaf node using Binary Search tree
insertion logic.
Step 4 - After insertion, Splay the newNode
Applications:
Decision-based algorithm is used in machine learning which works upon the algorithm of tree.
Databases also uses tree data structures for indexing. Domain Name Server(DNS) also uses tree
structures. File explorer/my computer of mobile/any computer.
Some applications of the trees are:
1. XML Parser uses tree algorithms.
2. Decision-based algorithm is used in machine learning which works upon the algorithm of tree.
3. Databases also uses tree data structures for indexing.
4. Domain Name Server(DNS) also uses tree structures.
5. File explorer/my computer of mobile/any computer
6. BST used in computer Graphics
7. Posting questions on websites like Quora, the comments are child of questions
Hash Tables :
Introduction:
We've seen searches that allow you to look through data in O(n) time, and searches that allow you to
look through data in O(logn) time, but imagine a way to find exactly what you want in O(1) time. Think
it's not possible? Think again! Hash tables allow the storage and retrieval of data in an average time
of 5
www.android.universityupdates.in | www.universityupdates.in |
At its most basic level, a hash table data structure is just an array. Data is stored into this array at
specific indices designated by a hash function. A hash function is a mapping between the set of input
data and a set of integers.
With hash tables, there always exists the possibility that two data elements will hash to the same integer
value. When this happens, a collision results (two data members try to occupy the same place in the hash
table array),
and methods have been devised to deal with such situations. In this guide, we will cover two methods,
linear probing and separate chaining, focusing on the latter.
A hash table is made up of two parts: an array (the actual table where the data to be searched is stored)
and a mapping function, known as a hash function. The hash function is a mapping from the input space
to the integer space that defines the indices of the array. In other words, the hash function provides a
way for assigning numbers to the input data such that the data can then be stored at the array index
corresponding to the assigned number.
Let's take a simple example. First, we start with a hash table array of strings (we'll use strings as the data
being stored and searched in this example). Let's say the hash table size is 12:
Next we need a hash function. There are many possible ways to construct a hash function. We'll discuss
these possibilities more in the next section. For now, let's assume a simple hash function that takes a
string as input. The returned hash value will be the sum of the ASCII characters that make up the string
mod the size of the table:
int hash(char *str, int table_size) { int sum; /* Make sure a valid string passed in */ if (str==NULL)
return -1; /*
Sum up all the characters in the string */ for( ; *str; str++) sum += *str; /* Return the sum mod the
table size */ return sum % table_size; }
www.android.universityupdates.in | www.universityupdates.in |
We run "Steve" through the hash function, and find that hash("Steve",12) yields 3:
Let's try another string: "Spark". We run the string through the hash function and find
that hash("Spark",12) yields 6. Fine. We insert it into the hash table:
What happened? A hash function doesn't guarantee that every input will map to a different output. There
is always the chance that two inputs will hash to the same output. This indicates that both elements
should be inserted at the same place in the array, and this is impossible. This phenomenon is known as a
collision.
There are many algorithms for dealing with collisions, such as linear probing an d separate chaining.
While each of the methods has its advantages, we will only discuss separate chaining here.
Separate chaining requires a slight modification to the data structure. Instead of storing the data
elements right into the array, they are stored in linked lists. Each slot in the array then points to one of
these linked lists. When an element hashes to a value, it is added to the linked list at that index in the
array. Because a linked list has no limit on length, collisions are no longer a problem. If more than one
element hashes to the same value, then both
are stored in that linked list.
Let's look at the above example again, this time with our modified data structure:
Figure %: After adding "Steve" to the table And "Spark" which hashes to 6:
54
www.android.universityupdates.in | www.universityupdates.in |
Problem : How does a hash table allow for O(1) searching? What is the worst case efficiency of a look
up in a hash table using separate chainging?
A hash table uses hash functions to compute an integer value for data. This integer value can then be
used as an index into an array, giving us a constant time access to the requested data. However, using
separate chaining, we won't always achieve the best and average case efficiency of O(1). If we have too
small a hash table for the data set size and/or a bad hash function, elements can start to build in one
index in the array. Theoretically, all n element could end up in the same linked list. Therefore, to do a
search in the worst case is equivalent to looking up a data element in a linked list, something we already
know to be O(n) time. However, with a good hash function and a well created hash table, the chances of
this happening are, for all intents and purposes, ignorable. Problem : The bigger the ratio between the
size of the hash table and the number of data elements, the less chance there is for collision. What is a
drawback to making the hash table big enough so the chances of collision is ignorable?
Wasted memory space
Problem : How could a linked list and a hash table be combined to allow someone to run through the
list from item to item while still maintaining the ability to access an individual element in O(1) time?
Hash Functions
As mentioned briefly in the previous section, there are multiple ways for constructing a hash function.
Remember that hash function takes the data as input (often a string), and return s an integer in the range
of possible indices into the hash table. Every hash function must do that, including the bad ones. So
what makes for a good hash function?
Rule 1: If something else besides the input data is used to determine the hash, then the hash value is not
as dependent upon the input data, thus allowing for a worse distribution of the hash values.
Rule 2: If the hash function doesn't use all the data would
www.android.universityupdates.in | www.universityupdates.in |
cause an inappropriate number of similar hash values resulting in too many collisions.
Rule 3: If the hash function does not uniformly distribute the data across the entire set of possible hash
values, a large number of collisions will result, cutting down on the efficiency of the hash table.
Rule 4: In real world applications, many data sets contain very similar data elements.
Hash Table is a data structure which stores data in an associative manner. In a hash table, data is
stored in an array format, where each data value has its own unique index value. Access of data becomes
very fast if we know the index of the desired data.
Thus, it becomes a data structure in which insertion and search operations are very fast irrespective of
the size of the data. Hash Table uses an array as a storage medium and uses hash technique to generate
an index where an element is to be inserted or is to be located from.
Hashing
Hashing is a technique to convert a range of key values into a range of indexes of an array. We're
going to use modulo operator to get a range of key values. Consider an example of hash table of size 20,
and the following items are to be stored. Item are in the (key,value) format.
(1,20)
(2,70)
(42,80)
(4,25)
(12,44)
(14,32)
(17,11)
(13,78)
(37,98)
5
www.android.universityupdates.in | www.universityupdates.in |
1 1 1 % 20 = 1 1
2 2 2 % 20 = 2 2
3 42 42 % 20 = 2 2
4 4 4 % 20 = 4 4
5 12 12 % 20 = 12 12
6 14 14 % 20 = 14 14
7 17 17 % 20 = 17 17
8 13 13 % 20 = 13 13
9 37 37 % 20 = 17 17
Linear Probing
As we can see, it may happen that the hashing technique is used to create an already used index
of the array. In such a case, we can search the next empty location in the array by looking into
the next cell until we find an empty cell. This technique is called linear probing.
1 1 1 % 20 = 1 1 1
2 2 2 % 20 = 2 2 2
3 42 42 % 20 = 2 2 3
4 4 4 % 20 = 4 4 4
5 12 12 % 20 = 12 12 12
6 14 14 % 20 = 14 14 14
7 17 17 % 20 = 17 17 17
8 13 13 % 20 = 13 13 13
9 37 37 % 20 = 17 17 18
5
www.android.universityupdates.in | www.universityupdates.in |
Basic Operations
Following are the basic primary operations of a hash table.
Search − Searches an element in a hash table.
Insert − inserts an element in a hash table.
delete − Deletes an element from a hash table.
DataItem
Define a data item having some data and key, based on which the search is to be conducted
in a hash table.
struct DataItem {
int data;
int key;
};
Hash Method
Define a hashing method to compute the hash code of the key of the data item.
int hashCode(int key){
return key % SIZE;
}
Search Operation
Whenever an element is to be searched, compute the hash code of the key passed and locate the
element using that hash code as index in the array. Use linear probing to get the element ahead if
the element is not found at the computed hash code.
Insert Operation
Whenever an element is to be inserted, compute the hash code of the key passed and locate the
index using that hash code as an index in the array. Use linear probing for empty location, if an
element is found at the computed hash code.
Delete Operation
Whenever an element is to be deleted, compute the hash code of the key passed and locate the
index using that hash code as an index in the array. Use linear probing to get the element ahead if
an element is not found at the computed hash code. When found, store a dummy item there to
keep the performance of the hash table intact.
5
www.android.universityupdates.in | www.universityupdates.in |
OpenAddressing
Like separate chaining, open addressing is a method for handling collisions. In Open Addressing,
all elements are stored in the hash table itself. So at any point, the size of the table must be greater
than or equal to the total number of keys (Note that we can increase table size by copying old data
if needed).
Insert(k): Keep probing until an empty slot is found. Once an empty slot is found, insert k.
Search(k): Keep probing until slot’s key doesn’t become equal to k or an empty slot is
reached.
Delete(k): Delete operation is interesting. If we simply delete a key, then the search may
fail. So slots of deleted keys are marked specially as “deleted”.
The insert can insert an item in a deleted slot, but the search doesn’t stop at a deleted slot.
Open Addressing is done in the following ways:
a) Linear Probing: In linear probing, we linearly probe for next slot. For example, the typical gap
between two probes is 1 as seen in the example below.
Let hash(x) be the slot index computed using a hash function and S be the table size
If slot hash(x) % S is full, then we try (hash(x) + 1) % S
If (hash(x) + 1) % S is also full, then we try (hash(x) + 2) % S
If (hash(x) + 2) % S is also full, then we try (hash(x) + 3) % S
Let us consider a simple hash function as “key mod 7” and a sequence of keys as 50, 700, 76, 85,
92, 73, 101.
5
www.android.universityupdates.in | www.universityupdates.in |
If (hash(x) + 2*2) % S is also full, then we try (hash(x) + 3*3) % S
6
www.android.universityupdates.in | www.universityupdates.in |
c) Double Hashing We use another hash function hash2(x) and look for i*hash2(x) slot in i’th
rotation.
let hash(x) be the slot index computed using hash function.
If slot hash(x) % S is full, then we try (hash(x) + 1*hash2(x)) % S
If (hash(x) + 1*hash2(x)) % S is also full, then we try (hash(x) + 2*hash2(x)) % S
If (hash(x) + 2*hash2(x)) % S is also full, then we try (hash(x) + 3*hash2(x)) % S
Comparison:
Linear probing has the best cache performance but suffers from clustering. One more advantage of
Linear probing is easy to compute. Quadratic probing lies between the two in terms of cache
performance and clustering.
Double hashing has poor cache performance but no clustering. Double hashing requires more
computation time as two hash functions need to be computed.
Chaining is Less sensitive to the hash Open addressing requires extra care to avoid
3. function or load factors. clustering and load factor.
Wastage of Space (Some Parts of hash table In Open addressing, a slot can be used even if
6. in chaining are never used). an input doesn’t map to it.
60
www.android.universityupdates.in | www.universityupdates.in |
PerformanceofOpenAddressing:
Like Chaining, the performance of hashing can be evaluated under the assumption that each key is
equally likely to be hashed to any slot of the table (simple uniform hashing)
Applications of Hashing
Message Digest.
Password Verification.
Data Structures(Programming Languages)
Compiler Operation.
Rabin-Karp Algorithm.
Linking File name and path together.
www.android.universityupdates.in | www.universityupdates.in |
PART-A (2 Marks)
7. Define Hashing.
Ans: Hashing is the transformation of string of characters into a usually shorter fixed length
6
www.android.universityupdates.in | www.universityupdates.in |
value or key that represents the original string. Hashing is used to index and retrieve items in a
database because it is faster to find the item using the short hashed key than to find it using the
original value.
6
www.android.universityupdates.in | www.universityupdates.in |
full.
6
www.android.universityupdates.in | www.universityupdates.in |
UNIT –IV
Divide and conquer: General method, applications-Binary search, Finding Maximum and
minimum, Quick sort, Merge sort, Strassen’s matrix multiplication
Greedy method: General method, applications-Job sequencing with deadlines,
knapsack problem, Minimum cost spanning trees, Single source shortest path problem.
4.1.General method:
Given a function to compute on ‘n’ inputs the divide-and-conquer strategy suggests splitting
the inputs into ‘k’ distinct subsets, 1<k<=n, yielding ‘k’ sub problems.
These sub problems must be solved, and then a method must be found to combine sub
solutions into a solution of the whole.
If the sub problems are still relatively large, then the divide-and-conquer strategy can possibly
be reapplied.
Often the sub problems resulting from a divide-and-conquer design are of the same type as the
original problem.
For those cases the re application of the divide-and-conquer principle is naturally expressed by
a recursive algorithm.
D And C(Algorithm) is initially invoked as D and C(P), where ‘p’ is the problem to be solved.
Small(P) is a Boolean-valued function that determines whether the i/p size is small enough that
the answer can be computed without splitting.
If this so, the function ‘S’ is invoked.
Otherwise, the problem P is divided into smaller sub problems.
These sub problems P1, P2 …Pk are solved by recursive application of D And C.
Combine is a function that determines the solution to p using the solutions to the ‘k’ sub
problems.
If the size of ‘p’ is n and the sizes of the ‘k’ sub problems are n1, n2 ….nk, respectively, then
the computing time of D And C is described by the recurrence relation.
Where T(n) is the time for D And C on any I/p of size ‘n’.
6
www.android.universityupdates.in | www.universityupdates.in |
g(n) is the time of compute the answer directly for small I/ps.
f(n) is the time for dividing P & combining the solution to
sub problems.
Example:
1) Consider the case in which a=2 and b=2. Let T(1)=2 &
f(n)=n. We have,
T(n) = 2T(n/2)+n
= 2[2T(n/2/2)+n/2]+n
= [4T(n/4)+n]+n
= 4T(n/4)+2n
= 4[2T(n/4/2)+n/4]+2n
6
www.android.universityupdates.in | www.universityupdates.in |
= 4[2T(n/8)+n/4]+2n
= 8T(n/8)+n+2n
= 8T(n/8)+3n
*
*
*
In general, we see that T(n)=2^iT(n/2^i )+in., for any log n >=I>=1.
BINARY SEARCH:
Algorithm Bin search(a,n,x)
// Given an array a[1:n] of elements in non-decreasing
//order, n>=0,determine whether ‘x’ is present and
// if so, return ‘j’ such that x=a[j]; else return 0.
{
low:=1; high:=n;
while (low<=high)
do
{
mid:=[(low+high)/2];
if (x<a[mid]) then high;
else if(x>a[mid]) then
low=mid+1;
else return mid;
}
return 0;
}
6
www.android.universityupdates.in | www.universityupdates.in |
Algorithm, describes this binary search method, where Binsrch has 4I/ps a[], I , l & x.
6
www.android.universityupdates.in | www.universityupdates.in |
Thus we have 2 sequences of integers approaching each other and eventually low becomes >
than high & causes termination in a finite no. of steps if ‘x’ is not present.
X=151
low high mid
1 14 7
8 14 11
12 14 13
14 14 14
Found
x=-14
low high mid
1 14 7
1 6 3
1 2 1
2 2 2
2 1 Not found
6
www.android.universityupdates.in | www.universityupdates.in |
x=9
low high mid
1 14 7
1 6 3
4 6 5
Found
Proof:
We assume that all statements work as expected and that comparisons such as x>a[mid] are
appropriately carried out.
Initially low =1, high= n,n>=0, and a[1]<=a[2]<=.........<=a[n].
If n=0, the while loop is not entered and is returned.
Otherwise we observe that each time thro’ the loop the possible elements to be checked of or
equality with x and a[low], a[low+1],……..,a[mid],……a[high].
If x=a[mid], then the algorithm terminates successfully.
Otherwise, the range is narrowed by either increasing low to (mid+1) or decreasing high to
(mid-1).
Clearly, this narrowing of the range does not affect the outcome of the search.
If low becomes > than high, then ‘x’ is not present & hence the loop is exited.
Let us consider another simple problem that can be solved by the divide-and-conquer
technique.
The problem is to find the maximum and minimum items in a set of ‘n’ elements.
In analyzing the time complexity of this algorithm, we once again concentrate on the no. of
element comparisons.
More importantly, when the elements in a[1:n] are polynomials, vectors, very large numbers,
or strings of character,
the cost of an element comparison is much higher than the cost of the other operations.
Hence, the time is determined mainly by the total cost of the element comparison.
7
www.android.universityupdates.in | www.universityupdates.in |
max:=min:=a[1];
for I:=2 to n do
{
if(a[I]>max) then max:=a[I];
if(a[I]<min) then min:=a[I];
}
}
On the average a[I] is > than max half the time, and so, the avg. no. of comparison is 3n/2-1.
A divide- and conquer algorithm for this problem would proceed as follows:
After having divided ‘P’ into 2 smaller sub problems, we can solve them by recursively invoking
the same divide-and-conquer algorithm.
7
www.android.universityupdates.in | www.universityupdates.in |
7
www.android.universityupdates.in | www.universityupdates.in |
}
}
The procedure is initially invoked by the
statement, MaxMin(1,n,x,y)
Suppose we simulate MaxMin on the following 9 elements
When ‘n’ is a power of 2, n=2^k for some +ve integer ‘k’, then
T(n) = 2T(n/2) +2
= 2(2T(n/4)+2)+2
= 4T(n/4)+4+2
*
*
= 2^k-1T(2)+
7
www.android.universityupdates.in | www.universityupdates.in |
= 2^k-1+2^k-2
= 2^k/2+2^k-2
= n/2+n-2
= (n+2n)/2)-2
T(n)=(3n/2)-2
*Note that (3n/3)-3 is the best-average, and worst-case no. of comparisons when ‘n’ is a power of 2.
QUICK SORT
The divide-and-conquer approach can be used to arrive at an efficient sorting method different
from merge sort.
In merge sort, the file a[1:n] was divided at its midpoint into sub arrays which were
independently sorted & later merged.
In Quick sort, the division into 2 sub arrays is made so that the sorted sub arrays do not need to
be merged later.
This is accomplished by rearranging the elements in a[1:n] such that a[I]<=a[j] for all I
between 1 & n and all j
between (m+1) & n for some m, 1<=m<=n.
Thus the elements in a[1:m] & a[m+1:n] can be independently sorted.
7
www.android.universityupdates.in | www.universityupdates.in |
Algorithm Partition(a,m,p)
//within a[m],a[m+1],…..,a[p-1] the elements
// are rearranged in such a manner that if
//initially t=a[m],then after completion
//a[q]=t for some q between m and
//p-1,a[k]<=t for m<=k<q, and
//a[k]>=t for q<k<p. q is returned
//Set a[p]=infinite.
{
v=a[m];I=m;j=p;
repeat
{
repeat
I=I+1;
until(a[I]>=v);
repeat
j=j-1;
until(a[j]<=v);
if (I<j) then interchange(a,i.j);
}until(I>=j);
a[m]=a[j]; a[j]=v;
retun j;
}
Algorithm Interchange(a,I,j)
//Exchange a[I] with a[j]
{ p=a[
I];
a[I]=a[j];
a[j]=p;
7
www.android.universityupdates.in | www.universityupdates.in |
}
Algorithm: Sorting by Partitioning
Algorithm Quicksort(p,q)
//Sort the elements a[p],….a[q] which resides
//is the global array a[1:n] into ascending
//order; a[n+1] is considered to be defined
// and must be >= all the elements in a[1:n]
{
if(p<q) then // If there are more than one element
{
// divide p into 2
subproblems
j=partition(a,p,q+1);
//’j’ is the position of the partitioning element.
//solve the
subproblems.
quicksort(p,j-1);
quicksort(j+1,q);
//There is no need for combining solution.
}
}
Partition(int m, int p)
{
int v,I,j;
v=a[m];
i=m;
j=p;
do
{
do
i=i+1;
while(a[i]<v);
if (i<j)
interchange(I,j);
} while (I<j);
a[m]=a[j];
a[j]=v;
return j;
}
Interchange(int I, int j)
{
7
www.android.universityupdates.in | www.universityupdates.in |
int p;
p= a[I];
a[I]=a[j];
a[j]=p;
}
Output:
Enter the no. of elements 5
Enter the array elements
3
8
1
5
2
The sorted elements are,
1
2
3
5
8
MERGE SORT
As another example divide-and-conquer, we investigate a sorting algorithm that has the nice
property that is the worst case its complexity is O(n log n)
This algorithm is called merge sort
We assume throughout that the elements are to be sorted in non-decreasing order.
Given a sequence of ‘n’ elements a[1],…,a[n] the general idea is to imagine then split into 2
sets a[1],…..,a[n/2] and a[[n/2]+1],….a[n].
Each set is individually sorted, and the resulting sorted sequences are merged to produce a
single sorted sequence of ‘n’ elements.
Thus, we have another ideal example of the divide-and-conquer strategy in which the splitting
is into 2 equal-sized sets & the combining operation is the merging of 2 sorted sets into one.
7
www.android.universityupdates.in | www.universityupdates.in |
7
www.android.universityupdates.in | www.universityupdates.in |
h = h+1;
}
else
{
b[I]= a[j];
j=j+1;
}
I=I+1;
}
if (h>mid) then
for k=j to high do
{
b[I]=a[k];
I=I+1;
}
else
for k=h to mid do
{
b[I]=a[k];
I=I+1;
}
for k=low to high do a[k] = b[k];
}
Consider the array of 10 elements a[1:10] =(310, 285, 179, 652, 351, 423, 861, 254, 450, 520)
Algorithm Mergesort begins by splitting a[] into 2 sub arrays each of size five (a[1:5] and
a[6:10]).
The elements in a[1:5] are then split into 2 sub arrays of size 3 (a[1:3] ) and 2(a[4:5])
Then the items in a a[1:3] are split into sub arrays of size 2 a[1:2] & one(a[3:3])
The 2 values in a[1:2} are split to find time into one-element sub arrays, and now the merging
begins.
8
www.android.universityupdates.in | www.universityupdates.in |
(310| 285| 179| 652, 351| 423, 861, 254, 450, 520)
1. Let A and B be the 2 n*n Matrix. The product matrix C=AB is calculated by using the
formula,
3. Divide and conquer method suggest another way to compute the product of n*n matrix.
4. We assume that N is a power of 2 .In the case N is not a power of 2 ,then enough rows and
columns of zero
can be added to both A and B .SO that the resulting dimension are the powers of two.
5. If n=2 then the following formula as a computed using a matrix multiplication operation for
the elements of
A & B.
6. If n>2,Then the elements are partitioned into sub matrix n/2*n/2..since ‘n’ is a power of 2
these product can be recursively computed using the same formula .This Algorithm will
continue applying itself to smaller sub
matrix until ‘N” become suitable small(n=2) so that the product is computed directly .
7. The formula are
8
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
2 2 2 2 1 1 1 1 4 4 4 4
2 2 2 2 * 1 1 1 1 1 = 4 4 4 4
2 2 2 2 1 1 1 1 4 4 4 4
2 2 2 2 1 1 1 4 4 4 4
Example
4 4 4 4
*
4 4 4 4
P=(4*4)+(4+4)=64
Q=(4+4)4=32
R=4(4-4)=0
S=4(4-4)=0
T=(4+4)4=32
U=(4-4)(4+4)=0
V=(4-4)(4+4)=0
C11=(64+0-32+0)=32
C12=0+32=32
8
www.android.universityupdates.in | www.universityupdates.in |
C21=32+0=32
C22=64+0-32+0=32
since n/2n/2 &matrix can be can be added in Cn for some constant C, The overall computing
time T(n) of the resulting divide and conquer algorithm is given by the sequence.
That is T(n)=O(n^3)
* Matrix multiplication are more expensive then the matrix addition O(n^3).We can attempt
to reformulate the equation for Cij so as to have fewer multiplication and possibly more
addition .
12. Stressen has discovered a way to compute the Cij of equation (2) using only 7 multiplication
and 18 addition or subtraction.
13. Strassen’s formula are
P= (A11+A12)(B11+B22)
Q= (A12+A22)B11
R= A11(B12-B22)
S= A22(B21-B11)
T= (A11+A12)B22
U= (A21-A11)(B11+B12)
V= (A12-A22)(B21+B22)
C11=P+S-T+V
C!2=R+t
C21=Q+T
C22=P+R-Q+V
8
www.android.universityupdates.in | www.universityupdates.in |
* The function select an input from a[] and removes it. The select input value is assigned to X.
Feasible is a Boolean value function that determines whether X can be included into the solution
vector.
The function Union combines X with The solution and updates the objective function.The function
Greedy describes the essential way that a greedy algorithm will once a particular problem is chosen
and the function subset, feasible & union are properly implemented.
8
www.android.universityupdates.in | www.universityupdates.in |
Example
Suppose we have in a country the following coins are available :
Dollars(100 cents)
Quarters(25 cents)
Dimes( 10 cents)
Nickel(5 Cents)
Pennies(1 cent)
Our aim is paying a given amount to a customer using the smallest possible number of coins.
For example if we must pay 276 cents possible solution then,
1 doll+7 q+ 1 pen9 coins
2 doll +3Q +1 pen6 coins
2 doll+7dim+1 nic +1 pen11 coins.
8
www.android.universityupdates.in | www.universityupdates.in |
K=1;
For I =1 to n do
{ // consider jobs in non increasing order of P[I];find the position for I and check feasibility
insertion
r=k;
while((d[J[r]]>d[i] )and
(d[J[r]] = r)do r =r-1;
if (d[J[r]]<d[I])and (d[I]>r))then
{
for q=k to (r+1) step –1 do J
[q+1]=j[q] J[r+1]=i;
K=k+1;
}
}
return k;
}
Example :
1. n=5 (P1,P2,…P5)=(20,15,10,5,1)
(d1,d2….d3)=(2,2,1,3,3)
8
www.android.universityupdates.in | www.universityupdates.in |
(2,5) (2,5) 16
(1,2,3) (3,2,1) 45
(1,2,4) (1,2,4) 40
KNAPSACK PROBLEM
we are given n objects and knapsack or bag with capacity M object I has a weight Wi where I
varies from 1 to N.
The problem is we have to fill the bag with the help of N objects and the resulting profit has to be
maximum.
Formally the problem can be stated as
Maximize xipi subject to XiWi<=M
Where Xi is the fraction of object and it lies between 0 to 1.
There are so many ways to solve this problem, which will give many feasible solution for which we
have to find the optimal solution. But in this algorithm, it will generate only one solution which is
going to be feasible as well as optimal. First, we find the profit & weight rates of each and every
object and sort it according to the descending order of the ratios. Select an object with highest p/w
8
www.android.universityupdates.in | www.universityupdates.in |
ratio and check whether its height is lesser than the capacity of the bag. If so place 1 unit of the first
object and decrement .the capacity of the bag by the weight of the object you have placed.
Repeat the above steps until the capacity of the bag becomes less than the weight of the object you
have selected .in this case place a fraction of the object and come out of the loop. Whenever you
selected.
ALGORITHM:
8
www.android.universityupdates.in | www.universityupdates.in |
Let G(V,E) be an undirected connected graph with vertices ‘v’ and edge ‘E’.
A sub-graph t=(V,E’) of the G is a Spanning tree of G iff ‘t’ is a tree.3
The problem is to generate a graph G’= (V,E) where ‘E’ is the subset of E,G’ is a Minimum
spanning tree.
Each and every edge will contain the given non-negative length .connect all the nodes with edge
present in set E’ and weight has to be minimum.
NOTE:
We have to visit all the nodes.
The subset tree (i.e) any connected graph with ‘N’ vertices must have at least N-1 edges and also it
does not form a cycle.
Definition:
A spanning tree of a graph is an undirected tree consisting of only those edge that are necessary to
connect all the vertices in the original graph. A Spanning tree has a property that for any pair of
vertices there exist only one path between them and the insertion of an edge to a spanning tree form
a unique cycle.
Application of the spanning tree:
1. Analysis of electrical circuit.
2. Shortest route problems.
9
www.android.universityupdates.in | www.universityupdates.in |
KRUSKAL’S ALGORITHM:
In kruskal's algorithm the selection function chooses edges in increasing order of length without
worrying too much about their connection to previously chosen edges, except that never to form a
cycle. The result is a forest of trees that grows until all the trees in a forest (all the components)
merge in a single tree.
In this algorithm, a minimum cost-spanning tree ‘T’ is built edge by edge.
Edge are considered for inclusion in ‘T’ in increasing order of their cost.
Algorithm:
Algorithm kruskal(E,cost,n,t)
//Eset of edges in G has ‘n’ vertices.
//cost[u,v]cost of edge (u,v).tset of edge in minimum cost spanning tree
// the first cost is returned.
{
for i=1 to n do parent[I]=-1;
I=0;mincost=0.0;
While((I<n-1)and (heap not empty)) do
{
j=find(n);
k=find(v);
if(j not equal k) than
{
9
www.android.universityupdates.in | www.universityupdates.in |
i=i+1
t[i,1]=u;
t[i,2]=v;
mincost=mincost+cost[u,v];
union(j,k);
}
}
if(i notequal n-1) then write(“No spanning
tree”) else return minimum cost;
}
Analysis
14. The time complexity of minimum cost spanning tree algorithm in worst case is O(|E|log|E|),
where E is the edge set of G.
Step 1. In the graph, the Edge(g, h) is shortest. Either vertex g or vertex h could be
representative. Lets choose vertex g arbitrarily.
Step 2. The edge (c, i) creates the second tree. Choose vertex c as representative for second
tree.
Step 3. Edge (g, g) is the next shortest edge. Add this edge and choose vertex g as
9
www.android.universityupdates.in | www.universityupdates.in |
representative.
Step 5. Add edge (c, f) and merge two trees. Vertex c is chosen as the representative.
Step 6. Edge (g, i) is the next next cheapest, but if we add this edge a cycle would be created.
Vertex c is the representative of both.
9
www.android.universityupdates.in | www.universityupdates.in |
Step 10. Again, if we add edge (b, c), it would create a cycle. Add edge (d, e) instead to
complete the spanning tree. In this spanning tree all trees joined and vertex c is a sole
representative.
9
www.android.universityupdates.in | www.universityupdates.in |
PRIM'S ALGORITHM
Start from an arbitrary vertex (root). At each stage, add a new branch (edge) to the tree already
constructed; the algorithm halts when all the vertices in the graph have been reached.
Algorithm prims(e,cost,n,t)
{
Let (k,l) be an edge of minimum cost in
E; Mincost :=cost[k,l];
T[1,1]:=k; t[1,2]:=l;
For I:=1 to n do
If (cost[i,l]<cost[i,k]) then near[i]:=l;
Else near[i]:=k;
Near[k]:=near[l]:=0;
For i:=2 to n-1 do
{
Let j be an index such that near[j]≠0
and Cost[j,near[j]] is minimum;
T[i,1]:=j; t[i,2]:=near[j];
Mincost:=mincost+ Cost[j,near[j]];
Near[j]:=0;
For k:=0 to n do
If near((near[k]≠0) and (Cost[k,near[k]]>cost[k,j])) then
Near[k]:=j;
}
Return mincost;
}
15. The prims algorithm will start with a tree that includes only a minimum cost edge of G.
16. Then, edges are added to the tree one by one. the next edge (i,j) to be added in such that I is a
9
www.android.universityupdates.in | www.universityupdates.in |
vertex included
in the tree, j is a vertex not yet included, and cost of (i,j), cost[i,j] is minimum among all the
edges.
17. The working of prims will be explained by following diagram
Step 1: Step 2:
Step 3: Step 4:
Step 5: Step 6:
Graphs can be used to represent the highway structure of a state or country with vertices
representing cities and edges representing sections of highway. The edges can then be assigned
weights which may be either the distance between the two cities connected by the edge or the
average time to drive along that section of highway. A motorist wishing to drive from city A to B
9
www.android.universityupdates.in | www.universityupdates.in |
The problems defined by these questions are special case of the path problem we study in this
section. The length of a path is now defined to be the sum of the weights of the edges on that path.
The starting vertex of the path is referred to as the source and the last vertex the destination. The
graphs are digraphs representing streets. Consider a digraph G=(V,E), with the distance to be
traveled as weights on the edges. The problem is to determine the shortest path from v0 to all the
remaining vertices of G. It is assumed that all the weights associated with the edges are positive.
The shortest path between v0 and some other node v is an ordering among a subset of the edges.
Hence this problem fits the ordering paradigm.
Example:
Consider the digraph of fig 7-1. Let the numbers on the edges be the costs of travelling along that
route. If a person is interested travel from v1 to v2, then he encounters many paths. Some of them
are
o v1 v2 = 50 units
o v1 v3 v4 v2 = 10+15+20=45 units
o v1 v5 v4 v2 = 45+30+20= 95 units
o v1 v3 v4 v5 v4 v2 = 10+15+35+30+20=110 units
The cheapest path among these is the path along v1 v3 v4 v2. The cost of the path is
10+15+20 = 45 units. Even though there are three edges on this path, it is cheaper than travelling
along the path connecting v1 and v2 directly i.e., the path v1 v2 that costs 50 units. One can also
9
www.android.universityupdates.in | www.universityupdates.in |
V1 V2 V3 V4 V5 V6
V1 - 50 10 Inf 45 Inf
Step 2: consider v1 to be the source and choose the minimum entry in the row v1. In the above
table the minimum in row v1 is 10.
Step 3: find out the column in which the minimum is present, for the above example it is column
v3. Hence, this is the node that has to be next visited.
9
www.android.universityupdates.in | www.universityupdates.in |
Step 4: compute a matrix by eliminating v1 and v3 columns. Initially retain only row v1. The
second row is computed by adding 10 to all values of row v3.
The resulting matrix is
V2 V4 V5 V6
V1 Vw 50 Inf 45 Inf
Minimum 50 25 45 inf
Step 5: find the minimum in each column. Now select the minimum from the resulting row. In the
above example the minimum is 25. Repeat step 3 followed by step 4 till all vertices are covered or
single column is left.
V2 V5 V6
V1 Vw 50 45 Inf
Minimum 45 45 inf
V5 V6
V1 Vw 45 Inf
V1 V3 V4 V2 Vw 45+10 45+inf
Minimum 45 Inf
V6
V1 Vw Inf
V1 V3 V4 V2 V5 Vw 45+inf
Minimum inf
Finally the cheapest path from v1 to all other vertices is given by V1 V3 V4 V2 V5.
9
www.android.universityupdates.in | www.universityupdates.in |
PART-A (2 Marks)
1
www.android.universityupdates.in | www.universityupdates.in |
1. Explain the solution to the problem of job sequencing with deadlines by Greedy technique.
2. Explain how travelling salesman problem is solved by using dynamic programming with an
example.
3. Describe Kanpsack problem. Find an optimal solution for the Greedy Knapsack instance for
n=3, m=6, profits are (P1, P2, P3)=(1, 2, 5) weights are (w1, w2, w3)=(2,3,4).
4. (a)Solve for Knapsack problem using Greedy method for the instance n = 7, profits (p1...p7) =
(10,5,15,7,6,18,3) and weights (W1...W7) = ( 2,3,5,7,1,4,1) and capacity of sack is m = 15.
(b) Explain the problem Job sequencing with Deadline and solve for n = 5, (P1...P5) = (20, 15,
10, 5, 1) and Deadlines (D1...D5) = (2, 2, 1, 3, 3).
5. Write the merge sort algorithm. Find out the best, worst and average cases of this algorithm. Sort
the following numbers using merge sort: 10, 12, 1, 5, 18, 28, 38, 39, 2, 4, 7
6. Explain quick sort algorithm with an example. Derive its time complexity.
7. Mention the advantage of Stressan’s matrix multiplication. Explain how the Stressan’s matrix
multiplication works.
8. Write an algorithm for finding the Maximum and Minimum values using Divide and Conquer
method and solve for: 22, 13, -5, -8, 15, 60, 17, 31, 47.
9. Explain Binary search algorithm and derive its time complexity.
1
www.android.universityupdates.in | www.universityupdates.in |
UNIT –V
Dynamic Programming: General method, applications- 0/1 knapsack problem, All pairs
shortest path problem, Travelling salesperson problem, Reliability design.
Backtracking: General method, applications-n-queen problem, sum of subsets problem, graph
coloring, Hamiltonian cycles.
Introduction to NP-Hard and NP-Complete problems: Basic Concepts
APPLICATIONS:
1. This problem is similar to ordinary knapsack problem but we may not take a fraction of an
object.
2. We are given ‘ N ‘ object with weight Wi and profits Pi where I varies from l to N and also a
knapsack with capacity ‘ M ‘.
3. The problem is, we have to fill the bag with the help of ‘ N ‘ objects and the resulting profit
has to be maximum.
n
4. Formally, the problem can be started as, maximize Xi Pi
i=l
1
www.android.universityupdates.in | www.universityupdates.in |
n
subject to Xi Wi L
M
i=l
5. Where Xi are constraints on the solution X i {0,1}. (u) Xi is required to be 0 or 1. if the object
is selected then the unit in 1. if the object is rejected than the unit is 0. That is why it is called as
0/1, knapsack problem.
6. To solve the problem by dynamic programming we up a table T[1…N, 0…M] (ic) the size is
N. where ‘N’ is the no. of objects and column starts with ‘O’ to capacity (ic) ‘M’.
7. In the table T[i,j] will be the maximum valve of the objects i varies from 1 to n and j varies
from O to M.
2. If i=l and j w (i) then T (i,j) = p(i), the cell is filled with the profit p[i], since only one object
can be selected to the maximum.
3. If i>l and j < w(i) then T(i,l) = T (i-l,j) the cell is filled the profit of previous object since it is
not possible with the current object.
4. If i>l and j w(i) then T (i,j) = {f(i) +T(i-l,j-w(i)),. since only ‘l’ unit can be selected to the
maximum. If is the current profit + profit of the previous object to fill the remaining capacity of
the bag.
Start with the last position of i and j, T[i,j], if T[i,j] = T[i-l,j] then no object of ‘i’ is required so
move up to T[i-l,j].
After moved, we have to check if, T[i,j]=T[i-l,j-w(i)]+ p[I], if it is equal then one unit of object
‘i’ is selected and move up to the position T[i-l,j-w(i)]
1
www.android.universityupdates.in | www.universityupdates.in |
Repeat the same process until we reach T[i,o], then there will be nothing to fill the bag stop the
process.
Time is 0(nw) is necessary to construct the table T.
Consider a
Example, M = 6,
N=3
W1 = 2, W2 = 3, W3 = 4
P1 = 1, P2 =2, P3 = 5
i 1 to N
j 0 to 6
o<2 T1,o =0
i=l, j=2
2 o,= T1,2 = l.
i=l, j=3
3>2,= T1,3 = l.
i=l, j=4
4>2,= T1,4 = l.
i=l, j=5
5>2,= T1,5 = l.
i=l, j=6
6>2,= T1,6 = l.
i=2, j=1
l<3= T(2,1) = T(i-
l) T 2,1 =0
1
www.android.universityupdates.in | www.universityupdates.in |
Let G=<N,A> be a directed graph ’N’ is a set of nodes and ‘A’ is the set of edges.
1.Each edge has an associated non-negative length.
2. We want to calculate the length of the shortest path between each pair of nodes.
3. Suppose the nodes of G are numbered from 1 to n, so N={1,2,...N},and suppose G matrix L
gives the length of each edge, with L(i,j)=0 for i=1,2...n,L(i,j)>=for all i & j, and L(i,j)=infinity, if
the edge (i,j) does not exist.
4. The principle of optimality applies: if k is the node on the shortest path from i to j then the part
of the path from i to k and the part from k to j must also be optimal, that is shorter.
5. First, create a cost adjacency matrix for the given graph.
6. Copy the above matrix-to-matrix D, which will give the direct distance between nodes.
7. We have to perform N iteration after iteration k.the matrix D will give you the distance between
nodes with only (1,2...,k)as intermediate nodes.
8. At the iteration k, we have to check for each pair of nodes (i,j) whether or not there exists a
path from i to j passing through node k.
D0 =L= 0 5
50 0 15 5
30 0 15
15 5 0
1 75 11 12 - -
2 72 21 - - 24
3 3 - 32 - -
4 41 41 – 43 -
vertex 1:
7 5 11 12 - -
7 12 2 21 212 - 24
3 - 32 - -
4 9 1 41 412 43 –
vertex 2:
7 5 7 11 12 - 124
7 12 2 21 212 - 24
10 3 5 321 32 - 324
4 9 1 11 41 412 43 4124
1
www.android.universityupdates.in | www.universityupdates.in |
vertex 3:
7 5 7 11 12 - 124
7 12 2 21 212 - 24
10 3 5 321 32 - 324
44 1 6 41 432 4 4324
vertex 4:
7 5 8 7 11 12 1243 124
6 6 3 2 241 2432 243 24
936 5 3241 32 3243 324
4 4 1 6 41 432 43 4324
1. At 0th iteration it nil give you the direct distances between any 2 nodes
D0= 0 5
50 0 15 5
30 0 15
15 5 0
2. At 1st iteration we have to check the each pair(i,j) whether there is a path through node 1.if so
we have to check whether it is minimum than the previous value and if I is so than the distance
through 1 is the value of d1(i,j).at the same time we have to solve the intermediate node in the
matrix position p(i,j).
0 5
50 0 15 5 p[3,2]= 1
D1= 30 35 0 15 p[4,2]= 1
15 20 5 0
15
30
5
5 50 5 15
15
Fig: floyd’s algorithm and work
3. likewise we have to find the value for N iteration (ie) for N nodes.
1
www.android.universityupdates.in | www.universityupdates.in |
0 5 20 10 P[1,3] = 2
D2= 50 0 15 5 P[1,4] = 2
30 35 0 15
15 20 5 0
0 5 20 10
D3= 45 0 15 5 P[2,1]=3
30 35 0 15
15 20 5 0
0 5 15 10
20 0 10 5 P[1,3]=4
D4= 30 35 0 15 P[2,3]=4
15 20 5 0
4. D4 will give the shortest distance between any pair of nodes.
5. If you want the exact path then we have to refer the matrix p.The matrix will be,
0042
3040 0 direct path
P= 0100
0100
Since,p[1,3]=4,the shortest path from 1 to3 passes through 4.
Looking now at p[1,4]&p[4,3] we discover that between 1 & 4, we have to go to node 2 but
that from 4 to 3 we proceed directly.
Finally we see the trips from 1 to 2, & from 2 to 4, are also direct.
The shortest path from 1 to 3 is 1,2,4,3.
ALGORITHM :
Function Floyd (L[1..r,1..r]):array[1..n,1..n]
array D[1..n,1..n]
D=L
For k = 1 to n do
For i = 1 to n do
For j = 1 to n do
D [ i , j ] = min (D[ i, j ], D[ i, k ] + D[ k, j ]
1
www.android.universityupdates.in | www.universityupdates.in |
Return D
ANALYSIS:
This algorithm takes a time of (n3)
MULTISTAGE GRAPH
1. A multistage graph G = (V,E) is a directed graph in which the vertices are portioned into K >
= 2 disjoint sets Vi, 1 <= i<= k.
2. In addition, if < u,v > is an edge in E, then u < = Vi and V Vi+1 for some i, 1<= i <
k. If there will be only one vertex, then the sets Vi and Vk are such that [Vi]=[Vk] = 1.
3. Let ‘s’ and ‘t’ be the source and destination respectively.
4. The cost of a path from source (s) to destination (t) is the sum of the costs of the edger on the
path.
5. The MULTISTAGE GRAPH problem is to find a minimum cost path from ‘s’ to ‘t’.
6. Each set Vi defines a stage in the graph. Every path from ‘s’ to ‘t’ starts in stage-1, goes to
stage-2 then to stage-3, then to stage-4, and so on, and terminates in stage-k.
This MULISTAGE GRAPH problem can be solved in 2 ways.
Forward Method.
Backward Method.
FORWARD METHOD
1
www.android.universityupdates.in | www.universityupdates.in |
PROCEDURE:
V1 V2 V3 V4 V5
4 6
2 2
5 4
9 1
4
7 3 2
7 t
s
3
11 5 5
2
11 6
1. Maintain a cost matrix cost (n) which stores the distance from any vertex to the destination.
2. If a vertex is having more than one path, then we have to choose the minimum distance path
and the intermediate vertex, which gives the minimum distance path, will be stored in the
distance array ‘D’.
3. In this way we will find out the minimum cost path from each and every vertex.
4. Finally cost(1) will give the shortest distance from source to destination.
5. For finding the path, start from vertex-1 then the distance array D(1) will give the minimum
cost neighbour vertex which in turn give the next nearest vertex and proceed in this way till we
reach the Destination.
6. For a ‘k’ stage graph, there will be ‘k’ vertex in the path.
7. In the above graph V1…V5 represent the stages. This 5 stage graph can be solved by using
forward approach as follows,
STEPS: - DESTINATION, D
Cost (12)=0 D (12)=0
Cost (11)=5 D (11)=12
1
www.android.universityupdates.in | www.universityupdates.in |
Cost
Cost(8) = min {C (8,10) (i,j) =(10),
+ Cost min C{C(8,11)
(j,l) ++Cost
Cost (11) }
(i+1,l) } l Vi + 1
= min (5 + 2, 6 + 5) (j,l) E
= min (7,11)
=7
cost(8) =7 =>D(8)=10
cost(7) = min(c (7,9)+ cost(9),c (7,10)+
cost(10)) (4+4,3+2)
= min(8,5)
=5
cost(7) = 5 =>D(7) = 10
cost(6) = min (c (6,9) + cost(9),c (6,10) +cost(10))
= min(6+4 , 5 +2)
= min(10,7)
=7
cost(6) = 7 =>D(6) = 10
cost(5) = min (c (5,7) + cost(7),c (5,8) +cost(8))
= min(11+5 , 8 +7)
= min(16,15)
= 15
cost(5) = 15 =>D(5) = 18
cost(4) = min (c (4,8) + cost(8))
= min(11+7)
= 18
cost(4) = 18 =>D(4) = 8
cost(3) = min (c (3,6) + cost(6),c (3,7) +cost(7))
= min(2+7 , 7 +5)
1
www.android.universityupdates.in | www.universityupdates.in |
= min(9,12)
=9
cost(3) = 9 =>D(3) = 6
cost(2) = min (c (2,6) + cost(6),c (2,7) +cost(7) ,c (2,8) +cost(8))
= min(4+7 , 2+5 , 1+7 )
= min(11,7,8)
=7
cost(2) = 7 =>D(2) = 7
cost(1) = min (c (1,2)+cost(2) ,c (1,3)+cost(3) ,c (1,4)+cost(4) ,c(1,5)+cost(5))
= min(9+7 , 7 +9 , 3+18 , 2+15)
= min(16,16,21,17)
= 16
cost(1) = 16 =>D(1) = 2
The path through which you have to find the shortest distance.
(i.e.)
9 2 3 2
1
www.android.universityupdates.in | www.universityupdates.in |
cost[n]=0.0;
for j=n-1 to 1 step-1 do
{
//compute cost[j],
// let ‘r’ be the vertex such that <j,r> is an edge of ‘G’ &
// c[j,r]+cost[r] is minimum.
P[1]=1;
P[k]=n;
For j=2 to k-1
do P[j]=d[p[j-
1]];
}
ANALYSIS:
The time complexity of this forward method is O( V + E )
BACKWARD METHOD
if there one ‘K’ stages in a graph using back ward approach. we will find out the cost of each &
every vertex starting from 1st
stage to the kth stage.
We will find out the minimum cost path from destination to source (ie)[from stage k to stage 1]
PROCEDURE:
It is similar to forward approach, but differs only in two or three ways.
Maintain a cost matrix to store the cost of every vertices and a distance matrix to store the
minimum distance vertex.
Find out the cost of each and every vertex starting from vertex 1 up to vertex k.
To find out the path star from vertex ‘k’, then the distance array D (k) will give the minimum
1
www.android.universityupdates.in | www.universityupdates.in |
cost neighbor vertex which in turn gives the next nearest neighbor vertex and proceed till we
reach the destination.
STEP:
PATH:
Start from vertex-12
D(12) = 10
D(10) = 6
1
www.android.universityupdates.in | www.universityupdates.in |
D(6) = 3
D(3) = 1
So the minimum cost path
is, 1 7 3 2 6 5 10 2
12
bcost[j] = bcost[r] +
c[r,j]; d[j] =r;
}
// find a minimum cost path.
P[1]=1;
P[k]=n;
For j= k-1 to 2
do
P[j]=d[p[j+1]];
}
1
www.android.universityupdates.in | www.universityupdates.in |
Let G(V,E) be a directed graph with edge cost cij is defined such that cij >0 for all i and j and cij
= ,if <i,j> E.
Let V =n and assume n>1.
The traveling salesman problem is to find a tour of minimum cost.
A tour of G is a directed cycle that include every vertex in V.
The cost of the tour is the sum of cost of the edges on the tour.
The tour is the shortest path that starts and ends at the same vertex (ie) 1.
APPLICATION :
Suppose we have to route a postal van to pick up mail from the mail boxes located at ‘n’
different sites.
An n+1 vertex graph can be used to represent the situation.
One vertex represent the post office from which the postal van starts and return.
Edge <i,j> is assigned a cost equal to the distance from site ‘i’ to site ‘j’.
the route taken by the postal van is a tour and we are finding a tour of minimum length.
every tour consists of an edge <1,k> for some k V-{} and a path from vertex k to vertex 1.
the path from vertex k to vertex 1 goes through each vertex in V-{1,k} exactly once.
the function which is used to find the path
is g(1,V-{1}) = min{ cij + g(j,s-{j})}
g(i,s) be the length of a shortest path starting at vertex i,
going through all vertices in S,and terminating at vertex 1.
the function g(1,v-{1}) is the length of an optimal tour.
1.
Find g(i,) =ci1, 1<=i<n, hence we can use equation(2) to obtain g(i,s) for all s to size 1.
2.
That we have to start with s=1,(ie) there will be only one vertex in set ‘s’.
3.
Then s=2, and we have to proceed until |s| <n-1.
4.
for example consider the graph.
1
www.android.universityupdates.in | www.universityupdates.in |
10
15
10
15
20 8 9 13
86
12
7
Cost matrix
0 10 15 20
5 0 9 10
6 13 0 12
8 8 9 0
starting position
g(i,s) =min{cij +g(j,s-{j})
STEP 1:
g(1,{2,3,4})=min{c12+g(2{3,4}),c13+g(3,{2,4}),c14+g(4,{2,3})}
min{10+25,15+25,20+23}
min{35,35,43}
=35
STEP 2:
g(2,{3,4}) = min{c23+g(3{4}),c24+g(4,{3})}
min{9+20,10+15}
min{29,25}
=25
g(3,{2,4}) =min{c32+g(2{4}),c34+g(4,{2})}
min{13+18,12+13}
min{31,25}
=25
g(4,{2,3}) = min{c42+g(2{3}),c43+g(3,{2})}
min{8+15,9+18}
min{23,27}
=23
1
www.android.universityupdates.in | www.universityupdates.in |
STEP 3:
1. g(3,{4}) = min{c34 +g{4,}}
12+8 =20
2. g(4,{3}) = min{c43 +g{3,}}
9+6 =15
3. g(2,{4}) = min{c24 +g{4,}}
10+8 =18
i =1 to n.
g(1,) = c11 => 0
g(2,) = c21 => 5
g(3,) = c31 => 6
g(4,) = c41 => 8
s =1
i =2 to 4
g(2,{3}) = c23 + g(3,)
= 9+6 =15
1
www.android.universityupdates.in | www.universityupdates.in |
s =2
i 1, 1 s and i s.
g(2,{3,4}) = min{c23+g(3{4}),c24+g(4,{3})}
min{9+20,10+15}
min{29,25}
=25
g(3,{2,4}) =min{c32+g(2{4}),c34+g(4,{2})}
min{13+18,12+13}
min{31,25}
=25
g(4,{2,3}) = min{c42+g(2{3}),c43+g(3,{2})}
min{8+15,9+18}
min{23,27}
=23
s = 3
g(1,{2,3,4})=min{c12+g(2{3,4}),c13+g(3,{2,4}),c14+g(4,{2,3})}
min{10+25,15+25,20+23}
min{35,35,43}
=35
1
www.android.universityupdates.in | www.universityupdates.in |
optimal cost is 35
is,
Reliability Design
Input:
A system composed of several devices in serial
Each device (D) has a fixed reliability rating (r)
Multiple copies of the same device can be used in parallel to increase reliability
Output:
A system with highest reliability rating subject to a cost constraint
D Di Di Dn
D Di Di Di
1 2
mi copies of
D devices Di at stage i, ri : say,90%
with a reliability rating of i 1 (1 r ) m i Connected in parallel
i At least one should work
max i (mi ) Connected in series
1i
c m
All of them have to work
subject to i i C and mi 1,1 i
n
1in
1
www.android.universityupdates.in | www.universityupdates.in |
Comparison
A knapsack of Total expenditure of C
capacity C Stages of cost ci and
Objects of size ci and reliability ri
profit pi Construct a system
Fill up the knapsack with 1 or more copies
with 0 or 1 copy of i of i
Maximize profit Maximize reliability
n
C c j
u 1 j 1
i
c i
(1 , x x x ) : (
n
xx ) m u s t b e o p t i m a l f o r C c 1
( 2 , y y x yy
y ) m u s t b e o p t i m a l f o r C 2 c 1
y ) :
(
( u 1 , y y y ) : ( y y y ) m u s t b e o p t i m a l f o r C u 1 c 1
(0.9,65) (0.99,95)
1 (1 0.8) 0.8
1 (1 0.8 )2 0.96
1 (1 0.8 )3 0.992
1 (1 0.5) 0.5
1 (1 0.5)2 0.75
1 (1 0.5 )3 0.875
1
www.android.universityupdates.in | www.universityupdates.in |
BACKTRACKING
1
www.android.universityupdates.in | www.universityupdates.in |
{
if (there remains all untried
X[k] T (X[1],[2],…..X[k-1]) and Bk (X[1],…..X[k])) is true ) then
{
if(X[1],……X[k] )is the path to the answer
node) Then write(X[1:k]);
k=k+1; //consider the next step.
}
else k=k-1; //consider backtracking to the previous set.
}
}
All solutions are generated in X[1:n] and printed as soon as they are determined.
T(X[1]…..X[k-1]) is all possible values of X[k] gives that X[1],……..X[k-1] have already
been chosen.
Applications:
N-Queens problem.
Sum of subsets.
Graph coloring.
Hamiltonian cycle.
N-Queens problem:
This 8 queens problem is to place n-queens in an ‘N*N’ matrix in such a way that no two queens
attack each otherwise no two queens should be in the same row, column, diagonal.
Solution:
The solution vector X (X1…Xn) represents a solution in which Xi is the column of the th row
where I th queen is placed.
First, we have to check no two queens are in same row.
Second, we have to check no two queens are in same column.
The function, which is used to check these two conditions, is [I, X (j)], which gives position
of the I th queen, where I represents the row and X (j) represents the column position.
Third, we have to check no two queens are in it diagonal.
1
www.android.universityupdates.in | www.universityupdates.in |
Consider two dimensional array A[1:n,1:n] in which we observe that every element on the
same diagonal that runs from upper left to lower right has the same value.
Also, every element on the same diagonal that runs from lower right to upper left has the
same value.
Suppose two queens are in same position (i,j) and (k,l) then two queens lie on the
same diagonal , if and only if |j-l|=|I-k|.
Initialize x array to zero and start by placing the first queen in k=1 in the first row.
To find the column position start from value 1 to n, where ‘n’ is the no. Of columns or no.
Of queens.
If k=1 then x (k)=1.so (k,x(k)) will give the position of the k th queen. Here we have to
check whether there is any queen in the same column or diagonal.
For this considers the previous position, which had already, been found out. Check
whether X (I)=X(k) for column |X(i)-X(k)|=(I-k) for the same diagonal.
If any one of the conditions is true then return false indicating that k th queen can’t be placed in
position X (k).
For not possible condition increment X (k) value by one and precede d until the position is
found.
If the position X (k) n and k=n then the solution is generated completely.
If k<n, then increment the ‘k’ value and find position of the next queen.
If the position X (k)>n then k th queen cannot be placed as the size of the matrix is ‘N*N’.
So decrement the ‘k’ value by one i.e. we have to back track and after the position of
the previous queen.
Algorithm:
Algorithm place (k,I)
//return true if a queen can be placed in k th row and I th column. otherwise it returns //
//false .X[] is a global array whose first k-1 values have been set. Abs® returns the //absolute value
of r.
{
For j=1 to k-1 do
If ((X [j]=I) //two in same column.
1
www.android.universityupdates.in | www.universityupdates.in |
Example: 4 queens.
Two possible solutions are
Solutin-1
(2 4 1 3) Solution 2
(3 1 4 2)
1
www.android.universityupdates.in | www.universityupdates.in |
SUM OF SUBSETS:
1) We are given ‘n’ positive numbers called weights and we have to find all combinations of
these numbers whose sum is M. this is called sum of subsets problem.
2) If we consider backtracking procedure using fixed tuple strategy , the elements X(i) of the
solution vector is either 1 or 0 depending on if the weight W(i) is included or not.
3) If the state space tree of the solution, for a node at level I, the left child corresponds to X(i)=1
and
4) right to X(i)=0.
Example:
a. Given n=6,M=30 and W(1…6)=(5,10,12,13,15,18).We have to generate all possible
combinations of subsets whose sum is equal to the given value M=30.
b. In state space tree of the solution the rectangular node lists the values of s, k, r, where s
is the sum of subsets,’k’ is the iteration and ‘r’ is the sum of elements after ‘k’ in the
original set.
c. The state space tree for the given problem is,
S, n, r
X(1)=1 x(1)=0
0,1,73
5,2,68 0,2,68
X(4)=1 x(4)=0
X(4)=0
B
15,5,33 5,5,33 10,5,33
X(5)=1 x(5)=1
A 20,6,18
1
www.android.universityupdates.in | www.universityupdates.in |
In the state space tree, edges from level ‘i’ nodes to ‘i+1’ nodes are labeled with the values of Xi,
which is either 0 or 1.
The left sub tree of the root defines all subsets containing Wi.
The right subtree of the root defines all subsets, which does not include Wi.
If S+X(k)=M then we print the subset b’coz the sum is the required output.
If the above condition is not satisfied then we have to check S+X(k)+W(k+1)<=M. If so, we
have to generate the left sub tree. It means W(t) can be included so the sum will be
incremented and we have to check for the next k.
After generating the left sub tree we have to generate the right sub tree, for this we have to
check S+W(k+1)<=M.B’coz W(k) is omitted and W(k+1) has to be selected.
Repeat the process and find all the possible combinations of the subset.
Algorithm:
Algorithm sumofsubset(s,k,r)
{
//generate the left child. note s+w(k)<=M since Bk-1 is true.
X{k]=1;
If (S+W[k]=m) then write(X[1:k]); // there is no recursive call here as W[j]>0,1<=j<=n.
Else if (S+W[k]+W[k+1]<=m) then sum of sub (S+W[k], k+1,r- W[k]);
//generate right child and evaluate Bk.
1
www.android.universityupdates.in | www.universityupdates.in |
GRAPH COLORING:
Let ‘G’ be a graph and ‘m’ be a given positive integer. If the nodes of ‘G’ can be colored in
such a way that no two adjacent nodes have the same color. Yet only ‘M’ colors are used. So
it’s called M-color ability decision problem.
The graph G can be colored using the smallest integer ‘m’. This integer is referred to as
chromatic number of the graph.
A graph is said to be planar iff it can be drawn on plane in such a way that no two edges cross
each other.
Suppose we are given a map then, we have to convert it into planar. Consider each and every
region as a node. If two regions are adjacent then the corresponding nodes are joined by an
edge.
4 5
2
1
1 is adjacent to 2, 3, 4.
2 is adjacent to 1, 3, 4, 5
3 is adjacent to 1, 2, 4
4 is adjacent to 1, 2, 3, 55 is adjacent to 2, 4
1
www.android.universityupdates.in | www.universityupdates.in |
1. First create the adjacency matrix graph(1:m,1:n) for a graph, if there is an edge between i,j
then C(i,j) = 1 otherwise C(i,j) =0.
2. The Colors will be represented by the integers 1,2,…..m and the solutions will be stored in the
array X(1),X(2),.............,X(n) ,X(index) is the color, index is the node.
3. He formula is used to set the color
is, X(k) = (X(k)+1) % (m+1)
4. First one chromatic number is assigned ,after assigning a number for ‘k’ node, we have to
check whether the adjacent nodes has got the same values if so then we have to assign the next
value.
5. Repeat the procedure until all possible combinations of colors are found.
6. The function which is used to check the adjacent nodes and same color
is, If(( Graph (k,j) == 1) and X(k) = X(j))
Example:
1 2
4 3
N= 4
M= 3
Adjacency Matrix:
0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0
1
www.android.universityupdates.in | www.universityupdates.in |
Algorithm:
Algorithm mColoring(k)
// the graph is represented by its Boolean adjacency matrix G[1:n,1:n] .All assignments //of 1,2,
……….,m to the vertices of the graph such that adjacent vertices are assigned //distinct integers
are printed. ’k’ is the index of the next vertex to color.
{
repeat
{
// generate all legal assignment for X[k].
Nextvalue(k); // Assign to X[k] a legal
color.
If (X[k]=0) then return; // No new color possible.
If (k=n) then // Almost ‘m’ colors have been used to color the ‘n’
vertices Write(x[1:n]);
Else mcoloring(k+1);
}until(false);
}
Algorithm Nextvalue(k)
// X[1],……X[k-1] have been assigned integer values in the range[1,m] such that //adjacent values
have distinct integers. A value for X[k] is determined in the //range[0,m].X[k] is assigned the next
1
www.android.universityupdates.in | www.universityupdates.in |
highest numbers color while maintaining //distinctness form the adjacent vertices of vertex K. If
no such color exists, then X[k] is 0.
{
repeat
{
X[k] = (X[k]+1)mod(m+1); // next highest color.
If(X[k]=0) then return; //All colors have been
used.
For j=1 to n do
{
// Check if this color is distinct from adjacent color.
If((G[k,j] 0)and(X[k] = X[j]))
// If (k,j) is an edge and if adjacent vertices have the same color.
Then break;
}
if(j=n+1) then return; //new color found.
} until(false); //otherwise try to find another color.
}
HAMILTONIAN CYCLES:
Let G=(V,E) be a connected graph with ‘n’ vertices. A HAMILTONIAN CYCLE is a round
trip path along ‘n’ edges of G which every vertex once and returns to its starting position.
If the Hamiltonian cycle begins at some vertex V1 belongs to G and the vertex are visited in
the order of V1,V2…….Vn+1,then the edges are in E,1<=I<=n and the Vi are distinct except
V1 and Vn+1 which are equal.
Consider an example graph G1.
1
www.android.universityupdates.in | www.universityupdates.in |
1 2 3 4
8 7 6 5
->1,3,4,5,6,7,8,2,1 and
->1,2,8,7,6,5,4,3,1.
The backtracking algorithm helps to find Hamiltonian cycle for any type of graph.
Procedure:
Define a solution vector X(Xi……..Xn) where Xi represents the I th visited vertex of the
proposed cycle.
Create a cost adjacency matrix for the given graph.
The solution array initialized to all zeros except X(1)=1,b’coz the cycle should start at vertex
‘1’.
Now we have to find the second vertex to be visited in the cycle.
The vertex from 1 to n are included in the cycle one by one by checking 2
conditions, 1.There should be a path from previous visited vertex to current
vertex.
2.The current vertex must be distinct and should not have been visited earlier.
6. When these two conditions are satisfied the current vertex is included in the cycle, else the
next vertex is tried.
7. When the nth vertex is visited we have to check, is there any path from nth vertex to first
vertex. if no path, the go back one step and after the previous visited node.
8. Repeat the above steps to generate possible Hamiltonian cycle.
1
www.android.universityupdates.in | www.universityupdates.in |
Basic concepts:
Tractability: Some problems are tractable: that is the problems are solvable in reasonable
amount of time called polynomial time. Some problems are intractable: that is as problem grow
large, we are unable to solve them in reasonable amount of time called polynomial time.
1
www.android.universityupdates.in | www.universityupdates.in |
CLASS P PROBLEMS:
Class P problems are the set of decision problems solvable by deterministic algorithms in
polynomial-time.
A deterministic algorithm is (essentially) one that always computes the correct answer
Examples: Fractional Knapsack, MST, Single-source shortest path
CLASS NP PROBLEMS:
NP problems are set of decision problems solvable by non-deterministic algorithms in
polynomial-time.
A nondeterministic algorithm is one that can “guess” the right answer or solution
Examples: Hamiltonian Cycle (Traveling Sales Person), Conjunctive Normal Form (CNF)
NP-Complete Problems:
A problem ‘x’ is a NP class problem and also NP-Complete if and only if every other problem
1
www.android.universityupdates.in | www.universityupdates.in |
NP-Hard Problems:
A problem ‘x’ is a NP class problem and also NP-Hard if and only if every other problem in NP
can be reducible (solvable) using non-deterministic algorithm in exponential time.
The class of problems to which every NP problem reduces.
The NP-Hard problems are decision problems and sometimes may be optimization problems.
Example : Integer Linear Programming.
Nondeterministic Algorithms:
Deterministic Algorithms:
• Let A be an algorithm to solve problem P. A is called deterministic if it has only one choice in
each step throughout its execution. Even if we run algorithm A again and again, there is no
change in output.
• Deterministic algorithms are identified with uniquely defined results in terms of output for a
certain input.
Nondeterministic Algorithms:
• Let A be a nondeterministic algorithm for a problem P. We say that algorithm A accepts an
instance of P if and only if, there exists a guess that leads to a yes answer.
• In non deterministic algorithms, there is no uniquely defined result in terms of output for a
133
www.android.universityupdates.in | www.universityupdates.in |
certain input.
• Nondeterministic algorithms are allowed to contain operations whose outcomes are limited to a
given set of instances of P, instead of being uniquely defined results.
• A Non-deterministic algorithm A on input x consists of two phases:
• The Nondeterministic algorithm uses three basic procedures, whose time complexity is O(1).
By the definition of nondeterministic algorithm, the output is -1 iff there is no j such that A[j] = x
Since A is not ordered, every deterministic search algorithm is of complexity O(n), whereas the
nondeterministic algorithm has the complexity as O(1).
Nondeterministic Sort Algorithm: The following algorithm sorts ‘n’ positive integers in non-
decreasing order and produces output in sorted order. The array B[] is an auxiliary array initialized to
0 and is used for convenience.
Algorithm nd_sort ( A, n )
{
for ( i = 0; i < n; B[i++] = 0;
); for ( i = 0; i < n; i++ )
{
j = choice ( 0, n - 1 );
if ( B[j] != 0 ) failure();
B[j] = A[i];
}
// Verify order
for ( i = 0; i < n-1; i++ )
if ( B[i] > B[i+1] )
failure(); write ( B );
success();
}
The time complexity of nd_sort is O(n). Best-known deterministic sorting algorithm like binary
search has a complexity of (n log n).
Satisfiability problem is to determine whether a formula is true for some assignment of truth
values to the variables
CNF--satisfiability is the satisfiability problem for CNF formulas
DNF--satisfiability is the satisfiability problem for DNF formulas
Polynomial time nondeterministic algorithm that terminates successfully iff a given
propositional formula E(x1, . . . , xn) is satisfiable
Non deterministically choose one of the 2n possible assignments of truth values to (x1, . . . , xn)
and verify that E(x1, . . . , xn) is true for that assignment
Algorithm eval ( E, n )
{
// Determine whether the propositional formula E is satisfiable.
Here variable are x1, x2, ...,
xn for ( i = 1; i <= n; i++ )
x(i) = choice ( true, false
); if ( E ( x1, ..., xn ) )
success();
else
failure();
}
1
www.android.universityupdates.in | www.universityupdates.in |
Decision Problem and Algorithm : Any problem for which the answer is either zero or one is called
a decision problem. An algorithm for a decision problem is termed a decision algorithm.
A decision algorithm will output 0 or 1
Implicit in the signals success() and failure()
Output from a decision algorithm is uniquely defined by input parameters and algorithm
specification.
Optimization Problem and Algorithm: Any problem that involves the identification of an optimal
(either minimum or maximum) value of a given cost function is known as an optimization problem.
An optimization algorithm is used to solve an optimization problem.
An optimization problem may have many feasible solutions
The problem is to find out the feasible solution with the best associated value
NP-completeness applies directly not to optimization problems but to decision problems.
Maximal Clique:
Clique is a maximal complete sub-graph of a graph G = (V,E), that is a subset of vertices in V all
connected to each other by edges in E (i.e., forming a complete graph).
1
www.android.universityupdates.in | www.universityupdates.in |
Example:
The Size of a clique is the number of vertices in it. The Maximal clique problem is an optimization
problem that has to determine the size of a largest clique in G. A decision problem is to determine
whether G has a clique of size at least ‘k’.
Input for Maximal clique problem: Input can be provided as a sequence of edges. Each edge in
E(G) is a pair of vertices (i, j) .The size of input for each edge (i, j) in binary representation is
Let us denote the deterministic decision algorithm for the clique decision problem as dclique(G, k).
If |V | = n, then the size of a maximal clique can be found
by for ( k = n; dclique ( G, k ) != 1; k-- );
If time complexity of dclique is f(n), size of maximal clique can be found in time
g(n) <= n.f(n). Therefore, the decision problem can be solved in time g(n)
Note that Maximal clique problem can be solved in polynomial time if and only if the clique
decision problem can be solved in polynomial time.
1
www.android.universityupdates.in | www.universityupdates.in |
The for loop selects or discards each of the n items It also re-computes the total weight and
profit corresponding to the selection
The if statement checks to see the feasibility of assignment and whether the profit is above a
lower bound r
The time complexity of the algorithm is O(n) . If the input length is q in binary, then O(q).
Algorithm nd_knapsack ( p, w, n, m, r, x )
{
W = 0; P = 0;
for ( i = 1; i <= n; i++ )
{
x[i] = choice ( 0, 1 );
W += x[i] * w[i];
1
www.android.universityupdates.in | www.universityupdates.in |
P += x[i] * p[i];
}
if ( ( W > m ) || ( P < r ) )
failure();
else
success();
}
Algorithm Sum_of_subsets ( A, n, m )
{
// A[n] is an array of integers
s = 1 // s is an m+1 bit word
// bit 0 is always
1 for i = 1 to n
s |= ( s << A[i] ) // shift s left by A[i]
bits if bit m in s is 1
write ( "A subset sums to m"
); else
write ( "No subset sums to m" );
}
1
www.android.universityupdates.in | www.universityupdates.in |
COOK’S THEOREM:
We know that, Class P problems are the set of all decision problems solvable by deterministic
algorithms in polynomial time. Similarly Class NP problems are set of all decision problems
solvable by nondeterministic algorithms in polynomial time.
Since deterministic algorithms are a special case of nondeterministic algorithms, P NP
Cook formulated the following question: Is there any single problem in NP such that if we showed
it to be in P, then that would imply that P = NP? This led to Cook’s theorem as :
1
www.android.universityupdates.in | www.universityupdates.in |
deterministic algorithm ‘Z’ in P.
So, the above construction shows that “if satisfiability is in P, then P=NP”
1
www.android.universityupdates.in | www.universityupdates.in |
3. Write the difference between the Greedy method and Dynamic programming.
Greedy method
1. Only one sequence of decision is generated.
2. It does not guarantee to give an optimal solution
always. Dynamic programming
1. Many number of decisions are generated.
2. It definitely gives an optimal solution always.
1
www.android.universityupdates.in | www.universityupdates.in |
other by being in the same row or in the same column or in the same diagonal.
7. Define Backtracking
Ans: Backtracking is used to solve problems with tree structures. Even problems seemingly
remote to trees such as a walking a maze are actually trees when the decision \'back-left-straight-
right\' is considered a node in a tree. The principle idea is to construct solutions one component at
a time and evaluate such partially constructed candidates
1
www.android.universityupdates.in | www.universityupdates.in |
1
www.android.universityupdates.in | www.universityupdates.in |
In computer science, a heuristic has a similar meaning, but refers specifically to algorithms.