0% found this document useful (0 votes)
297 views100 pages

ADA Techmax Searchable

Uploaded by

Shivam Prasad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
297 views100 pages

ADA Techmax Searchable

Uploaded by

Shivam Prasad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 100

Syllabus

Medute |:
Mumbai University
Dynamic Programming Approach :
Revised syllabus (Rev-2016) from Academic Year 2017-18
General Method ™
Analysis of Algorithms Multistage graphs
single source shortest path, all pair shortest path
| Coursa Coda Course Name Credits
Assembly-line scheduling
| cscso2 Analysis of Algorithms 4
O/1 knapsack
Travelling salesman problem
Course Objectives :
Longest common subsequence. (Refer Chapter4)
1. To provide mathematical approach for Analysis of Algorithms
Greedy Method Approach : Shr.
2. To solve problems using various strategies General Method
3. To analyse strategies for solving problems not solvable in polynomial time. Single source shortest path
Course Outcomes : At the end of the course student should be able to Knapsack problem
Job sequencing with deadlines
Analyze the running time and space complexity of algorithms.
Minimum cost spanning trees-Kruskal and prim’s algorithm Optimal storage on tapes.
2. Describe, apply and analyze the complexity of divide and conquer strategy.
(Refer Chapter 5)
3. Describe, apply and analyze the complexity of greedy strategy.
Backtracking and Branch-and-bound : 8hrs.
4. Describe, apply and analyze the complexity of dynamic programming strategy. General Method
some hard
5. Explain and apply backtracking, branch and bound and string matching techniques to deal with 8 queen problem( N-queen problem)
problems. Sum of subsets
problem is NP-Complete.
6. Describe the classes P, NP, and NP-Complete and be able to prove that a certain Graph coloring
requisites : Students should be familiar with concepts of Data structure
and discrete structures. 15 puzzle problem,
Hours (Refer Chapter
| Module | Detailed Content gon Travelling problem.
String Matching Algorithms :
12 hrs.
1. Introduction to analysis of algorithm ‘The naive string matching Algorithms
Performance analysis, space and time complexity The Rabin Karp algorithm
al background for String matching with finite automata
Growth of function — Big - Oh, Omega, Theta notation Mathematic
‘The knuth-Morris-Pratt algorithm. (Refer Chapter
algorithm analysis,
Non-deterministic polynomial algorithms :
Analysis of selection sort, insertion sort.
Polynomial time,
Recurrences : The substitution method
Polynomial time verification
- Recursion tree method NP Completeness andreducibility
- Master method NP Completeness proofs

Divide and Conquer Approach: Vertex Cover Problems


(Refer Chapter 8)
Clique Problems.
General method n a00
Binary search, Finding
Analysis of Merge sort, Analysis of Quick sort, Analysis of
matrix multiplication.
minimum and maximum algorithm and analysis, Strassen’s
(Refer Chapters 1, 2 and 3)

Scanned by CamScanner
ee Analysis of Algorithms (MU - Sem 4 - Comp) 1 Table of Contents
22 Marge Sort ... so
¥ >P Syilabus Topic : Analysis of Quick Sort
Lab Code yes Sort.
Analysis of Algorithms Lab
Chapter 1: Introduction to Analysis of Algorithm Syllabus Tople : Analysis of Binary Search
CSL401 1-1 to 1-19 8 Bay Search (Dec, 15, May 17).........
Lab Outcomes : Syflabus : Performance analysis, space and tme complexity s yilabus Tople : Finding Minimum and Maximum
Algorithm and Analysis.
1. Analyze the complexities of various problems in different domains. Growth of function - Big Oh, Omega, Theta notation Mathematical
the basic algorithms for those classic problems in
background for algorithm analysts, Analysis of selection sort, 2.5.7 Min-Max Problem
2. Prove the correctness and analyze the running time of insertion sort. i (May 14, Dec. 14, May 16, May 17)...
various domains. v ‘Syllabus Topic : Strassen's Matrix Multiplication .
1A Zn
3. Develop the efficient algorithms for the new problem with suitable designing techniques. 144 [ Strassen's Matrix Multiplication
4. Implement the algorithms using different strategies. 1.1.2 Properties of Algorithm. 1-4 (Dec. 13, May 15, Dec. 15, May 16, May 17)
Discrete structures. 4.1.3 Howto write an Algorithm 9.....c0..se 27 Exam Pack (University and Review Quastions)..
Prerequisites ; Students should be familiar with concepts of Data structure and __—
y Syllabus Topic : Performance Analysis
‘ Chapter 3 : Recurrence 3-1 to 3-26
‘Description : 12 e Analysis.
v Syllabus Tople : Space and Time Ce
Minimum 2 experiments should be implemented using any language on each algorithm design strategy ‘Syllabus : The substitution method - Recursion trea method -
(Divide and conquer, dynamic programming, Greedy method, backtracking and branch & bound,
string 1.24 Space Complexity (May 13, Dec. 12).... Mi faster method.
matching). 2ra Time Gomplon
(May 13, ity
Dec. 13)...
Syllabus Topic : Growth of Funclion Big Oh, Omega, v Syllabus Topic : The Substitution Method.... at
Suggested Laboratory Experiments : ‘Theta Notation. 34 The Substitution Method... a
1.23 Growth of Function ¥ Syllabus Topic : Recursion Tree Method a4
Sr. Module Name Suggested Experiment List Yo 1.2.4 Asymptotic Notation (wey Dec. 13, May 14, 32 4 Recursion Tree 34
No. Dec, 15, May 16, Dec, 16) . -
1.2.4(A) Big Oh.
5 ¥ Se
_-7Sillabus Tople : Master Method .. 35
1 Introduction to analysis of algorithm Divide and Selection sont, insertion sor. Merge sort, Quick sort, 1.2.4(8) Big Omeg 33° —K Master Method (May 15, May 17).. 35
Conquer Approach. Binary search.
1.2.4(C) Big Theta.. 4-8 | 34 Exam Pack (University and Review Questions) wejind8
Dynamic Programming Approach. Multistage graphs, single source shortest path, all pair “ Syllabus Topic : Mathematical Background for
Pp

shortest path, 0/1 knapsack, Travelling salesman problem, Algorithm Analysis...cccssseesse essen
Longest common subsequence. 1.3. Mathematical Background for Algorithm Analysis.
1.3.1 Framework for Analysis of Chapter 4: Dynamic Programming 41 to 441
Greedy Method Approach. Single source shortest path, Knapsack problem, Job N 1-9
sequencing with deadlines, Minimum cost spanning trees- Syllabus : General Method, Multistage graphs, single source
ie cutee" ie aos ct een Mignone WB shortest path, all pair shortest path, Assembly-line scheduling, O/t
Kruskal and prim” s algorithm, Optimal storage on tapes. yilabus Tople : Analysis of Selection Sort, knapsack, Travelling salesman problem, Longest common
Backtracking and Branch-and-bound. 8 queen problem ( N-queen problem), Sum of subsets, Insertion Son... subsequence.
14 Analysis of Selection Sort and Insertion Sort. % ae Gonara Matted
Graph coloring, 15 puzzle problem, Travelling salesman 14.1 Selection Sott...... 'yilabus Topic: General Method..
problem. 14.2 Insertion Sort (May 17)... at General Method...
String Matching Algorithms. The naive string matching Algorithms, The Rabin Karp 1.5 Exam Pack (University and Review Questions 4.1.1 Introuction
. 412 Control Abstraction.
algorithm, String matching with finite automata, The Chapter 2: Divide and Conquer 2-1 10 2-26
knuth-Morris-Pratt algorithm. 413° of Dynamic P 42
Syllabus : General method, Analysis of Merge sort, Analysis of || 44.4 Applications...
Any two Experiments, This will involve implementation of two algorithms for ‘Quick sort, Analysis of Binary search, Finding the minimum and
42 Principle of Optimality.
problems beyond the scope of syllabus. The exact set of maximum algorithm and analysis, Strassen's matrix multiplication.
algorithms to implement is to be decided by the course ¥ Syllabus Topic : General Method... 4.3 Elements of Dynamic Programming
24 General Method (May 13, Dec. 13, May 14). 44 Divide and Conquer Vs Dynamic Programmin, 43
instructor.
21.1 Introduction . ’ yllabus Topic : Multistage Graph 43
2.1.2 Control Abstraction (May 13, Dec. 13). ye Vasc Graph (Dee. 16, May 17).. 43
goog 2.1.3, Efficiency Analysis. a Syllabus Topic : Single Source Shortest Path .........4-B
L244 Sorting (May 15)...
7 ¢ — ¥ Syllabus Topic : Analysis of Merge Sort....... 2-3
4.8 Single Source Shortest Path 48

Scanned by CamScanner
2 Table of Contents
a Analysis ,
EP anatysis of algorinms (MU - Sem 4- Comp) 3 Table of Contents
ses Asctins (Wy Som oe
~ Path
_28yllabus Topic:Al Pair Shonest 4.9 | & 09 TAPES xn .m 8-30
Syllabus Topic : Optimal Storage
Module 5 6
[=moaue 6]
47° ANLPsit Shortest Path (May 14 56 Optimal Storage on Tapes (May 13, Dee. 14)
5-31
. on Single Tape. Non Deterministic Polynomial Algorithm
¢ Syllabus Topic : Assembty-Line Scheduling
aad Chapter7: String Matching 7-110 7-09 | Chapter 8:
5.62 Storage on Muttiple Tapes... 8-1 10 8-07
48 Assembly Line Scheduling . . =
5.7 Exam Pack (University and Review Questions) ......5-32 Syllabus : The nalva string matching Algorithms, The Rabin Komp
= Syllabus Topic: 0/1 knapsack... algorithm, String matching with finite aulomata, The Syllabus : Poly time, Poly time NP
ean c and reduciblty NP C Proofs Vertex
DOE 1 Reapsack (May 14, Dec. 15)... Pratt algorithm,
~ 491 First Approach. 7 74 || Cover Problems Clique Problems,
Backtracking & Branch and Bound v Syllabus Topic : The Nalve String ¥ Syflabus Topic : Polynomial Tima.
“ ‘Syllabus Topic : Travelling Salesman Problem: Chapter6:
81 Polynomial Time..
410 Travelling Salesman Problem (May 15). 428 61 to 6-29 ae Algorithms.
The Naive String Matching Algorithms ¥ Syllabus Topic : Polynomial Time Verification
§
__ Syllabus TopicTopic : : Longest Common Subsequence ...4-32 | Sytiabus : General Method, 8 queen problam (N-queen problem), (May 14, Dac. 14, May 15, May 16).. B2 Polynomial Tima Verification..
v Topic : The Rabin Karp Algorithm
Syllabus ¥ Syllabus Topic : NP-Completeness and Reducibility .8-3
‘Sum of subsets, Graph coloring, 15 puzzle problem, Travelling
S-The Rabin Karp Algorithm 83 NP-Completeness and Reducibility ....
+2
(May 14, May 15, Dec. 15, May 16)..... W731 4 Syllabus Tople : NP-Completeness Proof:
aed a4 NP-Completeness Profs...
Syllabus Topic : String Matching with
_Fipite Automata... 84.1 Vertex Cover Problem...
7.4. String Matching with Finite Automata (May 17)... ¥ Syllabus Toplc : Clique Problem.
Chapter 5: : it
Greedy Algorithms 5-1 to 5-33 oe ‘syne Topie : The Knuth-Morris-Pratt Algorithm....7-6 | 8.4.2 Clique Protlem
78° Tha Knuth-Morts-Pratt Algorithm B5 Exam Pack (Review Questions)
Sylabus : General Method, Single source shonest path (May 13, Dec. 14, Dec. 15, May 17).
Knapeack problem, Jeb sequencing with deadlines, Minimum cost || €1.2(A) Recursive Backtracking Method . ° Lab Experiments...

‘spanning trees:Kruskal and Prim's algorithm, Optimal storage on || 6.1.2(8) Iterative Backtracking Method pas Exam Pack (University Questions) . .
. ot
Y Syllabus Topic : General Method... St + A Sylabus Topic : 8Queen Problem
54 €&__1€-Queen Problem
ayy TN (ulay 14, Dec. 14, Dee. 15, May 18, Dec. 16)..
e12 v Syllabus Topic : Sum of Subsets... .
as 51.5 Sumo Subset Problem Des. 15) BT

si3_tunestoersognmmna
Ne bus Topic : Graph Coloring,

538” Dynamic Programm


Vs Greedying
a

Approach
ee fo geenamr 62
‘Syllabus Topic ; 15 Puzzie Problem

Branch
and Bound.....
(May 13)... . .. 621 General Method (May 14, Dec. 15).
¥ Syllabus Topic : Single Source Shortest Patn.........g | 822 Backtracking Vs Branch and Bound
52 Single Source Shortest Path 62.3 Applications of Backtracking,

~ ___Siflabus
Topic : Knapsack Problem... 59 cea a aaasien
_-53” ¥ Knapsack Problem (May 17)
st
Fractional Knapsack Problem... —
<-“Siflabus Topic : Jod Sequencing with Deadfines... 82.4(0} Control Abstraction for FIFO Branch and Bound
4 + Job Sequenc (May 14,ing
Dec. 16)... . 512 BEE 15 Puzzle Problem (May 13, May14, Dec. 14,
v ‘Syllabus Topic: Minimum Cost Spanning
ae
v Syllabus Topie : Travelling Salesman Problem,
> Trees - Kruskal and Prim's Algorithm...
626 Travelling Salesman Problem (May 13, May 15)......
55 Minimum Spanning Tree ..... 6-18
62.6(A) LCBB using Static State Space Tree.
5.5.1 Basics of Graph. 6.2.6(B) LCBB using Dynamic State
Space Trev.
5.5.2’ Prim's Algorithm for MST...... 627 — Comparison Divide and
‘Conquer Dynamic
5.5.3 ~~ Kruskal's Algorithm... . Programming and Backtracking (May 17},
5.5.4 Differentiate : Prim’s vs. and Kruskal's algorithm. 5-30 63 Exam Pack (University and Review
Questions)

Scanned by CamScanner
Table ofConary,

Module1
| Lab Experiments D ont aii)
Introduction to
ae aaa of te Program Analysis of Algorithm
No.

1 ‘Write a program to it ‘selection sort.


2 Write a program to implement Insertion sor.
3. .
Write a program to implement Merge Sort Performance analysis, space and time complexity Growth of function - Big Oh, Omega, Theta notation
4 Write a program to implement Quick sort. Mathematical background for algorithm analysis, Analysis of selection sort, insertion sort.
SS Write a program to implement Single source shortest path using Dynamic Programming L4
(Bellman Ford Algorithm). ——__| 1.1 Introduction ret
6. Weite a program to solve knapsack problem using4 dynamiciC programing. I. LgeS — The term Algorithm was coined by the Persian.
L Write a program to solve Traveling Salesman Problem using Dynamic Programming. Ls mathematician al-Khowarizmi in the ninth century. The
. algorithm is a sct of rules which are used to solve real-
8. Write a program to implement Longest Common Subsequence problem using Dynamic Lz life problems. The algorithm provides a loose form ofa
Programming solution in a pseudo-programming language.
9. . . - Given the algorithm, it is easy to program the solution.
* | Write a program to salve single source shortest path problem using greedy approach Le It bridges the gap between natural Ianguage description
| (Dijkstra’s algorithm) of the solution znd syntax of programming language. Fig. 1.1.1 : Design of solution to the problem
10.__| Write a program
to solve Knapsack Probie using Greedy approach. Lao — The
fo M firs Igoritha nabet developed
moreat Ook by Babyloni
wide find wone of the | ~ If the algorithm is correct, then the program should
We Write a program to solve Job ation, Euclid had
Sequencing problem using greedy approach. . equation. Euclid had proposed da a fi famous algorithm
algorit fo
ir produce correct output on valid input, otherwise, it
72 . Writea program
to solve Optimal Storage on Tapes problem using greedy approach.
Lt
L412
finding
numbers.
greatest common divisor (GCD) of two should generate
example,
an appropriate error message. For
to find the division A/B, correctly written
13. | Write a program to solve N-queen problem using backtracking. = We can solves
treat ana algorithm
a L413 which particular asproblem
a set ofwhen
finite applied
instructions
il is Program
would showwould retum message
the error value oflikeA/B “Invalid
for B divisor”
> 0, and forit
Wieite a program to solve sum of subset prablem using 5 Uta applied to that problem with legal inpuls. Bo
[ 15. writ - 1.1.2 Properties of Algorithm
2 3 program to solve Graph coloring problem using 1.1.1 What Is Algorithm? - -
Write a program to soe Travelingin Sale ; Lis Q. Discuss various characteristics of the algorithm.
- g Salesman Problem using Branch and Bound. L416 1. Define algorithm. (2 Marks) (4 Marks)
Write a program to implement a Naive String Matchingalgorithm Q. Explain ‘of the good (4 Marks)
Write a program
fog) to imp
implement string ; - -
matching algorithm using Finite ta
LA? Definition 7 ‘A good algorithm should have following properties- /
s =
. . 7 characteristics :
Write a program to implement sn - Austoma LAB ‘The algorithm is set of rules defined in specific
ent string matchin g
using KMP algorithm 19 order to do ceriain computation and carry out some
I Le predefined task. It is a step by step procedure to solve.
nog the problem.
- Initially, the solution to the problem is thought as a
natural language description, whose syntax is too far
from the programming language.
— Before the actual problem solved in a programming
language, natural language description of the solution is
first represented as an algorithm. The algorithm is then
converted to code,
— The process of solving a problem is depicted in the
Fig. 1.1.1.
Fig. C1.1 : Characteristics of good algorithm

Scanned by CamScanner
8
Introduction to Analysis of Algorithm
Sy
[PPanaiysis ot (MU - Sem 4 - Comp) 12 [eT @¢
analysisof Algorithms (MU - Sem 4- Comp)
peal 13 Introduction to Analysis of Algorithm
> (1) Input
Algorithm may take zero or more input arguments. \ Head while (condition) do
Do some work
Syllabus Topic : Performance Analysis
Depending on the problem, the input may be a scalar,
vector, array, uee, graph or some other data structures.
1.2 Performance Analysis
12. For loop is writicn as follows :
Logical sequence of
> (2) Output for index + Firsthndex to Lastfudex do Syllabus Topic
statements to solve problem Body : Space and Time Complexity
Algorithm reads input, processes it and produces at {
least one output. Depending on the problem being solved,
Do some work 1.2.1 Space Complexity
the output may of the form scalar, vector, array, wee, graph Fig. 1.1.2 : Structure ofalgorithm Sometimes curly braces are omitted and block is closed > (May
13, Dec. 13)
Of some other data structures, with end keyword.
An algorithm is a lucid form ofprogram and it does not Q. Explain space complexity in detail
> (3) Definiteness for index ¢ FirstIndex to LastIndex do Eee ree Ie
have rigid restrictions of syntax. One can write an algorithm
All i in al; should be using his own termi . Do some work Q. What are the basic components which contribute to
and simple to interpret. There should not be multiple ways However, some of the common rules followed for
end space complexity? (4 Marks)
(o interpret the same instruction. Instructions should be writing algorithms, which are stated below : «= Examples of how to write algorithms
@. What do you mean by space complexity of an
precise and concise. 1. The algorithm should stan with the keyword algorithm? How do we measure the space complexity
Algorithm, followed by name of algorithm, followed Ex. 1.1.1 of en algorithm? Explain with suitable example.
“> (4) Finiteness by a list of arguments to be passed to the algorithm. Writa an algorithm for finding the factorial of number n. = (4 Marks)
Space complexity is very Important notion of efficiency
Every algorithm must terminate after a finite number of Algorithm FIND_SUM(A, B)
Soin, : analysis.
steps. If algorithm contains a loop, the upper bound of the 2. Comments stant with // sign. In very next line, we
loop must be finite. Recursive calls should have a well- should specify the description and input-output of the Algorithm FACTORIAL(n) Definition
algorithm. // Description = Find factorial of given number
defined base case to terminate the algorithm. 4 Problem Description : Add two integer numbers. // Input : Number n whose factorial is to be computed Problem-solving using computer requires memory to
> © Effectiveness 4 Input : Two numbers A and B on which sum is to be // Output: Factorial ofn = n X (n—1) *...K 2K 1
hold temporary data or final result while the program is in
execution. The amount of memory required by the algorithm
performed.
The algorithm should be written with a basic set of to solve given problem is called space complexity of the
HM Output : Sum of given two numbers,
instructions. The operations should be basic enough ta 3. Next, comes body part, which contains various logical
if (n == 1) then algorithm.
perform exactly using basic set, just like one can perform retum ]
stalements in proper sequence. These siatements may Controlling components
them with pen and paper. Complex operations should be else
contain control statements, loops, expressions ctc. of Space Camplexity
Performed using a combination of basic instruction. For 4. Compound statements are enclosed within the opening retum n * FACTORIAL(n - 1)
example, multiplication should be performed using loop and and closing curly bracei.e. [ . J. end
1. Fixed size components
addition, sorting should be carried out using looping, 5. Use left arrow for assignment: CA +B, Ex. 1.1.2
comparison, swapping etc, 6. Array index usually starts with index 0 or 1. Write an algorithm to perform matrix multiplication, 2. Variable size components
7. Relational operations are performed using relational Soln.
1.1.3 How to write an Algorithm ? operators like <, >, ==, ¢, 2 and $ Fig, C1.2 : Controlling camponents of Space complexity
Algorithm MATRIX_MULTIPLICATION(A, B)
8. Logical are using logical \// Description : Perform matrix multiplication of twa matrices,
of.algorithm
(Q. Discuss the rules to write an algorithm. (5 Marks) operators like and (a), or (v) and < not (5).
'Q. What are the general rules followed while writing the} 9. Input and output are performed using read and write J Input : Two matricesA and B of size n Xn. The space complexity of the algorithm is controlled by
statement // Output : Resultant matrix containing multiplication of two components:
algorithm? + (Marks)
rend (A)/ read “A Aand > (1) Fixed size components.
- — The algorithm basically consists of two parts : Head write (A) / write “A” or print (A)/ print “A” Tt includes the programming part whose memory
and body. 10._Contro! statements are written as follows : for i <1 tondo fequirement does not alter on program execution. For
Head part consists of algorithm name, description of ion) then Statement end forj —1tondo example,
the problem being solved, input to the problem and h) then Statement else Statement end Chil] —0 © Instructions
expected output. It may also include the explanation of
input argument and output variable. The description latements are enclosed within [ fork @ 1 tondo ° Variables
11. Whilg loopis written as follows:
i ir idea (o the user about the functionality of ClAL) — CHG) + AGNI] * BEAL) o Constants
the algorithm. while (condition) do end © Armays
— Body part includes a logical sequence of statements end
involving, various — constructs like conditional Do some work jend
> (2) Variable
size components
statements, expressions, loops, breaks, function calls
— However, there is no strict rule to follow these It includes the part of the program which whose size
ete, ; Sometimes curly braces are omitted depends on the problem being solved. For example,
with end keyword.
and block is closed standards. Algorithm syntax may vary from person to
person, © Size of loop

Scanned by CamScanner
Algorithm
Intreduction to Analysis of Introduction to Analysis of ithm
tad Analysis of Algorithms (MU - Sem 4 - Comp) 4-5
BePanatyssof Algorittms (MU - Som 4 = Comp) clemen is requir es only one
The addition of all array
d as fori—liondo // one initialization, n- increment, ‘n Syllabus Teple : Growth of Function
© Stack required to handle recursive call
extra variable (i.e.one mi emory location) denote comparison
21 size,‘ Thus the space Big Oh, Omega, Theta Notation
© Dynamic data structures like linked list sum, this is independent of f array is constant and Cli] — Ati] + Bi] # addition and n assignment
of the algorithm
~ We use the notation S(n) to specify the space complexity end 1.2.3. Growth of Function
complexity of the problem for input size n. The term 1 S(n) = O(N). returnG
Q..; Define order of growth, List various efficiancy classes.
is treated as the size of input or the problem size.
1.2.2 Time Complexity As it can be observed from above code, addition array
— The notion of spuce complexity is explained with elements required iterating loop n times. Variable i is with example. Show the relationship between
> (May 13, Dec. 13) initialized once, the relation between control variable i and n efficiency classes. (6 Marks)
following examples: are checked n times, and i is incremented n times, Wilh the
Definition
Example 1 : Addition of two scalar variables loop, addition and assignment operations are performed n
time, ‘The efficiency of the algorithm is expressed in term
@. How do wa analyze and measure the time complex’
Algorithm ADD_SCALAR(A, B) «a Marks) of input size n, The relationship between input size and
of an algorithm? Thus the total time of algorithm is measured as,
# Description : Perform arithmetic addition of wo numbers performance of the algorithm is called order of growth.
@. Whal do you mean by tmo complexity of an T(n) = LCinitizli +n
(Anput : Two sealar variables A and B
algorithm? How do we measure tho time complexi ty + increment addition + assignment) — Order of growth indicates how quickly the time
4 Qutput : variable C, which holds the addition of A and B ithm? Explain with suitable exampla. = 14+4n required by algorithm grows with respect to input size.
otanenee ° (4 Marks) While doing efficiency analysis of the algorithm, we — For input size a, the algorithm may execule a number
Of steps in order of log n,n, nn’ or something else.
COAt+R = Goodness of ulgoritlin is ofien determined by the time are interested in the order of complexity in term of input size
retum complexity. Time complexity is the most fundamental n. What is the relationship between input size n and the — Efficiency classes are categorized into different classes
number of steps to be performed? So all multiplicative and as shown in Table 1.2.2.
The addition of two scalar numbers requires one extra divisive constants should be dropped. Thus, for given
memory location to hold the result. Thus the space Table 1.2.2 : Various efficlency classes
Definition algorithm T(n) = O(n)
complexity of this algorithm is constant, hence S(n) = O(1).
The valid algorithm takes a finite amount of time for Example 3 : Sum of elements of array Order of
Efficiency
Example2 : Addition of two arrays execution. The Lime required by the algorithm to solve given class growth Example
problem is called time complexity of the algorithm. Time Algorithm SUM_ARRAY_ELEMENTS(A) rate
Algorithm ADD_ARRAY(A, 3} complexity is very useful measure in algorithm analysis.
U Description : Performa element-wise arithmetic addition of 1 Description : Add ull clements of array A Delete the first node from
complexity is not measured in physical clock linked list
two arrays cks, rather it is a function of a number of primitive
/PInput : Array A of size n
Constant 1 Remove the largest. clement
[Input : Two number arrays A and B. operations. Primitive operation is the most frequent
Ourput : Variable Sum which holds the addition of array
from max heap
H Output : Array C holding the elementewise sum of array in algorithm
elements
Add two numbers
Nand B Sum <0
fori Lton do
— We use the notation T(n) to symbolize the time
fori ltondo Binary search
complexity of the problem for input size st Logarithmic | log n Insert / delete clement from
Ci] — Afi) + Bi) — The notion of time complexity is explained with Sum Sum + Ali} binary search tree
end following examples: end Linear search
TeturnC
Example 1: Additlon of two scalar variables — — The addition of all array clements requires n additions Insert node at the end of linked
= Adding corresponding elements of two arrays, each of (we shall omit the number of comparisons, assignment, Lincar n list
size n requires extra n memory locations to hold the Algorithm ADD_SCALAR(A, B) Find minimum / maximum
initialization cic. to avoid the multiplicative or additive
result, As input size 1 i required space to hold /f Description : Perform arithmetic addition of two numbers clement from array
constants). A number of additions depend on the size of
the result also grows in the linear order of input. Thus
the space complexity of above code segment would be Input : Two scalar variables A and B the array. It grows in the linear order of input size Merge sort
4 Quiput : variable C, which holds the addition of A and B Thus the time complexity of abave code is T(n) = O(n). Binary search
S{n) = O(n). nlogn nlogn
= Time and space complexity of discussed problem is Quicksort
Example 3 : Sum a1 elements of array compared in the Table 1.2.1 Heap Sort
CeHAt+B
return C Table 1.2.1 : Comparison of space and time complexity Selection Sort
f Description : Add all elements of array A The sum of twa scalar numbers requires one addition for varlous problems Bubble sor
UW Anput: Array A of sizen operation. Thus the time complexity of this algorithm is Quadratic n Input 2D array
1 Output : Variable Sum which holds the addition of array constant, so T(n} = O(1).
Problem | Space Complexity | Time Complexity Find maximum element form
elements S(n) Ta) 2D matrix
Example 2 : Perform addition of two arrays
Add scalar OU) Ol) Cubic a Matrix Multiplication
‘Algorithm ADD_ARRAY(A, B) Add two O(n) O(n)
Sum — 0 Finding power set of any set
}# Description ; Performa element-wise arithmetic addition of arrays
fori¢-]tondo two armys Find optimal solution for
Sum «Sum + Afi] #Anput : Two number arraysA and B of length n
Add array Ot!) O(n) Exponential 2 Koapsack problem
end elements Solve TSP using dynamic
# Output : Armay C holding the element-wise sum of array
A programming
return Sum and B

Scanned by CamScanner
nalysisofof Algorithm
Introduct tion to Analysis
16
Analysis of Algorithms (MU - Som 4 - Comp) . . _—— @FPanaiysis of Algorithms (MU - Sem 4 - Comp) 1-7 Introduction to Analysis of Algorithm
i 1.2.4 Asymptotic Notation
EMclency. Order of: 3 Dec. 18) ‘Table 1.2.4 : Steps comparison of function f(n) and g(n) = In Fig, 1.2.2, Horizontal axis represents problem size
Bre te * Examp! > (May 13, Dec. 13, May 14, Dec. 15, May and the vertical axis represents growth order (steps
class
ral
for different input size required to solve the prablem of size n) of functions.
2. Explain Asymptotic notations,
Generating permutations of
PUREE ee eee n [1 [2 [3 [4 [5 [6 |7_]
given set
Factorial nt
Solve TSP problem using brute
Tene ee nr O(n) | 7 | 13 | 23 | 37] 55 | 77 | 103
Explain O, @, and 0 notations with the help
of graph, g(n) | 10 | 20 | 30 | 40 | 50 | 60 | 70 cn)
foree approach Q.
Efficiency classes are sorted as 3 — For small input size, algorithm A may outperform B,
butas input size becomes sufficiently large (in this case fo}
Q(1) < Otlog'n) < O(log (log n)) < O(log n) < OCCloy Q. Dafino 0, 2, and 6 notations.
n= 5), f(n) always runs slower (perform more steps)
8)') < On) < O(n log! n) < Ofn log n) < O(n") < O(n’) ELLE)
than g(n). So it's very important to understand growth
< O12") < O(n!) rate of functions.
Definition
These are the widely used classes, there exist many - Asymptotic notation describes limiting,behaviour of the
other cl ses which represent intermediate growth Asymptotlc notatlons are a mathematical tool to find ume function. For example, if function {(n) = Bn’ + 4n - 32
order, Algorithms with running time towards the ‘or space complexity of an algorithm without implementing
then as n —¥ 2, the term 4n — 32, becomes insignificant. Fig. 1.2.2 : Upper bound
it in a programming language. This measure is independent
beginning of the list are considered better. Algorithms
of machine-specific constants.
So the growth of f(n) is limited by the n* term,
having exponential or factorial running time are
Ibis a way of describing a major component of the cost of
Following assumptions are made while doing Definition ® on
Unacceptable for practical use. complexity analysis.
the entire algorithm.
Effect of input size on growth mate is shown in For small input size, there may be many crossovers
Table 1.2.3. — Machine specifie constants involve — hardware = Assumptions between the growth rate of f(n) and‘c,g(n),: but once n
‘Table 1.2.3 : Growth of function architecture of the machine, RAM, supported virtual - The actual cost of operation is not considered. becomes significantly large, f(n) grows always slower
memory, the speed of the processor, available — Abstract cost ¢ is ignored : O(c. n’) reduces to O(n?) than c.g(n). This value of n is called crossover point and
Input} Constant | Log | Linear | Quadratic | Cubic | Exponent instruction set (RISC or CISC) etc. ~ Only teading term of polynomial is considered : is denoted as ny. rt
Hoa) | co) ||) |B _ | ~ the asymptotic notation docs the analysis of algorithm O(n’ + n) reduces to O(n’)
1 1 1 2 independent of all such parameters. Drop multiplicative or divisive constant if any : O(2n’) "=F Loose bounds
2 4 8 — Finding minimum clement from an array of size takes and O(n’/2) both reduces to O(n"). All the set of functions with growth rate higher than its
4 sl es a maximum comparisons, - Various notations like Big Oh (O), Big Omega (2), Big actual bound are called loose upper bound of that function,
a a a a ~The asymptotic complexity of this algorithm is linear Theta (©), Little Oh (0), Little Omega (CD) are used to 23 = O(n) = O(n") = O(n’) = O(n!)
deseribe the asymptotic running time of the algorithm.
é 256 7086 a (in order of n). This linear behavior is the main term in 6n+3 = O(n’)=O(n')=O(n!)
the ity formula. It says if we double the size of ‘Asymplotic notation 3n°+2n+4= O(n’) =O(n})
2 to2d__| 32768 | 4294967203, the array, then the numbers of comparisons are roughly 2n?t4n+5= O(2") =O(n!)
p between various efficiency classes is doubled.
shown in Fig. 1.2.1. The growth of log function is very © Incorrect bounds
slow compared to linear of a quadratic function. Definition
All the set of functions with a growth rate dower than
Similar observations ean be made for different ‘The primitive operation is the most frequent operation its actual bound are called incorrect bound of that function.
efficiency classes. appearing in the algorithm and it depends on the problem. {ici
Thera)
21g ]
2°, nl or n” functions grow very quickly. Even for a ‘The primitive operation is the major component of cost.
6n+3 # O(!)
Fig, C1.3 : Different asymptotic notations 3n’+2n+4¥% O(n) #O(1)
small problem size (ie. for small n), such problems — As discussed earlier, for sorting and searching
require lots of resources. problems, the primitive operation is a comparison. For > 1.2.4(A) Big Oh dn'+4n+5# O(a") O(n) O(1)
— Graphical comparison between different efficiency adding arrays or matrices, the primitive operation is an For fucntion f(a) =20" +n +3
classes is depicted in the Fig. 1.2.1. Q. Define and explain big Oh notations. Give example f(n) = O(n?) = O(n?) = O(2°) = O(n!) = O(n")
addition. For the factorial problem, the primitive
) (2 Mark: f(n) # O(nlog n) # O(n) # O(log n) # O(1)
Operation is multiplication.
Order of growth of functions is very crucial This notation is denoted by ‘O’, and it is pronounced as
in “Big Oh”. Big Oh notation defines upper bound for > 1.2.4(B) Big Omega
OXsh) Performance evaluation of algorithm. Suppose running the algorithm, it means the running time of algorithm
time of two algorithms A and B are canrot be more than its esymptotic upper bound for any | | @. Define and explain 2 notations. Give examples.
f(n) and g(n),
where, random of data. (2 )
fa) = alas Definition This notation is denoted by ‘2’, and it is pronounced as
a(n) = 10n “Big Omega’. Big Omega notation defines lower
— Here, 1 is the size of problem and Let f(n) and g(n) are two nonnegative functions
polynomials f(n) and indicating running time of two algorithms. We say, 'g(n) is bound for the algorithm. It means the running time of
a0) are a number of primiti
ve ‘operations performed by upper bound of f(n) if there exist some positive constants c
Igorithm A and B respectively. For dif and ny such that'O< f(n) Sc - g(n) for all n> ng: It is
algorithm cannot be less than its asymptotic lower
Input (number) n,wehaveTable124. rere valv of denoted as f(n) = O(g(n)). bound for any random sequence of data.
Fig, 1.2.1 : Relation between common efficiency classes
MR

Scanned by CamScanner
OO ————

Introduction
to Analysis of Algorithm,
Tanaiysis ot (MU - Sem 4 Comp) 18 —=
of ims (MU - Sam 4 - Comp) 1 Introduction to. of

Mn)
0S 3n+2S5n Syllabus Topic : Mathematical Background for
So,c=Sand g(n)=n, ng=1 Algorithm Analysis

dima cain) (i) T(n) = 10n?


+ 2n +1
1.3 Mathematical Background for
Mecton Growth of Ta find upper bound of f(n), we have to find ¢ and ny such
t function Algorithm Analysis
that 0S f(n)Sc-g(n) foralln2ng
0 5+ : Os £(a)<e-g(a) Before we start analyzing algorithm, we will first look
Fig. 1.2.3: Lower bound 7
OS 10n'+2n4+1Sc-g(n) at some important mathematical formulas, which will help
.——> 0 $ 10n'+2n+1<10n'+2n'
+n", forall n21 us to simplify the computation further.
Definition
Fig. 1.2.4: Tight bound OS 10n'+2n41 S130" "7 Mathematics to simplity the summation
Let f(n) and g(n) are two nonnegative functions So, ¢ = 13, g(n)=n? and ny=1
indicating running Gime of two algorithms. We say
the function g(n) is lower bound of function (0) if Definition
6 Lower
ound
Boun:
Del l+lele..4t=0=0@)
there exist some positive constants c and My such that ‘oO a(n+1
Let {(n) apd g(n) are two nonnegative functions W) Tin)=3n42 Di = 142434...¢0= 00= 2
0 Sc -g(n) S fin) for all n 2 ny, It is denotedas indicating running time of two algorithms. We say the To find lower bound of f(a), we have to find c and ny = 240 og?
f(n) = Q (g(n)). function g(n) is tight bound of function f(n) if there exist such that 0 <c.g(n) < f(n) for all n> ny =D =O)
17 Loose bounds some positive constants ¢;, c, and ng such that 0 $c, aa)
k
s fie) S cy g(t) for all n 2 np It is denoted as f{n)= Ose. a(n) <fin)
O<Sc+g(n)S3n+2 Df
ied = vetestestephya sot)
All the set of functions with growth rate slower than its
actual bound are called loose lower bound of that function,
O<3n<3n+2— te, foralln2ng x Ke kee + tee 7 =O)
= For big oh, big omega and big theta, g(n) is not a single
function but it’s a set of functions which always grow f(a) = Q (g(n)) = Q (n) fore =3,ng=1 int
6n+3 = Q(1)
quickly, slowly or at the same rate of {(n) respectively (i) T(n) = 10n* + 2n +1 a! = n-k+l
3a +2n+d= O(n)=Q.(1) ‘To find lower bound of f(n), we have to find and ng
for ¥n2 Ng.
2n'+4n+5= Q(n)}=2(n)=2(1) such that 0S¢.g(n)
$ f(n) forall nn 1.3.1. Framework for Analysis of
§¥ Incorrect bounds
"= Incorrect bounds
O<c- a(n) <f(n) Non-Recursive Algorithms
All the set of functions with a growth rate lower or O<c+g(n)<10n' +2n+1 - -
All the set of functions with growth rate lower than its greater than its actual bound are called incorrect bound of ar
O<10n°s 10n” +20 +1, true, for all nz 1 Q. Discuss the general plan for analyzing efficiency of
. rave algortn . ie)
actual bound are called incorrect bound of that function. that function. ; 2 non-recu! Marks)
24 Qin4 WFQ @) 42 0) 23 # O(n) ¢OM)¢0M)¢0(0)) Ka) = 22 (g(r) = 2 fn) fore=3,ng=1 Q, Explain the framework of efficiency analysis of
6n+3 # Qin) 4(n)EQ(O)
6n+3 # (1)4 O(n) #0 (Nn) #0 (a!) {C) Tight bound non-recursive algorithms with suitable mere
3+ In 44% O(1)FO (n) 4 O(n) #0 (0!) @) T(n)=3n+2 >
Bn +2n+4¢ Q(n') #2 inl) 2n°+4n+S# (1) #6 (n) £6 (n°) #O (2°) 4 O(n!) Finding complexity of the non-recursive algorithm is
an +4n+5¢ 2(2)¢ 900) For fuention f(n) = 2n? +n +3 To find tight bound of f{n), we have to find ¢,, c, and ny simpler than that of recursive algorithms. A number of
primitive operations define the complexity of the
For fucntion f(n) = 2n? +n +3 fa) # 8()¥O@) such that,
. algorithm. By following below steps we can find the
f(n) = Q (n’)=2 (nlogn) = Q (n) # 8 (n°) #02")40(n) 20(n") OS oy B(n) < fin) <c, gm) for all n> ny complexity of non-recursive algorithms :
= Q(logn) = Q(1) Loose bound does not exist for tight bound. OScy gin) $3n +2 $c, a(n) © Determine size of problem / input
O<3n<3n+2<5n, foralln2t co Find out primitive/ elementary operation
fin) # Q(n') 422) FQ (N42 0) Ex 1.21
© Find count of primitive operations for best,
Above inequality is true and there exists such infinite
+ 1.2.4(C) Big Theta Represent the following function using Big oh, Omega and worst or average case.
Theta Notations.
inequaities. o Simplify the summation by dropping
So, f(n) = © (g(n)) = © (a) for =3,e2=S,my=1 multiplicative and divisive constants of
Q. Define and explain
6 notations. Give examples. (i) (n)=3n42 i) © Tn) = 10n? + 2n4 4
(2 Marks) Soin. : (i) Ten) = 10n?+ 20 +1 highest degree polynomialterm in sum.

- — This notation is denoted by ‘@", and it is pronounced as (A) Big oh (upper bound) To find tight bound of f(n), we have to find c,, ¢, and ng Ex. 1.3.1
“Big Theta”. Big Theta notation defines tight bound (i) Ta) =3n4+2 such thal, Determine the complexity to find the sum of elements of the
for the algorithm. It means the running time of To find upper bound of f(n), we have O<c,-g(n) <f(n) Scy gin) for alln> ny array. .
algorithm cannot be Jess than or greater than it’s to find ¢ and Dy
such that 0S f (n)<c- 8 (0) foralln2>n, O<c,-g(n)s 10n?+2n+1 ‘Scy-g(n)
Soln.:
asymptotic tight bound for any random sequence of
0S f(n)<c-gin) O<10n? <10n'
+ 2n+1S 130%, foralln21 ‘Algorithm SUM_ARRAY(A, nj
data. Above inequality is true and there exists such infinite
0 € 3n+2<c-g(n) }/ Description: Find the sum ofelements of uray:
O S$ 3n+253n+2n, for all
n>] (There can be such inequaities, So, 1 Taput : Areay'A
of length n
infinite possibilities)
f(n)= © (g(n))= 8 (n°) for cy = 10, c= 13,n)=1 1 Ourput: ae eo which holds the samunaiion of trey

Scanned by CamScanner
Introduction 16 Analysis of Algorithm
PP anaysis of algorithms (MU - Sem 4- Comp) way of searching wegq
Linear search is a very simple g Analysis of Algorithms (MU = Sem 4 « Comp) Introduction to Analysis of Algorithm
element that
clement from the list. Let Key is is thecompared may be somewhere near the middle. So on an average, the Soln.:
Hterate over length of arrayA want 10 search. The key element with one algorithm does (n / 2) comparisons.
ty one all index locations of A. Algorithm halts in two ‘To delete the element from the linked list, we should
or entire array is scanned, Thus, Te = o(3) = O10) traverse the list to search the element to be deleted. The list
‘Add each element in partial sum cases : K e y element is found will be traversed until a node with the specified element is
= Alporithm for linear search is shown below : ‘The time complexity of all three eases is depicted in the found of end of the list is reached.
Sum < Sum + Ali] following table.
Algorithm LINAR_SEARCH(A, Key)
“Sum of Array Elements: ", Sum |), Deseription : Perform linear search on array A to search
[ Best cease | Average case | Worst case |
Step 1: Size of problem is n because length of array is 0 element Key Lom [om [om | /f Description: Delete node from the linked list having value!
Key
Step 2: Primitive operation is addition: if Input : Array of length n and Key to be searched Ex. 1.3. i/ Input: Linked list starting ot HEAD and Key
Sum = Sum + Ali) Write an algorithm to find Max Element from an unsorted // Ourput: Linked list after deletion of node with value Key
Step 3: For loop iterates, m time and hence addition is } Ourput : Success / failure message array of size n. Calculate complexity of this algorithm.
performed 1 times, so T(n) = O(n) flag 0 Soin. : Temp = HEAD = / TEMP points to the first node in list
for i <1 ton do To find the maximum element, first Max is set to A[1].
Tia) = Dy b= 41+1+...ntimes
= O(n) ‘Then Maa is compared with each element of array. If Ali) > MW Kerate until reach to secand last node or the male before tel
if Key == Afi] then Max, algorithm updates value of Max and sets it to A(i]. The node to be deleted
print “Element Found on Lacation” process is repeated over the length of array. The pseudo while Temp —> Next Neat # NULL && Temp — Nest |
Ex, 1.3.2
flag 1 Sat flag Key is found code of the process is given below : Data# Key do
Find complexity of bubble sort break [Algorithm FIND_MAX(A) Temp = Temp — Next Go up to second tast node
Soin. end i Description: Find the maximum element from given arrayi/ end
[Algorithm BUBBLE_SORT(A, n)
Koy is found, 50 stop further search Input : Array A of length n
11 Description : Sort the given numerical data It node to be deleted
is not the last node
Input : Array A of randomly placed n element Flag wont sct il Key not found Max <— A[1] if Temp — Next > Data == Key && Temp —> Next > Next
Hf Output : Sorted sequence of input data if lag == O then
fori 2tondo #NULL then
for i—1tondo print “Element Not Found” if Max < Afi] then Hold = Temp — Next Stop on nede before
forj—1ton-i-1do end Max © Afi] Temp — Nest = Hold —> Next the node to be deleted
if Afi] > Alj + 1) then end Free(Hold)
Swap(A[iJ,
a Ali) Move largest element at end
the end of unsorted list print “Maxi m Element of Array A is”, Max
end Complexity analysis Key && Temp > Next >
Best case
Step 1: Size of problemisn Complexity analysis Hold = Temp — Next
Step 2: Primitive operation is comparison The algorithm needs a minimum number of In every iteration, algorithm does one comparison and Temp —> Next = NULL
Step3: For each instance of outer loop, inner loop iterate comparisons if the key element is on the first position. In the problem size is reduced by 1. Recurrence for this problemis Free(Hold)
(ni) times. best case, the size of input array docs not matter, In the best formulated as,
For i=l, inner loop does n — 1 comparisons, for i=2, case, the algorithm does only one comparison irrespective of Tin) = T(n-1)4+3 1 Inode with given key does not exist
inner loop does n - 2 comparisons and so on, array length. Hence the running time of the linear search in Let us solve this using iteration method, else
Tin) = (n-1)+(n-2)+...+ 34241 the best case is, T(n) = O(1). = T(n-2)+1 print “Node not found”
a-t = [TMn-2)+1]+1=T(n-2)+2 end
= DD isl4+2+34...+(n-1) Worst case
= T(n-3)+1 The problem is similar to linear search. On cach
The algorithm docs a maximum number of = [T(n-3)+1]+2=T(n-3)+3 iteration, problem size is reduced by 1, and one comparison
comparisons if the key element is on the Inst position of the After iterations, is done. Thus, the recurrence for the above algorithm can be
array or it is not present at all. The entire array needs to be Tin) = T(a-k)+k formulated as,
Tn) = Tin-1j+t
Ex, 1.3.3 scanned, Numbers of comparisons linearly grow with the For k =n Let us solve this using iteration method,
size of the input. Hence the running time of linear search in T(a) = T(n—n)+n=T(O)+n
Wiite an algorithm for searching an element in array of size Cost for solving problem of size 0 is definitely 0,1, $0 Tin-1) = Tn-2)41
worst case is, T(n)= O(n) => T(n) = [Tin-2) 4141 =T(a-2) +2
1n. Calculate complexity of this algorithm. T()=0
Soin.
Average case Hence, T(n) = O(n) Tin-2) = T(n-3)+1
The average case occurs when an element is neither on [Tn
3) 41]+2=T(n-3)
+3
- We will discuss and derive the complexity of linear the first location nor at last. The key Ex. 1.3.5
search technique to search an element from an array of clement may be near to
the beginning of array or it may be towards Write an algorithm to delete an element from a linked list.
terations,
Tn) = Tin-k)+k
size n. the end, or it Also, mention the worst case running time for this operation.

Scanned by CamScanner
Introduction to Analysis ofAlgorithm
——=
hs 7 1-13 Introduction to Analysis
of Algorithm
Sen Con
k for Analysis of Recursive Pfanatysis of Algorithms (MU - Sem 4 - Comp)
ot Arte Sort Core —* Fr
Algorit hms Step 1: Move disk C from the sre peg to dst peg Recursive approach is the best suitable for solving this
ig
= Tia-n)+n=T(0)+0
Tin)et problem. The recursive formulation for tower of Hanoi is
Cost for solving problem of size 0 is definitely 0, so
the fram ework of efficiency analysis of

oe, | 4
Q. Explain given as,
T(Q)=0 with suitable examples. (5 Marks)
recursive algorithms HANOK(source, aux, dest, n) =
Hence, T(n) = O(n) we can find the complexity of ‘move from src to dst ifn=1
Worst case for the problem occurs when the node to be - By following steps,
deleted is at the end of the list or it is not present at all. For recursive algorithms:
HANOI (sre, dst, aux, n= 1)
the list of n nodes, the worst case running time would be — Determine size of problem HANOI (sre, aux, dst, L)
T(n) = O(a).
— Identify primitive operation Step2 : Move disk B from the src peg to aux peg HANOI(aux, src, dst,n- 1) otherwise
Ex. 1.3) — Find count of primitive operation in each call ‘Algorithm HANOI (are, aux, deat, n)
on using appropriate
Consider the following algorithm. — — Set up and solve recurrence equati Ii Description: Move n disks from source peg to destination
ALGORITHM sum (n) method. Peg
i! Input : A non-negative integer n {H Input: 3 pages, and n disks on source peg
Seo Ex. 1.3.7 st ts] _ __ #f Output: n disks on destination peg
Asnillary pog Destination peg
fori=1tondo Write a recursive algorithm for Towar of Hanoi problem, set
Step 3 : Move disk C from the dst peg to aux peg
SeS+i up its recurrence and solve it. ifn = = 1 then
tetum S. Soin. :
Move disk from sre lo dest
) What coes this algorithm compute 7 else
— The tower of Hanoi is very well known the recursive HANOl(sre, dest, aux, n- 1)
ii) Whatis its basic operation ? problem. The problem is based on 3 pegs (source, HANOI(sre, aux, dst, 1)
iil) How many times the basic operations executed 7 ‘Source pog Aunalla-y pog Destination pog
iv) Whatis the efficiency class of this algorithm ? auxiliary and destination) and n disks. Tower of Hanoi HANO!(aux, sre, dest, a — 1)
¥) Suggest an improved algorithm and indicate its is the problem of shifting all a disks from source peg to cud
efficiency class. If you cannot do it, try to prove that it destination peg using auxiliary peg with following Step 1: Size of problem is
Step 4: Move disk A from the src peg to dst peg Step2: Primitive operation is to move disk from one peg
cannot be done.

ow oh
constraints :
to another peg
Soin.: — Only one disk can be moved at a time. Step3: Every call makes other two recursive calls with
@ This algorithm computes the summation of numbers — A Larger disk cannot be placed on smaller disk. problem size a — 1. And each call corresponds to
from| ton ‘The initial and final configuration of the disks is shown one primitive operation, so recurrence for this
Gi) The basic operation is an addition, i.e. computing sum in Fig. P. 1.3.7(a) and Fig. P. 1.3.7(b), respectively. ‘Source peg, Aunsillary pog problem can be set up as follow :
(ili) Basic operation executes n times Ta) = 211-1) +1 vee
(iv) As it performs n basic operations, efficiency class of Step 5 : Move disk C from the aux peg to sre peg Let us solve this recurrence using forward and
this algorithm is O(n) backward substitution:
(¥)_An improved version of the algorithm: Substitute n by n— 1 in Equation (1),
Algorithm IMPROVED_SUM(n) Aapdliary peg Dostnation peg Ta-l) = 20(a-2)+1,
1! Deseription: Add first n numbers
Input: Number n Fig. P. 1.3.7(a) : Initial state of ‘Tower
of Hanai ST
‘Sourve peg
a Ee By putting this value back in Equation (1),
‘Auniliary pog Dostination pag Tin) = 3ETin 2) + +t
# Output: Variable Sum holding the summation of first Step Pp 6 : Move disk B from the aux peg to dst
n numbers POE 10 at PER = 2T(n-2) +241 )
2T(n-2)+(2-1 (2)
Sum n*(n +1)/2 Similarly, replace n by n—2 in Equation (1),
return Sum te [8]
fe
feo] Ta-2) = 2T(n-3)+ 1,
Source pop Aacoliary pg Destinauion peg Tana From
The complexity of this algorithm Equation (2),
only one computation to find the sum is ofO(1),first it nperforus
Source pog ‘Auallary pog Dastination peg Ta)
Fig P. = 2[2T@-3)+1]+241
numbers. natural 1.3.7(b) : Final stateof
Tower of Hanoi = 2T(n-3)+24241
Forn=6, There can be n number of disks Step 7 : Move disk C from the sre peg to dst peg = 2T(n-3)+(2-1)
Using algorithm SUMO : 1+24+344454+6=21 tace the problem for n=3 on ource peg, Let's
dis! ks. The trace of the In general,
Using Algorithm IMPROVED_SUMQ : solution is ; Ta) = 2T(n-k)+
(2 1)
at(n+ 1)/2=(6*7)/2=21 Lc] By putting k= n—1,
[6] Ton) = 2 "Tray +a" 1)
Asse
‘Source pog Auniliary peg Destination peg T(1) indicates problem of size 1. To shift | disk from
source to destination peg takes only one move, so T(1) = 1.

Scanned by CamScanner
a42 Introduction to Analysis of Algorithm
ta Analysis of (Mu - Sem4 -Come) Introduction 10 Analysis af Algorithm
Ta) = 2407-1 Tat) = Tn-1-)+1=Tn-2)+1 [Bh analysis of Algorithms {MU - Sem 4 - Comp) 1:15

2-1 $0, T(a) = T(n-2)+1+1=Tin-2)+2 Ex. 1.44


Thus, T(n) = O(2") Sort the letters of word "DESIGN" in alphabetical order using selection sort.
Tia-3)+1
Ex.1 = Te- =T(n-3) +3 Soln.: The following figure shows the simulation (o sort characters of word “DESIGN”
number of | $0:T¢0) = Te 3424 1=TI0-3)
x. 1.3.8
Pass - 1 Pass - 2 Pass -3
Solup and solve a Recurrence relation sforforthecomputin After k steps.
calls made by F(n), the recursive algorithm g nt
Th) = Tin-k) +k [ofets]: [ex |nn-0
on Fork = 9,
Write an algorithm to find factorial using recursion. Find the Tia) = Tn-n)+n =T()+N = O(n)
time comploxity.
Soin,
Factorial of number n is computed as : n! =n * (n- 1) * Syllabus Topic : Analysis of Selection Sort,
Insertion Sort
(n-2)*...*2"1
Recursive algorithm for finding factorial of any number
is described below: 1.4 Analysis of Selection Sort and let (els[e|nn-> ifeTs[ [| s|mn-e
Algorithm FACTORIAL(n) Insertion Sort
lel TelsTs}nn-> Bail e| LeLe[s}onee
! Description: Find factorial of given number 1.4.1 Selection Sort leper [s|on-o filets [Te] Ny] nn-e BESET Tels nm-s
H Input: Integer value n No swapping is required No swapping is required ‘Swap (8,6)
Q. Explain selection sort and derive its complexity.
If Output: Factorial of numbern (10 Marks)
Like bubble sort, selection sort is also comparison Pass- § Output
ifn == orn == 1 then based in place algorithm. Selection sont is simple and it Pass - 4

else
return J J Base case has obvious advantage of minimum swaps among all
algorithms. For list of size, selection sort performs ira a
end
return n * (n = 1) 4 Recursive case: maximum
(n — 1) swaps. However, it’s running time is quadratic BEE CP Ge
For each call, problem size reduces by one and cach
call performs one multiplication. We can sep the =
and heace not accepted for a large list
In every iteration i, Selection sort finds the minimum SEECCD)~-
No swapping is required
GEBme)~--
recurrence as follow: clement from the unsorted list and swap it with an i*
element in the list. At the end of the i” pass, i elements . Tn) =T(n-2)+ (0-1) +n
get sorted. Sorting starts from the beginning. Putn =n—2in above equation,
New problem Cost of one r= Complexity analysis T(n—-2)=T(n—-3) 40-2
of size (n- 1) multiplication — Atthe end of the first pass, minimum element seats on = Similarly bubble sort, selection sort also dose the same Put value of T(n— 2) in previous equation of T(n).
the first location, in the second pass, second minimum number of comparisons, It iterates both loops +. T(n) =T(n-3) +(n-2) + (0-10
element seats on the second Jocation and so on. irrespective of input data pattern. Alter k iterations,
Tin) = Tin~1)+1 (Inductive case) () = Unlike bubble sont, selection sort cannot detect
sorted
T(n-k) =Tin-k-1) +(9-k)
sequence. So running time of selection sort in best,
Tin) = 1, ifn=Oorn=1 (Base case) ~ If the list is reverse sorted, bubble sort does n — 1 swaps
T(n) = Tink+ ) (n-k + 1)4(0-k+2) +.
Let use solves this recurrence using two methods: in the first pass, whereas selection sort does only one average and worst case is O(n’). We ean comejustto this
Tike
a(n-1tn
Forward substitution
swap. conclusion by adding number of comparisons to ‘When k approaches to n,
—__ Algorithm for selection son is shown below : bubble sort, but here we use recurrence equation Tn) =TO)+ 142434. 4(0- en
From Equation (3), ‘Algorithm SELECTION_SORT(A) derive the complexity.
to solve the T(0) = 0, because it is running time of problem of size
T(2) = Tht sat4i=2 A is an array of size n —— Let’s assume T(n) defines the running time zero, and no effort is nceded to solve this problem.
ch iteration,
problem of size n. In selection son, aftersize eareduces 1424+34+..40 | a
T(3) = T(2)4+1=2+1=3 for i¢- 1 ton-1do by Tin) =
‘one element gets sorted and problem -% wins DDe
After k steps minti
one.
forj i+ Lion do n times, 2
Th) = k = For each problem of size n, inner loopsortiterates
can be written (max (.2))))-o(8)
Fork = 2 if ( Afi] < A[min))
do So recurrence equation for selection
min= j Tin) =O.)
Tin) = n=O(n)
end = rtTn)=Tin- N+
Backward substitution end Putn=n- | inabove equation,
To find Tin), we need to
find Tin — 1). To solve ewap (Ali |, A[min}) T(n- 1) T(n-2)+ (0
|
Mi
previouss equation n of Te
of T(n).
in Equation (1). at value of
Put Tin~ 1)1) in previou
of T(a
Tin- 1), fet us putn =n-1

Scanned by CamScanner
[eT analysis of Algorithms (MU - Sem 4 - Comp)
Introduction to Analysis of
Tunction of n. Average case running time of insertion
and, 2, G-1)=14243+..4¢n=1 sort is O(n’).
g [ Worst ease |
iei-l = 5 qn-y= 28D Oe")

4
end Teo) = cyemte-(a—
1) 463° (- N+ Ga"
Ex 142
> (May 17)
jend
Ag+ okey
(%..i) Sort the letters of word “EXAMPLE” in alphabetical order
+e-E, GDB 6-9 using insertion sort.
@. Explain insertion sort and derive its complaxit = Complexity Anslys!s Soin.:
Ee Peis
time taken by algorithm, +ee(n-1)
—— Size of input arra} y jg n. Total of jty Initial array of alphabets
We will analyze insertion sort algorithm in depth and is the summation ‘of time taken by cach
= eye mt eye (n= 1) Hey2(N—
N+ Ga"
for rest of the algorithms we will
find out its running nin +1) n(n=1)
Cefx]atulel
s
instruction. ( 2 -1) #55" 2
time from its structure.
carga Deere-Dte(Z )
— It’s obvious observation that to sort hundred elements +e, MH +e,-(n- 1) = One by one element is scanned from array. In iteration
will take more time than just sorting five elements.
Running time is function of input size. == (Bose)
(448 Sg k, if incoming character is greater than previous
in hand. clement, then copy newly read character at location Kk.
= Insertion sort uses the analogy of sorting cardscards. Vee O- 0
It works in a way people are used to sort One Dee Bah st a
(era +549-$-$ee) — If k™ element is smaller than (k - 1) element, then
keep movie elements on right side until we get element
card is removed at a time from the deck and it is
_ gives the tower bounddataof
Best ease analysis : Best casemeans smaller than newly coming element. Insert new elememtof
inserted at correct location in hand. Upcoming cards for any other
running time of algorithm, time cannot be less than its
ne(e,
+ 5+ 04+)
in created free space. Step by step simulation
are processed in same way. sequence of data, running = an +bnt+e¢ insertion sort is shown here :
To insert new card, all the cards in hand having value for insertion. sort
best case munning time. Best case which is quadratic function of n
larger than new card ant shifted on right side by one. ceours when data is alread y somted. For this case,
New card is inserted on space created after moving
= Ot)
will never satisfies and hence t
condition in while loop — Average ense analysis : Average case is often roughly
some k cards on right side. as bad as worst case. On an average, half of the
— Insertion sort in inplace algorithm, it does not itself. require =1,s0 less than
elements are greater than A(j] and other beis quadratic
extra memory. Sorting is done in input array In qe@aey nto
(a- 1 +e (a He4 24,146, that. So, t, = Gf 2). It again turns out to
iteration k, first k elements are always sorted. By, 404662), OF 0 Read X. B and X are in order, 50 no shiftingor
— Running time is the number of steps required to solve Read E. Fis the only chameter, so no shifting or Step2: swapp ing
is required.
problem on RAM model. Each instruction may take
Step 1:
Where, swapping
is required
different amount of time. Let us consider the cost of i
instruction is ¢;. Ze Lat). 1 (a— 1 times) =n-1 felx[eluteTeTe|
By MHC, N= Cy + Cy MC; +E Cy
+e,-n-C
Read M.M and X are out of order, so move X on 4”in
S (Cy Hey Hey Hy) = (Cy Hy Oy HC) Step 3: Read A. 7X and X are out of order, so move X on 3" Step 4: position in array. Compare M with E. They are
= an+b Which is linear function ofn position in array. Compare A with E. Again, order, so insert M on 3" vacant position
in array.
are out ordcr, so move E on 2™ position
= O(n) Now there are no more elements left in array, so
Worst case analysis : The worst-case running time insert A on 1” vacant position
gives an upper bound of running time for any input. It
indicates that for any arbitrary sequence of input dala,
running time of algorithm cannot get worse than its
worst case running time. Worst case for insertion sort
occurs when data is sorted in reverse order, So we must all three
have to compare A(j] with each clement of sorted array so move X on 5” Step 6: Read L. Lis less than X, P and M, so move L onca
Fig. 1.4.1 : Process of insertion sort Step 5: Read P. P and X are out of onder, charnct fers on fight by position and insert
AlL..j-U. m e P with M. They sre in
position array. Compar
5 Algorithm So t=. onder, so insert P on 4 vacant position
‘Algorithm Insertion Sort (A) x ag J=243 444.040
WA is array of size n
= (142434..¢n)-1=2n-1
cy n
for j<—2tondo =o— Afnt]
°, a-1
key — ALi]
Cy n-1
iej-1
while (i> 0 && Ali] > key)

Scanned by CamScanner
Introduction to Analysis ofAlgotitnm
[ATanaysis of Algorithms (MU - Sem 4 - Comp) 118
Step 8: No more elements are lefisorted. [fanaiysis of algoritnms (MU - Sem 4 - Comp) 119 Introduction to Analysis of Aigorithm
Step7: Read E. Eis less than X, P, Mand L, so move all four
characters on right by position and insert E on 3
in the array. The array is
1.5 Exam Pack Explain 0, 2, and @ notations with the help of graph.
vacant position.
(University and Review Questions) (Ans. : Refer section 1.2.4) (3 Marks) (May 2014)
Q. Define 0, @, and @ notations.
"= Syllabus Topic : Space and Time Complexity (Ans. ; Refer section 1.2.4) (3 Marks) (Dee. 2015)
a Explain space complexity in detail. @. = Dafina and explain big Oh notations. Give
(Ans. : Rater section 1.2.1) (3 Marks) examples. (Ans. : Refer section 1.2.4(A)) (2 Marks)
Ex. 1.4.3 [URE PS e (May 2013, Dec. 2013)
> Define and explain O notations. Give examples,
To sort the given sat of number using insertion sort and also show the result of each pass. < 11, 7,17, 3,9, 29, 85,9 What are the basic components which contribute to (Ans. : Refer section 1.2.4(8)) (2 Marks)
Soln. : Initial array of numbers space complexity? Q. Define and explain @ notations. Give examples.
(Ans. : Rafer section 1.2.1)4 Marks) (Ans. : Refer section 1.2.4(C)) (2 Marks)
What do you mean by space complexity of an Ex. 1.2.1 (7 Marks) (May 2014)
— One by one element is scanned from array. In iteration k, if incoming character is greater than previous element, then algorithm? How do we measure the space
complexity of an algorithm? Explain with suitable * Syllabus Toplc : Mathematical Background for
copy newly read characterat location k. example. (Ans, : Refer section 1.2.1) (4 Marks) Algorithm Analysis
If" element is smaller than (k — 1)* element, then keep movie elements on right side until we ¢ getget element
eln smaller
a than
coming clement, Insert new element in created free space. Step by step simulation of insertion sort is shown Explain time complexity in detall. @. _Discuss the general plan for analyzing efficiency of
herenewly
re = (Ans, : Refer section 1.2.2) (3 Marks) non-recursive algorithm
(May 2013, Dec. 2013) (Ans. : Refer saction 1.3.1) (6 Marks)
Step 1: Read 11. 11 is the only number, so shifting
or swapping Step 2: Read7.7 and 11 are out of order, so move 11 on 2”
How do we analyze and measure the time @. Explain the framework of efficiency analysis of non-
is not required, position in array. Now there are no more etements
complexity of an algorithm? recursive algorithms with suitable examples.
left in array on left
of side, so insert 7 on 1" vacant
position
(Ans. : Refer section 1.2.2)(4 Marks) (Ans. : Refer section 1.3.1) (6 Marks)
What do you mean by time complexity of an Q. Explain the framework af efficiency analysis of
algorithm? How do we measure the time complexity recursive algorithms with suitable examples.
of an algorithm? Explain with suitable example. (Ans. : Refer section 1.3.2)(5 Marks)
(Ans, : Refer section 1.2.2)(4 Marks) "¥ Syllabus Topic : Analysis of Selection Sort,
Syllabus Topic : Growth of Function - Big Oh, Insertlon Sort
Step 3: Read 17. It is in correct position, so Read 3. It is not in correct position, so shift elements ‘Omega, Theta Notation @ Explain selection sort and deriva its complaxity.
swapping is not required. greater than 3 on the right side by one position until Define order of growth. List various efficiency (Ans, : Refer section 1.4.4) (10 Marks)
the correct location for 3 is found. classes with example. Show the relationship a. Explain insertion sort and derive its complexity.
between efficiency classes. (Ans. : Refer section 1.4.2) (10 Marks) (May 2017)
(Ans. : Refer section 1.2.36 Marks) Ex. 1.4.3 (10 Marks) (Dec. 2014)
Explain Asymptotic notations.
(Ans. : Refer section 1.2.4) (5/4 Marks)
(May 2013, Dec, 2013, May 2016)
QoQ
Step 5: Read 9. It is not in correct position, so shift elements | Step 6: Read 29. It is in correct position, so shifting or
greater than 9 on the right side by one position until swapping is not required.
the correct location for 9 is found.

[s[>[s[ujo[a
3

Step 7 : Read 85. It is in correct pasition, so shifting or swapping | Step 8: Read 9, It is not in correct position, so shift elements
sreater than 9 on the right side by one position until
the correct location for 9 is found.

Scanned by CamScanner
Module 1 Panaiysis of Algorithms (MU - Sem 4 - Comp) 2.2 Divide and Conquer
or eee a - Applications 2.1.3 Efficiency Analysis

Divide and Conquer


Q. Enlist faw problems that can be solved using divide
and conquer approach. (3 Marks)
Q. “Derive a general equation to find the complexity of
divide and conquer approach, ” (5 Marke)
Many computer science problems are effectively solved — Ingeneral form, the time complexity of problem solved
using divide and conquer. Few of them are listed here: using divide and conquer approach is given by
1. Finding exponential of the number following recurrence equation:
2 Multiplying large numbers _f 3) » if'nistoo small
Ta)-{
<3e~ Multiplying matrices (Strassen's algorithm) Tn) +TO)... + Tin) + f(n), otherwise

Sorting elements (Quicksort and merge sort) T(n) is the total time required to solve the problem of
Searching element from the list (Binary search) size n. The g(n) is the cost of solving the very small
General method, Analysis of Marge sort, Analysis of Quick sort, Analysis of Binary search, Finding the Discrete Fourier Transform problem. It denotes the complexity of solving the base
minimum and maximum algorithm and analysis, Strassen’s matrix multiplication. Closest pair problem case. T(n) is the cost of solving subproblem of si
The function f(n) represents the time required to divide
<8” Max-min problem the problem and combine the solution of subproblems.
2.1.2 Control Abstraction Generalized recurrence for this strategy is written as
In this chapter, we will discuss an interesting problem- > (1) Divide : Recursively divide the problem into > (May 13, Dec. 13) T(o) = aT(n/b) + f(a), where a is the number of sub
solving technique called divide and conquer. The smaller subproblems.
problems, m/b is the size of each sub problem and {(n}
is the cost of division or combination. Thus,
technique divides the larger problem into smaller sub-
> are solved independently.
(2) Solve : Subproblems Tin) = a T(nb) +f)
problems, and solution of the original large problem is
> (3) Combine : Combine solutions of subproblems in- — As we discussed, divide and conquer approach works Assume that we have at least one problem, so a 2 1,
obtained by combining a solution of smaller
subproblems. Such algorithms are a prime candidate for
order to derive the solution of the original big problem. in three stages: and size of each problem is 1/2, sob =2,
Subproblems are similar to the original problem
1. Recursively divide the problem into smaller Assume n= b*, wherek = 1,2, 3,4, ...
parallel implementation. We will discuss and compare subproblems
the performance of various problems solved using smaller arguments, hence such problems can be easily TOY) = aT(b'm)+ 10)
2. Subproblems are solved independently = ato +104 (21.1)
waditional method and using divide and conquer solved using recursion.
3. Combine solutions of subproblems in-order to
approach. Divide and conquer method easily — When subproblem hits to smallest possible size, it is - Tocompute T(b‘~'), replace k by k - | in Equation
derive the solution of the original big problem. (2.1.1).
outperforms the traditional algorithms, solved and the results are recursively combined to - Control abstraction for divide and conquer (DC) TE") = att?)
+ f0'"')
a solution of the original bigger problem. approach is given below : >TO) = alaTO?) +f + AO)
Syllabus Topic : General Method - Divide and conquer is multi-branched, top-down ‘Algorithm DC(P) = al-tt-2)+ aftt-)]+ fb") w-(2.1.2)
recursive approach. Each branch indicates one _ 1 Pis the problem ta be solved By substituting k = k — 2 in Equation (2.1.1).
2.1 General Method subproblem and it calls itself with the smaller TF?) = Toot) +40?)
argument. if P is small enough then 2) in Equation (2.1.2)
> (May 13, Dec. 13,May 14) | — and of divide and conquer return Solution of P FATE) + £0) + aff) + £0)
Q. Explain Divide and Conquer strategy. List any Four algorithms needs skill and good reasoning. else = PTE} + H(t?) + af“) +0)
examples that can be solved by divide and conquer. Fig. 2.1.1 shows the graphical representation of divide divide larger problem P into k smaller subproblems P,, After k iterations,
MU - May 2013, Dec. 2013, 6 Marks and conquer strategy. Subproblems may not be of size Pay ove Py TOY) =a. TEAE"S) + aE) + ab 7A00") +... + aH)
Comment on the module of computation : Divide and solve euch small subproblem P, using DC strategy: - = ab-T(1) +a") +e HO) $e. +a)
Corgi AoA
exactly n/2, - x
return combined solution (DC(P,), DC(P,), -.., DC(P,))
end
= arty
s® +8 10) ++ FA)
2.1.1 Introduction — Each subproblem in divide and conquer arc k
solved
independent and hence sub-problems may be = iro z ar
Q. Explain the concept of divide and conquer. (5 Marks) multiple times. isl
If we create a problems, each of size wb, and if
Divide and conquer strategy operates in three stages : n= bt, sok =logpn
division/combinalion cost is f(n), the time complexity logya
Three stages of divide of such problem is given by the recurrence,
nm = ae] T+ D the
and conquer approach to solve Time to solve
problem of size n Problem of size (vb) i=l
By property of logarithm,
KP

(1) Divide WD = yh
3

T(n)n = a.Tinb) + f{n) logyn


(2) Solve A213)
Tima required to divide qo) = oF |T+ D Ae ye!
the problem and combine t
the solution of sub problems
(3) Combine
Number of sub
Fig. 2.1.1: Working Principle of divide problems
Fig. C2.1 : Divide and conquer algorithm stages and conquer

Scanned by CamScanner
Olvide

Algorithms (MU- Som 4 = Com


2
8 ol = Syllab
‘pic : Analysis of Merge Sor
aR (Panatysie of Algoril 24 Divide
and Conquer
on a (MU - Sem 4 - Comp)
~ ‘Thus, the complexity af problem depends
{the
22_Merge
Sort _____——_|
of problems a, size of the problem (wb) and 7 Algorithm for merge sort End :
divisioncombination cost f(n),
Q, Givon a sequenco of n-elements Ali] .....- A{n),
2.1.4 Sorting > (May 15)
asaumo that thoy ara split Into 2 sets A(t] ...... A(n/2) Insert sentinel symbol at the end of each array
Anal
wt. and Aln/2 + 1]... Aln), each sot Is Individually sorted LEFT[], + 1] ee
@. Which are different factors considered tor sort und cong RIGHT fl, + 1Jeo
alononta, Meg fe sort sorts the list USiNg divide ples ; and tho rosulling sequence !s merged to produce a
following two basic princi
Sorting isa process of arrunging elements in certain strategy. He works on larger lig
aingla sorlad sequonco of n olomonts.
than sortin g the
odor, Numeric data may be sorted In ascending or = Sorting smallerI ist is faster Using tho divido and conquor strategy, write a Morgo // Sart two-way merge process
Is faster than tht ofy
descending onder, Alphabets of strings may bo sorted in Combining two sorted sublists
Sort algorithm to ort the saquonco In non-decreasing
lecicogeuphical onder. Sorting is intensively studied and unsorted! lists, (6 Marks) fork < low ta high do
explored sive of computer science, In muy ¢ ordor,
Merge sort works in three stages
application witing ix the inheral need, single list into (w Q. Writo on algorithm far sorting ‘n' numbers using 1 Perform 2-way merge
on LEFT & RIGHT!
Divide : Recursively divide the
Lange chisses of algorithms ane avallabls for sorting. sublists until cuch sublist contains I
element, morgo sor. (6 Marks) if LEFT (i) S RICHT (j] thea
AML have certain limitations and fix application domain, List of]
Conquer ; Sort both sublists of parent list. Agarithin far merge sort is described below 4 Smaller element is in LEFT sub list, so copy it in B
No algorithm seeves all the purposes, However,
element is sorted, Blk] — LEFT [iJ
nunning time iy tho most commonly used measure (0 sorted sublists Algorithm MERG! SORT(A, low, high)
Combine z Recursively combine
select the best algorithm, the list af size n ix produced, # Don iin of array A using Merge sort ieitl
Waunbers of elements are small enough to sort in main
Merge sort divides the list of size 1 into two sublig {Input : Array of nize n, low <1 and high A else
emory, sorting is called Infernal sarting. UF numbers CONLNUCS UN 7 Smaller element is in RIGHT sub list, so copyit in
cuch of size w/2, ‘This subdivision {/ Output : Sorted array B
of clements aie lange such that some of then reside on
external starage dariug sorting procedure, it is called Rocurslvely sor first B
ub list Bik] — RIGHTE]
external sorting, if low < high then
Sorting algaritiim should possess following properties : mid & floor(low + high) /2 jejtl
—+ # Elomontaaf
Proportion of MERGE_SORT(A, low, mid) end
sorting algorithm ‘end
—+# Elomonta|
(yin plac MERGE_SORT (A, mid + eet Ex. 2.2.4
Logg —+ # Elomania
rn a
COMBINE(A, Jow, mid, high) "\ sacond ub tat Simulate morge sort on data sequence

kacacich ceca
Gi) Stable } J/ Merge two sorted sublists <77, 22, 33, 44, 11, 55, 66>

(lil) Orlin ]—+ # Elamonts end Soin. >

Procedure COMBINE performs a two-way merge to Let us understand the working of merge sort through
(Iv) Adaptive Fig. 2.2.1 : Merge sort - division stage
praduce a sorted 1 ay of ye n from two sorted arrays of graphical representation and call sequences. Consider the
(¥) Incremontal ] Definition size n /2. The subroutine works as fallow: sequence of the element as shown below:

‘Two-wny merge is the process of merging two so COMBIN! low. mid, high)
Fig. €2.2:: Properties of sorting algorithm 1, © mid—low + 1 Size of 1" urray
arrays of size m and n into n single sorted array of 1 2 3 4 5 6
°
aw Inplace (m+n). Lehigh-mid ff Sie
of 2" array
Only. require constant additional space to sort the ~ Merge sort compares (wo arrays, cach of size one fori @ 1 to |,do t t
low high
algorithm performing the 2-way merge. The sorted sequence
LEFT fi] «A (low + inl] /f Copy 1 sub list in array
kept in u new array of size lwo. Simulation of merge sort for given array is depicted in
> iii) Stable LEFT
— In next step, two sortcd arrays, cach of size two the Fig. P. 2.2.1. The number in a circle on left or right
Does not aller the relative position of same elements merged into a single array of size four, This p end comer indicates the call sequence, COMBINE
is called as
alter sorting continues till solution of the entire problem
constructed,
forj—1tol,do soon as two sublists of size | are created.
> id) Online RIGHT [i] —A [mid + j] W-Copy 2“ sub list in array]
~ Sentinel symbol is inserted ot the end of both the arta
Sort the data as it arrives to be merged to inform the algorithm that all clemed! RIGHT
> (iv) Adaptive in the referenced array are processed, When one of
Performance of algorithm vary with input pattern
arrays is reached to its Sentinel symbol, remainil
clements are simply copied into a final larger array.
> (y) Incremental
Build sorted sequence one number at a time

Scanned by CamScanner
Divide and Cong
ez, Analysis of (MU- Sem 4- Comp) = [ET anatysis of Algorithms (MU- Som 4- Comp)
Step:
LerT AIGHT
ESI ad A 2 detent ES) ples]
fork tow ta high de ' 1
setany ea 0 ' sansyhnd
[sJzfs5]
O71 BEElaETer
GebleierT
tet He77= 08,
RIGHT

Ler Bucur
Bisf4irt=] filslef=] nd Lert racer
H 1 fsieini=) Bilssfoel=]
' 1
Step: Finally, the sorted sequence of two sub lists
RIGHT
Eel Pls[el=] LEFT = (22, 33, 44, 77} and RIGHT = (11, 55, 65)
1 idany = : would be B=(11, 22, 33, 44, 55, 66,77).
(sifz2[s9[«4] Complexity analysis
x ‘Q.: State time complexity of merge sort. (4 Marks)
Q.... Formulate recurrence for merge sort and find the time
complexity of it (4 Marks)
On each recursive call, the list is divided into two
LEFT RIGHT sublists, and problem size reduces by half. After sorting
Bisiari<| (lsfel=] two sublists of size (n 2), combine procedure takes n
v 1 ‘comparisons to form the sorted list of size n. Total
= COMBINE(A.0.1.3) step 5: running time of merge sort is computed by summing
tert FIGHT complexity of following three steps:
zlfalalz7 faisleirt=] (sls=) (1) Divide : This step computes the middle index of
7 the array, which can be done in constant time.
Thus,
D(a) = @(1).
COMBINE(A0.3.6) erty =77 Conquer : We recursively solve two subproblems,
RIGHT
i | 22] aa] a | ss | os | 7 =>RIGHT each of size (n / 2), which contributes 2T(w/2) to
the running time.
Fig. P.2.21 (3) Combine : COMBINE procedure merges two
Ler RIGHT sublists, each of size n/2 and does n comparisons,
Let us see how COMBINE does two-way merge of Beer) fst) end thus C(n) = (a).
v 1 Hence, T(n) = T(Conquet) + T(Divide) + T¢Combine)
two array. Consider the two sorted sub lists LEFT = {22, 33, | 5#??* Lert RIGHT ~ 0
Step 6: ,ifnst
44,77) and RIGHT = {11, 55, 66). [slu[r[=] [ilssfesl=] jenjen Lert RIGHT

TO) eeG)emerce
B&lait=] (lst
let Tn)=
‘Step 1: . i for ke— low fo high do for ke low to Nigh do
7 1
LEFT
abslal7T=]
RIGHT
[ulssfes]=) fetiet ity k= 0
LEFTQ) < RIGHT) ‘WLEFT@) ¢ RIGHT
for!kee ow to high do LEFT (=22 then Where firstT G) = Cost for solving left sublist
i
Iniiatty k= 0 J
RIGHT =55
WLEFT O] < RIGHT f =SRIGHT
[] < LEFT |
Bide LEFT And second T( 3) = Cost for solving right sublist
G ¥ FT (oz levies
RIGHT D1=11 Tea) = a(3) +8()+00)
> RIGHT g)< LEFT
= a(3)+e +221)
ust RIGHT
Eisiaint=| flsleT=| ond ‘We will derive the time complexity of merge sort using
1 1 two methods.

Scanned by CamScanner
PPanatysis of Algorithms (MU - Sent come) Divide and Conquer
Methods af deriving time Ex. 2.2.3
complexity of merge SOF Sort the following elements using merge sort: 410, 385, 279,
752, 451, 523, 961, 354, 550, 620
(2) Stable /Unstable Soln. : Merge sort is divide and conquer approach. Merge
sort algorithm divides the list in 10 two halves. Splitting is
(3) Not Incremental continuing until problem size reaches to 1.
deriving time complexity We computed the mid value as follow:
Fig. C2.3: Methods of (4) Not online mid = ceil(low + high) /2
of merge sort
rather than,
aw Using substitution method (5) Not in place mid = floor(low + high) /2
Solving recurrence for n/2, Divide phase for given data is described in Fig. P. 2.23.

(3) = (8) 4
Fig. C24: Properties of merge sort
properties:
ses following
Substinute it in Equation (2.2.1) Merge sort posses
ei) = 2(2-7(8)+8) > (1) Not Adaptive
time of merge sort does not change with
= 21(3) +2n Running
sequence.
After
k substitutions, Stable/ Unstable
>
Tia) = a1(4) +kn arepossible.
Both implementations Fig. P. 2.2.3 : Divide stage
Suppose,n = 2*,sok=log,0
> (> NotIncremental ‘When problem size becomes trivial (i.c. n = 1), the
10) = at(Z) + logan nt in each pass, Fig, P. 2.2.2 : Merge sort simulation algorithm starts solving the problem and merging solutions
Does not sort one by one eleme
of smaller subproblems. The solution is constructed in a
= n-T(I)+ n-logyn not incremental. From the above pseudo code, we can scc that no two
bottom-up approach. Conquer procedure is described in
Bo, T(I) = 0 > ( Notonline alls are parallel. Each call is one of the three calls made in
17 Base case: no comparison is needed for problem of size 1 MERGF_SORT(A, low, high) procedure stated above. Fig. P. 2.2.3(a).
$0,
T (a) = O(n+log,n) Need all data to be in memory at the time of sorting Calling sequence for given data is shown below (according
> (2) Using Master method
merge sort is not online. o the picture, low = 0, high = 6) :
> (5) Notinplace
The number beside dotted line indicates call number in
Recurrence of the merge sort, T(a) n(3)+
= nis of the entire sequence of merge surt procedure. Call sequence
Jt needs O(n) extra space to sort two sublists of for given example is shown below:
the form T(n) =a. (2) +f(n). (0/2). tep-0: MERGE_SORTIA, 0, 6)
tep-1: MERGE_SORT(A, 0, 3)
Comparing both the recurrence, Ex.2.2.2 tep-2: MERGE_SORT(A,0, 1)
a = 2, b = 2 and f(n) =n with d = 1, where d is the Sori the sequence using merge sort algorithm : A {33, 22; tep-3: MERGE_SORT(A,0, 0)
degree of polynomial function f(n). 00, 99, 88, 11) tep-4: MERGE_SORT(A, 1, 1) Fig. P. 2.2.3(a) : Combine stage
b's2'=2 tep-5: COMBINE(A, 0, 0, !)
Soln.: “Btep-6: MERGE_SORT(A, 2, 3) Syllabus Topic : Analysis of Quick Sort
Here, a = b’, so from the Case 1 of Variant I of master Merge sort works as follow : tep-7: MERGE_SORT(A, 2, 2)
method, tep-B: MERGE_SORT(A, 3, 3) 2.3 Quick som
Sort
MERGE SORT (A, low, high) : £9 Quick
T(n) = ©(n‘log n) tep-9: COMBINE (A, 2, 2,3)
= O(nlogn) if low < high then, {tep-10 : COMBINE (A, 0, 1,3) Explain quick sort algorithm, Evaluate complexity with
Whether the list is already sorted, inverse sorted or mid = (low + high)/2 tep-1 7 Suitable example. (10 Marks}
randomly shuffled, all three steps must be performed Merge MERGE_SORT (A, low, mid) tep-12: MERGE_SORT(A, 4, 5) Explain quick sort algorithm using divide and conquer
Sort cannot detect if the list is sorted. So numbers of MERGE_SORT (A, mid +1, high) tep-13: MERGE_SORT(A, 4, 4) approach. (6 Marks)
tep-14 : MERGE_SORT(A, 5, 5) Discuss partition exchange sort and analyze it,
comparisons are same for all three cases, COMBINE (A, low, mid, high) tep-15: COMBINE (A, 4, 4, 5) (5 Marks)
The time complexity of merge sort for all three cases ig end ‘Btep-16: MERGE_SORT(A, 6, 6) Quick sort was invented by Tony Hoare. It is divide
stated in the following table: Narcble low and high represents the first and est and conquer based approach. Quick sort is also known
Bes ase war
tep-17: COMBINE (A, 4, 5, 6)
of the
Passed array. Call sequence is shown in Fig. P- 4 tep-18: COMBINE (A, 0, 3, 6) as partition exchange sort.

Scanned by CamScanner
tog,
240 Divi
[FPanatysis of Algorithms (MU - Sem 4 - Comp)
. o by (0 - 3) in Equation (23.2), =
=

azray from Jet tO ERE una (2) Using BS Master method ‘Substitute
ba found. Recurrence ofthe merge sort, Tia) = 27(5 ) +0 is of Tiel) = Ta-+@-1)
fe aay from Fight 10 left unt a a STi) = Te@-2+@-+0
See ifer then pivot is found. Subsite aby (0— 1) Euston 23.)
5 the form Tia) =a 2) +0).
sort, After findiD& such elements, Comparing both the
(9-2 = Tie-3)+(0-2)
- chigh, then exc] a
a<P . splike mere’
» wi
ta) = f(a).
22 edfunction d= |, where d is the 4 es +6
TO) = te Tla-3)4(a -Dele-Vee
°° high,willoni split Xchange lg
veh >hich a=2,b
of yee
degr ee
polynomial eu
~ Rath atti sted ei pased 08088 sition F
tbe PA!on actual OW
aftetl i spi te gee
here som pasos© 2ei
Mergepartitioning pase Here, a = bf, so from the Case 1 of Variant I of | “ Ti Tin-k) ata +(o-k+ 1) +(a-k +2)
em Solve t . in) =
ay Siemens - . master method, Let =m+4 e-em
value of elements.
vides Li the middl
complexity
analysis
analy Tia) = Ofatlog a) = O(olog 0)
~ Ee does notin enst anced‘i division. Divi” Téa) = TQ)4142434.4(0-Jela-Neon
pot the posit Worst case
sont
purely depeads onthe value Of PO ied a6 8 Best
case
-
complexity of quick sort
@. Dative worstease ime efficiency = 142+3+.¢n=E0
Firs, last or any therandom clement £ yd be fed |'q. Derve the best case time compieniygoq Prove that worst-case is O(n").
af quick sort
=O)
+ (072))
= ant 1)/2 = (82
:
(5 Marks’ J
pivot However, position of P*
for the Quick sort: case, quick | <ort| behaves| rer )o
Case {S Moris) | | Note 2: tn woretsot.
for all iteration. . New
FO io n) Best : Case @.: ‘Prove that for the Quick sort: Worst
_ is a tnew problom of size
Bach sublis Ie tn
continues until it hits = O(nlog
TIN) TIN) = O(n")
(5 Marks)
sort occ ease
Worst case for quick sort occurs when the list is already | Average
for quick
pivot is selected and the process itions are b ‘he When the fl
Best case he end. The For quick sort, average case running time is much
the base case. Partitions are balanced and iis] sorted. In that case, the pivot will be at eithersubproblem s | closer to the best case.
= Algorithm for Quick sort
from the middle.
merge SOT Hence complexity
‘ of bes, ca worst case behaviour for quick sort creates case Thats,
and zero elements. Worst
O(n log n). Let us prove it by solving Tecunexs|
with (n — 1) elements
the list is shown in Fig. 2.3.1. In this case, the 7
Q. Write an algorithm for quick sort.
(3 Marks)
Ta) = 0 partitioning of
height of tree would be n,
T(a) = O(n log, a)
= - - :: — h The time complexity of all three cases is stated in the
Algorithm for quick sort is s|
“ T@) = (7) +0(n) following table:
| Algorithm QUICKSORT (A, Tow, high)
// Description : Sort array A using
Quick sort case | Worst ease |
Best easé | Average
1) Input: Unsorted array A of size a, low = 0, high = 9 is an(§) is the time required to solve tiopl | Ocory |
Ofalogsn) | _OCatozsny
Mf Output : Sorted array A "Randomization
meee Find pivot indoxto spit ist }]] Size (2) time required to fix the positiong|
O(n) is the > (ay 15)
Fixing of pivot requires a scan of the entire ary,
if low Shigh thon
«1 PARTITION (a, low, ryt sotunaots_] Q. Explain randomized version of Quick sort and derive
The recurrence of the quick sort for thety its complexity.
mans) |) Santinee comet ct
QUICKSORT (A, low, ql

Gmsne Le Q. Explain randomized quick sort. {3 Marks)


re Q. How to achieve n) tme complexity in the
O(n log
? {3 Marke)
end worst case for quick sort.
GubcuiPARTITION determines the comect | (Wo methods: (1) Substitution method and (2) Master) = Worst case for quick sort depends upon how we select
position of pivot in a sorted array. PARTITION subroutine pivot element. Worst case for quick sort occurs
when
(1) Using substitution method
scans the array from left and right. Using low index, it finds | the array is already sorted and first or the last clement
element greater than the pivot and using a high index it finds Fig. 2.3.1: Worst case scenario for quick sort ion te an
Solving recurrence for n/2, is used as a pivot. We can add randomizat
the element smaller or equal to pivot.
.t(2) = ny 2 Combination cost for quick sort is constant because algorithm in order to obtain better performance.
The pseudo code of PARTITION subroutine is given} each call fixes the correct position of the pivot and no extra In the worst case, selection of first or last element
35
= 4 ) *2 —
below:
Ta) work is required. To divide the list, algorithm scans the pivot partitions list in (a - 1) and © size sublists. Bycan
PARTITION (A, low, high) Substitute it in Equation (2.3.1) and determines the correct position of the pivot,
so
choosing random clement as a pivot,
partitions
ie (a). In the
“7 = a(2-7 (3) +4) +n -#.1(§) diviion cost of QUICK SORT is sizelinear,
‘worst case, one of the sublists hus O, and other has size roughly be balanced, and performance close to meres
c€-A(highl ifs pivotclemént Le, st elemen nee ctiigesca|| Ay"? sort may be achicved. ;
i€low-1 After k substitutions, (n= 1), 80 in next call, the algorithm has to solve a problem — Selection of pivot point position is a key factor
in the
a
for j € low to high ~ 1 do
: Tn) = 2#&T(5
of size (n— 1). performance of a quick sort.
if AR] S te . *1(#) tka “Thus or pivot selection © |
ire TO = 1 (Base ease)
swap (Ali), ALD) Suppese,n = 2',sok=log,n Ta) = T(conquer) + T(divide) + (combine)
T(r) = n-T ( 7) log, nn
end 2.T(n) = Tin-1)+8n)+80)
ond 4
= T(n-l+n
(23.2)
= n-T(1)+ n-log,n i
swap (A[i + 1),
eee 1)
Afhigh})
1 Correct index of pivot hte Bu, T(l) = 0 ue Using iterative approach,
ing coi. 4 Base case; no comparison is need for

ST) = O(n. log, n)

Scanned by CamScanner
Divide ang
Fin a)
jos of quick sort 4q
“Ww O(loga) amount of extra tng [analysis of Algorithms (MU - Sem 4 - Comp) 242 Divide and Conquer
ee “is mas ssively recursive, Moy] Step 3: Allow] = 33 and Afpivot] = 44 Step 9: Afhigh) = 11 and A[pivot] = 44
Allow] < [pivot] => low = low +1 Alhigh] S Alpivot] => fix high and perform swap
“test caso, quick 8 sort is extremely fast
=2+1=3 Here, low 2 high so swap Alpivot] with Afhigh) divide list
very jkcomple pivot in two sub lists and reset pointers. Recursively sort both the
Output ; Sorted array A Find sort is bettor than merge sort? sub lists.
4 Outpa spit the list pivot
ig Lime of x
if low < high then
(s Jw, high) 1 aot is COS jered better than merge tt ay low high
) 4 quick sort rs.is © 4 The complexity of sorting a
q— RANDOMIZED-PARTITION eae J a(n’),2oe
ot comp ing gon Step 4: Aflow]=77 and Afpivot] = 44 01239456
QUICKSORT (A, low, ¢— I< f comparison operat
Perations pergadlhy Allow] > Alpivot] => fix low check for high pointer
high low
measured bY in, Mari:
py thealgonithm Alhigh] = 66 and A[pivot] = 44 Step 10 : Let us first sort the left sub list
QUICKSORT (A, q + 1, high). ye general, quick sort Goes TEWCK comparig,, llwe Afhigh
sort is in place algorithm and
> Alpivotl = high
high-1=6-125 Aflow] = 22 and Alpivot] = 11
soft. Quick
pivot Aflow] > A[pivot}, so fix low and check high pointer
The pseudo code of RANDO! - O(logn) extra space
-k sort
compared
also exhil ibits good
rin SPACE Of ma 2
cache locay;
ality, (z[s3[777 [11] 55[65]
Afhigh} = 33 and Afpivot] = 11
subroutine is given below: Alhigh] > A(pivot] => high
RANDOMIZED_PARTITION (A, low, high) number
oe izes therandomi of Memory ACCESS. Ty ag a
zed version, worst case running high
=high-1=2-1=1
jntroducing ey low
pe made O(nlogan).
i RANDOM(ow, high) quick sort cal
Exchange(A[iJ, AlhighD
Step 5 + Afhigh] = 55 and Alpivot] = 44
return PARTITION(A, low, high) Ex. 23.1 As Alhigh] > Afpivot] => high =
1 Correct index of pivot after sorting, Trace quick-sort for thedata set: A= {44, 22, 33, 77,414 high- 1=5-1=4 Lat sub ist Fgh
bu tet
is discussed in regular 66) pivot
The subroutine PARTITION Step 11 : Afhigh] = 22 and A[pivot] = 11
version of quick sort. Soin. ; ; [24]22[sa]
77] 11] 55] 66] Afhigh] > Alpivet] => high =
working principle, quick
«= Time Complexity According (to its 012345 6
high-1=1-1=0
algorithm finds the element larger than pivot while mny low high
element as pivot, eq
— If every time we select first or ast from left to right, and finds element smaller than or
lists of size Oand pivot while moving from right to left. When such elenegl Step 6 : A[high]= 11 and A[pivot] = 44
then sorted list will be divided in two
n—1 for sorted data. But randomized version selects are found, Alhigh] < Afpivot] => fix high and perform swap
the pivot as above stated, which avoids pivot being If low < high, exchange Allow] and A [high]. Here, low < high, so swap Aflow] with Afhigh] and
always first or last. Lists after division will be no more and Afhigh). Ty again start moving low pointer
If low 2 high, exchange A[pivot] Step 12: Afhigh] = 11 and Alpivot] = 11
biased now. On average we can consider that list will split the list into two sublists and both subliss pivot
will be divided in approximately two halves on Afhigh] < A[pivot] => so fix high and perform swap
sorted recursively. [+4 ]z2[99[ 77] 17] 55] 65] Here, low > high so swap A(pivot] with A{high}, divide list
each division, which leads to the recurrence 6
= Simulation of the quick sort for given data is sh
0123465
low high
in two sub lists and reset pointers. Recursively sort both the
T(n) = 27(n/2) + n. The solution to the recurrence is sub lists
below :
discussed here:
Step 1 : Fix pivot point, low and high pointers. Step 7: Aflow] = 1! and A[pivot] = 44 pivot
Solving recurrence for n/2, Keep moving low and high pointer until Allow] < Alpivot] = low =
tft
21(7) = a(7)aya+f Allow] > A[pivot] and A[high] < A[pivot]. low+1=341=4 o 1
And if low < high then swap A[low] and A [high] pivot fo \ high low
Substitute it in Equation (2.3.1)
low 2 high then swap A[pivot] and A[high) [olee] se] s177| ssl
(1) = a(2-7(2)+8 +n 3 o71 345 6 Step 13: A[low] =33 and A[pivot] = 22
Initially,
Aflow] > Afpivot] => so fix low and check high pointer
= 2:1(4) +2n [+[z]sa]77] 11] 55] 66] Step 8: Alhigh] = 33 and A(pivot] = 22
After k substitutions, o1e2 345 6 ‘A{low] =77 and Afpivot] = 44 Afright] > Afpivot] => high= high-1=2-1=1
Step2: Allow] =22 and A[pivot] = 44 Allow] > Alpivot] = fix low and check high strot
Tn) = 21(3) +k Allow] < Alpivot] => low = low +1 A(high] = 77 and Afpivot] = 44 EES
Suppose,n = 2' sok=log,n =14+1=2 Alhigh] > A{pivot] = high o 12
‘on
Tm) = 9-7 (2) +Hogn-n pivot
[aa]z2]s9]
11] 77| 58 [0] Fight
su ist
= n-T(1)+ 0: log,n [+4]22[sa]77[ 13] 55] 65] ; 0123456 Updated Army :
But, T(1) = 0 0123 465 6 fila Te aTo Ts 1s]
4 Base case: no comparison is needed for problem low high
of size 1

Scanned by CamScanner
forthe data set [Bl anatyeis of Algor (MU - Sem 4 -Comp)
[high 22 and A[pivot]
S Alpivot].=> low=low+1=3+1=4
Allow] > Alpivot}, so fix low and chee!
Alhigh] S A[pivot),so fix high and perform swapJ, divide list
Aflow]
A{high] = Alpivot] and low < high, so exchange
yeh sonrauae
Here, low 2 high Atbtoty with A(bigh
swap Afpivot] +e working principle, gy, Afhigh}- Increment low.
in two sub lists. Left sub list has size zero and Fijght sub list
uired. pra
has only one element {33}, no further process is 4
s0 ihe element larger than
sos ‘eft to right, and finds clemeng ™
pivot
iyot while moving from Heht ig
nts are found,
1 2 J, exchange Aflow] and A [higy Allow] > Alpivot), so fix low and check for high
high tow flow <hig! Alhigh) > Alpivot] => high=high-1=6-1=5
Step 15 : Let us now sort the right sub list of original array If lowlow 2 2 high,hig er two sublists
exchangeinto A[pivot) ang a Allow] < Alpivot],
so low=low+1=7

] zz Gis) | Gerrit
the List in!
‘This will split
derived in step 9
Allow] = 55 and Afpivot] = 77 sublists are sort ed rec ursively.
element of thepivlist
isthe [=]: Je]:
Let's asoume that first fT 2 3 * 78 6
Allow] < A[pivot], low = low +1=5+1=6 = ot + 7 :
set low
Initially, We will fow high
‘Aflow] > Alpivot],
so fix low and move high
high = last index. Alhigh) > A(pivot)=> high=high-1 =5-1=4
Step 1:

Step 16: Aflow] = 66 and A[pivot] = 77 Aflow] < Afpivot], pial tow
low= low+1=6+1=7 5 a\elo
1=24+1=3 ap2
low 2 > high so stop, and check for high index F ivot) => low=low+
= = =
Alhigh] = 66 and A[pivot] = 77 eae ) 1 2 ft re v
Afhigh] < A[pivot], so fix high and perform swap pivot high low
Here, low 2 high so swap A[pivot] with Afhigh], divide list Afhigh] < Alpivot], and high < low, so exchange
in two sub lists and reset pointers. Right sub list has size Alpivot] and Afhigh] and split list in two sub lists
fow high
zero. So only left sub list needs to be sorted.
met ow is — Afhigh] < Alpivot] and high < low, so exchange
and move hig Afpivot] with A{high] and split the list into two
Allow] > Alpivot], so fix low pointer,
pointer sublists. pivot low
> A[pivot), => high=high-1=8—1 — Left sub list (2, 1} and right sub list {5, 9, 4, 6, 5} are
Alhigh]
recursively processed in similar way. high
Step 17 : Aflow] = 55 and A[pivot] = 66 Step 3: the same procedure for both new sub lists. In
Reassign value of low and high in first sublist.
Aflow] < A[pivot],
low = low +1 =5+1=6 sub list (4, 5}, Allow] > A(pivot], so fix low, and check for
low > high so stop, and check for high index high. Afhigh] > A(pivot], so high = high— 1 =4
Alhigh] = 55 and A[pivot] = 66
Afhigh] < A[pivot). so fix high and perform swap
Here, low 2 high so swap A[pivot] with Afhigh], divide list pivot bow
high
in two sub lists and reset pointers. Right sub list has size
zero and right sub list is of size 1, so sorting is not required In first sub list (2, 1}, Allow) < Alpivot], but low pivot
at al. high
cannot go abead as it is pointing to last element in the list.
So fix low and check for high. Afhigh] < A[pivot}, and Afhigh] = A{pivot], and high < low, so exchange
[ss [ss]
Afpivot). Pivot will set on correct location and
high = low, so exchange Afhigh] and A(pivot] and divide | q(pyigh] theandlist into two sublists, one with element {5} and
* in pivot
tow high 1 the List. But new sub list has only one clement sono need t0 | divide
another list is null. As there is only one element in list (5),
Pe
Alhigh] < Afpivot] 2
As lowlow <=va
d oe mo need to perform sorting on it. Set pointers on list (6, 9)
Updetadsarees
amay: exchan,
ein.
A
Allow] with Afhigh] high. moving
tS andfix start
[st] 22] 33 [J 6] 5577]
The final sorted array is : pivot low high
[iT2Ts [aT s [esF 77] Repeat the procedure for second sublist (5, 9, 4.
6, 5}.
it and high
j Pivot is the first element in the list, low is next to
= Gray cells indicate movement of the pivot.
points to the Jast element.

Scanned by CamScanner
Divide and Congy
er
Panarysis of Algorithms (MU - Sem 4 - Comp) 245 ———— al (analysis
of Algorithms (MU - Sem 4 = Comp) 246 Divide and Conquer
In sub list (4, 5}, Allow] > Alpivot), so fix low, and jvot] => fixhi igh and perform . swap Pivot Allow) < Alpivol],
so low =low+1=1+1=2
igh,of) so swap Al low] with A(high) and agajg
check for high. Afhigh] > A[pivot]. so high = high - 1=7
inter [=] <]=]=
[eo] [|] 7]
a 1 2 3 4 5 6 7 8
Pivot
efo[elel*lel#l]|
High

pivot low 7 1 2 8 4 5 6 7 8B
Low
high Step 10: Step 15:
- Albigh] = Alpivor], and high < low, so exchange
Low High Alhigh] = 60 and Afpivot] = 65
Alhigh] and A[pivot). Pivot will set on correct location Alhigh] < Afpivot] => sa fix high and perform swap
und divide the list into two sublists, one with element + Al ow! = 80 and A[pivot] = 65 “ohn: Here, low 2 high so swap A[pivot] with Afhigh], divide list
(9} and another list is null. As there is only one Step Saat)
Allow] > Alpivot => fx low and check for high pointer in two sub lists and reset pointers, Recursively sort both the
element in list (9], 00 need to perform sorting on it. ‘Afhigh] = 75 and Alpivat]
vob y = vot] == 656° - sub lists
The list is sorted. ‘Afhigh > ALpivotl = high = high '=6 Pivot
Step 16:
[~[=]=[=s]]s[|
SaeeeeeeD
1SAPE) MU - May 2013. 10 Marks Pivot

Sort following numbers using Quick sort algorithm. Show all 0 2 3 4 5 6 7 8


passes of execution. Also state the time complexity. 65, 70, p12 3 4 5 68 7 8 High Low
75, 80, 85, 60, 55, 50, 45
Soin. : Simulation of the quick sort for given data is shown
Low High Step 11: Let us first sort left sublist
below: Step 6: Afbigh] = 55 and Alpivot] = 65 Allow] =45 and A[pivot] = 60
Step 1 : Fix pivot point, low and high pointers. swap
‘Afhigh] < A[pivor] = fix high and perform A[high Allow] < Alpivot]
= low = low +1=1+1=2 Step 17:
] ang
Keep moving low and high pointer until A[low] > A[pivot] Here, low < high, so swap Aflow] with Left sub list Right sub fist
and again start moving lowpointer
Alhigh] < A[pivot]. And if
low < high then swap Aflow] and A[high]
low > high then swap A[pivot] and A[high]
Pivot
[<[=[=["[#[@[=|] *]B [= [=] =[s] 5] 5] eo] 7] 7]
oO 1 2 3 4 5 6 7 6
Initially, o 1 2 3 4 5 6 7 Pivot Low High Pivot Low High Left sublist shown in step 11 is now sorted. In a similar way,
[ss] ]s]~[e[e]s[=]«| Low High right sublist will be sorted now. The output of each step is
Sup 12: showa.
Step 7: Aflow]= 85 and A[pivat] = 65

EPP
a 1 2 3 4 5 6 7 8
Allow] > Alpivot) = fix low and check high Aflow] = 50 and A[pivot] = 60 Step 18:
Step 2: Aflow] = 70 and A[pivot] = 65 Afhigh] = 80 and A[pivot] = 65
Allow] < Alpivot] = low =low + 1=24+1=3
Allow] > A[pivot] => fix low and check for high pointer Alhigh] > Afpivot] = high = high
Lo]s]| ss]
6-1=5
5 6 7 8
Afhigh] = 45 and A[pivot] = 65
A[high] < A[pivot] => fix high and perform swap Pivot Low High

[=][%]s]
=] J] 7] |
oO 1 2 3
Here, low < high, so swap A[low] with A[high] and again
start moving low pointer Pivot Low High Step 19:
0 1 2 3 4 5 6 7 8
Pivot Step 13 : A[low] = 55 and A[pivol] = 60
[ss] Jes] =| es] so] 5] | 6] Step 8: Afhigh) = 60 and A[pivot] = 65
Afhigh] < Alpivot] = fix high and perform swap
Low High A[low] < A[pivot] = low = low + 1 but not more elements
are left in the list, so check high pointer
Alhigh] = 55 and A[pivot] = 60
oO 1 2 3 4 5 6 7 8 Step 20:
Low High Here, low < high, so swap A[low] with A[high] and again Afhigh] < A[pivot] => fix high and perform swap
Start moving low pointer Here, low == high, so swap A[pivot] with Afhigh] and split
Step 3: Aflow] = 75 and A[pivot] = 65 Pivot
the list.
Allow] > A[pivot] = fix low and check for high pointer
A[high] = 70 and A[pivot] = 65
Alhigh > A[pivot] => high = high - 8-1=7
Ls] «[#]5[e
[eo [7] Step 21:
912345
6 7 8B o 1 2 8
Pivot Low High Pivot High
[=[=][~]«]°]=[#]=|
0 1 2 39 4 5 6 7 8
ri ‘ Allow) = 85
and Alpivot] = 65
OW]. > Afpivot], , so fix low
and check feck high Step 14 : Right sub list is empty. For left sub list,
Low
Alhi = 85 and
gh[pivo hig poi pointer
High ] t] = Aflow] = 45 and A[pivot] = 55
Low Alhigh] > Alpivot] = high 65 _e
= high-1=5— 4

Scanned by CamScanner
ooo
7
EP} acaiyss 0 Agontnms (mu «Sem Can) step 4 AP anaiysis of Algorithms (MU Sem 4 - Comp) Divide
and Conquer
Right sub ist Step 12:
733 4 5 6 4 Right gub list
e QG
[e]5 [| °
?
Low H "gh
6 7 Pit
o 12 3 6
siep3 Pivot Low
Pivot High
Right sub list Low
High
aqe(e[el7l*Te[5)
9123 4 5 6 7
Lets sort this right sublist
Step 13:
Let us sort left sublist.
Step 21:
Pivot Low High
Step 6+ 1 2 3
Step 24:
aye
[=] [=[*[>
7123 4
]][>] Step 14:
Pivot Low High
Step 22:
5 6 7
rect Low High
12 3
Step7: Pivot Low
Final sorted array would be:
“50 ol ot] 33/98 | 2] 81 | 77] 7 High
0 | ss | co | 65] 7] 75] 0 | 05 Step 15: Fight sub ist The list is sorted.
; 6
Phot912 $3 4 5
Low High6 7 ‘The final array will be:
Gray cells indicate movement of the pivot.
Ex. 23.4 TUE
Step8: [e[=[#]#]~[*]
eJ*[=[=]2]«[ [>]
1 2 3
Pivot Low
‘Sort the following numbers using Quick Sort, Also, derive the High Syllabus Topic : Analysis of Binary Search
Qui Sort. 50, 31, 71,
time ‘complexity ofof Quick 1 33
77, 81,Bt, 12,
7 88, 77, 1 2
| Soin. : OW
Let us solve this right sublist first 2.4 Binary Search
ma \ mt
See 1: Fix pivot point. low and highpointers. D> (Dec.15, May 17)
as

P moving low and high pointer until Aflow] > Alpi :



and AfRigh]< A[pivot]. And if Atos Q:. Implement the «binary ‘search and dative _ tts
> Ato
) | Si 2 3
as

i complexity. Pee eee na stat


low < high then swap Aflow] and A[high] Pivot Low of
s 7 i = meee Q: While the algorithm and derive the complexity
elo0 [=] [ole
High
[> [a]
eae then swap A[pivot] and A[high] eed
1: . De Sea
nitally, Is Binary Search Algorithm.
divide and conquer.
Step 17: GQ @. Explain binary search using

[2] [> [=[7[*Je]=] Pe Lowtr [al pproach, {6 Marko)

Hh tt
oe EE in compater
High Pivot Low High 2 3 Searching is very common operation
Pivot Low science applications. Searching is the problem of
cuss Low high Senan Sort the left sublist of data. We will
High finding an element from given set
search, and binary

BBEE
Giscuss two approaches, linear
‘Left sub list of step 9 is sorted. Let’s sort the right
sublist
search. Binary search is divide and conqyer approach,
The discussion will prove that binary
[2] [*[*
generated in step 9.

o 1 22 3
[[=Te]s]
45
o4 2
:3 Step 18: outperforms linear search.
Binary search is efficient than linear
search. For binary
° Step 11; Poot Low High search, the array must be sorted,
which is not required
Pivot h 5 6 7 in case of linear search.

sep: SEE Step 19:


Pivot Low High
— Binary search is divide and
conquer based search
technique. In each step, algorithm divides
the ist into
to be searched is
jo} ]=f=[r[s [ela]
element
two halves and check if the

Oo 1 2 3 4 5 6 7
ratte
"mat Low
‘on upper or lower half of
found on middle, algorithm
the array. If the clement is
retums.

Pivot Low High =

Scanned by CamScanner
Complexity analysis
cand maxima 1 index af the xr
Let us assiga minimum
to variabl es Jow and Ai’ respee ctively. The Best case [eM anatysis of Algorithms (MU - Som 4 - Comp) Divide
and Conquer
amay H In binary search, the key is first compa
middle index is computed as (low + Lompares § the a
&

on, the algorit hin ¢ the mul middle middle element of the array. IE the key js wethewith Tm) = 7) +1 (1) Ae mid = Ab1=33
ow +high)/2=(43¥2=62=3
In every iterati ithm doen
be searched. [ni ial the array, the algor Middle
clement of the array with a key - 10 1], When the key IS position of theanay, ot y og Substitute n by n/2 in Equation (1) to find T(nv/2) Key = AD].
of search is AO] to al [n compa rison , irrespective of the size of Element found, so return mid 3
range poss lities= algorithm would be, Tn)
* 'lent bs | 73) = T(R)+ +2)
compared with A [iid], there are three Almid] then key case running time of
The array is already sorted, so if key < Substitute value of T(n/2) in Equation (1), Ex.2.4.3
L
of the array. Search Worst case
cannot be present in bottom half
ata = T(S) +1 @) Consider the binary search algorithm so that it splits the input
space for problem will be reduced by selting the hist ot Into two sets of almost equal sizes, but into three sets of
Alfow
index to (mid - 1). New search range would be . "30 maximum log,n amr ay division“scan Substitute n by 1/2 in Equation (2) to find T(n/4), sizes approximalely one third, write down the recurrence for
10 Almid - 1]. a &
T( ai) = TG)a +1 this temary search algorithm and find the asymptote
22 tn A Ae Amid] omy.
then present °
cannotfor bepeblem willin | possible.
the key space
Search If the key isa ot the leaf of the tee of i * a complexity of this algorithm.
compari 8 nog Substitute value of T(n/4) in Equation (3),
be reduced by moving the Jow index to (mid
+ 1). New | present at all, then algorithm does , logan
present Ons, we OR
+ 1]10 A[high] is maximum. Numbers of comparisons wth
grow in jou OBRLithmie T(r) =1(S) +3 A binary search splits an array into two parts. If it splits an
search range .would be A[mid
- it auld size.
order of input Hence, ~ time of
worst2). cas, © running
\ 3. eeey = A[mid], the search is successful and algorithm stgorithi be, Tia) = Otlop, array into three parts, write down recurrence relation and
After k iterations,
complexity.
This process is repeated till index /ow is less than high After every iteration, problem size is teduced b Soln. :
orelementis found, factor of 2, and the algorithm does one ‘Writ
compar’in, aT (n) = t(%) +k
‘Temary search divides the array into three parts, so
Recurrence of binary search can be
&
.
Algorithm for binary search T(n)= T(n/2) + 1. Solution to this recurrence leads a a Supposc,a = 2%, Sok =log.n instead of mid index, we would compute oneThird and
Write an algorithm for binary search. (aMarks) | | running time, i.e. O(logon). Detuil derivation is discussed
Ex. 2.4.1.
Tm) = (3) +
Zz twoThird indices of the array as shown in the figure. The
array is divide in three parts: upper, middle and lower.
Algorithm for binary search is shown below : = TW)+k
[Algorithm BINARY SEARCH(A, Key) Average case So,T(n) = 1+k=1+log.n

// Description : Perform binary search on array A T(n) = O(og,n)


Average case for binary search occurs when a
Taput : Sorted array A of size n and Key to be searched element is neither on middle nor at the leaf level of the Ex.2.4.2
/ Output ; Success/ Failure search tree. On an average, it does half of the Io, n Find element 33 from array A (11, 22, 33, 44, 55, 86, 77, 88}
comparisons, which will turn out as ‘T'(n) = O(log, n). using binary search.3
The complexity of linear search and binary search for Soin. :
all three cases is compared in the following table. The array is already sorted, so we can directly apply
Best case | Average case | Worst case] binary search on A. Here Key = 33.
mid & (low + high) /2 | Binary Search | _O(i) Oflogan) | O(login) Iteration 1: Initial search space
if Afmid] == key then
return mid
else if Amid] < key then
Linear Search Od) O(n)
[1+] 22] se] +] ss] 8] 7 Joe]
12 3°4 5 6 7 8
Ex. 2.4.1
dow mid + 1 low I high =8, low=0
else Write the recurrence relation for binary search and solve it. mid (low + high) /2=(1+8¥2= 92 =4 high=8
high — mid -1 OR Amid] = A(4] =44 oneThird = (low + high) /3 = (0+8)/3 =2
Write the recurrence equation of binary search and prova As Key < A[4], update high two Third = 2/3 * (high- low) / 3 = 2/3%(8)/3=5
that running time of the recurrence is O(logzn). high = mid-1=4-153 Initially, low and high indices are pointing to minimum
Soln.:
Iteration 2 : New search space and maximum index of an array.
In every iteration, binary search does one comparison If the key is less than the AfoneThird], then it will be
~ Binary search reduces search space by half in every
iteration. In a linear search, search space was reduced and creates a new problem of size n/2. So recurrence definitely in the upper part of the array, so high index is
equation of binary search is given as, updated to oneThird — 1.
by one only.
If there are n elements in the array, binary search, Ta) = (3) +1, low = I,high=3, If key is greater than A(twoThird], then it will be.
ifm>l
linear search has to search among (n / 2) and
and mid = (low+high)/2 definitely in lower part of the array, so low index is
clemen
(n- 13)
Ti) = 1, ifn=t
= (L+3/2=42=2 updated to twoThird + 1
s respectively in the second iteration,
third iteration, the binary search has to scan
In the Only one comparison is needed when there is only on¢ Almid) = A[2]=22 And if the key is between AfoneThird] and.
only (n/ 4 As Key > A[2J, update low
elements, whereas linear search
has to scan (n — 3
element in the array, that’s the trivial
case.
low = mid+l=2+1=3 AftwoThird] then low and high indices will be updated
elements. This shows that binary
bottom very quickly.
search would hitie This is the boundary condition for recurrence. Let vs
Solve this by iterativeapproach, Iteration 3 ; New search space
to (oneThird + 1) and (twoThird - 1) respectively.
° low = 3,high=3,

Scanned by CamScanner
Divide and
Efanatysis of Algorithms (MU - Sem
4 - Comp)
2-21
. ; [Panatysis of Algorithms (MU - Sem 4 - Comp) 2-22
Soln. =
below : Max Min problem is to find a maximum and minimum
— Pseudo code for ternary search is given
arrangement,
_ We will discuss two cases of coin element from given array.
[Algorithm TERNARY SEARCH(A, Key) A Case I : List of coin is sorted
according to their weight
In the traditional approach, the maximum and
/ Description: Perform ternary search on array searche d in ascend ing orderof weight minimum element can be found by comparing each
apa: Sorted array A of size n and Key to be (A) List is sorted
element and updating Max and Min values as and when if LMin < RMin then 4 Combine solution
/ Output: Success / Failure In this case, there is no need of sorting the list, W, required.
directly perform the binary search. Binary min — LMin
iteration, jy
This approach is simple but it does (n — 1) comparisons
low 1 divides the list into halves in each for finding max and the same number of comparisons min <- RMin
high —n worst case, it performs O(logn) comparisons. for finding the min. It results in total 2(n - 1)
one comparison ead
But the linear search needs only comparisons.
in the first Position, Using divide and conquer approach, we can reduce the return (min, max)
while low S high do searching as the faulty coin is Soi
faulty coin. end
oneThird € floor(high — low) /3 takes O(1) time to find the number of comparisons.
twoThird < Mloor(2/3 * (high- low)) (B) List is sorted in descending order of weight Divide and conquer approach for Max. Min problem
works in three stages. «= Complexity analysis
Key is in upper part — List needs to be sorted first for binary search, Whigy If a, is the only element in the array, a is the
if AfoneThird] == key then takes O(nlong) time. And binary scarch itself rung; ‘The conventional algorithm takes 2(n — 1) comparisons
maximum and minimum. in worst, best and average case.
return oneThird O(logn) time. Over all, it runs in O(nlogn + logn) ting |
else if A[twoThird] == key then
© If the array contains only two elements a) and a2, DC_MAXMIN does two comparisons two determine
— This would be the worst case for linear search as tie then the single comparison between two elements minimum and maximum element and creates two problems
retarn twoThird faulty coin is at the end of list, and it will take O(a) can decide minimum and maximum of them. of size m2, so the recurrence can be formulated as
else if key < AfoneThird] then time. 4 © If there are more than two clements, algorithm a
high <oneThird - 1 Case I: List of coin is not sorted according to their weight divides the array from middle and creates two
else if key > AfoneThird] && key < A[twoThird] then - To perform the searching using binary search, we fing] subproblems, Both subproblems are treated as an Tie) = 1 4 ifn=2
low + oneThird + 1 need to sort the list. Divide and conquer based sorting independent problem and the same recursive T (+2 vifn>2
high — twoThird - 1 techniques (like merge sort, quick surt) takes nlogn| process is applied to them. This division continues
time for sorting. | until subproblem size becomes one or two. Let us solve this equation using interactive approach.
else
— The binary search runs in logn time, so overall finding — After solving two subproblems, their minimum and To) = 2 (3)+2 225.0)
fran | maximum numbers are compared to build the solution
,eod tore poThind + faulty coin takes O(nlogn + logn) time. Linear search
of the large problem. This process continues in a By substituting n by (n / 2) in Equation (2.5.1)
“@) ==)
does not require sorting of data, in the worst case, i
bottom-up fashion to build solution of a parent
return 0 performs O(n) comparisons. q
problem.
— So in any case, linear search outperforms binary search]
‘There are two cases of ternary search:
in particular given case.
‘Algorithm DC_MAXMIN (A, low, high) Tm = [zr (3 +2)
Case 1: Two comparisons fix the position in one of the }f Deseription: Find minimum and maximum element from
parts of the array, and the size of the new array using divide and conquer approach = 4T (3)4+2 052)
ff Input: Azray A of length n, and indices /ow = O and high =
problem would be n/3. Recurrence for this Syllabus Topic : Finding Minimum and Maximum}
Algorithm and Analysis
case would be T(n) = T(n/3) +2 a-l By substituting m by in Equation (2.5.1),
Case 2: After single comparison, list is divided into two fi Output: (min, max) variables holding minimum and
parts of size 1/3 and 2/3 respectively. In the
worst case, the key will be in the larger part of
size 2/3. Recurrence for this case would be
2.5 _Min-Max Problem

> (May 14, Dec. 14, May 16, May 17)


| maximum element of array
Array contain only one element
*@)Substitute==)"
it in Equation (2.5.2),
| then
ifn ==
T(n) = T(20/3)+ 1. retum (A(1], ADD) = Ti) = Afar (2) +2402
Solving the recurrence T(n) = T(2n/3) + 1. @. Write a Min-Max function to. find minimum: atl Array contain only
maximum value from given a set of values else if n == 2 then
The recurrence T(n) = T(2n/3) + 1 is of the form T(n) = HA{I] < A2] then two elements: “ ar(Z)+8+se2
aT(n/b) + f(n), with a = 1, b = 3/2 and f(n) = I with degree return (A(2], AL2])
of polynomial d = 0. = aa()se ates
b? = (3/2)°=1 return (A(2], A(L))
Here, a = b’, so from master method T(n) = @(n“logn) else
After
k — | iterations
= O(n"logn) mid € (low + high)/ 2
{[LMin, LMax] = DC_MAXMIN (A, low, mid) . Te) = ager je tte ez
= @(logn)
Thus, worst case running time of ternary search is G{logn). [RMin, RMax] = DC_MAXMIN (A, mid + 1, bigh)
now array of n-numbers that uses (31/2) - 2 compa
Solve right sub problem
in that one coin is for anyn.
‘Suppose you are given n number of coins, 6 solution
y, is less than standard coin weight. To find
its weight
fauit Design and analyze a divide and conquer If LMax > RMax thea 4 Combine
the faulty coin in a fist using the proper searching method. for findinga
maximum and minimum element in,
What will be the compiexity of the searching method? Ui-np.
- 6

Scanned by CamScanner
Panatyois of Algorithms (MU - Sam 4 - Comp) Fe
is is same as Ci2 derived using convent;
derive all Cy f0r Strasse [Befanaiysis of Algorithms (MU - Sem 4- Comp)
- 21(sBr van (Simplifying series) roach, Simi , fy we can| Algorithm for Steasset 7 Divide and Conquer

marin mlilieation,
shown below : TRY =* *(), ST Ba7 = G 4
i *A, int *B, Sine
3 ol= Lis %20
ma nts) SASSEN MAT MUL Gat
7 = il cy S, = fo bethy4
ne e(s
ns G ‘Thus, running time of Strassen's matrix multiplication
- (S16 a)
Ral = G ‘)r (Fy) +0- -2) fA and B are input matrices
algorithm O(n?*'), which is less then cubic order of
traditional approach. The difference between running time
= (4) T(2)+(n-2) AC is the output matri
x “CE SIEfe a)
All matrices are of size n x1
becomes significant when nis large.

For n = 2, T(n) = 1 (two elements require only 1


xample of Strassen’s matcix multiplication is ti s\e[h 3
time
discussed below;
‘comparison to determine min-max)
To) = (F ) +0- OTe sc + a) (2) Ex.2.64 = Ls ral
; .
Multiply given two matrices A and B using Strassen’s
B= . nha)
(3
BB)
‘ ‘})
It can be observed that divide and conquer approach STRASSEN_MAT_MUL (A, B, C, n/4) C + (n/4), nf4)© approach, where 0 ol-fo o
STRASSEN MAT_MUL (A, B + (0/4),
does only (#-2) comparisons compared 10 2(a - 1)
‘STRASSEN_MAT MUL (A + 2* (n/4), B,C +
> (al
rf EE sow |? 889) (BEB b
D
essa 284 ean
comparisons of the conventional approach.
For any random pattern, this algorithm takes the same om ASSEN MAT_MUL (A + 2 * (n/4), B+ ce}
number of comparisons. C +34 (n/4), n/4) Solin. =o 14 a
an 2 * (n/a), ¢
Syllabus Topic : Strassen’s Matrix Multiplication STRASSEN_MAT_MUL (A + (n/4), B +

eee a/4) Lac = AxB i |B =] Cy = S\-3-895

2.6 _Strassen’s Matrix Multiplication


STRASSEN MAT_MUL (A + (4), B + 3 *
C+ (0/4),n/4)
(a4)
Rea), - (2G S118 8
> (Dec. 13, May 15, Dec. 15, May 16, May 17)
STRASSEN_MAT
C+2* (0/4), n/A)
(A + _MU
3 * L
(n/4), B
B+
+ 2
3*
* (agp!
4
[03] 053] CELE
@ Wiite
a short note on Strassen's matrix muttipication.
PUREE Pree
STRASSEN_MAT_MUL (A + 3
C+3* (0/4), 0/4)
* (4),
[23] [ool cee S+8=[45 S]+Lis
Perret Parad
Explain the Strassen’s Matrix multiplication.
[Se] coal = [is 3)
= Complexity Analysis S) = (n+ An) x By + By) 20 15 5 15
Pree eed = 2 97,5
+ 3 cu = +Se[12 o] tLe
proposed conquer Conventional approach performs eight multiplications
sexed) voleodiinn which ie me ee er to multiply two matrices of size 2 x 2. Whereas Strassen’s (Ls 0 3] él) * [2 3]
oltiplications compar 10 this traditional way of marx approach performs seven multiplications on problem of size . (: 3] fo o}) Cy = S,+S,-S,+5,

ee Ue a) ee
1x 1, which in tum finds the multiplication of 2 x 2 = [3% Le - [33 -[2 3
matrices using addition. In short, to solve problem of size n,
Strassen's approach creates seven problems of size (n - 2),
Recurrence equation for Strassen's approach is given as, = [ists norte] [is 38 [ts -) = Lis 20.
T(n) = 11(3)
&- (ek alts . [eeCy
Two matrices of size 1 x 1 needs only one =ALa at* lo o, 43 912
multiplication, so base case would be, T (1)= 1. _ 76s 0 07 72 15 _ (231 (28
. [a 31*Ls 31-[2 9 = Cb 04 te
Let us find the solution using iterative approach. By Sy = Ay* (By -By) mS 9 1 24 20
substituting n = (n /2) in above equation,
73) = 77(2) - fe] (Es 6 o- 34o) . |B es
= 2 9 % 2.
2T(a) = 71(2) Ss, = Ls slo ol [hs ° 2.7. Exam Pack
Ts y-CE2 1-2 D) ____ and Review Questions) _
(University
65 00 ; S KF Syllabus
Topic : General Method
. approach, Tiny = #1(3) = Lo ol« ti 31-Lo 0 a. Explain Divide and Conquer strategy. List any Four
Ba) tht Aa) * Be | ys sesume Ss = (Ay +An)* Br examples that can be solved by divide and conquer.
n= 2 k= logy®
([5 s]+[o o])*Lo0 {Ans. : Refer section 2.1) (6 Marks)
(May 2013,
Dec. 2013)

Scanned by CamScanner
Divide at
and Con,
2:25
Diccuss partition exchange sort and analyze jt Divideand Conquer
13 (MU- Sem 4 - Comp)
PP anatysis of a. nalysis of Algorithms (MU - Sem 4 - Comp) 226
+ Divide
Comment on the module of computation
5 Marks) (Ans. : Rafer section 2.3) (5 Marks) Write an algorithm for finding maximum and | 5% — Syllabus Topic : Strassen’e Metrix Multiplication
and Conquer. (Ans. : Rafer section 2.ah Q@ = Write. a shot note on Strassen’s matrix
(May 2014) @. —_Weite an algorithm fer quick sort, minimum number from given set.
(Ans. : Refer section 2.5X5 Marks) (May 2017) multiplication. (Ans. : Refer section 2.6) (10 Marks)
Expiain the concept of divide andconquer. (Ans. : Rater sacton 2:5)(8 Marts) and conque r algorit hm (May 2015, May 2016, May 2017}
@. —_Dasign and analyz a a divide
(Ans, : Refer section 2.1.1)(5 Marks) @. Derive the best case time complenity of quick aoq for finding minimum and maximum number In the2 Q. Explain the Strasson's Matrix
Enlist few problems that can be solved using
divide (Ans,: Roler section 2348 Marks) amay of f-numbers that usos (82) - (Ans. : Refer section 2.6) (10 Marks) (Dec. 2015)
and conquer approach. a. Prove that for the Quick sort: Best Case efficiency. comparisons for any n.
)
TEN) = O(nlog n) (Ans, : Refer section 2.345Marks Marks) e
(Ans, : Refer section 2.1.13 Marks) . i (Ans.° : Refer section 2.56e
Write control abstraction (General method)
for divide @ Explain randomized version of Quick sort and deriv, @. Design and analyze a dda and conquer elemenalgorti m
and conquer. (Ans. : Refer section 2.1.2) (4 Marks) its complexity.(Ans. : Refer section 2.2) (10 Maria) for finding a maximum and minimum t
(May 20 ) Rater section 2.56 Marks)
(May 2013, Dec. 2013)
"8) list L{1 : nl. (Ans. : ogo
Derive a general equation to find the complexity of @. Explain randomized quick sort.
divide and conquer approach. (Ans. : Refer section 2.3) (3 Marks)
(Ans, : Retar section 2.1.3) (5 Marks) @ How to achieve Ofn log n) time complexity in the
Which ara different factors considered for sorting worst case for quick sort.
elements. (Ans. : Refer section 2.1.4) (6 Marks) (Ans. : Rafer section 2.3) (3 Marks)
(May 2015) @. Why quick sortis better than merge sort?
Syllabus Topic : Analysis of Merge Sort (Ans. : Refer section 2.3X3 Marks)
Ex. 2.3.3 (10 Marks) (May 2013)
Analyze merge sort and find time complexity of
p

merge sort. (Ans, : Refer section 2.2X5 Marks) Ex. 2.3.4 (10 Marks) (May 2016)
Given a sequence of n-elements Aft] ...... Afn], Syllabus Tople : Analysis of Binary Search
">
assume that they are split into 2 sets A[1] .... Aln/2]
a. Implement the binary search and derive its |
and Aln/2 + 1]... A[n}, each set is Individually sorted
complexity. (Ans. : Refer section 2.4)(10 Marks)
and the resulting sequence is merged to produce a
single sorted sequence of n elements. Q. Write the algorithm and derive the complexity of
Using the divide and conquer strategy, write a Binary Search Algorithm.
Merge sort algorithm to sort the sequence in non- (Ans, : Refer section 2.4) (5 Marks) (May 2017)
decreasing order. @. Explain binary search using divide and conquer
(Ans, : Refer section 2.2) (6 Marks) approach. (Ans. : Refer section 2.4) (6 Marks)
Write an algorithm for sorting ‘n' numbers using Q. _ Find worst-case efficiency of binary search.
merge sort. (Ans. : Refer section 2.2K6 Marks) (Ans. : Rafer section 2.444 Marks)
State time complexity of merge sort. Ex. 2.4.4 (10 Marks) (May 2018)
(Ans. : Refer section 2.2) (4 Marks)
‘& Syllabus Tople : Finding Minimum and Maximum
Formulate recurrence for merge sort and find the
Algorithm and Analysis
time complexity of it.
(Ans. : Reler section 2.24 Marks) a. Write a Min-Max function to find minimum and
maximum value from given a set of values using
Syllabus Topic : Analysis of Quick Sort
divide and conquer. Also, drive its complexities.
Explain quick sort algorithm. Evaluate complexity (Ans. : Refer section 2.5) (10 Marks)
with suitable example, (May 2014, Dec. 2014)
(Ans. : Refer section 2.3) (10 Marks)
Q@. Write an algorithm to find the minimum and
Explain quick sort algorithm using divide and maximum value using divide and conquer and als
conquer approach. darive its complexity.
(Ans. : Refer section 2.3) (6 Marks) (Ans. : Refer section 2.5)(10 Marks) (May 2016)

Scanned by CamScanner
[PP Analysis of Aigorttms (MU - Sem 4- Comp) 32 Recurrence
Put n= 1 in Equation (1) to solve problem of size 1. Replace n by n— 1 in Equation
(1),
9 CHAPTER = TU)=T(l-1)+1=T0)+1=04+1
= Ta 1)=T(n-2)+(0-1) wn)
(From Equation (2)) Put it in Equation
(1),
“T0) = 1
Recurrence Forn = 2, Te) = (Ti-2)+(n-1)J+n
Replace n by n—1 in Equation
(2)
TQ) = T2-1)+1=TH)4+1=141 Tn-2) = T(n-3)+(n-2) 8)
21Q) = 2
Forn = 3, = Tin) = Tin-3)+(n-2)+(n-
ten, wnf4)
13) = TG-1)+1=T2)+1=241
After k substitutions,
Tn) = .T(n-k) + (n-k +1)
«1@) = 3 +(a-k+2)4...(0- lan
After k substitutions, ‘When k approaches to n,
Master method.
The substitution method - Recursion tree method - T(k)
And for k
Tn) =
=T(n) = En=n(n+1)/2
Tw) = So, Tn) = On")
Linear homogeneous recurrence of polynomial onde,
‘Introduction greater than 2 hardly arises in practice. In this section, se > i) Backward substitution Forward substitution

@_Dafine recurrence equation. What is the use o shall discuss two unfolding methods which are widely seq This method substitutes the value of n by n — 1 Tn) = Ta-D+n
(4 Marks) to solve a large class of recurrence. recursively, to solve smaller and smaller problem. It
It is also known iteration methods. There are tuo works exactly in reverse order of forward substitution. Forward substitution finds the solution by replacing the
solution of smaller problem into large problem.
Definition ways to solve such equations. Ex. 3.4.2 For n= 0,T(1)=T)+1=1
Recurrence equation recursively defines a sequence Solve the recurrence of linear search using forwal rd For 2, TQ) =T1)+2=14+2
of function with different argument, Behavior of recursive Ways to Solve substitution method. For
algorithm is better represented using recurrence equations. Unfolding Method Forn=4,1(4)=1(3)+4=64+4=
Soin. :
Recurrence are normally of the form: {) Forward substitution Recurrence of linear search is, After ksubstitutions,
T(n) = T(n—1)+{(n), forn> 1. wo(BL1) Forn=k, T(n)=T(k- 1) +#k=14+2434...¢k
Tin) = Tin-1)+1, forn>Oand
Tm) = 0,forn=0 T(n) = 0, forn=0 (Base condition) ‘When k approaches to n,
— The function f(a) may represent constant or any Inductive case of the recurrence is
Tin-1)+n=1424+3+....4n=En
O(n’)
polynomial in n. = - T(n) = Tin-1)+1
Equation (3.1.1) is called recurrence equation. T(n) is Fig. C3.1 ; Ways to solve unfolding methods The problem can be solved if T(n - 1) is known, . To
interpreted as the time required to solve the problem of find T(n — 1), substitute n = n — 1 in Equation (1), Solve following recurrence using iteration method.
size n. On recursively solving T(n) for n =n — I, > (i) Forward substitution
Tin-1) = Tn-2)41 T(n) =T(n-1) +n"
recurrence will hit to the base case T(n) = 0, for n = 0. Forward substitution method finds the solution of the Replace this T(n — 1) in Equation (1)
Values will be buck propagated and the final value of smallest problem using base condition. A solution of the | 2T(n) = [T(n-2)4+1J41 Soln.: Given that, Ta) =T(a- 1) +0"
T(n) is computed. bigger problem is obtained using the previously computed ' = T(n-2)+2 (2) Substituten by n—1
— Recurrence of linear search / factorial : solution of the smaller problem. This process is repeated The problem can be solved if T(n — 2) is know: in. To =T@-1)= Ter-2)+@-1"
T(n) =T(n- 1) +1 until the solution for problem n is achieved. find T(n— 2), substitute n = n — 1 in Equation (2), 2T(n) = (T-24 (n-1'}40°
— Recurrence of selection / bubble sont : Tin-2) = Tin-3)41 ..Q) Substituting n by n-2 in main recurrence,
T(n) =Tin-D4+n Ex. 3.1.1 4
Replace this T(n - 2) in Equation (2)
Solve the recurrence of linear search using forward T(n-2) = Tin-3)+(0-2)°
Let us discuss various methods (o solve the recurrence 2T(n) = [Ta-3)+1]4+2=T(n-3)+3 Tea) = [Ma-3)+(n-2)}4(a- 1 +n!
equation. substitution method. After k substitutions,
= Use Soin.: T(n) = Taw-k}+k,
Recurrence relat iriesiech Recurrence
of linear search is, To match the base case, assume that k= n, After k iterations, é
- relation can effectively represent the Tin) = - 27) =
Tin—n)+n=TQ)=n=0+n0 T(n) = T(n-k)
+ (1-Kk + D+ (nk +2) +... 0!
running time of recursive algorithms, 1 = ae oe (From: ase-case) | Fork =n
cadaie)
Time complexity of certain recurrence can be easily
Inductive case of the recurrence is “Tn) = O(n) To) = T_T (n-ne) F(a 2).
solved using master methods. ts
Ta) = Tim—1)41 veel) Ex.3.1.3
Q
Syllabus Topic : The Substitution Method
Forward substitution finds the solution of T(a) by (n) = T (n= 1) +n using forward
= TOF e2esetal= DE
Solve recurrence equation T
solving the progressively bigger problems, starting from substitution and backward substitution method.
3.1_The
Substitution
3.1 The Substitution Method
Method
smallest possible case.
Here, the base case is
Soln. : Backward substitution
Given the recurrence,
2 Tn) = 6)

= 29 ""° 0 Tin) = Tin-})+n 20)

Scanned by CamScanner
Recune.
33
Brnctysis ot goths (uu - Ses Core) Substitute value of T(a/4) in Equation (3),
n . - Comp) a4
ET anatysis of Algorithms (MU - Sem 4
Recurrence
.T@) =T 4) +3
Ex. 315 - tose marnit 2 : Y. Cost ofbase problem
Solve following recurrence using iterati
on . Syllabus Topic : Re Cost at depth i would be ‘c(n/2 t.
P ecursion Tree Method has size 1, 30 we can consider the cost of (1) 25 constan
,net al every
Height of the tree is logon. By summing up the cost
1 .
R
After k iterations, 32
Tin) = 8 ecursion Tree
) { 2tin-t) ,> not
.T@) =T (5) +k
E
pel, cam find the cost oF whee pote+ ,
Soln. : . - U
SG tr
is recursion 7
(S Marks) Ta) = cnt @ cas (4) oo _
Giventht Te) = 200-1) = 7 Drawing a <a a = by- vesehv e, aif
guess is difficult for complex Qr 5
ee
, . Sok=logyn
renene, Roses te method provides effective and
2% #

es cach ame
Suppose,n = _ _
n-! inthis equation
2 So
n =
Substitute
T@ = 1(%) +keT()+k
. Til) = 27 @-2
se) = 2aTe-2 rt) = |
oe
functions, eac! i
s = eons) 6 a
g a ereeblem om tht ) ca + O(n)
tree represents the cost of solvin recurrence. * * |
So, T(n) = I+k=1+log,n Problems belonging to given =
4T(n-2) =2?-T@-2) j family a i give us the
=
T(n) = O(log,n) q n of cost across all branches at level = oN
cost of all levels, we get
Substitute n = o—2in main recurrence, total cost at Jevel i. And summing < Z 8) cn’ +0(0)
. _ (li) T(n) = 2T(n/2) +0 the eee Ta aia) + 008 i=0
the ret cos + (0°).
“T “ o a ie -3) 5
= ;
Given recurrence is of mergees sort. ae \s consider the securrence T(n) = step2T(a!2)
as shown in ,
) the recurr ence tree step by
a recites (a-3)) =? T@-3 i the list is divided Let us derive
proble cach
ms, =
In merge sort, after each iteration, m is divided in to twom sub
Fig. 3.2.1. Problecost
To) = 2Ta-k) and, probl em size reduces by half, of size n/2. The of solving proble of size nis rw. We ont
alike parts, search
bi inary in merge sort, we will get two the boundary
nl
After sorting two sub shall keep exploring the trec until it hits 22 700)
== O(n’)
1 Ta)k == n-1 eT
When ot
sub problems after divide stage.
| condition. F
a (n-n41)=2" -T()
/ 2) combigne timeproce dure takes n Tan)
lists of size So,(n total
= cn
*
Tia) = 02") (: TW=) runnin of merge sort ig 3
comparisons. aitthe followi‘ ng recurrence:
oe
bead 77 qii2) —Tiw2) Solve
Find oe time complexity foi for the recurrence equati
seen by 0 vifnsl T (0) =7 (92) +7 (w8)+ T (v8) +0.
(b) First Jevel expansion of TO) | Soin.
comple xity quation as (a) Ta) :
follows : Ta@)= @+r(¢ else
on for given recurrence
Recurrence tree
equation is shown
@ Tn) =T(n) +1 T(p)+T(g) + DU) +C(n) _—~
below =
(i) Tin) = 2T(v2) +nabove equations belong to which n
= cost for solving left sub list (vey,
oh
Also explain the 9 whi ‘Where fist t() o(v2y" a
searching/sorting algorithms. iB — Gen
And second T (3) = cost for solving right sub list
na wa
Soln. : “™
Tima) Tins) Tv) vie one mt 7 we
T(n) = T(v2) +1 Te) = m(5) +O (0) +9())
( wk a miso misma oy
{c) Second level expansion of Tin) i i
.
Given recurrence is of binary searchdoes a Fig, 32.1: Recurrence tree formul
ation i
one comparison .
In every iteration, binary search 5)2 +n being reduce¢ d by factor of
TQ) = 2T( At every level, problem js
size n/2. So 0 rec recurrence, is n/2'. Hence, size of
and creates then new Pl problem of
a n\ o size 2, Size of sub problem at level i = 1. Thus, tree has Tea) = Tad +T (4) +T (TBP 8) +0
+ IBD,
equation of binary search
is given as, a 2t(3) = 20 (3) + sub problem becomes 1 when n/2' Tt) < + (1/8)0-+ (TR) + +
tw = 1(2 1
logan + level. (depth vary 0
from to logzn) . Fully expanded snore, bis the beight of tee
n) = a) +! = 2 Q@ -T #42) ‘+n tree is shown in Fig. 3.2.2.
needed “TQ@
T a) = 1, n= Lie. only one comparison is F a BN
t in the array, that’s the ot =f x @
when there is only one elemen +2n
rival case. This is the boundary conditch,
ion for
approa
=2
:
(3)
iz0=o
urrence. 7 us solve this by iterative
“ «) cofn4) +10
= #-1(
es )+1
ZL ®
Ta) e= T(#
co su _
Hence, Tia) $n 0
in Equation (1) to find T(a/2)
: (2
ppose,n= 2°, sok=log,n =
Q
Pai da :
Téa)=-a- 7(2) +logyn-n=n-T(1)+ n-loga
3) = 1($) +1 ” aD
TD TH my) TH
Substitute
. value of T(n/2) in Equation (1), ay TH) TH) TH
Ths, T=
Fig. 32.2: Fully expanded recurrence tre
But, T (1) = 0, becau: se no comparison is needed 7
” , level in above tree.
mparison 15 8
(3) 42
.T() =T ‘We will compute the cost at each
I. og,n)
.=)_ | sort theT(n)list of= sizeO(n-l numb er of nodes at depth i
would
Pu 2
tn = n/2 Equation (2) to find T(n/4)
, As it
be2!.
is a binar y tee,

73) = 1)+!

Scanned by CamScanner
Recurr

Sem 4-Comp) 34 B
5 E , [ePanaiysis of Algorithms (MU - Sem 4 - Comp) Recurrence
i "analysis of Algorithms (MU_- va Examples of Mast
er method
Checking relation between a and 6°
: Master Method Ex 3.34 ‘As a=b (.¢.3< 3"), 3solution to this Equation is given as, ;
Syllabus Topic Ex. 3.3.1
T(n) = 8T (3) +nt Tn) = O(n’ log n) = O(n! logn) = O(n logn)
using master methog :
Solve following recurrance equation
Master Method oO 2) TQn)= (a) =5T (4) +0
3.3
tin) = T (2 +1 Soin. : Compare this equation with T(n) = aT (2) +f Cn) Compare given recurrence with equation with
> (May 15, May 17)
Here, a=8,b=2and f(n)=n',d=2 Ten) =aT (8) +f)
‘of master theorem. Soln. =
Explain three cases eee i +f)
-= a-T (2) Checking relation between a and b*
Given equation is of the form T(n)
@. oe
ne . soni Here,a=5,b=4and{(n)=n',d=2
Asa>b' (ie 8 > 2), solution to this Equation is given as, Checking relation between aand b*
od with example.
Explain masters meth
Q. Pee esas
Whereas 1,b= > and f(0)= 1 ‘Asacb’ (ie5<4), solution to this Equation is given as,
The time Tin) = O(n = Or")
strategy uses recursion,
——pivide and conqtheuerrecursive program is described using - ‘
comp lexi ty of studied a! = nlog,, Ex. 3.3.5
+ PEE) MU - Occ. 2015.51
ious chapter, We have ° Solve the following recurrence using Master method
fecurrence. In the prev ing the recurrence.
‘The master of recurrence would T(n) = aT (va) + Find the complexity of given recurrence relation
various meth ods for solv
find the time complexity of Here, £(0) = © (a!%8), sosolution Soin. : Compare given securrence with equation
with
theorem is often suitable to be,
@ Ten) =4T (a2) + v
lems .
© Clog") T(n)=aT (8) +f (n) Ty=2t2) 40°
4 to quickly solve the recurrence
e prob @.
Ta) = 9 (n!%- log”) =
recursiv
Master method is
uset
Here, a=4,b=3 and f (n)= nyd=2 Soin. : Given equations are of the form
£ (n). Master method

of the form T(a) = a+ T (2) + Ex. 3.3.2 Checking relation between a andis "T(n) = T(q/b) + fn). It can be easily solved using
method : is given as,
values of equation using master As acb! (1.4 <3), solution to this Equation
master’s method.
ut substituting the Solve following recurrence
finds the solution witho y O(n*)=96 ry @ Te)=4Ta2) +
Tin) = 3t (%) rntean
ion, Ta) =
‘T(n/b). In above equat
4,b=2and f(n) =n?,d=2
n= Size of the problem Ex. 3.36
Solve the following recurrences using the Master mathod : pat=a4
a= Number of sub problems Soln. :
From the master theory, here a=
bso
T(n) = 4° T (8) +f (0) T (1) =0.T (n) =9 T (nf) + A tog n; not.
created in recursive solution Given equation is of the form T(a) = 8( n' log n) = OC TT log n)
gn Soln. : Given equation is of the form
= Size of each sub probl
em
Where, a=3,b=4and f(a) =n!0 Gi) Ta) =2T) +0
Tn) = a 1(2) +f (n)
nb
call.
f(a) = Work done outside recursive oC a2) = o (als) =0 (0)
n'logn
Here, a=2,b=2, f(a) =0" and d=3.
ion of €= 0.2 Where, a =9,b=3 and f(n) = 3 baB=8
This includes cost of divis
thesolution.
Since, F(a) = 2(0"** ),with condition, ie.
2 9
_ log, = lB, ==o nit? From master theorem, as <b,
Tin) =8(¢9)=80)
problem and merging of regularity log,
is We shall check the
ni = ns =n w a 34 Exam Pack
ion using master method constant c < 1 and all suffic
ient
), with e=t Questions)
Solution of recurrence equat af(n/b) <c.f(n) for some Since, f(n) = 2(n?** regularity condition, i.e. (University an d Review
obtained as, large value of n. We shall check the
sufficient
constant ¢ < 1 and all tution Method
Variant I af(n/b) = 3(n/4)log(n/4) af(n/o) <c.f(n) for some §F Syllabus Topic: The Substi
= 314
d and 3¢q/4)log(n/4) < (3/4)alog m = c.f(n), with large value of 0.
Explain substitution method with examp
le.
If f(n) is the polynomial of degree é the condition 3 of master theorem,
and af(nfb) = 9(n/3)togw/3) a,
Thus, it satisfies with ¢ = 4
If ab 9(0/3)*log(n/3) S (1/3) a’logn = 3 c-f(a),
3.110 Marks),
1. Tin)= @(n'logn) hence of master theorem, and
(Ans. > Refer section

2 Te) = (nl) if aad! T(n) = 9 (nlog n) ‘Thus, it satisfies the condition «= Syllabus Topic: Recur
sion Tree Method

If a<bt hence
log n) Whats recursion trae? Describe.
3, Tin) = O00) Ten) = Oty =@ (n° @. )
tans, : Refer section 3.2) (© Mars
Ex, 3.3.3
Variant If Solve following recurrence
using master method.
tos Ex.3.3.7 Tn) = 3 Twa) + cn, and
1 Syllabus Tople + Maste
r Method

1 If £ (x) = 0( n!2 ) for some constant € >0, T(n) =4T (E)+9 Consider the recurrences.
e cis constant and nfs the
number master theorem.
T(n) =5T (ni) + rf wher bounds. Qa. Explain three cases of (May 2015)
then (5 Maris)
Soln.: of inputs. Find the asymp
totic
(Ans. : Refer section 3.3)
‘Soin. + with example.
T(n) =of n!8,) Compare this equation with
T(n) =a. T (2) +f£(0) a Explain masters method (May 2017)
a) T (n) =3T (3) +n (Ans. : Refer section 3.3) 6Marks)
2. If f(a) = ol n!8s.) then Where, a= 4, b= 2, f(a)
=n, d= 1, where d is the Compare givenrecu
rrence with equation with {Qec. 2018)
' £x.3.9.8 (5 Marks)
Tin) = ol n8etogn) degree of polynomial. ay =aT() +0)
are p= 2 0,d=1
= ln log, ), for some constant €> 0, Here, a= 3,b=3 and F(a)=
3. If f(a)
1, then
Here, a = bf
for some constant ¢< n).
and if a.f(n/b) $ c.f(n)
So, Tin) = @ (n° logn)= @ (a log
Ta) = 2 (f(m)).

PO

Scanned by CamScanner
analysis of Algoritims (MU - Sem 4 - Comp) 42 Dynamic Programmin:
- Cone abstraction
Contr i for dynamici programming
ing isi shown (iv) Travelling salesman problem
(¥) All pair shortest path problem
‘Algorithm DYNAMIC_ PROGRAMMING (P) (vi) Assembly line scheduling
if solved(P) then (vii) Multistage graph problem
retum lookup(P) It P is already solved, then
‘else rotriave stored answer 42 Principle of Optimality
‘Ans (-SOLVE(P)
store (P, Ans) State “Principle
of Optimality”. (2 Marks)
end What is dynamic programming approach to solve the
problem? (4 Marks)
Function SOLVE(P)
What is the principle of optimality in dynamic
programming 7 Give a suitable example for this
ly-ling
if eulficiently small(P) thea property of optimality in dynamic programming.
rtest path, all pair shortest path, Assemb solution(P)
Multistage graphs,
single source sho
n subsequence. // Find solution for sufficiently small problem £
General Method, problem, Long! est commo
0/1 knap sack , Travelling sal Jesman else Definition 5
scheduling,
Divide P into smaller subproblems P,, Py, -- Principle of optimality : “In an optimal sequence of
‘Ans, DYNAMIC_PROGRAMMIN(,) decisions or choices, each subsequence must also be
Ans, ~ DYNAMIC_PROGRAMMIN(?;) optimal”, 5
ively. Unlike divide ang)
Subproblems are solved recurs g are not) The principle of optimality is the beart of dynamic
ic programmin
Syllabus Topic : Gene
ral Method conquer, subproblems in dynam programming. It states thal to find the optimal solution
in dynamic programming of the original problem, a solution of each subproblem
independent. Subproblems
ons of subproblems ar also must be optimal. It is not possible to derive
General Method overlap with each other. Soluti Ans, — DYNAMIC_PROGRAMMIN(P,)
4.1 merged to get the solution
of the original large} optimal solution using dynamic programming if the
invented by US. Combine solutions of smaller sub problems in problem does not possess the principle of optimality.
problem.
Shortest path problem satisfies the principle of
programming was
— Dynamic y are independent order to achieve the solution to largar problem
Bellman in 1950. Like greed Jn divide and conquer,
subproblems
optimality. If A - X,- X;-... X,- Bis the shortest
mathematician Richard solve solved multiple times
algorithms, dynamic progr
amming is also used to and hence repeated problems are path between nodes A and B, then any subpath X, to X,
optimization problems.
But unlike greedy approa
ch,
Dynamic programmin g saves the solution in the tabh seturn(combine(Ans,, Ans, :.. , Ans,))
must be shortest. If there exist multiple paths from X; to
ensures optimal / best encounters again, th end path is not minimum, then.
dynamic programming always so when the same problem , and the selected
retrieved from the table. mi
Characteristics of Dynamic obviously the path from A to B cannot be shortest.
oO

solution. solution is 44.3.


programming is bottom-up approach. It starts solving Programming For example : In Fig. 4.2.1(a) shortest path from A to
Definition from B to C.
uses a solution of t Cis A-B-C. There exist two paths
that satisfies the smallest possible problem and of» Dynamic One is B — C and other is B~E-D-C. But
B - Cis
A feasible solution is a solution. smaller problem to build
solution of the largerproblem @. What are the — characteristics»
one in the final
problem has multiple (4 Marks) the shortest path so we should add that
constraints of the problem. When the with the j
Programming?
feasible solutions with different cost, theis solution Dynamic programming works on following principles + solution.
Limitations hand, longest path problem docs not
cost or maximum profit called * optimal those problems which i.e. build a On the other
the
minimum The method is applicable to only — Characterize structure of optimal solution,
satisfy the principle of optimality. In Fig. 4.2.1(b),
solution. lity.
of optima mathematical model of the solution. longest non-cyclic path from A to DisA-B-C-D.
. For sorting possess the property of principle — — Recursively define the value of the optimal solution. the C is just an edge
= Cost metric depends on the problem We must keep track of partial soluti
ons. of In this path, sub-path from B to
problem, cost metric may be a number
of comparisons
is more complex
— Using bottom-up approach, compute the value joining B and C. However, BC itself
is not the longest
a cost Dynamic programming optimal solution for euch possible
s. subproblem non-cyclic path from B to Cis
or number of swaps. For matrix multiplication, path because longest
"4 original problem sub path of the longest
For knapsack time-consuming. Construct optimal solution for the B-A-E-D-C. Thus the
metric is a number of multiplications.

principle of
using information computed in the previous step.
path may not be longest always, so violets
problem, cost metric is totalprofit earned. 4.1.2. Control Abstraction optimality.
4.1.4 Applications
4.1.1 the large pro? (8)
Dynamic Programming (DP) splits
Introduction
problem bee Programming,
State applications of Dynamic
What is Dynamic programming?’ “Is this: “the: every possible point. When the (3 Marks) (a) Oo @® ‘S)
@. 4 mass
optimization technique? Give reasons. What are
the sufficiently small, DP solves it.
used to solve optimization
it 4 Dynamic programming is
drawbacks of dynamic programming?
(6 Marks), Dynamic programming is bottom-up approach,ucls real-life problems such
G—@® ©—®
and constr problems. It is used to solve many
Dynamic programming is powerful design technique
the solution of the smallest problem @) b)
y as,
solution of the larger problem from alread
path
ming” of shortest and longest
for optimization problems. Here word “program (i) Make a change problem Fig. 42.1: Mlustration
problem
it does smaller problems.
refers to planning or construction of a solution, F a Knapsack problem
not have any resemblance to computer program
ming. To avoid recomputation of the same problem (iii) Optimal binary search
tree
the table. ' ‘i 4
into small the result of subproblems into
Divide and conquer divides the problem
subproblems.
time same problem encounters, the answer is
from the table by lookup procedure.

Scanned by CamScanner
-
- Sem 4 Comp)
Panaysis cof Algorithms (MU
pivide and Conquer Dynamic Progra
4.3 Elements of Dynamic Prog
ramming
No.Sr. | Braman PP analysis of Algorithms (MU - Sem 4 - Comp) 44
I repetition
hows of of | No repetition W Let x be a vertex auch that Gj, r) € E and off, +
Q. Discuss the elem ents of dynamic programming.
(aM jarks) at Work wat cost{r] is minimum
It splits input at gq, i costfj] < eff, ] + cost{r]
@. What are tha common in the dynamic
steps It splits input at a
programming to solve any problem? (6 Marks) specific point, every possible point.ich ang mii] <r
a

lend Sot successor of vertexj


Optimal substructure ; “For the optimal solution of Less efficient due to More efficient dye
— saving of solution, ‘0 the (Find minimum cost path
the problem, a solution of subproblem also must be pl}
optimal”, Dynamic programming builds the optimal Sometimes, a solution Usi,
solution of the bigger problem using the solution of dynamic programming ing t pik] <n
smaller subproblems, Hence we should consider on! ly simple. complex and tricky. 8 is for <2 tok-1do
those subproblems which have an optimal solution. . Onlyy one decisioni, | Many decision sequ se, pil rif - U)
- Overlapping subproblems : When the big problem 1511 9. | sequence is ] may be generated. ‘tenees
divided into small problems, it may create exponential generated.
subproblems. Only polynomial numbers of them are *F Complexity analysis [Vertex 6 is connected to vertices 9 and 10,
ing problems of If graph G has IEI edges, then cost computation time \Cost{[6] = minf{ ef6, 10] + Cost{10}, c[6, 9] + Cos{9] }
would be O(n +, IN). The complexity of tracing the
distinct. Fig. 4.3.1 shows the ove
the binomial coefficient. Dynamic saves Syllabus Topic : Multistage Graph = min(S + 2,6 + 4} = min{7, 10} =7
a inimum cost path would be O(k), k <n. Thus total time
solution in the table, so no rework is done. When
C(n — 3, r—2) problem encounters again, its solution is 4.5 Multistage Graph complexity of multistage graph using dynamic programming Vertex 7 is connected to vertices 9 and 10.
retrieved from the table. Divide and conquer solves
would be O(n + IE). Cost{7] = min{ c{7, 10] + Cost{10], o{%,9) + Cost{9]}
Cin — 3, r - 2) four times, whereas dynamic = min(3 + 2,4 + 4} = min{S,8} = 5
> (Dec. 16,May 17) Ex. 4.5.1
programming solves only once. There may exist many
subproblems with multiplicity greater than one. Qa. ‘Write a short note on Multistage graph. Find minimum path cost between vertex s and t for following a7] = 10

Eee eae as multistage graph using dynamic programming. ad


Vertex 8 is connected to vertex 10 and 11.
«) Q. What is multistage graph? How to solve it using| Cost{S] = min{ ¢[8, 11] + Cost{11}, c[8, 10] + Cost{10] }
dynamic programming? (6 Marks))
(a) )
— Multistage graph G = (V, E, W) is a weighted directed
Ne n-2] n-2) graph in which vertices are partitioned into k > 2
disjoint subsets V = {Vj, V2, .... Vg} such that if the
roa) (faa) (523) (3) 3) 28) edge (u, v) is present in E then wu V, and ve Vy,
1 £i<k. The goal of multistage graph problem is to
Fig. 4.3.1: Divide and conquer tree for ,C- find minimum cost path from source to destination
vertex.
4.4 Divide and Conquer Vs Dynamic = The input to the algorithm is a k-stage graph, n vertices Solin.:
are indexed in increasing order of stages. The algorithm Solution to multistage graph using dynamic
Programming programmingis constructed as,
operates in the backward direction, ie. it starts from the
@. Distinguish between dynamic Programming © and
last vertex of the graph and proceeds in a backward [Cost{j] = min{efj, r] + cost[r]}
divide and conquer technique. direction to find minimum cost path. Here, number of stages k = 5, number of vertices n = 12,
(6 Marks)
cost of vertex j © V; from vertex r € Vig, is
source § = 1 and target t= 12
Initialization: Vertex2 is connected to vertices 6, 7 and B. ;
defined as,
Costij] = min{cfj, r] + costfr]}
Cost{n} = 0 =>Cost[12] =.0. Cost{2] = min{ c[2, 6] + Cost{6}, ef2, 7] + Cost{7], of2, &]
hi
Dynamic programming is where, clj, 1] is the weight of edge <j, r> and cost{r] is pill =s=>pfl]=1 + Cost{8} }
= min{4 + 7,2+ 5,1 + 7} = min(Ll, 7,8} = 7
top-down bottom-up approach. the cost of moving from end vertex to vertex r. p(k] =t >pl5] = 12.
L {2} = 7 .
Algorithm for the multistage graph is described
below :
Dynamic programming rithm MULTI_STAGE(G, k, n, p) ‘ Vertex 3 is connected to vertices 6 and 7.
prefers iteration. # Description : Solve multi-stage problem using dynamic] (Cost{9] = c[9, 12) + coni{12] =4 + O=4 Cost{3] = min{ ef3, 6] + Cost{6], ef3,7 + Cost{7]}
and | Subproblems of dynamic programming in[9] = 12 11 Suecessor of vertex9 is 12 min{2 + 7,7 + 5} = min{9, 12} =9
sub | programming are dependent // Input: kz Number of stages in graph G = (V,
E)
are | and overlapping, oli, j): Costof edge (i, j) ICost{10} = c[10, 12] + cost[12] = 2+0=2
Mf Output: a
p[d:k]: Minimum cost path n{lo] = 12 If Successoofr vertex 10 is 12 Vertex 4 is connected to vertex 8. i :
Solutions oF Solutions Cost{4) = o[4, 8] + Costf8] = UA THI
of
subproblems are not are stored in the subpr
table.
oblems
Cost[11] =‘e[L1, 12] + cost[12]=5 +0 =5 nfs] = 8 zi
stored. pee
for e
j —n-Ltoldove Semana 7 n{l] = 12 /{ Successor of vertex 11 is 12

Scanned by CamScanner
4 COO)
PET anys ot igus (MU Som . Manas of Algorithms (MU - Sam 4 - Comp) 46
“Tp omne ctesed
wo vertic T and 8. multistage gtaph using Dynamic

Peedi , e5,8)+ Col


estcer+ 5,8 + 7+ 1)Cont’ 15) =15
Solution to
programming is const
ructed as, Stage 2 — Like Dijkstra, this algorithm is also based on the
principle of relaxation. The path is updated with better
= mintJ6, + cost{r]} Vertex 2 is connected to vertices 4, 5 and 6.
(Cost{j) = min(eli, 1] values until it reaches the optimal value.
ef}=8 Here, number of stages
k = \Cost[2] = min{ e[2, 4] + Cost(4], c[2, 5] + Cost[5], ¢{2, 6} — Dijkstra uses a priority queue to greedily select the
+ cost{6) +
sources = ] and target!
= min{3+ 7,2 + 5,3 +5} = min{9, 7,8} =7
closest vertex and perform relaxation on all its adjaceat
initialization: outgoing edges. By contrast, Beliman-Ford relaxes all
nf2]= 5
Cost{n] = 0 => Cost{9] = 0. the edges [VI -
time.
1 time. Bellman-Ford runs in Of{ViJEs}

pil =s= pH =2 Vertex 3 is connected to vertices 4, 5 and 6. [Algorithm BELLMAN-FORD (G)


pik] = t=> plS] = 9- Cosi{3] = min{ f3, 4] + Cost{4], ef3, 5] + Cost[5], <[3, 6] | Description : Find the shortest path from sourcevertex to all
+ cost{6]} other vertices using Bellman-Ford algorithm
+ 7,4
=min{ + 5} = min{13,9, 13} =9
6+ 5,8 iW Imput : Weighted graphG = (V,E, ®)
ix(3] = 5 w(u,¥) Weight of edge (1, )
(Cost{7] = el7, 9] + cost[9] = 7 +O=7 distance of vertex u from source
nf7] = 9 If Successor of vertex 7 is 9 successor of veries u
Stage 1
Vertex 1 is connected to vertices 2,3, 4 and 5. Cost[8] = c[8, 9] + cou] =3 +0=3
Cost(1] = min{ cf1, 2] + Cost[2], ef, 3} + Cost[3}, cf, 4] i{ Successor of vertex 8 is 9
+ Cost[4], c[1,5] + Cost{5]}
min{ 9 +7,7+9,3+
18,24 15}
min { 16, 16, 21,17} = 16
nfl]
= 2

Trace the solution '


inf c{1, 2} + Cost[2], e[1,3] + Cost{3] }
in{5 + 7,2 + 9} = min{12, 11) = 11
7
g{5] [6] [7] fe)f 9} [104 [11]; 0112] for each edge (u, v) € E do
a 8 1 1 Wii 12 12; 12 Stage 3 if d[u) + w (u,v) < d[v] then
Vertex 4 is connected to vertices 7 and 8. d[y] & d[u] + w(u, ¥)
Minimum cost path is: 1-2-7-10-12 Cost[4] = min{ c[4, 7] + Cost{7], c[4, 8] + Cost{] } fy] ou
= min{1 + 7,4 + 3} = min{8, 7} = 7 Minimum
cost path is: 1-3-5-8-9 end
In[4] = 8 end
Syllabus Topic : Single Source Shortest Path jend
Vertex 5 is connected to vertices 7 and 8.
Cost(5] = min{ ¢[5, 7] + Cost[7]J, c[5, 8) + Cost[8] } Single Source Shortest Path I Check for negative eycle
4.6
= min(6 + 7,2 + 3} = min{13,5} = 5 for each edge (u, v) € Edo
in[5] = 8 @. How to find single Bellman-Ford algorithm? (4 Marks) ifd{u) + w(u, v) < fy] then
Vertex 6 is connected to vertex 7 and 8. Q. How to solve single source shortest path problem error “Graph contains negative cycle”
Cost(6] = min{ c[6, 7] + Cost[7], ¢[6, 8] + Cost[8] } using a Bellman-Ford algorit'im? (4 Marks)
Cost of the path is: 9+2+3+2=16 Bellman-Ford algorithm is used to find the shortest
= min{6 + 7,2 + 3} = min{13, 5} = 5 —
Ex. 4.5.20 Tar Cah n[6] = 8 path from the source vertex to remaining all other
vertices in the weighted graph.
Findn @ minimum cost path from 1 to 9 In the i can
using dynamic programming. —— Itis slower compared to Dijksira’s algorithm but it
Sete handle negative weights also.
3
— If the graph contains negative-weight cycle (edge sum
find the
of the cycle is negative), it is not possible to
of cycle
minimum path, because on every iteration
gives a better result.
cycle in
~ Bellman-Ford algorithm can detect a negative
for such
the graph but it cannot find the solution
graphs.
Fig. P.4.6.1

Scanned by CamScanner
4-7
(mu - Sem 4 - Comp)
PF anatysisof Shortest path from any vertex can
Soin. : . | from the destination vertex and following - Dynamic Programming *
back For example, startin
to the source. Panaysis of Algorithms (MU - Sem 4 - Comp) 4-8
source vertex 10
Initialize the distance: Set distance of
and every other vertex 10 ©. * Ye
ula =2,02."=4, vde= 5 the shortest path," Iteration 1
{5.4,2,}}. Edges <3, 5> and <3, 4> will be relaxed as their
Graph contains negative weight edges, but it does not
distance will be updated to 8 and 3 respectively. contain any negative cycle.
Negative cycle check Initialize the distance : Set distance of source vertex to
We have to test following condition foreach og Oand men other ates ne to~.
if d{u] + w(v, v) < dv] then
error “Graph contains negative cycle”
end
ul.d+w(1,3)<v3.d > 64624
Iteration 1
yd
+ w(1,4) <vyd= 6 +322
Edges <1,2>, <1, 3> and <1, 4> will be relaxed as their
ty,.d+ w(2,1) <v.d > 34326 distance will be updated to 6, 5 and 5 respectively.
Iteration1 uz.d+w(3,4) <vd> 3+222
Edges <5, 2> and <5, 4> will be relaxed as their ugd+ w(4,2) <v.d 24123 7 - &)
distance will be updated to 4 and 2 respectively.
@
upd+ (4,3) <v3.d> 24123 avy | [7 |- | 343 “oo
us.+ W(5,2) <r.d> 04423
ts. + w(5,4) <vy.d > 042>2 Iteration2
Badges <3, 5>, <4, 5> and <5, 2> will be relaxed as their
Goh--€8)
distance will be updated to 5, 3 and 10 respectively.
o ©.
None of the above conditic is
not contain any negative cycle. ons satisfied, so graph dg
Ex, 4.6.2 fi! md) 10 Marks
Iteration 2 Find a minimum cost path from 3 to 2 in the given
_ Edges <2, 1>, <4, 3> and <2, 4> will be relaxed as their using dynamic programming,
distance will be updated to 7, 3 and 3 respectively.
Iteration 2
Path cost after second iteration is shown in following
diagram
divi | =| 10] 0 | 3 [5
av |? [5 |-[3|4
Oe Iteration 3
Iteration 3 Fig. P. 4.6.2 ; No updates in weight
Shortest path from any vertex can be found by starting
from the destination vertex and following its predecessor
Initialize the distance: Set distance of source vertex to0 ns back to the source. For example, starting at vertex
and every other vertex to co, 1, v2.0 = 5, uS.n = 4, u4.n = 3 = the shortest path from
veriex 3 to vertex 2 is {3,4,5,2}.

Ex. 4.6.3
oO

Solve the shortest path from source: 1 ‘for following graph.


using dynamic programming. Iteration 3
Iteration4
updated to 3,
No edge relaxed
Oo

So the final shortest


3 Path from source vertex
5 is:

Fig. P. 4.63

Scanned by CamScanner
49
se Principle of optimality 4-10 Dynamic Pr in
Pranayeis of Algorithms (MU - Sem 4 - Comp)
path from i to j, the
If k is the node on shortest Tnnermost loop has only one statement. The complexity of D'2, 3) min (D'2, 3}. D'(2,2)+D'(23}
also be shortest. Ip Fig
from i to k and k to j, must
P of summation i an) that statement is @(1). min {2,0+2}=2
itojis either
optimal path from FY DG. min (D'[3, J. D'G. 21+ D'L2, 11}
matrix is initializeq wit Running time of the algorithm is computed as +
pz In first step, solution '
Iteration 4 graph matrix. noo a non 1 min (3,7+ 6) =3
min (D'(3. 2), D'3, 21 + D2, 2)
Yews LT La= re D'B3,21
Iteration 5 Tia) = min (7,7 +0} =7
j=1 k=lisl k=l
[ole]
ke=li
Iteration 6 = 0@)
5
in weights in iteration 4,
There won't be any change ‘Thus, floyd's algorithm runs in cubic time. b
{s{ol2|
{7}0}
and 6.
: x. 4.7.4
No edge relaxed
Sis:
So the final shortest path from source vertex Solve the all pairs shortest path problem for the given graph. Iteration 3 (k= 3)
8
DL.2) = min (DU, 2), DLL, 3} +3, 2)
Fig. 4.7.1 min (4,647) =4
We update the distance matrix by considering one D'I.3) = min (DU, 3.31 + DB, 31}
min (6,6+0} =6
cone all vertices as intermediate vertices. In iteration j, on
vertex is used as an intermediate vertex in the shortest Path, D'R. UW = min (D'2, 1, D°2, 31+D3,1)
When 2 k” vertex is under consideration, we alreas min {6,243} =5
have explored I tok ~ I vertex as intermediate Verte D'2.3) = min (D'12, 3}, D412, 31+ DB, 3]
While considering k"' vertex as an intermediate vertex, ther " Soln.: min (2,24+0} =2
are two possibilities : Optimal substructure formula for Floyd’s algorithm, DB = min (DBD. +D. 1)
If k is not part of the shortest path from i to j, we keep] pi gg) = min( DEG i.DA GK + Doi) = min (3,04+3}=3
the distance Df, jJ as itis. DR = min (D3,21, D'S, 3} +03, 21
If k is part of shortest path from i to j, update distance = min (7,0+7)=7
Shortest path from any vertex can be found by starting
Dfi,j] as Dfi, k] + Dfk, j]-
from the destination vertex and following its predecessor 1's Optimal sub structure of the problem is given as :
back to the source. For example, starting at vertex D'fij) = min( D*~' i,j), D'"' K+ DS" kj} Iteration 1 (k= 1)
7 ula = 5, uS.a = 2, u2.n = 3, u3.0 = 4, ud. = 1 = the D* = Distance matrix after k” iteration D'(1,2] = 21, DL, 1+D0.21}
min (D°L,
shortest path to vertex Lis 1-4-3-2-5-7 This approach is also known as Floyd-warshall =4
= min [4,044]
D'[L,3) = min (DL, 3), Dt, 4D,31 F Ex. 4.7.2
shortest path . Algorithm for all pair shortest path
Syllabus Topic : All Pair Shortest Path (APSP) problem is described below: min {11,0+
11) =11
Apply Floyd's method to find the shortest path for below
D'f2, 11 min {D°(2, 11, D2, 1+ DL, 1} mentionad all pairs.
4.7 _ All Pair Shortest Path
Algorithia FLOYD_APSP (L) min {6,6 +0} =6 123 4
D'2.31 = min (D2, 31, D2, 1) + DLL, 3) } 1) O#7
3 @&
= min (2,6+11)=2
D'B, 1] = min ID, Uy DB. n+D%, uh
2| 2 0 » »
Q. Explain all pair shortest path al Wm. with @ suitable DeL min (3,3 +0} =3 3| 2701
example. EE ET
for k —ltondo min (D3, 21, D°3, 1) + D°t1, 21} 4|_6= = 0
@. Write Flayd's algorithm for all pairs shortest path and D'B, 2]
find time complexity. for i—ltondo min (2, 3+4 Soin. :

53 Problem Optimal substructure formula for Floyd’s algorithm,


forj—1tondo
Let Gav, E> be a directed graph, where V is set of Dklij) = min (DGD. MOD
vertices and E is set of edges with nonnegative le; DGi,jJ* — min ( Df, J", Dfi, ky"! + Diy 123 4
the shortest path between cach pair of end ;
noes. net nd
L = Matrix, which gives length of
each edge Iteration 2 (k = 2)
Li 9,ifi =j 4 Distance of node from itseis D'L.2) = min (D'(,2), D'E, 214+", 2)
lf
zero = min (4,440) =4
Uijl = ifiejani jek Dag] = min (D'u, 3), DL. 2+ D'?2,3)}
. = Complexity analysis = min {Ul,442}=6
Uijl = W Gj) ifiej and (i, Dek Iteration
1: k=1
It is very simple to derive the complexity D'2,1] = min (D'2, 11, D'R, 2+D'P, D(a) = min (D°0, 20° UL 14D" 0,21)
of LS = min {6,0+6}=6
the weight of the edge (i, j) approach from above algorithm. It uses three nested 00PS:

——=<—__

Scanned by CamScanner
FE} anatysis of algorthns (MU —Sem 4 Comp) = min (D' 4,3), D (4,2) 4 pr
= min {= 0+ =) = min{%<+5) = 9
D'U,3] = min{D°(1, 0 y+D°(1,3)}
[eManatysis of algorithms (MU - Sem 4 - Comp) 412
= min (3,043)=
D'p,4] = min {D"[1.4.D° th 1]+D? 1.41) D‘(2.3] = min { D123]. D°(2,414D° [4,311
= min [5,6+9) =5
i ppt) = min { D°2, 1), 0°72. 0+D°0, 1) D‘(2,4] = D*[2.4)=6
= min {2,2+0) =2
p'(2,3) = min { D"[2,3),D°(2, 1]+D° (1.31) DA (3,1) = min { D? (3,1), D? (3, 414+ D4, 1)
= min (~,2+3) = 5 = min {9.1+6) =7 3
D!(2,4] = min ( D°{2,4}, D°[2, 11+ D°U1, 4) } Iteratioin 3 (k= 3): D‘ (3,2) = min ( D*[3,2}.D*[3,
41+ D° (4.21 } Mig P.473
min {~,2+=] = 0 = min (7,1+16) =7 Soin.:
D'(3.1] = min{ D°/3,1},D°(3, J+D°0. 1) Dn. 2 = min( D'11,21,D7
11, 374p2 B,2 D'B,4) = DB.4)=1 Initial matrix D® for given graph is shown below :
= min (00, 0-400) = «0 = min (,3+7}
= 10 0 1
i D'13.2) = min { D°[3,2},.D°3,1+D°0,
21] D'4,) = D4, =6
= min (7,000) = 7 D't.3] = Dt,3}=3 D'4,2] = D’ [4,2] =16
D' {3,4} = min ( D° [3,4], D° (3, 1]+D°(!,
4) Dii,4) = ain (DP. 4.01, 3)4 Da, 9) [4,3] = D3] =9
= min (1,240) =1 = min {o,3+1) = 4 Final distance matrix is,
D't4. 1) = min { D°(4, 1], D° (4, 1)+D°UL
1)
= min (6,640) = 6 D*?2,1) = min {D'2,1,D'2,3]+D? fa, y)
D' (4,2) = min
{ D° (4,2), D°[4, 1]+D° 1,2) } = min (2,549) = 2
' min {%, 6 400) = 0
D’(2,3) = D (2.3) = 5
D'[4,3] = min ( D° (4, 3), D°[4, ]+D° 1, 3]}
= min {~,6+3) =9 D’2,4] = min ( D? (2, 4), D? [2, 31+D* 3,47) Optimal substructure formula for Floyd's algorithm,
min {2,541} = 6 Ex. 4.7.3
D'tj) = min (D' GAD B+ DY KH)
Values of matrices are shown below.
D’ 23,1) = DB1=9 Suppose Floyd-Warshall's algorithm is run on the weighted,
D? [3,2] = D33=7 directed graph shown belw, show the values of the matrices
that result from each iteration in the algorithm.
073, 4] = D4) =
D4, 1) = min ( D? (4,1), D? (4, 3]+D? (3, 19)
= min (6,9+9) = 6
lighted cell is an D'4,2) = min { D? (4,2), D? 14, 3) +? [3,2))
improvement over the original matrix.
Iteration 2 ( = min (,9+7) = 16

1.2] = D' 1,2] = D'(4,3] = D'f4,3] <9


D'E.3)= min( D'(1,3),D' (1,274! 2.3)
5 = min {3, 5)
D'Il4) = mint 'p, 00 24D! 2.4)
= min {22,204 00) = .
Dan = D'p,y=2
P23) = D'ia.3) = s
Dina} D'12,4) =
D Iteration 4 (k = 4:
Bil == minminidl, oie, 24D! Hd2,1
{= 74.2) = D*[1,2] = min {
D’[1,2), D*(1,
4] +? (4,2) }
rea = D'B.y=7
Bais min (D3, 4) p! i a's min {10,4 +16) =
Diy, mh T +e) = n dD‘, 3) = min ( D?(1,3), D*[1, 4] +D* 4, 0)
= roe 2 1] + D 1 (2,1)} min {3,449} = 3
D4) = D,4)=4
D'2,1) = min { D°(2,1),D* Iteration 4: k=4 Iteration
5: k=5 Iteration 6: k= 6
(2, 4)4D*[4, 1)}
min {2,646} = 2

Scanned by CamScanner
- Sem 4 — Comp)
[(Panaysis of Algorithms {MU

Ex. 4.74 Aanatysis of algorithms (MU - Sem 4 - Comp) 414 Dynamic Programming
given
CGbtain all pair shortest path using Fioy6's Algorithm for complete the manufacturing as quickly as possible. ‘We can derive the conclusion for assembly line 2 in
weighted graph. Some penalty of time t occurs when the product is simil: lar way. So mathematically we can formulate the
transferred from assembly | to 2 or 2 to 1. problem as :
Iteration 3 (k = 3) Problem For j>1:

3
D'1,2) = min (DU. 2. DU, 31+ D3, 24) Determine which station should be selected from fy = min, G-MN+a, GG -1W4+G31 +34)
min (8, 5+ 1}=6 assembly line 1 and which to choose from assembly fog)= min (f, = +1) 4+ ay, UM +35)
DLL. 3] = min (D113), DT,3] + D713, 3) ) line 2 in order to minimize the total time to build the Exit time from assembly line 1 and 2 are x, and x,
= min (5,5+0 entire product. respectively. Thus, minimum time to take away the product
Selecting stations by brute force attack is quite from assembly line is :
Dip, = min (D'P, 1,D°2, 31+ D3, 1) infeasible. If there arc n stations, unfortunately, there P= min(f in +x,,f lol+x)
Fig. P.4.7.4 = min (2,74+3}=2 are 2° possible ways to choose stations. Brute force Algorithm for assembly line scheduling using dynamic
Soin.
D'2,3] = min (D"2, 3], D2, 3] +", 3) ) attack takes ©(2") time, it is not acceptable when n is programming is described below =
Optimal substructure formula for Floyd’s algorithm,
= min (7,7+0}=7 large. [Algorithm ASSEMBLY _LINE_SCHEDU! ILING(n, e, a, 1, x)
D'fij] = min{ D*'fi,j], DY", +" [kj]
Di3,1) = min (D"13, 1), D'[3, 31+ D3, 11) General architecture of two assembly lines with n 4 2 is number of stations on both assemb| ly
= min (3,0+3}=3 stations is shown in Fig. 4.8.1. Terminologies are |\/e is aay of entry time on assembly

Erste] D'G.2) = min (D°G, 21, D3, 3) +73, 21 } explained here: 2 is array of ussembly time on given station
2 = min (1,0+1h=1 © — Sy = Station j on assembly line i. / t is array of the time required to change assembly line
oO a= Time needed to assemble the partial ||/ i array of exit time from assembly
[ei
]o | component at station j on assembly line i.
f, Mere, +a,,
42 Sia 4.
Iteration 1 (k= 1)
f, [I] e, +a,
D't,2) = min (D%, 21. DL, 1) + D1. 2) } for j—2tondo
= min (8,0+8)
=8 Hi G-N+a,shb-N+y ta, then
Dil,3) = min (D1, 3}, DY,
11+ DT, 31} Syliabus Topic : Assembly-Line Scheduling
KW G-N+,
min (5,0+5) =5 ile
j DR = ain (D'P, 1), D2, + DT, 1} 4.8 Assembly Line Scheduling else
= min (2,2+0)=2 LP -H-N+yi+a,

D'2,3) = min (D7, 3), DP, 11+ DU. 31) Q. - Explain Assembly line scheduling. (7 Mariay Lfe2
end
= min («,2+5}=7 ~ Assembly line scheduling is a manufacturing Fig. 4.8.1 : Architecture of assembly line
problem.
D'G.U = ain (D3, 1,043, 1 +D. NY In automobile industries, assembly lines
transfer parts from one station to another.
are used tp © tj = Time required to transfer component #£,§-M+,,< G-U+t,, +o, ,then
= min(>,2+0j=0 from one assembly to other from stationj to £H-E0-N+a,,
Manufacturing of large items like car,
D'3,2] = min (D%3, 2}, D3, 11+D%, 2) } generally undergoes through multiple
trucks ete G+b. Lbl-2
= min {1,1+8}=1 stations, where © —_& = Entry timc on assembly line i. else
each station is responsible for assembling fe NG-N4+t, +a,
particular
part only. Entire product will be ready after it goes © —_x,= Exit time from assembly line i.
through
| Lile1
predefined n stations insequence.
For example, manufacturing of car may be
"= Mathematical formulation end
several
done in The time required to build component on the first if f(a] + x, Sf, [n] + x, then
Stages like engine fitting, colouring, light station of both lines is a summation of entry time for the Fees fa) + x1
fitting, fixing of controlling system, gates,
seats and particular assembly line and assembly time on the first Lee
Df1.2) = min (D't1, 2) D'g,21+D"p2,2)) many other things.
station of a particular line.
= min (8,840)=8 The Particular task is carried F¥ effa] tm
dedicated to that task only. Based an
out at
station the f,G) = © +0, ; Initial condition (ifj = 1),i=1,2
‘mia [D'[1, 3), D'f1, 2) +" [2, 3) } requirement, there Minimum time required to put the partially assembled Lte2
: min {5,847} =5 may be more than one assembly line. end
product out of any stationj on line 1 is minimum of:
In case of {wo assembly lines, if
DRY = min (D' (2,1), D! (2,2) +D' 2,1) assembly
the load at stationj of! 1. (Minimum time taken up to station j — 1 on same
= min (2.0+2}=2 line 1 is Very high, then components
aft assembly line 1) + (assembly time on station j of same Above algorithm will just tell us the minimum time
DP2,3) = min (D* (2,3), D' transferred to stationj of assembly line 2, the required to take away product from station n. It does
(2, 21+ D! p23] } conversé assembly line)
» min (7,04 7}=7 .is also ;true. This helps to speed
speed up the manufacturing. not tell anything about the sequence of stations on
y 2. (Minimum time taken up to station j — 1 on assembly
8.1) = me 7 1D! assembly. We can find the station sequence by tracing
2]+D! 2, 1} line 2) + (assembly line transfer time) +
= min {2,142} 23 (assembly time on station j of line 2)
the solution using the following algorit
D*13,2) Algorithm PRINT_STATIONS(1, n)
min (D' (3, 2}, Bed, 2)
in .
ine "i, station "n

Scanned by CamScanner
4-15
Som 4.- Come)
[FTanatyals of Algorithms (MU - Compute fast
est path
for j—. o.down
to 2 do ]2{3/4 5| 6] AT analysis of Algorithms (MU - Sem 4 - Comp) 4-16 Dynamic Programming
y
im WT at min (18 +3, 164 1+3)=20 capacity of the knapsack and the total value earned is as
olf i ft [2] 2p F131 = min (21+ y+ a 12] +259)
print "line "i "tation "j—1 large-as possible.
Lo) |} 2! 2/242 = min(1B+3+6, 16+6)=22
Forj=4 — The knapsack problem is useful in solving resource
Ex, 4.8.1 h allocation problem. Let X= < xy. %. X3y
Trace back the pat £4) = min(6, (3D + a4, 5 [3] + by + ad the set of n items. Sets W = <wy. Wa. Ws,
las S00 tng
~ [*= 1, so we have to select min (20 +4,22+244)=24 V = <V Vay Vin so Vee are weight and value
1, so we wi I Selegs £4)
Now we will look at 1,[6], which is min (6; (3) + thy + agg, fy £3] + an)
associated with each item in X. Knapsack capacity is M
the previous station on line I that is
Sis
station = min (20+ 144,22
+4) =25
unit. The knapsack problem is to find the set of items
Now we will look at ls which is 2 so we wil change Forj=5 which maximizes the profit such that collective weight
the line to 2 and previous station that is S, ,. £6) = min [4] #a,5,6 (4) + ty, ays) of selected items does not cross the knapsack capacity.
we have to remain oni min (24 +8, 25+2+8)=32
Next, observe |,{4], it's 2 so — Select items from X and fill the knapsack such that it
5. Now observe 1 8
See same line and select station S,
(5) = min (f, [4] + ty, + dps f, [4] + a5)
1, Noy would maximize the profit. Knapsack problem has two
which is 1, so move on station S,2 on Tine = min(24+345,25+5)=30
variations. O/1 knapsack, that does not allow breaking
Soin.:
Gj = 1, i= 1, 2)
+41; Initial condition
observe 1,[3], which is 1, so remain on the same ling, Forj=6 of items. Either add an entire item or reject it It is also
FG) = ¢
Final path is shown in Fig. 4.8.1(a).

CR
- Wray G-M +b) +a) FO = min, Stag & Sths+ a0 known as a binary knapsack. Fractional knapsack
hol
AG) = min (f, G- Ut y+ ay fl M tay) HK—P)—D—9—H—7]
= min(32+4,30+144)=35
£,(6) = min (0, [5] + tas + age fy [5] + 24) allows breaking of items. Profit will be eamed
fe = min(f, [n] +x, f [ol+x;) = min (324+4+7,30+7)=37
proportionally.
al = +2=6 f = min(f, (a) +x, fn) +X) Fs Mathematical formutation
yl] = etd =2+6 =8 where n = number
of stations = 6
f(2] = min (f,G—1) Fay G- 1+ by rt ay) x, and x, are exit time from line 1 and line 2 ‘We can select any item only ones. We can put items x;
= amin (F, [1] + ayy F (1) + ty #42) respectively. in knapsack if knapsack can accommodate it. If the item is
Fig, P. 4.8.1(a)
= min(6+8,8+3+8) x = 3. x =2 added to a knapsack, associated profit is accumulated.
= min (14, 19) = Ex. 4.8.2 f = min (f,16]
+x) f, 16] +%,) n
Gla] = min (Fj — 1) + 41+ Ay GU - 1+ ap)
given assembly [ing
= min 35 +3,37+2)=38 Objective is to maximize 2. = x, v,, Subjected to
= min (f [A] + by) + arf (1) + a2) Find an optimal solution for
configuration. fi] [9 | 18 | 20 | 24 | 32 | 35 i=l
= min (6+3+411,8+11) =min(20, 19) =19 é é 6 a f(j1| 12 | 16 | 22 | 2s | 30 | 37 | m= 38 n

FECECEE
Ala] = min (G1) +4, GU -N+G))+ay) Zxyw,s M
= min (f,[2] + 4,5. & [21 + bog + ayy ) 1 fil2|3[4[sle
i=l
min (14 +9, 19 +449)
min (23, 32) = 23
min (1) = 1) +41 + ay GG - 1] +)
Wil Top]
lt
Where,
v, = Profit associated with i* item
= min (G{2} + y3+ gy, fy [2] + 059) Lj] 2jifala|r y, Weight of i” item
Capacity of knapsack
= min(14+1+2,19+2) Sa Sa Sa Da
Syllabus Topic : 0/1 knapsack
Fraction of i” item
= min (17, 21)= Fig.
P. 4.8.2 = (0,1) + 0/1 knapsack
= min (GG - 1+ ay & G14 ty tay) = (0, 1] — fractional knapsack.
= min (13) +4,,.f, +t; +a.) Soln.: 4.9 0/1 Knapsack ' We will discuss two approaches for solving knapsack
= min(23+3,17+1+3)=min
(26,21) fl] = ¢4a,, fori=i,2 using dynamic programming.
> (May 14, Dec. 15)
= 21 Here, i indicates assembly line, i.e. | or 2,
G4] = mini G11 +45_,+ 4,5 0-M+a,) 49.1 First Approach
GM = eta,
= 24+7=9 Explain 0/1 Knapsack Problem with an example.
min(f, (3) + 6), + aay, f [3] + a4) E,[I) = tay =4+8= UST VENUS}
@Q. ‘How to solve knapsack problem’ set Snee
= min (23+2+2, 1742) Forj > 1, wherej indicates station numbers. Q. Explain 0/1 Imapsack problem using « dynami ic programming?
programming. TEE en
= min(27, 19) =19 FG) = min (,G- 1+ ay fG-U+tyj-1+
ay) Q. Define the knapsack problem: How to solve it using If the weight of the item is larger than remaining,
Hence we can summarize fyg and fy) as follows AU = min G- +t,j_, +a,
BU - 1 +aq) of the
dynamic programming?: (5 Marks) knapsack capacity, we skip the item and solution
Forj=2 add the
previous step remain as it is. Otherwise, we should
612] = min (F,[1] +a), {1} + ty +a) 5 Problem item to solution set and problem size will be reduced
by the

f = min, fn) +x, fy [a] +x)


3 G02) =
min
(9 +9, 12+24+9)=18
min(f, 1] +,+a,,6
min(9+2+5,12+5)=
[I] + ay)
- Given a set of items, each having different weight and
value or profit associated with it. Find the set of items
weight of that item. Corresponding profit
| the selected item.
will be added to

FF = min(25 +3, 29+7)= min (28, such that the total weight is less than or equal to a
36) =28 For j=3

f.B) = min 12) +a). 6 (2) + ty + ay)

Scanned by CamScanner
Sem 4= Comp)
[BP anatysis of Algorithms (MU-
m into sm: all
Dynamic programming divi ides the proble solution of ET anatysis of Algorithms (MU - Sem 4 - Comp) 4418 Dynamic Programming
subproblems. Let V is an array of the problem size Boundary conditions would be V (0, j] = Vii, 0) = 0. + VOL) = Vi0.31=0
subproblems. V[i, j] represen! 1s the solution for Initial configuration of table looks like, V2.3] = i=2, j=3, maw,6
.
J with first7 items. St As, j<w, Vij) = Vii- 10)
is given a
‘Mathematical notion of knapsack problem Item detal! 0 2) 3)4)5 V(2.3) = VU1,3}=0
0 ifi=Oorj=0 ko 0 O}Ojajojo jojo
na Viet jew et |ms4 w=10 0 = Vii-ui
Vii DV —%
max (Vi, vit VOl— 1, j- wt jew joi-vll W=6 v= 12 0
V[1....0, 0... M1: Size of the table SW<SW +wfi] W3=8 Vg= 15 0

a
i vin, ” Solution SP +-SP +v[i]
Filling first column, 1
= Number
of items
VU, isl j=lwew=4
algorithm for binary knapsack is described below: As, j<w, VG) =VG-19
[Algorithm DP_BINARY_KNAPSACK (V, W, M) = Complexity analysis - VOM V (0, 1)=0
[/ Description : Solve binary knapsack problem using dynamic) With n items, there exist 2" subsets, brute fore V[2,1] = i=2, j=1w=w, = 6
programming approach examines all sub sets to find optimal solution, As, j<w, V(ijl =Vl-1j) oe “i
[/Loput : Set of items X, set af weight W, profit of items V2 Hence, running time of brute force approach is O(2"). Thisi » VM = Vfl, =o
Vila)
= i=}, j=4, mews 4y= 10
knapsack capacityM unacceptable for large n. V3.1) => i=3, j=
As, j2, V (i, l= max (V fi-1jh.y+VG-bj-wd)
As, j<w, VJ) = Vi-1Li
Dynamic programming finds an optimal solution by . VBA = ViZu=0 = max {V (0,4),
10+ V {0,0))
7 Ourpue
: Array: V, which holds the solution of problem constructing a table of size n x M, where n is a number of —_—- Vil,4) = max (0,10+0) = 10
items and M is the capacity of the knapsack. This table cay tom detall ale 2|a}a]s peck. VIA] = i=2 jd. maw,= 6y2=12
for i¢— 1 tondo be filled up in O(nM) time, same is the space complexity,
Vii,
0] — 0
Running time of Brute force approach is O(2"),
0[o/o[o[o]o[ofa}ololo| As, jew, VILi) = Vi-LF)
lend i w= t0}0\0 | | V2.4) = V{14)=10
for ie— 1 to M do Running time using dynamic programming with)
memorization is O(n * M). v2=12]0]0 LU | V3.4)
= i=3, §=4. wawy = Bev 15
Vo, J-0
end vs=150]0 As, j<w, Vij) = VOG-Li
Ex. 4.9.1
Filling second colurnn, =2 2 ¥BA) = VP.Al=10
for i<— 1 tondo Consider 0/1 knapsack problem number of items N = 4,
Solution after adding i” iter = (4, 6, 8), p = (10, 12, 15) using dynamic programming VUL2) i=l j=2, W=w,= 4
for j 010 Mdo device the recurrence relations for the problem and solve the As, j<w,. Vinil = Vi-Mi
ifwfi] <j same. Determine the optimal profit for the knapsack of « VO0,2] = V{0,2]=0 i
Vfi,i] — max {VEi-l, j), vf] VG - 1,j- will} capacity M = 10. 2.2] = i=2, j=2. w=w,=6
else Soln.: As, j<Wy Vij) = VG-hi
Vi Vi-1,j] Here, knapsack capacity is sufficiently small, so we - » V[221 = VUL2=0
b>;
i item is not feasible, so use the first approach to solve the problem.
end V1.2] = 1=3, J=2 Wawa 8 Filling second columa, j= 5
select old solution Solution of knapsack problem is defined as, As, i<™% Vii, Vii-1i
V[L.5] > i=],
jes, yew =
0 iti=Oorj=0 3 . VB,2 = V27=0
Above algorithm will just tell us the maximum value Vitl= Mi-1.i ifj<wi | As, j2 wy VGjl= max {VG-1.jl4+Vb-1j-w))
we can cam with the dynamic programming, max (V 10, 5], 10+ V (0, 1}
It does not max (V{i- 1, jbvi+Vii~ 1, j- wil iftj=w
Speak anything about which items should be = max(0,1040)= 10
selected. We
can find the items that give optimum result using
the
We have following stats about problem, i
20 | [a}olofojolafa[ofololo| oe
following algorithm. Table P. 4.9.1 .) 1) fia[wr=afr=rolofolol [TTT | arin ree ae -1,“ "
Algorithm TRACE _KNAPSACK(w, v, M)
Mw ts arcay of weight ofn items Item | Weight (w) Value (¥,) ;
i veal=r
TA s, j<wp,
ts
Vii) =

Uy is array of valve of n items


\ | 4 Flin exoaicciamnl 3 A ad
//Mis the knapsack capacity
L | 6 vl 32 ish, ja3mene4 As, jxme VED = VOOR
, + VBS) = VI2,5]=10
Luis 8 [os | As. j<w, VE] = VGi-bi

Scanned by CamScanner
419 Dynamic
A analysis
of Algorithms (MU
= Som 4 J As, 52% Vl jJ=max (VO-1§)
4+ VE jo
wip
=max (V[0, 8}, 10+ V [0, 4])
V[I,8] =max
(0, 10+0) = 10
qT analysis of algortims (MU- Sem 4 Comp)
-
V [2,8] = i=2 j=8, w= 4) =6,=12 AS, j2% Vij =max (VUi-Ljly+ VC-Lj— wl
As, j2) Vf j]=max (VGi-1.ih
y+ Vfi- = max (¥ [2, 10], 15+
V (2, 21}
=max (V (1,8), 124+ V1, 2)) hi-wg) VIS, 10) = max (22,15
+0) = 22

moses (Ublslsblolsba| | aceilhlctaelsl eal


Vi2,8] = max (10,12+0)
=12 1
Filling second column, VB,8) > i=3, j=8 w=, =8,v9=15
VEL6
=> i=l, j=6 w= As, j2 wp VEi,j)=max (Vfi- 1 jh ¥+ VG -1joy,
vl blll lee olole) | afeslsalil fala aaa
‘[ts=incolblldofelaelelael | [aleaansblldele al lala
As, j2w, Vfijl=max {V [iL jh +VG-1i-wd b = max (V [2, 8). 15+
V [2, 0]}
= mux [V (0, 6], 10+ V (0, 2J} V3.8) = max (12, 15 +0) = 15
V{I,6] = max(0,10+0) = 10
V2.6) = i=2, j=6, w=,= 6,¥,=12 bn] We =6| ve=12]0}0 0 a|s0 10! tale 12| Vij} = Vil. 10}=10

As, {2 V fi jl=max (VEi-1, jb ¥j+V li-Lj-w)}


dl 1:3 |ws=8|vs=15]0]0}0{0|Jo| 10] | 10) | 12] | 12| 15. Vii-1.i1 = V0, 19)=0
Viail #. Vii 1.il-20 add item & =1) in soliton
= max (V [1,6], 12+V 1, 0])
V[2, 6} =max (10, 12+0)= 12 [nw
= 10 of [0 of Find selected items for M = 10
Step1: Initially, i=n=3,j)=M=10
set Reduce problem size j by
j = j-we=j-w=4-4=0
V3.6) 3 1=3, j=6, w= Wy = B vy =15 [iz|ws=8] v= 12{0[0/0]0] 10/0] 12] i Solution Set
As, j<m, Vibil = VEi-Lj
« VB,6] = Vi2,61=12
[is] m =a) v= 15[0/0/o[o[ro}10]2]
Filling second column, j = 9
vomacat_[ol fle
fs Jefe [oo S= Ub}
Problem size has reached to 0, hence the final solution
ia ojo}olajo
|o jo Jo \o |o |o isS= (UL
—+ i VUL9 => i=l, j=9 w=w= 4v=10 Eamed profit =p1 + p2= 104 12=22
[T wemdetat [ofi[2[a]«
[5 [s [7]o[a] As, j2w, VG, j]=max (V[i-1,
jl v,+V Gi-1,
si wl} tet [r=4] r= 10 alo aol 10 10|10 40 10] 10 10)
fof TT = max(V (0, 9], 10+
V [0, 51) --| Ex. 4.9.2

VU,9] =max (0, 10+0) = 10 i-2|wo=6|v2=12/0]a|o}0 1o|s0] 2 4212] 1222} Find an optimal solution
for following O/1 Knapsack problem
V[2,9] > i=2, j=9, w=w, = 6,v.=12
using dynamic programming : Number of objects
i=3]W=8] va = 15] 0} 0/0] 0] 10] 10] 12) 12) 15] 15 5224 n=4, Knapsack Capacity M = 5, Weights
(Wi, Wz, Ws, Wa) =
As, j2™, Viinjl = max (VEi~1, jh v,+V Li Lj—w)} Vii,
j] = VOB, 10]
= 22 (2,3, 4, 5) and profits (P1, Pz, Pa, Ps)= (3, 4, 5, 6).
= max (VL, 9}, 12+ V[1, 31} Vli-
1, jl = VI2, 10)
= 22 Solin. :
Filling second column, j =7 V[2, 9] = max (10, 12 +0) = 12 Here, V[i, j] = VIi— 1, j], so don’t select i” item. Check for Solution of knapsack problem is defined as,
VUL7] > i=l, j=7, w=,
= 4,4,=10 VB,9]
= i=3, j=9, w=w,
28, vy=15 the previous item 0 Hi=Oorj=0
As, j2w, Vi, j]=max (V[i-1, jh ¥,+Vli-1.j-wd} Soi 1=3-1=2 Vivil= Mi-1] iij<wi
As, jw, V Li, jl= max (V[i- Lj], y+VG-1,j-wd}
= max (V (0, 7], 10+
V [0, 3})
= max
{V [2, 9], 15+
V [2, 1]} Step2:
leper i=2,j=10
tee] i ‘e an tage
tbe WoL oh a tjew
VI3,9] =max (12, 15+0) = 15
VUI,7] = max (0, 10+0) = 10
V2,7) => i=2, j=7, mew, = 6v212
[Irom detsn ae[ols [2[afa [5 [6 [7 |e lata7 Incr Item detail
cbab pl | |
0)1)213)4 15 |6 \7 |8 5 (10 teresa le P. 4.9 5
=
As j>w™, Vij) =max (Vfi- 1, jl v,+Vfi-1j-wd }
= max {V[I, 7, 12+V (1, 1) ft Pelee oo fof ie oflolof fo fo fo Jo fo fo + ae eae
V(2, 7] = max (10, 12 +0)= 12 Whe
| iat[m =4]¥1=10{[ofo[o
10] 0/10/10) 10] 0 iTet |wi= In=t0 alo|o(o|10| 10:0 0|s0| sof [2
V(3,7] = i=3, j=7, W=W,= 8,515 i=2/ Wo =6) v, = 12

As i<w, Vij) = Vfi-1,j]


* VB7 = V2,7=12
[x|m=eh=78ofooof
[:
Filling second column, j= 10
ol oes
: | : fof0[ofofo[io|2[12|@ -2| Wo=6 vex 1200 ol oso 1a 1212 12 ob
Jffolebe ela el
ia) Wy =8]va=15]0}0}0{0| 10| 10] 12| 12] 18 | 15 |22 — Boundary conditions would be V (0.1) = Vii, 0) = 0.
VOL 10] => i=1, j= 10, w=w, = 4, y= 10 Vii.) = Vi2, 10]= 22 of table looks like.
Initial configuration
As, j2w, VG, max (Vfi-1, i]. y+VLi-1,j-w) } V{i-1,j]=VLL, 10]
= 10 i> _ -

= max {V (0, 10}, 10+ V (0, 61} Vii. j] # VIi- 1, j], so add item Ti = I, in solution set. Item Detail ofalzistals
VE.10] = max (0, 10+0)= 10 Reduce problem size j by W, iz0 bus
ojojojojolo
fo
tn i=10, w=w,= 6,v.=12 j = j-we-j-w=10-6=4 ci fwic2
asi wera
* 12% Vi j]= max (Vi Lj], v,+Vi-Lj-w) i = icte2-tel 2 |w=3 | wed [ol | [ | [
=max (V1, 10}, 12+ V[1, 4]) Solution Set = [13} is3 [w=4 [wes [0
V2, 10] =max (10, 12 + 19) = 22 i |was [ves [0
V[3, 10) = i=3, j=10, Wi=W, = 8,v3=15

Scanned by CamScanner
Cat Fillmefret coluraJ=n,4
A atysis of Algorithms (MU SSX je ems 2MA3
y=! vital 2 isk 1 jy
OA E
OLUM
max (V
Filling first column, As. JE viidl Step 1: Initially, i=a=
yoni ish james? = vii- hdl4 emat (VIOEI+VL.M
As jem Wield
Vent = Viole? vied amar (0.3) = 3
Jed meme Smad
vps iad jah weer™ 3 v2.4) = ied
3
ag, jmp VUigd= ma IVE TSR MAYC1.+¥G1
i=2
As j<e vig) = Vu-bl 1))
ma) 133 |woe4 | va=5 3
2. vaus VE = emax (V(14).4
[was | we 3
i=4
viau 21 wewy 4 vp,4} = max G.4 +0) =4 VOLS) = VUL=3
As ism Vili jee Vi-k vipa) 2 ied Jed wie = 4.y=5 Vi-Li = V0.2)
~ VE vi2u20 V[iglemo (VL wt Vibe f
1) Viki) = V.5)=7 Vij] # Vii 1, jh so add iter f=, in solution set.
tet jel we
#5 As, j2%
= max {V [2,5+4].
V [2, 0}) Vtiebj) = VO.5127 j by w,
lem
size
VEU = Reduce prob
Vfi-1. J], 50 don't select (* item and
pa.4) = max 4,540) =5
Vij) = j= joweaj-w=2-2=0
As. i<we Viidl = Vii- 1d) check for the previous item. .
Solution Set
2 VM = VBM=° Vida] =o iad Jaa Mea 5,y=6 soizi-l=4-1=3
s= (hl
Filling first column,j = 2
As, j<™%Viidl = VOi-Li
Problem size has reached to 0, so final solution is
(hyo ish j=2¥e =
AMF 3 . V4) = VB4)=5 s={h,h}
i- 1 j-wd | J=5 armed pro= fit Py +P2=7
AS, j20V -bihvt VE
j] = max (Vli Filling first column,
= max (V[0,2)3+¥ £0,011 vfs] 2 ih J=5 EM = 2y=3 Ex. 4.9.3
Lj-wy Consider the knapsack problem : n= m= 3, Wi, Wz, Ws)
As, J2% V fj) = max (V iL jl. y+V fi 6, Solve the
VU.2) = max (0,3) = 3
= (2, 3,4) (Py, Pa, Pa) = (1, 2, 5) and
V[2.2) = i=2, Je MEY = 3,y=4 = max (V (0, 51.3+ V (0, 3]} probiem using dynamic programming approach.
3
‘VUI, 5] = max (0,3) =
As, j<w Vii) = vfi-1J) ‘Soln. +
» VR = VOL V [2,5]
= i=2, j=5. ewe =3M=4 Solution of knapsack problem is defined as, =0
0 Hl=0orj
Ms AME 5
153, j= 2M
V (32)= AS J2%s V (ijl = max {VGi-1. jh y+ ¥ G1 j-w)] VUiil = VI351=7 W-4.0 thew
= VU-bd Vile
As, jew, VG) =max {V[1, 5].4+V (1.2) Vii-Lj) = VI2S1=7 max (Vi-1, i+ V- 4) -i itjew
2 VB = V2Q71=3 vil = j}. so don’t select i* item and
V[2, 5) = max (3.443) = 7 We have following stats about problem,
V2)> isd. fe waMe= SMF check for the previous item. ‘Table P. 4.9.3
j=5, 4a = 4ya5 soi = i-l=
As j<wViidl = VO-bil V3.5) > i=3,
max {V [i- 1, jle y+ Vfi- 5-4)
Solution SetS = (_}
s Vi) = VB=3 As, j2™% V Step3: i=2j=5
= max {V [2,5}.5+¥
(2, 1} k
Filling first column, =3
V(L3) isl, j= wew, = a3 =max (7,5+0) =7
V(5]3,
Boundary conditions would be V like. 10, i] = Vii, 01 = 0.
As, j2Wy V fig max (Vi- Lj) y+ VE Li-Wd) V [4,5] = i=4, f=5, WH 5. =O i
Initial configuration of table looks
Li -W)
As, JZ V Gigi) = max {V (i-1,j), w+ Vi
=max {V [0,3], 3+ V0, 1)) | —_—~
= max {V (3,5). 6+ V [3 Ol) t

V4.5) =max(7,64+0)= 7
As, j2w, Vi j)=max (Vfi- bj y+ VG 1j-™)) Final table would be,
=max {V (1, 3}, 4+V (1, 0}} vil = YR
i?__
V2.3] =max (3,4) = 4 4 Mi-tg = ViLS1=3
o11\2|3 n set,
[ij] # Vi I,J), 50 add item i=, in solutio
Item Detail
V3.3)
> i=3, j=3wew,
= 4,y=5 ololololo 0)
As, j<w, Vfijl= Vli-1j]
i=0 Reduce problem. sizej by Ww, i
Filling firstcolumn, J
VIL e ish J 2
[0/0 3 [31383

way
- VB.3] = V23}=4
;
el
i=] [wia2
= [we3= As, j<my VE) = VOL
(4 |4U 1
V3) > i=4 j23, y= 5,36 2 lwz3 fwz4 folol3 Vin = Vion=0
As, j<w. Vii jl = Vli- IJ) 3 [wed [ues folo|3]4l>
|v=6 [olo]3 [415 y
+ V3) = VBae4 it [wees

Scanned by CamScanner
(MU Sem 4 - Comp) a "
FT anatysis of Algortnms j=5. =
i<¥y wm
i=As, j= 9
Vid)a = Vi-Ld
V[1,5] 3 i=l
viTo i
PPararysis of Algorithms (MU - Sem 4 - Comp) Dynamic Programming
i = Vit) = ViLU=0 As, JB V (iJ)
= max (Vi j~yy } Reduce problem size j by ‘We have following stats about problem,
/ =max {V (0,5).
+V (0, 3))
VOT em, Vg) = VO-SiI VE,5] = max (0,1) = 1 se
{= ireedomae Table P44
vou = ¥2,u=0 V [2,5] = i=2 j=5 WW 3, v.05 a Set S = (1,}
Solution
Ena j 4 Step2: i=2,j=2 a |
first column, j = 2
Filling As, j2@, V [i jl= max WORLEY

vu.g 2" 2 ish j=24=42 201 0,2


=max (VOLSL24V ji
max {V[i- bj y+VG-1j-¥)) =max (1,241) =3
As J=5. WH Ws=
4 =5 a i=0 o|o;o}]ojajojo
= max (V (0, 2}, 1+ V [0, 0}) Boundary conditions would be V {0, 0) = 0. Initial
AS, j2.Wy V fijl= max (VHT VG jay | w=2|w=1[ofo fyi [aii [is of table looks like,
ue? =max (V[2,5}.5+V [2, 1)) il ij 2[2|3[3
V[3, 5] =max (3,5+0) =5 i 142)5/5]6
Vi-1i

Vi2.2) = VO,2I=1 Filling first column, j = 6 Vii


= i=3 j
VB 5 VIL = i =6 w=me dy Vi-ljl = VO.2=t
J, so don’t select i item and
s, j<w,a Vii]
As, ve a
na = Vii-1, .
As, j2m, ai
VOii)=max 15, %+V fi °
e414
(VO- 1j-wg) Vil. ed 1,tae
Vfi- previons tx im
~
Bal = vi) , 2)=
= max {V [0,6]. 1+ V (0, 4])
u
woizi-1=2-
Filling; first column, j = 3
oe 2] =

VO, 6) =max (0,1) = | Solution Set


V(L3 > i=l, j=3,4=6,= w= i 3.y=2 Step3: Filling first column, j=1

As, j2™)V (ijl max (V[i- 1, jh yj+ VGi-1.j-¥J) Ly+ V Gb j-wg ys) VU > isk j
= max {V (0,3) 1+-V 10,19) 31)
= max (V [I, 6],2+V[1, As, j< » Viel = VG-1i
VU,3] =max (0, 1) = 1 max (1,241) =3 Vi) = VOn=0
V2.3]> i=2, j=3,w, we? V(2.1) 3 i=2, jal, wew= 1
As, j2W, VG,j] =max (Vfi- Lj y+ Vi-j-w)
i As, j2w, V [i,j] = max {V [i-1, jh vj+Vli-1,j-w1)
| = max (V[1,3,.2+VI1,0)) = max (V [2, 6], 5+ V [2, 2]} 11.0))
= max (V{1, 1,104
Vi2, 3] =max (1,2) = 2 VI3,6] =6
= max (3,5+1) y Vij] = VO.21=1 = max {0,10+0}=10

= 1=3, j=3.w=w,=
V3 4425 _—» 4 Vi-14j] = Vi0,21=0 3
VB) = 183, jalVil, wavesVi-ui
J 1 # Vii — 1, j],
Vii, Li = 1, in solution set.
so add item As, jew,
As, i<w, Vii] = Vi-Lil
ES Reduce problem size j by w; VB Vi2.N=10
VBS = VE

o}o} j= j-waj-w=2-2=0 Vist) =i sme wae 2


Filling first column, j=4 are Nas | fo [ ofo
vith ey, nde i=1| wi=2| wel fofofifafafa ie Solution Set As, j<m Vini] = VG-hi
1,4) > i=1 j=4, we 1 2[w=3lye2]0 8 = U,h) . Via = VB sto
Asi2w, Vi; " oj1/2/2]3]3 oh
+42 Vij max {¥ fi jhy,+ V1 j-w)) we4[y=s[o lofi [2}s]sle Problem size has reached to 0, so final solution is | Filling second column, j=2
VU.) > ist, j=2, Wew= 24-12
S=(L.1s}
= max {V (0, “4], 1 + V [0,2]) Find selected items for M=6 + . ; i f= 1i
VI Famed profit= pi + ps=1+5=6 As, j2wy Vig) =max (VOi- Lj 4+ V-Li-w]
1,4] =max (0,1) = 1
Vv24)>
Step p 1:1: Initially,
Initial y, ii = = max (V {0,2} 12+V10,0))
J=4, Wew,=3,y=2 Ex. 4.9.4
As, j
5 J2 Me WE j= © max (V [i~1,j)
a w
¥,+VUi 1,5-w)] Solve following knapsack problem using dynamic = max (0, 12+0)= 12
jad jad wee Lyetd
programming algorithm with given capacity W = 5, Weight We Me "
=max {V[1,4],24+V [1,1
5
: (2,12), (1,10), (8,20), (2.15).
and Valua are as follows VARA => 1R% JR
V2.4} = max (J nm g a ci:\.: As, j2-wy Vfii}=max (Vfi- Li WAVE hi -w)
Vado41> ee= 4
123, j24, vw, y= =max {V[1, 2}, 104V 0
5 {1 Solution of knapsack problem is defined as,
As, 2, Viij)=max{V ” ‘ fy = 0 itizoorj-0 = max (12,1040)=12
G-1 jl. y+V fi-1,j-w))
él Vile Well itjew a isd
von i223
=max (V [2,4],5+ V2, 0])
VIB, 4] = max (2,5+0)
=5 max(V—1,viaVi-1j-w) jew As J<p Vij) = Viki
Vii-j) = V[2,6)=3 '
Vii] * VOi- 1, j], 80 add item Ii = 1, in solution sel

Scanned by CamScanner
AS i2¥ > Viilemx (VO 1j). YtVe Lj
= max (V[1, 5], 10+V[1, 3)) anaes of Algorlthms (MU - Sem 4 - Comp) eric Programe,
Solution set
maz (VE- ars Li-wd) = max (12, 10+ 12) =22 Now we have, 8 = Uplslhisi-w=2-2=0.
pa, VAI
218+ V BO VB. 5) = i=3, 5=5, w; 3 and, je 3, Andi -le1-1=0
1S +0)=15 Problem size has reached to zero. Selected items are
=max {V [2 5}.20+V
2,2) S=(1,b.4).
Eamed profit =p, + Pa + Pa= 12+ 10+ 15 = 37
2 y=y=12 = max (22, 20+ 12)=
a= 1j-w)) Vas} = id, j=5. [oto lo Ex. 4.9.5
Solve the following instance using Dynamic programming,
As, j2w, VE. jJ=max{VO-1 jy Vi .
wi=2|u=t2[olo [12 write the algorithm also, Knapsack Capacity = 10, P=< 1,6,
=max (V3.5. 15+, a w=1[ v=10 [0] 10] 12 [7 18, 22, 28 > andw=<1,2,5,6,7>.
= max (32, 15 + 22)=37 ¥5=3| %=20] 0| 10 | 12 Soln. z Solution ofknapsack problem is defined as,
1s iti=Oorj=0
So finally, table looks like, Vinil= nu itlew
max (Vfl-1, vi + V1, ]-«} tjew
We have following stats about problem,
Table P. 4.9.5

Item detail jo jo [o lo
i= ofo
izt [w=2|y=2lofo | v{fiz]2|n
The maximum profit we can earn is 37 units. Ths w= 1 | v,=10 [a | 10 | 12 [22 2 | 22
explicitly does not say anything about which items
be selected to eam this profit. Let us trace the tabletp 0 | 0 | 10 | 12 | 22 [30 | 32
Boundary conditions would be V {l, 0] = vy and
the items to be added in a knapsack to earn the Profit of
V (0, 1] =0. Initial configuration of table looks like.
units.
AS j2™, Vd =mas (Vfi-Lihy+VGi-Lj-w]) Tracing solution Viel,
V(2, 3] = 22
VOL, 3) =
LvfaTala]
stele ]alof wl]
= max {V0.4 12+V 1, 2]} For the last cell in the table,i andj would
be 4 and Here, V{i, j] # V[i - 1, jh so add I, in solution set and LT |e (|
respectively. reduce problem size j by w; and reduce i because the same
item cannot be added again.
Solution setS = (Ly, 1},
jaj-w=3-1=2
Andi = i-1=2-1=1
3 [4
ofo fo [o 0 Io j VQ = i=2, j=1, 4,
Liky,#VBi-1,j-w)} i=t |w=2 w=Rfo 0 [ofa le are rs As, i<w, VE Vii-1il
=m (¥ (24) 04V 221]
= max (2 + 10)=30 10 [of 10f 12 | 22 | 22] 2 ojo Koyo jo Jo Similarly, we will Via © vies
get,
VE} > 5-4. weu=2, yevjets . T
of 10 | 12 [2 [off taaif 2 | 2]12 Vou
= 1
AS IBN, Vii =am (Vfi— 1 jks, +V fit je) 5] 0} 10] 15 | 25 | 30 (3d Vian= t
Sat (¥B,4), 154VB,2)) V(4, 5] =37 d VEU = 1
= max (30, 15 +12)=39 hl -i1= VB, 5) =32 Filling second column, j= 2
Filling fifth column,
j =5
VIL) > isi, j _ rece bien + ilk so add Ii in solution
set Vii) = Vil.2=12 V2.2] = i=2, j=% mama 2 w=6
> : =Yvsvwe2d
nis item cannot be added again,
j by w; and reduce
i because the S404 Vti-bil = v(0,2]=0 As, w2jG Sw), Vii jJ=maxtV
fi- Ljh
As j20, Vijg=Fm [VG-1,j),
Here, V[i, j] # V[i- 1, j], so add I, in the solution set v,+Vfi-Lj-wll
Solution set = max {V [1,2],
6+ (1, 0}
Smax[V (0, 5), 124 ¥ (0, 3)} and reduce problem sizej by w, and reduce i because the
= mar Q12+0)=12 same item cannot be added again. =max (1,640) = 6

Scanned by CamScanner
As, j2™ VE Dynamic P 2
2 max [V3.6]
22+ V3, 0)) Panatysis of Algorithms (MU - Sem 4 - Comp)

max (19,2240) = 22 Step3: i=3,j=5 Let dfi, j} indicates the distance between cities
i andj. Function Cix, V ~ { x }] is the costof
Similarly.we will get.V 15, 6) = j; ———- the path starting from city x. V is the ofsetTSPof
way, final table would look i=O] Nem detail 0}1{2)3]4) 5] 6) 7] 8) 910) 11 cities/vertices in given graph. The aim
v5.2) = & If we continue in 2 similar is to minimize the cost function.
|; ———- roa wet |w=d
Piting third column, J =3 ofafsfatata fafa ja da [tt Step 2: ‘Assume that graph contains n verticesall
3, w= w2=
tom detall [0/1|2/9]4] 5 | 6 | 7 we=2[ve=6 lolale|7|7b7|z |7 |r |7 \7 | Vis Var ony Va- TSP finds a path covering
v2.3) 3 i=2 J
4Vfi-1j-il }
[w=18 [ol +[s]7| $12] 9 [20[25|25 [25 25 vertices exactly once, and the same time it
As, w2i.VI i,j] = max (Vv
(i-1Jh%
et {met folttpeiy| [tit tries 10 minimize the overall travelling
amas
(V (1.3264 VU.)
[0]t|8|7|7|7 [7 |7 wa=6 [v=22 [ol] {7|7|1|z2|24{ 26] 20|20|40| distance.
= max (1,6+1)
= 7 wee fue=6
vz? \vozan tolslel7|z|re\z2|2 0|0|2|a5|«0] | Step3: Matbematieal formula to find minimum
woz5 uy= 18 [0/1 |8|7|7| 18 | 19 |24 [a5 $
Lb shoe oe folelrfr||clo t nlo|ors| 0} distance is stated below:
Similarly, we will get.
Vind) Vii = 1), so add item li= by in solutionset. |G, vy = min { afi. + OG, V- 141) J eV and
vi33) = 7 wz |ve=22 [0|1]8 {717 | 18 |22 [24 iev.
via) = 7 wWs=7 [¥s=28 0] 44617] 7 | 18 | 22 | 28 — TSP problem possesses the principle of optimality, i.c.
V{5.31 = 7 for d[V;, Va} to be minimum, any intermediate path
Solution Set
Filling fourth column, j= 4 Trace for W=11 S = Uyh)
(Mj, Vj) must be minimum.
j), ali. k} + fk, jD
2, y=¥226 11 - From Fig. 4.10.1,d{i, j] = min(dli,
V (2,4) = i=2, J=4 Wie Step 1: Initially, i=5,)= Eamed profit = pr+ps=18+22=40 — Dynamic programming always selects the path which is
bib vs
+Vfi-1j-~)
As, wi2 ie V bij] = max (VG Syllabus Topic : Travelling Salesman Problem
2 mmax (V (1,4). 64+¥ 0.2) em detall 0/142
= max (1,641) =7 (vet fojtfat|t 4.10 Travelling Salesman Problem
wet
Similarly, we will get, We=2 [ve=8 [0/1/86] 7|7 > (May 15)
visa) = 7
3]wy25 [va= 18 ]0]1]6/7|7 Write short on Travelling sales person problem.
vid} = 7
V5.4) = 7 4] =6 |ve=22 [0]1/6|7|7 Dafine and solve Travelling Salesman Problem using
(10 Marks) Fig. 4.10.1
.

Filling fifth column, j= 5 lws=7 |vs=28 /0]1/6]7|7 dynamic programming.


V [2,5] = i= 2, f=5, W=W2= 2 M==6 — ‘Traveling Salesman Problem (TSP) is well studied and
Vii) = V{i- 1, j]. so don't select i" item and well-cxplored problem of computer science. Due to its «= Complexity analysis
As, 2), Vfij)=max (Vi Lj yt Vi“ bj- wd) check for the previous item, application in diverse ficlds, TSP has becn one of the Dynamic programming creates n.2" subproblems for n
= max (V{I, 5], 6+ VO, 3)) 80, =5-1=4 4 most interesting problems for researchers and | cities. Each subproblem can be solved in linear time. Thus
mathematicians. the time complexity of TSP using dynamic programming
=max (1,64 1) = Step2: i=4, 4 — Traveling salesman problem is stated as, “Given a set would be O(n’2"). It is much less than n! but still, it is an
; —— is also exponential.
of n cities and distance between each pair of eities, find | XPOnent. Space complexity
VBS] = i=3, j=5, w=.
As, w,2), V fi j= max (Vfi- L jy V- 1, j-w) the minimum length path such that it covers cach city Let us explore the TSP problem with the help of an
i=0| temdett [o|t]2{3/4/ 5] 6|7| 8] 9 example.
= max {V [2,5], 18 + V [2,0] exactly once and terminates the tour at starting city.”
ifnet [wet olrfafsta}a fa fa ft fa — It is not difficult to show that this problem is Ex. 4.104
Solve the travelling salesman problem with the associated
=max (7, 18+0)
= 18
i= 2|wp=2 us olilelzt7| 7|7 |7 |z |7 NP-complete problem. There exists n/ paths, a search
Similarly, we will get,
of the optimal path becomes very slow when n is cost adjacency matrix using dynamic programming.
VS] = 18 i [a5 vs=18 [0] 1]6]7|7)
18] 1924 | 25]25 considerably large.
V5.5) = 18 [on=6 [v=22 [o|t[o|7]7|18|22]24 2629] Each edge (u, v) in TSP graph is assigned some non-
Filling sixth column, j = 6 negative weight, which represents the distance between
Ji=S|us=7 [s=26 [ofs[a]7]7]18] 22 [28 aa] city u and ¥. This problem can be solved by finding the
V[2,6] = 7 Hamiltonian cycle of the graph,
V[3,6) => i=3, j=6, w=wy=5, y,
Reduce problem sizej by w;= Ws — The distance between cities is best described by the
As, j2, weighted graph, where edge (u,v) indicates the path Soln.=
j = j-wisj-w=11-6=5 distance
VGi,j} = max (Vfi-1,jhv,+V fi-1j-w)) from city u to v and w(u, v) represents the Let us start our tour from city 1.
between citics u and v. between
= max {V [2,6], 18+ V2, 1]) Step 1: Tnitially, we will find the distance
Let us formulate the solution of TSP
using dynamic
city 1 and city (2, 3,4, 5] without visiting
= max (7,18+1)=19 any intermediate city
Vi4,6]
> i=4, j=6, w= 6, ¥,=Y,
~8

Scanned by CamScanner
hms (MU - Som 4; COMB) = min{20, 40) =20
(403, 2)
= tozandy
Faia, y 2) repesens te asian {Om ® ° Cost’, (241) = +minCost(2, (4), 1), 3, 44 Le analysis of Algorithms (MU- Sem 4 - Comp)
7
as an intermediate cilf- Step 4:
Costt2. % D + Cost(4, (2}, 1)])
. siting 3 cities as an intermediate city. city.
= vet [125] +15}, eee (3,4,5).1) = min { d{2, 3} ate
Cost(2,visting
a 4+ Cost(3, 14,5), 1.612. 41
Step2: : 7 , Costt3, (4, 5},0 = min {413,47 4 + Cast, (3. 5}. pats
1) 812.5)
+ Cost, (5}. 0, d[3,5] 1 + Cost’, (3.4). 0)
in this step, we will fiad the minimum1 distance by + Cosit5, {4}, II} min (2 +23, 5425, 11 +36}
visiting 1 city 2s intermediate city. 47) =25
= min{25,30,
Cost{2, (3h, 1] = 23] + Cost. 1) = win (18+ 15,0742}
24s 13 = min(23,28) =23 4 Cost3. (2.4.5h1) = min ( d{3.21
~ ee + Cost, [4,5]. 1). 413.41
+ Cost(4, @ 1)
Cost (2, (4).1) = dl2,4] + Cost(4, (2,5). 1.413, 51 ‘Let us start our tour from city 1.
5+10=15 Cost(3, {2,5}. 1) = min{ uf3, 2]
+ Cost(5, {2,4}. 1) Step 1: Initially, we will find the distance between city 1
Cost{?, (S}.1F @. 1)
= dl2, $] + Cost(5, + Cost(2, [5}, 1), d[3, 5] (2, 3, 4) without visiting any
= min (12 420,8+34,7+19) and sy
+ Cost(5, (2}, 11) - intermediate city.
= +9=20
. == mi
min{32,42,26}=26
ost{3, { {2},1) == 4[3,ae
2) + Cost(2, 1) = min { [12 +20], 2
[7+ 28}} cost’, {2.3,5).1) = min 414.2) Cost(x, y, 2) represents the distance from x to z and y
Cost(3,
= 12422 35) = 32
= min(32, j am cost, (3,51, 1),€14.3) city.
as an intermediate
Cost(4, {2,3}, 1) = min{ d[4, 2) + Coma, 12.51, n.614.5} Cos2,9.1) = di N=5
Cost{3, (4}.1] = 413, 4] + Cast(4,@ D
1231.10)
scenes, ues Cosi3.9.1) = 613,11=6
‘ane (5, 5,41)
+ Cost(2, (3), 1), 4f4, 3] a =
Cost{3, : d[3,
wee
(5) 1}be =
‘ost(3, (5), 5] + Costs, 4,4, + Cost(3, (24, D]} = min (23 + 30,24 + 32,6417) Step2
:
aais sep.
. we willSieate as
find the minimum distance by
749= ; '
= 23.4),1 . aoe visiting 1 city as an intermediate city.
Cost(4, (2},1) = dl4,2]+ Cost(2, 1) = min (123 + 13], [24 + 36) }
= mmin(36, 60} = 36 Costs 03.41. = Costt2, (3.1) = Al2,314 Cos, @, 1)
= B+ =a47 ca * dS, 3) = 946=15
Cost(4, (3), 1} = fd, 3] + Costt3,¢, 1 Cost(4, (3,5},1) = min( afd, 3) Cost{2. (4).11 = dl2,4]1+ Coss, 6,1)
= 411238 + Cosi, (53,1), [4,5] cue foe
= mn (4430,84.27 11436) = 10+8=18
¢, 1)
Cost(4, (5), 1] = dl4, 5] +Cost(5. +Cost(S, {3}, U)]}
| = 649215 = min { [24+ 16), [6 +19] ) = min(34,35,47) =34 Cost. (21.1) = ol3.21+ Cont &. 1D
13+5=18
Cost{5, {2}, 1} = [5,2] +Cos 1 = mit =
25} =25
= min{40,
meas
Step 5: 1n this step, we will find the minimum distance Cont3, (44.1) = alS.4] +Cones, 1)
he nam 2, #1) by visiting 4 cities as an intermediate city =2
= = Cost(4, (2,5),1) = min{ d[4, 2] = 12+8=20
Cost{5, (3}.1} 9, 1)
= df5, 3] +Cost(3,
+Cost(2, [5], 1), df4, 5) Cost(1, 12,3.4,5}1) = min { d{1.21 Costl4. (21.1) = {4,21 + Coste2, 9.)
« Seiints = 8+5=13
+ Cost(5, (2}, II} + Cost(2, (3, 4,5], 1). df1, 3]
Costs, (4). 1} = ofS, 41+ Cost, ¢ 1) Cost(4, {3}, 1} = {4,314 Cost, @. 1)
= min { (23 +20}, [6 +28] ] + Cost(3, (2,4, 5), 1), dll 4)
= 11410221 = 94+6=15
34) = 34
= min(43, + Cost(4, (2.3.51, 1). dll, 5]
Step 3: In this step, we will find the minimum distance by + Cost(5, {2.3.4}, 19} Step 3: In this step, we will find the minimum distanceby
visiting 2 cities as an intermediate city. Cost(5, {2,3}, 1) = minf [5,2
. +26, 10
= min (24+25,11 city.
visiting 2 cities as an intermediate
Cost(2, (3,4), 1) = min { €f2, 3} +Cost(2, {3}, 1), df5, 3] +23,9+34) Cost(2, (3,4).1) = min {4{2, 3] + Cos(3, 14). D.
+ Cost(3, {4}, 1), d[2, 4] +Cost(3, (2}, DP = min{49,
37, 33, 43] =33 dl2, 4] + Cost, (34, DN
. eae " = min ( [4+ 13), (8+ 36] } Thus, minimum length tour would be of 33. = min { (9 +20), [10+ 15})

af, 344} = 17
=min(17, Trace the B path = min{29, 25} =25
-
‘min{20, ht5+35))
40} = 20 Cost(S,(3,4}.1) = mint
( dQ, 4) : —— of 33.
Letus find the path that gives the distance Cost(3, (2.4). 1) = min { Al3,2] + Cost, 141. 0.
Cost(2, (4,5). 1) = min = Cost(1, {2, 3, 4, 5}, 1) is minimum due to df], 4], so 419,4)4 Cost, 2,00
+ Cost, (5), 1), d[2,5) + Cost(3, (4), 0, af5, 4)
Cos, (4) 1) +Cost(4, (3}, 11} move from 1 to 4. Path= (1,4). _ 15418), 02413))
= min (15 +15} [11421]} = min { (8 + 18}, [11 +35) } = Cost(4, (2, 3, 5}, 1) is minimum due to d{4, 5], so malt
== min(31,25) =25;
= min(26, 46) = 26 move from 4 to 5, Path = (1, 4, 5}.
= min(20,32} =29 = Cost(5, {2, 3}, 1) is minimum due to d{5, 2], so move Cost(4, {2,3}.1) = min{ d{4,2] + Cost(2, (3), Ds
Cost (3,5},1) = min (af2, 3) Cost(s, (2,4},1) = min{ d{s, 2]
] from 5 to 2, Path = (1,4, 5,2). afd, . 3} + Cost(, (2}.1)(2)
4 1), d[2,4] 4
+ Cost(2, — Cost(2, {3}, 1) is minimum due to 4[2, 3}, so move = min { {8+ 15)19+18))
+Cos Cost(3, {4}, coats (4),1), d[5,4]
+ Cost(4, (3}, 1))) (2), | from 2 to 3. Path = (1, 4,5, 2.3)- = min{23,27) =23
= _min( (2+ 18}, (5+35)) = amin ( [4+ 15], [11447]} All cities are visited so come back to 1 Hence the
= min(19,
58} = 19

Scanned by CamScanner
find the
(APanatysis of Algorithms (MU - Sem 4 = Comp) Dynamic Programming
this ermediate CY Cost(4. (31. = Soln.: Cost(l, (2.3.4). 1) = min{a{t,2} + Coset2t3.4h02
visiting 3 cities aint{df . 2) + Cost (3. 10 10+ 10=20 Above graph can be represeated as, (1,3) + Con, (2,41. Ds
Cost, (2.3.44 1) = min afl, 3] + Cost3, (2.41 0- : Inthis step. we will find the minimum dig, O | 10 | 15.] 20
fl, 4] + Cost(4, (2.31. DE
ft, 4] + Costes,
(2.31 Db Step 3: Uring 2 cities as an intermediate city, "aneby slo 9 10
+
= on aiaennil

= min (1 0-425, 15-+25,20+23) Cost? 13.41. = min (412, 3] + C0313, (4), 4)


= min{35,40, 43} =35
= min[35, 40.43)
= 35 2. 41 + Costes, (3), 199) 6 18 Oe
d beof 35. 8 [seule “Thus, minimum length tour would be of 35.
Thus, minimum length tour woul
= min { (13+25),[6+20})
= min{38, 26) =26 Let us start our tour from city 1. Trace the path
distance of 35.
find the path that give thedue (0 dl, 2], 50
Let us tn s Cost(3, (2,4), == min (413.21 + Cost, (4), 1) Step 1: Initially, we will find the distance between city
1
— Let us find the path that gives the distance of 35.
ee is minimum [3, 41-+ Cost(4, (24, 1))) and city (2, 3, 4) -without visiting any Cost(L. (2, 3. 44, 1) is minimum due to dU, 21, so
Cos, (2, 3. 1 1) move from 1 to 2. Path= {1,2}
move from I t0 2. Pat = h
(1, 2] = min ( (9 +26], (5+27]] intermediate city.
lis minimum due to
4],
JL: 2, 4], so move
= min(35,3 Cost(x, y, 2) represents the distance from x to 7 and y — Cost(2, (3, 4}, 1) is minimum due to d{2, 4], so move
- Cos
2 to 4. Path = (1, 2,4}
as an intermediate city. from 2 to 4. Path= (1,2,4}
um a ue to di4, 3], 0 itedmoveso Cosi(4, [2.3}.1) = min{ dfs, 2] + Cost(2, (3), 1), Cost(2.9.1) = 62, =5 — Cost(4, 3}. 1) is minimum due to d[4, 3}, so move
from
Cosi(4, (3), 1) is minim4. 3] . are visi
All cities from4 to 3. Path= (1, 2,4, 3]. All cities are visited
so
fom 4 t0 3, Path= {I, 2, dL4, 3] + Cost(3, (2},1))) cost3.9.1) = 43,6
come back to 1. Hence the optimum tour would be
come back to L. Hence the optimum
tour would be = min { (15 +23], (10421)) Cost(4,9.1) = ad = 8 1-2-4-3-1.
1-2-4-3-
t ee = min(38,31) =31 Step 2: In this step, we will find the minimum distance a
In this step, we will find the minimum distang, by visiting | city as intermediate city. Syllabus Topic : Longest Common Subsequence
Ex. 4.10.3 Step4: eee
Sys
Define the Traveling Salesperson Problem. Solve
the TSP
by visiting 3 cities as intermediate city, Cost{2, (3}.1]} = d[2,3]+Cost(3, 9,1)
problem using Dynamic programming where the Longest Common Subsequence
edge
< 946513 4.11
Jengths are given as :
Cost(l, (2.3.41, 1) = min(dll,
2] + Cost(2,(3, 4),1,
[t, 3] + Cost(3, (2,4), 1), + Cost(4. @, 1D
Cost(2. 49, L) = Al2,4] ee
= W+8=18 > (May 14, May 17)
d[1, 4] + Cost(4, {2, 3}, 1)]
Explain: longest) common_subsequence with an
+ Cost(2, 0, 1)
= min (9+26,8+32,8+31} Cost{3, (2},1]
=
= [3,2]
194518 example. eee 2014,10 Marks
= min{35, 40, 39} =35
cost(3, (ht) = dl3,4)-+ Cost 6. nce
problarn?
@.- What's a longest common subseque
Soln. =
‘Thus, minimum length tour would be of 35. = 1248220
Traveling salesman problem is stated as, “Given a set Trace the path
Cost[4, (21.1) = dl. 2) +Costt2,4, 1) o odes leaddibelnemen an
ofa cities and distance between each pair of cities, find the Let us find the path that gives the distance of 35, = 84523
minimum length path such that it covers each city exactly
‘once and terminates the tour al starting city.”
Cost(1, (2, 3, 4), 1) is minimum due to df, 2},0 Cost{4, (3),1) = dld, 3] + Costa,6, 0) = Problem
move from 1 to 2. Path = {1, 2] = 946215 The longest common sequence is the problem of
Let us start our tour from city 1.
Cost(2, (3, 4}, 1) is minimum due to d[2, 4], so move finding maximum length common subsequence from given
Step 1; Initially, we will find the distance between city 1 Step 3: In this step, we will find the minimum distance two strings A and B.
and city (2, 3, 4} without visiting any from 2 to 4, Path = [1, 2, 4} by visiting 2 cities as an intermediate city.
intermediate city. - Cost(4, (3}, 1) is minimum due to d[4, 3], so move 1 Explanation
from4 to 3. Path = (1, 2,4, 3}. All cities are visitedso Cosi(2, (3.4). 1) = min (d{2, 3} + Cost(3, (4). 1,
Cosi(x, y, z) represents the distance from x to z and y
come back to 1. Hence the optimum tour would be (2, 4] +Cost(4, 31, DI} Let A= <.aj, ay A ...8,> and B =< by,by by «b> DE
{
as an intermediate
city. two strings over analphabets. Then B is a subsequence of A
1 -2-4-3-1 min { (9 +20}, [10+ 15] }
Cost(2, 4,1) = df, 1}=12 if B can he generated by striking out some elements from A.
Cost(3, 9,1) = (3, = 10
min(29, 25} = 25 By subsequence, we mean that the values must occur in
Cost(3, (2, 4}, 1) = min { d[3, 2] + Cost(2, (4), 1), the order of the sequence, but they need not be consecutive,
Cosi(4,9, 1) = df4,1]=20 Find the path of travelling salesperson problem of ow d{3,4] + Cost(4, (2), DI IfA=<X,Y,X,X,Z, ¥,X>andB =<X,XY,X>
then by deleting A(2], A(4] and A{5] fram A, we can derive
Step 2: In this Step, we will find the minimum distance by
visiting | city as intermediate city.
Y | graph. min ( (13+ 18}, (12 +13] )
B. So B is the subsequence of C.
Cost{2, (3), 1] = d[2, 3) +Cost(3, 9, 1) min{31, 25] = 25 Common sub sequence of A and B is the sub sequence
"

13410=23 Cost(4, (2,3), 1) = min{ did,2] + Cost(2, (3), Ds which can be generated by striking some characters from A
b, b,
Cost{2, (4},1) = d[2,4]+ Costa, 6, 1) d{4, 3] + Cost’, (2), 1] andB both. lf A= {a,b a,c, b, ¢, b} and B = {a, b, ¢,
a, c}, then the sequence {a,c}, (a, b, ch (& c chy
= 6+20=26 min { (8+ E51, [9 + 18] } and B, but
{a,b, c, b} etc are common sub sequences of A
Cost(3, {2}, 1] = df3, 2] +Cost2,4, 1) = min(23,27}=23 {a,b,c, b, a} is not (a,b, ¢,b, a} is sub sequence
of B but it
= 9+12=21 Step 4: In this step, we will find the minimum
distance is not sub sequence of A.
Cost{3, (4), 1} = 13, 4] + Cost(4,@, 1) by visiting 3 cities as intermediate
city.
= 5+20=25
Fig. P. 4.10.4

Scanned by CamScanner
= complexity analysis
a
we need to Perform BM anaiysta of Algorithms (MU - Sem 4 - Comp) 434 Dynamic Programming
In brute force attack,
USt
Jongest common subsequences algorithms are
sn] 10 sce if tis also a guy XY; => LCSEj) = max (LCS fi-1.j1, LSPS)
2 max CS (1,5) LCS 24)
+ exponential ime. Such ‘ay, Checking membersh ip of one suf Les fi, j-1) = max(1.2)= 2
_.n] takes O(n) time, 2 oy LCS [1,2] = max (LCS (0,2),
Les [1,1 | LES(2.6) >i=2, j=6 X=C, ¥,=E
1 Mathematical Formulation
1 P of length m. So worst case max (0,1) = 1 X\#¥; => LCS(i,j] = max (LCS (i-1,j),
Les 1,3) >i=1, LES [i,j- 1)

paracter of both strings isby same table of <. Ke, = LCSfij] = max @Sp-1jh LCS (2,6) = max (LCS {1,6}, LCS [2,5))
one In dynamic programming, the only So running (8 eran compautontorowas max (1,2) = 2
jength of LCS is incremented new is+ a usingning
filed upagmm 1o0ps. ke ned
ech for WoL
two vested ‘omy
ed by one. The
and length of om swings ‘ reduc rama LCS[1.3] = max(LCS{0,
= max(Q, 1)= 1
31, LCS{1,21) Les TD eins=3 j=1, j=1, X=B,
X=B, YnA Y=
problem is now 1o fin Xe¥, = LOS) = -
by. we shall consider two subproblems
LCS [1,4] >i=1, j=4, XRA, YHA°
! =“ va enn i i~
= Apply [.CS on strings Aw. and B X=¥,=> LOSfij)
LCS [i,4]
= 1+Lesi-1.j-0
= 14+LCSf[0,3)
LCS 13,1) = max (LCS [2, 11, LCS (3,0))
= max(1,0) = L .
Apply LCS on strings A and By 1
1 the result which gives the longest subsequence.
= 1+0=1 LCS B,2) >i=3, j= X,=B, Y\=B
Les,5) >i X=Y¥, > LOS = 1+Lcsi-1j-n
2s,
Thus, optimal substructure of LCS problem is defined XrY, = LCS (dD LCS 3,2] = 1+LCSI2,N=141 =2
LCS [3,3] >i=3, j=3, K=B, Y=
Optimal substructure of LCS problem using ga. | LCS [1,5]
programming is given as: i
Les [1,6] >i= LCS [3,3] = max(LCS[2,3}, LCS(3, 2)
=max(2,2)=2
X#¥, => LCSHj] = max(LCs[i-1.j), «
The algorithm ta solve LCS problem is described below : LCS i,j
LCS [3,4] >i=3, j=4, X=B, ¥/=A
Algorithm LONGEST_COMMON_SUBSEQUENCE(X, ¥) Xe#¥,=> LSE) = max (Les f-1, 9),
max (LCS [i,j 1], LCS fi-1,j)) LCS [1,6] = max(LCS(0,
6}, LCS[1, 5])
Lesfi,j-
Initially, table looks like, = max(Q, 1) = LCS [3,4] = max (LCS (2,4),
Computation for row 2 LES (3, 31) = max (2,2) = 2
LCS [2,1] > i=2, j=. X=, Y=A LCS [3,5] >i=3, j=5, X=B. Y,
X#Y, => LCSEj] = mx (LCS[i-1,j], X=¥,=> LOSGj) = 1+Lcesfi-1j-0
LCS {i,j-1) LCS(3,5] = 14LCS[2,4)=14+2=3
LCS [2,1] = max (LCS (1, 11, LCS (2, 0) LCS [3,6] >i=3, j=6, X=B, ¥j=E
= max(1,0) =1 YY, => LCSEj) = max (LCS [i-1. jh.
LCS [2,2] =i=2 j LCS fi,j-1)
X\#Y, = LCs i LCS [3,6} = max (LCS
|2, 6],
LCS [i,j-1) LCS [3, 5]) = max (2,3) = 3
LCS [2,2] = max (LCS [1, 2], LCS [2, 1) Computation for row 4
LCS [4.1)
= i=4, j=l, XA, YHA
LCS 2,3] >i=2, j= X=Y¥,=> LOSE = 14+LCsi-1,j-0
LCSIi, j] — LCSfi-
1, j- 1) +1 X=¥,> LCSfi,j) = 1+LCsi-1,j-1) LCS [4,1] = 1+LCS[3,0]=1+0
; Let's compute the remaining cell values row by row? LCS [2,3] = 1+LCS{I,2] LCS [4,2] >i=4, ) X=A, Yj=B
| HUCSTi— 1, j] 2 LESfi,j— 1} then :
To be consistent with the previous i P=
examples, let X,#¥,=> LCS{i,j] = max (LCSfi-1,jh
and Q=Y = P=ABCAED and Q= ABCABE LCS [2,4] >i=2, j LCS {i,j-1)
Computation for row 1 X4Y,> LCS Oj] LCS (4,2) = max (LCS (3,21,
LCS {4, 1]) = max (2, 1) = 2
LCS(1,1] > i=l, j=l, X=A, Yj=A LCS [2,4] = max (LCS [I, 4), LCS [2, 3]) LOS 4,3] sis4, j=3, X=A, Yj=xC
= max(1,2) =2 X,#¥,=> LCS(,j) = max QcSti-1.jh
LCS [1,1] = 1+LCS{0, 0} LCS (2,5) >i=2, j=5, X\=C, Y=B LCS
fi, j- 1D
X,#¥, => LCS{ij] = max(LCSfi-1 Jj), LCS 14, 2))
LCS [4,3] = max (LCS [3,3],

LCS [1,2] >i=1, j= LCS fi,j- 1) = max (2,2)=2

Scanned by CamScanner
LCS [6.2] i= 6 7-2,
J %=D. YB
Ta, YA x#¥, 2 LOSU = max (LCS [i~
Les fi,j-1] EBT anai of Algorit
ysts hms {MU = Som 4 - Comp)
uestij) = ECs MIN
ei ag) = HEC = #2 = Les [6,2] = max (LCS [5,2], Log I, Ky
Find LCS 0
Dynamic Programing.
vifinOorj=0
=max (2, 1) = 2 Longest common subsequence for tees: 1 i
ess) sist I 25 Xd, ¥j28 -1ih fength
4. Let us find LCS of X and
Y: /en strings is of | Lcs{i,j) = 1 si P,=Q,
ey, LES List = mx ECSU Tdi Les (6, 3] 2i=6, j=2 X%=D. Y=c Step1: i=6,j=6,X,=D,¥, max (LOS {i,j-ILLCSG-1LjD if P,#Q, |
a LOSti ny) XyaY, = LCS fail = max CLCSH— 1 X# Yy and Lest Ininitially table Looks like,
Les 4.5} = max (LCST. (3, 5}, LCS 14, 41) LCS fi,j- 1) ‘Table P. 4.11.2
move in vertical direction
LC 6,3] = max LESS, 31, Los 76 » Solution setS = [} Q
= max (2,2) = 2 +2) Step2: i=5,j=6,X)=B, Yj=6
Lest. X#¥,= LCST)
LCS [6,4] > i=6, j X=D, Y=A X;= Yq. 0 add Xi to solution set. And move diagonally
= max (LCS (i- 1 ,j), LCS f,j- 1) = max (LCS Ei—1, 3,
Xe¥,> LCS(ij) back.
LCS [4,6] = max (LCS (3, 6}, LCS (4, 51) Les fi, 7-1) Solution setS =
=max (3,3) = 3 (BE)
LCS [6,4] = max (LCS [5, 4], Les. 16, » Step 3: 1=4,j=5,X,2A,Y,
Computation for row 5 nax (3,2)= 3 X, # ¥) and LCS(i, jis derived from LCS[i — 1, j} or
Les {6,5} >i=6, j LCSfi, j - 1]. We can move in any direction, Let us choose
Les{s.N) > i=5, jab XB Yea (=D, Y/=B
Key, => LCSLj} = max (LCs fi-1 jh X,Y, => LCS max (LCS [i= 1, j], vertical.

LCs fi,j-) Solution set$ = {E}


Lesfi, j- 1D step: 1i=3,j=5,X,=L, ¥j=B
LOS [5,11 = max (LCS (4, 1], LCS [5, 0) LCS [6,5] = max (LCS (5, 5], LCS (6, 4)
=max (1,0) = 1 max (3,3) = 3 X, = Y;, 50 add Xi to solution set. And move diagonally
LCS [6,6] i=6, j=6, X=D, ¥j=E back.
LCS (5,2) Si=5, f=2, X=E Y Solution set$ =(E,B} Let's compute the remaining cell values row by row :
X#¥, > LCSGij] = max (LCS fi~1, i), X#¥, > LCS) = max (LCs [i-1, i),
Sep5: i=2j=4%=C, =A Computation for row 1 }
Les fi,j- 0) LCS [i,j- 1) X,# ¥;, and LCS[i,j] is derived from LCS[i,j - 1]. So LCS[I,M > isl, j=1, Rea, Qac }
LCS [5,2] = max (LCS [4,2], LCS [6,6] = max (LCS [5, 6}, LCS (6, 5) move horizontally.
' LCS 15, 1)) =max(2, 1) = 2 = max (4,3) = 4 Solution setS = [{E,B} P4Q = LCSE,j] = max (Les{ij-1),
| LCS (5,3) > i=5, j=3, H=B, Yj=C So finally, table would be, Step6: i=2,j=3,X,=C,¥j=C
X#Y¥, > LCS [ij] = max (LCS fi-1,jh Les i-1,j) \
Table P. 411.1(a) : LCS table for X = ACBAED aap X, = ¥;, so add Xito solution set. And move diagonally + LES(L1] = max (LCS (1, 0}, LCS (0, 1))
Les fi,j-1) Y= ABCABE q back. ,
!
= max (0,0) =0 "
LCS [5,3] = max (LCS [4, 3], Table P. 4.11.1(a) 7 Solution setS ={E,B,C} LCS [1,2] > P.=A, Q)=B |
LCS [5, 2]) = max (2, 2) = 2 Step7: i=1,j=2,X=A,Y/=B P.#Q=> LCS[I,2] = max (LCS[I,
LCS(5.4] >i=5, j=4, X=B Yea 1], LCS (0,2) |
X, # Yj, and LCS[i,j] is detived from LCS{i, j - 1]. So = max(0,0) =0
X#¥,=> LOS{ij] = max@cSfi-1,jh move horizontally. LES[L.3] > =A, Q,
LCS fi,j-1) Solution setS = [(E,B,C} R=Q> LCS[1,3] = 1+LCSfi-1,j-1] 4
LCS[5,4] = max (LCS [4, 4}, Step8: i=l, j=2%=A,Yj=A = 1+LCS(0,2)
LCS [5, 3]) = max (3, 2) = 3 X,= Yj, so add Xi to solution
set. And move diagonally \
= 440-1 ¢ |
LCS (5,5) >i=5, j=5, X=E, ¥j=B back. LES [1,4] > Pk=X, Q=F
X#Y,> LCSH,jl = max (LCS fi-1,j), Solution setS = (EB.CA} P,#Q,= LCS(1.4] = max (LCS[I, 3], LCS [0, 4])
Les
fi, j- 1) Moving diagonally back i and j become zero. So stop. = hank
max(1,0)=1 {|
= max (LCS
(4, 5], tf Alo 1 | ie We have collected characters from the last position of the
string. So reverse the solution set, which is the LCS of X | Computation for row 2 \
LCS [5, 4]) = max (3, 3) = 3 and Y. LES (2,1)> i=2, j=1, B=B, Q=C
1. $=6, X=E, Yj=E
2/C/]0] Impl 27h 272
So, LCS = ACBE P.#Q = LCS(21] = max (LCS {1,0}, LCS (0, 1}) {
%e¥)> LOSfijl = 1+LCsfi~1,j—1] 37B}]o] 1 2 = max (0,0) = 0 \
LOS{5.6) = L+Les[4,sj= 143=4
Computation
for row 5 Usinging a.
algorithm
rithm determin
determine an longest
longest co! common sequence of LCS (2,2]
_ > P=B, Q=B
_
4] Ayo 2 3s (A,B,C,D,B,A,C,D,F) and (C,B,AF) PaQ = LCS{22) = t+Lestl.t)
Le: 316,11
= i=6,
i= j=1, x=p, Y=A Soin. : = 1+0=1
5 /E]o |
X FY) => LCS ij] =max(Lcs
fit, LCS Bj 1 Let us consider P = (A, B, C, D, B, A, C, D, F) and | LCS(2,3] > P\=B. Q=A
LCS {6, 1] = max (LCS Q=(C.B, A, F) , P.#Q > LCSP,3]
[5, 1], LCS (6, 0})
=max(1,0)
=| Optimal substructure of LCS problem using dynamic = max (LCS [i, 3], LCS (2, 2))
programming is given as : = max(1,1) = 1

Scanned by CamScanner
“37 Dynamic p, |
«atu = Sem 4-.Comp), Fg 2 LOS(6.2] = mar OL.CS (5, 11S EY
[ET anatysis of Algocthns, Eanaryss of Algorthms (MU~ Som 4- Comp)
USE oe = ae QeFsan (LCS [1.4] LCS 12, 3)
= max (2,1) = 4-38
FT +Lcsit -1j-N=14+LCS/8,3]=14324 __
LCS [6,3] > P= A Qea So finally table would be
Dynamic Programming
7,40, > LCS 2 mx(I,)=l p.=Q,= LCSI6.3) = 14LCSfi-1.j-1
Solution sec§ = IRA BC)

“ymputation forfor row3 ‘ 2141055.) 14203


Table P. 4.11.2(a)
fave Moving
collected diagonally
characters back
from j thebecomes zero. So stop. We
lst position of the suing.
olifa2 So reverse the solution set, which is the LCS of P and a
typi = h=c. = Les [6,4] > ZA, QaF
= 1+LCS(2,0) R40, > LCSI6S) = marLCSIS.ALLCS16, 9p So, LCS, CBAF

= 14021 = max (2, 3)=3 ‘43


spy) 2 R= Q=B Given two sequences of characters, P
LCS.2 = max(LCS (220108 0,10
frg, > Mesia2) Computation for row 7 A = <MLNOM>
soe Les(7.1}=> Bel, QC ,
(Qe<MNOMb>. Obtain the tongast common subsequ ence.
= max(i, = 1 B
Soin.:
P=Q > LCS (7.1) = 1+LCS[i-1,j-1)
LCS[3.3]> F=C. QA c Given two strings are P = <MLNOM> and
P.#Q,=> LCS13.3] = max (LCS (2. 3}, LCS
[3, 2) ‘ =14+LCS[6,0}=140=4 Q=<MNOM>
LCS (7, 2] => P=C, Oey D '
= max(I,t)= 1 Optimal substructure of LCS problem using dynamic
LES (3,4) > P.=C. Q=F P,Q, = LCS(7.2) = max(LCS [6,2]. LCS (7, 1p B Programming is given as:
249,
14 > Lcs13.4) = max (LCS [2, 4], LCS [3, 3)) =max(2,1) = 2 A 3 Les [ijl
0
=

= max(i, =) LCSI7.3} = PEC, Qoy rifi=00rj=0


P,#Q,=> LCSIZ3] = max (LCS (6, 3], LCS [7, 2))
Cc 3 1+LeSfi-1.j-11 rif P,Q,
Computation for row 4 8 D
= max(3,2)=3 1 3 max (LCS fi, j- 11, LCS fi-1,
j) ifP,.4Q,
LCS [4,1] > P,=D, Q=C
P.#Q,> LCSI4.1J = max (LCS (3,1),
LCS 14.0) LCS (7.4) > PC, Quer 9 F 1 4 Initially, table looks like,
P.#Q, => LCS 17,4) Table P. 4.11.3
= max(I,0)=1
= max (LCS 16,41, LCS 17,3) Longest. comunon subsequence for given strings is of
LCS [4,2] > R=D. Q=B Jength 4, Let us find LCS of Pand Q: —_
P#Q,= LCS[4,2) = max (LCS LCS (4,1) = max(3, 3)=3 i=9,j=4,P,=F,Q=F
= max(1,1)= 1 ‘Computation for row 3 = Q, so add P; to solution set. And move
LCS [4,3] > P,=D, QaA
LCS [8.1] > P=D, Q=C : diagonally back.
P.#Q,= LCSIS,3] = max (LCS [3.3).LCS
4,2) P,+Q,=> LCS(8,1]) = max (LCS[7, 1), Solution setS = [F}
= max(I,1) = 1 i=8,j=3,P=D,Q=A 1
LCS [4,4]
= P=D, Q=F LCS [8, O}) = max(1,
0) =1
LCS 18,2]
= P=D, Q=B P, # Q, and LCS[i, j] is derived from LCS{i — |, j]. So 2
P,#Q = LCS [4,4] = max (LCS (3, 4], LCS (4, 3)) P.+Q,> LCS[8,2) = max(LCS[7,2), move in vertical direction. 3
= max(I,1I)=1 Solution setS = [F) 4
LCS (81 1}) = max(2,1) = 2
Computation for row 5 LCS [8,3] => P.=D, Q=A i=7,j=3,P)=C,Q=A s|mlio
LCSI5,1]= P,=B, Q=C P.#Q, => LCS[8,3] = max (LCS [7.3],
LCS [8, 2)) P, # Q, and LCS[i, j] is derived from LCS[i— 1, j].'S0 Let's compute the remaining cell values row by row:
P#Q, > LOSI.) = max QCS44, 1), = max(3,
2) =3 move in vertical direction.
Computation for row 1 :
: LCS (5, 0)) = max (1, 0) = 1 LCS [8,4] => P,=D, Q=F Solution setS (F} !
P#Q,=> LCS [8,4] LCS (1, 1] = i=1, j=1, R=M, Q=M
P.=Q = LCS(5,2] = =max (LCS [7, 4], LCS (8, 3]) = max(3, 3) = 3 P, = Q,, so add Pi to solution set, And move diagonally Pi=Q > LCSfij] = 1+LCS{i-1,j-1)
I+1=2 LCS [1,1] = 1+LCS{0,0}
Computation for row 9
LCSI5,3]> P.=B, Q=A SolutionsetS = (FA) ' = 1+0=1
P.#Q, > LCS(5,3] = max (LCS (4, 3], LCS{5,2) LCS [9.1] > P\=F, Q=C i=5,j=2,P,=B,Q=B LCS (1,2) >i=1, j=2, R=M, Q=N
= max(I,2) = 2 PQ > LCS(9,1] = max (LCS[8, 1], P, = Q,, so add Pi to solution set. And move diagonally P.4Q => LCSEij] = max (LCS fi-1, jh
LES(5.4)> P.=B, Q=F LCS (9, 0}) = max(1,0)
Les fi.j- 1)
Pr#Q, = LCSIS.4] = max (LCs (4,4), LES 5, 3)) LCS19,2)
= R=F, Q= Solution setS = {FAB} LCS [1,2] = max (LCS [0,2],
= max (1,2)=2 P#Q=> LCS(9,2) i=4,j=1,P,=D,Q= LCs 1,1)
Computation for row 6 smax (LCS [B, 2}, LCS (9, 1]) = max(2, 1) = 2 P, # Q,, and LCS{i,j] is derived from LCSfi,j — 1}. So = max(Q,1) =1
LCS [6,1] > P=a, Q=c LCS 9,3]
> =F Q=A move in horizontal direction. LCS [1,3] = ) R=M, Q=0
P#Q = LCSI6N = max(ics Pi#Q > LCS|9,3) Solution setS = {F,A,B} PQ => LCS j] = max (LCs fi-1, jl,
1S, 1], LCS [6, 0)
1 =max (LCS (8, 3), LCS (9, 2]) = max(3, 2) = 4 i=3,j=1,P=CQec Les fi,j- 1)
LCS 19.4)> P=F, Q=F P, = — Qy Soadd Pi to the solution set. And move 3),
LeS(t,3] = max(LCS{0,
F=Q = Les,4 diagonally back.

Scanned by CamScanner
cS 13.4) > PTanaysis of Atgorthms (MU = Som 4 Comp)
of Algorithms (MU = SOU.
Lest), 2)
we max(d, 184 qeM
p2Q

ics 3,4)
> Les (i,j) =
* max (LCS [2,4],
Ls (3,3) = max (1,2) = 2
. Sth
P, = Q 80 add Pi to solution set. And move diagonally State "Principle of Optimatty’,
(Ans. : Reter section
4.2) (2 Marks)
Dynamic Programming
= \
Je* REM, pack.
Solution setS = (M,O,N)
ies [at aiet * petcsti- td row 4: ‘What is dynamic programming approach to solve
pnQ, = LCSIhi) Com; putation i=for jel Qam b= 2d= L.Q=M the problem? (Ans. : Refer section 4.2) (4 Marks)
is derived from LCSti— 1, ), $0
vest 2 PO sep4:
p,#Q, and LCSG,j]
= 14LCS(0.3)
LCstal ‘What is the principle of optimality in dynamic
po, 2 LCSTid) = max (LCS fi- 1 .jbLCS (i,j yp)gy
i
move in vertical direction.
2 1402)
Programming 7 Give a suitable example for this
2+
4
sea} = mae LCS 13. ALCS 4,0) = max Solution set = [M,O.N) Property of optimality in dynamic programming
Compututlon for row d e h Piel Qe So finally, table would be, (Ans, : Flefer section 4.2) (7 Marks)
2 «1
Les [a1 2 ess.21 wind 12% Pie Q=N
Table P. 4.11.3(a) Discuss the elements of dynamic programming.
ny#Q,-> LCST =m 7.0, 2 LCSIiil= xLCSI SELES hj Uy (Ans. : Rafer section 4.3) (4 Marks)
LCS [i= 1 J) LCS ti J~ WD = max (LCS (ET = max (LCS [3, 2]. LCS [4,
1) = max (2,
a; >
What are the common steps in the dynamic
Les (2,1) Les4.2)
2 ofifelsla
a
LCS 12, 0) Programming to soive any problem?
= max (1,0) = 1 Les (4.3) i= 4 j=3, P20, Q=so u[wlo|m (Ans. : Refer section 4.348 Marks)
Pele gen a. Distinguish between dynamic programming and
Les (22) 2i=% 2% ololololololo
p.=Q, > LCSIiil 14LCS[i- 1.j- 4
[i-1 J) 1=9, 1+2=3 divide and conquer technique.
P,#Q,= LCSIAd) = max (LCS Lestia} = 1FECS(3.2= P, 1 Mj)Oo 4 1 1 4 (Ans. : Refer section 4.4) (6 Marks)
Les fi,j- 1) Lcs [4.4] vied J24 =O. Q=M
20, 7 > LCSHaile max LCS fi J}LCS bj My
[1.2]. | 2{cjol whith "© Syllabus Topic : Multistage Graph
LCS (2,2) = max (LCS
(2,3)
Les (4.4) = mae (LCS 13,4], LCS (4, 3)) = max 4 a. Writa a short note on Multistage graph.
LCs (2, 1D 3)N]0 Vy2 2,2

= max(Il) =! = 3Computa row 5 :


fortion 4)0|,0]1 2);3\|3 (Ans. : Refer section 4.5) (5 Marks)
i=5, j=! P=M. Q=M (Dec. 2016, May 2017)
LCs [2.3] sis? Les (5.1) = 5 M o 1 2 3 4
P#Q, = LCSIij) = max (LCS
P=Q> LCS [i, jJ= 1+ LCSLi- 15-1) a What is multistage graph? How to solve it using
LCS fi,j- ID 14+LCSH4, 0}= 14+0=1 dynamic programming?
LCS (5, 1] siepS: i= }J=1,=M,Q=M (Ans, : Reter section 4.56 Marks)
LCS [2,3] = mariLCs{l, 3),
Lcs{5.2] 2i=5. 1=2, P=M, Q=N P, = Q, s0 add Pi to solution set. And move diagonally
LCS|2, 2))
P4Q> LCS [i,j] = max (LCS fi- tj],
LCS i, j- 1) Ex. 4.5.2 (10 Marks) (Dec. 2014)
= max(1, I= back.
Les (5,2) = max (LCS [4,2]. LCS (5. 1) * Solution setS = {M,O,N,M] "@ Syllabus Topic ; Single Source Shortest Path
LCS [2,4] 2i=2, j=4, P=L, Qj=M ax (2, 1) = 2 |g,
P.#Q,= LCSIij) = max (LCSfi-1.jh |, P=M, Q=0
Moving diagonally back i and j become zero. So stop. How to find single Bellman-Ford algorithm?

LCS |i, j- 1)
LCS [5,3] = i=5.
We have collected characters from the last position of the (Ans. : Refer section 4.6)(4 Marks)
(LCS i- 1. j], LCS [i,j- 1)
LCS [2, 4) = max (LCS (1,4),
RzQ> LCS [i, j]
= max
string. So reverse the solution set, which is the LCS of P a. How to solve single source shortest palh problem
LCS [5,3] = max (LCS [4, 3}, LCS [5, 2]) = max (3,2) and Q. using a Bellman-Fard algorithm?
LCS (2, 3})
So, LCS = MNOM (Ans. : Refer section 4.6X4 Marks)
= max(1,1)=1
LCS [5,4] >i =4, PR.=M, Q=M Ex. 4.6.2 (10 Marks) (Dec. 2015)
Computation for row3: > i=5, j=4. R=M,
LCS [5,4] Q=M 412 Exam Pack
(May 2013)
LCS [3,1]
=> i=3, j=]. PR=N, Q=M Ex, 4.6.3 (10 Marks)
1 +LCS{i- 15-1]
P=Q,=> LCSFi,jJ= (University and Review Questions)
P2Q> LCS{i,j] = max (LCS [i-1.j), 1 Syllabus Topic : All Palr Shortest Path
LCS[5,4) = 14LCS[5,3)= 1+3=4
Les fi,j-1) Longest common subsequence for given strings is of
F Syllabus Topic : General Method
Explain all pair shortest path algorithm with 2
LCS [3,1] = max (LCS (2,1), length 4. Let us find LCS of P and Q: Q. What is Dynamic programming? Is this the . suitable example.
i=5,j=4,P,=M,Q=M are the (Ans. : Refer section 4.710 Marks) (May 2014)
LCS (3, 0)) Step1: optimization technique? Give reasons. ‘What
t path
= max(1,0) = 1 P, = Q,, so add Pi to solution set. And move diagonally drawbacks of dynamic programming? a.
Wite Floye's algorithm for all pairs shortas
LCS [3,2] >i=3, j=2, R=N, Q=N back. (Ans. : Refer section 4.1.1) (5 Marks) " and find time complexity.
(7 Marks)
=Q > LCS [i,j] = 1+LCSfi-1,j-1) Solution set = (M} @ What are the characteristics of | Dynamic (Ans, : Refer: section 4.7)
usp Scheduling
iso. V+Lesp, t)= + Step2: i=4,j=3,P,=0,Q=0 Programming? (Ans, : Refer section 4. 1.3) (4
Marks)
s@ Syllabus Tople : Assembly-Line
c ntl i=3. R=N, Q=0 P, = Q, so add Pi to solution set. And move diagonally Q State applications of Dynamic Program
ming.
Explain Assembly fine
scheduling.
P
1#Q> LESH j) = max (LCS fi- 1 JRLCS fij- 1) back. (Ans. : Refer section 4.1.4) (3 Marks)
a
Marks)
(Ans. ; Retor section 4.8) (7_————_—————_
LES(43) = max(LCS[2, 3], LCS[3, 2]) = max(1, 2)=2 SolutionsetS = {M,O)

Scanned by CamScanner
Gina)4
Sales: man
ve ravelling *oteg
Define and sol ramming.
usin g dynamic rog (10 Marks) OD Weleuaisal
: OF kna psa ck er sec tio n 4.1 0)
syllabus Topic (Ans. .Ref Marts
o blem with *
bet toe 204 10
204) sx aroa emo fi
Greedy Algorithms
apsack Pro (may .
a. Explain 0/1 Kn 49) 10 Mare)
ion c
ian Roo goc sack problem using dynami Top ic : Longes!
©o Syllabus
7 (10 Marks) | | subsequence win
(ANS. °—s section
4,9) ne Explain longest common &
° pronegrae mmiveng. tans
ftusing example. (May ag, rm
(Ans. + Refer section 4.4 1)(10 Marks) l pr )
Defing the knapsack problem. How
to solve
lier ana
a.
ing? What is etlongest oblen
dynamic programmn Qa.
n Single source short
est ‘i a
4.9X5 Marks) (Ans. i fersectio
.
path, Knapsack problem, J
(Ans. : Peter section i g dynamic ic
How to solve it tek,
General Method,
kal and Prim's algorithm, Optimal Viorage enta
pes. oiemdiiel
psack problem usin Marks) | Q,
ria
What is LCS problem? Minimum cost spanning trees-Krus
@. How to solve kna - eter section 4.9.1) (5 in detail.
programming (Ans. dynamic programming? Explain s)
) (7 Mark
Travelling Salesman
Problem (ans. : Refer section 4.11 + Once the choice is made, it cannot be
sa Syllabus Topic: person problem.
(May my rejected)
altered, i.e. if a feasible solution is selected (orlected)
Travelin g sales Marks) Syllabus Topic : General Method i
Q. — Write short on (May 2015)
Ex 4.11.1 (10 = in step i, it cannot be rejected
4.10) (10 Marks)
(Ans. : Refer section subsequent stages. oa
4_General Method 5.1.2 Control Abstraction
ad Introduction
- > May 18)
"yan is the greedy algorithmic approach? Tx Wite an abstrac algorithm for greedy design mothod,
solutions with method.
@.._ Explain the control abstraction of the greedy (Marks
When the problem has many feasible the best solution is
)
different cost or benefit, finding
known as an optimi zation proble m and the best Greedy algorithm derives solution step by ster, by
le at the current moment.
optimal solution. Jooking at the information availab
solution is known as the
there exist uncountable prospects. Decisions are
In real life scenario, It does not look at future greedy
such as make a change, The choice made underwe have
completely Tceally optimal.irrevoc
optimization problems able, means ance

=
ing andsoon. Solution procedure are
knapsack, shortest path, job sequenc and many other , it cannot be backtracked.
Like dynamic programming selected the local best solution approach. is
techniques, greedy algorit
hms are also used to solve Control ubstraction for the greedy
optimization problems. are very
described below:
algorithms is that they
‘The beauty of greedy and and quick
10 underst ‘Algorithm GREEDY_APPROACH( 0),
simple in nature. They are easyalgorithms are the least [Description : Solve the given problem using sredy approach
t

to build. Perhaps greedy n


zation techniques. List of possible chotoes, nt size of solutio
complex among all optimi
n sep by step, by n of given problem _
Greedy algorithm derives solutio available at the current
+ Set Solution containing solutio
tion
fooking at the informa
look at future prospects.
Decisions Satect locally optimal,
moment. It docs not constructs inevocabla choice from L
optimal. This method
are completely locally at current benefit
looking
the solution simply by they
possibilities and hence Choice + Selear(L)
‘without exploring future
Check i selected
are known as greedy.
greedy solution procedure
are thea | choico is Teasible of not
The choice made under have selected the local best
i {feoble(Choice U Solution)
means once we YSolution.
imevocable, Solution € Choice Z
packtracked. Sa
solution, it cannot be
step in the greedy method
Thus, a choice made at each 7 end
Add feasible choice
to partial satution
should be:
Feasible : Choice should satisfy problem constraints. yeturn Solution

Locally optimal : Bestshouldsolution from all feasible


be selected.
solution at current stage

Scanned by CamScanner
Define and solve Tray
using dynamic prog a

(Ans. : Refer section 4,10) (19


Ex. 4.104 (Dec. 2014, 10 Marks)
sx Syllabus Topic : Longest Con, D Beas
Explain longest common §
a
Greedy Algorithms
(Ans. : Refer section 4.11) (19
What is a longest common
Sut
(Ans. : Refer section
4.11) (shea
What is LCS problem? Hoy ) %.
dynamic programming?Epica Xe
en
General Method, » Single
Sir source shortest path, Knapsack problem, Job sequencing
(Ans. : Refer section 4.11) Mange wi i
Minimum cost spanning trees-Kruskal and Prim's algorithm, Optimal storage cities
9 wih eadlnes:
Ex. 4.11.1 (10 Marks) )

- 1 + Once the choice is made, it cannot be


Syllabus Topic : General Method altered, i.e. if a feasible solution is selected (or rejected)
in step i, it cannot be rejected (or selected) in
5, 1 General Method subsequent stages.
5.1.2 Control Abstraction
5.11 Introduction
> (May 15)
Q. Whatis the greedy algorithmic approach?
| Q... Write an abstract algorithm for greedy design method,
(5 Marks)
Drees - May 2015,5 Marks
When the problem has many feasible solutions with
Q. Explain the contro! abstraction of the greedy methad.
different cost or benefit, finding the best solution is
known as an optimization problem and the best
(3 Marks)
solution is known as the optimal solution. Greedy algorithm derives solution step by step, by
In real life scenario, there exist uncountable looking at the information available at the current moment.
Tt does not Jook at future prospects. Decisions are
optimization problems such as make a change,
completely locally optimal. The choice made under greedy
knapsack, shortest path, job sequencing and so on.
solution procedure arc irrevocable, means once we have
Like dynamic programming and many other
selected the local best solution, it cannot be backtracked.
techniques, greedy algorithms are also used to solve
Control abstraction for the greedy approach is
optimization problems.
described below :
The beauty of greedy algorithms is that they are very
simple in nature. They are easy to understand and quick Algorithai GREEDY_APPROACH(L, n} i
to build. Perhaps greedy algorithms are the least { Description : Solve the given problem sing greedy approach -
complex among all optimization techniques.
UYapur L: List of possible choices, a: size of solution
Greedy algorithm derives solution step by step, by
// Output : Set Solution containing solution of given problem.
looking at the information available at the current
moment. It does not look at future prospects. Decisions
Select locally optimal,
are completely locally optimal. This method constructs Solution — @
Imevacatle choice from L
the solution simply by looking at current benefit fori <—1 tondo
‘without exploring future possibilities and hence they Choice <— Select(L)
are known as greedy.
are
The choice made under greedy solution procedure. ‘Check if selected
the local best if ((easible(Choice U Salution)) then | choica is feasible or
irrevocable, means once we have selected 7
solution, it cannot be backtracked. Salution Choice U Solution
Thus, a choice made at each step in the greedy method ~ :
end
should be: . end Add feasible choice to partial solution]
Feasible ; Choice should satisfy problem constraints. return Solution
feasible
Locally optimal : Best solution from all
solution at current stage should be selected.

Scanned by CamScanner
52 Gi RELY Alor,
Gfanavssis ofof Algorithm
Algorithm ( (MU - Sem Comp)
4_
[Fanatysis of Aigothm (MU - Sem = Comp) Global OS
the optimal
The ‘greed! ty approach does : not ensure ‘ure s,
si maximum Local a Divide and conquer: Greedy Alor
provide ee maximum:
solution, but in mest of the cases, it
approximation. For certain problem s, aren alas jo. |—____________|
|= obproblem is solved Syllabus Top
ic : Single
Is ue feasible Solutions Source Shortest Path
produces an optimal solution (like Prim's
and ,
independently and
algorithm). If greedy provides the optimal solution, then itis solutions of thé smaller 5, 2 Sinee
gle Source
Source Shortest Path
Shortest Pa
the best among all other techniques. problems are combined to th
Local find the solution to the
5.1.3 Characteristics Global
maximum maximum large problem.
> (May 14)
H re Fig. 5.1.2: Cost curve Subproblems are Greedy algori
@. Comment on the module of Teen thm doesTot

rs
Mathod. independent, so DC Consider the previo The. nieck
— Suppose we want to find maximum number f; usly distance a“ Pils Widely accepted data structure to represent
Q. Enlist and explain the characteristics of greedy. binary tee in Fig. 5.1.1. We start from theroot, any might solve same Solved instan
ce again, thus P. The distance between cities effectively
algorithms. ( (4 Marks) are greedy to find the maximum element, at each subproblem multiple it avoids the Tepresented Using graph.
level
Problems which can be solved using the greedy method we compare local maximum element with its two time. fe-computation, Dijkstra proposed an efficient way
generally possesses following two interesting properties : children and will take a decision based on that leva] | + ee - - shortest path from the weighte to find single source
4, [DC approach is recursive Greedy Source vertex s, the algorithm
d graph. For given
algorithms
Characteristics only, without exploring future branches,
jn nature, so it is slower
are
every other vertex finds the shortest path to
of Greedy Method At level 1, the algorithm selects the branch having g
iterative in ature and v in the graph.
maximum value, so-it selects the left branch ag it
and inefficient. hence faster, Assumption : Weight of all edges is non-negative
(1) Greedy choice property value 42 which is higher than right branch value 19, Steps of the Dijkstra’s algorithm are explained here:
5, |Example : Merge sort, Example Knapsack
: 1 Initializes the distance of source vertex to zero and
~ Hence, the algorithm terminates with 73, insteaq of Quick sort, Binary Problem, Huffman codes,
(2) Optimal substructure finding 91 as best solutions. Greedy approuch fails tg Temaining all other vertices to infinity.
search. Minimum spanning ee, |) 2+ Set source node to current node and put remaining
find a maximum element of this case. all
Fig. C5.1 : Characteristics of Greedy Method ~ Same way, in some search space, suppose we are at
nodes in the list of unvisited vertex list. Compute
the
> (1) Greedy choice property 5.1.6 Dynamic Programming Vs Greedy tentative distance of all immediate neighbour vertex
some point p as shown in Fig. 5.1.2, if the problem Approach of
isof
— The global uptimal solution is derived by making maximization or minimization, in no case greedy can
the current node.
locally optimal choices, i.e. the choice which looks best > (May 13) If the newly computed value is smaller than the old
generate an optimal solution.
at moment. Q. Write a short note : Differentiate: between: greedy value, then update it.
~- Greedy can achieve only local maximum or local
~ If the choice is feasible, then add it to the solution set approach and dynamic programm For example, C is the current node, whose distance
and reduce the problem size by the same amount.
minimum solution in discussed case. from source S is dist (S, C) = 5. Consider N is the
The current choice may depend on previously made 5.1.4 Applications of Greedy Approach Q@. Compare dynamic programming with the greedy neighbour of C and weight of edge (C, N) is 3. So the
choices but it does not depend on future choice. approach. (6 Marks)} distance of N from source via C would be 8. If the
Greedy algorithms are used to find an optimal distance of N from source was already computed and if
The greedy choice is irrevocable, so it does not or near
optimal solution to many real-life problems. Few it is greater than 8 then relax edge (S, N) and update it
reconsider any choice. of them
are listed below: to 8, otherwise don’t update it.
Greedy choice property helps to derive optimal solution Sr. Dynamic Greedy Approach
by reducing the problem size by solving local optimal (1) Make a change problem
No. Programmi 5 5
(2) Knapsack problem
subproblems,
(3) Minimum spanning tree 1. |It guarantees an optimal | It does not guarantee an
> (2) Optimal substructure (4) Single source shortest path nv 7
~ We say given problem exhibits optimal
solution. optimal solution.
substructure if (5) Activity selection problem
the optimal solution to given
problem contains the (6) Job sequencing problem
Subproblems overlap. _ | Subproblems do not
Optimal solution to its subprob
lems too, In the Problem (7) Hoffman code generation. . overlap. (8, N)=11 a(S, N)=7
which possesses the Optimal
choice always leads to an optimal
substructure, best next a(S, C) + d(C, N) < d(S, N) d(S, C) + a(C, N) > d{S, N)
solution. 5.1.5 Divide and Conquer vs. Greedy It does more work. Tt does little work. = Relax edge (S,N) = Don’t update d(S, N)
© Cases of failure Algorithms Update d(8,N)=8
Considers the future |Only considers the cusent
In many cases, the Sreedy
algorithm fails to generat
the best solution, Somet
imes it may even create
e Sr. Divide and conquer choices. Fig. 5.2.1 : Weight updating in Dijkstra’s algorithm
Worst solut the Greedy Algorithm
ion. There is no specialized| Construct
the solution from’ are explored,
When all the neighbours of a current node
@) Divide and conquer is/A greedy
algorithm is set of feasible choices. _ |the set of feasible solutions.
1,
‘ mark it as visited. Remove it from unvisited vertex list.
list with
used to find the solution, optimi Mark the vertex from unvisited vertex

@Q (2)
zation technique. It Select choice which is|Select choice which is , j
it does not aim for theltries to minimum distance and repeat the procedure
i globally optimum. locally optimum. is tested or when
optimal solution. 2. Stop when the destination node
© ©) &@ Lt | Employ memorization. There is no concept_of , unvisited vertex list becomes empty.
DC approach divides the
Fig. 5.1.1: Binary tree memorization.
Problem into small
ibprob

Scanned by CamScanner
ay
Se a }
orth (MU - Sem4 - Comp)
= Ty Show the final shortest path veo
[Ef anatysis of A below = /
a Igorithm is described
‘ijkstra’s shortest path TEST_PATH(G, 4 ) soln. : PT anatyste of Algorithm (4 - Som 4 Comp)
hax DUAKSTRA_SHOR' Here, source vertex is A. ‘Here, vallF] < dist{F], so updatedisttF]
dist{u] indicates distance of vertex u from, w.dist{F] = 7 and a
8 ‘OUneg
werier
nu] indicates parent / previous node of y val[G] = dist{B] (F=8
+ weight, gy = 444

ei es
fu) stares theparesit ‘t
ingrahE InItialization
= 146
=a
= 8
Here, vallH] < dist(H), so update dist{H
)
Initaize aistance of source node dist[source] =O = dist{A]=0 Here, val{G] < dist{G}, so Sis] = 8 and niet] ep
ina]«= NIL0 Stores parent of node s update dist[G]
nj] n{source]=undefined =>7% [A] =NIL sdistfGl_=_7nd niG]=B
foreach veres ¥ CV Stores parant of node $ dist{B] = dist(C] = dis{D) = disepey — dist mR, Vertexu | A
dist[G]= dist(H] =e Q= | dist) | 0,
ifvss thea n{B] = IC] = x(D] = a[E] = 2[F] = mG) = Aer, Neration 6
pletion
_ j and ru) Nit
dist[v] °2
Initialize distance
poration 2
[=[=
|
4 = un
a
[me a = ty
veinre Q having
[o_| minimum dist{u} =G x
ENQUEUE(*, Q) Tinsert vito quewe Q u = unprocessed vertex in
lead Qhaving minimum dist{u) =c
“GrenfGl = fae
hile Q is not empty do
u€ vertex in Q having minimum dist{u]
;
Iteration 1 (0.6) un
~e
5 spree

fu ==tthen u= unprocessed vertex in Q having minimum dist(u) = 4 = ae ‘ weight(C, Dy


= = Here, val[F) < dist[F), so update
break Path from source s to target Adjacent{A] = (B, E, F} dist{F]
is constructed Here, val[D] < dist{D}, so update “distF] = 6 anda[Fj=G
end
val[B] = dist(A]+ weight(A, B) dist(D] .
vallH] = dist{G ]+ weight(G, H)
DEQUELE(u. Q) f Remove
u from quete @ = O+1
-dist{D] = 4and nD] =¢ ne

val[G] = dist{C]
+ weight(C, G) -¢€
+1
far each adjacent node v of u do = 1
val @ dist{u] + weight(u, ¥) Here, val[B] < dist{B], so update dist{B] = 3+2=45 Here, val[H] < dist(H}, so update dist{¥]
ifval < dist{e] then Here, val[G] < dist{G}, so update dist{G] dis] = 6 and nfl] =G
, | ndist{B] = 1,andrfB)=A
vdis[G] = Sandnf[G]=c
ahr eu val[E] = distlA] + weight(A, E)
Vertexu | A
end = O+4
ead = 4 Q= | sty [0
Here, val] <dist{B}, so update dist{E] atu) | Nii
© Complexity analysis dist{E) = 4 and n[6]=A
First for loop does initialization in O(IV1) time. As there eration 4 U = unprocessed
vertex in Q having
val[F] = dist{A}+ weight(A, F)
are IVI nodes in the graph, size of queue Q would be V, and u = unprocessed vertex in Q having ‘minimum dist(u)
=F
= 0+8
hence while loop iterates VI times in worst case. For loop ininimum dist{u] = E Adjacent{F] = { }
inside while loop run maximum IVI time, because a node can = 8 Adjacent[E] = {F} So, no change in table
have maximum {VI - 1 neighbours. The worst
case upper Here, val[F} <dist[F], so update dist[F] val[F] = dist{E] + weight(R,F) [olelr|
bound running time of this algorithm is described
sdist(F] = 8 and n[F]=A
= ae
as O(IV")).
Note :_Dijkstra’s algorithm cannot handle
[vetxuTa Tato To Jelrle [n o-| aes fo [i [a la [ate|
_.
negative weight
Ex, 5.2.1 ed » |alele Here,
.
val[F] > dist{F], so no change in table [eu __[wefalalclalal
venoxu[a [sl clo[elrlc]H| tion 8
| Meration sent seinen
Z\s

Suppose Dijkstra’s algorithm is AJ NIL] NIL] A| A] NIL


run on the following graph,
z=

starting at node A, Shaded cells are processed vertexIteration 2 -|a . u = unprocessed vertex in Q having
1
us Unprocessed vertex in 9} ast fo [ta ]s]a|r|s] rinimum dist{a} = H
aul [falsfelataje|m]) | von= ty
Qhaving minimum dist{u] = B
Adjacent{B) = (C, F, G} Neration 5 So, noel hange in table
inQ tari [verexu [aJelclolelF
lala
"pine
minimum def-D o-[aut_lo
[ela felele ls [s|
val[C] = dist{B] + weight(B,
C)
1+2 dist{u] = D racetelelatatelal
“m0

Fig. P. 5.2.1 3
MODI- = en)
di tT -
at‘ily table, the shortest path treeindicates
derive the for given
In theshot table, fu]
Here, val{C] < dist{C), so update val[G] dist{D]
+ weight(D,
“ rates ye showing dist[C]
G) em the
the intermediate distance
Sorin.of the nodes at each ach sdist(C] = 3 anda(c] =B
iteration
iterat i of the set eet node of vertex u. The shortest path tree is shown in
val[F] = dist{B) + weight®, F) = 3 Fig. P.5.2.1(2).
= 1+657 Here, val[G] = dist{G], so don’t update dist[G]
val] = dist{D] + weight(D. H)

Scanned by CamScanner
Brest |
‘Shaded cells are processed i ysis of Algorithm (MU - Som 4 Comp)
Heration 2 Ss] + (5,7)
u = unprocessed vertex in
Qhaving minimum dist{u) = 2
Here, val[7] <dist{7], so update
Adjacent{u]= (3) dist{7)
val[3] = dist{2] + weight(2, 3) wdist{7] = 42 and prev[7] =5
Fig. P. 5.2.2(a) = 10+20 venexu | 21 a] 6Ts[aTa
= 30 O=| dstul [0 | 10 | 20 | 90 Fas | ag Pm
Ex. 5.22
Find minimum distance path from
vertex 1 ta 7.
Here, val[3] < dist{3], so update dist[3)
pet) [| Te Taps lets
EY s.dist(3] = 30 and prev[3] =2
Rteration 6
u = unprocessed vertex in Q having a minimum
yertex 7 is the target vertex, dist{u) = 7
so stop, Let US trace the path,
Vertex |is the source vertex, From above table, .
Vertex | is predecessor of vertex 2, path: 1-2
Vertex 2 is predecessor of vertex 3, path: 12. — 3
u = unprocessed vertex in Q having Vertex 3 is predecessor of vertex 5, path: | 23-5
minimum dist{u] = 3 Vertex 5 is predecessor of vertex 7, path: 1-23-57
Fig. P. 5.2.2 Adjacent{u] = (4,5) Hence, the shortest path is 1-23 _ 5-7 and the
val[4] = dist{3]+ weight(3, 4) path has cost 42.
Soln.: Here, source vertex is 1 and destination vertex is 7.
= 30+15=45 Ex, $.2.3
Initialization
Here, val[4] < dist([4], so update dist{4) Find the shortast path from the source vertex A using
dist[source] =O => dist] =0 <.dist{4] AS and prev[4] = 3 Dikstra's algorithm. '
prev[source] = undefined => prev[1) = NIL val[5] = dist[3] + weight(3, 5)
dist{2] = dist[3] = dist{4] = dist[5] = dist [6] = dist{7] = 00 = 3045235
prev[2] = prev[3] = prev[4] = prev[5] = prev[6) Here, val[5] < dist[S}, so update dist(5]
= prev{7]
= NIL. s.dist{[5] = 35 and prev[5] =3
i_[2 [a [4 [5 [oe [7 [vortexu [1

Iteration 1 eee MU - May 2014, 10 Marks


Iteration 4 '
U= unprocessed vertex in Q having minimum
disi[u] = 1 4 = unproc vertexess
Ta find Dijkstra's shortest path from vertex 1'to
vertex4 for the following graph:
Adjacent[u] = (2, 6} in Q having
ed
vall2] = dist + weight(1 minimum dist{u] = 6
(i], 2) Adjacent[u] = (7)
= 0+10
val{7] = dist
= 10 + weight(6,
{6] 2
Here, val<(2] dist{2), so update dist{2] = 30435=65
+dist(2] = 10, and prev{=]| Here, val[7] < dist(7}, so update
dist{7]
U6] == ditt)
+ weft 6 “dis{7]_ = 65 and prev[7
Fig. P.5.24
[venexu] 1 | 2
o+30
a Q=
[9 | 10 [30 [20
[2 [3] 6] Soin. :
Fa
Hes ‘all6]
= 30 <ang
dist(6), so= update dist{6]
“-dist{6) previ6] 1 [ nat | P| Visit a
isited vertices 3 oa - 7 4 Comment
Iteration 5 ‘ L 2 ols 6 .
g
U = unprocessed vertex in List distance of vertices
3 | from L. select the
Qhaving minimum dist{u] =5 7 oo © - vertex having minimum
Adjacent{u] = (7) tll
distance, ie.3

Scanned by CamScanner
Greed,
otithm (MU
58 Al hg
‘Analysis of Al - Sem 4 -: COMP) )
Bn — disi{u)
4
*
le| Comment
Visited vertices |, 3 Visit remaini ~ing ve
+ from 1 vin @ "tlcas
in {0,3 +6) distance ang’ 4 |min(11,4+<0}
min{7,3+2} | minfo3+5} | oo 79 3 vertex having ming, © A-B-E =11
1-6 =5 =8 distance from 6, j my

in (9, 5+}
5 min(8,5+4) |, as { to a Visit remam
1-6-2 = - minimum distancstan,
e “8 A-B-E|, [mint =) fron mind venices
E,ie.3 $e from 2 =i from A via B, E and G. .
"6
Visit remainin B Vertices
from 1 via 2,
min{e,8+8) — { min (9,8 +5} 3 | update distance ang 3, |_—_+__}—___
1-6-2-3 =16 =9 to vertex having
minimum distance
3, ie. 5 from -
A-B-E), in{ 11, 643)
|min(l,
Visit remaining vert
-G-F =9
=min (16,943)
5
from 1 via 6, 2, 34
a 3 | update distance
and gp
1-6-2-3-5 5 8 ~ | to vertex havin,
inimum distance fro mi
S,ie.4 nf
‘Shortest path for the given graph is: !-6- 2-3-5 oe-B-E 2 .
|mini2, Brey =,|
-G- =D +
Ex 525) Br SNES
a 2)
Find the shortest path from source vertex A using Dijkstra’s algorithm
9
10
mi
nf Visit remaining vertices'
A-B_-E 10, from A via B, E, G, F,
-G-F-|2 |9 94/4 6 5 8 Hand C, update
i Fig. P.5.2.5 H-C 4) distance and go to
= vertex having minimum
13 distancefrom Cie. D
dist{u]
- Bold edges indicate the minimum cost path in the Syllabus Topic : Knapsack Problem
following figure.
List distance of vertices}
from A. select the
5.3 Knapsack Problem
vertex having minimum > (May 17)
distance, i.e. B
Visit remaining vertices problem. é
the greedy|
: (249 from A via B, update Q. State and solve knapsack problem using
(6 Marks)|
method,
=4 distance and go |
vertex having minimum
distance from B, ie. £ Fig. P. 5.2.5(a)

Scanned by CamScanner
Greedy a
«= gem 4+ Comp) $10 sith, sig of f lg Algorithm (MU - Sem 4.
BiTanaiysis of Algerie (Mu - So while iS n do
if (SW + wfi]) SM then se Of Slested ite
Problem Raviyingn 80 some weight and
und sesuXtil Addition of Item does not gy teres *e, Knapsa ms, $= |]
savers a set of il Heras The knapste problem i ® knapsack capacity, so add whotg a ck capacity M = 29,
Giver qgsociatedsucwilh tHthat the fotal weighgist es
is Jess et ™ SOT SW E SW iw) 0415
era
Hes
value/thepeoftseoftiver
fingd 215
res} | saarseeth
swesw + Wii]
Whole item Is added
spest + Vil to solution sat
We shallseleet5 one by one item [eae]
7 from above lable. If the | tena ey nae oes
SW 4W1>M, sobs i J,
‘The remainin, . + 50 break down item
into pasts, into parts. Compute the item docs not cross the knapsack capacity,
ofit, anOtherw select only Susteren knapsack is 5 unit, so
Fractional knapsack: Item 1 can be divided fraction to be added i clus ise, break the current ite
lem frac (MSW) / WE] thea fe portion of item equivalent to eemaj ™ and select fae -SH)/ WE) = 0-18) 118=51 18
Fractional Knapsack Prob $e SUNXL] * frnel/ Add fraction of tew Xfi] only Tm ‘Select the profit accordingly. We sh ining knapsack =(ht*sng)
Add fraction of profit capac js full or all items are scanned, ould stop when
SPESP + VE fie SP=SP +, * frac=25 +24
SW SW + Whi] * frac // Add fmetion of weight ks pitalze, Weight of selected items, SW = 0, * (5/18))

jend items, SP =0,


=25 +6.67 = 31.67
ieitl Select next item profit of selected SWE SW + wy frac= 15 +(18 * (5/18))
end get of selected items, $= { }, =1545=20
Here, Knapsack capacity M = 100, Knapsack is full. Fractional Greedy algorithm
set of weight and value associated with each , i.e. = Complexity analysis selects
respectively. Let M is the capacity of the knapsack iteration 1 SW= (SW + w,)=0+40-=40 “ems { In 1, * 5/18 }, and it gives profi
of 31.67t uni
more
knapsack cannot hold items having collective ofweight
filling the For one item there are two choices, cither to select or , I,
sa select
sW <M
than M. Knapsack problem is the problem the profit reject. For2 items we have four choices:
knapsack using items in X such that it maximizes © Select both items sz {Js} SW =40, SP =0+40=49
and collective weight of added items should not cross the o Reject both items Iteration 2: SW = (SW + w,) =40+60= 100
capacity. ;
In fractional knapsack, breaking of the item is allowed. o Select first and reject second sw < M,soselectI,
of value to weight o Reject first and select second
If items are selected in decreasing order s=[1,1, }, SW= 100, SP =40 +50
=90
ratio, fractional knapsack guarantees the optimal solution. | _ In general, for n items, knapsack has 2° choices. So
With fractional knapsack, knapsack would be full at the end brute force approach runs in O(2") time. Knapsack is full, no more items can be added to knapsack,
of the algorithm. | _ We can improve performance by sorting items in Fractional Greedy algorithm selects items [ 1), 1}, and it
Binary knapsack does not guarantee an optimal advance. Using merge sort or heap sort, n items can be gives profit of 90 units, which is optimal.
solution, and knapsack may not be full when algorithm sorted in O(nlog,n) time. Merge sort and heap sort are
halts. non-adaptive and their running time is same in best,
Knapsack problem can be formulated as, average and worst case. If items are selected according to decreasing onder of
a a Consider the following instances of the fractional knapsack
20) profit to weight ratio using fractional knapsack strategy,
To select the items, we need one scan to this sorted list, problem: n = 3, M= 20, V = (24, 25, 15) and W = (18, 15, then algorithm always finds the optimal solution, First _
Maximize Y” v,x,subjected to 7 wjn<M ~ which will take O(n) time. find the feasible solutions.
i=l i=l - So total time required is T(n) = O(nlogn) + O(n) arrange items in decreasing order of profit density. Ttems are
XE (0, 1) for binary knapsack Sofn.= labeled as X = (3, 1,2, 4,5), have profit V = (27, 18, 25, 10,
= O(nlog,n). 15) and weightW = (4, 3, 5,3, 6}.
x€ (0, 1) for fractional knapsack Let us arrange items by decreasing order of profit
Algorithm for fractional knapsack is described below: Ex. 5.3.1 density. Assume that items are labeled as X = (1, I, I),
Algorithm GREEDY_FRACTIONAL_KNAPSACK(X, V, W, MM) have profit V = (24, 25, 15} and weight
W = {18, 15, 20}.
Find an optimal solution for the following knapsack instance
1/7 Description : Solve the knapsack problem using greedy approach using the greedy method. Number of objects n = 5, capacity Item (xj) | Value (vi) | Weight (wo. | p= ¥.4¥;
#Tnpot : X: An array ofn items of knapsack M = 100, profit V = (10, 20, 30, 40, 50), weights hb a2 | is {| 16 |
Vz An array of profit associated with each item W= (20, 30, 66, 40, 60).
Ws An array of weight associsted with each item
Mz Capacity of knapsack Solin. :
if Ourpat: SW: Weight of selected items If items are selected according to decreasing order of table. Ifthe
_ We shall select one by one item from above table. Ifthe “We shall select one by one item fram above
SP: Profit of selected items profit to weight ratio using fractional knapsack strategy, inclusion of an item does not cross the knapsack capacity, inclusion of an item does not cross the knapsack cupacity,
select
4 Items are presarted in decreasing order of pi = vi/
wi ratio then algorithm always finds the optimal solution. then add it, Otherwise, break the current item and select then add it. Otherwise, break the current item andknapsack
item equivalent to remaining
4 Set of selected items, initially empty
First arrange items in decreasing order of profit density. caly the portion of item equivalent to remaining knapsack
Capacity. Select the profit accordingly. We should stop when
only the portion theof pro
ity. Select it accordingly. We should stop when
Assi lume that items are labeled as X = (I), Typ I5, Iq, I,), have knapsack is fulloral items are:sen owe
if weight of selected items ‘apsack is full or all items arc scanned. ,
Mt profit of selected items profit V = (10, 20, 30, 40, 50} and weight W = (20, 30, 66. =, Initialize, Weight of selected items,
40, 60}. Initialize, Weight of selected items, SW
Profit of selected items, SP Profit of selected items, SP=

Scanned by CamScanner
¢ of Algorithm (MU - Sem 4 - Comp)
= 8 span of 9 tin,oe An
cvs npenne
ady A, Greg

come 5-12 -
“Fett in sft. STOKES Bean
jtially>
.
PET anatyss of Aigorthm, (aS==Sems
{1
8 ) job is scheduled in
g(t) =O implies no
stot, ~ y

of slots S(t), S(t) & rn


Initially,
mom COLT
o. 1. 2 3 s
Set of selected items Schedule S is an army 223, jobs, SP=0
ity M = 12. .3..)N) profit of scheduled
Here, Knupsack capac N} foreach t€ {1.2 if SQ) =i, then (<4, (56 . Oo
e4 ble ' 2 8 4 se 7
Iteration 1+ sw=
(SW +s) = o+4 _ scheduleS is feasi i beste jreratlon 1 Profit yates bs SP=0
(= 1 tot =2) is free, 5g | Reratton
ine) 4
item 3 job must meet its deadl adline for job J: is 2. Slot 2
SW <M, so select schedule
SP=0F Q7=27 Our goal is t© find feasibled job. high slo t 2.
Deadlinefor job Jy i
ule it in
s={3 }, Sw =4, of schedule sc 0) t 1 tot = 2) is free,50
maximizes the profit ), and Profit SP= (10 scheduleitinsts Jyis2. Slo 2 (t=
43=7 as follow: Sort at)
;
solution set S = (J,
Iteration 2: SW = (sw+w,)=4 The goal can be achie ved 8 iy
SWS M, so select item | 18=45
decreasing order
of profil. Start
with the
at a time and if it ig
fe
2 o
stl
1 2
18 4 Solution set $ = (J,), and Profit SP = {30}
|
s=(3,1},SW=7,SP= 27+ 512 schedule, select once job stor. asibly ‘ 8 ° Li tTyT tT 4
Jsis. 1. Slot L = Otot= 1) is fee, .so
latest possi ble eration
¥2) = 745 then schedule it in the peadline
sine for
for job 4 2 3 4 8 6 7
Iteration 3: SW = (SW + described below:
SW < M, so select item 2 ‘Alecrithm for job scheduling is a it insset S = (Jy, Ja}, and Profit SP = (100, 27) Kteration 2
NG( 4,D, P) sot edule
solution De
25=70
S=[3, 1,2}, SW=12,SP= 45+ can be added to “Algorithm JOB_SCHEDULI approach eadline for job Js is 4. Slot4 (t=3 tot
= 4) is free, so
the jobs using greedy whi
"7 Deseription: Schedule
Knapsack is full, no more {3,i items1, 2}, which gives the s ee int
_
— maximizes the profit
knapsack. Selected items are [input J: Array of N fobs
o lution set S = (J,, 3}, and Profit
SP = (30, 20)
profit of 70. D: Array of deadline for each
job
each job
erates CTs Tet P14
Sequencing with Dead
lines P: Aray of profit associated with r Job J, is not feasible because first two slots are already o 1 2 a@ 4 6 B 7
Syllabus Topic : Job in J in decre asing or of profit
de
Sart all jobs t=2, it cannot | Iteration3
occupie dand if we schedule J, any time J,later
is discard ed,
pe finish od before its deadline 2. So
job Deadline for job J, is 3. Slot 3 (( = 2 to t= 3) is free, so
5.4 Job Sequencing If job does not miss (5), J}, and Profit SP = {100,27} schedule it in slot 3.
its deadiina Solution set S = Solution set § = [J5, Js, Ju}, and Profit SP = (30, 20, 18)
> (May 14, Dec. 16)
it is empty
$4-@ | Sis set of scheduled jobs, initially
neration 4
lob sequencing with deadli
nes.
not feasible because first two slots
are already 8
Q. Write anote on
SP<0 //Su mieamed
the profit s Job J, is t= 2, it cannot 2 8 4 § 6 7
(ENS ec, 2016, 10 Marks
occupied and if we schedule J, any time later 9 1
olutic n set
‘Add 1d job jobto solutio
sequencing problem using greedy fori 10 Ndo e 1. So job J, is discarded,
Q. Slate and solve job be finished before its deadlin Iteration4
then SP = (100, 27)
(6 Marks)
if Job J[i] is feasible Solution set S = {J,,Ja)5 and Profit so
approach.
Schedule the job in Intest possible free slot’
meeting is
approach, we will be able to schedule
two Deadline for job Jy is 1. Slot 1 (t= Oto t= 1) is free,
with the greedy schedule it in slot 1.
deadline. profit of 100 + 27 = 127 units. Profi t
@ Problem jobs (Tis J,}, which gives a Solution set S = (Jy Jy, Jas Aghs and
Schedule jobs out of a set
of N jobs on a single sesulli]
as much as possible. SP= (30, 20, 18, 6}
processor which maximizes profit Add respective profit
fat TT}
Ex, 5.4.2 UE) 2015, 10 Marks
@ Explanation
end
Solve .the following instan
ce of *iob sequencing.” with,
8 3 4 5 6 7
time for execution. deadlines" problem : 0 1
Consider N jobs, each taking unit
~ 1) = (8,5: 20,18, 1, 6,90)
Complexity analysis
11=7, Profits (Py. Par Par Pas Ps» Pos
the
Each job is having some profitand deadline
associated @ occupied and none of
on or 4 3,2, 1,2): First, all four slots are none of the
most of the time
with it. Profit camed only if the job is completed Simple greedy algorithm spends and deadtines (dj, da ds: da dss dg, Gy) = (1,8.
remaining jobs has deadli
ne lesser than 4. So
as
before its deadline. Otherwise, we have to pay profit looking for latest slot a job can
use. On average, N jobs
Schedule the jobs in such way
$0 aS. to get maximum profit. remaining jobs can be
scheduled.
a penalty. Each job has deadline dj2 1 and profit p,2 0.
to
ach, we will be able
search N/2 slots. This would take O(N’) time. ‘Thus, with the greedy appro which give a profit of
Ata time, only ane job can be active on the processor. e (find and
However, with the use of set data structur schedule four jobs Uy,
Js Sa Jel
The job is feasible only if it can be finished on or time. units.
— union), the algorithm runs nearly in O(N) 40 +20 + 18 + 6 = 74
before its deadline. A feasible solution is a subset of N roars
Ex.5.44 Tienes £543 owing data of jobs.
27) end (Br de
jobs such that each job can be completed on or before
its deadline. An optimal solution is a solution with Let n= 4, (Ps, Pz: Ps Pa) = (100, 10, 15,solution s, using, >, Soin. : Obtain the optimal
job sequence for foll
and (D1, Dz, Ds,
maximum profit. d,) = (2, 1, 2, 1). Find feasible , (Pu Pas Pas Pas Pe) = (25,15.12.5.1)
order of profit. N25
The simple and inefficient solution is to generate all sequencing with deadlines. pe Sort all jobs in descending
J = On Sa Sa Soo Ds, De) = (8:21.33)
So, P = (30, 20, 18, 6, 5, 3, Ds
1

subsets of given set of jobs and find the feasible set that Soln. : profit.
select one by ending order of
maximizes the profit. For N jobs, there exist 2% Ja dids) and D= (2,43, 1,3, 1,2). red in desc
fe shall
Sort all jobs in descending order of profit. | of sorted d job
jobs
schedules, so this brute force approach runs in o(2") J, an d check if itsatis
fies job t from the list
et»
and D=( one job from the list of sorted jobs Ye shall select one the job in
So, P= (100, 27, 15, 10), J = (i, Jajob Jus) job in the| atest free
slot. If the dead line. If so, schedule
the deadline. If so, schedule the
time.
one from the list ol Me xs jf it satis fies d, then skip the
1, 2, 1). We shall select one by such slot js foun
7 However, greedy approach produces an optimal result deadline. If 9% lb and process the ear 1ap free slot. If no
infairly less time. As each job takes the same amount sorted jobs, and check if it satisfies theIf no such slot 8 No such slot is found, skip the currentj the lates and process the neXt ON:
of time, we can think of the schedule S consisting ofa
schedule the job in the latest free slot. Next one,
sequence of job slots 1, 2, 3, ...,.N, where S(t) indicates found, skip the current job and process the next On®-

Scanned by CamScanner
Sem 44 -- Cony
(MU )-- Sem t
alysisis ofof Algorithm
G reedy
4 nm(MU me) ng eee515 |e
a = Abert, Gloss os : Minimum Cost Spanni
Initially. Z opie ——
Fig. 553 and ee \\
syllabY” cruska l and Prim's Algo na Trees - Multiple trees WS tree and sub
are pos Seraph. \
\
@ Possible for given graph.
1 2 4
55 Minimum Spanning Tree ®)
\
°
led SP= Sect
profit of schedu
t
d jobs, SP = 0 pasics of Graph i
201= DBR
Di: S Profit of schedule 55:1 (5)
his. Se3 CH ;
ate
5
Sine the terms + Graph, welghted. gra OS ® 4
Hon jorot3. XN
'
Hteration1
=2) 5, a. Orang tes, minim spanning Woo, : Ce
scbeduleit in sl $=on t}
oSPf=i{25 for job J1 is 2. Slot 2 (= 1 tot t
Sosoatusetci Ui}+and Pr Deadline
2.
trea,9
~ Graph
schedule it in slot (20) ‘Tree of G
8 Solution set S = {J,), and ProfSP it=
pefinition Graph G
o 1 2 8 a set of vertices (V)
- ph G= (V, B) is defined by
2 (1= 11012 is free, SO and set Of cages (B) joining those vertices,
Desdlin for job Js is 2. Slot?
it in slot 2.
schedule graph shown in Fig. 5.5.1,
Profit SP = {25, 15)
For the:
‘Solution setS = [J J2}- and Vv = {A,B,C,D,B)
Iteration 2 B= {AB, AD, AE, BD, BE, DC)
: 1 2 3 4 5s Deadline for job J: is 2. Slot 2
@= 1 tot=),
slot. The a
Subgraph of G
Fig. 553 : Graph, its tree and subgraph
ceccupied, s0 look for the previous possible1.
oO

schedule it in slot 4. Spanning Tree


3 (t=Otot=1)is free, so
is free, so 15)
1. Slot] ((=Otot= 1) Solution set S = }, and Profit SP = (20,
Deadline for job Js is Definition
schedule it in slot 1. 15, 12)
and Profit SP= {25. Spanning tree T = (V', E’) is a tree of connected,
Solution set S={J,, Fig. 5.5.1: Graph undirected, weighted graph G = (V, E, W), which contains
s(al2[T 1 1& 2, Weighted Graph all the vertices of G and some or all edges of G. So "= V
o 1 2 3 4 andE cE.
Iteration 3
iteration4 Job J, is not feasible because first two
slots are already Definition
h if some Fig. 5.5.4 illustrates the spanning tree of graphG.
e first three slots are Graph G = (V, E, W) is called welghtedof grap
Job J, is not feasible becaus after any time later t= 2, it canno
ied and if we schedule J, any time
alresdy occup
occupied and if we schedule J,
1. So job J, is discarded, ‘weight. OT cost is associat
ed with each ‘its edges. W
its deadline 3. So jobJ, is be finished before its deadline associated with each edge.
1 =3, it cannot be finished before SP = {20, 15} represents the set of weight
Solution set S = {J,, Jo}, and Profit nce between cities is a
discarded,
15, 12] A graph representing the dista
Solution set S = (34, Jz, Jy). and Profit SP= (25, the distance between
Iteration 4 weighted graph, where cost / weighttheis weighted graph is
. For
tot= 3) is free,so two corresponding cities
lteration5 Deadline for job Jy is 3. Slot 3 (= 2 illustrated in Fig. 5.5.2
,
three slots are
Job J, is not feasible because first schedule it in slot 3. v= ,
{Vi,V2,V3V4)
J, any time after SP = {20, 15,5]
already occupied and if we schedule Solution setS = {J}, Jz, 1g}, and Profit
J, is
1 =3, it cannot be finished before its deadline 3, So job Spanning tree of G
discarded,
sfeT4T e] TI tree
Solution setS = {J,, Js, I}, and Profit SP= (25, 15, 12} o 1 2 4 5 Fig. 554 : Graph and its spanning
With the greedy approach, we will be able toschedule
+ 12
three jobs {J;, Jz, Js }, which gives a profit of 25415 Kteration 5
first three slots ae
= 52 units. Job J, is not feasible because
J; any time later t= 3, PE)
a
z
already occupied und if we schedule . .
nning tree Ta h Ge
2
job 5, is
it cannot be finished before its deadline 3. So Definition : Spa weighted grap
d,
Explain Job ing with deadiiner for the given instance connected, undirecte vertices of G and some oF
the ae
Graph which contains all BCE.
i
1 =5,{P;) Po: Py Py Pa} = (20, 15, 10, 5,3} discarded,
= (20. 15,5] Fig. 5.5.2: Weighted pl nae
Solution set § = [J,, J, 4), and Profit SP
Grapis
Wand BY CE. Gra
and (0), da, dy dy ds) = {2, 2, 1, 3, 3} G.SoV 7 ‘Vand t cost. Min a imum sp)
will be able to schedule ith a@ differen
trees with
Soin. : — With greedy approach, we
Jobs are already sorted in descendin; g order of profit. three jobs {J,, J, Jyh: which gives profit of 20+ 15+5 G= WB
‘Tree T = (V’, E’) is a subset of Graphof E. i oe
spanning Wee swith miniea ©
E” is'a subset
We shall select one by one job from the list of sorted jobs, = 40 units. where V" is a subset of V and cam Bs) Fig. 555.
and check if it satisfies the deadline. If so, schedule the job
om a cycle, while Graph or gubgraphé 4
in the latest free slot. If no such slot is found, skip the cycle,
“urrent job and process the next one.

Scanned by CamScanner
6 2 Ol
forward approach to this is
kal performs better im the case OFSense cy matrix for each vertex. It takes
.
with V << E. ihKrusV=E. the.
stices and IE! edges, whereM1 ene ne it
‘lize Uy — list of uavisited
-.
vertices with all vertices
a

\
sparsegraphwit
& Algorithm for Prim’s approach Prim’s algorith
with the help of some other data Uwe adds some arbitrary veriex V, from
described belay,- Tmprove the performance, Using bis
Al gorithm for Prim’s approach is Partial solution and removes V, from Uy.
dum PRIM_SIST(G. =) i ~ Set Of all neighbour edges of vertices im partial
| Description: Fad MST oftedgraph G using Prim’s algorithm
er n
graph G and numb of vertices
uput : Weighted connect ing tree MSTand weight of i.e. eoip O(EI + IVI log ws ‘The neighbour edge with minimum cost is added in
// Output : Minimum sponm can be improved to Partial solution if it does not form cycle and not already
Minimum spanning tree cost 0 added to the partial solution.
ofG nitialize tree nodes Garris. lc next minimum cost edge and at
and its minimum spanning tree ori #0 to n—1 do Partial solution and remove it from the list of
ig. 5.55; Graph MSIE] «0
Jend
7 Applications of MST
design. ; MSt{O}2 If Vis the initial vertex ‘This process is repeated until all vertices are visited. Le.
q) MST js used in network ient routing algorithm.
For given graph, initially the set of unvisited vertices
lfork <-_1tondo
to implerment effic rmin_dist 2°
QQ) Irisused
used to solve travelling
salesman problem. u-1do
G@) Its fori¢-1t0
for MST for | 1ton—1do AND
55.2 Pri im’s Algorithm if GO, j] AND ((MSTG] AND MSTG) OR (MST
MST. Mention its time
©. Wite PRISM's algorithm of (7 Marks)
MST))) thea
£Gfi,j] < min_dist thea
min_dist & Cf j]
' complexity.
lexity.
fexity of Prim's algorithm.
thm using Greedy
on the compl

Analysis complexity of Prim's algori uci


@. Comment
(7 Marks) vej
approach.
Q._ Explain prim's al gorithm. ead
ead

© Problem ead
Find minimu 1m spanning
tee from given weighted, 1.
Wwe il.2.3, 4, 5), Let us start with arbitrary vertex
undirected graph G = <V, E> print (u,v; mia_dist)
@ Explanation MST{u] MISTI] <1
ch to find a
cost & cost + min_dist
Prim's algorithm is a greedy approa ed, connected, lend
minimum spanning wee from weight print (“Tatal cost = ", cost) Edge <1, 2> has minimum cost, $0 add
it
a’s algorithm.
undirected graph. It is a ¥ ariation of Dijkstr
is very simple and
Working principle of Prim's algorithm Complexity analysis
explained here: In every phase, algorithm search for minimum weight
on
Initialize list of unvisited vertices with edge. Running time of algorithm heavilyn. depends
Uy= (Ve Va Vinee Val how efficiently it performs search operatio Edge <1, 3> has minimum cosh, 0 addi
I. Select any arbitrary vertex from input graph G, call that The time taken by the algorithm can
be computed as,
subgraph as partial MST. Remove a selected vertex nnd ont
from Uy. 3,(5 1454
ro) kethic0 (FO.
2, Forma set Np -a set of unvisited neighbour edges of all
vertices present in partial MST.
Tima taken by the Time takon
by the
3, Select an edge e = (u, v) from Ng with minimum Time taken by the
middle loop of | innor most leapoff
weight, and add it to partial MST if it does not form a outer mast loop of k
cycle and it is not already added.
If the addition of edge e forms a cycle, then skip that Rewriting the equation,
edge and select next minimum weight edge from Ng. n a a 1
Continue this procedure until we get an edge e which Ta) = & Et+ Dil= D orm
does not form a cycle. Remove corresponding added iNel j=l k=!
vertices u and v from Uy.
4, Goto step 2 and repeat the procedure until U,is empty.

Scanned by CamScanner
ot Aigortnm (MU - $0004 COE)
goes! Algorithm
(MU - Sem 4 - Comp)

so te veo generated sop S18 the minimum span


Parle! solution
8 ay te cas,
gen” a “eb, o> has Minimum Cost, so add jt
Edge
cot 0 0 smn arco went 201 4
<2. tv has pcm
of solution:
Cost
3 ©
1
+ 4,34 913.4) +403, 5)
(1.2)
21494442
#10

seep 4 pan
1c <b, and <c, f> have same minimum cost, so we can
add<e, f>
add ar of it Let vs 1
os onthm,
salgo!
Ex, 5.6.2
mini mum epann ing trea of ho given graph by using Prim'
Find tho cost of 1

Fig. P. 5.5.2

- with all vertices in given graph. \


Solin. :
Uyis empty So the tree generated in step 6 is the minimum
Tpititize Uy ~ list of unvisited vertices V, from Uy. Step 6: Edge <b, f> has minimum cost, but its inclusion
in
=
algori thm adds some arbitra ry vertex V, from Uyto the partial solution and removes above partial solution creates cycle, so
skip edge <b, f> and spanning tree of given graph.
= Prim’s s in partial solutio n is explore d. Let-w(u, v) represent the weight of edge (0, ¥)
Ng-setof all neighbour edges of vertice cost edge i.e. <a, f>
— not already added to check for next minimum
n if it does not form cycle and cycle so skip it and check for Cost of solution: w(a, b) + wtb, o) + wie, ) + wif, d)
um cost 1s added in partial solutio Inclusion of <a, {> also creates
— The neighbour edge with minim
next minimum cost edge,
ie. <f,d> +w(fe)
the partial solution. the list of unvisited
it to the partial solution and remove it from ercate a cycle, so it is a 3341444542215
— Otherwise, select next minimum cost dge and add The inclusion of <f, d> does not
vertict Us. This process is repeated until all
vertices are visited, i
i.e. Uy becomes empty.
feasible edge, add it. |
6,d,¢, F}-
= Forgiven graph, initially the set of unvisited vertices Uy
Set of neighbour Cost of neighbour
Partial solution
edges Ne
: <a, b>
Step 1:
[2, b, cd, , f}. Let us start with arbitrary vertex a. <a, f>
Uy =
Initial partial solution <a, e>
—x.5.5.3
Find MST using Prim’s algorithm.
Step 2: <a, f>
Edge <a, b> has minimum cost, so add it. <a, e>
3
<b, c>
Fig. ?.553
3 <b, f>
b.
® Soln. + . - ices n Bra Bap.
in givegiven
vertices with all vertices in
Initialize Uy — list of unvisited

Scanned by CamScanner
-
mau - Sem 4 ~ COMO), s Ys from Uy.
othea paral solution and remove
arbil y vertex Vs A ti : ‘ itialize. ize SV
Uv led Vertices
it
Vertis with, all vert in gi ‘
adds some arbitrar and not already ade < imn'Salgorithm adds some arbiu UY vertex V,from ‘VerticesU, Riven graph,
if it does not form cycle ‘dig Pri f all neighbour ed 5
Pr t is added in part
ial solution - Ne St lees of vertices in Partial solu
edge with min imu m cos
; / unvisi
‘The neighbour and remove it from the list of
solution. it to the part ial solution led
the partial minimum cost edge and
add
- ouecvs select next empty.
i.e. Uv becomes vertices ©" verti
is repeated until all ‘vertices
until all veri 5 are visited,
vertices Uy.
Uy= fabd.c, def. This Pp rocess eae are visited,
is. Uy becomes empy
This process is repeated unvisited vertices For given STP yh, initially the set of unvisited vertic ce
For givengraph, initi ally the set of
SS Uy= lab. def}
: \
Partial solution
{ bed) Cost ot ueatoar ‘Updated Ur
Step 1: arbitrary vertex@ Step1 By C.D, B, F }. Let us stan ‘i . {B,C,D, BF
Uy= [abc d). Leuststart with “f(A,
with arbitrary
Initial partial solution N'A, Initial partial solution 1
S

" ®
© (ed} 7 Edge <A, B> has minimum cost, so add it {C,DEF}
2
Step 2: Edge
‘<a, b> has minimum cost, so add it.
®
1

(¢}
so add it.
Step 3: Edge <a, d>h fas minimum cost,
1
()
2
@
it. Uy is empty so the tree generated in
step 4 is the minimum
Edge <d, c> has a minimum cost, so add
spanning tree of given graph.
(u,v)
1 Let w(u, ¥) represent the weight of edge
Cost of solution: w(a, b) + w(a, d) + w(c, 4)
2 =14+2+3=6

Oo
ex. 5.54 DORI
Find minimum spanning treefor the following graph.

nimum cost .
Step 6 : Bdge <A, F> and <B, D> havecyclemis, so reject it
i inclusion of <A, F> forms form cycle $2
ereas inclusion of <B, D> does not

Scanned by CamScanner
Bh of Algorithm (MU_- Sem 4- COMP)
Cost of f neighbour.
edges

2 yRootrank then Usi ng Disjoi sy


Ze
ent « > yRoot Verticesin yee tO tructures,wit
Si < soot then the in the ' tgorite See amevt
'oe IB) oF Of BaINv0 time,
ean both are identical.
identical,For
nt «= xRoot Fe
yRootpare Eons
Arbitrary
Toot the make ong “gan
~ Son ce ing ee VS
ieee ee
arent <— yRoot ‘te
‘:
Root NEW parent Could son it in One :
Rootrank +1 5 k EN 15) tne Seennae
ws
OME total
The running ge are done i
ge, est timeME is Biven
gi assee OES +Seep
IE Nog IE) =
Uy is empty so the tree generated in step 6 is the Minimum
coh
spanning tree of given graph.
Let w(u, v) represent the weight of edge (u, v)
Cost of solution: w(A, B) + w(B, C) + w(B, D) + yy (CB)
+wiC, F) =1+2+4+3+2=12
& Algorithm for Kruskal’s approach
Algorithm for Kruskal’s approach is shown below:
Kruskal's algorithm for finding
a minimum
es tee. Compute the time complexiy of the [Algorithm KUSKAL_MST(C)
(7 Marks) 7 Desexptice: Find minimum spanning tre of graph G on verion
ff Taput: Weighted undirected graph C
I Output : Minimum spanning tree of G

| MST D | soln:
Find minimum ing wee from given weighted, | lt each ye Vdo Create disjoint-set tree
undirected graph
G= <V, E, W> MAKE-SET(+) - Kruskal’ s algorithm sorts the edges according to decreasing onder
of ther Weight Soned edges ar ised in following
= Explanation jend Add x to disjoint-set tree table: *
- Joseph Kruskal has suggested a greedy approach to find
MST from given weighted, undirected graph
for each (u, v) € E ordered by weight in increasing order do [2] 4o[es[aulas
[eo]
if FIND-SET(u) # FIND-SET(s) then
- The algorithm first sorts all the edges in nondecreasing MST — MST U (u, »)
order of their weight. UNION(u, +) - One by one edge is added to a partial solution ifitis not forming the cycle,
- Edge with minimum weight is selected and its - Initially, set of unvisited edges Ug = [<1, 2>, <3, 5>, <1, 3>, <2, 3>, G, 4>, <4, 5, <2,
feasibility is tested. lend
4>}
~ If inclusion of the edge to a partial solution does not Partial solution Updated Up
form the cycle, then the edge is feasible and added to Function MAKE-SET(x) Step 1: Minimum cost edge is <1, 2>, so add it to MST | Up={ <3,
5>, <1, 3>, <2, 3, <3, 4>, <4, S>, <2, 4> }
the partial solution.
if x is not already present then and remove from Ug
~ If is not feasible then skip it and check for the next
edge. The process is repeated until all edges are
scanned.
~ Let, list unvisited edges Us = (€}, €), ey... 5g), be Minimum cost edge is <3, 5>, so add it to MST Ug= [ <1, >, <2, 3>, <3, 4>, <4, 5>, <2, 4>}
the edges sorted by increasing order of theirweight and remove from Ug
I. Select minimum weight edge pi from input graph G,
which is not already added. O—®
2. If the addition of edge ens t0 a partial solution does not Determine the roots of
woh acree add it. Otherwise look for next minimum tree x and y belongs to
weight ¢ until we get i
checked edges from E, ft te fasle edge. Remove
3. Bo ser 1 jnd repeat the
Goto procedure untili all edges in If x and y are already

Scanned by CamScanner
Ge oA big htbor WII taen 6 - Cary us ‘
gas wep WA the P
yee Ue LIK tyone
5>, <2, 4> ) gasrnien ame
rll yous Tye 1, 3, Bi A2 Ay FAue
eye 4 els Ir
wep il antDU Ora
pnd eis
sang
Cra

ney eB AA 8 tl vba ig Hi 4 Sonn


ih varhind Aaa Vy = Le1,, Vo.
soil. Ss 4, ONeee
¥,
Mv Mie Abs Va MeinNie, MMi Ai to, Ve, Cte Vo, Ms, ¥.
IS, eV, ee Vo
Fartial vhution
Minti tam] ed “1, Vp,
pio Ae
viten cele a0 10 uP a7» Any MST
eA ee,
PT oe ae usion MV, Me heli,
4, Ns tes, A: eV
endo Li 25, Ae ame Incl a a
eww mili pont nt MASSE andl fone from Up Ven
Sy, Ver Ha Vid, Wn Vr
sfovea ana fana b eye ava
1 ey

guep2 7 Went minirnsin cord hip 8 Oh, Vo,


rp MST a | U, = (aly Vo AV WD
0 #40
pomerve from Up, .

Nuvo enn clue


_—————_ je
Ws ey Se jut Hts dnedusion
st 4 in the
Up is empty so the tree generated in
step @ \y Vey Vie I, VO}
0 Va, Vere

Step m spanning tree of given graph,


ininimu
Uy
vaniilua peter ne aennyowel HE fea) Lat w(u, ¥) represent the weight of edge
(0, v) \
Ay Uys fede Cont of solution: WC, 2) + w(1, 3) + (3, 4) + w03, 5)
dts inclusion e
Mindi pont ely bed, ess Int wl 434442510
evel ay gvmone fit Uy
fin th=(]
Fe and
algoritien +
Fi ant viel coat apanning trae (ing Knunke! Negt minimum con edge it Vz, Vy, wad
td
* WAST eed | Up= Ve Vir. <V5, Vr, Va Vr,
semis from Ma VN VV VN VP
V2, V,V2)

(pV)
(MyVp) | 9 | Ye
(YpMed | 1 | ose)
(av) | 18 | MeN | 96 to MST acd | U,= [<¥3. V>.
Step 4: Next minisnurn cont edge is <Vy Vir,0 od it
Balin e Chapt cepraxcttation af yuiven shila Py abaya by Pig, 9.5.6, remove from Up Vy VeVi
Mi, Ve. eV Vir
NG V2 NV
V2)

Scanned by CamScanner
Up= (<¥: a Vow SV VI SV Vy
[ext |
VV V5, View <V¥5. Vee cy, Va} age ts added to & partial soutio n
ont eel eat uavsited edges Up =(<1, 6 ren
foil
7 Mfini cost cdge is <1, 6>, 50 add ito M: ST and rey move | Up= a
pl! pom Ur. >, <2, I>, <5,5. 6, <2, 3>, <A, Ts
<A.5,c5, eth

4>, so
T minimum cost edge is <3, add it to MST and at
Ig, Name <Mgn Vers <Vo Vr} Ge?* Nefove from Us Tel} Si, 6>, <2, 3, <4, Tm, <4, Sr

Next ini
remove fron
he cost cdge is <2, 7>, so add it to Ms
IST and | Up= is, ©, 2,3, <A, To, <A, So, ST

oo 2®
ted in step 4 is
Ug is empty SO the ; uee genera .
Vz>, but its inclusion the minimum spanning te of given
graph. and | U,=(<2,3>,<4, 7>, <A, Sa, <3,7> <2)
Next Tminimum cost edge is <5, 6>, so add it to MST
a me is <Vs
= sot Oe a pi Pe DES Gep4:
reo Let w(v, ¥) represent the weight of edge remove from Ue
qv)
V>
' «sede yy, <Vs.
<Ver
V>
=
form
Cost of solution: w(Vi, Vs) + 6(Vne Vd + 1G)
vee ost edges at <Vs, Vr and + w(V4, V9)
Sa w(V3. V3 2) + w(V 7. Va) + OV. Vi)
247
234144494347

eas? DES
and kustat's Agari. —
“rognd MSTof folowing raph using pin’
<2, 3>,
Step5: Next minimum cost edge is
remove from Ug,

Fig. P.5.5.7

ng
their weight. Sorted edges are listed in followi
n sons the edges acconding to decreasing ordofer

Scanned by CamScanner
a i min ;io costUr. edge is <D, Po, 55
oY Dorn i;
= Come)
Come) 5 Updated U,
Boxe tap
is G)
7, <1, >)
5 rt
rage BAP s creates Upa (<4, 531 Se
Por is inctusion
pee
is <B, Es,
mum cost edge
Next miniom Ut
remove fr

cio po |
irelusion Up=t<h2) ee

Nea chamois OE BES NS Tew bar ts 4
Kap TS NCE ANE HOTS
Se RS | Us= ated in step 7 ip)
Us is empty so the tree gener
Incis ion create s
Tada Bah DN Ra its
graphy,
fees SRF na ek
ne
ths minimum spanningsenttreetheof given
weigh of edge
t
ween Let w(a, ¥) repre ie
5) 4
Cast of solution: W(1, 6) #918. 8 464,
@y)

wi +029 40D
1G D+
wIOF ISH IZHI
SSD

as Dns
792 HEIN HONS ION UY ANAS atv
eng e saniwon squares <B, D>, 80 add it to M: E>, <C, D>, <C, >}
Next minimum cost edge is ST and | Ue= (<B, C>, <A, B>, <D,
remove from Un,

PRR SRS
KupS: Next minimum cost edge is <B, C>, so add it to MST and Ucs (<A, Bo, <D, E>, <C, D>, <C, Fo}
temove from Up,

ag cake of hie weighl, Soutaat etges ane Hiseet ia following


Konak asthe eg hy aes NUE
ae:

BRE RN AR AS Am ASA | | os | xe. |


[: ? 8 |» |
= _ 3 Is
(ee omit Saitama btn FRNA Homage ges
es
ine SERENA SA SAMA CA SA ASR
= ELAN
Hi as SA BAAD BAS DS
eda) a ee

Scanned by CamScanner
y - Sem 4- Comp) :
ton t . E>, <C, D>, <C, >)
i | Ue =(<D
but its inclusion

‘Next minimum cost ©d8e T° B>,
eds igo ©is <A,
t
<C, FI
creates cycle $0 FEmO ve it is <D, E>, but Fe tnawusion | Up= (<C, D> sub}pjeotedto ay
Big Complexity analyate
‘Next minim vam eost edge Us — me
eater eyele so remove it fom Length of tape Primitive operation
Of program lene erie” iO above algorithm is the addition
is <Co Dos PS
ext minimam cost edge Ur: Lenath of |” program
Whic
y h is enclo:
ra ning time of al 2th sesed WiWithin0 Loops. The
creates cycle so remove it from <C Fs put its inclusion u;={1 n by,
Nex T minimum cost edge is Us. Ur is emply so the tree generated in ste P Sis ty Cost of addition
creates cycleso remove it from minimum spanning te of given graph, 4 storage ‘on Single Tape Tiny te FB
aL
ome
. 56° ain an
Lat wu, ¥) represent The Weigh of gy /
wy . 5 at usingalgor ithm to derive
groedy approach far a ‘Cplim al
Cost of solution: w(A, C) + WCB, C) + wo, sings ee eval time tor
FB,
E) + w(D, FP) 44644424 Jo yy (10 Marks)
jh this ease, We have (© find the permutation of the
ithm ram order which minienizes the MRT afer cyte’
vs. and Kruskal’s algor
5.5.4 Differentiate : Prim’s al programs on single iape only e
exist many Permutations of programs, Each Bives
‘ ibly different MRT. Consider three programe
‘Prim’s Algorittm P,, Pas Pa) with length (Li, La, La) =(5, 10, 2),
's chooses
¢
Prim’s algorithm alwayspartially the next cdge which is 4 ‘The Kruskal algorithin always chooses the edge
having a minimum—— weight Ye find the MRT for different permutations, ¢ = B+) rin
general ed solution. 2 343
neighbour of vertices in In Kruskal's algorithm, eanutations are possible for 3 items. Mean Retrieval To) = og?)
a tree-
Prim’s algorithm ensures that partial solution is always a forest. fine for each permutation is listed in the following
table. ‘This algorithm cun i (0?) time.

cost ‘As edges are always sorted, minimum cost edge


Picking up next edge requires finding an edge with minimum can be chosen in O(1) time.
Explaia n ‘optimal stora 1g¢
On tapes and find
{or given instance, N= 3, and (iy, by fy) = (5,the10,optimal order
froma set of adjacent edges. 3).
Kruskal’s algorithm is a better choice for the Soin. :
sparse graph. a
ah orange the programs in, non-de
creasing order of

List L = {i3, §1,i2}


= (3,5, 10)
‘Syllabus Topic : Optimal Storage on Tapes
Mean celrieval time for the given programs is computed as
5.6 Optimal Storage on Tapes Length of k” program
should be observed from above table that the MRT is
268, which is achieved by storing the programs in
> (May 13, Dec. 14)
Mean retrieval time of n programs is the average time ascending order of their length.
Write short note on optimal storage on tapes. required to retrieve any program. It is required to store Thus, greedy algorithm stores the programs on tape in
programs in an order such that their Mean Retrieval Time is nondecreasing order of their length, which ensures the
. - Explain optimal storage on tape with example. minimum MRT.
minimum, MRT is computed as,
Average retrieval time order. The greedy algorithm finds the MRT as
2. How iosolve optimal storage problems using greedy ovar n programs n= number
of programs =3
following:
(6 Marks) Fori=1:
lAeriim MRT_SINGLE_TAPE(L) T)=T +L} =04+3=3
|! Description : Find storage order of m programs to such that mean
retrieval time is minimum Fori=2:
P, of length
Ly, Lo, ..., Ly H lopat= Lis array of program length sarted in ascending order THT +Lfl]=34+3=6
Fespectively, store them on tap of length L such that Mean » Opa: Minimum Mean Retrieval Time T= Trt Lie0+5=5
Retrieval Time (MRT) be minimum. Time to retrievej Length of K” program
program P; Fori=3:
Retrieval time of the j* program is a summation of the THT
+L s6+329
length of frst j programs on tap, Let T; be
the time to Tr=Ti+ La=5+5=10
fetrieve program Pj. Retrieval time of P; is computed fr} 1 toido Ty=TstLB]=0+
10= 10
Te + 1h]

Scanned by CamScanner
i algorithm (MU{ ~ =
pO" nd solve job sequencing iMG pr— =
rate. 9 approach foblem Using
omoay
7 Exam Pack gi ns frofer section 5.46 Marks)
(University and Review Questions) Ar

@& Syllabus Top!


ic ; General Method
as (io marks) (ee. 2015, May 201
pe” so marks) "7
2967 rithmic approach?
Storage On
Multiple Tap
es @. __ Whatis the greedyionalgo5.1.1) (5 Marks)
(ans. : Fefer sect oeae (io Ma Mote) pis 240
(ay 2015)
desta, ‘opie : Minimum Cost Spannin,
Waite an abstract algorithm for greedy n &
Pris ‘Algorithm 9 Trees .
He al gna
r gyfteet
a. Marks)
5.1.2) (5
. method. (Ans. : Refer section
storage on multiple tapes
's algorithm
ple,
(May 2015) pefine the tens 3 Graph, weighteg
raph,
With a suitable exam gra Wee,
Explain the contol abstraction of tha spanning tee, ma Strage on ropen
a o
muliitapa problem using
gready panning 02 minimum
5.1) ($ Mako) en
mizing MRT on retric
val of
method. (Ans. : Rafer section 5.1.2) (3 Marks)
fon. Rotor section Tite sh ort note .
This is the problem of mini MST?
what iise MS | (Ans, : ON optimal storage
oe Fetor section 5.6) (x9 farts) tay a0ts)
. : Groedy
the program from multiple tapes on Comment on the module of computation
0 ln. Fer section 6.5.10)%2 Marts a
— Instead of single tape, programs are 10 be stored Method. (Ans. : Refer section 5.1.35 Marks) : in
em in a similar a tape with example. be
5.610on Marks
multiple tapes. Greedy solves this probl to increasing @. Enlist and explain the characteristics
of greedy write ty. (ANS.algorithm
comptonPRISM’s of MST. Mention its tim
: Refer soction 55.217 Hae) 1S |
(Ans:“er ect Storage
. Reon
) (Dec. 2014)
way. It sorts the programs accor
ding algorithms. (Ans. : Rafer section §.1.9)(4
Marks) aon a opt
m in oneby one Storage problems using greedy
Jength of program and stores the progra oe
algonth
between greedy comment on the complexity of Prim's m, - Reler section 5.6) (6 Marks
Write a short note : Differentiate )
gpalysis complexity of Prim’s. algortivm using “1a
a.
in each tape. approach and dynamic programming, 9% an algorithm
Working of this approach is explained
in the following
(Ans, : Refer section 5.1.6) (5 Marks) (May 201g) creed approach. Schedule usin 9 greedy toapproach optimal storage
(ans. ter secion 5.5.72) (7 Marks) von for a single tape.
e ample. ee @ Compare dynamic programming with
the greedy (Ars. :Relor secon 56,
(Ans. < Refer section 5.1.6) (5 Marks)
Ex. 5.6.2 4 - Bein pis algeti esau
: Roter section n,
approach.
L
Given the program lengths = (12, 34, n three taps and
56, 73, 24, 11, wu,
Syllabus Topic: Single Source Shortest
Path
astans.
st) 5.5.2) (6 Marks) lain Bole
56, 78, $1, 34, 91, 45}. Store them oF
@&
(May 2017)
greedy method
Storage on multiple tapes,
to solve the ian
problem of
minimize MAT. a Write an algorithm to compute the shortest distance (Ans, Refer section 8.6.2) (3 Matis
‘Soin. between the source and destination vertices
of a wite Kruskel's algorithm for finding minimum )
a Ww th a suitable example, discuss
Given data : connected graph. Will your algorithm work for spanning tree. Compute the time complexity of the the storage on
[Pa] Po] o[Pal ro same. (Ans. : Refer section 6.5.3)(7 Marks) Mmultitape problem usi
[eles] negative weights?
(Ans. : Refer section 5.2) (6 Marks) (Ans. : Refer section
ing greedy approach,
m
[alae eal [oe Jof [5]their size. (May 2014) Qo0
First sort the programs in increasing order Ex. 5.2.4 (10 Marks)
Sorted data: Ex. 5.2.5 (10 Marks) (May 2016)
& Syllabus Topic : Knapsack Problem
a. What is 0/1 Knapsack and fractional knapsack
problem. (Ans. : Refer section 5.3) (3 Marks)
Qa. State and solve knapsack problem using the greedy
method. (Ans. : Refer section 5.3)

| Explain and write the algorithm for O/1 Knapsack


| MRT ropes = (C11) + (11 + 34) + (IL + 34 +45) + (LL + using greedy approach. (Ans. : Refer section 5.3.1)
34 4+45 +73) + (11 +34+45 + 73491) )/4
(6 Marks)
=563/5= 112.6
MRT gapeg = (12) + (12 + 34) + (12 + 34 + 56) + (12 + Ex. 5.3.3 (7 Marks) (May 2017)
| 34+ 56 +78) )/4 = syllabus Topic : Job Sequencing with Deadlines
t = 4340/4 = 85
a. Write 2 note on : Job sequencing with deadlines.
MRT rages = ((24) + (24 + 34) + (24-4 34 56) + (24 +
4456491) )/4 CA+ (Ans. : Refer section 5.4) (10 Marks)
= 353 /4= 88,25 (May 2014, Dec. 2016)
ape MRT Taped)
MRT rage
(MRT rer 95.28
MRT =+88.25)/3 ges) / 3= (112.6(12. + 85

Scanned by CamScanner
is of Algorithms (MU - Sem 4. Com
— ST) 62
node is either¢ a non-ppo) Promisin=
A Jeafacomp lete solution,
g noge =
ode OF it
‘ posers

The c state sem


wich space ofane sem iis the set
O Wel tag of states in
uti of oh ie Which
sequence \
pace tres
is derived by making
‘Ons can be represented using
state
‘The
patie x ae tee is the state before

1
making any
a at the first level in the tree
alan responding to all. possible
Comespondin; posi choices
i for the first
ool
fo don't explore furthar and DIodes atfor second
© Rchoices level corresponding to all possi!
the second decision
m of subsets, Graph coloring, 15 puzzig and wo forth, me
(N-queen problem), Sui
General Method, 8 queen
problem Fig. 6.1.1 : Process of backtracking Usually, : number of d lecision
owderofinputnne is in exponential-
‘si sequence isi
sales man probl em.
problem, Travelling — State on
Fie g for the 8-puzzle problem is shown in the
We can consider the backtracking process as finding a
articular leaf in the tree. It works as follows :
that it maximize or minimize given criterion funetion
CK, Xa ey Fa)> Ifnode N is goal node, then retum success and exit
6.1 Backtrackin:
Let C be the set of constraints on problem P, and let
p If node N is leaf node but node goal node, then returns
satisfy the failure.
Syllabus Topic : General
Method js the set of all the solutions which
constraints C, then Otherwise. for each child C of node N
o Explore child node C
General Method co Finding if given solution is feasible or not? ~ is
6.1.1
the decision problem. a IfC is goal node, return “success”
6.1.1(A) Introduction best solution - is the optimization o Retum failure
co Finding
> (May 14) problem. Jn backtracking, the solution is expressed in form of
Q. Comment on model of computa
tion >Backtracking. © Listing all feasible solution — is enumeration tuple (X1, X25 «++» Xn), each x8, where §; is some finite
Ree e Uh EIoss problem. set of choices.
as a making all feasible
= Solution to many problems can be viewed - Backtracking systematically searches set of Backtracking algorithm tries to maximize or minimize
given
sequence of decisions. For example,
TSP can be solved solutions called solution space to solve the certain criterion function {(.) by selecting or not
which city
by making the sequence of the decision of problem. selecting item x. Fig, 6.1.2: State space tree of 8-puzzle problem
set of partial solutions.
should be visited next. — Bach choice leads to a new = While solving the problem, backtracking method - Backtracking guaranteed to find the solution to any
Similarly, knapsack is also viewed as a sequenc
e of the
Partial solutions are explored in DFS (Depth First problem modeled by a state space tree.

or
imposes two constraints :
decision, At each step, the decision is made to select are
Search) order.
function o Explicit constraints : It restricts the section of x} — Based on traversal order of the tree, two strategies
reject the given item. If partial solution satisfies certain bounding from 5; Explicit constraints are problem. defined to solve the problem.
g order.
Backtracking is an intelligent way of gradually buildin than the partial solution is explored in depth-first dependent. All tuples from solution space must Backtracking traverse the tree in depth-first order.
the solution. Typically, it is applied to constraint the constraint, it breadth-first
- If the partial solution does not satisfy satisfy explicit constraints. Branch and bound traverse the tree in
satisfaction problems like Sudoku, crossword, 8-queen will not be explored further. Algorithm backtrack
s from determine order. As backtracking always
explores the current
o Implicit constraints : Such constraints
that point and explores the next possible candidate.
puzzles, chess and many other games. explicitly implementing
which tuple in solution space satisfies the
criterion node first, there is no need of
— Backtracking builds the solution incrementally. Partial represent using state
Such processing is convenient to function. the state space tree.
are
solutions which do not satisfy the constraints are the initial to state space tree
space tree. In state space tree, root represents Some. terminologies related
abandoned.
state before the search begins. 6.1.1(B) Terminology discussed here + ion of all feasible
Backtracking is recursive approach and it is a refinement of
working of backtracking : C collect
y
- Fig, 6.1.1 shows the (1) Solution space .
(6 Marks) space
solution is called solution
brute force method. Brute force approach evaluates all
possible candidates, whereas backtracking limits the search
algorithms. Q. Write a short note on state space tree. ct to state in the tree from
Q. What is a state space tree and with espe space * All the paths ,
by eliminating the candidates that do not satisfy certain It keeps exploring the partial solution by adding next (2) State n
m the state space of
constraints. Hence, backtracking algorithms are often faster possible choice in DFS order and builds the new Pa space tree explain the following terms: oot to other nodes form
than brute force approach. solution, This process continues till partial solution
() solution state ¢
— ie thee e space
stats
satisfies the given constraints or solution is
not found. 3)
r
Pro bleim state Ee
- Backtracking algorithms are used when we have set of
This is an incremental approach. sed fin the
choices, and we don't know which choice will lead toa states
answeroa wer state 1S
s Anse
correct solution. The algorithm generates all partial A node in state space tree is called promising if it
(ii)
(sree Answer stat sati sfie s the implicit
whi ch
which may lead 1° & tate space Ue
candidates that may generate a complete solution, constructed solution
represents partially Non-prom a
const
The solution in baci ising node violates constraints ) dynamic trees
Wheres sen fom he fake
— king is complete solution.
Gite
D + Xnds 7 from the finite set of
choices, S;. Elements in solution tuple are chosen such
and hence cut down from the further computation.
ee
livecnod
©(vi) n

Scanned by CamScanner
Baoktracking & Branch and By rt 6 aS Algorithm: S (MU
= Som 4 Comp
ga —
(mu-Sem4- Comp) if (Bk(«LU) #1)
wo x(k= 2) == TRUE then,
« A (Represents feasible solution)
=
applications ofBacktracking
ED 8 path to answer node then
etd king is useful in solving followin, seen from Fie
Abeen
in row 3, 6 8,4: 90 Place .is left to place next
Solution-state = atte} 2h
@)
from the root to
§ represents & hich pint (x(1> x[2]y ey =f) gat" queen problem 8 Problems ; SO queen 2 backtracks to next possible
solution space is
called solution state
.
W nn Bi Ys of subset problem bam Process continue
space tree, a node end for
(© Live nod e: In state
its children are
yet to ifk <n then Sif Groph coloring problem aie 1 en Af 1 postion isnot putfeasible the first
is generated but ; REC_BACKTRACK(k + 1) queen in cell one backtracks and
e.
generated is called livenodwho simplicity, only’ 2), and repeats the procedure. For
+ napsack problem
se children " Hamiltonian cycle problem Fig. 6.14, mY 2 few nodes are shown in the
+ Live node end
(1) E-Node
y bein g expl ored is called end @ Games likechess, tic-tac-toe, Sudoku ete,
currentl /
(Expansion) node. ch
w Constraint satisfaction problems
space mee, &node whi
{8) Dead node: In state
it’s all children are 6.1.2(B) Iterative Backtracking
Method (vi) gificial intelligence
it
not be expanded ¢ 7 Nework communication
generated or node will crite! rion function, What is backtracking? a general iterative
Robotics
further due to a violation
of (7 Marks)
| algorithm for backtrackin: Optimization problems
is called dead node. tracking method ig 7
for back a logic programmin
Bounding Functions : Bound
ing function Iterative approach Jt js also a basis of ng language
Gai alle PROLOG.
(9) ay
t exploring its described below:
kills the live node withou
children if the bound value of live node ‘Algorithm ITERATIVE_BACKTRACK(r)
vector
crosses the bound limits. WX f...k— Lis the solution = Syllabus Topic : 8-Queen Problem
tree
(10) Static tree : If tree is indep
enden t of instance AT), xls HK ~ If is the state space ———sSaaaE
called static tree A Bk() is the hounding function
of problem being solved, it is 8-Queen Problem
an instance
(11) Dynamic tree : If tree depends on 6.1.4 16)
(May 14, Dec. 14, Dec. 15, May 16, Dec.
of the problem being solved, it is called kel >
dynamic tree. while k #0 do Q Write note on N-Queen Problem.
if (ontried x{k] © Thx(1} x[2) «x(k— 1) AND
6.1.2 Control Abstraction (Bk(e[1), #12)... xk) is path to answer node) then Q. Explain 8 Queen problem.
e print x[1J, x{2).-..x[k] /Salution
In backtracking, solution is defined as n-tupl 1 Consider next candidate in set
X= (Kr Xe ons Xe)s here x; = 0 oF 1. x; is chosenedfrom set kek+1
Write a short note on 8-Queen problem.
Fig. 6.1.4: Snapshot of backtracking procedure of
of finite components §. If component x; is select set then else 4-Queen problem
ng approach tries to find the kek-1 U Backtrack to recent node
x, = Lelse setit to 0. Backtracki
vector X such that it maximize or minimize certain criterion end Problem : Given 8 x 8 chess board, arrange 8 queens Complete state space tree for the 4-queen problem is
end other.
Function P(x x). in a way such that no two queens attack each shown in the Fig. 6.1.5.
Conte! ction for backtracking approach is shown Two queens are attacking each other if they-are in the |
_ ‘The number within the cirele indicates the order in
which the node gets explored. The height of node from
@ Performance analysis by the
below: same row, column or diagonal. Cells attacked
The performance of backtracking algorithm depends on queen Q is shown in the Fig. 6.1.3. root indicates row and label besides arc indicate that
possible
6.1.2(A) Recursive Backtracking Method four ps : the Q is pluced in an i* column, Out of all
I. Time to compute the tuple x[k] states, few are the answer state.
i Q. Write a formulation of the recursive backing algorithm.
} (8 Marks) 2. Number of x[k] which satisfy explicit constraint
@. Write recursive backtracking algorithm for the sum of 3. Time taken by bounding function Bk to generate
‘Subset problem. {6 Marks) Q
Fig. 6.1.3 : Attacked cells by queea
feasible sequence
— Backtraci is rather 2 typical recursive algorithm. 4. A number of x[k] which satisfy the bounding function
different
Problems solved using backtracking are often solved Bk for all k. 8 queen problem has. “c,= 4,42,61,65,.368
ments are
recursion. Generel algorithm discussed below
First three factors are independent and they can be arrangements, out of these only 92 arrange
finds all answer nodes, not just one.
valid’ solution, Out of which, only 12 ae te OOOO
computed in polynomial time. Whereas the last factor is can be
~ Let x.x ) be the path from the root to the i*
problem dependent. In the worst case, checking of x[k] can fundam ental solution, rest of 80 solutions
generated using reflection and rotation.
node. Let Ty X} be the set all of all possible
be done in factorial time because the tree might have n!
values for next node x.; such that (x1. X2, ..4 Xie) is problem
2iso a path from the root to 3 problem state. nodes. Time complexity of backtracking method is given 2s, - 2Queen problem is not feasible. Minimo
Recursive approach for backtracking is stated below. size for which solution can be found 16 ‘le 7
{Alesis REC_BACKTRACKiE)
O(P(n) nt) understand the working of backtracking on theaquest q
:
. .
PA ke — If the sobston vector Problem.
the 8-queen pro
blem is shown
A Wisfth x82}... fE é Time to exectute Time to exectute For simplicity, partial state space iree| TT of solution for
{i the breending fssction first thres steps fourth three steps Fig. 6.1.4. Queen 1 is placed in a r colume en 1 in Fig. 6.16.
a 3
Tow. All the positions are crossed in
attacking. In next level, queen 2 is Pa biccansabe
column in row 2 and all cells 3° “Ta
attacked by already placed queens | 2

Scanned by CamScanner
4 - Comp)
mo sgof Algorithms (MU -Sem
65 <p oonstraints 8
42 3 4 5 & Branch and Bound
Bacitracking '
= r oe implicit constraint is to determine wich of tye |
ot lgrtrns (MU S204
(EF anaysis
se asers >
Sanam
FTPLACEE: i) thea A
roe
on jc Of Se
func
oli
wo ns, The implicit constraint for n quee
wo quecns MUSt NOt appear in t ‘0 problem,
aye—= n thea
.
. placed inition
Mahgu
tums true he same rovy,
Tk canbe
prist Xf1..-0l
else all queens are i n choices,Creates
With a
it ha oe Qseon (5,2) 5 Faitil + Backtrack i
Step 13: Place

i S.
3
N_QUEEN(: + I,n) a

1
|, - ie Mase Qson (5,3) + Fail > Backtr

=
Fpferent problems of size (n- 1) at level

2
processed, then
JE: Place ack i
end display solution tuple CE function determines the
position of 1
the queen
Qsan 5, 4} Successful
ood is called n times, roe Solution Of this instance is (1,3,5
ne time. This function ,2, 4].
chalk een Pp problem cod
Fanaion PLACE (k, i) tums true if the E gueen \ is defined as 1S mre combinae ati showons below
Fig. 6.1.6 Solution of Fa ve ee = ‘Thu
Tor a*T(n — 1) + 0°.2 Solution. problem
Js, ue recurrence of n-Queen
123945
to recurrence Would 12345
= Solution tuple for the solution shown in en Fig. 6.1.6 be placed in i® column. This function enumerates<n be.
oxn!)-
defined as <4, 6, 8, 2,7, 1, 3, 5>.1) From
positions, can
the previously kept queen's positions to check itt 7
eens are on same diagonal, It also checks that
two queens placed at (i, j) and (k, us Ad
same diagonal only if, distinct from all previously arranged queens, ad all possible solutions for five queen problem using
Gi-j) = (k-lor Function PLACE(k, i) ing approach.
(i+) = +) i k is the number of queen being processed
From first equality, j of columns
{iis the number In.* Salution: [1,4,2,5,3} Solution:
(2,4, 1,9,5} Solution: £2,5,3, 1,4)
golution of N queen problem is represented using 1234
.n, 5 42345 12345
From second equality, j-1=k-i Loop through placed queens, ple
gruX= Dens Xas 3s oooey Xp]- Each x;= 1,2,...
‘So queens can be in diagonal only if - =1i~kt
ja:
(k = 1) queens are already placed Ifqueen Q, can be placed successfully in column
j, then,
The amangement shown in the Fig. 6.1.7 leads to and k” queen is being processed stjzj-Q is always placed in row i,
failure. As it can be seen from the Fig. 6.1.6. Queenof Q5Q6 12348 12345 123458 12345
cannot be placed anywhere in the 6* row. So Position ~~——*——\
is backtracked and it is placed in another feasible cell. This |} forje-ltwok-1do ~,
process is repeated until the solution is found.
03 stacks OP acacas ® andJ quean are in Solution: (3,5, 2,4, 1) Sohtion;
(4, 1, 3,5,2}
(OF stacks 02 ands
ae OS same column 12345

ifs{j] == i OR ((abs(x{j)) - i) == abs{j - k)) then up: Place Q; on (1, 1) —> Successful |
siep2: Place Qr on (2, 1) — Fail -» Backtrack
Siep3: Place Qzon (2, 2) + Fail + Backtrack
return false i andj” queen are in Place Q) on (2, 3) —> Successful
same diagonal
end 2.4,4,9) Solution:
(5, 3,1, 4, 2)
Retum true if kK" queen is nat attacked
retumn true by any of previous (k - 1) queens Current configuration Is (6, 4, 7, 1) for 6-queens problem.
Find answer tuple.
be BT Soln.:
Step $: Place Qs on (3, 1) — Fail + Backtrack the e
= Tuple (6, 4, 7,1) indicates the first queen is inthe third
- Function abs(a) returns absolute value of argument a.
Step 6: Place Q, on (3, 2) > Fail > Backtrack
column, the second queen is in the 4" column, the 1*
The array X is the solution tuple.
Step7: Place Qs on (3, 3) —> Fail — Backtrack queen is in
Fig. 6.1.7 : Non-feasible state of the 8-queen problem
- Like all optimization problem, n queen problem also
Place Q; on (3, 4) > Fail > Backtrack
queen is in the 7 column and forth
has some constraints, which it must satisfy. These column. Given arrangement
of queen is,
; Following algorithm arran; ges n quel constraints are divided into two categories : Implicit 42345678
board and explicit constraints
using a backtracking algorithm. wenonnxe
‘Algorithin N_QUEEN (k, n) Explicit constraints
// Desoription : To find the solution of n x n queen
problem usin, 8 ~ Explicit constraints are the rules that allow/disallow
selection of x; to take value from the given set. For
example, x; = 0 or 1.
4 = LifLB,S x, SUB, Step 9.
id 3% Place Q; on (3, 5) > Successful
Qs on (4, 1) > Fail Backtrack
% = Ootherwise
/ Output
: n x 1 Solution tuple
Solution space is formed by the collection of all tuple I Place
se 10:
wes Place Q, on (4, 2) > Successful LJ U
Om Which satisfies the constraint.
P12: Place Q, on (5, 1) > Fail > Backtrack

Scanned by CamScanner
EEE"
& Bh ng,
Bactiacing
67 nd Se
(mu - Sem 4 -
Comp).
Syllabus Top ic : Sum T=
m Backracking & Branch and Bound
cum of Subset Proble
of 'eading to the ser erin
Q
would(i. beJ andon same
Q tion <Ta
1). edQi onand posi
- neon posiQ, is(k, plac
.
te a0 algorit the order
ne of tleCUISION
° On. calls,
Also, ig number the nodes ini the tree in
= 2014, May 201
condition: (iu Th
satisfy following ry for subset sum problem i The correct combinati
diagonal if they
= K+10R i-j =k-! ygorithin Bien W = (3, 45, a to get the sum of M = 13 for
ij position (5, 1) as one @. Explain sum of subset problem. ag SUB_SET_PROBLEMG, sumo, W, remSum) 61, Solution vector” for
a place next
queen oD ere (et -Golve sub ofsubset problem using backtrack 13.4, 6) woutd be spe= (1, 1,0,B 1]4 because
smn. If we place next bsq
We i *ktracking element5 is not
-
in the same colu! @. Write a recursive backtracking algorithm for sm ptr i computed 50 XB) = 0. Lat deri
queen is already yp" We Number for which subset is to be Numbers in wa derive the solution using
tem index acktracking.
queen on (5.2) then, p97 * SeX = 10,0,0,01 are already sorted.
Let Q, = (4, 1) positon of queen inrows @ Problem
so far
f integers selecte d
Set Sum .
of queen in row 5 in umbers fomwn Sum indicates summation of selected
Qs = (5.2) position Given a set of positive integers, find the combinat;
2, so they are in same 10N of
_ For these two queens, 4 _ J =5~— position numbers that sum to given
value M. Step 1: 151, Adding
j item w,
ot place next queen on
diagonal, so we cann the ‘Sum of subset problem is analogs to knapsack Proble, Wf addition of number SimsSum4w/= Sums =043=3
(5, 3) and sepeat
Knapsack problem tries to fill the knapsack using given ya
location
(5, 2). Try for next dose not cross lum $M, so add item ito solution set.
sitions.
rocedure for all possible of items to maximize the profit. Items are selected in‘ates
5 XUl= XU} = 1X =[1,0,0,0)
‘Action * item wy
tep 2: i=2, Adding
that total weight in knapsack does not cross the capacity ‘Sum ol selected integer is same asW
the knapsack. Inequality condition in
knapsack problem is zg so problem is solved, print solution ,
Sum=Sum+w,=Sum+w)=3+4=7 ‘
poo replaced by equality in the sum of subset problem. weueh Sum <M, so add item ito solution set.
guB_SET_PROBLEM( + 1, sum + wfi] +1, W, Xfil=X(2\=1 = X=[1, 10,0)
Given the set of n positive imtegers W = (Wi, Wa. ..., Wa), Step3: i=3, Adding item w;
Column 1 is already occupied and given a positive integer M, the sum of subset fensurn— whl + 1) Sum = Sum + wj=Sum + w3=7+5=12
M Include i* item to solution
Let us test postion
(5, 2) problem can be formulated as follows (where w; and Sum SM, so add item i to solution set.
capacity jn set and proceed
| |a-t+5-2Centit__| correspond
knapsack problem) :
to item weights and knapsack
X{iJ=XB)=1—> X=[1, 1, 1,0]
Let us test position (5, 3) xt co Exclude the item Step 4: |. Adding item ws
[No conf, so place ton (6.3) guB_SET_PROBLEM( + 1, sum, W,remSum— wi] +1)
Q Sum = Sum + w; = Sum + W,=12+6=18
E w4x4=M Where, x (0, 1). Sum > M, so backtrack and remove previously added
Column 1 is already occupied cod
i=l item from solution set. ¥
Lot us test position
(6, 2) Exclude i” item fo solution
are sorted in ascending order, such tha XfiJ=XB]=0> X=LL, 1,0, 0}.
Numbers ‘
5+3=6+2, Conflict
wy SW. < Wy Sve S Wee The solution is often
sel and proceed
Update Sum accordingly, So, Sum = Sum— Ws
4 are already :
5 Column 3 and
represented using solution vector X. If the i® item is =12-5=7

occupied included, set x, to 1 else set it to O. In each iteration,
Function FEASIBLE_SUI8_SET() And don’t increment i.
not
Let us test position (6, 5) one item is tested. If inclusion of an item does if (sum + remSum 2 W) AND
(sum Step 5: Adding item Ws
No conflict,
so place it on (6, 5) violet the constraint of the problem, add it. cecum + wfi] + 1 <W) then Sum=Sum+w,=Sum+ws=74+6=13
to
2! | Column 1 is already occupied Otherwise, backtrack, remove the previously added Sum = M, so solution is found and add item i
item and continue the same procedure
for all remaining solution set,
Let us test position
(6, 2) Xfi}= X= 1 = Xf), 1,0, 1
No confit,
so place it on (7, 2) items.
The solution is easily described by state space tree.
First recursive call represents the case when the current Complete state space tree for given data is shown in
Column 1 to 7 are already
Each left edge denotes the inclusion of wi and right is reduced by Fig. P, 6.1.3.
item is selected, and hence the problem size
or
=
=
o
=
o

the root to
oecupled edge denotes exclusion of w;. Any path from wij.
for n = 3is
Let us test position
(8, 8) Jeaf forms a subset. A state space tree Second recursive call represents the case when we
do
[ No confit, so place it on (8, 8) demonstrated in the Fig. 6.1.8. Not select the current item. .
te derive the
Thus, final board position would be, Complexity Analysis : It is intuitive
12345678 state space tree, at
complexity of sum of subset problem. Inn items,
Q level, the tree has 2! nodes. So piven tol number
Qa of nodes in tree would be 1 +2+2°+2°+ at
PNOMRONA

To) = 142Pe 424 Pem- 120


in exponential order.
Thus, sum of sub set problem runs
oO

643
oD

= 19, and
.
Consider the sum-of-subset problem, = 4 SUM, ihe
Fig. 6.1.8: State space tree for n=3 wW, = 6- Fi da sotution ree
M=3, Wy = 4 W3 = 5 and
Algorithm
4
for solving sum of subset problem * J
in
Breblem using backtracking. Show the state-space
Tecursion is stated below.

Scanned by CamScanner
Y
——

6-9
Backtracking & Bran ch and Ba,
5 ithms (MU - Sem - Comp) ay exist multiple solutions.
To the inclusion of There muy CIs ie: Numbers P86 tee
TAL level oti, nso
hooaiyie left branch corresponds 1
o! nds to exclu
sion or | above sequence is s : M leftmost go)
consideration. Lefy an ‘Olu
number w, and right branch corresp is stated shows the element under
r for the node and exclusion Tight
number w;. Recursive call numbe n 11 ode, The bold solid branch in tree indicate inclusion
the node. Node 8 is the solutio
corresponding elemenont that level respectively, OF the
below
node.
Jine shows the path to the output

Ex. 6.1.4
on data :
Analyze sum of subset algorithm
M =35 and
i) we,7, 10, 12, 15, 18, 20}
ii) w= (20, 18, 15, 12, 10, 7,5)
itl) w=15, 7,20, 5, 18, 10, 12}
in the com iputing time: ?
Are there any discemible differences
In above graph, the black circle shows the
Soin. : Gray node shows from where algorithm backtra
sult.
Let us run the algorithm on first instance wba rs in leftmost column indicatesClement
w= (5,7, 10, 12, 15, 18, 20}. vderation
consideration at at that t level. Left and rightbyranch unde;
* | SoveTollowing sum
ipcsion and exclusion of that clement respectively ‘Subset problem, using backtracking: | i
W=(1/3,4,5} im. = .
We get two solutions : Find rane :
: ahi 2 247) Soln,:Soorbleibel
ob at ante
Numbers in leftmost column indicates element under Ex 6.1.6
30. Find all possible Let us run the algorithm on first instancew = (5.7, 10, 12,
consideration at that level. Gray circle indicates the node LotW = (5, 10, 12, 13, 15, 18}, M = portion of state space 15, 18,20).
which cannot accommodate any of the next value, so we to M. Draw the
subsets of W that sum
will cut sort them from further expansion. White leaves do
Select 20 and Add next
not lead to a solution. An intermediate white node may or {ree that is generated.
clement may not lead to a solution. The black circle is the solution soln. =
Select 12 and Add next states. In state space tree, left branch follows inclusion of
element We get the four solutions:
number and right branch follows exclusion of number. All
Sum exceeds M, so 1. (5, 10, 20} 2. (5, 12, 18) 3. (7, 10, 18} 4. (15, 20) numbers are sorted in increasing order, so if partial sum
backtrack and remove 12 For efficient execution of subset sum problem, input after adding i number exceeds M, then the addition of any
element, should be sorted in non-decreasing order. If
Sum exceeds M, so subsequent numbers form (i + 1) to n definitely exceeds M.
elements are not in non-decreasing order, the algorithm does
backtrack and remove 15 more backtracking. So second and third scquence would So the node where partial sum exceeds M is pruned ‘There may exist multiple solutions. State space tree for
Add next element take more time for execution and may not find as many immediately. No need to generate state space tree from that above sequence is shown here: Number on leftmost column
Sub set sum exceeds, so solutions as we get in the first sequence. node onwards. The algorithm will backtrack from that point shows the clement under consideration. Left and right
backtrack and explores next child of its parent. This process will and exclusion of the
branch in tree indicate inclusion
Ex. 6.1.5
continue until all numbers are checked. corresponding element on that level respectively.
Solva the sum of subset problems using backtracking State space tee for given problem is depicted in the
{ 5,10, 12} 27<35 Sub set sum exceeds, so
algorithmic strategy for the following data n = 4 Fig. P. 6.1.6. Adding next number to a gray node exceeds
backtrack
{5,10,12, 15}
W = (1, Wa, Wa, Wa) = (11, 13, 24, 7) and M = 31. M, so the tree is pruned at that node. Black nodes
Sub set sum excceds, so
backtrack Soln. : Cotrespond to answer node.

(5.1015) [30<35 | Add next element Items in sub set | Condition | C Fig. P. 6.1.6 : State space tree for W = [5, 10, 1,13,
(5, 0, 15, 18} Sub set sum exceeds, so UO a Initial condition
'S,18) and M = 30.
Possible subsets and their solution tuple are:
backtrack {il} 11<31__| Add next element
33 <35 Add next element {5, 10, 15} > X=[I, 1,0, 0, 1,01 indicates clement under
{11,13} (5, 12, 13) > X=[1, 0,1, 1.0.0) - a That level, The black circle is the
jn leftmost column . :
w4<31 Add next element
3 > 35 Sub set sum exceeds, so
(1, 13,24) 48<31 Sub set sum exceeds, 8 (12, 18) > x =[0, 0, 1,0,0, J solution states.
backtrack
backtrack 1 two solutions>

(11,13,7} 31 Solution Found


vera) OS)

Scanned by CamScanner
Backtracking & Branch ang
- sem 4-Comp) 614 "
[FT anatyis of Algodthms (MU . teeBong
Syllabus Topic :Graph Coleting
Bc ete z ro i
We and axpiin urn ot euboot agri em for = 5, W= [75 8,9, 15) M= 17 416 Graph Coloring
tree forth e given probl is shown in Fig. P. 6.1.8.
Soin. in:: Suale space
> (May 19, dee. 13
May 18)
explain Graph coloring problem us casi
wiite algorithm for same. Ei ar

j
a
, state Graph coloring algorithm, Explain atntogy
for solving it along with examplo, Seed Fle 6.9: Graph and its adjacency matrix
; n Eee ~The problem
Tepresentation

Write @ short note on Graph icoloring, unique: color eae ee


optimal n bua NY is 0 enmiag
solution is notx
Y eee selon Tee possible to color the graph with
: = oth sung at Fig. 6.1.10 and 6.1.11 demonstrate
ion ‘uch instances,
1 Let Ci denotes the i* color.

C)}-—@)
Ly
Graph coloring is the problem of Coloring vertices of
graph in such a way a
so that no two adjacent
Vettices|
have the same color. This
is also called vertex colori

O—9
ng]
problem.

Fig. P. 6.1.8 _ ‘if coloring is done using at most k colors, it is called Fig. 6.1.10 : Nonoptimal solution (uses 5 colors)
k-coloring.

ey:
The smallest number of colors required for coloring C}-—® '
v

graph is called its chromatic number,


Find a possible subsets of weight that sum to m, letn = 6, m= 30, and w{1 ] ={5, 10, 12, 13, 15, 18} ~The chromatic number is denoted by X(G). Finding the
Soin.:
chromatic number for the graph is NP-complete €}—©)
problem.
— State space uve for the given problem is shown in Fig. P. 6.1.9. Graph coloring problem is both, decision problem as
Fig. 6.L.11 : Optimal solution (uses 2 colors)

well as an optimization problem. A decision problem is - This problem can be solved using backtracking
stated as, “With given M colors and graph G, whether algorithms as follows:
such color scheme is possible or not?”.
List down all the vertices and colors in two lists
— The optimization problem is stated as, “Given M colors
and graph G, find the minimum number of colors Assign color 1 to vertex 1 {
required for graph coloring.” 0 If vertex 2 is not adjacent to vertex | then assign
- Graph coloring problem is a very interesting problem thd same color, otherwise assign color 2.
of graph theory and it has many diverse applications. co Repeat the process until all vertices are colored.
Few of them are listed below. — Algorithm backtracks (A) (8)
* Applications whenever color i is not
(@) Design a timetable possible to assign to any
(i) Sudoku vertex k and it selects next
ed. ©) (0)
(ii) Register allocation in compiler color i+ 1 and test isrepeat
in
(iv) Map coloring ' Consider the graph shown
Fig. 6.1.12
() Mobile radio frequency assignment . Fig. 6.1.12.
A, the same colercant
The input to the graph is adjacency hil If we assign color Lto vertex ed
C.Innext step. B is assign
representation of the graph. Value MG, j) = 1 inthe mans be assigned to vertex B or
A is already colored and
Tepresents there exists an edge between vertex ! and je Ain some different color 2. Vertex beassigned
Fig.P. 6.1.9 .
vertex D is a neighbor
of B, so D cannot
Staph and its adjacency matrix representation are show goes on. State space
tree is shown
The possible solutions are {5, 12, 13}, {12, 18} and {5, 10, 15) color 2. The process
the Fig. 6.1.9, in Fig. 6.1.13.

Scanned by CamScanner
:
Backtracking & Branch an (2
ig ol f Algorithms (MU - <Comp)
=m

Panatysis of Algorithms (MU - Sem 4-com) 6.13 = complexity Analysis


SS Poing | FE initive rom the Feu that we at
| 3 colors for graph coloring problem it ma
The number of anode increases exponentially at | bd tite None of the vertexes in and y is, Eraph is
every evel state spaze strein With M colors
state 5]
and n vere “
ce tee
ior
a
vertex OF He set ef, “onnected
total nue a oe we pace We Would ye ‘I yertices in U can be colored using one col
1+M+ =
@ Hence, Fa) = 1+M+M?+ 2 M+...
> no MYT nat
MP = Sd
* ue ere
ries
is nO adjacency
AC) betwe .
een vertices, Sin ‘Or as
in V can be colored using ong color. "

So, Tes) = OOM. time, pipartite graph needs only two colors Tor raph colaring
‘Thus, graph coloring algorithm runs in exponential

1 Planar Graphs
@. p
What are planar graphs? (2 Marks ob
rst th followingi the map, Explain
planar graph fceor ne
© — A graph is called planar if it can be drawn on a 2p to find m-coloring of this planar graph by using an
plane such that no two edges cross each other, Graph ‘ering Backtracking algorithm,
coloring problem is a well-known problem of a Planar me
® graph.
- Planar and non-planar graphs are illustrated jin
a
is 6.1.13 Fig. 2. 6.1.14:
Fig. : State space treeof the graph
of Fig. 6.1.12 0 SRR
©-F.6.1.10(b) reer
: State space tree of piven graph
|
Thus, vertices A and C will be colored with color 1, ——Silsbus Topic 15 Purse Problem __
DN ;
and vertices B and D will be colored with color 2.
Algorithm f h coloring is described here :

NI
_COLOMING, COL
‘Algorithmm GRAPHGRAPH _COLORING(G, COLOR, i) YOO Fig. P. 6.1.10 6.2 Branch and Bound
/f Description : Solve the graph coloring problem using backtracking Soln, : 6.2.1 General Method
// Input : Graph G with n vortices, list of colors, initial vertex i © In Fig. P. 6.1.10, area 1 is adjacent to area 2, 3 and 4,
> (May 14, Dec. 15)
J! COLOR(1...n] is the array ofn different colora Planar graph Non planar graph Soina planar graph, vertex 1 will be connected to vertices
Q. Comment on model of ison: Branch and
2,3and 4, Area 2 is adjacent to area 1, 3, 4 and 5, so ina
# Oulput : Colored graph with minimum color Fig. 6.1.14: Illustration of planar and Bound.
planar graph, vertex 2 will be connected to vertices 1, 3,4 Q. Write a short note on Branch and bound strategy.
Jif CHECK_VERTEX(i) non planar graph
ad 5. This process will be repeated for all areas. Hence, the
If Lis the last vertex UU Tare EL
ifi == N then in graph, print 2. Bipartite graph planar graph would be, Q. Describe the method with respect to Branch. and
print COLOR(L.. solution tuple Q. What is bipartite. graph? How many colors are Bound. ; {7 Marka)
(5 Marks) . Explain the branch and bound algorithmic strategy for
else © solving the problem. (7 Marks)
ic Assign color j 7 Graph
3 G is said to be bipartite graph if we can‘ divide r the statespe tee tre aa fea d
jel
jto the vertices of graph G into two disjoint sets U and V = Branch and boond builds
: ‘eta co
while ($M) do aust) such that each edge ts a vertex in U to one the optimal solution quickly by pruning few 0
branches which does not satisfy the bound.
COLOR(
+ 1) -j vertex in V. Disjoint independent sets U and V are
Fiat inched Backtracking can be useful where some other
jejtl called partite sets. If we cut the edges by vertical lines, raiannation techniques like greedy or dynamic
end Loop through already colored all vertices become isolated vertex. Fig. 6.1.15 progaming fail, Such algorithms
are typically slower
(i- 1) vertices describes the bipartite graph. . sr counterparts. In the worst case, it may nan in
end t Fig, P. 6.1.10(a) Ta tal ne, bt careful selection of bounds and
makes an algoria thm to run reason ably faster. *
using iB backtracking branches a
od
Function CHECK_VERTEX(i aleori P problem
This can be sol ved _ Most ofthe terminologies of backtracking are used in
a Retum true if verticesi ‘orithms as follows : . ve chapter 00, In branch and bound, all the children
j
forj—1t0i-1 ilado © List down all the vertices and colors
in two lists we odes are generated before any other live node
and j are adjacent
if Adjacent(i, j) then
if COLOR() == COLOR() then ° AssignBn color 1 to vertex 1 becomes Ene. ode. nique in which 5 E-node puts its
© Tf vertex 2 is not adjacent to vertex | thea assign | Branch and bound te! alled FIFO branch and bound
the same color, otherwise assign color 2. children in the queve is
If color assigned to vertex | and
ui Repeat the process until all vertices are ee appa st children in the sie then it is
any of its adjacent vertex is same,
A
then backtrack to aa backtracks whenevet F color! set +) ~ aed LIFO branch and ve neon. Heuristic
Ssign to any vertex k and it sclects ne! ing 4 Bounding functions °° which maximizes the
"td (est is repeated. This graph can be colored using * | — function computes the mi
_H Fig. 6.1.15 : ipartite Graph “lors. Gray node indicates backtracking:

Scanned by CamScanner
O
ig of
ranslysis Algorithms (MU - Sem 4-co!
Backtracking & Branch and
f 23 Applications of Backtracking
=
4Comp) Backtracking
[Manatysis of Algorithms (MU - Sem cach and bound technique "DUE isj Uschi tg ta TEWale
eos ag 2 id dng & Branch
Sah oe and Bound t
search oF minimizes te ‘
probability of better |_ Used 19 . ly nodes to becomeE
probability of worst search. Accordingot 10lowest izaniec
maximnheuristi ‘Typically used to solve
-
We, tc,
Mode,
or
the path from answer node to root ib
pronol
aevmminimzation problem, highest expansi 25S decision problems. optimization
Nodes in tee iy,
pro} aapsack problem
‘St . Any measur
ooo is selected for further on from space tree (uy proble
{lingng salesman problem
travelli
ay
Malor seach me Searching the state space tree incurs
“| Nodes in state
of nodes. are explored in dept h- explored in dept be Assi gament problem And
hence, nodes are ranked using
— Example : FIFO Branch and bound approac h. of breadth-first 9, reg tena 1) eo,
and bound first order. be the adgin
Let us try to understand
the FIFObranch
from current Next move is aw 5c job scheduling problem x. Functi effort needed to reach an
answer node fkromAdditional
problem. State space tee Next move
with the help of 4-queen
boun d meth od is shown
state can lead to bad towards better solutigg”® 62 .!
4 Control Abstraction
Node x such that, Funetion &() assigns ranking to
branch and
generated by FIFO choice. Search
in Fig. 6.2.1. - wa = (OQ) + a6)
1s defined by Entire state space 62.4(Al ) LC
= Solution vector for the 4-queeneachproblem
% indicates column
‘On successful sear
ch of
Wee ig ~ tn en of reaching functi fromx to root.
4-tuple X = Oy, XxX x4),
solution in state
space searched in order
to fing What is LC search? 2 Marks) € search, the cost inction e(.) is. defined as
optimal solution, a FIFO or LIFO is a very crude way of follows:
number of the i® queen in i® row. tree, search stops. a
Initially, when chess board is empty, only node 1 Bente Medoes
— not check the goodness of node.“Tey wither eet the oO AEX iis answer t node,ro t, then e(x)(x) iiss the cost of
‘Applications Applications
E node and generates teachingx from
would be a live node. It becomes live node
node 2, 18, 34 and 50, and they are kept on that queen
Haniiltonian cycle Travelling salesman E node strictly in FIFO ot LIFO order,
represent
list. These four nodes at level one+respectively. problem problem any preference 10 the node having beuer oe of ne Tot answer node and subtree of x does not

°
, ces = eo;
1 is placed in column I, 2, 3 and the order in
Graph coloring problem _| Knapsack problem quickly.
getting" answers :is Otherwise, the ¢(x)
i answerc()nodeis than an
ia
The number inside the circleTheindicates ° ce
tale
Example: 4 Queen Probl
em. the answer node. When
E node. number beside circle — In Fig. 6.2.1, node 31
which node becomes —
ang
State space tree for 4-queen problem using branch ang i i should become the E-node, s0 that. on answer node in subtree x. of miniemum. cost
indicates the order in which node is generated. node 2is 30 is generated, it
are shown in Fig. 6.2.1
bound and backuacking
When alll the children of node I are generated,
every next step we get the solution. . But the FIFO | 6.2. AB} Contr Abstraction for Least Cost
— 3, 8 and 13. Fig. 6.2.2, respectively varategy forces the search to expand all the lve ie
selected as E — node. It generates node so node 3 is _ in depth-firg
Backtracking method explores the node fails.
Queen Q; cannot be placed in
column 2,
13 are visiting nodes Then the
until it
generated before 30 i.e. node (35,40, 45,51, 56, 61,9a” Q. on the control abstraction for LC search with
killed usingbounding function. Node 8 and order. It keeps
11, 14, 16}.
added to the list of live nodes and node 18 becomes the method backtracks. Nodes ara explored in LIFO order. 6 sspact to Branch and Bound, {6 Marks)
thm generates This search can be speeded up using the ranking
next E node. It generates node 19,
24 and 29. If Q: is in Refer Fig. 6.2.1, at level 0, the algori _
TE
; How does {t-help in findlng.a solution for branch and
node 1, the Fig.
the 2™ column, we cannot place Q2 in column | or 3,80 node 1, then instead of generating all child of function é(.)for live nodes, Consider bound algorithm? (6 Marks)
list of
Kill node 19 and 24. Node 29 is inserted in the backtracking approach expands node 2. After node 2, ranking function assigns a better value to node 30 than Q. . Explain in detail Control abstraction for LC search.
live nodes. node 6 becomes the E-node. Node 6 fails to find the remaining all live nodes, then node 30 will become the < (6 Mar
E-node in the order solution, so algorithm backtracks and genera
tes another
E- node as soon as it is generated and we
will get the - Let T and c(.) represent state space tree and cost
- In FIFO approach, nodes become
es E-node and
they are generated. child of node 2, i.e, 8. Then 8 becom solution on very next step, ie. node 31. function for the node in T.
child 35, ng rms a depth-
— Node 34 will be the next E node, It generates genera tes node 9. Thus backt racki perfo
Ranking basically depends on the additional effort — For node x € T, ¢(x) represents the minimum cost of
Q: cannot be in
40, and 45. If Q, is in column 3, first search. answer node in the subtree rooted at x. Thus, cry
node 35 (cost) required to reach to answer node from the live
column 2 and 4, so kill node 40 and 45, Insert represents the cost of the iminimum-cost answer
in state
in the list of the live node. This is how
this search is node. spaceT.
number of
proceeding. - For any node x, the cost is determined by (1) - Finding exact cost function c(.) is challenging,
so in
answer
nodes in subtree x needs to be generated before practice, the heuristic funetion £() is used to estimate
of levels from x to
node is generated, or (2) number of).
answer node. simple to compute and
— Heuristic functions are often
it can be seen from the
- Using the second measure, provide close to optimal perfomance.
31 is is described. below. Function
Fig. 6.2.1 that the cost of answer node is 4 (node = Algorithm for LCSearch n .
four level away from the root node). Leasi() finds the live node
with the minimum c(.). This
answer node, So the cost the list of live nodes and returned.
Node 39 (child of 38) is also node js deleted from the fist of
are the new live node to
problem using of nodes (18, 34}, (29, 35}, and (30, 38) Function Add(x) adds
data structure is used
10
Fig. 6.2.2 : State space tree for four queen respectively 3, 2 and 1. And remaining all nodes on five nodes. Min-heap
50 that the node with
backtracking ter than 3, 2 and I, ' plemen a list of live nodes
iy t that Least)
Fig. 6.2.1 : State space tree for 4-queen problem using level 2, , 3 and 4 have a cost greal minimum cost always be at the from. So
further respectively.
branch and bound - Branch and bound cut down the node from the function can Fetriene the sishmum ost node In
exploration by applying a certain
bounding function 09 ~ With this approach, only 1, 18, 29 and 30 becomes constant time. path from answer
6.2.2 Backtracking Vs Branch and Bound the bounding
cach node. If the node does not satisfy selection
E-node
. ot D the
in 4
a. function then it rejects that node. Node ~ And the other nodes which are se and - : at te path to the root,
Differentiate Backtracking and Branch and Bound technique i node x.
jg stored withi each Vive
de to the 100
Method. Illustrate with an example of 4-Queen’s queen problem using the branch and bound con verted to E node wo! wuld be 2, 34, 30 2° pore
Problem. (6 Marks) discussed in the previous section. 31. per ofnodes.
{ o
Q. What is the difference between backtracking Cost measure 1 generates minim mn
approac h and bound
and branch approach. (5 Marks)

Scanned by CamScanner
4 - Comp)
ysis of Algorithms (MU - Sem
a .
517 Backtracking ig & Branc! hand Boung o a the initial state with random dis !
tribution of
: Givens petween 1 to 15, aim is to achieve ty
ef. Analysis of Algorithms (MU - Sem 4 ~Comp) for FIFO branch ane
Discuss the ‘control abstraction gE
=
Algorithm LCSearch(T}
7 Marke)
qumP>, moving the empty tile. This is Np
Ji Description : Find answer node from state space
tree and print
boun
y cither BFS or DFS
sem. Jnitial and goal state of thepr oblem Complete
is shown
Branch and bound method emplo t 4p, 6. 623%
ce
path expanded nodes are kept in
search. During B
{Input : State space treeT on the slack
oat node Is the queue, whereas in DFS, nodes are kept
4 Output : Success/ Failure answer node BES approach is first in first out (FIFO) method, white
if T is an answer node then . In FIFO branch ang
output T DFS is Inst in first out are(LIFO)
inserted into the queue and

rete mtmine ]
bound, expanded nodes

rt the first generated node becomes next live node,


end Root be the first live node and bound
Control abstraction for FIFO branch
EeT
Initialize list of live node to empty technique is shown here.
(b) Goal state
While (true) do ‘Algorithm BB_FIFOO in tree T
(a) Initial state
for cac! x is new live noda and 1 Tis the state space trce and a is the node
Fig. 6.2.3 : States of 15-puzzle problem,
ifx is an answer node then added to list of live
output path from x to T. nodes if Tis answer node thea
1 € min(cost{T}, upperBound(T) + E) Cost function : Each node in the state space
ttee is Fig. 6.25: Tile positions afler shsequent maves
print (T) associated with some cost, Cost function is applied to Algorithm for 1S-puzzle problem is stated below:
Add(x)
x.parent — E
return each node and the function helps to select the next B | |Stuct Node ~
en end node. Z node is the node being evaluated. The node a
if there are no more live nodes thea EeT
with minimum cost should be selected for the further Node *next,
print “No answer node” while (true) do Node
Auton *parent;
return List of live node is empty and for each child x of T do expansion.
end answer node is nal found ifx is answer node then
‘The cost function is defined as, C(x) = g(x) + h(x),
E = Least() print “Display path fromx 1o rool”
where g(x) is the cost of reaching to current state from || Algnitha LC_SEARCH(Node * 1)
noderer
{|if*isanans hen
the initial state and h(X) is the cost of reaching from
Current node was not answer node, returm
so update E node with minimum cost end
node from list of live nodes ENQUEUE(s) // Insert new node at the end of QUEUE current state to the answer state,
xparent = £ // R[x] indicates parent of x _ Goal function for 15-puzzle problem is defined as the
6.2.4(C) Bounding ifx is answer node AND cost(x) < upperBound number of tiles on wring location.
Q._ Describe bounding with respect to Branch and Bound. then
= C(x) = f() + hGOd, where f(x) is the length
from the Irtelizethe live noes tobe empty
(7 Marks) u € min(cost[T], ub(T) + E)
root node in state space tree, ie. number
of moves so hile (rue) do
— Branch and bound technique generates all the child end which are not on the for cach sande
far, and h(x) is the non-blank tiles
nodes of E-node before another node becomes E node. end i . oa ryantate then. a
ifxis
if no more live nodes then correct
Ch location
— Let e(x) represent the cost of answer node x. The aim is there are four ae
to find minimum cost answer node.
print “No live node ex - For the initial state shown in Fig, 6.2.3,
the cost for all
print “Minimum cost :' ub
possible moves (left, right, up, down), end
- Search strategies like FIFO, LIFO and LC search Tetum n
four configurations is shown in
Fig, 6.2.4, Tiles on AédGi) (Ade to ist of live nodes
differs in terms of the sequence in which they explore parentofx
the nodes in state space tree.
end
wrong position are highlighted in red color. (3 Paret) CE Set Eto thethen
E« DEQUEUE() // Remove front node fram the QUEUE
ifuhere are no mare live nodes
Let ranking function ¢ (x) £ ¢ (x) be a lower bound of end
print “Amster no found”
the solution. If upper represents the upper bound of 4
6.2.5 15 Puzzle Problem cost od 2Enode
minimum cost answer node, then all live node x with fe Teas) Stanninum
é (x) > upper may be killed because all the answer
> (May 13, May 14, Dec. 14, May 15, May 16, May 17)
node reachable from x have cost ¢(x) 2 ¢ (x) > upper. a
Q._ Explain 15-puzzle problem using branch and bound. ic ; Travelling Sal
esman Problem
The initial value of upper bound may be taken as <, or MU - May 2013, 10 Marks
it may be computed using some heuristic function. m
Q. Write note on: 15-puzzle problem. Ui an Proble
Value of upper is updated for every new answer node. i & cay 1, May 15)
VEE eke ravelling Sales!
Maximization problems can be directly converted to
(MENU ARKO [jargon problem using branch
minimization
objective function
problem by changing the sign of Q. Explain how branch and bound strater
Ea = evi ere
in 15 puzzle problem, and jig Dour sees TSO
raveling
6.2.4(D) Control Abstraction for FIFO Branch 15-puzzle problem is the problem of arranging 15 titles + WC) afer frst 0°"
Fig. 6.24: Cost fi) note 0": [eee
and Bound
Thi ™configuration |]. wae" ee erks)
andon willcashnotbe |)"
in 4 x 4 board such that tiles are ordered from left 0
-
Q. Explain the FIFO branch and bound with respect to Tight and bottom to top such that the bottom right tile
contains empty space.
Thitd has the ™
Temaining three states will be cut dow on
ext move raveving sansren oben ‘

explored further. The possibilities


branch and bound technique with a suitable example.
(10 Marks) the me see

Scanned by CamScanner
& Branch end bo
of Algorithms (MU 4- Comp)
Backtrackin
eManaiysia of Algorithms (MU - Sem 4 - Comp) 6-19 : e po ing ~ww and column Of f M, Mesires has at le,
has © value. Let M represents the distanc tris oF god i vpaltix is reduced matrix, = * one zero
— Travelling Salesman Problem (TSP) is interesting Sastee, M can be reduced as follow :
problem. Problem is defined as “given n cities th )) 300 mt reduction cost y= 1404349
distance between each pair ‘of cities, find out the Mreres = (My~min (My!1SjSn, and Myces +054
Consider the following distance matrix: o space tee for 5 city problem j S depicted in
which visits each city exaclly once and come back 10 Sumber within circle indicates the
(he fe | 20/30] 10] 11 orderin which
starting city. with constraint of minimizing Oo generated and number of edge in, Mdicates
travelling distance.” isle ]tel4 |2 thecity
— TSP has many practical applications. It is used in M=/3 [5 [= [2 [4 wer sited
network design, transportation route design. Objective
9[6 | is] = [3 os
is to minimize the distance. We can start tour from any
random city and visit other cities in any order. with n 16]4 |7 | 16] M. is 10300

Find the minimum element from cach row and subtra, 4


‘not
cities, n! different permutations are possible. Exploring minim vue fe Matrix. Reduce it subtracting
all paths using brute force attack may not be useful in
real life applications.
it from each cell of matrix. et, “om corresponding column. Doing this we
= | 20] 30] tof 11] 490
6.2.6(A) LCBB using Static State Space Tree 15} |16]4 [2 | 42
~ Branch and bound is effective way to find better, if not m=-[3 [s [= ]2 [4 [2
best, solution in quick time by pruning some of the 916 | il~ [3 |s3
unnecessary branches of search tree. fie[4 [7 isle |g
Tt works as follow :
Reduced matrix would be:
Consider directed weighted graph G = (V, E), where
«| 10] 20] 0 |1 Fig. 6 2.6 : State space diagram for 5-cit
ty TS P
node represents cities and weighted directed edges
ans
Row reduction cos + Column reduction cost
represents direction and distance between two cities. Bilis] 2 lo Tien (1042424344) (143}=25
, ” . Teans
Initially, graph is represented by cost matrix C, where Mronrer= {1 | 3 | [0 [2 nat is travelling salesman problem? Find the solution of This isthe optimal allcosttours
ofthein graph
cae, haslength
rength at
at least
least 25.
CG, = costof edge, if there is a directpath owing travelling salesman problem using branch and optimal Path.
from city ito city j 3} 1S} e]0 i nd method. ‘State space tree
Cj, = = if there is no direct path from city fifo [3 [iazfe (1)25
Eto city j. Row reduction cost is the summation of all the values
Coavert cost matrix to reduced matrix by subtracting subtracted from each rows: Cost Matrix =
bp

minimum values from appropriate rows and columns,


Row reducti (M) = 10+2+24+3+4=21
cost on
such that each row and column contains at least one 1
zero entry. Column reduction ® © © ©
ied cost of reduced marix. Cost is given by
Matrix Mgosgat is row reduced but not the column Fig, P. 62.1 : Partial state space tree
samm of subtracted amount from the cost matrix
to convert it in to reduce matrix,
reduced, Matrix is called column reduced if each of its sant Letus find cost of edge from node | to 2.3,4,5.
column has at least one zero entry or alls° entries.
Prepare sate space tree for the reduce matrix
Mooses
= (Mj—min {Mz 11<j<n, and Mg<o }}
To reduced above matrix, we will find the minimum Toot node SaM,(0= MMPs
element from each column and subtract it from each cell of
= Derive cost of path from node i to j by sting all ) SetM,ILI= = a
matrix.
ge is to be included. eats in row and j column as =. Reduce the resultant marx if required
[= [10 [20[o i
Set all values
in row
Set MUj]Ii] = ©
{3[= [i]2 fo ~ Cost of corresponding node N for path i toj is
Maczs={1 [3 [= ]o |2 summation of optimal cost + reduction cost +MOIG
[ae] 3 [isle fo ~ After exploring all nodes at level i, set node with
{eto ls [2l=| tuinimum cost as E node and repeat the procedure wai
t+ibusdt
10 3 00 all nodes are visited.
Wher L ise.
cost of reduced cox matrix and ris Ali j]. Given matrix is not reduced. In order to find reduced
8 fall nodes are noe Column reduced matrix Meares Would be:
Matrix of it, we will first find the row reduced me ode 2:
e [so] i7fo [1 followed by column reduced matrix if needed. Weea “o@) = (o(1) + Reduction cost-+ M, {13 (2)
12] [u]2 fo find row reduced matrix by subtracting ane = 40410235
Moseas={0 [3 [~ |o [2 clement of each row from each element ©
Matrix M is called reduced matrix if cach of its row
and column bas atleast one zero entry or entire row 15] 3 12] {0
or entire cortesponding row. Procedure is described bel
ufo fo [i2[e
Reduce above cost matrix by subtracting
Value from each row and column.

Scanned by CamScanner
rin
GFP cays of Ago(MU t- SO
- Sen ms
Cos of nade9
Oy) = 19) + Re
tocontsi
+ Myoa
£243 }
= Beer +1=82
MSdede25 Path 1425)
SeMyg = QUIEN RID =M iS} ==
Let sexploce
all three nodes.
SEMIS a)
Mat x Mg is reduced. Reduce resultant
matrix if requ
Select path 1-2: (Add edge 42)
cast of node 8 ire.
saM0= MEID Og) = CO) + Reduction cost +s, [ats]
=MOPI== 2 WH11+0=36
seM, RIM= = ;
mairix if required.
Reduce resultant
= lols |ele}ox
Mo f= [~|u]e[o]so
0 fel=[=[2]30 =m, Costof node 10
\
oe felo[e|e|ox CU) = C(6}+ Reducost
ctio + My [2165)
n \
lif=]o |e}=|30 = 28+04+0=28
a \
State space tree
o 00 x 0 uy
Matrix M, is already reduced.
Cost
of node 6
CO) = C(4)+ Reduction cost + M, [4] [2]
ooo x 0 = 254+0+3=28 50
cost. Let's find the cost
Matrix
M, is already reduced. Path 1-4-2 leads to minimum
Select edge 4-3 (Path 1-4-3)
Costof node + for two possible paths.
Set M, (U0 = M10 = M,0Bl==
C(4) = O(1)+ Reduction
cost + M, [1] [4]
Set M(3][1] =
Add edge 2-3 (Path 1-4-2-3)
= 25+0+0=25
Reduce the resultant matrix if required. saM, (0 = Mg (410
Select edge 1-5 @l=|=|=|=]2x [= Jale]=l= = M210
Set M, (= M,O(S]=< M=|12[=[2|=[oJ+0 [12]~]~[~]o = MOBI="
SiMe) =
SetM, [3] [1]= oo [3 Jeofeo]2 |+2 == [1 ]=l=[0 Jon.
Reduce the resultant matrix if required.
ce [eo ]eo]oe|e2]—seo [oe fo]
0] 200
Hfofe[ele|so [t1]o [efele
blidd
Woxxo
Mis not reduced. Reduce it by subtracting 11 from
column 1.
0 | 2 | co | oo
wlols

L | 2] 0 | 00
Cost
of node 5
oMa]e] lle fe
C(5) = C(I)
+ reduction cost
+ M, [1] [5] rost of node It |
| co | £0 | ce | 20 cost+ Mp S18]
= 25+5+1=31 (to) + Redaction
8

=
{0 [0 [afalo ‘oun
= B+0+0=2B
State space diagram
Cost
of node 7
Node 4 has minimum cost for path 1-4. We can go to
C(7) = C(4)+ Reduction cost
+ M, [4] [3]
vertex 2, 3 or 5.
= 254+2+11+12=50

Scanned by CamScanner
ays of Algorithms (MU - Sem4 =Comp)

Backiracking & Branch and Bouny -7= (2) = CO) + Reduction


egg pn
6-23 h (py
~
Panatysis of Algorithms (MU - Sem 4 Comp) cach ‘element of
cach row fromis described
clement of row.
2 25+04+0=35

Conesponding Procedure below:


Stale space tree by reducing minimum value from
_ Reduce given
ling rows.

0 entry.
Column 3 and 4 in matrix M, does not contain C5) =

So to reduce the matrix M, subtract


minimum value © == CO +Retason
443239
con 1
Add edge 2~4 Path 1-2-4)
= 5
o[]3 /o
ced matrix
o[7[=|1 |=, Matrix My is redu
cay= COF C (1) + Reduction
cost +M, [1] 3]
ojolofe
' 2 35414+4=40
addeage 1-4
Matrix M, is reduced matrix because each of its row
and column has 0 entry saM, UE) = M, [1 f4l=%
Ex. 6.2.2
of TSP using branch and bound
Cost(M,) = C(I) sam, (411) = °° C() = C(Q)+ Reduction
cost + M, [21 (41
Solve following instance
method. = Row reduction cost + Column reduction cost Reduce resultant matrix if required. = 354040235
R¥ = (10454+648)+(1+5)=35 State space tree
This means all tours in graph has length at least 35.
ooo

This is the optimal cost of the path.
0
=

oe State space tree

(435
Fig. P. 6.2.2
000 x

®e © ® My is reduced matrix
Cot(M,) = C(4)=C()+ Reduction cost+ M, (1] 4]

Vertex 1 is connected with remaining all vertices let us = 354+0+5=40


Soln.: final lower bound for each node in state space tree.
State space tree
- The procedure for dynamic reduction is as follow: Add edge 1-2
Add edge 4-3 (Path 1-2-4-3)
h
et My (1) (J= M2) 1 Mh 1 = Me B=
~— Draw state space tree with optimal reduction cost at
root node SetM, [1]0 = My[[2]=0
SetM 2] = © SetM, G10] = © \
- Derive cost of path from node i toj by setting all
mmatsix if required.
entries in i" row andj® column as e. Reduce resultant matrix if required. Reduce resultant
Set M[jJ[i] = = oo | oo | co | op | 3x
- Cost of corresponding node N for path i to j is co {oo} 3 [0 |30
summation of optimal cost + reduction cost + M[j][i)
0 }|oje}1}>0 =M,
Afier exploring all nodes at level i, set node with
minimum cost as E node and repeat the procedure until
0 |] 0 |o} 50
Node 2 is the least cost node so select that path from
all nodes are visited. tiid 1~2, we have two choices, select 3 or4
— — Given matrix is not reduced. In order to find reduced x 0 0 Addedge 4 ~2-3
matrix of it, we will first find the row reduced matrix Each row and column in M; either contains at least on€
followed by column reduced matrix if needed, We can O entry or all o» entries. So it is already reached matrix. 21 = Mtl
find row reduced matrix by subtracting minimum
= MD (1=%

Scanned by CamScanner
isof Algorithms (MU -Sem 4 =Comp)
Backtracking & Branch aNd Bo, ix Mj=Costofnode3,
Panarysis of Algorithms (MU - Sam 4 - Comp) _.
6-25
graph costs at lea25st
the 25. y=une Y cost FT MUSIC =25+11+0=36
impliesies, aany routehas ofcost We shall select tpg
Matrix M, is already reduced. 2 .
ee his pointsdy
mamic state aspace tre CE WOUld look like,
leads to maximum
Cost of node 7 edge such that exclusion of that
gives maxima
CQ) = C()+Reduction cost + M, [4] other words, select the edge which
probability of minimum cost of path on inet lusion ofm Include <3,1%, exclude <3,1>
= 35+0+0=35
;
State space tree
that edge. 25 38
the ed
We can achieve this by considering one ofmatrix,
reduced :
with reduced cost zero in “ we
6.2.7(0) edge <I, 4>, <2, 5>, <3, In,
jncll sion of <3, l>, we get matrix M,, In M,,
Fig.
ed, 3>, 5, 2> and <5, 3> has b>O reductio n cost, tf yt On inep>, a, <2y So, 271 <4, Sy 5>, ns
<5, 2> and <5, . 35 ed ges
with Oninclus
Inclusion of .
of the edge from this list, ie
<a, poe ae selecting any of the edge <a, b> from thislist have <I, 4p, 25>, ane Bet matrix My. In My, we
select any
resullant cost matrix M will have entry = on position 10% ola] = ©. Repeating previous step, we get wets my ofthe edge cx te'g ig iO wali:
ill7 rae | [4 [ae [as] [2}fb} = ce,Repeating. previous siep,romwe getthis ist will make
Mfal(b).
If we include edge <1, 4>, set M[1][4] = 09, and reduce
a 2 se@—_
Toa ee
ERS
M. This is done by subtracting 1 from row 1. So cost of
right child will increase by 1.
I we include edge <2, 5>, set M[2][5] = co, and reduce wd
an ~
ZS, 3p maximizes oF right child, so eva | Selectedge
meme
edge <1,
Te, csp, 4> '> maximizes
Lette - so let us
< child,
maximi: Cof right
M. This is done by subtracting 2 from row 2. So cost of
right child will increase by 1. Set MAUI]=<
If we include edge <3, I>, set M[3][1] =o, and reduce Set My{ [4] = 22
M. This is done by subtracting 11 from column 1. So Set M,[4][5} = © to
cost of right child will increase by 11. avoid cycle
Thus we will have, Reduce the matrix if
Optimal path = 1 -2~4~3—1
and cost of path = 35. Edge <1,4> | <2,5> 6b) 3 e451 55] ab required

6.2.6(B) LCBB using Dynamic State Space Increment | 1 2 "1 0 3 3 7 it


Tree fight child
Here, edge <3, I> and <5, 3> maximizes the cost of Inclusion of <1, 4>| Mx = Maiix Mg is already
Describe least cost search (LC Search) with respect| in Mo reduced,
L =0
right child, so we can select any one of them. Let us select
to Branch and Bound. (7 Marks)
edge <3, I>. Cost of matrix M,= Cost of node 6, C(6) = C(4) + L+
Q. Explain the Least cost search with respect to branch
and bound technique with suitable example. Set M[3][ ] =< co | 10 ad Qo 1 Mi{i][4] = 28 +0 +028
By excluding <1, 4> we get,
(7 Marks) Set M[ ][1] = eo] 2 I 2 0
— Static space tree is m-ary tree. In dynamic state space Set M[I][3] == co | oo | o | o | o
tree, left and right branch indicates inclusion and Reduce the matrix co | 3 12 | © 0
exclusion of edge respectively. Bounding function is not reduce|M, = Reduced matrix,
computation is same as previous method. Dynamic
‘frequired o|ofo]/ nie This
matrix, reduce it by|L=3
State space tree is binary tree. At each node, left branch subtracting 3 from
(i, j) indicates all the paths including edge (i, j). Right Inclusion of <3, 1> Mp = Matrix is already reduced,
2" column
branch (i,j) indicates all the paths excluding (i, j). inM L=0.
Cost of matrix My= Cost of node 4, C(4)=C(2) + L+
At each level, we select the edge which gives highest Cost of matrix M,= Cost of node 2,
M513] = 25 +3 +0= 28 set Mil, 41 = © and
probability of minimum tour cost. This can be done by reduce matrix if required oa
C(2)= CCl) + L+ MBBI[1] = 25 +04+0=25 By excluding <5, 3> we get,
‘Cost of matrixix ‘My=M= Cost of f . _
selecting the right edge which gives highest value. We
By excluding <3, I> we get, +0537
can find such edge from reduced cost matrix. Consider em=Ca) + LeM[iyis}=28+9
the following instance of TSP. State space tree would be =
[~ ] 20] 30] 10] 11] [ Jiolizfo fa ce |1o/i7/o {t = [10 [a7 fo [1]
{tse [16 [4 HY EE a9 0 12{ |11]2 |o fe ful2 [o}
3s f=|2{4| [fo [3l=lo{2 @ [3 [«|o|2 =[3
l= |0 |2|
fiofe [isl [3 |] [15/3 fate fo 15[3 ]12[% [o 4{3 [f= [o|
{is}4
[7 [te[| [fo fo fate t1f0 fo | ia loo ofo fo | 2lel
(a) Cost matrix C (b) Reduced matrix M with L = 25
Fig. 6.2.7 : Cost matrix and its reduced matrix Set M3, 1] = co and reduce Reduce matrix with Cost
q of matrix My= Cost of node 5, 56
Reduced matrix is computed in same way, as we did in
matrix if required
1S) = C2) + L + MASS] = 254 HHO te,
previous case. Reduction cost of the matrix is 25, This this point, dynamic state space tree WO

Scanned by CamScanner
Backtracking & Branch angBo p=" .
- Comp)
6: 27 one und
yal =o 10 aval id = and Bou Bound
sac and
(FPanatysis of Algorithms (MU - Sem 4 ‘Thus we will have, Kona
On inclusion of <1. 4>,
Node 6 will be the next E-node. edge’
we have <2, 5> and <4, 2> t matrix if
we get matrix M,, In My,
with 0 value. So selecting any of the edge <2, b> from this
list will make Melalfb) = =. Repeating previous step, we 8¢t pe
Increment in Cof right child
path Here, edge <1, 2> maximizes Gof right child, so tot ys
So we can select any of the edge. Thus<4,the2.final2, 5 select edge <I, 2>-
includes the edges <3, I> <3, 3> <1, 4>,
Set MUILI =?
8g

path has cost


that forms the path 1-4 -2-5-3-1. ‘This
olols |g

|e

of 28. Set M( J[2]=


8138/8

Ex. 6.2.3
Set M[2](1]
=° 4) Ex: Graph coloring
Reduce the matrix if
O/8

N-Queen Problem
Find tha solution of the following travelling salesperson required x
problem using Dynamic approach and Branch and Bound
approach, Tine
M; = Matrix is already reduced, Scheduling
L=0 a T matrix Ms= Cost of node 5,
0 10 15 20 Q 2) ,
=C()+ L+Ma[2][4] = 35+3+0=38 6.3 Exam Pack
5 0 9 10 Cost of matrix M2= Cost of node 2, (University and Review
Questions)
613 0 12 Oo CQ)=CQ)+ L+MI2] =35 +0+0=
[1] 35
jus poin ., dynamic state space tree would look like,
By excluding <1, 2> we get. © ‘Syllabus Topic: General Method
Soln.
88 9 0
@) (3)
ee eee) © |=|3 /0
@. Comment on model of computation : Backttacking.
(Ans. : Refer section 6.1.1(A) (5 Marks) (May 2014)
0 [=]3 pa 2 Qa Write a short note on state space trae.
ol7f{o{1 O|7 toll
dlolole 0 [0 [0 |= (Ans. : Refer section 6.1.1(B)) (S Marks)
a. What is a state space tree and with respect to state

Set M[3, I] = = and reduce M; = Reduce matrix with Oninclusion of <2, 3>, we get matrix My. In My, we space treo explain the following terms:
ined, [> and <4, 3> edges with O value. So selecting any
matrix if required dite edge <a, b> from this list will make () solution state (i) state space
Cost of matrix M;= Cost of node 3,
Mplbl =~». Repeating previous step, we get Gi) answer states (iv) static trees
C3) = C(I) + L+MUI[2)
jee Ss | ty) dyranictees (@)_twenade
=354+44+0=39
Original cost matrix Mi = Row reduced matrix | M = Reduced matrix Increment in € of right child jo | (vi) bounding function (vi) state space tree
the matrix is 35. This implies any At this point, dynamic state space tree would look like,
Reduction cost of Sowe can select any of the edge. Thus the final path (ix) E-node.
route of the graph costs at least 35. Thus root of the uee has
35 ivades the edges <1, 2>, <2, 4>, <4, 3>, <3, L>, that forms section 6.1.1(B)K19 Marks)
cost 35. We shall select the next edge such that exclusion of :
(Ans. Refer
Include <1,2>, exclude <1,2> tepah
| - 2-4 - 3 — |. This path has cost
of 35. backing
that leads to maximum =, In other words, select the edge Write aé formulation of the recursive
Qa. (5 Marks)
Which gives maximum probability of minimum cost of path 36 39 127 Comparison Divide and Conquer, algorithm. (Ans. : Refer section 6.1.2(A)) the sum
on inclusion of that edge. ng algorthm for
Dynamic Programming and a Write recursive backtracki
We can achieve this by considering one of the edge ‘of subset problem.
with reduced cost zero in reduced matrix M. In matrix M,
On inclusion of <1, 2>, we get matrix M2. In M2, we Backtracking 6.1.2(A)) (6 Marks)
have <2, 4>, <3, I>, <4, I> and <4, 3> edges with 0 value. (Ans. : Refer section
> (Mey 17) ral iterative
edge <I, 2>, <2, 1>, <2, 4>, <3, I>, <4, L>, <4, 2> and <4,
So selecting any of the edge <a, b> from this list will make king? Write @ gene
What is backtrac
3> has O reduction cost. If we select any of the edge <a, b> king.
M,[a]fb) =~, Repeating previous step, we get JP Compare divide and conquer, dynamic programing gorithm for backtrac ks)
from this list, then resultant cost matrix M will have entry e ion 6.1.2(B) 7Mar
Edge <2,4> Use ged for algorithm Ons +: Refer sect
backtrackin ig approacn‘ es e gyflabus Top: p-oueen P nen
on position Mfa][b]. <3, I> <4, I> | <4.3> |
|“d1 [ee ese esa ||
If we include edge <I, 2>, set M[1][2] = *, and reduce Increment 4 1 0 3 Nn.
a nn
M. This is done by subtracting 4 from row 1. So cost of right inéof right Backtracking g. — Writenote on: sue
child will increase by 4. child (Ans: Rater s22820 °°" ay 2044, Dee. 2014)
Here, edge <2, 4> maximizes Cof right child, so let Us c.2015)
If we include edge <2, 1>, set M[2][1] = =, and reduce
roblam.
M. Updated matrix is already reduced. select edge <2, 4>. rsa ..4)(10 Matts) (De
If we include edge <2, 4>, set M[2][4] = =, and reduce ei 8. :
M. This is done by subtracting 1 from column 4. So cost of
right child will increase by 1.

Scanned by CamScanner
~
Backtracking & Branch and By
eae Analysis of Algorithms (MU - Sam 4 - Comp) _ £29 und
a. What is LC search?
Q. Write a short note on 8-Queen problem.
(Ans. : Refer section 6.2.4(A)) (2 Marks)
(Ans. : Refer section 6. 1.4) (10 Marks) Lc Search yj
(May 2016, Dec. 2016) Q. Describe the control abstraction for
respect to Branch and Bound. th
@ Syllabus Topic : Sum of Subsets (Ans. : Refer section 6.2.4(B)) (6 Marks)
How does it help in finding a solution for branch ang
Q, Explain sum of subset problem.
a.
(Ans. : Refer section 6.1.5) (5 Marks) (Dec. 2015)
bound algorithm?
Q. Writa a recursive backtracking algorithm for sum of
(Ans. : Refer section 6.2.4(B)) (6 Marks)
subsel problem.
(Ans. : Refer section 6.1.5) (5 Marks) Q. Explain in detail Control abstraction for Lo search
is
(Ans. : Refer section 6.2.4(B)) (6 Marks)
Q. White an algorithm of sum of subsets. ch i
(Ans. : Refer section 6.1.5) (5 Marks) Q. Describe bounding with respect to Bran
Bound.
(May 2014, May 2015, May 2016) (Ans. : Refer section 6.2.4(C)) (7 Marks)
Ex. 6.1.4 (5 Marks) (May 2014)
Q. Explain the FIFO branch and bound with respect to
Ex. 6.1.7 (10 Marks) (May 2013)
branch and bound technique with a Suitable
Ex. 6.1.8 (10 Marks) (May 2015) example. (Ans. : Refer section 6.2.4(D))\(10 Marks)
Ex. 6.1.9 5 Marks) (Dec. 2015, May 2016) Q. Discuss the control abstraction for FIFO branch ang
@ Syllabus Topic : Graph Coloring bound. (Ans. : Refer section 6.2.4(D)) (7 Marks)
Qa. Explain Graph coloring problem using backtracking. Qa. Explain 15-puzzle problem using branch and bound.
White algorithm for same. (Ans. : Refer section 6.2.5) (10 Marks) (May 201 3)
(Ans. : Refer section 6.1.6)(10 Marks) (May 2013) Q. Write note on : 15-puzzle problem. (10 Marks)
Q. State Graph coloring algorithm. Explain strategy (May 2014, Dec. 2014, May 2015, May 2016)
used for solving it along with example.
Qa. Explain how branch and bound strategy can be
| (Ans. : Refer section 6. 1.6) (10 Marks) (Dec. 2013) used in 15 puzzle problem.
| Q, Write a short note on Graph coloring. (Ans. : Refer section 6.2.5) (10 Marks) (May 2017)
(Ans. : Refer section 6.1.6) (10 Marks) (May 2016) @ Syllabus Topic : Travelling Salesman Problem
— Se

Q. What are planar graphs?


Qa. Explain travelling salesperson problem using branch
(Ans. : Refer section 6.1.6(1)) (2 Marks) and bound method.
Q. What is bipartite graph? How many colors are (Ans. : Refer section 6.2.6) (10 Marks) (May 2013)
required to color bipartite graph?
Q, Write note on : Travelling sales person problem.
(Ans. : Refer section 6.1.6(2)) (5 Marks)
(Ans. : Refer section 6.2.6)(10 Marks) {May 2015)
@ Syllabus Topic : 15 Puzzle Problem
Q. What is travelling salesman problem?
Q. Comment on model of computation ; Branch and (Ans. : Refer section 6.2.6) (6 Marks)
Bound. (Ans. : Refer section 6.2.1) (5 Marks)
Q. Describe laast cost search (LC Search) with respect
(May 2014)
to Branch and Bound.
Q. Write a short note on Branch and bound strategy. (Ans. : Refer section 6.2.6(B)) (7 Marks)
(Ans. : Refer section 6.2.1) (10 Marks) (Dec. 2015)
Q. Explain the Least cost search with respect to branch
Q. Describe the method with respect to Branch and
and bound technique with suitable example.
Bound. (Ans. : Refer section 6.2.1) (7 Marks) (Ans. : Refer section 6.2.6(B)) (7 Marks)
Q. Explain the branch and bound algorithmic strategy a. Compare divide and conquer, dynamic
for solving the problem.
programming and backtracking approaches used for
(Ans. : Refer section 6.2.1) (7 Marks)
algorithm design.
Q. Differentiate Backtracking and Branch and Bound (Ans. : Refer section 6.2.7) (5 Marks) (May 2017)
Method. Illustrate with an example of 4-Queen's
Problem. (Ans. : Refer section 6.2.2) (5 Marks)
Q. What is the difference between backtracking
approach and branch and bound approach.
(Ans. : Refer section 6.2.2)(5 Marks)
000

Scanned by CamScanner
Backtracking & Branch angBoy
et, Analysis of Algorithms (MU - Sam 4 - Comp)
=—=—
ind
What is LC search?
Qa. Write a short note on 8-Queen problem.
(Ans. : Rater section 6.2.4(A)) (2 Marks)
(Ans, : Refer saction 6.1.4) (10 Marks) search wi
{May 2016, Dec. 2016) Describe the control abstraction for LC
Branch and Bound.
@ Syllabus Topic : Sum of Subsets tan por section 6.2.4(B)) (6 Marks)
a Explain sum of subset problem. forbranch wisi
How does it help in finding a solution
(Ans. : Refer section 6.1.5) (5 Marks) (Dec. 2015)
bound algorithm’?
a. Write @ recursive backtracking algorithm for sum of (6 Marks)
(Ans. : Refer section 6.2.4(B))
subset problem.
Explain in detail Control abstraction for Lc Search
(Ans. : Refer section 6.1.5) (5 Marks) .
(Ans, : Refer section 6.2.4(B)) (G6 Marks)
Q. White an algorithm of sum of subsets.
Describe bounding with respect to Branch and
{Ans. : Refer section 6.1.5) (5 Marks)
Bound.
(May 2014, May 2015, May 2016) (Ans. : Rafer section 6.2.4(C)) (7 Marks)
Ex. 6.1.4 (5 Marks) (May 2014)
Explain the FIFO branch and bound with Fespect Ip
Ex. 6.1.7 (10 Marks) (May 2013) branch and bound technique with a Suitable
Ex. 6.1.8 (10 Marks) (May 2015) example. (Ans. : Refer section 6.2.4(D)(10 Marks)
Ex. 6.1.9 5 Marks) (Dec. 2015, May 2016) Discuss the control abstraction for FIFO. branch and
@ Syllabus Topic : Graph Coloring bound, (Ans. : Refer section 6.2.4(D)) (7 Marks)
a, Explain Graph coloring problem using backtracking. Explain 15-puzzle problem using branch and boung,
Write algorithm for same. (Ans. : Refer section 6.2.5) (10 Marks) (May 2013) 1 Introduction
Syllabus Topic: The Naiv
(Ans. : Refer section 6, 1.610 Marks) (May 2013) Write note on: 15-puzzle problem. (10 Marks) suing matching operation
e String Matching
is
core Part in many text Algorithms
Qa. State Graph coloring algorithm. Explain strategy
(May 2014, Dec. 2014, May 2015, May 2016) a proces ing applications.
Objective of string matching
used for solving it along with example. algorithm is to find Pattern
{Ans. : Refer section 6.1.) (10 Marks) (Dec. 2013)
Explain how branch and bound strategy can be P from given text T. 7.2 The Naive String Matching
used in 15 puzzle problem. Typically IPI << ITI. In design of compi
a Write a short note on Graph coloring. (Ans. : Refer section 6.2.5) (10 Marks) (May 2017) editors, string matching operation
lers and text Algorithms
(Ans. : Reter section 6.1.6) (10 Marks) (May 2016) is crucial. Se ee
@& Syllabus Topic : Travelling Salesman Problem Jocating P in T efficiently is very important.
Qa. What are planar graphs?
a Explain travelling salesperson problem using branch
D (May 14, Dec. 14, May 15, May 16)
(Ans. : Refer section 6.1.6(1}) (2 Marks) String matching problem is defined as follow: “Given
and bound method. some text string T[1....n] of size n, find all occurrences
Explain naive sting matching algorthm wih example
Q. What is bipartite graph? How many colors are
(Ans. : Refer section 6.2.6) (10 Marks) (May 2013) DURE Eee eed
of pattern P[1...m] of size m in T.”
required to color bipartite graph?
Write note on : Travelling sales person problem. ISNeenes
(Ans. : Reter section 6.1.6{2)) (5 Marks) We say that P occurs in text T with number of shifis s,
(Ans. : Refer section 6.2.6)(10 Marks) (May 2015) ~ This is simple and inefficient brute force approach. It
* Syllabus Topic : 15 Puzzle Problem if0Ss<n-mandT[ (s + 1)... (s +m)]=P{l_m].
What is travelling salesman problem? compares first character of pattem with searchable text
2. Comment cn model of computation ; Branch and
(Ans. : Refer section 6.2.6) (6 Marks) Consider the following example : If match
is found, pointers in both strings are advanced.
Bound. (Ans. : Refer section 6.2.1) (5 Marks)
(May 2014)
Describe least cost search (LC Search) with respect Tf match is not found, pointer of text is incremented and
1o Branch and Bound. 12203 4 5 6 7 9 1011 12 pointer of patter is reset. This process is repeated till
[sfolw] Tataqey [+] jefe]?|
1. Waite a short note on Branch and bound strategy.
(Ans. : Refer section 6.2.1) (10 Marks) (Dec. 2015) (Ans. : Refer section 6.2.6(B)) (7 Marks) the end of text.
Explain the Least cosi search with respect to branch ~ Naive approach does not require any pre-processing.
. Describe the method with respect to Branch and
and bound technique with suitable example.
Bound. (Ans. : Refer section 6.2.1) (7 Marks) Given text T and pattem P, it directly starts comparing
(Ans. : Refer section 6.2.6(B)) (7 Marks)
. Explain the branch and bound algorithmic strategy both strings character
by character. .
for solving the problem. Compare divide and conquer, dynamic
- ‘After each comparison, it shifts pattern string one _
(Ans, : Refer section 6.2.1) (7 Marks) Programming and backtracking approaches used for
Differentiate Backtracking and Branch and Bound
algorithm design.
Fig. 7.1.1 : Pattern matching in text T position
wo the right the working of naive
Method, Illustrate with an example of 4-Queen’s (Ans. : Refer section 6.2.7) (5 Marks) (May 2017) Following example illustrates
In this example, pattem P = ARE is found in text T string matching algorithm. Here,
Problem. (Ans. : Refer section 6.2.2) (5 Marks) afler four shifts, = PLANINGANDANALYASIS andP = AND
What is the difference between backtracking
Classical application of string matching algorithm is to Ts are indices of text and pattern
approach and branch and baund approach.
nd particular protein pattern in DNA sequence. Here, and P;
n(Ans. : Refer section 6.2.25 Marks) ively.
Sting may be encoded using set of chara aracter alphabets
TU 4PCA Ij soadvane
xt pointer, Le. t+.
es we
god {ab,...,2), binary alphabets {0, 1), deci mal alphabets Sep 1 :THI 6
oper lee
cod
OT1,2... 9}, DNA alphabets {A, C,G, T}. Bo
string direcil y affects the efficiency ofsear ag
ext sections, we will discuss and analyze fe¥S

Matching algorithms.

Scanned by CamScanner
String Matching Soe Sting Mate
pointers, ie. tree par ER —
paar bothrerer eer
eo aves
[PPanaiysis of Algorithms (MU - Sem 4 - Comp) The Rabin Karp Algorithm
Ste} ? 9: TIS) =P
Step 2 : T[2] # P[1], so advance text pointers ic. ++ 123 [aL] DP (May 14, May 15, Dec. 15, May 46
LEEEER
Hash(ababaa)
ET
won 2s
n different string matching algorithms.
ET SeN eer
) |
T 2 3 shart note on Rabinkarp algori a
123 EETthm, Hash(Patiom)
Step 10: T[10] = P[3}, so advance both pointers, i.e. 44, Fig. 73.10); Ral
KarperAppreach

sinth ee pa
Given pattem PU... a we
bincan

Rabin Key ait npeags| se


is casier and, chea; per than

TGs
ptt jumbers
Step 3: T[3] = PII), so advance both pointers i.e. t+, Py+t paring
4 5 6 7 8 9 OM 8 HS 8 7 ag
39
1267 eo i ws
45 Similarly, weg tt + d{P{2] + PLY) . .)
rings i numbers. ~
g
to pater substring yafleagth tumeric value of first
sents correspondin
_values Remaining alt, 1,2, Se TL in Oem) time,
T 2 3 - sano1.) i iprepre
© “of length m. And t, represents
nts_values of constant time sy TM, can be derived in
substrings T[{(s + 1) . . (8 + m)] for s=0, 1,2,
om ength substi — GivenNese
This process continues till the end of text string. aan
naive string matching approach is
Algorithm for
We can com O(n— (4,3, 1, 5, 6&7, 5, 9, 3] and
computed iin put e pm in+ 1)O(m)
described below= time. time and all t, can be | ~ so
i e{1 a
5, 6). i
Here length
to P is : 3, s
of te so m= 3, Consider
ihm NAIVE.STRING_MATCHING(I, P) at, for given pattern P, its value p=156,and
Rabin Karp algorithm is based on hashing technique. It
i: ‘Tis the text string of length u=431,
/ is the pattern of length m first computes the hash value.
eof panand t.
\
for i¢<-Olon—mdo ff hash_values are same, i.e if hash(p)= hash(t,),
we = XG! TID + THA = 101431 - 400) +5 {
ifP[L... m] == TH+1...i+m ]
then of inverse hash similar to naive
=315
print “Match Found” check the equality
‘Values of p and t, may be too large to process. We can 1
not same, no need to
Step 5 : T[5] # P[3], so advance text pointer and reset method. If hash values are reduce these values by taking it’s modulo with suitable
pattern pointer, i.e. t+, pj= 1 number q, typically, q is prime number.
hash “match,
‘OnQn_hash_m actual characters of both strings are Mod function has some nice mathematical property.
© Complexity analysis © {(amod k) +(b mod k)} mod k = (a +b) mod k
compared using brute ‘force. approach. If pattem is
There are two cases of consideration is called © (amod
k) mod k =a modk
found then it is called hit, Otherwise it Computing hash value of every subsequence of m
1 Pattern found “ypu fous hit. character of text may tur out to be time consuming.
‘Worst case occurs when pattern is at last position and For example, let us consider the hash value of string However, bit of mathematics makes it easier.
of substring
there are spurious hits all the way. T = ABCDE is 38 and hash of string P = ABCDX is Suppose t, represents decimal value
hash value
Step 6 : T(6] # P[1], so advance text pointer, i.c. ++ Example, strings cannot TEG+1)..(s+m)] Tf hash of t, is known,
71. Clearly, hash values are not same, so for tye) as follow
P = AAAB, To move pattern can directly be derived
1294 5 46 7 8 8 OH ow MS 6 7 T = AAAAAAAAAAB, be same. Brute force approach does five compansons
one position right, m comparisons are made. Hash for tay = (* = Tis + 1) + Tfs+m+ 1)
where as Rabin Karp dose only one compatison. mod q, Where h= a! modq
Searchable text in T has length (n - m). Hence, in worst
However, same hash value does not ensure the
He string
case algorithm runs in O(m*(n - m)) time. ‘Two facts
1 have same _hasb |
match, ‘Two different strings can
2 Pattern not found
values, That is why we need to compare themsm charactet 1,=p mod q, does not mean l= P
ster 7:T(7] # PL], so advance text pointer
i.e. t+ In best case, scarchable text does not contain any of the
bycharacter on hash hit. 1, p modq, means iP
o fo MW 12 13 4 15 16 17 18 prefix of pattern. Only one comparison requires moving Ifp= 45365, t,= 64371 and q= ll,
the difference between Brute Force
TE D PDT
DD ET Te pattern one position right, ~ Fig. 7.3.1 shows
and Rabin Karp approach. 45365 mod 13=8
Example,
T = ABABCDBCAC, P = XYXZ. Algorithm does 64371 mod B= a o
=twoedifferent
cn it it is cleea
12 3 O(n - m) comparisons. fon nt
Step 8 : T[8] = PLL], so advance bothte poi, Le. rH ppt In worst case, first (m — 1) characters of pattern and wal ssible inv
ae to overcome
1415 16 17 18 text are matched and only last character does not
wna te validated
e
Tposi
all t

and it
er $i reduce with larger value of q,

P= AAAAB. Alport es O(m*(n—m)) time. Fig, 7.3.1(a) : Brute DN


force eet

Scanned by CamScanner
ysis of Alge! rithms (MU - Sem 4+ Com 1p) )
‘Ane
Sting —Matching set of states in Finite automata
mac inmen PT We me

one Mok “
Algorithms (MU - Sem 4 -Come) 7: 7) Set of input symbols
ee “Analysisés of
Hash Vict),givenSo there may be 9 spurious hit OF actuay

bow
Jnitial state
~ inbas tex'T, one hit 8 actual MAICh and one set of final states
[Algo Aer RABIN_KARP(T,
<_ rithm P)
ep
is spurious hit. ny sig te o ‘Transition function define

now

ia in length
fPamencioen
have two possibilities:
o Bilt: Patem and
text are same.
is same but patter For
xample
das6:QXxL3Q

(0.1. 2345h
For the strF
1S aera,
number ® OF1 Feng
hy ™m, FABA vl ya te have m4
Spurious bit : Hash value
is not same. Will be initi.y »
and corre sponding text = 0F=(23)
m= lengihof pattem =5. 0
he pen oats ay eng wat "Stan state and state mwill
Here, EASk chaser be the
<1 tomdo Fe gery, = 23590. Next vets is derived as, = {a,b}, And transition function:
m + 1]) § Velbe in sae, ST OF PAE match with the text, FA
10(t,) — 10" * T[s+I) + Tis+ b
ae modq
10De & ((d * 10) + TI)
tet
(1023590)- 10° *2+2 Tit hy..
7235900 - 200000 + 2 3 F j baat
- 35902 eX chy
] + TIs +m +21)
tee = 10ftqi)— 10 - * 10°*Tls#2343
4
5
ke leh
bn
eat nx Meiches wih py Prot
Ae Matchedand FA seer? 2 nelils frst
Goes in (K+ 1)" state,
“pecwuee, (10*35902)
359020- 300000 + 3
5 tf Ped = key
tend: = ‘T[stl
Ts mj the then
s+ + my = 3m TEN! charaeter t,
* “Match found laat shift”, s
print
the next ty; using
5 5S 5 FA ters in ong often * match with py, , then
dn same way, we can compute Graphically we can represent this finite automaton ee shift :
cxhangey TiS Pater right ki there is a match or p iis
nd = incremental approach. rather than gown in Fig. 7.4.1.
as
ifsi < (n—m) then Rabin Karp algorithm matches hash value, hha...
ts+1 € (d*(ts—Ths+ 1) *h) + Ts +41 directly comparing actual string value.
[f hash value of
T are
|. Finite automata arc widely used in Compilers and text
processors for string matching.
Given the sting [bea bea tan Sie
pattern P and hash value of subsequ ence in string S over Pry. “ek
compured using brute alphabet set Z. Finite automata, Starts with input Tm Geet Bs
same, actual value of strings arc ich .
force approach. Like t,.1, we can also derive hash value state Qy continue fund enter in site ky else
= Complexity analysis incrementally as shown below. Reads the input string character by character and
most
— Rabin karp algorithm is randomized algorithm. in In O(n). changes the state according to transition function
of the cases, it runs in linear time, Calculation for hash of
14152
Tht bay , He
However, worst case of rabin karp algorithm is as bad Ift,is known, hash value can directly be derived for ts, ] P:
as naive algorithm, i.e. O(mn), but it’s rare. as follow. Pet
— It can happen only when prime number used for Hash forte = (d*(t,— Ths+1]h) +T[s+m-+ 1]) modg Uf match found, enter in state k —
hashing is very small. + 2 mod 13
10#(31415 — (310° mod 13))
10(319)415— +2 mod 13 1, else continue
Ex. 7.3.1
= 314062 mod 13
Explain spurious hits in Rabin-Karp string matching = 38 Fig. 74d T:t, nate ty) Ware hepa bake
algorithm with example. Working modulo q = 13, how
many spurious hits does the Rabin-Karp_ matcher Syllabus Topic : String Matching with Finite ltaccepts the string if a finite automaton ends up in one
encounter in the text T = 2359023141526739921 when Automata of the final / accepting st ates. If match found, enter in
looking for the pattem P = 31415 ? Itrejects the string if a finite automaton does not end state 1, else state0
Soin. : 7.4 String Matching with Finite Automata upin final state, Tf FA reaches to stale m, pattem is found and
computation stops.
Given pattern P = 31415, and prime numberq = 13 a La us trace some string for above given finite
Pmodq = 31415 mod 13=7 > (May 17) em Consider the string S = abababaaabab. Following Search time for this approach is linear i.e. O(n). Each
eshows the transitions on every input symbol, character in text is examined exactly once,
Let us find hash value for given text : Q. Write and axplain string matching with finite automata
T[1...5] = 23590 mod 13=8 Mes — Algorithm for pattern matching, using finite automata is
with an example. VI Ena
T[2...6] = 35902 mod 13=9
bla}]bjal/blal{alajblalb shown below :
- Idea of this approach is to build finite ‘automata to scan
Ti(m—4) ... m} = 39921 mod 13
= 11 text T for finding all occurrences of pattern P. 3}11341 3/1 f2fatalels| © Algorithm
- This approach examines each character of text exactly After Scanning entire st ring, finite automata is in final ‘Algorithm FINTE_AUTOMATACT,F)
nets
once to find the pattem. Thus it takes linear time for © string is accey pted by the automata, Consider the WTR
matching bul preprocessing time may be large. #S=babababb, Pie pattern of lengthmi
- Finite automata is defined by tuple
M = (Q,2,a9,F,8} Input | |b | a ‘Paitia! state isO
stale0
Fig. P. 7.3.1 State | 0 | 3

Scanned by CamScanner
oh Algis ori
asi=m Sems4 - Comp)
(MUthm
also known as KMqeP
5s algo! oe P (Kn
String Matching Mons
first linear a algorith
1H algorithm. This Itis theutilizes the ¢ m
the string abb are Pr rink matching.
Teves made by the FA to accept fot roach i some different way. This
span Taive
h Keeps
fori = 1 ton do showninin Fig. Fie P.Oa
7-4-1)+ » 20 eof matcied part of pattern,
state < d{state, t) Retum the new state aller wn jdea of the algorithm is to avoid compre:
reading character ty é eigon function 8 and reducing tsele SS. shiftsof >a
weaned in naive approach. Fle) =a do
(qq-2) = 91
if state == m then
1 kenny
end
print “Match found on position”, i- m + ning the information of alre; dy processeq
BY maintai h reduce the time 0 G
end
+ st "thisyorstapproac
* ng case, KMP algorithm ” n+ Tha ben = Plies
ifPhk+1) =ep

Bldg) = ay is ters of input pattern and text exactly rues a end


© Complexity analysis auxiliary fame
automata takes O(m'tz)) time
in sition steps jnprovernent is achieved by using g it has on n
Construction of finite Fig. P- 74L(a)* Tran Naive approach was not storing
anythin
Thal
— pattem and [1 is
worst case, where m is the length of finite automata is given
as, the pattem fend
number of input symbols. ‘Transition table for above arched, and that’s why it was shifting
characte r of text exactly t one. Computation of auxiliary func
This approach ex:mines cach Table P. 7.4.1 “ 0
linear time for i
once to find the pattem. So it takes
matching, But pre-processing time may be large. ction 7 is very useful, it comput
es
part for patie fs pf pattern by finding pattem nae at
Constructing automata is the difficult
matching. iuself.
considering ume HP is pattern oflength m
{ft runs fairly in O(n) time without
spend to build transitionfunction. giiding rule
Finite automata for few patterns are shown in Fig. 7.4.2 Naive approach slides the pattern by one right side
4 1 XMP approach slides the string multiple steps. ang ® PREFIX FUNCTION)
shown in
Simulation of given data on this FA is
reduces comparisons. Let us consider pattem

Table P. 7.4.1(a).
P= P,P.Pa-PaaPun iq +-0
Table P. 7.4.1(a) For any string P, sub string P* of first i characters is fori
1 tondo
called prefix of P, where i = 1 to m. Null string also
FA accepting string a" belongs prefix of P. String P’ is proper prefix of P if while q > O and Pq + 1] # TIi} do
and only if P’ # P.
Similarly, for any string P, sub string P* of last i
[Match found characters is called suffix of P, where i= 1 to m. Null
string also belongs suffix of P. String P* is proper
suffix ofP if and only if P’ +P.
FA accepting string a” Consider the pattern P = abchdahc ;Prefix of P are: {g,
a, ab, abe, abed, abcda, abcdab, abcdabe}, where
abedabc is not proper prefix of P.
Suffix of P are : {6, c, be, abc, dabe, cdabe, bedabe,
print “Pattern found with shift”, i—m
Match found abedabe}, where abedabc is not proper suffix of P.
b Suppose the pattern P[1....q] is matched with the text at end
FA accepting string (ab)" [i — q + 1)... i}. First mismatch occurs at position a aal
Fig. 7.4.2 rerett Algorithm Pq + 1] and Tfi + 1]. Slide the pattern right by x,
Syllabus Topic : The Knuth-Morris-Pra © Complexity analysis
where x is the length of longest proper prefix of where as actual
Ex. 7.4.1
P{l...q and also longest suffix of P[1...q] — Function x is computed in O(rm) time
Construct and show simulation of finite automata for 7.5 The Knuth-Morris-Pratt Algorithm string matching routine scans n
characters. So worst
matching the pattern P = abb for taxt T = ababbaababba Norking principle case running time of algorithm
is given by O(m+n).
Finite automata that can accept the string abb is > (May 13, Dec. 14, Dec. 15, May 17)
Soin. :
shown in Fig. P. 7.4.1. > TEP[I...q]) matches with T[G - q+ 1) ...i}, thee are Ex. 7.5.1
Explain’ and write Knuth-Morris Pratt algorithm.
two possibilities: for pattem ababaca.
Explain with an example.
I Plg + 1] = ThIf q+ = 1],m, matche d string length is Compute the pratix functicn
increased by one. complete match is found
To implement the Knuth-Morris-Pratt, string matching
stn and algorithm terminates. .
Write a short note on Knuth-Morris-Pratt’s Patiom P(g + i) # TEi + 1, then the slide pater P to High
Fig. P. 7.4.1 Umea by longest common suffix and prefix of PLL. a.
Matching. thm PREFIX, FUNCTION() eat
the pattern of length m

Scanned by CamScanner
ieof Algorithms (MU - Sem 4 - Comp)

String Matching cpr PEe Piri


x «felelelele|a|
78
- Sem 4 - Comp) 4 (Ans, :: eet secon
[FT anatysis of Algorithms (Mu k= mk) > k=m{t]=0 73) 10 ar
Iteration 1:4 =0 Ask0, =
partial Jength - x[partial length}
loop
ic is not greater than 0, so skip while o Syllabus:
2
loop breaks now
While
se 7 3-3] =3-1=2 (ay 2015, Dec. 2015)
Pik+1] = P{l]=aand Pla) =PO2}=0 Pk+iy = PUll=a Rito OP: Sting Matching with Fnuie
skip two characters ahead.

epee EEEEEEREE
+ fz] =0
and P[q] = PI61=¢
a
Pk+ll # Plab Wite and emplein
So,nfq) = k > 116] = 0 an
Automata with Steg malching with finite
i jil2tsa|4[s}el7 (Ans. »: + Reter
Refer: section 7,4
at io
q=3 n0
,k= abu Tope: The (WOMarks) (aay 2017)
1) (10 Marks)
Iter2: loop pa|a|bfalbjalela than the remaining text, so
Ts
ik is not greater than 0, so skip while _ Now pattern is longer a
= Plll=a [o}1}2 3|0 does not found.
Pet) mil | Oo patch
and fq] =PI=4 Iterat ion
6 :q=7, k=0 is exam Pack (University Questions) (Ans. : Refer section
P+] = Pla loop
75) (10 Marks)
ok=t
k is not greater than 0, so skip while
, (May 2013, Dec. 2015)
Pik+1] = P{ll=a and Piq]=P(7]=a
sok=k+1
Naive String Matching
¢ Syllabus Topic : The To implement the Kru: Montie-Pratt,
Pk+1] = Pig), sok=k+l=1 Algorithms sting
nig) =_k > 7M=1 string matching algorithm with matching algorithm.
Explain naive
example. (Ans. - Refer section 7.2)
(10 Marks) (Ans. : Reler section
7.5) (10 Marks) (Dec. 2014)
(May 2014, Dec. 2014, May 2015, May 2016) a Write a short note on Knuth-Momis-Pratt's Patter
‘Matching. (An
: Refers.
section 7.5)
(7 Marks)
Iteration3:q=4,k=1 ic : The Ri
abin Karp Algorithm
to while loop @ Syllabus Top
kis greater than O, so enter in (May 2017)
= PI4] =b, matching algorithms.
Pik+1] = P(2]=b and P[q] Check if pattem P = abababca exits in text o Explain different string
> 0 but Pik + 1] = Pla break while loop (10 Marks)
| Pik+1] = Pig sok=k+l—k=2
T = bachababaabebab.
Soln.:
(Ans. ¢ Refer secti on 7.3)
(May 2014, May 2018)
niq) =_k > nf4]=2 be,
The 7 function for given pattern would
i til2|3i4{5)6|718
Pailal|ol{alblalble
p

mi lofo[1}2{3]4iol!
Iteration4: q=5,k=2 second position.
kis greater than 0, so enter in to while
loop The first partial match is found on
= 0, so we won't
Piql=PU5)=4 Length of this partial match is 1. [1]
Pik+1] = Pi3l=a and e matching by
break while loop be able to skip ahead. We will continu
k > O but Pik + 11=Plq] moving pattern right by one.
Pik+1) = Pfah

sok=k+1 +k=3
n[q] =_k > m5] =3
of
i j1f2[3]4|5]6]7 — Next partial match is found on position 5. Length
not
this match is 5. Next character of P and T does
Phyja}bjajbljalc}a match. . n[5] =3, means we get skip ahead.

PrEPEPE PETE!
nfiJ | 0} 0} 1 12/3
Iteration
5: q=6,k=3
loop
k is greater than 0, so enter in to while
Pik+1) = Pl4]=band PIql=PI6]=<,
k > a Skip = partial length - x[partial length]
and Pfk + 1] # Pq] “Enter in while loop = §5-n[5)=5-3=2
k = mk] + k=7(3)=1 — So we can skip two characters ahead.
Now, P(k+1] = P[2]=b — Next partial match is found on position 7. Length of
and P{q] = © this match is 3. Next character of P and T does not
0 and Pik + 1) #Plal
match. n[3] =l, means we get skip ahead. Cross
Stillk >
indicates skipped characters.
W Enter in while loop again

Scanned by CamScanner
ts ofAIDEN SE 4 = Comp)
ent. Satisfiability problem fings¢
;“ven input an
expression. ig yyy hte
otha
ment. >
jbility * Let-P) and P, are two 2 Sort
cxsts sonse deterministic algorithm a fas and Merge
sor
ol CHAPTER 3.
pcan pe solved in polynomial time Using a Such)
Non Deterministic ¢ algorithm can solve the problem P) then Withe
We can
Polynomial Algorithms
iat P2 is seducible to Py.
ay.

Gyllabus Topic : Polynomial Time

polynomial Time
a
ess and reducibility NP Completeness Proofs Swat do you mean by P problems? Give an example .
Polynomial time, Polynomial time verification NP Completen a ton
Vertex Cover Problems Clique Problems. time. NP.
short note on P problems, “ a ths Solution in
polynomial c 4 tr
"It-can be verified
in polynomial ine
For problem inst ”
ance <G, v,
Definition are set of problems that can be solvedin Whether there exis ¥, 1 we‘acan check
‘@ Basic Definitions Pproblems ts a yah b from venex u to v of
jynomial time by deterministic algorithms, Yength at mos. And
Q. Explain the following: ~The problem is said to be decision problem if they if so, p 1s called certificate for
produce output “Yes” or “No” for given input. An DTIME complexity the given instance,
(i) Computational complexity |p is also known as PTIME or
(ii) Decision problems algorithm which solves the decision problem is called class. Finding a path from u to v can be done in linear time
to verify and take and verifying the certificate also takes the same time.
They are simple to solve, casy
(iii) Deterministic and non-deterministic algorithms decision algorithm.
An optimization problem aims for the best
solution For this particular problem, verifying certificate takes
for solving any
computationally acceptable time
\

(iv) Complexity classes


(v) Intractability. (5 Marks) from the set of all feasible solutions. An algorithm a long as solving the problem.
is called instance of the problem. Such problems are also known
We can classify the problem in one of the following which solves optimization problem However, in practice there exist many problems for
optimization algorithms. as “tractable”. whom polynomial time solution is not known but still
categories:
Optimization algorithm secks to find best profit or In the worst case, scarching an element from the
list of given the solution, it can be verified in polynomial
1, The problem which cannot be even defined in a proper
way. least cost solution. Decision problems arc simpler size n takes n comparisons. The number of time, Consider the following problem:

2. The problem which can be defined but cannot be than optimization problem. comparisons increases linearly with respect to input Hamlttonlan cycle

q
solved, Computational complexity : Computational problems size. So linear search is P problem. ‘The hamiltonian cycle of undirected graphG = <V, E>
3. The problem which can be solved theoretically, but have infinite instances. Each instance in the set In practice, most of the problems are P problems. js the cycle containing each vertex in V.
computationally they are not feasible. The algorithm contains the solution. Typically, the solution to the it is called
Jf graph contains a Hamiltonian cycle,
insertingan
Searching an element in the array (O(n),
takes very long time to solve such problems, and the problem in computational complexity is Boolean ie. tonian.
time is practically not ucceptable. For example, *Yes’ or ‘No’. element at the end of linked list (O(a)),
sorting data Hamiltonian graph otherwise itis non-Hamil
cracking the password of 256 characters by brute force The computational problem can be function problem using selection sort(O(n’)), finding height of wee
too. Solution to such problem differs on each execution gsn)),
(O(logan)), sort data using merge sort(O(nlo
method may take years.

Definition even for the same input. So such problems are more matrix multiplication O(n*) are few of the examples of
complex than decision problems.
Problem is called intractable or i ible if it takes P problems.
time
too long time to be solved. A solution of such problems
has Definitions An algorithm with Q(2") complexity takes double
no practical application.
Complexity classes are sct of problems of related if it is tested on a problem of size (0 + 1), Such
4. Problems which can be solved theoretically and ~ like P problems, NP problems, Decision
complexity, Problems do not belong to class P. |
practically in a reasonable amount of time. For such problems, Optimization problems etc. The complexity in
Ttexcludes all the problems which cannot be solved
problems, there exists a deterministic Turing machine of any problem in given class falls within certain
range.
that can solve the problem in O (p(n)) time, where p(n) Problems which takes practically unacceptable lime ie.
Polynomial time. Knapsack problem using bre force
is polynomial in n, and 7 is a problem size. very long time to be solved are called intractable approach cannot be solved in polynomial time.Hence,
problems.
itis not Pproblem. _
Definition whose ene
time, it is If the running time of the algorithm is pounded to There exist many important problems
If the problem is solvable in polynomial
by P problems. O(p(n)), where p(n) is some polynomial in 7, where# ot found in polynomial time so far, "or its h
called tractable. Such problems are denoted
known whether it is in P or represents the size of the problem, we say that the Droved that such solution does aot exis: 59> e
5. The problem which is not
in between problem has polynomial time complexity. knapsack
not in P. Such problems falls somewhere Solouring, partition problem,
a Satisfiablity Problem is to check the comeciness of
class 3 and 4. Sxamples of such class.

Scanned by CamScanner
Nom Dasarmbistc Panama’ §j Se
ss SEES 3
mapencinia Sune SENSE can Sul DeStshock
eee
as s
sae cunts

Rang GNU asstanas,widSIN SS 2 Samiky “Sim x


eae on humi
——ee—L
Sietas Tope : NSomniktensss ang

©) Vener cover problem


@) Graph colouwing problem

7 Maris)

Fig. 8.3.2 : Reductions


used in some fondamental
‘Vertex cover of Graph G = (V, E) is set of vertices
NP-completeness proof
such that any edge (u,v) € E, incident to at least one
~ ‘vertex in the cover. In other words, vertex cover is a
— Here p represen ihe length af encoding of grb G Syllabus Topic : NP-Completeness Proofs
subset of vertices WC V such that if the edge
the given grotiem cama be solved i (ave Ethmue Vorvev.
polynamuel time. 8.4 NP-Completeness Proofs = “The size of the cover is a mumber of vertices
in V7.
minimam.
‘Vertex cover problem is 10 find out such
© Verification Component design check if given.
you mean by NP-Complete Problems? Give ‘ze cover. A decision problem is to
ven the solmion sting of Hamihonian graph, it is Consider two decision problems A and B.
Reduction
equivalent inpat an example. {2 Marks), graph has vertex cover of size k.
easy I prowe the comecmess of it We just need 10 from A to B transforms input x of Ato prove that
iona problemsP's ‘The simplest way of finding vertex cover
of graph
check i the solution contains all the vertices of V and fix) of B by applying some transformation
function What are the conto
dit
G=(V. Epis randomly select tbe edge (5) Eand
(8 Marks)
he an edee between Two consecutive NP-Complete?
B is not dete adjacent edges of u and v. Repeat the procedure
to A andble
oni.
done in Oj) time. Thus, the So given an input x to A, reduction algorithm produces ~ If B<,, A, impliesB is redl uci tml the cover is fomd. For example, consider
solution
can be verified in polynomial
time, where a is imermediate result f(x) to problem B such that f(*)toB factor.
harder than A by some polynomia Fig. 84.1.
A retums “Yes”.
the length of encoded graph G. retums “yes” only if input x to
Fig. 8.3.1 shows the scenario. Definition
QQ) oA

icaie y such that


algorithm Verifies the string x
A(x,
following two properties :
1. Cis in NP, and polynomial !
qo © Y
defined by the algorithm is, L =
2. Every problem X in NP is reducible to Cin Fig 841
time, i.e. For every X & NP,XSC
existsy € {0,1} such that A(x. y) = 1} problems a on set. Initially, S=[}
For each legal string x © L, A must verify the string Fig. 8.3.1 : Reduction ~ These two facts prove that NP-complete Let $ represents the soluti
and Produce the certificate y. And for any string x pol;
F
(suchtoB
reducible
jal time ie
if the harder problems in class NP- They
which is not in L, A must not verify the string, and no
us, wewe say A iiss polynomi:
Thus, Teferred as NPC.
certificate can prove that x € L. For example if the there exists some transformation function
graph is Hamiltonian, the proof can be checked in

Scanned by CamScanner
[Branatysis of Algorithms (MU - Se 4 -mCom)
BS.
Non Deterministic Polynomial,
Audition to this, we add following : for gan
Swe
2 <1, 2> as Clause
Ga(atb+ c) form a triangle with vertices a b
Step 1 : Select any random edge, let us select de!
incident edges
Ande,
7 on ple: ‘ b= Siv7amv
any
: a(any vm) A
shown in the Fig. 8.4.2. Remove the a2 Clique Problem v

of vertex 1 and 2. So, $= {1, 2) (2) We vil3 se w ta the transformati


on
q. Prove that Clique Decision Problem is NP-Hard, tnial reduy ction. Let us consider @ 0 G is
Polynom
der that @ has
Marka) signment, All litliterals x; iin each clause are
fo, Prove that a clique probiem isNPcconglaus ORed with exch other.
° a rat One fiteral in cach clause is assigned value
© ©

1
1.
@ Problem a Pick up one such literal from each clause, it forms
Set V' ofk vertices as we have k clauses in . There
Fig, 8.4.2 : Affer selecting edge <1, 2> Fig. 8.4.6 Clique is the complete subgraph of graph G. In exist an edge for each, v eV".
vertices in S, 80 g0 complete subgraph, there exists an edge between every
Still few edges are not adjacent ta Any vertex cover will have to include at least two pair of vertices.
on. the vertices from {a, b, c}. Join the comesnona POndi
. The size of a clique is given by a number of vertices in
Step2 :Let us now sclect edge <5, 6>. Remove adjacent vertices from triangle to edge as per clause. a
jt, Max clique is the clique of maximum size.
edges to vertex 5 and 6. — Vertex cover of such graph contains k = n 4 2m
_ Finding max clique is obviously optimization problem.
So, $= [1,2, 5,6} vertices, where n is a number of variables and m ig a Checking if graph G has a clique of size k is decision
number of clauses.
prablem.
— Lets build the graph 3-SAT Boolean function. _ The optimization problem is to find a clique of
6 = (atbtc)(atb+e)@+etd) maximum size for given graph. A decision problem is

Fig. 8.4.3 : Affer selecting edge <5, 6>


This Boolean expression generates the graph as shown to check whether a clique of size k exists for graph G,
in Fig. 8.4.7.

SAINI
— Still few edges are not adjacent to vertices in S, so go a —— - 3 ¢ g — NE satisfiability is NP-complete and it is reducible to
on. clique, so clique is also NP-complete.
Step3: Let us now select the edge <3, 7>.
2 8.5 Exam Pack
SoS = {1, 2,3, 5, 6) and all the edges are adjacent
to at (Review Questions)
least one vertex in S. So $ is the cover of graph G. Graph & Ciquactszeg | Ciqueotsz03
¢ ob ° ° & Syllabus Tople : Polynomial Time
Fig. 8.4.7 .9
: Graph
Fig, 8.4 G and its cliques @. What do you mean by P problems? Give an
example. (Ans, : Refer section
: ic
Marks)
8.1) (5(5 Marks)
For given Boolean function, Let algorithm CLIQUE(G, k) retums true if graph0-1,G Write a short note on P problems.
n= number of variables = 4 has a clique of size k. We shall start
with k=, ©.
m = number of clauses = 3 n—2, ... until CLIQUE(G, k) retums
true. (Ans. : Refer section 8.1) (5 Marks)
Fig. 8.4.4: After selecting edge <3, 6> time verification
m Is NP-complete Syllabus Topic: Polynomial
k=n+2m=10
© Theorem : Clique Decision Proble
— However, S is the cover of the graph but it may not be So vertex cover of this graph must contain 10 vertices.
minimum. Instead of selecting edge <5, 6> in step 2, if To prove that clique belong
s to NP-complete, we oe @, Wie short note on Hamitenian cycle,
One vertex of each triangle and one vertex from each -
Checking if V" @is
we would have selected edge <3, 6>, it would have V" as a certificate for graph G. time by checking the Marks)
{Ans, : Refer section 82) (5
resulted in a minimum number of vertices, edge.
clique, can be done in
polynomial
mpleteness and
Reduclblity,
Vertices, which are part of vertex cover are shown in u, VE Vv. © syllabus Tople NP-Co
@ Theorem : Vertex cover is NP-complete presence of edge for each in NP-completenass
proofs?
Fig. 8.4.8, we will show that What is Reduct ion
To show that clique is NP-complete, a.
What are types of reductions?
Proof ~
This is how 3-SAT problem is reduced to vertex cover 2-CNF-SAT Sp CLIQUE.
function
ah 33) 7 Marks)
To prove that vertex cover is NP-complete, we will problem, so vertex cover is NP-complete problem. Aw A C, be a Boolean 8 (Ans. :Refer section
Let p= Cy A CAC thm.
reduce 3-SAT problem to vertex cover problem. Let > of k clause, where each clause Ci is in 3-CNF, a ta a. Explain Potynoriat Time Algori
)
(ans. Refor section 83) (Marks
literals. We te and
be the Boolean function with k clauses. For each literal clause has exactly three
6 be saustt ean be ,
graph such that Boolean functi
on eness Proote
@ Syllabus Tople : Np-complet
‘an’ in the clause, we create an edge as shown in
size k. The STP!
Fig. 8.4.5. Edge is truth setting component, a vertex only if G has a clique of What do you mean
mplete
by NP-Co Problems’
a.
cover must include at least one of a or 4, constructed as follows:
Each vertex corresponds to
a Titer. * Give an example.
~ ng all. vertices in
@—® , ~ Connect each vertex 10 remain (Ans. : Refer
section a4)
(2 Marks)

=
Fig. 8.4.5 remaining cl: ause except for and

Scanned by CamScanner
8-7 Non Deterministic Polynomial Algorithms
Panatysis of Algorithms (MU - Sem 4 - Comp)
What are the conditions to prove that a problem P is | a” Syllabus Tople + Clique Problem
a.
NP-Complete? (Ans. : Refer section 8.4) (8 Marks) | 4 prove that Clique Decision Problem Is NP-Harg, __
(Ans. : Refer section 8.4.2) (7 Marks)
Qa. Specify one example of the NP-complete problem. .
Ray a se =
Also, justify that why it is NP-complete. a. Prove that a clique problem is NP-complete,
ae "1 ae
(Ans. : Reter section 8.4.1) (10 Marks) (Ans. : Refer section 8.4.2) (7 Marks)
he ie = 55:
a. Prove that vertex cover problem is Np complete.
S311
(Ans. : Refer section 8.4.1) (7 Marks)
goo
Output After Pats 1-31 72
Output Alter Pass 2 => 11 2277 $9.44
Output After Puss 3-5. 1) s5iaq
99 4455 77
Ou Afr
tp Pasou4->
t 11 90.44's37p
i i AO) ig jos ks Program 2
| artemp, Min; ‘
Number of Elements :~> ");
ner

Scania", Sn)s
Y ori=0s <n i++)
& i:
© printi("\nEnter Ad] --> i+];
int ADO), ij 0, , key,
(Ged",d", &ALi)il);
=~ seanf("%
printf('Enter Number
of Elements :=> ");
forfi=03 i<n-1; i+ +) scant(’%a", 8);
it for(i=0; i<a; i++)
) Min
— for 1; j<nj j++) print (\nEnter Afsed] :-> *i + 1s
sean", BAT;
if (Ai] < A[Min]) }
. Min = js
} .
fe ~ stemp = ALi]; foi icm**)
Ali] = A[Min}; i
_A(Min] = temp; cet =a
iejels
: leg >= 0G
ANT > BD
| Printf(\nOutput After Pass %d >"> it) = 3
E ees k<n; k++) Atl
=A “é
joie

Print("%ed_ ",A{k])s } ; 2.
agen =r nena? inh

fats ket)

Scanned by CamScanner
ysis of Algorithms (MU - Som 4 - Compy
12!

Lab Experiments
Analysis of Algorithms (MU - Sem 4 - Comp) L2 Ss

if(ow!=high)
{
mid=(low+high)/25
lerge(l Ns
Merge(mid+1.bigh);
MSort(low,mid,bigh)s ram to implement Quick sort.
} gaing Divide and Conquer Prin(Ester Number of
Disp0s
printi("\n");
fo Seana" aj Hlemenis :-> >,
Eater A[4] > 11 stdio.h>
Enter A[S] :~-> 88 3 jeaude<
Prind{(\nEnter elements :
int mid, int high)
low, int for(i=1, i<=n;i44) a
[Output After Pass 0 :—> 33. 66 void MSort( iv Aeols {
(int af], int p, int 1);
Output After Pass 1 :—> 33 55 66 { id Quicksort
int p, intr); Prin Ener Ae
:—> 9,
Output After Pass0 :-> 11 33 55 66 int parttion(int a[), int);
int temp[MAX];
lee Exchangetnt i, "
JOutput Afier Pass 4 > 11 33 55 66 88
ante a:
int i=low;
int j=mid+1;
int k=low; int p, int r)
Program 3 oidQuicksortGint af],
Write a program to implement Merge sort. Quicksort(A,1,a);
while((i<=mid) && j-<=high))
#include<stdio.h> intq
iffe < 9)
print{("wSorted Elements >");
#define MAX 20 for{i=1; i<=n; i++)
if(ali] <= ali)
temp[k+ +)=alit+ +);
= Partition(a, py e)s {
else printl('%34 ALi)s
temp[k++]=alj+
+s Quickeort(a, p, ¢-1):
void Merge(int,int); Quicksort(a, q+, 7)s
void MSort(int,intint); while(i<=mid) }
void Disp): templk+ +]=alit+ +]; }
while(j<=high)
int main()
templk+ +]=aj++]; fat Partition(int af), int p, int r)
{ for(i=low;i<=highsit +) {
print{("Enter Number of Elements:~>"); int x, j,i
a[i]=tempfi];
sean{("%d" &n)s
} x= alr};
for(i=0;i<nsit+) i=p-b
void Disp)
{
printf("Enter A[%d]:-->",i)s { for(i = pij <= (1s j++) inter ATL => 7
for(int i=0;i<nji++) iffalj] < x) mer Al2] =>
seanf("S¢d",&a[i]);
printi(" %d",afi); aM Enter AGI:-? 5
}
} isith
Merge(0,n-1)s Output Exchange(i,);
Diap0s Enter Number of Elements:~>4
Enter A[0]:-->7 Exchange(i + 1,1);
return 0;
Enter Af1]:—>5 } retum(i + 1);
J
void Merge(int low,int high)
{ fe Exchange(int i,int j)
int mid;

Scanned by CamScanner
algorithms (MU Sem4-
= =Comp)_

Lab Experiments i : m 6
ee Analysis of Algorithms (MU - Sem 4 - Comp) L4 , po= knapsack Problam Using
iesoae its)
Program 5 for(k
Write a program to implement Single source shortest { ecole h> 2 iD:
path using Dynamic Programming (Bellman Ford u = edge(k](0] . v = edge[KIEL) + fp p prin! C0")
Algorithm). it(distance[u] + DistMatful[y] < distance[y]) igus
°
%
4
y+ 7 Fen
{ a {pak yal (12]121,Woa.ij.ks
#include <stdio.h>
#include <stdlib.h>
distance(v) = distanceu] + DistMat{ul[v] , ) “= a
parent[v}=u 5 atin")
asks :
} else
int main) {
}
{ ) "ATE = Mar, 2
int aun Vertex.edge[20][2].DistMat{20]{20],ij,k=0; }
for(k=0;k <Ek++)
print{("Enter Number of vertices >");
scanf("4ed",SmumVertex); {
u = edge[k][0] , v = edgefKIL1] +
}
if(distance{u) + DistMat{u][v] < distanee[v)} )
printf(‘Enter Distance Matrix:\n"};
{ disp;
for(i=0; i<numVertex; i++) flog = 0
for(j=0; j<numVertex; j++)
}
{
}
ant("Enter wed :—-> "i)s int Max(int mint n)
scanf("%d",&DistMat{i] fi); eat "90d" SewLiD)s
if(DistMatfi]G] !=0)
if(flag) iff >=)
E Se —>"\i);
edge[k][0]=iedgelk+ +] =i: for(i=0; i<numVertex; i++) Felum m;

if(Bellman_Ford(DistMat, numVertex, k, edge))


{
printf('Vertex Yd -> cost = %d, parent =
{ ced\n",i+1,distance{i},parent[i]+1);
printf(\nNo negative weight eycle\n"); }
}
return flag;

printf("\nNegative weight cycle exists\n"); t


Output
return 0; Enter Number of vertices :--> 4
Enter Distance Matrix:
peint{("e02d "5;
=

ym
3568 printi(”
int Bellman_Ford(int DistMat{20](20] , int numVertex, int E, 3251 for(i=0s i
int edge[20][2])
3168 print{('-
{ Py
int isuyv,k,distance[20],parent{20],S,flog=1; 4693
forfi=1)i<=ns i++)
for(i=0; i<numVertex; i++) Enter Source Vertex Number :—> 2
{ Vertex 1 -> cost = 3, parent = 2 xis 024 |) Ds
distance[i] = 1000, parent[i] = -1; for(i=035<= 4 itt)
Vertex 2 -> cost = 0, parent = 0
} Vertex 3 -> cost = 5, parent = 2
printf("Enter Source Vertex Number :--> "5 prin imo "wal
seanf("S%d",&9); Vertex 4-> cost = 1], parent = 2
}
distance[S-1]=0 ; print"
INo negative weight cycle found
for(i=0; i<numVertex-1; i++)
{

Scanned by CamScanner
Lab Experiments
[EP anatysis of Algorithms (MU - Sem 4 = Comp) L6

Output prinif("wEnter the Distance Matrix -->


Enter number
af Items :—> 4 i {)!=0)
i && (visited Cityfi
for(i=0; i<n; i++) i janet i]+DistMatfi]{e] < min)
sonst]
print{("\aEnter Distance of Row: Gédln"yi+ 1);
7
t
<iMat{i}{0] + DistMarfe}(;
for j=0;j < j++)
Enter v3 :—> 12 {
scanf("Hd",&DieMatLiLD)s e220, Bon) 2.05,
Enter w4:—> 4
[Enter v3 :—> 20 }
visited City[i]=05 int Maxtinyi,
Enter Knapsack Capacity (W—->6 } oid LCS,
oid disp(),
printi(*\n\aCost Matrix :—-> %
Joo 01 02 03 0: 05 06
int maint)
j01 6S | 00 65 05 05 05 05 05
for i=0; i<n; i++)
(02 G7 | 00 05 OF 12 12 12: 12
'o3 12} 00 OS 2
oop Prin (Eater Sing A:->
jos 20}
00 05 0 Bm > 4 sean),
fas Kimberof Cities == nt Eae Seog B-->
print(*uSed"
Diss at{i] fi); bare Distance Matrix —> seanf("Ss"B).
} farrDistance of Row: 1 M=stolen(Ay,
} 5710 12=stilen(By,
J
‘oid mincost(int city) Distance of Raw: 2
Css [10] acxt=9, for{i= 5 AG! =NULLi+ +)
|f 058
int i, ncitys {
<Distance of Row: 3 C+ 4]=Alii
| visitedCityfeity] = 1; i205 }
Distance of Row: 4 CiJ=NULL;

for{i=0j=1; Bfl!=NULLi++)
{
D+ +J=Bhi:
}
Df)=NULL;
printi("Sed", neity + 1);
eost+ =DistMat[city][ncity]; forfi=0,i<=lhi++)
return;
{
for(i=05<=!25+*)
mincest(ncity);
5
t iti==0 | 5-29)
MiIDI=8
int Jeast(int c)
| print Enter Number of Cities :-~> ");
secant
Gd" En,
q
int i, ne = 999;

Scanned by CamScanner
ysis of Algorithms (MU - Sam 4 - Cg
LB LabSS
Experirimen
LBP analysis of Algorithms (MU - Sem 4- Comp)
forfi=O,BEi NULL;i+ +) prin menter Adjacency Matrix => yr),
prinif("\n\nLength of Longest Common Subsequence > printf("-—" Pred) = staring
ed", M[LIILIZN; foti= ;i<n; it +) \ Visited[i)
= 0,
riecodcethit+) { :
return 05; for(j=0; j<me it +)
{ distance{startnode] =0
if(i==0) {
prin" ")s geanf("%d" ,&GFiI[j])s Visited|startnode] = 1;
if(i>0) count = |;
}
print(('%e | "A[1])s }
while(count < n-1)
for(j=05)< =12++) {
mindistance = INFINITY;
{
forG=1;j<=12;j++) printf('9202d "M[i]LiN; printf("\nEnter Initial Node :--> ");
U{nextnode gives the node at minimum distance
{ } scanl("Sed",&u)s for(i=0; i<n; i++)
ifCEiJ==DGj) print{("a\n")s Dijkstra(G,n,u); if(distanceli] < mindistance &&!visited|i])
{
MLE]J= OMG LU)+ return 0; mindistance = distance[i);
}
}
else
{ Enter String A :--> abebca
oid Dijkstra(int G[MAX][MAX]
int n,int startnode)
al =(M[i-1]E) i Memory Funetion Enter String B :~-> abcab
{ Hcheck if a better path exists through nextnode
{F)G-U)s visited[nextnode] = 1;
M[i}{j]=Max(al,a2);
int cost{MAX][MAX], distance[MAX], pred[MAX]; for{i=0; i<n; i++)
int visited[MAX], count, mindistance, nextnode, i,j; if(tvisited[ip)
J
if(mindistance + cost{nextnode|fi) < distance[i]})
PP; b}o12222 Hipred[] stores the predecessor of each node {
of nodes seen so far
Heount gives the number distance[i] = mindistance +
dispQ)s le [012333
cost[nextnode][if;
bf O12334 predfi] = neatnode;
} [ }012334 Mf Create cost matrix
afO12344 for(i=0;
{ i<n; i++) count+ ate +;
int Max(int m,int n)
{ Length of Longest Common Subsequence :--> 4 for(j=0; j<n; j++) }
if(m>=n) { HMprint the path and distance of each node
relum m; Program 9 if(G[i][j] == 0) for(i=0; i<n; i+ +)
else
Write a program to solve single source shortest path { iffi |= strtnode)
Problem using greedy approach (Dijkstra’s algorithm) cost[i)[j] = INFINITY; {
printf(\wDistunce of node%d = Stl",iglistance{i]}s
#include<stdio.h>
else printf(\aPath = di):
#include<conio.h>
#define INFINITY 9999 { jai
#define MAX 10 cost{i]§j] = Cli
void Dijkstra(int G(MAX] [MAX], int n,int startnode); }
printi(” "i
}
}
for(i=0;B[i]!=NULL;i+ +) int main()
printf(°%0.2c "\B[i}); {
Hinitiative pred],distance(] and visited)
P; im GIMAX]IMAX],i,,n,u; for(i=0; i<n; i++)
printf” "); Print{('Enter Number of Nodes :-->
"5 {
seanf("%d",&n); distancefi] = costfstarinodells :

Scanned by CamScanner
ee Analysis of Algorithms (MU - Sem 4 - Comp)

iffi< n)
Enter Number of Nodes :-> 4 x[i] = v/ weight[i}; { Yalue | 3.99
satio[i)=value[i)/weight{ij; x| £00 15.00 990
Enter Adjacency Matrix :=> 1.00
tp = tp + Gfi]*valuefi))s } Matime, pn 100 025
um Possible Profit => 96 05
printf("in nna,
printf(\aweight |"); 1p Sort items according to value to weight rain Program 14
for(i=O:i< nsi+ +) for(i=0; i< m3 i+ +) Write a progam to soke ‘ob /
{ using greedy approach, Sequencing problem
{
print{("Ge1.2u" weightfi]); for(j=itliy< m5 j++) #indude<stdio h>
} { #define MAX 109
Distance of node) = 8 print(invalue |" if(ratio[i] < ratiofj})
peas =0<-i { Struct Job{
for{i=O:i< n:it +) temp = ratio[j]; har id[5);
{ ratio[j) = ratio[i}; int deadline;
Distance of nex printf("%1.2P1" value[i]); int profit; |
Path = 3<-]
}
prinf(in x |"); temp = weight{jJ; id JobSequencing(struet Job jobs[)
int n):
Program 10 for(i=O;i< nit +) weight{j] = weight
Write a program to solve Knapsack Problem using { weight{i) = temps int main(void)
Greedy approach. printh(S¢1.2f".x[i]);
| | Simelude<stdioh> } temp = valuelj]s inti js
| #include <conio.h> value{j] = valuefijs
| printf(in Maximum Possible Profit :—> %.2f", tp); value{i] = temp; struct Job jobs[5] = {
[roid Knapsacklint n, float weightf]. float value[], float W) } {70",2, 30},
{ } {'2",1, 80},
}
float x{20], tp = 0: "j3",3, 40},
} t}¥",2, 25},
int main() Knapsack(n, weight, value, W);
175", 1, 30},
{ return 0;
i
| float weight{20], value{20], W; }
| Vector int o, i js struct Job temp;
| Output
float ratio[20], temp:
lt Enter Number of Items :--> 4
inta = 5;

| x{i]=0.0; printf (\n Enter Number of Ites Sort the jobs in descending order of profit
jo} seanf ("%d", &n); Enter Weight of Item 1 :=-> 1 for(i= li <mpi++)
{
Enter Value of Item 1 :--> 3 {
forlj = 0;j<n-igj++)
| for {i=O; 1 < nzi++) for (i=0;
i< n; i++) Enter Weight of Item 2 :—> 2 {
jt { Enter Valuc of Item 2 :--> 6 iffjobs [i+] |.profit > jobs{j]-profit)
if(weight{i] > u) printf (\nEnter Weight of Item %d :~> ", i+); {
break: seanfC%", &weighi[i]); Enter Weight of Item 3 :—-> 4 temp = jobsfj+15
else printf (Enter Value of Item Sed :—> ", i-+1)s Enter Value of Item 3 :--> 9 :
jobsfj+1] = jobsb:
I o¢ scanf("%!", &valuefi);
jobsfi] = temp;

x[i] = 1.0; } Enter Weight of Item 4:-> 5 }


ip = ip+valve[i]; Enter Value of Item 4 :--> 15 }
u = u-weighi[i}; printf (“\nEnter The Knapsack Capacity (W) :-> "5 Enter The Knapsack Capacity (W) :-> 9 }
printf('410 GlOs Hl
o:\u", "ob?, "Deadline", "Profit;
seanf (f", &W); fon = G1 < itt) {

Scanned by CamScanner
analysisol Algorthns (MU -Sem 4
™ anayss of algontnins (AU + Som 4 ~ Com)
LA =Semp) Le
(i OptinalStorageSchedul)
ef CSEADs SIV BONN, obs, ja Sort Progs()i
_ bsti phos
Jirequuired jobs
prinif(Wattequired Jobs: 7); fat main
Bago fori= Agi <= dmax; i++) { HsOpti malStorageSchedule();
prints", jobsftimestotfi]]
id);
serum O;
voter Program 13
if < dmax) {
prind? > Me OptimalStorageSchedule() Me @ Program
backtracking, to sohe Nequeen problem using
Ht .
intk = 0% HFinclude <sutio >
print{("Original Program Length: —> "), #inelude<mathh>
{required profit for (305 Stu i++)
maxprofit = 0: { nt Board16), count;
for(i= 1;1 <= dmax; i++){ print((” Sed", LU);
| maxprofit+= jobstimeslot[i]].profit: int main)
}
H] 3 SartProgs();
intn, i,j;
| printf(\oMax Profit: dln", maxprofit); print{{"\nSorted Program Length: —> ");
void Queenfintrow.into);
for ((=0; i<n: i++)
prin{(\n\aEnter number ofQueens:”);
| printi(” Sed”, L{i]);
| int minValue(int x, int y) seanf(ed",én);,
} Queen(I.n);
ssfi].deadline > dmax) { { forfi=0; i<n;it+ +)
return 0;
x = jabs[ipdeadl ifix < y) { }
return x; pSum = pSum + void Queen{int row, int n)
retum y; Sum = pSum + L[is
RT[i] = Sum; int cal;
} for{col = 1; ecl<=n; ++cal)
print{("\n\nTotal Retrieval Tim —> Ged", RT[2); {
Deadline — Profit print{("\n\nMean Retrieval Time :—> Sf", RT[2}(Coat)n}; if(FlaceQueen{row,
cal))
i {
[roid SortProgs() Board[row] = eal; Place queen into’ row and in
if col column
inti, j, temp;
forfi=0; i<n - 1; i++)
ifrow == n)f Al ows are scanned - stop
k>= DL
if(timestor[k] DisplaySolation(s);/ Pint the salution
forj=0;j < (n - i - 1);j+4)
timeslot [k] }
filledTimeSto1
{ else j/ Check for the raw
break;
if (LG +1] < Li)
Program 12 { Queen(row-+ 1, n);
Write a program to salve Optimal Storage on Tapes temp = Lj}
problem using greedy approach. LG) = Lj +1
}
Hif all time slots are filled then stop #include <stdio.h> Lj + 1] = temp;
if(filledTimeSlot == dmax) { int n = 3, Sum = 0, pSum = 0;
break; int L] = {8, 12, 2};
}
int RT] = {0, 0, 0}; int Place! cenint ro it lune)

Scanned by CamScanner
number of Queensst whl=eGtth
i wlit Hetemp;
int iz
for(i= 1: iS rom ne ptt |Solution |

coluna) (7 Celume conflict


if(Board{i] ==
“nj;
{
primfwrFtements in Sorted Order :=>
for (=O MIF +)
{
print Alii Enter Number 6 +> 8
jfiJ-column) == alnfi-row)} JEmer Number 7 :-> 6
}
ig((toral <sum))
print{(\wSubyset construction isnot possible)
+
14 else
Program ym of subset problem using
} rogram to solve Sut +)
for ((=O:i<mjit
Wrote 2 p
return 1; {/No conflicts
incfiJ=Os
void DisplaySalution{iat a) | #include printf("\nPe ‘ine solutions using backtrackingsin’};
Su (SulsSet(-1,0,total)s Program 15
problem using
nin’ .+ +e \ Write a program to solve Graph coloring
ninSolutionHd return O; backtracking.

suum) | | (nt fi] include <stdioh>


int wl,int total)
oid SumofSubSct(int i, int G{SOYESO} x150];
printf eRe,
(
+ ti int js
forti=slricen;
( if(promising(iwtaota
l))
print{| Wain
+H Solution Vector
forgets F< {Elementsin Sets? 7% ickjit+) ehecking
all k-I vertices-

== }) I connected and has


sacynge 0 88x04 == 1
{
it(Board[i]
for §=05 ;
edlor than x(]
ae) = ais sion Juigher color
> itl)
printf Q"); Cell «i iffine|j})
printt((Ged" ls
print’ 0")
to erate sub net“? ”
the SUM value
print((71.); // Cell without quern ecanft ed" Mout) :
_
Son er i n ascending order incl H}= TRUE jt erie
for (=O SOT 7) oes Totten
inc{i+ H}=FALSEs
printf(\n\"}; for j= j<n-ljt+)
} 4

Scanned by CamScanner
js of Algorithms (MU

a So
Laz

Lab Experiments,
Saree as
Lab Experenents

}
‘itsacost(ncity)s
Program 16
Vertices to solve Traveling Salesman Problem
printf("Enter Numofber Write a program d Bound. y
| ecanfd" fn}: using Branch an
rof Edges => i least
int °)
| printf( Enuer Numbe [#include <stdioh>
"Ged? Beh | #include <stdlib.h> if erin
= 29%
Adjacency Matrix
i<n3 ) jint Distant eMatrix[0}[10], Visi
tedCities[10].n,cost=0; jatrain 999, kmin;

void getData()
K { =
is (pistanceMatrix[e]E}'=0) 88 (VisitedCin i) == 0)
int igi »
i{DistanceMatrix{o}fi}<sin) Write a programto
to is Si Matching
‘hie String
"5 algorithm implement a Naive
printf("w\nEnter Number of Cities:—-> {
min = DistanceMatrix{i}[0] + DistanceMatixfe}fi); \#include<stdioh>
scanf((%d",80)5
x %d) Distans .ce Matrix: \n", n, n)5
printf(\nEnter (Ged DistanceMatrix{e][i}; : #inelude<stingh>
print Enter indexes where value is 1-7 \0"}
forli=05 i<m3 i++) int NaiveStringMatch
(char Texi(100),char Patiern{100]);
{
for( j=O5<i++)
{ iffnin = 999)
scanf(*Ga",&DistanceMatrix {IUDs char Text {100} Pattera{100};
{ int situs,
+ cost += kmin;
VisitedCities{i] = 05
i=0; i<n; i+ +) }
} setum fC%
\print{("Enter the Text :—> \n");
);
GraphColor{i): prinu((\n\nThe Distance Matrixisn" I} gets{Text);
for( i=D;i<njit+ +) prini{("Enter the Pattern => \n"}s
printf("Colors of vertices => inscolor of each vertex scid DisplayPath() gets(Pattern);
{
forli=Osi<mi++) /displayin) printf("\n\o"}s status = NaiveStringMatch(Text,Pattern);
printf("Vertex[%d] : Sodhn"yi+1x01)5 for( j=0;j<nyj++)
peint((\n\nMinimam cost") if(status == -1)
prinif("%d" cost); prit{(\nMatch Not Found "5
{
dlse
print{(1u%d" Distance Matris{i]G)s int main() Y status);
prim Match Found on 4 position
K retum 0;
getData();
a"}s
print{("\a\n'The Path is:\n\ m[}00])
Enter Number of Edges :- > 6 mincost((); nt NeiveStringMateh(cba
r‘Test{ 100) char Pate
Enter indexes where value is 1-> void mincost(int city) DisplayPathQ)s {
12 1=0,erp=05
{ ) jnt nym,ij,coum
24 int incily; ncatrlen(Texth
Output
43
VisitedCities[city] = 1: peetlen(Patler")s
3.1 Enter Number of Cities:--> 4 fond ic=eam
lt +)
13 printf("%d --> ",city+1);
neity = Jenst( lEnter (4x 4) Distance Matrix:
14 tempt +5
if(neity==999)
Colors of vertices :--> fogeogemi*)
Vertex[1] : 1 {
ncity=0; {
Vertex[2]: 1
print{("%d" ncity +1);
Vertex(3] : 2 ][ncity
cost+=DistanceMatrix[city
Vertex[4] : 2

Scanned by CamScanner
poe =ebD
——
of Final States
>= “exueDFACHIOD
P pmpE meer Namie yerura
TS
cal. Seofinals

t 7) output
foot shicefinaisi

=> 2 |
Number of Inputs
er Empat 0 Symbols
impor >
State. Inpet {iepot wl \1 Bed the vet array forthe ven pera *
T | enter ino
payer Number of Final States :—> | inti = 2) = 0, lem = stten(word,
ql
Final state | Input Spmbol) =
[Define Transition Rule : (Current State,
Final State |e
(qo.0)= 9° jan=c,
(qi-0) =a
go.) =a! hile
(i <len) {
qi.) = i (wordt -1) == woodliD{

Enter Input String. 1010101 puri


Invalid String i=ith
§ > 04
} elseif
Program 19 ng matching using
m to implement stri j= publ
Write a progra
Yelse{
KMP algorithm
puri]
=0
#inelud stdio.h> iaith
#include <string.h>
>
include <stdlib.h
forti=0
i <eli
{
char *word, int *pt)
int KMPichar str,
imi = 0,5 =%
print((Ta valid n(ste))
while ((i + j) + strle + jnt main() 4
pattem string char 02815
| scant Sei Anatau cle /* matc h foun d on the target and char word 256} st0
i
fener ne printt(” nvalid string’) «f (wordhj] == steli iD jnt * ptt
seanfi"Ced”.S ninputs’s if == (stem word)
- wt
*!
|
index Seah", , E from the user
| prinfi"Ce located at the eget the target SH
ee Input Symbol sive inh
i +1) Text -7 %
word, print” “Enter
l
fflush(stdin): return; gee. 1024 si ~ We
srlentstt)

i+ +}
for{i=0; i<ninputs;
4] pe gptthe patter
} i J else{ . s 19compare
[printf Tnput it}; next indice
/* ma nipulating
scant(e%ec", efi);
{Mushistdin); ait j- publ

Scanned by CamScanner

You might also like