Anda di halaman 1dari 82

101401 DESIGN AND ANALYSIS OF ALGORITHMS 3104

UNIT I 9
Algorithm Analysis Time Space Tradeoff Asymptotic Notations Conditional asymptotic
notation Removing condition from the conditional asymptotic notation - Properties of big-
Oh notation Recurrence equations Solving recurrence equations Analysis of linear
search.
UNIT II 9
Divide and Conquer: General Method Binary Search Finding Maximum and Minimum
Merge Sort Greedy Algorithms: General Method Container Loading Knapsack
Problem.

UNIT III 9
Dynamic Programming: General Method Multistage Graphs All-Pair shortest paths
Optimal binary search trees 0/1 Knapsack Travelling salesperson problem .

UNIT IV 9
Backtracking: General Method 8 Queens problem sum of subsets graph coloring
Hamiltonian problem knapsack problem.

UNIT V 9
Graph Traversals Connected Components Spanning Trees Biconnected components
Branch and Bound: General Methods (FIFO & LC) 0/1 Knapsack problem Introduction
to NP-Hard and NP-Completeness.

TUTORIAL = 15 Total = 60

TEXT BOOK:
1. Ellis Horowitz, Sartaj Sahni and Sanguthevar Rajasekaran, Computer Algorithms/
C++, Second Edition, Universities Press, 2007. (For Units II to V)
2. K.S. Easwarakumar, Object Oriented Data Structures using C++, Vikas Publishing
House pvt. Ltd., 2000 (For Unit I)

REFERENCES:
1. T. H. Cormen, C. E. Leiserson, R.L.Rivest, and C. Stein, "Introduction to Algorithms",
Second Edition, Prentice Hall of India Pvt. Ltd, 2003.
2. Alfred V. Aho, John E. Hopcroft and Jeffrey D. Ullman, "The Design and Analysis of
Computer Algorithms", Pearson Education, 1999.

1
DESIGN AND ANALYSIS OF ALGORITHM
UNIT I
ALGORITHM ANALYSIS
Algorithm
An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for
Obtaining a required output for any legitimate input in a finite amount of time.

Characteristics of an Algorithm
More precisely, an algorithm is a method or process to solve a problem satisfying the
following properties:

Finiteness
terminates after a finite number of steps
Definiteness
Each step must be rigorously and unambiguously specified.
-e.g.,stir until lumpy
Input
Valid inputs must be clearly specified.
Output
can be proved to produce the correct output given a valid input.
Effectiveness
Steps must be sufficiently simple and basic.

Notation of an Algorithm

2
Sequence of steps in designing and analyzing the algorithm

Understanding the problem


Asking questions, do a few examples by hand, think about special cases, etc.
Decision making on
choice for either exact or approximate problem solving method
Appropriate data structure
Algorithmic Strategies
Specification of an algorithm
Proving correctness
Analyzing an algorithm
Time efficiency: how fast the algorithm runs
Space efficiency: how much memory the algorithm needs.
Implementation of an algorithm

Performance Analysis
The efficiency of an algorithm can be decided by measuring the performance of an
algorithm. We can measure the performance of an algorithm by following factors.
Space Complexity
The space complexity can be defined as amount of memory required by an algorithm
to run. To compute space complexity we use two factors: constant and instant characteristics.
S(p) = C + Sp
Time Complexity
The time complexity of an algorithm is the amount of time required by an algorithm
to run. Executing time depends on many factors such as
System load
Number of other programs running
Hardware capacity
The time complexity is given in terms of frequency count.
Frequency count is a count denoting number of times of execution of statement.

Measuring an input size

3
The input size of any instance of a problem is defined to be the number of elements
needed to describe that instance.
Measuring Running Time
Count the number of times an algorithms basic operation is executed.
Basic operation: the operation that contributes the most to the total running time.
For example, the basic operation is usually the most time-consuming operation in
the algorithms innermost loop.
Order of Growth
Measuring the performance of an algorithm in relation with the input size n is called
order of growth.
Time Space Tradeoff
Time space tradeoff is basically a situation where either space efficiency can be
achieved at the cost of time or a time efficiency can be achieved at the cost of memory.
Example 1: Suppose, in a file, if we store the uncompressed data then reading the data
will be an efficient but if the compressed data is stored then to read such data
more time will be required.
Asymptotic Notation
To choose the best algorithm, we need to check efficiency of each algorithm. The
efficiency can be measured by computing time complexity of each algorithm. Asymptotic
notation is a shorthand way to represent the time complexity.
Using Asymptotic Notation we can give time complexity as fastest possible,
slowest possible or average time. Various notations such as , O , used are called
asymptotic notations.
Big oh Notation
The Big oh notation is denoted by O. It is a method if representing the upper
bound of algorithms running time. Using Big oh notation we can give longest amount of
time taken by the algorithm to complete.
Definition
Let f(n) and g(n) be two non-negative functions. Let n0 denotes some value of input
and n > n0 . Similarly c is some constant such that c > 0. We can write
f(n) c * g(n)
then f(n) is big oh of g(n). It is also denoted as f(n) O(g(n)). In other words f(n) is
less than g(n) if g(n) is multiple of some constant c.

4
Omega Notation

The Omega notation is denoted by . It is a method if representing the lower


bound of algorithms running time. Using Omega notation we can give shortest amount of
time taken by the algorithm to complete.
Definition
Let f(n) and g(n) be two non-negative functions. Let n0 denotes some value of input
and n > n0 . Similarly c is some constant such that c > 0. We can write
f(n) c * g(n)
It is also denoted as f(n) (g(n)). In other words f(n) is greater than g(n) if g(n) is
multiple of some constant c.

5
Notation
The theta notation is denoted by . By this method the running time is between
upper bound and lower bound.

Definition
Let f(n) and g(n) be two non negative functions. There are two positive constants
namely c1 and c2 such that
c1 g(n) f(n) c2 g(n)

Little Oh Notation ( o )
It is used to describe the worst case analysis of algorithm and concerned with small
value of n. A function g(n) is said to be o(g(n)) is denoted by
The function f(n) = 0(g(n))
iff
lim f(n) = 0
n- g(n)
Little Omega Notation ( )

6
It is used to describe the best case analysis of algorithm and concerned with small
value of n.
The function f(n) = (g(n))
iff
lim f(n) =
n- g(n)

Conditional Asymptotic Notation


Many algorithms are easier to analyze if we impose conditions on them initially.
Imposing such conditions, when we specify asymptotic value is called asymptotic notation.
Let f : N-> R0 is eventually non decreasing if there exists an integer threshold no
such that f(n) f(n+1) for all n n0. This implies by mathematical induction that f(n)
f(m) wherever m n n0.

Removing conditions from the CAN


The function f : N-> R0 =0 is eventually non decreasing if such that
f(n) f(n+1) n n0
P-Smooth function
Let P 2 be an integer. Let f be an eventually non decreasing function, f is said to be
P-Smooth if
F(Pn) O(f(n))
Properties of Big Oh Notation
Following are some important properties of Big oh notation.
1. If there are two functions f1(n) and f2(n) such that f1(n) = O(g1(n)) and f2(n)
=O(g2(n)) then f1(n) + f2(n) = max(O(g1(n)) , O(g2(n))).
2. If f(n) = O(g(n)) and g(n) = O(h(n)) then f(n) = O(h(n)).
3. Any function can be said as an order of itself. That is f(n) = O(f(n)).
4. Any constant value is equivalent to O(1). That is C = O(1) where C is a Constant.
5. It
lim f(n) = C implies that f(n) has the same order of growth as g(n)
n- g(n) then f(n) (g(n)).
6. It
lim f(n) = 0 implies that f(n) has a smaller order of growth than g(n),

7
n- g(n) then f(n) O(g(n)).
7. It
lim f(n) = implies that f(n) has a larger order of growth than g(n),
n- g(n) then f(n) (g(n)).

Recurrence Equation
The recurrence equation is an equation that defines a sequence recursively. It is
normally in following form.
T(n) = T(n 1) + n for n > 0 ..(1)
T(0) = 0 ..(2)
Here equation 1 is called recurrence relation and equation 2 is called initial
condition. The recurrence can have infinite number of steps.

Solving Recurrence Equations


The recurrence relation can be solved by following methods.
1. Substitution Method
2. Masters Method
3. Recursion Tree Method
Substitution Method
The substitution method is a kind of method in which a guess for the solution is
made. There are two types of substitution.
1. Forward Substitution
2. Backward Substitution
Forward Substitution Method
This method makes use of an initial condition in the initial term and value for the
next term is generated. This process is continued until some formula is guessed.
Example
Consider a recurrence relation
T(n) = T(n - 1) + n
With initial condition T(0) = 0
Let,
T(n) = T( n 1 ) + n
If n = 1 then

8
T(1) = T(0) + 1
=0+1
T(1) = 1
If n = 1 then
T(2) = T(1) + 2
=1+2
T(2) = 3
If n = 3 then
T(3) = T(2) + 3
=3+3
T(3) = 6
By observing above generated equations we can derive a formula
T(n) = n(n + 1)/2
= n2 /2 + n /2
We can also denote T(n) in terms of big oh notation as follows
T(n) = O(n2)

Backward Substitution Method


In this method backward values are substituted recursively in order to derive some
formula.
Example
Consider a recurrence relation
T( n) = T(n 1 ) + n (1)
With initial condition T(0) = 0
T(n 1 ) = T(n 1 1 ) + (n 1 ) .(2)
Putting equation 2 in equation 1 we get,
T(n) = T(n 2 ) + (n 1 ) + n .(3)
Let
T(n 2 ) = T(n 2 1 ) + (n 2 ) .(4)
Putting equation 4 in equation 3 we get,
T(n) = T(n 3 ) + (n 2 ) + (n 1 ) + n
.
.
.

9
T(n) = T(n k ) + (n k 1) + (n k 2 ) + + n
If k = n then
T(n) = T(0) + 1 + 2 + 3+ + n
T(n) = 0 + 1 + 2 + 3 + . + n
T(n) = n(n + 1)/2
= n2 /2 + n /2
We can also denote T(n) in terms of big oh notation as follows
T(n) = O(n2)
Recursion Tree Method
In a recursion tree method, converts the recurrence into a tree whose node represents
the cost incurred at various levels of the recursion.
The sum of the cost at each level to determine the overall cost. Thus recursion tree
helps us to make a good guess of the time complexity.
Masters Method
We can solve recurrence relation using a formula denoted by masters method.
T(n) = aT(n/b) + f(n)
Where,
a >=1 and b > 1 are constants and f(n) asymptotically positive function.
According to master method the problem is decomposed into n number of sub problems.
Each sub problems can be solved separately.
Masters Theorem
Let a >=1 and b > 1 be constants. Let f(n) be a function and T(n) be defined on the
non negative integers by the recurrence.
T(n) = aT(n/b) + f(n)
T(n) can be bounded asymptotically as follows.
1. If

