0% found this document useful (0 votes)
2 views

Design and Analysis of Algorithms

The document provides lecture notes for a course on Design and Analysis of Algorithms at Malla Reddy College of Engineering & Technology, focusing on algorithm performance, data structures, and design methods. It outlines course objectives, unit topics including Divide and Conquer, Greedy methods, Dynamic Programming, Backtracking, and Branch and Bound, along with performance analysis criteria such as time and space complexity. The notes also introduce asymptotic notation for analyzing algorithm efficiency and provide examples of various algorithm complexities.

Uploaded by

ytdownloader00
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Design and Analysis of Algorithms

The document provides lecture notes for a course on Design and Analysis of Algorithms at Malla Reddy College of Engineering & Technology, focusing on algorithm performance, data structures, and design methods. It outlines course objectives, unit topics including Divide and Conquer, Greedy methods, Dynamic Programming, Backtracking, and Branch and Bound, along with performance analysis criteria such as time and space complexity. The notes also introduce asymptotic notation for analyzing algorithm efficiency and provide examples of various algorithm complexities.

Uploaded by

ytdownloader00
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 103

MRCET CAMPUS (AUTONOMOUS INSTITUTION – UGC, GOVT.

OF INDIA)

Department of CSE
(Emerging Technologies)
(Data Science, Cyber Security and Internet of Things)

DESIGN AND ANALYSIS OF


ALGORITHMS
(R20A0505)

LECTURE NOTES
B.Tech – CSE (Emerging Technologies) R-20

DESIGN AND ANALYSIS OF ALGORITHMS

LECTURE NOTES

B.TECH (R-20 Regulation)


(II YEAR – II SEM)
(2022-23)

DEPARTMENT OF CSE
(EMERGING TECHNOLOGIES)
(Data Science, Cyber Security and Internet of Things)

MALLA REDDY COLLEGE OF ENGINEERING &


TECHNOLOGY
(Autonomous Institution – UGC, Govt. of India)
Recognized under 2(f) and 12 (B) of UGC ACT 1956
(Affiliated to JNTUH, Hyderabad, Approved by AICTE - Accredited by NBA & NAAC – ‘A’ Grade - ISO 9001:2015 Certified)
Maisammaguda, Dhulapally (Post Via. Hakimpet), Secunderabad – 500100, Telangana State, India
B.Tech – CSE (Emerging Technologies) R-20

Department of Computer Science and Engineering

(EMERGING TECHNOLOGIES)
Vision

 To be at the forefront of Emerging Technologies and to evolve as a Centre of


Excellence in Research, Learning and Consultancy to foster the students into
globally competent professionals useful to the Society.

Mission

The department of CSE (Emerging Technologies) is committed to:

 To offer highest Professional and Academic Standards in terms of Personal growth


and satisfaction.

 Make the society as the hub of emerging technologies and thereby capture
opportunities in new age technologies.

 To create a benchmark in the areas of Research, Education and Public Outreach.

 To provide students a platform where independent learning and scientific study are
encouraged with emphasis on latest engineering techniques.

Quality Policy

 To pursue continual improvement of teaching learning process of Undergraduate and


Post Graduate programs in Engineering & Management vigorously.

 To provide state of art infrastructure and expertise to impart the quality education
and research environment to students for a complete learning experiences.

 Developing students with a disciplined and integrated personality.

 To offer quality relevant and cost effective programmes to produce engineers as per
requirements of the industry need.

For more information: www.mrcet.ac.in


B.Tech – CSE (Emerging Technologies) R-20

INDEX
B.Tech – CSE (Emerging Technologies) R-20

MALLA REDDY COLLEGE OF ENGINEERING & TECHNOLOGY


II Year B. Tech CSE-Emerging Technologies-Cyber Security ‐ II Sem L T/P/D C
3 -/-/- 3
(R20A0505) DESIGN AND ANALYSIS OF ALGORITHMS

COURSE OBJECTIVES:
1. To analyze performance of algorithms.
2. To choose the appropriate data structure and algorithm design method for a specified
application.
3. To understand how the choice of data structures and algorithm design methods impacts the
performance of programs.
4. To solve problems using algorithm design methods such as the greedy method, divide and
conquer, dynamic programming, backtracking and branch and bound.
5. To understand the differences between tractable and intractable problems and to introduce P
and NP classes.

UNIT-I Introduction: Algorithms, Pseudo code for expressing algorithms, performance analysis-
Space complexity, Time Complexity, Asymptotic notation- Big oh notation, omega notation,
theta notation and little oh notation. Divide and Conquer: General method. Applications- Binary
search, Quick sort, merge sort, Strassen’s matrix multiplication.

UNIT-II Disjoint set operations, Union and Find algorithms, AND/OR graphs, Connected
components, Bi-connected components.
Greedy method: General method, applications- Job sequencing with deadlines, Knapsack
problem, Spanning trees, Minimum cost spanning trees, Single source shortest path problem.

UNIT-III Dynamic Programming: General method, applications- Matrix chained multiplication,


Optimal binary search trees, 0/1 Knapsack problem, All pairs shortest path problem, Traveling
sales person problem, Reliability design.

UNIT-IV Backtracking: General method, Applications- n-queue problem, Sum of subsets


problem, Graph coloring, Hamiltonian cycles.

UNIT-V Branch and Bound: General method, applications- Travelling sales person problem, 0/1
Knapsack problem- LC branch and Bound solution, FIFO branch and Bound solution.
NP-Hard and NP-Complete Problems: Basic concepts, Non deterministic algorithms, NP-Hard
and NPComplete classes, NP-Hard problems, Cook’s theorem.

TEXT BOOKS:
1. Fundamentals of Computer Algorithms, Ellis Horowitz, SartajSahni and Rajasekharan,
Universities press
2. Design and Analysis of Algorithms, P. h. Dave,2ndedition,Pearson Education.

REFERENCES:
1. Introduction to the Design And Analysis of Algorithms A Levitin Pearson Education
2. Algorithm Design foundations Analysis and Internet examples, M.T.Goodrich and R Tomassia
John Wiley and sons
3. Design and Analysis of Algorithms, S. Sridhar, Oxford Univ.Press
4. Design and Analysis of Algorithms,Aho , Ulman and Hopcraft , Pearson Education.
5. Foundations of Algorithms, R. NeapolitanandK.Naimipour , 4th edition
B.Tech – CSE (Emerging Technologies) R-20

UNIT-I
Introduction: Algorithms, Pseudo code for expressing algorithms, performance analysis-
Space complexity, Time Complexity, Asymptotic notation- Big oh notation, omega notation,
theta notation and little oh notation.
Divide and Conquer: General method. Applications- Binary search, Quick sort, merge sort,
Strassen’s matrix multiplication.

INTRODUCTION TO ALGORITHM

What is an Algorithm?
Algorithm is a set of steps to complete a task.

For example,

Task: to make a cup of tea.

Algorithm:

· add water and milk to the kettle,


· boil it, add tea leaves,
· Add sugar, and then serve it in cup.

"a set of steps to accomplish or complete a task that is described precisely enough that a
computer can run it ".

• An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In


addition, all algorithms must satisfy the following criteria:
• Input. Zero or more quantities are externally supplied.
• Output. At least one quantity is produced.
• Definiteness. Each instruction is clear and unambiguous.
• Finiteness. The algorithm terminates after a finite number of steps.
• Effectiveness. Every instruction must be very basic enough and must be feasible.
• Algorithms that are definite and effective are also called computational procedures.
• A program is the expression of an algorithm in a programming language
PSEUDOCODE:

• Algorithm can be represented in Text mode and Graphic mode


• Graphical representation is called Flowchart
• Text mode most often represented in close to any High level language
such as C, Pascal Pseudocode
• Pseudocode: High-level description of an algorithm.
 More structured than plain English.
 Less detailed than a program.
 Preferred notation for describing algorithms.
 Hides program design issues.
• Example of Pseudocode:

• To find the max element of an array

Algorithm arrayMax(A, n)
Input array A of n integers
Output maximum element of A
currentMax  A[0]
for i  1 to n  1 do
if A[i]  currentMax then
currentMax  A[i]
return currentMax

DESIGN AND ANALYSIS OF ALGORITHMS Page 6


RULES FOR PSEUDOCODE
1. Write only one stmt per line Each stmt in your pseudocode should express just one action for the
computer. If task list is properly drawn, then in most cases each task will correspond to one line of
pseudocode.
2. Capitalize initial keyword In the example above, READ and WRITE are in caps. There are just a
few keywords we will use: READ, WRITE, IF, ELSE, ENDIF, WHILE, ENDWHILE, REPEAT,
UNTIL
3. Indent to show hierarchy We will use a particular indentation pattern in each of the design
structures:
SEQUENCE: keep statements that are “stacked” in sequence all starting in the same column.
SELECTION: indent the statements that fall inside the selection structure, but not the keywords that
form the selection.
LOOPING: indent the statements that fall inside the loop, but not the keywords that form the loop
4. Keep stmts language independent Resist the urge to write in whatever language you are most
comfortable with. In the long run, you will save time! There may be special features available in the
language you plan to eventually write the program in; if you are SURE it will be written in that
language, then you can use the features. If not, thenavoid using the special features.

PERFORMANCE ANALYSIS:

• What are the Criteria for judging algorithms that have a more direct relationship to performance?
• computing time and storage requirements.

Performance evaluation can be loosely divided into two major phases:


• a priori estimates and
• a posteriori testing.

• The space complexity of an algorithm is the amount of memory it needs to run to completion.
• The time complexity of an algorithm is the amount of computer time it needs to run to
completion.

Space Complexity:

• Space Complexity Example:


Algorithm abc(a,b,c)
{
return a+b++*c+(a+b-c)/(a+b) +4.0;
}

The Space needed by each of these algorithms is seen to be the sum of the followingcomponent.

DESIGN AND ANALYSIS OF ALGORITHMS Page 7


1.A fixed part that is independent of the characteristics (eg:number,size)of the inputs andoutputs.
The part typically includes the instruction space (ie. Space for the code), space for simple variable
and fixed-size component variables (also called aggregate) space for constants, andso on.

2. A variable part that consists of the space needed by component variables whose size is dependent on
the particular problem instance being solved, the space needed by referenced variables (to the extent
that is depends on instance characteristics), and the recursion stack space.

The space requirement s(p) of any algorithm p may therefore be written as,
S(P) = c+ Sp(Instance characteristics)
Where ‘c’ is a constant.

Example 2:
Algorithm sum(a,n)
{
s=0.0;
for I=1 to n do
s= s+a[I];
return s;
}
 The problem instances for this algorithm are characterized by n,the number ofelements to be
summed. The space needed d by ‘n’ is one word, since it is of type integer.
 The space needed by ‘a’a is the space needed by variables of tyepe array of floating point
numbers.
 This is atleast ‘n’ words, since ‘a’ must be large enough to hold the ‘n’elements to be summed.
• So,we obtain Ssum(n)>=(n+s) [ n for a[],one each for n,I a& s]

Time Complexity:

• The time T(p) taken by a program P is the sum of the compile time and therun time(execution
time)

• The compile time does not depend on the instance characteristics. Also we may assume that a
compiled program will be run several times without recompilation .Thisrum time is denoted by
tp(instance characteristics).

• The number of steps any problem statement is assigned depends on the kind ofstatement.

• For example, comments à 0 steps.


Assignment statements is 1 steps.

[Which does not involve any calls to other algorithms]


Interactive statement such as for, while & repeat-untilà Control part of the statement.

We introduce a variable, count into the program statement to increment count with
initial value 0.Statement to increment count by the appropriate amount are introduced
into the program.

This is done so that each time a statement in the original program is executes
count is incremented by the step count of that statement.

DESIGN AND ANALYSIS OF ALGORITHMS Page 8


Algorithm:
Algorithm sum(a,n)
{
s= 0.0;
count = count+1;
for I=1 to n do
{
count =count+1;
s=s+a[I];
count=count+1;
}
count=count+1;
count=count+1;
return s;
}

1. If the count is zero to start with, then it will be 2n+3 on termination. So each invocation
of sum execute a total of 2n+3 steps.
2. The second method to determine the step count of an algorithm is to build atable in
which we list the total number of steps contributes by each statement.

o First determine the number of steps per execution (s/e) of the statement and
thetotal number of times (ie., frequency) each statement is executed.

o By combining these two quantities, the total contribution of all statements, the
step count for the entire algorithm is obtained.

Statement Steps per Frequency Total


execution
1. Algorithm Sum(a,n) 0 - 0
2.{ 0 - 0
3. S=0.0; 1 1 1
4. for I=1 to n do 1 n+1 n+1
5. s=s+a[I]; 1 n n
6. return s; 1 1 1
7. } 0 - 0

Total 2n+3

DESIGN AND ANALYSIS OF ALGORITHMS Page 9


We usually consider one algorithm to be more efficient than another if its worst-case running
time has a smaller order of growth.

Complexity of Algorithms

The complexity of an algorithm M is the function f(n) which gives the running time and/or
storage space requirement of the algorithm in terms of the size ‘n’ of the input data. Mostly,
the storage space required by an algorithm is simply a multiple of the data size ‘n’.

Complexity shall refer to the running time of the algorithm.

The function f(n), gives the running time of an algorithm, depends not only on the size ‘n’ of
the input data but also on the particular data. The complexity function f(n) for certain cases
are:

1. Best Case : The minimum possible value of f(n) is called the best case.

2. Average Case : The expected value of f(n).

3. Worst Case : The maximum value of f(n) for any key possible input.

ASYMPTOTIC NOTATION

Formal way notation to speak about functions and classify them

The following notations are commonly use notations in performance analysis and used to
characterize the complexity of an algorithm:

