0% found this document useful (0 votes)
2 views406 pages

Algorithm Notes

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views406 pages

Algorithm Notes

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CS3401 ALGORITHMS UNIT 1 MEC

UNIT I
INTRODUCTION

Algorithm analysis: Time and space complexity - Asymptotic Notations and its
properties Best case, Worst case and average case analysis – Recurrence relation:
substitution method - Lower bounds – searching: linear search, binary search and
Interpolation Search, Pattern search: The naïve string matching algorithm - Rabin-
Karp algorithm - Knuth-Morris-Pratt algorithm. Sorting: Insertion sort – heap sort

PART A

1. What is an algorithm? Or Define an algorithm. (Apr\May- 2017)


 An algorithm i s a finite set of instructions t h at , if followed, accomplishes a
particular task.
 In addition, all algorithms must satisfy the following criteria:
 input
 Output
 Definiteness
 Finiteness
 Effectiveness.
2. Define Program.
A program is the expression of an algorithm in programming language.

3. What is performance measurement?


Performance measurement is concerned with obtaining the space and the time
requirements of a particular algorithm.

4. What is recursive algorithm?


 Recursive algorithm makes more than a single call to itself is known as recursive call.
 An algorithm that calls itself is Direct recursive.
 Algorithm A is said to be indeed recursive if it calls another algorithm, which in turn
calls A

5. What is space complexity?


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

6. What is time complexity? ( Nov/Dec 2024)


The time complexity of an algorithm is the amount of time it needs to run to
completion.

7. Define input size.


The input size of any instance of a problem is defined to be the number of elements
needed to describe that instance.

8. Define best-case step count.


The best-case step count is the minimum number of steps that can be

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 1


CS3401 ALGORITHMS UNIT 1 MEC

executed for the given parameters.


9. Define worst-case step count.
The worst-case step count is the maximum number of steps that can be executed
for the given parameters.

10. Define average step count.


The average step count is the average number of steps executed an instances with
the given parameters.

11. Define the asymptotic notation “Big oh” (0).


A function t(n) is said to be in O(g(n)) (t(n) Є O(g(n))), if t(n) is bounded above by
constant multiple of g(n) for all values of n, and if there exist a positive constant c and
non negative integer n0 such that
t(n) ≤ c*g(n) for all n ≥ n0.

12. Define the asymptotic notation “Omega” ( Ω ).


A function t(n) is said to be in Ω(g(n)) (t(n) Є Ω(g(n))), if t(n) is bounded below by
constant multiple of g(n) for all values of n, and if there exist a positive constant c and
non negative integer n0 such that t(n) ≥ c*g(n) for all n ≥ n0.

13. Define the asymptotic notation “theta” (Θ).


A function t(n) is said to be in Θ(g(n)) (t(n) Є Θ(g(n))), if t(n) is bounded both above
and below by constant multiple of g(n) for all values of n, and if there exist a positive
constant c1 and c2 and non negative integer n0 such that
C2*g(n) ≤ t(n) ≤ c1*g(n) for all n ≥ n0.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 2


CS3401 ALGORITHMS UNIT 1 MEC

14. What is a Computer Algorithm?


An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for
obtaining a required output for any legitimate input in a finite amount of time.

15. What are the features of an algorithm?


More precisely, an algorithm is a method or process to solve a problem satisfying the
following properties:
Finiteness-Terminates after a finite number of steps
Definiteness-Each step must be rigorously and unambiguously specified.
Input-Valid inputs must be clearly specified.
Output-Can be proved to produce the correct output given a valid input.
Effectiveness-Steps must be sufficiently simple and basic.

16. Show the notion of an algorithm. Dec 2009 / May 2013


An algorithm is a sequence of unambiguous instructions for solving a problem in a finite
amount of time.

17. What are different problem types?


o Sorting
o Searching
o String Processing
o Graph problems
o Combinatorial Problems
o Geometric problems
o Numerical problems

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 3


CS3401 ALGORITHMS UNIT 1 MEC

18. What are different algorithm design techniques/strategies?


o Brute force
o Divide and conquer
o Decrease and conquer
o Transform and conquer
o Space and time tradeoffs
o Greedy approach
o Dynamic programming
o Backtracking
o Branch and bound

19. How to measure an algorithm’s running time? Nov/Dec 2017, Apr/May 2024
Unit for measuring the running time is the algorithms basic operation. The running time
is measured by the count of no. of times the basic operations is executed.
Basic operation: the operation that contributes the most to the total running time.
Example: the basic operation is usually the most time-consuming operation in the
algorithm’s innermost loop.

20. How time efficiency is analyzed?


Let cop – execution time of algorithms basic operation on a particular computer.
c(n) – no. of times this operation need to be executed.
T(n) – running time.
Running time is calculated using the formula T(n) ≈ cop c(n)

21. What are orders of growth?


Orders of Growth

22. What are basic efficiency classes?


Basic Efficiency classes

1 Constant
log n Logarithmic
n Linear
n log n Linearithmic
n2 Quadratic
n3 Cubic
2n Exponential
n! Factorial

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 4


CS3401 ALGORITHMS UNIT 1 MEC

23. Give an example for basic operations.


Input size and basic operation examples
Problem Input size measure Basic operation
Searching for key in a Number of list’s items, Key comparison
list of n items i.e. n
Multiplication of two Matrix dimensions or Multiplication of two
matrices total number of elements numbers
size = number of digits
Checking primality of a Division
(in binary
given integer n
representation)
Number of vertices Visiting a vertex or
Typical graph problem
and/or edges traversing an edge

24. What are six steps processes in algorithmic problem solving? Dec 2009
 Understanding the problem.
 Ascertaining the capabilities of a computational device.
 Choosing between exact and approximate problem solving.
 Deciding on appropriate data structures.
 Algorithm Design Techniques.
 Methods of specifying an algorithm
 Proving an algorithm's correctness.
 Analysing an algorithm.
 Coding an algorithm.

25. Define order of an algorithm.


Measuring the performance of an algorithm in relation with the input size n is known as
order of growth.

26. How is the efficiency of the algorithm defined? Or How do you measure the
efficiency of an algorithm? May/June 2019
The efficiency of an algorithm is defined with the components.
(i) Time efficiency -indicates how fast the algorithm runs
(ii) Space efficiency -indicates how much extra memory the algorithm
needs

27. What are the characteristics of an algorithm?


Every algorithm should have the following five characteristics
(i) Input
(ii) Output
(iii) Definiteness
(iv) Effectiveness
(v) Termination

28. What are the different criteria used to improve the effectiveness of algorithm?
(i) The effectiveness of algorithm is improved, when the design, satisfies the

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 5


CS3401 ALGORITHMS UNIT 1 MEC

following constraints to be minimum.


Time efficiency - how fast an algorithm in question runs.
Space efficiency – an extra space the algorithm requires.
(ii) The algorithm has to provide result for all valid inputs.

29. Analyse the time complexity of the following segment:


for(i=0;i<N;i++)
for(j=N/2;j>0;j--)
sum++;

Time Complexity= N * N/2 = N2 /2 Є O(N2)

30. Write general plan for analysing non-recursive algorithms.


 Decide on parameter indicating an input’s size.
 Identify the algorithm’s basic operation
 Check the no. of times basic operation executed depends on size of input.
if it depends on some additional property, then best, worst, average
cases need to be investigated
 Set up sum expressing the no. of times the basic operation is executed.
(establishing order of growth)

31. How will you measure input size of algorithms?


The time taken by an algorithm grows with the size of the input. So the running time of
the program depends on the size of its input.
The input size is measured as the number of items in the input that is a parameter n is
indicating the algorithm’s input size.

32. Write general plan for analysing recursive algorithms.


 Decide on parameter indicating an input’s size.
 Identify the algorithm’s basic operation
 Checking the no. of times basic operation executed depends on size of input. if it
depends on some additional property, then best, worst, average cases need to be
investigated
 Set up the recurrence relation, with an appropriate initial condition, for the
number of times the basic operation is executed
 Solve recurrence (establishing order of growth)

33. What do you mean by Combinatorial Problem?


Combinatorial Problems are problems that ask to find a combinatorial object-such as
permutation, a combination, or a subset-that satisfies certain constraints and has some
desired properties.

34. Define Little “oh”.


The function f(n) = 0(g(n)) if and only if
Lim f(n) / g(n) = 0
n →∞
35. Define Little Omega.
The function f(n) = ω (g(n)) )) if and only if

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 6


CS3401 ALGORITHMS UNIT 1 MEC

Lim f(n) / g(n) = 0


n →∞

36. Describe the recurrence relation for merge sort.


If the time for the merging operation is proportional to n, then the computing time of
merge sort is described by the recurrence relation

37. Define Algorithm validation. Dec 2012


The process of measuring the effectiveness of an algorithm before it is coded to know
whether the algorithm is correct for every possible input. This process is called
validation.

38. What is a recurrence equation? ( Nov/Dec 2024)


A recurrence [relation] is an equation or inequality that describes a function in terms of
its values on smaller inputs.
Examples:
Factorial: multiply n by (n –1)!
T(n) = T(n – 1) + O(1) ---> O(n)

Fibonacci: add fibonacci(n – 1) and fibonacci(n – 2)


T(n) = T(n – 1) + T(n – 2) ---> O(2n)

39. What is average case analysis? May 2014


The average case analysis of an algorithm is analysing the algorithm for the average
input of size n, for which the algorithm runs at an average between the longest and the
fastest time.

40. Define program proving and program verification. May 2014


 Given a program and a formal specification, use formal proof techniques
(e.g. induction) to prove that the program behaviour fits the specification.
 Testing to determine whether a program works as specified.

41. Define asymptotic notation. May 2014


Asymptotic notations are mathematical tools to represent time complexity of
algorithms for measuring their efficiency.
Types:
 Big Oh notation - 'O'
 Omega notation - 'Ω'
 Theta notation - ’Θ’
 Little Oh notation - 'o '
 Little Omega notation - 'Ω'

42. Establish the relation between O and Ω . Dec 2010


f(n) ∈ Ω(g(n)) ⟺ g(n) ∈ O(f(n))
Proof:

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 7


CS3401 ALGORITHMS UNIT 1 MEC

O(f(n))={g:N→N | ∃c,n0∈N ∀n≥n0:g(n)≤c⋅f(n)}


Ω(g(n))={f:N→N | ∃c,n0∈N ∀n≥n0:f(n)≥c⋅g(n)}
Step 1/2: f(n) ∈ Ω(g(n)) ⟺ g(n) ∈ O(f(n))
∃c,n0∈N ∀n≥n0: f(n)≥c⋅g(n)⇒f(n)g(n)≥c⇒1g(n)≥cf(n)⇒g(n)≤1c⋅f(n)
And this is exactly the definition of O(f(n)).
Step 2/2: f(n)∈Ω(g(n))⇐g(n)∈O(f(n))
∃c,n0∈N ∀n≥n0: g(n)≤c⋅f(n)⇒...⇒f(n)≥1c⋅g(n) Hence proved.

43. What is best case analysis or Best case efficiency ?


The best case analysis of an algorithm is analysing the algorithm for the best case input
of size n, for which the algorithm runs the fastest among all the possible inputs of that
size.

44. what do you mean worst case efficiency of [Link]/Dec 2017, Apr/May 2024
The worst case analysis of an algorithm is analysing the algorithm for the worst case
input of size n, for which the algorithm runs the longest among all the possible inputs of
that size.

[Link] an algorithm that finds the number of binary digits in the binary
representation ofa positive decimal integer. (AU april/may 2015)
Number of major comparisons=⌊ log2n⌋ + 1∈log2n.
Algorithm :
Finding the number of binary digits in the binary representation of a positive decimal
integer.
Algorithm Binary(n)
count:=1;
whilen >1
do
count:=count+ 1;
n:=⌊ n/2⌋ ;
end
return count;

46. Write down the properties of asymptotic notations. (AU april/may 2015)
The following property is useful in analyzing algorithms that comprise two
consecutively executed parts.
Theorem
If t1(n) Є O(g1(n)) and t2(n) Є O(g2(n)) then,

t1(n) + t2(n) Є (max {g1(n), g2(n)})


Proof
Since t1(n) Є O(g1(n)), there exist some constant C1 and some non
negative integer n1 such that
t1(n) ≤ C1 (g1(n)) for all n ≥ n1
Since

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 8


CS3401 ALGORITHMS UNIT 1 MEC

t2(n) Є O(g2(n))
t2(n) ≤ C2 (g2(n)) for all n ≥ n2
Let us denote,
C3=max {C1, C2} and
Consider n ≥ max {n1, n2}, so that both the inequalities can be used.
The addition of two inequalities becomes,
t1(n)+ t2(n) ≤ C1 (g1(n))+ C2 (g2(n))
≤ C3 (g1(n))+ C3 (g2(n))
≤ C3 2 max{g1(n), (g2(n))}
Hence,
t1(n) +t2(n) Є O (max {g1(n),g2(n)}),

with the constants C and n0 required by the definition being


2C3 = 2 max (C1, C2) and max {n1, n2} respectively.
The property implies that the algorithms overall efficiency will be determined by
the part with a larger order of growth.
(i.e.) its least efficient part is
t1(n) Є O(g1(n)) t1(n) +t2(n) Є O (max {g 1(n), g2(n)})
t2(n) Є O(g2(n))

47. Give the Euclid’s algorithm for computing gcd(m, n) (AU nov 2016) or write an
algorithm to compute the greatest common divisor of two numbers. (Apr-2017, 18)

ALGORITHM Euclid_gcd(m, n)
//Computes gcd(m, n) by Euclid’s algorithm
//Input: Two nonnegative, not-both-zero integers m and n
//Output: Greatest common divisor of m and n
while n ≠ 0 do
r ←m mod n
m←n
n←r
return m
Example: gcd(60, 24) = gcd(24, 12) = gcd(12, 0) = 12.

[Link] the order of growth n(n-1)/2 and n2. (AU nov 2016)

n(n-1)/2 is lesser than the half of n2

49. Fibonacci algorithm and its recurrence relation


Algorithm for computing Fibonacci numbers
First method
Algorithm F(n)
//Computes the nth Fibonacci number recursively by using its definition.
//Input: A nonnegative integer n
//Output: The nth Fibonacci number

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 9


CS3401 ALGORITHMS UNIT 1 MEC

if n<1
return n
Else
return F(n-1)+(n-2)
the algorithm’s basic operation is addition.
Let A(n) is the number of additions performed by the algorithm to compute F(n).
The number of additions needed to compute F(n-1) is A(n-1) and the number of
additions needed to compute F(n-2) is A(n-2).

50. What is a basic operation?


 A basic operation could be: An assignment. A comparison between two variables. An
arithmetic operation between two variables. The worst-case input is that input
assignment for which the most basic operations are performed.
 Basic Operations on Sets. The set is the basic structure underlying all of mathematics. In
algorithm design, sets are used as the basis of many important abstract data types,
and many techniques have been developed for implementing set-based
abstract data types.

51. Define algorithm. List the desirable properties of an algorithm.


 Algorithm is a step-by-step procedure, which defines a set of instructions to be
executed in a certain order to get the desired output. Algorithms are generally created
independent of underlying languages, i.e. an algorithm can be implemented in more
than one programming language.
 An algorithm must satisfy the following properties: Input: The algorithm must have
input values from a specified set. ... The output values are the solution to a problem.
Finiteness: For any input, the algorithm must terminate after a finite number of steps.
Definiteness: All steps of the algorithm must be precisely defined.

52. Define best, worst, average case time complexity.

The worst-case complexity of the algorithm is the function defined by the maximum number
of steps taken on any instance of size n. It represents the curve passing through the highest
point of each column.

The best-case complexity of the algorithm is the function defined by the minimum number of
steps taken on any instance of size n. It represents the curve passing through the lowest
point of each column.

Finally, the average-case complexity of the algorithm is the function defined by the average
number of steps taken on any instance of size n.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 10


CS3401 ALGORITHMS UNIT 1 MEC

[Link] that the of f(n)=o(g(n)) and g(n)=o(f(n)),then f(n)=θ g(n). OR


state the transpose symmetry property of O and Ω April/May 2019,Nov/Dec 2019
Given function:
f(n) and g(n)
f(n)= O(g(n)) when f(n) ≤C1g(n) for all n≥n0---------(1)
f(n)= Ω(g(n)) when f(n) ≥C2g(n) for all n≥n0---------(2)
from (1) and (2)
C2 g(n) ≤f(n) ≤ C1g(n) for all n≥n0 -------(3)
(i.e) Θ(g(n)) = O(g(n))Ω(g(n))
From (3) f(n) = Θ(g(n)) hence proved

54. Define recursion.


A function may be recursively defined in terms of itself.
A familiar example is the Fibonacci number sequence:
F(n) = F(n − 1) + F(n − 2).
For such a definition to be useful, it must be reducible to non-recursively defined
values: in this case F(0) = 0 and F(1) = 1. Occurs when a thing is defined in terms of
itself or of its type. Recursion is used in a variety of disciplines ranging
from linguistics to logic.
The most common application of recursion is in mathematics and computer science,
where a function being defined is applied within its own definition.
While this apparently defines an infinite number of instances (function values), it is
often done in such a way that no loop or infinite chain of references can occur.

55. Define Substitution Method


In the substitution method, we have a known recurrence, and we use induction to prove
that our guess is a good bound for the recurrence's solution.
Steps
 Guess a solution through your experience.
 Use induction to prove that the guess is an upper bound solution for the given
recurrence relation.
Example:
T (n) =1 if n=1
= 2T (n-1) if n>1
T (n) = 2T (n-1)
= 2[2T (n-2)] = 22T (n-2)
= 4[2T (n-3)] = 23T (n-3)
= 8[2T (n-4)] = 24T (n-4)

Repeat the procedure for i times


T (n) = 2i T (n-i)
Put n-i=1 or i= n-1 in (Eq.1)
T (n) = 2n-1 T (1)
= 2n-1 .1 {T (1) =1 .....given}
= 2n-1

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 11


CS3401 ALGORITHMS UNIT 1 MEC

56. Define Searching.


Searching is a technique that helps to find whether the given element is
present in the set of elements. Any search is said to be successful or unsuccessful
depending upon whether the element that is being searched is found or not. Some of the
standards searching techniques are:
 Linear Search or Sequential Search
 Binary Search
 Interpolation Search

57. Give the Complexity Analysis of Binary Search.


Time Complexity
 Best case - O(1)
The best case occurs when the target element is found in the middle of list/array.
Since only one comparison is made, the time complexity is O(1).

 Worst-case - O(logn)
The worst occurs when the algorithm keeps on searching for the target element
until the size of the array reduces to 1. Since the number of comparisons
required is logn, the time complexity is O(logn).

 Average case - O(logn)


Binary search has an average-case complexity of O(logn).
Space Complexity
 Since no extra space is needed, the space complexity of the binary search is O(1).
58. Define Pattern Search.

The Pattern Searching algorithms are sometimes also referred to as String Searching
Algorithms. These algorithms are useful in the case of searching a pattern in a string.

Algorithms used for String Matching:

Various string matching algorithms are:

 The Naive String Matching Algorithm


 The Rabin-Karp-Algorithm
 Finite Automata
 The Knuth-Morris-Pratt Algorithm
 The Boyer-Moore Algorithm

59. Define Rabin Karp Algorithm.


Rabin-Karp algorithm is an algorithm used for searching/matching patterns in the
text using a hash function. Unlike Naive string matching algorithm, it does not travel

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 12


CS3401 ALGORITHMS UNIT 1 MEC

through every character in the initial phase rather it filters the characters that do not
match and then performs the comparison.
 Initially calculate the hash value of the pattern.
 Start iterating from the starting of the string:
o Calculate the hash value of the current substring having length m.
o If the hash value of the current substring and the pattern are same, check
if the substring is same as the pattern.
o If they are same, store the starting index as a valid answer. Otherwise,
continue for the next substrings.
 Return the starting indices as the required answer.
60. Define Sorting.
Sorting is the processing of arranging the data in ascending and descending order.
There are several types of sorting in data structures namely,
 Bubble sort
 Insertion sort
 Selection sort
 Bucket sort
 Heap sort
 Quick sort
 Radix sort etc.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 13


CS3401 ALGORITHMS UNIT 1 MEC

PART – B

1. Explain the notion of an algorithm with diagram. May2014


Synopsis:
 Introduction
 Definition
 Diagram
 Characteristics of an Algorithm / Features of an Algorithm
 Rules for writing an Algorithm
 Implementation of an Algorithm
 Order of an Algorithm
Introduction:
 An algorithm is a sequence of finite number of steps involved to solve a
particular problem.
 An input to an algorithm specifies an instance of the problem the algorithm
solves.
 An algorithm can be specified in a natural language or in a pseudo code.
 Algorithm can be implemented as computer programs.
 The same algorithm can be represented in several different ways.
 Several algorithms for solving the same problem may exist.
 Algorithms for the same problem can be based on different ideas and can solve
the problem with dramatically different speeds.
Definition:
 An algorithm is a sequence of non ambiguous instructions for solving a problem
in a finite amount of time.
 Each algorithm is a module, designed to handle specific problem.
 The non ambiguity requirement for each step of' an algorithm cannot be
[Link] range of inputs for which an algorithm works has to be
specified carefully as shown in Fig.1.1

Diagram:

Fig.1.1 notion of an algorithm

Characteristics of an algorithm / Features of an Algorithm


The important and prime characteristics of an algorithm are,
 Input:Zero or more quantities are externally supplied.
 Output:At least one quantity is produced.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 14


CS3401 ALGORITHMS UNIT 1 MEC

 Definiteness:Each instruction is clear and unambiguous.


 Finiteness:For all cases the algorithm terminates after a finite number of steps.
 Efficiency:Every instruction must be very basic.
 An algorithm must be expressed in a fashion that is completely free of
ambiguity.
 It should be efficient.
 Algorithms should be concise and compact to facilitate verification of their
correctness.
Writing an algorithm
 Algorithm is basically a sequence of instructions written in simple English
language.
 The algorithm is broadly divided into two sections

Algorithm heading
It consists of name of algorithm, problem description, input

Algorithm Body
and output.
It consists of logical body of the algorithm by making use of
various programming constructs and assignment statement.

Rules for writing an algorithm.


Algorithm is a product consisting of heading and body. The heading consists of keyword
algorithm and name of the algorithm and parameter list.
The syntax is Algorithm name ( p1, p2,.......pn )

1. Then in the heading section we should write following things :


// Problem Description;
// Input:
//Output:
2. Then body of an algorithm is written, in which various programming constructs
like if , for , while or some assignment statement may be written.
3. The compound statements should be enclosed within { and } brackets.
4. Single line comments are written using // as beginning of comment.
5. The identifier should begin by latter and not by digit. An identifier can be a
combination of alphanumeric string.
 It is not necessary to write data types explicitly for identifiers. It will be
represented by the context itself.
 Basic data types used are integer, float, and char, Boolean and so on.
 The pointer type is also used to point memory locations.
 The compound data type such as structure or record can also be used.
6. Using assignment operator ← an assignment statement can be given.
For instance: Variable ← expression
7. There are other types of operators’ such as Boolean operators such as true or
false. Logical operators such as AND, OR, NOT. And relational operators such as <

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 15


CS3401 ALGORITHMS UNIT 1 MEC

, <= , >, >=, = , !=.


8. The array indices are stored with in square brackets ‘[‘ ‘]’. The index of array
usually starts at zero. The multidimensional arrays can also be used in algorithm.
9. The inputting and outputting can be done using read and write.
For example:
Write (“this message will be displayed on console “);
Read (Val);
10. The conditional statements such as if –then – else are written in following form
If (condition) then statement
If (condition) then statement else statement
If the if – then statement is of compound type then {and} should be used for
enclosing block
11. While statement can be written as :
While (condition)do
{
Statement 1
Statement 2
:
Statement n
}
While the condition is true the block enclosed with { } gets executed otherwise
statement after} will be executed.
12. The general form for writing for loop is :
For variable ← value1 to valuen do
{
Statement 1
Statement 2
:
Statement n
}
Here value1 is initialization condition and valuen is a terminating condition the
step indicates the increments or decrements in value1 for executing the for
loop.
Sometime a keyword step is used to denote increment or decrement the value of
variable for example
For i ← 1 to n step 1 Here variable i is incremented by
{ 1 at each iteration
Write (i)
}

13. The repeat – until statement can be written as


Repeat
Statement 1
Statement 2
:
Statement n
Until (condition)
14. The break statement is used to exit from inner loop. The return statement is

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 16


CS3401 ALGORITHMS UNIT 1 MEC

used to return control from one point to another. Generally used while exiting
from function
Note: The statements in an algorithm executes in sequential order i.e. in the
same order as they appear – one after the other

Example 1 : Write an algorithm to count the sum of n numbers

Algorithm sum (1, n)


//Problem description : this algorithm is for finding the
//sum of given n numbers
//Input: 1 to n numbers
//Output: the sum of n numbers
Result ← 0
For i 1 to n do
i ← i+1
Result ← result + i
Return result

Example 2: Write an algorithm to check whether given number is even or odd.

Algorithm eventest ( val)


//Problem description : this algorithm test whether given
//number is even or odd
//Input: the number to be tested i.e .val
//Output: appropriate messages indicating even or odd
If (val % 2 = 0) then
Write (“given number is even “)
Else
Write (“given number is odd”)

Example 3: Write an algorithm for sorting the elements.

Algorithm sort (a, n)


//Problem description: sorting the elements in ascending
//order
//Input: an array in which the elements in ascending order
//is total number of elements in the array
//Output: the sorted array
For i 1 to n do
For j i + 1 to n-1 do
If (a[i]>a[j]) then
{
temp ← a[i]
a[i] ←a[j]
a[j] ←temp
}
Write ( “ list is sorted “)

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 17


CS3401 ALGORITHMS UNIT 1 MEC

Example 4: Write an algorithm to find factorial of n number.

Algorithm fact (n)


//Problem description: this algorithm finds the factorial.
//for given number n
//Input : the number n of which the factorial is to be
//calculated.
//Output : factorial value of given n number.
If( n ← 1) then
Return 1
Else
Return n * fact(n-1)
Example 5:
Write an algorithm to perform multiplication of two matrices

Algorithm mul (A, b, n)


//Problem description: this algorithm is for computing
//multiplication of two matrices
//Input : the two matrices A, B and order of them as n
//Output : The multiplication result will be in matrix c
For i ← 1 to n do
For j ← 1 to n do
C [i,j] ← 0

For k ← 1 to n do
C[I ,j ] ←c[i, j] +A[i,k]B[k,j]

Implementation of algorithms
An algorithm describes what the program is going to perform. It states some of the actions
to be executed and the order in which these actions are to be executed.
The various steps in developing algorithm are,
1. Finding a method for solving a problem. Every step of an algorithm should be in
a precise and in a clear manner. Pseudo code is also used to describe the
algorithm.
2. The next step is to validate the algorithm. This step includes, all the algorithm
should be done manually by giving the required input, performs the required
steps including in the algorithm and should get the required amount of output in
a finite amount of time.
3. Finally, implement the algorithm in terms of programming language.

Order of an algorithm
The order of an algorithm is a standard notation of an algorithm that has been
developed to represent function that bound the computing time for algorithms.
It is an order notation. It is usually referred as O-notation.

Example
Problem size = 'n'

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 18


CS3401 ALGORITHMS UNIT 1 MEC

Algorithm = 'a' for problem size n


The document mechanism execution = Cn2 times
where C – constant
Then the order of the algorithm 'a' = O(n2)
where n2 = Complexity of the algorithm 'a'.
Program
 A set of explicit and unambiguous instructions expressed using a programming
languages constructs is called a program.
 An algorithm can be converted into a program, using any programming
language. Pascal, Fortran, COBOL, C and C++ are some of the
programminglanguages.

Difference between program and algorithm:

Sno Algorithm Program


1 Algorithm is finite. Program need to be finite.
2 Algorithm is written using Programs are written using a specific
natural language or algorithmic programming language.
language.

2. Explain the fundamentals of the analysis framework. Or explain time-space


trade off of the algorithm designed. April/May 2019

 Efficiency of an algorithm can be in terms of time or space.


 This systematic approach is modelled by a frame work called as analysis frame
work.

Analysis framework
o The efficiency of an algorithm can be decided by measuring the performance
of an algorithm.
o The performance of an algorithm is computed by two factors
 amount of time required by an algorithm to execute
 amount of storage required by an algorithm
Overview
(i) Space complexity
(ii) Time complexity
(iii) Measuring an Input's size
(iv) Measuring Running Time
(v)Orders of Growth

(i) Space complexity


 The space complexity can be defined as amount of memory required by an
algorithm to run.
 To compute the space complexity, we use two factors: constant and instance
characteristics.
 The space requirement S(p) can be given as S(p) = C+ S(p)
Where C is a constant i.e. fixed part and it denotes the space of inputs and
outputs.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 19


CS3401 ALGORITHMS UNIT 1 MEC

(ii) Time complexity


 The time complexity of an algorithm is the amount of computer time
required by an algorithm to run to completion.
 For instance, in multiuser system, executing time depends on many factors
such as
o System load
o Number of other programs running
o Instruction set used
o Speed underlying hardware
 The time complexity is therefore given in term of frequency count as shown
in Table 1.1
o Frequency count is a count denoting number of times of execution of
statement
Example
For (i=0; i<n; i++)
{
sum = sum + a[i];
}
Table:1.1 Time complexity of an algorithm

Statement Frequency count


i=0 1
i<n This statement executes for (n+1) times. When
conditions is true i.e. when i<n is true , the
execution happens to be n times , and the
statement execute once more when i<n is false

i++ n times
sum = sum + a[i] n times
Total 3n + 2

(iii) Measuring an Input's size

 All algorithms run longer on larger inputs.


 Ex: Sorting larger arrays, multiply larger matrices etc.
 Investigates an algorithm efficiency as a function of some parameter n indicating
the algorithm input size.
 Example:
o In problem of evaluating a polynomial p(x) = an x n + ….+ a0 of degree n,
the parameter will be the polynomial's degree or the number of its
coefficients which is larger by one than its degree.
 In spell checking algorithm,
o If algorithm examines the individual character of its input, then the size of
the input is the no. of characters.
o If the algorithm processes the word, the size of the input is the no. of

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 20


CS3401 ALGORITHMS UNIT 1 MEC

words.

(iv) Measuring Running Time

 Some units of time measurement such as a second, a millisecond and so on can


be used to measure the running time of a program implementing the algorithm.
 Drawbacks
 Dependence on the speed of a particular computer
 Dependence on the quality of a program implementing the
algorithm.
 The compiler used in generating the machine code.
 The difficulty of clocking the actual running time of the program
 Since we are in need to measure an algorithm's efficiency, we should have a
metric that does not depend on these factors.
 One possible approach is to count the number of times of the algorithm's
operations is executed. But this approach is difficult and unnecessary.
 The main objective is to identify the most important operation of the algorithm,
called the Basic Operation - the operation contributing the most to the total
running time, and compute the number of times the basic operation is executed.
 It is not so difficult to identify the basic operation of an algorithm: it is usually
the most time consuming operation in the algorithm's innermost loop.
Example
 Most sorting algorithms work by comparing the elements (keys) of a list
being sorted with each other. For such algorithms the basic operation is a
Key Comparison as shown in Table 1.2

Table 1.2 Key comparisons


Problem Input Size Basic operation
statement
Searching a key List of n elements Comparison of key with every
element from the element of list
list of n elements
Performing matrix The two matrixes with Actual multiplication of the
The multiplication order n×n elements in the matrices
formul Computing GCD of Two numbers Division
a to two numbers
comput
e the execution time using basic operation is
. T(n) ≈ Cop C(n)
Where T(n) – running time
C(n) – no. of times this operation is executed.
Cop – execution time of algorithms basic operation.

(v) Orders of Growth

 Measuring the performance of an algorithm in relation with the input size n is


called order of growth.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 21


CS3401 ALGORITHMS UNIT 1 MEC

Worst Case, Best Case and Average Case efficiencies


 It is reasonable to measure an algorithm's efficiency as a function of a
parameter indicating the size of the algorithm's input.

 But for many algorithms the running time depends not only on an input size but
also on the specifics of a particular input.

Example: Sequential Search or Linear Search AU: Dec -11, Marks 10

ALGORITHM SequentialSearch(A[0..n − 1], K)


//Searches for a given value in a given array by sequential search
//Input: An array A[0..n − 1] and a search key K
//Output: The index of the first element in A that matches K
// or −1 if there are no matching elements
i ←0
while i < n and A[i] _= K do
i ←i + 1
if i < n return i
else return -1
 This algorithm searches for a given item using some search key K in a list of 'n'
elements by checking successive elements of the list until a match with the
search key is found or the list is exhausted.
 The algorithm makes the largest number of key comparisons among all possible
inputs of size n:Cworst(n)=n

Worst case efficiency


 The worst case efficiency of an algorithm is its efficiency for the worst case input
of size n, which is an input (or inputs) of size n. For which the algorithm runs the
longest among all possible of that size.
 The way to determine the worst case efficiency of an algorithm is that:
o Analyse the algorithm to see what Kind of inputs yield the largest value of
the basic operations count C(n) among all possible inputs of size n and
then compute is w value Cworst = (n).

Best case efficiency


 The best case efficiency of an algorithm is its efficiency for the best case input of
size n, which is an input (or inputs) of. size n for which the algorithm runs the
fastest among all possible inputs of that size.
 The way to determine the best case efficiency of an algorithm is as follows.
o First, determine the kind of inputs of size n.
o Then ascertain the value of C(n) on these inputs.
 Example: For sequential search, the best case inputs will be lists of size 'n' with
their first elements equal to a search key: Cbest(n) = 1.

Average case efficiency


 It yields the necessary information about an algorithm's behaviour on a "typical"
or "random" input.
 To determine the algorithm's average case efficiency some assumptions about

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 22


CS3401 ALGORITHMS UNIT 1 MEC

possible inputs of size 'n'.


 The average number of key comparisons Cavg (n) can be computed as follows:
o In case of a successful search the probability of the first match occurring
in the position of the list is p/n for every i. and the number of
comparisons made by the algorithm in such a situation is obviously ‘i’.
o In case of an unsuccessful search, the number of comparisons is 'n' with
the probability of such a search being (1-p). Therefore,

Cavg(n)=[1. +2. +......i. +...n. +]+n.(1-p) There may be n elements at


which chances of ‘not getting
= [1+2+3+....+i+...+n]+n(1-p) element’ are possible. Hence
n . (1-p)

= +n(1-p)

Cavg(n)= + n(1-p)

Example:
o If p = 1 (i.e.) if the search is successful, then the average number of key
comparisons made by sequential search is (n+1)/2.
o If p = 0 (i.e.) if the search is unsuccessful, then the average number of key
comparisons will be 'n' because the algorithm will inspect all n elements on
all such inputs.

3. Explain the Asymptotic Notations and its properties? Or explain briefly Big oh
notation, Omega notation and Theta notation give an example (Apr/May-2017) or what
are the Rules of Manipulate Big-Oh Expression and about the typical growth rates of
algorithms? Nov/Dec 2017 Nov/Dec 2018 OR Define Big O notation, Big Omega and Big
Theta Notation. Depict the same graphically and explain. May/June 2018, Nov/Dec 2019,
Apr/May 2024 OR Explain various complexity measures and the role of asymptotic
notations toward algorithm analysis. ( Nov/Dec 2024)

Asymptotic Notations

In asymptotic notation, the complexities of an algorithm are represented only by the


most significant terms and ignore least significant terms (Here complexity is, Space Complexity
or Time Complexity).
Example,
 Algorithm 1 : 25n3 + 2n + 1
 Algorithm 2 : 1223n2 + 8n + 3
The term '2n + 1' have least significance than the term '25n 3', and the term '8n + 3' in
algorithm has least significance than the term '1223n 2'.
Definition:

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 23


CS3401 ALGORITHMS UNIT 1 MEC

Asymptotic notations are mathematical tools to represent the time and space
complexity of algorithms for asymptotic analysis.

There are mainly three asymptotic notations:


(i) Big-O Notation (O-notation)
(ii) Omega Notation (Ω-notation)
(iii) Theta Notation (Θ-notation)

(i) Big Oh notation (O)


o The big oh notation is denoted by ‘O’.
o It is a method of representing the upper bound of algorithm’s running
time.
o Using big oh notation we can give longest amount of time taken by the
algorithm to complete as shown in Fig.1.2
Definition
A function t(n) is said to be in O(g(n)) (t(n) Є O(g(n))),
if t(n) is bounded above by constant multiple of g(n) for all values of n, and if there exist
a positive constant c and non negative integer n0 such that

t(n) ≤ c*g(n) for all n ≥ n0.

Fig.1.2 Big Oh notation (O)


Example :
Consider function t(n) = 2n + 2 and g(n) = n2.
Then we have to find some constant c, so that f(n) ≤ c*g(n).
As t(n) = 2n + 2 and g(n) = n2.
Then we find c for n=1 then
t(n) = 2n + 2
= 2(1) +2
t(n) = 4
And g(n) = n2
= (1) 2
g(n) = 1
i.e t(n) > g(n)

if n = 2 then,
t(n) = 2n + 2

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 24


CS3401 ALGORITHMS UNIT 1 MEC

= 2(2) +2
t(n) = 6
And g(n) = n2
= (2) 2
g(n) = 4
i.e t(n) > g(n)

if n = 3 then,
t(n) = 2n + 2
= 2(3) +2
t(n) = 8
And g(n) = n2
= (3) 2
g(n) = 9

i.e t(n) < g(n) is true.


Hence we can conclude that for n> 2, we obtain
t(n) < g(n)
Thus always upper bound of existing time is obtained by big oh notation.

(ii) Omega Notation (Ω)


o Omega notation is denoted by ‘Ω’.
o This notation is used is to represent the lower bound of algorithm’s running
time.
o Using omega notation we can denote shortest amount of time taken by
algorithm as shown in Fig.1.3

Definition
A function t(n) is said to be in Ω(g(n)) (t(n) Є Ω(g(n))),
if t(n) is bounded below by constant multiple of g(n) for all values of n, and if there exist
a positive constant c and non negative integer n0 such that
t(n) ≥ c*g(n) for all n ≥ n0.

Fig.1.3 Omega Notation (Ω)

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 25


CS3401 ALGORITHMS UNIT 1 MEC

Example:
Consider t(n)=2n2 + 5 and g(n) = 7n
Then if n = 0
t(n) = 2 (0)2 + 5
=5
g(n) = 7(0)
= 0 i.e t(n) > g(n)
But if n = 1
t(n) = 2 (1)2 + 5
=7
g(n) = 7(1)
= 7 i.e t(n) = g(n)
But if n = 2
t(n) = 2 (2)2 + 5
= 13
g(n) = 7(2)
= 14 i.e. t(n) < g(n)
But if n = 3
t(n) = 2 (3)2 + 5
= 18 + 5
= 23
g(n) = 7(3)
= 21 i.e t(n) > g(n)
Thus for n>3 we get t(n) > c * g(n).
It can be represented as
2n2 + 5 Ω(n)

(iii) Theta Notation (Θ)


The theta notation is denoted by Θ. By this method the running time is between
upper bound and lower bound as shown in Fig.1.4
Definition
A function t(n) is said to be in Θ(g(n)) (t(n) Є Θ(g(n))),
if t(n) is bounded both above and below by constant multiple of g(n) for all values of n,
and if there exist a positive constant c1 and c2 and non negative integer n0 such that
C2*g(n) ≤ t(n) ≤ c1*g(n) for all n ≥ n0.

Fig.1.4 Theta Notation (Θ)

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 26


CS3401 ALGORITHMS UNIT 1 MEC

Example :
If t(n) = 2n + 8 and g(n) = 7n, 5n
Where n ≥ 2
C2*g(n) ≤ t(n) ≤ c1*g(n) for all n ≥ n0
Θ(g(n)) = O(g(n) ) Ω(g(n))
(t(n) Є Θ(g(n)))
Similarly t(n) = 2n + 8
g(n) = 7n
g(n) = 5n
i.e 5n < 2n + 8 < 7n for n ≥ 2
Here c2 = 5 and c1 = 7 with n0 = 2
Little oh notation(o)
The function t(n) = o(g(n)), if O(g(n)) and t(n) <> (g(n))
Example
t(n) = 3n+2
Where n>0, 3n+2 ≤ 5 n2
By definition of Big Oh
t(n) = Cg(n)
C = 5; g(n) = n2
But t(n) = 3n+2 < > (n2)
Therefore t(n) = 3n+2 = o(n2)

Properties Best case, Worst case and average case analysis

Useful property involving the Asymptotic notation:

The following property is useful in analyzing algorithms that comprise two


consecutively executed parts.
Theorem
If t1(n) Є O(g1(n)) and t2(n) Є O(g2(n)) then,
t1(n) + t2(n) Є (max {g1(n),g2(n)})
Proof
Since t1(n) Є O(g1(n)), there exist some constant C1 and some non
negative integer n1 such that
t1(n) ≤ C1 (g1(n)) for all n ≥ n1
Since
t2(n) Є O(g2(n))
t2(n) ≤ C2 (g2(n)) for all n ≥ n2

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 27


CS3401 ALGORITHMS UNIT 1 MEC

Let us denote,
C3=max {C1, C2} and

Consider n ≥ max {n1, n2}, so that both the inequalities can be used.
The addition of two inequalities becomes,

t1(n)+ t2(n) ≤ C1 (g1(n))+ C2 (g2(n))


≤ C3 (g1(n))+ C3 (g2(n))
≤ C3 2 max{g1(n), (g2(n))}
Hence,
t1(n) +t2(n) Є O (max {g1(n),g2(n)}),
with the constants C and n0 required by the definition being 2C3 = 2 max (C1, C2)
and max {n1, n2} respectively.

The property implies that the algorithms overall efficiency will be determined by
the part with a larger order of growth.
(i.e.) its least efficient part is
t1(n) Є O(g1(n)) t1(n) +t2(n) Є O (max {g1(n),g2(n)})
t2(n) Є O(g2(n))

Using limits for comparing orders of growth


There are 3 principal cases,

0, Implies that (n) has a smaller order


of growth than g(n)
C, Implies that (n) has a same order
of growth than g(n)
∞, Implies that (n) has a larger order
of growth than g(n)
L' Hospital's rule.

Stirling’s formula

n!≈ n for large values of n.

Asymptotic Growth Rate

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 28


CS3401 ALGORITHMS UNIT 1 MEC

Three notations used to compare orders of growth of an algorithm’s basic


operation count

 O(g(n)): class of functions f(n) that grow no faster than g(n)


 Ω(g(n)): class of functions f(n) that grow at least as fast as g(n)
 Θ (g(n)): class of functions f(n) that grow at same rate as g(n)

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 29


CS3401 ALGORITHMS UNIT 1 MEC

Table 1.3 Basic Asymptotic Efficiency Classes


Class Name Comments
1 Constant Short of best-case efficiencies
logn Logarithmic Cutting a problem size by a constant factor
Algorithms that scan a list of size n.(eg
n Linear
sequential search)
n logn n-log-n Many divide and conquer algorithm
Efficiency of algorithm with two embedded
n2 Quadratic
loops.
Efficiency of algorithm with three embedded
n3 Cubic
loops.
2 n Exponential Generate all the subsets of an n element set.
Algorithm that generate all permutations of an
n! Factorial
n element set
O(1) - Constant time
O(1) describes algorithms that take the same amount of time to compute regardless of the input
size. As shown in Table 1.3 For example, if a function takes the same time to process ten
elements and 1 million items, then it is O(1).
Examples:
 Find if a number is even or odd.
 Check if an item on an array is null.
O(n) - Linear time
Linear time complexity O(n) means that the algorithms take proportionally longer to complete
as the input grows. These algorithms imply that the program visits every element from the
input.
Examples
 Get the max/min value in an array.
 Find a given element in a collection.

O(n2) - Quadratic time


A function with a quadratic time complexity has a growth rate of n2. If the input is size 2, it will
do four operations. If the input is size 8, it will take 64, and so on.
Examples
 Check if a collection has duplicated values.
 Sorting using bubble sort, insertion sort, or selection sort.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 30


CS3401 ALGORITHMS UNIT 1 MEC

O(log n) - Logarithmic time


Logarithmic time complexities usually apply to algorithms that divide problems in half every
time. For example, to find a word in a book which is sorted alphabetically, there are two ways to
do it.
Method 1:
 Start on the first page of the book and go word by word until you find matching word.
Method 2:
 Open the book in the middle and check the first word on it.
 If the word you are looking for is alphabetically more significant, then look to the right.
Otherwise, look in the left half.
 Divide the remainder in half again, and repeat above step until you find matching.

Method 1 - go word by word - O(n)


Method 2 - split the problem in half for each iteration - O (log n)
Example
 Binary search.

O(n log n) - Linearithmic


Linearithmic time complexity it’s slightly slower than a linear algorithm. However, it’s still
much better than a quadratic algorithm.
Examples
 Sorting algorithms like merge sort, quicksort, and others.

O(2n) - Exponential time


Exponential (base 2) running time means the calculations performed by an algorithm double
every time as the input grows.
Examples:
 Fibonacci series generation
 Travelling salesman problem using dynamic programming

4. Explain the Mathematical analysis for recursive algorithm. (Apr/May-2017) or


Discuss the steps in Mathematical analysis for recursive algorithms. Do the same for
finding Factorial of a number. Nov/Dec 2017 or solve the following recurrence
equations using Iterative method Nov/Dec 2019 or Discuss various methods
used for mathematical Analysis of recursive algorithms. May/June 2018

Recurrence Relation
A recurrence relation is an equation that defines a sequence based on a rule that gives
the next term as a function of the previous term(s). It helps in finding the subsequent
term (next term) with the previous term. If we know the previous term in a given
series, then we can easily determine the next term.
Example 1:

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 31


CS3401 ALGORITHMS UNIT 1 MEC

 Recursive definition for the factorial function


n!=(n−1)! * n

Example 2:
Recursive definition for Fibonacci sequence
Fib(n)=Fib(n−1)+Fib(n−2)

Recurrence relations are often used to model the cost of recursive functions. For
example, the number of multiplications required by a recursive version of the factorial
function for an input of size n will be zero when n=0 or n=1 (the base cases), and it will
be one plus the cost of calling fact on a value of n−1.
General plan for analyzing efficiency of Recursive algorithms

1. Decide the input size based on parameter n .


2. Identify algorithms basic operations
3. Check how many times the basic operation is executed.
To find whether the execution of basic operation depends upon the
input size n. determine worst, average, and best case for input of size n.
if the basic operation depends upon worst case average case and best case
then that has to be analyzed separately.
4. Set up the recurrence relation with some initial condition and expressing
the basic operation.
5. Solve the recurrence or at least determine the order of growth. While
solving the recurrence we will use the forward and backward
substitution method. And then correctness of formula can be proved
with the help of mathematical induction method.

Example :Computing factorial of some number n.

To compare the factorial F(n)=n! for an arbitrary non negative integer


N! =1.2.3……(n-1).n
= (n-1)!.n ,for n>1
0! =1
By definition F(n)=F(n-1)!.n

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 32


CS3401 ALGORITHMS UNIT 1 MEC

Mathematical Analysis:
Step 1: The algorithm’s input size is n.
Step 2: The algorithm’s basic operation in computing factorial is multiplication .
Step 3 : The recursive function call can be formulated as
According to the formula, F(n) is computed as
F(n) = F(n-1) * n, for n>0
And the number of execution is denoted by M(n).
The number of multiplication M(n) is computed as
M(n) = M(n-1) + 1, for n>0
To compute To multiply
F(n-1) F(n-1) by n

M(n-1) multiplication are spent to compute F(n-1).


One more multiplication is needed to multiply the result by n.
Step 4: in step 3 the recurrence relation is obtained.
The equation is
M(n)=M(n-1) +1, for n>0

Defines M(n)not explicitly(i.e.)as a function of n, but implicitly as function of its


value at another point, namely n-1. These equations are called as recurrence
relations or recurrences.
o Recurrences relations play an important role in the analysis of algorithm
and some area of applied mathematics.
o To solve a recurrence relation M(n)=M(n-1)+1 the formula for the
sequence M(n) in terms of n only should be find.
o To determine the unique solution, an initial condition is needed that tells
the value with which the sequence starts.
o The initial value is obtained from the condition if n=0 return 1 that makes
the algorithm stops.
The condition, if n=0 return 1 tells 2 things
1. The recursive call stops when n=0 the smallest value for which the
algorithm is executed. Hence M(n)=0.
2. When n=0 the algorithm performs no multiplication

Forward Substitution:
M(1) = M(0) +1

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 33


CS3401 ALGORITHMS UNIT 1 MEC

M(2) = M(1) + 1 = 1 + 1 =2
M(3) = M(2) + 1 = 2 + 1=3
The recurrence relation and the initial condition for the algorithm
number of multiplication M(n) is
M(n)=M(n-1)+1,for n<0, M(0)=0
Backward substitution:
M(n) = M(n-1) + 1
Substitute M(n-1) = M(n-2) + 1
Now M(n) becomes
M(n) = [M(n-2)+1]+1
= M(n-2) + 2
Substitute M(n-2) =M(n-3)+1
Now M(n) becomes
M(n)=[M(n-3)+1] + 2
= M(n-3) + 3
From the substitution method we can establish a general formula as :
M(n)= M(n-i) + i;
Since n=0, substitute i=n;
Now let us prove correctness of this formula using mathematical induction as
follows
Proof
M(n) = n by using mathematical induction
Basis : let n = 0 then
M(n) =0
i.e M(0) = 0=n
Induction: if we assume M(n – 1) = n-1 then
M(n) = M( n-1) + 1
= n-1 + 1
=n
i.e M(n) = n Thus the time complexity of factorial function is Θ (n)

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 34


CS3401 ALGORITHMS UNIT 1 MEC

5. Give the general plan for Analyzing the time efficiency of Recursive Algorithms
and use recurrence to find number of moves for Towers of Hanoi problem.
May/June 2018

Example : Tower of Hanoi puzzle

o In this puzzle, there are n disks of different sizes, and three pegs.
o Initially all the disks are on the first peg in order if size, the largest on the bottom
and the smallest on the top as shown in Fig 1.4
o The goal is to move all the disks from peg 1 to peg 3 using peg 2 as auxiliary.
o One disk should be moved at a time and do not place a larger disk on top of a
smaller one.
The following steps are used to move n>1 disks from peg 1 to peg 3, peg 2 as auxiliary
as shown in Fig 1.5
1. Move n-1 disks recursively from peg 1 to peg 3.( peg 2 as auxiliary).
2. Move the largest disk directly from peg 1 to peg 3.
3. Move n-1 disks recursively from peg 2 to peg 3.( peg 2 as
auxiliary).

For example, if n=1 then the single disks is moved from source peg to destination peg
directly.
A B C

Fig 1.5 Tower of Hanoi puzzle

Fig 1.6 Tower of Hanoi puzzle

General plan to tower of Hanoi problem


The input size is the number of disks “n”.
The algorithm basic operation is moving one disks at a time.
The number of moves M(n) depends only on n.
The recurrence equation is,
M(n)=M(n-1)+1+M(n-1),for n>1;
M(n)=2M(n-1)+1, for n>1;
The initial condition M(1)=1
Now the recurrence relation for number of moves is,

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 35


CS3401 ALGORITHMS UNIT 1 MEC

M(n)=2M(n-1)+1,for n>1
M(1)=1
The recurrence relation is solved by using backward substitution method
Backward substitution Method
M(n)=2M(n-1)+1
Substitute
M(n-1)=2M(n-2)+1
M(n)=2[2M(n-2)+1]+1
M(n)=22M(n-2)+2+1
Substitute
M(n-2)=2M(n-3)+1
Now, M(n) becomes
M(n)=22[2M(n-3)+1]+2+1
M(n)=23[M(n-3)+22+2+1
Hence after I substitution M(n) becomes
M(n)=2iM(n-i)+2i-1+2i-2+2i-3+…….2+1
=2iM(n-i)+2i-1
Therefore te general formula is 2iM(n-i)+2i-1

Solution to recurrence relation is


Since the initial condition is n=1 becomes i=n-1.
The recurrence relation is
M(n)=2iM(n-i)+2i-1 ..................(1)
Substitute I=n-1 in (1)
M(n)=2n-1M(n-(n-1)+2n-1-1
=2n-1M(1)+2n-1-1
=2n-1+2n-1-1
=2n-1
M(n)= 2n-1 Thus this is an exponential algorithm, It runs unimaginably long time for
moderate values of n.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 36


CS3401 ALGORITHMS UNIT 1 MEC

Example :To find the number of binary digits in binary


representation
Algorithm BinRec(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation
if n=1
return 1
else
return BinRec([n/2])+1
Recurrence and Initial Condition
A Recurrence for the number of addition A(n) made by the algorithm is the number of
addition made in computing BinRec([n/2]) is A([n/2]) plus one more addition is made
Thus recurrence is
A(n)=A([n/2])+1,for >n
A(n)->number of addition made by the algorithm
A([n/2])->number of addition made to compute A9[n/2])
The recursive call end when n is equal to 1 and no addition is made.
The initial condition is A(1) = 0
To solve the recurrence, backward substitutions cannot be [Link] reason is the
presence on [n/2] in the functions argument and the value of n is not power of 2.
A theorem called Smoothness rule is used to solve the recurrence.
The standard approach for solving such recurrence is to solve it only for n = 2k .
The order of growth observed for n = 2 k gives a correct answer about the order of
growth of all values of n.
n = 2k takes the form
A(2k) = A(2k−1) + 1 for k > 0,
A(20) = 0.
Now, backward substitutions can be applied.
Backward Substitution Method
A(2k) = A(2k−1) + 1
substitute A(2k−1) = A(2k−2) + 1
= [A(2k−2) + 1] + 1

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 37


CS3401 ALGORITHMS UNIT 1 MEC

= A(2k−2) + 2
substitute A(2k−2) = A(2k−3) + 1
= [A(2k−3) + 1] + 2
= A(2k−3) + 3 ... ...
After i iteration
A(2k) = A(2k−i) + i
= A(2k−k) + k
= A(20) + k
= A(1) + k
Thus, we end up with
A(2k) = A(1) + k = k
After returning to the original variable
n = 2k and hence k = log2 n,
A(n) = log2 n ∈ Ө(log n)
Example : Fibonacci series
A sequence of Fibonacci numbers is 0,1,1,2,3,5,8,13,21,34………..
The Fibonacci sequence can be defined by the simple recurrence
F(n)=F(n-1)+F(n-2),for n>1…………………1
The two initial conditions are

F(0)=0
F(1)=1
Explicit formula for the nth Fibonacci number
Backward substitution method is not used to solve the recurrence F(n)=F(n-1)+F(n-
2),for n>1,because which fails to produce easily discernible pattern.
So, the theorem that describes solution to a homogeneous second order linear
recurrence with constant coefficient is used to solve the problem.
The homogenous with constant coefficient is
ax(n)+bx(n-1)+cx(n-2)=0 ……………(2)
Where,
a,b,c are fixed real numbers called the coefficients of recurrence and a≠0
x(n) is the unknown sequence to be found

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 38


CS3401 ALGORITHMS UNIT 1 MEC

The characteristics equation of the recurrence equation is


Ar2+br+c=0 ………………….(3)
The recurrence relation can be written as
F(n)-F(n-1)-F(n-2)=0 ………….(4)
The characteristics equation for (4)
r2-r-1=0
The roots are

R1,2=

R1,2=

R1=

R2=
The characteristics equation has two distinct real roots.
Now the recurrence relation is
X(n)=αr1n+βr2n ……..(5)
Substitute r1 and r2 in (5),

F(n)= α( )n+β( )n ……….(6)


Now substitute the value of f(0) and F(1) in equation(6)

F(0) = α( )0+β( )0 =0 ……….(7)

F(1)= α( )1+β( )1 =0 ……….(8)


By solving equation (7) and (8),the linear equation in two unknown α and β
α+ β=0 ----------(9)

α( )+β( )=0

( ) β-( ) β=-1

+ β- + β = -1

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 39


CS3401 ALGORITHMS UNIT 1 MEC

β = -1

β=-

Substitute β = - in (9)

α+β=0
α- =0

α= β=-

Substitute the value of α and β in equation (6)

F(n) = n- n

F(n) =

Where

Φ=

Φ = 1.61803
Φ^ =-

Φ^ = - 0.61803
The constant Φ is known as, Golden Ratio.
The value of Φ^ is lies between -1 and 0.
When n goes to infinity, Φ^ gets infinitely small value. So, it can be omitted.
Therefore F(n) = Φn

So, for every non negative n, F(n) = Φ n is rounded to the nearest integer.

Algorithm for computing Fibonacci numbers


First method
Algorithm F(n)
//Computes the nth Fibonacci number recursively by using its definition.
//Input: A nonnegative integer n
//Output: The nth Fibonacci number
if n<1

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 40


CS3401 ALGORITHMS UNIT 1 MEC

return n
Else
return F(n-1)+(n-2)
the algorithm’s basic operation is addition.
Let A(n) is the number of additions performed by the algorithm to compute F(n).
The number of additions needed to compute F(n-1) is A(n-1) and the number of
additions needed to compute F(n-2) is A(n-2).
The algorithm needs one more addition to compute the sum of A(n-1) and A(n-2).
Thus the recurrence for A(n) is
A(n)=A(n-1) + A(n-2)+1, for n>1
A(0)=0
A(1)=0
The recurrence A(n)-A(n-1)-A(n-2)=1 is same as F(n)-F(n-1)-F(n-2)=0, but its right
hand side not equal to zero. These recurrences are called inhomogeneous
recurrences.
General techniques are used to solve inhomogeneous recurrences.
The inhomogeneous recurrences is converted into homogeneous recurrence by
rewriting the in homogeneous recurrence as,
A(n)+1]-[A(n-1)+1]-[A(n-2)+1]=0
Now substitute, B(n)=A(n)+1
Now (14) becomes, B(n)-B(n-1)-B(n-2)=0
B(0)=0
B(1)=1
Here B(n)=F(n+1)
Since B(n)=A(n)+1
B(n-1)=A(n)
So A(n)=B(n)-1
Substitute F(n+1)-1
We know that

F(n)=

F(n+1)=

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 41


CS3401 ALGORITHMS UNIT 1 MEC

Substitute (16) in (15)

A(n)= -1
Hence

A(n)€
The poor efficiency class of algorithm could be anticipated from the class of recurrence
The reason behind the algorithm inefficiency can be traced by looking at the tree of
recursive calls n=6
The same values of the function are evaluated again and again which is extremely
inefficiently.

6. Solve the following recurrence relations: or solve the following recurrence


equation:

(i) T(n)=T(n/2) +1, where n=2k for all k>=0


(ii) T(n)= T(n/3) + T(2n/3) +cn,
where ‘c’ is a constant and ‘n’ is the input size. Dec 2012 April/May 2019,
Apr/May 2024

1. T(n)= 2T(n/2)+3 n>2


2 n=2

T(n)=2T(n/2)+3
=2{(2T(n/2)+3)/2}+3
=2{(2T(n/4)+3/2}+3
....
=4T(n/4)+6
= 4{(2T(n/2)+3)/4}+6
.....
=8T(n/8)+9
----
=2kT(n/2k)+3n
T(n)=nlogn+3n Time complexity=o(nlog n)

2. T(n)= 2T(n/2)+cn n>1


a n=1 where a and c constants

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 42


CS3401 ALGORITHMS UNIT 1 MEC

T(n)=2T(n/2)+cn
=2{(2T(n/2)+cn)/2}+cn
=2{(2T(n/4)+cn/2}+cn
----
=4T(n/4)+cn+cn
= 4{(2T(n/8)+cn/4}+ cn+cn
------
=8T(n/8)+ cn+cn+cn
---
=2kT(n/2k)+k(cn)
T(n)=nlogn+ k(cn)
Time complexity=o(nlog n)

7. Show the following equalities are correct. June 2013, Nov 2010

i. 5n2-6n = Φ(n2)
ii. n!=O(nn)
iii. n3+106n2=Θ(n3)
iv. 2n22n + n log n = Θ(n22n)

i. 5n2-6n = Φ(n2) =>higest order of grouth is n2


ii. n!=O(nn) =>higest order of grouth O(n)
iii. n3+106n2=Θ(n3) =>higest order of grouth O(n3)
iv. 2n22n + n log n = Θ(n22n) =>higest order of grouth O(n2)

8. Prove that for any two functions f(n) and g(n), we have f(n)-> Θ(g(n))
if and only if f(n) -> O(g(n)) and f(n) ->Ω(g(n)) Nov 2010
Given function:
f(n) and g(n)
f(n)= O(g(n)) when f(n) ≤C1g(n) for all n≥n0---------(1)
f(n)= Ω(g(n)) when f(n) ≥C2g(n) for all n≥n0---------(2)
from (1) and (2)
C2 g(n) ≤f(n) ≤ C1g(n) for all n≥n0 -------(3)
(i.e) Θ(g(n)) = O(g(n)) Ω(g(n))
From (3) f(n) = Θ(g(n)) hence proved

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 43


CS3401 ALGORITHMS UNIT 1 MEC

9. Derive the worst case analysis of merge sort using suitable illustration (AU april
2015)
Efficiency of Merge Sort:

 In merge sort algorithm the two recursive calls are made. Each recursive call
focuses on n/2 elements of the list .
 After two recursive calls one call is made to combine two sublist i.e to merge all
n elements.
 Hence we can write recurrence relation as
T(n) = T(n/2) + T(n/2) + cn
T(n/2) = Time taken by left sublist
T(n/2) = time taken by right sublist
T(n) = time taken for combining two sublists
where n> 1 T (1) = 0
The time complexity of merge sort can be calculated using two methods
 Master theorem
 Substitution method
Master theorem
Let , the recurrence relation for merge sort is
T(n) = T(n/2) + T(n/2) + cn
Let T(n) = aT(n/b) + f(n) be a recurrence relation
i.e. T(n) = 2T(n/2) + cn ------- ( 1 )
T(1) = 0 ----------- (2 )
As per master theorem T(n) = Θ (n d long n ) if a = b
As equation ( 1),a =2 , b = 2 and f(n) = cn and a = bd i.e 2 = 2`
This case gives us , T (n) =Θ (n log2 n)
Hence the average and worst case time complexity of merge sort is
C worst (n) = (n log2 n)
Substitution method Let, the recurrence relation for merge sort be
T(n) = T(n/2) + T(n/2) + cn for n>1
i.e. T(n) = 2T(n/2) + cn for n>1 ------- (3)
T(1) = 0 -------(4)
Let us apply substitution on equation ( 3) .
Assume n=2k

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 44


CS3401 ALGORITHMS UNIT 1 MEC

T(n) = 2T(n/2) + cn
T(n) = 2T(2k/2 ) + c.2k
T(2k) = 2T(2k-1) + c.2k
If k = k-1 then,
T(2k) = 2T(2k-1) + c.2k
T(2k) = 2[2T(2k-2) + c.2k -1] + c.2k
T(2k) = 22 T(2k-2) + 2.c.2k -1 + c .2k
T(2k) = 22 T(2k-2) + 2.c.2k /2 + c.2k
T(2k) = 22 T(2k-2) + c.2k + c.2k
T(2k) = 22 T(2k-2) + 2c .2k
Similarly we can write,
T(2k) = 23 T(2k-3) + 3c .2k
T(2k) = 24 T(2k-4) + 4c .2k
…..
….
T(2k) = 2k T(2k-k) + k.c.2k
T(2k) = 2k T(20) + k.c.2k
T(2k) = 2k T(1) + k.c.2k -------- (5)
But as per equation (4), T(1) =0
There equation (5) becomes ,
T(2k) = 2k .0 +. k. c . 2k
T(2k) = k. c . 2k
But we assumed n=2k , taking logarithm on both sides.i.e. log 2 n = k
Therefore T(n) = log 2 n. cn
Therefore T (n) =Θ (n log2 n)
Hence the average and worst case time complexity of merge sort is
C worst (n) = (n log2 n)
Time complexity of merge sort
Best case Average case Worst case
Θ (n log2 n) Θ (n log2 n) Θ (n log2 n)

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 45


CS3401 ALGORITHMS UNIT 1 MEC

[Link] the most appropriate notation to indicate the time efficiency class of sequential
search algorithm in the worst case,best case and the average case.

Solution : Sequential search


“Given a target value and a random list of values, find the location of the target in
the list, if it occurs, by checking each value in the list in turn”

get (NameList, PhoneList, Name)


i=1
N = length(NameList)
Found = FALSE
while ( (not Found) and (i <= N) ) {
if ( Name == NameList[i] ) {
print (Name, “’s phone number is ”, PhoneList[i])
Found = TRUE
}
i = i+1
}
if ( not Found ) { print (Name, “’s phone number not found!”) }

Central unit of work: operations that occur most frequently

Central unit of work in sequential search:


Comparison of target Name to each name in the list
Also add 1 to i

Typical iteration: two steps (one comparison, one addition)


Given a large input list:
Best case: smallest amount of work algorithm must do
Worst case: greatest amount of work algorithm must do
Average case: depends on likelihood of different scenarios occurring

 Best case: target found with the first comparison (1 iteration)


 Worst case: target never found or last value (N iterations)
 Average case: if each value is equally likely to be searched, work done varies
from 1 to N, on average N/2 iterations

11.(i) Prove that if g(n) is Ω(f(n)) then f(n) is O(g(n)).May/June 2018

f(n) ∈ Ω(g(n)) ⟺ g(n) ∈ O(f(n))


Proof:
O(f(n))={g:N→N | ∃c,n0∈N ∀n≥n0:g(n)≤c⋅f(n)}

Ω(g(n))={f:N→N | ∃c,n0∈N ∀n≥n0:f(n)≥c⋅g(n)}

Step 1/2: f(n) ∈ Ω(g(n)) ⟺ g(n) ∈ O(f(n))

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 46


CS3401 ALGORITHMS UNIT 1 MEC

∃c,n0∈N ∀n≥n0: f(n)≥c⋅g(n)⇒f(n)g(n)≥c⇒1g(n)≥cf(n)⇒g(n)≤1c⋅f(n)

And this is exactly the definition of O(f(n)).

Step 2/2: f(n)∈Ω(g(n))⇐g(n)∈O(f(n))

∃c,n0∈N ∀n≥n0: g(n)≤c⋅f(n)⇒...⇒f(n)≥1c⋅g(n)

Hence proved.

12. Explain the Mathematical analysis for non-recursive algorithm or write an


algorithm for determining the uniqueness of an array. Determine the time
complexity of your algorithm. (Apr/May-2017) April/May 2019

General plan for analyzing efficiency of non-recursive algorithm

1. Decide the input size based on parameter n.


2. Identify the algorithm basic operation(s).
3. Check whether the number of times the basic operation is executed depends on
only on
the size of the input.
4. Set up a sum expressing the number of times the algorithm basic operation is excited
5. Simplify the sum using standard formula and rules

Example 1: Problem for finding the value of the largest element in a


list of n numbers
The pseudo code to solving the problem is

ALGORITHM MaxElement(A[0..n-1])
//Problem Description : This algorithm is for finding the
//maximum value element from the array
//Input:An array A[0..n-1] of real numbers
//Output: Returns the largest element from array
Maxval ← A[0]
For i ← 1 to n-1 do Searching the maximum element from an array
{
If ( A[i]>max_value)then
Maxval ← A[i] If any value is large than current
} Max_ Value then set new Max_value
Return Max_value by obtained larger value
Mathematical Analysis
Step 1: The input size is the number of elements in the array(ie.),n
Step 2 : The basic operation is comparison in loop for finding larger value There are
two
operations in the for loop
 Comparison operation a[i]->maxval

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 47


CS3401 ALGORITHMS UNIT 1 MEC

 Assignment operation maxval->a[i]


Step 3: The comparison is executed on each repetition of the loop. As the
comparison is made for each value of n there is no need to find best case
worst case and average case analysis.
Step 4: Let C(n) be the number of times the comparison is executed.
The algorithm makes comparison each time the loop executes.
That means with each new value of I the comparison is made.
Hence for i= 1 to n – 1 times the comparison is made . therefore we can
formulate C(n) as
C(n) = one comparison made for each value of i
Step 5 : let us simplify the sum
Thus C(n) =
=n-1 θ (n)

Using the rule θ (n)


The frequently used two basic rules of sum manipulation are,
i=C i R1

i+bi)= I + i R2

The two summation formulas are


1. =u-l+1

Where l≤ u are some lower and upper integer limits S1

2. = =1+2+…..+n
=n(n+1)/2
=1/2n2 o(n2) S2

Example 2: Element uniqueness problem-check whether all the


element in the list are distinct April/May 2019

ALGORITHM UniqueElements(A[0..n-1])
//Checks whether all the elements in a given array are distinct
//Input :An array A[0..n-1]
//Output Returns ‘true’ if all elements in A are distinct and ‘false’
//otherwise
for i  to n-2 do
for j i+1 to n-1 do If any two elements in the array
if a[i] = a[j] then are similar then return .false
return false indicating that the array elements
else are not distinct
return true
Mathematical analysis
Step 1: Input size is n i.e total number of elements in the array A
Step 2: The basic iteration will be comparison of two elements . this
operation the innermost operation in the loop . Hence
if a[i] = a[j] then comparison will be the basic operation .

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 48


CS3401 ALGORITHMS UNIT 1 MEC

Step 3 : The number of comparisons made will depend upon the input n .
but the algorithm will have worst case complexity if the same
element is located at the end of the list. Hence the basic operation
depends upon the input n and worst case

Worst case investigation

Step 4: The worst case input is an array for which the number od elements
comparison cworst(n) is the largest among the size of the array.
There are two kinds of worst case inputs, They are
[Link] with no equal elements.
[Link] in which the last two elements are pair of equal elements.
For the above inputs, one comparison is made for each repetition of the inter most
loop (ie) for each value of the loop's variable 'j' between its limits i+1 and n-1 and
this is repeated limit for each values of the outer loop (ie) for each value of the
loop's variable `i' between 0 and n-2. Accordingly,

C worst (n) = Outer loop × Inner loop

Cworst(n) =

Step 5: now we will simplify C worst as follows

= Θ

= -

Now taking (n-1) as a common factor, we can write


This can be obtained using
formula /2

This can be obtained using formula

= (n-1)

Solving this equation we will get

= 2( n-1) (n-1) – (n-2) (n-1)/2


= ( 2(n 2 – 2n + 1) – (n 2- 3n + 2)) /2

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 49


CS3401 ALGORITHMS UNIT 1 MEC

= (( n2 – n) / 2
=1/2 n2  Θ (n2)

We can say that in the worst case the algorithm needs to compare all
n (n – 1 )/2 distinct of its n elements.

Therefore C worst(n)= 1/2n2 € o(n2)

13. Explain in detail the various Searching techniques with an example. (APR/MAY
2023), Apr/May 2024 or Describe Binary search and interpolation search algorithm
with an [Link] its respective complexity measures.( Nov/Dec 2024)

Searching is a technique that helps to find whether the given element is


present in the set of elements. Any search is said to be successful or unsuccessful
depending upon whether the element that is being searched is found or not. Some of the
standards searching techniques are:
(i)Linear Search or Sequential Search
(ii)Binary Search
(iii)Interpolation Search

(i) Linear Search:


It is one of the most simple and straightforward search algorithms. In this, you
need to traverse the entire list and compare the current element with the target
element. If a match is found, you can stop the search else continue as shown in
Fig 1.6
Linear search is implemented using following steps
Step 1: Read the search element from the user
Step 2: Compare, the search element with the first element in the array.
Step 3: If both are matched, then display "Given element found!!!" and terminate
the program as shown in Fig 1.7
Step 4: If both are not matched, then compare search element with the next
element in the array.
Step 5: Repeat steps 3 and 4 until the search element is compared with the last
element in the array.
Step 6: If the last element in the array is also not matched, then display "Element
not found!!!" and terminate the function.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 50


CS3401 ALGORITHMS UNIT 1 MEC

Example

Fig 1.6 Linear Search


Given the array of elements: 59,58,96,78,23 and the element to be searched is 96, the
working of linear search is as follows:

Fig 1.7 The Element is FOUND. Hence stop the searching process.

defLinearSearch(mylist, n, k):
for j in range(0, n):
if (mylist[j] == k):
return j
return -1

mylist = [1, 3, 5, 7, 9]
print("Given Elements : ", mylist)

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 51


CS3401 ALGORITHMS UNIT 1 MEC

k = int(input("Enter the element to be searched : "))


n = len(mylist)
result = LinearSearch(mylist, n, k)
if(result == -1):
print("Element not found")
else:
print("Element found at index: ", result)

Execution:
Input
Given Elements : [1, 3, 5, 7, 9]
Enter the element to be searched : 3
Output
Element found at index: 1

Complexity Analysis of Linear Search


Time Complexity
 Best case - O(1)
The best case occurs when the target element is found at the beginning of the
list/array. Since only one comparison is made, the time complexity is O(1).
Example :
Array A[] = {3,4,0,9,8} &Target element = 3
Here, the target is found at A[0].

 Worst-case - O(n), where n is the size of the list/array.


The worst-case occurs when the target element is found at the end of the list or
is not present in the list/array. Since you need to traverse the entire list, the time
complexity is O(n), as n comparisons are needed.

 Average case - O(n)


The average case complexity of the linear search is also O(n).

Space Complexity
 The space complexity of the linear search is O(1), as we don't need any auxiliary
space for the algorithm.

(ii) Binary search


Binary search is a searching algorithm which works efficiently on sorted elements. It
uses divide and conquers method in which we compare the target element with the
middle element of the list.
If they are equal, then it implies that the target is found at the middle position; else, we
reduce the search space by half, i.e. apply binary search on either of the left and right

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 52


CS3401 ALGORITHMS UNIT 1 MEC

halves of the list depending upon whether target<middle element or target>middle


element.
We continue this until a match is found or the size of the array reaches 1.

Binary search is implemented using following steps:


Step 1: Read the search element from the user
Step 2: Find the middle element in the sorted array
Step 3: Compare, the search element with the middle element in the sorted array.
Step 4: If both are matched, then display "Given element found!!!" and terminate the
function
Step 5: If both are not matched, then check whether the search element is smaller or
larger than middle element.
Step 6: If the search element is smaller than middle element, then repeat steps 2, 3, 4
and 5 for the left sub array of the middle element.
Step 7: If the search element is larger than middle element, then repeat steps 2, 3, 4 and
5 for the right sub array of the middle element.
Step 8: Repeat the same process until we find the search element in the array or until
the sub array contains only one element.
Step 9: If that element also doesn't match with the search element, then display
"Element not found in the array!!!" and terminate the function as shown in fig 1.8
/* Program to search the given element in the list of items using
Binary Search using Iterative approach */
Method 1 – Iterative approach:
Given an array of elements: 6, 12, 17, 323, 38, 45, 77, 84, 90
The element to be searched: 45
Formula for calculating middle is,

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 53


CS3401 ALGORITHMS UNIT 1 MEC

Fig 1.8 :The Element is FOUND. Hence stop the searching process.

Def mybinarySearch(myarray, x, low, high):


# Binary Search using Iterative approach
while low <= high:
mid = low + (high - low)//2
if myarray[mid] == x:
return mid
elifmyarray[mid] < x:
low = mid + 1
else:
high = mid - 1
return -1

myarray = [3, 4, 5, 6, 7, 8, 9]
print("Elements in the array: " , myarray)
x = int(input("Enter the element to be searched : "))
result = mybinarySearch(myarray, x, 0, len(myarray)-1)

if result != -1:
print("Element is present at index :" + str(result))

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 54


CS3401 ALGORITHMS UNIT 1 MEC

else:
print("Element not found ")

Execution:
Input
Elements in the array: [33, 44, 55, 66, 77, 88, 99]
Enter the element to be searched: 66
Output
Element is present at index :3

/* Program to search the given element in the list of items using


Binary Search using Recursive approach */

Method 2 – Recursive approach:


Method 2 is the recursive approach. In the recursive approach the function calls itself again
and again. We declared a recursive function and its base condition. The condition is the
lowest value is smaller or equal to the highest value. We calculate the middle number as in
the last program.
We have used if statement to proceed with the binary search.
 If the middle value equal to the number that we are looking for, the middle
value is returned.
 If the middle value is less than the value, we are looking then our recursive
function binary search () again and increase the mid value by one and assign to low.
 If the middle value is greater than the value we are looking then our recursive
function binary search() again and decrease the mid value by one and assign it to
low.

Program:
Def mybinary_search(myarr, low, high, x):
if high >= low:
mid = (high + low) // 2
if myarr[mid] == x:
return mid
# If element is smaller than mid, then it can only
# be present in left subarray
elifmyarr[mid] > x:
return mybinary_search(myarr, low, mid - 1, x)
# Else the element can only be present in right subarray
else:
return mybinary_search(myarr, mid + 1, high, x)
else:
# Element is not present in the array
return -1

# Test data
myarr = [ 2, 3, 4, 10, 40 ]
print("Elements in the array :", myarr)

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 55


CS3401 ALGORITHMS UNIT 1 MEC

x = int(input("Enter the element to be searched : "))

# Function call
result = mybinary_search(myarr, 0, len(myarr)-1, x)

if result != -1:
print("Element is present at index : ", str(result))
else:
print("Element is not present in array")

Execution:
Input
Elements in the array : [2, 3, 4, 10, 40]
Enter the element to be searched : 10
Output
Element is present at index : 3

Complexity Analysis of Binary Search


Time Complexity
 Best case - O(1)
The best case occurs when the target element is found in the middle of list/array.
Since only one comparison is made, the time complexity is O(1).
 Worst-case - O(logn)
The worst occurs when the algorithm keeps on searching for the target element
until the size of the array reduces to 1. Since the number of comparisons
required is logn, the time complexity is O(logn).
 Average case - O(logn)
Binary search has an average-case complexity of O(logn).
Space Complexity
 Since no extra space is needed, the space complexity of the binary search is O(1).

(iii) Interpolation Search


 The interpolation search is basically an improved version of the binary
search. This searching algorithm resembles the method by which one
might search a telephone book for a name.
 It performs very efficiently when there are uniformly distributed
elements in the sorted list. In a binary search, we always start searching
from the middle of the list, whereas in the interpolation search we
determine the starting position depending on the item to be searched.
 In the interpolation search algorithm, the starting search position is most
likely to be the closest to the start or end of the list depending on the
search item. If the search item is near to the first element in the list, then
the starting search position is likely to be near the start of the list as
shown in fig 1.9

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 56


CS3401 ALGORITHMS UNIT 1 MEC

Important points on Interpolation Search


 Interpolation search is an improvement over binary search.
 Binary Search always checks the value at middle index. But, interpolation search
may check at different locations based on the value of element being searched.
 For interpolation search to work efficiently the array elements/data should be sorted
and uniformly distributed.

Interpolation search is implemented using following steps:


Step 1: Let A - Array of elements, e - element to be searched, pos - current position
Step 2: Assign start = 0 & end = n-1
Step 3: Calculate position ( pos ) to start searching by using formula:

Step 4: If A[pos] == e , element found at index pos.


Step 5: Otherwise if e > A[pos] we make start = pos + 1
Step 6: Else if e < A[pos] we make end = pos -1
Step 7: Do steps 3, 4, 5, 6.
While : start <= end && e >= A[start] && e =< A[end]
 start<= end is checked until we have elements in the sub-array.
 e >= A[start] is done when the element we are looking for is greater than or equal
to the starting element of sub-array we are looking in.
 e =< A[end] is done when the element we are looking for is less than or equal to the last
element of sub-array we are looking in.

/*Program to search the given element in the list of items


using Interpolation Search */

Example: Element to be searched = 4.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 57


CS3401 ALGORITHMS UNIT 1 MEC

Fig 1.9 Interpolation Search


Program
Def interpolationSearch(arr, lo, hi, x):
if (lo <= hi and x >= arr[lo] and x <= arr[hi]):
pos = lo + ((hi-lo)//(arr[hi]-arr[lo])*(x - arr[lo]))
if arr[pos] == x:
return pos
if arr[pos] < x:
return interpolationSearch(arr, pos + 1, hi, x)
if arr[pos] > x:
return interpolationSearch(arr, lo, pos - 1, x)
return -1

arr = [10, 12, 13, 16, 18, 19, 20,


21, 22, 23, 24, 33, 35, 42, 47]
print("Elements in the array :", arr)
x = int(input("Enter the element to be searched : "))

n = len(arr)
index = interpolationSearch(arr, 0, n - 1, x)

if index != -1:
print("Element found at index", index)
else:
print("Element not found")

Execution:
Input
Elements in the array : [10, 12, 13, 16, 18, 19, 20, 21, 22,
23, 24, 33, 35, 42, 47]
Enter the element to be searched : 20
Output
Element found at index 6

Complexity Analysisof InterpolationSearch


Time Complexity
 Best case - O(1)
The best-case occurs when the target is found exactly as the first expected
position computed using the formula. As we only perform one comparison, the
time complexity is O(1).

 Worst-case - O(n)
The worst case occurs when the given data set is exponentially distributed.

 Average case - O(log(log(n)))

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 58


CS3401 ALGORITHMS UNIT 1 MEC

If the data set is sorted and uniformly distributed, then it takes O(log(log(n)))
time as on an average (log(log(n))) comparisons are made as shown in Table
1.5 Space Complexity
 Since no extra space is needed, the space complexity of the interpolation search
is O(1).

Table 1.5 Comparative analysis:

Time Complexity Space


Algorithm
Best case Worst-case Average case Complexity
Linear Search O(1) O(n) O(n) O(1)
Binary Search O(1) O(logn) O(logn) O(1)
Interpolation Search O(1) O(n) O(log(log(n))) O(1)

14. Explain in detail the Pattern Search or String Searching Algorithms with Various
string matching algorithms. (APR/MAY 2023), Apr/May 2024

The Pattern Searching algorithms are sometimes also referred to as String Searching
Algorithms. These algorithms are useful in the case of searching a pattern in a string.

Algorithms used for String Matching:

Various string matching algorithms are:

 The Naive String Matching Algorithm


 The Rabin-Karp-Algorithm
 Finite Automata
 The Knuth-Morris-Pratt Algorithm
 The Boyer-Moore Algorithm

Algorithms based on character comparison


Naive Match Algorithm:
It slides the pattern over text one by one and checks for a match. If a match is
found, then slides by 1 again to check for subsequent matches.

KMP (Knuth Morris Pratt) Algorithm:


KMP algorithm is used to find a "Pattern" in a "Text". This algorithm compares
character by character from left to right. But whenever a mismatch occurs, it uses a pre-
processed table called "Prefix Table" to skip characters comparison while matching.

Algorithms based on Hashing Technique b


Rabin Karp Algorithm:
It matches the hash value of the pattern with the hash value of current substring
of text, and if the hash values match then only it starts matching individual characters.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 59


CS3401 ALGORITHMS UNIT 1 MEC

(i) Naive Match Algorithm:


 This is simple and efficient brute force approach. It compares the first character
of pattern with given string. If a match is found, pointers in both strings are
advanced.
 If a match is not found, the pointer to text is incremented and pointer of the
pattern is reset. This process is repeated till the end of the text. The naïve
approach does not require any pre-processing as shown in fig 1.9
 Given a text array, T [1.....n], of n character and a pattern array, P [1......m], of m
characters. The algorithms are to find an integer s, called valid shift
where 0 ≤ s < n-m.
 In other words, we need to find, if P is in T, i.e., where P is a substring of T. The
item of P and T are character drawn from some finite alphabet such as {0, 1} or
{A, B .....Z, a, b..... z}.
Steps:
1. n ← length [T]
2. m ← length [P]
3. for s ← 0 to n -m
4. do if P [1.....m] = T [s + 1....s + m]
5. then print "Pattern occurs with shift" s

Input:
string = “This is my class room”
pattern = “class”
Output:
Pattern found at index 11
Input:
string = “AABAACAADAABAABA”
pattern = = “AABA”
Output:
Pattern found at index 0
Pattern found at index 9
Pattern found at index 12

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 60


CS3401 ALGORITHMS UNIT 1 MEC

Fig 1.9: Working of Naïve Pattern matching algorithm

/* Program to search the pattern in the given string using Naïve


Match algorithm */

def naïve_algorithm(string, pattern):


n = len(string)
m = len(pattern)
if m > n:
print("Pattern not found")
return
\
for i in range(n - m + 1):
j = 0
while j < m:
if string[i + j] != pattern[j]:
break
j += 1
if j == m:
print("Pattern found at index: ", i)

string = "hellohihello"
print("Given String : ", string)
pattern = input("Enter the pattern to be searched :")
naïve_algorithm(string, pattern)

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 61


CS3401 ALGORITHMS UNIT 1 MEC

Execution:
Input
Given String : hellohihello
Enter the pattern to be searched :hi
Output
Pattern found at index: 5

Complexity Analysis of Naïve Match


Time Complexity
 Best Case Complexity- O(n).
Best case complexity occurs when the first character of the pattern is present in
string.
String = “HIHELLOHIHELLO”
Pattern = “ HI”
The number of comparisons in best case is O(n).

 Worst Case Complexity - O(m*(n-m+1)).


Worst case complexity of Naive Pattern Searching occurs in following cases.
Case 1: When all the characters of the string and pattern are same.
String = “HHHHHHHHHHHH”
Pattern = “ HHH”

Case 2: When only the last character is different.


String = “HHHHHHHHHHHM”
Pattern = “ HHM”
The number of comparisons in the worst case is O(m*(n-m+1)).

Space Complexity
 Since no extra space is needed, the space complexity of the naïve search is O(1).

Merits & Demerits:


Advantages:
 The comparison of the pattern with the given string can be done in any order
 No extra space required
 Since it doesn’t require the pre-processing phase, as the running time is equal to
matching time

Disadvantage:
 Naive method is inefficient because information from a shift is not used again.

(ii) Rabin Karp Algorithm:


Rabin-Karp algorithm is an algorithm used for searching/matching patterns in the text
using a hash function. Unlike Naive string matching algorithm, it does not travel through

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 62


CS3401 ALGORITHMS UNIT 1 MEC

every character in the initial phase rather it filters the characters that do not match and then
performs the comparison as shown in Fig 1.10
 Initially calculate the hash value of the pattern.
 Start iterating from the starting of the string:
o Calculate the hash value of the current substring having length m.
o If the hash value of the current substring and the pattern are same, check if the
substring is same as the pattern.
o If they are same, store the starting index as a valid answer. Otherwise, continue
for the next substrings.
 Return the starting indices as the required answer.

Hash(acad) = 1466 Hash(acad) = 1466


Hash(abra) = 1493 Hash(brac) = 1533
Hash(acad) ≠ Hash(abra) Hash(acad) ≠ Hash(brac)
Hence, it is mismatch Hence, it is mismatch

Hash(acad) = 1466 Hash(acad) = 1466


Hash(raca) = 1595 Hash(acad) = 1466
Hash(acad) ≠ Hash(raca) Hash(acad) ≠ Hash(acad)
Hence, it is mismatch Match found at index 3
Fig:1.10 Rabin Karp Procedure
Steps in Rabin-Karp Algorithm:
Step 1:
 Take the input string and the pattern, which we want to match.
Given string: Pattern
A B C C D D A E F G C D D

Step 2:
 Here, we have taken first ten alphabets only (i.e. A to J) and given the weights.
A B C D E F G H I J
1 2 3 4 5 6 7 8 9 10
Step 3:
n Length of the text
mLength of the pattern
Here, n = 10 and m = 3.
d Number of characters in the input set.
Here, we have taken input set {A, B, C, ..., J}. So, d = 10.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 63


CS3401 ALGORITHMS UNIT 1 MEC

Note: we can assume any suitable value for d.


Step 4:
 Calculate the hash value of the pattern (CDD)
hash value for pattern(p) = Σ(v * d m-1) mod 13
= ((3 * 102) + (4 * 101) + (4 * 100)) mod 13
= 344 mod 13
=6
In the calculation above, choose a prime number (here, 13) in such a way that we can
perform all the calculations with single-precision arithmetic.
 Now calculate the hash value for the first window (ABC)
hash value for text(t) = Σ(v * dm-1) mod 13
= ((1 * 102) + (2 * 101) + (3 * 100)) mod 13
= 123 mod 13
=6
 Compare the hash value of the pattern with the hash value of the text. If they match
then, character-matching is performed. In the above examples, the hash value of the first
window (i.e. text) matches with pattern, so go for character matching between ABC and
CDD. Since they do not match so, go for the next window.

Step 5:
 We calculate the hash value of the next window by subtracting the first term and adding
the next term as shown below.
 Simple Numerical example:
o Pattern length is 3 and string is “23456”
o Let us assume that we computed the value of the first window as 234.
o How to compute the value of the next window “345”?
It’s just (234 – 2*100)*10 + 5 and we get 345.

hash value for text(t) = ((1 * 102) + ((2 * 101) + (3 * 100) - (1 * 102)) * 10 + (3 * 100)) mod 13
= 233 mod 13
= 12
For BCC, t = 12 (≠6). Therefore, go for the next window.
After a few searches, we will get the match for the window CDA in the text.

/* Program to search the pattern in the given string using Rabin-


Karp algorithm */

d = 10
def search(pattern, text, q):
m = len(pattern)
n = len(text)
p = 0
t = 0
h = 1

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 64


CS3401 ALGORITHMS UNIT 1 MEC

i = 0
j = 0

for i in range(m-1):
h = (h*d) % q

# Calculate hash value for pattern and text


for i in range(m):
p = (d*p + ord(pattern[i])) % q
t = (d*t + ord(text[i])) % q

# Find the match


for i in range(n-m+1):
if p == t:
for j in range(m):
if text[i+j] != pattern[j]:
break

j += 1
if j == m:
print("Pattern is found at position: " + str(i+1))

if i< n-m:
t = (d*(t-ord(text[i])*h) + ord(text[i+m])) % q

if t < 0:
t = t+q

text = "hihellohi"
print("Given String : ", text)
pattern = input("Enter the pattern to be searched :")
q = int(input("Enter the prime number :"))

search(pattern, text, q)

Execution:
Input
Given String : hihellohi
Enter the pattern to be searched :hello
Enter the prime number :3
Output
Pattern is found at position: 3

Complexity Analysisof Rabin-Karp algorithm


Time Complexity
 Best Case Complexity - O(n+m).
The average and best-case running time of the Rabin-Karp algorithm is O(n+m), but its
worst-case time is O(nm).

 Worst Case Complexity - O(nm).

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 65


CS3401 ALGORITHMS UNIT 1 MEC

The worst case of the Rabin-Karp algorithm occurs when all characters of pattern and
text are the same as the hash values of all the substrings of text matches with the hash
value of pattern.

Space Complexity
 Since no extra space is needed, the space complexity of the naïve search is O(1).

Merits & Demerits:


Advantages:
 Extends to 2D patterns.
 Extends to finding multiple patterns.
Disadvantage:
 Arithmetic operations is slower than character comparisons.

(ii) Knuth-Morris-Pratt Algorithm


 KMP Algorithm is one of the most popular patterns matching algorithms. KMP stands for
Knuth Morris Pratt algorithm. KMP algorithm was the first linear time complexity algorithm
for string matching.
 KMP algorithm is used to find a "Pattern" in a "Text". This algorithm compares character by
character from left to right. But whenever a mismatch occurs, it uses a pre-processed table
called "Prefix Table" to skip characters comparison while matching.
 Sometimes prefix table is also known as LPS Table. Here LPS stands for "Longest proper
Prefix which is also Suffix".

Steps for Creating LPS Table (Prefix Table)

Step 1: Define a one dimensional array with the size equal to the length of the Pattern.
(LPS[size])
Step 2: Define variables i& j. Set i = 0, j = 1 and LPS[0] = 0.
Step 3: Compare the characters at Pattern[i] and Pattern[j].
Step 4: If both are matched then set LPS[j] = i+1 and increment both i& j values by one.
GotoStep 3.
Step 5: If both are not matched then check the value of variable 'i'. If it is '0' then set LPS[j] = 0
and increment 'j' value by one, if it is not '0' then set i = LPS[i-1]. Goto Step 3.
Step 6: Repeat above steps until all the values of LPS[] are filled.

Example:
Given Pattern Initialize LPS[] table with size 7 which is equal to
A B C D A B D the length of the pattern
0 1 2 3 4 5 6
LPS
Step 1:
 Define variables i& j.
 Set i = 0, j= 1 and LPS[0] = 0.
0 1 2 3 4 5 6

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 66


CS3401 ALGORITHMS UNIT 1 MEC

LPS 0

Step 2:
 Compare Pattern[i] with Pattern[j] ====>A is compared with B. Since both were not
matching, check the value of i.
 i = 0, so set LPS[j] = 0 and increment ‘j’ value by 1.
0 1 2 3 4 5 6
LPS 0 0
 Now, i = 0 & j = 2

Step 3:
 Compare Pattern[i] with Pattern[j] ====>A is compared with C. Since both were not
matching, check the value of i.
 i = 0, so set LPS[j] = 0 and increment ‘j’ value by 1.
0 1 2 3 4 5 6
LPS 0 0 0
 Now, i = 0 & j = 3

Step 4:
 Compare Pattern[i] with Pattern[j] ====>A is compared with D. Since both were not
matching, check the value of i.
 i = 0, so set LPS[j] = 0 and increment ‘j’ value by 1.
0 1 2 3 4 5 6
LPS 0 0 0 0
 Now, i = 0 & j = 4

Step 5:
 Compare Pattern[i] with Pattern[j] ====>A is compared with A. Since both are
matching, set LPS[j] = i+1 and increment both ‘i’ & ‘j’ value by 1.

0 1 2 3 4 5 6
LPS 0 0 0 0 1
 Now, i = 1 & j = 5

Step 6:
 Compare Pattern[i] with Pattern[j] ====>B is compared with B. Since both are
matching, set LPS[j] = i+1 and increment both ‘i’ & ‘j’ value by 1.
0 1 2 3 4 5 6
LPS 0 0 0 0 1 2
 Now, i = 2 & j = 6
Step 7:
 Compare Pattern[i] with Pattern[j] ====>C is compared with D. Since both were not
matching, check the value of i.
 i !=0, so set i= LPS[i-1]====>i= LPS[2-1]
 i= 0

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 67


CS3401 ALGORITHMS UNIT 1 MEC

0 1 2 3 4 5 6
LPS 0 0 0 0 1 2
 Now, i = 0 & j = 6

Step 7:
 Compare Pattern[i] with Pattern[j] ====>A is compared with D. Since both were not
matching, check the value of i.
 i = 0, so set LPS[j] = 0 and increment ‘j’ value by 1.
0 1 2 3 4 5 6
LPS 0 0 0 0 1 2 0
 Now, i = 0 & j = 7

Final LPS[] table is as follows:


0 1 2 3 4 5 6
LPS 0 0 0 0 1 2 0

Working mechanismof KMP:


We use the LPS table to decide how many characters are to be skipped for comparison when a
mismatch has [Link] a mismatch occurs, check the LPS value of the previous character of
the mismatched character in the pattern.
 If it is '0' then start comparing the first character of the pattern with the next character to the
mismatched character in the text.
 If it is not '0' then start comparing the character which is at an index value equal to the LPS
value of the previous character to the mismatched character in pattern with the mismatched
character in the Text.

Example:
Consider the following Text and Pattern
Text : ABC ABCDAB ABCDABCDABDE
Pattern : ABCDABD
LPS[] table for the above pattern is as follows:
0 1 2 3 4 5 6
LPS 0 0 0 0 1 2 0

Step 1:
 Start comparing the first character of the pattern with the first character of Text
from left to right.
Text A B C A B C D A B A B C D A B C D A B D E

0 1 2 3 4 5 6
Pattern A B C D A B D

 Here mismatch occurs at pattern[3], so we need to consider LPS[2] value is ‘0’ we


must compare first charater in pattern with next character in Text.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 68


CS3401 ALGORITHMS UNIT 1 MEC

Step 2:
 Start comparing first charater in pattern with next character in Text.

Text A B C A B C D A B A B C D A B C D A B D E

0 1 2 3 4 5 6
Pattern A B C D A B D

 Here mismatch occurs at pattern[6], so we need to consider LPS[5] value. LPS[5]= 2,


now we must compare first charater in pattern[2] with next character in Text.

Step 3:
 Since LPS value is ‘2’ no need to compare Pattern[0] & Pattern[1] values..

Text A B C A B C D A B A B C D A B C D A B D E

0 1 2 3 4 5 6
Pattern A B C D A B D

 Here mismatch occurs at pattern[2]. We need to consider LPS[2] value is ‘0’. Hence
compare first charater in pattern with next character in Text.

Step 4:
 Since LPS value is ‘2’ no need to compare Pattern[0] & Pattern[1] values..

Text A B C A B C D A B A B C D A B C D A B D E

0 1 2 3 4 5 6
Pattern A B C D A B D

 Here mismatch occurs at pattern[6]. We need to consider LPS[5] value. LPS[5]= 2,


now we must compare first charater in pattern[2] with next character in Text.

Step 5:
 Since LPS value is ‘2’ no need to compare Pattern[0] & Pattern[1] values. Compare
pattern[2] with mismatched character in Text.

Text A B C A B C D A B A B C D A B C D A B D E

0 1 2 3 4 5 6
Pattern A B C D A B D

 Here all the characters of the pattern matched with the substring in the Text, which
starts at index value 15. Hence, conclude that pattern found at index 15.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 69


CS3401 ALGORITHMS UNIT 1 MEC

/* Program to search the pattern in the given string using Knuth-


Morris-Pratt Algorithm */

Def KMP_String(pattern, text):


a = len(text)
b = len(pattern)
prefix_arr = get_prefix_arr(pattern, b)

initial_point = []
m = 0
n = 0

while m != a:

if text[m] == pattern[n]:
m += 1
n += 1

else:
n = prefix_arr[n-1]

if n == b:
initial_point.append(m-n)
n = prefix_arr[n-1]
elif n == 0:
m += 1

return initial_point

def get_prefix_arr(pattern, b):


prefix_arr = [0] * b
n = 0
m = 1
while m != b:
if pattern[m] == pattern[n]:
n += 1
prefix_arr[m] = n
m += 1
elif n != 0:
n = prefix_arr[n-1]
else:
prefix_arr[m] = 0
m += 1
return prefix_arr
string = "hihellohihellohi"
print("Given String : ", string)
pat = input("Enter the pattern to be searched :")

initial_index = KMP_String(pat, string)


for i in initial_index:
print('Pattern is found at index: ',i)

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 70


CS3401 ALGORITHMS UNIT 1 MEC

Execution:
Input
Given String : hihellohihellohi
Enter the pattern to be searched :hi
Output
Pattern is found at index: 0
Pattern is found at index: 7
Pattern is found at index: 14

Complexity Analysisof Knuth-Morris-Pratt Algorithm


Time Complexity
 Worst case complexity of KMP algorithm is O(m+n).
o O(m) time is taken for LPS table creation.
o Once this prefix suffix table is created, actual search complexity is O(n).
Space Complexity
 Space complexity of KMP algorithm isO(m) because some pre-processing
work is involved as shown in Table 1.6

Merits & Demerits:


Advantages:
 The running time of the KMP algorithm isO(m + n), which is very fast.
 The algorithm never needs to move backwardsthe input text T. It makes the
algorithm good for processing very large files.
Disadvantage:
 Doesn’t work so well as the size of the alphabets increases.

Table 1.6 Comparative analysis:

Pre-processing the Time Space


Algorithm
Pattern Complexity Complexity
Naive Match Algorithm No pre-processing O(m*(n-m+1)) O(1)
Rabin-Karp Algorithm No pre-processing O(nm) O(1)
Knuth-Morris-Pratt Algorithm Pre-process the pattern O(m + n) O(m)

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 71


CS3401 ALGORITHMS UNIT 1 MEC

[Link] Sorting and Explain in detail about the Insertion Sorting.(Apr/May


2023)
Sorting is the processing of arranging the data in ascending and descending
order. There are several types of sorting in data structures namely,
 Bubble sort
 Insertion sort
 Selection sort
 Bucket sort
 Heap sort
 Quick sort
 Radix sort etc.

Insertion Sort
Insertion sort is a simple sorting algorithm that works similar to the way you
play cards in your hands. The array is virtually split into a sorted and an unsorted
part. Values from the unsorted part are picked and placed at the correct position in
the sorted part as shown in fig 1.11

Fig.1.11 Insertion sort


Steps:
Step 1:
 The first element in the array is assumed to be sorted.
Step 2:
 Take the second element and store it separately in currentvalue. Compare currentvalue
with the first element. If the first element is greater than currentvalue, thencurrentvalue
is placed in front of the first [Link], the first two elements are sorted.
Step 3:
 Take the third element and compare it with the elements on the left of it. Placed it just
behind the element smaller than it. If there is no element smaller than it, then place it at
the beginning of the array.
Step 4:
 Similarly, place every unsorted element at its correct position. Repeat until list is sorted.

Working of Insertion Sort algorithm:


Example:

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 72


CS3401 ALGORITHMS UNIT 1 MEC

List = [12, 11, 13, 5, 6]


First Pass:
 Initially, the first two elements of the array are compared in insertion sort.
12 11 13 5 6
 Here, 12 is greater than 11. They are not in the ascending order and 12 is not at its
correct position. Hence, swap 11 and 12.
 So, for now 11 is stored in a sorted sub-array.
11 12 13 5 6

Second Pass:
 Now, move to the next two elements and compare them
11 12 13 5 6
 Here, 13 is greater than 12, thus both elements seems to be in ascending order, hence,
no swapping will occur. 12 also stored in a sorted sub-array along with 11

Third Pass:
 Now, two elements are present in the sorted sub-array which are 11 and 12
 Moving forward to the next two elements which are 13 and 5
11 12 13 5 6
 Both 5 and 13 are not present at their correct place so swap them
11 12 5 13 6
 After swapping, elements 12 and 5 are not sorted, thus swap again
11 5 12 13 6
 Here, again 11 and 5 are not sorted, hence swap again
5 11 12 13 6

Fourth Pass:
 Now, the elements which are present in the sorted sub-array are 5, 11 and 12
 Moving to the next two elements 13 and 6
5 11 12 13 6
 Clearly, they are not sorted, thus perform swap between both
5 11 12 6 13
 Now, 6 is smaller than 12, hence, swap again
5 11 6 12 13
 Here, also swapping makes 11 and 6 unsorted hence, swap again
5 6 11 12 13
Finally, the list is completely sorted.

/* Program to sort the elements in the list using Insertion sort */

definsertionSort(arr):
for index in range(1,len(arr)):

currentvalue = arr[index]
position = index

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 73


CS3401 ALGORITHMS UNIT 1 MEC

while position>0 and arr[position-1]>currentvalue:


arr[position]=arr[position-1]
position = position-1

arr[position]=currentvalue

arr = [54,26,93,17,77,91,31,44,55,20]
print("Given list : ", arr)
insertionSort(arr)
print("Sorted list : ",arr)

Execution:
Input
Given list : [54, 26, 93, 17, 77, 91, 31, 44, 55, 20]
Output
Sorted list : [17, 20, 26, 31, 44, 54, 55, 77, 91, 93]

Complexity Analysis of Insertion sort


Time Complexity
 Best case complexity - O(n)
It occurs when there is no sorting required, i.e. the array is already sorted.
 Worst case complexity - O(n2)
It occurs when the array elements are required to be sorted in reverse order. It means
suppose we need to sort the array elements in ascending order, but its elements are in
descending order.
 Average case complexity - O(n2)
It occurs when the array elements are in jumbled order that is not properly ascending and
not properly descending.

Space Complexity
 Space complexity of insertion sort is O(1)

[Link] Heap sort and Explain in detail about the Heap sort. Aprl/May 2024
Heap sort is a comparison-based sorting technique based on Binary Heap data
structure. It is similar to the selection sort where we first find the minimum element and place
the minimum element at the beginning.
Repeat the same process for the remaining elements. Heap sort processes the elements
by creating the min-heap or max-heap using the elements of the given array. Min-heap or max-
heap represents the ordering of array in which the root element represents the minimum or
maximum element of the array as shown in Fig 1.12

Heap
 A heap is a complete binary tree, and the binary tree is a tree in which the node can have
the utmost two children. A complete binary tree is a binary tree in which all the levels
except the last level, i.e., leaf node, should be completely filled, and all the nodes should
be left-justified.

5.2.2 Relationship between Array Indexes and Tree Elements

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 74


CS3401 ALGORITHMS UNIT 1 MEC

 A complete binary tree has an interesting property that we can use to find the children
and parents of any node.

 If the index of any element in the array is i, the element in the index 2i+1 will become
the left child and element in 2i+2 index will become the right child. Also, the parent of
any element at index iis given by the lower bound of (i-1)/2 as shown in Table 1.4

Example:
Given array elements:


0 1 2 3 4 5
1 12 9 5 6 10
Array is converted to Heap

Fig 1.12 Concept of Heap sort

Steps to convert array elements to Heap


Left child of 1 (index 0) Right child of 1
= element in (2*0+1) index = element in (2*0+2) index
= element in 1 index = element in 2 index
= 12 =9
Left child of 12 (index 1) Right child of 12
= element in (2*1+1) index = element in (2*1+2) index
= element in 3 index = element in 4 index
=5 =6

Rules to find parent of any node


Parent of 9 (position 2) Parent of 12 (position 1)
= (2-1)/2 = (1-1)/2
=½ = 0 index
= 0.5 =1
~ 0 index
=1
Table 1.7 Convert elements into Heap

5.2.3 Heap Data Structure


Heap is a special tree-based data structure. A binary tree is said to follow a heap data structure
if
 it is a complete binary tree
 All nodes in the tree follow the property that they are greater than their children i.e. the
largest element is at the root and both its children and smaller than the root and so on.
Such a heap is called a max-heap. If instead, all nodes are smaller than their children, it
is called a min-heap as shown in Fig 1.13

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 75


CS3401 ALGORITHMS UNIT 1 MEC

Fig 1.13 Max Heap and Min Heap


5.2.4 "Heapify" process
 Starting from a complete binary tree, we can modify it to become a Max-Heap by
running a function called heapify on all the non-leaf elements of the heap. Heapify
process uses recursion.
Pseudocode
heapify(array)
Root = array[0]
Largest = largest( array[0] , array [2*0 + 1]. array[2*0+2])
if(Root != Largest)
Swap(Root, Largest)

Fig 1.14 Heapify process


 The top element isn't a max-heap but all the sub-trees are [Link] maintain the
max-heap property for the entire tree, we will have to keep pushing 2 downwards until
it reaches its correct position as shown in Fig 1.14

Steps

Step 1: Construct a Binary Tree with given list of Elements.


Step 2:Transform the Binary Tree into Max Heap.
Step 3:
Since the tree satisfies Max-Heap property, then the largest item is stored at the root node.
Three operations at each step are -
 Swap: Remove the root element and put at the end of the array (nth position) Put the
last item of the tree (heap) at the vacant place.
 Remove: Reduce the size of the heap by 1.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 76


CS3401 ALGORITHMS UNIT 1 MEC

 Heapify:Heapify the root element again so that we have the highest element at root.
Step 4: Put the removed element into the Sorted list.
Step 5: Repeat the same until Max Heap becomes empty.
Step 6:Display the sorted list.

Working of Heap Sort Algorithm


Example:
Construct binary heap with the given list of elements

Given array elements:

6 7

0 1 2 3 4 5
81 89 9 11 14 76 54 22
Array is
converted
to Heap

Convert the constructed heap to max heap using heapify algorithm

After converting the given heap into max heap, the array elements are -
0 1 2 3 4 5 6 7
89 81 76 22 14 9 54 11

Next, we have to delete the root element (89) from the max heap. To delete this node, we have
to swap it with the last node, i.e. (11). After deleting the root element, we again have to heapify
it to convert it into max heap.

After swapping the array element 89 with 11, and converting the heap into max-heap, the
elements of array are –
0 1 2 3 4 5 6 7
81 22 76 11 14 9 54 89

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 77


CS3401 ALGORITHMS UNIT 1 MEC

In the next step, again, we have to delete the root element (81) from the max heap. To delete
this node, we have to swap it with the last node, i.e. (54). After deleting the root element, we
again have to heapify it to convert it into max heap.

After swapping the array element 81 with 54 and converting the heap into max-heap, the
elements of array are –
0 1 2 3 4 5 6 7
76 22 54 11 14 9 81 89

In the next step, we have to delete the root element (76) from the max heap again. To delete this
node, we have to swap it with the last node, i.e. (9). After deleting the root element, we again
have to heapify it to convert it into max heap.

After swapping the array element 76 with 9 and converting the heap into max-heap, the
elements of array are –
0 1 2 3 4 5 6 7
54 22 9 11 14 76 81 89

In the next step, again we have to delete the root element (54) from the max heap. To delete this
node, we have to swap it with the last node, i.e. (14). After deleting the root element, we again
have to heapify it to convert it into max heap.

After swapping the array element 54 with 14 and converting the heap into max-heap, the
elements of array are –
0 1 2 3 4 5 6 7
22 14 9 11 54 76 81 89

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 78


CS3401 ALGORITHMS UNIT 1 MEC

In the next step, again we have to delete the root element (22) from the max heap. To delete this
node, we have to swap it with the last node, i.e. (11). After deleting the root element, we again
have to heapify it to convert it into max heap.

After swapping the array element 22 with 11 and converting the heap into max-heap, the
elements of array are –
0 1 2 3 4 5 6 7
14 11 9 22 54 76 81 89

In the next step, again we have to delete the root element (14) from the max heap. To
delete this node, we have to swap it with the last node, i.e. (9). After deleting the root
element, we again have to heapify it to convert it into max heap.

After swapping the array element 14 with 9 and converting the heap into max-heap, the
elements of array are –
0 1 2 3 4 5 6 7
11 9 14 22 54 76 81 89

In the next step, again we have to delete the root element (11) from the max heap. To delete this
node, we have to swap it with the last node, i.e. (9). After deleting the root element, we again
have to heapify it to convert it into max heap.

After swapping the array element 11 with 9, the elements of array are –
0 1 2 3 4 5 6 7
9 11 14 22 54 76 81 89

Now, heap has only one element left. After deleting it, heap will be empty.

After completion of sorting, the array elements are –


0 1 2 3 4 5 6 7
9 11 14 22 54 76 81 89
Now, the array is completely sorted.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 79


CS3401 ALGORITHMS UNIT 1 MEC

/* 5.2.5 Python Program to sort the elements in the list using Heap sort */

defheapify(array, a, b):
largest = b
l = 2 * b + 1
root = 2 * b + 2

if l < a and array[b] < array[l]:


largest = l

if root < a and array[largest] < array[root]:


largest = root

# Change root
if largest != b:
array[b], array[largest] = array[largest], array[b]
heapify(array, a, largest)

# sort an array of given size


defHeap_Sort(array):
a = len(array)

# Building maxheap..
for b in range(a // 2 - 1, -1, -1):
heapify(array, a, b)

# swap elements
for b in range(a-1, 0, -1):
array[b], array[0] = array[0], array[b]
heapify(array, b, 0)

array = [81,89,9,11,14,76,54,22]
print("Original Array :", array)
Heap_Sort(array)
a = len(array)
print ("Sorted Array : ", array)

Execution:
Input
Original Array : [81, 89, 9, 11, 14, 76, 54, 22]
Output
Sorted Array : [9, 11, 14, 22, 54, 76, 81, 89]

5.2.6 Complexity Analysis of Heap sort


Time Complexity
 Best case complexity - O(nlogn)
It occurs when there is no sorting required, i.e. the array is already sorted.
 Worst case complexity - O(nlogn)

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 80


CS3401 ALGORITHMS UNIT 1 MEC

It occurs when the array elements are required to be sorted in reverse order. It means
suppose we need to sort the array elements in ascending order, but its elements are in
descending order.
 Average case complexity - O(nlogn)
It occurs when the array elements are in jumbled order that is not properly ascending and
not properly descending as shown in Table 1.8

Space Complexity
 Space complexity of Heap sort is O(1)

Table 1.8Comparative analysis:

Time Complexity Space


Algorithm
Best case Worst-case Average case Complexity
Insertion sort O(n) O(n2) O(n2) O(1)
Heap Sort O(nlogn) O(nlogn) O(nlogn) O(1)

IMPORTANT QUESTIONS
Part A

1. What is time and space complexity? Dec 2012


2. Define Algorithm validation. Dec 2012
3. Differentiate time complexity from space complexity. May 2010
4. What is a recurrence equation? May 2010
5. What do you mean by algorithm? May 2013
6. Define Big Oh Notation. May 2013
7. What is average case analysis? May 2014
8. Define program proving and program verification. May 2014
9. Define asymptotic notation. May 2014
10. What do you mean by recursive algorithm? May 2014
11. Establish the relation between O and Ω Dec 2010
12. If f(n) = amnm + ... + a1n + a0. Prove that f(n)=O(nm).Dec 2010
13. Define the Fundamentals of Algorithmic Problem Solving

Part B

1. Find the time complexity and space complexity of the following problems. Factorial using
recursion and compute the nth Fibonacci number using iterative statements. Dec 2012
[Link] the following recurrence relations: Dec 2012
1. T(n)= 2T(n/2)+3 n>2
2 n=2

2. T(n)= 2T(n/2)+cn n>1

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 81


CS3401 ALGORITHMS UNIT 1 MEC

a n=1 where a and c constants

[Link] between Big Oh, Theta and Omega notation. Dec 2012
[Link] the best case, average and worst case analysis for linear search. Dec 2012
[Link] how time complexity is calculated. Give an example. Apr 2010
[Link] on asymptotic notation with example. Apr 2010
[Link] explain the time complexity, space complexity estimation June 2013
[Link] linear search algorithm and analyse its complexity. June 2013
[Link] the following equalities are correct June 2013
i. 5n2-6n = Φ(n2)
ii. n!=O(nn)
iii. n3+106n2=Θ(n3)
iv. 2n22n + n log n = Θ(n22n)
10. What is space complexity? With an example explain the components of fixed and variable
part in space complexity. June 2014
[Link] towers of Hanoi problem and solve it using recursion. June 2014
[Link] the recurrence relation for Fibonacci series algorithm : also carry out time
complexity analysis. June 2014
[Link] in details about the efficiency of the algorithm with [Link] 2014
14. Explain the procedure to calculate the time complexity of binarysearch using nonrecursive
Algm.
[Link] briefly the time complexity and space complexity [Link] 2010
[Link] a linear search algorithm and analyse its best, worst and average case time
complexity.
[Link] that for any two functions f(n) and g(n), we have f(n)-> Θ(g(n))
if and only if f(n) - > O(g(n)) and f(n) ->Ω(g(n)) Nov 2010

APRIL/MAY 2024
PART A
1. How to measure an algorithm’s running time? Nov/Dec 2017, Apr/May 2024 [Link] 4
[Link].19
2. what do you mean worst case efficiency of algorithm. Nov/Dec 2017, Apr/May 2024 [Link] 8
[Link].44

PART B
1. Explain the Asymptotic Notations and its properties? Or explain briefly Big oh notation, Omega
notation and Theta notation give an example (Apr/May-2017) or what are the Rules of Manipulate
Big-Oh Expression and about the typical growth rates of algorithms? Nov/Dec 2017 Nov/Dec 2018
OR Define Big O notation, Big Omega and Big Theta Notation. Depict the same graphically and
explain. May/June 2018, Nov/Dec 2019, Apr/May 2024 [Link] 23 [Link].3
2. Use Substitution method to shoe that T(n)= 2T(n/2)+n is O(n log(n)) Apr/May 2024 [Link] 42
[Link].6
3. Explain in detail the various Searching techniques with an example. (APR/MAY 2023),
Apr/May 2024 [Link] 50 [Link].13
4. Explain the working of naïve string matching algorithm with ABCCDDAEFG as the text
input and CDD as the search string. Apr/May 2024 [Link] 59 [Link].14

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 82


CS3401 ALGORITHMS UNIT 1 MEC

PART C
16. (a) (i) How many spurious hits does the Rabin-Karp matcher encounter in the text
T3141592653589793 when Working modulo q 11 and looking for the pattern P = 26. Briefly write
about the processing time, worst-case running time and average-case running time of Rabin-Karp
algorithm. [Link] 62, Q.no14 & Refer Class work
(ii) With an example to show the best-case, worst-case and average case analysis of heap sort.
[Link] 74, Q.no16

Nov/Dec 2024
PART-A
[Link] time complexity of an [Link].6
[Link] is reccurence relation? [Link].38

PART – B
[Link] various complexity measures and the role of asymptotic notations toward algorithm
analysis. [Link].3

[Link] Binary search and interpolation search algorithm with an [Link] its
respective complexity measures. [Link].13

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE , 83


CS3401 ALGORITHMS UNIT 2 MEC

GRAPH ALGORITHMSUNIT –II

Graph algorithms: Representations of graphs - Graph traversal: DFS – BFS -


applications - Connectivity, strong connectivity, bi-connectivity - Minimum spanning
tree: Kruskal’s and Prim’s algorithm- Shortest path: Bellman-Ford algorithm -
Dijkstra’s algorithm - Floyd-Warshall algorithm Network flow: Flow networks - Ford-
Fulkerson method – Matching: Maximum bipartite matching

PART - A

1. Define graph. (APR/MAY 2017)

 A graph is a non-linear data structure. A graph G= (V,E) consists of a set of


vertices, V and set of edges E.
Example:

2. Define directed graph or digraph. (APR/MAY 2017,2023)


 If an edge between any two nodes in a graph is directionally oriented, a graph is
called as directed .it is also referred as digraph.
Example:

3. Define undirected graph. (APR/MAY 2017)


 If an edge between any two nodes in a graph is not directionally oriented, a graph
is called as undirected .it is also referred as unqualified graph.

Example:

4. Define path in a graph.


 A path in a graph is defined as a sequence of distinct vertices each adjacent to the
next except possibly the first vertex and last vertex is different.
1
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Example:

From the diagram , the path from V1 to V2 is V1,V2,V3.


5. Define a cycle in a graph.
 A cycle is a path containing at least three vertices such that the starting and the
ending vertices are the same
Example:

6. Define a strongly connected graph. Aprl/May 2024


 A graph is said to be a strongly connected graph, if for every pair of distinct
vertices there is a directed path from every vertex to every other vertex. It is also
referred as a complete graph.
Example:

7. Define a weakly connected graph.


 A directed graph is said to be a weakly connected graph if any vertex doesn’t have
a directed path to any other vertices.
Example:

8. Define a weighted graph.


 A graph is said to be a weighted graph if every edge in the graph is assigned some
weight or value.
2
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

 The weight of an edge is a positive value that may be representing the distance
between the vertices or the weights of the edges along the path.
Example:

9. Define adjacency matrix.


 The adjacency matrix A, for a graph G = (V,E) with n vertices is an n*n matrix,
such that
Aij=1,if there is an edge Vi to Vj
Aij=0,if there is no edge.

10. What does traversing a graph mean? State the different ways of traversing a
graph? ( Nov/Dec 2024)
 Traversing a graph means visiting all the nodes in the graph. The two important
graph traversal methods are
 Depth first traversal or depth first search (DFS)
 Breadth first traversal or breadth first search (BFS)

11. Prove that the number of odd degree vertices in a connected graph should be
even. (May/June 2007)
 The sum of degree of all the vertices is equal to the sum of the degree of all the
odd degree vertices plus sum of the degree of all the even degree vertices.
Sum = 2 * e. where e denotes edges.
Sum of the degree of all odd degree vertices is even.

12. What is a simple graph?


 A simple graph is a graph, which has not more than one edge between a pair of
nodes than such a graph is called a simple graph.

13. When a graph is said to be bi-connected? (APR/MAY 2010)


 A connected undirected graph is biconnected if there is no vertices whose
removal disconnects the rest of the graph.

14. What are the applications of graph?


 The graph theory is used widely in the computer science very widely. The
applications of graph theory are
 In computer networking such as Local Area Network(LAN), wide Area
Networking(WAN) internetworking the graph is used.
 In telephone cabling graph theory is effectively used.
 In job scheduling algorithm the graph is used.

3
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

15. How a graph is represented?OR What are the representation of the graphs?
[Apr/May 2015] Nov/Dec 2018 Aprl/May2024
There are two way of representing the graph are
 Adjacency matrix representation
 Adjacency list representation

16. Define complete graph.


 A complete graph is a graph in which there is an edge between every pair of
vertices. A complete graph n vertices will have n(n-1)/2 edges.

Number of vertices is 4
Number of edges is 6

17. Define Acyclic graph.


 A directed graph which has no cycles is referred to acyclic graph. It is abbreviated
as DAG →Directed Acyclic Graph.

18. What is breadth-first traversal?


Breath First Search (BFS) of a graph, G starts from an unvisited vertex u. Then all
unvisited vertices vi adjacent to u are visited and then all unvisited vertices wj
adjacent to vi are visited and so on. The traversal terminates when there are no
more nodes to visit.

19. What is activity node graph?


 Activity node graph represents a set of activity’s and scheduling constraints. Each
node represent activity (task) and an edge represent the next activity.

20. Define indegree and outdegree of a graph. (Nov/Dec 2011) ( Nov/Dec 2024)

Indegree
Indegree of a vertex in a digraph is the number of edges that are incident on it.
Outdegree
Outdegree of vertex is the number of edges that leave the vertex.
Eg:
Indegree(V1) = 1
Indegree(V2) = 1
Outdegree(V3) = 1
Outdegree(V4) = 1

4
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

21. When does a graph become a tree? (Apr/May 2009)


A graph can be a tree it is connected.

22. Write short notes on connected components. [Nov/Dec 2014]


Undirected Graphs
A undirected graph is ‘connected ‘ if and only if a depth first search starting from
any node visits every node.

Biconnectivity
A connected undirected graph is biconnected if there are no vertices if there are no
vertices whose remo0vel disconnects the rest of the graph.

23. When a graph is said to be connected.[Nov/Dec 2015]


A graph is said to be connected if there is a path between every pair of vertex.
From every vertex to any other vertex, there should be some path to traverse. That
is called the connectivity of a graph.
24. Draw expression tree for (a+b*c) +((d*e+f)*g).[May/Jun 2016]

25. Differentiate breadth first and depth first search strategies.(NOV/DEC


2016)
BFS DFS
BFS starts traversal from the root node and DFS starts the traversal from the root node
then explore the search in the level by level and explore the search as far as possible
manner i.e. as close as possible from the from the root node i.e. depth wise.
root node.
Breadth First Search can be done with the Depth First Search can be done with the
5
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

help of queue i.e. FIFO implementation. help of Stack i.e. LIFO implementations.
This algorithm works in single stage. The This algorithm works in two stages – in the
visited vertices are removed from the queue first stage the visited vertices are pushed
and then displayed at once. onto the stack and later on when there is no
vertex further to visit those are popped-off.
BFS is slower than DFS. DFS is more fasterthan BFS.
BFS requires morememory compare to DFS. DFS require less memory compare to BFS
BFS is useful in finding shortest path. BFS DFS in not so useful in finding shortest path.
can be used to find the shortest distance It is used to perform a traversal of a general
between some starting node and the graph and the idea of DFS is to make a path
remaining nodes of the graph. as long as possible, and then go back
(backtrack) to add branches also as long as
possible.

26. What is the space requirement of an adjacency list representation of a graph?


(Nov/Dec2005)
O (|E|+|V|) where E - no .of edges and V- no. of vertices.

27. Explain the topological sort. (May/June 2006) (APR/MAY 2010)


Topological sort is a process of assigning a linear ordering to the vertices of a DAG so
that if there is an arc from vertex i to vertex j, then i appear before j in the linear
ordering. Useful in scheduling applications
Example: consider the DAG in figure
A C

B D

A topological sort is given by : B,A,D,C,E. there could be several topological sorts for a
given DAG.

28. What is an articulation point? (Nov/Dec2009) (APR/MAY 2010)


 The vertices whose removal would disconnect the graph are known as
 articulation points.
 Here the removal of ‘C’ vertex will disconnect g from the graph.
 Similarly removal of ‘D’ vertex will disconnect E&F from the graph.
Therefore ‘C’ &’D’ are articulation points.

A B

C D
F
G E 6
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

[Link] Cut Vertex.


Cut vertex A vertex which when deleted would disconnect the remaining graph.
30. Define Euler circuits. Nov/Dec 2018
Euler circuit
1. An Euler circuit is similar to an Euler path, except that the starting and ending
points must be the same.
2. It is interesting that Euler never published an algorithm for finding an Euler circuit
,but only provided a method of determining if one existed or not. In a note from Ed
sandifer he states, “In his paper on the Kongsberg bridge problem, all he says about
finding such paths is that if you remove all double edges, then it will be easy to find a
solution”
31. What is Bi-connectivity? Apr/May 2019
BICONNECTIVITY
A connected undirected graph is biconnected if there are no vertices if there are no vertices
whose removal disconnects the rest of the graph.
Articulation Points
The vertices whose removal would disconnect the graph are known as articulation points.

B A

C D
F

G E

Here the removel of ‘C’ vertex will disconnect G from the graph.
Similarly removal of ‘D’ vertex will disconnect E & F from the graph. Therefore ‘C’ & ‘D’ are
articulation points.

32. Given a weighted, undirected graph with |V| nodes, assume all weights are non-
negative. If each edge has weight <= w , What can you say about the cost of Minimum
spanning tree?Apr/May 2019
Given an undirected and connected graph G=(V,E), a spanning tree of the graph G is a tree
that spans G(that is, it includes every vertex of G) and is a sub graph of G (every edge in the
tree belongs to G)

33. What is a Minimum Spanning Tree?(Apr/May2023)


The cost of the spanning tree is the sum of the weights of all the edges in the tree. There can
be many spanning trees. Minimum spanning tree is the spanning tree where the cost is
minimum among all the spanning trees. There also can be many minimum spanning trees.

7
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Minimum spanning tree has direct application in the design of networks. It is used in
algorithms approximating the travelling salesman problem, multi-terminal minimum cut
problem and minimum-cost weighted perfect matching. Other practical applications are:
1. Cluster Analysis
2. Handwriting recognition
3. Image segmentation

34. What are the Applications of Dynamic Programming?

 Multistage Graph
 Optimal Binary Search Tree (OBST)
 0/1 Knapsack Problem
 Travelling Salesman Problem.
 All Pair Shortest Path Problem
35. Define Warshall’s algorithm.

Warshall’s algorithm is an application of dynamic programming technique which is


used to find the transitive closure of a directed graph.

36. Define Floyd’s algorithm.

 Floyd’s algorithm is an application of dynamic programming, which is used to


find the all pairs shortest path problem.
 It is applicable to both directed and undirected weighted graph, but they do not
contain a cycle of negative length.
37. Define maximum flow problem.

The problem of maximizing the flow of a material through a transportation network is


called the maximum flow problem.

38. Define flow network.

A digraph satisfying the following properties is called a flow network or simply a


network.

8
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

 It contains exactly one vertex with no entering edges is called source and
assumed to be numbered 1.
 It contains exactly one vertex with no leaving edges is called the sink and
assumed to be numbered n.
 The weight u ijof each directed edge (i, j )is a positive integer, called the edge
capacity. (defines upper bound)
39. What is flow conservation requirement?

The total amount of the material entering an intermediate vertex must be equal to the
total amount of the material leaving the vertex is called the flow-conservation
requirement.

40. Define Max-Flow Min-Cut Theorem.

The value of a maximum flow in a network is equal to the capacity of its minimum cut.

41. Define preflow.

A preflowis a flow that satisfies the capacity constraints but not the flow- conservation
requirement.

42. What is maximum cardinality matching?

A matching in a graph is a subset of its edges with the property that no two edges share
a vertex. A maximum matching also referred as maximum cardinality matching is a
matching with the largest number of edges.

43. Define bipartite graph .

In a bipartite graph, all the vertices can be partitioned into two disjoint sets V and U,
not necessarily of the same size, so that every edge connects a vertex in one of these
sets to a vertex in the other set.

44. What is bipartite graph ? Nov/Dec 2017

The graph G = (V, E) in which the vertex set V is divided into two disjoint sets X and Y
in such a way that every edge e € E has one end point in X and other end point in Y.

45. What is two colorable graph?

The two colorable graph is a graph that can be colored with only two colors in such a
way that no edge connect the same color .the bipartite graph is two colorable graph
9
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

46. What is maximum cardinality matching ?

It is a matching with largest number of matching edges.

47. What is maximum matching problem ?

The maximum matching problem is a problem of finding maximum matching in a


graph.

48. What do you mean by perfect matching in bipartite graph? (MAY 2015, Apr/May -
2017)

A perfect matching is a matching which matches all vertices of the graph. That is, every vertex
of the graph is incident to exactly one edge of the matching. Figure (b) above is an example of a
perfect matching. Every perfect matching is maximum and hence maximal. In some literature,
the term complete matching is used. In the above figure, only part (b) shows a perfect
matching. A perfect matching is also a minimum-size edge cover. Thus, ν(G) ≤ ρ(G) , that is, the
size of a maximum matching is no larger than the size of a minimum edge cover.

49. Define flow cut.(AU MAY 2015)

Maximum flow
Definition. The capacity of an edge is a mapping c :E→R+, denoted by cuv or c(u, v). It
represents the maximum amount of flow that can pass through an edge.
Definition. A flow is a mapping f : E→R+, denoted by fuv or f (u, v), subject to the following
two constraints:
1. Capacity Constraint:

2. Conservation of Flows:

Definition. The value of flow is defined by

wheres is the source of N. It represents the amount of flow passing from the source to the
sink.
Maximum Flow Problem. Maximize | f |, that is, to route as much flow as possible from s to
t.

10
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Minimum cut
Definition. An s-t cutC = (S, T) is a partition of V such that s∈S and t∈T. The cut-set
of C is the set

Note that if the edges in the cut-set of C are removed, | f | = 0.


Definition. The capacity of an s-t cut is defined by

where if and , 0 otherwise.


Minimum s-t Cut Problem. Minimize c(S, T), that is, to determineS and T such that the capacity
of the S-T cut is minimal.

50. What is Depth first search? Or write procedure for DFS algorithm? APR/MAY-2017
 Select an unvisited node x, visit it, and treat as the current node
 Find an unvisited neighbor of the current node, visit it, and make it the new current
node;
 If the current node has no unvisited neighbors, backtrack to the its parent, and make
that parent the new current node;
 Repeat steps 3 and 4 until no more nodes can be visited.
 If there are still unvisited nodes, repeat from step 1.
Note: DFS can be implemented efficiently using a stack

51. List the applications of depth first [Link]/MAY-2016

 Finding connected components.


 Topological sorting.
 Finding strongly connected components.
 Solving puzzles with only one solution, such as mazes.
 Finding bi-connectivity in graph

52. Define minimum spanning tree of a Graph. NOV/DEC 2014 NOV/DEC-2016


A spanning tree is its connected acyclic subgraph that contains all vertices of the graph.

• A minimum spanning tree connects all nodes in a given graph


• A MST must be a connected and undirected graph
• A MST can have weighted edges
• Multiple MSTs can exist within a given undirected graph
1. Convert the given graph with weighted edges to minimal spanning tree.

11
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

The equivalent minimal spanning tree is:

53. DefineKruskal’s algorithm.


Kruskal’s algorithm uses a greedy technique to compute a minimum spanning tree. This
algorithm select the edges in the order of smallest weight and accept an edge if it does
not cause a cycle.

54. Define prim’s algorithm.

Prim’s algorithm is one of the ways to compute a minimum spanning tree which uses a
greedy technique. This algorithm begins with a set U initialized to {1}. It then grows a
spanning tree, one edge at a time .At each step, it finds a shortest edge(u, v) such that the
cost of(u, v) is the smallest among all edges ,where u is in Minimum spanning tree and V is
not in Minimum spanning tree.

55. What is the purpose of Dijikstra’s algorithm?


Dijkstra’s Algorithm

 The general method to solve the single source shortest path problem is known as
Dijkstra’s algorithm. The solution is prime example of greedy algorithms.
 At each stage, it select a vertex ‘V’, which has the smallest dv among all the unknown
vertices, and declares that the shortest path form’s’ to ‘V’ is known.
Formula: To find the adjacency distance value

VW

T[W].dist=Min[T[W].dist,T[V].dist+CVW]

T[W].path=V

56. What is the principle behing Bellman-Ford algorithm to detect the negative
weight cycles? APR/MAY 2015
The Bellman-Ford algorithm solves the single-source shortest-paths problem in the general
case in which edge weights may be negative.

Given a weighted, directed graph G =(V,E) with source s and weight function w : E -> R, the
Bellman-Ford algorithm returns a boolean value indicating whether or not there is a
negative-weight cycle that is reachable from the source. If there is such a cycle, the
algorithm indicates that no solution exists. If there is no such cycle, the algorithm produces
the shortest paths and their weights.

12
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

57. Give a note on Floyd - Warshall algorithm.


Different dynamic-programming formulation to solve the all-pairs shortest-paths
problem on a directed graph G =(V,E), is known as the Floyd-Warshall algorithm, runs in
ө (V3)[Link] before, negative-weight edges may be present, but we assume that there are
no negative-weight cycles.

58. Difference between Kruskal and Prim’s Algorithm.


[Link]. Prim’s Algorithm Kruskal’s Algorithm

1 This algorithm begins to construct the This algorithm begins to construct the
shortest spanning tree from any shortest spanning tree from the vertex having
vertex in the graph. the lowest weight in the graph.

2 To obtain the minimum distance, it It crosses one node only one time.
traverses one node more than one
time.

3 The time complexity of Prim’s The time complexity of Kruskal’s algorithm is


algorithm is O(V2). O(E log V).

4 In Prim’s algorithm, all the graph Kruskal’s algorithm may have disconnected
elements must be connected. graphs.

5 When it comes to dense graphs, the When it comes to sparse graphs, Kruskal’s
Prim’s algorithm runs faster. algorithm runs faster.

6 It prefers list data structure. It prefers the heap data structure.

[Link] the drawbacks of Floyd-Warshall algorithm NoV/DEC-2016

 Slower.O(V^3)
 Harder to understand

13
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

PART-B

1. Explain in detail about graph and write different types of Graphs.


Definition
 A graph G = {V, E} consists of a set of vertices V and set of edges E.
 Vertices are referred to as nodes in graph and the line joining the two vertices are
referred to as Edges.
Types of Graphs
Graphs are of two types
 Directed graphs.
 Undirected Graphs.

(i) Directed Graphs


 Directed graph is a graph which consists of directed edges. It is also referred as
Digraph.

 In Directed graph, the edges between the vertices are ordered. E1 is the edge between
the vertices V1 and V2.
 V1 is called the Head and V2 is called the Tail.
 So, E1 is a set of (V1, V2) and not of (V2, V1).

(ii) Undirected Graphs:


 Undirected graph is a graph, which consists of undirected edges.

 In Undirected graph, the edges between the vertices are not ordered.
 So, E1 is a set of (V1, V2) or (V2, V1).

Terms Related To Graphs


Adjacent nodes
 two nodes are adjacent if they are connected by an edge
Path
 a sequence of vertices that connect two nodes in a graph
14
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Length of path of graph


 The length of a path in a graph is the number of edges in the path
In-degree and Out-degree in graph
Let G be a directed graph
 Thein-degree of a node x in G is the number of edges coming to x
 The out-degreeof x is the number of edges leaving x.

Degree and Neighbor:


Let G be an undirected graph
 The degree of a node x is the number of edges that have x as one of their end nodes
 The neighbors of x are the nodes adjacent to x

Weighted Graphs
 A graph is said to be weighted graph if every edge in the graph is assigned a weight
or value. It can be either a directed or an undirected graph.

Complete Graph
 A complete graph is a graph in which there is an edge between every pair of vertices.
 A complete graph with n vertices will have n(n - 1) /2 edges.

Sub Graph
 A sub graph G’ of G is a graph G such that the set of vertices and set of edges of G’ are
proper subset of the set of edges of G.

Connected Graphs
 An undirected graph is said to be connected if for every pair of distinct vertices Vi
and Vj, there is a path from Vi to Vj in G.

15
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Strongly Connected Graphs:


 A directed graph is said to be strongly connected if and only if, for each pair of
distinct vertices Vi and Vj, there is a path from Vi to Vj in G.

Cyclic Graphs
 A directed graph is said to be a cyclic graph in which no vertex is repeated except the
first and last vertex are the same.

Cycle = A -> B-> C ->A

 An undirected graph is said to be a cyclic graph in which if any edge appears more
than once it appears with the same orientation.

Cycle = A BC A
Acyclic Graphs
 A graph is said to be a acyclic graph if it has no cycles.

DAG – Directed acyclic graph (no specific cycle)

16
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Undirected acyclic graph


2. Explain in detail about different ways of representation of Graphs.
For graphs to be computationally useful, they have to be conveniently represented in
programs.

There are two computer representations of graphs:

Representation of graphs:
 There are two representations of graphs:
 Adjacency matrix representation
 Adjacency lists representation

In this representation, each graph of n nodes is represented by an n x n matrix A, that is, a


two-dimensional array A. Adjacency matrix of an undirected graph is always a symmetric
matrix, i.e. an edge (i, j) implies the edge (j, i). Adjacency matrix of a directed graph is never
symmetric. The matrix is filled as:

- A[i][j] = 1 if (i,j) is an edge


- A[i][j] = 0 if (i,j) is not an edge

Adjacency matrix of an undirected graph

17
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Adjacency matrix of a directed graph

Adjacency matrix of a weighted graph

A weighted graph has a numerical value assigned to its edges.

Advantages of adjacency matrix:

- Simple to implement
- Easy and fast to tell if a pair (i, j) is an edge: simply check if A[i][j] is 1 or 0.
- Degree of a vertex can easily be calculated by counting all non-zero entries in the
corresponding row of the adjacency matrix.
-
Disadvantages of adjacency matrix:

- No matter how few edges the graph has, the matrix takes O (n2) in memory.

Adjacency list representation

A graph can also be represented using a linked list. For each vertex, a list of adjacent
vertices is maintained using a linked list. It creates a separate linked list for each vertex Vi
in the graph G = (V, E).

Adjacency list of a graph with n nodes can be represented by an array of pointers.


Each pointer points to a linked list of the corresponding vertex.

18
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Adjacency list of a graph

Advantages of adjacency list:

- Adjacency list representation of a graph is very memory efficient when the graph
has a large number of vertices but very few edges.
Disadvantages of adjacency list:

- Checking the existence of an edge between two vertices i and j is also time
consuming. Linked list of vertex i must be searched for the vertex j.

Example for adjacency list – Undirected Graph

Example for adjacency matrix – Directed Graph

Adjacency Matrix Vs Adjacency List Representation


Adjacency matrix Adjacency list
Good for dense graphs --|E|~O(|V| )
2 Good for sparse graphs -- |E|~O(|V|)
Memory requirements: O(|V| + |E|) Memory requirements: O(|V| +
= O(|V|2 ) |E|)=O(|V|)
19
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Connectivity between two vertices can be tested Vertices adjacent to another vertex can be
quickly found quickly

3. Explain the Graph Traversals. What are the types of traversals?

The order in which the vertices of a graph are visited is called as graph
traversal.

During a traversal we must keep track of which vertices have been visited. There are two
types of graph traversals:

 Depth First Traversal (DFS)


 Breadth First Traversal(BFS)

4. Write an algorithm for depth first search (DFS)on a graph and give the nodes of the
graph ‘G’ given in the fig based on the algorithm. (NOV/DEC-2016)(APR/MAY 2023)

Depth First Search (DFS)

In DFS, go as far as possible along a single path until reach a dead end (a vertex with
no edge out or no neighbor unexplored) then backtrack. After visiting a vertex v, which is
adjacent to w1, w2, w3. Next we visit one of v's adjacent vertices, w1 say. Next, we visit all
vertices adjacent to w1 before coming back to w2, etc. Must keep track of vertices already
visited to avoid cycles. The method can be implemented using recursion or iteration.

o In case of ties, chose the vertex in alphabetical order or in increasing order.

o If we perform this process on an arbitrary graph, we need to be careful to


avoid cycles.

To do this, when we visit a vertex v, we mark it visited, since now we have been there, and
recursively call depth-first search on all adjacent vertices that are not already marked.

To implement the depth first search perform the following steps:

Step 1: Choose any node in the graph. Fix it as the search node and mark it as Visited.

Step 2: Using the adjacency matrix of the graph, find a node adjacent to the search node
that has not been visited yet. Fix this as the new search node and mark it as visited.

Step 3: Repeat step 2 using the new search node. If no nodes satisfying (2) can be Found,
return to the previous search node and continue from there

Step 4: When a return to the previous search node. In (3) is impossible, the search from the
originally choose search node is complete.

Step 5: If the graph still contains unvisited nodes, choose any node that has not been
visited and repeat step (1) through (4).

20
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Example: Find the DFS of the given graph.

B D E

Adjacency Matrix of the given graph

V A B C D E

A 0 1 0 0 1

B 1 0 1 1 0

C 0 1 0 1 1

D 0 1 1 0 0

E 1 0 1 0 0

Implementation

1. Start at vertex ‘A’ then mark A as visited and call DFS(A).


A

2. B is adjacent node of A which is not visited and call DFS(B) then mark B as visited.
A

3. C is adjacent node of B which is not visited and call DFS(C) then mark C as visited.

21
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

4. D is adjacent node of C which is not visited and call DFS(D) then mark D as visited.
A

5. There is no adjacent node of D which is unvisited, so we return back to DSF(C)


6. E is adjacent node of C which is not visited and call DFS(E) then mark E as visited.

D E

Since all the vertices starting from ‘A’ are visited, the above graph is said to be connected. If
the graph is not connected, then processing all nodes requires calls to DFS, and each
generates a tree. This entire collection is a defth first spanning forest.
22
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

ALGORITHM

DFS (vertices, start)


Input: The list of all vertices, and the start node.
Output: Traverse all nodes in the graph.
Begin
initially make the state to unvisited for all nodes
push start into the stack
while stack is not empty, do
pop element from stack and set to u
display the node u
if u is not visited, then
mark u as visited
for all nodes i connected to u, do
ifith vertex is unvisited, then
pushith vertex into the stack
markith vertex as visited
End
end
End
Applications of Depth First Search

 DFS traversal of the graph produces the minimum spanning tree and all pair
shortest path tree for an unweighted graph.
 Detects cycle in a graph
 Path Finding
 Topological Sorting
 To test if a graph is bipartite (Bipartite means the vertices can be colored red or
black such that no edge links vertices of the same color).
 Finding Strongly Connected Components of a graph.
 Solving puzzles with only one solution, such as mazes.
Time Complexity

Depth-first search is a generalization of preorder traversal. Starting at some vertex,


v, we process v and then recursively traverse all vertices adjacent to v. If this process is
performed on a tree, then all tree vertices are systematically visited in a total of O(|E|) time,
since |E| = (|V|).

23
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

5. Write an algorithm for breadth first search on a graph and give the nodes of the
graph ‘G’ given in the fig based on the algorithm. (NOV/DEC-2016)(APR/MAY 2023)

Breadth First Search (BFS)

The strategy for searching a graph is known as breadth-first search. It operates by


processing vertices in layers: the vertices closest to the start are evaluated first, and the
most distant vertices are evaluated last. This is much the same as a level-order traversal for
trees.

Breadth-first search (BFS) is a general technique for traversing a graph.

Breath First Search (BFS) of a graph, G starts from an unvisited vertex u. Then all unvisited
vertices vi adjacent to u are visited and then all unvisited vertices wj adjacent to vi are
visited and so on. The traversal terminates when there are no more nodes to visit.

STEPS:

Step to implement breath first search

Step 1: Choose any node in the graph, fix it as the search node and mark it as visited.

Step 2: Using the adjacency matrix of the graph, find all the unvisited adjacent nodes to the
search node and enqueue them in to the queue Q.

Step 3: Then the node which is dequeued from the queue. Mark that node as visited and fix
it as the new search node.

Step 4: Repeat step 2 and 3 using the new search node.

Step 5: This process continues until queue Q which keeps track of the adjacent nodes is
Empty.

Example: Find the BFS of the given graph.

B D E

24
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Adjacency Matrix of the given graph

V A B C D E
A 0 1 0 0 1
B 1 0 1 1 0
C 0 1 0 1 1
D 0 1 1 0 0
E 1 0 1 0 0

Implementation

1. Let ‘A’ be the source vertex. Mark it as visited

2. Find the adjacent unvisited vertices of ‘A’ and enqueue then into the queue. Here B and E
are adjacent node of A.

Queue Q:

B E

and B and E are enqueued.

3. Then vertex ‘B’ is dequeued and marks it as visited. Its adjacent vertices C and D are
taken from the adjacency matrix for enqueuing.

Queue Q:

E C D

Here B is dequeued, C & D is enqueued.

4. Then vertex ‘E’ is dequeued and marks it as visited. Its adjacent vertex C is taken from
the adjacency matrix for enqueuing. Since vertex C is already in the queue, it’s not
enqueued.

Queue Q:

C D
Here E is dequeued.

5. Then vertex ‘C’ is dequeued and marks it as visited. Its adjacent vertices B, D and E are
taken, in which vertices B and E are visited and vertex D is already in the queue, so all
vertices are not enqueued.

Queue Q:

D
25
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Here C is dequeued.

6. Then vertex ‘D’ is dequeued and marks it as [Link] this process terminates
since all the vertices are visited and the queue is also empty.

Queue Q:
Null

Here D is dequeued.

B E

D
D
Algorithm:
BFS(vertices, start)
Input: The list of vertices, and the start vertex.
Output: Traverse all of the nodes, if the graph is connected.
Begin
define an empty queue que
at first mark all nodes status as unvisited
add the start vertex into the que
whileque is not empty, do
delete item from que and set to u
display the vertex u
for all vertices 1 adjacent with u, do
if vertices[i] is unvisited, then
mark vertices[i] as temporarily visited
add v into the queue
mark
done
mark u as completely visited
26
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

done
End
Applications of BFS
 Find and report a path with the minimum number of edges between two given
vertices.
 Find a simple cycle, if there is one.
 To find Shortest path
 To find Single Source & All pairs shortest paths
 Used to construct a Spanning tree
 Used to check Connectivity
6. Explain the graph connectivity With an examples.
Connectivity
A graph is said to be connected if there is a path between every pair of vertex. From
every vertex to any other vertex, there should be some path to traverse. That is called the
connectivity of a graph. A graph with multiple disconnected vertices and edges is said to be
disconnected.
Example 1
In the following graph, it is possible to travel from one vertex to any other vertex. For
example, one can traverse from vertex 'a' to vertex 'e' using the path 'a-b-e'.

Example 2
In the following example, traversing from vertex 'a' to vertex 'f' is not possible because
there is no path between them directly or indirectly. Hence it is a disconnected graph.

27
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Connectivity Types
Graph Connectivity can be classified broadly into two categories −
 Edge Connectivity
 Vertex Connectivity
Edge Connectivity
Let 'G' be a connected graph. The minimum number of edges whose removal makes 'G'
disconnected is called edge connectivity of G.
Notation − λ(G)
In other words, the number of edges in a smallest cut set of G is called the edge
connectivity of G.
If 'G' has a cut edge, then λ(G) is 1. (edge connectivity of G.)
Example 3
Take a look at the following graph. By removing two minimum edges, the connected graph
becomes disconnected. Hence, its edge connectivity (λ(G)) is 2.

Here are the four ways to disconnect the graph by removing two edges −

28
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Vertex Connectivity
Let 'G' be a connected graph. The minimum number of vertices whose removal makes 'G'
either disconnected or reduces 'G' in to a trivial graph is called its vertex connectivity.

Notation − K(G)

Example 4
In the above graph, removing the vertices 'e' and 'i' makes the graph disconnected.

If G has a cut vertex, then K(G) = 1.


Notation − For any connected graph G,
K(G) ≤ λ(G) ≤ δ(G)
Vertex connectivity (K(G)), edge connectivity (λ(G)), minimum number of degrees of
G(δ(G)).
Example 5
Calculate λ(G) and K(G) for the following graph −

Solution:
From the graph,
δ(G) = 3
K(G) ≤ λ(G) ≤ δ(G) = 3 (1)
K(G) ≥ 2 (2)
Deleting the edges {d, e} and {b, h}, we can disconnect G.
Therefore,
λ(G) = 2
29
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

2 ≤ λ(G) ≤ δ(G) = 2 (3)


From (2) and (3), vertex connectivity K(G) = 2
7. Explain in strong connectivity with an example.

In a directed graph is said to be strongly connected, when there is a path between each
pair of vertices in one component.

To solve this algorithm, firstly, DFS algorithm is used to get the finish time of each vertex,
now find the finish time of the transposed graph, then the vertices are sorted in
descending order by topological sort.

Algorithm

traverse(graph, start, visited)


Input: The graph which will be traversed, the starting vertex, and flags of visited nodes.
Output: Go through each node in the DFS technique and display nodes.

Begin
mark start as visited
for all vertices v connected withstart,do
if v isnotvisited,then
traverse(graph, v, visited)
done
End

topoSort(u, visited, stack)


Input − The start node, flag for visited vertices, stack.
Output − Fill stack while sorting the graph.
Begin
mark u as visited
for all node v, connected with u, do
if v is not visited, then
topoSort(v, visited, stack)
done
30
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

push u into the stack


End

GetStrongConComponents(graph)
Input: The given graph.
Output − All strongly connected components.
Begin
initially all nodes are unvisited
for all vertex i in the graph, do
if i is not visited, then
topoSort(i, vis, stack)
done

make all nodes unvisited again


transGraph := transpose of given graph

while stack is not empty, do


pop node from stack and take into v
if v is not visited, then
traverse(transGraph, v, visited)
done
End
8. Explain in detail about Bi-Connectivity with an example.
BICONNECTIVITY
An undirected graph is said to be a biconnected graph, if there are two vertex-disjoint paths between any two vertices
are present. In other words, we can say that there is a cycle between any two vertices.

We can say that a graph G is a bi-connected graph if it is connected, and there are no articulation points or cut vertex are
present in the graph.

To solve this problem, we will use the DFS traversal. Using DFS, we will try to find if there is any articulation point is
present or not. We also check whether all vertices are visited by the DFS or not, if not we can say that the graph is not
connected.

Articulation Points
The vertices whose removal would disconnect the graph are known as articulation points.

31
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

B A

C D

G E

Connected Undirected Graph

Here the removal of ‘C’ vertex will disconnect G from the graph.
Similarly removal of ‘D’ vertex will disconnect E & F from the graph. Therefore ‘C’ & ‘D’ are
articulation points.

B A

C D

G E
Removal of vertex ‘C’

B A

C D

G E
Removal of Vertex ‘D’
The graph is not biconnected, if it has articulation points.

32
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Depth first search provides a linear time algorithm to find all articulation points in a
connected graph.
Step 1: Perform depth first search starting at any vertex.
Step 2: Number the vertex as they are visited, as Num (V).
Step 3: Compute the lowest numbered vertex for every vertex V in the Depth
First spanning tree, which we call as low (W),that is reachable from v by
Taking zero or more tree edges and then possible one back edge. By
Definition, Low (V) is the Minimum of
(i) Num(V)
(ii) The lowest Num (w) among all back edges (V,W)
(iii) The lowest low (W) among aii tree edge (V,W)
Step 4: (i) they root is an articulation if and only if it has more than two child.
(ii) Any vertex V other than root is an articulation point if and only if V has same
child W such that low (W) >Num (V), the time taken to compute this algorithm
an a graph is O (|E| + |V|)

D G

Depth First Tree For with Num and Low.

8. Write short notes minimum spanning tree with an example.


MINIMUM SPANNING TREE (MST)

 A spanning tree of a graph is just a sub graph that contains all the vertices and
is a tree.

 A graph may have many spanning trees.

 Informally spanning tree is defined as: given n points, connect them in the
cheapest possible way so that there will be a path between every pair of nodes.

 The Minimum Spanning Tree for a given graph is the spanning Tree of minimum
cost for that graph.
33
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

 A Minimum Spanning Tree (MST) is a sub graph of an undirected graph such


that the sub graph spans (includes) all nodes, is connected, is acyclic, and has
minimum total edge weight

 The weight of a tree is defined as the sum of the weights on all its edges.

 Constructing a minimum spanning tree by exhaustive search would face two


serious obstacles:

 The number of spanning trees grows exponentially with the graph size.

 Generating all spanning trees for a given graph is difficult.

Tree for an in directed graph

There are two algorithms or methods to construct minimum spanning trees: Prim’s and
Kruskal’s algorithm.

 Both Prim’s and Kruskal’s Algorithms work with undirected graphs.


 Both work with weighted and un weighted graphs but are more interesting
when edges are weighted. When the edges are weighted, the algorithms yield
weighted spanning trees.
 Both are greedy algorithms that produce optimal solutions.

[Link] Prim’s algorithm. Find the minimum spaning tree for the following graph
using any node of the [Link]/MAY2016
Prim’s Algorithm

Prim’s algorithm was initially discovered in 1930 by VojtěchJarník, then


rediscovered in 1957 by Robert C. Prim. This algorithm considers the spanning tree to
consist of both nodes and edges. The running time O (V + E) log V, where V is the number of
vertices and E is the number of edges.
The following are the steps:
 Label the starting node, with a 0 and all others with infinity.
 Starting from A, update all the connected nodes’ labels to A with their weighted
edges if it less than the labeled value.
 Find the next smallest label and update the corresponding connecting nodes.

34
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

 Repeat until all the nodes have been visited.

Construct MST using prim’s algorithm


2
1 2
4 1 3 10 INITIALSTATE

2 7 V Known Dv pv
3 4 5
V1 0 0 0
8 4
5 V2 0 ∞ 0
6
6 7 V3 0 ∞ 0
1
V4 0 ∞ 0

V5 0 ∞ 0

V6 0 ∞ 0

V7 0 ∞ 0

Vertex V1 is marked as visited and then distance of its adjacent vertices are updated as
follows

The tables after V1 is declared know


1 2
V Known Dv pv

V1 1 0 0
3 4 5
V2 0 2 V1

V3 0 4 V1
6 7 V4 0 1 V1

V5 0 ∞ 0
T[V2].dist = min[T[V2].dist ,CV1,V2] V6 0 ∞ 0
= min [∞, 2] = 2 V7 0 ∞ 0
T[V4].dist = min[T[V4].dist ,CV1,V4]

= min [∞, 1] = 1

T[V3].dist = min[T[V3].dist ,CV1,V3]

35
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

= min [∞, 4] = 4

Vertex V4 is marked as visited and then the distance of its adjacent vertices are updated.

1 2 The tables after V4is declared know

V Known Dv pv

3 4 5 V1 1 0 0

V2 0 2 V1

6 7 V3 0 2 V4
T[V2].dist = min[T[V2].dist ,CV4,V2] V4 1 1 V1
= min [2, 3] = 2 V5 0 2 V4
T[V3].dist = min[T[V3].dist , CV4,V3] V6 0 8 V4
= min [4, 2] = 2 V7 0 4 V4
T[V5].dist = min[T[V5].dist ,CV4,V5]

= min [∞, 2] = 7

T[V6].dist = min[T[V6].dist ,C V4,V6]

= min [∞, 8] = 8

T[V7].dist = min[T[V7].dist ,C V4,V7]

= min [∞, 4] = 4

Vertex V2 is marked as visited and then the distance of its adjacent vertices are updated.
The tables after V2 is declared know

V Known Dv pv
1 2
V1 1 0 0

V2 1 2 V1
3 4 5
V3 0 2 V4

V4 1 1 V1

6 7 V5 0 7 V4

V6 0 8 V4
T[V4].dist = min[T[V4].dist ,CV2,V4] V7 0 4 V4

36
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

= min [1, 3] = 1

T[V5].dist = min[T[V5].dist ,CV2,V5]

= min [7, 10] = 7

Vertex V3 is marked as visited and then the distance of its adjacent vertices are updated

The tables after V3is declared known


1 2
V Known Dv pv

V1 1 0 0
3 4 5
V2 1 2 V1

V3 1 2 V4
6 7 V4 1 1 V1
T[V6].dist = min[T[V6].dist ,CV3,V6] V5 0 7 V4
= min [8, 5] = 5 V6 0 5 V3

V7 0 4 V4

Vertex V7 is marked as visited and then the distance of its adjacent vertices are updated
1 2 The tables after V7is declared known

V Known Dv pv

3 4 5 V1 1 0 0

V2 1 2 V1

V3 1 2 V4
6 7
V4 1 1 V1
T[V6].dist = min[T[V6].dist ,CV7,V6] V5 0 6 V7
= min [5, 1] = 1 V6 0 1 V7
T[V5].dist = min[T[V5].dist ,CV7,V5] V7 1 4 V4
= min [7, 6] = 6

Vertex V6 is marked as visited and then the distance of its adjacent vertices are updated
37
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

The tables after V6 is declared known

V Known Dv pv
1 2
V1 1 0 0

V2 1 2 V1
3 4 5 V3 1 2 V4

V4 1 1 V1

6 7 V5 0 6 V7
Vertex V5 is marked as visited and then V6 1 1 V7 the
distance of its adjacent vertices are
updated V7 1 4 V4

The tables after V5 is declared known

V Known Dv pv
1 2
V1 1 0 0

V2 1 2 V1
3 4 5 V3 1 2 V4

V4 1 1 V1

6 7 V5 1 6 V7

V6 1 1 V7
The edges in the spanning tree can be V7 1 4 V4 read
from the table as follows:

(V1, V2) = 2

(V3, V4) = 2

(V1, V4) = 1

(V7, V5) = 6

(V7, V6) = 1(V4, V7) = 4 TOTAL = 16

ROUTINE FOR PRIMS ALGORITHM


Void prims (Table T)
{
38
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Vertex V, W:
\* table initialization*/
For (i=0; i<num vertex; i++)
{
T[i].known = false;
T[i].Dist = infinity;
T[i].path = Not A vertex;
}
T [start]. Dist = 0;
For (;;)
{
V = smallest unknown distance vertex;
If (V==not a vertex)
Break;
T [V}].known = true;
For each w adjacent to V
If (! T [W].known)
{
T [W].dist = min [T [W].dist, C v w]
T [W].path = V;
}}}

ANALYSIS OF THE PRIMS ALGORITHM


The running time is O(|V| 2 )without heaps, which is optimal for dense graphs, and
O (|E| log |V|) using heap, which is good for the sparse graphs.
Note: On adding edges to the MST, it should not form cycles
Example 2:

39
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

10. Consider the following graph, Construct MST using Kruskals algorithm.(Apr/May-
2016)
 In kruskal’s algorithm, we select edges inorder of smallest weight and accept an
edge if it doest not form a cycle.
 The algorithm uses two data structure namely find and union.
 Find (U) returns the root of the tree that contains the vertexU.
 Union (S, U, V) merge the two trees by making the root pointer of one node point to
the root node of the other tree.

1 2

3 4 5

6 7

Kruskals algorithm after each step can be represented as follows:

Step 1:

1 2

3 4 5

6 7
Step 2:

1 2

3 4 5

6 7

40
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Step 3:

1 2

3 4 5

6 7
Step 4:

1 2

3 4 5

6 7

Step 5:
1 2

3 4 5

6 7

Action of kruskal’s algorithm

Edge Weight action

(V1,V4) 1 ACCEPTED

(V6,V7) 1 ACCEPTED

41
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

(V1,V2) 2 ACCEPTED

(V3,V4) 2 ACCEPTED

(V2,V4) 3 REJECTED

(V1,V3) 4 REJECTED

(V4,V7) 4 ACCEPTED

(V3,V6) 5 REJECTED

(V5,V7) 6 ACCEPTED

ROUTINE FOR KRUSKAL’S ALGORITHM:

Void kruskal (Graph G)

int edges Accepted = 0;

Disjoint Set;

Heap H;

Vertex U, V;

Set Type Uset ,Vset;

Edge E;

Initialize (s);

Build Heap (H); \* construction of min Heap *\

While (Edges Accepted <NumVertex – 1)

E = Delete min (H);

Uset = Find (U,S);

Vset = find (V,s);

If (Uset ! = Vset)

Edges Accepted ++;

Set Union (S, Uset, Vset);


42
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

}}}

Analysis of the kruskal’s algorithm

The worst –case running time of this algorithm is O (|E| log |E|), which is dominated
by heap operation.

Notice that since |E|= O|V| 2), this running time is actually O|E| log |V|).

Example 2:

43
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Differences between Prim’s and Kruskal’s algorithm


Prim’s algorithm Kruskal’s algorithm

Prim's algorithm it start with a node. Kruskal’s algorithm it begins with an edge

Prim's algorithm it move from one node to Kruskal's Algorithm select the next edge in
another. increasing order

Prim's algorithm restricted on connected Kruskal's algorithm works on both


graph. connected and disconnected graph

Prim's better if the number of edges to


vertices is high. Kruskal can have better performance if the
edges can be sorted in linear time, or are
already sorted.

The running time is O (V + E) log V. The running time is O (E log V).

11. Write short notes on shortest path Problem Algorithm.


Shortest path.

• Shortest path: the path whose total weight is minimum


The shortest path algorithm determines the minimum cost of the path from source to every
other vertex

Two type of shortest path problems, exit namely,

 The single source shortest path problem.


 The all pairs shortest path problem.
Single source shortest path algorithm

 The single source shortest path algorithm finds the minimum cost from single
source vertex to all other vertices.
 Common algorithms: Dijkstra's algorithm, Bellman-Ford algorithm
 Dijkstra's algorithm solves the single-source shortest path
problem.
 Bellman–Ford algorithm solves the single-source problem if edge
weights may be negative.
 is used to solve this problem which follows the greedy technique.

All pairs shortest path algorithm


 All pairs shortest path problem finds the shortest distance from each vertex to all
other vertices.

44
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

• To solve this problem dynamic programming technique known as Floyd–


Warshall algorithm solves all pairs shortest paths.
 BFS can be used to solve the shortest graph problem when the graph is weightless
or all the weights are the samefloyd’s algorithm is used.

12. Using Dijiktra’s algorithm, find the shortest path from the source to all nodes of
the graph ‘G’ given in the following figure. (APR/MAY 2023)

1. A unweighted directed graph


2. A weighted directed graph

1) Unweighted shortest paths

All the edges are assigned a weighted of ‘1’ for each vertex.
Three piece of information
1) Known  Specifies whether the vertex processed or not.
2) dv  Specifies the distance from the source
3) pv Actual path

Algorithm:
1) Assign the source node (s) and enqueue s.
2) Dequeue S and assign the value (known) then find its adjacency vertices.
3) If distance of adjacent vertices is equal to infinity then change the distance
T[W].dist=T[V].distance+1 if dw = infinity
T[W].path=V
VW
4) Repeat from step – 2 , until the queue becomes empty.
unweighted directed graph
STEP 1: INITIAL STATE

DEQUEUE
1 2 V Known Dv pv
V1 0 ∞ 0
V2 0 ∞ 0
4 5 V3 0 0 0
3
V4 0 ∞ 0
V5 0 ∞ 0
6 7 V6 0 ∞ 0
V7 0 ∞ 0
ENQUEUE V3
We choose‘s’ to be source vertex ‘V3’
The shortest path form’s’ to ‘V3’ is a path of length 0.

STEP 2: DEQUEUED V3

45
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

1 2 DEQUEUED V3
V Known Dv pv
V1 0 1 V3
4 5 V2 0 ∞ 0
3
V3 1 0 0
V4 0 ∞ 0
6 7 V5 0 ∞ 0
V6 0 1 V3
V7 0 ∞ 0
ENQUEUE V1,V6

Dequeue V3 and assign the value (known=1) then find its adjacency vertices.
adjacency vertices [V3]= V1, V6 enqueue it.

If distance of adjacent vertices is equal to infinity then change the distance


T[V1].dist=T[V3].distance+1
= 0+1=1
T[V1].path=V3

T[V6].dist=T[V3].distance+1
= 0+1=1
T[V6].path=V3

STEP 3: DEQUEUED V1

1 2 DEQUEUED V1
V Known Dv pv
V1 1 1 V3
V2 0 2 V1
4 5 V3 1 0 0
3
4 0 2 V1
V5 0 ∞ 0
V6 0 1 V3
6 7 V7 0 ∞ 0
ENQUEUE V6,V2,V4

Dequeue V1 and assign the value (known=1) then find its adjacency vertices.
adjacency vertices [V1]= V2, V4 enqueue it.
If distance of adjacent vertices is equal to infinity then change the distance
T[V2].dist=T[V3].distance+1
= 1+1=2
T[V2].path=V1
T[V4].dist=T[V3].distance+1
= 1+1=2
46
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

T[V4].path=V1
STEP 4: DEQUEUED V6

DEQUEUED V6
1 2
V Known Dv pv
V1 1 1 V3
V2 0 2 V1
4 V3 1 0 0
3 5 V4 0 2 V1
V5 0 ∞ 0
V6 1 1 V3
6 7 V7 0 ∞ 0
ENQUEUE V2,V4
Dequeue V6 and assign the value (known=1) then find its adjacency vertices.
adjacency vertices [V3]= no adjacency enqueue it.
STEP 5: DEQUEUED V2
DEQUEUED V2
1 2 V Known Dv pv
V1 1 1 V3
V2 1 2 V1
V3 1 0 0
4 5 V4 0 2 V1
3
V5 0 3 V2
V6 1 1 V3
V7 0 ∞ 0
6 7 ENQUEUE V4,V5
Dequeue V2 and assign the value (known=1) then find its adjacency vertices.
adjacency vertices [V2]= V4, V5

If distance of adjacent vertices is equal to infinity then change the distance


T[V4].dist=T[V2].distance+1
= 2+1=3
T[V4].path=V2

T[V5].dist=T[V3].distance+1
= 2+1=3
T[V5].path=V2
STEP 6: DEQUEUED V4

1 2 DEQUEUED V4
V Known Dv pv
V1 1 1 V3
4 V2 1 2 V1
3 5
V3 1 0 0

6 7 47
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

V4 1 2 V1
V5 0 3 V2
V6 1 1 V3
V7 0 3 V4
ENQUEUE V5,V7

Dequeue V4 and assign the value (known=1) then find its adjacency vertices.
adjacency vertices [V4]= V3, V5,V6,V7

If distance of adjacent vertices is equal to infinity then change the distance


T[V3].dist=T[V4].distance+1
= 2+1=3
T[V3].path=V4

T[V5].dist=T[V4].distance+1
= 2+1=3
T[V5].path=V4

T[V6].dist=T[V4].distance+1
= 2+1=3
T[V6].path=V4

T[V7].dist=T[V4].distance+1
= 2+1=3
T[V7].path=V4

STEP 7: DEQUEUED V5
DEQUEUED V5
V Known Dv pv
1 2
V1 1 1 V3
V2 1 2 V1
V3 1 0 0
4 V4 1 2 V1
3 5 V5 1 3 V2
V6 1 1 V3
V7 0 3 V4
6 7 ENQUEUE V7

Dequeue V5 and assign the value (known=1) then find its adjacency vertices.
adjacency vertices [V5]= V7

If distance of adjacent vertices is equal to infinity then change the distance


48
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

T[V7].dist=T[V5].distance+1
= 3+1=4
T[V7].path=V5
STEP 8: DEQUEUED V7

1 2

DE 4 5 QUEUED V7
3
V Known Dv pv
V1 1 1 V3
6 7 V2 1 2 V1
V3 1 0 0
V4 1 2 V1
V5 1 3 V2
V6 1 1 V3
V7 1 3 V4
ENQUEUE empty
Dequeue V7 and assign the value (known=1) then find its adjacency vertices.
adjacency vertices [V7]=V6
If distance of adjacent vertices is equal to infinity then change the distance
T[V6].dist=T[V7].distance+1
= 3+1=4
T[V1].path=V7
Data changes during the unweighted shortest path algorithm.
The shortest distance from the source vertex V3 to all other vertex is listed below:
V3->V1 is 1
V3->V2 is 2
V3->V4 is 2
V3->V5 is 3
V3->V6 is 1
V3->V7 is 3
ROUTINE FOR ALGORITHM

Void unweighted (Table T)


{
Queue Q;
Vertex V,W;
Q = CreateQueue (Num vertex);
makeEmpty (Q);
EnQueue (s,Q);
While (!is empty (Q))
{
V= Dequeue (Q);
49
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

T[V].known = true;
For each W adjacent to V
If ( (T[W}.dist = = infinity)
{
T[W].dist = T[V].dist + 1;
T[W].path = V;
Enqueue (W,Q);
}}
Dispose queue (Q);
}
ANALYSIS

The running time of this algorithm is O (! E! +! V!).If adjacency list is used.

Where E->Edge & V->Vertices of the graph.

Weighted Directed graph

The input is a weighted graph: associated with each edge (vi, vj) is a cost ci,j to
traverse the arc. The cost of a path v1v2 ... vn is referred to as the weighted path length.

FORMULA: To find the adjacency distance value


VW
T[W].dist=Min[T[W].dist,T[V].dist+CVW]
T[W].path=V
13. UsingDijiktra’s algorithm, find the shortest path from the source to all nodes of
the graph ‘G’ given in the following figure. (APR/MAY 2023)

Consider the following direct graph.


1 2 2
1 3 10
4
2 4 2
3 5
5 8 4 6

6 7 50
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Step 1: Initial Configuration

The following table represents the initial configuration, assuming that the start node, S is
V1.

INITIAL STATE

2 V Known Dv pv
1 2
10 V1 0 0 0
4 1 3
V2 0 ∞ 0
2 2
4 5 V3 0 ∞ 0
3
8
V4 0 ∞ 0
5 6
6 7 V5 0 ∞ 0
1
V6 0 ∞ 0

V7 0 ∞ 0

Step 2

Select the minimum distance vertex from unknown vertex the first vertex selected is V1,
with path length 0.

After V1 is declared known


2
Vertex Known dv pv
1 2
10 V1 1 0 0
4 1 3

2 V2 0 2 V1
2
4 5
3 V3 0 ∞ 0
8
5 6 V4 0 1 V1
6 7 V5 0 ∞ 0
1
V6 0 ∞ 0

V7 0 ∞ 0

51
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Dequeue V1 and this vertex is marked ‘1’( known)then find its adjacency [Link]
vertices [V1]=V2,V4

V 1V2,V4

T[V2].dist =Min[T[V2].dist, T[V1].dist+CV1V2]

=Min[∞,0+2] =2

T[V2].path=V1

T[V4].dist =Min[T[V4].dist, T[V1].dist+CV1V4]

=Min[∞,0+1] =1

T[V4].path=V1

Step 3:

Next select V4 is minimum from unknown and marked known ‘1’.

After V4 is declared known

2 V Known Dv pv
1 2
10 V1 1 0 0
4 1 3
V2 0 2 V1
2 2
4 5 V3 0 3 V4
3
8
V4 1 1 V1
5 6
6 7 V5 0 3 V4
1
V6 0 9 V4

V7 0 ∞ 0

Dequeue V4 and this vertex is marked ‘1’( known)then find its adjacency vertices.

adjacency vertices [V4]=V3,V5,V6

V 4V3,V5,V6

T[V3].dist =Min[T[V3].dist, T[V4].dist+CV4V3]

=Min[∞,1+2] = Min[∞,3] =3

TV3].path=V4

52
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

T[V5].dist =Min[T[V5].dist, T[V4].dist+CV4V5]

=Min[∞,1+2] = Min[∞,3] =3

T[V5].path=V4

T[V6].dist =Min[T[V6].dist, T[V4].dist+CV4V6]

=Min[∞,1+8] = Min[∞,9] =9

T[V6].path=V4

Step 4: Next select V2 is minimum from unknown

After 2V2is declared known


1 2 V Known Dv pv
10
4 1 3
V1 1 0 0
2 2
4 5 V2 1 2 V1
3
8
V3 0 3 V4
5 6
V4 1 1 V1
6 7
1
V5 0 3 V4

V6 0 9 V4

V7 0 ∞ 0

Dequeue V2 and this vertex is marked ‘1’( known) then find its adjacency vertices.

adjacency vertices [V2]=V4,V5

V 2V4,V5

T[V4].dist =Min[T[V4].dist, T[V2].dist+CV2V4]

=Min[1,2+3] = Min[1,5]

=1

T[V5].dist =Min[T[V5].dist, T[V2].dist+CV2V5]

=Min[3,2+10] = Min[3,12]

=3

53
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Step 5: Next select V3 is minimum from unknown

After V3 is declared known


2 V Known Dv pv
1 2
10 V1 1 0 0
4 1 3

2
V2 1 2 V1
2
4 5
3 V3 1 3 V4
8
5 6 V4 1 1 V1
6 7 V5 0 3 V4
1
V6 0 8 V3

V7 0 ∞ 0

Dequeue V3 and this vertex is marked ‘1’( known) then find its adjacency vertices.

adjacency vertices [V3]=V1,V6

V 3V1,V6

T[V1].dist =Min[T[V1].dist, T[V3].dist+CV3V1]

=Min[0,3+4] = Min[0,7] =0

T[V6].dist =Min[T[V6].dist, T[V3].dist+C V3V6]

=Min[9,3+5] = Min[9,8] =8

T[V6].path=V3

Step 6:

Next select V5 is minimum from unknown

After V5 is declared known


2
V Known Dv pv
1 2
10
4 1 3 V1 1 0 0

2 2 V2 1 2 V1
4 5
3 V3 1 3 V4
8

5 6
6 7 54
1 ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
PREPARED BY: [Link]
CS3401 ALGORITHMS UNIT 2 MEC

V4 1 1 V1

V5 1 3 V4

V6 0 8 V3

V7 0 9 V5

Dequeue V5 and this vertex is marked ‘1’( known) then find its adjacency vertices.

adjacency vertices [V5]=V7

V 5V7
T[V7].dist =Min[T[V7].dist, T[V5].dist+CV5V7]
=Min[∞,3+6] = Min[∞,9]
=9
T[V7].path=V5
Step 7: Next select V6 is minimum from unknown
2
1 2
1
4 1 3
0
2 2
4 5
3
8 After V6 is declared known
5 6 V Known Dv pv
6 7
1 V1 1 0 0

V2 1 2 V1

V3 1 3 V4

V4 1 1 V1

V5 1 3 V4

V6 1 6 V3

V7 0 9 V5

Dequeue V6 and this vertex is marked ‘1’( known) then find its adjacency vertices.

No adjacency vertice for V6.

Step 8: Next select V7 is minimum from unknown

55
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

2 V Known Dv pv
1 2
10 V1 1 0 0
4 1 3
V2 1 2 V1
2 2
4 5 V3 1 3 V4
3
8
V4 1 1 V1
5 6
6 7 V5 1 3 V4
1
V6 1 6 V7

V7 1 9 V5

V 7V6

T[V7].dist =Min[T[V7].dist, T[V5].dist+CV5V7]

=Min[9,3+6]

=9

The shortest distance from the source vertex V1 to all other vertex is listed below:

V1 -> V2 is 2 V1 -> V3 is 3 V1 -> V4 is 1

V1 -> V5 is 3 V1 -> V6 is 8 V1 -> V7 is 9

ROUTINE FOR ALGORITHM


Void Dijkstra(Graph G, Table T)
{
Int i:
Vertex V, W;
Read graph (G, T) \* read graph from adjacency list *\
\ * Table initialization *\
For (I =0; i<num vertex; i++)
{
T[i].known = false;
T[i].Dist = infinity;
T[i].path = Not A vertex;
}
T [start]. Dist = 0;
For (;;)
{
V = smallest unknown distance vertex;
56
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

If (V==not a vertex)
Break;
T [V}].known = true;
For each w adjacent to V
If (! T [W].known)
{
T [W].dist = min [T [W].dist, T [V].dist + C v w]
T [W].path = V;
}}}

ANALYSIS OF THE DIJKSTRA’S ALGORITHM

The total running time of this algorithm is O (|E|+|V| 2) = O(|V|2 )

13. Explain the Warshall’s Algorithm with an example.

Warshall’s algorithm constructs the transitive closure of given diagraph with n vertices
through a series of n × n boolean matrices.
The computations in Warshall’s algorithm are given by following sequence,
R(0), . . . , R(k−1), R(k), . . . R(n).

Thus the idea inWarshall’s algorithm is building of boolean matrices.


Digraph : the graph in which all the edges are directed then it is called digraph or directed
graph .

Adjacency matrix : it is a representation of a graph by using matxi. If there exists an edge


between the vertices Vi and vj directing from vi to vj then entry in adjacency matrix in ith
row and jth colums is 1

Edge from b to d

adjacency matrix.
Transitive closure : Transitive closure is basically a boolean matrix ( matrix with 0 and 1
values ) in which the existence of directed paths of arbitrary lengths between vertices is
mentioned.

57
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

transitive closure.
The transitive closure can be generated with Depth First Search (DFS) or with Breadh
First Search (BFS).
This traversing can be done on any vertex.
While computing transitive closure we have to start with some vertex and have to find all
the edges which are reachable to every other vertex . the reachable edges for all the
vertices has to obtained
Procedure to be followed :
 Start with computation of R(0). In R(0) any path with intermediate vertices is not
allowed . the means only direct edges towards the vertices are considered. in other
words the path length of one edge is allowed in R (0). Thus V R(0) is adjacency matrix
for the diagraph.
 Construct R(1) in which first vertex is used as intermediate vertex and a path length
of two edges is allowed. Note that R(1) is build using R(0) which is already computed.
 Go on building R(k) by adding one intermediate vertex each time and with more
path length . each R(k) has to be built from R(k-1).
 The last matrix in this series is R(n), in thisR(n) all yhe n verticesare used as
intermediate vertices . and the R(n) which is obtained is nothing but the transitive
closure of given digraph.
 Let us understand this algorithm with some example
 Obtain the transitive closure for the following digraph using Warshall’s algorithm.

 Let us first obtain adjacency matrix for given digraph .it is denoted by R(0) .

58
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Algorithm :

Algorithm Warshall( matrix [1..n, 1..n]

//problem description : this algorithm is for computing

//transitive closure using warshall’s algorithm

//input: the adjacency matrix given by matrix [ 1..n , 1..n]

//Output : the transitive closure of digraph

R(0 )← Matrix // initially adjacency matrix of

//diagraph becomes R(0 )

59
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

for (k ← 1 to n ) do
{

for(i ← 1 to n ) do
{
for (j ← 1 to n ) do
{
R(k) [ i, j] ← R(k-1) [ i, j] OR R(k-1) [ i, k]
AND R(k-1) [ k, j]
}}}
return R(n)
V A B C d Analysis:

Clearly A 0 ∞ 3 ∞ time complexity of above algorithm is Θ(n3)


because in above algorithm the basic operation is
B 2 0 ∞ ∞ computation of R(k) [ i, j].

This C ∞ 7 0 1 operation is located within three nested for


loops.
D 6 ∞ ∞ 0

The time complexity warshall ‘s algorithm is Θ(n3)

14. Explain the Floyd’s algorithm with an example.

Floyd’s algorithm is used for finding the shortest path between every pair of vertices of a
graph. It is all pairs shortest path algorithm.
The algorithm works for both directed and undirected graphs. This algorithm is invented
by R. Floyd hence is the name.
Weighted graph: the weighted graph is a graph in which weights or distances are given
along the edges. The weighted graph can be represented by weighted matrix as follows,

Here

w[i][j] = 0 if i=j

W[i][j] =∞ if there is no edge ( directed edge) between i

and j .

60
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

W[i][j] = weight of edge.

Formulation:

Let ,Dk [i,j] denotes the weight of shortest path from vi to vj using {v1 , v2, v3…vk} as
intermediate vertices.

Initially D(k) is computed as weighted matrix

There exits two case –

1. A shortest path from vi to vjwith intermediate vertices from {v1 , v2, v3…vk} that
does not use vk. in this case
Dk [i,j] = D(k-1)[i,j]
2. A shortest path from vi to vjrestricted to using intermediate vertices {v 1 , v2, v3…vk}
which uses vk. in this case-
Dk [i,j] = D(k-1) [i,k] + D(k-1) [k,j]
The graphical representation of these two case is shortest path using vertices from

{v1 , v2, v3…vk}

Basic concept of Floyd’s algorithm:

1. The Floyd’s algorithm is for computing shortest path between every pair of vertices
of graph.
2. The graph may contain negative edges but it should not contain negative cycles.
3. The Floyds algorithm requires a weighted graph.
4. Floyd’s algorithm computes the distance matrix of a weighted graph with n vertices
through a series of n × n matrices :
D(0), . . . , D(k−1), D(k), . . . , D(n).
5. In each matrix D(k) the shortest distance “dij” has to be computed between vertex vi
and vj
6. In particular the series starts withD(0) with no intermediate vertex. That means D(0)
is a matrix in which vi and [Link] row and jth column contains the weights given by
direct edges . in D(1) matrix – the shortest distance going through one intermediate
vertex ( starting vertex as intermediate) with maximum path length of 2 edges is
given continuing in this fashion we will compute D (n), contains the lengths of

61
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

shortest paths among all paths that can use all n vertices as intermediate. Thus we
get all pair shortest paths from matrix D(n)
Obtain the all pair – shortest path using Floyd’s algorithm for the

following weighted graph,

Algorithm:

ALGORITHM Floyd(W[1..n, 1..n])

//Implements Floyd’s algorithm for the all-pairs shortest-paths //problem

//Input: The weight matrix W of a graph with no negative-length //cycle

//Output: The distance matrix of the shortest paths’ lengths

D ←W //is not necessary if W can be overwritten

fork←1 to n do
62
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

fori ←1 to ndo
{
forj ←1 to n do
{
D[i, j ]←min{D[i, j ], D[i, k]+ D[k, j]}
}
}
}
returnD
Analysis :
In the above given algorithm the basic operation is –

D[i, j ]←min{D[i, j ], D[i, k]+ D[k, j]}

This operation is within three nested for loops , we can write


𝑛
C(n) = ∑𝑛𝑘=1 ∑ ∑𝑛𝑗=1 1
𝑖=1
𝑛
C(n) = ∑𝑛𝑘=1 ∑𝑖=1(n − 1 + 1) therefore∑𝑛𝑖=1 1 = u − 1 + 1

C(n) = ∑𝑛𝑘=1 ∑𝑛𝑖=1 n

C(n) = ∑𝑛𝑘=1 n2

C(n) = n3

The time complexity of finding all pair shortest path is Θ (n3)

15. Explain the Bellman-Ford algorithm in detail with an [Link]/May 2024


The Bellman–Ford algorithm is an algorithm that computes shortest paths from a
single source vertex to all of the other vertices in a weighted digraph.

If a graph G = (V, E) contains a negative-weight cycle, then some shortest paths may
not exist.

Bellman-Ford algorithm finds all shortest-path lengths from a source s ∈ V to all v ∈


V or determines that a negative-weight cycle (i.e. a cycle whose edges sum to a negative
value) exists.

The relaxation procedure takes two nodes as arguments and an edge connecting
these nodes. If the distance from the source to the first node ( ) plus the edge length is
less than distance to the second node, than the first node is denoted as the predecessor of

63
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

the second node and the distance to the second node is recalculated (
). Otherwise no changes are applied.

The path from the source node to any other node can be at maximum
edges long, provided there is no cycle of negative length. Hence if we perform for all nodes
the relaxation operation , than the algorithm will find all shortest paths. We will
verify the output by running the relaxation once more – if some edge will be relaxed, than
the algorithm contains a cycle of negative length and the output is invalid. Otherwise the
output is valid and the algorithm can return shortest path tree.

Properties of Bellman-Ford Algorithm:

 Works for negative weights


 Detects a negative cycle if any exist
 Finds shortest simple path if no negative cycle exists
 Bellman–Ford is based on the principle of relaxation
Negative Cycle: Directed cycle whose sum of edges is negative

Bellman –Ford Algorithm:

The bellman-Ford algorithm solves the single source shortest path problems even in
the cases in which edge weights are negative. This algorithm returns a Boolean value
indicating whether or not there is a negative weight cycle that is reachable from the source.
If there is such a cycle, the algorithm indicates that no solution exists and it there is no such
cycle, it produces the shortest path and their weights.

Algorithm:

Input Format: Graph is directed and weighted. First two integers must be number of
vertices and edges which must be followed by pairs of vertices which has an edge between
them.

 maxVertices represents maximum number of vertices that can be present in the


graph.
 vertices represent number of vertices and edges represent number of edges in the
graph.
 graph[i][j] represent the weight of edge joining i and j.
 size[maxVertices] is initialed to{0}, represents the size of every vertex i.e. the number
of edges corresponding to the vertex.
64
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

 cost[maxVertices][maxVertices] represents the cost of going from one vertex to


another.
 visited[maxVertices]={0} represents the vertex that have been visited.
 Initialize the graph and input the source vertex.
 BellmanFord function is called to get the shortest path.
Implementation of Bell Ford-Algorithm:

This function takes the graph obtained (graph[ ][maxVertices]), cost


(cost[][maxVertices]) of going from one vertex to other, size (size[maxVertices]) of
vertices, source vertex and the total number of vertices (vertices) as arguments.

Algorithm:

void BellmanFord(int graph[ ][maxVertices],int cost[][maxVertices],int size[],intsource,int


vertices)

int distance[vertices];

intiter,jter,from,to;

for(iter=0;iter<vertices;iter++)

distance[iter] = INF;

distance[source] = 0;

/* We have to repeatedly update the distance |V|-1 times where |V| represents

number of vertices */

for(iter=0;iter<vertices-1;iter++)

for(from=0;from<vertices;from++)

for(jter=0;jter<size[from];jter++)

to = graph[from][jter];

if(distance[from] + cost[from][jter] < distance[to])

65
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

distance[to] = distance[from] + cost[from][jter];

}
}
}
}
for(iter=0;iter<vertices;iter++)
{
printf("The shortest distance to %d is %d\n",iter,distance[iter]);
}
}

66
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

ASYMPTOTIC COMPLEXITY

The Bellman-Ford algorithm runs in time O(VE), since the initialization takes Θ(V)
time, each ofthe |V| - 1 passes over the edges takes O(E) time and calculating the distance
takes O(E) times.

The asymptotic complexity of Bellman-Ford algorithm is , because the inner


loop is performed and the inner loop iterates over all edges of the graph.

16. Explain Network flow and flow network.

In combinatorial optimization, network flow problems are a class of computational


problems in which the input is a flow network (a graph with numerical capacities on its
edges), and the goal is to construct a flow, numerical values on each edge that respect the
capacity constraints and that have incoming flow equal to outgoing flow at all vertices
except for certain designated terminals.
Specific types of network flow problems include:

 The maximum flow problem, in which the goal is to maximize the total amount of flow
out of the source terminals and into the sink terminals
 The minimum-cost flow problem, in which the edges have costs as well as capacities
and the goal is to achieve a given amount of flow (or a maximum flow) that has the
minimum possible cost
 The multi-commodity flow problem, in which one must construct multiple flows for
different commodities whose total flow amounts together respect the capacities
 Nowhere-zero flow, a type of flow studied in combinatory in which the flow amounts
are restricted to a finite set of nonzero values

67
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

The max-flow min-cut theorem equates the value of a maximum flow to the value of
a minimum cut, a partition of the vertices of the flow network that minimizes the total
capacity of edges crossing from one side of the partition to the other. Approximate max-
flow min-cut theorems provide an extension of this result to multi-commodity flow
problems. The Gomory–Hu tree of an undirected flow network provides a concise
representation of all minimum cuts between different pairs of terminal vertices.
Algorithms for constructing flows include

 Dinic's algorithm, a strongly polynomial algorithm for maximum flow


 The Edmonds–Karp algorithm, a faster strongly polynomial algorithm for maximum
flow
 The Ford–Fulkerson algorithm, a greedy algorithm for maximum flow that is not
in general strongly polynomial
 The network simplex algorithm, a method based on linear programming but specialized
for network flow
 The out-of-kilter algorithm for minimum-cost flow
 The push–relabel maximum flow algorithm, one of the most efficient known techniques
for maximum flow
Otherwise the problem can be formulated as a more conventional linear program or similar
and solved using a general purpose optimization solver.

Flow Network

In graph theory, a flow network is defined as directed graph G= (V,E) constrained with a
function c, which bounds each edge e with a non-negative integer value which is known
as capacity of the edge e with two additional vertices defined as source S and sink T.

As shown in the flow network given below, a source vertex has all outgoing edges and no
incoming edges, more formally we can say Indegree[source]=0 and sink vertex has all
incoming edges and no outgoing edge more formally outdegree[sink]=0

Also, any flow network should satisfy all the underlying conditions --

68
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

 For all the vertices (except the source and the sink vertex), input flow must be equal
to output flow.
 For any given edge Ei) in the flow network, 0≤flow(Ei)≤Capacity)≤ (Ei) hold, we
cannot send more flow through an edge than its capacity.
 Total outflow from the source vertex must be equal to total inflow to the sink vertex.

17. Explain in detail about algorithm for maximum flow problem Apr/May 2024

 Given a flow network G with source s and sink t, the maximum flow problem is
an optimization problem to find a flow of maximum value from s to t. Flow
network G=(V, E), is essentially just a directed graph where each edge has a
nonnegative flow capacity.

The Ford Fulkerson Method

Ford-Fulkerson algorithm is a greedy approach for calculating the maximum possible flow
in a network or a graph.
A term, flow network, is used to describe a network of vertices and edges with a source (S)
and a sink (T). Each vertex, except S and T, can receive and send an equal amount of stuff
through it. S can only send and T can only receive stuff.
We can visualize the understanding of the algorithm using a flow of liquid inside a network
of pipes of different capacities. Each pipe has a certain capacity of liquid it can transfer at
an instance. For this algorithm, we are going to find how much liquid can be flowed from
the source to the sink at an instance using the network.

Terminologies Used
Augmenting Path
It is the path available in a flow network.

Residual Graph
It represents the flow network that has additional possible flow.

69
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Residual Capacity
It is the capacity of the edge after subtracting the flow from the maximum capacity.

The algorithm follows:

1. Initialize the flow in all the edges to 0.


2. While there is an augmenting path between the source and the sink, add this path to the
flow.
3. Update the residual graph.
Ford-Fulkerson Example
The flow of all the edges is 0 at the beginning.

1. Select any arbitrary path from S to T. In this step, we have selected path S-A-B-T.

2. Find a path
The minimum capacity among the three edges is 2 (B-T). Based on this, update
the flow/capacity for each [Link] the capacities

70
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

3. Select another path S-D-C-T. The minimum capacity among these edges is 3 (S-D).

4. Findnextpath

5. Update the capacities according to this. Update the capacities


6. Now, let us consider the reverse-path B-D as well. Selecting path S-A-B-D-C-T.
The minimum residual capacity among the edges is 1 (D-C).

7. Find next path

8. Updating the capacities. Thecapacity for forward and reverse paths is considered
separately.
71
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

9. Adding all the flows = 2 + 3 + 1 = 6, which is the maximum possible flow on the flow
network.

Note that if the capacity for any edge is full, then that path cannot be used.

Algorithm
FORD-FULKERSON METHOD (G, s, t)
[Link] flow f to 0
2. while there exists an augmenting path p
3. do argument flow f along p
4. Return f
FORD-FULKERSON (G, s, t)
1. for each edge (u, v) ∈ E [G]
2. do f [u, v] ← 0
3. f [u, v] ← 0
4. while there exists a path p from s to t in the residual network Gf.
5. docf (p)←min?{ Cf (u,v):(u,v)is on p}
6. for each edge (u, v) in p
7. do f [u, v] ← f [u, v] + cf (p)
8. f [u, v] ←-f[u,v]
Example 2: let us first find the augmenting path.

Step 1:

o The augmenting path is marked by thick line.


o This is a path which gives maximum flow.
o Now we will find the residual capacity cf(p).
o Find out the minimum value along the residual path.
o Here it is 4 i.e. c(2,4).
o Now we will design a residual graph by considering residual
capacity 4.

7 5
3

8 3
t
s 2

2 4 6

2 4
72
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

We will apply while loop of above given algorithm.

Step2: here along the augmenting path the residual capacity is c(3,t) =5 . hence we
can draw residual network.

Step3: now we will again mark augmenting path for maximum flow.
Here cf=c(2,3) =3

Hence the residual graph is as follow:

This is the residual graph, we will now find the augmenting path giving maximum
flow from source to sink. Here the only remaining path is s-1-4-t.

The cf=(s,1)=2

Step 4:

Step 5: Now there is no path from s to t in following graph, hence we will exit the
while loop.

Hence the graph marked by thick line in step 4 is a maximum flow.

Now once again consider the original graph which we have taken for discussing above
example.

73
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Here the cut is shown by dotted line Hence Cut = (s,3),(s,2),(s,1)}

When the graph has maximum flow then it gives minimum cut which is as shown below.

Analysis : The algorithm for Ford-Fulkerson has a while loop which executes for O (E).
Hence running time of Ford-Fulkerson algorithm is O(EF*) where F* is the maximum flow
found b algorithm.

18. Explain the Maximum Matching in Bipartite Graph algorithm with supporting example
Aprl/May 2024

Bipartite Graph:

The graph G = (V, E) in which the vertex set V is divided into two disjoint sets X and Y in
such a way that every edge e € E has one end point in X and other end point in Y.

For example

74
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Matching:

A matching M is a subset of edges such that each node in V appears in at most one
edge in M. In other words matching in a graph is a subset of edges that no two edges
share a vertex.

Two-colorable Graph:

A graph can be colored with only two colors (i.e. two colorable graph) such that no
edge connects the same color. The bi-partite graph is 2-colorable.

Free vertex:

µ € V is a fee vertex, if no edge from matching M os incident to v (that means if v is


not matched).

Alternating path:

The alternating path P is a path in graph G, such that for every pair of subsequent
edges one of them is matching pair M and other is not.

Augmenting path:

The augmenting path P is a path in graph G, such that it is an alternating path with
special property that its start and end vertices are free or unmatched.

Theorem: A matching M is a maximum matching if and only if there exists no augmenting


path with respect to M.

Algorithm Maximum Bipartite Matching(G)

initialize set M of edges // can be the empty set

initialize queue Q with all the free vertices in V

while not Empty (Q) do

w <Front(Q)
75
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

if w ε V then

for every vertex u adjacent to w do // u must be in U

if u free then // initialize set M of edges // can be the empty set

initialize queue Q with all the free vertices in V

while not Empty (Q) do

w <Front(Q)

if w ε V then

for every vertex u adjacent to w do // u must be in U

augment

M< M union (w, u)

v< w

while v is labeled do // follow the augmenting path

u< label of v

M< M – (v, ) // (v, u) was in pervious M

V< label of u

M< M union (v, u) // add the edge to the path

// start over vertex labels

reinitialize Q with

remove all

all the free vertices in V

break // exit for loop

else// u is matched

if (w, u) not in M and u is unlabeled then

label u with w // represents an edge in E-M

Enqueue(Q, u)

// only way for a U vertex to enter the queue

else // w ƹ U and therefore is matched with v


76
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

V <w’s mate // (w, v) is in M

Label v with w // represents in M

Enqueue(Q, v) // only way for a mated v to enter Q

Return M // maximum matching

Application of Algorithm

Step 1:

Step 2:

Step 3: Augmenting from 2

Step 4: Augmenting from 5

77
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Step 5:

This is also a perfect matching.

Example: Apply the maximum matching algorithm to Following bi-


partite graph.

Solution:

Step 1: Step 2:

Step 3: Step 4:

78
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Step 5: Step 6:

Step 7:

19. Write the pseudocode for BFS and DFS traversals on the graph given below and
compare the time and space complexity of the two traversals. Apr/May 2024

Fig 12.a(i)
Breadth-First Search (BFS) Pseudocode
BFS explores the graph level by level using a queue (FIFO structure).

BFS(Graph, start_node):

Create an empty queue Q

Create an empty set visited

Enqueue start_node to Q

Add start_node to visited

while Q is not empty:

node = Dequeue from Q

Process node (print or store result)


79
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

for each neighbor in adjacency_list[node]:

if neighbor is not in visited:

Enqueue neighbor to Q

Add neighbor to visited

Depth-First Search (DFS) Pseudocode

DFS explores as deeply as possible before backtracking, using either recursion (implicit stack)
or an explicit stack (LIFO structure).

Recursive DFS

DFS(Graph, node, visited):

if node is not in visited:

Process node (print or store result)

Add node to visited

for each neighbor in adjacency_list[node]:

DFS(Graph, neighbor, visited)

Iterative DFS using a Stack

DFS(Graph, start_node):

Create an empty stack S

Create an empty set visited

Push start_node to S

while S is not empty:

node = Pop from S

if node is not in visited:

Process node (print or store result)

Add node to visited

for each neighbor in adjacency_list[node] (in reverse order for proper order of traversal):
80
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

if neighbor is not in visited:

Push neighbor to S

Comparison of Time and Space Complexity


Algorithm Time Complexity Space Complexity Data Structure

BFS O(V + E) O(V) (for queue & visited set) Queue (FIFO) Key
Points:
DFS (Recursive) O(V + E) O(V) (call stack in worst case) Stack (LIFO via recursion)
 BFS
DFS (Iterative) O(V + E) O(V) (for stack & visited set) Stack (LIFO) is ideal for
finding the
shortest
path in an unweighted graph.
 DFS is useful for exploring all paths and solving connectivity problems.
 DFS can have worse space complexity in the worst case (O(V) due to recursion depth).

20. Find the minimum spanning tree for the following graph using Kruskals algorithm Apr/May 2024

Solution Using Kruskal’s Algorithm

Kruskal’s algorithm is a greedy algorithm used to find the Minimum Spanning Tree (MST) of
a graph. It works by sorting edges by weight and adding them one by one while avoiding cycles.

Step 1: List All Edges with Their Weights

Extract the edges from the given graph:

Edge Weight

(a, b) 4

(a, h) 8

(b, h) 11

81
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Edge Weight

(b, c) 8

(h, i) 7

(h, g) 1

(i, g) 6

(c, d) 7

(g, f) 2

(d, f) 14

(d, e) 9

(f, e) 10

Step 2: Sort the Edges in Ascending Order

We sort the edges based on weight:

1. (h, g) - 1
2. (g, f) - 2
3. (a, b) - 4
4. (i, g) - 6
5. (c, d) - 7
6. (h, i) - 7
7. (b, c) - 8
8. (a, h) - 8
9. (d, e) - 9
10. (f, e) - 10
11. (b, h) - 11
12. (d, f) - 14

Step 3: Apply Kruskal’s Algorithm

We use the Union-Find data structure to add edges while ensuring there are no cycles.

1. Add (h, g) - 1 ✅
2. Add (g, f) - 2 ✅

82
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

3. Add (a, b) - 4 ✅
4. Add (i, g) - 6 ✅
5. Add (c, d) - 7 ✅
6. Add (h, i) - 7 ✅
7. Add (b, c) - 8 ✅
8. Add (d, e) - 9 ✅

Now we have V-1 = 8 edges (since we have 9 vertices, the MST should have 8 edges).

Step 4: MST Result

The Minimum Spanning Tree (MST) consists of the following edges:

Edge Weight

(h, g) 1

(g, f) 2

(a, b) 4

(i, g) 6

(c, d) 7

(h, i) 7

(b, c) 8

(d, e) 9

Step 5: Compute MST Weight

Total weight of the MST:

1+2+4+6+7+7+8+9=441 + 2 + 4 + 6 + 7 + 7 + 8 + 9 = 441+2+4+6+7+7+8+9=44

83
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

21. Given a graph and a source vertex in the graph, find the shortest paths from the source
vertex 0 to all vertices in the given graph Apr/May 2024

solution

[Link] kruskals algorithm and to find the minimum spanning tree for the following
[Link]/Dec 2024

Kruskal's Algorithm for Minimum Spanning Tree (MST)

Kruskal’s Algorithm Steps:

1. Sort all edges in ascending order of their weights.


2. Initialize an empty MST (Initially, no edges are included).
3. Pick the smallest edge and add it to the MST if it does not form a cycle.
4. Repeat step 3 until there are (V - 1) edges in the MST, where V is the number of vertices.
5. Return the MST and its total cost.

Edges included:

84
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

 (F,E) → 1
 (A, F) → 2
 (A, B) → 3
 (D, I) → 4
 (E, H) → 5
 (E, G) → 6
 (C, D) → 8
 (I, J) → 9
 (I, E) → 10

Total Weight of MST:

1 + 2 + 3+ 4+ 5 + 6 + 8 + 9 + 10 = 48

[Link] the given graph, the vertex represents the city and edge represents the cost between
the two vertices. Apply Dijikstra’s shortest algorithm and find the optimal cost to reach the
destination. Also determine the worst case time complexity of the algorithm. Nov/Dec 2024

Step 1: Initialize Distance Table

Set the initial distances:

 Distance from s to itself = 0


 Distance to all other vertices = ∞

Vertex Distance Previous Vertex

s 0 -

t ∞ -

x ∞ -

y ∞ -

z ∞ -

85
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Step 2: Visit Neighbors of s

From s, update distances to its neighbors:

 t: 0+10=100 + 10 = 100+10=10
 y: 0+5=50 + 5 = 50+5=5

Vertex Distance Previous Vertex

s 0 -

t 10 s

x ∞ -

y 5 s

z ∞ -

Mark s as visited.

Step 3: Visit Neighbors of y (Smallest Distance)

From y (5):

 t: 5+2=75 + 2 = 75+2=7 (better than 10)


 z: 5+2=75 + 2 = 75+2=7

Vertex Distance Previous Vertex

s 0 -

t 7 y

x ∞ -

y 5 s

z 7 y

Mark y as visited.

86
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Step 4: Visit Neighbors of t (Smallest Distance)

From t (7):

 x: 7+1=87 + 1 = 87+1=8
 z: 7+9=167 + 9 = 167+9=16 (not better than 7)

Vertex Distance Previous Vertex

s 0 -

t 7 y

x 8 t

y 5 s

z 7 y

Mark t as visited.

Step 5: Visit Neighbors of z (Smallest Distance)

From z (7):

 x: 7+4=117 + 4 = 117+4=11 (not better than 8)

No updates.

Vertex Distance Previous Vertex

s 0 -

t 7 y

x 8 t

y 5 s

z 7 y

Mark z as visited.

87
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Step 6: Visit Neighbors of x (Smallest Distance)

From x (8):

No new updates.

Vertex Distance Previous Vertex

s 0 -

t 7 y

x 8 t

y 5 s

z 7 y

Mark x as visited.

Final Optimal Costs

 t=7
 x=8
 y=5
 z=7

Time Complexity

Dijkstra’s algorithm runs in O(V²) using an adjacency matrix, where V is the number of vertices.
Using a priority queue (binary heap), it runs in O((V + E) log V).

For this graph:

 V = 5 (s, t, x, y, z)
 E = 6 (number of edges)

With a priority queue, the worst-case time complexity:


O((5 + 6) log 5) = O(11 log 5), which simplifies to O(E log V).

88
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

24..Apply ford Fulkerson algorithm for the following graph and determine the maximum flow in the
graph.(Nov/Dec2024)

Below is one systematic way to find the maximum flow using the (Edmond–Karp) Ford–
Fulkerson method. The network (with source V1V_{1}V1 and sink V6V_{6}V6) has edges and
capacities:

 V1→V2=16V_{1}\to V_{2} = 16V1→V2=16


 V1→V3=12V_{1}\to V_{3} = 12V1→V3=12
 V2→V3=10V_{2}\to V_{3} = 10V2→V3=10
 V2→V4=13V_{2}\to V_{4} = 13V2→V4=13
 V3→V4=9V_{3}\to V_{4} = 9V3→V4=9
 V3→V5=20V_{3}\to V_{5} = 20V3→V5=20
 V4→V5=7V_{4}\to V_{5} = 7V4→V5=7
 V4→V6=14V_{4}\to V_{6} = 14V4→V6=14
 V5→V6=4V_{5}\to V_{6} = 4V5→V6=4

We want the maximum flow from V1V_{1}V1 (source) to V6V_{6}V6 (sink).

1. Initialize all flows to 0

Initially, no flow is sent along any edge.

2. Find augmenting paths via BFS (Edmond–Karp)

We repeatedly look for a path from V1V_{1}V1 to V6V_{6}V6 in the residual network (i.e.,
along edges that still have available capacity > 0). Once found, we send flow equal to the
minimum residual capacity (the “bottleneck”) on that path, update the residual capacities, and
repeat until no augmenting path remains.

89
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Augmenting Path 1

 Search: A straightforward BFS finds V1 → V2 → V4 → V6


V_{1}\;\to\;V_{2}\;\to\;V_{4}\;\to\;V_{6}V1→V2→V4→V6 The capacities on that path are:
(V1→V2)=16,(V2→V4)=13,(V4→V6)=14. (V_{1}\to V_{2}) = 16,\quad (V_{2}\to V_{4}) = 13,\quad
(V_{4}\to V_{6}) = 14.(V1→V2)=16,(V2→V4)=13,(V4→V6)=14.
 Bottleneck: min⁡(16, 13, 14)=13\min(16,\,13,\,14) = 13min(16,13,14)=13.
 Send flow: Send 13 units along that path.

After this, the flow is 13 on (V1→V2)(V_{1}\to V_{2})(V1→V2), (V2→V4)(V_{2}\to


V_{4})(V2→V4), and (V4→V6)(V_{4}\to V_{6})(V4→V6).
Residual capacities become:

 V1→V2V_{1}\to V_{2}V1→V2 now has 16−13=316 - 13 = 316−13=3 left.


 V2→V4V_{2}\to V_{4}V2→V4 now has 13−13=013 - 13 = 013−13=0 left.
 V4→V6V_{4}\to V_{6}V4→V6 now has 14−13=114 - 13 = 114−13=1 left.
 Reverse edges (e.g.\ V2→V1V_{2}\to V_{1}V2→V1, V4→V2V_{4}\to V_{2}V4→V2, etc.) appear
with capacity equal to the flow just sent (13), allowing flow “undo” if beneficial in later steps.

Current total flow = 131313.

Augmenting Path 2

 Search: Next BFS on the residual network can find V1 → V3 → V4 → V6.


V_{1}\;\to\;V_{3}\;\to\;V_{4}\;\to\;V_{6}.V1→V3→V4→V6. Available capacities are:
(V1→V3)=12,(V3→V4)=9,(V4→V6)=1. (V_{1}\to V_{3}) = 12,\quad (V_{3}\to V_{4}) = 9,\quad
(V_{4}\to V_{6}) = 1.(V1→V3)=12,(V3→V4)=9,(V4→V6)=1.
 Bottleneck: min⁡(12, 9, 1)=1\min(12,\,9,\,1) = 1min(12,9,1)=1.
 Send flow: Send 1 unit along that path.

Residual capacities update accordingly:

 V1→V3V_{1}\to V_{3}V1→V3 becomes 12−1=1112 - 1 = 1112−1=11.


 V3→V4V_{3}\to V_{4}V3→V4 becomes 9−1=89 - 1 = 89−1=8.
 V4→V6V_{4}\to V_{6}V4→V6 becomes 1−1=01 - 1 = 01−1=0.

Current total flow = 13+1=1413 + 1 = 1413+1=14.

Augmenting Path 3

 Search: Another BFS finds V1 → V3 → V5 → V6. V_{1}\;\to\;V_{3}\;\to\;V_{5}\;\to\;V_{6}.V1


→V3→V5→V6. Residual capacities on that route: (V1→V3)=11,(V3→V5)=20,(V5→V6)=4.

90
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

(V_{1}\to V_{3}) = 11,\quad (V_{3}\to V_{5}) = 20,\quad (V_{5}\to V_{6}) = 4.(V1→V3)=11,(V3


→V5)=20,(V5→V6)=4.
 Bottleneck: min⁡(11, 20, 4)=4\min(11,\,20,\,4) = 4min(11,20,4)=4.
 Send flow: Send 4 units along that path.

Residual capacities update:

 V1→V3V_{1}\to V_{3}V1→V3 becomes 11−4=711 - 4 = 711−4=7.


 V3→V5V_{3}\to V_{5}V3→V5 becomes 20−4=1620 - 4 = 1620−4=16.
 V5→V6V_{5}\to V_{6}V5→V6 becomes 4−4=04 - 4 = 04−4=0.

Current total flow = 14+4=1814 + 4 = 1814+4=18.

Attempting another BFS

Now we try to find any further augmenting path from V1V_{1}V1 to V6V_{6}V6. In the
residual network:

 (V4→V6)(V_{4}\to V_{6})(V4→V6) is at 0 residual capacity (fully used).


 (V5→V6)(V_{5}\to V_{6})(V5→V6) is also at 0 residual capacity (fully used).

Hence there is no way to push additional flow into V6V_{6}V6. The BFS fails to find a path to
the sink. No more augmenting paths remain.

3. Conclude the Maximum Flow

The total flow we have found is


13 + 1 + 4 = 18. 13 \;+\; 1 \;+\; 4 \;=\; 18.13+1+4=18.

We can also confirm this by noting a cut of capacity 18: for instance, take
S={V1,V2,V3,V4,V5},T={V6}. S = \{V_{1}, V_{2}, V_{3}, V_{4}, V_{5}\}, \quad T = \{V_{6}\}.S={V1,V2,V3,V4
,V5},T={V6}.

All edges crossing from SSS to TTT are exactly V4→V6V_{4}\to V_{6}V4→V6 (capacity 14)
and V5→V6V_{5}\to V_{6}V5→V6 (capacity 4), summing to 18. Since no flow can exceed
any cut’s capacity, 18 is indeed the maximum.

Final Answer
The maximum flow in the given network is 18.

91
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

IMPORTANT QUESTION

PART-A

1. What is feasible solution and feasible region? Define optimal solution.


2 Define maximum flow problem.
3 Define flow netw
4 What is flow conservation requirement?
5 .Define Max-Flow Min-Cut Theorem.
6 . Define Max-Flow Min-Cut Theorem.
7 .Define Max-Flow Min-Cut Theorem.
8 . Define preflow.
9 .What is maximum cardinality matching?
10 . Define bipartite graph
11 .What is bipartite graph? Colorable graph?
12. What is maximum cardinality matching? Matching problem?
[Link] graph. (Nov/Dec2006) (Nov/Dec2007)
[Link] is the space requirement of an adjacency list representation of a graph?
(Nov/Dec2005)
15. Explain the topological sort. (May/June 2006) (APR/MAY 2010)
16. Prove that the number of odd degree vertices in a connected graph should be even.
(May/June 2007)
17. What is minimum spanning tree? Name any two algorithms used to find MST.
(Nov/Dec2007) (Nov/Dec2009)
18. What is an articulation point? (Nov/Dec2009) (APR/MAY 2010)
[Link] a graph is said to be bi-connected? (APR/MAY 2010)
20. Define spanning tree. (Nov/Dec2010)
[Link] is breadth-first traversal? (Nov/Dec2010)
[Link] is activity node graph? (NOV\DEC 2010)
23. Define indegree and outdegree of a graph. (APR/MAY 2010)
24. Represent the following graph as an adjacency matrix? (NOV\DEC 09)
[Link] the drawbacks of Floyd- Warshall Algorithm.
[Link] out the application of depth- first-search.
[Link] is the principle behind Bellman Ford algorithm to detect the negative weight
cycles?
[Link] are the different ways to represent the graph? Explain each of them
29. What does Floyd’s algorithm do ?

PART-B
1. Write algorithm for weighted and unweightedshorest paths. Explain the above
algorithms with suitable examples. (May/June 2006)

92
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

2. Explain briefly about the Dijkstra’s algorithm. obtain the single –source shortest
path for the following graph. (Nov/Dec2005) (Nov/Dec2006) (May/June 2006)
(Nov/Dec2007) (Nov/Dec2010) (Nov/Dec2010)
Find the shortest path from ‘a’ to ‘d’ using Dijkstra’s algorithm in the graph . (NOV\DEC
09) (APR/MAY2010)

3. Find a Minimum Spanning Tree for the graph. (Nov/Dec2005) or


Describe the prim’s Algorithm and kruskal’s algorithm with example.
(Nov/Dec2006) (May/June 2006) (May/June 2006) (Apr/May 2010)
4. Explain Depth first and breadth first traversal? (May/June 2006)

5. Consider the following 'graph. Determine the 'shortest distance to all other nodes
using Dijikstra's algorithm. Write Procedure. (10+3)

6. Determine the minimum spanningtree of a given Graph using Kruskal's algorithm.


Write Kruskal's MST algorithm. (Refer Class note)
7. Present the pseudocodes of the different graph traversal methods and demonstrate
with an example
8. Explain how transitive closure of a graph can be found using Warshalls algorithm
9. Illustrate the comparison of Floyd’s algorithm with Dijkstra’ algorithm (4)
[Link] the Minninum spanning tree for the given graph using both Prim’s and
Kruskal’s algorithm and write the algorithms ([Link].7) (16) (Refer Class note)

93
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

[Link] the Dijkstra’s algorithm for finding the shortest path following graph
12. Illustrate the working of Warshall’s algorithm

[Link] the minimum spanning tree for the following graph. (8)

1 6
A B C

3 5 1 4 2
1 4
D E F

14. Discuss any two applications of depth first search. (8)


[Link] the shortest path from each vertex to all other vertices. (Mention and use the
appropriate algorithms.
2

3 4

1 8 3
7 1
4 5
2
5 4
6
16. Assume the following keys form the Binary Search tree {50, 30, 60, 40, 35, 80, 90}.
Analyze the time complexity involved in searching the keys 90 and then 80, when the
given BST is converted into AVL or Splay tree. Identify the suitable tree data structure
for representing this data and
justify your answer with valid reasons.
94
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

[Link] the all-pair shortest-path problem for the digraph with the following weight
matrix(16)

18. Apply kruskal’s algorithm to find a minimum spanning tree of the following graph.(16)

19. Apply the shortest-augmenting-path algorithm to find a maximum flow and a minimum cut in
the following networks.

20. Explain the maximum flow problem in detail with example.


21. Write down the optimality condition and algorithmic implementation for finding M-
augmenting paths in bipartite graphs
APRIL/MAY 2024
UNIT -2
PART A
1. How a graph is represented? OR What are the Data structure used for representation of the
graphs? [Apr/May 2015] Nov/Dec 2018, Apr/May 2024 [Link] 4 [Link] 15
2 .Define a strongly connected graph. Apr/May 2024 [Link] 2 [Link] 6

PART B

1. Write the pseudocode for BFS and DFS traversals on the graph given below in fig. 12 (a) (i)
and compare the time and space complexity of the two traversals. Apr/May 2024 [Link] 78
[Link] 19

95
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

Fig 12.a(i)

2. Find the minimum spanning tree for the following graph using Kruskals algorithm Apr/May
2024 pg no 80 [Link] 20

3. Given a graph and a source vertex in the graph, find the shortest paths from the source
vertex 0 to all vertices in the given graph Apr/May 2024 [Link] 83 [Link] 21

4. Explain in detail about Ford–Fulkerson algorithmfor maximum flow problem. The


maximum flow problem Apr/May 2024 [Link] 68 [Link] 17

PART C

(b) iRun the Bellman-Ford algorithm on the directed graph of figure below using vertex s as the source and show
the results after each pass of an algorithm. Refer Class work, [Link] 63, [Link] 15

96
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 2 MEC

ii With an example, Show that the cardinality of a maximum matching M in a bipartite graph G
equals the value of a maximum flow f in its corresponding flow network G'. [Link] 74 , [Link] 18

Nov/Dec 2024
PART-A
[Link] the graph traversal techniques.([Link] 10)
[Link] indegree and outdegree. .([Link] 20)
PART-B
[Link] kruskals algorithm and to find the minimum spanning tree for the following graph. ([Link] 22)

[Link] the given graph, the vertex represents the city and edge represents the cost between the two
vertices. Apply Dijikstra’s shortest algorithm and find the optimal cost to reach the destination.
Also determine the worst case time complexity of the algorithm. ([Link] 23)

PART-C
[Link] ford Fulkerson algorithm for the following graph and determine the maximum flow in the graph.
([Link] 24)

97
PREPARED BY: [Link] ASP/CSE [Link] AP/CSE , [Link] PRIYA ,AP/CSE
CS3401 ALGORITHMS UNIT 3 MEC

UNIT-III
ALGORITHM DESIGN TECHNIQUES 9

Divide and Conquer methodology: Finding maximum and minimum - Merge sort - Quick
sort Dynamic programming: Elements of dynamic programming — Matrix-chain
multiplication - Multi stage graph — Optimal Binary Search Trees. Greedy Technique:
Elements of the greedy strategy - Activity-selection problem –- Optimal Merge pattern —
Huffman Trees.

PART –A

1. Define the divide and conquer method. Nov/Dec 2024, Apr/May 2024

Divide & conquer technique is a top-down approach to solve a problem.

The algorithm which follows divide and conquer technique involves 3 steps:

 Divide the original problem into a set of sub problems.

 Conquer (or Solve) every sub-problem individually, recursive.

 Combine the solutions of these sub problems to get the solution of original problem.

2. What is the binary search?

Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the search
interval in half. The idea of binary search is to use the information that the array is sorted and
reduce the time complexity to O(Log n).

3. What is the time complexity of Binary search? June 2011 & 12

The time complexity of the binary search algorithm is O(log n). The best-case time complexity
would be O(1) when the central index would directly match the desired value.

4. Define external path length.

The root has a path length of zero and the maximum path length in a tree is called the tree's
height. The sum of the path lengths of a tree's internal nodes is called the internal path and the
sum of the path lengths of a tree's external nodes is called the external path.

5. Define internal path length.

The sum over all internal (circular) nodes of the paths from the root of an extended binary tree to
each node. For example, in the tree above, the internal path length is 11 (Knuth 1997, pp. 399-
400).

6. Is insertion sort better on the merge sort.

Insertion Sort is preferred for fewer elements. It becomes fast when data is already sorted or

1
CS3401 ALGORITHMS UNIT 3 MEC

nearly sorted because it skips the sorted values.

Efficiency: Considering average time complexity of both algorithms we can say that Merge Sort is
efficient in terms of time and Insertion Sort is efficient in terms of space.

7. Give the recurrence relation of divide-and-conquer.

The recurrence relation is

T(n)= g(n) n is small

T(n1)+T(n2)+ T(nk)+f(n) otherwise

Where T(n) is the time for DAndC on any input of size n and g(n) is the time to compute
the answer directly for small inputs.
The function f(n) is the time for dividing P and combining the solutions to subproblems.

8. Give the recurrence equation for the worst case behavior of merge sort.

The recurrence equation for the worst case behavior of merge

sort is T(n) = 2T(n/2) + cn for n>1, c is a constant

Total number of comparison required by the merge sort is Θ(n logn)

9. Find the number of comparisons made by the sequential search in the worst case and
best case.

Worst case: The algorithm makes the largest number of key comparisons among all
possible

input of size n. Cworst(n)=n

Best Case: The best case inputs will be lists of size n with their first element equal to
search key. Cbest(n)=1

10. What are the objectives of sorting algorithms?

 A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-
used orders are numerical order and lexicographical order.

 Efficient sorting is important for optimizing the use of other algorithms (such as search and
merge algorithms) that require sorted lists to work correctly;

 More formally, the output must satisfy two conditions: The output is in non-decreasing
order (each element is no smaller than the previous element according to the desired total
order)

 The output is a permutation (reordering) of the input.

2
CS3401 ALGORITHMS UNIT 3 MEC

11. What do you meant by Divide and conquer strategy? May 2013

Divide & conquer technique is a top-down approach to solve a problem. The algorithm which
follows divide and conquer technique involves 3 steps:

 Divide the original problem into a set of sub problems.


 Conquer (or Solve) every sub-problem individually, recursive.
 Combine the solutions of these sub problems to get the solution of original problem.
12. What are the merits of binary search?

 A binary search or half-interval searchalgorithm finds the position of a specified


value (the input "key") within a sorted array.
 In each step, the algorithm compares the input key value with the key value of the
middle element of the array.
 A binary search halves the number of items to check with each iteration, so locating
an item (or determining its absence) takes logarithmic timeIt is faster than the
sequential search.
 It requires lesser number of key comparisons than the sequential search.

13. Is merge sort stable sorting algorithm.

 Yes, merge sort is the stable sorting algorithm.

 A sorting algorithm is said to be stable if it preserves the ordering of similar


elements after applying sorting method.

 And merge sort is a method which preserves this kind of ordering. Hence merge sort
is a stable sorting algorithm.

14. Give efficiency analysis of divide and conquer.

The efficiency of divide and conquer algorithms is given by recurrences of the form.

T(n)= T(n) n=1

aT(n/b)+f(n) n>1

Where a and b are known constants. We assume that T(1) is known and n is a power of b ( n=b k).

15. What is the idea behind binary search?

 A binary search or half-interval searchalgorithm finds the position of a specified


value (the input "key") within a sorted array.

 In each step, the algorithm compares the input key value with the key value of the
middle element of the array.

 If the keys match, then a matching element has been found so its index, or position,

3
CS3401 ALGORITHMS UNIT 3 MEC

is returned.

 Otherwise, if the sought key is less than the middle element's key, then the
algorithm repeats its action on the sub-array to the left of the middle element or, if
the input key is greater, on the sub-array to the right.

 If the remaining array to be searched is reduced to zero, then the key cannot be
found in the array and a special "Not found" indication is returned.

16. Give the time efficiency and drawback of merge sort.

algorithm? Dec 2005

17. What is the difference between quick sort and merge sort? May 2013

BASIS FOR
QUICK SORT MERGE SORT
COMPARISON

Partitioning of the The splitting of a list of elements is Array is always


elements in the array not necessarily divided into half. divided into half
(n/2).

Worst case complexity O(n2) O(n log n)

Works well on Smaller array Operates fine in any


type of array.

Speed Faster than other sorting Consistent speed in all


algorithms for small data set. type of data sets.

Additional storage Less More


space requirement

4
CS3401 ALGORITHMS UNIT 3 MEC

18. What is the difference between sequential and binary search? Apr 2013

Sequential technique binary search technique

This is the simple technique of searching an This is the efficient technique of searching an
element element

This technique does not require the list to be This technique require the list to be sorted.
sorted Then only this method is applicable

The worst case time complexity of this The worst case time complexity of this
technique is O(n) technique is O(log n)

Every element of the list may get compared Only the mid element of the list is compared
with the key element. with key element.

19. What is the necessary precondition for the binary search ?

For the binary search the list should be sorted either in ascending or descending order

20. List out two drawbacks of binary search algorithm. Dec 2007

In binary search the elements have to be arranged either in ascending or descending order
Each time the mid elements has to be computed in order to partition the list in two sub lists

21. Give the control abstraction for divide and conquer. Dec 2012

divide_and_conquer( P )

if ( small ( P ) ) // P is very small so that a solution is trivial

return solution ( n );

divide the problem P into k instances P1, P2, ..., Pk;

return ( combine ( divide_and_conquer ( P1 ),

divide_and_conquer( P2 ),

...

5
CS3401 ALGORITHMS UNIT 3 MEC

divide_and_conquer( Pk ) ) );

The solution to the above problem is described by the recurrence,

assuming size of P denoted by n

where f(n) is the time to divide n elements and to combine their solution.

22. What is called substitution method? Jun 2010

A substitution method is one, in which we guess a bound and then use mathematical
induction to prove our guess correct.

It is basically two step process:

Step1: Guess the form of the Solution.

Step2: Prove your guess is correct by using Mathematical Induction.

Example 1.

Solve the following recurrence by using substitution method.

Solution:

Step1: The given recurrence is quite similar with that of MERGESORT, you guess the
solution is

or

Step2: Now we use mathematical Induction.

Here our guess does not hold for n=1 because

6
CS3401 ALGORITHMS UNIT 3 MEC

Now for n=2

23. What is called optimal solution? Jun 2010

A feasible solution that maximizes the given objective function is called as optimal solution.

24. What do you mean by divide and conquer strategy? Jun 2013

The divide-and-conquer paradigm is often used to find an optimal solution of a problem. Its basic
idea is to decompose a given problem into two or more similar, but simpler, subproblems, to solve
them in turn, and to compose their solutions to solve the given problem.

25. State the principle of substitution method. Jun 2014

 Step 1: Simplify the given equation by expanding the parenthesis if needed.


 Step 2: Solve any one of the equations for any one of the variables. You can use any
variable based on the ease of calculation.
 Step 3: Substitute the obtained value of x or y in the other equation.
 Step 4: Now, simplify the new equation obtained using arithmetic operations and solve
the equation for one variable.
 Step 5: Now, substitute the value of the variable from Step 4 in any of the given
equations to solve for the other variable.
26. Define feasible and optimal solution. Jun 2014

Given n inputs form a subset such that it satisfies some given constraints then such a subset
is called feasible solution.

A feasible solution that maximizes the given objective function is called as optimal solution

27. Trace the operation of binary search algorithm for the input – 15, -6, 0, 7, 9, 23, 54, 82,
101.

Input :

15 -6 0 7 9 23 54 82 101 112 125 131 142 151

7
CS3401 ALGORITHMS UNIT 3 MEC

0 1 2 3 4 5 6 7 8 9 10 11 12 13

Iteration 0:

Left = 0

Right = 13

Mid = (Left + Right) / 2

= (0 + 13) / 2

Mid = 6

Midelement = 54

Search key = 9

Since 9 < 54, search the element 9 in the left of midelement 54.

Iteration 1:

Left = 0

Right = 5

Mid = (Left + Right) / 2

= (0 + 5) / 2

Mid = 2

Midelement = 0

Search key = 9

Since 9 > 0, search the element 9 in the right of midelement 0.

Iteration 2:

Left = 3

Right = 4

Mid = (Left + Right) / 2

= (3 + 4) / 2

Mid = 3

Midelement = 7

Search key = 9

8
CS3401 ALGORITHMS UNIT 3 MEC

Since 9 > 7, search the element 9 in the right of midelement 7.

Therefore 9 is found in the position 4.

[Link] a brute force algorithm for computing the value of a polynomial. (April/May
2015)

Problem: Find the value of polynomial


p(x) = anxn+ an-1xn-1 +… + a1x1 + a0 at a point x = x0
Algorithm:

x := x0

p := 0.0

fori := n down to 0 do

power := 1

for j := power * x

p := p + a:= 1 to i do

power [i] * power

returnp

Efficiency: (n2)

29. Derive complexity of binary search algorithm. (AU april/may 2015)


Worst Case Analysis

The worst case includes all arrays that do not contain a search key.

The recurrence relation for

Cworst(n) = Cworst (n/2) + 1, for n > 1 ----- (1)

Time required to one comparison

compare leftsublist made with middle element or right sub list

Cworst(1) = 1 -------- ( 2 )

The above recurrence relation can be solved further.

assume n=2k the equation ( 1 ) becomes

Cworst(2k) = Cworst(2 k /2)+ 1

9
CS3401 ALGORITHMS UNIT 3 MEC

Cworst(2k) = Cworst(2 k-1)+ 1 ------ ( 3 )

Using backward substation method, we can substitute

Cworst(2k-1) = Cworst(2k-2)+ 1

Then equation (3) becomes

Cworst(2k) = [Cworst( 2 k-2)+ 1] + 1

Cworst(2k) = Cworst( 2 k-2)+ 2

Then Cworst(2k) = [Cworst( 2 k-3)+1]+ 2

Cworst(2k) =Cworst( 2 k-3)+3

….

Cworst(2k) =Cworst( 2 k-k)+k

Cworst(2k) =Cworst( 2 0)+k

Cworst(2k) =Cworst( 1 )+k ----- (4)

But as per equation (2 )

as we have assumed n = 2k taking logarithm (base 2 )on both sides

log 2 n = log 2 2k

log 2 n = k. log 2 2

log 2 n = k(1) therefore log 2 2 =1

therefore k = log 2 n

Cworst(1) = 1 the we get equation ( 4 )

Cworst(2k) = 1 + k

Cworst(n) = 1 + log2n ----- (2)

Cworst(n) = log2n for n>1

The worst case time complexity of binary search is Θ(log2n)

As Cworst(n) = log2n + 1

we can verify equation ( 1) with this value.

Cworst(n) = Cworst[(n/2)] + 1

In equation (1) put n = 2i

10
CS3401 ALGORITHMS UNIT 3 MEC

L.H.S

Cworst(n) = log2n + 1

= log2(2i )+ 1

= log 2 2 + log 2i + 1

= 1+ log 2i + 1

= 2 + log 2i

Cworst(n) =2 + log 2i

R.H.S

Cworst(n/2)+1 = log 2(2i/2 )+ 1

= log 2i + 1

= log 2 2i + 1+ 1

= 2 + log 2i

Cworst(n/2) =2 + log 2i

L.H.S = R.H.S

Hence

Cworst(n) = log 2n + 1 and

Cworst(i) = log 2i + 1 are same

Hence

Cworst(n) = Ө(log n )

30. Give the General plan divide and conquer method. Nov/Dec 2017

A divide and conquer algorithm works by recursively breaking down a problem into twoo
r more subproblems of the same (or related) type (divide), until these become simple enou
gh tobe solved directly (conquer).Divideandconquer algorithms work according to the follo
wing general plan:

 A problem is divided into several subproblems of the same type, ideally of about equal size
 The subproblems are solved (typically recursively, though sometimes a different algorith
m is employed, especially when subproblems become small enough).
 If necessary, the solutions to the subproblems are combined to get a solution to the origi
nal
problem.

11
CS3401 ALGORITHMS UNIT 3 MEC

Example: Merge sort, Quick sort, Binary search, Multiplication of Large Integers

And Strassen’s Matrix Multiplication.

31. Devise an algorithm to make for 1655 using the Greedy strategy. The coins available
are {1000,500,100,50,20,10,5}.

Algorithm:

while(there are more coins and the instance is not solved) {

grab the largest remaining coin; // selection procedure if(adding the coin makes the change
exceed the amount owed )

reject the coin; // feasibility check

else

add the coin to the change;

if (the total value of the change equals the amount owed) //

solution check the instance is solved;

Solution for the given instance 1655 = 1000 + 500 +100 + 50 + 5.

32. Write the advantage of insertion sort.

 The main advantage of the insertion sort is its simplicity.

 It also exhibits a good performance when dealing with a small list.

 The insertion sort is an in-place sorting algorithm so the space requirement is


minimal.

33. Write the disadvantage of insertion sort.

The disadvantage of the insertion sort is that it does not perform as well as other, better
sorting algorithms.

With n-squared steps required for every n element to be sorted, the insertion sort does not
deal well with a huge list.

Therefore, the insertion sort is particularly useful only when sorting a list of few items.

34What are the differences between dynamic programming and divide and

conquer approaches? Nov/Dec 2018

12
CS3401 ALGORITHMS UNIT 3 MEC

Both techniques split their input into parts, find sub solutions to the parts, and synthesize
larger solutions from smaller ones.

Divide and conquer splits input at pre-specified deterministic points (eg., always in the middle)

Dynamic programming splits its every possible split rather than at pre-specified points. After
trying all split points, it determines which split point is optimal.

35. Give an example for Hamiltonian circuit. Nov/Dec 2018

 complete graph with more than two vertices is Hamiltonian


 cycle graph is Hamiltonian
 tournament has an odd number of Hamiltonian paths (Rédei 1934)
 platonic solid, considered as a graph
36. Write brute force algorithm to string matching.
ALGORITHM BruteForceStringMatch (T[0…n-1], P[0…m-1])

//Implements brute-force string matching

//Input: An array T[0…n-1] of n characters representing

// a text and an array P[0…m-1] of m characters

// representing a pattern

//Output: The index of the first character in the text that

// starts a matching substring or -1 if the search is

// unsuccessful

fori ← 0 to n – m do

j←0

while j < m and P[j] = T[i + j] do

j←j+1

if j = m return i

return -1

37. What is time and space complexity of merge sort?


Space complexity of this Merge Sort here is O(n). However, if I choose to perform in-place
merge sort using linked lists (not sure if it can be done with arrays reasonably) will the space
complexity become O(log(n))

Time complexity of merge sort


Best case Average case Worst case

13
CS3401 ALGORITHMS UNIT 3 MEC

Θ (n log2 n) Θ (n log2 n) Θ (n log2 n)

38. Write the difference between Greedy method and Dynamic programming. May 2011

Greedy Method Dynamic Programming

Makes the locally optimal choice at each Solves subproblems recursively and combines their solutions to solve the main
step, hoping for a globally optimal problem optimally.
solution.
Relies on the fact that a local choice leads Uses optimal solutions to subproblems to build the overall optimal solution.
to a global optimum.
May not always lead to an optimal Always finds the optimal solution if the problem exhibits optimal substructure
solution. and overlapping subproblems.
39. Write algorithm to find shortest path between all pairs of nodes. May 2011

40. Write any two characteristics of Greedy Algorithm.

The greedy method is a simple and straightforward way to solve optimization


problems. It involves making the locally optimal choice at each stage with the
hope of finding the global optimum.
 The main advantage of the greedy method is that it is easy to implement and
understand. However, it is not always guaranteed to find the best solution and
can be quite slow.
 The greedy method works by making the locally optimal choice at each stage in
the hope of finding the global optimum. This can be done by either minimizing
or maximizing the objective function at each step.
 The main advantage of the greedy method is that it is relatively easy to
implement and understand.
 However, there are some disadvantages to using this method. First, the greedy
method is not guaranteed to find the best solution. Second, it can be quite slow.
Finally, it is often difficult to prove that the greedy method will indeed find the
global optimum.
41. What is an optimal solution? May 2010
Any point in the feasible region of a linear programming problem that gives the optimal value
(maximum or minimum) of the objective function is called an optimal (feasible) solution
42. What is Knapsack problem? Dec 2011
 A bag or sack is given capacity C and n objects are given.
 Each object has weight w i and profit p i .

14
CS3401 ALGORITHMS UNIT 3 MEC

 Fraction of object is considered as xi(i.e)0 ≤ xi≤1.


 Iffractionis1thenentireobjectisput into sack.
 When we place this fraction into the sack we get wi xi and pi xi.
43. What are greedy algorithms? Dec 2011
 Greedy is an algorithmic paradigm that builds up a solution piece by piece, always
choosing the next piece that offers the most obvious and immediate benefit. So the
problems where choosing locally optimal also leads to global solution are the best fit
for Greedy.

44. State the general principle of greedy algorithm.


Greedy method. In this approach, the decision is taken on the basis of current
available information without worrying about the effect of the current decision in
future.
 Greedy algorithms build a solution part by part, choosing the next part in such a way,
that it gives an immediate benefit. This approach never reconsiders the choices taken
previously. This approach is mainly used to solve optimization problems. Greedy
method is easy to implement and quite efficient in most of the cases. Hence, we can say
that Greedy algorithm is an algorithmic paradigm based on heuristic that follows local
optimal choice at each step with the hope of finding global optimal solution.
 In many problems, it does not produce an optimal solution though it gives an
approximate (near optimal) solution in a reasonable time.
45. What is the limitation of Greedy algorithm? May 2010
 An optimization problem:
o Given a problem instance, a set of constraints and an objective function.
o Find a feasible solution for the given instance for which the objective function has
an optimal value
o either maximum or minimum depending on the problem being solved.
 A feasible solution satisfies the problem’s constraints
 The constraints specify the limitations on the required solutions.

46. State ordefine the principle of optimality. Apr/May 2019 Dec 2010,Nov/Dec 2017
The principle of optimality states that an optimal sequence of decisions has the
property that whatever the initial state and decision are, the remaining decisions
must constitute an optimal decision sequence with regard to the state resulting
from the first decision.

47. What is dynamic programming? or what do you mean by dynamic programming?


Apr/May-2017

 Dynamic programming is typically applied to optimization problems. For a given


problem we may get any number of solutions. from all those solutions we seek for
optimum solution ( minimum value or maximum value solution).
 And such an optimal solution becomes the solution to the given problem.

15
CS3401 ALGORITHMS UNIT 3 MEC

48. What are the Applications of Dynamic Programming?

 Multistage Graph
 Optimal Binary Search Tree (OBST)
 0/1 Knapsack Problem
 Travelling Salesman Problem.
 All Pair Shortest Path Problem

49. What are optimal binary search trees OBST? May 2010
 Let { a1, a2,….an} be a set of identifiers such that a1<a2<a3…let p(i) be the
probability with which we can search for ai is Successful search.
 Let , qi be the probability of searching an element x such that
ai<x<ai+1 where 0≤i≤ n is unsuccessful search . thus p(i) is
probability of successful search and q(i) is the probability of
unsuccessful search.
 Then a tree which is build with optimum cost from
𝑛
𝑛
∑ p(i) ∑𝑖=1 q(i)is called optimal binary search tree
𝑖=1

50. What is a Feasible solution ?Dec 2013 / May 2014


For solving the particular there exits n inputs and we need to obtain a subset that
satisfies some constraints .then any subset that satisfies these constrains is called
feasible solution.

51. State the applications of Huffman ‘s tree.

Application of Huffman trees:

• Huffman encoding is used in file compression algorithm


• Huffman’s code is used in transmission of data in an encoded form
• This encoding is used in game playing method in which decision trees need to be
formed

52 . Differentiate between subset paradigm and ordering paradigm.Dec12

subset paradigm ordering paradigm

In this paradigm , the decision is made by


At each step the decision about the input
considering the inputs in some order . this
is made. That means at each steps it is
paradigm is useful for solving those
decided whether the particular input is
problems that do not call for selection of
in an optimal solution or not
optimal subset in greedy manner

53. What is the drawback of greedy algorithm ?May 2012

16
CS3401 ALGORITHMS UNIT 3 MEC

 Greedy method is comparatively efficient than divide and conquer but there is no as
such guarantee of getting optimum solution
 In Greedy method , the optimum selection is without revising previously generated
solutions
54. Write control abstraction for the ordering paradigm. May 2012

Algorithm store (n, limit)


{
j = 0;
For(i← 1 to n ) do
{
Write (“append program”, i);
Write (“permutation for tap “,j);
j = ( j+1) mod limit ;
}
}
55. What is minimum Spanning tree? Dec 2010 APR 2018

 A Minimum Spanning tree of a weighted graph connected graph G is its spanning


tree of the smallest weight, where the weight of a tree is defined as the sum of
the weights on all its edges.
 The total number of edges in minimum spanning tree (MST) is |V|-1 where V is
the number of vertices.
56. Give any two properties of dynamic programming approaches.

Optimal substructure:

The dynamic programming technique makes use principle of optimality to find the
optimal solution from sub problems.
Overlapping Sub-problems:

 The dynamic programming is a technique in which the problem


 is divided into sub problems.
 The solutions of sub problems are shared to get the final solution
 to the problem.
 It avoids repetition of work

57. Give the commonly used designing steps for dynamic programming algorithm.

Dynamic programming design involves 4 major steps

 Characterize the structure of optimal solution.


 Recursively defines the value of an optimal solution.
 By using bottom up technique compute value of optimal solution.
 Compute an optimal solution from computed information.
58. What does dynamic programming have in common with divide and conquer?

17
CS3401 ALGORITHMS UNIT 3 MEC

o Both the divide and conquer and dynamic programming solve the problem by
breaking it into number of sub-problems.
o In both these methods solutions from sub-problems are collected together to
form a solution to given problem.
59. Define Catalan number.

The total number of binary search trees with n keys is equal to

nthcatalan number.

C(n) = (2n to n)1/(n+1) for n>0

C(0) = 1

60. State time and space efficiency of OBST.

Space Efficiency : Quadratic

Time Efficiency : Cubic

61. List out the advantages of dynamic programming. Jun 2014

Dynamic programming enables you to develop sub solutions of a large program.


The sub solutions are easier to maintain use and debug. And they possess overlapping also
that means we can reuse these sub solutions are optimal solutions for the problem
62. Compare divide and conquer with dynamic programming and Dynamic programming
with greedy technique. Dec 2010

Divide and Conquer Dynamic Programming

The divide-and-conquer paradigm


involves three steps at each level of the The development of a dynamic-
recursion: programming algorithm can be broken
into a sequence of four steps.
• Divide the problem into a number of
sub problems. a. Characterize the structure of an
optimal solution.
• Conquer the sub problems by solving
them recursively. If the sub problem b. Recursively define the value of an
sizes are small enough, however, just optimal solution.
solve the sub problems in a c. Compute the value of an optimal
straightforward manner. solution in a bottom-up fashion.
• Combine the solutions to the sub d. Construct an optimal solution from
problems into the solution for the computed information
original problem.

They call themselves recursively one or Dynamic Programming is not recursive.


more times to deal with closely related

18
CS3401 ALGORITHMS UNIT 3 MEC

sub problems.

D&C does more work on the sub-


DP solves the sub problems only once
problems and hence has more time
and then stores it in the table.
consumption.

In D&C the sub problems are In DP the sub-problems are not


independent of each other. independent.

Example: Merge Sort, Binary Search Example : Matrix chain multiplication

Dynamic Programming Greedy Technique

Focuses on principle of optimality. Greedy method focuses on expanding


partially constructed solutions.

It provides specific answers. It provides many results such as feasible


solution.

Less efficient More efficient

63. Write down the optimization techniques used for warshall’s algorithm. state the rules
and assumption which are implied behind that.(AU April/may 2015)
Dynamic programming algorithms are used for optimization (for example, finding the
shortest path between two points, or the fastest way to multiply many matrices).. The
alternatives are many, such as using a greedy algorithm, which picks the locally optimal
choice at each branch in the road.

The locally optimal choice may be a poor choice for the overall solution. While a greedy
algorithm does not guarantee an optimal solution, it is often faster to calculate. Fortunately,
some greedy algorithms (such as minimum spanning trees) are proven to lead to the
optimal solution.

64. Define the single source shortest path problem.

Dijkstra’s algorithm solves the single source shortest path problem of finding shortest paths
from a given vertex( the source), to all the other vertices of a weighted graph or digraph.
Dijkstra’s algorithm provides a correct solution for a graph with non negative weights.
65. State Assignment problem.
There are n people who need to be assigned to execute n jobs, one person per job. (That is,each
person is assigned to exactly one job and each job is assigned to exactly one person.) The cost
that would accrue if the ith person is assigned to the jth job is a known quantity

19
CS3401 ALGORITHMS UNIT 3 MEC

[𝑖, 𝑖] for each pair

𝑖, 𝑖 = 1, 2, . . .

[Link] to calculate the efficiency of dijkstra’s Algorithm?

1. The time efficiency of Dijkstra’s algorithm depends on the structure used for
implementing the priority queue and for representing as input graph.
2. The efficiency is ϴ(|V|2) for graphs represented by their weight matrix and the
priority queue implemented as an unordered array.
3. The efficiency is ϴ(|E|log|V|) for graphs represented by the adjacency linked list
and the priority queue implemented as a min heap.
4. Better efficiency can be achieved if priority queue is implemented using a
sophisticated data structure called the Fibonacci Heap.

67. State how binomial co-efficient is computes.


Computing a Binomial Coefficient is a typical example of applying dynamic programming in
mathematics, particularly in combinatory.
Binomial Coefficient is a Coefficient of any of the term in the expansion of (a+b) n.
The binomial coefficient is denoted by C(n, k) or (𝑛𝑘)
The binomial coefficient is the number of combinations or subsets of K elements from an n
element set(0≤ k ≥ n).
The name binomial coefficient comes from the participation of these numbers in the
binomial formula.
The binomial formula is
(a+ b) n =C(n.0)a n +---+C(n, i) a n-1 b n +----+ C(n. n)b n.
The binomial coefficient has several properties are.
The three important properties are
C(n, k)=C(n-1,k-1)+C(n-1,k), for N>k>0

C(n,0)= 1

C(n, n)=1
68. What is best algorithm suited to identity the topology for a graph? Mention its
efficiency factors.

An alternative algorithm for topological sorting is based on depth-first search. The algorithm
loops through each node of the graph, in an arbitrary order, initiating a depth-first search that
terminates when it hits any node that has already been visited since the beginning of the
topological sort or the node has no outgoing edges (i.e. a leaf node):

The usual algorithms for topological sorting have running time linear in the number of nodes
plus the number of edges, asymptotically, O(|V|+|E|)

69. Define multistage graphs. Give an example. NOV-2018,Apr/may 2023

The multistage graph problem is to find a minimum cost path from S to t.

20
CS3401 ALGORITHMS UNIT 3 MEC

Problem description:

A multistage graph G=(V,E) is a directed graph in which the vertices are portioned into K> 2
disjoint sets Vi, 1<i<=K.

if (u,v) is an edge in E, then u E Vi and VEVi+1 for some i., 1< = i< =K.

The sets V1 and Vk are such that (V1)= VK/=1, Let S and t respectively the vertex in b1 and bk.

The vertex S is the source, and t is the sin R. Let C (i, j) be the cost of edge (i,j)

The cost of a path from S to t is the sum of the cost of edges on the path.

Each set vi defines a stage in the graph Because of the constraints on E.

Every path from S to t starts in stage 1, goes to stage 2, then to stage 3, then to stage 4, etc., and
finally terminates in stage K.

70. How dynamic programming is used to solve Knapsack problem? NOV-2018

Dynamic programming is a method for solving Optimization problems.

The idea:

Compute the solutions to the sub-problems once and store the solutions in a table, so that
they can be reused (repeatedly)later

 Structure
 Principle of Optimality
 Bottom-up computation
 Construction of optimal solution
71. Define transitive closure of a directed graph. APR-2018

Transitive closure of a graph. Given a directed graph, find out if a vertex j is reachable
from another vertex i for all vertex pairs (i, j) in the given graph. Here reachable mean that
there is a path from vertex i to j. The reach-ability matrix is called transitive closure of a
graph.

72. What is the constraint of for binary search tree insertion? April/May 2019
A binary search tree is a tree with one additional constraint — it keeps the elements in
the tree in a particular order. Formally each node in the BST has two children (if any are
missing we consider it a nil node), a left child and a right child.

73. Define Brute Force. Or what is brute force method? Nov/Dec 2019

Brute Force is a straightforward approach to solve a problem, which is directly based on


the problem statement and definition of the concepts. Brute Force strategy is one of the
easiest approach.

74. Define a binary search tree. Nov/Dec 2019

21
CS3401 ALGORITHMS UNIT 3 MEC

A binary search tree (BST), also known as an ordered binary tree, is a node-based data
structure in which each node has no more than two child nodes. Each child must either be a
leaf node or the root of another binary search tree. The left sub-tree contains only nodes
with keys less than the parent node; the right sub-tree contains only nodes with keys
greater than the parent node.

The BST data structure is the basis for a number of highly efficient sorting and searching
algorithms, and it can be used to construct more abstract data structures including sets,
multisets, and associative arrays.

75. List the elements of Greedy strategy. Apr/May 2024, Nov/Dec2024

The greedy strategy is a problem-solving approach that follows a simple, intuitive process:
make the locally optimal choice at each step, hoping that these local solutions will lead to a
globally optimal solution. Here are the main elements that define a greedy strategy:

1. Greedy Choice Property:


At each step, the algorithm makes the best possible choice, locally, without worrying
about the global consequences. The decision made at each step is the one that seems the
best at that moment.
2. Feasibility Check:
Each choice made must be feasible, meaning it must satisfy the problem’s constraints or
conditions.
3. Irrevocability:
Once a choice is made, it cannot be undone. This characteristic distinguishes greedy
algorithms from others like dynamic programming, where decisions might be revisited.
4. Optimal Substructure:
The problem must exhibit an optimal substructure, meaning the optimal solution to the
overall problem can be constructed from the optimal solutions to its subproblems. This
property allows the greedy approach to work.

76. what kind of problem can be solved using divide and conquer method Apr/May 2024
1. Sorting Problems
 Merge Sort: Divides the array into halves, sorts them recursively, and merges.
 Quick Sort: Partitions the array, recursively sorts smaller partitions.

2. Searching Problems
 Binary Search: Repeatedly divides the search space into halves until the target is found.

22
CS3401 ALGORITHMS UNIT 3 MEC

PART-B

1. Explain Divide and Conquer technique. Dec 2009

 Divide & conquer technique is a top-down approach to solve a problem.


 The algorithm which follows divide and conquer technique involves 3 steps:
Divide the original problem into a set of sub problems.

Conquer (or Solve) every sub-problem individually, recursive.

Combine the solutions of these sub problems to get the solution of original problem.

Divide and Conquer is one of the best algorithm design technique

Algorithm DC(p)

If P is too small then

Return solution of P.

Else{

Divide (p) and obtain p1, p2, …..pn where n ≥ 1

Apply DC to each sub problem

Return combine (DC(p 1),

DC( p2)….Dc(pn));

}}

The diagrammatic representation of the divide and conquer technique is shown in


figure(3.1) which divides the problem into two smaller sub problems.

23
CS3401 ALGORITHMS UNIT 3 MEC

Fig.3.1 divide and conquer

Example: To compute sum of n numbers then by divide and conquer we can solve the problem as
(a0 + ….an-1)

(a0 + ….a[n/2]-1) (a[n/2] + ….an-1)

Solution 1 Solution 2

(a0 + ….an-1)

If we want to divide a problem of size n in to a size of n /b taking f(n) time to divide and
combine , then we can set up recurrence relation for obtaining time for size n is

T (n) = a T (n/b) + f (n),

T(n/b) = Time for size n/b time required for dividing the problem in to sub
problem.

T(n) = Time for size n

n = number of sub instances

The above equation is called general divide and conquer recurrence. The order of growth of
T(n) depends upon the constants a, b and order of growth function f(n).

Divide and Conquer technique

Examples for divide and conquer method are,

 Binary search
 Quick sort
 Merge sort
Example 1 :

Consider the problem of computing the sum of number a 0 …… [Link] n > 1, the
problem is divided into two instances of the same problem.

They are

To compute the sum of the first [n/2] numbers.

To compute the sum of the remaining [n/2] numbers.

Once the two instances are computed, add their values to get the sum of original problem.

a0 + a1 +……+ an-1 = (a0 + a1 +……+ a[n/2]-1) + (a[n/2] +……+ an-1)

An instance of size n can be divided into several instances of size n/b,

24
CS3401 ALGORITHMS UNIT 3 MEC

Where a and b are constants a ≥ 1 and b > 1

The recurrence for the running time T(n) is

T(n) = aT(n/b) + f(n) ,

which is called as general divide and conquer recurrence

 where, f(n) is a function that accounts for the time spent on dividing the problem into
smaller ones and on combining their solutions.
 The order of growth of T(n) depends on the values of the constants ‘a’ and ‘b’ and the order
of growth of the function f(n).
For example, the recurrence equation for the number of additions is

a(n) = 2a(n/2) + 1

Applications

1. As the name suggests, ‘Divide and Conquer is a strategy in which a given problem is split
into a set of sub problems.
2. Each sub-problem is then handled/solved individually.
3. Once all the sub-problems are solved, we combine the sub-solutions of these sub
problems and find the final solution.

Advantages of divide and conquer


 The time spent on executing the problem using divide and conquer is smaller than
other methods.
 The divide and conquer approach provides an efficient algorithm in computer
science.
 The divide and conquer technique is ideally suited for parallel computation in which
each sum problem can be solved simultaneously by its own processor.

2. Explain the Finding maximum and minimum algorithm with the help of illustrative
Example.

 Divide and Conquer (DAC) approach has three steps at each level of recursion:

 Divide and Conquer (DAC) approach has three steps at each level of recursion:

 Divide the problem into number of smaller units called sub-problems.

 Conquer (Solve) the sub-problems recursively.

 Combine the solutions of all the sub-problems into a solution for the original
problem.

Maximum and Minimum:


 Let us consider simple problem that can be solved by the divide-and conquer
technique.

25
CS3401 ALGORITHMS UNIT 3 MEC

 The problem is to find the maximum and minimum value in a set of ‘n’ elements.

 By comparing numbers of elements, the time complexity of this algorithm can be


analyzed.

 Hence, the time is determined mainly by the total cost of the element comparison.

Explanation:

a. Straight MaxMin requires 2(n-1) element comparisons in the best, average & worst cases.

b. By realizing the comparison of a [i]max is false, improvement in a algorithm can be done.

c. Hence we can replace the contents of the for loop by, If (a [i]> Max) then Max = a [i]; Else
if (a [i]< 2(n-1)

d. On the average a[i] is > max half the time, and so, the avg. no. of comparison is 3n/2-1.
A Divide and Conquer Algorithm for this problem would proceed as follows:
a. Let P = (n, a [i],……,a [j]) denote an arbitrary instance of the problem.
b. Here ‘n’ is the no. of elements in the list (a [i],….,a[j]) and we are interested in finding the
maximum and minimum of the list.
c. If the list has more than 2 elements, P has to be divided into smaller instances.
d. For example, we might divide ‘P’ into the 2 instances, P1=([n/2],a[1],……..a[n/2]) & P2= (
n-[n/2], a[[n/2]+1],….., a[n]) After having divided ‘P’ into 2 smaller sub problems, we can
solve them by recursively invoking the same divide-and-conquer algorithm.
Algorithm:

26
CS3401 ALGORITHMS UNIT 3 MEC

Example:

A 1 2 3 4 5 6 7 8 9

Values 22 13 -5 -8 15 60 17 31 47

Tree Diagram:

i. In this Algorithm, each node has 4 items of information: i, j, max & min.
ii. In root node contains 1 & 9 as the values of i& j corresponding to the initial call to
MaxMin.
iii. This execution produces 2 new calls to MaxMin, where i& j have the values 1, 5 & 6, 9
respectively & thus split the set into 2 subsets of approximately the same size.
iv. Maximum depth of recursion is 4.
Complexity:
If T(n) represents this no., then the resulting recurrence relations is
T (n)=T([n/2]+T[n/2]+2 n>2
1 n=2
1 n=1
When ‘n’ is a power of 2, n=2k for some positive integer ‘k’, then
T (n) = 2T(n/2) +2
= 2(2T(n/4)+2)+2

27
CS3401 ALGORITHMS UNIT 3 MEC

= 4T(n/4)+4+2
*
*
= 2k-1 T (2) + Σ 1 ≤ I ≤ k-1 ≤ 2i
= 2k-1+ 2k - 2
T(n) = (3n/2) – 2
Note that (3n/2) - 2 is the best-average and worst-case no. of comparisons when ‘n’ is a
power of 2.
3. Explain the Merge Sort algorithm with the help of illustrative Example.
Dec2013/14/15/16OR Explain the working of Merge Sort Algorithm with an example.
Nov/Dec 2017. (APR/MAY 2023)

Explain Merge sort algorithm with an example. April/May 2018

 The merge sort is a sorting algorithm that uses the divide and conquer strategy.
Division is dynamically carried out.

 Merging is the process of combining two or more files into a new sorted file.
Merge sort on an input array with n elements consists of three steps:

Divide: partition array into two sub lists s1 and s2 with n/2 elements each Conquer: then
sort sub list s1 and sub list s2.

Combine: merge s1 and s2 into a unique sorted group.

 Merge sort is a perfect example of a successful application of the divide and conquer
technique.

 It sorts a given array A[0…..n − 1] by dividing it into two halves A[0…..[n/2]−1] and
A[[n/2]…..n − 1].

 It sorts each half separately by using recursive procedure, and Then, merging the

 two smallersorted arrays into a single sorted one.

Steps to be followed

 The first step of the merge sort is to chop the list into two.
 If the list has even length, split the list into two equal sub lists.
 If the list has odd length, divide the list in two by making the first sub list one entry
greater than the second sub list.
 Then split both the sub lists into two and go on until each of the sub lists are of size
one.
Finally, start merging the individual sub lists to obtain a sorted list.

Example:

The operation of the algorithm for the array of element (8,3,2,9,7,1,5,4) is explained in the figure

28
CS3401 ALGORITHMS UNIT 3 MEC

given below.

ALGORITHM

Algorithm Mergesort(A[0..n − 1])

//Sorts array A[0..n − 1] by recursive mergesort

//Input: An array A[0..n − 1] of orderable elements

//Output: Array A[0..n − 1] sorted in nondecreasing order

If n > 1

copy A[0..n/2] − 1] to B[0..n/2] − 1]

copy A[[n/2]..n − 1] to C[0..[n/2]] − 1]

Merge sort(B[0..[n/2] − 1])

Mergesort(C[0..[n/2] − 1])

Merge (B, C, A) //see below

 The merging of two sorted arrays can be performed as follows.


 Two pointers are initialized to point to the first elements of the arrays being
merged.
 Then the elements are compared and the smaller of both is added to a new array or
list being constructed.
 Then the index of that smaller element is incremented to point to its immediate
successor in the array.
The above steps are continued until one of the two given array is exhausted.

Then the remaining elements of the other array are copied to the end of the next array.

29
CS3401 ALGORITHMS UNIT 3 MEC

Algorithm Descriptive and Implementation

ALGORITHMMerge(B[0..p − 1], C[0..q − 1], A[0..p + q − 1])

//Merges two sorted arrays into one sorted array

//Input: Arrays B[0..p − 1] and C[0..q − 1] both sorted

//Output: Sorted array A[0..p + q − 1] of the elements of B //and C

i ← 0; j ← 0; k ← 0

whilei<p and j<q do

if B[i] ≤ C[j ]

A[k]← B[i];

i←i+1

else

A[k]← C[j ];

j←j+1

k←k+1

ifi = p

copy C[j..q − 1] to A[k..p + q − 1]

else

copy B[i..p − 1] to A[k..p + q − 1]

Efficiency of Merge Sort

In merge sort algorithm the two recursive calls are made. Each recursive call focuses on
n/2 elements of the list .

After two recursive calls one call is made to combine two sub list i.e to merge all n
elements.

Hence we can write recurrence relation as

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

T(n/2) = Time taken by left sub list

T(n/2) = time taken by right sublist

T(n) = time taken for combining two sub lists

30
CS3401 ALGORITHMS UNIT 3 MEC

where n> 1 T (1) = 0

The time complexity of merge sort can be calculated using two methods

Master theorem

Substitution method

Master theorem

Let , the recurrence relation for merge sort is

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

let

T(n) = aT(n/b) + f(n) be a recurrence relation

i.e. T(n) = 2T(n/2) + cn ------- ( 1 )

T(1) = 0 ----------- (2 )

As per master theorem

T(n) = Θ (n d long n ) if a = b

As equation ( 1),

a =2 , b = 2 and f(n) = cn and a = bd

i.e 2 = 2`

This case gives us , T (n) =Θ (n log2 n)

Hence the average and worst case time complexity of merge sort is

C worst (n) = (n log2 n)

Substitution method

Let, the recurrence relation for merge sort be

T(n) = T(n/2) + T(n/2) + cn for n>1

i.e. T(n) = 2T(n/2) + cn for n>1 ------- (3)

T(1) = 0 -------(4)

Let us apply substitution on equation ( 3) .

Assume n=2k

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

31
CS3401 ALGORITHMS UNIT 3 MEC

T(n) = 2T(2k/2 ) + c.2k

T(2k) = 2T(2k-1) + c.2k

If k = k-1 then,

T(2k) = 2T(2k-1) + c.2k

T(2k) = 2[2T(2k-2) + c.2k -1] + c.2k

T(2k) = 22 T(2k-2) + 2.c.2k -1 + c .2k

T(2k) = 22 T(2k-2) + 2.c.2k /2 + c.2k

T(2k) = 22 T(2k-2) + c.2k + c.2k

T(2k) = 22 T(2k-2) + 2c .2k

Similarly we can write,

T(2k) = 23 T(2k-3) + 3c .2k

T(2k) = 24 T(2k-4) + 4c .2k

…..….

T(2k) = 2k T(2k-k) + k.c.2k

T(2k) = 2k T(20) + k.c.2k

T(2k) = 2k T(1) + k.c.2k -------- (5)

But as per equation (4), T(1) =0

There equation (5) becomes ,

T(2k) = 2k .0 +. k. c . 2k

T(2k) = k. c . 2k

But we assumed n=2k , taking logarithm on both sides.

i.e. log 2 n = k

Therefore T(n) = log 2 n. cn

Therefore T (n) =Θ (n log2 n)

Hence the average and worst case time complexity of merge sort is

C worst (n) = (n log2 n)

Time complexity of merge sort

32
CS3401 ALGORITHMS UNIT 3 MEC

Best case Average case Worst case

Θ (n log2 n) Θ (n log2 n) Θ (n log2 n)

Application of Merge Sort

 Sorting
 Tape Sorting
 Data Processing
Demerit

The algorithm requires linear amount of extra storage.

4. Explain the Quick Sort algorithm with the help of illustrative example Or Explain the
time complexity of quick sort method in detail OR Write the algorithm for Quick Sort and
write its time complexity with example list are 5, 3, 1, 9, 8, 2, 4, 7. Apr/May 2017

Write the algorithm for quick sort. Provide a complete analysis of quick sort
for the given set of numbers 12, 33, 23, 43, 44, 55, 64, 77 and 76. (13) Nov/Dec
2018Or Write the quick sort algorithm and explain it example. Derive the worst
case and average case time complexity April/May 2019

 Quick sort is a sorting algorithm that uses the divide and conquers strategy.
The three steps of quick sort are as follows:

Divide:

 Split the array into two sub arrays that each element in the left sub array
is less thanor equal the middle element and each element in the right sub array is
greater than the middle element .
 The splitting of the array into two sub array is based on pivot element. All the
elements that are less than pivot should be in left sub array and all the elements that
are more than pivot should be in right sub array
Conquer: Recursively sort the two sub arrays.

Combine: Combine all the sorted elements in a group to form a list of sorted elements
Quick sort is also referred as Partition Exchange sort.

 The problem of sorting a set is reduced to the problem of sorting two smaller
subsets.
 Quick sort divides input elements according to their position in the [Link] also
divides the input elements according to the value of element.

 To achieve the partition, quick sort rearrange the given array element a[0,..n-1]

 It is a situation where all the elements before the position ‘S’ are smaller than or
equal to a[s] and all the elements after position ‘s’ are greater than or equal to a[s].

The partition is shown as

33
CS3401 ALGORITHMS UNIT 3 MEC

After partitioning, a[s] will be in its final position in the sorted array.

Then sorting of element of two sum arrays preceding and following a[s] can be done
independently.

After both scans stop, three situations may arise, depending on whether or not the
scanning indices have crossed.

1. If scanning indices i and j have not crossed, i.e., i< j, we simply xchange A[i] and A[j ] and
resume the scans by incrementing i and decrementing j, respectively:

2. If the scanning indices have crossed over, i.e., i> j, we will have partitioned the Sub array after
exchanging the pivot with A[j].

3. Finally, if the scanning indices stop while pointing to the same element, i.e.,i = j, the value they
are pointing to must be equal to p.

Thus, we have the sub array partitioned, with the split position s= i=j:

Combine the last case with the case of crossed-over indices (i>j ) by exchanging the pivot with A[j]
whenever i ≥ j .

Example

The array elements are

5 3 1 9 8 2 4 7

34
CS3401 ALGORITHMS UNIT 3 MEC

The first element of array is chosen as pivot element.

Two indices i and j are used for scanning.

P i j

5 3 1 9 8 2 4 7

P i j

5 3 1 9 8 2 4 7

P i j

5 3 1 9 8 2 4 7

Now exchange the elements 9 and 4 now array becomes,

P i j

5 3 1 4 8 2 9 7

Now also exchange a[i] and a[j], the resultant array becomes,

P i j

5 3 1 4 2 8 9 7

Now the scanning indices i and j have not crossed (ie) i< j, simply exchange i and j.

The array becomes

P j i

5 3 1 4 2 8 9 7

Since a[j] < pivot, (2<5) exchange them.

The result is

2 3 1 4 5 8 9 7

Now, the array has been sub divided into sub array with pivot element as middle.

Sub array 1

2 3 1 4

P i j

2 3 1 4

35
CS3401 ALGORITHMS UNIT 3 MEC

P i j

2 3 1 4

Exchange a[i] and [j]

P i j

2 1 3 4

Since i< j, exchange a and j

P j i

2 1 3 4

Since a[j] < pivot, (1 < 2) exchange i and j

P i j

1 2 3 4

Sub array 3

3 4

P ij

3 4

i=j, ie both points to the same element.

j j

3 4

Sub array 2

8 9 7

P i j

8 9 7

36
CS3401 ALGORITHMS UNIT 3 MEC

Exchange a[i] and a[j]

The sub array 2 becomes

P i j

8 7 9

Here i< j simply exchange i and j

It becomes

P j i

8 7 9

Since a[j] < pivot, exchange them

7 8 9

Hence, the array elements are sorted. The sorted array is

1 2 3 4 5 7 8 9

RecursiveCalls Tree

Tree of recursive calls to Quicksort with input values l and r of subarray bounds and split position
s of a partition obtained.

ALGORITHM FOR QUICK SORT

ALGORITHM Quicksort(A[l..r])

//Sorts a subarray by quicksort

//Input: Subarray of array A[0..n − 1], defined by its left and

//right indices l and r

37
CS3401 ALGORITHMS UNIT 3 MEC

//Output: Subarray A[l..r] sorted in nondecreasing order

if l<r

s ←Partition(A[l..r]) //s is a split position

Quicksort(A[l..s − 1])

Quicksort(A[s + 1..r])

ALGORITHM Hoare Partition(A[l..r])

//Partitions a subarray by Hoare’s algorithm, using the first //element as a pivot

//Input: Subarray of array A[0..n − 1], defined by its left and //right indices l and r (l < r)

//Output: Partition of A[l..r], with the split position returned //as this function’s value

p ← A[l]

i ← l; j ← r + 1

repeat

repeati ← i + 1 until A[i] ≥ p

repeat j ← j − 1 until A[j ] ≤ p

swap(A[i], A[j ])

untili ≥ j

swap(A[i], A[j ]) //undo last swap when i ≥ j

swap(A[l], A[j ])

return j

Efficiency of Quick Sort

The number of key comparisons made before a partition is achieved is n + 1 if the scanning
indices i and j cross over.

The number of key comparisons is n, if the scanning indices i and j coincides.

Best Case Analysis( split in the middle)

If the array is always partitioned at the mid , then it brings the best case efficiency of an
algorithm

The number of key comparisons in the best case satisfies the recurrence

38
CS3401 ALGORITHMS UNIT 3 MEC

Using Master Theorem

Solve equation (1) using Master Theorem

If f(n) ∈Θ (n d ) then

T(n) = Θ (n d) if a <bd
T(n) = Θ (n d log n ) if a = bd
T(n) = Θ (n log ba ) if a> b bd
C(n) = 2 C(n/2) +n

Here f(n) ∈ n1 therefore d = 1

Now , a = 2 and b = 2

As from case 2 we get a = bd i.e. 2 = 21

We get ,

T(n) i.e C(n) = Θ (nd log n )

Cbest(n) = Θ (n log n)

Best case time complexity of quick sort is Θ (n log n)

Using substitution method

C(n) = C (n/2) + C (n/2) + n ----------( 1 )

C(n) = 2C (n/2) +n

Assume n = 2K since each time the list is divide into two equal halves .then equation becomes,

C(2K) = 2C(2k /2) + 2k

C(2K) = 2C(2k-1) + 2k

Now substitute C(2k-1 ) = 2C(2k-2) + 2k-1

C(2K) = 2[2C(2k-2) + 2k-1] + 2k

C(2K) = 22C(2k-2) + 2.2k-1 + 2k

39
CS3401 ALGORITHMS UNIT 3 MEC

C(2K) = 22C( 2k-2) + 2k + 2k

C(2K) = 22C( 2k-2) +2.2k

If we substitute C(2k-2) then ,

C(2K) = 22C( 2k-2) +2. 2k

C(2K) = 22[2 C(2k-3) + 2k– 2] + 2.2k

C(2K) = 23C(2k-3) +22. 2k– 2 + 2.2k

C(2K) = 23C(2k-3) + 2k + 2.2k

C(2K) = 23C(2k-3) + 3.2k

Similarly we can write

C(2K) = 24C(2k-4) + 4.2 k

----

C(2K) = 2kC(2k-k) + k.2 k

C(2K) = 2kC( 20) + k.2 k

C(2K) = 2kC( 1) + k.2 k

C(1) = 0

hence the above equation becomes

C (2K) = 2k.0 + k.2 k

now as we assumed n = 2k we can also say

n = log 2 n [by taking logarithm on both side]

C( n) = n.0 + log 2 n . n

Thus it is proved that best case time complexity of quick sort

isΘ (n log n)

Worst Case Analysis (sorted array)

The worst case for quick sort occurs when the pivot is a minimum or maximum of all the
elements in the list .

For example,

if A[0..n − 1] is a strictly increasing array and we use A[0] as the pivot,

40
CS3401 ALGORITHMS UNIT 3 MEC

The left-to-right scan will stop on A[1]

The right-to-left scan continues uptoA[0]

The total number of key comparisons made will be equal to

Cworst(n) = (n -1) + n

Cworst(n) = (n - 1) +( n-2) + ... + 2 + 1

But as we know

1 + 2+ 3 +---- + n = n (n + 1)/2 = ½ n2

Cworst(n) ∈ θ(n2)

The time complexity of worst case of quick sort is θ (n2)

Average Case Analysis (random array)

Let Cavg(n) be the average number of key comparison made by Quick Sort.

The partition split can be happen in each position S (0≤S≤n-1) with the probability 1/n.

The recurrence relation is

Cavg (n) ≈ 2n ln n ≈ 1.39 n log2 n.

Thus, on the average case, Quick Sort makes 38% more comparison the best case.
Hence average case time complexity of quick sort is Θ ( n log n)

Time complexity of quick sort


Best case Average case Worst case

Θ(n log n) Θ(n log n) θ (n2)

Application

Internal sorting of large data sets.

To improve the efficiency of the Quick sort various methods are used to choose the pivot
element.

One such method is called, median of three partitioning that uses the pivot element as the
median of left most, right most and the middle element of the array.

41
CS3401 ALGORITHMS UNIT 3 MEC

5. Define dynamic programming and explain the problems that can be solved using
dynamic Programming.

Synopsis:

Introduction
Problems that can be solved using dynamic programming
Principle of optimality
Computing a Binomial Coefficient
Example
Introduction:

 Dynamic Programming is an algorithm design technique.


 It was invented by a U.S. Mathematician Richard Bellman in the year 1950, as a
general method for optimizing multistage decision processes.
 Dynamic Programming is a technique for solving problems with overlapping sub
problems. The smaller sub problems are solved only once and recording the results
in a table from which the solution to the original problem is obtained.

 A dynamic-programming algorithm solves each sub problem just once and then
saves its answer in a table, thereby avoiding the work of recomputing the answer
every time it solves each sub problem.

 We typically apply dynamic programming to optimization problems. Such problems


can have many possible solutions. Each solution has a value, and we wish to find a
solution with the optimal (minimum or maximum) value. We call such a
solution an optimal solution to the problem, as opposed to the optimal solution,
since there may be several solutions that achieve the optimal value.
When developing a dynamic-programming algorithm, we follow a sequence of four
steps:
1. Characterize the structure of an optimal solution.
2. Recursively defines the value of an optimal solution.
3. Compute the value of an optimal solution, typically in a bottom-up fashion.
4. Construct an optimal solution from computed information
Problems that can be solved using dynamic programming:

Various problems those can be solved using dynamic programming are

For computing nth Fibonacci number

 Computing binomial coefficient


 Warshall’s algorithm
 Floyd’s algorithm
 Optimal binary search trees
Principle of optimality:

42
CS3401 ALGORITHMS UNIT 3 MEC

 The dynamic programming makes use of principle of optimality when finding


solution to given problem.
 The principle of optimality states that “ in an optimal sequence of choices or
decisions , each subsequence must also be optimal”.
 When it is not possible to apply principle of optimality, it is almost impossible to
obtain the solution using dynamic programming approach.
Example:
 While constructing optimal binary search tree we always select the value of k which
is obtained from minimum cost. Thus it follows principle of optimality.

Applications of Dynamic Programming Approach


 Matrix Chain Multiplication
 Longest Common Subsequence
 Travelling Salesman Problem

6. How dynamic programming approach is used in binomial coefficient?

Computing a Binomial Coefficient:

Computing a Binomial Coefficient is a typical example of applying dynamic programming in


mathematics, particularly in combinatory.
Binomial Coefficient is a Coefficient of any of the term in the expansion of (a+b) n.
The binomial coefficient is denoted by C(n, k) or (𝑛𝑘)
The binomial coefficient is the number of combinations or subsets of K elements from an n
element set(0≤ k ≥ n).
The name binomial coefficient comes from the participation of these numbers in the
binomial formula.
The binomial formula is
(a+ b) n =C(n.0)a n +---+C(n, i) a n-1 b n +----+ C(n. n)b n.

The binomial coefficient has several properties are.


The three important properties are
C(n, k)=C(n-1,k-1)+C(n-1,k), for N>k>0

C(n,0)= 1

C(n, n)=1

Example: Compute C(4,2)

Solution:

n=4, k=2

C(4,2) = C(n-1,k-1)+C(n-1,k)

C(4,2) =C(3,1) +C(3,2) --------(1)

As there are two unknowns : C(3,1) and C(3,2) in above


43
CS3401 ALGORITHMS UNIT 3 MEC

equation we will compute these sub instance of C(4, 2)

Therefore n=3 , k=1

C(3,1) =C(2,0) +C(2,1)

C(n, 0) = 1 we can write

C(2, 0) = 1

C(3, 1) = 1 + C(2,1) -------(2)

Hence let us compute C (2, 1)

Therefore n=2 , k=1

C(2,1) = C(n-1,k-1)+ C(n-1,k)

C(2,1) =C(1,0) +C(1,1)

But as C (n, 0) = 1 and C(n, n) = 1 we get

C( 1,0) = 1 and C( 1,1) = 1

C( 2,1) = C( 1, 0) + C( 1,1)

= 1 +1

C(2,1) = 2 ------------------(3)

Put the equation (2) and we get

C(3,1) = 1 +2

C(3,1) = 3 -------------------(4)

Now to solve equation 1 we will first compute C(3, 2) with n = 3 and k = 2.


Therefore

C(3,2) = C(n-1,k-1)+ C(n-1,k)

C( 3,2) = C( 2, 1) + C( 2,2)

But as C(n,n) = C( 2,2) = 1 , we will put values of C(2, 1) obtained in equation (3)

And C(2,2) in C(3,2) we get,

C( 3,2) = C( 2, 1) + C( 2,2)

= 2+ 1

C( 3,2) = 3 --------------------(5)

44
CS3401 ALGORITHMS UNIT 3 MEC

Put equation (4 ) and (5) in equation , then we get

C( 4,2) = C( 3, 1) + C( 3,2)

C( 4,2) = 3 + 3

C(4,2) = 6 is the final answer

The recurrence equation C(n,k)=C(n-1,k-1)+C(n-1,k) expresses the problem of computing


C(n,k) in terms of C(n-1,k-1) and C(n-1,k) lends itself to solve by the dynamic
programming technique.
The values of the binomial coefficient are recorded in a table of n+1 rows and K+1
columns, numbered from 0 to n and from 0 to k respectively, which is shown in figure
below

To compute the value of C(n,k), the table of figure is filled by row , starting with row 0 and
ending with row n.
Each row i(0≤ i ≤ n) is filled left to right, starting with 1 because C(n,0)=1.
Rows 0 through k also end with 1 on the table’s main diagonal (ie)
C(i,i)=1for 0≤ i ≤ k
The other entries of the table is computed by using the formula C(n,k)=C(n-1,k-1)+C(n-1,k),
for n>k>0 adding the contents of the cells in the preceding row and the previous column in
the preceding row and the same column.
Algorithm

Algorithm Binomial(n,k)

//Computes C(n,k) by the dynamic programming algorithm

// Input: A pair of nonnegative integers n≥k≥0.

//Output: The value of C(n,k)

for i←0 to n do

45
CS3401 ALGORITHMS UNIT 3 MEC

for j ←0 to min(i,k) do

if j=0 or j=k

C[i,j] ← 1

else

C[i,j] ← C[i-1,j-1]+ C[i-1,j]

return C[n,k]

Analysis:

The basic operation is addition i.e.


C[i,j] ← C[i – 1, j-1] +C[i- 1, j]
Let A(n,k) denotes total additions made in computing C(n,k).
In the table, first K+1 rows of the table form a triangle and the remaining n-k rows
form a rectangle.
So the recurrence relation for A(n,k) is divided into the parts.
The recurrence relation is,

7. Explain the Elements of dynamic programming.

Two main properties of a problem suggest that the given problem can be solved using
Dynamic Programming.
These properties are
 Overlapping sub-problems

46
CS3401 ALGORITHMS UNIT 3 MEC

 Optimal substructure.

Overlapping Sub-Problems
Similar to Divide-and-Conquer approach, Dynamic Programming also combines solutions
to sub-problems. It is mainly used where the solution of one sub-problem is needed
repeatedly. The computed solutions are stored in a table, so that these don’t have to be re-
computed. Hence, this technique is needed where overlapping sub-problem exists.
For example, Binary Search does not have overlapping sub-problem. Whereas recursive
program of Fibonacci numbers have many overlapping sub-problems.

Fibonacci series

A sequence of Fibonacci numbers is 0,1,1,2,3,5,8,13,21,34………..

The Fibonacci sequence can be defined by the simple recurrence

F(n)=F(n-1)+F(n-2),for n>1…………………1

The two initial conditions are

F(0)=0

F(1)=1

Explicit formula for the nth Fibonacci number

Backward substitution method is not used to solve the recurrence F(n)=F(n-


1)+F(n-2),for n>1,because which fails to produce easily discernible pattern.

So, the theorem that describes solution to a homogeneous second order


linear recurrence with constant coefficient is used to solve the problem.

The homogenous with constant coefficient is

ax(n)+bx(n-1)+cx(n-2)=0 ……………(2)

Where,

a,b,c are fixed real numbers called the coefficients of recurrence and a≠0

x(n) is the unknown sequence to be found

The characteristics equation of the recurrence equation is

Ar2+br+c=0 ………………….(3)

The recurrence relation can be written as

F(n)-F(n-1)-F(n-2)=0 ………….(4)

47
CS3401 ALGORITHMS UNIT 3 MEC

The characteristics equation for (4)

r2-r-1=0

The roots are

R1,2=

R1,2=

R1=

R2=

The characteristics equation has two distinct real roots.

Now the recurrence relation is

X(n)=αr1n+βr2n ……..(5)

Substitute r1 and r2 in (5),

F(n)= α( )n+β( )n ……….(6)

Now substitute the value of f(0) and F(1) in equation(6)

F(0) = α( )0+β( )0 =0 ……….(7)

F(1)= α( )1+β( )1 =0 ……….(8)

By solving equation (7) and (8),the linear equation in two unknown α and β

α+ β=0

α( )+β( )=0 ……….(11)

(11)-(10) gives

( ) β-( ) β=-1

48
CS3401 ALGORITHMS UNIT 3 MEC

𝛃 √𝟓 𝛃 √𝟓
+ β- 𝟐 + β = -1
𝟐 𝟐 𝟐

√𝟓
𝟐 β = -1
𝟐

√𝟓
β=-𝟐

√5
Substitute β = - 2 in (9)

α+β=0
𝟏
α- =0
√𝟓

𝟏 √𝟓
α= β=-
√𝟓 𝟐

Substitute the value of α and β in equation (6)


𝟏 𝟏+√𝟓 n - 𝟏 𝟏+√𝟓 n
F(n) = ⟦ ⟧ ⟦ ⟧
√𝟓 𝟐 √𝟓 𝟐

𝟏
F(n) = ⟦𝝓𝐧 − 𝝓^𝐧⟧
√𝟓

Where
𝟏+√𝟓
Φ= 𝟐

Φ = 1.61803
1
Φ^ =- Φ

Φ^ = - 0.61803

The constant Φ is known as, Golden Ratio.

The value of Φ^ is lies between -1 and 0.

When n goes to infinity, Φ^ gets infinitely small value. So, it can be omitted.

Therefore
𝟏
F(n) = Φn (13)
√𝟓

𝟏
So, for every non negative n, F(n) = Φ n is rounded to the nearest integer.
√𝟓

Algorithm for computing Fibonacci numbers

First method

49
CS3401 ALGORITHMS UNIT 3 MEC

Algorithm F(n)

//Computes the nth Fibonacci number recursively by using its definition.

//Input: A nonnegative integer n

//Output: The nth Fibonacci number

if n<1

return n

Else

return F(n-1)+(n-2)

the algorithm’s basic operation is addition.

Let A(n) is the number of additions performed by the algorithm to compute F(n).

The number of additions needed to compute F(n-1) is A(n-1) and the number of
additions needed to compute F(n-2) is A(n-2).

The algorithm needs one more addition to compute the sum of A(n-1) and A(n-2).

Thus the recurrence for A(n) is

A(n)=A(n-1) + A(n-2)+1, for n>1


A(0)=0

A(1)=0

The recurrence A(n)-A(n-1)-A(n-2)=1 is same as F(n)-F(n-1)-F(n-2)=0, but its right


hand side not equal to zero. These recurrences are called inhomogeneous
recurrences.

General techniques are used to solve inhomogeneous recurrences.

The inhomogeneous recurrences is converted into homogeneous recurrence by


rewriting the in homogeneous recurrence as,

A(n)+1]-[A(n-1)+1]-[A(n-2)+1]=0 (14)

Now substitute,

B(n)=A(n)+1

Now (14) becomes,

B(n)-B(n-1)-B(n-2)=0

50
CS3401 ALGORITHMS UNIT 3 MEC

B(0)=0

B(1)=1

Here

B(n)=F(n+1)

Since

B(n)=A(n)+1

B(n-1)=A(n)

So A(n)=B(n)-1

Substitute F(n+1)-1 …………(15)

We know that

F(n)=

F(n+1)= ………(16)

Substitute (16) in (15)

A(n)= -1

Hence

A(n)€

The poor efficiency class of algorithm could be anticipated from the class of
recurrence

The reason behind the algorithm inefficiency can be traced by looking at the tree of
recursive calls n=6

The same values of the function are evaluated again and again which is extremely
inefficiently.

51
CS3401 ALGORITHMS UNIT 3 MEC

F(6)

F(5) F(4)

F(3) F(2)
F(4) F(3)

F(1) F(0)
F2) F(1) F(2) F(1)
F(3) F(2)

F(1) F(0) F(1) F(0)

F(2) F(1)

F(0)
F(1)

Fig Tree of recursive calls for computing the Fibonacci number for n = 6

Optimal Sub-Structure
 A given problem has Optimal Substructure Property, if the optimal solution of the
given problem can be obtained using optimal solutions of its sub-problems.
 For example, the Shortest Path problem, A rod Cutting problem has the following
optimal substructure property −
 If a node x lies in the shortest path from a source node u to destination node v, then
the shortest path from u to v is the combination of the shortest path from u to x, and
the shortest path from x to v.
 The standard All Pair Shortest Path algorithms like Floyd-Warshall and Bellman-
Ford are typical examples of Dynamic Programming.
Example of rod Cutting problem:
A rod is given of length n. Another table is also provided, which contains different size and
price for each size. Determine the maximum price by cutting the rod and selling them in
the market.
To get the best price by making a cut at different positions and comparing the prices after
cutting the rod.
Let the f(n) will return the max possible price after cutting a row with length n. We can
simply write the function f(n) like this.
f(n) := maximum value from price[i]+f(n – i – 1), where i is in range 0 to (n – 1).

52
CS3401 ALGORITHMS UNIT 3 MEC

Input and Output


Input:
The price of different lengths, and the length of rod. Here the length is 8.

Output:
Maximum profit after selling is 22.
Cut the rod in length 2 and 6. The profit is 5 + 17 = 22

Algorithm
rodCutting(price, n)
Input: Price list, number of different prices on the list.
Output: Maximum profit by cutting rods.

Begin
define profit array of size n +1
profit[0] := 0
for i := 1 to n, do
maxProfit := - ∞
for j := 0 to i-1, do
maxProfit := maximum of maxProfit and (price[j] + profit[i-j-1])
done
profit[i] := maxProfit
done
return maxProfit
End
[Link] the Matrix chain multiplication problem with an example.(Apr/may 2023,
2024) Aprl/May 2024

Matrix chain multiplication problem: Determine the optimal parenthesization of a product


of n matrices.

Matrix chain multiplication (or Matrix Chain Ordering Problem, MCOP) is an optimization
problem that to find the most efficient way to multiply a given sequence of matrices. The
53
CS3401 ALGORITHMS UNIT 3 MEC

problem is not actually to perform the multiplications but merely to decide the sequence of
the matrix multiplications involved.
Following is the recursive algorithm to find the minimum cost:

 Take the sequence of matrices and separate it into two subsequences.


 Find the minimum cost of multiplying out each subsequence.
 Add these costs together, and add in the price of multiplying the two result matrices.
 Do this for each possible position at which the sequence of matrices can be split, and
take the minimum over all of them.
Example of Matrix Chain Multiplication

Example: Given the sequence {4, 10, 3, 12, 20, and 7}. The matrices have size 4 x 10, 10 x 3,
3 x 12, 12 x 20, 20 x 7. We need to compute M [i,j], 0 ≤ i, j≤ 5. We know M [i, i] = 0 for all i.

Let us proceed with working away from the diagonal. We compute the optimal solution for
the product of 2 matrices.

Here P0 to P5 are Position and M1 to M5 are matrix of size (pi to pi-1)

On the basis of sequence, we make a formula

In Dynamic Programming, initialization of every method done by '0'.So we initialize it by


'0'.It will sort out diagonally.

We have to sort out all the combination but the minimum output combination is taken into
consideration.

Calculation of Product of 2 matrices:

54
CS3401 ALGORITHMS UNIT 3 MEC

1. m (1,2) = m1 x m2
= 4 x 10 x 10 x 3
= 4 x 10 x 3 = 120
2. m (2, 3) = m2 x m3
= 10 x 3 x 3 x 12
= 10 x 3 x 12 = 360
3. m (3, 4) = m3 x m4
= 3 x 12 x 12 x 20
= 3 x 12 x 20 = 720
4. m (4,5) = m4 x m5
= 12 x 20 x 20 x 7
= 12 x 20 x 7 = 1680

o We initialize the diagonal element with equal i,j value with '0'.
o After that second diagonal is sorted out and we get all the values corresponded to it

Now the third diagonal will be solved out in the same way.

Now product of 3 matrices:

M [1, 3] = M1 M2 M3
1. There are two cases by which we can solve this multiplication: ( M1 x M2) + M3,
M1+ (M2x M3)
2. After solving both cases we choose the case in which minimum output is there.

M [1, 3] =264

As Comparing both output 264 is minimum in both cases so we insert 264 in table and (
M1 x M2) + M3 this combination is chosen for the output making.

M [2, 4] = M2 M3 M4
1. There are two cases by which we can solve this multiplication: (M2x M3)+M4,
M2+(M3 x M4)

55
CS3401 ALGORITHMS UNIT 3 MEC

2. After solving both cases we choose the case in which minimum output is there.

M [2, 4] = 1320

As Comparing both output 1320 is minimum in both cases so we insert 1320 in table and
M2+(M3 x M4) this combination is chosen for the output making.

M [3, 5] = M3 M4 M5
1. There are two cases by which we can solve this multiplication: ( M3 x M4) + M5,
M3+ ( M4xM5)
2. After solving both cases we choose the case in which minimum output is there.

M [3, 5] = 1140

As Comparing both output 1140 is minimum in both cases so we insert 1140 in table and (
M3 x M4) + M5this combination is chosen for the output making.

Now Product of 4 matrices:

M [1, 4] = M1 M2 M3 M4

There are three cases by which we can solve this multiplication:

1. ( M1 x M2 x M3) M4
2. M1 x(M2 x M3 x M4)
3. (M1 xM2) x ( M3 x M4)

After solving these cases we choose the case in which minimum output is there

56
CS3401 ALGORITHMS UNIT 3 MEC

M [1, 4] =1080

As comparing the output of different cases then '1080' is minimum output, so we insert
1080 in the table and (M1 xM2) x (M3 x M4) combination is taken out in output making,

M [2, 5] = M2 M3 M4 M5

There are three cases by which we can solve this multiplication:

1. (M2 x M3 x M4)x M5
2. M2 x( M3 x M4 x M5)
3. (M2 x M3)x ( M4 x M5)

After solving these cases we choose the case in which minimum output is there

M [2, 5] = 1350

As comparing the output of different cases then '1350' is minimum output, so we insert
1350 in the table and M2 x( M3 x M4 xM5)combination is taken out in output making.

Now Product of 5 matrices:

M [1, 5] = M1 M2 M3 M4 M5

There are five cases by which we can solve this multiplication:

1. (M1 x M2 xM3 x M4 )x M5
2. M1 x( M2 xM3 x M4 xM5)
3. (M1 x M2 xM3)x M4 xM5
4. M1 x M2x(M3 x M4 xM5)

After solving these cases we choose the case in which minimum output is there

57
CS3401 ALGORITHMS UNIT 3 MEC

M [1, 5] = 1344

As comparing the output of different cases then '1344' is minimum output, so we insert
1344 in the table and M1 x M2 x(M3 x M4 x M5)combination is taken out in output making.

Final Output is:

Step 3: Computing Optimal Costs:

 let us assume that matrix Ai has dimension pi-1x pi for i=1, 2, 3....n.
 The input is a sequence (p0,p1,......pn) where length [p] = n+1.
 The procedure uses an auxiliary table m [1....n, 1.....n] for storing m [i, j] costs an
auxiliary table s [1.....n, 1.....n] that record which index of k achieved the optimal
costs in computing m [i, j].
 The algorithm first computes m [i, j] ← 0 for i=1, 2, 3.....n, the minimum costs for the
chain of length 1

Algorithm of Matrix Chain Multiplication

MATRIX-CHAIN-ORDER (p)

1. n length[p]-1
2. for i ← 1 to n
3. do m [i, i] ← 0
4. for l ← 2 to n // l is the chain length
5. do for i ← 1 to n-l + 1
6. do j ← i+ l -1
7. m[i,j] ← ∞
8. for k ← i to j-1
9. do q ← m [i, k] + m [k + 1, j] + pi-1 pk pj
10. If q < m [i,j]
11. then m [i,j] ← q
12. s [i,j] ← k
13. return m and s.

58
CS3401 ALGORITHMS UNIT 3 MEC

Step 1: Constructing an Optimal Solution:

PRINT-OPTIMAL-PARENS (s, i, j)
1. if i=j
2. then print "A"
3. else print "("
4. PRINT-OPTIMAL-PARENS (s, i, s [i, j])
5. PRINT-OPTIMAL-PARENS (s, s [i, j] + 1, j)
6. print ")"

Analysis: There are three nested loops. Each loop executes a maximum n times.

1. l, length, O (n) iterations.


2. i, start, O (n) iterations.
3. k, split point, O (n) iterations

Body of loop constant complexity

Total Complexity is: O (n3)

9. using dynamic approach programming, solve the following Multistage Graph using
the forward and backward approach. (APRIL/MAY 2011)

Multistage Graph:-

Concept:

The multistage graph problem is to find a minimum cost path from S to t.

Problem description:

 A multistage graph G=(V,E) is a directed graph in which the vertices are


portioned into K> 2 disjoint sets Vi, 1<i<=K.
 if (u,v) is an edge in E, then u E Vi and VEVi+1 for some i., 1< = i< =K.
 The sets V1 and Vk are such that (V1)= VK/=1, Let S and t respectively the vertex
in b1 and bk.
 The vertex S is the source, and t is the sin R. Let C (i, j) be the cost of edge (i,j)
 The cost of a path from S to t is the sun of the cost of edges on the path.
 Each set vi defines a stage in the graph Because of the constraints on E.
 Every path from S to t starts in stage 1, goes to stage 2, then to stage 3, then to
stage 4, etc., and finally terminates in stage K.
Procedure for multistage problems:

Multistage graph using forward Approach:-

 Find path from s to t, stage by stage.

59
CS3401 ALGORITHMS UNIT 3 MEC

 Every s to t path is the result of a sequence of K-2 decisions.


 The ith decision involves determining which vertex in Vi+1, 1<i< K-2, is to be on
the path.
 P (i,j) be a minimum cost path from vertex j in vi to vertex t.
 cost (i,j) be the cost of the path.
 Find cost of path using the formula.
Cost (i,j) = min {C(f,l) + Cost (i+1, l)}

l€ Vi+1

(j,l) €E

i) Cost (K-1,j) =if c (j,t) €E


ii) Cost ( K-1, j) = α if (j,t) ) €E
iii) The shortest distance between source S and sink t using following formula.

Cost (K-2, j) for all j €Vk-2

Cost (K-3,j) for all j € VK-3

Cost ( 1,s)

Example:

Find the shortest distance between source ‘s’ and sink ‘t’.

Using 5 stage graph

1. Compute cost (k-2,j) for all j€Vk-2

K=5,because it is 5 stage graph.

III stage contains 6,7&8 ie., 3nodes.

i)Cost (i,j) = Min{c(j,l) + cost (i+1, l)}

ii)Cost (3,6) = Min{6+ Cost (4,9), 5+ cost (4,10)}

= Min{(6+4), 5+2)}

= Min {10,7}

60
CS3401 ALGORITHMS UNIT 3 MEC

Cost (3,6) =7

iii)Cost (3,7) = Min{c(j,l) + cost (i+1, l)}

=Min{( 4+ cost (4,9), 3+ cost (4,10)}

= Min {( 4+4, 3+2)}

= Min {( 8,5)}

Cost (3,7) =5

iV)Cost (3,8) = Min{c(j,l) + cost (i+1, l)}

=Min{5+cost (4,10) 6+cost (4,11)}

= Min { 5+2, 6+5}

= Min (7,11)

Cost (3,8) = 7

2. Compute cost (k-3,j) for all j€Vk-3

II stage contains 2,3,4& 5nodes.

 Cost (2,2) = Min (4+cost(3,6) 2+cost (3,7), 1+ Cost (3,8))


= Min ( 4+7, 2+5,1+7)

= Min (11,7,8)

Cost (2,2) =7

 Cost (2,3) = Min (2+ Cost (3,6) 7+ Cost (3,7)


= Min (2+7, 7+5)
= Min ( 9,12)
Cost (2,3) = 9
 Cost (2,4) = Min ( 11+ Cost ( 3,8)
= Min ( 11+7)

= Min ( 18)

Cost (2,4) = 18

 Cost (2,5) = Min (11+ Cost (3,7) 8+Cost (3,8)


= Min (11+5, 8+7)
= Min ( 16, 15)
Cost (2,5) = 15
3. Compute cost (1,s)

I stage contains 1 node

61
CS3401 ALGORITHMS UNIT 3 MEC

1. Cost (1,1) = Min ( 9+ Cost (2,2) 7+ Cot (2,3),3+ Cost (2,4) 2+ Cost
(2,5))
= Min (9+7, 7+9, 3+18, 2+15)

= Min (16,16,21,17)

Cost (1,1) = 16

Conclusion:- (forward approach)

A Minimum cost S to t path has a cost of 16.

Program:-

Multistage graph using forward approach.

Void F Graph (graphG, int K, int n, int P ( ) )

// The input is a K-stage graph G=(V,E) with n Vertices

// indexed in order of stages

//E is a set of edges and c(I,j) is the cost of (i,j)

// P(i:K) is a minimum cost path vertex

Cost [n]=0.0;//cost of vertex n is zero

for (j= n-1; j>=1; j --)

{// compute cost (j)

//Let r be the vertexsuch that (j,r) is an edge of G and

//c[j,r] + cost[r] is minimum

Cost[j] = C[j,r]+ Cost[r];

D[j] = r;

//find a minimum cost path

P[1] = 1

P[k] = n;

for(j=2,j<K-1; j+1)

P[j]=d(P[j-1]];

62
CS3401 ALGORITHMS UNIT 3 MEC

Let the minimum cost path be s=1, v2, v3 , Vk-1, t

for the above figure

v2 =d(1,1)=2

v3 =d(2,D(1,1))

v3 =d(2,2)=7

=d(3,d(2,d(1,1)))

=d(3,7)

=10

Multistage graph using Backward Approach:-

i) The multistage graph can be solved using the backward approach.


ii) Let, bp(i,j) be a minimum cost path from vertex S to a vertex j in Vi
iii) bcost(i,j) be the cost of bp(i,j)
iv) Shortest path from source‘s’ to sink ‘t’ using backward
v) bcost(i,j) = min {bcost ( i-1, l) + c (l, j)}
l€vi-1

(l,j)€E

vi) bcost(2,j)=c(1,j) if(1,j) €E


vii) bcost(2,j)=α if(1,j) €E
Find shortest path from source ‘s’ to sink ‘t’ for the following graph using backward
approach.

Compute bcost for i=2

bcost(2,2)=min{c(1,2)}
=9
bcost(2,3)=min{c(1,3)}
=7
bcost(2,4)=min{c(1,4)}
=3
bcost(2,5)=min{c(1,5)}
=2

63
CS3401 ALGORITHMS UNIT 3 MEC

 Compute bcost for i=3


bcost (3,6) = min { bcost(2,2) +c(2,6),bcost(2,3) +c(3,6)}
= min {(9+2), (7+2)}

= min {13,9}

=9

bcost (3,7) = min ( bcost (2,2) +c(2,7),bcost(2,3) +c(3,7),bcost(2,5)+c(5,7)}

= min {(9+2),(7+7 ),(2+11)}

= min {11,14,13)

=11

bcost (3,8) = min ( bcost (2,2) +c(2,8),bcost(2,4) +c(4,8),bcost(2,5)+c(5,8)}


= min {(9+1),(3+11),(2+8)}

= min {10,14,10)

=10

 Compute bcost for i=4


bcost (4,9) = min { bcost(3,6) +c(6,9),bcost(3,7) +c(7,9)}
= min {(9+6), (11+4)}

= min {15,15}

=15

bcost (4,10) = min { bcost(3,6) +c(6,10),bcost(3,7) +c(7,10),

bcost(3,8)+c(8,10)}}
= min {(9+5),(11+3), (10+5)}

= min {14,14,15}

=14

bcost (4,11) = min { bcost(3,8) +c(8,11)}


= min {(10+6)}

= min {16}

=16

 Compute bcost for i=5


bcost (5,12) = min { bcost(4,9) +c(9,12),bcost(4,10) +c(10,12),

64
CS3401 ALGORITHMS UNIT 3 MEC

bcost(4,11)+c(11,12)}
= min {(15+4), (14+2),(16+5)}

= min {19,16,21}

=16

Conclusion: (Backward approach)

A minimum cost s to t path has a cost of 16.

Algorithm: (Backward approach)

Void Bgraph (graph G, int K, int n, intp[])

bcost [1] = 0.0;

//cost of vertex 1 is zero

for (j=2; j<n, j++)

// compute bcost [j]

//Let r be such that (r,j) is an edge of G and bcost[r]+C[r,j] is

// minimum

Bcost[j] = bcost[r]+C[r,j];

D[j]=r;

//find a minimum cost path

P[1] =1;

P[k]=n;

for(j=k-1,j>=2,j--)

P[j]=d[P[j+1]];

Complexity of multistage graph for both forward and backward approach.

Time complexity:

65
CS3401 ALGORITHMS UNIT 3 MEC

Finding the minimum cost for each and every stage – θ(|V|+|E|)

Shortest path from source s to sink t->θ(k)

Space complexity:

Storage space for cost array cost[] - n location

Storage space for minimum cost path array p[] - n location

Storage space for decision array d[] - n location

Storage space for stage ‘K’ variable -1

Storage space for variable ‘n’ -1

Storage variable ‘j’ -1

Total storage space -3n+3 =3(n+1)

Application of multistage problem:

Multistage graphs can be used to model a variety of real-world phenomena,

 Including social networks, transportation systems

 Communication networks.

 They also have applications in computer science and engineering, such as in


the design of parallel algorithms and distributed system

 Resource allocation problem

10. Write a pseudo code to find Optimal binary search trees using dynamic
programming(OBST) May 2008/2011 & Dec 2013 may 2015Or Obtain a optimal binary
search tree for following nodes (do, if ,int , while) with following probabilities ( 0.1, 0.2 , 0.4,
0.3)Or(i) outline dynamic programming approach to solve the optimal binary search tree
problem and analyse its time complexity

(ii) Construct the optimal binary search tree for the following 5 keys with probabilities
asindicated. Nov/Dec 2019

I 0 1 2 3 4 5

Pi 0.15 0.10 0.05 0.10 0.20

Pj 0.05 0.10 0.05 0.05 0.05 0.10

 A binary search tree is one of the most important data structures in computer
science.
66
CS3401 ALGORITHMS UNIT 3 MEC

 One of its principal application is to implement a dictionary, a set of


Elements with the operations of searching, insertion, and deletion.
Example:
Consider four keys A, B, C, and D to be searched for with probabilities 0.1, 0.2, 0.4, and 0.3,
respectively.

Figure depicts two out of 14 possible binary search trees containing these keys.
The average number of comparisons in a successful search in the first of these trees is
0. 1+ 0.2 .2 + 0.4 .3+ 0.3 .4 = 2.9,

for the second one it is


0. 2 + 0.2 .1+ 0.4 .2 + 0.3 .3= 2.1.
Neither of these two trees is, in fact, optimal.
For our tiny example, we could find the optimal tree by generating all 14 binary search
trees with these keys.
As a general algorithm, this exhaustive-search approach is unrealistic: the total number of
binary search trees with n keys is equal to the nth Catalan number,

Optimal binary search tree

Definition

 Let {a1, a2,….an} be a set of identifiers such that a1<a2<a3…let , p(i) be the
probability with which we can search for ai is Successful search.
 Let qi be the probability of searching an element x such that ai<x<ai+1 where 0≤i≤ n
is unsuccessful search.
 Thus p(i) is probability of successful search and q(i) is the probability of
unsuccessful search.
𝑛
𝑛
 Then a tree which is build with optimum cost from ∑ p(i) ∑𝑖=1 q(i) is called
𝑖=1
optimal binary search tree.

67
CS3401 ALGORITHMS UNIT 3 MEC

1. For such a binary search tree , the root contains key ak, the left sub tree Tik -1
contains keys ai, . . . , ak−1 optimally arranged, and the right subtree Tjk+1 contains
keys ak+1, . . . , ajalso optimally arranged.
2. If we count tree levels starting with 1 to make the comparison numbers equal the
keys’ levels, the following recurrence relation is obtained:

Initially we assume that C[I ,i-1] = 0 for I ranging from 1 to n+1.


Then set C[I, i] = pi where 1≤ i≤ j≤n.

that means we have to two tables in optimal binary search tree.

Table of the dynamic programming algorithm for constructing an optimal binary search
tree.

68
CS3401 ALGORITHMS UNIT 3 MEC

 Fill up C[i ,i-1 ] by 0 and C[ n+1, n] by 0


 Fill up C[i ,i ] by p[i]
 Fill up C[i ,j] using formula

EXAMPLE

Obtain a optimal binary search tree for following nodes (do, if ,int ,while) with
following probabilities ( 0.1, 0.2 , 0.4, 0.3)

Solution : There are 4 nodes.

Hence n = 4

69
CS3401 ALGORITHMS UNIT 3 MEC

70
CS3401 ALGORITHMS UNIT 3 MEC

71
CS3401 ALGORITHMS UNIT 3 MEC

72
CS3401 ALGORITHMS UNIT 3 MEC

Cost Table : 0 1 2 3 4

73
CS3401 ALGORITHMS UNIT 3 MEC

1
0 0.1
2

3 0 0.2

4
0 0.4
5

0 0.3

Root Table 0 1 2 3

1 1
2
2
3

4 3

Cost Table 4
C[ 1, 0] = 0

C[ 2, 1] = 0
C[ 3, 2] = 0 using formulae C[ i, i – 1] =0 and C[ n + 1,n] =0

C[ 4, 3] = 0

C[ 5, 4] = 0

C[ 1, 1] = 0.1

C[ 2, 2] = 0.2 using formulae C[ i, i ] =p[i]

C[ 3, 3] = 0.4

C[ 4, 4] = 0.3

The root table

74
CS3401 ALGORITHMS UNIT 3 MEC

R[1, 1] =1

R[2, 2] =2 using formulae R[ i, i ] =i

R[3, 3] =3

R[4, 4] =4

Now let us compute C[i, j] diagonally using formula

- -- (1)

Compute C[ 1, 2 ]

The value of k can be either 1 or 2

Let i = 1 , j = 2 , use formula in equation (1),

k= 1

C[1,2]= C[ 1,0] + C[2 ,2] + p[1] +p[2]

= 0 + 0.2 + 0.1 + 0.2

= 0.5

k=2

C[1,2]=C[ 1,1] + C[3 ,2] + p[1] +p[2]

= 0.1 + 0 + 0.1 + 0.2

= 0.4-> minimum value therefore consider k = 2

Therefore in cost table C[1,2] = 0.4 and R[1,2]=2

Compute C[2, 3]

The value of k can be 2 or 3.

Let i = 2, j = 3, use formula equation (1)

k=2

C[2,3] =C[ 2,1] + C[3 ,3] + p[2] +p[3]

=0 + 0.4+ 0.2+ 0.4

=1.0

75
CS3401 ALGORITHMS UNIT 3 MEC

k=3

C[2,3]= C[ 2,2] + C[4 ,3] + p[2] +p[3]

= 0.2 + 0 + 0.2 + 0.4

= 0.8 -> minimum value therefore consider k = 3

Therefore in cost table C[2,3] = 0.8 and R[2,3]=3

Compute C[3, 4]

The value of k can be 3 or 4.

Let ,i = 3, j = 4, use formula equation (1)

k=3

C[3,4]= C[ 3,2] + C[4 ,4] + p[3] +p[4]

= 0 + 0.3+ 0.4+ 0.3

= 1.0 -> minimum value therefore consider k = 3

k=4

C[3,4]= C[ 3,3] + C[5 ,4] + p[3] +p[4]

= 0.4 + 0 + 0.4 + 0.3= 1.1

Therefore in cost table C[3,4] = 1.0 and R[3,4]=3The table contains values
obtained upto this calculations

Cost Table 0 1 2 3 4

0 0.1 0.4
1
0 0.2 0.8
2
0 0.4 1.0
3

4 0 0.3

0
5

76
CS3401 ALGORITHMS UNIT 3 MEC

Root Table

0 1 2 3

1 1 2
2
2 3

3
3 3
4

Compute C[1,3] 4
The value of k can be 1,2 or 3

Consider i=1 , j=3.

k=1

C[1,3] = C[ 1,0] + C[2 ,3] + p[1] +p[2] + p[3]

= 0 + 0.8 + 0.1 + 0.2 + 0.4

= 1.5

k =2

C[1,3]= C[ 1,1] + C[3 ,3] + p[1] +p[2] + p[3]

= 0.1 + 0.4 + 0.1 + 0.2 + 0.4

= 1.2

k =3

C[1,3]= C[ 1,2] + C[4 ,3] + p[1] +p[2] + p[3]

= 0 .4+ 0 + 0.1 + 0.2 + 0.4

= 1.1-> minimum value therefore consider k = 3

Therefore C[1,3] = 1.1 and R[1,3] = 3

Compute C [2,4]

The value of k can be 2,3 or 4

Consider i=2, j=4 and using equation (1)

77
CS3401 ALGORITHMS UNIT 3 MEC

k=2

C[2,4]= C[ 2,1] + C[3 ,4] + p[2] +p[3] + p[4]

= 0 + 1.0 + 0.2 + 0.4 + 0.3

= 1.9

k =3

C[2,4]=C[ 2,2] + C[4 ,4] + p[2] +p[3] + p[4]

= 0.2 + 0.3 + 0.2 + 0.4 + 0.3

= 1.4 -> minimum value therefore consider k = 3

k =4

C[2,4]=C[ 2,3] + C[5 ,4] + p[2] +p[3] + p[4]

= 0 .8+ 0 + 0.2 + 0.4 + 0.3

= 1.7

Therefore C[2,4]= 1.4 and R[2,4]= 3

Cost Table

0 1 2 3 4

0 0.1 0.4 1.1


1
0 0.2 0.8 1.4
2

3
0 0.4 1.0
4

5
0 0.3

78
CS3401 ALGORITHMS UNIT 3 MEC

Root Table

0 1 2 3

1 2 3
1

2 2 3 3

3
3 3
4

4
Compute C[1,4]

The value of k can be 1, 2, 3 or 4.

Consider i=1, j=4 and using equation (1)

k=1

C[1,4] = C[ 1,0] + C[2 ,4] + p[1] + p[2] +p[3] + p[4]

= 0 + 1.4 + 0.1+ 0.2 + 0.4 +0.3

= 2.4

k =2

C[1,4]= C[ 1,1] + C[3 ,4] + p[1] + p[2] +p[3] + p[4]

= 0.1 + 1.0 + 0.1 + 0.2 + 0.4 +0.3

= 2.1

k =3

C[1,4]=C[ 1,2] + C[4 ,4] + p[1] + p[2] +p[3] + p[4]

= 0.4 + 0.3 + 0.1 + 0.2 + 0.4 +0.3

= 1.7-> minimum value therefore consider k = 3

k=4

C[1,4]= C[ 1,3 + C[5 ,4] + p[1] + p[2] +p[3] + p[4]

= 1.3 + 0 + 0.1 + 0.2 + 0.4 +0.3

= 2.3 Therefore C[1,4]= 1.7 and R[1,4]= 3

79
CS3401 ALGORITHMS UNIT 3 MEC

Cost Table 0 1 2 3 4

0 0.1 0.4 1.1 1.7


1
0 0.2 0.8 1.4
2

3
0 0.4 1.0
4

5
0 0.3

0
Root Table

0 1 2 3

1 2 3 3
1

2 2 3 3

3
3 3
4

To build a tree R[1][n] = R[1][4] =3 becomes root.

1 2 3 4

A[i] Do If Int while

Key = 3 means int

Therefore “int” becomes root of optimal binary search tree.

The tree is

80
CS3401 ALGORITHMS UNIT 3 MEC

Tk Value of key

T[i, k-1]
T[k + 1, j]

Here i =1 , j=4 and k=3.

R[1,4]
=3

R[1,2]= R[4,4]
2 =4

R[1,1]
=1

The tree can be with optimum cost C[1,4] = 1.7

int

if while

do

Optimal binary search tree

ALGORITHM

ALGORITHM OptimalBST(P [1..n])

//Finds an optimal binary search tree by dynamic //programming

//Input: An array P[1..n] of search probabilities for a sorted //list of n keys

81
CS3401 ALGORITHMS UNIT 3 MEC

//Output: Average number of comparisons in successful //searches in the

//optimal BST and table R of sub trees’ roots in the optimal //BST

fori←1 to ndo

C[i, i− 1]←0

C[i, i]←P[i]

R[i, i]←I

C[n + 1, n]←0

ford←1 to n − 1 do //diagonal count

fori←1 to n − d do

j←i+ d

minval←∞

fork←ito j do

ifC[i, k − 1]+ C[k + 1, j]<minval

minval←C[i, k − 1]+ C[k + 1, j];

kmin←k

R[i, j ]←kmin

sum←P[i];

fors ←i+ 1 to j do

sum←sum+ P[s]

82
CS3401 ALGORITHMS UNIT 3 MEC

C[i, j ]←minval+ sum

returnC[1, n], R

Analysis:

The basic operation in above algorithm is computation of C[i,j] by finding the minimum
valued k.
This operation is located within three nested for loops hence the time complexity C(n) can
be

Hence the time complexity of optimal binary search algorithm is C(n)


= Θ (n3)

11. Explain in detail about Greedy Techniques.

The greedy method is a straightforward method.


This method is popular for obtaining the optimized solutions
In greedy technique, the solution is constructed through a sequence of steps , each
expanding a partially constructed solution obtained so far, until a complete solution to the
problem is reached .
o At each step the choice made should be, Feasible-It has to satisfy the
problem’s constraints.
o Locally optimal-It has to be the best local choice among all feasible choices
available on that step.
o Irrevocable -Once made, it cannot be changed on subsequent steps of the
algorithm.
General method

 The greedy method uses the subset paradigm or ordering paradigm to obtain the
solution.
 In subset paradigm, at each stage the decision is made based on whether a
particular input is in optimal solution or not .

The components that can be used in the greedy algorithm are:

83
CS3401 ALGORITHMS UNIT 3 MEC

o Candidate set: A solution that is created from the set is known as a candidate set.
o Selection function: This function is used to choose the candidate or subset which
can be added in the solution.
o Feasibility function: A function that is used to determine whether the candidate or
subset can be used to contribute to the solution or not.
o Objective function: A function is used to assign the value to the solution or the
partial solution.
o Solution function: This function is used to intimate whether the complete function
has been reached or not.

Pseudo code of Greedy Algorithm


Algorithm Greedy (a, n)
{
Solution : = 0;
for i = 0 to n do
{
x: = select(a);
if feasible(solution, x)
{
Solution: = union(solution , x)
}
return solution;
}}

The above is the greedy algorithm. Initially, the solution is assigned with zero value. We
pass the array and number of elements in the greedy algorithm. Inside the for loop, we
select the element one by one and checks whether the solution is feasible or not. If the
solution is feasible, then we perform the union.

Applications of greedy method

 Knapsack problem
 Prim’s algorithm for minimum spanning tree
 Kruskal’s algorithm for minimum spanning tree
 Finding shortest path
 Job sequence with deadlines
 Optimal storage on tapes
For solving all above problems, a set of feasible solutions is obtained. From this
solution, optimum solution is selected.

This optimum solution then becomes the final solution for given problem.

84
CS3401 ALGORITHMS UNIT 3 MEC

Divide and Conquer Vs greedy method:

Divide and Conquer Greedy method

Greedy method is used to obtain


Divide and conquer is used to obtain a optimum solution
solution to given problem.

In this technique, the problem is divided


into small sub problems are solved In greedy method a set of feasible
independently. Finally all the solutions solution is generated and optimum
of sub problems are collected together to solution is picked up
get the solution to the given problem

In this method , duplications in sub In greedy method , the optimum


solutions are neglected. The means selection is without revising previously
duplicate solution may be obtained generated solutions

Greedy method is comparatively


Divide and conquer is less efficient
efficient but there is no as such
because of rework on solutions
guarantee of getting optimum solution.

Example : knapsack problem, finding


Example : quick sort, binary search
mining spanning tree

Greedy method Vs Dynamicprogramming

GreedyMethod Dynamic Programming

Greedy method is used for obtaining Dynamic Programming is also for obtaining
optimum solution optimum solution

Greedy method a set of feasible solutions There is no special set of feasible


and the picks up the optimum solution solutions in this method

In Greedy method the optimum selection is Dynamic Programming considers all


without revising previously generated possible sequences in order to obtain the
solutions optimum solution

85
CS3401 ALGORITHMS UNIT 3 MEC

It is guaranteed that the dynamic


In Greedy method there is no as such
programming will generate optimal
guarantee of getting optimum solution
solution using principle of optimality

12. Explain the Elements of the greedy strategy.

A greedy algorithm obtains an optimal solution to a problem by making a sequence of


choices.

For each decision point in the algorithm, the choice that seems best at the moment is
chosen. This heuristic strategy does not always produce an optimal solution, but as we saw
in the activity-selection problem, sometimes it does.

Some of the general properties of greedy methods.

The process that we followed in to develop a greedy algorithm was a bit more involved
than is typical. We went through the following steps:
1. Determine the optimal substructure of the problem.
2. Develop a recursive solution.
3. Prove that at any stage of the recursion, one of the optimal choices is the greedy
choice. Thus, it is always safe to make the greedy choice.
4. Show that all but one of the sub problems induced by having made the greedy choice
are empty.
5. Develop a recursive algorithm that implements the greedy strategy.
6. Convert the recursive algorithm to an iterative algorithm.

In going through these steps, we saw in great detail the dynamic-programming


underpinnings of a greedy algorithm.

In practice, however, we usually streamline the above steps when designing a greedy
algorithm.

We develop our substructure with an eye toward making a greedy choice that leaves just
one sub problem to solve optimally.

For example, in the activity-selection problem, we first defined the sub problems Sij, where
both i and j varied. We then found that if we always made the greedy choice, we could
restrict the sub problems to be of the form Si.n+1.

Alternatively, we could have fashioned our optimal substructure with a greedy choice in
mind. That is, we could have dropped the second subscript and defined sub problems of the
form Si = {ak € S : fi ≤ sk}.

86
CS3401 ALGORITHMS UNIT 3 MEC

Then, we could have proven that a greedy choice (the first activity am to finish in Si),
combined with an optimal solution to the remaining set Sm of compatible activities, yields
an optimal solution to Si.

More generally, we design greedy algorithms according to the following sequence of steps:
1. Cast the optimization problem as one in which we make a choice and are left with
one subproblem to solve.
2. Prove that there is always an optimal solution to the original problem that makes the
greedy choice, so that the greedy choice is always safe.
3. Demonstrate that, having made the greedy choice, what remains is a subproblem
with the property that if we combine an optimal solution to the subproblem with the
greedy choice we have made, we arrive at an optimal solution to the original
problem.

How can one tell if a greedy algorithm will solve a particular optimization problem? There
is no way in general, but the greedy-choice property and optimal sub-structure are the two
key ingredients. If we can demonstrate that the problem has these properties, then we are
well on the way to developing a greedy algorithm for it.

Greedy-choice property

The first key ingredient is the greedy-choice property: a globally optimal solution can be
arrived at by making a locally optimal (greedy) choice. In other words, when we are
considering which choice to make, we make the choice that looks best in the current
problem, without considering results from subproblems.

Optimal substructure

A problem exhibits optimal substructure if an optimal solution to the problem contains


within it optimal solutions to subproblems.

This property is a key ingredient of assessing the applicability of dynamic programming as


well as greedy algorithms.

Given this optimal substructure, we argued that if we knew which activity to use as ak, we
could construct an optimal solution to Sij by selecting ak along with all activities in optimal
solutions to the subproblems Sik and Skj. Based on this observation of optimal
substructure.

use a more direct approach regarding optimal substructure when applying it to greedy
algorithms. As mentioned above, we have the luxury of assuming that we arrived at a
subproblem by having made the greedy choice in the original problem. All we really need
to do is argue that an optimal solution to the subproblem, combined with the greedy choice
already made, yields an optimal solution to the original problem.

This scheme implicitly uses induction on the subproblems to prove that making the greedy
choice at every step produces an optimal solution.

87
CS3401 ALGORITHMS UNIT 3 MEC

Greedy versus dynamic programming

Because the optimal-substructure property is exploited by both the greedy and dynamic-
programming strategies, one might be tempted to generate a dynamic-programming
solution to a problem when a greedy solution suffices, or one might mistakenly think that a
greedy solution works when in fact a dynamic-programming solution is required.

To illustrate the subtleties between the two techniques, let us investigate two variants of a
classical optimization problem.

The 0-1 knapsack problem is posed as follows. A thief robbing a store finds n items; the ith
item is worth vi dollars and weighs wi pounds, where vi and wi are integers. He wants to
take as valuable a load as possible, but he can carry at most W pounds in his knapsack for
some integer W.

Which items should he take? (This is called the 0-1 knapsack problem because each item
must either be taken or left behind; the thief cannot take a fractional amount of an item or
take an item more than once.)

In the fractional knapsack problem, the setup is the same, but the thief can take fractions of
items, rather than having to make a binary (0-1) choice for each item.

Both knapsack problems exhibit the optimal-substructure property. For the 0-1 problem,
consider the most valuable load that weighs at most W pounds.
13. Explain in detail about Activity-selection problem with an example. Apr/May 2024

The Activity Selection Problem is an optimization problem which deals with the selection of
non-conflicting activities that needs to be executed by a single person or machine in a given
time frame.

Each activity is marked by a start and finish time. Greedy technique is used for finding the
solution since this is an optimization problem.

What is Activity Selection Problem?

Let's consider that you have n activities with their start and finish times, the objective is to
find solution set having maximum number of non-conflicting activities that can be executed
in a single time frame, assuming that only one person or machine is available for execution.

Some points to note here:

It might not be possible to complete all the activities, since their timings can collapse.

Two activities, say i and j, are said to be non-conflicting if si >= fj or sj >=


fi where si and sj denote the starting time of activities i and j respectively, and fi and fj refer
to the finishing time of the activities i and j respectively.

88
CS3401 ALGORITHMS UNIT 3 MEC

Greedy approach can be used to find the solution since we want to maximize the count of
activities that can be executed. This approach will greedily choose an activity with earliest
finish time at every step, thus yielding an optimal solution.

Input Data for the Algorithm:

 act[] array containing all the activities.

 s[] array containing the starting time of all the activities.

 f[] array containing the finishing time of all the activities.

Output Data from the Algorithm:

 sol[] array referring to the solution set containing the maximum number of non-
conflicting activities.

Steps for Activity Selection Problem

Following are the steps we will be following to solve the activity selection problem,

Step 1: Sort the given activities in ascending order according to their finishing time.

Step 2: Select the first activity from sorted array act[] and add it to sol[] array.

Step 3: Repeat steps 4 and 5 for the remaining activities in act[].

Step 4: If the start time of the currently selected activity is greater than or equal to the
finish time of previously selected activity, then add it to the sol[] array.

Step 5: Select the next activity in act[] array.

Step 6: Print the sol[] array.

Activity Selection Problem Example

In the table below, we have 6 activities with corresponding start and end time, the
objective is to compute an execution schedule having maximum number of non-conflicting

activities:

89
CS3401 ALGORITHMS UNIT 3 MEC

A possible solution would be:

Step 1: Sort the given activities in ascending order according to their finishing time.

The table after we have sorted it:

Step 2: Select the first activity from sorted array act[] and add it to the sol[] array, thus sol =
{a2}.

Step 3: Repeat the steps 4 and 5 for the remaining activities in act[].

Step 4: If the start time of the currently selected activity is greater than or equal to the
finish time of the previously selected activity, then add it to sol[].

Step 5: Select the next activity in act[]

For the data given in the above table,

A. Select activity a3. Since the start time of a3 is greater than the finish time
of a2 (i.e. s(a3) > f(a2)), we add a3 to the solution set. Thus sol = {a2, a3}.

B. Select a4. Since s(a4) < f(a3), it is not added to the solution set.

C. Select a5. Since s(a5) > f(a3), a5 gets added to solution set. Thus sol = {a2, a3, a5}

D. Select a1. Since s(a1) < f(a5), a1 is not added to the solution set.

90
CS3401 ALGORITHMS UNIT 3 MEC

E. Select a6. a6 is added to the solution set since s(a6) > f(a5). Thus sol = {a2, a3, a5,
a6}.

Step 6: At last, print the array sol[]

Hence, the execution schedule of maximum number of non-conflicting activities will


be:

(1,2)

(3,4)

(5,7)

(8,9)

In the above diagram, the selected activities have been highlighted in grey.

Implementation of Activity Selection Problem Algorithm

Algorithm Of Greedy- Activity Selector:


GREEDY- ACTIVITY SELECTOR (s, f)
1. n ← length [s]
2. A ← {1}
3. j ← 1.
4. for i ← 2 to n
5. do if si ≥ fi
6. then A ← A ∪ {i}
7. j ← i
8. return A

91
CS3401 ALGORITHMS UNIT 3 MEC

Time Complexity Analysis

Following are the scenarios for computing the time complexity of Activity Selection
Algorithm:

 Case 1: When a given set of activities are already sorted according to their finishing
time, then there is no sorting mechanism involved, in such a case the complexity of
the algorithm will be O(n)

 Case 2: When a given set of activities is unsorted, then we will have to use
the sort() method defined in bits/stdc++ header file for sorting the activities list.
The time complexity of this method will be O(nlogn), which also defines complexity
of the algorithm.

Real-life Applications of Activity Selection Problem:

Following are some of the real-life applications of this problem:

 Scheduling multiple competing events in a room, such that each event has its own
start and end time.

 Scheduling manufacturing of multiple products on the same machine, such that each
product has its own production timelines.

 Activity Selection is one of the most well-known generic problems used in


Operations Research for dealing with real-life business problems.

14. Explain in detail about optimal merge pattern with example.

Optimal merge pattern is a pattern that relates to the merging of two or more sorted files
in a single sorted file. This type of merging can be done by the two-way merging method.

If we have two sorted files containing n and m records respectively then they could be
merged together, to obtain one sorted file in time O (n+m).

There are many ways in which pair wise merge can be done to get a single sorted file.
Different pairings require a different amount of computing time. The main thing is to pair
wise merge the n sorted files so that the number of comparisons will be less.

The formula of external merging cost is:

n
∑f(i)d(i)
i=1

92
CS3401 ALGORITHMS UNIT 3 MEC

Where, f (i) represents the number of records in each file and d (i) represents the depth

Algorithm for optimal merge pattern

An optimal merge pattern corresponds to a binary merge tree with minimum weighted
external path length. The function tree algorithm uses the greedy rule to get a two- way
merge tree for n files.

The algorithm contains an input list of n trees. There are three field child, rchild, and
weight in each node of the tree. Initially, each tree in a list contains just one node. This
external node has lchildand rchild field zero whereas weight is the length of one of the n
files to be merged.

For any tree in the list with root node t, t = it represents the weight that gives the length of
the merged file. There are two functions least (list) and insert (list, t) in a function tree.
Least (list) obtains a tree in lists whose root has the least weight and return a pointer to
this tree. This tree is deleted from the list. Function insert (list, t) inserts the tree with
root t into the list.

The main for loop in this algorithm is executed in n-1 times. If the list is kept in increasing
order according to the weight value in the roots, then least (list) needs only O(1) time
and insert (list, t) can be performed in O(n) time. Hence, the total time taken is O (n2).

If the list is represented as a minheap in which the root value is less than or equal to the
values of its children, then least (list) and insert (list, t) can be done in O (log n) time. In this
condition, the computing time for the tree is O (n log n).

Example:

 Given a set of unsorted files: 5, 3, 2, 7, 9, 13


 Now, arrange these elements in ascending order: 2, 3, 5, 7, 9, 13
 After this, pick two smallest numbers and repeat this until we left with only one
number.

93
CS3401 ALGORITHMS UNIT 3 MEC

Now follow following steps:

94
CS3401 ALGORITHMS UNIT 3 MEC

[Link] in detail about Huffman Trees. Or Write the Huffmans’ algorithm. Construct.
The Huffmans’ tree for the following data and obtain its Huffmans’ code. Nov/Dec 2017 or
(i)write the Huffman code algorithm and derive its time complexity(5+2)

(ii)generate the Huffman code for the following data comprising of alphabet and their
frequency.(6)

a:1, b :1 ,c :2, d :3, e :5, f: 8,g : 13,h : 21 Apr/May 2019, (APR/MAY 2023)

 A Huffman tree is a binary tree that minimizes the weighted path length from the
root to the leaves containing a set of predefined weights.
 The most important application of Huffman trees are Huffman codes.
 A Huffman code is a optimal prefix tree variable length encoding scheme that
assigns bit strings to characters based on their frequencies in a given text.
 This is accomplished by a greedy construction of a binary tree whose leaves
represent the alphabet characters and whose edges are labeled with 0’s and 1’s.
 To encode a text that comprises n characters from some alphabet by assigning to
each of the text’s characters some sequence of bit called the code word.

Types of encoding
 Fixed length encoding
 Variable length encoding
Fixed length encoding
It assigns to each character a bit string of the some length m (m>=log 2n)

Variable length encoding

95
CS3401 ALGORITHMS UNIT 3 MEC

It assigns code words of different lengths to different characters.

Prefix free code or Prefix code

 In a prefix code no codeword is a prefix of a codeword of another characters.


 To construct a tree that would assign shorter bit strings to high frequency
characters and longer ones to low frequency characters can be done by greedy
algorithm invented by David Huffman.
Huffman’s algorithm

Step 1

Initialize n one node trees and label them with the characters of the alphabet.
Record the frequency of each character in its tree’s root to indicate the tree’s weight.
The weight of a tree will be equal to the sum of the frequencies in the tree’s leaves.
Step 2

Repeat the following operation until a single tree is obtained.


Find two trees with the smaller weight.
Make them, the left and right sub tree of a new tree and record the sum of their weights in
the root of the new tree as its weight.
Example
Consider the five character alphabet {A,B,C,D,-} with the following occurrence probabilities.

Character A B C D -

Probability 0.35 0.1 0.2 0.2 0.15

The huffman tree construction for the input is

96
CS3401 ALGORITHMS UNIT 3 MEC

The Huffman coding tree is

97
CS3401 ALGORITHMS UNIT 3 MEC

Hence the resulting codeword for the characters are

Character A B C D -

Probability 0.35 0.1 0.2 0.2 0.15

Codeword 11 100 00 01 101

Bits 2 3 2 2 3

Hence the string DAD is encoded as

D A D DAD

01 11 01 011101

And BAD_AD is encoded as

B A D _ A D BAD_AD

100 11 01 101 11 01 10011011011101

With the occurrence probabilities given and the codeword lengths obtained, the expected
number of bits per character in this code is calculated as,Sum of the multiplications of
probability of characters and number of bits in the code word.

(i.e) =0.35*2+0.1*3+0.2*2+0.2*2+0.15*3

= 0.7+0.3+0.4+0.4+0.45

=2.25

Therefore the expected number of bit per character is 2.25

In fixed length encoding, minimum three bits are used per characters.

Compression Ratio

Huffman’s code achieves the compression ratio, which is a standard measure of


compression algorithms effectiveness of

(3-2.25)/3*100=0.75/3*100

= 0.25*100

=25%

So, Huffman encoding of a text will use 25%less memory than its fixed length encoding.

Advantage of Huffman’s encoding

98
CS3401 ALGORITHMS UNIT 3 MEC

1) Huffman’s encoding is one of the most important file compression methods.


2) It is simple
3) It is versatility
4) It provides optimal and minimum length encoding
Simple Version

1. The simple version of Huffman compression calls for a preliminary scanning


of a given text to count the frequencies of character occurrences in it.
2. The frequencies are used to construct a Huffman coding tree and to encode
the text.
Drawback of simplest version

 The information about the coding tree has to be included in to the


encoded text to make the decoding possible.

Dynamic Huffman encoding

Dynamic Huffman encoding is used to overcome the drawback of simplest version.



In dynamic Huffman encoding, the coding tree is updated each time a new character

is read from the source text.
Weighted path length n

The weighted path length is defined as the sum ∑ liwi

I=1
Where, li is the length of the simple path from the root to the ithleaf.
wi is the length of the frequency.
 In coding application,
o li is the length of the codeword.
o wi is the length of the frequency.
Huffman algorithm is used to construct a binary tree with a minimum weighted path
length.
Example:

Consider the game of guessing a chosen object from n possibilities.

When n=4, the decision tree is

99
CS3401 ALGORITHMS UNIT 3 MEC

n>2
No Yes

n>1 n>3

No Yes No Yes

n=1 n=2 n=3 n=4

The another decision tree is,


n=4
No Yes

n=3 n=4

No Yes

n=2 n=3

No Yes

n=1 n=2

The length of simple path from=number of questions needed to get to

The chosen the root to a leaf in a decision tree number represented by the leaf.

If number I is chosen with probability pi , the the sum is ∑ li Pi

I=1
Where, li is the length of the path from the root Pi is probability.
The sum indicates the average number of question needed to guess the chosen number.
Application of Huffman trees:

 Huffman encoding is used in file compression algorithm


 Huffman’s code is used in transmission of data in an encoded form
 This encoding is used in game playing method in which decision trees need to be
formed
so, the merging cost = 5+10+16+23+39=93

100
CS3401 ALGORITHMS UNIT 3 MEC

16. Demonstrate divide and conquer approach by Performing quick sort on the following values. 44,
33, 11, 55, 77, 90, 40, 60, 99, 22, 88 Apr/may 2024

Quicksort Using Divide and Conquer Approach

QuickSort is a sorting algorithm that follows the divide and conquer strategy. It works as
follows:

1. Divide: Select a pivot element and partition the array such that elements smaller than the
pivot go to the left, and elements larger go to the right.
2. Conquer: Recursively apply QuickSort to the left and right subarrays.
3. Combine: The sorted left and right subarrays are combined (implicitly by recursion).

Performing QuickSort on Given Values

Given List:

44, 33, 11, 55, 77, 90, 40, 60, 99, 22, 88

Step-by-Step Execution

Let's pick the last element as the pivot for partitioning.

Step 1: First Partition (Pivot = 88)

 Elements smaller than 88: 44, 33, 11, 55, 77, 40, 60, 22
 Pivot: 88
 Elements greater than 88: 90, 99
 New order: [44, 33, 11, 55, 77, 40, 60, 22] 88 [90, 99]

Step 2: Sorting Left Subarray [44, 33, 11, 55, 77, 40, 60, 22] (Pivot = 22)

 Elements smaller than 22: None


 Pivot: 22
 Elements greater than 22: 44, 33, 11, 55, 77, 40, 60
 New order: [22] [44, 33, 11, 55, 77, 40, 60]

Step 3: Sorting [44, 33, 11, 55, 77, 40, 60] (Pivot = 60)

 Elements smaller than 60: 44, 33, 11, 55, 40


 Pivot: 60
 Elements greater than 60: 77
 New order: [44, 33, 11, 55, 40] 60 [77]

Step 4: Sorting [44, 33, 11, 55, 40] (Pivot = 40)

101
CS3401 ALGORITHMS UNIT 3 MEC

 Elements smaller than 40: 33, 11


 Pivot: 40
 Elements greater than 40: 44, 55
 New order: [33, 11] 40 [44, 55]

Step 5: Sorting [33, 11] (Pivot = 11)

 Elements smaller than 11: None


 Pivot: 11
 Elements greater than 11: 33
 New order: [11] 33

At this point, everything is sorted, and we combine:

Final Sorted Array:

[11, 22, 33, 40, 44, 55, 60, 77, 88, 90, 99]

[Link] merge sort algorithm to sort the given set of numbers


(40,25,69,65,31,53,86,24,55,57,19,21,16) and compute the worst case, average case and best case
time complexity of the algorithm. Nov/Dec 2024
Merge Sort Algorithm:

1. Divide: Recursively divide the array into two halves until each sub-array contains a single
element.
2. Conquer: Merge the divided sub-arrays in a sorted manner.
3. Combine: Combine all sub-arrays into one sorted array.

Given Set of Numbers:

40, 25, 69, 65, 31, 53, 86, 24, 55, 57, 19, 21, 16

We'll start by sorting this set using the Merge Sort algorithm. I'll break down the steps for you.

Step-by-Step Execution of Merge Sort:

1. Divide the array into two halves:

less
Copy
Left Half: [40, 25, 69, 65, 31, 53, 86]
Right Half: [24, 55, 57, 19, 21, 16]

2. Recursive division of the left half ([40, 25, 69, 65, 31, 53, 86]):

less
Copy

102
CS3401 ALGORITHMS UNIT 3 MEC

Left: [40, 25, 69]


Right: [65, 31, 53, 86]

Further divide:

less
Copy
Left: [40, 25] Right: [69]
Left: [65, 31] Right: [53, 86]

Continue until each part is of size 1.

3. Recursive division of the right half ([24, 55, 57, 19, 21, 16]):

less
Copy
Left: [24, 55] Right: [57, 19]
Left: [21, 16]

Continue until each part is of size 1.

4. Merge the sub-arrays in sorted order: After recursively merging the arrays, you'll get
the sorted array.

The sorted array using merge sort will be:

[16, 19, 21, 24, 25, 31, 40, 53, 55, 57, 65, 69, 86]

Time Complexity Analysis:

1. Worst-case Time Complexity:


o The worst-case time complexity of Merge Sort is O(n log n). This occurs when we need
to split the array at each level and merge each of the halves back together.
o This is because in each level of the recursion, we divide the array in half, and merging
each pair of sub-arrays takes linear time.
2. Average-case Time Complexity:
o The average case is also O(n log n) because the algorithm divides the array and merges
it in a way that is not dependent on the input data.
3. Best-case Time Complexity:
o The best-case time complexity is O(n log n). Merge Sort's performance is not
significantly affected by the initial order of the elements. Even if the array is already
sorted, it still has to divide and merge recursively.

Summary of Time Complexities:

 Worst-case Time Complexity: O(n log n)


 Average-case Time Complexity: O(n log n)
 Best-case Time Complexity: O(n log n)

103
CS3401 ALGORITHMS UNIT 3 MEC

This consistency in time complexity makes Merge Sort an efficient sorting algorithm, especially
for large data sets.

18. Produce Huffman tree for the following data and encode the data abbcddeef. Apr/May
2024 , Nov/Dec2024

Character Frequency
a 5
b 9
c 12
d 13
e 16
f 45
OR
A character-coding problem. A data file of 100,000 characters contains only the characters
a-f, with the frequencies indicated as below

a b c d ef
Frequency (in thousands) 45 13 12 16 9 5

104
CS3401 ALGORITHMS UNIT 3 MEC

Solution:
Step 1. Build a min heap that contain 6 nodes where each node represents root of a tree with
single node.
Step2 . Extract two minimum frequency nodes from min heap. add a new internal node with
frequency 5+9 =14

Now minheap contains 5 nodes where 4 nodes are roots of trees with single element each , and
one heap is root of tree with 3 elements
character Frequency
c 12
d 13
Internal Node 14
e 16
f 45
Step 3: Extract two minimum frequency nodes from heap .Add a new internal node with
frequency 12+13 =25

Now minheap contains 4 nodes where 2 nodes are roots of trees with single element each,, and
two heap nodes are root of tree with more than one nodes
character Frequency
Internal Node 14
e 16
Internal Node 25
f 45
Step 4. extract two minimum frequency nodes. add a new internal node with frequency 14+16
=30

105
CS3401 ALGORITHMS UNIT 3 MEC

Now min heap contains 3 nodes


character Frequency
Internal Node 25
Internal Node 30
f 45
Step 5: extract two minimum frequency nodes add a new internal node with frequency 25+30
=30

Now minheap contains 2 nodes


character Frequency
f 45
Internal Node 55
Step 6: extract two minimum frequency nodes add a new internal node with frequency
45+55=100

Now minheap contains only one nodes

character Frequency
Internal Node 100
Since the heap contains only one node, the algorithm stops here.
Steps to print codes from Huffman tree:
Travers the tree formed starting from the root. maintain an auxiliary arry. While moving to the
left child write 0 to the array. while moving 106
to the child, write 1 to the array.
CS3401 ALGORITHMS UNIT 3 MEC

19. Consider the given set of numbers (65,70,75,80,60,55,40,45) Apply quick sort by using

(i)First element as Pivot element

(ii)Middle element as Pivot element (Nov/Dec 2024)

(i) First Element as Pivot Element

Step-by-Step Execution:

1. Initial array:

[65, 70, 75, 80, 60, 55, 40, 45]

o Pivot = 65 (first element).


2. Partitioning step:
o We move elements smaller than 65 to the left and elements larger than 65 to the
right.

After partitioning:

[60, 55, 40, 45, 65, 70, 75, 80]

o The pivot 65 is placed at the correct position (index 4).


o Now we recursively apply Quick Sort to the two sub-arrays:
 Left sub-array: [60, 55, 40, 45]
 Right sub-array: [70, 75, 80]
3. Left Sub-array ([60, 55, 40, 45]):
o Pivot = 60 (first element).

After partitioning:

[55, 40, 45, 60]

o Pivot 60 is placed at index 3.


o Recursively apply Quick Sort to:
 Left sub-array: [55, 40, 45]
 Right sub-array: [] (empty, no sorting needed)
4. Left Sub-array ([55, 40, 45]):
o Pivot = 55 (first element).

After partitioning:

[40, 45, 55]

107
CS3401 ALGORITHMS UNIT 3 MEC

o Pivot 55 is placed at index 1.


o Recursively apply Quick Sort to:
 Left sub-array: [40]
 Right sub-array: [45]

Both sub-arrays are already sorted.

5. Right Sub-array ([70, 75, 80]):


o Pivot = 70 (first element).

After partitioning:

[70, 75, 80]

o Pivot 70 is placed at index 5.


o Recursively apply Quick Sort to:
 Left sub-array: [] (empty, no sorting needed)
 Right sub-array: [75, 80]
6. Right Sub-array ([75, 80]):
o Pivot = 75 (first element).

After partitioning:

[75, 80]

o Pivot 75 is placed at index 6.


o Recursively apply Quick Sort to:
 Left sub-array: [] (empty, no sorting needed)
 Right sub-array: [80]

Both sub-arrays are already sorted.

Final Sorted Array (First Element as Pivot):

[40, 45, 55, 60, 65, 70, 75, 80]

(ii) Middle Element as Pivot Element

Step-by-Step Execution:

1. Initial array:

[65, 70, 75, 80, 60, 55, 40, 45]

o Pivot = 75 (middle element, index 3).


2. Partitioning step:

108
CS3401 ALGORITHMS UNIT 3 MEC

o We move elements smaller than 75 to the left and elements larger than 75 to the
right.

After partitioning:

[65, 70, 60, 55, 40, 45, 75, 80]

o Pivot 75 is placed at index 6.


o Now we recursively apply Quick Sort to the two sub-arrays:
 Left sub-array: [65, 70, 60, 55, 40, 45]
 Right sub-array: [80]
3. Left Sub-array ([65, 70, 60, 55, 40, 45]):
o Pivot = 60 (middle element, index 2).

After partitioning:

[55, 40, 45, 60, 70, 65]

o Pivot 60 is placed at index 3.


o Recursively apply Quick Sort to:
 Left sub-array: [55, 40, 45]
 Right sub-array: [70, 65]
4. Left Sub-array ([55, 40, 45]):
o Pivot = 40 (middle element, index 1).

After partitioning:

[40, 45, 55]

o Pivot 40 is placed at index 0.


o Recursively apply Quick Sort to:
 Left sub-array: [] (empty, no sorting needed)
 Right sub-array: [45, 55]
5. Right Sub-array ([45, 55]):
o Pivot = 45 (middle element).

After partitioning:

[45, 55]

o Pivot 45 is placed at index 1.


o Recursively apply Quick Sort to:
 Left sub-array: [] (empty, no sorting needed)
 Right sub-array: [55]
6. Right Sub-array ([70, 65]):
o Pivot = 65 (middle element).

109
CS3401 ALGORITHMS UNIT 3 MEC

After partitioning:

[65, 70]

o Pivot 65 is placed at index 5.


o Recursively apply Quick Sort to:
 Left sub-array: [] (empty, no sorting needed)
 Right sub-array: [70]
7. Right Sub-array ([80]):
o Only one element, already sorted.

Final Sorted Array (Middle Element as Pivot):

[40, 45, 55, 60, 65, 70, 75, 80]

IMPORTANT QUESTIONS

Part A

1. What is the time complexity of Binary search? June 2011 & 12

2. Give the recurrence equation for the worst case behavior of merge sort? Dec 2010

3. What do you meant by Divide and conquer strategy? May 2013

4. Give the time efficiency and drawback of merge sort algorithm? Dec 2005

5. What is the difference between quick sort and merge sort? May 2013

6. Give the control abstraction for divide and conquer. Dec 2012

7. What is called substitution method? Jun 2010

8. What is called optimal solution? Jun 2010

9. What do you mean by divide and conquer strategy? Jun 2013

10. State the principle of substitution method? Jun 2014

11. Define feasible and optimal solution? Jun 2014

12. . Define Brute Force method

13. Is merge sort stable sorting algorithm?

14. What is the difference between quick sort and merge sort?

PART-B

1. Construct the optimal binary search tree for the following 5 keys with probabilities as

110
CS3401 ALGORITHMS UNIT 3 MEC

indicated.

i 0 1 2 3 4 5

Pi 0.15 0.10 0.05 0.10 0.20

pj 0.05 0.10 0.05 0.05 0.05 0.10

2. Write the Huffman code algorithm and derive its time complexity
3. Generate the Huffman code for the following data comprising of alphabet and their
frequency.(6)
a:1, b :1 ,c :2, d :3, e :5, f: 8,g : 13,h : 21

4. What is divide and conquer strategy and explain the binary search with suitable example
problem.

5. Trace the steps of merge sort algorithm for the elements 122, 25, 70, 175, 89, 90, 95, 102, 123
and also compute its time complexity. Dec 2012

6. Explain merge sort problem using divide and conquer technique example. Apr2010

7. Write a pseudo code using divide and conquer technique for finding the position of the

largest element in the array of N numbers. Jun 2014

8. Sort the following set of elements using merge sort :12,2,8,71,4,23, 6, 89, 56 Jun14

9. Distinguish between quick sort and merge sort and arrange the following numbers in

increasing order using merge sort (18, 29,68, 32, 43, 37, 87, 24, 47, 50). Jun13

[Link] the algorithm for Quick Sort and write its time complexity with example list are 5, 3, 1, 9,
8, 2, 4, 7.

April/May 2024
PART A

1. what kind of problem can be solved using divide and conquer method Apr/May 2024 [Link] 22, [Link] 76

2. List the elements of Greedy strategy. Apr/May 2024 [Link] 22, [Link] 75

PART B

1. Demonstrate divide and conquer approach by Performing quick sort on the following values. 44, 33, 11, 55,
77, 90, 40, 60, 99, 22, 88 Apr/may 2024 [Link] 101 [Link] 17

111
CS3401 ALGORITHMS UNIT 3 MEC

2. Using Dynamic programming solve Matrix chain multiplication problem with an


example.(Apr/may 2023, 2024) [Link] 53 [Link] 9

3. Explain in detail about Activity-selection problem with an example. Apr/May 2024 Pg .no
88 [Link] 14

4. A character-coding problem. A data file of 100,000 characters contains only the


characters a-f, with the frequencies indicated as below

a b c d ef
Frequency (in thousands) 45 13 12 16 9 5
Show the steps in constructing the final Huffman tree representing the optimal prefix
code. Apr/May 2024 Pg .no 104 [Link] 19

Nov/Decc 2024

PART- A
[Link] Divide and Conquer approach.([Link] 1)
[Link] the elements of Greedy strategy.([Link] 75)

PART- B

[Link] merge sort algorithm to sort the given set of


numbers(40,25,69,65,31,53,86,24,55,57,19,21,16) and compute the worst case,average case and
best case time complexity of the algorithm. .([Link] 16)
[Link] Huffman tree for the following data and encode the data abbcddeef. .([Link] 17)
Character Frequency
a 5
b 9
c 12
d 13
e 16
f 45

PART- C
1. Consider the given set of numbers (65,70,75,80,60,55,40,45) Apply quick sort by using

(i)First element as Pivot element


(ii)Middle element as Pivot element.([Link] 19)

112
CS3401 ALGORITHMS UNIT 4 MEC

UNIT IV
STATE SPACE SEARCH ALGORITHMS

Backtracking: n-Queens problem - Hamiltonian Circuit Problem - Subset Sum Problem –


Graph colouring problem Branch and Bound: Solving 15-Puzzle problem - Assignment
problem - Knapsack Problem - Travelling Salesman Problem

PART-A

1. What is knapsack?
The knapsack problem, another well-known NP-hard problem.
The Knapsack problem is, given n items of known weights w1, . . . , wn and values v1, . . . , vn
and a knapsack of weight capacity W, find the most valuable subset of the items that fits
into the knapsack.

2. What are the factors that influence the efficiency of the backtracking algorithm?

The efficiency of the backtracking algorithm depends on the following four


factors. They are:
 The time needed to generate the next xk
 The number of xk satisfying the explicit constraints.
 The time for the bounding functions Bk
 The number of xk satisfying the Bk.

3. State 8 – Queens problem.


The problem is to place eight queens on a 8 x 8 chessboard so that no two queen
“attack” that is, so that no two of them are on the same row, column or on the
diagonal.

4. State Sum of Subsets problem.


Given n distinct positive numbers usually called as weights, the problem calls for
finding all the combinations of these numbers whose sums are m.

5. State m – Colorability decision problem.


Let G be a graph and m be a given positive integer. We want to discover whether the
nodes of G can be colored in such a way that no two adjacent nodes have the same color
yet only m colors are used.

6. Define bounding function.


Backtracking is to build up the solution vector one component at a time and to use
modified criterion function Pi(x1,…….xi) (sometimes called bounding function) to test
whether the vector being formed has any chance of success. Desired solution expressed as
an n-tuple (x1, x2,…….xn) where xi are chosen from some set Si.
 If |Si|= mi m=m1, m2,………mn candidates are possible
 Yielding the same answer with far fewer than m trials
Advantage: if it is realized that the partial vector (x1,…….xi) can in no way lead to an
optimum solution, then mi+1,… mn, possible test vectors can be ignored entirely.”

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 1


CS3401 ALGORITHMS UNIT 4 MEC

7. Give the categories of the problem in backtracking.


 Decision’s problem: - Whether there is any feasible solution.
 Optimization problem: - Whether there exists any best solution.
 Enumeration problem: - Finds all possible feasible solution.

8. List down the examples of backtracking. Or what are the applications of backtracking.
They are,
1. The 8 - Queens problem
2. Hamiltonian cycles
3. Sum of Subsets
4. Graph Coloring
5. Knapsack Problem.

9. Explain promising and non promising node. Nov/Dec 2017


Promising Node
A node in a state space tree is said to be promising if it corresponds to a partially
constructed solution that may still lead to a complete solution.
Non – Promising node
A node in a state space tree is said to be non-promising if it corresponds to a partially
constructed solution that would not be able to lead to a complete solution further.

[Link] are the two types of constraints used in Backtracking?


They are,
1. Explicit constraints
2. Implicit constraints

[Link] implicit constraint.


 Implicit constraints are rules that determine which of the tuples in the solution
Space of I that satisfy the criterion function.
 Implicit constraints describe the way in which the xi is must relate to each other.

12. Define explicit constraint.


 Explicit constraints are rules that restrict each xi to take on values only from a
given set.
 Explicit constraints depend on the particular instance I of problem being solved
 All tuples that satisfy the explicit constraints define a possible solution space for I
Examples of explicit constraints:
xi >= 0, or Si = {all nonnegative real numbers}
xi = {0, 1} or Si = { 0, 1 }
li ≤ xi ≤ ui or Si = {a : li ≤ a ≤ ui }

13. How can you represent the solution for 2 queen’s problem?
There is no solution for 2 Queen’s problem since however the queens are arranged
both queens would be in same diagonal or column.

14. How can you represent the solution for 8 queen’s problem?
All solutions represented as 8-tuples (x1, x2,…, x8) where xi is the column on which
queen “i” is placed.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 2


CS3401 ALGORITHMS UNIT 4 MEC

Constraints are,
Explicit constraints
Si = {1, 2, 3, 4, 5, 6, 7, 8}
Implicit constraints
 No two xi‘s can be the same column or row.
 No two queens can be on the same diagonal.

15. Define n-queens problem Or Give the formal definition of n- queens


Problem. AU : May -08
Place n queens on an nXn chessboard so that no queen attacks another queen
which means no two queen’s are in same row/column or same diagonal solution space
consists of all n! Permutations of the n-tuple (1, 2 . . . n)

16. Define sum of subsets problem? OR Describe the sum of subsets problem.
May-13,Dec-2018
In the Sum-of-Subsets problem, there are n positive integers (weights) wi and a positive
integer W.
The goal is to find all subsets of the integers that sum to W.
For example, n = 4, w = (11, 13, 24, 7), and m = 31, the desired subsets are
(11, 13, 7) and (24, 7)
The solution vectors can also be represented by the indices of the numbers as
(1, 2, 4) and (3, 4).
All solutions are k-tuples, 1 ≤ k ≤ n

17. Define problem state.


Problem State: Each node in a tree defines a problem state.
Problem state is each node in the depth-first search tree.

18. Define state space of the problem.


State space: All paths from the root to other nodes define the state space of the problem.
State space is the set of all paths from root node to other nodes.

19. Define solution states and answer state.


Solution state:
 Solution states are the problem states s for which the path from the root node to ‘s’
defines a tuple in the solution space.
 In variable tuple size formulation tree, all nodes are solution states.
 In fixed tuple size formulation tree, only the leaf nodes are solution states.
 Partitioned into disjoint sub-solution spaces at each internal node.
Answer state:
 Answer states are that solution states s for which the path from root node to s
defines a tuple that is a member of the set of solutions.
 These states satisfy implicit constraints

20. Define Optimal Binary Search. Nov/Dec 2024

 Divide and Conquer: The algorithm repeatedly divides the search interval in half by
comparing the target value to the middle element.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 3


CS3401 ALGORITHMS UNIT 4 MEC

 Logarithmic Performance: In the worst-case scenario, binary search requires at most


O(log⁡n)O(\log n)O(logn) comparisons, which is provably optimal for comparison-
based search in sorted data.
 Optimality: Under the assumption that each comparison has the same cost and the list is
sorted, no algorithm can guarantee a lower worst-case number of comparisons.

21. Define static tree.


Static trees are ones for which tree organizations are independent of the problem instance
being solved
 Fixed tuple size formulation
 Tree organization is independent of the problem instance being solved

22. Define dynamic tree.


Dynamic trees are ones for which organization is dependent on problem instance

23. Define live node.


Live node is a generated node for which all of the children have not been generated yet
E-node is a live node whose children are currently being generated or explored.

24. Define dead node.


Dead node is a generated node that is not to be expanded any further
All the children of a dead node are already generated
Live nodes are killed using a bounding function to make them dead nodes

25. Define m-colorability decision problem.


Let G be a graph and m be a given positive integer. The nodes of G can be colored in such a
way that no two adjacent nodes have the same color yet only m colors are used. This is
termed the m-colorability decision problem. If d is the degree of the given graph, then it
can be colored with d+ 1 color.

26. Define Hamiltonian circuit problem. Or State Hamiltonian circuit problem


May 2019,Dec 2019, Apr/May 2024
Suppose we are given a graph G = (V, E) that is undirected.
We say that a path v1, v2, …. vn is a Hamiltonian path if every vertex of the graph occurs
exactly once in this path. Moreover, if the last vertex of the path duplicates the first one
without ever repeating any other vertex in between, then the path is called a Hamiltonian
circuit

27. State the equation which is used to represent 2 queens are in same diagonal.
 Let the diagonals be (i, j) and (k, l)
 The two queens lie on the same diagonal if and only if | j - l |=| i – k |

28. What is minimum spanning tree? (Nov/Dec 2010)


A minimum spanning tree of a weighted connected graph is its spanning tree of the
smallest weight, where the weight of a tree is defined as the sum of the weights of all its
edges.

29. Define Hamiltonian cycle OR Circuit and give an example. (May2010,15)

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 4


CS3401 ALGORITHMS UNIT 4 MEC

Let G = (V,E) be a connected graph with n vertices. A Hamiltonian cycle is a round


trip path along n edges or G that visits every vertex once and returns to its starting
position.
Example: 1 2 8 7 6 5 4 3 1

30. Draw a graph with a cycle but no Hamiltonian cycle. (April/May 2011)
A Hamiltonian cycle (or Hamiltonian circuit) is a Hamiltonian Path such that there is an
edge (in the graph) from the last vertex to the first vertex of the Hamiltonian Path.

A B

C D

31. Explain briefly branch and bound technique for solving problems. (April/May 2008)
It is an algorithm that enhances the idea of generating a state space tree with the idea of
estimating the best value obtainable from a current node of the decision tree.
It refers to all state space search methods in which all children of an E-node are generated
before any other live node can become the E-node.

32. Define the term live node, E-node and dead node. AU : May -10
Live node: It is a node that has been generated but whose children have not been
generated.
E-node: It is a live node whose children are currently being explored. In other words an E-
node is node currently being explored.
Dead node: It is a generated node that is not to expanded or explored any further. All
children of a death node have already been explored.

33. Differentiate explicit and implicit constraints


Implicit constraint.
 Implicit constraints are rules that determine which of the tuples in the solution
 Space of I that satisfy the criterion function.
 Implicit constraints describe the way in which the xi is must relate to each other.
Explicit constraint.
 Explicit constraints are rules that restrict each xi to take on values only from a
given set.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 5


CS3401 ALGORITHMS UNIT 4 MEC

 Explicit constraints depend on the particular instance I of problem being solved


 All tuples that satisfy the explicit constraints define a possible solution space for I
Examples of explicit constraints:
xi >= 0, or Si = {all nonnegative real numbers}
xi = {0, 1} or Si = { 0, 1 }
li ≤ xi ≤ ui or Si = {a : li ≤ a ≤ ui }

34. State the principle of backtracking OR Explain the idea behind the backtracking.
Apr/May 2023

o Backtracking is a more intelligent variation of this approach.


o The principal idea is to construct solutions one component at a time and evaluate
such partially constructed candidates as follows.
o If a partially constructed solution can be developed further without violating the
problem’s constraints, it is done by taking the first remaining legitimate option for
the next component. If there is no legitimate option for the next component, no
alternatives for any remaining component need to be considered. In this case, the
algorithm backtracks to replace the last component of the partially constructed
solution with its next option.

35.. List the Applications of Graph Coloring.


Some applications of graph coloring include −
 Register Allocation
 Map Coloring
 Bipartite Graph Checking
 Mobile Radio Frequency Assignment
 Making time table, etc.

36. How to find valid colorings in backtracking ?


We will use the following strategy to find all valid colorings of a graph
G = (V, E):
1. Order nodes arbitrarily.

2. Assign the first node a color.


3. Given a partial assignment of colors (c1, c2, ..., ci−1) to the first−i-1
nodes, try to find a color for the i-th node in the graph.
4. If there is no possible color for the i-th node given the previous
choices, backtrack to a previous solution.

37. Define Branch-and-Bound.


 The term branch-and-bound refers to all state space search
methods in which all children of the E-node are generated
before any other live node can become the E-node. We have
already seen two graph search strategies, BFS and D-search, in
which the exploration of a new node cannot begin until the
node currently being explored is fully explored.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 6


CS3401 ALGORITHMS UNIT 4 MEC

 Both of these generalize to branch-and- bound strategies. In


branch-and- bound terminology, a BFS-like state space search
will be called FIFO (First In First Out) search as the list of live
nodes is a first-in-first-out list (or queue).
 A D-search-like state space search will be called LIFO (Last In
First Out) search as the list of live nodes is a last-in-first-out
list (or stack).
As in the case of backtracking, bounding functions are used to help
avoid the generation of subtrees that do not contain an answer node.

39. Difference between Branch and bound and Backtracking.


 The techniques differ in the nature of problems they can apply to. Branch and
bound is applicable only to optimization problems. Backtracking is applied to non
optimization problems.
 The other difference between backtracking and branch and bound lies in the order
in which nodes of the state space tree are generated.
In backtracking technique, the state space tree is developed using depth first which is
similar to [Link] branch and bound the nodes of a state space tree is generated using best
first rule.

40. What is meant by state-space tree?

A space state tree is a tree that represents all of the possible states of the problem, from
the root as an initial state to the leaf as a terminal state.

41. What is branch and bound method briefly explain?


The branch and bound approach is based on the principle that the total set of feasible
solutions can be partitioned into smaller subsets of solutions. These smaller subsets can
then be evaluated systematically until the best solution is found.

[Link] are the applications of branch and bound?


Application of the Branch and Bound Technique to Some
 Flow-Shop Scheduling Problems Applied computing
 Enterprise computing
 Computing methodologies
 Artificial intelligence
 Search methodologies
 Heuristic function construction
 Theory of computation
 Design and analysis of algorithms

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 7


CS3401 ALGORITHMS UNIT 4 MEC

 Approximation algorithms analysis

43. What is branch and bound Travelling salesman problem?(Apr/may 2023, 2024)

Given a set of cities and distance between every pair of cities, the problem is to find the
shortest possible tour that visits every city exactly once and returns to the starting point.

44. Give an Example of Backtracking Algorithm.


 Now, this tutorial is going to use a straightforward example to explain the theory
behind the backtracking process. You need to arrange the three letters x, y, and z so
that z cannot be next to x.
 According to the backtracking, you will first construct a state-space tree. Look for
all possible solutions and compare them to the given constraint. You must only
keep solutions that meet the following constraint:

 The following are possible solutions to the problems: (x,y,z), (x,z,y), (y,x,z), (y,z,x),
(z,x,y) (z,y,x).
 Nonetheless, valid solutions to this problem are those that satisfy the constraint
that keeps only (x,y,z) and (z,y,x) in the final solution set.

45. When to Use a Backtracking Algorithm?


There are the following scenarios in which you can use the backtracking:
 It is used to solve a variety of problems. You can use it, for example, to find a
feasible solution to a decision problem.
 Backtracking algorithms were also discovered to be very effective for solving
optimization problems.
 In some cases, it is used to find all feasible solutions to the enumeration problem.
 Backtracking, on the other hand, is not regarded as an optimal problem-solving
technique. It is useful when the solution to a problem does not have a time limit.
46. List the Types of Backtracking Algorithm .

 Backtracking algorithms are classified into two types:


 Algorithm for recursive backtracking
 Non-recursive backtracking algorithm

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 8


CS3401 ALGORITHMS UNIT 4 MEC

47. What is Hamiltonian cycle in backtracking?


 A Hamiltonian cycle (or Hamiltonian circuit) is a Hamiltonian Path such that there
is an edge (in the graph) from the last vertex to the first vertex of the Hamiltonian
Path. Determine whether a given graph contains Hamiltonian Cycle or not. If it
contains, then prints the path.

48. What is the time complexity of Hamiltonian backtracking?


 Time complexity of the above algorithm is O(2nn2).
 Depth first search and backtracking can also help to check whether a Hamiltonian
path exists in a graph or not. Simply apply depth first search starting from every
vertex v and do labeling of all the vertices.

49. How do you solve subset sum problems using backtracking?


Steps:
a. Start with an empty set.
b. Add the next element from the list to the set.
c. If the subset is having sum M, then stop with that subset as solution.
d. If the subset is not feasible or if we have reached the end of the set, then
backtrack through the subset until we find the most suitable value.

50. What is 15-puzzle problem using branch and bound?


 15 Puzzle Problem by Branch and Bound (Least Cost Search)
 The problem consist of 15 numbered (0-15) tiles on a square box with 16 tiles(one
tile is blank or empty). The objective of this problem is to change the arrangement
of initial node to goal node by using series of legal moves.

51. How do you solve a knapsack problem using branch and bound?
LC branch and bound solution for knapsack problem is derived as follows:
a. Derive state space tree.
b. Compute lower bound. ...
c. If lower bound is greater than upper bound than kill that node.
d. Else select node with minimum lower bound as E-node.
e. Repeat step 3 and 4 until all nodes are examined.

52. What is travelling sales man problem?


The traveling salesman problem (TSP) is an algorithmic problem tasked with finding the
shortest route between a set of points and locations that must be visited. In the problem
statement, the points are the cities a salesperson might visit. The salesman ‘s goal is to
keep both the travel costs and the distance traveled as low as possible.

[Link] the time complexity for solving n-Queens problem. (Nov/Dec 2024)

The time complexity for solving the n-Queens problem depends on the algorithm used. One of
the most common approaches is using backtracking, which explores possible configurations of
queens on the board and backtracks whenever an invalid configuration is encountered.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 9


CS3401 ALGORITHMS UNIT 4 MEC

Time Complexity of the Backtracking Approach:

 Worst-case time complexity: O(n!)

PART-B

1. What is Backtracking problem? Or With an example explain general method solving problem
using backtracking Or Explain elaborately recursive backtracking algorithm Au: Dec-11,
May-13Or Explain the general method of backtracking Or How do you estimate the efficiency
of backtracking? Au: dec-13 Apr/May 2024

Introduction
 Backtracking and Branch and bound are two algorithm design techniques for solving problems
in which the number of choices grows atleast exponentially with their instances [Link]
techniques construct a solution one component at a time, trying to terminate the process as soon
as one can ascentain that no solution can be obtained as a result of the choices already made.
 This approach makes it possible to solve many large instances of NP hard problems in an
acceptable amount of time.
 The techniques branch and bound and backtracking are base on the construction of a state space
tree.
 A state space tree is a rooted tree whose nodes represent partially constructed solutions to the
problems.
 Both techniques terminate a node as soon as it can be guaranteed that no solution to the problem
can be obtained by considering choices that correspond to the node’s descendants.
Difference between Branch and bound and Backtracking
 The techniques differ in the nature of problems they can apply to. Branch and bound is
applicable only to optimization problems. Backtracking is applied to non optimization
problems.
 The other difference between backtracking and branch and bound lies in the order in which
nodes of the state space tree are generated.
 In backtracking technique, the state space tree is developed using depth first which is similar to
DFS.
In branch and bound the nodes of a state space tree is generated using best first rule.

Backtracking
 Backtracking is a more intelligent variation of this approach.
 The principal idea is to construct solutions one component at a time and evaluate such partially
constructed candidates as follows.
 If a partially constructed solution can be developed further without violating the problem’s
constraints, it is done by taking the first remaining legitimate option for the next component. If
there is no legitimate option for the next component, no alternatives for any remaining
component need to be considered. In this case, the algorithm backtracks to replace the last
component of the partially constructed solution with its next option.
 It is convenient to implement this kind of processing by constructing a tree of choices being
made, called the state-space tree.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 10


CS3401 ALGORITHMS UNIT 4 MEC

 Its root represents an initial state before the search for a solution [Link] nodes of the
first level in the tree represent the choices made for the first component of a solution; the
nodes of the second level represent the choices for the second component, and so on.

Promising and nonpromising node


 A node in a state-space tree is said to be promising if it corresponds to a partially
constructed solution that may still lead to a complete solution; otherwise,it is called
nonpromising. Leaves represent either nonpromising dead ends or complete solutions
found by the algorithm.
 A state space tree for a backtracking algorithm is constructed in the manner of depth
first search. If the current node is promising, its child is generated by adding the first
remaining legitimate option for the next component of a solution,
 If the current node turns out to be nonpromising, the algorithm backtracks to the nodes
parent to consider the next possible option for its last component.
 If there is no such option, it backtracks one more level up the tree, and so on.
 Finally, if the algorithm reaches a complete solution to the problem, it either stops (if
just one solution is required) or continues searching for other possible solutions.
 Backtracking problems require that all the solutions satisfy a complex set of constraints.
Two types of constraints are
(i) Explicit constraint
(ii) Implicit constraint
Backtracking technique is applied to
(i) N-Queens problem
(ii) Hamiltonian circuit problem
(iii)Subset sum problem
Sub set
 The best problem to be solved using backtracking approach is the n-Queens problem. The
problem is to place n queens on an n × n chessboard so that no two queens attack each other by
being in the same row or in the same column or on the same diagonal.
For example –Consider 4 × 4 board
For n = 1, the problem has a trivial solution, and it is easy to see that there is no solution
for n = 2 and n = 3.
 So let us consider the four-queens problem and solve it by the backtracking technique. Since
each of the four queens has to be placed in its own row, all we need to do is to assign a column
for each queen on the board presented in Figure
1 2 3 4
1  queen 1
2  queen 1
3  queen 1
4  queen 1
Chessboard for 4 Queens problem
Procedure

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 11


CS3401 ALGORITHMS UNIT 4 MEC

Step 1 First start with the empty board.


1 2 3 4
1
2
3
4
Step 2 Then place queen 1 in the first possible position of its row, which is in column 1 of row 1.
1 2 3 4
1 Q
2
3
4
Step 3Then we place queen 2, after trying unsuccessfully columns 1 and 2, in the first
acceptable position for it, which is square (2, 3), the square in row 2 and column 3.

1 2 3 4
1 Q
2 Q
3
4
Step 4This proves to be a dead end because there is no acceptable position for queen 3. So, the
algorithm backtracks and puts queen 2 in the next possible position at (2, 4).
1 2 3 4
1 Q
2 Q
3
4

Step 5Now queen 3 is placed at position (3,2), which is acceptable position. Now the
chessboard is
1 2 3 4
1 Q
2 Q
3 Q
4

Step 6Then queen 3 is placed at (3, 2), which proves to be another dead end. The algorithm then
backtracks all the way to queen 1 and moves the queen 1 from (1,1) to (1, 2).
1 2 3 4
1 Q
2
3
4

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 12


CS3401 ALGORITHMS UNIT 4 MEC

Step 7Queen 2 then goes to (2, 4)


1 2 3 4
1 Q
2 Q
3
4

Step 8Now queen 3 is placed at the position (3, 1), which is acceptable position. Now the board
becomes
1 2 3 4
1 Q
2 Q
3 Q
4
Step 9Finally the queen 4 to (4, 3), which is a solution to the problem, which is e required
solution to the problem. Now the board for four queens is

1 2 3 4
1 Q
2 Q
3 Q
4 Q
 The state-space tree of this search is shown in Figure. If other solutions need to be found (how
many of them are there for the four queens problem?), the algorithm can simply resume its
operations at the leaf at which it stopped. Alternatively, we can use the board’s symmetry for
this purpose.
 Finally, it should be pointed out that a single solution to the n-queens problem for any n ≥ 4 can
be found in linear time. In fact, over the last 150 years mathematicians have discovered several
alternative formulas for non attacking positions of n queens. Such positions can also be found
by applying some general algorithm design strategies.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 13


CS3401 ALGORITHMS UNIT 4 MEC

3. Write down and explain the procedure for tackling the 8 queens problem using backtracking
Approach or Describe the Backtracking solution to solve 8-Queen problem. Apr/May 2017
8 Queens problem
The is to successfully place 8 queens on a 8 × 8 chess board such that no two queens attack each
other. Two queens are said to be in the attack state if they are.
1. Placed in the same row
2. Placed in the same column
3. Placed along the same diagonal
Initially when 8 queens have to be placed on the (8 × 8) chess board,
si = { 1,2,3,4,5,6,7,8} ie. xi should have any of these positions.
1st queen position x1 can have any of these 8 values.
2nd queen position x2 can have any of these 8 values.
:
8th queen position x8 can have any of these 8 values.
The solution space will have 8 tuples. When these 88 tuples are bound by the implicit conditions (
ie) the xi , s should be related such that they cannot have the same value since no two queens are
allowed to be placed on the same row or column , or along the same diagonal, and hence the
solution tuple can only be a permutation of si { 1,2,…8}. Hence the solution space reduces to 8!
From 88
We have test whether two queens are on the same diagonal, it must satisfy the following
conditions.
• The chessboard squares being numbered as the indices of the two-
dimensional array a[1:8,1:8]
• Every element on the same diagonal that runs from the upper left to the
lower right has the same row-column value.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 14


CS3401 ALGORITHMS UNIT 4 MEC

8-Queens problem
Explanation:
• Place(k, i) returns a Boolean value that is true if the kth queen can be placed
in column i. it tests both whether i is distinct from all previous values
x[1],…..x[k-1] and whether there is no other queen on the same diagonal.
• Its computing time is O(k-1)
All solutions to the n-queens problem:

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 15


CS3401 ALGORITHMS UNIT 4 MEC

Algorithm Place(k,i)
// This algorithm returns true if a queen can be placed in kth row and ith column. Otherwise, it
returns false.
// x[] is a global array whose first(k-1) values have been set.
// Abs(r) returns the absolute value of r.
{
for j = 1 to k-1 do
if ((x[j] = i) // Checks whether two Queens are in the same column
or (Abs(x[j] - i) = Abs(j - k))) // Checks whether they are in the same diagonal
then
return false;
return true;
}
Algorithm NQueens(k, n)
// Using backtracking, this procedure prints all
//possible placements of n queens on an n x n
// chessboard so that they are non attacking.
{
for i:=1 to n do
{
if Place(k, i) then
{
x[k]:=i;
if (k==n) then write (x[1:n]);
else NQueens(k+1, n);
}} }

(Fig 4.1) One solution to the 8-Queens problem


Solution Set:
The solution set contains the following value S = {4, 6, 8, 2, 7, 1, 3, 5}
The total number of nodes in the 8-queens state space tree is 8 as shown in Fig 4.1

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 16


CS3401 ALGORITHMS UNIT 4 MEC

4. Using backtracking enumerate how can you solve the following problem Hamiltonian
Circuit Problem Au: Dec-10 , 08,09,11,May-14, (APR/MAY 2023)

Definition
Given an undirected connected graph and two graph and two nodes x and y then find a path
from x to y visiting each node in the graph exactly once
V1, V2…… Vn and the Vi are distinct except for V1, and Vn+1, which are equal.
The next example let us consider the problem of finding a Hamiltonian circuit in the graph in
Figure 4.2.

Figure 4.2
 Without loss of generality, we can assume that if a Hamiltonian circuit exists, it starts at vertex
a. Accordingly, we make vertex a the root of the state-space tree.
 If solution exist for a Hamiltonian circuit problem, the first component of our future solution, if
it exists, is a first intermediate vertex of a Hamiltonian circuit to be constructed.
 Using the alphabet order to break the three-way tie among the vertices adjacent to a, we select
vertex b. From b, the algorithm proceeds to c, then to d, then to e, and finally to f, which proves
to be a dead end.
 So the algorithm backtracks from f to e, then to d, and then to c, which provides the first
alternative for the algorithm to pursue.
Going from c to e eventually proves useless, and the algorithm has to backtrack from e to c and
then to b as shown in FIGURE 4.3
 From there, it goes to the vertices f , e, c, and d, from which it can legitimately return to a,
yielding the Hamiltonian circuit a, b, f , e, c, d, a. Hence the solution is obtained. If we wanted
to find another Hamiltonian circuit, we could continue this process by backtracking from the
leaf of the solution found.

FIGURE 4.3 State-space tree for finding a Hamiltonian circuit. The numbers above the
nodes of the tree indicate the order in which the nodes are generated.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 17


CS3401 ALGORITHMS UNIT 4 MEC

Example 2:Definition

 Given an undirected connected graph and two graph and two nodes x and y then find a path
from x to y visiting each node in the graph exactly once
 Then the Hamiltonian cycle A-B-D-E-C-F-A. this problem can be solved using backtracking
approach. The state space tree is generated in order to find all the
Hamiltonian cycle in the graph as shown in Fig4.4 (a).
 Only distinct cycles are output of this algorithm. The Hamiltonian cycle can be identified as
follows fig 4.4 (b).
fig 4.4 (b) clearly the backtrack approach is adopted. For instance A-B –D- F- C- E; here we get
stuck. For returning to A we have to revisit atleast one vertex.

Hence we backtracked and from D node another path is chosen A- B- D- E-C-F-A which is
Hamiltonian cycle.

Fig4.4 (a) Graph G

Fig4.4 (b): finding Hamiltonian cycle


ALGORITHM AND ANALYSIS
Algorithm
Algorithm Hamiltonian (k)
// This algorithm finds all the Hamiltonian cycle of a graph.
// The graph is stored as an adjacency matrix G[1..n, 1..n]
// All cycles begin at node 1
{
repeat
{
//Generate values for x[k]

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 18


CS3401 ALGORITHMS UNIT 4 MEC

NextValue(k); //Assign a legal next value to x[k]


if(x[k] = 0) then
return;
if (k = n) then
print x[1..n];
else
Hamiltonian(k+1);
} until(false);
}
Algorithm NextValue(k)
// This algorithm generates a next vertex
// x[1..k-1] is a path of k-1 distinct vertices
// if x[k]=0, then no vertex has as yet been assigned to x[k].
// After execution, x[k] is assigned to the next highest numbered vertex which
does not already appear in x[1..k-1] and is connected by an edge to x[k-1]
// Otherwise x[k]=0. If k=n, then in addition x[k] is connected to x[1]
{
repeat
{
x[k] = (x[k] + 1) mod (n + 1); //next vertex
if(x[k]=0) then
return;
if(G[x[k-1], x[k]]  0) then
{
//is there an edge?
for j =1 to k-1 do
if(x[j] = x[k]) then
break;
//check for distinctness
if(j = k) then //if true, then the vertex is distinct
if((k < n) or ((k = n) and G[x[n], x[1]  0))
then return;
}
}until(false)
}
 The algorithm is started by first initializing the adjacency matrix G[1..n.1..n], setting
x[2..n] to zero and x[1] to 1 and then executing Hamiltonian(2).

[Link] Subset-Sum Problem and discuss the possible solution strategies using backtracking
or write an algorithm for subset sum and explain with an example.(13.m) Aprl/May 2019
The subset sum problem is used to find a subset of a given set. A = {a1, . . . , an} of n positive
integers whose sum is equal to a given positive integer d. It always convenient to sort the sets
elements in ascending order. That is,
A1 ≤ A2 ≤ ….≤An
Let us first write a general algorithm for sum of subset problem

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 19


CS3401 ALGORITHMS UNIT 4 MEC

Algorithm:
Let , S be a set of elements and d is the expected sum of subset. Then
Step 1: start with an empty set
Step 2: add to the subset, the next element from the list
Step 3: if the subset is having sum d then stop with the subset as solution.
Step 4: if the subset is not feasible or if we have reached the end of the set then
Backtrack through the subset until we find the most suitable value.
Step 5: if the subset is feasible then repeat step 2
Step 6: if we have visited all the elements without finding a suitable subset and if no
backtracking is possible then stop without solution as shown in Table 4.1

For example 1, for A = {1, 2, 5, 6, 8} and d = 9, there are two solutions: They are,

Solution :
Table 4.1 subset
Initially subset = {} Sum = 0
Now add the next
1 1 element

1, 2 3 therefore 3<9 Add next the element


1,2,5 8 therefore 8<9 Add next the element
1,2,5,6 14 Sum exceeds the given
constraint hence
backtrack
1,2,5,8 16 Sum exceeds the given
constraint hence
backtrack
1,2,6 9=9 Solution is found
1,8 9=9 Solution is found

1. {1, 2, 6} is a first subset


2. {1, 8}is a second subset

Before finding the subset of a given set, the set’s elements are sorted in increasing order. So,
we will assume that
a1≤a2 ≤ . . . ≤ an.
For subset sun problem, the state space tree is constructed as a binary tree which is
shown in the figure 4.6 below.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 20


CS3401 ALGORITHMS UNIT 4 MEC

Fig 4.6 state space tree


ALGORITHM AND ANALYSIS
Algorithm SumOfSub(s,k,r)
// This algorithm finds all subsets of w[1..n] that sum to d
// The values of x[j], 1 j< k, have already been determined.
k 1 n
// s =  w[ j] * x[ j] and r =
j 1
 w[ j ]
j k

// The w[j]’s are in non-decreasing order.


n
//It is assumed that w[1]  d and  w[i]  d
i 1
{
// Generate left child.
x[k] =1; The subset is printed
if (s + w[k] = d) then
print x[1..k] // subset found
// There is no recursive call here as w[j] > 0, 1  j  n.
else if(s + w[k] +w[k+1]  d)
then SumOfSub(s + w[k], k+1, r-w[k]);
Search the next element
//Generate right child
which can make sum ≤ d
if (( s + r – w[k]  d ) and ( s + w[k+1]]  d)) then
{
x[k] = 0;
SumOfSub( s, k+1, r–w[k] );
}
}
n
The initial call to the algorithm is SumOf Sub(0,1,  w i ).
in

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 21


CS3401 ALGORITHMS UNIT 4 MEC

Example 2:
A = {3, 5, 6, 7} and d = 15

A = {3, 5, 6, 7} and d = 15
The state-space tree can be constructed as a binary tree like that in Figure 4.7 for the
instance A = {3, 5, 6, 7} and d = 15.
The root of the tree represents the starting point, with no decisions about the given
elements made as yet.
 Its left and right children represent, respectively, inclusion and exclusion of a1 in a set
being sought. Similarly, going to the left from a node of the first level corresponds to
inclusion of a2 while going to the right corresponds to its exclusion, and so on.
Thus, a path from the root to a node on the ith level of the tree indicates which of the
first i numbers have been included in the subsets represented by that node. We record the value
of s, the sum of these numbers, in the node.
 If s is equal to d, we have a solution to the [Link] can either report this result and
stop or, if all the solutions need to be found, continue by backtracking to the node’s
parent as shown in Table 4.2
 If s is not equal to d, we can terminate the node as nonpromising if either of the
following two inequalities holds:
s + ai+1> d (the sum s is too large),

s+ < d (the sum s is too small).\


Solution :
Table 4.2 state-space

Initially subset = {} Sum = 0


Now add the next
3 3 element

3,5 8 therefore 8 <15 Add next the element


3,5,6 14 therefore 14 < 15 Add next the element
3,5,6,7 21 Sum exceeds the given
constraint hence
backtrack
3,5,7 15=15 Solution is found

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 22


CS3401 ALGORITHMS UNIT 4 MEC

Figure 4.7 Complete state-space tree of the backtracking algorithm applied to the instance A =
{3, 5, 6, 7} and d = 15 of the subset-sum problem.
The number inside a node is the sum of the elements already included in the subsets represented
by the node.

[Link] an algorithm to determine the sum of subsets for a given sum and a set of numbers .
draw the tree representation to solve the subset sum problem given the numbers set as
A = {3, 5, 6, 7 , 2} and with sum = 15 Derive all the subsets Au : Dec -10

solution
The inequality below a leaf indicates the reason for its termination.
A = {3, 5, 6, 7 , 2} and sum = 15
The state-space tree can be constructed as a binary tree like that in Figure 4.8 for
the instance A = {3, 5, 6, 7 , 2} and sum = 15.

Table 4.3 state-space

Initially subset = {} Sum = 0


Now add the next
3 3 element

3,5 8 therefore 8 <15 Add next the element


3,5,6 14 therefore 14 < 15 Add next the element
3,5,6,7 21 Sum exceeds the given
constraint hence
backtrack
3,5,6,7,2 23 Sum exceeds the given
constraint hence
backtrack
3,5,7 15=15 Solution is found
6,7,2 15 = 15 Solution is found

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 23


CS3401 ALGORITHMS UNIT 4 MEC

3,5,6,2 16 Sum exceeds the given


constraint hence
backtrack
State Space Tree

Figure 4.8 sum of subsets for a given sum and a set of numbers

Example 4.
Let, w = { 5,7,10,12,15,18,20} and m = 35. Find all possible subset of w whose sum is
equivalent to m. draw the portion of state space tree for this problem. Au : Dec – 12
Solution:
Table 4.4 all possible subset

Initially subset = {} Sum = 0


5 Now add the next
5 element

5,7 12 therefore 12 <35 Add next the element


5,7,10 22 therefore 22 < 35 Add next the element
5,7,10,12 34 therefore 34 < 35 Add next the element
5,7,10,12,15 49 Sum exceeds the given
constraint hence
backtrack
5,7,10,12,18 52 therefore 52< 35 Sum exceeds the given
constraint hence
backtrack
5,7,10,12,20 54 therefore 54> 35 Sum exceeds the given
constraint hence
backtrack

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 24


CS3401 ALGORITHMS UNIT 4 MEC

5,7,10,15 37therefore 37> 35 Sum exceeds the given


constraint hence
backtrack
5,7,10,18 40therefore 40> 35 Sum exceeds the given
constraint hence
backtrack
5,7,10,20 42therefore 42> 35 Sum exceeds the given
constraint hence
backtrack
5,7,12 24therefore 24< 35 Add next the element
5,7,12,15 39therefore 39> 35 Sum exceeds the given
constraint hence
backtrack
5,7,12,18 42therefore 42> 35 Sum exceeds the given
constraint hence
backtrack
5,7,12,20 44therefore 44> 35 Sum exceeds the given
constraint hence
backtrack
5,10,20 35=35 Solution is found
5,12,18 35=35 Solution is found
15,20 35=35 Solution is found

The portion of state space tree is as follows as shown in fig 4.9

Fig 4.9 state space tree


General Remarks about Backtracking
 An output of a backtracking algorithm can be thought of as an n-tuple (x1, x2, . . . , xn) where
each coordinate xi is an element of some finite linearly ordered set Si .
 Forexample, for the n-queens problem, each Si is the set of integers (column numbers) 1
through n.
 The tuple may need to satisfy some additional constraints (e.g., the nonattacking requirements
in the n-queens problem).

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 25


CS3401 ALGORITHMS UNIT 4 MEC

 Depending on the problem, all solution tuples can be of the same length (the n-queens and the
Hamiltonian circuit problem) and of different lengths (the subset-sum problem).
 A backtracking algorithm generates, explicitly or implicitly, a state-space tree; its nodes
represent partially constructed tuples with the first i coordinates defined by the earlier actions
of the algorithm.
 If such a tuple (x1, x2, . . . , xi) is not a solution, the algorithm finds the next element in Si+1 that
is consistent with the values of (x1, x2, . . . , xi) and the problem’s constraints, and adds it to the
tuple as its (i + 1)st coordinate.
If such an element does not exist, the algorithm backtracks to consider the next value of
xi, and so on. To start a backtracking algorithm, the following pseudocode can be called for i =
0 ; X[1..0] represents the empty tuple.

ALGORITHM Backtrack(X[1..i])
//Gives a template of a generic backtracking algorithm
//Input: X[1..i] specifies first i promising components of a solution
//Output: All the tuples representing the problem’s solutions
if X[1..i] is a solution
write X[1..i]
else //see Problem 9 in this section’s exercises
for each element x ∈ Si+1 consistent with X[1..i] and the constraints do
X[i + 1]←x
Backtrack(X[1..i + 1])
Several tricks that might help reduce the size of a state-space tree
1. One is to exploit the symmetry often present in combinatorial problems.
For example, the board of the n-queens problem has several symmetries so that some
solutions can be obtained from others by reflection or rotation.
2. Another trick is to preassign values to one or more components of a solution, as we did
in the Hamiltonian circuit example.

Estimation of size of the state space tree of backtracking algorithm


 Generally it is difficult to estimate the size of the state space tree.
However. Knuth suggested generating a random path from the root to a leaf and using
the information about the number of choices available during the path generation for estimating
the size of the tree.
 Specifically, let c1 be the number of values of the first component x1 that are
consistent with the problem’s constraints.
 We randomly select one of these values (with equal probability 1/c1) to move to
one of the root’s c1 children.
 Repeating this operation for c2 possible values for x2 that are consistent with x1
and the other constraints, we move to one of the c2 children of that node.
We continue this process until a leaf is reached after randomly selecting values for x1, x2,
. . . , xn.
Therefore estimate the number of nodes in the tree as
1+ c1 + c1c2 + . . . + c1c2 . . . cn.
Generating several such estimates and computing their average yields a useful
estimation of the actual size of the tree

Conclusion (or) Advantages


Three things on behalf of backtracking need to be said.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 26


CS3401 ALGORITHMS UNIT 4 MEC

1. First, it is typically applied to difficult combinatorial problems for which no efficient


algorithms for finding exact solutions possibly exist.
2. Second, unlike the exhaustive search approach, which is doomed to be extremely slow
for all instances of a problem, backtracking at least holds a hope for solving some
instances of nontrivial sizes in an acceptable amount of time. This is especially true for
optimization problems, for which the idea of backtracking can be further enhanced by
evaluating the quality of partially constructed solutions. How this can be done is
explained in the next section.
3. Third, even if backtracking does not eliminate any elements of a problem’s state space
and ends up generating all its elements, it provides a specific technique for doing so,
which can be of value in its own right.

[Link] the Assignment Problem by the branch and bound algorithm with an example or Find
Optimal solution using Branch and Bound for the following assignment problem.
Nov/Dec 2017

 Let us illustrate the branch-and-bound approach by applying it to the problem of assigning n


people to n jobs so that the total cost of the assignment is as small as possible.
An instance of the assignment problem is specified by an n × n cost matrix C.

 We have to find a lower bound on the cost of an optimal selection without actually solving
the problem.
We can do this by several methods. For example, it is clear that the cost of any
solution, including an optimal one, cannot be smaller than the sum of the smallest elements in
Each of the matrix’s rows. For the instance here, this sum is 2 + 3+ 1+ 4 = 10.
 It is important to stressthat this is not the cost of any legitimate selection (3 and 1 came from
the same column of thematrix); it is just a lower bound on the cost of any legitimate selection.
 We can and will apply thesame thinking to partially constructed solutions. For example, for a
ny legitimate selection thatselects 9 from the first row, the lower bound will be 9 + 3 + 1+ 4 = 17.

 It is sensible to consider a node with the best bound as most promising, although this does
not, of course, preclude the possibility that an optimal solution will ultimately belong to a differen
tbranch of the state-space tree.
This variation of the strategy is called the best-first branch-and-bound.

The lower-bound value for the root, denoted lb, is 10. The nodes on the first level of the tree
correspond to selections of an element in the first row of the matrix, i.e., a job for person a as
shown in Figure 4.10

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 27


CS3401 ALGORITHMS UNIT 4 MEC

FIGURE 4.10 Levels 0 and 1 of the state-space tree for the instance of the assignment problem
being solved with the best-first branch-and-bound algorithm. The number above a node shows the
order in which the node was generated. A node’s fields indicate the job number assigned to person
a and the lower bound value, lb, for this node.

FIGURE 4.11 Levels 0, 1, and 2 of the state-space tree for the instance of the assignment problem
being solved with the best-first branch-and-bound algorithm.

Of the six live leaves—nodes 1, 3, 4, 5, 6, and 7—that may contain an optimal solution, we again
choose the one with the smallest lower bound, node 5.
First, we consider selecting the thirdcolumn’s element from c’s row (i.e., assigning person c to job 3)
; this leaves us with no choice but
to select the element from the fourth column of d’s row (assigning person d to job 4). This yields
leaf 8 (Figure 4.12), which corresponds to the feasible solution {a→2, b→1, c→3, d →4} with the
total cost of 13. Its sibling, node 9, corresponds to the feasible solution {a→2, b→1, c→4, d →3}
with the total cost of 25.
Since its cost is larger than the cost of the solution represented by leaf 8,
node 9 is simply terminated. (Of course, if its cost were smaller than 13, we would have to replace
the information about the best solution seen so far with the data provided by this node.)

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 28


CS3401 ALGORITHMS UNIT 4 MEC

FIGURE 4.12 Complete state-space tree for the instance of the assignment problem solved with
the best-first branch-and-bound algorithm.
Now, as we inspect each of the live leaves of the last state-space tree—nodes 1, 3, 4, 6, and
7 in Figure 4.12—we discover that their lower-bound values are not smaller than 13, the value of
the best selection seen so far (leaf 8). Hence, we terminate all of them and recognize the solution
represented by leaf 8 as the optimal solution to the problem.

8. Solve the following instance of the knapsack problem by the branch and bound
AU: Dec-06,08,10(APR/MAY 2023)
Knapsack Problem
The branch-and-bound technique is used to solving the knapsack problem.
The Knapsack problem is given n items of known weights wi and values vi , i = 1, 2, . . . , n, and
a knapsack of capacity W, find the most valuable subset of the items that fit in the knapsack.
It is convenient to order the items of a given instance in descending order by their value-to-
weight ratios.
Then the first item gives the best payoff per weight unit and the last one gives the worst payoff
per weight unit, with ties resolved arbitrarily:
v1/w1 ≥ v2/w2 ≥ . . . ≥ vn/wn.

Each node on the ith level of this tree, 0 ≤ i ≤ n, represents all the subsets of n items that include
a particular selection made from the first i ordered items.
This particular selection is uniquely determined by the path from the root to the node.

A branch going to the left indicates the inclusion of the next itemA branch going to the
right indicates its exclusion.
We record the total weight w and the total value v of this selection in the node, along with some
upper bound ub on the value of any subset that can be obtained by adding zero or more items to
this selection.
A simple way to compute the upper bound ub is to add to v, the total value of the items already
selected, the product of the remaining capacity of the knapsack W − w and the best per unit
payoff among the remaining items, which is vi+1/wi+1:
ub = v + (W − w)(vi+1/wi+1)

Construction of a State space Tree


It is natural to structure the state-space tree for this problem as a binary tree constructed as
follows, which is shown in figure.
Item Weight Value Value/Weight
1 4 $40 10
2 7 $42 6
W= capacity of Knapsack’s = w =10.
3 5 $25 5
4 3 $12 4

We will first compute the upper bond by using above given formula
ub = v + (W − w)(vi+1/wi+1)

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 29


CS3401 ALGORITHMS UNIT 4 MEC

Initially v=0 , w=0 and vi +1 = v1 = 40 and w i+1 = wi+1 = w1 = 4. The capacity W = 10


Ub = 0 +(10-0)(40/4)
= (10) (10)
Ub = 100$
Now we will construct a state space tree by selecting different items
Computation at node 0
i,e . root of state space tree
Initially v=0 , w=0 and vi +1 = v1 = 40 and w i+1 = wi+1 = w1 = 4. The capacity W = 10
Ub = 0 +(10-0)(40/4)
= (10) (10)
Ub = 100$
Computation at node I
At node I in state space tree we assume the selection of item 1.
Therefore v= 40 , w = 4
The capacity W = 10
Now vi +1/ w i+1 -> means next item to item 1
i.e v2/w2 = 6
ub = v + (W − w)(vi+1/wi+1)
= 40 +(10-4)*6
= 40 + 6 *6
ub =76
Computation at node II
At node II in state space tree we assume the selection of item 1. Not selected
Therefore v= 0 , w = 0
The capacity W = 10
Next to item 1 is item 2
Now vi +1/ w i+1 -> means 2
i.e v2/w2 = 6
ub = v + (W − w)(vi+1/wi+1)
= 0 +(10-0)*6
= 40 + 6 *6
ub =60

Computation at node III


At this node we pick up item 1 and item 2 but the weight becomes 4+7 = 11. This exceed the
capacity of knapsack. Hence we will not consider this node
Computation at node IV
At node item 2 is not selected and only item 1 is selected.
Therefore v= 40 , w = 4
The capacity W = 10
Next item will be item 3 selected
Now vi +1/ w i+1 = v3/w3 = 20/5=5
ub = v + (W − w)(vi+1/wi+1)
= 40 +(10-4)*5
= 40 + 6 *5
ub =70
Computation at node V
Node VI is an instance at which only item 1 and item 3 are selected . and item 2 is not selected
vi +1/ w i+1 = v3/w3 = 25/5 =5

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 30


CS3401 ALGORITHMS UNIT 4 MEC

v = 40 + 25 = 65
w = 4 +5 = 9
The capacity W = 10
The next item would be vi +1/ w i+1 -> item 4
Therefore v4/w4 = 12/3 =4
ub = v + (W − w)(vi+1/wi+1)
= 65 +(10-9)*4
= 65 + 1 *4 ub =69
Computation at node VI
At node VI is an instant at which item 1 is selected , item 2 and item 3 are not selected .
Therefore v= 40 , w = 4
The capacity W = 10
The Next item being selected is item 4
Now vi +1/ w i+1 = v4/w4= 12/3 =4
i.e v4/w4 = 4
ub = v + (W − w)(vi+1/wi+1)
=4 0 +(10-4)*4
= 40 + 6 *4 ub =64
Computation at node VII
At node VII , we consider selection of item 1, item 3, item 4. There is no next item given
problem statement
vi +1/ w i+1=0
w = 4 + 5+ 3 = 12 -> but this is exceeding capacity W = 10
v = 40 + 25 + 12 = 72
W = 10
ub = v + (W − w)(vi+1/wi+1)
= 72 + (10 -12)* 0 ub = 72
But as weight of selected items exceed the capacity W this is not a feasible solution.
Computation at node VIII
At node VIII , we consider selection of item 1and item [Link] is no next item given problem
statement
vi +1/ w i+1=0
w = 4 + 5= 9 -> but this is exceeding capacity W = 10
v = 40 + 25 = 65
W = 10
ub = v + (W − w)(vi+1/wi+1)
= 65 + (10 -9)* 0 ub = 65
The node IX is a node indicating maximum profit of selected items with maximum weight of
item = 9 i.e . <capacity of knapsack ( W=10) as shown in fig 4.13
Thus solution is pick up { item 1, item3 } and gain maximum profit 65$

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 31


CS3401 ALGORITHMS UNIT 4 MEC

Fig 4.13 :State-space tree of the best-first branch-and-bound algorithm for the
instance of the knapsack problem.

9. Explain how to solve TSP (Traveling Salesman Problem) using branch and bound Au:Dec -13
Apr/May 2017 Apr-18
Problem statement
 The branch-and-bound technique is applied to the instances of the traveling salesman problem. If
there are n cities and cost of travelling from any city to any other city is given then we have to
obtain the cheapest round –trip such that each city is visited exactly once and then returning to
starting city , completes the tour Typically travelling salesman problem is represented by
weighted graph
 In this method we consider computing of lower bounds. The lower bound is denoted by LB and
can be obtained using following formula

LB= ⅀v€V ( sum of costs of the two least cost edges adjacent to v)/ 2 or
lb = [s/2]

This method can be well understood with the help of some examples
Consider following graph for solving TSP

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 32


CS3401 ALGORITHMS UNIT 4 MEC

Fig: Weighted graph

We will first obtain lower bound is computed as,


LB= ⅀v€V ( sum of costs of the two least cost edges adjacent to v)/ 2
a b c d e
lb =[[(1+ 3) + (3 + 6) + (1+ 2) + (3 + 4) + (2 + 3)]/2]
lb = 28/2
lb = 14
Because a = 2 minimum cost edges adjacent to a
= ac +ab a = 1 + 3 =4
b = 2 minimum cost edges adjacent to b
= ba +bc b = 3 + 6 =9
c = 2 minimum cost edges adjacent to c
= ac +ce c = 1+ 2 =3
d = 2 minimum cost edges adjacent to d
= de +dc c = 3 + 4 =7
e = 2 minimum cost edges adjacent to e = ce +ed e = 2 + 3 =5

Consider node 1 : it says that consider distance a-b in computation of the corresponding
vertices along with one minimum distance.
a= (a-b) + (a-c) = 3 + 1
b = ( a-b) + ( b-c) = 3+ 6
c = ( a-c) + (c-e) = 1+2 -> cannot consider ( a-b) because an edges (a-b) is not adjacent
to c
d = ( d-e) + ( c-d) = 3 +4 -> can not consider (a-b)
e = ( c-e) + ( d-e) = 2 +3 -> can not consider (a-b)
lb =[[(3+ 1) + (3 + 6) + (1+ 2) + (3 + 4) + (2 + 3)]/2]
lb = 28/2lb = 14. Is for node 1.
Consider node 2 : it says that consider distance a-c in computation of the corresponding
vertices along with one minimum distance.

a= (a-b) + (a-c) = 3 + 1
b = ( a-b) + ( b-c) = 3+ 6 -> cannot consider( a-c) because an edges (a-c)
is not adjacent to b
c = ( a-c) + (c-e) = 1+2
d = ( d-e) + ( c-d) = 3 +4 -> can not consider (a-c) here
e = ( c-e) + ( d-e)= 2 +3 -> can not consider (a-c)

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 33


CS3401 ALGORITHMS UNIT 4 MEC

lb =[[(3+ 1) + (3 + 6) + (1+ 2) + (3 + 4) + (2 + 3)]/2]


lb = 28/2
lb = 14. Is for node 2
but cannot be considered because b is before c
Consider node 3 : it says that consider distance a-d in computation of the corresponding
vertices along with one minimum distance.
a= (a-c) + (a-d) = 1 + 5
b = ( a-b) + ( b-c) = 3+ 6 -> cannot consider ( a-d)
c = ( a-c) + (c-e) = 1+2 -> cannot consider ( a-d)
d = ( d-e) + ( a-d) = 3 +5
e = ( c-e) + ( d-e)= 2 +3 -> can not consider (a-d)
lb =[[(1+ 5) + (3 + 6) + (1+ 2) + (3 + 5) + (2 + 3)]/2]
lb = 31/2
lb = 15
Consider node 4 : this node say include edge a-e wherever possible
a= (a-c) + (a-e) = 1 + 8 =9
b = ( a-b) + ( b-c) = 3+ 6 =9 -> cannot consider ( a-e)
c = ( a-c) + (c-e) = 1+2 =3 -> cannot consider ( a-e)
d = ( c-d) + ( d-e) = 4 +3 = 7-> can not consider (a-e)
e = ( c-e) + ( a-e)= 2 +8 =10
lb =[[(1+ 8) + (3 + 6) + (1+ 2) + (4 + 3) + (2 + 8)]/2]
lb = 38/2
lb = 19
Consider node 5 : this node say path a-b-c i.e include edges (a-b) ,(b-c)
wherever possible
a= (a-b) + (a-c) = 3 + 1 =4 -> cannot include (b-c) here because (b-c) is
not adjaect to a
b = ( a-b) + ( b-c) = 3+ 6 =9
c = ( a-c) + (b-c) = 1+6=7 -> minimum distance (a-c) is include but (a-b)
d = ( d-e) + ( d-c) = 3+4 = 7
e = ( c-e) + ( d-e)= 2 +3 =5
lb =[[(3+ 1) + (3 + 6) + (1+ 6) + (3 + 4) + (2 + 3)]/2]
lb = 32/2
lb = 16.
similarly we can compute LB at node 6,7,8,9,10
Consider node 8 : it says a-b-c-d that means include ( a-b), (b-c), (c-d) whichever is
minimum and whichever is applicable. As this the leaf node and from this node we try to reach
to source node. That is after a-b-c-d we go to e and from e – to – a. hence.
a= (a-b) + (a-e) = 3 + 9 =12
b = ( a-b) + ( b-c) = 3+ 6 =9
c = ( b-c) + (c-d) = 6+4=10
d = ( c-d) + ( d-e) = 4+3 = 7
e = ( a-e) + ( d-e)= 8 +3 =11
lb =[[(3+ 9) + (3 + 6) + (6+ 4) + (4 + 3) + (8 + 3)]/2]
lb = 49/2
lb = 24
Consider node 11: it says a-b-d-e that means include (a-b), (b-d), (d-e) in computation..
a= (a-b) + (a-c) = 3 + 1 =4
b = ( a-b) + ( b-d) = 3+ 7 =10

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 34


CS3401 ALGORITHMS UNIT 4 MEC

c = ( a-c) + (c-e) = 1+2= 3


d = ( b-d) + ( d-e) = 7+3 = 10
e = ( c-e) + ( d-e)= 2 +3 =5
lb =[[(3+ 1) + (3 + 7) + (1+ 2) + (7 + 3) + (2 + 3)]/2]
lb = 32/2
lb = 16
At node 11 we get optimum tour i.e. a-b-d-e
Hence the optimum cost tour of TSP is a-b-d-e-c-a with cost 16
Hence we have obtained LB = ½ ⅀v adjacent distances of all vertices. This forms root of the state
space tree . then we consider a-b-c, a-b-d, a-b-e. then at level 4 we consider a-b-c-d and a-b-c-e then a-
b-d-c and a-b-d-e . Thus the space tree can be

To reduce the amount of potential work, two factors are considered. They
1. Without loss of generality, we can consider only tours that start at a.
2. Second, because our graph is undirected, we can generate only tours in which b is visited before
c.
In addition, after visiting n − 1= 4 cities, a tour has no choice but to visit the remaining unvisited city
and return to the starting one. The state-space tree tracing the algorithm’s application is given in Figure
12.9b.
The state space tree of graph in figure for branch and bound technique is shown in figure.

Strength and weakness of branch and bound


Strength
The branch and bound technique solves large instances of difficult combinational problems.

Weakness
Using branch and bound technique, it is impossible to predict which instances will be solvable in a
realistic amount of time and which will not.

Approximation algorithms for np hard problemsApproximation algorithms are often used to find
approximation solutions to difficult problems of combinatorial optimization
The optimization versions of difficult combinational problems such as the traveling salesman problem
and the knapsack problem.

NP-hard problems are problems that are at least as hard as NP-complete problems.
For NP hard problems there is no known polynomial time algorithms.
The notation of an NP hard problem can be defined more formally by extending the notation of
polynomial reducibility to problems that are not necessary in class NP, including optimization
problems.
Many of the approximation algorithms are greedy algorithm based on some problem-specific heuristic.
A heuristic is a common-sense rule drawn from experience rather than from a mathematically proved
assertion.
For example, going to the nearest unvisited city in the traveling salesman problem is a good
illustration of this notion.
Accuracy
The accuracy of an approximate solution sa to a problem of minimizing some function f by the size of
the relative error of this approximation,
re (sa) =
Where S* is an exact solution to the problem.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 35


CS3401 ALGORITHMS UNIT 4 MEC

Alternatively, since re(sa) = f (sa)/


re (sa) =
re (sa) = Accuracy Ratio
The accuracy ratio is defined as a measure of accuracy of sa. It is given by
re (sa) =
For scale uniformity, the accuracy ratio of approximate solutions to maximization
problems is usually computed as
re (sa) =
The accuracy ratio is greater than or equal to 1, as it is for minimization problems.
Obviously, the closer r( ) is to 1, the better the approximate solution [Link] Ratio
Performance ratio of the algorithm and denoted by RA and it serves as the principal
metric indicating the quality of the approximation algorithm.
The performance ratio (RA) value of approximation algorithms as close to 1 as possible.
Unfortunately, as we shall see, some approximation algorithms have infinitely large
performance ratios (RA=∞). This does not necessarily rule out using such algorithms, but it does
call for a cautious treatment of their outputs.
Polynomial time approximation algorithm
A polynomial-time approximation algorithm is said to be a c approximation algorithm,
where c ≥ 1, if the accuracy ratio of the approximation it produces does not exceed c for any
instance of the problem in question:
It is given by,
F(s a) ≤ f(s*)
Where
s a – approximate solution
s* - exact solution
c - constant

10. The knight is placed on the first block of an empty board and moving according to the
rules of chess, must visit each square exactly once.

Solve the above problem using backtracking procedure(AU MAY15)


For example, consider the following Knight’s Tour problem.
The knight is placed on the first block of an empty board and, moving according to the rules of
chess, must visit each square exactly once.
Let us first discuss the Naive algorithm for this problem and then the Backtracking algorithm.
Naïve Algorithm for Knight’stour

The Naive Algorithm is to generate all tours one by one and check if the generated tour satisfies
the constraints.
while there are untried tours
{
generate the next tour
if this tour covers all squares
{
print this path;
}

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 36


CS3401 ALGORITHMS UNIT 4 MEC

Backtracking works in an incremental way to attack problems. Typically, we start from an


empty solution vector and one by one add items (Meaning of item varies from problem to
problem. In context of Knight’s tour problem, an item is a Knight’s move).
When we add an item, we check if adding the current item violates the problem constraint, if it
does then we remove the item and try other alternatives.
If none of the alternatives work out then we go to previous stage and remove the item added in
the previous stage.
If we reach the initial stage back then we say that no solution exists. If adding an item doesn’t
violate constraints then we recursively add items one by one. If the solution vector becomes
complete then we print the solution.
BacktrackingAlgorithmforKnight’stour
Following is the Backtracking algorithm for Knight’s tour problem.
If all squares are visited
print the solution
Else
a) Add one of the next moves to solution vector and recursively check if this
move leads to a solution. (A Knight can make maximum eight moves.
We choose one of the 8 moves in this step).
b) If the move chosen in the above step doesn't lead to a solution then
remove this move from the solution vector and try other alternative moves.
c) If none of the alternatives work then return false (Returning false will
remove the previously added item in recursion and if false is returned by
the initial call of recursion then "no solution exists" )
Following are implementations for Knight’s tour problem. It prints one of the possible solutions
in 2D matrix form. Basically, the output is a 2D 8*8 matrix with numbers from 0 to 63 and
these numbers show steps made by Knight.

[Link] Backtracking for Graph colouring problem


Graph Coloring Algorithm using Backtracking
What is Graph Coloring Problem?
We have been given a graph and we are asked to color all vertices with the ‘M’ number of given
colors, in such a way that no two adjacent vertices should have the same color.

 It it is possible to color all the vertices with the given colors then we have to output the
colored result, otherwise output ‘no solution possible’.
 The least possible value of ‘m’ required to color the graph successfully is known as
the chromatic number of the given graph.
Graph Coloring Solution
 Using Backtracking Algorithm
The backtracking algorithm makes the process efficient by avoiding many bad decisions made
in naïve approaches.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 37


CS3401 ALGORITHMS UNIT 4 MEC

In this approach, we color a single vertex and then move to its adjacent (connected) vertex to
color it with different color as shown in fig 4.14
After coloring, we again move to another adjacent vertex that is uncolored and repeat the
process until all vertices of the given graph are colored.
 In case, we find a vertex that has all adjacent vertices colored and no color is left to
make it color different, we backtrack and change the color of the last colored vertices
and again proceed further.
 If by backtracking, we come back to the same vertex from where we started and all
colors were tried on it, then it means the given number of colors (i.e. ‘m’) is insufficient
to color the given graph and we require more colors (i.e. a bigger chromatic number).

Fig 4.14 Steps To color graph using the Backtracking Algorithm:


Different colors:
A. Confirm whether it is valid to color the current vertex with the current color (by
checking whether any of its adjacent vertices are colored with the same color).
B. If yes then color it and otherwise try a different color.
C. Check if all vertices are colored or not.
D. If not then move to the next adjacent uncolored vertex.
If no other color is available then backtrack (i.e. un-color last colored vertex).
Here backtracking means to stop further recursive calls on adjacent vertices by returning false.
In this algorithm Step-1.2 (Continue) and Step-2 (backtracking) is causing the program to try
different color option.
Continue – try a different color for current vertex.
Backtrack – try a different color for last colored vertex.

Example of Graph coloring


E. Let G be a graph and m be a given positive integer. We want to discover whether the
nodes of G can be colored in such a way that no two adjacent nodes have the same color
yet only m colors are used.

 This is termed them-colorability decision problem .Note that if d is the degree


of the given graph, then it can be colored with d + 1 colors.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 38


CS3401 ALGORITHMS UNIT 4 MEC

The m-colorability optimization problem asks for the smallest integer m for which the
graph G can be colored. This integer is referred to as the chromatic number of the
graph.

For example, the graph of Figure 4.15 can be colored with three colors 1, 2, and 3. The
color of each node is indicated next to it. It can also be seen that three colors are needed to
color this graph and hence this graph's chromatic number is 3.

Fig 4.15:Example Graph and coloring


A graph is said to be planar if it can be drawn in a plane in such a way that no
two edges cross each other. A famous special case of the m- colorability decision
problem is the 4-color problem for planar graphs.

This problem asks the following question: given any map, can the regions be
colored in such a way that no two adjacent regions have the same color yet only
four colors are needed?
This turns out to be a problem for which graphs are very useful, because a map can
easily be transformed into a graph. Each region of the map becomes a node, and if
two regions are adjacent, then the corresponding nodes are joined by an edge.

Figure 4 . 1 5 shows a map with five regions and its corresponding graph. This
map requires four colors. For many years it was known that five colors were
sufficient to color any map, but no map that required more than four colors had
ever been found.
After several hundred years, this problem was solved by a group of mathematicians
with the help of a computer. They showed that in fact four colors are sufficient. In
this section we consider not only graphs that are produced from maps but all
graphs.
We are interested in determining all the different ways in which a given graph can
be colored using at most m colors.

Fig 4.16 :Map and its planar Graph representations


Suppose we represent a graph by its adjacency matrix

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 39


CS3401 ALGORITHMS UNIT 4 MEC

G[l : n,1 : n], where G[i, j] = 1 if (i, j) is an edge of G, and G[i, j] = 0


otherwise.
The colors are represented by the integers 1, 2, ... , m and the solutions
are given by the n-tuple (xi, ... , Xn), where Xi is the color of node i.

The underlying state space tree used is a tree of degree m and height
n + l. Each node at level i has m children corresponding to the m
possible assignments to Xi, 1 ::; i ::; n. Nodes at level n + l are leaf
nodes.
Figure 4.17 shows the state space tree when n =3 and m = 3.
Function mColoring is begun by first assigning the graph to its adja- cency matrix,
setting the array x[] to zero, and then invoking the statement m Coloring(l);.

Notice the similarity between this algorithm and the general form of the recursive
backtracking schema of Algorithm 7.1. Function NextValue (Algo- rithm 7.8)
produces the possible colors for xk after x1 through Xk-l have been defined.
The main loop of mColoring repeatedly picks an element from the set of
possibilities, assigns it to xk, and then calls mColoring recursively.

For instance, Figure 4.17 shows a simple graph containing four nodes. Below that is
the tree that is generated by mColoring.

Each path to a leaf repre- sents a coloring using at most three colors. Note that
only 12 solutions exist with exactly three colors.

 In this tree, after choosing x1 = 2 and x2 = 1, the possible choices for


X3 are 2 and 3.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 40


CS3401 ALGORITHMS UNIT 4 MEC

 After choosing x1 = 2, x2 = 1, andx3 = 2, possible values for x4 are 1


and 3. And so on.

Figure 7.13 State space tree for mColoring when n =3 and m = 3

Fig 4.17 mColoring

Algorithm Generating a next color

An upper bound on the computing time of mColoring can be arrived at by noticing that
the number of internal nodes in the state space tree is :

At each internal node, O(mn) time is spent by NextValue to determine the children
corresponding to legal colorings.
Hence the total time is bounded by :

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 41


CS3401 ALGORITHMS UNIT 4 MEC

12. Explain Branch-and-Bound for The 15-puzzle problem.

Branch-and-Bound
 The term branch-and-bound refers to all state space search methods
in which all children of the E-node are generated before any other
live node can become the E-node.
 We have already seen two graph search strategies, BFS and D-
search, in which the exploration of a new node cannot begin until
the node currently being explored is fully explored.
 Both of these generalize to branch-and- bound strategies. In branch-
and- bound terminology, a BFS-like state space search will be called
FIFO (First In First Out) search as the list of live nodes is a first-
in-first-out list (or queue).
 A D-search-like state space search will
be called LIFO (Last In First Out) search as the list of live nodes
is a last-in-first-out list (or stack).
 As in the case of backtracking,
bounding functions are used to help avoid the generation of
subtrees that do not contain an answer node.

The 15-puzzle: An Example


The 15-puzzle (invented by Sam Loyd in 1878) consists of 15 numbered tiles on a
square frame with a capacity of 16 tiles (Figure 4.18).
We are given an initial arrangement of the tiles, and the objective is to transform this
arrangement into the goal arrangement of Figure 4.18 (b) through a series of legal
moves. The only legal moves are ones in which a tile adjacent to the empty spot
(ES) is moved to ES.
Thus from the initial arrangement of Figure 4 . 1 8 (a), four moves are possible.
We can move any one of the tiles numbered 2, 3, 5, or 6 to the empty spot.

FIG 4.18 :15 Puzzle arrangememts

 Following this move, other moves can be made. Each move creates a new
arrangement of the tiles. These arrangements are called the states of the
puzzle. The initial and goal arrangements are called the initial and goal states.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 42


CS3401 ALGORITHMS UNIT 4 MEC

 A state is reachable from the initial state iff there is a sequence of legal
moves from the initial state to this state.

 The state space of an initial state consists of all states that can be reached
from the initial state. The most straightforward way to solve the puzzle would
be to search the state space for the goal state and use the path from the
initial state to the goal state as the answer.

 It is easy to see that there are 16! (16! :::::: 20.9 x 1012) different arrangements
of the tiles on the frame. Of these only one-half are reachable from any given
initial state. Indeed, the state space for the problem is very large.

 Before attempting to search this state space for the goal state, it would be
worthwhile to determine whether the goal state is reachable from the initial
state. There is a very simple way to do this.

Let us number the frame positions 1 to 16. Position i is the frame position containing
tile numbered i in the goal arrangement of Figure 4.18(b). Position 16 is the empty
spot. Let position(i) be the position number in the initial state of the tile numbered i.
Then position(16) will denote the position of the empty spot.

For any state let [Link]( i) be the number of tiles j such that j < i and position(j) >
position(i).

For the state of Figure 4.18 (a) we have, for exam- ple, less(l) = 0, less(4) = 1, and
less(12) = 6.

Let x =1 if in the initial state the empty spot is at one of the shaded positions of
Figure 4.18 (c) and x = 0 if it is at one of the remaining positions. Then, we have the
following theorem:

Theorem 8.1 The goal state of Figure 4 . 1 8 (b) is reachable from the initial state iff :

Proof: Left as an exercise.

Theorem 8.1 can be used to determine whether the goal state is in


the state space of the initial state. If it is, then we can proceed to
determine a sequence of moves leading to the goal state.
To carry out this search, the state space can be organized into a tree.
The children of each node x in this tree represent the states reachable
from state x by one legal move.
It is convenient to think of a move as involving a move of the empty
space rather than a move of a tile. The empty space, on each move,
moves either up, right, down, or left. Figure 4 . 1 9 shows the first
three levels of the state space tree of the 15-puzzle beginning with the
initial state shown in the root.
Parts of levels 4 and 5 of the tree are also shown. The tree has been
pruned a little. No node p has a child state that is the same as p's parent.
The subtree eliminated in this way is already present in the tree and has
root parent(p). As can be seen, there is an answer node at level 4.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 43


CS3401 ALGORITHMS UNIT 4 MEC

Edges are labeled according to the direction in which


the empty space moves

Figure 4 . 1 9 Part of the state space tree for the 15-puzzle

 A depth first state space tree generation will result in the subtree of Figure 8.4
when the next moves are attempted in the order: move the empty space up, right,
down, and left. Successive board configurations reveal that each move gets us
farther from the goal rather than closer.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 44


CS3401 ALGORITHMS UNIT 4 MEC

 The search of the state space tree is blind. It will take the leftmost path from the root
regardless of the starting configuration. As a result, an answer node may never be
found ( unless the leftmost path ends in such a node).
 In a FIFO search of the tree of Figure 4 . 2 0 , the nodes will be generated in the order
numbered.
 A breadth first search will always find a goal node nearest to the root.
However, such a search is also blind in the sense that no matter what the initial
configuration, the algorithm attempts to make the same sequence of moves.
A FIFO search always generates the state space tree by levels

Figure 4 . 2 0 First ten steps in a depth first search

 What we would like, is a more "intelligent" search method, one that


seeks out an answer node and adapts the path it takes through
the state space tree to the specific problem instance being solved.
 We can associate a cost c(x) with each node x in the state space
tree. The cost c(x) is the length of a path from the root to a
nearest goal node (if any) in the subtree with root x.

 Thus, in c(l) = c(4) = c(lO) = c(23) = 3. When such a cost function


is available, a very efficient search can be carried out.
 We begin with the root as the E-node and generate a child node
with c()-value the same as the root.
 Thus children nodes 2, 3, and 5 are eliminated and only node 4
becomes a live node. This becomes the next E-node.
 Its first child, node 10, has c(lO) = c(4) = 3. The remaining
children are not generated. Node 4 dies and node 10 becomes the
E-node.
 In generating node lO's children,node 22 is killed immediately
 as c(22) > 3. Node 23 is generated next.
 It is a goal node and the search terminates. In this search strategy,
the only nodes to become E-nodes are nodes on the path from the
root to a nearestgoal node.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 45


CS3401 ALGORITHMS UNIT 4 MEC

 Unfortunately, this is an impractical strategy as it is not possible to


easily compute the function c(·) specified above.
An LC-search of using c(x) will begin by using node 1 as the E-node. All its
children are generated.

Figure 4 . 2 1 Problem state


Node 1 dies and leaves behind the live nodes 2, 3, 4, and [Link] next node to become
the E-node is a live node with least c(x). Then c(2) = 1+4, c(3) = 1+4, c(4) = 1+2,
andc(5) = 1+4. Node 4 becomes the E-node. Its children are generated as shown in
fig 4.21 The live nodes at this time are 2, 3, 5, 10, 11, and 12. So c(lO) = 2 + 1,
c(ll) = 2 + 3, and c(12) = 2 + 3. The live node with least c is node 10.
This becomes the next E-node. Nodes 22 and 23 are generated next. Node 23 is
determined to be goal node and the search terminates.
In this case LC-search was almost as efficient as using the exact function c(). It
should be noted that with a suitable choice for c(), an LC-search will be far more
selective than any of the other search methods we have discussed.

13. Explain Backtracking for Graph coloring problem


Example:
Input: A (undirected) graph G = (V, E) and an integer k.
Output: An assignment of one of k colors to each node, such that adjacent
nodes get different colors.

For example, let G = (V, E) where V = 1, 2, 3, 4 and {E = (1, 2),} (2, 3), (2, 4),
{ (3, 4) and suppose that k}=
3. A valid coloring c of G is: c(1) = R, c(2) =
G, c(3) = B, c(4) = R.

Potential solutions
Suppose that V=N Then (c1, c2, ..., cn) is a possible coloring of G where ci is the
color of node i in G. Note that there are kn possible colorings. A coloring is feasible
or validif no two adjacent nodes are given the same color, that is, if (i, j) ∈ E then ci
ƒ= c j .
Consider a graph G = (V, E) where V = {1, 2, 3, 4 }
and E = {( 1, 2), (1, 3),(2, 3), (2, 4), (3, 4)}
and let k = 3. There are six valid colorings of G given in the
following T able 4.4:

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 46


CS3401 ALGORITHMS UNIT 4 MEC

node p q r s t u
1 R R G G B B
2 G B B R R G
3 B G R B G R
4 R R G G B B

Note that all these colorings are sort of equivalent. They all share the following
structure:
• The same color is used for both node 1 and node 4. For colorings p and
q, it is R, for colorings r and s, it is G, and for colorings t and u, it is
B.
• Nodes 2 and 3 must have distinct colors different from each other and
from the color used for nodes 1 and 4.

Definition Two colorings are equivalent if one can be transformed into another by permuting
the k colors.

Using backtracking to find valid colorings

We will use the following strategy to find all valid colorings of a graph
G = (V, E):

 Order nodes arbitrarily.


 Assign the first node a color.
 Given a partial assignment of colors (c1, c2, ..., ci−1) to the− first i-1
nodes, try to find a color for the i-th node in the graph.
 If there is no possible color for the i-th node given the previous
choices, backtrack to a previous solution.

Consider the graph G given earlier, where


E = {(1, 2), (1, 3), (2, 3), (2, 4), (3, 4)} and k = 3.

Step 1: Choose a color for node 1. It can be one of: R, B or G. Say we choose R.
Step 2: Given partial coloring (R), we choose a color for node 2. It can be one of: G or B.
Say we choose G.
Step 3: Given partial coloring (R, G), we choose a color for node 3. It cannot be either R or
G, so it must be B since k = 3.
Step 4: Given partial coloring (R, G, B), we choose a color for node 4. It cannot be B or G,
so it must be R. This gives the coloring (R, G, B, R) which is coloring p.
We have no more choices of colors in step 4, and in step 3. We have one choice in step
2.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 47


CS3401 ALGORITHMS UNIT 4 MEC

Step 5:Given partial coloring (R), we choose a different color for node [Link] choose B for node 2.
Step 6: Given partial coloring (R, B), we choose a color for node 3. It cannot be R nor B, so
it must be G.
Step 7: Given partial coloring (R, B, G), we choose a color for node 4. It cannot be B or G,
so it must be R. This gives the coloring (R, B, G, R) which is coloring q.
We have no more choices of colors in steps 4, 3 and 2. We go back to step 1.
Step 8: We choose a different color for node 1, say B. This will produce a branch in the
tree equivalent to the first branch where R and B are switched. Thus we will get the
colorings t and u.
If we choose G for node 1 then, we will again get a branch equivalent to the first one
with R and G swapped. This will produce the colorings r and s.
Algorithm

We now give a recursive version of the graph coloring algorithm. Let C[1...j−
1] be a partial coloring for the first j − 1 nodes.

Color(C,j,k,n)
if j = n+1 then
output C
return or
quit
for i = 1 to k
C[j] = i
if valid(C,j,n)
then
Color(C,j+1,k,n
)
where
Valid(C,j,n)
for all neighbors v of j with v <
j if C[v] = C[j] then
return false
return true

Pruning

If we are simply looking for a single solution, we can cut off the equivalent
branches of the tree to save time. For example, the three main branches of the
backtracking tree obtained in section 4.2 all gave equivalent solutions. We
need only consider the first branch if we want a single solution.
The following algorithm prunes the tree to remove equivalent branches. It
uses the following strategy:

• Keep track of the largest color used so far.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 48


CS3401 ALGORITHMS UNIT 4 MEC

• Try the previously used colors first.

• Never try more than one new color.

ColorP(C,j,last,k,n)
if j = n+1 then
output C
return or quit
for i = 1 to last //try old colors first
C[j] = i
if valid(C,j,n) then
ColorP(C,j+1,last,k,n)
if last < k then
C[j] = last + 1
ColorP(C,j+1,last+1,k,n)

Applications of Graph Coloring


Some applications of graph coloring include −
 Register Allocation
 Map Coloring
 Bipartite Graph Checking
 Mobile Radio Frequency Assignment
 Making time table, etc.
13. Solve the following subset sum problem using back tracking. Let S = \{3, 7, 9, 13, 26, 41\}
d(sum) = 51 Apr/May 2024
Step 1: Understanding Backtracking Approach

Backtracking is a depth-first search approach where:

1. We explore possible subsets recursively.


2. We include/exclude elements to check if a subset sums to 51.
3. If the sum exceeds 51 or all elements are used without reaching 51, we backtrack.

Step 2: Recursive Backtracking Algorithm

1. Start with an empty subset.


2. At each step, either include or exclude the next element.
3. If the subset sum equals 51, print the subset.
4. If the sum exceeds 51, stop exploring that path.

Step 3: Implementing Backtracking

We explore subsets systematically.

1. Include 3 → Remaining sum: 51 - 3 = 48


o Include 7 → Remaining sum: 48 - 7 = 41

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 49


CS3401 ALGORITHMS UNIT 4 MEC

 Include 9 → Remaining sum: 41 - 9 = 32


 Include 13 → Remaining sum: 32 - 13 = 19
 Include 26 → Remaining sum: 19 - 26 = -7 (Backtrack)
 Exclude 26, Include 41 → Remaining sum: 19 - 41 = -22
(Backtrack)
 Exclude 13, Include 26 → Remaining sum: 32 - 26 = 6
 Include 41 → Remaining sum: 6 - 41 = -35 (Backtrack)
 Exclude 26, Include 41 → Remaining sum: 32 - 41 = -9
(Backtrack)
 Exclude 9, Include 13 → Remaining sum: 41 - 13 = 28
 Include 26 → Remaining sum: 28 - 26 = 2
 Include 41 → Remaining sum: 2 - 41 = -39 (Backtrack)
 Exclude 26, Include 41 → Remaining sum: 28 - 41 = -13
(Backtrack)
 Exclude 13, Include 26 → Remaining sum: 41 - 26 = 15
 Include 41 → Remaining sum: 15 - 41 = -26 (Backtrack)
 Exclude 26, Include 41 → Remaining sum: 41 - 41 = 0 ✅ Solution
found: {3, 7, 41}

Step 4: Solution

One valid subset that sums to 51 is:


{3, 7, 41}

Other possible solutions can be found by exploring different paths, but this is one correct subset.

Step 5: Time Complexity

 The worst-case complexity is O(2ⁿ) (exponential), where n = 6.


 Since we use pruning (backtracking), the practical runtime is reduced.

14. Explain the branching mechanism in the Branch and Bound Strategy to solve 0/1
Knapsack problem. Apr/May 2024

Introduction

The 0/1 Knapsack Problem is an optimization problem where we have:

 n items, each with a weight wiw_iwi and value viv_ivi.


 A knapsack with capacity WWW.
 The goal is to maximize the total value while ensuring the total weight does not exceed
WWW.
 Each item can either be included (1) or excluded (0).

Branch and Bound (B&B) is an exhaustive search technique that efficiently finds the optimal
solution by systematically exploring and pruning branches in a search tree.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 50


CS3401 ALGORITHMS UNIT 4 MEC

Branching Mechanism in Branch and Bound

The branching mechanism determines how we explore different possible solutions


(subproblems). It works as follows:

Step 1: Represent the Problem as a Search Tree

Each node in the tree represents a partial solution, with the following:

1. Level: The index of the item being considered.


2. Bound: An estimate of the maximum value that can be obtained from this node.
3. Weight: The total weight of items selected so far.
4. Value: The total value of items selected so far.

Step 2: Branching - Include or Exclude an Item

At each step (node in the tree), we make two branches:

1. Left Branch (Include the item)


o Add the item to the knapsack.
o Update weight and value.
o Move to the next item.
2. Right Branch (Exclude the item)
o Skip the item.
o Keep weight and value unchanged.
o Move to the next item.

Example of Branching

Let’s consider an example:

Item Weight Value


1 2 40
2 3 50
3 5 100

Knapsack capacity W=5W = 5W=5.

Step 1: Root Node (Start with no items)

 Weight = 0, Value = 0.
 Compute an upper bound (greedy estimation).
 Branch into two cases:
o Left child: Include item 1.
o Right child: Exclude item 1.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 51


CS3401 ALGORITHMS UNIT 4 MEC

Step 2: Left Branch (Include Item 1)

 Weight = 2, Value = 40.


 Branch again:
o Left child: Include item 2.
o Right child: Exclude item 2.

Step 3: Right Branch (Exclude Item 1)

 Weight = 0, Value = 0.
 Branch again:
o Left child: Include item 2.
o Right child: Exclude item 2.

Bounding - Pruning Unnecessary Branches

To avoid exploring every possibility, we use bounding:

 Calculate an upper bound on the best possible solution from each node.
 Prune (cut off) nodes where the bound is lower than the best solution found so far.

The bound is calculated using a greedy fractional knapsack approach, where:

 Items are considered in decreasing value/weight ratio.


 Remaining capacity is filled with fractional items (hypothetically).

If the bound of a node is less than the current best solution, we prune (discard) the branch.

Conclusion

The branching mechanism systematically explores choices, while the bounding function
prevents unnecessary searches, making the Branch and Bound approach more efficient than
brute force for solving the 0/1 Knapsack problem.

[Link] the following graph. The vertex represents the city and edge represents the cost
between the two vertices. A salesman starts from node1, visit all the cities exactly once and
return to the starting node. Justify that the algorithm that uses optimality principle produces an
optimal tour cost to visit all [Link]/Dec 2024

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 52


CS3401 ALGORITHMS UNIT 4 MEC

The graph provided illustrates a problem related to the Traveling Salesman Problem (TSP),
where a salesman must visit all cities (vertices) exactly once and return to the starting point,
minimizing the total cost (edge weights).

Optimality Principle in TSP

An algorithm that follows the optimality principle (e.g., dynamic programming with Bellman-
Held-Karp or greedy heuristics) assumes that optimal solutions to sub-problems lead to an
optimal solution for the entire problem.

Justification

1. Structure of Optimal Solutions:


o If a salesman follows the optimal path, then every subset of cities visited (along
with the cost incurred) must also be optimal. For example, if the optimal tour
includes a path from City 1 to City 3, that path is the cheapest way to travel
between those cities, given the preceding and following cities in the tour.
2. Dynamic Programming (Held-Karp Algorithm):
o This algorithm solves TSP using optimal substructure. It computes the cost of
visiting subsets of cities and builds up to the full problem.
o Example:
 Starting from City 1 and visiting {2, 3, 4} involves considering all
permutations, storing optimal paths for subsets, and combining these
into the final solution.
 This guarantees the minimal cost because every sub-tour considered is
itself optimal.
3. Cost Analysis in Graph:
o By evaluating all possible tours and using the optimality principle, we ensure no
better (lower-cost) tour is overlooked.
o For instance:
 Possible tours starting at Node 1: 1→2→3→4→11 \to 2 \to 3 \to 4 \to
11→2→3→4→1, 1→3→4→2→11 \to 3 \to 4 \to 2 \to 11→3→4→2→1,
and so on.
 Comparing their costs ensures the algorithm selects the least-cost tour.
4. Non-Optimal Algorithms:
o Without the optimality principle, a tour might skip evaluating the best sub-tour,
resulting in a non-optimal overall solution.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 53


CS3401 ALGORITHMS UNIT 4 MEC

16. Apply backtracking approach and determine whether the given graph can be colored using
4 colors with graph colouring techniques. Nov/Dec 2024

Steps for Backtracking Approach:

1. Input Representation:
o Represent the graph as an adjacency matrix or adjacency list.
o Each vertex has an index, and edges represent adjacency (connections) between
vertices.
2. Assign Colors:
o Attempt to assign one of 4 colors (e.g., 1, 2, 3, 4) to each vertex.
o Start with vertex 1 and assign a color.
o Move to the next vertex and assign a color that is not used by any of its adjacent
vertices.
3. Backtracking:
o If a vertex cannot be assigned any of the 4 colors due to adjacent vertices already
using them, backtrack to the previous vertex and try a different color.
o Continue this process until all vertices are successfully colored or it is
determined that 4 colors are insufficient.
4. Termination:
o If all vertices are colored, the graph is 4-colorable.
o If not, then 4 colors are insufficient.

Application to the Given Graph:

We can visualize that the graph has 12 vertices connected by edges. To solve it using
backtracking, we need to:

1. Construct the Adjacency Matrix: Based on the image, create a matrix where:
o A[i][j]=1A[i][j] = 1A[i][j]=1 if there is an edge between vertex iii and vertex jjj,
o A[i][j]=0A[i][j] = 0A[i][j]=0 otherwise.
2. Use Backtracking Algorithm:
o Start with vertex 1, assign a color.
o Move sequentially through vertices, checking constraints.
o Backtrack if needed.

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 54


CS3401 ALGORITHMS UNIT 4 MEC

IMPORTANT UNIVERSITY QUESTIONS


PART A
1. Define n-queens problem
2. Give the formal definition of n- queens problem AU : May -08
3. Define sum of subsets problem? AU May -13
4. Describe the sum of subsets problem?
5. Define state space tree? AU : Dec -06
6. Define Hamiltonian cycle and give an example. (May/June 2010)
7. Draw a graph with a cycle but no Hamiltonian cycle. (April/May 2011)
8. Explain briefly branch and bound technique for solving problems. (April 2008)
9. Define the term live node, E-node and dead node. AU : May -10
10. State the principle of backtracking AU : Dec -05,10,11,May -12
11. Explain the idea behind the backtracing
PART –B
1. What is Backtracking problem? Or With an example explain general method solving problem
using backtracking Or Explain elaborately recursive acktracking algorithm Au: Dec-11, May-13
Or Explain the general method of backtracking OrHow do you estimate the efficiency of
backtracking? Au: dec-13
3. Write an algorithm for N queen problemn-Queens Problem ( Au : May -13)
4. Write down and explain the procedure for tackling the 8 queens problem using backtracking
approach ( AU: Dec-11,8,May 12,13,10)
5. Using backtracking enumerate how can you solve the following problem Hamiltonian Circuit
Problem Au: Dec-10 , 08,09,11,May-14
7. Explain Subset-Sum Problemand discuss the possible solution strategies using backtracking
8. Write an algorithm to determine the sum of subsets for a given sum and a set of numbers . draw
the tree representation to solve the subset sum problem given the numbers set as
A = {3, 5, 6, 7 , 2} and with sum = 15 Derive all the subsets Au : Dec -10

9. Let, w = { 5,7,10,12,15,18,20} and m = 35. Find all possible subset of w whose sum is equivalent
to m. draw the portion of state space tree for this problem. Au : Dec – 12
[Link] the following instance of the knapsack problem by the branch and boundAU: Dec-06,08,10
11. Explain how to solve TSP(Traveling Salesman Problem) using branch and bound Au : Dec –

Aprl/May 2024
UNIT-4
PART A
1. Define Hamiltonian circuit problem. [Link] 4 [Link] 26
2. What is branch and bound Travelling salesman problem?(Apr/may 2023, 2024) [Link] 7 [Link] 43

PART B
1. Write an algorithm for N Queen Problem-Queens Problem or elaborate how backtracking
technique can be used to solve n-queue problem. Explain with an example. Au :Nov/Dec 2019
Apr/may 2024 [Link] 10 [Link] 1
2. Solve the following subset sum problem using back tracking. Let S = \{3, 7, 9, 13, 26, 41\} d(sum) =
51 Apr/May 2024 [Link] 52 [Link] 13

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 55


CS3401 ALGORITHMS UNIT 4 MEC

3. Discuss briefly about the general method of branch and Bound approach and state how it differs
from backtracking. Apr/May 2024 [Link] 10 [Link] 1
14. Explain the branching mechanism in the Branch and Bound Strategy to solve 0/1 Knapsack
problem. Apr/May 2024 [Link] 54 [Link] 14

Nov/Dec 2024
PART-A
1. Write the time complexity for solving n-Queens problem. [Link] 53
2. Define optimal binary search. [Link] 20
PART-B
1. Apply backtracking approach and determine whether the given graph can be colored using 4
colors with graph colouring techniques. [Link] 16

2. Consider the following graph. The vertex represents the city and edge represents the cost
between the two vertices. A salesman starts from node1, visit all the cities exactly once and return
to the starting node. Justify that the algorithm that uses optimality principle produces an optimal
tour cost to visit all cities. [Link] 15

PREPARED BY: [Link] ASP/CSE, [Link] AP/CSE , [Link] PRIYA ,AP/CSE 56


CS3401 ALGORITHMS UNIT 5 MEC

UNIT V
NP-COMPLETE AND APPROXIMATION ALGORITHM
Tractable and intractable problems: Polynomial time algorithms – Venn diagram
representation - NP algorithms - NP-hardness and NP-completeness – Bin Packing
problem - Problem reduction: TSP – 3- CNF problem. Approximation Algorithms: TSP -
Randomized Algorithms: concept and application - primality testing - randomized quick
sort - Finding kth smallest number

PART-A
1. What are NP- hard and NP-complete problems?
The problems whose solutions have computing times are bounded by polynomials of
small degree.

2. Define bounding.
Branch-and-bound method searches a state space tree using any search
mechanism in which all children of the E-node are generated before another node
becomes the E-node.
Each answer node x has a cost c(x) and we have to find a minimum-cost answer
node. Common strategies include LC, FIFO, and LIFO.
Use a cost function ˆc(·) such that ˆc(x)  c(x) provides lower bound on the
solution obtainable from any node x.

3. List example of NP hard problem.


NP hard graph problem
clique decision problem(CDP) Node cover decision problem(NCDP).

4. What is meant by NP hard and NP complete problem? (NOV/DEC 2011&NOV/DEC


2012, NOV/DEC 2024)
NP-Hard Problem: A problem L is NP-hard if any only if satisfy ability reduces to
L. NP- Complete: A problem L is NP-complete if and only if L is NP-hard and L є NP.
There are NP-hard problems that are not NP-complete. Halting problem is NP-
hard decision problem, but it is not NP-complete.

5. An NP-hard problem can be solved in deterministic polynomial time, how?


(NOV/DEC 2012)
If there is a polynomial algorithm for any NP-hard problem, then there are
polynomial algorithms for all problems in NP, and hence P = NP.
If P ≠ NP, then NP-hard problems cannot be solved in polynomial time, while P =
NP does not resolve whether the NP-hard problems can be solved in polynomial time.

6. How NP-Hard problems are different from NP-Complete?


These are the problems that are even harder than the NP-complete problems.
Note that NP-hard problems do not have to be in NP, and they do not have to be
decision problems.
The precise definition here is that a problem X is NP-hard, if there is an Complete
problem Y, such that Y is reducible to X in polynomial time. But since any NP-complete
problem can be reduced to any other NP-complete problem in polynomial time, all NP-
complete problems can be reduced to any NP-hard problem in polynomial time.

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


1
CS3401 ALGORITHMS UNIT 5 MEC

Then, if there is a solution to one NP-hard problem in polynomial time, there is a


solution to all NP problems in polynomial time.

7. Define P and NP problems. (APR/MAY 2017)


P- Polynomial time solving. Problems which can be solved in polynomial time,
which take time Like O(n), O(n2), O(n3). Eg: finding maximum element in an array or to
check whether a string is Palindrome or not.
So there are many problems which can be solved in polynomial time.
NP- Non deterministic Polynomial time solving. Problem which can't be solved
in polynomial time like TSP (travelling salesman problem)

8. What are tractable and non-tractable problems? (APR/MAY 2018)

Generally, we think of problems that are solvable by polynomial time algorithms


as being tractable, and problems that require super polynomial time as being
intractable.

9. Define P and NP problems. (NOV/DEC 2018)


All problems in P can be solved with polynomial time algorithms, whereas all
problems in NP - P are intractable. It is not known whether P = NP. However, many
problems are known in NP with the property that if they belong to P, then it can be
proved that P = NP. If P ≠ NP, there are problems in NP that are neither in P nor in NP-
Complete.
The problem belongs to class P if it’s easy to find a solution for the problem. The
problem belongs to NP, if it’s easy to check a solution that may have been very tedious
to find.

10. Define NP completeness and NP hard. (APR/MAY 2019)


NP-complete problems are the hardest problems in NP set. A decision problem L is
NPcomplete if:
1) L is in NP (Any given solution for NP-complete problems can be verified quickly,
but there is no efficient known solution).
2) Every problem in NP is reducible to L in polynomial time (Reduction is defined
below). A problem is NP-Hard if it follows property 2 mentioned above, doesn’t need to
follow property 1. Therefore, NP-Complete set is also a subset of NP-Hard set.

11. What do you meant by primality testing?

The basic structure of randomized primality tests is as follows:


 Randomly pick a number a.
 Check equality (corresponding to the chosen test) involving a and the given number
n. If the equality fails to hold true, then n is a composite number and a is a witness for
the compositeness, and the test stops.
 Get back to the step one until the required accuracy is reached. After one or more
iterations, if n is not found to be a composite number, then it can be declared probably
prime.

12. What is Kth smallest number?

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


2
CS3401 ALGORITHMS UNIT 5 MEC

Given an array and a number k where k is smaller than the size of the array, we
need to find the k’th smallest element in the given array. It is given that all array
elements are distinct.

13. How quick sort using random pivoting?


In Quicksort we first partition the array in place such that all elements to the left
of the pivot element are smaller, while all elements to the right of the pivot are greater
than the pivot. Then we recursively call the same procedure for left and right subarrays.
Unlike merge sort, we don’t need to merge the two sorted arrays. Thus
Quicksort requires lesser auxiliary space than Merge Sort, which is why it is often
preferred to Merge Sort. Using a randomly generated pivot we can further improve the
time complexity of Quicksort.

14. Define NP Hard and NP Completeness (Nov/Dec 2010) Apr/May 2019


NP Hard
A problem L is NP hard if and only if, satisfiability reduces to L.
NP complete
A problem L is complete iff L is NP hard and L Є NP
If L Є {0,1} X in NP complete if L Є NP
L is polynomial time readable to L for only LЄ NP

15. What is meant by class p?


It is deterministic in nature .
Solved by conventional computers in polynomial tim.e
It takes the following time complexity
O(1) - constant
O(log n) - sun linear
O(n) - linear
O(n log n) - nearly linear
O(n )2 - quadratic
It takes polynomial upper and lower bound.

16. Differentiate between decision and optimization problem.


Decision Optimization problem.
Computational problem with intended Computational problem where we try to
output of yes or No (i.e.,) 1 or 0 maximize or minimize the objective
function.

17. Define SATISFIABILITY.


Let x1, x2, x3….,xn denotes Boolean variables. Let xi denotes the relation of xi. A literal
is either a variable or its negation.
A formula in the prepositional calculus is an expression that can be constructed using.
Literals and the operators and ^ or v. A clause is a formula with at least one positive
literal. The satiability problem is to determine if a formula is true for some assignment
of truth values to the variables.

18. State the property of NP-Complete problem. (Nov/Dec 2013)

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


3
CS3401 ALGORITHMS UNIT 5 MEC

A problem L is completer if and only if L is NP-hard and L € NP

19. Expalin promising and non promising node. Nov/Dec 2017


Promising Node
A node in a state space tree is said to be promising if it corresponds to a partially
constructed solution that may still lead to a complete solution.
Non – Promising node
A node in a state space tree is said to be non-promising if it corresponds to a partially
constructed solution that would not be able to lead to a complete solution further.

20. Give the purpose of lower bound. AU may 2016


The elements are compared using operator < to make selection.
Branch and bound is an algorithm design technique that uses lower bound comparisons.
Main purpose is to select the best lower bound.
Example: Assignment problem and transportation problem.
21. Give the Advantages and Disadvantages of randomized algorithms.

There are two major advantages of randomized algorithms.


1. These algorithms are simple to implement.
2. These algorithms are many times efficient than traditional algorithms.

However randomized algorithms may have some drawbacks –


1. The small degree of error may be dangerous for some applications.
2. It is not always possible to obtain better results using randomized algorithm

22. Define NP hard and NP completeness.


NP hard: The NP hard problem is a class of problems in computational complexity that
is as hard as the hardest problem in NP. If an NP hard problem can be solved in polynomial
time, then all the NP complete problems can also be solved in polynomial time. For example,
the Sum of Subset problems, traveling Salesman problem are NP hard.
NP completeness: A problem D is called NP-complete if –
i) It belongs to class NP.
ii) Every problem in NP can also be solved in polynomial time.
For example: Finding Hamiltonian path is NP complete.
All the NP complete problems are NP-hard but there are some NP hard problems that
are not known to be NP complete.

[Link] a problem is said to be NP hard? Apr/May2024


A problem A is said to be NP hard if an algorithm for solving problem A can be
translated into the problem which solves NP-problem. NP hard problems are at least
hard as any NP-problem

[Link] the two properties that must be satisfied by a problem L to be NP complete.


Following NP-complete are the two properties that must be satisfied by problem L for
being
i) The problem I must be in NP.
ii) All other problems in NP reduce to it

[Link] is non deterministic polynomial time ?

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


4
CS3401 ALGORITHMS UNIT 5 MEC

The polynomial time means the complexity of algorithm can be expressed in O(nK).
The nondeterministically polynomial time is the time required by the algorithm to
execute which can not be expressed in O(nK).
Example: Travelling salesperson problem, Knapsack problem.

[Link] between NP complete and NP hard. OR How NP-Hard problem are


different from NP complete.
The NP complete has a property that it can be solved in polynomial time if and only if
all other NP complete problems can also be solved in polynomial time.
If an NP-hard problem can be solved in polynomial time, then all the NP complete
problems can be solved in polynomial time.
All the NP complete problems are NP-hard but there are some NP hard problems that
are not known to be NP complete.

[Link] any three problems that have polynomial time algorithms. Justify your answer.

The problems that can be solved in polynomial time are called P- class problems.
For example -

1. Binary search - In searching an element using binary search method, the list is
simply divided at the mid and either left or right sub list is searched for key element.
This process is carried out in O(logn).

2. Evaluation of polynomial - In a polynomial evaluation we make out the summation


of each term of polynomial. The evaluated result is simply an integer. This process is
carried out in O(n) time.

3. Sorting a list - The elements in a list can be arranged either in ascending or


descending order. This procedure is carried out in O(nlogn) time. This shows that all
the above problems can be solved in polynomial time.

[Link] between "polynomial" and "nondeterministically polynomial".


Polynomial: An algorithm is called polynomial time algorithm (P-class) when for given
input the same output is generated for a function. This is deterministic Algorithm

Non-deterministically polynomial: An algorithm is called non deterministically


polynomial time algorithm (NP class) when for given input there are more than one
paths that the algorithm can follow. Due to this one can not determine which path is to
be followed after a particular stage. All the NP class problems are basically non
deterministic.

Examples of polynomial time algorithms: Binary search, bubble sort.


Examples of non-deterministic algorithm: 0/1 Knapsack problem, traveling sales
person problem.

[Link] is the time complexity of all the deterministic search algorithm?


The time complexity of all the deterministic search algorithm is Q(n).
[Link] is the property of NP-complete problem?
A problem D is called NP-Complete if-

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


5
CS3401 ALGORITHMS UNIT 5 MEC

i) It belongs to class NP.

ii) Every problem in NP can also be solved in polynomial time.

[Link] NP-hard problem can be solved in deterministic polynomial time, how?


If the class of NP complete problem under the NP hard problems is solved in
deterministic polynomial time then an NP-hard problem can be solved in polynomial
time. To solve the class of NP complete problems in deterministic polynomial time the
reduction technique is used.

[Link] the proof which says that a problem 'A' is no harder or no easier than
problem 'B'.

Let a be the instance of problem A and B be the instance of problem B.


The reduction technique is applied for transformation from A to B with following
properties:
i) The transformation takes polynomial time.
ii) The answer for a is "Yes" if and only if answer for ẞ is "Yes".

[Link] is exact and approximation algorithm?


Exact algorithm: The exact algorithms are algorithms that always solve the
optimization problem to optimality.
Approximation algorithm: The approximation algorithms are algorithms that are used
to find approximate solutions to optimization problem.

[Link] P and NP problems.


Polynomial problems - An algorithm is called polynomial time algorithm (P-class)
which solves the problem in polynomial time. For example, searching key element.
NP problems - It stands for non-deterministic polynomial time. That means these are
the kind of problems that can be solved in non-deterministic polynomial time for
example - Traveling salesman problem

[Link] are tractable and non-tractable problems? Apr/May2024

Tractable problem: A problem that is solvable by a polynomial-time algorithm is called


tractable problem. For example - sorting a list.
Non-tractable problem: A problem that cannot be solved by a polynomial-time
algorithm is called non-tractable problem. For example - Traveling salesman problem,
Knapsack problem.

[Link] the bin packing problem.

The bin packing algorithm is used to find the most efficient arrangement of
values in a series of finite sized bins. It is an optimization problem in which items of
different sizes must be packed into a finite number of bins or containers, each of a fixed
given capacity, in a way that minimizes the number of bins used. The algorithm is used
in many real-world applications such as loading trucks, meeting weight capacities, and
creating/storing file backups.

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


6
CS3401 ALGORITHMS UNIT 5 MEC

37. List some applications of using randomized algorithm. Nov/Dec 2024

Randomized algorithms are used in a wide range of applications. Here are some key
areas where they are particularly effective:

 Cryptography:
o Secure key generation
o Random number generation for encryption protocols
 Primality Testing:
o Algorithms like the Miller-Rabin test efficiently determine if numbers are
prime.
 Monte Carlo Methods:
o Used in simulations, numerical integration, and optimization by
leveraging random sampling.
 Randomized Data Structures:

Structures like skip lists and treaps achieve good average-case performance
using randomness.

PART-B

[Link] short notes on: Tractable and Non-Tractable problem.

Tractable Problem: A problem that is solvable by a polynomial-time algorithm.


The upper bound is polynomial.
Here are examples of tractable problems (ones with known polynomial-time algorithms):
– Searching an unordered list
– Searching an ordered list
– Sorting a list
– Multiplication of integers (even though there’s a gap)
– Finding a minimum spanning tree in a graph (even though there’s a gap)

Intractable Problem: a problem that cannot be solved by a polynomial-time algorithm. The


lower bound is exponential.
From a computational complexity stance, intractable problems are problems for which there
exist no efficient algorithms to solve them.
Most intractable problems have an algorithm that provides a solution, and that algorithm is the
brute-force search.
This algorithm, however, does not provide an efficient solution and is, therefore, not feasible
for computation with anything more than the smallest input.

Examples
Towers of Hanoi: we can prove that any algorithm that solves this problem must have a worst-
case running time that is at least 2n − 1.
* List all permutations (all possible orderings) of n numbers.

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


7
CS3401 ALGORITHMS UNIT 5 MEC

[Link] the P, NP, and NP Complete problems with suitable example Au: Dec -13 or
Outline the steps to find an approximation algorithm solution to NP-hard optimization
problems using approximation algorithms with an example. Nov/Dec2019,Apr/may
2023

There are two groups in which a problem can be classified.


 The first group consists of the problems that can be solved in polynomial
time are called tractable. For example : searching of an element from the
list O(log n), sorting of elements O( log n).
 The second group consists of problems that can be solved in non-
deterministic polynomial time are called intractable. For example
Knapsack problem O (2 n/2 ) and travelling salesperson problem (O(n 2 2 n
))
Any problem for which answer is either yes or no is called decision problem. The
algorithm for decision problem is called decision algorithm.
Any problem that involves the identification of optimal cost (minimum or maximum) is
called optimization problem. The algorithm for optimization problem is called
optimization algorithm.

There are several reasons for drawing the intractability line in this [Link], the
entries of Table and their discussion imply that we cannot solve arbitrary instances of
intractable problems in a reasonable amount of time unless such instances are very
small.
Second, although there might be a huge difference between the running times in
O(p(n)) for polynomials of drastically different degrees, there are very few useful
polynomial-time algorithms with the degree of a polynomial higher than three. In
addition, polynomials that bound running times of algorithms do not usually have
extremely large coefficients.
Third, polynomial functions possess many convenient properties; in particular, both
the sum and composition of two polynomials are always polynomials too.
Fourth, the choice of this class has led to a development of an extensive theory
called computational complexity, as shown in fig 5.1 which seeks to classify problems
according to their inherent difficulty. And according to this theory, a problem’s
intractability remains the same for all principal models of computations and all
reasonable input-encoding schemes for the problem under consideration.

P and NP Problems Definition of P


Class P is a class of decision problems that can be solved in polynomial time by
(deterministic) algorithms. This class of problems is called polynomial. are called
tractable.(“ P stands for polynomial)
Examples : searching of key elements, sorting of elements , all pair shortest path.

Definition of NPIt stands for “non- deterministic polynomial time “. Note that NP does
not stand for “non-polynomial time” is called intractable.

Examples – Travelling salesperson problem, graph coloring problem, knapsack problem,


Hamiltonian circuit problems
The NP class problems can be further categorized in to NP-complete and NP hard
problems.

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


8
CS3401 ALGORITHMS UNIT 5 MEC

A problem D is called NP – complete if

Computational complexity problems

P- class NP- class

NP- complete NP- hard

FIG:5.1 computational complexity


The problem in question is called the halting problem: given a computer
program and an input to it, determine whether the program will halt on that input or
continue working indefinitely on it.
Proof:
Assume that A is an algorithm that solves the halting problem. That is, for any
program P and input I,

A(P, I ) = 1, if program P halts on input I ;


0, if program P does not halt on input I .

can consider program P as an input to itself and use the output of algorithm A for pair
(P, P) to construct a program Q as follows:

Q(P)= halts, if A(P, P) = 0, i.e., if program P does not halt on input P;


does not halt, if A(P, P) = 1, i.e., if program P halts input P.
Then on substituting Q for P, we obtain

Q(Q)= halts, if A(Q, Q) = 0, i.e., if program Q does not halt on input Q;


does not halt, if A(Q, Q) = 1, i.e., if program Q halts on input Q.

This is a contradiction because neither of the two outcomes for program Q


ispossible, which completes the proof.
There are many important problems, however, for which no polynomial-time
algorithm has been found, Such problems are Hamiltonian circuit problem.
` Determine whether a given graph has a Hamiltonian circuit—a path that starts
and ends at the same vertex and passes through all the other vertices exactly once.
Traveling salesman problem
Find the shortest tour through n cities with known positive integer distances
between them (find the shortest Hamiltonian circuit in a complete graph with positive
integer weights).
Knapsack problem
Find the most valuable subset of n items of given positive integer weights and
values that fit into a knapsack of a given positive integer capacity.

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


9
CS3401 ALGORITHMS UNIT 5 MEC

Partition problem
Given n positive integers, determine whether it is possible to partition them into
two disjoint subsets with the same sum.
Bin-packing problem
Given n items whose sizes are positive rational numbers not larger than 1, put
them into the smallest number of bins of size 1.

Graph-coloring problem
For a given graph, find its chromatic number, which is the smallest number of
colors that need to be assigned to the graph’s vertices so that no two adjacent vertices
are assigned the same color.

Integer linear programming problem


Find the maximum (or minimum) value of a linear function of several integer-
valued variables subject to a finite set of constraints in the form of linear equalities and
inequalities.

Nondeterministic algorithm
A nondeterministic algorithm is a two-stage procedure that takes as its input
an instance I of a decision problem and does the following.

Nondeterministic (“guessing”) stage: An arbitrary string S is generated that can be


thought of as a candidate solution to the given instance I (but may be complete
gibberish as well).

Deterministic
Deterministic (“verification”) stage: A deterministic algorithm takes both I and
S as its input and outputs yes if S represents a solution to instance I. (If S is not a
solution to instance I, the algorithm either returns no or is allowed not to halt at all.)
A nondeterministic algorithm solves a decision problem if and only if for every
yes instance of the problem it returns yes on some execution.
In other words, we require a nondeterministic algorithm to be capable of
“guessing” a solution at least once and to be able to verify its validity.
Finally, a nondeterministic algorithm is said to be nondeterministic polynomial
if the time efficiency of its verification stage is polynomial. Now we can define the class
of NP problems.
In the above given nondeterministic algorithm there are three functions used-
1. Choose – arbitrarily choose one of the element from given input set
2. Fail- indicates the unsuccessful completion
3. Success – indicates successful completion
The algorithm is of non deterministic complexity O(1), when A is not ordered then the
Deterministic search algorithm has a complexity Ω(n)

Class of NP problems Apr-18


Class NP is the class of decision problems that can be solved by nondeterministic
polynomial algorithms. This class of problems is called nondeterministic polynomial.
Most decision problems are in NP. First of all, this class includes all the problems
in P:

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


10
CS3401 ALGORITHMS UNIT 5 MEC

This is true because, if a problem is in P, we can use the deterministic polynomial


time algorithm that solves it in the verification-stage of a nondeterministic algorithm
that simply ignores string S generated in its nondeterministic (“guessing”) stage.
NP also contains the Hamiltonian circuit problem, the partition problem, decision
versions of the traveling salesman, the knapsack, graph coloring, and many hundreds of
other difficult combinatorial optimization problems.
The halting problem, on the other hand, is among the rare examples of decision
problems that are known not to be in NP.
This leads to the most important open question of theoretical computer science:
Is P a proper subset of NP, or are these two classes, in fact, the same? We can put this
symbolically as
P = NP.
Note that P = NP would imply that each of many hundreds of difficult
combinatorial decision problems can be solved by a polynomial-time algorithm,
although computer scientists have failed to find such algorithms despite their persistent
efforts over many years. Moreover, many well-known decision problems are known to
be “NP-complete” (see below), which seems to cast more doubts on the possibility that
P = [Link]-Complete Problems
Informally, an NP-complete problem is a problem in NP that is as difficult as any
other problem in this class because, by definition, any other problem in NP can be
reduced to it in polynomial time (shown symbolically in Figure).
Most of the decision problems are NP complete problems

Fig:5.2 NP problems
As shown in Fig 5.2 Notation of an NP complete problem. Polynomial tie reductions of NP
problems to an NP complete problems are shown by arrows.
A decision problem D1 is said to be polynomially reducible to a decision
problem D2, if there exists a function t that transforms instances of D1 to instances of D2
such that:
1. t maps all yes instances of D1 to yes instances of D2 and all no
instances of D1 to no instances of D2
2. t is computable by a polynomial time algorithm
This definition immediately implies that if a problem D1 is polynomially
reducible to some problem D2 that can be solved in polynomial time, then problem D1
can also be solved in polynomial time (why?).
A decision problem D is said to be NP-complete if:
1. It belongs to class NP
2. every problem in NP is polynomially reducible to D

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


11
CS3401 ALGORITHMS UNIT 5 MEC

For example, we can prove that the Hamiltonian circuit problem is polynomially
reducible to the decision version of the traveling saleman problem.

Proof:Let G be a yes instance of the Hamiltonian circuit problem. Then G has a


Hamiltonian circuit, and its image in G will have length n, making the image a yes
instance of the decision traveling salesman problem.
Conversely, if we have a Hamiltonian circuit of the length not larger than n in G,
then its length must be exactly n (why?) and hence the circuit must be made up of edges
present in G, making the inverse image of the yes instance of the decision traveling
salesman problem be a yes instance of the Hamiltonian circuit problem. This completes
the proof.
The notion of NP-completeness requires, however, polynomial reducibility of all
problems in NP, both known and unknown, to the problem in question. Given the
bewildering variety of decision problems, it is nothing short of amazing that specific
examples of NP-complete problems have been actually found.

Cooks theorem
Nevertheless, this mathematical feat was accomplished independently by
Stephen Cook in the United States and Leonid Levin in the former Soviet Union.2 In his
1971 paper, Cook [Coo71] showed that the so-called CNF-satisfiability problem is
NPcomplete as shown in fig 5.3
The CNF-satisfiability problem deals with boolean expressions.
Each boolean expression can be represented in conjunctive normal form, such as
the following expression involving three boolean variables x1, x2, and x3 and their
negations denoted ¯x1, ¯x2, and ¯x3, respectively:
(x1 ¯x2 ¯x3)&( ¯x1 x2)&( ¯x1¯x2 ¯x3).
The CNF-satisfiability problem asks whether or not one can assign values true
and false to variables of a given boolean expression in its CNF form to make the entire
expression true.
Showing that a decision problem is NP-complete can be done in two steps.
1. First, one needs to show that the problem in question is in NP; i.e., a randomly
generated string can be checked in polynomial time to determine whether or not
it represents a solution to the problem. Typically, this step is easy.
2. The second step is to show that every problem in NP is reducible to the problem
in question in polynomial time.

Fig:5.3 Proving NP completeness by reduction

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


12
CS3401 ALGORITHMS UNIT 5 MEC

[Link] Bin Packing problem with example.


Bin Packing problem involves assigning n items of different weights and bins each of
capacity c to a bin such that number of total used bins is minimized. It may be assumed
that all items have weights smaller than bin capacity as shown in fig 5.4

Fig:5.4 Bin Packing problem

Table of contents:
1. Mathematical Formulation of Bin Packing
2. A brief outline of Approximate Algorithms
3. Lower Bound on Bins
4. Input Order dependent or Online Algorithms
 Next Fit algorithm
 First Fit algorithm
 Best Fit Algorithm
 Worst Fit Algorithm
5. Input Order Independent or Offline Algorithms
 First Fit Decreasing
 Best Fit Decreasing
6. Applications of Bin-Packing Algorithms

Mathematical Formulation of Bin Packing


The Bin-Packing Problem (BPP) can also be described,using the terminology of
knapsack problems, as follows. Given n items and n knapsacks (or bins), with
Wj = weight of item j,
cj = capacity of each bin
assign each item to one bin so that the total weight of the items in each bin does
not exceed c and the number of bins used is a minimum. A possible mathematical
formulation of the problem

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


13
CS3401 ALGORITHMS UNIT 5 MEC

is

We will suppose, as is usual, that the weights Wj are positive integers. Hence, without
loss of generality, we will also assume that c is a positive integer
Wj < c for j belonging to N.
As you can see, we have a broad rule of approximation and not an exact algorithm. Such
algorithms are called NP problems. In fact Bin Packing Problem is a NP-hard problem

A brief outline of Approximate Algorithms


Many optimization problems exist for which there is no known polynomial time
algorithm for its execution.
Approximation algorithms allow for getting a solution close to the (optimal) solution of
an optimization problem in polynomial time.
An algorithm is an α-approximation algorithm for an optimized problem if:
 The algorithm runs in polynomial time
 The algorithm always produces a solution that is within a factor of α of the
optimal solution
For a given problem instance I,
Approximation ratio(α) = Algo(I)/z(I),
where Algo(I) is the algorithm under scrutiny and z(I) is the optimal solution.

Step:1 For the Bin-Packing problem, let us consider bins of size 1

Step:2 Assuming the sizes of the items be {0.5, 0.7, 0.5, 0.2, 0.4, 0.2, 0.5, 0.1, 0.6}.
The most optimal solution (z(I))for this instance I would be

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


14
CS3401 ALGORITHMS UNIT 5 MEC

Lower Bound on Bins


We can always find a lower bound on minimum number of bins required. The lower
bound can be given as :Min no. of bins >= Ceil ((Total Weight) / (Bin Capacity))

Let us now look at the various optimization algorithms for Bin Packing Problem.

Input Order dependent or Online Algorithms


The following 4 algorithms depend on the order of their inputs. They pack the item
given first and then move on to the next input or next item

1) Next Fit algorithm


The simplest approximate approach to the bin packing problem is the Next-Fit (NF)
algorithm which is explained later in this article. The first item is assigned to bin 1.
Items 2,... ,n are then considered by increasing indices : each item is assigned to the
current bin, if it fits; otherwise, it is assigned to a new bin, which becomes the current
one.
Visual Representation
Step:1 Let us consider the same example as used above and bins of size 1

Step:2 Assuming the sizes of the items be {0.5, 0.7, 0.5, 0.2, 0.4, 0.2, 0.5, 0.1, 0.6}.
The minimum number of bins required would be Ceil ((Total Weight) / (Bin Capacity))=
Celi(3.7/1) = 4 bins.
The Next fit solution (NF(I))for this instance I would be-
Considering 0.5 sized item first, we can place it in the first bin

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


15
CS3401 ALGORITHMS UNIT 5 MEC

Step:3 Moving on to the 0.7 sized item, we cannot place it in the first bin. Hence we
place it in a new bin.

Step: 4Moving on to the 0.5 sized item, we cannot place it in the current bin. Hence we
place it in a new bin.

Step:5 Moving on to the 0.2 sized item, we can place it in the current (third bin)

Step:6 Similarly, placing all the other items following the Next-Fit algorithm we get-

Thus we need 6 bins as opposed to the 4 bins of the optimal solution. Thus we can see
that this algorithm is not very efficient.

Analyzing the approximation ratio of Next-Fit algorithm


The time complexity of the algorithm is clearly O(n). It is easy to prove that, for any
instance I of BPP,the solution value NF(I) provided by the algorithm satisfies the bound

2) First Fit algorithm


A better algorithm, First-Fit (FF), considers the items according to increasing indices
and assigns each item to the lowest indexed initialized bin into which it fits; only when
the current item cannot fit into any initialized bin, is a new bin introduced
Visual Representation

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


16
CS3401 ALGORITHMS UNIT 5 MEC

Step:1 Let us consider the same example as used above and bins of size 1

Step:2 Assuming the sizes of the items be {0.5, 0.7, 0.5, 0.2, 0.4, 0.2, 0.5, 0.1, 0.6}.
The minimum number of bins required would be Ceil ((Total Weight) / (Bin Capacity))=
Celi(3.7/1) = 4 bins.
The First fit solution (FF(I))for this instance I would be-
Step: 3 Considering 0.5 sized item first, we can place it in the first bin

Step:4 Moving on to the 0.7 sized item, we cannot place it in the first bin. Hence we
place it in a new bin.

Step:5 Moving on to the 0.5 sized item, we can place it in the first bin.

Step:6 Moving on to the 0.2 sized item, we can place it in the first bin, we check with the
second bin and we can place it there.

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


17
CS3401 ALGORITHMS UNIT 5 MEC

Step:7 Moving on to the 0.4 sized item, we cannot place it in any existing bin. Hence we
place it in a new bin.

Step:8 Similarly, placing all the other items following the First-Fit algorithm we get-

Thus we need 5 bins as opposed to the 4 bins of the optimal solution but is much more
efficient than Next-Fit algorithm.

Analyzing the approximation ratio of Next-Fit algorithm


If FF(I) is the First-fit implementation for I instance and z(I) is the most optimal
solution, then:

It can be seen that the First Fit never uses more than 1.7 * z(I) bins. So First-Fit is better
than Next Fit in terms of upper bound on number of bins.
Complexity
 Worst case time complexity: Θ(n*n)

 Average case time complexity: Θ(n*n)


 Best case time complexity (Can be achieved using Self-balancing Binary
trees): Θ(nlogn)
 Space complexity: Θ(n)
3) Best Fit Algorithm
The next algorithm, Best-Fit (BF), is obtained from FF by assigning the current item to
the feasible bin (if any) having the smallest residual capacity (breaking ties in favor of
the lowest indexed bin).
Simply put, the idea is to places the next item in the tightest spot. That is, put it in the
bin so that the smallest empty space is left.
Visual Representation

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


18
CS3401 ALGORITHMS UNIT 5 MEC

Step:1 Let us consider the same example as used above and bins of size 1

Step:2 Assuming the sizes of the items be {0.5, 0.7, 0.5, 0.2, 0.4, 0.2, 0.5, 0.1, 0.6}.
The minimum number of bins required would be Ceil ((Total Weight) / (Bin Capacity))=
Ceil(3.7/1) = 4 bins.
The First fit solution (FF(I))for this instance I would be-
Step:2 Considering 0.5 sized item first, we can place it in the first bin

Moving on to the 0.7 sized item, we cannot place it in the first bin. Henc e we place it in a
new bin.

Step:3 Moving on to the 0.5 sized item, we can place it in the first bin tightly.

Step:4 Moving on to the 0.2 sized item, we cannot place it in the first bin but we can
place it in second bin tightly.

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


19
CS3401 ALGORITHMS UNIT 5 MEC

Step:5 Moving on to the 0.4 sized item, we cannot place it in any existing bin. Hence we
place it in a new bin.

Step:6 Similarly, placing all the other items following the First-Fit algorithm we get-

Thus we need 5 bins as opposed to the 4 bins of the optimal solution but is much more
efficient than Next-Fit algorithm.

Analyzing the approximation ratio of Best-Fit algorithm


It can be noted that Best-Fit (BF), is obtained from FF by assigning the current item to
the feasible bin (if any) having the smallest residual capacity (breaking ties in favour of
the lowest indexed bin). BF satisfies the same worst-case bounds as FF

Analysis Of upper-bound of Best-Fit algorithm


If z(I) is the optimal number of bins, then Best Fit never uses more than 2 * z(I)-2 bins.
So Best Fit is same as Next Fit in terms of upper bound on number of bins
 Worst case time complexity: Θ(n*n)

 Average case time complexity: Θ(n*n)


 Best case time complexity (Can be achieved using Self-balancing Binary
trees): Θ(nlogn)
 Space complexity: Θ(n)
4) Worst Fit Algorithm

This algorithm involves an idea to places the next item in the least tight spot to even out
the bins. In other words, put it in the bin so that most empty space is left.
Analysis Of upper-bound of Worst-Fit algorithm
Worst Fit can also be implemented in O(n Log n) time using Self-Balancing Binary
Search Trees.
If z(I) is the optimal number of bins , then Worst Fit never uses more than 2 * z(I)-2
bins. So Worst Fit is same as Next Fit in terms of upper bound on number of bins.
Applications of Bin-Packing Algorithms
 Loading of containers like trucks.
 Placing data on multiple disks.
 This is used extensively while transporting goods over ships
PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,
20
CS3401 ALGORITHMS UNIT 5 MEC

 Job scheduling.

Input Order Independent or Offline Algorithms


In the offline version, we have all items at our disposal since the start of the execution.
The natural solution is to sort the array from largest to smallest, and then apply the
algorithms discussed henceforth.
NOTE: In the online programs we have given the inputs upfront for simplicity but it can
also work interactively
Let us look at the various offline algorithms

1) First Fit Decreasing


We first sort the array of items in decreasing size by weight and apply first-fit algorithm
as discussed above
Algorithm
 Read the inputs of items

 Sort the array of items in decreasing order by their sizes


 Apply First-Fit algorithm
Visual Representation
Step:1 Let us consider the same example as used above and bins of size 1

Step:2 Assuming the sizes of the items be {0.5, 0.7, 0.5, 0.2, 0.4, 0.2, 0.5, 0.1, 0.6}.
Sorting them we get {0.7, 0.6, 0.5, 0.5, 0.5, 0.4, 0.2, 0.2, 0.1}
The First fit Decreasing solution would be-
We will start with 0.7 and place it in the first bin

Step:3 We then select 0.6 sized item. We cannot place it in bin 1. So, we place it in bin 2

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


21
CS3401 ALGORITHMS UNIT 5 MEC

Step:4 We then select 0.5 sized item. We cannot place it in any existing. So, we place it
in bin 3

Step:5 We then select 0.5 sized item. We can place it in bin 3

Step:6 Doing the same for all items, we get.

Thus only 4 bins are required which is the same as the optimal solution.
C++ Implementation
#include <bits/stdc++.h>
using namespace std;
void swap(double *xp, double *yp)
{
double temp = *xp;
*xp = *yp;
*yp = temp;
}
int firstFit(double size[], int n, int c)
{

int res = 0;
int bin_rem[n];
for (int i = 0; i < n; i++) {
int j;
for (j = 0; j < res; j++) {
if (bin_rem[j] >= size[i]) {
bin_rem[j] = bin_rem[j] - size[i];
break;
}

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


22
CS3401 ALGORITHMS UNIT 5 MEC

}
if (j == res) {
bin_rem[res] = c - size[i];
res++;
}

}
return res;
}
int main()
{
double size[] = {0.5, 0.7, 0.5, 0.2, 0.4, 0.2, 0.5, 0.1, 0.6};
int c = 1;
int n = sizeof(size) / sizeof(size[0]);

for (int i = 0; i < n-1; i++)


for (int j = 0; j < n-i-1; j++)
if (size[j] > size[j+1])
swap(&size[j], &size[j+1]);

cout << "Number of bins required in First Fit Decreasing : "<< nextFit(size, n, c);
return 0;
}

Output: Number of bins required in First Fit Decreasing : 4


Complexity
 Worst case time complexity: Θ(n*n)

 Average case time complexity: Θ(n*n)


 Best case time complexity (Can be achieved using Self-balancing Binary
trees): Θ(nlogn)
 Space complexity: Θ(n)
2) Best Fit Decreasing
We first sort the array of items in decreasing size by weight and apply Best-fit algorithm
as discussed above
Algorithm
 Read the inputs of items

 Sort the array of items in decreasing order by their sizes


 Apply Next-Fit algorithm
Visual Representation

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


23
CS3401 ALGORITHMS UNIT 5 MEC

Step:1 Let us consider the same example as used above and bins of size 1

Step:2Assuming the sizes of the items be {0.5, 0.7, 0.5, 0.2, 0.4, 0.2, 0.5, 0.1, 0.6}.
Sorting them we get {0.7, 0.6, 0.5, 0.5, 0.5, 0.4, 0.2, 0.2, 0.1}
The Best fit Decreasing solution would be-
We will start with 0.7 and place it in the first bin

Step:2We then select 0.6 sized item. We cannot place it in bin 1. So, we place it in bin 2

Step:3 We then select 0.5 sized item. We cannot place it in any existing. So, we place it
in bin 3

Step:4 We then select 0.5 sized item. We can place it in bin 3

Step:5 Doing the same for all items, we get.

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


24
CS3401 ALGORITHMS UNIT 5 MEC

Thus only 4 bins are required which is the same as the optimal solution.

Complexity
 Worst case time complexity: Θ(n*n)
 Average case time complexity: Θ(n*n)
 Best case time complexity (Can be achieved using Self-balancing Binary
trees): Θ(nlogn)
 Space complexity: Θ(n)
Applications of Bin-Packing Algorithms
 Loading of containers like trucks.
 Placing data on multiple disks.
 This is used extensively while transporting goods over ships
 Job scheduling.
[Link] explain about Problem Reduction.
To prove whether particular problem is NP complete or not we use polynomial
reducibility. That means if

The reduction is an important task in NP completeness proofs. This can be illustrated


by Fig. 5.5

FIG:5.5 Problem Reduction


Various types of reductions are
Local replacement - In this reduction A-B by dividing input to A in the form of
components and then these components can be converted to components of B.

Component design - In this reduction A→B by building special component for input B
that enforce properties required by A.

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


25
CS3401 ALGORITHMS UNIT 5 MEC

 If L and L₂ are two problems, then problem L reduces to L which can be denoted
as L L2 if and only if there is a way to solve L by deterministic polynomial time
algorithm using a deterministic algorithm that solves L in polynomial time. It
means that if we have polynomial time algorithm for L, then we can solve L, in
polynomial time.

 We can also state that if

L₁ ∞ L2 and L2∞L3
then L₁ ∞L3

• Two problems P and Q are said polynomially equivalent if and only if P ∞ Q and

Q ∞ P.
If problem P₁ is NP-complete and there is polynomial time reduction of P₁ to P2

then P₂ is NP-complete.

5. Explain NP-completeness.

In this section we will discuss two problems namely Vertex cover and the 3SAT
problem which are actually NP Complete problems. Their proof of NP completeness is
based on reduction technique. That means there are some problems which are already
proved as NP Complete problems and using these problems we will prove that the
vertex cover and the 3SAT problems are NP Complete problems.

Steps for proving NP-complete :

Consider that, we have to prove that B is in NP-

Step 1: Select an NP complete language say A.

Step 2: Construct a function f that maps the members of A to members of B.

Step 3: Show that x is in A if and only if f(x) is in B.

Step 4: Now show that the function f can be computed in polynomial time.

Step 5: This if A is NP complete and it can be reduced to B in polynomial time,

then B comes out to be NP complete.

[Link] The 3-SAT, CNF Problem.

Show that the satisfiability of Boolean formulas in 3-conjunctive normal form (3-
CNF) is NP-complete. Apr/May2024

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


26
CS3401 ALGORITHMS UNIT 5 MEC

The CNF-SAT Problem


Before understanding the 3 SAT problem we will get introduced with the
satisfiability
problem.

1. CNF SAT problem


This problem is based on Boolean formula. The Boolean formula has various Boolean
operations such as OR(+), AND (.) and NOT. There are some notations such as --
>(means implies) and <---> (means if and only if).

A Boolean formula is in Conjuctive Normal Form (CNF) if it is formed as collection of


sub expressions. These sub expressions are called clauses.

For example

This formula evaluates to 1 if b, c, d are 1.

The CNF-SAT is a problem which takes Boolean formula in CNF form and checks
whether any assignment is there to Boolean values so that formula evaluates to 1.

Prove that SAT problem is NP complete

Proof :
1) SAT is NP.

2) Circuit SAT or (C-SAT) reduces to SAT. The reduction function f is as follows-

i) For every input wire add a new variable.

ii) For every output wire add a new variable.

iii) An equation is prepared for each gate.

iv) These sets of equations are separated by values and adding final output
variable at the end.

This transformation can be done in linear time or polynomial time.


For instance -Consider following circuit for C-SAT problem as shown in fig:5.6

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


27
CS3401 ALGORITHMS UNIT 5 MEC

FIG: 5.6 Circuit SAT

This shows that we can reduce arbitrary instance of circuit-SAT problem to a


specialized instance of SAT in polynomial time. And as we know that circuit-SAT is NP
complete and reduction of circuit-SAT to SAT is in polynomial time, we must say that
SAT is also an NP complete problem.

The 3-SAT problem

A 3-SAT problem is a problem which takes a Boolean formula S with each clause
having exactly three literals and check is 5 is satisfied or not.

Prove that 3-SAT is NP complete

Proof: The language: 3-SAT is a restriction of SAT. We replace each clause C that
represents the SAT problem to a function f by family of De of clauses that represent
satisfiability.
For example say
C =a v b v c v d v e
One can simulate this by
Dc = (a v b v x)(x v e v y)^( v d v e)
where x and y are new variables.
Need to verify:
1) If C is FALSE, then Dc is FALSE; and
2) 2) If C is TRUE, then one can make Dc TRUE.
If f is satisfiable then there is assignment where each clause C is TRUE. This can be
extended to make Dc TRUE
Further if f is evaluated to FALSE, then some clauses say C must be FALSE and thus
corresponding family Dc evaluates to FALSE
This conversion process can be done in polynomial time. Thus we have shown that SAT
reduces to 3-SAT in polynomial time. As we know that SAT is a NP complete problem, so
we must say that 3-SAT is also NP complete problem.

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


28
CS3401 ALGORITHMS UNIT 5 MEC

[Link] the Traveling Salesman Problem (TSP).

The traveling salesman problem consists of a salesman and a set of cities. The salesman
has to visit each one of the cities starting from a certain one and returning to the same
city. The challenge of the problem is that the traveling salesman wants to minimize the
total length of the trip

Proof:
 To prove TSP is NP-Complete, first we have to prove that TSP belongs to NP.
 In TSP, we find a tour and check that the tour contains each vertex once. Then
the
 total cost of the edges of the tour is calculated. Finally, we check if the cost is
minimum.
 This can be completed in polynomial time. Thus TSP belongs to NP.

NP Hard Problem

A problem A is NP-hard if there is an NP-complete problem B, such that B is reducible to


A in polynomial time. NP-hard problems are as hard as NP-complete problems. NP-hard
problem need not be in NP class.
 A NP problem such that, if it is in P, then NP P. If a (not necessarily NP) problem
has this same property then it is called "NP-hard". Thus the class of NP-
complete problem is the intersection of the NP and NP-hard classes.

Fig:5.7 NP Hard Problem


Normally the decision problems are NP-complete but optimization problems are NP-
hard as shown in fig:5.7. However if problem L, is a decision problem and L is
optimization problem then it is possible that L La For instance the Knapsack decision
problem can be Knapsack optimization problem.

There are some NP-hard problems that are not NP-complete as shown in fig:5.8 For
example halting problem. The halting problem states that: "Is it possible to determine
whether an algorithm will ever halt or enter in a loop on certain input ?"

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


29
CS3401 ALGORITHMS UNIT 5 MEC

Fig:5.8 NP-hard problems that are not NP-complete

[Link] the Approximation Algorithms.

In this section we will discuss two important issues namely, "What is NP-hard blem?"
and "How approximation algorithms are used for NP-hard problems 7"" us start
our discussion with the understanding of NP-hard problems. In mutational complexity
theory there are different types of problems. Some problems decision problems for
which answer is yes or no, others are search problems and any others are optimization
problems.
The solvability of problems in polynomial time eternally tested by nondeterministic
Turing machines. Hence the complexity class of blems that are intrinsically harder
than those that can be solved by a deterministic Turing machine in polynomial time are
called NP-hard problems. Let have a formal definition of NP-hard.

lf decision version of a combinatorial optimization problem is proved to be NP-


complete then optimization version is NP-hard.
For example: Consider a problem of Hamiltonian cycle. If we want to find
Hamiltonian cycle with length less than k then this is a decision problem and it is a NP
complete problem. Because it is easy to determine the Hamiltonian cycles with length
less than k. But (optimization version) "What is the shortest Hamiltonian cycle 7" is a
NP-hard problem. Because it is not easy to determine if the cycle obtained is shortest or
not.

Approximation algorithm

The approximation algorithms are algorithms used to find approximate


solutions to optimization problems. Approximation algorithms are often associated
with NP-hard problems because it is very difficult to get an efficient polynomial time
exact algorithm for solving NP-hard problems.
Note that approximate algorithms are mainly useful for solving those problems
where exact polynomial algorithms are known but running such algorithm is too
expensive because of their sizes of data sets. The philosophy behind approximation
algorithm is find good solution fast.
That means you won't get the exact solution but you will get the approximate
solution quickly. For approximation algorithm we start with inaccurate data, hence

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


30
CS3401 ALGORITHMS UNIT 5 MEC

optimization may be as good as optimal solution. Hence approximation algorithms are


based on heuristics (ie. proceeding to solution by trial and error).

A heuristics can be defined as a collection of common sense rules defined from


experience.
For example: In travelling salesperson problem going to next nearest city or in
knapsack problem start with highest value and minimum weight item.

Accuracy ratio
It is always necessary to know the accuracy of approximation to the actual optimal
solution. Hence accuracy ratio is defined as

where 5, denotes approximate solution, r(s) denotes accuracy ratio, f(s) is a value of
objective function for solution given by approximation algorithm, f(s) is a value of
objective function. Generally r(s,)>=1. When r(s) reaches close to 1 then is a better
approximate solution

Performance ratio
The best upper bound on accuracy ratio taken over all instances of the problem
is called performance ratio. It is denoted by R. By knowing performance ratio one can
judge quality of approximation algorithm. The approximation algorithms with R value
nearer to 1 is supposed to be a better approximation algorithm.

C-Approximation algorithm

there exists a value c which is >= 1 and r(s) sc for all instances of problem then
algorithm is called c-approximation algorithm. If c value is 1 then corresponding are
good. For a c-approximation algorithm for any instance problem is-

9. Explain the Approximation Algorithms for the Travelling Salesman Problem.


Nov/Dec2024

The travelling salesman problem is based on the idea of obtaining optimum tour hen
travelling between many cities. The decision version of this algorithm belongs to a of
NP-complete problem and optimization version of this algorithm belongs to hard class
of problems. There are two approximation algorithms used for TSP: A hard class of
problem and those are

[Link] neighbor algorithm


[Link]-around-the tree algorithm

Let us discuss each algorithm with some simple example.

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


31
CS3401 ALGORITHMS UNIT 5 MEC

Nearest neighbor algorithm

This algorithm is based on the idea of choosing nearest-neighbor while travelling


mone city to another. This nearest neighbor should be unvisited one. Let see the

1. Start at any arbitrary city.


2. Repeat until all the nodes are visited: Go to the nearest city (the unvisited
one) each time.
3. Return to the starting city.
Let us apply this algorithm on some example as shown in Fig:5.11 a and b Travelling
Salesman Problem

[Link]

dist[ i, j]= dist [ j, i]

The distance from city i to j should be same as distance between city j to i. The
Eudidiean instances satisfy following conditions about the accuracy ratio

where n is total number of cities.


Now let us discuss another approximation algorithm i.e. twice-around-the tree
algorithm. This is a 2-approximation (ie. c-approximation with c to be 2) algorithm for
travelling salesman problem with Euclidean distances.
Algorithm
1. Compute minimum spanning tree from the given graph.
2. Start at any arbitrary city and walk around the tree (i.e. depth first search) and
record nodes visited.
3. Eliminate duplicates from the generated node list

Example: Consider the graph as given below and apply the twice-around-the-tree
algorithm.

Step 1: Now we will obtain the Minimum


Spanning Tree (MST) for given graph.

Step 2: Start from city A and have a DFS walk


around the tree.

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


32
CS3401 ALGORITHMS UNIT 5 MEC

Fig:5.11 a and b Travelling Salesman Problem

Step 3 : Record the visited nodes A B CBD-E-D- B - A. Eliminate duplicates then A-B-C-
D-E-A. This basically gives Hamiltonian circuit.

But the tour obtained is not the optimal tour as shown in fig 5.12

Fig:5.12 optimal tour

In the traveling salesman Problem, a salesman must visits n cities. We can say that salesman
wishes to make a tour or Hamiltonian cycle, visiting each city exactly once and finishing at
the city he starts from. There is a non-negative cost c (i, j) to travel from the city i to city j.

The goal is to find a tour of minimum cost. We assume that every two cities are connected.
Such problems are called Traveling-salesman problem (TSP).

We can model the cities as a complete graph of n vertices, where each vertex represents
a city.

It can be shown that TSP is NPC.

If we assume the cost function c satisfies the triangle inequality, then we can use the
following approximate algorithm.

Triangle inequality

Let u, v, w be any three vertices, we have

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


33
CS3401 ALGORITHMS UNIT 5 MEC

One important observation to develop an approximate solution is if we remove an edge


from H*, the tour becomes a spanning tree.

Approx-TSP (G= (V, E))


{
1. Compute a MST T of G;
2. Select any vertex r is the root of the tree;
3. Let L be the list of vertices visited in a preorder tree walk of T;
4. Return the Hamiltonian cycle H that visits the vertices in the order L;
}

Traveling-salesman Problem

Fig:5.13 Traveling-salesman Problem

Intuitively, Approx-TSP first makes a full walk of MST T, which visits each edge exactly
two times. To create a Hamiltonian cycle from the full walk, it bypasses some vertices
(which corresponds to making a shortcut) as shown in fig 5.13

3CNF SAT
Concept: - In 3CNF SAT, you have at least 3 clauses, and in clauses, you will have almost
3 literals or constants
Such as (X+Y+Z) (X+Y+Z) (X+Y+Z)
You can define as (XvYvZ) ᶺ (XvYvZ) ᶺ (XvYvZ)

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


34
CS3401 ALGORITHMS UNIT 5 MEC

V=OR operator
^ =AND operator
These all the following points need to be considered in 3CNF SAT.
To prove: -
1. Concept of 3CNF SAT
2. SAT≤ρ 3CNF SAT
3. 3CNF≤ρ SAT
4. 3CNF ϵ NPC
1. CONCEPT: - In 3CNF SAT, you have at least 3 clauses, and in clauses, you will
have almost 3 literals or constants.
2. SAT ≤ρ 3CNF SAT:- In which firstly you need to convert a Boolean function
created in SAT into 3CNF either in POS or SOP form within the polynomial time
F=X+YZ
= (X+Y) (X+Z)
= (X+Y+ZZ') (X+YY'+Z)
= (X+Y+Z) (X+Y+Z') (X+Y+Z) (X+Y'+Z)
= (X+Y+Z) (X+Y+Z') (X+Y'+Z)
3. 3CNF ≤p SAT: - From the Boolean Function having three literals we can reduce
the whole function into a shorter one.
F= (X+Y+Z) (X+Y+Z') (X+Y'+Z)
= (X+Y+Z) (X+Y+Z') (X+Y+Z) (X+Y'+Z)
= (X+Y+ZZ') (X+YY'+Z)
= (X+Y) (X+Z)
= X+YZ
4. 3CNF ϵ NPC: - As you know very well, you can get the 3CNF through SAT and
SAT through CIRCUIT SAT that comes from NP.
Proof of NPC:-
1. It shows that you can easily convert a Boolean function of SAT into 3CNF SAT
and satisfied the concept of 3CNF SAT also within polynomial time through
Reduction concept.
2. If you want to verify the output in 3CNF SAT then perform the Reduction and
convert into SAT and CIRCUIT also to check the output
NP-Completeness
A decision problem L is NP-Hard if
L' ≤p L for all L' ϵ NP.
Definition: L is NP-complete if
1. L ϵ NP and
2. L' ≤ p L for some known NP-complete problem L.' Given this formal definition,
the complexity classes are:
P: is the set of decision problems that are solvable in polynomial time.

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


35
CS3401 ALGORITHMS UNIT 5 MEC

NP: is the set of decision problems that can be verified in polynomial time.
NP-Hard: L is NP-hard if for all L' ϵ NP, L' ≤p L. Thus if we can solve L in polynomial
time, we can solve all NP problems in polynomial time.
NP-Complete L is NP-complete if
1. L ϵ NP and
2. L is NP-hard
If any NP-complete problem is solvable in polynomial time, then every NP-Complete
problem is also solvable in polynomial time. Conversely, if we can prove that any NP-
Complete problem cannot be solved in polynomial time, every NP-Complete problem
cannot be solvable in polynomial time.
Reductions
Concept: - If the solution of NPC problem does not exist then the conversion from one
NPC problem to another NPC problem within the polynomial time. For this, you need
the concept of reduction. If a solution of the one NPC problem exists within the
polynomial time, then the rest of the problem can also give the solution in polynomial
time (but it's hard to believe). For this, you need the concept of reduction.
Example: - Suppose there are two problems, A and B. You know that it is impossible to
solve problem A in polynomial time. You want to prove that B cannot be solved in
polynomial time. So you can convert the problem A into problem B in polynomial time.
Example of NP-Complete problem
NP problem: - Suppose a DECISION-BASED problem is provided in which a set of
inputs/high inputs you can get high output.
Criteria to come either in NP-hard or NP-complete.
1. The point to be noted here, the output is already given, and you can verify the
output/solution within the polynomial time but can't produce an
output/solution in polynomial time.
2. Here we need the concept of reduction because when you can't produce an
output of the problem according to the given input then in case you have to use
an emphasis on the concept of reduction in which you can convert one problem
into another problem.
Note1:- If you satisfy both points then your problem comes into the category of NP-
complete class
Note2:- If you satisfy the only 2nd points then your problem comes into the category of
NP-hard class
So according to the given decision-based NP problem as shown in fig:5.14 , you can
decide in the form of yes or no. If, yes then you have to do verify and convert into
another problem via reduction concept. If you are being performed, both then decision-
based NP problems are in NP compete.
Here we will emphasize NPC.

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


36
CS3401 ALGORITHMS UNIT 5 MEC

Fig:5.14 Decision-based NP problem

[Link] the Randomized Algorithms: Concept and Application


In probability theory various "experiments" are carried out. The outcomes or results of
those experiments determine the specific characteristics.

For example: Picking up a card from a deck of 52 cards, tossing coin for five times,
choosing a red ball from an urn containing red and white balls, rolling die four times.
Each possible result of such experiment is called sample point. The set of all sample
points is called sample space. The sample space is denoted by S. The sample space S is
finite set. An event E occurs from sample space. For m sample points there are 2m
possible events.

Probability: The probability of event E is the ratio of E to S. Hence

Probability |E|/|S|

For example: Picking up a card from a deck of 52 cards, tossing coin for five times,
choosing a red ball from an urn containing red and white balls, rolling die four times.
Each possible result of such experiment is called sample point. The set of all sample
points is called sample space. The sample space is denoted by S. The sample space S is
finite set. An event E occurs from sample space. For m sample points there are 2m
possible events.

Probability: The probability of event E is the ratio of E to S. Hence


Probability |E|/|S|

For example: When a coin is tossed, then we may get either head (H) or tail (T).
Suppose, we have tossed four coins together then there are 16 possible outcomes:
HHHH, HHHT HHTH, HHTT, HTHH, HTHT, HTTH, HTTT, THHH, THHT, THTH THIT,
TTHH, TTHT, TTTH, TTTT. For 4 events (HHTH, THTT, TTHH, TTTT), the

probability is 4/16= 1/ 4

Mutual exclusion: Two events A and B are said to be mutually exclusive if they do not
have any common sample point. Hence A cap B= emptyset For example A=(HHTH,
HHTT), B= (HTTT, THHT) are mutually exclusive

Independence: Two events A and B are said to be independent if [A cap B]= P[A] * P[B]

Random variable: The random variable is basically a function that maps elements of

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


37
CS3401 ALGORITHMS UNIT 5 MEC

S to set of real numbers.

For sample point a S the F. (a) denotes the mapping. If F denotes a finite set of elements
then it is called discrete. Thus the random variables can be discrete random variables.

For example - If we pick up four balls from an urn containing red and white balls
then the number of red balls that get selected is F (RRRW) = 3 or (RWWW) = 1 and so
on.

Probability distribution: If F is discrete random variable for sample space S then


probability distribution can be defined for a range of elements \ a_{1}, a_{2} ,...a n \ such
that P [F=a 1 ], P[F = a_{2}] ...,P[F=a n ] . For a probability distribution sum i = 1 to n P *
[F = a_{i}] = 1 .

For example: if we pick up four balls randomly from an Turn containing red and white
balls and F is number of red balls, then F can take on five values 0, 1, 2, 3 and 4 then

Table:5.1 Turn containing red and white balls


0 0 0 0
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1

Here 1 means presence of Red balls and 0 Means absence of red ball from the four balls
that are picked up as shown in table 5.1
Hence probability distribution of F is given by
P [F = 0]=1/16 i.e. no red ball present.

P [F=1] = 4/16 i.e. only one red ball present.

P [F=2] =6/16 i.e. two red balls present from picked up balls

P [F=3]=4/16 i.e. when 3 red balls present from picked ball

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


38
CS3401 ALGORITHMS UNIT 5 MEC

P[F=4] = 1/16 i.e. when 4 red balls are present from the picked balls.

Binomial distribution:
Suppose an experiment is conducted then the result of such experiment can be either
success or failure.

Let, p represents success outcome.

Let n be number trials or experiments. These experiments are called Bernoulli trial.

The sample space S contains 2" sample points. The random variable F has binomial
distribution with (n, p) which can be given by -

Markov inequality: The Markov inequality is given by following formula –

where F is a non-negative random variable whose mean is µ.

Concept of random number generator:

A random number generator or randomizer makes use of randomized algorithms. In


randomized algorithms the decision is always taken based current output.

For a randomized algorithm –

i) Execution time may vary from run to run.

ii) For different inputs there may be different outcomes on each execution.

iii) For same inputs there may be different outcomes on each execution.

Primality Testing

'Deciding whether the given number n is prime or not is a problem of primality testing.
The application of primality testing is cryptology.

Any integer greater than one is said to be prime if it is divisible by 1 or by that number
itself. We consider 1 as non-prime number but 2, 3, 5, 7, 11 and 13 are someprime
numbers. But if a number n is non-prime (or composite) then it must have a divisior
<=[√n] To check whether given number n is prime or not we must check every m
elements from interval 2 to [√n] whether m divides n. If there is no such element which

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


39
CS3401 ALGORITHMS UNIT 5 MEC

divides n then n is prime. Otherwise it is non-prime or composite. The following


algorithm is used for primality testing.

In the above algorithm,we have used

This equation is from Fermat's theorem. Suppose, we want to test if n is prime, then
we can pick random a's in the interval and see whether equality holds or not. If equality
does hold then that means n is not prime. But even if equality holds then we can say
that n is probably prime. Thus from above algorithm may or may not get correct prime
number.

[Link] the Randomized Quick Sort with example.(Apr/May 2023)


The worst case for quick sort depends upon how we select our partition or pivot
element. If an input array is already sorted and if we select the first or a last element as
a pivot for applying the quick sort method then it results in worst case.

• To bring improvement over the quick sort choice of pivot is the key factor. Wecan
have following choices of pivot –

*Use the middle element of the subarray as pivot.

*Use a random element of the array as the pivot.

*Take the median of three elements(first, last or middle) as a pivot

*The quick sort works better in average case but its performance is very poor in worst
case.

*Hence from above mentioned three choices randomization is the better choice to
improve the performance of the quick sort in worst case.

*Let us discuss the algorithm for randomized quick sort –

In above algorithm we invoke the random() function only if the (high-low) The number
5 is not a magic number but it is observed that it gives best results with this value. The
time complexity of above algorithm is O(nlogn)

Finding Kth Smallest Number:

Basic concept: An array of elements and value of K is given where K is smaller than size
of array. Find the Kth smallest element in given array. Note that all the elements of
array are distinct.

The steps to solve the problem are:

Step 1: Sort the input array in ascending order.

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


40
CS3401 ALGORITHMS UNIT 5 MEC

Step 2: Return the (K-1)th index in the sorted array.

For example –

Randomized algorithm:
In this algorithm we apply the quick sort method, but we do not apply the quick sort
method completely, but stop the algorithm at the point when pivot itself is Kth smallest
element. The steps for the algorithm are as follows –

Step 1: Apply quick sort algorithm on the input array


Step 2: During quick sort, select a pivot element randomly from the range of the array
from low to high and move it to it's correct position

Step 3: If index of pivot is equal to K then return the value,

Step 4: Else if the index of pivot is greater than K, then scan for the left subarray
recursively, else scan for the right subarray recursively.

Step 5: Repeat this process until the element at index K is not found.

Complexity: O(N) in average case


O(N2 ) in worst case

Advantages and Disadvantages

There are two major advantages of randomized algorithms.

1. These algorithms are simple to implement.


2. These algorithms are many times efficient than traditional algorithms.

However randomized algorithms may have some drawbacks –

1. The small degree of error may be dangerous for some applications.


2. It is not always possible to obtain better results using randomized algorithm

12. Explain the Polynomial Time Algorithms. April/May 2024,Nov/Dec 2024

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


41
CS3401 ALGORITHMS UNIT 5 MEC

A computational problem instance has an input and an objective it wants to compute on


that input. An algorithm is a procedure to compute the objective function. The
algorithm is said to run in polynomial time, or equivalently the algorithm is a
polynomial time algorithm, if there exists a polynomial p(), and the time taken by the
algorithm to solve every instance of the problems is upper bounded by p(size(input)),
the polynomial evaluated at the size of the input.

To calculate the time taken to solve, we normally evaluate the number of steps the
procedure takes to solve the problem, assuming each step takes unit time. The
definition of step depends on the model of computation, however we will not go into the
details of this, and suffice ourselves with the assumption that an arithmetic step or a
comparison step taken one unit of time. Thus, for instance, if ever an algorithm requires
the addition of two numbers, we will assume this can be done in unit time, irrespective
of the value of the numbers.

The size of the input is more subtle. It consists of two parts.


The first is the number of input points that must be provided for the input. For
example, in a scheduling problem, (1||PCj) say, the input needs to provide the
processing times of all the jobs on the single machine. Thus the number of input points
is n, the number of jobs.
The second is the size of each input value. The size of an input value pj, the
processing time, depends on how this input is encoded. For instance, the value pj,
assume it is an integer, could be stored as a bit-array of pj 1’s, in which case the size is
exactly the value pj, or it can be stored as a binary numeral, as it normally is, in which
case the size is dlog2 pje. The first method is called the unary representation of the
input, the second is the binary representation. The default input mode is assumed to be
the binary representation of the data. The size of a computational problem X is
generally denoted as |X|.

So, coming back to the problem (1||PCj), the size of the input is (n + Pjdlog2 pje)
which is at least n+log2 pmax. An algorithm for (1||PCj) is polynomial time if the
running time is bounded by p(n+log2 pmax), where p() doesn’t depend on the input of
the instance.
An algorithm which runs in time polynomial of the data when the data is
represented in unary is called a pseudo-polynomial time algorithm. Pseudo-polynomial
time algorithms are not polynomial time. An algorithm which runs in time polynomial of
the number of input points, irrespective of the size of the actual data, is called a strongly
polynomial time algorithm. Recall that the algorithm SPT, which orders the jobs in
increasing order of processing time, runs in time O(nlogn) and thus is strongly
polynomial time. (Note that we assume here that the time taken to compare two
numbers is unit even if the numbers are huge).
Polynomial Time Reductions
We have already looked at reduction among scheduling problems. The same can be
extended to any computational problem.
Definition
A computational problem X is polynomial time reducible to a computational problem Y ,
if given an algorithm A for the problem Y with time complexity T(|Y |) (note that T
might or might not be a polynomial), we can solve an instance of X in time (p(|X|) +

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


42
CS3401 ALGORITHMS UNIT 5 MEC

q(|X|)T(|X|)). Note that the input to the time function T is the size of X. We denote this
as X E Y or X ≤P Y .

Most often a polynomial time reduction proceeds in the following manner: given an
instance of X, we come up with an instance of Y , and argue that given the solution to
this instance of Y , one can recover, in polynomial time the solution for the instance of X.
We give an example.
Example Consider the following two problems.
Hamiltonian Circuit Problem (HCP): Given a graph G on n vertices and m edges, is there
a Hamiltonian circuit in G?
Travelling Salesman Problem (TSP): Given a graph G on n vertices (cities) with distance
dij between any two cities i and j, find a tour of minimum distance.
Note that HCP ≤P TSP. To see this, given an instance of HCP, construct the instance of
TSP by defining the distance between vertices i and j as follows – dij = 1 is (i,j) is an
edge, and dij = 2 otherwise. It is now not too hard to see that there is a Hamiltonian path
in the graph iff the optimal tour has cost n.
Why are polynomial time reductions useful? Well, if X ≤P Y , and if we have a
polynomial time algorithm for Y , then we have a polynomial time algorithm for X as
well. Thus, these reductions help in classifying the problems into “easy” and “hard”
problems. Furthermore, if there is a reason to believe that the problem X does not have
any polynomial time algorithm, then Y cannot have a polynomial time algorithm either.

This is where reductions come in most handy. Unfortunately, there is not a single
problem for which we can say there are no polynomial time algorithms. However, as we
see in the next section, there is a classification of problems into “easy” and “hard”
problems with the property that if any of the hard problems have a polynomial time
algorithm, then they all do. This is probably the most compelling reason why computer
scientists believe that there can be no polynomial time algorithm for any of them. Pretty
flaky? That is unfortunately the state of affairs!

P, NP and all that Jazz


Before going into the classification of problems, we comment on two different flavours
of problems we have seen.

Decision vs Optimization Problems


Look at the example above. The problem HCP was a “yes/no” question. It asked whether
a graph has a Hamiltonian cycle or not. The answer can be given in one bit. Such
problems are called decision problems. The problem TSP however was an optimization
problem. It asked for the minimum distance tour. We will concern ourselves with
decision problems in this section (although all problems in this course are optimization
questions).

Every optimization problem X has a decision version X0. For instance, the decision
version of TSP has input the input of TSP and an extra integer B, and the problem is:
”Given an input of TSP, is there a tour of distance at most B?”. Note that this is a decision

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


43
CS3401 ALGORITHMS UNIT 5 MEC

problem. Furthermore, TSP0 ≤P TSP, for if we have an algorithm for the optimization
problem, we can surely solve the decision problem. The converse is always not true.
However, in most cases, the solution to the decision problem also comes up with a
solution which can be used for the optimization problem. Lastly, note that since X0 ≤P X,
X is harder than X0.

The complexity classes


A computational (decision or optimization) problem is said to be in the complexity class
P if there exists an polynomial time algorithm to solve it.

A decision problem X is said to be in NP, if for every “yes” instance there exists a
polynomial sized “certificate” which can be “verified” in polynomial time. Let us
elucidate. A “yes” instance of the problem X is one in which the answer is “yes”. For
instance, a graph which has a Hamiltonian circuit is a yes-instance for HCP. A
“certificate” for the yes-instance is a proof that the instance is indeed a yes-instance. For
instance, given a graph which has a Hamiltonian circuit, the circuit is the certificate.
The size of the certificate is required to be a polynomial in the size of the input. A
“verifier” is nothing but a polynomial time algorithm which takes input the yes-instance
and the certificate. If for each yes-instance there exists a certificate, and if the yes-
instance can be verified in polynomial time in the size of the input, then the problem is
in NP. Since given a candidate Hamiltonian circuit of the graph, we can verify it is
Hamiltonian by simply checking the existence of the various edges claimed by the
circuit, we get that the problem HCP is in NP.

Let us look at another example. Consider the problem COMPOSITE which given an
input a natural number N, outputs yes if the number is composite, and no if the number
is prime. (What is the size of the input?) We claim that COMPOSITE ∈ NP. Why? Because
for every yes-instance, that is, a composite number N, we can give a certificate, the
factors of the numbers, say a and b, and this can be checked to be a proper certificate by
just multiplying the two numbers and seeing if it gives N or not.
The P in P stands for “polynomial time”. However, NP does not stand for “not
polynomial”. It stands for non-deterministic polynomial time. This is because any
problem in NP can be solved in polynomial time of one could “non-deterministically
guess” the polynomial sized certificate for every yes-instance.
We now give a rigorous definition of the classes. Any decision problem X has two
instances, the yes-instance and the no-instance. A certificate or a proof, y, of an instance
is a string of {0,1} which may depend on x. y is said to have polynomial size if |y| =
poly(|x|). A verifier V takes as input an instance, x, and a proof y, and outputs yes or no.

Definition A decision problem X ∈ P, if there exists a polynomial time algorithm A such


that for every yes instance x ∈ X, Ax = yes; and for every no instance x ∈ X, we have Ax =
no.

Definition a polynomial time verifier V such that For every yes instance x ∈ X, there
exists a polysized proof y such that V (x,y) = yes. For every no instance x ∈ X, and for
every proof y, we have V (x,y) = no. What is the relation between the classes P and NP?
Well P ⊆ NP.

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


44
CS3401 ALGORITHMS UNIT 5 MEC

Theorem P ⊆ NP.

Proof. Let X be any decision problem in P (the optimization problems can be handled
similarly). Let A be an algorithm to solve the problem. Let x be a yes-instance of X. As a
verifier, one can just run A on x to check it is a yes-instance. Thus, the certificate is
empty and the verifier is just the algorithm A.
Are all decision problems in NP? All the problems we have looked at so far are in NP.
But that doesn’t mean that all problems lie in this class, or at least are not known to lie
in this class.
Example Let us turn the COMPOSITE problem on its head to get the PRIMES problem:
Given a natural number N, decide whether the number N is a prime number or not. Can
you now find a good certificate for yes-instances? That is, given a prime number N, can
you provide some extra proof that N is a prime which can be verified in polynomial
time? It turns that one can, however the answer is not simple and one needs to use
algebra to give this certificate.
We end this section with the following million dollar exercise.
Exercise Prove or disprove: P = NP.

NP-completeness
It is safe to say that most researchers believe that P 6= NP. However, many researchers
also believe we do not have an inkling of an idea how to solve this problem. What
researchers have done however is they can identify the “hardest” problems in NP.

Definition A problem Y is called NP-hard if any problem X ∈ NP can be reduced to Y ,


that is, X ≤P Y . Y is called NP-complete if Y ∈ NP.
Thus, NP-complete problems are the hardest problems in NP, if one can be solved in
polynomial time then so can all other problems in NP. A priori there is no reason to
believe that there will be any NP-complete problem. However, Stephen Cook from UofT
showed that there is at least one. The problem is CIRCUIT SAT, which asks given a
circuit (made of gates and wires) with n Boolean ({0,1}) inputs and 1 Boolean output, is
there an assignment of the inputs which makes the output 1?
Theorem (S. Cook, 1971). CIRCUIT SAT ∈ NP.

Now comes the beauty of reductions. Given an NP-complete problem Y , if we can


reduce it to another problem Z ∈ NP, then Z also is NP-complete. This is because the
reductions are transitive; X ≤P Y , Y ≤P Z implies X ≤P Z. Therefore when one problem is
proved to be NP-complete, reductions can be used to prove many are. Shortly after
Cook’s paper was published, Richard Karp from Berkeley published the idea of
reductions and gave a list of 21 NP-complete problems. Now there are hundreds of NP-
complete problems.
We now give a list of some useful NP-complete problems which will be useful to
prove NP-completeness of some scheduling problems.

1. HCP and TSP are NP-complete.

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


45
CS3401 ALGORITHMS UNIT 5 MEC

2. Partition: Given n positive integers a1,...,an, with B = Pi ai assumed to be even, is


there a partition of the numbers into two sets S and T such that Pai∈S ai = Paj∈T aj
= B/2.
3. 3-partition: Given 3n numbers a1,...,a3n, and a target B, with B/4 ≤ ai ≤ B/2 and Pai
= nB, can we partition the numbers into n-sets S1,...,Sn each of cardinality 3 such
that each set has numbers summing up to exactly B.

4. Vertex Cover: Given a graph G and a number B, are there at most B vertices such
that each edge in G is adjacent to at least one of these vertices.

In the next lecture or two we will see a list of scheduling problems which are
NPcomplete. The basic schema we will use to prove NP-completeness of a problem X
will be the following:
1. Show X ∈ NP (This is normally easy).

2. Pick a suitable NP-complete problem Y .

3. Reduce Y ≤P X.

To reduce a problem X to Y , normally the following three steps is used.


• Come up with a procedure which for an arbitrary instance x ∈ X gives an instance
y ∈ Y in polynomial time such that

– YES case: If x is a yes-instance of X, then y is a yes-instance of Y .


– NO case: If x is a no-instance of X, then y is a no-instance of Y .

• Thus if Y can solved in polynomial time, one can use the above procedure to
convert an instance of X to an instance of Y , and check if the Y -instance is yes or
no, using the algorithm.

Claim Knapsack problem is NP-complete.


Proof. The decision version of the knapsack problem asks if there exists a feasible
subset of items with profit at least P, where P is an input parameter. We leave checking
if Knapsack in NPas an exercise. We reduce Partition to the knapsack problem. Given an
instance of the partition problem: {a1,...,an} construct a knapsack instance with n items
with item i having weight ai and profit ai as well. Let the capacity of the knapsack be
B/2 and the value of P be B/2 as well.

If the instance of partition is a yes-instance, then the instance of knapsack is also a


yes-instance. This is because the set of numbers which add up to B/2 gives a profit of
B/2 as well. If the instance of partition is a no-instance, then any set of numbers which
add up to at most B/2 must be strictly less. Thus, any feasible solution of the knapsack
instance must have total weight and therefore total profit equaling strictly less than P.
Thus, it is a no-instance of the knapsack problem.

This completes the proof. In fact, it proves the special case of knapsack when weight
of an item equals its profit is also NP-complete.

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


46
CS3401 ALGORITHMS UNIT 5 MEC

13. Illustrate polynomial-time approximation scheme for the sum of subsets


problem Aprl/May 2024

A Polynomial-Time Approximation Scheme (PTAS) for the Subset Sum Problem is


an algorithm that, for any given ε > 0, finds a solution within a factor of (1 - ε) of the
optimal solution in polynomial time for fixed ε.

Subset Sum Problem Definition

Given a set of positive integers S={s1,s2,…,sn}S = \{s_1, s_2, \dots, s_n\}S={s1,s2,…,sn}


and a target sum TTT, the goal is to find a subset whose sum is as close as possible to
TTT without exceeding it.

PTAS for Subset Sum

The PTAS for this problem is based on a technique called list pruning, where we
iteratively maintain a list of potential subset sums while discarding unnecessary
elements to keep computation efficient.

Algorithm Outline

1. Initialization
o Start with an empty subset that sums to 0: L0={0}
2. Iterative List Expansion
o For each element si in the input set S, generate a new list by adding si to
each element in the current list.
o Merge the new sums with the existing list.
3. Pruning Step
o Sort the list of sums in increasing order.
o Remove sums that are "too close" together, meaning if x and y are two
sums and x≤(1+ϵ)x, we remove y.
4. Final Step
o Find the largest value in the pruned list that does not exceed T

Key Properties

 Approximation Guarantee: The final solution is at least (1−ϵ) times the optimal
sum.
 Polynomial Time Complexity: The algorithm runs in O(n/ϵ) time, which is
polynomial for fixed ϵ

Illustration

Consider the set S={3,5,7,10} with T=14 and ϵ=0.1:

1. Start with L0={0}


2. After adding 3: L={0,3}
3. After adding 5: L={0,3,5,8}
4. After adding 7: L={0,3,5,7,8,10,12,15}

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


47
CS3401 ALGORITHMS UNIT 5 MEC

5. After adding 10: L={0,3,5,7,8,10,12,13,15,17,18,22,25}


6. Pruning Step: Remove elements that are too close together.

Final result: Approximate sum close to 14 but within factor (1−ϵ).

Conclusion

This PTAS effectively reduces the number of subset sums considered while ensuring a
near-optimal result. It balances accuracy and efficiency, making it suitable for large
instances where exact algorithms (e.g., dynamic programming) would be too slow.

14. Illustrate the working of Miller-Rabin randomized primality test Aprl/May 2024

The Miller-Rabin Primality Test is a probabilistic algorithm used to check whether a


number nnn is prime. It is based on the properties of modular arithmetic and is widely
used because of its efficiency.

Algorithm Overview

The test is based on Fermat’s Little Theorem, which states that if nnn is prime, then
for any integer aaa,

a(n−1)≡1(mod n)

However, some composite numbers (called Carmichael numbers) also satisfy this
condition for many values of a, so we refine the test using strong pseudoprime
checks.

Steps of the Miller-Rabin Test

1. Express n−1 in the form 2s⊆d:

Find s and d such that:

n−1=2s⋅d

where d is odd.

2. Pick a random base a such that 2≤a≤n−2

3. Compute admod admod n

 If ad≡1mod n, then n might be prime (continue with other bases).


 Otherwise, proceed to the next step.

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


48
CS3401 ALGORITHMS UNIT 5 MEC

4. Perform the squaring test:

 Compute a2r⋅dmod n for r=0 to s−1.


 If for some r, a2r⋅d≡−1mod n then n might be prime.

5. If none of the above conditions hold, n is composite.

 Otherwise, repeat the test for multiple bases to improve confidence.

Illustration with Example

Let’s test if n=37 is prime using a=2

1. Compute n−1=36 as 22⊆9


o Here, s=2 and d=9.
2. Pick a=2 and compute 29 mod 37:

29≡511≡29mod 37

Since 29≠1 continue.


o
3. Compute 218mod 37:

218=(29)2≡292≡4 mod 37

4. Compute 236mod 37:

236=(218)2≡42≡16mod 37

o Since this is not ±1n fails the test for a=2 and should be tested with other
bases.

Since 37 is actually prime, another base (e.g., a=3,5,7) would confirm primality.

April/May 2024

PARTA

1. State the difference between tractable and non-tractable problems. [Link] 35, [Link] 7

2. When is a problem said to be NP- hard? Give an example. [Link] 23, [Link] 5

PART-B

1. Show that if an algorithm makes atmost a constant number of calls to polynomial


time subroutines and performs an additional amount of work that also takes
polynomial time, then it runs in polynomial time. Q.no12

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


49
CS3401 ALGORITHMS UNIT 5 MEC

2. Show that the satisfiability of Boolean formulas in 3-conjunctive normal form (3-
CNF) is NP-complete. Q.no7

3. Illustrate polynomial-time approximation scheme for the sum of subsets problem.


[Link] 13

4. Illustrate the working of Miller-Rabin randomized primality test. [Link] 14

Nov/Dec 2024

PART-A
1. Give example for NP hard and NP complete problem. [Link] 4
2. List some applications of using randomized algorithm. [Link] 37
PART-B
[Link] polynomial time algorithm problems with an example. [Link] 12
[Link] approximation algorithm for travelling salesman problem with suitable
example. [Link] 9

PREPARED BY: Mrs.E,INDRA ASP/CSE,[Link] AP/CSE , [Link] PRIYA ,AP/CSE ,


50
[Link]

Downloaded from [Link]


[Link]

Downloaded from [Link]


[Link]

Downloaded from [Link]


[Link]

Downloaded from [Link]

You might also like