Analysis of Linear Search

10
The sequential search method, the element to be searched is considered as a
key element.
Then each successive element is compared with the key element.
If a match is found then it is called successful search.
Algorithm
Algorithm seq_search(a[0,1,..n-1],key)
Begin
for i <- 0 to n 1 do
if a[i] = key then
return i
else
return -1
End
Analysis
The input size is n (ie) total number of elements in the array.
The basic operation is comparing a[i] with key value.
The running time of the algorithm is not only dependent upon the input size
but it is dependent on worst case, best and average case time complexities.
Best case time Complexity
Best case time complexity is a time complexity when the algorithm runs for
short time. In linear search key algorithm the element key is searched from
the list on n elements.
If the key element is present at first location in the list (a[0] n-1) then
algorithm runs for a very short time and thereby we will get the best case
time complexity.
We can denote the best case time complexity as,
Cbest = 1
Worst case time Complexity
Worst case time complexity is a time complexity when the algorithm runs for
longest time. In linear search key algorithm the element key is searched from
the list on n elements.

11
If the key element is present at nth location in the list (a[0] n-1) then
algorithm runs for longest time and thereby we will get the worst case time
complexity.
We can denote the worst case time complexity as,
Cworst = n
Average case time Complexity
This type of complexity gives information about the behavior of an algorithm
on specific of random input.
Let us understand some technologies that are required for computing average
case time complexity.
Let the algorithm is for sequential search
P be the probability of getting successful search
n is the total number of elements in the list
The first match of the element will occur at 1 location.
Hence probability of occurring first match is P/n for every nth element.
The probability of getting unsuccessful search is (1 P). Now, we can find
average case time complexity Cavg (n) as
Cavg (n) = Probability of Successful search +
Probability of unsuccessful search
Cavg (n) = [ 1. P/n + 2.P/n + ..+i.P/n ] + n ( 1 P )
= P / n [ 1 + 2 + + i.n] + n ( 1 P )
= P / n . n(n + 1 ) / 2 + n ( 1 P )
= P (n + 1 ) / 2 + n ( 1 P )
Thus we can obtain the general formula for computing average case time
complexity.
Suppose if P=0 , there is no successful search. (ie) we have scanned the entire
list of n elements and still we do not found the desired element in the list.
Such a situation
Cavg (n) = n
Thus the average case running time complexity becomes equal to n.
Suppose if P=1 we get a successful search then,
Cavg (n) = 1 ( n + 1 ) / 2 + n ( 1 1 )

12
Cavg (n) = ( n + 1 ) / 2
The algorithm scans half of the elements from the list.
For calculating average case time complexity we have to consider probability
of getting success of the operation.
And any operation in the algorithm is dependent on input elements.
Thus computing average case time complexity is difficult then computing
worst case and best case time complexities.

UNIT II
Divide and Conquer , Greedy Algorithm
Divide and Conquer
Many recursive algorithms take a problem with a given input and divide it
into one or more smaller problems.
This reduction is repeatedly applied until the solutions of smaller problems
can be found quickly.
This procedure is called divide and conquer algorithm.

Basic Steps

Divide and Conquer is the most well known algorithm design strategy.
Divide the problem into two or more smaller sub problems.
Conquer the sub problems by solving them recursively.
Combine the solutions to the sub problems into the solutions for
the original problem.
The best case for the recursion is sub problems of constant size. Analysis can
be done using recurrence equations.

A typical Divide and Conquer case

13
A problem of
size n

Sub problem Sub problem


m1 of size n/2 m2 of size n/2

A solution of
A solution of
sub problem 2
sub problem 1

Solution to the
Original
problem

Algorithm