1. Big–OH (O) ,
2. Big–OMEGA (Ω),
3. Big–THETA (Θ) and
4. Little–OH (o)

Asymptotic Analysis of Algorithms:

Our approach is based on the asymptotic complexity measure. This means that we don’t try to
count the exact number of steps of a program, but how that number grows with the size of the
input to the program. That gives us a measure that will work for different operating systems,
compilers and CPUs. The asymptotic complexity is written using big-O notation.

· It is a way to describe the characteristics of a function in the limit.


· It describes the rate of growth of functions.
· Focus on what’s important by abstracting away low-order terms and constant factors.
· It is a way to compare “sizes” of functions:

O≈ ≤

DESIGN AND ANALYSIS OF ALGORITHMS Page 10


Ω≈ ≥
Θ≈=
o ≈ <
ω≈>

Time complexity Name Example


O(1) Constant Adding an element to the
front of a linked list
O(logn) Logarithmic Finding an element in a
sorted array
O (n) Linear Finding an element in an
unsorted array
O(nlog n) Linear Logarithmic Sorting n items
by ‘divide-and-conquer’-
Mergesort
O(n2) Quadratic Shortest path between two
nodes in a graph
O(n3) Cubic Matrix Multiplication
O(2n) Exponential The Towers of Hanoi
problem

Big ‘oh’: the function f(n)=O(g(n)) iff there exist positive constants c and no such that
f(n)<=c*g(n) for all n, n>= no.
Omega: the function f(n)=(g(n)) iff there exist positive constants c and no such that
f(n) >= c*g(n) for all n, n >= no.
Theta: the function f(n)=(g(n)) iff there exist positive constants c1,c2 and no such that c1
g(n) <= f(n) <= c2 g(n) for all n, n >= no

Big-O Notation

This notation gives the tight upper bound of the given function. Generally we represent it as
f(n) = O(g (11)). That means, at larger values of n, the upper bound off(n) is g(n). For
example, if f(n) = n4 + 100n2 + 10n + 50 is the given algorithm, then n4 is g(n). That means
g(n) gives the maximum rate of growth for f(n) at larger values of n.

O —notation defined as O(g(n)) = {f(n): there exist positive constants c and no such that
0 <= f(n) <= cg(n) for all n >= no}. g(n) is an asymptotic tight upper bound for f(n). Our
objective is to give some rate of growth g(n) which is greater than given algorithms rate of
growth f(n).

In general, we do not consider lower values of n. That means the rate of growth at lower
values of n is not important. In the below figure, no is the point from which we consider the
rate of growths for a given algorithm. Below no the rate of growths may be different.

DESIGN AND ANALYSIS OF ALGORITHMS Page 11


Note Analyze the algorithms at larger values of n only What this means is, below no we do
not care for rates of growth.

Omega— Ω notation

Similar to above discussion, this notation gives the tighter lower bound of the given
algorithm and we represent it as f(n) = Ω (g(n)). That means, at larger values of n, the
tighter lower bound of f(n) is g
For example, if f(n) = 100n2 + 10n + 50, g(n) is Ω (n2).
The . Ω. notation as be defined as Ω (g (n)) = {f(n): there exist positive constants c and
no such that 0 <= cg (n) <= f(n) for all n >= no}. g(n) is an asymptotic lower bound for
f(n). Ω (g (n)) is the set of functions with smaller or same order of growth as f(n).

Theta- Θ notation
This notation decides whether the upper and lower bounds of a given function are same or
not. The average running time of algorithm is always between lower bound and upper bound.

DESIGN AND ANALYSIS OF ALGORITHMS Page 12


If the upper bound (O) and lower bound (Ω) gives the same result then Θ notation will also
have the same rate of growth. As an example, let us assume that f(n) = 10n + n is the
expression. Then, its tight upper bound g(n) is O(n). The rate of growth in best case is g (n) =
0(n). In this case, rate of growths in best case and worst are same. As a result, the average
case will also be same.

None: For a given function (algorithm), if the rate of growths (bounds) for O and Ω are not
same then the rate of growth Θ case may not be same.

Now consider the definition of Θ notation It is defined as Θ (g(n)) = {f(71): there exist
positive constants C1, C2 and no such that O<=5 c1g(n) <= f(n) <= c2g(n) for all n >= no}.
g(n) is an asymptotic tight bound for f(n). Θ (g(n)) is the set of functions with the same
order of growth as g(n).

Important Notes

For analysis (best case, worst case and average) we try to give upper bound (O) and lower
bound (Ω) and average running time (Θ). From the above examples, it should also be clear
that, for a given function (algorithm) getting upper bound (O) and lower bound (Ω) and
average running time (Θ) may not be possible always.
For example, if we are discussing the best case of an algorithm, then we try to give upper
bound (O) and lower bound (Ω) and average running time (Θ).
In the remaining chapters we generally concentrate on upper bound (O) because knowing
lower bound (Ω) of an algorithm is of no practical importance and we use 9 notation if upper
bound (O) and lower bound (Ω) are same.

Little Oh Notation

The little Oh is denoted as o. It is defined as : Let, f(n} and g(n} be the non negative
functions then

DESIGN AND ANALYSIS OF ALGORITHMS Page 13


DIVIDE AND CONQUER

General Method

In divide and conquer method, a given problem is,


i) Divided into smaller subproblems.
ii) These subproblems are solved independently.
iii) Combining all the solutions of subproblems into a solution of the whole.

If the subproblems are large enough then divide and conquer is reapplied. The generated subproblems
are usually of some type as the original problem.

Hence recursive algorithms are used in divide and conquer strategy.

DESIGN AND ANALYSIS OF ALGORITHMS Page 14


Algorithm DAndC(P)
{
if small(P) then return
S(P)else{
divide P into smaller instances P1,P2,P3…Pk;
apply DAndC to each of these subprograms; // means DAndC(P1), DAndC(P2)…..
DAndC(Pk)
return combine(DAndC(P1), DAndC(P2)….. DAndC(Pk));
}
}
//PProblem
//Here small(P) Boolean value function. If it is true, then the function S is
//invoked

Time Complexity of DAndC algorithm:


T(n) = T(1) if n=1
aT(n/b)+f(n) if n>1

a,b contants.
This is called the general divide and-conquer recurrence.

Example for GENERAL METHOD:


As an example, let us consider the problem of computing the sum of n numbers a0, ... an-1.
If n > 1, we can divide the problem into two instances of the same problem. They are sum of
the first | n/2|numbers
Compute the sum of the 1st [n/2] numbers, and then compute the sum of another n/2 numbers.
Combine the answers of two n/2 numbers sum.
i.e.,
a0 + . . . + an-1 =( a0 + ....+ an/2) + (a n/2 + ........ + an-1)
Assuming that size n is a power of b, to simplify our analysis, we get the following
recurrence for the running time T(n).
T(n)=aT(n/b)+f(n)

This is called the general divide and-conquer recurrence.


f(n) is a function that accounts for the time spent on dividing the problem into smaller ones
and on combining their solutions. (For the summation example, a = b = 2 and f (n) = 1.

Advantages of DAndC:
The time spent on executing the problem using DAndC is smaller than other method.
This technique is ideally suited for parallel computation.
This approach provides an efficient algorithm in computer science.

Master Theorem for Divide and Conquer


In all efficient divide and conquer algorithms we will divide the problem into subproblems,
each of which is some part of the original problem, and then perform some additional work to
compute the final answer. As an example, if we consider merge sort [for details, refer Sorting
chapter], it operates on two problems, each of which is half the size of the original, and then
uses O(n) additional work for merging. This gives the running time equation:

DESIGN AND ANALYSIS OF ALGORITHMS Page 15


T(n) = 2T(n)+ O(n)
2

The following theorem can be used to determine the running time of divide and conquer
algorithms. For a given program or algorithm, first we try to find the recurrence relation for
the problem. If the recurrence is of below form then we directly give the answer without
fully solving it.

If the recurrence is of the form T(n) = aT(n) + Θ (nklogpn), where a >= 1, b > 1, k >= O
2
and p is a real number, then we can directly give the answer as:

Applications of Divide and conquer rule or algorithm:


 Binary search,
 Quick sort,
 Merge sort,
 Strassen’s matrix multiplication.

Binary search or Half-interval search algorithm:


1. This algorithm finds the position of a specified input value (the search "key") within
an array sorted by key value.
2. In each step, the algorithm compares the search key value with the key value of the
middle element of the array.
3. If the keys match, then a matching element has been found and its index, or position,
is returned.
4. Otherwise, if the search 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 search
key is greater, then the algorithm repeats on sub array to the right of the middle
element.
5. If the search element is less than the minimum position element or greater than the
maximum position element then this algorithm returns not found.

DESIGN AND ANALYSIS OF ALGORITHMS Page 16


// A recursive binary search function. It returns
// location of x in given array arr[l..r] is present,
// otherwise -1
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;

// If the element is present at the middle


// itself
if (arr[mid] == x)
return mid;

// If element is smaller than mid, then


// it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);

// Else the element can only be present


// in right subarray
return binarySearch(arr, mid + 1, r, x);
}

// We reach here when element is not


// present in array
return -1;
}

Merge Sort:
The merge sort splits the list to be sorted into two equal halves, and places them in separate
arrays. This sorting method is an example of the DIVIDE-AND-CONQUER paradigm i.e. it
breaks the data into two halves and then sorts the two half data sets recursively, and finally
merges them to obtain the complete sorted list. The merge sort is a comparison sort and has an
algorithmic complexity of O (n log n). Elementary implementations of the merge sort make use of
two arrays - one for each half of the data set. The following image depicts the complete procedure
of merge sort.

DESIGN AND ANALYSIS OF ALGORITHMS Page 17


Advantages of Merge Sort:
1. Marginally faster than the heap sort for larger sets
2. Merge Sort always does lesser number of comparisons than Quick Sort. Worst case for
merge sort does about 39% less comparisons against quick sort’s average case.
3. Merge sort is often the best choice for sorting a linked list because the slow random-
access performance of a linked list makes some other algorithms (such as quick sort)
perform poorly, and others (such as heap sort) completely impossible.

Program for Merge sort:


#include<stdio.h>
#include<conio.h>
int n;
void main(){
int i,low,high,z,y;
int a[10];
void mergesort(int a[10],int low,int high);
void display(int a[10]);
clrscr();
printf("\n \t\t mergesort \n");
printf("\n enter the length of the list:");
scanf("%d",&n);
printf("\n enter the list elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
low=0;
high=n-1;
mergesort(a,low,high);
display(a);
getch();
}
void mergesort(int a[10],int low, int high)

DESIGN AND ANALYSIS OF ALGORITHMS Page 18


{
int mid;
void combine(int a[10],int low, int mid, int high);
if(low<high)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
combine(a,low,mid,high);
}
}
void combine(int a[10], int low, int mid, int high){
int i,j,k;
int temp[10];
k=low;
i=low;
j=mid+1;
while(i<=mid&&j<=high){
if(a[i]<=a[j])
{
temp[k]=a[i];
i++;
k++;
}
else
{
temp[k]=a[j];
j++;
k++;
}
}
while(i<=mid){
temp[k]=a[i];
i++;
k++;
}

while(j<=high){
temp[k]=a[j];
j++;
k++;
}
for(k=low;k<=high;k++)
a[k]=temp[k];
}
void display(int a[10]){
int i;
printf("\n \n the sorted array is \n");
for(i=0;i<n;i++)
printf("%d \t",a[i]);}

DESIGN AND ANALYSIS OF ALGORITHMS Page 19


Algorithm for Merge sort:
Algorithm mergesort(low, high)
{
if(low<high) then // Dividing Problem into Sub-problems and
{ this “mid” is for finding where to split the set.
mid=(low+high)/2;

mergesort(low,mid);
mergesort(mid+1,high); //Solve the sub-problems
Merge(low,mid,high); // Combine the solution
}
}
void Merge(low, mid,high){
k=low;
i=low;
j=mid+1;
while(i<=mid&&j<=high) do{
if(a[i]<=a[j]) then
{
temp[k]=a[i];
i++;
k++;
}
else
{
temp[k]=a[j];
j++;
k++;
}
}
while(i<=mid) do{
temp[k]=a[i];
i++;
k++;
}

while(j<=high) do{
temp[k]=a[j];
j++;
k++;
}
For k=low to high do
a[k]=temp[k];
}
For k:=low to high do a[k]=temp[k];
}

DESIGN AND ANALYSIS OF ALGORITHMS Page 20


Computing Time for Merge sort:
T(n)= a if n=1;
2T(n/2)+ cn if n>1

The time for the merging operation in proportional to n, then computing time for merge sort
is described by using recurrence relation.

Here c, a Constants.

If n is power of 2, n=2k

Form recurrence relation

T(n)= 2T(n/2) + cn

2[2T(n/4)+cn/2] + cn

4T(n/4)+2cn

22 T(n/4)+2cn

23 T(n/8)+3cn

24 T(n/16)+4cn

2k T(1)+kcn

an+cn(log n)

By representing it by in the form of Asymptotic notation O is

T(n)=O(nlog n)

Quick Sort
Quick Sort is an algorithm based on the DIVIDE-AND-CONQUER paradigm that selects a pivot
element and reorders the given list in such a way that all elements smaller to it are on one side
and those bigger than it are on the other. Then the sub lists are recursively sorted until the list gets
completely sorted. The time complexity of this algorithm is O (n log n).

 Auxiliary space used in the average case for implementing recursive function calls is
O (log n) and hence proves to be a bit space costly, especially when it comes to large
data sets.
2
 Its worst case has a time complexity of O (n ) which can prove very fatal for large
data sets. Competitive sorting algorithms

DESIGN AND ANALYSIS OF ALGORITHMS Page 21


