Design and Analysis of Algorithm
Design and Analysis of Algorithm
Design and Analysis of Algorithm
A Course Material
On
Design and Analysis of Algorithm
By
L.Sankaran
Assistant Professor
Computer Science and Engineering Department
Quality Certificate
Year/Sem:II/IV
Name: L.Sankaran
This is to certify that the course material being prepared by Mr. L.Sankaran is of the
adequate quality. He has referred more than five books and one among them is from
abroad author.
Seal: Seal:
OUTCOMES:
At the end of the course, the student should be able to:
Design algorithms for various computing problems.
Analyze the time and space complexity of algorithms.
Critically analyze the different algorithm design techniques for a given problem.
Modify existing algorithms to improve efficiency.
TEXT BOOK:
1. Anany Levitin, ―Introduction to the Design and Analysis of Algorithms‖, Third Edition,
Pearson Education, 2012.
REFERENCES:
1. Thomas H.Cormen, Charles E.Leiserson, Ronald L. Rivest and Clifford Stein,
―Introduction to Algorithms‖, Third Edition, PHI Learning Private Limited, 2012.
2. Alfred V. Aho, John E. Hopcroft and Jeffrey D. Ullman, ―Data Structures and
Algorithms‖, Pearson Education, Reprint 2006.
3. Donald E. Knuth, ―The Art of Computer Programming‖, Volumes 1& 3 Pearson
Education, 2009. Steven S. Skiena, ―The Algorithm Design Manual‖, Second Edition,
Springer, 2008.
4. http://nptel.ac.in/
CONTENTS
1 Unit – I 7
2 Unit – II 40
3 Unit – III 88
4 Unit – IV 122
5 Unit – V 160
Prerequisite
Programming data structure I (CS 6202), Programming data structure II(CS 6301). This
class assumes familiarity with asymptotic analysis (Big-O, etc.), recurrence relations
and the correct implementation of basic algorithms. Students without the required
background may struggle to keep up with the lectures and assignments.
Unit – I
Introduction
Part – A
13. Define the asymptotic notation “Big oh” (0). [ CO1 - L1 - Nov/Dec 2010]
A function t(n) is said to be in O(g(n)) (t(n) Є O(g(n))), if t(n) is bounded above by
constant multiple of g(n) for all values of n, and if there exist a positive constant c
and non negative integer n0 such that
15. Define the asymptotic notation “theta” (Θ). [ CO1 - L1 - Nov/Dec 2013]
A function t(n) is said to be in Θ(g(n)) (t(n) Є Θ(g(n))), if t(n) is bounded both above
and below by constant multiple of g(n) for all values of n, and if there exist a
positive constant c1 and c2 and non negative integer n0 such that
C2*g(n) ≤ t(n) ≤ c1*g(n) for all n ≥ n0.
Key comparison
Searching for key in a Number of list‘s items,
list of n items i.e. n
29. How is the efficiency of the algorithm defined? [ CO1 - L1 - Nov/Dec 2012]
The efficiency of an algorithm is defined with the components.
Time efficiency -indicates how fast the algorithm runs
Space efficiency -indicates how much extra memory the algorithm needs
30.Write an algorithm that finds the number of binary digits in the binary
representation of a positive decimal integer. [ CO1 - L1 – Apr/May 2015]
Number of major comparisons=⌊log2n⌋+ 1∈log2n.
Algorithm 3: Finding the number of binary digits in the binary representation of a positive
decimal integer.
Algorithm Binary(n)
count:=1;
whilen >1
do
count:=count+ 1;
n:=⌊n/2⌋;
end
return count;
PART – B
Writing an algorithm
Algorithm heading
Algorithm Body
Implementation of algorithms
An algorithm describes what the program is going to perform. It states some of the
actions to be executed and the order in which these actions are to be executed.
The various steps in developing algorithm are,
1. Finding a method for solving a problem. Every step of an algorithm should be in
a precise and in a clear manner. Pseudo code is also used to describe the
algorithm.
2. The next step is to validate the algorithm. This step includes, all the algorithm
should be done manually by giving the required input, performs the required
steps including in the algorithm and should get the required amount of output in
an finite amount of time.
3. Finally, implement the algorithm in terms of programming language.
Example
Problem size = 'n'
Algorithm = 'a' for problem size n
The document mechanism execution = Cn2 times
where C – constant
Then the order of the algorithm 'a' = O(n2)
where n2 = Complexity of the algorithm 'a'.
Step 5: Stop
t ← min ( a, b)
while (t>=1) do
{
If ( a mod t == 0 AND b mod t == 0) then
Return t
Else
t ← t-1
}
Return 1
Choosing between exact and appropriate problem solving :The next principal
decision is to choose between solving the problem exactly or solving the problem
approximately.The algorithm used to solve the problem exactly called exact
algorithm.The algorithm used to solve the problem approximately is called
approximation algorithm.
Analysing an algorithm
Efficiency of an algorithm is determined by measuring the time, space and amount of
resources, it uses for executing the program.The efficiency of the algorithm is
determined with respect to central processing units time and internal memory.
Coding an Algorithm
Implementing an algorithm correctly is necessary but not sufficient to diminish the
In-place
An algorithm is said to be in-place if it does not require extra memory, except, possibly
Internal Sorting
The key principle of internal sorting is that all the data items to be sorted are retained in
the main memory and random access memory.
This memory space can be effectively used to sort the data items.
The various internal sorting methods are
1. Bubble sort
2. Selection sort
3. Shell sort
4. Insertion sort
5. Quick sort
6. Heap sort
External Sorting
The idea behind the external sorting is to move data from secondary storage to mail
memory in large blocks for ordering the data.The most commonly used external sorting
method is merge sort.
Searching
One of the important applications of array is searching,Searching is an activity by which
we can find out the desired element from the list. The element which is to be searched
is called search key.There are many searching algorithm such as sequential search ,
Fibonacci search and more.
In such a situation searching an element is difficult. To handle such lists supporting data
structures and algorithms are needed to make the list balanced (organized)
String processing
A string is a collection of characters from an alphabet.
Different type of strings are
Text string
Bit string
Text String
It is a collection of letters, numbers and special characters.
Bit String
It is collection of zeros and ones.
Graph Problems
Graph is a collection of vertices and edges. Formally, a graph G={ V, E } is defined by a
pair of two sets.A finite set V of items called Vertices and a set E of pairs of these items
called edges.If the pairs of vertices are ordered, then G is called a directed graph
because every edge is directed.In a directed graph the direction between two nodes are
not same G(V,W)!=G(W,V) If the pair of the vertices are unordered then G is called an
undirected graph.In undirected graph, the edges has no specific direction.The graph
problems involve graph traversal algorithms, shortest path algorithm and topological
sorting and so on. Some graph problems are very hard to solve. For example travelling
salesman problem, graph colouring problems
Combinatorial Problems
The travelling salesman problem and the graph colouring problems are examples of
combinatorial problems.
A combinatorial object such as a permutation a combination or a subset that satisfies
certain constraints and has some desired property such as maximizes a value or
minimizes a cost should be find.
Combinatorial problems are the most difficult problems.
1. As problem size grows the combinatorial objects grow rapidly and reach
to huge value. size.
2. There is no algorithms available which can solve these problems in finite
amount of time
3. Many of these problems fall in the category of unsolvable problem.
Some combinatorial problems can be solved by efficient algorithms.
Geometric Problems
Geometric algorithms deal with geometric objects such as points ,lines and polygons.
The procedure for solving a variety of geometric problems includes the problems of
constructing simple geometric shapes such as triangles, circles and so on.
The two classic problems of computational geometry are the
1. Closest pair problem
2. Convex hull problem
The closest pair problem is self explanatory. Given n points in the plane, find the closest
pair among them.The convex hull problem is used to find the smallest convex polygon
that would include all the points of a given set.
The geometric problems are solved mainly in applications to computer graphics or in
robotics
Numerical problems
Numerical problems are problems that involve mathematical objects of continuous
nature such as solving equations and systems of equations computing definite integrals
evaluating functions and so on.
Most of the mathematical problems can be solved approximate algorithms.
These algorithms require manipulating of the real numbers; hence we may get wrong
output many times.
Analysis framework
The efficiency of an algorithm can be decided by measuring the performance of an
algorithm. The performance of an algorithm is computed by two factors
amount of time required by an algorithm to execute
amount of storage required by an algorithm
Overview
Space complexity
Time complexity
Measuring an Input's size
Measuring Running Time
Orders of Growth
Space complexity
The space complexity can be defined as amount of memory required by an algorithm to
run.To compute the space complexity we use two factors: constant and instance
characteristics.The space requirement S(p) can be given as S(p) = C+ S(p) Where C is
a constant i.e. fixed part and it denotes the space of inputs and outputs.
Time complexity
The time complexity of an algorithm is the amount of computer time required by an
algorithm to run to completion.For instance in multiuser system, executing time depends
on many factors such as
o System load
o Number of other programs running
o Instruction set used
o Speed underlying hardware
o The time complexity is therefore given in term of frequency count
o Frequency count is a count denoting number of times of execution of
statement
Example
For (i=0; i<n; i++)
{
sum = sum + a[i];
}
Statement Frequency count
i=0 1
i<n This statement executes for (n+1) times. When
conditions is true i.e. when i<n is true , the
execution happens to be n times , and the
statement execute once more when i<n is false
i++ n times
sum = sum + a[i] n times
Total 3n + 2
Drawbacks
l. Dependence on the speed of a particular computer
2. Dependence on the quality of a program implementing the algorithm.
3. The compiler used in generating the machine code.
4. The difficulty of clocking the actual running time of the program .
Basic Operation - the operation contributing the most to the total running time, and
compute the number of times the basic operation is executed.
It is not so difficult to identify the basic operation of an algorithm: it is usually the most
time consuming operation in the algorithm's innermost loop.
Example :Most sorting algorithms work by comparing the elements (keys) of a list being
sorted with each other. For such algorithms the basic operation is a Key Comparison.
Problem Input Size Basic operation
statement
Searching a key List of n elements Comparison of key with
element from the every element of list
list of n elements
Performing matrix The two matrixes with Actual multiplication of the
multiplication order n×n elements in the matrices
Computing GCD Two numbers Division
of two numbers
Orders of Growth
Measuring the performance of an algorithm in relation with the input size n is called order of
growth.
Example:
Sequential Search or Linear Search
ALGORITHM SequentialSearch(A[0..n − 1], K)
//Searches for a given value in a given array by sequential search
//Input: An array A[0..n − 1] and a search key K
//Output: The index of the first element in A that matches K
// or −1 if there are no matching elements
i ←0
while i < n and A[i] _= K do
i ←i + 1
if i < n return i
else return -1
This algorithm searches for a given item using some search key K in a list of 'n' elements by
checking successive elements of the list until a match with the search key is found or the list
is exhausted.The algorithm makes the largest number of key comparisons among all possible
inputs of size n:Cworst(n)=n
The best case efficiency of an algorithm is its efficiency for the best case input of size n, which
is an input (or inputs) of. size n for which the algorithm runs the fastest among all possible
inputs of that size.
The way to determine the best case efficiency of an algorithm is as follows.
First, determine the kind of inputs of size n.
Then ascertain the value of C(n) on these inputs.
Example: For sequential search, the best case inputs will be lists of size 'n' with their first
elements equal to a search key: Cbest(n) = 1.
Example 1:
Problem for finding the value of the largest element in a list of n numbers
The pseudo code to solving the problem is
ALGORITHM MaxElement(A[0..n-1])
//Problem Description : This algorithm is for finding the
//maximum value element from the array
//Input:An array A[0..n-1] of real numbers
//Output: Returns the largest element from array
Maxval ← A[0]
For i ← 1 to n-1 do Searching the maximum element from an array
{
If ( A[i]>max_value)then
Maxval ← A[i] If any value is large than current
}
Max_ Value then set new Max_value
Return Max_value
by obtained larger value
Mathematical Analysis
Step 1: The input size is the number of elements in the array(ie.),n
Step 2 : The basic operation is comparison in loop for finding larger value
There are two operations in the for loop
1.Comparison operation a[i]->maxval
2.Assignment operation maxval->a[i]
Step 3: The comparison is executed on each repetition of the loop. As the
comparison is made for each value of n there is no need to find best case
worst case and average case analysis.
Step 4: Let C(n) be the number of times the comparison is executed.
The algorithm makes comparison each time the loop executes.
That means with each new value of I the comparison is made.
Hence for i= 1 to n – 1 times the comparison is made . therefore we can
formulate C(n) as
C(n) = one comparison made for each value of i
Step 5 : let us simplify the sum
Thus C(n) =∑
=n-1 θ (n) Using the rule ∑ θ (n)
∑ i=C∑ i R1
∑ i+bi)=∑ +∑ I i R2
The two summation formulas are
1. ∑ =u-l+1
2. ∑ =∑ =1+2+…..+n
=n(n+1)/2
=1/2n2 o(n2) S2
Example 2
Element uniqueness problem-check whether all the element in the list are distinct
ALGORITHM UniqueElements(A[0..n-1])
//Checks whether all the elements in a given array are distinct
//Input :An array A[0..n-1]
//Output Returns ‗true‘ if all elements in A are distinct and ‗false‘
//otherwise
for i to n-2 do
for j i+1 to n-1 do
If any two elements in the array
if a[i] = a[j] then
return false are similar then return .false
else indicating that the array elements
return true are not distinct
Mathematical analysis
Step 1: Input size is n i.e total number of elements in the array A
Step 2: The basic iteration will be comparison of two elements . this
operation the innermost operation in the loop . Hence
if a[i] = a[j] then comparison will be the basic operation .
Step 3 : The number of comparisons made will depend upon the input n .
but the algorithm will have worst case complexity if the same
element is located at the end of the list. Hence the basic operation
depends upon the input n and worst case
Step 4: The worst case input is an array for which the number od elements
comparison cworst(n) is the largest among the size of the array.
There are two kinds of worst case inputs, They are
1.Arrays with no equal elements.
2.Arrays in which the last two elements are pair of equal elements.
For the above inputs, one comparison is made for each repetition of the inter most
loop (ie) for each value of the loop's variable 'j' between its limits i+1 and n-1 and this
is repeated limit for each values of the outer loop (ie) for each value of the loop's
variable `i' between 0 and n-2. Accordingly,
C worst (n) = Outer loop × Inner loop
Cworst(n) = ∑ ∑
Step 5: now we will simplify C worst as follows
=∑ Θ∑
=∑
=∑ -∑
Now taking (n-1) as a common factor, we can write
This can be obtained using
formula∑ /2
where C[i, j ]= A[i, 0]B[0, j]+ . . . + A[i, k]B[k, j]+ . . . + A[i, n − 1]B[n − 1, j] for every
pair of indices 0 ≤ i, j ≤ n − 1.
b b
00 01
a a a
C= 00 01 03 1 2
b b
1 2 3 × 10 11
a a a
10 11 12 3 4
4 5 6
a00 ×b00 + a01 ×b10 + a02 ×b20 a00 ×b01 + a01×b11 + a02×b21
C= a10×b00 + a11×b10 + a12×b20 a10×b01 + a11×b11 + a12×b21
C= 1×1+2×3+3×5 1 × 2 + 2 × 4 + 3× 6
4×1+5×3+6×5 4×2+5×4+6×6
C= 22 28
49 64
Mathematical analysis
Step 1: The input‘s size of above algorithm is simply order of matrices i.e n.
Step 2: The basic operation is in the innermost loop and which is
M(n)= n3
Thus the simplified sum is n3. Thus the time complexity of matrix
multiplication Θ (n3)
EXAMPLE 4
The following algorithm finds the number of binary digits in the binary
representation of a positive decimal integer.
Mathematical analysis
Step 1: The input size is n i.e . The positive integer whose binary digit in binary
representation needs to be checked.
Step 2 : The basic operation is denoted by while loop. And it is each time checking
whether n > 1. The while loop will be executed for the number of time at which n>1 is
true . it will be executed once more when n>1 is false . but when n>1 is false the
statements inside while loop wont get executed.
Step 3: The value of n is halved on each repetition of the loop. Hence efficiency
algorithm is equal to log2 n
Step 4: hence total number of times the while loop gets executed is
[log2 n] + 1
Hence time complexity for counting number of bits of given number is Θ
(log2 n). the indicates floor value of log 2 n
6.Derive the worst case analysis of merge sort using suitable illustration. [ CO1
– H3 – Apr/May 2015]
Efficiency of Merge Sort
In merge sort algorithm the two recursive calls are made. Each recursive call focuses on
n/2 elements of the list . After two recursive calls one call is made to combine two
sublist i.e to merge all n elements. Hence we can write recurrence relation as
T(n) = T(n/2) + T(n/2) + cn
T(n/2) = Time taken by left sublist
T(n/2) = time taken by right sublist
T(n) = time taken for combining two sublists
where n> 1 T (1) = 0
The time complexity of merge sort can be calculated using two methods
Master theorem
Substitution method
Master theorem
Let , the recurrence relation for merge sort is
T(n) = T(n/2) + T(n/2) + cn
let
T(n) = aT(n/b) + f(n) be a recurrence relation
i.e. T(n) = 2T(n/2) + cn ------- ( 1 )
T(1) = 0 ----------- (2 )
As per master theorem
T(n) = Θ (n d long n ) if a = b
As equation ( 1),
a =2 , b = 2 and f(n) = cn
and a = bd
i.e 2 = 2`
This case gives us ,
T (n) =Θ (n log2 n)
Hence the average and worst case time complexity of merge sort is
C worst (n) = (n log2 n)
Substitution method
Let, the recurrence relation for merge sort be
T(n) = T(n/2) + T(n/2) + cn for n>1
i.e. T(n) = 2T(n/2) + cn for n>1 ------- (3)
T(1) = 0 -------(4)
Let us apply substitution on equation ( 3) .
Assume n=2k
T(n) = 2T(n/2) + cn
T(n) = 2T(2k/2 ) + c.2k
T(2k) = 2T(2k-1) + c.2k
If k = k-1 then,
T(2k) = 2T(2k-1) + c.2k
T(2k) = 2[2T(2k-2) + c.2k -1] + c.2k
T(2k) = 22 T(2k-2) + 2.c.2k -1 + c .2k
T(2k) = 22 T(2k-2) + 2.c.2k /2 + c.2k
T(2k) = 22 T(2k-2) + c.2k + c.2k
T(2k) = 22 T(2k-2) + 2c .2k
T(2k) = 23 T(2k-3) + 3c .2k
T(2k) = 24 T(2k-4) + 4c .2k
T(2k) = 2k T(2k-k) + k.c.2k
Unit – 2
Part - A
2.What are the types of Brute Force Algorithm? [ CO2 - L1 - May/June 2010]
There are two types of Brute force algorithm. They are Consecutive integer checking
algorithm for computing the greatest common divisor of two integer[gcd(m,n)] Definition
based algorithm for matrix multiplication.
3.What are the Advantages of Brute Force Approach? [ CO2 - L1 – Nov/Dec 2015]
Advantages
A brute Force algorithm can be useful for solving small size instances of a problem.A
brute Force algorithm can serve an important theoretical or educational purpose.The
brute force approach provides reasonable algorithms of atleast some practical value
with no limitation on instance size for sorting, searching, matrix multiplication and string
matching problem.
An exhaustive search, also known as generate and test, is a very general problem-
solving technique that consists of systematically enumerating all possible
candidates for the solution and checking whether each candidate satisfies the
problem's statement.
16. Is insertion sort better than the merge sort? [ CO2 – H2]
Insertion sort works exceedingly fast on arrays of less then 16 elements, though for
large ‗n‘ its computing time is O (n2).
19. Give the recurrence equation for the worst case behavior of merge sort? [
CO2 - L1 – Nov/Dec 2010]
The recurrence equation for the worst case behavior of merge
sort is T(n) = 2T(n/2) + cn for n>1, c is a constant
20. Find the number of comparisons made by the sequential. [ CO2 - L1 - Nov/Dec
2011]
search in the worst case and best case?
Worst case: The algorithm makes the largest number of key comparisons among all
possible input of size n. Cworst(n)=n
Best Case: The best case inputs will be lists of size n with their first element equal to
search key. Cbest(n)=1
22. What is the idea behind binary search? [ CO2 - L1 - Nov/Dec 2013]
A binary search or half-interval search algorithm finds the position of a specified value
(the input "key") within a sorted array.In each step, the algorithm compares the input
key value with the key value of the middle element of the array. If the keys match, then
a matching element has been found so its index, or position, is returned. Otherwise, if
the sought key is less than the middle element's key, then the algorithm repeats its
action on the sub-array to the left of the middle element or, if the input key is greater, on
the sub-array to the right. If the remaining array to be searched is reduced to zero, then
the key cannot be found in the array and a special "Not found" indication is returned.
23. Give the time efficiency and drawback of merge sort. [ CO2 - L1- Nov/Dec
2013 ]
24. Trace the operation of binary search algorithm for the input –
15, -6, 0, 7, 9, 23, 54, 82, 101, 112, 125, 131, 142, 151, if you
0 1 2 3 4 5 6 7 8 9 10 11 12 13
Iteration 0:
Left = 0
Right = 13
Mid = (Left + Right) / 2
= (0 + 13) / 2
Mid = 6
Midelement = 54
Search key = 9
Since 9 < 54, search the element 9 in the left of midelement 54.
Iteration 1:
Left = 0
Right = 5
Mid = (Left + Right) / 2
= (0 + 5) / 2
Mid = 2
Midelement = 0
Search key = 9
Since 9 > 0, search the element 9 in the right of midelement 0.
Iteration 2:
Left = 3
Right = 4
Mid = (Left + Right) / 2
= (3 + 4) / 2
Mid = 3
Midelement = 7
Search key = 9
Since 9 > 7, search the element 9 in the right of midelement 7.
Therefore 9 is found in the position 4.
x := x0
p := 0.0
for i := n down to 0 do
power := 1
for j := power * x
p := p + a:= 1 to i do
power [i] * power
return p
Efficiency: (n2)
26. Distinguish between sequential and binary search. [ CO2 – L2 – Apr/May 2013]
Sequential technique binary search technique
This technique does not require the list This technique require the list to be sorted.
to be sorted Then only this method is applicable
The worst case time complexity of this The worst case time complexity of this
technique is O(n) technique is O(log n)
Every element of the list may get Only the mid element of the list is
compared with the key element. compared with key element.
where f(n) is the time to divide n elements and to combine their solution.
29. What is the necessary precondition for the binary search ? [ CO2 - L1 -
May/June 2015]
For the binary search the list should be sorted either in ascending or descending order
30.Derive complexity of binary search algorithm. ? [CO2 –L2– Apr/ May 2015]
Worst Case Analysis
The worst case includes all arrays that do not contain a search key.
The recurrence relation for
Cworst(n) = Cworst (n/2) + 1, for n > 1 ----- (1)
PART – B
The basic operation in above algorithm is computing Euclidian distance between two
points.
Then the basic operation of the algorithm will be squaring a number.
The number of times it will be executed can be computed as follows:
C(n) = ∑ ∑ 2
= 2∑
= 2[(n − 1) + (n − 2) + . . . + 1]
= (n − 1)n ∈ θ(n2).
Of course, speeding up the innermost loop of the algorithm could only decrease the
algorithm‘s running time by a constant factor, but it cannot improve its asymptotic
efficiency class.
Convex-Hull Problem
Definition:
Given a set S { p1, p2 ,p3, …pn} of points in the plan , the convex hull H(S) is the
smallest convex polygon in the plane that contains all of the points of S. the set S is
called as coves set .
A polygon is convex if and only if any two points from the set forming a line
segment with end points entirely within the polygon
P
P
P
P
P
P P
Example 2:
The convex hull for this set of eight points is the convex polygon with vertices at {p1,
p5, p6, p7, and p3.}
This algorithm solves linear programming problems, which are problems of finding a
minimum or a maximum of a linear function of n variables subject to linear constraints.
Here, however, we are interested in extreme points because their identification solves
the convex-hull problem. Actually, to solve this problem completely, we need to know a
bit more than just which of n points of a given sets are extreme points of the set‘s
convex hull: we need to know which pairs of points need to be connected to form the
boundary of the convex hull.
Note that this issue can also be addressed by listing the extreme points in a
clockwise or a counter clockwise order.
A few elementary facts from analytical geometry are needed to implement this
algorithm.
1. The straight line through two points (x1, y1), (x2, y2) in the
coordinate plane can be defined by the equation
ax + by = c
This problem can also be started as finding shortest Hamiltonian circuit of the graph.
The shortest Hamiltonian circuit is a cycle in the given graph such that all the vertices
of the graph can be visited only once.And the tour obtained in such a way has
shortest distance.
Example
Consider the graph as given below.This is a weighted graph in which weight along
the edges represent the distances among the cities. We have to find shortest
Hamiltonian circuit i.e. the path in which each city is visited once and returning
to the city from which it has started initially.
2
a b
Tour 5 8 7 3 Length
a -> b -> c -> d -> a I = 2+8+1+7 = 18
a -> b -> d -> c-> a I =c 2+3+1+5d = 11
optimal 1
a-> c -> b -> d -> a I = 5+8+3+7 = 23
a-> c -> d -> b -> a I = 5+1+3+2 = 11
optimal
a-> d ->b -> c -> a I = 7+3+8+5 = 23
a-> d -> c -> b -> a I = 7+1+8+2 = 18
Fig: Solution to a small instance of the travelling salesman
problem by exhaustive search
Thus we have to try each possible path and find the shortest distance which gives
optimal tour.
It is easy to see that a Hamiltonian circuit can also be defined as a sequence of n +
1 adjacent vertices vi0, vi1, . . . , vin−1, vi0, where the first vertex of the sequence is the
same as the last one and all the other n − 1 vertices are distinct
2. Knapsack Problem
This is another popular problem which can be solved using exhaustive search.
It can be stated as follow : suppose that there are n objects from I = 1,2 , 3, …n. each
object I has some weight wi and values associated with each object is vi .
And capacity of knapsack is W. a person has to pickup the most valuable objects to
fill the knapsack to its capacity.
Nil 0 $0
{1} 7 $42
{2} 3 $12
{3} 4 $40
{4} 5 $25
{1,2} 7+3=10 $42+$12=$54
7+4=11
{1,3} Not feasible
(11>10)
7+5=12
{1,4} Not feasible
(12>10)
{2,3} 3+4=7 $12+$40=$52
{2,4} 3+5=8 $12+$25=$37
$40+$25=$65
{3,4} 4+5=9
(Feasible)
7+3+4=14
{1,2,3} Not feasible
(14>10)`
7+3+5=15
{1,2,4} Not feasible
(15>10)
7+4+5=16
{1,3,4} Not feasible
(16>10)
3+4+5=12
{2,3,4} Not feasible
(12>10)
7+3+4+5=19
{1,2,3,4} Not feasible
(19>10)
Since the subset {3, 4} gives the maximum value $65, it is the feasible solution, so
item 3 and item 4 can be put in the Knapsack bag.
Because in this method, each element of the problem‘s domain has to be searched
for obtaining solution. Hence these problems are also called as NP-hard problems
3. Assignment Problem
Consider that there are n people who need to be assigned to execute n jobs i.e only
one person is assigned to execute one job at a time. Then problem is to find such
assignment that gives smallest total cost. The cost can be computed as cost C[i, j, k, l]
C[i, j, k, l] = Job i is assigned to Person 1, Job j is assigned to Person 2, Job k is
assigned to Person 3, Job 4 is assigned to Person 4.
Example 1
Person Job 1 Job 2 Job 3 Job 4
Person 1 9 2 7 8
Person 2 6 4 3 7
Person 3 5 8 1 8
Person 4 7 6 9 4
Person 2 7 5 4 8
Person 3 6 9 2 9
Person 4 8 7 10 5
Algorithm DC(p)
{
If P is too small then
Return solution of P.
Else
{
Divide (p) and obtain p1, p2, …..pn where n ≥ 1
Apply DC to each sub problem
Return combine (DC(p 1),
DC( p2)….Dc(pn));
}
}
divides the problem into two smaller sub problems.
(a0 + ….an-1)
If we want to divide a problem of size n in to a size of n /b taking f(n) time to divide
and combine , then we can set up recurrence relation for obtaining time for size n
is T (n) = a T (n/b) + f (n),
T(n/b) = Time for size n/b time required for dividing the
problem in to sub problem.
T(n) = Time for size n
n = number of sub instances
The above equation is called general divide and conquer recurrence. The order of
growth of T(n) depends upon the constants a, b and order of growth function f(n).
Divide and Conquer technique
Examples for divide and conquer method are,
Binary search
Quick sort
Merge sort
Example 1 :
Consider the problem of computing the sum of number a 0 …… an-1.If n > 1, the
problem is divided into two instances of the same problem.They areTo compute the
sum of the first [n/2] numbers.To compute the sum of the remaining [n/2] numbers.Once
the two instances are computed, add their values to get the sum of original problem.
a0 + a1 +……+ an-1 = (a0 + a1 +……+ a[n/2]-1) + (a[n/2] +……+ an-1)
An instance of size n can be divided into several instances of size
n/b, Where a and b are constants
a ≥ 1 and b > 1
The recurrence for the running time T(n) is
T(n) = aT(n/b) + f(n) ,
which is called as general divide and conquer recurrence where,
f(n) is a function that accounts for the time spent on dividing the problem into smaller
ones and on combining their solutions.
The order of growth of T(n) depends on the values of the constants ‗a‘ and ‗b‘ and the
order of growth of the function f(n).
For example, the recurrence equation for the number of additions is
a(n) = 2a(n/2) + 1
5. Explain the Merge Sort algorithm with the help of illustrative. [ CO2 – L2 -
Dec 2011/2012/2013]
Example.
The merge sort is a sorting algorithm that uses the divide and conquer strategy.
Division is dynamically carried out.
Merging is the process of combining two or more files into a new sorted file.
Merge sort on an input array with n elements consists of three steps:
Divide: partition array into two sub lists s1 and s2 with n/2 elements each
Conquer: then sort sub list s1 and sub list s2.
Combine: merge s1 and s2 into a unique sorted group.
Merge sort is a perfect example of a successful application of the divide and conquer
technique.
It sorts a given array A[0…..n − 1] by dividing it into two halves A[0…..[n/2]−1] and
A[[n/2]…..n − 1].
It sorts each half separately by using recursive procedure, and Then, merging the two
smaller sorted arrays into a single sorted one.Steps to be followed
The first step of the merge sort is to chop the list into two If the list has even length, split
the list into two equal sub lists.If the list has odd length, divide the list in two by making
the first sub list one entry greater than the second sub list.Then split both the sub lists
into two and go on until each of the sub lists are of size one.Finally, start merging the
individual sub lists to obtain a sorted list.
Example:
The operation of the algorithm for the array of element (8,3,2,9,7,1,5,4) is explained in
the figure given below
8 3 2 9 7 1 5
4
8 3 2 7 1 5
9 4
8 2 9 7 1 5 4
3
8 3 2 9 7 1 5 4
3 8 2 9 1 7 4 5
2 3 8 1 4 5
9 7
1 2 3 4 5 7 8
9
ALGORITHM
Algorithm Mergesort(A[0..n − 1])
Master theorem
Let , the recurrence relation for merge sort is
T(n) = T(n/2) + T(n/2) + cn
let
T(n) = aT(n/b) + f(n) be a recurrence relation
i.e. T(n) = 2T(n/2) + cn ------- ( 1 )
T(1) = 0 ----------- (2 )
As per master theorem
T(n) = Θ (n d long n ) if a = b
As equation ( 1),
a =2 , b = 2 and f(n) = cn and a = bd
i.e 2 = 2`
This case gives us ,
T (n) =Θ (n log2 n)
Hence the average and worst case time complexity of merge sort is
C worst (n) = (n log2 n)
Substitution method
Let, the recurrence relation for merge sort be
T(n) = T(n/2) + T(n/2) + cn for n>1
i.e. T(n) = 2T(n/2) + cn for n>1 ------- (3)
T(1) = 0 -------(4)
Let us apply substitution on equation ( 3) .
Assume n=2k
T(n) = 2T(n/2) + cn
T(n) = 2T(2k/2 ) + c.2k
T(2k) = 2T(2k-1) + c.2k
If k = k-1 then,
T(2k) = 2T(2k-1) + c.2k
T(2k) = 2[2T(2k-2) + c.2k -1] + c.2k
T(2k) = 22 T(2k-2) + 2.c.2k -1 + c .2k
T(2k) = 22 T(2k-2) + 2.c.2k /2 + c.2k
T(2k) = 22 T(2k-2) + c.2k + c.2k
T(2k) = 22 T(2k-2) + 2c .2k
Similarly we can write,
T(2k) = 23 T(2k-3) + 3c .2k
T(2k) = 24 T(2k-4) + 4c .2k
…..….
T(2k) = 2k T(2k-k) + k.c.2k
T(2k) = 2k T(20) + k.c.2k
T(2k) = 2k T(1) + k.c.2k -------- (5)
But as per equation (4), T(1) =0
There equation (5) becomes ,
T(2k) = 2k .0 +. k. c . 2k
T(2k) = k. c . 2k
But we assumed n=2k , taking logarithm on both sides.
i.e. log 2 n = k
Therefore T(n) = log 2 n. cn
Therefore T (n) =Θ (n log2 n)
Hence the average and worst case time complexity of merge sort is
C worst (n) = (n log2 n)
Time complexity of merge sort
Best case Average case Worst case
Θ (n log2 n) Θ (n log2 n) Θ (n log2 n)
6. Explain the Quick Sort algorithm with the help of illustrative example.[ CO2 –
L2 - Nov/Dec 2012]
Quick sort is a sorting algorithm that uses the divide and conquers strategy.
Divide:
Split the array into two sub arrays that each element in the left sub array is less than or
equal the middle element and each element in the right sub array is greater than the
middle element .
The splitting of the array into two sub array is based on pivot element.
All the elements that are less than pivot should be in left sub array and all the elements
that are more than pivot should be in right sub array
Combine:
Combine all the sorted elements in a group to form a list of sorted elements
Quick sort is also referred as Partition Exchange sort. The problem of sorting a set is
reduced to the problem of sorting two smaller subsets.Quick sort divides input elements
according to their position in the array.It also divides the input elements according to the
value of element.To achieve the partition, quick sort rearrange the given array element
a[0,..n-1].It is a situation where all the elements before the position ‗S‘ are smaller than
or equal to a[s] and all the elements after position ‗s‘ are greater than or equal to a[s].
The partition is shown as
a[0]….a[s-1] a[s] a[s+1]……a[n-1]
P- pivot element
2. If the scanning indices have crossed over, i.e., i > j, we will have
partitioned the Sub array after exchanging the pivot with A[j].
j i
P All elements ≤P ≥P ≤ P All elements ≥P
3. Finally, if the scanning indices stop while pointing to the same element, i.e.,
i = j, the value they are pointing to must be equal to p.
Thus, we have the sub array partitioned, with the split position s = i = j :
i=j
P All elements ≤P =P All elements ≥P
Combine the last case with the case of crossed-over indices (i > j ) by exchanging the
pivot with A[j] whenever i ≥ j .
Example
The array elements are
5 3 1 9 8 2 4 7
The first element of array is chosen as pivot element.
Two indices i and j are used for scanning.
P i j
5 3 1 9 8 2 4 7
P i j
5 3 1 9 8 2 4 7
P i j
5 3 1 9 8 2 4 7
Now exchange the elements 9 and 4 now array becomes,
P i j
5 3 1 4 8 2 9 7
Now also exchange a[i] and a[j], the resultant array becomes,
P i j
5 3 1 4 2 8 9 7
Now the scanning indices i and j have not crossed (ie) i < j, simply exchange i and j.
The array becomes
P j i
5 3 1 4 2 8 9 7
Since a[j] < pivot, (2<5) exchange them.
The result is
P
2 3 1 4 5 8 9 7
Now, the array has been sub divided into sub array with pivot element as middle.
Sub array 1
2 3 1 4
P i j
2 3 1 4
P i j
2 3 1 4
Exchange a[i] and [j]
P i j
2 1 3 4
Since i < j, exchange a and j
P j i
2 1 3 4
Since a[j] < pivot, (1 < 2) exchange i and j
P i j
1 2 3 4
Sub array 3
3 4
P ij
3 4
Here i=j, ie both points to the same element.
j j
3 4
Sub array 2
8 9 7
P i j
8 9 7
Exchange a[i] and a[j]
The sub array 2 becomes
P i j
8 7 9
Here i < j simply exchange i and j
It becomes
P j i
8 7 9
Since a[j] < pivot, exchange them
7 8 9
Hence, the array elements are sorted. The sorted array is
1 2 3 4 5 7 8 9
Tree of recursive calls to Quicksort with input values l and r of subarray bounds and split
position s of a partition obtained.
repeat
repeat i ← i + 1 until A[i] ≥ p
repeat j ← j − 1 until A[j ] ≤ p
swap(A[i], A[j ])
until i ≥ j
swap(A[i], A[j ]) //undo last swap when i ≥ j
swap(A[l], A[j ])
return j
Application
Internal sorting of large data sets.
To improve the efficiency of the Quick sort various methods are used to choose the
pivot element.
One such method is called, median of three partitioning that uses the pivot element as
the median of left most, right most and the middle element of the array.
The binary search algorithm is one of the most efficient searching techniques which
require the list to be sorted in ascending order.
To search for an element in the list, the binary search algorithms split the list and locate
the middle element of the list.
The middle of the list is calculated as middle := (l + r) div n-numberof element in
list
The algorithm works by comparing a search key element ‗k‘ with the array middle
element a[m] After comparison, any one of the following three conditions occurs.
If the search key element ‗k‘ is greater than a[m], then the search element is only in the
upper or second half and eliminate the element present in the lower half. Now the
value of l is middle m+1.
If the search key element ‗k‘ is less than a[m], then the search element is only in the
lower or first half. No need to check in the upper half. Now the value of r is middle
m-1.If the search key element ‗k‘ is equal to a[m] , then the search key element k is
found in the position m, Hence search operation is complete.
The above steps are repeated until the search element is found, which is equal to
the middle element or the list consists of only one element that is not equal to the
search key element.
a[0]….a[m-1] a[m] a[m+1]……a[n-1]
Example:
The list of element are 3,14,27,31,39,42,55,70,81,85,93,98 and searching for k=70
in the list.
0 1 2 3 4 5 6 7 8 9 10 11 12
3 14 27 31 39 42 55 70 74 81 85 93 98
m- middle element
m = n div 2
= 13 div 2
m=6
0 1 2 3 4 5 6 7 8 9 10 11 12
3 14 27 31 39 42 55 70 74 81 85 93 98
m
0 1 2 3 4 5 6 7 8 9 10 11 12
3 14 27 31 39 42 55 70 74 81 85 93 98
l m
Since k> a[m] , then , l=7.
7 8 9 10 11 12
70 74 81 85 93 98
l m r
Since k< a[m] and 70 < 81
So, the element is present in the first half
If the array element at ‗l‘ position is equal to the value to be found then
Return found and position
else
Return not found and -1.
Algorithm
Algorithm BinSearch (var a, n elements, n, x: integer)
Var l,r,m:integer;
//Input: Given an array a[0….n-1] sorted in ascending order //and search key k.
//Output: An index of the array‘s element that is equal to k or //-1 if there is no such
element.
begin
l:=0; r := n-1;
while (l≤ r) do
begin
mid := [(l + r) div 2];
if k = a[mid] then
return m;
else if k <a[m] then
r:=m-1;
else
l:=m+1;
end:
return -1
end
The above recurrence relation can be solved further . assume n=2k the equation ( 1 )
becomes
Cworst(2k) = Cworst(2 k /2)+ 1
Cworst(2k) = Cworst(2 k-1)+ 1 ------ ( 3 )
Using backward substation method , we can substitute
Cworst(2k-1) = Cworst(2k-2)+ 1
Then equation (3) becomes
Cworst(2k) = [Cworst( 2 k-2)+ 1] + 1
Cworst(2k) = Cworst( 2 k-2)+ 2
Then
Cworst(2k) = [Cworst( 2 k-3)+1]+ 2
Cworst(2k) =Cworst( 2 k-3)+3
---
---
Cworst(2k) =Cworst( 2 k-k)+k
Cworst(2k) =Cworst( 2 0)+k
Cworst(2k) =Cworst( 1 )+k ----- (4)
But as per equation (2 )
as we have assumed n = 2k taking logarithm (base 2 )on both sides
log 2 n = log 2 2k
log 2 n = k. log 2 2
log 2 n = k(1) therefore log 2 2 =1 therefore k = log 2 n
Cworst(1) = 1 the we get equation ( 4 )
Cworst(2k) = 1 + k
Cworst(n) = 1 + log2n ----- (2
Cworst(n) = log2n for n>1
The worst case time complexity of binary search is Θ(log2n)
As Cworst(n) = log2n + 1
we can verify equation ( 1) with this value.
Cworst(n) = Cworst[(n/2)] + 1
In equation (1) put n = 2i
L.H.S
Cworst(n) = log2n + 1
= log2(2i )+ 1
= log 2 2 + log 2i + 1
= 1+ log 2i + 1
= 2 + log 2i
Cworst(n) =2 + log 2i
R.H.S
Cworst(n/2)+1 = log 2(2i/2 )+ 1
= log 2i + 1
= log 2 2i + 1+ 1
= 2 + log 2i
Cworst(n/2) =2 + log 2i
L.H.S = R.H.S
Hence
Cworst(n) = log 2n + 1 and
Cworst(i) = log 2i + 1 are same
Hence
Cworst(n) = Ө(log n )
if n= 2 then
log2 2 = 1
then c= log 2 2 + 1
c=1+1
c =2
if n = 8 , then
c = log2 n + 1
= log2 8 + 1
=3+1
C=4
Average number of comparison made by the binary search is slightly smaller than
worst case.
Cavg(n) log2 n
The average number of comparison in the successful search is Θ(log 2n)
Advantages
In this method elements are eliminated by half each time. So it is very faster than
the sequential search.
It requires less number of comparisons than sequential search to locate the search
key element.
Disadvantages
An insertion and deletion of a record requires many records in the existing table be
physically moved in order to maintain the records in sequential order.
The ratio between insertion/deletion and search time is very high.
This is the simple technique of searching an This is the efficient technique of searching
element an element
This technique does not require the list to be This technique require the list to be sorted.
sorted Then only this method is applicable
The worst case time complexity of this The worst case time complexity of this
technique is O(n) technique is O(log n)
Every element of the list may get compared Only the mid element of the list is compared
with the key element. with key element.
But this method is not convenient for performing multiplication of large integers . hence
let us discuss an interesting algorithm of multiplying large integer .
For example
To demonstrate the basic idea of the algorithm, let us start with a case of two-digit integers,
say, 23 and 14.
These numbers can be represented as follows:
2. 101 + 3 .100 and 14 = 1 . 101 + 4 . 100
Now let us multiply both the numbers
23 ∗ 14 = (2 . 101 + 3 . 100) * (1 . 101 + 4 . 100 )
= (2 ∗ 1) 102 + (2 ∗ 4 + 3 ∗ 1) 101 + (3 ∗ 4) 100
=2*100+ (8 +3)10 +(12) 1
=200+110+12
= 322
Let us formulate this method Let
c=a∗b
Analysis
In this method there are 3 multiplication operations 1 digit numbers
i.e c2 = a1 ∗ b1 --> multiplication 1
c0 = a0 ∗ b0 --> multiplication 2
c1 = (a1 + a0) ∗ (b1 + b0) − (c2 + c0)->multiplication 3
The multiplication of n digit numbers requires three multiplications of n/2 digit
numbers, the recurrence equation for the number of multiplication M(n) will be,
M(n)=3M(n/2), for n>1
And M(1)=1 where n = 1
Now put n=2k Solving it by backward substitutions for
M(2k)=3 M(2k /2)
M(2k)=3 M(2k-1)
=3[3 M(2k-2)]
=32 M(2k-3)
=3k M(2k-k)
=3k M(20) Since 20 = 1
k
=3
Using equation (3) , M(n) = 1
Therefore M(2k) = 3k ------ (4)
as n= 2k we get k = log2n ,equation (4)
M(n)= 3 log 2n
=n log 2 3 therefore a log b c = c log2 a
≈ n 1.585
M(n) ≈ n1.585
Where
m1 = (a00 + a11) * (b00 + b11)
m2 = (a10 + a11) * b00
m3 = a00 * (b01 - b11)
m4 = a11 * (b10 - b00)
m5 = (a00 + a01) * b11
m6 = (a10 + a00) * (b00 + b01)
m7 = (a01 + a11) * (b10 + b11)
The matrix A,B and their product is divided into 4,n/2 by n/2 submatrices each as
follows
The value C00 can be computed as either a00 * b00 or a01 * b10 or as M1 + M4 – M5 +
M7 , where M1 , M4 , M5 and M7 are found by strassen‘s formula with the numbers
replaced by the corresponding submatrices. The seven products of n/2 and n/2 matrices
are computed recursively by the strassen‘s matrix multiplication algorithm.
The convex – hull problem of finding the smallest convex polygon that contains given
n points in a plane can be solved using divide and conquer method.
This version of solving convex hull problem is called quick hull because this method is
based on quick sort technique.
Algorithm
Step 1 : Sort the points ( p1 ,p2 ,p3,…pn) by their x – coordinates
Step 2 : Repeatedly find the convex hull through p1 to p n/2
Step 3 : Repeatedly find the convex hull through p n/2+1 to p n
Step 4 : Merge the two convex hulls
points of a closer pair can lie on the opposite sides of the separating line.
Let S be the list of points inside the strip of width 2d around the separating line,
obtained from Q and hence ordered in non decreasing order of their y coordinate.
We will scan this list, updating the information about dmin, the minimum distance seen
so far, if we encounter a closer pair of points.
Initially, dmin = d, and subsequently dmin ≤ d. Let p(x, y) be a point on this list.
For a point p(x, y) to have a chance to be closer to p than dmin, the point must follow
p on list S and the difference between their y coordinates must be less than dmin.
ALGORITHM EfficientClosestPair(P, Q)
//Solves the closest-pair problem by divide-and-conquer
//Input: An array P of n ≥ 2 points in the Cartesian plane //sorted in non decreasing
order of their x coordinates and an //array Q of the same points sorted in non
decreasing order of //the y coordinates
//Output: Euclidean distance between the closest pair of //points
if n ≤ 3
return the minimal distance found by the brute-force algorithm
else
copy the first _n/2_ points of P to array Pl
copy the same _n/2_ points from Q to array Ql
copy the remaining _n/2_ points of P to array Pr
copy the same _n/2_ points from Q to array Qr
dl←EfficientClosestPair(Pl, Ql)
dr←EfficientClosestPair(Pr, Qr)
d ←min{dl, dr}
m←P[_n/2_ − 1].x
copy all the points of Q for which |x − m| < d into array S[0..num − 1]
dminsq ←d2
for i ←0 to num − 2 do
k←i + 1
while k ≤ num − 1 and (S[k].y − S[i].y)2 < dminsq
dminsq ←min((S[k].x − S[i].x)2+ (S[k].y − S[i].y)2, dminsq)
k←k + 1
return sqrt(dminsq)
The algorithm spends linear time both for dividing the problem into two problems half
the size and combining the obtained solutions. Therefore, assuming as usual that n
is a power of 2, we have the following recurrence for the running time of the
algorithm:T (n) = 2T (n/2) + f (n),where f (n) ∈ Ө(n). Applying the Master Theorem
(with a = 2, b = 2, and d = 1), we get T (n) ∈ Ө(n log n). The necessity to presort input
points does not change the overall efficiency class if sorting is done by a O(n log n)
algorithm such as merge sort.
Convex-Hull Problem
Let S be a set of n>1 points p1(x1, y1), . . . , pn (xn, yn) in the Cartesian plane.
We assume that the points are sorted in non decreasing order of their x coordinates,
with ties resolved by increasing order of the y coordinates of the points involved.
It is not difficult to prove the geometrically obvious fact that the leftmost point p1 and
the rightmost point pn are two distinct extreme points of the set‘s convex hull
Let p1 pn be the straight line through point‘s p1 and pn directed from p1to pn.
This line separates the points of S into two sets: S1 is the set of points to the left of this
line, and S2 is the set of points to the right of this line. We say that point q3 is to the
left of the line q1q2 directed from point q1 to point q2 if q1 q2 q3 forms a
counterclockwise cycle. Later, we cite an analytical way to check this
condition, based on checking the sign of a determinant formed by the
coordinates of the three points.
The points of S on the line p1 pn, other than p1 and pn, cannot be extreme points
of the convex hull and hence are excluded from further consideration.
If there is a tie, the point that maximizes the angle pmax p pn can be selected. (Note
that point pmax maximizes the area of the triangle with two vertices at p1and pn
and the third one at some other point of S1.)
Pmax
Pn
P1
Then the algorithm identifies all the points of set S1 that are to the left of the line p1
pmax; these are the points that will make up the set S1,The points of S1 to the left of
the line pmax pn will make up the set S1,It is not difficult to prove the following:
pmax is a vertex of the upper hull.The points inside p1 pmax pn cannot be vertices of
the upper hull (and hence can be eliminated from further consideration).There are no
points to the left of both lines p1 pmax and pmax pn.
Therefore, the algorithm can continue constructing the upper hulls of p1 U S1,1
Upmax and pmax U S1,2 U pn recursively and then simply concatenate them to get
the upper hull of the entire set p1 U S1 U pn.
If q1(x1, y1), q2(x2, y2), and q3(x3, y3) are three arbitrary points in the Cartesian plane,
then the area of the triangle q1q2q3 is equal to one-half of the magnitude of the
determinant
x1 y1 1
x2 y2 1 = x1 y2 + x3 y1 + x2 y3− x3 y2 − x2 y1− x1 y3
x3 y3 1
while the sign of this expression is positive if and only if the point q3 = (x3, y3) is to the
left of the line q1 q2.
Using this formula, we can check in constant time whether a point lies to the left of
the line determined by two other points as well as find the distance from the point
to the line.
Example 2:
The merge procedure requires finding a bridge between two hulls that are adjacent
to each other . concatenate left part of left hull and right part of right hull
Quick hull has the same Ө(n log n) worst-case efficiency as quick sort.
In the average case Ө(n2), however, we should expect a much better performance.
The average-case efficiency of quick hull turns out to be linear. ‗n‘ its computing time is
O(n2).
Unit - 3
Dynamic Programming And Greedy Technique
Part – A
1. Write the difference between Greedy method and Dynamic programming.[ CO3 –
L2 - May/June 2011]
2. Write an algorithm to find shortest path between all pairs of nodes. [ CO3
- L1 - May/June 2011]
– Find a feasible solution for the given instance for which the objective
function has an optimal value
– either maximum or minimum depending on the problem being solved.
A feasible solution satisfies the problem‘s constraints
The constraints specify the limitations on the required solutions.
20. Write control abstraction for the ordering paradigm. [ CO3 - L1 - May/June 2012]
Algorithm store (n, limit)
{
j = 0;
For( i ← 1 to n ) do
{
Write (―append program‖, i);
Write (―permutation for tap ―,j);
j = ( j+1) mod limit ;
}
}
.
PART B
1. Define dynamic programming and explain the problems that can be solved
using dynamic programming. [ CO3 – L2 – Nov/Dec 2011/2013]
Introduction:
Dynamic Programming is a technique for solving problems with overlapping sub
problems. The smaller sub problems are solved only once and recording the results in a
table from which the solution to the original problem is obtained.
Problems that can be solved using dynamic programming:
Various problems those can be solved using dynamic programming are
For computing nth Fibonacci number
1. Computing binomial coefficient
2. Warshall‘s algorithm
3. Floyd‘s algorithm
4. Optimal binary search trees
Principle of optimality:
The dynamic programming makes use of principle of optimality when finding solution to
given problem.
The principle of optimality states that ― in an optimal sequence of choices or decisions ,
each subsequence must also be optimal‖.
When it is not possible to apply principle of optimality, it is almost impossible to obtain
the solution using dynamic programming approach.
Example:
While constructing optimal binary search tree we always select the value of k which is
obtained from minimum cost. Thus it follows principle of optimality
Computing a Binomial Coefficient:
Computing a Binomial Coefficient is a typical example of applying dynamic
programming in mathematics, particularly in combinatory.
Binomial Coefficient is a Coefficient of any of the term in the expansion of (a+b) n.
The binomial coefficient is denoted by C(n, k) or ( )
0 1
1 1 1
2 1 2 1
3 1 3 3 1
4 1 4 6 4 1
5 1
K 1 1
N 1 C(n,k)
To compute the value of C(n,k), the table of figure is filled by row , starting with row 0
and ending with row n. Each row i(0≤ i ≤ n) is filled left to right, starting with 1 because
C(n,0)=1.Rows 0 through k also end with 1 on the table‘s main diagonal (ie)
C(i,i)=1for 0≤ i ≤ k
The other entries of the table is computed by using the formula C(n,k)=C(n-1,k-1)+C(n-
1,k), for n>k>0 adding the contents of the cells in the preceding row and the previous
column in the preceding row and the same column.
Algorithm
Algorithm Binomial(n,k)
//Computes C(n,k) by the dynamic programming algorithm
// Input: A pair of nonnegative integers n≥k≥0.
//Output: The value of C(n,k)
for i←0 to n do
for j ←0 to min(i,k) do
if j=0 or j=k
C[i,j] ← 1
else
C[i,j] ← C[i-1,j-1]+ C[i-1,j]
return C[n,k]
Analysis:
The basic operation is addition i.e.
C[ i,j] ← C[i – 1, j-1] +C[i- 1, j]
Let A(n,k) denotes total additions made in computing C(n,k).
In the table, first K+1 rows of the table form a triangle and the remaining n-k
rows form a rectangle.
So the recurrence relation for A(n,k) is divided into the parts.
The recurrence relation is,
Therefore ∑
= ∑ ∑
= [ 1 + 2 + 3 + ….( k-1)] + k ∑
= +k∑
= +k(n-(k + 1) + 1)
= +k(n-k - 1 + 1)
As no edge from a to d ,
value is 0
= +k(n-k )
Fig: Digraph
Edge from b to d
adjacency matrix.
Transitive closure : Transitive closure is basically a boolean matrix ( matrix with 0 and
1 values ) in which the existence of directed paths of arbitrary lengths between vertices
is mentioned.
transitive closure.
The transitive closure can be generated with Depth First Search (DFS) or with Breadh
First Search (BFS).
This traversing can be done on any vertex.
While computing transitive closure we have to start with some vertex and have to find
all the edges which are reachable to every other vertex . the reachable edges for all the
vertices has to obtained
Algorithm :
Algorithm Warshall ( matrix [1..n, 1..n]
//problem description : this algorithm is for computing
//transitive closure using warshall‘s algorithm
//input: the adjacency matrix given by matrix [ 1..n , 1..n]
//Output : the transitive closure of digraph
Analysis:
Clearly time complexity of above algorithm is Θ(n3) because in above algorithm the basic
operation is computation of R(k) [ i, j].
This operation is located within three nested for loops.
V A B C d
A 0 ∞ 3 ∞
B 2 0 ∞ ∞
C ∞ 7 0 1
D 6 ∞ ∞ 0
Weighted graph: the weighted graph is a graph in which weights or distances are given
along the edges. The weighted graph can be represented by weighted matrix as follows,
Here
w[i][j] = 0 if i=j
W[i][j] =∞ if there is no edge ( directed edge) between i
and j .
W[i][j] = weight of edge.
Formulation:
Let , Dk [i,j] denotes the weight of shortest path from vi to vj using {v1 , v2, v3…vk}
as intermediate vertices.
Initially D(k) is computed as weighted matrix
There exits two case –
1. A shortest path from vi to vj with intermediate vertices from {v1 , v2, v3…vk} that
does not use vk . in this case
Dk [i,j] = D(k-1)[i,j]
2. A shortest path from vi to vj restricted to using intermediate vertices {v1 , v2,
v3…vk} which uses vk. in this case-
Algorithm:
ALGORITHM Floyd(W[1..n, 1..n])
//Implements Floyd‘s algorithm for the all-pairs shortest-paths //problem
//Input: The weight matrix W of a graph with no negative-length //cycle
//Output: The distance matrix of the shortest paths‘ lengths
D ←W //is not necessary if W can be overwritten
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]}
}}}
return D
Analysis :
In the above given algorithm the basic operation is –
D[i, j ]←min{D[i, j ], D[i, k]+ D[k, j]}
This operation is within three nested for loops , we can write
C(n) = ∑ ∑
C(n) = ∑ ∑ therefore ∑
C(n) = ∑ ∑
C(n) = ∑ n2
C(n) = n3
The time complexity of finding all pair shortest path is Θ (n3)
5. Write the algorithm to compute the 0/1 knapsack problem using dynamic
programming and explain it. [ CO3 – L2 – Dec 2010]
Given n items of known weights W 1….. W n and values a knapsack of capacity W, find the
most valuable subset of the items that fit into the knapsack.
Assume that the weights and the knapsack capacity are positive integers.
The item values do not have to be integers.
To design a dynamic programming algorithm, it is necessary to derive a recurrence relation
that expresses a solution to an instance of the knapsack problem in terms of solutions to its
smaller sub instances.
The recurrence relation is, equation
Our goal is to find F[n,w], the maximum value of a subset of the n items that fit into the
knapsack of capacity w, and an optimal subset itself.
F[0,j] = 0 as well as F[i,0] = 0 when j≥0 and i≥0.
The table can be filled by either row by row or column by column.
0 j- W i j W
0 0 0 0
W i Vi i-1 0 F[i-1,j- W i] F[i-1,j]
i 0 F[i,j]
n 0 Goal
Example
For the given instance of problem obtain the optimal solution for the knapsack problem
Item Weight
Value
1 2 $3
2 3 $4
3 4 $5
4 5 $6
0 1 2 3 4 5
0 0 0 0 0 0
0
0
1
2 0
3
0
4
0
Let us start filling the table row by row using following formula:
0 0 0 0 0 0 0
1
0 0 3 3 3 3
2
3 0
4
0
0 0 0 0 0 0 0
1
0 0 3 3 3 3
2
3 4 4 7
3 0 0
4
0
0 0 3 4 5 7
F[i,k] = F[ i – 1, k]
F[ 4,5] = F[3,5] , Don‘t select i th item.
Now set i = i - 1, i = 3
Capacity -> 0 1 2 3 4 5
0 0 0 0 0 0 0
1
0 0 3 3 3 3
2
0 0 3 4 4 7
3
4 0 0 3 4 5 7
0 0 3 4 5 7
F[3,5] = F[2,5] Don‘t select ith item i.e ., 3 rd item . now set i = i-1, i=2
Capacity -> 0 1 2 3 4 5
0
0 0 0 0 0 0
1
0 0 3 3 3 3
2
3 0 0 3 4 4 7
4
0 0 3 4 5 7
0 0 3 4 5 7
F [i,k]≠ F[ i – 1, k] F[2,5] ≠ F[1,5] Select ith item, ie , select 2nd item.Set i = i -1 and k =
k- wi i.e . i = i -1, i=1 and k = 5 - 3 =2
Capacity -> 0 1 2 3 4 5
0 0
0 0 0 0 0
1
0 0 3 3 3 3
2
3 0 0 3 4 4 7
4 0 0 3 4 5 7
0 0 3 4 5 7
As F [i,k]≠ F[ i – 1, k]
i.e F[1,2] ≠ F[0,2]
select ith item
C(n) =
∑
C(n) =
∑
C(n) = W.
C(n) = W( n- 0 +) + ( n-0+1)
C(n) = W n +W + n + 1
C(n) = W n
The time complexity of this algorithm is Θ( nW)
return F[i, j ]
Example
Apply the dynamic programming following instance of the knapsack problems and
solve. The instance is
Item Weight Value
1 2 $12
2 1 $10
3 3 $20
4 2 $15
Capacity W=5
Solution :
Initially , F[0,j] = 0 and F[ i,0] = 0.
There are 0 to n rows and 0 to W columns in the table.
0 0 0 0 0 0
Now we will fill up the table either row by row or column by Colum. Let us start filling the
table row by row using following formula:
1 0 0 0 0 0 0
2
3 0 0 12 12 12 12
4
5 0
0 0 0 0 0 0
0 0 12 12 12 12
0 10 12 22 22 22
0 1 2 3 4 5
1 0 0 0 0 0 0
2
3 0 0 12 12 12 12
4
5
0 10 12 22 22 22
0 10 12 22 30 32
1 0 0 0 0 0 0
2
3 0 0 12 12 12 12
4
5 0 10 12 22 22 22
0 10 12 22 30 32
0 10 15 25 30 37
Therefore i = 4, k = 4
As F [i,k]= F[ I – 1, k]
i.e F[4,5] = F[3,5]
Select ith item i.e ., 4 th item .
Now set i = i-1 and k = k- wi
Therefore i=4-1= 3 and k = 5 – 2= 3
As F [i,k]= F[ I – 1, k]
i.e F[3,3] = F[2,3]
do not Select ith item i.e ., 3 rd item .
Now set i = i-1 and k = k- wi
Therefore i =3-1 = 2 and k = 5 – 2= 3
As F [i,k]≠ F[ I – 1, k]
i.e F[2,3] ≠ F[1,3]
Select ith item i.e ., 2 nd item .
Now set i = i-1 and k = k- wi
Therefore i =2-1 = 1 and k = 3 – 2= 2
As F [i,k]≠ F[ I – 1, k]
i.e F[1,2] ≠ F[0,2]
Select ith item i.e ., 1 st item .
Unit – 4
Iterative Improvement
Part - A
1. Describe feasible solution and feasible region? [CO4 – L2 - May 2012]
A solution that satisfies all the constraints of the problem is called the feasible
solution; the set of all such feasible points (solution) is called the feasible region.
Linear programming problems with the empty feasible region are called infeasible.
1.When the feasible region in linear programming becomes unbounded.
[ CO4 - L1 - May 2014]
If feasible region for the objective function may or may not attain a finite optimal
value, then such problems are called unbounded.
3.What are the requirements for the standard form of linear programming
problem? [ CO4 - L1]
The standard form has the following requirements:
It must be a maximization problem.
All the constraints (except the non negavity constraints) must be in the form of
linear equations with nonnegative right-hand sides.
All the variables must be required to be nonnegative.
maximize cx
subject to Ax = b
where x ≥ 0,
x1 a11 a12 a1n
b1
c = [c1 c2 cn], x =
, A =
x2
, b =
b2
am1 am2 amn
xn bm
5. What is basic and non basic solution? [ CO4 - L1]
If the system obtained has a unique solution—as any non degenerate system of
linear equations with the number of equations equal to the number of unknowns
does is called a basic solution; its coordinates set to zero before solving the system
are called nonbasic, and its coordinates obtained by solving the system are called
basic.
For example
Definition. A flow is a mapping f : E → R+, denoted by fuv or f (u, v), subject to the
following two constraints:
1. Capacity Constraint:
2. Conservation of Flows:
where s is the source of N. It represents the amount of flow passing from the source to
the sink.
Maximum Flow Problem. Maximize | f |, that is, to route as much flow as possible from
s to t.
Minimum cut
PART B
convert to
-4x+6y+z=0
The given equations is linear programming form-
-X+Y=12
X+Y=30 These are called constraints
2X +y=90
We will write these equations in the form
-c1x1-c2x2-c3x3-……………cnxn+(0)s1+(0)s2+….z=0
-X + Y + S1 = 12
X + Y + S2 = 30
2X + 5Y + S3 = 90
X Y S1 S2 S3 RHS
-1 1 1 0 0 12
1 1 0 1 0 30
2 5 0 0 1 90
-4 -6 0 0 0 0
Here the basic variables are s1,s2 and s3 whereas the non basic variable are
X and Y.
As the current solution (x, y, s1, s2, s3)=(0, 0,12, 30, 90) corresponds to z-
value of 0 …..(1)
x y S1 S2 S3 RHS
-1 1 1 0 0 12
1 1 0 1 0 30
2 5 0 0 1 90
Entering variable
-4 -6 0 0 0 0
To locate departing variable
we apply min(RHS/yi}
12/1=12 , 30/1= 30, 90/5=18
The minimum value is with y1 and s1.hence departing variable is
s1. pivot
x y S1 S2 S3 RHS
s1 Departing
-1 1 1 0 0 12
variable
1 1 0 1 0 30
s2
2 5 0 0 1 90
s3
-4 -6 0 0 0 0
Entering variable
Applying Gauss Jordan elimination method to obtain improved solution
x Y S1 S2 S3 RHS
-1 1 1 0 0 12
1 1 0 1 0 30
2 5 0 0 1 90
-4 -6 0 0 0 0
Will
make 0
by
gauss
Jordan
metho
x y S1 S2 S3 RHS
Replacing
y departing
-1 1 1 0 0 12
variable s1
s2 by y
2 0 -1 1 0 18
(i.e.entering
s3
7 0 -5 0 1 30 variable)
-10 0 6 0 0 72
x y S1 S2 S3 RHS
-1 1 1 0 0 12
2 0 -1 1 0 18 y
7 0 -5 0 1 30 s2
-10 0 6 0 0 72 s3
Entering variable
x y S1 S2 S3 RHS
-1 1 1 0 0 12
2 0 -1 1 0 18
-10 0 6 0 0 72
x y S1 S2 S3 RHS
-1 1 1 0 0 12
2 0 -1 1 0 18
1 0 - 0 1/7 30/7
5/7
-10 0 6 0 0 72
y
s2
s3
x y s1 s2 s3 RHS
-1 1 1 0 0 12
2 0 -1 1 0 18 -1 1 1 0 0 12
1 0 - 0 1/7 30/7 2 0 -1 1 0 18
5/7
1 0 - 0 1/7 30/7
-10 0 6 0 0 5/7
72
0 0 -8/7 0 10/7
804/7
Similarly row 1 and row2 can be simplified. Now the simplex table will be
x y s1 s2 s3 RHS
0 1 0 -2/3 1/3 10
1 0 0 5/3 -1/3 20 S1
x y s1 s2 s3 RHS
The option solution will be
(x,y,s1,s2,s3)= (20,10,66/3,0,0 )
Z=4x+6y =4(20)+6(10) =140 ………(4)
Thus we moved on with
(0,0) (0,12) (30/7,114/7) (20,10)
With z=0 z=72 z=115 z=140
The graph for representing feasible solution region will
feasible
region(20,10)
(0, 12)
10
5
(0,0)
5 10 15 20 25 (30, 0)
2. Explain and solve the maximum flow problem. [ CO4 – H1 - Dec 2015]
The maximum flow problem
Maximum flow is a maximum feasible flow between source and sink. Here the word
flow means the rate at material moves through underlying object.But when the material
moves through some object it is very much essential to consider its capacity.In
maximum flow problem , we want to compute the greatest rate at which material can be
moved or travelled from source to sink without violating any capacity constraint. There
are many algorithm that can find out the maximum flow from a given network. We will
discuss one efficient and simple algorithm named ford-flukerson for finding the
maximum flow.Let us now define some basic terminologies of flow networks.
Flow Network:
The flow network can be define by graph G=(V,E) which is a directed graph with
edge(u,v)€E. The edge (u,v) has non-negative capacity denoted by c(u,v)≥0iIf c(u,v)=0.
There are two distinguishing nodes in flow network called source and sink through
which the material moves. That means a flow is from source to sink.
There are three important properties of flow networks.
1.Capacity constraint: For all u,v€ V the flow of edge(u,v) is always less than or
equal to the capacity of that edge(u,v).i.e. f(u,v)≤c(u,v).
2.Skew symmetry : Forall u,v € v f(u,v)= -f(v,u), that means reverse edge.
3.Flow conservation: For any vertex v except s,t i.e. source and sink
respectively.
∑ f(u,v)=0
v€V
The value of flow from u to v can be defined as
│f│=∑f(s,v)
v€V
For example
3
7 5
8 3
s 2 t
2 4 6
1 3 4
3 F(u,v)
5/7 5/5 c(u,v)
3/3
7/8 t
s 2
4/4 5/6
2/3
1 4
3 F(u,v)
3/3
7/8 t
s 2
4/4 5/6
2/3
1 4
=7-5
Cf(u,v) =2
5 2 5
3
1
7 t
5 1
s 2 4/4
2 1
2
4
1 Fig: Residual graph
7/8 17/19
S
T
12 7 4/8
B C
Step 1:
o The augmenting path is marked by thick line.
o This is a path which gives maximum flow.
o Now we will find the residual capacity cf(p).
o Find out the minimum value along the residual path.
o Here it is 4 i.e. c(2,4).
o Now we will design a residual graph by considering residual
capacity 4.
7 5
t
s 2
8 3
2 2 4 4 6
3
Step2: here along the augmenting path the residual capacity is c(3,t) =5 . hence
we can draw residual network.
Step3: now we will again mark augmenting path for maximum flow.
Here cf=c(2,3) =3
Hence the residual graph is as follow:
This is the residual graph, we will now find the augmenting path giving maximum
flow from source to sink. Here the only remaining path is s-1-4-t.
The cf=(s,1)=2
Step 4:
Step 5: Now there is no path from s to t in following graph, hence we will exit the
while loop.
Analysis : The algorithm for Ford-Fulkerson has a while loop which executes for O
(E). Hence running time of Ford-Fulkerson algorithm is O(EF*) where F* is the
maximum flow found b algorithm.
3. Explain the Maximum Matching in Bipartite Graph algorithm with supporting
example [ CO4 – L2 – Apr/May 2015]
Bipartite Graph:
The graph G = (V, E) in which the vertex set V is divided into two disjoint sets X and Y in
such a way that every edge e € E has one end point in X and other end point in Y.
For example
Matching:
A matching M is a subset of edges such that each node in V appears in at most one
edge in M. In other words matching in a graph is a subset of edges that no two edges
share a vertex.
Two-colorable Graph:
A graph can be colored with only two colors (i.e. two colorable graph) such that no edge
connects the same color. The bi-partite graph is 2-colorable.
Free vertex:
µ € V is a fee vertex, if no edge from matching M os incident to v (that means if v is not
matched).
Alternating path:
The alternating path P is a path in graph G, such that for every pair of subsequent
edges one of them is matching pair M and other is not.
Augmenting path:
The augmenting path P is a path in graph G, such that it is an alternating path with
special property that its start and end vertices are free or unmatched.
Maximum matching or Maximum cardinality matching:
It is a matching with largest number of matching edges.
Here the vertices D, 3, E and 4 are matched and vertices A, 1, B, 2, c, 5 are free or
unmatched vertices.
By adding a pair (A, 2) for matching
We get larger matching Ma
Something to get larger matching, for inclusion of some pair, we may need removal of
existing pair. Such a matching adjustment is called augmentation.
Following figures illustrate this concept
Thus we have got maximum matching. This is also called as perfect matching
because all vertices of graph are matched.
Theorem:
A matching M is a maximum matching if and only if there exists no augmenting
path with respect to M.
Algorithm Maximum Bipartite Matching(G)
initialize set M of edges // can be the empty set
initialize queue Q with all the free vertices in V
while not Empty (Q) do
w < Front(Q)
if w ε V then
for every vertex u adjacent to w do // u must be in U
if u free then // initialize set M of edges // can be the empty set
initialize queue Q with all the free vertices in V
Step 2:
Step 5:
Solution:
Step 1: Step 2:
Step 3: Step 4:
Step 5: Step 6:
Step 7:
A marriage matching M is called stable if there is no blocking pair for it; otherwise, it‘s
called unstable.
The stable marriage problem is to find a stable marriage matching for men‘s and
women‘s given preferences.
An instance of the stable marriage problem can be specified either by two sets of
preference lists or by a ranking matrix, as in the example below.
men‘s preferences women‘s preferences
1st 2nd 3rd 1st 2nd 3rd
Bob: Lea Ann Sue Ann: Jim Tom Bob
Jim: Lea Sue Ann Lea: Tom Bob Jim
Tom: Sue Lea Ann Sue: Jim Tom Bob
ranking matrix
Ann Lea Sue
Freemen: Tom
Jim 3,1 1,3 2,1
Tom proposed To Sue Sue rejected
Tom 3,2 2,1 1,2
Freemen:
Tom Ann Lea Sue
Tom proposed to Lea
Lea replaced Bob with Tom Bob 2,3 1,2 3,3
5. Maximize p=2x+3y+z<=40
2x+y-z>=10
-y+z>=10
x>=0,y>=0,z>=0. [ CO4 – L3 - May/June 2015]
General maximization problems are linear programming problems in which you
are asked to maximize an objective function. It may be non-standard: one or more
of the constraints may be a constraint.
Rewrite the following LP problem as a system of linear equations.
Maximize p = 2x + 3y + z subject to
x + y + z 40
2x + y - z 10
- y + z 10
x 0, y 0, z 0
Use slack or surplus variables s, t and u respectively, and type all equations with the
variables in the order shown above
The first constraint is x + y + z 40.
To turn it into an equation, we must add a slack variable s to the left-hand side,
getting x + y + z + s = 40.
The next constraint is 2x + y - z 10,
and we must subtract the surplus variable t to the left-hand side, getting
2x + y - z - t = 10.
The last constraint is - y + z 10,
and we must subtract the surplus variable u to the left-hand side, getting
- y + z - u = 10.
Finally, the objective is p = 2x + 3y + z.
We must subtract 2x + 3y + z from both sides to get the desired equation:
-2x - 3y - z + p = 0.
Step 2: Set up the initial tableau.
Step 1 We convert the LP problem into a system of linear equations by putting in the
slack variables and rewriting the objective function:
x + y + z + s = 40
2z + y - z - t = 10
- y + z - u = 10
3y z + p = 0
2x
x y z s t u p Ans
s 1 1 1 1 0 0 0 40
t 2 1 -1 0 -1 0 0 10
u 0 -1 1 0 0 -1 0 10
p -2 -3 -1 0 0 0 1 0
x y z s t u p Ans
s 1 1 1 1 0 0 0 40
t 2 1 -1 0 -1 0 0 10
u 0 -1 1 0 0 -1 0 10
p -2 -3 -1 0 0 0 1 0
Reading across the first row (active variable s), we find s = 40/1 = 40.
Reading across the second row (active variable t), we find t = 10/(-1) = -10.
Reading across the third row (active variable u), we find u = 10/(-1) = -10.
Reading across the bottom row (active variable p), we find p = 0/1 = 0
Since all the other variables are inactive, their values are zero.
Notice that the values of the surplus variables t and u are currently negative. This is
not permitted, since all variables are required to be non-negative. This tells us that
the current basic solution (x, y, z) = (0, 0, 0) is not feasible, (it does not satisfy the
second and third constraints). We use asterisks to mark the rows corresponding to
those negative basic variables:
x y z s t u p Ans
s 1 1 1 1 0 0 0 40
*
2 1 -1 0 -1 0 0 10
t
*
0 -1 1 0 0 -1 0 10
u
p -2 -3 -1 0 0 0 1 0
Our first order of business is to get into the feasible region, or, equivalently,
Phase I: Get rid of the stars.
We can (eventually) get rid of all the stars by pivoting one or more times. The only way
this differs from the procedure for pivoting in standard maximization
problems is the way in which we select the pivot column.
For standard maximization problems, the pivot column was chosen by selecting
the most negative number in the bottom row.
In Phase I, on the other hand, the pivot column is chosen by selecting the largest
positive number in the first starred row.
x y z s t u p Ans
10/2=5
*t 2 1 -1 0 -1 0 0 10 (Smallest).
*u 0 -1 1 0 0 -1 0 10
p -2 -3 -1 0 0 0 1 0
x y z s t u p Ans
s 0 1 3 2 1 0 0 70
X 2 1 -1 0 -1 0 0 10
*u 0 -1 1 0 0 -1 0 10
p 0 -2 -2 0 -1 0 1 10
x y z s t u p Ans
Test Ratio:
s 0 1 3 2 1 0 0 70
70/3
X 2 1 -1 0 -1 0 0 10
Test Ratio:
*u 0 -1 1 0 0 -1 0 10 10/1
p 0 -2 -2 0 -1 0 1 10
Since the smallest test ratio is in the u-row (Row 3), we select the entry in Row 3
Column 3 as pivot. Using the correct pivot, now obtain the second tableau.
x y z s t u p Ans
s 0 1 3 2 1 0 0 70 R1-3R3
X 2 1 -1 0 -1 0 0 10 R2+3R3
*u 0 -1 1 0 0 -1 0 10
p 0 -2 -2 0 -1 0 1 10 R4+2R3
x y z s t u p Ans
s 0 4 0 2 1 3 0 40
X 2 0 0 0 -1 -1 0 20
Z 0 -1 1 0 0 -1 0 10
p 0 -4 0 0 -1 -2 1 30
x y z s t u p Ans
Y 0 4 0 2 1 3 0 40
X 2 0 0 0 -1 -1 0 20
Z 0 0 4 2 1 -1 0 80
p 0 0 0 0 0 1 1 70
Unit – 5
Part - A
A decision tree is a decision support tool that uses a tree-like graph or model of
decisions and their possible consequences, including chance event outcomes, resource
costs, and utility. It is one way to display an algorithm.Decision trees are commonly
used in operations research, specifically in decision analysis, to help identify a strategy
most likely to reach a goal.
15. State the property of NP-Complete problem. [ CO4 - L1 - May 2010/Dec 2013]
A problem L is completer if and only if L is NP-hard and L € NP
16. What are the factors that influence the efficiency of the backtracking
algorithm? [ CO4 - L1 - May/June 2015]
The efficiency of the backtracking algorithm depends on the following four
factors. They are:
The time needed to generate the next xk
The number of xk satisfying the explicit constraints.
The time for the bounding functions Bk
The number of xk satisfying the Bk.
Given n distinct positive numbers usually called as weights, the problem calls for
finding all the combinations of these numbers whose sums are m.
21. Give the categories of the problem in backtracking. [ CO4 - L1 - Nov/Dec 2013]
Decision‘s problem: - Whether there is any feasible solution.
Optimization problem: - Whether there exists any best solution.
Enumeration problem: - Finds all possible feasible solution.
23. Define promising and non promising node. [ CO4 - L1 - Nov/Dec 2013]
Promising Node
A node in a state space tree is said to be promising if it corresponds to a partially
constructed solution that may still lead to a complete solution.
Non – Promising node
A node in a state space tree is said to be non-promising if it corresponds to a partially
constructed solution that would not be able to lead to a complete solution further.
27. How can you represent the solution for 2 queen‟s problem? [ CO4 - L1 -
May/June 2012]
There is no solution for 2 Queen‘s problem since however the queens are arranged
both queens would be in same diagonal or column.
28. How can you represent the solution for 8 queen‟s problem? [ CO4 - L1 -
May/June 2014]
All solutions represented as 8-tuples (x1, x2,…, x8) where xi is the column on which
queen ―i‖ is placed.
Constraints are,
Explicit constraints
Si = {1, 2, 3, 4, 5, 6, 7, 8}
Implicit constraints
No two xi‗s can be the same column or row.
No two queens can be on the same diagonal.
PART-B
This lower bound is tight because both the right-to-left evaluation algorithm and
Horner‘s rule are both linear.
Similarly for ‗ product of two n × n matrices algorithm‘ the trivial lower bound is Ω(n2)
because in this algorithm2 n2 elements ( input) are to be processed and n2 element
get produced as an output.
Problem in this method
Trivial lower bounds are often too low to be useful. For example, the trivial bound for the
traveling salesman problem is Ω(n2), because its input is n(n − 1)/2 intercity distances and
its output is a list of n + 1 cities making up an optimal tour.
But this bound is all but useless because there is no known algorithm with the
running time being a polynomial function of any degree.
There is another obstacle to deriving a meaningful lower bound by this method. It lies
in determining which part of an input must be processed by any algorithm solving the
problem in question.
For example, searching for an element of a given value in a sorted array does not
require processing all its elements.
Information-Theoretic Arguments
This method is a lower bound based on the amount of information it has to produce by
algorithm.
For example: Consider a game of guessing number ―, first of all think of some number
from 1 to n. find answer by asking questions. Answers can be either yes or no. Let us
encode number by [log2 n], bits.
Each answer will give information about each bit for instance.
Is first bit zero ? --> No = 1
Is Second bit zero ? --> yes = 0
Is third bit zero ? --> yes = 0 => the numbers is 8
(encoded in binary
form)
Is forth bit zero ? --> yes = 0
Consequently, any such algorithm will need at least [log2 n] such steps before it can
determine its output in the worst case.
The approach we just exploited is called the information-theoretic argument because of its
connection to information theory.
It has proved to be quite useful for finding the so-called information-theoretic lower
bounds for many problems involving comparisons, including sorting and searching.
Adversary Arguments
Adversary argument is a method of proving lower bound by playing a role of adversary
in which algorithm has to work more for adjusting input consistently.
For example: consider a ― game of guessing a number ― between 1 and n with yes / no
questions.
Adversary:
When adversary gives the answer one can get a larger set of number from which a
secret number has to be found out.
This algorithm needs [log2 n] iterations to reduce the set of n elements to single
element. That also implies that [log2 n] questions in worst case.
This example illustrates the adversary method for establishing lower bounds.
A lower bound is obtained by measuring the amount of work needed to shrink a set of
potential inputs to a single input along the most time-consuming path.
Example, Consider the problem of merging two sorted lists of size n
a1< a2 < . . . < an and b1< b2 < . . . < bn
into a single sorted list of size 2n. For simplicity, we assume that all the a‘s and b‘s are
distinct, which gives the problem a unique solution.
The number of key comparisons in the worst case for this algorithm for merging is 2n −
1.
Problem Reduction
Getting an algorithm for problem P by reducing it to another problem Q solvable with a
known algorithm.
A similar reduction idea can be used for finding a lower bound. To show that problem P
is at least as hard as another problem Q with a known lower bound, we need to reduce
Q to P (not P to Q!).
x.y=
and
x2 = x . x
Show that the problems of computing the product of two n-digit integers and
squaring an n-digit integer belong to the same complexity class, despite the latter
being seemingly simpler than the former.
There are several similar results for matrix operations. For example, multiplying two
symmetric matrices turns out to be in the same complexity class as multiplying two
arbitrary square matrices. This result is based on the observation that not only is the
former problem a special case of the latter one, but also that we can reduce the
problem of multiplying two arbitrary square matrices of order n, say, A and B, to the
problem of multiplying two symmetric matrices
0
X=[ ]
0
and
0
y=[ ]
0
where AT and BT are the transpose matrices of A and B (i.e., AT [i, j ]= A[j, i]and BT [i, j
]= B[j, i]), respectively, and 0 stands for the n × n matrix whose elements are all zeros.
0 0
Indeed,X Y= [ ] [ ]
0 0
0
=[ ]
0
from which the needed product AB can be easily extracted.
Hence, the number of comparisons in the worst case is equal to the height of the
algorithm‘s decision tree.The central idea behind this model lies in the observation that
a tree with a given number of leaves, which is dictated by the number of possible
outcomes, has to be tall enough to have that many leaves. Specifically, it is not difficult
to prove that for any binary tree with l leaves and height h,
h ≥ [log2 l]. ---eqn 1
Indeed, a binary tree of height h with the largest number of leaves has all its leaves on
the last level. Hence, the largest number of leaves in such a tree is 2h.
In other words , 2h ≥ l, which immediately implies (eqn 1).
Inequality eqn 1 puts a lower bound on the heights of binary decision trees and hence
the worst-case number of comparisons made by any comparison-based algorithm for
the problem in question. Such a bound is called the information theoretic lower bound .
We illustrate this technique below on two important problems: sorting and searching in a
sorted array.
Inequality above fig.(a) implies that the height of a binary decision tree for any
comparison-based sorting algorithm and hence the worst-case number of comparisons
made by such an algorithm cannot be less than [log2 n!]:
C worst (n) ≥ [log2 n!]
Using Stirling‘s formula for n!, we get
[ log n!] = ( log 1) + ( log 2) + …+ (log n)
[log2 n!] ≈ log2 √2πn(n/e)n
,= n log2 n − n log2 e +(log2 n/2)+(log2 2π/2)
≈ n log2 n.
Fig (b) Decision tree for the tree-element selection sort. A triple above a node
indicates the state of the array being sorted. Notes two redundant comparisons
b<a with a single possible outcome because of the results of some previously
made comparisons.
In other words, about n log2 n comparisons are necessary in the worst case to sort
an arbitrary n-element list by any comparison-based sorting algorithm. Note that merge
sort makes about this number of comparisons in its worst case and hence is
asymptotically optimal.
This also implies that the asymptotic lower bound n log2 n is tight and therefore cannot
be substantially improved. We should point out, however, that the lower bound of [log 2
n!] can be improved for some values of n.
Average-case efficiency can be
C avg (n) ≥ log2 n!. )
As we saw earlier, this lower bound is about n log2 n. You might be surprised that the
lower bounds for the average and worst cases are almost identical. Remember,
however, that these bounds are obtained by maximizing the number of comparisons
made in the average and worst cases, respectively. For a particular sorting algorithm,
the average-case efficiency can, of course, be significantly better than their worst-case
efficiency.
For example, for We will use decision trees to determine whether this is the smallest
possible number of comparisons.Since we are dealing here with three-way comparisons
in which search keyK is compared with some element A[i] to see whether K <A[i],K =
A[i], orK >A[i], it is natural to try using ternary decision trees. Figure (d) presents such a
tree for the case of n = 4.The internal nodes of that tree indicate the array‘s elements
being compared with the search key. The leaves indicate either a matching element in
the case of a successful search or a found interval that the search key belongs to in the
case of an unsuccessful search.
We can represent any algorithm for searching a sorted array by three-way comparisons
with a ternary decision tree similar to that in Figure 11.4. For an array of n elements, all
such decision trees will have 2n + 1 leaves (n for successful searches and n + 1 for
unsuccessful ones). Since the minimum height h of a ternary tree with l leaves is [log3 l],
we get the following lower bound on the number of worst-case comparisons:
Cworst(n) ≥ [log3(2n + 1]
This lower bound is smaller than [log2(n + 1)], the number of worst-case comparisons
for binary search, at least for large values of n (and smaller than or equal to [log2(n +
1)]for every positive integer n—see Problem 7 in this section‘s exercises). Can we prove
a better lower bound, or is binary search far from being optimal? The answer turns out
to be the former. To obtain a better lower bound, we should consider binary rather than
ternary decision trees, such as the one in Figure (e). Internal nodes in such a tree
correspond to the same three way comparisons as before, but they also serve as
terminal nodes for successful searches. Leaves therefore represent only unsuccessful
searches, and there are n + 1 of them for searching an n-element array
Fig (d) Ternary decision tree for binary search in a four-element array
As comparison of the decision trees in Figures (d) and (e) illustrates, the binary decision
tree is simply the ternary decision tree with all the middle subtrees eliminated. Applying
inequality fig (a) to such binary decision trees immediately yields
Cworst(n) ≥ [log2(n + 1)]. Total number of leaf nodes
Fig (e) Binary decision tree for binary search in a four-element array
This inequality closes the gap between the lower bound and the number of worstcase
comparisons made by binary search, which is also [log2(n + 1)]. A much more
sophisticated analysis shows that under the standard assumptions about searches,
binary search makes the smallest number of comparisons on the average, as well. The
average number of comparisons made by this algorithm turns out to be about log 2 n − 1
and log2(n + 1) for successful and unsuccessful searches, respectively.
3.Explain the P, NP, and NP Complete problems with suitable example[ CO4 –
L2 – Dec 2013]
Computational complexity
problems
The problem in question is called the halting problem: given a computer program and an
input to it, determine whether the program will halt on that input or continue working
indefinitely on it.
Proof:Assume that A is an algorithm that solves the halting problem. That is, for any
program P and input I,
can consider program P as an input to itself and use the output of algorithm A for pair
(P, P) to construct a program Q as follows:
This is a contradiction because neither of the two outcomes for program Q ispossible,
which completes the proof.There are many important problems, however, for which no
polynomial-time algorithm has been found, Such problems are Hamiltonian circuit
problem.Determine whether a given graph has a Hamiltonian circuit—a path that starts
and ends at the same vertex and passes through all the other vertices exactly once.
Traveling salesman problem:Find the shortest tour through n cities with known positive
integer distances between them (find the shortest Hamiltonian circuit in a complete
graph with positive integer weights).
Knapsack problem:Find the most valuable subset of n items of given positive integer
weights and values that fit into a knapsack of a given positive integer capacity.
Graph-coloring problem:For a given graph, find its chromatic number, which is the
smallest number of colors that need to be assigned to the graph‘s vertices so that no
two adjacent vertices are assigned the same color.
Integer linear programming problem:Find the maximum (or minimum) value of a linear
function of several integer-valued variables subject to a finite set of constraints in the
form of linear equalities and inequalities.
Nondeterministic algorithm
A nondeterministic algorithm is a two-stage procedure that takes as its input an instance
I of a decision problem and does the following.
Nondeterministic (―guessing‖) stage: An arbitrary string S is generated that can be
thought of as a candidate solution to the given instance I (but may be complete
gibberish as well).
Class of NP problems
Class NP is the class of decision problems that can be solved by nondeterministic
polynomial algorithms. This class of problems is called nondeterministic polynomialMost
decision problems are in NP. First of all, this class includes all the problems in P:
This approach makes it possible to solve many large instances of NP hard problems in
an acceptable amount of time.The techniques branch and bound and backtracking are
base on the construction of a state space tree.A state space tree is a rooted tree whose
nodes represent partially constructed solutions to the problems.
Backtracking
Backtracking is a more intelligent variation of this approach.
The principal idea is to construct solutions one component at a time and evaluate such
partially constructed candidates as follows If a partially constructed solution can be
developed further without violating the problem‘s constraints, it is done by taking the first
remaining legitimate option for the next component. If there is no legitimate option for
the next component, no alternatives for any remaining component need to be
considered. In this case, the algorithm backtracks to replace the last component of the
partially constructed solution with its next option.
It is convenient to implement this kind of processing by constructing a tree of choices
being made, called the state-space tree.
Its root represents an initial state before the search for a solution begins.The nodes of
the first level in the tree represent the choices made for the first component of a
solution; the nodes of the second level represent the choices for the second component,
and so on.
If the current node turns out to be nonpromising, the algorithm backtracks to the nodes
parent to consider the next possible option for its last component.If there is no such
option, it backtracks one more level up the tree, and so on. Finally, if the algorithm
reaches a complete solution to the problem, it either stops (if just one solution is
required) or continues searching for other possible solutions.Backtracking problems
require that all the solutions satisfy a complex set of constraints.
Two types of constraints are
(i) Explicit constraint
(ii) Implicit constraint
Backtracking technique is applied to
(i) N-Queens problem
(ii) Hamiltonian circuit problem
(iii) Subset sum problem
1 2 3 4
1 Q
2 Q
3
4
Step 4:This proves to be a dead end because there is no acceptable position for queen
3. So, the algorithm backtracks and puts queen 2 in the next possible position at (2, 4).
1 2 3 4
1 Q
2 Q
3
4
Step 5:Now queen 3 is placed at position (3,2), which is acceptable position. Now the
chessboard is
1 2 3 4
1 Q
2 Q
3 Q
4
Step 6:Then queen 3 is placed at (3, 2), which proves to be another dead end. The
algorithm then backtracks all the way to queen 1 and moves the queen 1 from (1,1) to
(1, 2).
1 2 3 4
1 Q
2
3
4
Step 7:Queen 2 then goes to (2, 4)
1 2 3 4
1 Q
2 Q
3
Step 8:Now queen 3 is placed at the position (3, 1), which is acceptable position. Now
the board becomes
1 2 3 4
1 Q
2 Q
3 Q
4
Step 9:Finally the queen 4 to (4, 3), which is a solution to the problem, which is e
required solution to the problem. Now the board for four queens is
1 2 3 4
1 Q
2 Q
3 Q
4 Q
The state-space tree of this search is shown in Figure. If other solutions need to be
found (how many of them are there for the four queens problem?), the algorithm can
simply resume its operations at the leaf at which it stopped. Alternatively, we can use
the board‘s symmetry for this purpose.
Finally, it should be pointed out that a single solution to the n-queens problem for any n
≥ 4 can be found in linear time. In fact, over the last 150 years mathematicians have
discovered several alternative formulas for non attacking positions of n queens. Such
positions can also be found by applying some general algorithm design strategies.
8-Queens problem
Explanation:
• Place(k, i) returns a Boolean value that is true if the kth queen can be placed
in column i. it tests both whether i is distinct from all previous values
x[1],…..x[k-1] and whether there is no other queen on the same diagonal.
• Its computing time is O(k-1)
All solutions to the n-queens problem:
Algorithm Place(k,i)
// This algorithm returns true if a queen can be placed in kth row and ith column.
Otherwise, it returns false.
// x[] is a global array whose first(k-1) values have been set.
// Abs(r) returns the absolute value of r.
{
for j = 1 to k-1 do
if ((x[j] = i) // Checks whether two Queens are in the same column
or (Abs(x[j] - i) = Abs(j - k))) // Checks whether they are in the same diagonal
then
return false;
return true;
}
Algorithm NQueens(k, n)
// Using backtracking, this procedure prints all
//possible placements of n queens on an n x n
// chessboard so that they are non attacking.
{
for i:=1 to n do
{
if Place(k, i) then
{
x[k]:=i;
if (k==n) then write (x[1:n]);
else NQueens(k+1, n);
}
}
}
7. Using backtracking enumerate how can you solve the following problem
Hamiltonian Circuit Problem [ CO4 – H3 – Dec 2008/2009/2010/2011/May 2014]
Definition
Given an undirected connected graph and two graph and two nodes x and y then find a
path from x to y visiting each node in the graph exactly once V1, V2…… Vn and the Vi
are distinct except for V1, and Vn+1, which are equal. The next example let us consider
the problem of finding a Hamiltonian circuit in the graph in Figure.
Without loss of generality, we can assume that if a Hamiltonian circuit exists, it starts at
vertex a. Accordingly, we make vertex a the root of the state-space tree .If solution exist
for a Hamiltonian circuit problem, the first component of our future solution, if it exists, is
a first intermediate vertex of a Hamiltonian circuit to be constructed.
Using the alphabet order to break the three-way tie among the vertices adjacent to a,
we select vertex b. From b, the algorithm proceeds to c, then to d, then to e, and finally
to f, which proves to be a dead end.So the algorithm backtracks from f to e, then to d,
and then to c, which provides the first alternative for the algorithm to pursue.Going from
c to e eventually proves useless, and the algorithm has to backtrack from e to c and
then to b. From there, it goes to the vertices f , e, c, and d, from which it can legitimately
return to a, yielding the Hamiltonian circuit a, b, f , e, c, d, a. Hence the solution is
obtained. If we wanted to find another Hamiltonian circuit, we could continue this
process by backtracking from the leaf of the solution found.
FIGURE (b) State-space tree for finding a Hamiltonian circuit. The numbers above the
nodes of the tree indicate the order in which the nodes are generated.
Example 2:Definition
Given an undirected connected graph and two graph and two nodes x and y then find a
path from x to y visiting each node in the graph exactly once
Then the Hamiltonian cycle A-B-D-E-C-F-A. this problem can be solved using
backtracking approach. The state space tree is generated in order to find all the
Hamiltonian cycle in the graph.
Only distinct cycles are output of this algorithm. The Hamiltonian cycle can be identified
as follows fig (b).
fig (b) clearly the backtrack approach is adopted. For instance A-B –D- F- C- E; here we
get stuck. For returning to A we have to revisit atleast one vertex. Hence we
backtracked and from D node another path is chosen A- B- D- E-C-F-A which is
Hamiltonian cycle.
A B
D
C
E
Fig (a) Graph G
else
Hamiltonian(k+1);
} until(false);
}
Algorithm NextValue(k)
// This algorithm generates a next vertex
// x[1..k-1] is a path of k-1 distinct vertices
// if x[k]=0, then no vertex has as yet been assigned to x[k].
// After execution, x[k] is assigned to the next highest numbered vertex which
does not already appear in x[1..k-1] and is connected by an edge to x[k-1]
// Otherwise x[k]=0. If k=n, then in addition x[k] is connected to x[1]
{
repeat
{
x[k] = (x[k] + 1) mod (n + 1); //next vertex
if(x[k]=0) then
return;
if(G[x[k-1], x[k]] 0) then
{
//is there an edge?
for j =1 to k-1 do
if(x[j] = x[k]) then
break;
//check for distinctness
if(j = k) then //if true, then the vertex is distinct
if((k < n) or ((k = n) and G[x[n], x[1] 0))
then return;
}
}until(false)
}
The algorithm is started by first initializing the adjacency matrix G[1..n.1..n], setting
x[2..n] to zero and x[1] to 1 and then executing Hamiltonian(2).
3. Before finding the subset of a given set, the set‘s elements are sorted in
increasing order. So, we will assume that
a1≤a2 ≤ . . . ≤ an.
For subset sun problem, the state space tree is constructed as a binary tree
which is shown in the figure below.
{
// Generate left child.
x[k] =1; The subset is printed
if (s + w[k] = d) then
print x[1..k] // subset found
// There is no recursive call here as w[j] > 0, 1 j n.
else if(s + w[k] +w[k+1] d)
then SumOfSub(s + w[k], k+1, r-w[k]); Search the next element
//Generate right child which can make sum ≤ d
if (( s + r – w[k] d ) and ( s + w[k+1]] d)) then
{
x[k] = 0;
SumOfSub( s, k+1, r–w[k] );
}
}
n
The initial call to the algorithm is SumOf Sub(0,1, w i ).
in
Example 2:
A = {3, 5, 6, 7} and d = 15
A = {3, 5, 6, 7} and d = 15
The state-space tree can be constructed as a binary tree like that in Figure for
the instance A = {3, 5, 6, 7} and d = 15.
The root of the tree represents the starting point, with no decisions about the
given elements made as yet.
Its left and right children represent, respectively, inclusion and exclusion of a1 in
a set being sought. Similarly, going to the left from a node of the first level
corresponds to inclusion of a2 while going to the right corresponds to its
exclusion, and so on.
Thus, a path from the root to a node on the ith level of the tree indicates which of
the first i numbers have been included in the subsets represented by that node. We
record the value of s, the sum of these numbers, in the node.
9. Draw the tree representation to solve the subset sum problem given the
numbers set as A = {3, 5, 6, 7 , 2} and with sum = 15 Derive all the subsets.
[ CO4 – H3 – Nov/Dec 2010]
Solution:
The inequality below a leaf indicates the reason for its termination.
A = {3, 5, 6, 7 , 2} and sum = 15
The state-space tree can be constructed as a binary tree like that in Figure for the
instance A = {3, 5, 6, 7 , 2} and sum = 15.
Example 4.
Let, w = { 5,7,10,12,15,18,20} and m = 35. Find all possible subset of w whose sum
is equivalent to m. draw the portion of state space tree for this problem. Au : Dec
– 12
Solution:
10.Explain the Assignment Problem by the branch and bound algorithm with an
example[ CO4 – L2 - May/June 2015]
The branch-and-bound approach by applying it to the problem of assigning n people to
n jobs so that the total cost of the assignment is as small as possible.
An instance of the assignment problem is specified by an n × n cost matrix C so that we
can state the problem as follows:
select one element in each row of the matrix so that no two selected elements are in the
same column and their sum is the smallest possible.
We will demonstrate how this problem can be solved using the branch-and-bound
technique by considering the same small instance of the problem A small instance of
this problem with the table‘s entries representing the assignment C[i,j].
Start
Lower = 2 +3+1+4
Bound (lB)=10
a-> 1
LB = 9+3+1+4 =17
6 4 3 7
5 8 1 8
7 6 9 4
Start
Lower = 2 +3+1+4
Bound (lB)=10
a-> 2
LB = 2 +3+1+4=10
a-> 3
LB = 7 +4+5+4=20
Start
Lower = 2 +3+1+4
Bound (lB)=10
a-> 4
LB = 8+3+1+6=18
Start
LB = 10
9 2 7 8
9 2 7 8
6 4 3 7
6 4 3 7
5 8 8
1 5 8 8
7 6 9 1
4 Minimum select 6 9 4
7
from reaming rows
Do not select remaining
9 2 7 8 elements from these columns of c
6 4 3 7 and d
8 1 8
5
Do not select remaining 7 6 9
elements from these columns 4
Do not select remaining
elements from these columns
fig :Levels 0, 1, and 2 of the state-space tree for the instance of the
assignmentProblem being solved with the best-first branch-and-bound algorithm
it is a solution because
Fig Complete state-space tree for the instance of the assignment problem solved
with the best-first branch-and-bound algorithm.
11.Solve the following instance of the knapsack problem by the branch and
bound[ CO4 – H3 – Nov/Dec 2006/2008/2010]
Knapsack Problem
The branch-and-bound technique is used to solving the knapsack problem.The
Knapsack problem is given n items of known weights wi and values vi , i = 1, 2, . . . , n,
and a knapsack of capacity W, find the most valuable subset of the items that fit in the
knapsack.It is convenient to order the items of a given instance in descending order by
their value-to-weight ratios.Then the first item gives the best payoff per weight unit and
the last one gives the worst payoff per weight unit, with ties resolved arbitrarily:
Each node on the ith level of this tree, 0 ≤ i ≤ n, represents all the subsets of n items that
include a particular selection made from the first i ordered items.This particular selection
is uniquely determined by the path from the root to the node.
A branch going to the left indicates the inclusion of the next item
A branch going to the right indicates its exclusion.
We record the total weight w and the total value v of this selection in the node, along
with some upper bound ub on the value of any subset that can be obtained by adding
zero or more items to this selection. A simple way to compute the upper bound ub is to
add to v, the total value of the items already selected, the product of the remaining
capacity of the knapsack W − w and the best per unit payoff among the remaining
items, which is vi+1/wi+1:
ub = v + (W − w)(vi+1/wi+1)
We will first compute the upper bond by using above given formula
ub = v + (W − w)(vi+1/wi+1)
Initially v=0 , w=0 and vi +1 = v1 = 40 and w i+1 = wi+1 = w1 = 4. The capacity W = 10
Ub = 0 +(10-0)(40/4)
= (10) (10)
Ub = 100$
Now we will construct a state space tree by selecting different items
Computation at node 0
i,e . root of state space tree
Initially v=0 , w=0 and vi +1 = v1 = 40 and w i+1 = wi+1 = w1 = 4. The capacity W = 10
Ub = 0 +(10-0)(40/4)
= (10) (10)
Ub = 100$
Computation at node I
At node I in state space tree we assume the selection of item 1.
Therefore v= 40 , w = 4
The capacity W = 10
Now vi +1/ w i+1 -> means next item to item 1
i.e v2/w2 = 6
ub = v + (W − w)(vi+1/wi+
= 40 +(10-4)*
= 40 + 6 *6
ub =76
Computation at node II
At node II in state space tree we assume the selection of item 1. Not selected
Therefore v= 0 , w = 0
The capacity W = 10
Next to item 1 is item 2
Now vi +1/ w i+1 -> means 2
i.e v2/w2 = 6
ub = v + (W − w)(vi+1/wi+1)
= 0 +(10-0)*6
= 40 + 6 *6
ub =60
The capacity W = 10
The next item would be vi +1/ w i+1 -> item 4
Therefore v4/w4 = 12/3 =4
ub = v + (W − w)(vi+1/wi+1)
= 65 +(10-9)*4
= 65 + 1 *4 ub =69
Computation at node VI
At node VI is an instant at which item 1 is selected , item 2 and item 3 are not selected .
Therefore v= 40 , w = 4
The capacity W = 10
The Next item being selected is item 4
Now vi +1/ w i+1 = v4/w4= 12/3 =4
i.e v4/w4 = 4
ub = v + (W − w)(vi+1/wi+
=4 0 +(10-4)*4
= 40 + 6 *4 ub =64
W = 10
ub = v + (W − w)(vi+1/wi+1)= 65 + (10 -9)* 0 ub = 65
The node IX is a node indicating maximum profit of selected items with maximum
weight of item = 9 i.e . <capacity of knapsack ( W=10)
Thus solution is pick up { item 1, item3 } and gain maximum profit 65$