Algorithm DAndC ( P )
Begin
If small ( P ) then
return S(P)
else
Divide P into smaller instances P1 , P2 ,. Pk ,
Apply DAndC to each of these sub problems
return combine (DAndC(P1) , (DAndC(P2) , (DAndC(Pk)
End
Efficiency Analysis of Divide and Conquer
The computing time of Divide and Conquer(DAndC) on any input of size n is
described by the following recurrence relation.
T(n) = g(n) n is small
= T(n1 + n2 + + nk) + f(n) otherwise
T(n) is the time for Divide and Conquer on any input size n.
g(n) is the time to compute the answer directly.
f(n) is the time for dividing P and combining the solutions.
Divide and Conquer Recurrence Relation
14
Suppose that a recursive algorithm divides a problem of size n into number
of sub problems where each sub problem of size n/b.
T(n) be the number of operations required to solve the problem of size n.
T(n) = T(1) n=1
aT(n/b) + f(n) n>1
where,
T(n) - Time for size n
a - number of sub instances
n/b - time for size n/b
f(n) - time required for dividing the problem into
sub problems.

Solving Recurrence Relations


The recurrence relation is
T(n) = aT(n/b) + f(n) ..( 1 )
Assume n = bk where k= 1,2,
Substitute n = bk in equation 1,
T(bk) = aT(bk /b) + f(bk)
T(bk) = aT(bk-1) + f(bk)
= a[aT(bk-2) + af(bk-1) ] + f(bk)
= a2 T(bk-2) + af(bk-1) + f(bk)
= a3 T(bk-3)+ a2 f(bk-2) + af(bk-1) + f(bk)
After kth iteration
T(bk) = ak T(bk-k)+ ak-1 f(b1) + ak-2f(b2) +.+ ak-k f(bk)
The above equation can be written as
T(bk) = ak T(1)+ ak /a f(b1) + ak/ a2f(b2) +.+ ak/ ak f(bk)
Now ak h has taken as common factor
T(bk) = ak [ T(1)+ f(b) /a + f(b2) / a2+.+ f(bk)/ ak ]
k

T(bk) = ak [ T(1) + f(b ) / a ]


j j

j=1
By the property of logarithm
alogn

15
Binary Search
Binary Search uses divide and conquer strategy. Divide and conquer consists
of following major phases.
Breaking the problem into several sub problems that are similar to
the original problem but smaller in size.
Solve the sub problem recursively.
Combine these solutions to sub problems to create a solution to
the original problem.
Binary search is an efficient searching method.
An element which is to be searched from the list of element stored in array
A[0,1,.n-1] is called key element.
Let A[m] be the mid element of array A.
There are three conditions that needs to be tested while searching the array
using this method.
o If key = A[m] then the desired element is present in the list.
o If key < A[m] then search the left sub list.
o If key > A[m] then search the right sub list.
This can be represented as
A[0],A[m-1],A[m],A[m+1],..A[n-1]

key < A[m] key key > A[m]


Algorithm
Algorithm Binarysearch(a[0,1,n-1],key)
Begin
low <- 0
high <- n-1
while low < high do

16
{
m <- low+high / 2
if key = A[m] then
return m
else if key < A[m] then
high <- m-1
else
low <- m+1
}
End
Analysis
The basic operation in binary search is comparison of search key with the
array elements.
To analyze the efficiency of binary search based on number of times the
search key gets compared with the array elements.
The comparison is also called a three way comparison because algorithm
makes the comparison to determine whether key is smaller , equal to (or)
greater than A[m].
The worst case complexity of binary search given by
Cworst (n) = Cworst (n/2) + 1 n>1
Cworst (1) = 1
Where
Cworst (n/2) - time required to compare left (or) right sub list
1 - one comparison made with middle element
Solving Recurrence Relation
The recurrence relation is
Cworst (n) = Cworst (n/2) + 1 ..(1)
Assume n = 2k where k= 1,2,
Substitute n = 2k in equation 1,
Cworst (2k) = Cworst (2k /2) + 1
Cworst (2k) = Cworst (2k-1) + 1
Using the Backward Substitution method
Cworst (2k) = Cworst (2k-2) + 2
Cworst (2k) = Cworst (2k-3) + 3

17
.
.
.
Cworst (2k) = Cworst (2k-k) + k
= Cworst (20) + k
=1+k
Cworst (2k) = 1 + k
We assume n = 2k
Take log on both sides, we can get
log2n

Time Complexity of Binary Search is O(log n)

Finding Maximum and Minimum


The divide and conquer technique is used to solve the maxmin problem.
In the maxmin problem, the minimum and maximum value from the given
list of number is to be found.
This technique can be done very easily; the usual technique does not give
better results when applied to the list with large set of numbers.
The aim of this problem is to find the minimum and maximum value from the
given list of numbers.
If there is only one number in the list then the maximum and minimum values
both are the same number.
If there are 2 numbers then the smaller number will be minimum and the
greater number will be the maximum number.

18
If the list consists of more than 2 numbers then it will be divided into smaller
numbers till you get list containing either 1 (or) 2 elements.
The solution of the smaller list is later combined to give the final solution for
the larger problem.
Thus the maxmin problem is solved by using following algorithm.
Algorithm
Algorithm maxmin(low,high,max,min)
Begin
if low = high then
max = min = A[i]
else
if low = high 1
if A[low] < A[high]
max = A[high]
min = A[low]
else
max = A[low]
min = A[high]
end if
else
mid = low + high / 2
maxmin ( low,mid,max,min )
maxmin ( mid+1,high,max1,min1)
if max < max1 then
max = max1
end if
if min > min1 then
min = min1
end if
End
Analysis
The recurrence relation for the maxmin problem is
T(n) = T(n/2) + T(n/2) + 2 if n > 2
T(n) = 1 if n = 2

19
T(n) = 0 if n = 1
Solving Recurrence Equation
T(n) = 2T(n/2) + 2
Assume n = 2k
T (2k) = 2T (2k /2) + 2
T (2k) = 2T (2k /2) + 2
= 2T (2k-1) + 2
= 2[2T (2k-2 ) + 2] + 2
= 22 T (2k-2 ) + 4+2
= 22 T (2k-2 ) + 6
= 22 T (2k-2 ) + 23 2
T (2k) = 23T (2k-3 ) + 24 - 2
T (2k) = 24T (2k-4 ) + 25 2
.
.
.
.
.

K th iteration
T (2k) = 2k-1T (2k-(k-1) ) + 2k 2
T (2k) = 2k-1T (21) + 2k 2 T(n) = 1
T (2k) = 2k-1 (1) + 2k 2
T (2k) = 2k / 2 + 2k 2
T (n) = 3n / 2 2
Time Complexity of maxmin problem is is O( n)

Sorting
Sorting is an operation of arranging data, in some given order such as
increasing (or) decreasing with numerical data (or) alphabetically with
character data.
Sorting methods can be characterized into two categories:
1. Internal sorting
2. External sorting

20
Internal Sorting
Internal sorting methods are the methods that can be used when the list to be
sorted is small enough so that the entire sort can be carried out in main
memory.
The key principles of internal sorting is that all the data items to be stored are
retained in the main memory and random access in the memory space can be
efficiently used to sort the data items.
The various internal sorting techniques are:
Bubble sort
Selection sort
Insertion sort
Shell sort
Quick sort
External Sorting
External sorting methods are the methods to be used when the list to be sorted
is large and cannot be accommodated entirely in the main memory. In this
case, some of data is present in the main memory and some is kept in
auxiliary memory such as hard disk, floppy disk, tape etc.
The key principle of external sorting is to move data from secondary storage
to main memory in large blocks for ordering the data.
The various external sorting methods are:
Merge sort
Tape sort

Merge Sort
Merge sort is one of the external sorting technique.
Merge sort algorithm follows divide and conquer strategy.
Given a sequence of n elements A[1],A[2],A[N].
The basic idea behind the merge sort algorithm is to split the list into two sub
lists A[1],.A[N/2] and A[(N/2)+1],.A[N].
If the list has even length, split the list into 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 list is to two and go on until each of the sub lists are
of size one.
Finally, start merging the individual sub list to obtain a sorted list.
Time complexity of merge sort is (n log n).

Algorithm
Algorithm mergesort(A[0,1,n-1,low,high)

21
Begin
if low < high then
mid <- low + high / 2
mergesort(A , low , mid)
mergesort(A, mid+1, high)
combine(A, low, mid, high)
end if
End
Algorithm combine(A[0,1,..n-1,low,mid,high)
Begin
k <- low
i <- low
j <- mid + 1
while ( i <= mid and j < = high) do
{
if A[i] <= A[j] then
temp[k] <- A[i]
i <- i + 1
k <- k + 1
else
temp[k] <- A[j]
j <- j + 1
k <- k + 1
end if
}
while( i <= mid) do
{
temp[k] <- A[i]
i <- i + 1
k <- k + 1
}
while( j <= high) do
{
temp[k] <- A[j]

22
j <- j + 1
k <- k + 1
}
End
Analysis
The recurrence relation for the merge sort is
T(n) = T(n/2) + T(n/2) + cn
Where
T(n/2) - time taken by left sublist to get sorted
T(n/2) - time taken by right sublist to get sorted
cn - time taken for combining two sublists

Solving Recurrence Relation


The recurrence relation is
T(n) = 2T(n/2) + cn
Assume n = 2k
T(2k) = 2T(2k /2) + c. 2k
T(2k) = 2T(2k-1) + c. 2k
T(2k) = 2 [ 2T(2k-2) + c. 2k-1) ] + c. 2k
T(2k) = 22T(2k-2) + 2. c. 2k-1 + c. 2k
T(2k) = 22T(2k-2) + 2. c. 2k /2 + c. 2k
T(2k) = 22T(2k-2) + 2. c. 2k
T(2k) = 23T(2k-3) + 3. c. 2k
.
.
.
T(2k) = 2kT(2k-k) + k. c. 2k
T(2k) = 2kT(1) + k. c. 2k
T(2k) = k. c. 2k [ T(1) = 0 ]
We assume n = 2k
Take log on both sides, we can get

23
Hence the average , best and worst case complexity of merge sort is O(n log n).

Greedy Algorithm
General Method
The greedy method is the most straightforward design technique that can be
applied to a wide variety of problems.
Term and Definition
Objective Function
A problem in which some function ( called the optimization or objective
function) is to be optimized ( usually minimized or maximized ) subject to
some constraints.
Feasible Solution
A feasible solution is a solution that satisfies the constraints.
Optimal Solution
An optimal solution is a feasible solution that optimizes the objective /
optimization function. In general, finding an optimal solution is
computationally hard.
It solves a problem by making a sequence of decisions and the decisions are
made one by one in some order.
Each decision is made using a greedy criterion. At each stage we make a
decision that appears to be the best at the time. A decision once made is not
changed order.

Algorithm
Algorithm Greedy(Type a[], n)
// a[1:n] contains n inputs

24
Begin
Soltype solution = EMPTY // Initialize the solution
for i=1 to n do
Type x = select ( a )
if feasible(solution,x)
solution <- union(solution,x)
end if
return solution
End
Container Loading Problem
Container loading problem is one of the optimization problem is solved by
using greedy technique.
The container loading problem is similar to 0/1 knapsack problem as
containers can not be taken in fraction.
In this problem there is a ship with capacity m. The number of containers n
is given an their respective weights are also given.
The containers should be loaded in such a way that we load maximum
number of containers in the ship does not exceed the capacity of ship.
Aim of the Problem
Load the containers into the ship in such a way that maximum number of
container is loaded.
n
X i is maximized
i=1
Constraints
The total weight of the containers loaded should not exceed the capacity of
the ship. n
XW i i m
i=1
Example
Consider the following input for container loading problem
n = 5 , m = 60, w = { 5, 10, 20, 30,40 }
Iteration 1
Take the first container. Compare the weight with the capacity of the ship.
5 < 60

25
So that, the first container is selected. The available capacity becomes 55.
The output vector X is
X= { 1, 0, 0, 0, 0 }
Iteration 2
Take the second container. Compare the weight with the capacity of the ship.
10 < 55
So that, the second container is selected. The available capacity becomes 45.
The output vector X is
X= { 1, 1, 0, 0, 0 }
Iteration 3
Take the third container. Compare the weight with the capacity of the ship.
20 < 45
So that, the third container is selected. The available capacity becomes 25.
The output vector X is
X= { 1, 1, 1, 0, 0 }
Iteration 4
Take the fourth container. Compare the weight with the capacity of the ship.
30 > 25
Hence fourth container is not selected. The output vector X is
X= { 1, 1, 1, 0, 0 }
Iteration 5
Take the last container. Compare the weight with the capacity of the ship.
40 > 25
Hence last container is not selected. The final output vector X is
X= { 1, 1, 1, 0, 0 }
Total Weight of the ship = 35

Algorithm
Algorithm ContainerLoading(n,m,w[],X[])
Begin
for i <- 1 to n
X[i] <- 0
end for
for i <- 1 to n
if w[i] > m
break
else
X[i] <- 1

26
m <- m w[i]
end if
end for
End
Complexity of Container Loading
The complexity of algorithm measured by space and time complexity.
Space Complexity 2n + 3
Time Complexity O(n log n)
Knapsack Problem
The knapsack problem is one of the optimization problem.
In the knapsack problem, assume the capacity of sack is m.
There are n number of objects provided. The weights of each object is given
in the form of an array w[].
The profit of each object is provided using the array p[].
Now, we have to fill the sack with those objects in such a way that the total
weight of the objects put into the sack should be lesser than (or) equal to the
capacity of the sack.
We should select the objects in such a way that we get maximum profit.

Aim of the Problem


To fill the sack upto maximum capacity such that the total profit of selected
object is maximized.
n
XP i i is maximized
i=1
Constraints
The total weight of the selected objects should not exceed the maximum
capacity of the sack.
n
XW i i m
i=1

Example

27
Consider the following input for knapsack problem
n = 3 , m = 20, w = { 5, 10, 15 } , p = { 50, 30, 30 }
Iteration 1
Initially, the total weight and profit is zero. Select the first object. Compare its
weight with the capacity of the sack.
5 < 20
Since the weight of the object does not exceed the capacity of the sack. This
can be put inside the sack. To indicate this, set the first position of X[] as 1.
X= { 1, 0, 0 }
Now, the remaining capacity of the sack becomes m = 15.
Iteration 2
Select the second object. Compare its weight with the available capacity of
the sack.
10 < 15
Since the weight of the object does not exceed the capacity of the sack. This
can be put inside the sack. To indicate this, set the second position of X[] as
1.
X= { 1, 1, 0 }
Now, the remaining capacity of the sack becomes m = 5.
Iteration 3
Select the third object. Compare its weight with the capacity of the sack.
15 > 5
Since the weight of the object is exceeding the capacity of the sack. We can
not put the whole object inside the sack.
But a fraction of that which fits into the sack exactly can be taken. i.e) 5 / 15
= 1/3 of the third object can be put in the sack. It is a fractional knapsack
problem. The final solution is,
X= { 1, 0, 0.33 }
Total Profit = 90 ( 50 + 30 + 10 )
Total weight = 20 ( 5 + 10 + 5 )
Algorithm
Algorithm knapsack(n,m,w[],X[])
Begin
for i <- 1 to n
X[i] <- 0
end for

28
for i <-1 to n
if w[i] > m
break
else
X[i] <- 1
m <- m w[i]
end if
end for
if i <= n
X[i] <- m / w[i]
end if
End
Complexity of Knapsack Problem
The complexity of algorithm measured by space and time complexity.
Space Complexity 3(n + 1)
Time Complexity O(n)

UNIT III
Dynamic Programming
Definition
It 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
soltuion to the original problem is obtained.
General Method
It is an algorithm design method that can be used when solution to a problem
can be viewed as the result of sequence of decisions.
Enumerate all decision sequences and then pick out the best.
Optimal sequence of decision is obtained.
Many decision sequences may be generated.

Multistage Graph
The multistage graph is to find a minimum cost path from source S to sink t ie)
destination.
Problem Description

29
A multistage graph G=(V,E) in a directed graph in which the vertices are
partitioned into k >= 2 disjoint sets Vi 1 i k
If (u,v) is an edge in E, then U Vi and V Vi+1 for some i, 1 i k
The sets V1 and Vk are such that | V1| = | Vk| = 1
The vertex S is the source and t is the sink.
Let C(i,j) be the cost of edge (i,j)
The cost of a path from S to t is the sum of the cost of the edges on the
path.
Each set Vi defines a stage in the graph.
Every path from S to t starts in stage 1, goes to stage 2, then to stage 3 and so
on, and eventually terminates at stage K.
Multistage Graph using Forward Approach
Find the path from S to t, stage by stage.
Every S to t is the result of a sequence of K-2 decisions.
The i th decision involves determining which vertex in V i+1, 1 i k-2 is to
be on the path.
P(i,j) be a minimum cost path from vertex j in Vi to vertex t.
Cost(i,j) bethe cost of the path.
Find cost of path using the formula
cost(i,j) = min { c( j , l ) + cost( i+1 , l ) }
l Vi+1
(j,l) E
Shortest distance between source S to sink t using following formula
cost( K-2 , j ) for all j Vk-2
cost( K-3 , j ) for all j Vk-3
cost( 1 , s )
Algorithm for Multistage Graph using Forward Approach
Algorithm Graph(G,k,n,P[])
Begin
cost[n] = 0.0
for j <- n-1 to 1
cost[j] = c[ j,r ] + cost[r]
d[j] = r
end for
P[1] = 1
P[k] = n
for j <- 2 to K-1
P[j] = d [ P [ j 1 ] ]
end for

30
End
Multistage Graph using Backward Approach
Multistage can be solved using backward graph approach.
Let bp(i,j) be a minimum cost path from vertex S to a j in Vi
bcost(i,j) be the cost of bp(i,j)
Shortest distance between source S to sink t using following formula
bcost(i,j) = min { bcost( i-1 , l ) + c ( l , j ) }
l Vi-1
(j,l) E
bcost(2,j) = C( 1 , j ) if ( 1 , j ) E

Algorithm for Multistage Graph using Backward Approach


Algorithm Graph(G,k,n,P[])
Begin
bcost[1] = 0.0
for j <- 2 to n
bcost [j] = bcost [r ] + C[r,j]
d[j] = r
end for
P[1] = 1
P[k] = n
for j <- K-1 to 2
P[j] = d [ P [ j + 1 ] ]
end for
End
All Pair Shortest Path Algorithm ( Floyds Algorithm )
The all pair shortest path algorithm is invented by R.Floyds. So it is called as
Floyds algorithm.
This algorithm is useful for finding the shortest path between every pair of
vertices of a graph.
It works for both undirected and directed graphs.

Weighted Graph

The weighted graph is a graph in which weight (or) distance are given along
the edges.
The weighted graph can be represented by weighted matrix.
W[i][j]=0 if i=j

31
W[i][j]= if there is no edge between the vertices
i and j
W[i][j]= weight of the edge

Concepts of Floyds Algorithm

Floyds algorithm is for computing shortest path between every pair of


vertices of a graph.
The graph may contain negative edges but it should not contain negative
cycles.
This algorithm requires a weighted graph.
The floyds algorithm computes the distance matrix of a weighted graph with
n vertices through a series of n by n matrices.

D(0), D(1), D(2), ., D(k-1),. D(n)

The series starts with D(0) with no intermediate vertex. D(0) is a matrix in
which Vi and Vj .
In D(1) matrix, the shortest distance going through one intermediate vertex
with maximum path length of two edges is given continuing in this fashion.
We will compute D(n) containing the length of shortest path among all paths
that can use all n vertices as intermediate.
Finally, we can get all pair shortest path from matrix D(n)

Example

Find the All pair shortest path for the following graph.

5
1 1

8 2 1

Step 1:

First compute the weighted matrix with no intermediate vertex.

1 2 3
D(0)
1 0 8 5
2 2 0
3 1 0

D(0) is the weighted matrix (or) adjacency matrix for the given graph.

32
Step 2:

Now the node 1 will be considered as the intermediate node. D(1) can be
calculated by using the intermediate vertex as 1.

1 2 3
(1)
D 1 0 8 5
2 2 0 7
3 1 0
In this matrix there is no edge between the vertex 2 and 3. But using the
intermediate vertex we can travel from 2 to 3 with the shortest path is
213=2+5=7
So we can replace the 2nd row and 3rd column by 7.

Step 3:

Now the node 2 will be considered as the intermediate node. D(2) can be
calculated by using the intermediate vertex as 1 and 2.

1 2 3
(2)
D 1 0 8 5
2 2 0 7
3 3 1 0
The shortest path between vertices 3 and 1 is 3 2 1 = 3 using the
intermediate vertex 1.

Step 4:

Now the node 3 will be considered as the intermediate node. D(3) can be
calculated by using the intermediate vertex as 1,2 and 3.

1 2 3
(3)
D 1 0 6 5
2 2 0 7
3 3 1 0
The shortest path between 1 2 is
1 2 =8
132=6
The matrix D(3) is representing shortest path for all pair of vertices.

33
Algorithm

Algorithm AllPair(cost, A, n)
Begin
for i=1 to n do
for j=1 to n do
A[i,j] = cost[I,j]
end for
end for
for k=1 to n do
for i=1 to n do
for j=1 to n do
A[i,j] = min(A[i,j] , A[i,k] + A[k,j])
end for
end for
end for
End
Analysis
The basic operation is
D [ i , j ] = min { D [ i , j ] , D [ i , k ] + D [ k , j ] }
It has three nested for loops
n n n

C(n) = 1
k=1 j=1 i=1
C(n) = n3
The time complexity of finding all pair shortest path is (n3)

Optimal Binary Search Tree


We are searching a word from a dictionary. For every required word we are
looking in the dictionary then it becomes a time consuming process.
To perform this lookup more efficiently we can build the binary search tree of
common words as key elements.
We can make the binary search tree efficient by arranging frequently used
words nearer to the root and less frequently used words away from the root.
Searching process is an arrangement of BST is more simplified as well as
efficient.
The optimal binary search tree technique is invented for this approach.
The element having more probability of appearance should be placed nearer
to the root of the BST.
The element with lesser probability should be placed away from the root.

34
The BST created with such kind of arrangement is called as an Optimal
Binary Search Tree.
Let [ a1, a2,. an] be the set of identifiers such that a1 a2 a3
Let P(i) be the probability with which we can search for a i (successful
search ).
Let qi be the probability of searching an element such that a i < x < ai+1 where
0 i n . ( unsuccessful search )
The tree which is build with optimum cost from
n n
P(i) and qi is called OBST.
i=1 i=1
To obtain the OBST for the key values using dynamic programming, we will
compute the cost of the tree Cij and the Root of the tree Rij.
Formula for calculating C [ i , j ]
j

C [ i , j ] = min { C [ i , k-1 ] + C [ k+1 , j ] } + Ps where 1 i j n


s=i
Assume that
C[i,i1]=0 i ranging from 1 to n + 1
C [ i , i ] = Pi where 1 i n
Optimum Binary Search Tree consists of two tables. Such as,
o Cost table
o Root table
The cost table should be constructed in this fashion. Assume n = 3

0 1 2 3 ( j ranging from 0 to n )
i ranging 1 0 P1
from 1 to 2 0 P2
n+1 3 0 P3
4 0
The root table should be constructed in this fashion.
1 2 3
1 i
2 i
3 i
Fill up R [ i , i ] by i
Fill up R [ i , j ] by k value which is obtained as minimum.
The table should be filled up diagonally.
Example
Find the OBST for the following nodes
do if int while
Probabilities : 0.1 0.2 0.4 0.3

35
Solution
First, number out the nodes ( do, if, int, while ) as 1, 2, 3, 4 respectively.
There are 4 nodes. So n=4
1 2 3 4
Probabilities : 0.1 0.2 0.4 0.3
Step 1
Initial Table
0 1 2 3 4
0 0.1 1

0 0.2 2
0 0.4 3
4
0 0.3
0 5
C[ 1 , 0 ] = 0
C[ 2 , 1 ] = 0
C[ 3 , 2 ] = 0 C[ i , i-1 ] = 0 & C[ n +1, n ] = 0
C[ 4 , 3 ] = 0
C[ 5 , 4 ] = 0

C[ 1 , 1 ] = 0.1
C[ 2 , 2 ] = 0.2 C [ i , i ] = P[i]
C[ 3 , 3 ] = 0.4
C[ 4 , 4 ] = 0.3

Root Table 1 2 3 4
1
1
2 2

3 3
4 4

R [ 1, 1 ] = 1
R [ 2, 2 ] = 2 R[i,i]=i
R [ 3 , 3] = 3
R [ 4, 4 ] = 4
Compute C[ i , j ]
j

36
C [ i , j ] = min { C [ i , k-1 ] + C [ k+1 , j ] } + Ps where 1 i j n
s=i
Step 2

Compute C [ 1 , 2 ]
k can be either 1 ( or ) 2
Let i = 1 , j = 2

C [ 1, 0 ] + C [ 2, 2 ] + P[1] + P[2] when k = 1


0 + 0.2 + 0.1 + 0.2 = 0.5
C [ 1 , 2 ] = C [ 1, 1 ] + C [ 3, 2 ] + P[1] + P[2] when k = 2
0.1 + 0 + 0.1 + 0.2 = 0.4
C [ 1 , 2 ] = 0.4 with k = 2
Cost Table C [ 1 , 2 ] = 0.4 & R [ 1, 2 ] = 2

Compute C [ 2 , 3 ]
k can be either 2 ( or ) 3
Let i = 2 , j = 3

C [ 2, 1 ] + C [ 3, 3 ] + P[2] + P[3] when k = 2


0 + 0.4 + 0.2 + 0.4 = 1.0
C[2,3] = C [ 2, 2 ] + C [ 4, 3 ] + P[2] + P[3] when k = 3
0.2 + 0 + 0.2 + 0.4 = 0.8

C [ 2 , 3 ] = 0.8 with k = 3
Cost Table C [ 2 , 3 ] = 0.8 & R [ 2, 3 ] = 3

Compute C [ 3 , 4 ]
k can be either 3 ( or ) 4
Let i = 3 , j = 4

C [ 3, 2 ] + C [ 4, 4 ] + P[3] + P[4] when k = 3


0 + 0.3 + 0.4 + 0.3 = 1.0
C[3,4] = C [ 3, 3 ] + C [ 5, 4 ] + P[3] + P[4] when k = 4
0.4 + 0 + 0.4 + 0.3 = 1.1

C [ 3 , 4 ] = 1.0 with k = 3
Cost Table C [ 3 , 4 ] = 1.0 & R [ 3, 4 ] = 3

Now the cost table and Root table is updated as

0 1 2 3 4 1 2 3 4

37
1 1 2
0 2 0.12 0.4
0 0.2 0.8
3 3
4 0 0.4 1.0
0 0.3
5 4
0
1
Step 3 2 3
Compute 3 3 C[1,3]
k can be either 1 , 2 ( or ) 3
Let i 4 =1,j=3

C [ 1, 0 ] + C [ 2, 3 ] + P[1] + P[2] + P[3] when k = 1


0 + 0.8 + 0.1 + 0.4 + 0.2 = 1.5
C[1,3] = C [ 1, 2 ] + C [ 4, 3 ] + P[1] + P[2] + P[3] when k = 2
1.1 + 0.4 + 0.1 + 0.2 + 0.4 = 1.2
C [ 1, 2 ] + C [ 4, 3 ] + P[1] + P[2] + P[3] when k = 3
0.4 + 0 + 0.2 + 0.1 + 0.4 = 1.1

C [ 1 , 3 ] = 1.1 with k = 3
Cost Table C [ 1 , 3 ] = 1.1 & R [ 1, 3 ] = 3

Compute C [ 2 , 4 ]
k can be either 2 , 3 ( or ) 4
Let i = 2 , j = 4

C [ 2, 1 ] + C [ 3, 4 ] + P[2] + P[3] + P[4] when k = 2


0 + 1.0 + 0.2 + 0.4 + 0.3 = 1.9
C[2,4] = C [ 2, 2 ] + C [ 4, 4 ] + P[2] + P[3] + P[4] when k = 3
0.2+ 0.3 + 0.2 + 0.4 + 0.3 = 1.4
C [ 2, 3 ] + C [ 5, 4 ] + P[2] + P[3] + P[4] when k = 4
0.8 + 0 + 0.2 + 0.4 + 0.3 = 1.7

C [ 2 , 4 ] = 1.4 with k = 3
Cost Table C [ 2 , 4 ] = 1.4 & R [ 2, 4 ] = 3

Now the cost table and Root table is updated as


0 1 2 3 4 1 2 3 4

38
1 1 2
0 2 0.12 0.4 1.1
0 0.2 0.8 1.4
3 3
4 0 0.4 1.0
3
0 0.3
5 4
0
Step 4
1
Compute 2 3 3 C[1,4]
k can be either 1 , 2 , 3 ( or ) 4
Let i 3 3 =1,j=4

4 C [ 1, 0 ] + C [ 2, 4 ] +
P[1] + P[2] + P[3] + P[4] k = 1
0 + 1.4 + 0.1 + 0.2 + 0.4 + 0.3 = 2.4
C [ 1 , 4 ] = C [ 1, 1 ] + C [ 3, 4 ] + P[1] + P[2] + P[3] + P[4] k = 2
1.1 + 0.1 + 1.0 + 0.2 + 0.3 + 0.4 = 2.1
C [ 1, 2 ] + C [ 4, 4 ] + P[1] + P[2] + P[3] + P[4] k = 3
0.4 + 0.3 + 0.1 + 0.2 + 0.4 + 0.3 = 1.7
C [ 1, 3 ] + C [ 5, 4 ] + P[1] + P[2] + P[3] + P[4] k = 4
1.1 + 0 + 0.1 + 0.2 + 0.4 + 0.3 = 2.3

C [ 1 , 4 ] = 1.7 with k = 3
Cost Table C [ 1 , 4 ] = 1.7 & R [ 1, 4 ] = 3

Now the cost table and Root table is updated as


0 1 2 3 4 1 2 3 4
1 1
0 2 0.12 0.4 1.1 1.7 2
0 0.2 0.8 1.4
3 3
4 0 0.4 1.0
3 3
0 0.3
5 4
0
Step 5
To build a 1 tree R [ 1 , n ] = R [ 1 , 4 ] = 3
becomes Root 2 3 3

3 3 1 2 3 4
do if int
while 4

39
key= 3 , Hence int becomes Root of OBST

Tk

Ti , k-1 T k+1,j

Here i = 1 , j = 4 and k = 3
R(1,4)
key = 3 ( int )

key = 2 ( if ) R(1,2) R(4,4) key = 4 ( while )

R(1,4)
key = 3 ( int )

key = 2 ( if ) R(1,2) R(4,4) key = 4 ( while )

key=1( do) int


R(1,1)

if while

do
The tree can be with Optimum Cost C [ 1 , 4 ] = 1.7

Algorithm
Algorithm OBST(P[1,2,..,n])
Begin

40
for i <- 1 to n
C[i,i-1] <- 0
C[i,i] <- P[i]
R[I,i] <- i
end for
for i <- 1 to n+1
for j <- 0 to n
min <-
for k <- i to j
if (C[i,k-1] + C[k+1,j]) < min
min <- C[i,k-1] + C[k+1,j]
kmin <- k
end if
end for
R[i,j] <- k
sum <- P[i]
for s <- i+1 to j
sum <- sum + P[i]
C[i,j] <- min + sum
end for
end for
for i <- 1 to n+1
for j <- 0 to n
write Cost[i,j]
end for
end for
for i <- 1 to n
for j <- 1 to n
write Root[i,j]
end for
end for
End
Analysis

41
The basic operation of OBST algorithm is computation of C[i,j] by finding
the min valued k. The basic operation is located with in three nested for
loops. Hence the Time complexity can be O(n3).
Time Complexity O(n3)
Space Complexity O(n2)

0/1 Knapsack Problem

The 0/1 knapsack problem is solved by using dynamic programming.


Dynamic programming can be used when the solution to a problem can be viewed as
the result of sequence of decisions on the variables x1, x2, x3, xn.
A decision on variable xi involves determining which of the values 0 ( or ) 1 is to be
assigned to 1.
Aim of the Problem
Fill the sack upto maximum capacity such that the total profit of selected object is
maximized. n

Xi Pi is maximized
i=1
Constraints
Total weight of the selected objects should not exceed the maximum capacity of the
sack.
The condition is ,
n

Xi Wi m
i=1
Where ,
X={ 0,1 }

Initial Condition
V[i,0] = 0 and V[0,j] = 0
All the values should be positive and weights should be integer.
Profit can be float.

42
It is also called as 0/1 knapsack problem (i.e. fraction part of item should not be
considered.
The rows will be from 0 to the number of objects n.
The columns from 0 to knapsack capacity m.

V [ i , j ] = max { V[i] + V [ i 1 , j wi ] , if j - wi 0
V[i1,j] if j - wi 0 }
Example
Item weight Value
1 2 12
2 1 10
3 3 20
4 2 15
Solution
Initial condition
V[ i , 0 ] = 0
V[ 0 , j ] = 0
V [ 1 , 1 ] = max { V[i] + V [ i 1 , j wi ] , V [ i 1 , j ]
= max { 12 + V [ 0 , -1 ] , V [ 0 , 1 ]
=0
Find the remaining values.

0 1 2 3 4 5

0 0 0 0 0 0
0
0 0 12 12 12 12
1
2 0 10 12 22 22 22

3 0 10 12 22 30 32
4 0 10 15 25 30 37
Value checked with
previous value. If it is equal dont select the item. Otherwise we can select the
item. The solution set is
X = { 1, 0, 1 }

43
Algorithm
Algorithm Dknapsack(n,m,w[1,..,n],v[1,.n])
Begin
for j = 0 to m
v[0,j]=0
end for
for i = 0 to n
v[i,0]=0
end for
for i = 0 to n
for j = 0 to m
if (w[i] j )
v[ i , j ] = v[i] + v[ i 1 , j w[i] ]
else
v[ i , j ] = v[ i-1 , j ]
end if
end for
end for
return v[ n , m ]
End
Analysis
Time complexity is O(nm)
Travelling Salesperson Problem
The Travelling Salesperson problem is solved by using dynamic programming.
A Travelling Salesperson problem presents a graph showing n number of cities
which are connected by using the edges.
The cost (or) distance of each edge is given using the cost matrix.
Aim of the Problem
The Travelling Salesperson problem is to find the shortest path to cover all
the cities and come back to the same city.
Problem Definition
Let G = ( V, E ) be a directed graph with edge costs Cij
Cij > 0 for all i and j and
Cij = < i , j > E

44
Let |V| = n and n > 1
A tour of G is a directed simple cycle that includes every vertex in v.
Constraints
All the cities should be covered
Each city should be visited only once. The starting and ending point should
be the same.
The cost of the tour is the sum of the cost of the edges on the tour. The
travelling salesperson problem is to find a tour of minimum cost.
Application
The Travelling Salesperson problem finds application a variety of situations.
Suppose we have to route a postal van to pick up mail from mail boxes
located at n different cities.
An n + 1 vertex graph can be used to represent the situation. One vertex
represents the post office from which the postal van starts and to which it
must return.
Edge <i,j> is assigned a cost equal to the distance from site i to site j.
The route taken by the postal van is a tour and we are interested in finding a
tour of minimum length.
Dynamic Programming Formulation
Let v be the collection of vertices. Every tour consists of an edge<1,k> for
some k v { 1 }
The path from vertex k to vertex 1 goes through each vertex in v { 1 k }
exactly once.
Let g(i,s) be the length of a shortest path starting at vertex i , going through
all the vertices in s and terminating at vertex 1.
The function g( 1 , v {1} ) is the length of an optimal salesperson tour.
g ( i , s ) = min { Cij + g ( j , s { j } ) }
js

Solving the Recurrence Relation


The equation g ( 1 , v {1} ) may be solved, if we know g(k,v-{1,k}) for all
choices of k. The g values can be obtained by using equation g(i,s)
g( i , ) = Ci1 , 1 i n
where, g( i , ) is the length of the shortest path starting at i going through
no vertex and ending at 1.
Example

45
Solve the TSP for the following graph.

1 C(i,j 1
1 2 3 4
)
1 0 10 15 20

2 5 0 9 10
1 1
3 6 13 0 12

4 8 8 9 0

Step 1:
We can choose any one of vertex as source and destination.
Let 1 be the starting and ending vertex. To compute g( i , )
If there is no intermediate vertex, use the following formula
g( i , ) = Ci1 , 1 i n
g( 2 , ) = C21 = 5
g( 3 , ) = C31 = 5
g( 4 , ) = C41 = 8
Step 2:
If there is intermediate vertex, use the following formula
g ( i , s ) = min { Cij + g ( j , s { j } ) }
js
g ( 2 , {3} ) = C23 + g( 3 , )
= 9 + 6 = 15
g ( 2 , {4} ) = C24 + g( 4 , )
= 10 + 8 = 18

g ( 3 , {2} ) = C32 + g( 2 , )
= 13 + 5 = 18
g ( 3 , {4} ) = C34 + g( 4 , )
= 12 + 8 = 20
g ( 4 , {2} ) = C42 + g( 2 , )
= 8 + 5 = 13
g ( 4 , {3} ) = C43 + g( 3 , )

46
= 9 + 6 = 15
Step 3:
To compute g ( i , s ) with |s| = 2
g(2,{3,4}) = min{ C23 + g( 3,{4}) , C24 + g( 4,{3}) }
= min { 9 + 20 , 10 + 15 }
= 25
g(3,{2,4}) = min{ C32 + g( 2,{4}) , C34 + g( 4,{2}) }
= min { 13 + 8 , 12 + 13 }
= 25
g(4,{2,3}) = min{ C42 + g( 2,{3}) , C43 + g( 3,{2}) }
= min { 8 + 15 , 9 + 18 }
= 23
Step 4:
To compute g ( i , s ) with |s| = 3
g(1,{2,3,4}) = min{ C12 + g( 2,{3,4}) , C13 + g( 3,{2,4}) , C14 + g( 4,{2,3}) }
= min { 10 + 25 , 15 + 25 , 20 + 23 }
= 35
Optimal Solution is
124 31
Total cost = 35
Algorithm
Algorithm TSP(cost[] , n)
Begin
Cost({1},1) <- 0 // since the path cant both start and end at 1
for s=2 to n
for all subset s {1,2,..n}
C(s,1) <-
for all j s , j 1
cost(i,j) <- min{Cij + cost (j, s-{j})}
return Cost(i,j)
end for
end for
End
Analysis
47
Let N be the number of cost(i,s) that have to be completed. For each value of |s|,
there are n-1 choices for i. The number of distinct sets S of size k not including 1
and i is (kn-2)
Time Complexity of TSP is O(n22n)

UNIT IV
Backtracking
Definition

Backtracking is one of the most general techniques for searching a set of solutions
(or) for searching an optimal solution.
The name backtrack was first coined by D.H.Lehmer in the 1950s.

48
Backtracking is a systematic way to search for the solution to a problem.

N Queens Problem
N Queens problem is solved by using backtracking.
The problem is to place n queens on n * n chess board.
The following constraints are used to place queens on the chess board.
o No two queens should be placed on the same diagonal
o No two queens should be placed on the same column
o No two queens should be placed on the same row
2 Quenns Problem

The 2 - Queens problem is not solvable. Because 2-Queens can be placed on 2 * 2


chessboard as

Q Q
Q
Q
Illegal Illegal

Q
Q Q
Q
Illegal Illegal

There is no solution when the value of n = 1 , 2 and 3

4 - Quenns Problem

The solution for the 4 Queens problem is easily obtained using the backtracking
algorithm.
The aim of the problem is to place 4 Queens on the chessboard in such a way that
none of the queens hit each other.
Solving Procedure

49
Step 1:

Let us assume the queens are placed in the row wise. The 1st Q is placed I 1st row at
(1,1)

Q1

Step 2:

The second Queen has to be placed in 2nd row. It is not possible to place the Q2 at the
following places.
(2,1) placing the queens in the same column
(2,2) placing the queens in the same diagonal.
So the queen can be placed in (2,3)

Q1
Q2

Step 3:

The third Queen has to be placed in 3rd row. It is not possible to place the Q3 at the
following places.
(3,1) placing the queens in the same column
(3,2) placing the queens in the same diagonal.
(3,3) placing the queens in the same column
(3,4) placing the queens in the same diagonal.

Q1
Q2
X X X X

50
Backtracking to the previous solution

So this tells that Q3 can not be placed at 3rd row and it will not given a solution. So
backtracking to the previous step ie) step 2. So instead of placing the Q2 at (2,3), Q2
can
be placed in (2,4).

Q1
Q2

Step 4:

The third Queen has to be placed in 3rd row. It is not possible to place the Q3 at the
following places.
(3,1) placing the queens in the same column
(3,3) placing the queens in the same diagonal.
(3,4) placing the queens in the same column
So the queen has to be placed in (3,2)

Q1
Q2
Q3

Step 5:

The fourth Queen has to be placed in 4th row. It is not possible to place the Q4 at the
following places.
(4,1) placing the queens in the same column
(4,2) placing the queens in the same column
(4,3) placing the queens in the same diagonal.
(4,4) placing the queens in the same column

Q1
Q2

51
Q3
X X X X
Backtracking to the previous solution

So this tells that Q4 can not be placed at 4th row and it will not given a solution. So
backtracking to the previous step ie) step 4. So instead of placing the Q3 at (3,2), Q3
can
be placed in (3,3) (or) (3,4), that is also not possible because it comes in a same
diagonal
and same column. So again backtracking to the previous step of step 4 ie) step3 and
do
the same process. Finally Q3 is placed in (3,1)

Q1
Q2
Q3

Step 6:

Q1
Q2
Q3
Q4

Basic Terminologies
Solution Space
Tuples that satisfy the constraints
The solution space can be organized into a tree
State Space
State Space is the set of all paths from root node to other nodes.
State Space Tree
State Space Tree is the tree organization of the solution space.

52
In backtracking technique while solving a given problem a tree is constructed based
on the choices made.
Such a tree with all possible solutions is called State Space Tree.
Promising and Non-Promising Node
A node in State Space Tree is said to be promising
The node which are not promising for solution in a state space tree is called non
promising node.

Q1 = 1 1 Q1 = 3
Q1= 2

2 6 10
Q2 = 3 Q2 = 4 Q2 = 1

3 4 7 11

Not a Q3 = 2 Q3 = 1 Q4 = 2
Solution
5 8 12

Not a Q4 = 3 Q4 = 2
Solution
9 13

Solution Solution

8 Queens Problem
The solution for the 8 Queens problem is easily obtained using backtracking
algorithm.
The aim of the problem is to place 8 queens on 8 * 8 chessboard in such a way that
none of queens hit each other.

The following constraints are used to place queens on the chess board.
No two queens should be placed on the same diagonal
No two queens should be placed on the same column
53
No two queens should be placed on the same row
Let us assume two queens are placed at positions ( i , j ) and ( k , l ) then, the two
queens are said to be in same diagonal, if the following conditions are satisfied.
i+j=k+1
ij=k1
The above two equations can be rearranged as follows
i-k=1-j
ik=j1
Combining these two equations
Abs ( i k ) = Abs ( j - 1 )
If this condition is true, then the queens are in the same diagonal.
In any stage, we are unable to place a queen then we have to backtrack and change
the position of the previous queen.
This is repeated till we place all the 8 queens on the board.
Algorithm
Algorithm Nqueens(k,n)
Begin
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)
end for
End
Algorithm place(k,i)
Begin
for j=1 to k-1 do
if x[j] = i
return false
else
return true

54
end for
End
Time complexity of 8 Queen problem is O(k-1)

Q1

Q1
Q2

Q1
Q2
Q3

55
Q1
Q2
Q3
Q4

Q1
Q2
Q3
Q4
Q5

Q1
Q2
Q3
Q4
Q5
Q6

Q1
Q2
Q3

56
Q4
Q5
Q6
Q7

Q1
Q2
Q3
Q4
Q5
Q6
Q7
Q8

1
Q1 = 1
Q1 = 2 Q1 = 3
2
9 14
Q2 = 3 Q2 = 4 Q2 = 6

Q3 = 2 Q3 = 6 Q3 = 1

Q4 = 7 Q4 = 3 Q4 = 3

Q5 = 1 Q5 = 7 Q5 = 5

Q6 = 2 Not a Q6 = 2
Solution

57
Q7 = 4 Q7 = 8
3 15
11

Not a Q8 = 5
16
Solution
4 11

5
Solution
12 17
Analysis
Time complexity of 8 Queens problem is O ( k 1 )

6 13 18

Sum of Subsets
7 19
The sum of subsets is solved using the backtracking method.
Problem Definition
Given n distinct positive numbers (weights) Wi, 1 i n, and m, find all subsets of
the Wi whose sum
8 is m. 20
The element Xi of the solution vector is either one (or) zero depending on whether
the weight Wi is included or not.
Xi = 1 , Wi is included
21
Xi = 0 , Wi is not included
Solution to Sum of Subsets Problem
Sort the weights in ascending order.
The root of the space tree represents the starting point, with no decision about the
given elements.
The left and right child represents inclusion and exclusion of Xi in a set.
The node to be expanded, check it with the following condition.
k
Wi Xi + Wi +1 m
i=1
The bounded node can be identified with the following condition.
The choice for the bounding function is Bk (X1, Xk ) =true iff

58
k n
Wi Xi + Wi m
i=1 i=k+1
Backtrack the bounded node and find alternative solution.
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 can terminate the node as non promising if either of the 2
ingratiation holds
S + Wi +1 > m (where S in too large)
S + Wi +1 < m (where S in too small)
k
S = Wi Xi
i=1
Constraints
Implicit Constraints
No two elements in the subset can be same.
Explicit Constraints
The tuple values can be any of the value between 1 and n and needed to be
ascending order.
Procedure
Let S be a set of elements and m be the expected sum of subsets.
Step 1
Start with an empty set.
Step 2
Add to the subset, the next element from the list
Step 3
If the subset is having sum m then stop with that subset as the solution.
Step 4
If the subset if not feasible (or) if we have reached the end of the set then
backtrack through the subset until we find the most suitable value.
Step 5
If the subset is feasible, then repeat step 2.
Step 6

59
If we have visited, all the elements without finding a suitable subset and no
backtracking is possible then stop without solution.
Example
Consider a set S = {3, 5, 6, 7} m=15. Solve it for obtaining sum of subsets?

Subset Sum Action


3 3 < 15 Add next element
3, 5 8 < 15 Add next element
3, 5, 6 14 < 15 Add next element
3, 5, 6, 7 21 > 15 Backtrack ( sum exceeds )
Condition satisfies. Subset = sum.
3,5,7 15 = 15
Solution obtained

Solution set X = { 1, 1, 0, 1 }
Algorithm
Algorithm sumofsubset(s,m,n)
Begin
solution := 0
for i <- 1 to n
solution <- solution + Wi
if ( solution > m )
solution <- solution Wi
Xi <- 0
end if
Xi <- 1
end for
write The tuples X[1n]
End
Graph Coloring
Graph coloring is the problem of coloring each vertex in a graph is also solved by
using backtracking technique.
Aim
To color the nodes of a graph in such a way that no two adjacent nodes have the
same color.
Description

60
Let G be a graph and m be a given positive integer.
The nodes of G can be colored in such a way that no two adjacent nodes have the
same color needs m colors are used. This is called m - colorability decision problem (
or ) m colorability problem (or) Graph Coloring Problem.
If the degree of the given graph is d, then the graph can be colored with d+1 colors.
Procedure of Graph Coloring
The basic idea behind the solution is that once a vertex is assigned a color then all
the vertices which are connected to that are refrained from using the same color.
We have some set of color C, then initially all the color are available to all the
vertices.
We start assigning colors to the vertices, the number of available colors to the
remaining vertices would also start reducing depending on the existence of edges
between vertices.
Process of assigning color to vertices
1.We are given graph G = (V,E) and set of colors C=(C1, C2, Cn )
2. Sort the vertices in non decreasing order of their node degree.
3. Build a list of available colors to each vertex. Initially all colors are available to all
vertices.
4. Now select the first vertex from the set of sorted vertices and for the selected vertex
assign the first available color.That color is not assigned to its adjacent vertices.
For example the vertex V1 can be assigned color C1, then all the vertices which are
connected to V1 can not use C1. So remove C1 from available colors for all vertices which are
connected to V1.
5. Repeat step 4 till no vertex remains on asigned.
Chromatic Number
The chromatic number of a graph G is the smallest number of color need to
color the vertices of G, so that no 2 adjacent vertices have the same color.
Example
Consider the following graph.

2 5

61
3 4
Step 1:
Vertex Adjacency Vertex

1 2,3
2 1,3,5
3 1,2,4,5
4 3,5
5 2,3,4

Step 2:
Let the set of available colors
C={Red , Green , Blue, Yellow }
Sort all the vertices in decreasing order of their node degrees.
{ V3, V2, V5, V1, V4 }
Now , we start with V3 and assign the first color Red to it, them all its adjacent
vertices V2, V5, V1 and V4 can not use Red.
So we can construct a table

Vertex Red Green Blue Yellow


V3
V2 X
V5 X
V1 X
V4 X

Red color is assigned to V3. Red color is not assigned to V2, V5, V1, V4

Step 3:
Vertex Red Green Blue Yellow
V3 X
V2 X
V5 X X
V1 X X
V4 62
X
Green color is assigned to V2. Green color is not assigned to V3, V5, V1

Step 4:
Vertex Red Green Blue Yellow
V3 X X
V2 X X
V5 X X
V1
X X
V4 X X

Blue color is assigned to V5 and V1 . Because V5 is not a adjacent vertex of V1

Step 5:
Vertex Red Green Blue Yellow
V3 X X
V2 X X
V5 X X
V1
X X
V4 X X

Green color is assigned to V4. Because V2 is not a adjacent vertex of V4


Chromatic Number = 3
Planar Graph
A graph is said to be planar if it can be drawn in a plane in such a way that no two
edges cross each other. For every planar graph the chromatic number is 4.

63
Algorithm
Algorithm mcoloring(k)
Begin
Repeat
Nextvalue(k)
If x[k] = 0 then
Return
If k = n then
Color the n vertices
Write x[1:n]
Else
Mcoloring(k+1)
Until(false)
End
Algorithm Nextvalue(k)
Begin
x[k] = (x[k] + 1) mod (m+1)
if x[k] = 0 then
return
for j = 1 to n do
if (G[k,j] <> 0 ) and (x[k] = x[j] ) then
break
if j = n+1 then
return
End
Hamiltonian Cycle Problem
Hamiltonian cycle problem is solved by using backtracking method.
Problem Definition
Let G=(V,E) be a connected graph with n vertices. A Hamiltonian cycle is a round
trip path along n edges of G which visits every vertex once and returns to its starting
position. The tour of a Travelling salesperson problem is a Hamiltonian cycle. A tour
may exist (or) not.
Constraints

64
Implicit Constraints
All the vertices (or) nodes should be visited exactly once. The source and destination
point are same.
Explicit Constraints
The graph should be an directed (or) undirected graph.

Example
Solve the Hamiltonian Cycle Problem for the following graph.

1 2 3 4

8 7 6 5

2 3
7
1

8 3 6 8 4

65
7 4 5 2 5

6 5 4 3
6

5 6 3 1
7

4 7 2 8

Algorithm 3 1 1 2
Algorithm Hamiltonian(k)

Begin 1 1

repeat
//Generate values for x[k]
NextValue(k) // Assign a legal next value to x[k]
if ( x[k] = 0 ) then
return
if ( k = n ) then
write ( x [1 : n ] )
else
Hamiltonian ( k + 1 )
until ( false )
End
Algorithm NextValue(k)
Begin
while(1)
x[k] = (x[k] + 1 ) mod ( n+1 ) // Next Vertex
if ( x[k] = 0 ) then
return
if ( G[x[k-1],x[k] ) = 1 ) then // if edge between k & k-1

66
// Is there an edge?
for j=1 to k-1 do
if(x[j] = x[k] ) then
break; // not a distinct vertex
if ( j = k ) then // if true then the vertex is distinct
if ( ( k < n ) or ( k = n ) ) and (G[ x[n] , x[1] ] ) = 1) then
return // return a distinct vertex
until(false)
End