Quick sort program
#include<stdio.h>
#include<conio.h>
int n,j,i;
void main(){
int i,low,high,z,y;
int a[10],kk;
void quick(int a[10],int low,int high);
int n;
clrscr();
printf("\n \t\t mergesort \n");
printf("\n enter the length of the list:");
scanf("%d",&n);
printf("\n enter the list elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
low=0;
high=n-1;
quick(a,low,high);
printf("\n sorted array is:");
for(i=0;i<n;i++)
printf(" %d",a[i]);
getch();
}

int partition(int a[10], int low, int high){


int i=low,j=high;
int temp;
int mid=(low+high)/2;
int pivot=a[mid];
while(i<=j)
{
while(a[i]<=pivot)
i++;

DESIGN AND ANALYSIS OF ALGORITHMS Page 22


while(a[j]>pivot)
j--;
if(i<=j){
temp=a[i];
a[i]=a[j];
a[j]=temp;
i++;
j--;
}}
return j;
}
void quick(int a[10],int low, int high)
{
int m=partition(a,low,high);
if(low<m)
quick(a,low,m);
if(m+1<high)
quick(a,m+1,high);
}
Algorithm for Quick sort
Algorithm quickSort (a, low, high) {
If(high>low) then{
m=partition(a,low,high);
if(low<m) then quick(a,low,m);
if(m+1<high) then quick(a,m+1,high);
}}

Algorithm partition(a, low, high){


i=low,j=high;
mid=(low+high)/2;
pivot=a[mid];
while(i<=j) do { while(a[i]<=pivot)
i++;
while(a[j]>pivot)
j--;
if(i<=j){ temp=a[i];
a[i]=a[j];
a[j]=temp;
i++;
j--;
}}
return j;
}

Time Complexity
Name Best case Average Worst Space
Case Case Complexity
Bubble O(n) - O(n2) O(n)
Insertion O(n) O(n2) O(n2) O(n)
Selection O(n2) O(n2) O(n2) O(n)

DESIGN AND ANALYSIS OF ALGORITHMS Page 23


Quick O(log n) O(n log n) O(n2) O(n + log n)
Merge O(n log n) O(n log n) O(n log n) O(2n)
Heap O(n log n) O(n log n) O(n log n) O(n)

Comparison between Merge and Quick Sort:


 Both follows Divide and Conquer rule.
 Statistically both merge sort and quick sort have the same average case time i.e., O(n
log n).
 Merge Sort Requires additional memory. The pros of merge sort are: it is a stable sort,
and there is no worst case (means average case and worst case time complexity is
same).
 Quick sort is often implemented in place thus saving the performance and memory by
not creating extra storage space.
 But in Quick sort, the performance falls on already sorted/almost sorted list if the
pivot is not randomized. Thus why the worst case time is O(n2).

Randomized Sorting Algorithm: (Random quick sort)


 While sorting the array a[p:q] instead of picking a[m], pick a random element (from
among a[p], a[p+1], a[p+2]---a[q]) as the partition elements.
 The resultant randomized algorithm works on any input and runs in an expected O(n
log n) times.

Algorithm for Random Quick sort


Algorithm RquickSort (a, p, q) {
If(high>low) then{
If((q-p)>5) then
Interchange(a, Random() mod (q-p+1)+p, p);
m=partition(a,p, q+1);
quick(a, p, m-1);
quick(a,m+1,q);
}}

DESIGN AND ANALYSIS OF ALGORITHMS Page 24


Strassen’s Matrix Multiplication

DESIGN AND ANALYSIS OF ALGORITHMS Page 25


UNIT- II:

Disjoint set operations, Union and Find algorithms, AND/OR graphs, Connected components, Bi-
connected components. Greedy method: General method, applications- Job sequencing with deadlines,
Knapsack problem, Spanning trees, Minimum cost spanning trees, Single source shortest path problem.

Disjoint Sets: If Si and Sj, i≠j are two sets, then there is no element that is in both Si and Sj..
For example: n=10 elements can be partitioned into three disjoint sets,
S1= {1, 7, 8, 9}
S2= {2, 5, 10}
S3= {3, 4, 6}
Tree representation of sets:

10
S1 S2 S3

Disjoint set Operations:


Disjoint set Union
Find(i)

Disjoint set Union: Means Combination of two disjoint sets elements. Form above
example S1 U S2 ={1,7,8,9,5,2,10 }
For S1 U S2 tree representation, simply make one of the tree is a subtree
of the other.

1 1

5 5 7 8 9
7 8 9

S1 U S2 10
2 2 10
S1 U S2 S2 U S1

Find: Given element i, find the set containing i.


Form above example:
Find(4) S3
Find(1) S1
Find(10) S2

DESIGN AND ANALYSIS OF ALGORITHMS Page 26


Data representation of sets:

Tress can be accomplished easily if, with each set name, we keep a pointer to the root of the
tree representing that set.

For presenting the union and find algorithms, we ignore the set names and identify sets just
by the roots of the trees representing them.
For example: if we determine that element ‘i’ is in a tree with root ‘j’ has a pointer to entry
‘k’ in the set name table, then the set name is just name[k]

For unite (adding or combine) to a particular set we use FindPointer function.


Example: If you wish to unite to Si and Sj then we wish to unite the tree with rootsFindPointer (Si)
and FindPointer (Sj)
FindPointer is a function that takes a set name and determines the root of the tree thatrepresents it.
For determining operations:
Find(i) 1St determine the root of the tree and find its pointer to entry in setname table.
Union(i, j) Means union of two trees whose roots are i and j.

If set contains numbers 1 through n, we represents tree node P[1:n]. n Maximum number of elements

Each node represent in array

i 1 2 3 4 5 6 7 8 9 10
P -1 5 -1 3 -1 3 1 1 1 5

DESIGN AND ANALYSIS OF ALGORITHMS Page 27


find(i) by following the indices, starting at i until we reach a node with parent
Example: Find(6) start at 6 and then moves to 6’s parent. Since P[3] is negative, we reached
the root.

Algorithm for finding Union(i, j): Algorithm for find(i)


Algorithm Simple union(i, j) Algorithm SimpleFind(i)
{ {
P[i]:=j; // Accomplishes the union While(P[i]≥0) do i:=P[i];
} return i;
}

If n numbers of roots are there then the above algorithms are not useful for union and find.
For union of n trees Union(1,2), Union(2,3), Union(3,4),…..Union(n-1,n).
For Find i in n trees Find(1), Find(2),….Find(n).

Time taken for the union (simple union) is O(1) (constant). For the n-1 unions O(n).

Time taken for the find for an element at level i of a tree is O(i).
For n finds O(n2).

To improve the performance of our union and find algorithms by avoiding the creation of
degenerate trees. For this we use a weighting rule for union(i, j)

Weighting rule for Union(i, j):


If the number of nodes in the tree with root ‘i’ is less than the tree with root ‘j’, then make ‘j’
the parent of ‘i’; otherwise make ‘i’ the parent of ‘j’.

DESIGN AND ANALYSIS OF ALGORITHMS Page 28


Algorithm for weightedUnion(i, j)
Algorithm WeightedUnion(i,j)
//Union sets with roots i and j, i≠j
// The weighting rule, p[i]= -count[i] and p[j]= -count[j].
{
temp := p[i]+p[j];
if (p[i]>p[j]) then
{ // i has fewer nodes.
P[i]:=j;
P[j]:=temp;
}
else
{ // j has fewer or equal nodes.
P[j] := i;
P[i] := temp;
}
}

For implementing the weighting rule, we need to know how many nodes there are
in every tree.
For this we maintain a count field in the root of every tree.i
root node
count[i] number of nodes in the tree.
Time required for this above algorithm is O(1) + time for remaining unchanged is
determined by using Lemma.

Lemma:-Let T be a tree with m nodes created as a result of a sequence of unions eachperformed


using Weighted Union.The height of T is no greater than |log2 m|+1.

DESIGN AND ANALYSIS OF ALGORITHMS Page 29


Collapsing rule:If ‘j’ is a node on the path from ‘i’ to its root and p[i]≠root[i], then set p[j] to
root[i].
Algorithm for Collapsing find.
Algorithm CollapsingFind(i)
//Find the root of the tree containing element i.
//collapsing rule to collapse all nodes form i to the root.
{
r;=i;
while(p[r]>0) do r := p[r]; //Find the root.
While(i ≠ r) do // Collapse nodes from i to root r.
{
s:=p[i];
p[i]:=r;
i:=s;
}
return r;
}

Collapsing find algorithm is used to perform find operation on the tree created byWeighted
Union.

For example: Tree created by using Weighted Union

Now process the following eight finds: Find(8), Find(8), ............ Find(8)
If SimpleFind is used, each Find(8) requires going up three parent link fields for a total of 24
moves to process all eight finds. When CollapsingFind is uised the first Find(8) requires going
up three links and then resetting two links. Total 13 movies requies for process all eight finds.

DESIGN AND ANALYSIS OF ALGORITHMS Page 30


AND-OR GRAPHS
The AND-OR GRAPH (or tree) is useful for representing the solution of problems that can
solved by decomposing them into a set of smaller problems, all of which must then be solved.
This decomposition, or reduction, generates arcs that we call AND arcs. One AND arc may
point to any number of successor nodes, all of which must be solved in order for the arc to point
to a solution. Just as in an OR graph, several arcs may emerge from a single node, indicating a
variety of ways in which the original problem might be solved. This is why the structure is
called not simply an AND-graph but rather an AND-OR graph (which also happens to be an
AND-OR tree)

EXAMPLE FOR AND-OR GRAPH

ALGORITHM:
1. Let G be a graph with only starting node INIT.
2. Repeat the followings until INIT is labeled SOLVED or h(INIT) > FUTILITY
a) Select an unexpanded node from the most promising path from INIT (call it NODE)
b) Generate successors of NODE. If there are none, set h(NODE) = FUTILITY (i.e.,
NODE is unsolvable); otherwise for each SUCCESSOR that is not an ancestor of
NODE do the following:
i. Add SUCCESSSOR to G.
ii. If SUCCESSOR is a terminal node, label it SOLVED andset h(SUCCESSOR)
= 0.
iii. If SUCCESSPR is not a terminal node, compute its h
c) Propagate the newly discovered information up the graph by doing the following: let S
be set of SOLVED nodes or nodes whose h values have been changed and need to have
values propagated back to their parents. Initialize S to Node. Until S is empty repeat the
followings:
i. Remove a node from S and call it CURRENT.
ii. Compute the cost of each of the arcs emerging from CURRENT. Assign
minimum cost of its successors as its h.
iii. Mark the best path out of CURRENT by marking the arc that had the minimum
cost in step ii
iv. Mark CURRENT as SOLVED if all of the nodes connected to it through new
labeled arc have been labeled SOLVED
v. If CURRENT has been labeled SOLVED or its cost was just changed,
propagate its new cost back up through the graph So add all of the ancestors of
CURRENT to S.

DESIGN AND ANALYSIS OF ALGORITHMS Page 31


EXAMPLE: 1
STEP 1:
A is the only node, it is at the end of the current best path. It is expanded, yielding nodes B, C, D. The
arc to D is labeled as the most promising one emerging from A, since it costs 6compared to B and C,
Which costs 9.
STEP 2:

Node B is chosen for expansion. This process produces one new arc, the AND arc to E and F,
with a combined cost estimate of 10.so we update the f’ value of D to 10.Going back one more
level, we see that this makes the AND arc B-C better than the arc to D, so it is labeled as the
current best path.

STEP 3:

DESIGN AND ANALYSIS OF ALGORITHMS Page 32


we traverse the arc from A and discover the unexpanded nodes B and C. If we going to find a
solution along this path, we will have to expand both B and C eventually, so let’s choose to

explore B first. This generates two new arcs, the ones to G and to H. Propagating their f’
values backward, we update f’ of B to 6(since that is the best we think we can do, which we
can achieve by going through G). This requires updating the cost of the AND arc B-C to
12(6+4+2). After doing that, the arc to D is again the better path from A, so we record that as
the current best path and either node E or node F will chosen for expansion at step 4.
STEP4:

` Connected Component:
Connected component of a graph can be obtained by using BFST (Breadth
first search andtraversal) and DFST (Dept first search and traversal). It is
also called the spanning tree.

BFST (Breadth first search and traversal):


In BFS we start at a vertex V mark it as reached (visited).
The vertex V is at this time said to be unexplored (not yet discovered).
A vertex is said to been explored (discovered) by visiting all vertices
adjacent from it. All unvisited vertices adjacent from V are visited
next.
The first vertex on this list is the next
to be explored. Exploration continues
until no unexplored vertex is left. These
operations can be performed by using
Queue.

DESIGN AND ANALYSIS OF ALGORITHMS Page 33


Algorithm for BFS to convert undirected graph G to Connected component or spanningtree.

Algorithm BFS(v)
// a bfs of G is begin at vertex v
// for any node I, visited[i]=1 if I has already been visited.
// the graph G, and array visited[] are global
{
U:=v; // q is a queue of unexplored vertices.
Visited[v]:=1;
Repeat{
For all vertices w adjacent from U do If
(visited[w]=0) then
{
Add w to q; // w is unexplored
Visited[w]:=1;
}
If q is empty then return; // No unexplored vertex.
Delete U from q; //Get 1st unexplored vertex.
} Until(false)
}
Maximum Time complexity and space complexity of G(n,e), nodes are in adjacency list.
T(n, e)=θ(n+e)
S(n, e)=θ(n)

If nodes are in adjacency matrix then


T(n, e)=θ(n2)
S(n, e)=θ(n)

DESIGN AND ANALYSIS OF ALGORITHMS Page 34


DFST(Dept first search and traversal).:
Dfs different from bfs
The exploration of a vertex v is suspended (stopped) as soon as a new vertex is
reached.
In this the exploration of the new vertex (example v) begins; this new vertex has been
explored, the exploration of v continues.
Note: exploration start at the new vertex which is not visited in other vertex exploring
and choose nearest path for exploring next or adjacent vertex.

DESIGN AND ANALYSIS OF ALGORITHMS Page 35


Algorithm for DFS to convert undirected graph G to Connected component or spanningtree.
Algorithm dFS(v)
// a Dfs of G is begin at vertex v
// initially an array visited[] is set to zero.
//this algorithm visits all vertices reachable from v.
// the graph G, and array visited[] are global
{
Visited[v]:=1;
For each vertex w adjacent from v do
{
If (visited[w]=0) then DFS(w);
{
Add w to q; // w is unexplored
Visited[w]:=1;
}
}

Maximum Time complexity and space complexity of G(n,e), nodes are in adjacency list.
T(n, e)=θ(n+e)
S(n, e)=θ(n)

If nodes are in adjacency matrix then


T(n, e)=θ(n2)
S(n, e)=θ(n)

A biconnected component of G is a maximal set of edges such that any two edges in the set lie on a common
simple cycle

DESIGN AND ANALYSIS OF ALGORITHMS Page 36


DESIGN AND ANALYSIS OF ALGORITHMS Page 37
.
Greedy Method:
The greedy method is perhaps (maybe or possible) the most straight forward design
technique, used to determine a feasible solution that may or may not be optimal.

Feasible solution:- Most problems have n inputs and its solution contains a subset of inputs
that satisfies a given constraint(condition). Any subset that satisfies the constraint is called
feasible solution.

Optimal solution: To find a feasible solution that either maximizes or minimizes a given
objective function. A feasible solution that does this is called optimal solution.

The greedy method suggests that an algorithm works in stages, considering one input at a
time. At each stage, a decision is made regarding whether a particular input is in an optimal
solution.

Greedy algorithms neither postpone nor revise the decisions (ie., no back tracking).

DESIGN AND ANALYSIS OF ALGORITHMS Page 38


Example: Kruskal’s minimal spanning tree. Select an edge from a sorted list, check, decide,
and never visit it again.
Application of Greedy Method:
Job sequencing with deadline
0/1 knapsack problem
Minimum cost spanning trees
Single source shortest path problem.

Algorithm for Greedy method


Algorithm Greedy(a,n)
//a[1:n] contains the n inputs.
{
Solution :=0;
For i=1 to n do
{
X:=select(a);
If Feasible(solution, x) then
Solution :=Union(solution,x);
}
Return solution;
}
Selection Function, that selects an input from a[] and removes it. The selected input’s
value is assigned to x.
Feasible Boolean-valued function that determines whether x can be included into the
solution vector.
Union function that combines x with solution and updates the objective function.

Knapsack problem

The knapsack problem or rucksack (bag) problem is a problem in combinatorial optimization: Given a set of
items, each with a mass and a value, determine the number of each item to include in a collection so that the
total weight is less than or equal to a given limit and the total value is as large as possible

There are two versions of the problems

1. 0/1 knapsack problem


2. Fractional Knapsack problem
a. Bounded Knapsack problem.
b. Unbounded Knapsack problem.

DESIGN AND ANALYSIS OF ALGORITHMS Page 39


Solutions to knapsack problems

Brute-force approach:-Solve the problem with a straight farward algorithm


Greedy Algorithm:- Keep taking most valuable items until maximum weight is
reached or taking the largest value of eac item by calculating vi=valuei/Sizei
Dynamic Programming:- Solve each sub problem once and store their solutions in
an array.

0/1 knapsack problem:


Let there be items, to where has a value and weight . The maximum
weight that we can carry in the bag is W. It is common to assume that all values and weights
are nonnegative. To simplify the representation, we also assume that the items are listed in
increasing order of weight.

Maximize subject to

Maximize the sum of the values of the items in the knapsack so that the sum of the weights
must be less than the knapsack's capacity.
Greedy algorithm for knapsack
Algorithm GreedyKnapsack(m,n)
// p[i:n] and [1:n] contain the profits and weights respectively
// if the n-objects ordered such that p[i]/w[i]>=p[i+1]/w[i+1], m size of knapsack and
x[1:n] the solution vector
{
For i:=1 to n do x[i]:=0.0
U:=m;
For i:=1 to n do
{
if(w[i]>U) then break;
x[i]:=1.0;
U:=U-w[i];
}
If(i<=n) then x[i]:=U/w[i];
}

Ex: - Consider 3 objects whose profits and weights are


defined as(P1, P2, P3) = ( 25, 24, 15 )
W1, W2, W3) = ( 18, 15, 10 )
n=3 number of objects
m=20 Bag capacity
Consider a knapsack of capacity 20. Determine the optimum strategy for placing the
objects in to the knapsack. The problem can be solved by the greedy approach
where in the inputs are arranged according to selection process (greedy strategy) and
solve the problem in stages. The various greedy strategies for the problem could be
as follows.

DESIGN AND ANALYSIS OF ALGORITHMS Page 40


(x1, x2, x3) ∑ xiwi ∑ xipi
(1, 2/15, 0) 2 2
18x1+ x15 = 20 25x1+ x 24 = 28.2
15 15
(0, 2/3, 1) 2 2
x15+10x1= 20 x 24 +15x1 = 31
3 3

(0, 1, ½ ) 1 1
1x15+ x10 = 20 1x24+ x15 = 31.5
2 2
(½, ⅓, ¼ ) ½ x 18+⅓ x15+ ¼ x10 = 16. 5 ½ x 25+⅓ x24+ ¼ x15 =
12.5+8+3.75 = 24.25

Analysis: - If we do not consider the time considered for sorting the inputs then all of thethree
greedy strategies complexity will be O(n).

Job Sequence with Deadline:


There is set of n-jobs. For any job i, is a integer deadling di≥0 and profit Pi>0, the profit Pi is
earned iff the job completed by its deadline.

To complete a job one had to process the job on a machine for one unit of time. Only one
machine is available for processing jobs.

A feasible solution for this problem is a subset J of jobs such that each job in this subset can
be completed by its deadline.

The value of a feasible solution J is the sum of the profits of the jobs in J, i.e., ∑i∈ jPi
An optimal solution is a feasible solution with maximum value.

The problem involves identification of a subset of jobs which can be completed by its deadline.
Therefore the problem suites the subset methodology and can be solved by the greedy method.

DESIGN AND ANALYSIS OF ALGORITHMS Page 41


Ex: - Obtain the optimal sequence for the following jobs.
j1 j2 j3 j4
(P1, P2, P3, P4) = (100, 10, 15, 27)

(d1, d2, d3, d4) = (2, 1, 2, 1)


n =4

Feasible Processing Value


solution sequence
j1 j2
(2,1) 100+10=110
(1, 2)
(1,3) (1,3) or (3,1) 100+15=115
(1,4) (4,1) 100+27=127
(2,3) (2,3) 10+15=25
(3,4) (4,3) 15+27=42
(1) (1) 100
(2) (2) 10
(3) (3) 15
(4) (4) 27

In the example solution ‘3’ is the optimal. In this solution only jobs 1&4 are processed and
the value is 127. These jobs must be processed in the order j4 followed by j1. the process of
job 4 begins at time 0 and ends at time 1. And the processing of job 1 begins at time 1 and
ends at time2. Therefore both the jobs are completed within their deadlines. The optimization
measure for determining the next job to be selected in to the solution is according to the
profit. The next job to include is that which increases ∑pi the most, subject to the constraint
that the resulting “j” is the feasible solution. Therefore the greedy strategy is to consider the
jobs in decreasing order of profits.
The greedy algorithm is used to obtain an optimal solution.
We must formulate an optimization measure to determine how the next job is chosen.

DESIGN AND ANALYSIS OF ALGORITHMS Page 42


algorithm js(d, j, n)
//d dead line, jsubset of jobs ,n total number of jobs
// d[i]≥1 1 ≤ i ≤ n are the dead lines,
// the jobs are ordered such that p[1]≥p[2]≥---≥p[n]
//j[i] is the ith job in the optimal solution 1 ≤ i ≤ k, k subset range
{
d[0]=j[0]=0;
j[1]=1;
k=1;
for i=2 to n do{
r=k;
while((d[j[r]]>d[i]) and [d[j[r]]≠r)) do
r=r-1;
if((d[j[r]]≤d[i]) and (d[i]> r)) then
{
for q:=k to (r+1) setp-1 do j[q+1]= j[q];
j[r+1]=i;
k=k+1;
}
}
return k;
}

Note: The size of sub set j must be less than equal to maximum deadline in given list.

Single Source Shortest Paths:

Graphs can be used to represent the highway structure of a state or country with
vertices representing cities and edges representing sections of highway.
The edges have assigned weights which may be either the distance between the 2
cities connected by the edge or the average time to drive along that section of
highway.
For example if A motorist wishing to drive from city A to B then we must answer the
following questions
o Is there a path from A to B
o If there is more than one path from A to B which is the shortest path
The length of a path is defined to be the sum of the weights of the edges on that path.

Given a directed graph G(V,E) with weight edge w(u,v). e have to find a shortest path from
source vertex S∈ v to every other vertex v1∈ v-s.

DESIGN AND ANALYSIS OF ALGORITHMS Page 43


To find SSSP for directed graphs G(V,E) there are two different algorithms.

 Bellman-Ford Algorithm
 Dijkstra’s algorithm

Bellman-Ford Algorithm:- allow –ve weight edges in input graph. This algorithm
either finds a shortest path form source vertex S∈ V to other vertex v∈ V or detect a –
ve weight cycles in G, hence no solution. If there is no negative weight cycles are
reachable form source vertex S∈ V to every other vertex v∈ V
Dijkstra’s algorithm:- allows only +ve weight edges in the input graph and finds a
shortest path from source vertex S∈ V to every other vertex v∈ V.

Consider the above directed graph, if node 1 is the source vertex, then shortest path
from 1 to 2 is 1,4,5,2. The length is 10+15+20=45.

To formulate a greedy based algorithm to generate the shortest paths, we must


conceive of a multistage solution to the problem and also of an optimization measure.

This is possible by building the shortest paths one by one.

As an optimization measure we can use the sum of the lengths of all paths so far
generated.

If we have already constructed ‘i’ shortest paths, then using this optimization measure,
the next path to be constructed should be the next shortest minimum length path.

The greedy way to generate the shortest paths from Vo to the remaining vertices is to
generate these paths in non-decreasing order of path length.

For this 1st, a shortest path of the nearest vertex is generated. Then a shortest path to
the 2nd nearest vertex is generated and so on.

DESIGN AND ANALYSIS OF ALGORITHMS Page 44


Algorithm for finding Shortest Path

Algorithm ShortestPath(v, cost, dist, n)


//dist[j], 1≤j≤n, is set to the length of the shortest path from vertex v to vertex j in graph g
with n-vertices.
// dist[v] is zero
{
for i=1 to n do{
s[i]=false;
dist[i]=cost[v,i];
}
s[v]=true;
dist[v]:=0.0; // put v in s
for num=2 to n do{
// determine n-1 paths from v
choose u form among those vertices not in s such that dist[u] is minimum.
s[u]=true; // put u in s
for (each w adjacent to u with s[w]=false) do
if(dist[w]>(dist[u]+cost[u, w])) then
dist[w]=dist[u]+cost[u, w];
}
}

SPANNING TREE: - A Sub graph ‘n’ of o graph ‘G’ is called as a spanning tree if
(i) It includes all the vertices of ‘G’
(ii) It is a tree

DESIGN AND ANALYSIS OF ALGORITHMS Page 45


Minimum cost spanning tree: For a given graph ‘G’ there can be more than one spanning
tree. If weights are assigned to the edges of ‘G’ then the spanning tree which has the
minimum cost of edges is called as minimal spanning tree.

The greedy method suggests that a minimum cost spanning tree can be obtained by contacting
the tree edge by edge. The next edge to be included in the tree is the edge that results in a
minimum increase in the some of the costs of the edges included so far.

There are two basic algorithms for finding minimum-cost spanning trees, and both are greedy
Algorithms

 Prim’s Algorithm
 Kruskal’s Algorithm

Prim’s Algorithm: Start with any one node in the spanning tree, and repeatedly add the
cheapest edge, and the node it leads to, for which the node is not already in the spanning tree.

DESIGN AND ANALYSIS OF ALGORITHMS Page 46


PRIM’S ALGORITHM: -
i) Select an edge with minimum cost and include in to the spanning tree.
ii) Among all the edges which are adjacent with the selected edge, select the
onewith minimum cost.
iii) Repeat step 2 until ‘n’ vertices and (n-1) edges are been included. And the
subgraphobtained does not contain any cycles.

Notes: - At every state a decision is made about an edge of minimum cost to be included
into the spanning tree. From the edges which are adjacent to the last edge included in
the spanning tree i.e. at every stage the sub-graph obtained is a tree.

Prim's minimum spanning tree algorithm


Algorithm Prim (E, cost, n,t)
// E is the set of edges in G. Cost (1:n, 1:n) is the
// Cost adjacency matrix of an n vertex graph such that
// Cost (i,j) is either a positive real no. or ∞ if no edge (i,j) exists.
//A minimum spanning tree is computed and
//Stored in the array T(1:n-1, 2).
//(t (i, 1), + t(i,2)) is an edge in the minimum cost spanning tree. The final cost is returned
{
Let (k, l) be an edge with min cost
in E Min cost: = Cost (x,l);
T(1,1):= k; + (1,2):= l;
for i:= 1 to n do//initialize
near
if (cost (i,l)<cost (i,k) then n east
(i): l; else near (i): = k;
near (k): = near (l): =
0; for i: = 2 to n-1 do
{//find n-2 additional edges for t
let j be an index such that near (i) 0 & cost (j, near (i)) is
minimum;t (i,1): = j + (i,2): = near (j);
min cost: = Min cost + cost (j, near
(j)); near (j): = 0;
for k:=1 to n do // update near ()
if ((near (k) 0) and (cost {k, near (k)) > cost
(k,j))) then near Z(k): = ji
}
return mincost;
}

DESIGN AND ANALYSIS OF ALGORITHMS Page 47


The algorithm takes four arguments E: set of edges, cost is nxn adjacency matrix cost of (i,j)=
+ve integer, if an edge exists between i&j otherwise infinity. ‘n’ is no/: of vertices. ‘t’ is a
(n- 1):2matrix which consists of the edges of spanning tree.
E = { (1,2), (1,6), (2,3), (3,4), (4,5), (4,7), (5,6), (5,7), (2,7) }
n = {1,2,3,4,5,6,7)

i) The algorithm will start with a tree that includes only minimum cost edge of
G. Then edges are added to this tree one by one.
ii) The next edge (i,j) to be added is such that i is a vertex which is
already included in the treed and j is a vertex not yet included in the
tree and cost of i,j is minimum among all edges adjacent to ‘i’.
iii) With each vertex ‘j’ next yet included in the tree, we assign a value
near ‘j’. The value near ‘j’ represents a vertex in the tree such that cost (j,
near (j)) is minimum among all choices for near (j)
iv) We define near (j):= 0 for all the vertices ‘j’ that are already in the tree.
v) The next edge to include is defined by the vertex ‘j’ such that (near (j))
0 and cost of (j, near (j)) is minimum.
Analysis: -
The time required by the prince algorithm is directly proportional to the no/: of vertices.
If agraph ‘G’ has ‘n’ vertices then the time required by prim’s algorithm is 0(n2)

DESIGN AND ANALYSIS OF ALGORITHMS Page 48


Kruskal’s Algorithm: Start with no nodes or edges in the spanning tree, and repeatedly
add the cheapest edge that does not create a cycle.
In Kruskals algorithm for determining the spanning tree we arrange the edges in the increasing
order of cost.
i) All the edges are considered one by one in that order and deleted from the graph
and areincluded in to the spanning tree.
ii) At every stage an edge is included; the sub-graph at a stage need not be a
tree. Infect it is a forest.
iii) At the end if we include ‘n’ vertices and n-1 edges without forming cycles then
we get a single connected component without any cycles i.e. a tree with
minimum cost.
At every stage, as we include an edge in to the spanning tree, we get disconnected trees
represented by various sets. While including an edge in to the spanning tree we need to
check it does not form cycle. Inclusion of an edge (i,j) will form a cycle if i,j both are in
same set. Otherwise the edge can be included into the spanning tree.
Kruskal minimum spanning tree algorithm
Algorithm Kruskal (E, cost, n,t)
//E is the set of edges in G. ‘G’ has ‘n’ vertices
//Cost {u,v} is the cost of edge (u,v) t is the set
//of edges in the minimum cost spanning tree
//The final cost is returned
{ construct a heap out of the edge costs using heapify;
for i:= 1 to n do parent (i):= -1 // place in different sets
//each vertex is in different set {1} {1}
{3} i: = 0; min cost: = 0.0;
While (i<n-1) and (heap not empty))do
{
Delete a minimum cost edge (u,v) from the heaps; and reheapify using
adjust; j:= find (u); k:=find (v);
if (j k) then
{ i: = 1+1;
+ (i,1)=u; + (i, 2)=v;
mincost: =
mincost+cost(u,v); Union
(j,k);
}
}
if (i n-1) then write (“No
spanningtree”);else return
mincost;
}

Consider the above graph of , Using Kruskal's method the edges of this graph are considered
for inclusion in the minimum cost spanning tree in the order (1, 2), (3, 6), (4, 6), (2, 6), (1, 4),
(3, 5), (2, 5), (1, 5), (2, 3), and (5, 6). This corresponds to the cost sequence 10, 15, 20, 25,
30, 35, 40, 45, 50, 55. The first four edges are included in T. The next edge to be considered
is (I, 4). This edge connects two vertices already connected in T and so it is rejected. Next,
the edge (3, 5) is selected and that completes the spanning tree.

DESIGN AND ANALYSIS OF ALGORITHMS Page 49


Analysis: - If the no/: of edges in the graph is given by /E/ then the time
for Kruskals algorithm is given by 0 (|E| log |E|).

DESIGN AND ANALYSIS OF ALGORITHMS Page 50


UNIT-III
Dynamic Programming: General method, applications- Matrix chained multiplication, Optimal
binarysearch trees, 0/1 Knapsack problem, All pairs shortest path problem, Traveling sales
person problem, Reliability design.

Dynamic Programming

Dynamic programming is a name, coined by Richard Bellman in 1955. Dynamic


programming, as greedy method, is a powerful algorithm design technique that can be
used when the solution to the problem may be viewed as the result of a sequence of
decisions. In the greedy method we make irrevocable decisions one at a time, using a
greedy criterion. However, in dynamic programming we examine the decision
sequence to see whether an optimal decision sequence contains optimal decision
subsequence.

When optimal decision sequences contain optimal decision subsequences, we can


establish recurrence equations, called dynamic-programming recurrence equations,
that enable us to solve the problem in an efficient way.

Dynamic programming is based on the principle of optimality (also coined by


Bellman). The principle of optimality states that no matter whatever the initial state
and initial decision are, the remaining decision sequence must constitute an optimal
decision sequence with regard to the state resulting from the first decision. The
principle implies that an optimal decision sequence is comprised of optimal decision
subsequences. Since the principle of optimality may not hold for some formulations of
some problems, it is necessary to verify that it does hold for the problem being
solved. Dynamic programming cannot be applied when this principle does not hold.

The steps in a dynamic programming solution are:

 Verify that the principle of optimality holds

 Set up the dynamic-programming recurrence equations

 Solve the dynamic-programming recurrence equations for the value ofthe


optimalsolution.

 Perform a trace back step in which the solution itself is constructed.

DESIGN AND ANALYSIS OF ALGORITHMS Page 51


DESIGN AND ANALYSIS OF ALGORITHMS Page 51
All pairs shortest paths

In the all pairs shortest path problem, we are to find a shortest path between every pair of
vertices in a directed graph G. That is, for every pair of vertices (i, j), we are to find a
shortest path from i to j as well as one from j to i. These two paths are the same when G is
undirected.
When no edge has a negative length, the all-pairs shortest path problem may be solved
by using Dijkstra’s greedy single source algorithm n times, once with each of the n vertices
as the source vertex.

The all pairs shortest path problem is to determine a matrix A such that A (i, j) is the lengthof a
shortest path from i to j. The matrix A can be obtained by solving n single-source problems
using the algorithm shortest Paths. Since each application of this procedure requires O (n2) time,
the matrix A can be obtained in O (n3) time.

The dynamic programming solution, called Floyd’s algorithm, runs in O (n3) time. Floyd’s
algorithm works even when the graph has negative length edges (provided there are no
negative length cycles).

The shortest i to j path in G, i ≠ j originates at vertex i and goes through some


intermediate vertices (possibly none) and terminates at vertex j. If k is an intermediate
vertex on this shortest path, then the subpaths from i to k and from k to j must be shortest
paths from i to k and k to j, respectively. Otherwise, the i to j path is notof minimum length.
So, the principle of optimality holds. Let Ak (i, j) represent the length of a shortest path
from i to j going through no vertex of index greater than k, we obtain:

Ak (i, j) = {min {min {Ak-1 (i, k) + Ak-1 (k, j)}, c (i, j)}
1<k<n

Algorithm All Paths (Cost, A, n)


// cost [1:n, 1:n] is the cost adjacency matrix of a graph which
// n vertices; A [I, j] is the cost of a shortest path from vertex
// i to vertex j. cost [i, i] = 0.0, for 1 < i < n.
{
for i := 1 to n do
for j:= 1 to n do
A [i, j] := cost [i, j]; // copy cost into A.
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]);
}

DESIGN AND ANALYSIS OF ALGORITHMS Page 51


Complexity Analysis: A Dynamic programming algorithm based on this recurrence
involves in calculating n+1 matrices, each of size n x n. Therefore, the algorithm has a
complexity of O (n3).

DESIGN AND ANALYSIS OF ALGORITHMS Page 51


TRAVELLING SALESPERSON PROBLEM
For the following graph find minimum cost tour for the traveling sales person problem:

1 2

3 4

Let us start the tour from vertex 1:


g (1, V – {1}) = min {c1k + g (k, V – {1, K})} - (1)
2<k<n

More generally writing:


g (i, s) = min {cij + g (J, s – {J})} - (2)

Clearly, g (i, T) = ci1 , 1 ≤ i ≤ n. So,

g (2, T) = C21 = 5
g (3, T) = C31 = 6
g (4, ~) = C41 = 8
Using equation – (2) we obtain:
g (1,{2, 3, 4}) = min {c12 + g (2, {3,
4}, c13 + g (3, {2, 4}), c14 + g (4, {2, 3})}
g (2, {3, 4}) = min {c23 + g (3, {4}), c24 + g (4, {3})}
= min {9 + g (3, {4}), 10 + g (4, {3})}
g (3, {4}) = min {c34 + g (4, T)} = 12 + 8 = 20
g (4, {3}) = min {c43 + g (3, ~)} = 9 + 6 = 15

Therefore, g (2, {3, 4}) = min {9 + 20, 10 + 15} = min {29, 25} = 25
g (3, {2, 4}) = min {(c32 + g (2, {4}), (c34 + g (4, {2})}

g (2, {4}) = min {c24 + g (4, T)} = 10 + 8 = 18

g (4, {2}) = min {c42 + g (2, ~)} = 8 + 5 = 13

Therefore, g (3, {2, 4}) = min {13 + 18, 12 + 13} = min {41, 25} = 25
g (4, {2, 3}) = min {c42 + g (2, {3}), c43 + g (3, {2})}
g (2, {3}) = min {c23 + g (3, ~} = 9 + 6 = 15
g (3, {2}) = min {c32 + g (2, T} = 13 + 5 = 18

DESIGN AND ANALYSIS OF ALGORITHMS Page 55


Therefore, g (4, {2, 3}) = min {8 + 15, 9 + 18} = min {23, 27} = 23

g (1, {2, 3, 4}) = min {c12 + g (2, {3, 4}), c13 + g (3, {2, 4}), c14 + g (4, {2, 3})} = min
{10 + 25, 15 + 25, 20 + 23} = min {35, 40, 43} = 35

The optimal tour for the graph has length = 35 The

optimal tour is: 1, 2, 4, 3, 1.

OPTIMAL BINARY SEARCH TREE


Let us assume that the given set of identifiers is {a1, . . . , an} with a1 < a2 < < an.
Let p (i) be the probability with which we search for ai. Let q (i) be the probability that the
identifier x being searched for is such that ai < x < ai+1, 0 < i < n (assume a0 = - ~ and
an+1 = +oc). We have to arrange the identifiers in a binary search tree in a way that
minimizes the expected total access time.
In a binary search tree, the number of comparisons needed to access an element at depth 'd'is
d + 1, so if 'ai' is placed at depth 'di', then we want to minimize:
n
~ Pi (1 + di ) .
i ~1

Let P (i) be the probability with which we shall be searching for 'ai'. Let Q (i) be the
probability of an un-successful search. Every internal node represents a point where a
successful search may terminate. Every external node represents a point where an
unsuccessful search may terminate.

The expected cost contribution for the internal node for 'ai' is:

P (i) * level (ai ) .

Unsuccessful search terminate with I= 0 (i.e at an external node). Hence the costcontribution
for this node is:

Q (i) * level ((Ei) - 1)

The expected cost of binary search tree is:


n n
~ P(i) * level (ai) + ~ Q (i) * level ((Ei ) - 1)

Given a fixed set of identifiers, we wish to create a binary search tree organization. We
may expect different binary search trees for the same identifier set to have different
performance characteristics.
The computation of each of these c(i, j)’s requires us to find the minimum of m
quantities. Hence, each such c(i, j) can be computed in time O(m). The total time for all
c(i, j)’s with j – i = m is therefore O(nm – m2).

The total time to evaluate all the c(i, j)’s and r(i, j)’s is therefore:

DESIGN AND ANALYSIS OF ALGORITHMS Page 56


~ (nm - m2 ) = O (n3
) 1<m<n

Example 1:

Let n = 4, and (a1, a2, a3, a4) = (do, if, need, while) Let P (1: 4) = (3, 3, 1, 1) and Q (0:
4) = (2, 3, 1, 1, 1)

Solution:
Table for recording W (i, j), C (i, j) and R (i, j):
Column
Row 0 1 2 3 4
0 2, 0, 0 3, 0, 0 1, 0, 0 1, 0, 0, 1, 0, 0
1 8, 8, 1 7, 7, 2 3, 3, 3 3, 3, 4
2 12, 19, 1 9, 12, 2 5, 8, 3
3 14, 25, 2 11, 19, 2
4 16, 32, 2

This computation is carried out row-wise from row 0 to row 4. Initially, W (i, i) = Q
(i) and C (i, i) = 0 and R (i, i) = 0, 0 < i < 4.
Solving for C (0, n):

First, computing all C (i, j) such that j - i = 1; j = i + 1 and as 0 < i < 4; i = 0, 1, 2 and
3; i < k ≤ J. Start with i = 0; so j = 1; as i < k ≤ j, so the possible value for k = 1

W (0, 1) = P (1) + Q (1) + W (0, 0) = 3 + 3 + 2 = 8


C (0, 1) = W (0, 1) + min {C (0, 0) + C (1, 1)} = 8
R (0, 1) = 1 (value of 'K' that is minimum in the above equation).
Next with i = 1; so j = 2; as i < k ≤ j, so the possible value for k = 2
W (1, 2) = P (2) + Q (2) + W (1, 1) = 3 + 1 + 3 = 7
C (1, 2) = W (1, 2) + min {C (1, 1) + C (2, 2)} = 7
R (1, 2) = 2

Next with i = 2; so j = 3; as i < k ≤ j, so the possible value for k = 3

W(2, 3) = P (3) + Q (3) + W (2, 2) = 1 + 1 + 1 = 3


C (2, 3) = W (2, 3) + min {C (2, 2) + C (3, 3)} = 3 + [(0 + 0)] = 3
ft (2, 3) = 3

Next with i = 3; so j = 4; as i < k ≤ j, so the possible value for k = 4


W(3, 4) = P (4) + Q (4) + W (3, 3) = 1 + 1 + 1 =3

DESIGN AND ANALYSIS OF ALGORITHMS Page 57


C (3, 4) = W(3, 4) + min {[C (3, 3) + C (4, 4)]} = 3 + [(0 + 0)] = 3
ft (3, 4) = 4

Second, Computing all C (i, j) such that j - i = 2; j = i + 2 and as 0 < i < 3; i = 0, 1, 2; i <
k ≤ J. Start with i = 0; so j = 2; as i < k ≤ J, so the possible values for k = 1 and 2.
W (0, 2) = P (2) + Q (2) + W (0, 1) = 3 + 1 + 8 = 12
C (0, 2) = W (0, 2) + min {(C (0, 0) + C (1, 2)), (C (0, 1) + C (2, 2))} = 12
+ min {(0 + 7, 8 + 0)} = 19
ft (0, 2) = 1
Next, with i = 1; so j = 3; as i < k ≤ j, so the possible value for k = 2 and 3.
W(1, 3) = P (3) + Q (3) + W (1, 2) = 1 + 1+ 7 = 9
C (1, 3) = W (1, 3) + min {[C (1, 1) + C (2, 3)], [C (1, 3) 2) + C (3, 3)]}
= W(1, + min {(0 + 3), (7 + 0)} = 9 + 3 = 12
ft (1, 3) = 2
Next, with i = 2; so j = 4; as i < k ≤ j, so the possible value for k = 3 and 4.

W (2, 4) = P (4) + Q (4) + W (2, 3) = 1 + 1 + 3 = 5


C (2, 4) = W (2, 4) + min {[C (2, 2) + C (3, 4)], [C (2, 3) + C (4, 4)]
= 5 + min {(0 + 3), (3 + 0)} = 5 + 3 = 8
ft (2, 4) = 3
Third, Computing all C (i, j) such that J - i = 3; j = i + 3 and as 0 < i < 2; i = 0, 1; i < k
≤ J. Start with i = 0; so j = 3; as i < k ≤ j, so the possible values for k = 1, 2 and 3.
W(0, 3) = P (3) + Q (3) + W (0, 2) = 1 + 1 =+ 12 = 14
C (0, 3) W (0, 3) + min {[C (0, 0) + C (1, 3)], [C (0, 1) + C (2, 3)],
[C (0, 2) + C (3, = 3)]}
14 + min {(0 + 12), (8 + 3), (19 = 2 + 0)} = 14 + 11 = 25
ft (0, 3)

DESIGN AND ANALYSIS OF ALGORITHMS Page 58


Start with i = 1; so j = 4; as i < k ≤ j, so the possible values for k = 2, 3 and 4.

W(1, 4) = P (4) + Q (4) + W (1, 3) = 1 + 1 + 9 = 11 = W 2)


C (1, 4) (1, 4) + min {[C (1, 1) + C (2, 4)], [C (1, + C (3, 4)],
[C (1, 3) + C (4, 4)]} + 8 = 19
= 11 + min {(0 + 8), (7 + 3), (12 + 0)} = 11 = 2
ft (1, 4)

Fourth, Computing all C (i, j) such that j - i = 4; j = i + 4 and as 0 < i < 1; i = 0; i < k ≤
J.
Start with i = 0; so j = 4; as i < k ≤ j, so the possible values for k = 1, 2, 3 and 4.

W (0, 4) = P (4) + Q (4) + W (0, 3) = 1 + 1 + 14 = 16


C (0, 4) = W(0, 4) + min {[C (0, 0) + C (1, 4)], [C (0, 1) + C (2, 4)],
[C (0, 2) + C (3, 4)], [C (0, 3) + C (4, 4)]}
= 16 + min [0 + 19, 8 + 8, 19+3, 25+0] = 16 + 16 = 32 ft (0,
4) = 2
From the table we see that C (0, 4) = 32 is the minimum cost of a binary search tree for
(a1, a2, a3, a4). The root of the tree 'T04' is 'a2'.

Hence the left sub tree is 'T01' and right sub tree is T24. The root of 'T01' is 'a1' and the
root of 'T24' is a3.

The left and right sub trees for 'T01' are 'T00' and 'T11' respectively. The root of T01 is
'a1'

The left and right sub trees for T24 are T22 and T34 respectively.

The root of T24 is 'a3'.

The root of T22 is null


The root of T34 is a4.
a2
T 04 if

a1 a3
T 01 T 24 do read

a4
T 00 T 11 T 22 T 34 while

DESIGN AND ANALYSIS OF ALGORITHMS Page 59


0/1 – KNAPSACK

We are given n objects and a knapsack. Each object i has a positive weight wi and a positive
value Vi. The knapsack can carry a weight not exceeding W. Fill the knapsack so that the value
of objects in the knapsack is optimized.
A solution to the knapsack problem can be obtained by making a sequence of decisions on
the variables x1, x2, . . . . , xn. A decision on variable xi involves determining which of the
values 0 or 1 is to be assigned to it. Let us assume that

decisions on the xi are made in the order xn, xn-1,......... x1. Following a decision on xn,
we may be in one of two possible states: the capacity remaining in m – wn and a profit
of pn has accrued. It is clear that the remaining decisions xn-1, ........ , x1 must be optimal
with respect to the problem state resulting from the decision on xn. Otherwise, xn,. .
. . , x1 will not be optimal. Hence, the principal of optimality holds.
Fn (m) = max {fn-1 (m), fn-1 (m - wn) + pn} -- 1

For arbitrary fi (y), i > 0, this equation generalizes to:

Fi (y) = max {fi-1 (y), fi-1 (y - wi) + pi} -- 2

Equation-2 can be solved for fn (m) by beginning with the knowledge fo (y) = 0 for all y
and fi (y) = - ~, y < 0. Then f1, f2, . . . fn can be successively computed using
equation–2.

When the wi’s are integer, we need to compute fi (y) for integer y, 0 < y < m. Since fi (y)
= - ~ for y < 0, these function values need not be computed explicitly. Since each fi
can be computed from fi - 1 in Θ (m) time, it takes Θ (m n) time to compute fn. When
the wi’s are real numbers, fi (y) is needed for real numbers y such that 0 < y < m. So, fi
cannot be explicitly computed for all y in this range. Even when the wi’s are integer, the
explicit Θ (m n) computation of fn may not be the most efficient computation. So, we
explore an alternative method for both cases.
The fi (y) is an ascending step function; i.e., there are a finite number of y’s, 0 = y1 < y2
< . . . . < yk, such that fi (y1) < fi (y2) < ............ < fi (yk); fi (y) = - ~ , y < y1; fi (y) = f
(yk), y > yk; and fi (y) = fi (yj), yj < y < yj+1. So, we need to compute only fi (yj), 1 < j
< k. We use the ordered set Si = {(f (yj), yj) | 1 < j < k} to represent fi (y). Each number
of Si is a pair (P, W), where P = fi (yj) and W = yj. Notice that S 0 = {(0, 0)}. We can
compute Si+1 from Si by first computing:

Si 1 = {(P, W) | (P – pi, W – wi) e Si}

Now, Si+1 can be computed by merging the pairs in Si and Si 1 together. Note that if Si+1
contains two pairs (Pj, Wj) and (Pk, Wk) with the property that Pj < Pk and Wj > Wk,
then the pair (Pj, Wj) can be discarded because of equation-2. Discarding or purging
rules such as this one are also known as dominance rules. Dominated tuples get purged. In
the above, (Pk, Wk) dominates (Pj, Wj).

DESIGN AND ANALYSIS OF ALGORITHMS Page 60


Reliability Design

The problem is to design a system that is composed of several devices connected in series.
Let ri be the reliability of device Di (that is ri is the probability that device i will
function properly) then the reliability of the entire system is fT ri. Even if the individual
devices are very reliable (the ri’s are very close to one), the reliability of the system may
not be very good. For example, if n = 10 and ri = 0.99, i < i < 10, then fT ri = .904.
Hence, it is desirable to duplicate devices. Multiply copies of the same device type are
connected in parallel.

If stage i contains mi copies of device Di. Then the probability that all mi have a
malfunction is (1 - ri) mi. Hence the reliability of stage i becomes 1 – (1 - r )mi.
i

DESIGN AND ANALYSIS OF ALGORITHMS Page 61


DESIGN AND ANALYSIS OF ALGORITHMS Page 62
DESIGN AND ANALYSIS OF ALGORITHMS Page 63
UNIT IV:

Backtracking: General method, Applications- n-queue problem, Sum of subsets problem,


Graph coloring, Hamiltonian cycles.
Backtracking (General method)
Many problems are difficult to solve algorithmically. Backtracking makes it possible to solve at
least some large instances of difficult combinatorial problems.
Suppose you have to make a series of decisions among various choices, where
 You don’t have enough information to know what to choose
 Each decision leads to a new set of choices.
 Some sequence of choices ( more than one choices) may be a solution to your problem.

Backtracking is a methodical (Logical) way of trying out various sequences of decisions, until
you find one that “works”

Given a maze, find a path from start to finish.


 In maze, at each intersection, you have to decide between 3 or fewer choices:
 Go straight
 Go left
 Go right
 You don’t have enough information to choose correctly
 Each choice leads to another set of choices.
 One or more sequences of choices may or may not lead to a solution.
 Many types of maze problem can be solved with backtracking.

Sorting the array of integers in a[1:n] is a problem whose solution is expressible by an n-tuple
xi is the index in ‘a’ of the ith smallest element.
The criterion function ‘P’ is the inequality a[xi]≤ a[xi+1] for 1≤ i ≤ n
Si is finite and includes the integers 1 through n.
mi size of set Si
m=m1m2m3---mn n tuples that possible candidates for satisfying the function P.
With brute force approach would be to form all these n-tuples, evaluate (judge) each one with P
and save those which yield the optimum.
By using backtrack algorithm; yield the same answer with far fewer than ‘m’ trails.
Many of the problems we solve using backtracking requires that all the solutions satisfy a
complex set of constraints.
For any problem these constraints can be divided into two categories:

DESIGN AND ANALYSIS OF ALGORITHMS Page 64


 Explicit constraints.
 Implicit constraints.

Explicit constraints: Explicit constraints are rules that restrict each xi to take on values only
from a given set.
Example: xi ≥ 0 or si={all non negative real numbers}
Xi=0 or 1 or Si={0, 1}
li ≤ xi ≤ ui or si={a: li ≤ a ≤ ui }
The explicit constraint depends on the particular instance I of the problem being solved. All
tuples that satisfy the explicit constraints define a possible solution space for I.
Implicit Constraints:
The implicit constraints are rules that determine which of the tuples in the solution space of I
satisfy the criterion function. Thus implicit constraints describe the way in which the Xi must
relate to each other.
Applications of Backtracking:
 N Queens Problem
 Sum of subsets problem
 Graph coloring
 Hamiltonian cycles.

N-Queens Problem:
It is a classic combinatorial problem. The eight queen’s puzzle is the problem of placing eight
queens puzzle is the problem of placing eight queens on an 8×8 chessboard so that no two
queens attack each other. That is so that no two of them are on the same row, column, or
diagonal.
The 8-queens puzzle is an example of the more general n-queens problem of placing n queens on
an n×n chessboard.

Here queens can also be numbered 1 through 8


Each queen must be on a different row
Assume queen ‘i’ is to be placed on row ‘i’
All solutions to the 8-queens problem can therefore be represented a s s-tuples(x1, x2, x3—x8)
xi the column on which queen ‘i’ is placed
si {1, 2, 3, 4, 5, 6, 7, 8}, 1 ≤ i ≤8
Therefore the solution space consists of 88 s-tuples.
The implicit constraints for this problem are that no two xi’s can be the same column and no two
queens can be on the same diagonal.
By these two constraints the size of solution pace reduces from 88 tuples to 8! Tuples.
Form example si(4,6,8,2,7,1,3,5)

DESIGN AND ANALYSIS OF ALGORITHMS Page 65


In the same way for n-queens are to be placed on an n×n chessboard, the solution space consists
of all n! Permutations of n-tuples (1,2, ---n).

Some solution to the 8-Queens problem


Algorithm for new queen be placed All solutions to the n·queens problem
Algorithm Place(k,i) Algorithm NQueens(k, n)
//Return true if a queen can be placed in kth // its prints all possible placements of n-
row & ith column queens on an n×n chessboard.
//Other wise return false {
{ for i:=1 to n do{
for j:=1 to k-1 do if Place(k,i) then
if(x[j]=i or Abs(x[j]-i)=Abs(j-k))) {
then return false X[k]:=I;
return true if(k==n) then write (x[1:n]);
} else NQueens(k+1, n);
}
}}

DESIGN AND ANALYSIS OF ALGORITHMS Page 66


Sum of Subsets Problem:
Given positive numbers wi 1 ≤ i ≤ n, & m, here sum of subsets problem is finding all subsets of
wi whose sums are m.
Definition: Given n distinct +ve numbers (usually called weights), desire (want) to find all
combinations of these numbers whose sums are m. this is called sum of subsets problem.
To formulate this problem by using either fixed sized tuples or variable sized tuples.
Backtracking solution uses the fixed size tuple strategy.

For example:
If n=4 (w1, w2, w3, w4)=(11,13,24,7) and m=31.
Then desired subsets are (11, 13, 7) & (24, 7).
The two solutions are described by the vectors (1, 2, 4) and (3, 4).

In general all solution are k-tuples (x1, x2, x3---xk) 1 ≤ k ≤ n, different solutions may have
different sized tuples.

 Explicit constraints requires xi ∈ {j / j is an integer 1 ≤ j ≤ n }


 Implicit constraints requires:
No two be the same & that the sum of the corresponding wi’s be m
i.e., (1, 2, 4) & (1, 4, 2) represents the same. Another constraint is xi<xi+1 1 ≤ i ≤ k

Wi weight of item i

DESIGN AND ANALYSIS OF ALGORITHMS Page 67


M Capacity of bag (subset)
Xi the element of the solution vector is either one or zero.
Xi value depending on whether the weight wi is included or not.
If Xi=1 then wi is chosen.
If Xi=0 then wi is not chosen.

The above equation specify that x1, x2, x3, --- xk cannot lead to an answer node if this condition
is not satisfied.

The equation cannot lead to solution.

Recursive backtracking algorithm for sum of subsets problem


Algorithm SumOfSub(s, k, r)
{

X[k]=1
If(S+w[k]=m) then write(x[1: ]); // subset found.
Else if (S+w[k] + w{k+1] ≤ M)
Then SumOfSub(S+w[k], k+1, r-w[k]);
if ((S+r - w{k] ≥ M) and (S+w[k+1] ≤M) ) then
{
X[k]=0;
SumOfSub(S, k+1, r-w[k]);
}
}

DESIGN AND ANALYSIS OF ALGORITHMS Page 68


Graph Coloring:
Let G be a undirected graph and ‘m’ be a given +ve integer. The graph coloring problem is
assigning colors to the vertices of an undirected graph with the restriction that no two adjacent
vertices are assigned the same color yet only ‘m’ colors are used.
The optimization version calls for coloring a graph using the minimum number of coloring.
The decision version, known as K-coloring asks whether a graph is colourable using at most k-
colors.
Note that, if ‘d’ is the degree of the given graph then it can be colored with ‘d+1’ colors.
The m- colorability optimization problem asks for the smallest integer ‘m’ for which the graph G
can be colored. This integer is referred as “Chromatic number” of the graph.
Example

 Above graph can be colored with 3 colors 1, 2, & 3.


 The color of each node is indicated next to it.
 3-colors are needed to color this graph and hence this graph’ Chromatic Number
is 3.
 A graph is said to be planar iff it can be drawn in a plane (flat) in such a way that no two
edges cross each other.
 M-Colorability decision problem is the 4-color problem for planar graphs.
 Given any map, can the regions be colored in such a way that no two adjacent regions
have the same color yet only 4-colors are needed?
 To solve this problem, graphs are very useful, because a map can easily be transformed
into a graph.
 Each region of the map becomes a node, and if two regions are adjacent, then the
corresponding nodes are joined by an edge.

o Example:

o
DESIGN AND ANALYSIS OF ALGORITHMS Page 69
The above map requires 4 colors.
 Many years, it was known that 5-colors were required to color this map.

 After several hundred years, this problem was solved by a group of mathematicians with
the help of a computer. They show that 4-colors are sufficient.
Suppose we represent a graph by its adjacency matrix G[1:n, 1:n]

Ex:

Here G[i, j]=1 if (i, j) is an edge of G, and G[i, j]=0 otherwise.


Colors are represented by the integers 1, 2,---m and the solutions are given by the n-tuple (x1,
x2,---xn)
xi Color of node i.

State Space Tree for


n=3 nodes
m=3 colors

1st node coloured in 3-ways


2nd node coloured in 3-ways
3rd node coloured in 3-ways
So we can colour in the graph in 27 possibilities of colouring.

DESIGN AND ANALYSIS OF ALGORITHMS Page 70


Finding all m-coloring of a graph Getting next color
Algorithm mColoring(k){ Algorithm NextValue(k){
// g(1:n, 1:n) boolean adjacency matrix. //x[1],x[2],---x[k-1] have been assigned
// k index (node) of the next vertex to integer values in the range [1, m]
color. repeat {
repeat{ x[k]=(x[k]+1)mod (m+1); //next highest
nextvalue(k); // assign to x[k] a legal color. color
if(x[k]=0) then return; // no new color if(x[k]=0) then return; // all colors have
possible been used.
if(k=n) then write(x[1: n]; for j=1 to n do
else mcoloring(k+1); {
} if ((g[k,j]≠0) and (x[k]=x[j]))
until(false) then break;
} }
if(j=n+1) then return; //new color found
} until(false)
}

Adjacency matrix is

DESIGN AND ANALYSIS OF ALGORITHMS Page 71


Hamiltonian Cycles:
 Def: Let G=(V, E) be a connected graph with n vertices. A Hamiltonian cycle is a round
trip path along n-edges of G that visits every vertex once & returns to its starting
position.
 It is also called the Hamiltonian circuit.
 Hamiltonian circuit is a graph cycle (i.e., closed loop) through a graph that visits each
node exactly once.
 A graph possessing a Hamiltonian cycle is said to be Hamiltonian graph.
Example:

 In graph G, Hamiltonian cycle begins at some vertiex v1 ∈ G and the vertices


of G are visited in the order v1,v2,---vn+1, then the edges (vi, vi+1) are in E, 1 ≤ i ≤
n.

g1
The above graph contains Hamiltonian cycle: 1,2,8,7,6,5,4,3,1

The above graph contains no Hamiltonian cycles.

 There is no known easy way to determine whether a given graph contains a


Hamiltonian cycle.
 By using backtracking method, it can be possible
 Backtracking algorithm, that finds all the Hamiltonian cycles in a graph.
 The graph may be directed or undirected. Only distinct cycles are output.
 From graph g1 backtracking solution vector= {1, 2, 8, 7, 6, 5, 4, 3, 1}
 The backtracking solution vector (x1, x2, --- xn)
xi ith visited vertex of proposed cycle.

DESIGN AND ANALYSIS OF ALGORITHMS Page 72


 By using backtracking we need to determine how to compute the set of possible
vertices for xk if x1,x2,x3---xk-1 have already been chosen.
If k=1 then x1 can be any of the n-vertices.
By using “NextValue” algorithm the recursive backtracking scheme to find all Hamiltoman
cycles.
This algorithm is started by 1st initializing the adjacency matrix G[1:n, 1:n] then setting x[2:n]
to zero & x[1] to 1, and then executing Hamiltonian (2)
Generating Next Vertex Finding all Hamiltonian Cycles
Algorithm NextValue(k) Algorithm Hamiltonian(k)
{ {
// x[1: k-1] is path of k-1 distinct vertices. Repeat{
// if x[k]=0, then no vertex has yet been NextValue(k); //assign a legal next value to
assigned to x[k] x[k]
Repeat{ If(x[k]=0) then return;
X[k]=(x[k]+1) mod (n+1); //Next vertex If(k=n) then write(x[1:n]);
If(x[k]=0) then return; Else Hamiltonian(k+1);
If(G[x[k-1], x[k]]≠0) then } until(false)
{ }
For j:=1 to k-1 do if(x[j]=x[k]) then break;
//Check for distinctness
If(j=k) then //if true , then vertex is distinct
If((k<n) or (k=n) and G[x[n], x[1]]≠0))
Then return ;
}
}
Until (false);
}

DESIGN AND ANALYSIS OF ALGORITHMS Page 73


UNIT-V
Branch and Bound: General method, applications- Travelling sales person problem, 0/1
Knapsack problem- LC branch and Bound solution, FIFO branch and Bound solution.

NP-Hard and NP-Complete Problems: Basic concepts, Non deterministic algorithms, NP-Hard
and NP Complete classes, NP-Hard problems, Cook’s theorem.
Branch & Bound
Branch & Bound (B & B) is general algorithm (or Systematic method) for finding optimal
solution of various optimization problems, especially in discrete and combinatorial
optimization.
The B&B strategy is very similar to backtracking in that a state space tree is used to solve
a problem.
The differences are that the B&B method
 Does not limit us to any particular way of traversing the tree.
 It is used only for optimization problem
 It is applicable to a wide variety of discrete combinatorial problem.
B&B is rather general optimization technique that applies where the greedy method &
dynamic programming fail.
It is much slower, indeed (truly), it often (rapidly) leads to exponential time complexities
in the worst case.
The term B&B refers to all state space search methods in which all children of the “E-
node” are generated before any other “live node” can become the “E-node”
 Live node is a node that has been generated but whose children have not yet been
generated.
 E-node is a live node whose children are currently being explored.
 Dead node is a generated node that is not to be expanded or explored any further. All
children of a dead node have already been expanded.

Two graph search strategies, BFS & D-search (DFS) in which the exploration of a new
node cannot begin until the node currently being explored is fully explored.
Both BFS & D-search (DFS) generalized to B&B strategies.
 BFS like state space search will be called FIFO (First In First Out) search as the list of
live nodes is “First-in-first-out” list (or queue).
 D-search (DFS) Like state space search will be called LIFO (Last In First Out) search
as the list of live nodes is a “last-in-first-out” list (or stack).
In backtracking, bounding function are used to help avoid the generation of sub-trees that
do not contain an answer node.
We will use 3-types of search strategies in branch and bound
1) FIFO (First In First Out) search
2) LIFO (Last In First Out) search

DESIGN AND ANALYSIS OF ALGORITHMS Page 74


3) LC (Least Count) search

FIFO B&B:
FIFO Branch & Bound is a BFS.
In this, children of E-Node (or Live nodes) are inserted in a queue.
Implementation of list of live nodes as a queue
 Least() Removes the head of the Queue
 Add() Adds the node to the end of the Queue

Assume that node ‘12’ is an answer node in FIFO search, 1st we take E-node has ‘1’

LIFO B&B:
LIFO Brach & Bound is a D-search (or DFS).
In this children of E-node (live nodes) are inserted in a stack
Implementation of List of live nodes as a stack
 Least() Removes the top of the stack
 ADD() Adds the node to the top of the stack.

Least Cost (LC) Search:


The selection rule for the next E-node in FIFO or LIFO branch and bound is sometimes
“blind”. i.e., the selection rule does not give any preference to a node that has a very good
chance of getting the search to an answer node quickly.

The search for an answer node can often be speeded by using an “intelligent” ranking
function. It is also called an approximate cost function “Ĉ”.

DESIGN AND ANALYSIS OF ALGORITHMS Page 75


Expended node (E-node) is the live node with the best Ĉ value.
Branching: A set of solutions, which is represented by a node, can be partitioned into
mutually (jointly or commonly) exclusive (special) sets. Each subset in the partition is
represented by a child of the original node.
Lower bounding: An algorithm is available for calculating a lower bound on the cost of any
solution in a given subset.

Each node X in the search tree is associated with a cost: Ĉ(X)


C=cost of reaching the current node, X(E-node) form the root + The cost of reaching an
answer node form X.
Ĉ=g(X)+H(X).

Example:
8-puzzle
Cost function: Ĉ = g(x) +h(x)
where h(x) = the number of misplaced tiles
and g(x) = the number of moves so far
Assumption: move one tile in any direction cost 1.

DESIGN AND ANALYSIS OF ALGORITHMS Page 76


Note: In case of tie, choose the leftmost node.

Travelling Salesman Problem:


Def:- Find a tour of minimum cost starting from a node S going through other nodes
only once and returning to the starting point S.
Time Conmlexity of TSP for Dynamic Programming algorithm is O(n22n)
B&B algorithms for this problem, the worest case complexity will not be any better than
O(n22n) but good bunding functions will enables these B&B algorithms to solve some
problem instances in much less time than required by the dynamic programming alogrithm.
Let G=(V,E) be a directed graph defining an instances of TSP.
Let Cij cost of edge <i, j>

Cij =∞ if <i, j> ∉ E


|V|=n total number of vertices.
Assume that every tour starts & ends at vertex 1.
Solution Space S= {1, Π , 1 / Π is a permutation of (2, 3. 4 ----- n) } then |S|=(n-1)!
The size of S reduced by restricting S
Sothat (1, i1,i2,-----in-1, 1}∈ S iff <ij, ij+1>∈ E. O≤j≤n-1, i0-in=1
S can be organized into “State space tree”.
Consider the following Example

State space tree for the travelling salesperson problem with n=4 and i0=i4=1

The above diagram shows tree organization of a complete graph with |V|=4.
Each leaf node ‘L’ is a solution node and represents the tour defined by the path from the root
to L.

Node 12 represents the tour.


i0=1, i1=2, i2=4, i3=3, i4=1
Node 14 represents the tour.
i0=1, i1=3, i2=4, i3=2, i4=1.

DESIGN AND ANALYSIS OF ALGORITHMS Page 77


TSP is solved by using LC Branch & Bound:

DESIGN AND ANALYSIS OF ALGORITHMS Page 78


Row Reduction-

Consider the rows of above matrix one by one.


If the row already contains an entry ‘0’, then-
 There is no need to reduce that row.
If the row does not contains an entry ‘0’, then-

 Reduce that particular row.


 Select the least value element from that row.
 Subtract that element from each element of that row.
 This will create an entry ‘0’ in that row, thus reducing that row.

Following this, we have-

 Reduce the elements of row-1 by 4.


 Reduce the elements of row-2 by 5.
 Reduce the elements of row-3 by 6.
 Reduce the elements of row-4 by 2.

Performing this, we obtain the following row-reduced matrix-

DESIGN AND ANALYSIS OF ALGORITHMS Page 79


Finally, the initial distance matrix is completely reduced.
Now, we calculate the cost of node-1 by adding all the reduction elements.

Cost(1)
= Sum of all reduction elements
=4 +5 +6+2 +1
= 18

Step-02:

 We consider all other vertices one by one.


 We select the best vertex where we can land upon to minimize the tour cost.

DESIGN AND ANALYSIS OF ALGORITHMS Page 80


Choosing To Go To Vertex-B: Node-2 (Path A → B)

 From the reduced matrix of step-01, M[A,B] = 0


 Set row-A and column-B to ∞
 Set M[B,A] = ∞

Now, resulting cost matrix is-

DESIGN AND ANALYSIS OF ALGORITHMS Page 81


DESIGN AND ANALYSIS OF ALGORITHMS Page 82
DESIGN AND ANALYSIS OF ALGORITHMS Page 83
DESIGN AND ANALYSIS OF ALGORITHMS Page 84
Column Reduction-

 There is no need to reduce column-1.


 There is no need to reduce column-2.
 There is no need to reduce column-3.
 We can not reduce column-4 as all its elements are ∞.

Thus, the matrix is already column-reduced.


Finally, the matrix is completely reduced.
Now, we calculate the cost of node-4.

Cost(4)
= Cost(1) + Sum of reduction elements + M[A,D]
= 18 + 5 + 3
= 26

Thus, we have-

 Cost(2) = 36 (for Path A → B)


 Cost(3) = 25 (for Path A → C)

DESIGN AND ANALYSIS OF ALGORITHMS Page 85


 Cost(4) = 26 (for Path A → D)

We choose the node with the lowest cost.


Since cost for node-3 is lowest, so we prefer to visit node-3.
Thus, we choose node-3 i.e. path A → C.

Step-03:

We explore the vertices B and D from node-3.


We now start from the cost matrix at node-3 which is-

DESIGN AND ANALYSIS OF ALGORITHMS Page 86


Now, we calculate the cost of node-5.

Cost(5)
DESIGN AND ANALYSIS OF ALGORITHMS Page 87
= cost(3) + Sum of reduction elements + M[C,B]

= 25 + (13 + 8) + ∞

=∞

Choosing To Go To Vertex-D: Node-6 (Path A → C → D)

 From the reduced matrix of step-02, M[C,D] = ∞


 Set row-C and column-D to ∞
 Set M[D,A] = ∞

Now, resulting cost matrix is-

DESIGN AND ANALYSIS OF ALGORITHMS Page 88


DESIGN AND ANALYSIS OF ALGORITHMS Page 89
DESIGN AND ANALYSIS OF ALGORITHMS Page 90
O/1 Knapsack Problem

What is Knapsack Problem: Knapsack problem is a problem in combinatorial optimization,


Given a set of items, each with a mass & a value, determine the number of each item to
include in a collection so that the total weight is less than or equal to a given limit & the total
value is as large as possible.
O-1 Knapsack Problem can formulate as. Let there be n items, Z1 to Zn where Zi has value
Pi & weight wi. The maximum weight that can carry in the bag is m.
All values and weights are non negative.
Maximize the sum of the values of the items in the knapsack, so that sum of the weights must
be less than the knapsack’s capacity m.

DESIGN AND ANALYSIS OF ALGORITHMS Page 91


The formula can be stated as

Xi=0 or 1 1 ≤ i ≤ n

To solve o/1 knapsack problem using B&B:

 Knapsack is a maximization problem

 Replace the objective function bythe function to make it into a


minimization problem
 The modified knapsack problem is stated as

Fixed tuple size solution space:


o Every leaf node in state space tree represents an answer for which

is an answer node; other leaf nodes are infeasible


o For optimal solution, define

for every answer node x

For infeasible leaf nodes,


For non leaf nodes
c(x) = min{c(lchild(x)), c(rchild(x))}

Define two functions cˆ(x) and u(x) such that for every
node x,
cˆ(x) ≤ c(x) ≤ u(x)

DESIGN AND ANALYSIS OF ALGORITHMS Page 92


Computing cˆ(·) and u(·)

Algorithm ubound ( cp, cw, k, m )


{
// Input: cp: Current profit total
// Input: cw: Current weight total
// Input: k: Index of last removed item
// Input: m: Knapsack capacity
b=cp; c=cw;
for i:=k+1 to n do{
if(c+w[i] ≤ m) then {
c:=c+w[i]; b=b-p[i];
}
}
return b;
}

DESIGN AND ANALYSIS OF ALGORITHMS Page 93


FIFO stands or First In First Out, is follows the BFS technique. As like BFS, In this FIFO Branch and Bound
reach a node to find upper bound and lower bound values and next it reaches another node in the same level of
state space tree. And next reaches the node which is presented in the queue. FIFO Branch and Bound
applications are knapsack problem and travelling salesman problem. This FIFO is one of the techniques of
LC– search. When implementing a FIFO branch and bound algorithm, here reach each and every node in the
state space tree. Here can find out more than one solution for the problem, if the problem has the multiple
paths of solution. Unlike LCBB, The FIFO BB needs more memory to show each node in the state space tree.
The main principle of the FIFO is reaching every node which is generated by solution process in the state
space tree.
LCBB

For speeding up the search process here need to intelligent ranking function for live nodes. Each time, the
next E- node is selected on the basis of this ranking function. For this ranking function additional
computation (normally cost) is needed to reach the answer node from the live node. LC-search is a kind of
search in which least cost involved for reaching to answer node. At each E-node the probability of being an
answer node is checked. BFS and D-search are special cases of LC search. An LC search with bounding
functions in known as LC Branch and Bound search. The applications of LC Branch and Bound are 0/1
Knapsack problem and Travelling salesman problem.
The search mainly based on the state space tree. To find upper bound value and lower bound value at the each
nodeto get the ranking value to identify which node is least cost.
Basic concepts:
NP Nondeterministic Polynomial time
-)

The problems has best algorithms for their solutions have “Computing times”, that cluster
into two groups
Group 1 Group 2

> Problems with solution time bound by > Problems with solution times not
a polynomial of a small degree. bound by polynomial (simply non
polynomial )

> It also called “Tractable Algorithms”


These are hard orintractable
> problems

DESIGN AND ANALYSIS OF ALGORITHMS Page 94


Most Searching & Sorting algorithms
> are polynomial time algorithms
None of the problems in this group
> has been solved by any polynomial
> Ex: time algorithm
Ordered Search (O (log n)),
Polynomial evaluation O(n) > Ex:
Traveling Sales Person O(n2 2n)
Sorting O(n.log n)
Knapsack O(2n/2)

No one has been able to develop a polynomial time algorithm for any problem in the 2nd
group (i.e., group 2)

So, it is compulsory and finding algorithms whose computing times are greater than
polynomial very quickly because such vast amounts of time to execute that even moderate
size problems cannot be solved.

Theory of NP-Completeness:
Showthat may of the problemswith no polynomial time algorithmsare computationaltime
algorithms are computationally related.

There are two classes of non-polynomial time problems

1. NP-Hard
2. NP-Complete

DESIGN AND ANALYSIS OF ALGORITHMS Page 95


DESIGN AND ANALYSIS OF ALGORITHMS (UNIT-VIII)

NP Complete Problem: A problem that is NP-Complete can solved in polynomial time if


and only if (iff) all other NP-Complete problems can also be solved in polynomial time.

NP-Hard: Problem can be solved in polynomial time then all NP-Complete problems can be
solved in polynomial time.

All NP-Complete problemsare NP-Hard but some NP-Hard problemsare not know to be NP-
Complete.

Non deterministic Algorithms:


Algorithms with the property that the result of every operation is uniquely defined are termed
as deterministic algorithms. Such algorithms agree with the way programs are executed on a
computer.

Algorithmswhich contain operations whose outcomes are not uniquely defined but are
limited to specified set of possibilities. Such algorithms are called nondeterministic
algorithms.
The machine executing such operations is allowed to choose any one of these outcomes
subject to a termination condition to be defined later.

To specify nondeterministic algorithms, there are 3 new functions.

Choice(S) arbitrarily chooses one of the elements of sets S


-)

Failure () Signals an Unsuccessful completion


-)

Success () Signals a successful completion.


-)

Example for Non Deterministic algorithms:


Algorithm Search(x){ Whenever there is a set of choices
//Problem is to search an element x that leads to a successful completion
then one such set of choices is
//output J, suchthat A[J]=x; or J=0 if x is not in A
always made and the algorithm
J:=Choice(1,n);
terminates.
if( A[J]:=x) then {
A Nondeterministic algorithm
Write(J); terminates unsuccessfully if and
Success(); only if (iff) there exists no set of
} choices leading to a successful
signal.
else{
write(0);
failure();

DESIGN AND ANALYSIS OF ALGORITHMS Page 96


DESIGN AND ANALYSIS OF ALGORITHMS (UNIT-VIII)

Nondeterministic Knapsack algorithm

Algorithm DKP(p, w, n, m, r, x){ p given Pro fit s


-)

W:=0; w given Weights


-)

P:=0; n Number of elements (nu mber of


-)

for i:=1 to n do{ p or w)


x[i]:=choice(0, 1); m Weight of bag limit
-)

W:=W+x[i]*w[i]; P Final Pro fit


-)

P:=P+x[i]*p[i]; W Final weight


-)

}
if( (W>m) or (P<r)) then Failure();
else Success();
}

The Classes NP-Hard & NP-Complete:


For measuring the complexity of an algorithm, we use the input length as the parameter. For
example, An algorithm A is of polynomial complexity p() such that the computing time of A
is O(p(n)) for every input of size n.
Decision problem/ Decision algorithm: Any problem for which the answer is either zero or
one is decision problem. Any algorithm for a decision problem is termed a decision
algorithm.
Optimization problem/ Optimization algorithm: Any problem that involves the
identification of an optimal (either minimum or maximum) value of a given cost function is
known as an optimization problem. An optimization algorithm is used to solve an
optimization problem.

P is the set of all decision problems solvable by deterministic algorithms in polynomial


-)

time.
NP is the set of all decision problems solvable by nondeterministic algorithms in
-)

polynomial time.

Since deterministic algorithmsare just a special case of nondeterministic, by this we


concluded that P NP ⊆

Commonly believed relationship between P & NP

DESIGN AND ANALYSIS OF ALGORITHMS Page 97


DESIGN AND ANALYSIS OF ALGORITHMS (UNIT-VIII)

The most famous unsolvable problems in Computer Science is Whether P=NP or P≠NP
In considering this problem, s.cook formulated the following question.

If there any single problem in NP, such that if we showed it to be in ‘P’ then that would
imply that P=NP.

Cook answered this question with

Theorem: Satisfiability is in P if and only if (iff) P=NP

-)Notation of Reducibility
Let L1 and L2 be problems, Problem L1 reduces to L2 (written L1 α L2) iff there is a way to
solve L1 by a deterministic polynomial time algorithm using a deterministic algorithm that
solves L2 in polynomial time
This implies that, if we have a polynomial time algorithm for L2, Then we can solve L1 in
polynomial time.

Here α-) is a transitive relation i.e., L1 α L2 and L2 α L3 then L1 α L3


A problem L is NP-Hard if and only if (iff) satisfiability reduces to L ie., Statisfiability α L

A problem L is NP-Complete if and only if (iff) L is NP-Hard and L Є NP

Commonly believed relationship among P, NP, NP-Complete and NP-Hard

Most natural problems in NP are either in P or NP-complete.


Examples of NP-complete problems:
> Packing problems: SET-PACKING, INDEPENDENT-SET.
> Covering problems: SET-COVER, VERTEX-COVER.
> Sequencing problems: HAMILTONIAN-CYCLE, TSP.
> Partitioning problems: 3-COLOR, CLIQUE.
> Constraint satisfaction problems: SAT, 3-SAT.
> Numerical problems: SUBSET-SUM, PARTITION, KNAPSACK.

DESIGN AND ANALYSIS OF ALGORITHMS Page 98


DESIGN AND ANALYSIS OF ALGORITHMS (UNIT-VIII)

DESIGN AND ANALYSIS OF ALGORITHMS Page 99


DESIGN AND ANALYSIS OF ALGORITHMS Page 100
DESIGN AND ANALYSIS OF ALGORITHMS Page 101
Cook’s Theorem: States that satisfiability is in P if and only if P=NP If
P=NP then satisfiability is in P
If satisfiability is in P, then P=NP
To do this
> A-) Any polynomial time nondeterministic decision algorithm.
I-)Input of that algorithm
Then formula Q(A, I), Such that Q is satisfiable iff ‘A’ has a successful
termination with Input I.

> If the length of ‘I’ is ‘n’ and the time complexity of A is p(n) for some polynomial
p() then length of Q is O(p3(n) log n)=O(p4(n))
The time needed to construct Q is also O(p3(n) log n).

> A deterministic algorithm ‘Z’ to determine the outcome of ‘A’ on any input ‘I’
Algorithm Z computes ‘Q’ and then uses a deterministic algorithm for the
satisfiability problem to determine whether ‘Q’ is satisfiable.

> If O(q(m)) is the time needed to determine whether a formula of length ‘m’ is
satisfiable then the complexity of ‘Z’ is O(p (n) log n + q(p (n)log n)).
3 3

> If satisfiability is ‘p’, then ‘q(m)’ is a polynomial function of ‘m’ and the
complexity of ‘Z’ becomes ‘O(r(n))’ for some polynomial ‘r()’.

> Hence, if satisfiability is in p, then for every nondeterministic algorithm A in NP, we


can obtain a deterministic Z in p.
By this we shows that satisfiability is in p then P=NP

DESIGN AND ANALYSIS OF ALGORITHMS Page 102

You might also like