Advantage
Find all situation , can decide Hamiltonian cycles exists ( or ) not.
Disadvantage
Worst case needs exponential time. Normally , the hamiltonian cycle problem takes a
long time.
Analysis
Hamiltonian will find a minimum cost tour if the tour exists.
If the common edge cost is C, the cost of the tour is Cn, Since there are n edges in a
Hamiltonian cycle.
Knapsack Problem
In this method a solution space is defined for the knapsack problem as shown.

The level depends on the number of values.


The solution space consists of all feasible solutions.
A DFS traversal is applied on the solution space tree.
If an object is selected then we travel left child as the left edge carries the cost 1,
indicating that the object is selected.

67
If at any stage of solution, it is found that the constraints are not satisfied then we
backtrack to the previous node and go to the right child indicating that the particular
object is not selected.
This algorithm continues till we reach the left node.
Algorithm
Algorithm Bknap(k,cp,cw)
Begin
if cw+w[k] <= m then
Y[k] = 1
if k < n then
Bknap(k+1, cp+p[k], cw+w[k] )
if(cp+p[k] > fp) and (k=n) then
fp = cp + p[k]
fw = cw = w[k]
for j = 1 to n do
x[j] = y[j]
if(bound(cp,cw,k) >= fp) then
y[k] = 0
if k < n then
Bknap(k+1, cp, cw)
if(cp > fp) and k= n then
fp = cp
fw = cw
for j = 1 to k do
x[j] = y[j]
End
Algorithm Bound(cp , cw , k)
Begin
b = cp
c = cw
for i = k+1 to n do
c = c+ w[i]
if ( c < m) then
b = b + p[i]

68
else
return (b+(1-(c-m) / w[i] ) * p[i] )
end for
return b
End

UNIT V
GRAPH TRAVERSAL
GRAPH

Graph is a non-linear data structure used to represent the network structure.


A graph is a collection of nodes and edges.
G=(V,E)
V is the set of vertices ( or ) nodes
E is the set of edges connecting the nodes
Graph Algorithms
There are two methods for traversing through the nodes of the graph. They are.
* Breadth First Search Traversal
* Depth First Search Traversal
Graph Searching
The Systematic follow up of the edges of the graph in same specific order to visit the
vertices of the graph is called graph searching.
Breadth First Search Traversal
As the name implies, this method traverse the nodes of the graph by searching
through the nodes breadth wise.
Rules
Select an unvisited node V, visit it. Its level is called the current level.

69
From each node X in the current level, in the order in which the level nodes were
visited. Visit all the unvisited neighbors of X. The newly visited nodes from this level
form a new level. This new level becomes the next current level.
Repeat step 2 for all unvisited vertices.
Repeat from step 1 until no more vertices are remaining.
Algorithm
Algorithm BFT ( G , n )
Begin
repeat for i=1 to n
visited[i] = 0
end repeat
repeat for i=1 to n
if visited[i] = 0
BFS(i)
end if
end repeat
end
Algorithm BFS ( v)
u=v
visited [ v ] = 1
repeat while ( true )
repeat for all vertices w adjacent from u
if visited [w] = 0
Add w to Queue
visited [ w ] = 1
end if
end repeat
if Queue is empty
return
end if
delete u from Queue
End while
End BFS

70
Depth First Search Traversal
As the name implies, this method traverse the nodes of the graph by searching
through the nodes depth wise.
Rules
Select an unvisited node V, visit it. Its level is called the current level.
Find unvisited neighbors of the current node, visit it and make it the current node.
If more than one unvisited neighbors then resolve the tie by following the
alphabetical order.
If the current node has no unvisited neighbors, backtrack to its parent and make it
new current node.
Repeat from step 2 and 3 until no more nodes can be visited.
Repeat from step 1 for the remaining nodes.
Algorithm
Algorithm DFT ( G , n )
Begin
repeat for i=1 to n
visited[i] = 0
end repeat
repeat for i=1 to n
if visited[i] = 0
DFS(i)
end if
end repeat
end
Algorithm DFS ( v)
u=v
visited [ v ] = 1
repeat for each vertex w adjacent from v
if visited [w] = 0
DFS( w )
end if
End
Depth First Search Traversal Breadth First Search Traversal

71
This traversal is done with the help of stack This traversal is done with the help of queue
data structure. data structure.
It works using two ordering. The first order is It works using one ordering. The order in
the order in which the vertices are reached for which the vertices are reached in the same
the first time and second order in which the order they get removed from the queue.
vertices become dead.
DFS sequence is composed of tree edges and BFS sequence is composed of tree edges and
back edges. cross edges.
The efficiency of the adjacency matrix graph is The efficiency of the adjacency matrix graph is
O(V2) (V2)
The efficiency of the adjacency list graph is O(| The efficiency of the adjacency list graph is (|
V|+|E|) V|+|E|)
SPANNING TREE

A Spanning Tree is a tree that contains all of the vertices in a graph. A Minimum
Spanning tree is a spanning tree in which the total weights of the edges are
guaranteed to be minimum of all possible trees in a graph. A minimum spanning tree
of an undirected graph G is a tree formed from graph edges that connects all the
vertices of G at lowest total cost.
A minimum spanning tree exists if and only if G is connected. The number of edges
in the minimum spanning tree is |V| - 1 .The minimum spanning tree is a tree because
it is Acyclic. It is spanning because it covers vertex with minimum cost. For any
spanning tree T, if an edge e that is not in T added, a cycle is created. The removal of
any edge on the cycle reinstates the spanning tree property. The cost of the spanning
tree is lowered if e has lower cost than the edge that was removed.

Applications of Minimum Spanning Tree:

House wiring can be done with a minimum length of cable.


It is used to connect all computers in a network with shortest length cable.
So, the following 2 algorithms is used to find the minimum spanning tree for the
given graph.

1. Prims algorithm
2. Kruskals algorithm
Kruskals Algorithm
The Kruskals algorithm is used for determining minimum spanning tree.

72
Rules
Each vertex of a graph is taken as a one node tree.
A minimum weighted edge is selected to connect two trees together.
This iteration continues till all the trees of the forest form a single tree.
Algorithm
sort the edges of G in increasing order by length
keep a sub graph S of G, initially empty
for each edge e in sorted order
if the endpoints of e are disconnected in S
add e to S
return S
Prims Algorithm
The Prims algorithm is used for determining minimum spanning tree.
Rules
The tree is started with the root.
It is connected to the other vertex with minimum weighted edge.
This iteration continues till all the vertices are covered in the tree without any
cycles.
Algorithm
let T be a single vertex x
while (T has fewer than n vertices)
{
find the smallest edge connecting T to G-T
add it to T
}

73
Introduction to NP Completeness

Mostly for all the graph theory problem is the running time is either linear or only
slightly more than the linear (O (|E| log |E|)).
The single source unweighted shortest path problem for directed graph is also
solvable in linear time.
There are no known algorithms that are guaranteed to run in polynomial time. We
will see that there are a host of important problems that are roughly equivalent in
complexity. These problems form a class called the NP- Completeness.

Easy Vs Hard

When classifying problems, the first step is to examine the boundaries. For instance,
the GCD algorithm, when applied to 2 nos N & M, takes O (log (N)) time.
A GCD algorithm is really taking time that is linear in the amount or size of input. At
the other end of the spectrum lie some truly hard problems.
That is real numbers are not sufficient to express a solution to x 2 < 0, one can prove
that computers cannot solve every problems. These impossible problems are called
undecidable problems.
One particular undecidable problem is the halting problem. For eg, is it possible to
have C complier have an extra feature that not only detects syntax error but also all
infinite loop, for the reason of these problems might have a hard time checking itself,
they are sometime called recursively undecidable problem.

The class NP

NP stands for Nondeterministic Polynomial- time.

74
A deterministic machine, at each point in time, is executing an instruction.
Depending on the instruction, it then goes to some next instruction which is unique.
A nondeterministic machine has a choice of next steps. It is free to choose any that it
wishes, and if one of these steps leads to a solution , it will always choose the correct
one.
It is very useful theoretical construct .A Simple way to check if a problem is in NP is
to phrase the problem as a yes/no question.
The problem is in NP if, in polynomial time. We can prove that any yes instance is
correct. The class NP includes all problems that have polynomial time solutions.

NP Complete Problems

Among all the problems known to be in NP, there is a subset, known as the NP-
complete problems which contains the hardest.
An NP- Complete problem has the property that any problem in NP can be
polynomially reduced to it.
The reason that NP- complete problems are the hardest problem that is NP- complete
can essentially be used in NP, with only a polynomial amount of overhead.

Eg: Travelling Salesman problem

Given a complete graph G= (V, E) with edge cost an integer K, is there a simple
cycle that visits all vertices and has total cost <= K.
The main application of this problem is, in printed circuit boards need to have holes
punched. So that chips, resistors and other electronic components can be placed.
The time required for positioning depends on the distance traveled from one hole to
another. The main aim is to minimize the amount of time spent for traveling.
The problem is NP- complete to show that it is NP- complete, are construct a new
graph G. G has the same vertices as G. for G, each edge (V, W) has a weight of 1,
if (V,W) G and 2 otherwise. We choose k= |v|.

75
V1

V2 V3

V4 V5

V1
1 1

2
V2 V3

2 1
1
1 2

2
V4 V5

It is easy to verify that G has a Hamiltonian cycle if and only if G has a traveling
salesman tour of total weight |v|.
Hamiltonian cycle problem asks for a simple cycle that contains every vertex. No
linear algorithm is known for this problem.
Some of other NP complete problems are binpacking, Knapsack, graph coloring and
clique.

76
Unit I Algorithm Analysis
Part A
1. What is an algorithm?
2. What is recursive algorithm?
3. What is space complexity?
4. What is time complexity?
5. Define Big oh notation.
6. Enumerate some important types of problems.
7. How will you measure Input size of algorithms?
8. What are algorithm design techniques?
9. How is an algorithms time efficiency measured?
10. Mention any four classes of algorithm efficiency?
11. Define order of an algorithm?
12. Define loop.
13. Give the syntax for FOR loop.
14. What is time space tradeoff?
15. What are the various asymptotic notations available?
16. What is a recurrence equations?
17. What is worst case efficiency?
18. What is best case efficiency?
19. What is average case efficiency?
20. Define Big Omega notation.
21. Define Big Theta notation.
22. What is recurrence equations?

Part B
1. Define the asymptotic notations used for best case average case and worst case analysis of
algorithms.
2. Write an algorithm for finding maximum element of an array, perform best, worst and
average case complexity with appropriate order notations.
3. Describe briefly the notations of complexity of an algorithm.
4. List the properties of various asymptotic notations.
5. Explain the various criteria used for analyzing algorithms.

77
6. Briefly explain how to solve recurrence equations with an example.
7. What are the loop control statements available in C++?
8. Describe about the analysis of linear search with an example, if any.
9. What are the various methods of solving recurrences? Explain them in Brief.
10. Derive the recurrence equation for Fibonacci series. Perform complexity analysis for the
same.
11. Define Input Size. Write an algorithm for matrix addition and construct step table for it.
12. Explain with examples Big Oh, Omega, Theta and Little Oh asymptotic notations.

Unit 2 Divide and Conquer & Greedy Algorithms


Part A

1. Define merge sort.


2. Give the algorithm for merge sort.
3. Give the algorithm to merge two sorted arrays into one.
4. What is binary search?
5. Define the Divide and Conquer method.
6. What is the maximum and minimum problem?
7. Give the recurrence relation of divide and conquer.
8. What is the recurrence relation of merge sort?
9. What is meant by feasible solution?
10. Write any two characteristics of Greedy Algorithm.
11. Define optimal solution.
12. What is knapsack problem?
13. Give the time efficiency and drawback of merge sort algorithm.
14. What is the average case complexity of linear search algorithm?

Part B

1. Explain in detail merge sort. Illustrate the algorithm with a numeric example. Provide
complete analysis of the same.
2. Give a detailed note on divide and conquer techniques.
3. Discuss the use of Greedy method in solving Knapsack problem and container loading.
4. Briefly explain about the binary search and write the algorithm using recursion and
iteration for binary search.
5. Explain in brief about finding the maximum and minimum elements in a set of n
elements.
6. Write a pseudo code for a divide and conquer algorithm for finding values of both the
largest and the smallest elements in any array of n numbers
7. Write an algorithm for searching an element using binary search method. Give an
example.
8. Mention any three search algorithms which are preferred in general? Why?

78
Unit 3 Dynamic Programming
Part A
1. What is dynamic programming?
2. Define multistage graph.
3. Write the difference between the Greedy method and Dynamic programming.
4. What are the features of dynamic programming?
5. What are the drawbacks of dynamic programming?
6. Write the general procedure of dynamic programming.
7. Define principle of optimality.
8. Give an example of dynamic programming and explain.
9. Explain any one method of finding the shortest path.
10. Define 0/1 knapsack problem.
11. What is the formula to calculate optimal solution in 0/1 knapsack problem?
12. Write about travelling salesperson problem.
13. Write some applications of TSP.
14. Give the time complexity and space complexity of TSP.
15. How will you construct an optimal binary search tree?

Part B

1. Describe the travelling salesman problem and discuss how to solve it using dynamic
programming.
2. Write an algorithm for finding an optimal binary search tree.
3. What is dynamic programming? Explain in detail.
4. Explain in detail about the multistage graphs.
5. Explain the concepts of travelling salesperson problem.
6. How to insert and delete an element in a Binary Search Tree?
7. Write and explain the concepts of 0/1 knapsack problem using dynamic programming.
8. Write an algorithm of all pairs shortest path problem.
9. Solve the following 0/1 knapsack problem using dynamic programming m = 6, n = 3,
(w1,w2,w3) = (2,3,3), (p1,p2,p3) = (1,2,4).
10. What is Travelling Sales person problem and what are its applications?
11. Write a pseudocode of the dynamic programming algorithm for solving Optimal Binary
Search tree and determine its time and space efficiencies.

79
Unit 4 Backtracking
Part A

1. What is backtracking?
2. What are the requirements that are needed for performing Backtracking?
3. What are the factors that influence the efficiency of the backtracking algorithm?
4. State the principle of backtracking?
5. What is n queens problem?
6. Draw the solution for the 4 queen problem.
7. State 8 queens problem.
8. How can the output of a backtracking algorithm be thought of?
9. What is knapsack problem?
10. Define the Hamiltonian circuit.
11. State Sum of Subsets problem.
12. State m - colorability decision problem.
13. Define chromatic number of the graph.

Part B

1. Explain subset sum problem and discuss the possible solution strategies using
backtracking.
2. Write short notes on n Queens problem.
3. Discuss the use of Greedy method in solving Knapsack problem and subset sum
problem.
4. What is backtracking? Explain in detail.
5. Explain in detail about the graph coloring techniques.
6. Explain the applications of backtracking.

80
Unit 5 Graph Traversals and Branch & Bound

Part A
1. What is minimum cost spanning tree?
2. When do you say a tree as minimum spanning tree?
3. What is Hamiltonian cycle in an undirected graph?
4. What is branch and bound? Explain in detail.
5. When can a search path be terminated in a branch and bound algorithm?
6. Compare backtracking and branch and bound.
7. What are the additional features required in branch and bound when compared to
backtracking?
8. What are the searching techniques that are commonly used in branch and bound method?
9. What are NP hard and NP complete problems?
10. What is maxclique problem?
11. What is approximate solution?
12. What is decision problem?
13. What is traversal of a graph? What are the types of traversal?
14. What is a graph? How are the graphs represented?
15. Define the term strongly connected components.

Part B

1. Explain the method of finding the minimum spanning tree for a connected graph using
Prims algorithm.
2. How will you find the shortest path between two given vertices using Dijkstras
algorithm? Explain.
3. Discuss the solution for traveling salesman problem using branch and bound technique.
4. Define spanning tree? Discuss the design steps in Prims Algorithm to construct minimum
spanning tree with an example.
5. Give a suitable example and explain the Breadth First search and Depth first search
algorithms.
6. Write the Dijkstras algorithm for single source shortest path problem
7. Explain Kruskals algorithm.
8. Explain the difference between Breadth First search and Depth first search.
9. Explain in detail about NP hard Graph problems.
10. Write and explain the techniques in branch and bound method.
11. Write and explain the concepts of 0/1 knapsack problem.
12. Prove that any weighted connected graph with distinct weights has exactly one minimum
spanning tree
13. Explain the terms: NP-Hard and NP Complete problems
14. Discuss with examples, various graph traversal techniques.
15. Explain how bi connected components can be determined for a given graph. What is its
complexity.
16. Mention salient features of NP hard and NP complete classes. Discuss two examples for
each case.
17. Differentiate between NP Complete and NP hard.

81
82

Anda mungkin juga menyukai