You are on page 1of 106

Geethanjali College of Engineering and Technology

UGC AUTONOMOUS
Cheeryal (V), Keesara (M), Medchal District – 501 301 (T.S)

DESIGN AND ANALYSIS OF ALGORITHMS


COURSE FILE

DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
(2020-21)

Faculty In-charge
Prof. V.Madhusudan
Dr.A.Srilaxmi
Dr.BV Swathi
Mr.D.Venkateswarlu
Mr.Dr. Santhi
HoD-CSE
Dr.A.Srilaxmi
Course file contents
S.No Topic Page. No.
1 Cover Page 3
2 Syllabus copy 4
3 Vision of the Department 5
4 Mission of the Department 5
5 PEOs and POs 6
6 Course objectives and outcomes 7
7 Brief notes on the importance of the course and how it fits into the curriculum 9
8 Prerequisites if any 10
9 Instructional Learning Outcomes 10
10 Course mapping with POs 11

11 Class Time Table 11


12 Individual Time Table 12
13 Lecture schedule with methodology being used/adopted 12
14 Detailed notes 15

15 Additional topics 81

16 University Question papers of previous years 82

17 Question Bank 90

18 Assignment Questions 93

19 Unit wise Quiz Questions and long answer questions 96


20 Tutorial problems 101

21 Known gaps ,if any and inclusion of the same in lecture schedule 152

22 Discussion topics , if any 103

23 References, Journals, websites and E-links if any 103

24 Quality Measurement Sheets 103


A Course End Survey
B Teaching Evaluation
25 Student List 103

26 Group-Wise students list for discussion topic 103


Geethanjali College of Engineering and Technology
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

(Name of the Subject/Lab Course): Design and Analysis of Algorithms


( CODE: 18CS2201 ) Program: UG
Branch: CSE Version No: 1
Year: II Document Number: GCET/CSE
Semester: 2 No. of Pages: 106
Classification status (Unrestricted/Restricted )

Prepared by :
1) Name : Prof. V.Madhusudan, Dr.A.Srilaxmi, Dr.BV.Swathi, D.Venkateswarlu, Dr. Santhi.
2) Sign :
3) Designation : Prof./ Associate Prof/
4) Date : 01.11.2019
Verified by : *For Q.C only
1) Name : 1)Name :
2) Sign : 2) Sign :
3) Desig : 3) Desig :
4) Date : 4) Date :
Approved by (HOD) :
1) Name: Dr.A.Srilaxmi
2) Sign :
3) Date :
GEETHANJALI COLLEGE OF ENGINEERING AND TECHNOLOGY
(Autonomous)
Cheeryal (V), Keesara (M), Medchal Dist. - 501 301, Telangana State

18CS2201 – DESIGN AND ANALYSIS OF ALGORITHMS


L T P/D C
II Year. B.Tech. CSE – II Sem 3 - -/- 3

Prerequisite(s):
16CS2101 MATHEMATICAL FOUNDATIONS OF COMPUTER SCIENCE
16CS2102 DATA STRUCTURES
UNIT-I
INTRODUCTION: Algorithm, Pseudo code for expressing algorithms, Performance analysis - Time
complexity and space complexity, Asymptotic Notations: O notation, Omega notation, Theta notation,
and little oh notation, probabilistic analysis and amortized complexity.

DIVIDE AND CONQUER: General method, applications – binary search, merge sort, quick sort,
Strassen’s matrix multiplication.

UNIT-II
SEARCHING AND TRAVERSAL TECHNIQUES : Efficient non-recursive binary tree traversal
algorithms, spanning trees, graph traversals- BFS and DFS, Connected components, bi-Connected
components, AND/OR graphs, game tree.
Disjoint sets: operations, union and find algorithms.

UNIT-III
GREEDY-METHOD: General method, Applications-Job sequencing with deadlines, 0/1 knapsack
problem, minimum cost spanning tree, single source shortest path problem.

DYNAMIC PROGRAMMING: General method, applications-multistage graphs, matrix chain


multiplication, optimal binary search trees, 0 /1 knapsack problem, travelling sales person problem,
reliability design problem.

UNIT-IV
BACK TRACKING: General method, applications: n-queens problem, sum of sub set problem,
graph coloring problem, Hamiltonian cycles.

BRANCH and BOUND: General method, applications: Job Sequencing with deadlines, travelling
sales person problem, 0 /1 knapsack problem, LC branch and bound, FIFO branch and bound solution

UNIT-V
NP-Hard and NP-Complete problems: Basic concepts, non deterministic algorithms, NP-hard and
NP- complete classes, NP- Hard problems, and Cook’s theorem.

TEXT BOOK(S)
1. Fundamentals of Computer Algorithms Ellis Horowitz, Satraj Sahni and Sanguthevar
Rajasekharan, 2nd Edition, Universities Press, 2009 Reprint.
2. Design and Analysis of Algorithms, Aho, Ullman and Hopcroft, Pearson education, Reprint
2004.

REFERENCE BOOK(S)
1. Introduction to Algorithms, secondediton, T.H.Cormen, C.E.Leiserson, R.L.Rivest, and
C.Stein, PHI Pvt.Ltd/Person Education
2. Introduction to Design and Analysis of Algorithms A strategic approach, R.C.T.Lee,
S.S.Tseng, R.C.Chang and T.Tsai, Mc.Graw Hill
3. Data structures and Algorithm Analysis in C++, Allen Weiss, Second edition, Perason
education
4. Algorithms-Richard Johnson baugh and Marcus Schaefer, Pearson Education
5. Design and Analysis Algorithms-Parag Himanshu Dave, Himanshu Bhalachndra Dave
Publisher: Person
6. Algorithm Design: Foundations, Analysis and Internet examples, M.T.Goodrich and
R.Tomassia, John wiley and sons
3. Vision of the Department
To produce globally competent and socially responsible computer science engineers contributing to
the advancement of engineering and technology which involves creativity and innovation by
providing excellent learning environment with world class facilities.
4. Mission of the CSE Department

1. To be a center of excellence in instruction, innovation in research and scholarship, and service to


the stake holders, the profession, and the public.
2. To prepare graduates to enter a rapidly changing field as a competent computer science engineer.
3. To prepare graduate capable in all phases of software development, possess a firm understanding
of hardware technologies, have the strong mathematical background necessary for scientific
computing, and be sufficiently well versed in general theory to allow growth within the discipline
as it advances.
4. To prepare graduates to assume leadership roles by possessing good communication skills, the
ability to work effectively as team members, and an appreciation for their social and ethical
responsibility in a global setting.

5. PROGRAM EDUCATIONAL OBJECTIVES OF CSE:

PEO-1. To provide graduates with a good foundation in mathematics, sciences and engineering
fundamentals required to solve engineering problems that will facilitate them to find employment
in industry and / or to pursue postgraduate studies with an appreciation for lifelong learning.
PEO-2. To provide graduates with analytical and problem solving skills to design algorithms, other
hardware / software systems, and inculcate professional ethics, inter-personal skills to work in a
multi-cultural team.
PEO-3. To facilitate graduates get familiarized with state of the art software / hardware tools, imbibing
creativity and Innovation that would enable them to develop cutting-edge technologies of multi-
disciplinary nature for societal development.
PROGRAMME OUTCOMES:
1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex engineering
problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
3. Design/development of solutions : Design solutions for complex engineering problems
and design system components or processes that meet the specified needs with
appropriate consideration for the public health and safety, and the cultural, societal, and
environmental considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of
the information to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modeling to complex engineering
activities with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities
relevant to the professional engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and
need for sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or leader
in diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give and receive
clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a
member and leader in a team, to manage projects and in multidisciplinary environments.
12. Life-long learning : Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological change.

PSO (Program Specific Outcome) of CSE:


PSO 1: To identify and define the computing requirements for its solution under given constraints.
PSO 2: To follow the best practices namely SEI-CMM levels and six sigma which varies from time
to time for software development project using open ended programming environment to produce
software deliverables as per customer needs.

6. Course Objectives & Outcomes


Course Objectives
Develop ability to
1). Realize the asymptotic performance of algorithms.

2). Understand the behavior of Greedy strategy, Divide and Conquer approach, Dynamic
Programming, backtracking and branch and bound theory for several problem solving techniques.

3). Understand how the choice of data structures and algorithm design methods impact the
performance of programs.

4). Distinguish deterministic and non-deterministic algorithms and their computational complexities.

Course Outcomes
After completion of the course, student would be able to

CO1. Analyze algorithms and estimate their best-case, worst-case and average-case behaviour in
terms of time and space and execute the same through programming.

CO2. Prove the correctness of an algorithm and check whether the algorithm found is the most
efficient for solving problems employing trees, graphs and disjoint sets.

CO 3. Identify suitable problem solving technique for a given problem and design algorithms using
greedy strategy, divide and conquer approach, and dynamic programming and execute the same
through programming
CO 4. Identify suitable problem solving technique for a given problem and design algorithms using
backtracking, and branch and bound theory and execute the same through programming
CO5. Design deterministic and non-deterministic algorithms for tractable and intractable problems
and categorize them as P Class/ NP Class/ NP-Hard/ NP-complete problems accordingly.
7. Brief Importance of the Course and how it fits into the curriculum
a. What role does this course play within the Program?
• This course strengthens logical thinking and programming capabilities of the students.
• This course provides insight on analyzing various solutions to a given problem to select the
efficient one that is suitable to the context.
b. How is the course unique or different from other courses of the Program?
• This is the course wherein students learn various algorithm design methods which help the
students in developing efficient algorithms for real world problems.
• Algorithms developed by employing standard design methods attract appreciation from
everybody.
c. What essential knowledge or skills should they gain from this experience?
• Students acquire design and analysis capabilities of algorithms.
d. What knowledge or skills from this course will students need to have mastered to perform
well in future classes or later (Higher Education / Jobs)?
• Learning various algorithm design methods.
• Mastering analysis of solutions
e. Why is this course important for students to take?
• To become an efficient and good developer of software.
f. What is/are the prerequisite(s) for this course?
• Knowledge of any basic programming language such as C.
• Data Structures.
• Mathematical Foundations of Computer Science.
g. When students complete this course, what do they need know or be able to do?
• Able to design, validate and analyze the Algorithms.
h. Is there specific knowledge that the students will need to know in the future?
• In future, students have to apply these concepts in designing, validating and analyzing the
solutions for real world problems.
i. Are there certain practical or professional skills that students will need to apply in the future?
• YES. They should survey various algorithms to compare their performances.
j. Five years from now, what do you hope students will remember from this course?
• Designing algorithms using various design strategies.
k. What is it about this course that makes it unique or special?
• It is the only course that facilitates students in analyzing algorithms and appreciating efficient
algorithms developed by others.
l. Why does the program offer this course?
• This program offers student various programming languages to code the solutions for real
world problems.
• Student requires skills of analyzing algorithms in terms of space and time to select an efficient
one suitable for context. This course inculcates the above mentioned skills in students.
m. Why can’t this course be “covered” as a sub-section of another course?
• It cannot be part of any prerequisite courses as it includes important and critical concepts
which require lot of practice to comprehend the concepts.
n. What unique contributions to students’ learning experience does this course make?
• It helps in promoting algorithms developed by them to an even large section of people
confidently.
o. What is the value of taking this course? How exactly does it enrich the program?
• This course plays a vital role in design and analysis of algorithms in the discipline of
Computer Science and Engineering and this course also helps for the student’s professional
career growth in terms of professional career.
This course makes significant contributions to the following program outcomes:
▪ an ability to apply knowledge of mathematics, science, and engineering,
▪ an ability to design and conduct experiments, as well as to analyze and interpret data,
▪ an ability to design a system, component, or process to meet desired needs within
realistic constraints
▪ an ability to identify, formulate, and solve engineering problems,
p. What are the major career options that require this course
▪ Code Developer
▪ Software Product Developer

8. Prerequisites if any
1. C Programming.
2. Data Structures.
3. Mathematical Foundations of Computer Science.
9. Instructional Learning outcomes
Unit-1:
1. Use proper conventions for writing an algorithm.
2. Differentiate different types of Asymptotic notations used for representing complexity of an
algorithm
3. Use asymptotic notation to formulate the time and space requirements of algorithms.
4. Perform probabilistic and amortized analysis for a given algorithm.
5. Apply the technique of divide and conquer to write algorithms for searching, sorting and
matrix multiplication.
Unit-2:
1. Apply disjoint set operations
2. Use union and find algorithms
3. Draw spanning trees
4. Determine connected and bi-connected components
5. Comprehend applications of trees through game trees.
Unit-3:

1. Apply the technique of greedy technique.


2. Write algorithm for sequencing jobs with deadlines using greedy technique.
3. Apply greedy technique to solve 0/1 knapsack problem
4. Apply greedy approach for Single source shortest problem

5. Apply Dynamic programming to solve problems not suitable to be solved by greedy method.
Unit-4:
1. Use backtracking technique.
2. Analyze which problems can be solved using backtracking technique.
3. Write algorithm to solve 8 queens problem using backtracking.
4. Apply backtracking for sum of subsets problem.

Unit-5:
1. Categorize algorithms as NP-Hard and NP-Complete
2. Distinguish deterministic and non deterministic algorithms.
3. Analyze cooks theorem.

10. Course mapping with POs and Mapping Of Course To PEOs


Course PEO’S PO’S & PSO’S Teaching Aids
Design & PEO1, PEO2, PO1,PO2,PO3,PO4,PO6,PO7,PO9,PO10, Chalk &Talk, LCD Projector, Internet, Q&A, Group

Analysis of PEO3 PO11,PO12,PSO1,PSO2 Tasks

Algorithms

Course Outcomes PO’s & PSO’s


1 2 3 4 5 6 7 8 9 10 11 12 PSO1 PSO2
16CS2201.1 Analyze algorithms and estimate their best- 3 3 3 2 - 1 1 - 2 2 1 3 3 1
case, worst-case and average-case behavior in terms of
time and space and execute the same through
programming.
16CS2201.2 Prove the correctness of an algorithm and 3 3 1 3 - 1 1 - 2 2 1 2 3 1
check whether the algorithm found is the most efficient
for solving problems employing trees, graphs and disjoint
sets.
16CS2201.3 Identify suitable problem solving technique for 3 3 1 3 - 1 1 - 2 2 1 3 3 1
a given problem and design algorithms using greedy
strategy, divide and conquer approach, and dynamic
programming and execute the same through programming.
16CS2201.4 Identify suitable problem solving technique 3 2 3 3 2 1 1 - 2 2 1 3 3 1
for a given problem and design algorithms using
backtracking, and branch and bound theory and execute
the same through programming.
16CS2201.5 Design deterministic and non-deterministic 3 2 3 3 2 1 1 - 2 2 1 3 3 1
algorithms for tractable and intractable problems and
categorize them as P Class/ NP Class/ NP-Hard/ NP-
complete problems accordingly.

11. Class Time Table


13. Lecture schedule with methodology being used/adopted

II-CSE
No. of Regular/ Teaching Aids Used Expected
S.No. Topics to be covered Date(s)
Periods Additional LCD/OHP/BB
UNIT-I: Introduction & Divide –and-Conquer
Introduction to subject course
1 1 Regular BB
objectives and outcomes
Algorithm, Pseudo code for
2 1 expressing algorithms. Writing Regular BB
algorithm for sample problems
Performance analysis: Space
complexity
3 1 Regular BB
Time complexity: using variable
count
Time complexity: using
4 1 Regular BB
frequency count table
Asymptotic Notation: Big oh,
BB,OHP/LCD,Q
5 1 Omega, Theta & Little oh Regular
&A
notation
Divide and Conquer: General
BB,OHP/LCD,Q
6 1 Method. Recurrence Relations : Regular
&A,E-Tutorials
Substitution method
BB,OHP/LCD,
Learning By
7 1 Applications: Binary Search Regular practice E-
Tutorials, Internet,
Q&A
Merge sort: Example and
8 1 Regular BB,OHP/LCD
Algorithm
Quick sort: Example Algorithm
9 1 Regular BB
explanation
BB,OHP/LCD,
Analysis of Quick Sort: Best,
10 1 Regular Learning by
Worst and Average cases
Practice
BB,OHP/LCD,
Divide &Conquer Approach to
11 1 Regular Learning by
Matrix Multiplication
Practice
BB,OHP/LCD,
12 1 Strassen’s Matrix multiplication Regular Learning By
practice
BB,
Tutoria
13 1 Solving Recurrence Relations Q&A,Learning by
l-3
Practice
BB,OHP/LCD,
Probabilistic Analysis,
14 1 Regular Learning by
Amortized Complexity
Practice
UNIT-II: Search & Traversal Techniques
Binary Tree Traversals –inorder,
15 1 preorder and Post order Regular BB
Recursive algorithms
Non Recursive Tree Traversal
16 1 Regular BB
algorithms
Simple Union and Find
17 1 Regular BB,OHP/LCD
algorithms
Weighted Union and Collapsing
18 1 Regular BB,OHP/LCD
Find algorithms
BB, Q&A,
Binary Tree traversals, Tutoria
19 1 Learning by
operations on Disjoint sets l-4
practice
20 1 Graphs – BFS and DFS Regular BB,OHP/LCD
BB,OHP/LCD,
Bi connected components -
21 1 Regular Learning By
Articulation Point
practice
BB, Q&A,
Determining articulation points Tutoria
22 1 Learning by
thru dfn() l-5
Practice
BB,OHP/LCD,
Algorithm for Articulation point,
23 1 Regular Learning By
Biconnected components
practice
Disjoint Sets: operations and
24 1 Regular BB,OHP/LCD
representation
UNIT-III: Greedy Method & Dynamic Programming
25 1 Greedy Method: General method Regular BB
Applications: Knapsack
26 1 Problem, minimum cost Regular BB
spanning trees
Prim’s Algorithm, Kruskal’s
27 1 Regular BB
Algorithm
BB,OHP/LCD
single source shortest path
28 1 Regular ,Learning By
problem
practice
29 1 Job Sequencing with deadlines Regular BB
Dynamic Programming: General
30 1 Regular BB
Method Principle of Optimality
BB,OHP/LCD,
Applications- Matrix-chain
31 1 Regular Learning By
multiplication
practice
Optimal Binary Search BB,OHP/LCD,
32 1 Trees(OBST) - Dynamic Regular Learning by
Programming approach Practice
BB,OHP/LCD,
33 1 OBST- Example, Algorithm Regular Learning By
practice
BB, Q&A,
Tutoria
34 1 Matrix-chain multiplication Learning by
l-7
practice
35 1 0/1 knapsack problem Regular BB
BB,OHP/LCD,
36 1 0/1 knapsack problem Regular Learning By
practice
37 1 traveling sales person problem Regular BB
38 1 Reliability design Regular BB
0/1 knapsack problem, BB, Q&A,
Tutoria
39 1 Reliability Design Learning by
l-8
All pairs shortest path problem practice
UNIT –IV :Backtracking & Branch-and-Bound
40 1 Backtracking : General method Regular BB
State space tree – Terminology,
41 1 fixed vs variable tuple size Regular BB
formulation
Recursive & Iterative
42 1 Regular BB
Backtracking algorithm
State space tree BB, Q&A,
Tutoria
43 1 Backtracking approach to Learning by
l-9
problems practice
44 1 Applications- n-queens problem Regular BB
Graph coloring, Hamiltonian
45 1 Regular BB
cycles
46 1 sum of subsets problem Regular BB
Determining Bounding function Tutoria
47 1 BB
Generating state space tree l-10
Branch & Bound: General
48 1 Regular BB
Method
49 1 LC Search: 15- Puzzle Problem Regular BB
Job Sequencing with Deadlines:
50 1 Regular BB
Branch & Bound
Job Sequencing with Deadlines: Tutoria
51 BB
FIFOBB, LIFOBB, LCBB l-11
Applications - 0/1 knapsack
52 1 problem FIFOBB, 0/1 Knapsack Regular BB,OHP/LCD
Problem- LCBB
Traveling Sales Person(TSP)-
53 1 Regular BB
Reduced Cost Matrix
TSP- Optimal Solution thru
54 1 Regular BB
LCBB
Tutoria
55 1 TSP Another example BB
l-12
UNIT-V: NP-Hard and NP-Complete problems
Tractable vs Intractable
56 1 Regular BB,OHP/LCD
problems
57 1 Non Deterministic algorithms Regular BB,OHP/LCD
More examples on Non
58 1 Regular BB,OHP/LCD
Deterministic algorithms
59 1
60 1

14. Detailed notes


UNIT-I
Introduction of Algorithm
Informal Definition:
An Algorithm is any well-defined computational procedure that takes some value or set of values
as Input and produces a set of values or some value as output. Thus algorithm is a sequence of
computational steps that transforms the i/p into the o/p.
Formal Definition:

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


addition, all algorithms should satisfy the following criteria.
• INPUT : Zero or more quantities are externally supplied.
• OUTPUT : At least one quantity is produced.
• DEFINITENESS: Each instruction is clear and unambiguous.
• FINITENESS: If we trace out the instructions of an algorithm, then for all cases, the algorithm
terminates after a finite number of steps.
• EFFECTIVENESS: Every instruction must very basic so that it can be carried out, in principle,
by a person using only pencil & paper.
Issues or study of Algorithm:
1. How to device or design an algorithm: creating and algorithm.
2. How to express an algorithm: definiteness.
3. How to analysis an algorithm: time and space complexity.
4. How to validate an algorithm: fitness.
5. Testing the algorithm: checking for error.
Algorithm Specification:
Algorithm can be described in three ways.
1. Natural language like English: When this is chosen care should be taken, we should ensure that
each & every statement is definite.

2. Graphic representation called flowchart: This method will work well when the algorithm is
small& simple.

3. Pseudo-code Method: In this method, we should typically describe algorithms as program,


which resembles language like Pascal & algol.

Pseudo-Code Conventions:
1. Comments begin with // and continue until the end of line.
2. Blocks are indicated with matching braces {and}.
3. An identifier begins with a letter. The data types of variables are not explicitly declared.
4. Compound data types can be formed with records. Here is an example,
Node. Record
{
data type – 1 data-1;
.
.
.
data type – n data – n; node *
link;
}

Here link is a pointer to the record type node. Individual data items of a record can be
accessed with and period.
5. Assignment of values to variables is done using the assignment statement.
<Variable>:= <expression>;
6. There are two Boolean values TRUE and FALSE.

Logical Operators AND, OR, NOT


Relational Operators <, <=,>,>=, =, !=
7. The following looping statements are employed.
For, while and repeat-until While Loop:
While < condition > do
{
<statement-1>

<statement-n>
}
For Loop:
For variable: = value-1 to value-2 step step do
{
<statement-1>
<statement-n>
}repeat-until:
repeat
<statement-1>

<statemet-
until<condition>
8. A conditional statement has the following forms.
If <condition> then <statement>
If<condition then <statement- > Else
<statement-1>
Case statement:
Case
{
: <condition-1> : <statement-1>
.
: <condition-n> : <statement-n>
: else : <statement-n+1>
}
1. Input and output are done using the instructions read & write.
2. There is only one type of procedure:
Algorithm Name (Parameter lists)
As an example, the following algorithm fields & returns the maximum of „n‟ given numbers:
1. Algorithm Max(A,n)
2. // A is an array of size n
3. {
4. Result := A[1];
5. for I:= 2 to n do
6. if A[I] > Result then
7. Result :=A[I];
8. return Result;
9. }
In this algorithm (named Max), A & n are procedure parameters. Result & I are Local
variables.
Next we present 2 examples to illustrate the process of translation problem into an
algorithm.
Recursive Algorithms:
A Recursive function is a function that is defined in terms of itself.
Similarly, an algorithm is said to be recursive if the same algorithm is invoked in the body.
An algorithm that calls itself is Direct Recursive.
Algorithm „A‟ is said to be Indirect Recursive if it calls another algorithm which in turns calls „A‟.
The Recursive mechanism, are externally powerful, but even more importantly, many times they can
express an otherwise complex process very clearly. Or these reasons we introduce recursion here.
The following 2 examples show how to develop a recursive algorithms.
In the first, we consider the Towers of Hanoi problem, and in the second, we generate all possible
permutations of a list of characters.
1. Towers of Hanoi:

.
.
.

Tower A Tower B Tower C

According to legend, at the time the world was created, there was a diamond tower
(labeled A) with 64 golden disks. The disks were of decreasing size and were stacked on the
tower in decreasing order of size bottom to top. Besides these tower there were two other
diamond towers(labeled B & C) Since the time of creation, Brahman priests have been
attempting to move the disks from tower A to tower B using tower C, for intermediate
storage. As the disks are very heavy, they can be moved only one at a time. In addition, at
no time can a disk be on top of a smaller disk. According to legend, the world will come to an
end when the priest have completed this task. A very elegant solution results from the use of
recursion. Assume that the number of disks is „n‟. To get the largest disk to the bottom of
tower B, we move the remaining „n-1‟ disks to tower C and then move the largest to tower B.
Now we are left with the tasks of moving the disks from tower C to B. To do this, we have tower A
and B available. The fact, that towers B has a disk on it can be ignored as the disks larger than the
disks being moved from tower C and so any disk scan be placed on top of it.
Algorithm:
1. Algorithm TowersofHanoi(n,x,y,z)
2. //Move the top „n‟ disks from tower x to tower y.
3. {
.
.
4.if(n>=1) then
5. {
6. TowersofHanoi(n-1,x,z,y);
7. Write(“move top disk from tower “ X ,”to top of tower “ ,Y);
8. Towersofhanoi(n-1,z,y,x);
9. }
10. }
2. Permutation Generator:
Given a set of n>=1elements, the problem is to print all possible permutations of this set.
For example, if the set is {a,b,c} ,then the set of permutation is,
{ (a,b,c),(a,c,b),(b,a,c),(b,c,a),(c,a,b),(c,b,a)}
It is easy to see that given „n‟ elements there are n! different permutations.
A simple algorithm can be obtained by looking at the case of 4 statement(a,b,c,d) The
Answer can be constructed by writing
1. a followed by all the permutations of (b,c,d)
2. b followed by all the permutations of(a,c,d)
3. c followed by all the permutations of (a,b,d)
4. d followed by all the permutations of (a,b,c)
Algorithm:
Algorithm perm(a,k,n)
{ if(k=n) then write (a[1:n]); // output permutation
else //a[k:n] a has more than one permutation// Generate this recursively.
for I:=k to n do
{t:=a[k]; a[k]:=a[I];a[I]:=t; perm(a,k+1,n); //all permutation of a[k+1:n] t:=a[k];
a[k]:=a[I]; a[I]:=t;
}}
Performance Analysis:

1. Space Complexity:The space complexity of an algorithm is the amount of money it needs to


run to compilation.
2. Time Complexity:The time complexity of an algorithm is the amount of computer time it
needs to run to compilation.
Space Complexity:
Space Complexity Example:
Algorithm abc(a,b,c)
{
return a+b++*c+(a+b-c)/(a+b) +4.0;
}
The Space needed by each of these algorithms is seen to be the sum of the following component.
1.A fixed part that is independent of the characteristics (eg:number,size)of the inputs and outputs. The
part typically includes the instruction space (ie. Space for the code), space for simple variable and
fixed-size component variables (also called aggregate) space for constants, and so on.
2.
A variable part that consists of the space needed by component variables whose size is
dependent on the particular problem instance being solved, the space needed by referenced
variables (to the extent that is depends on instance characteristics), and the recursion stack
space. The space requirement s(p) of any algorithm p may therefore be written as, S(P) = c+
Sp(Instance characteristics) Where ‘c’ is a constant.
Example 2:
Algorithm sum(a,n)
{
s=0.0;
for I=1 to n do s=
s+a[I]; return s;
}
The problem instances for this algorithm are characterized by n, the number of elements to be
summed. The space needed d by “n‟ is one word, since it is of type integer.

The space needed by “a” a is the space needed by variables of tyepe array of floating
point numbers.
This is atleast „n‟ words, since „a‟ must be large enough to hold the „n‟ elements to be summed.
So,we obtain Ssum(n)>=(n+s) [ n for a[],one each for n,[ a& s]
Time Complexity:
The time T(p) taken by a program P is the sum of the compile time and the run
time(execution time). The compile time does not depend on the instance characteristics. Also
we may assume that a compiled program will be run several times without recompilation .This
rum time is denoted by tp (instance characteristics).The number of steps any problem statement
is assigned depends on the kind of statement. For example, comments 0 step. An assignment
statement which does not involve any calls to other algorithms is assigned 1 step. Iterative
statement such as for, while & repeat-until Control part of the statement.

1. We introduce a variable, count into the program statement to increment count with initial value
0.Statement to increment count by the appropriate amount are introduced into the program.
This is done so that each time a statement in the original program is executes count
is incremented by the step count of that statement.
Algorithm:
Algorithm sum(a,n)
{ s= 0.0;
count = count+1; for
I=1 to n do
{
count =count+1;
s=s+a[I];
count=count+1;
}
count=count+1;
count=count+1;
return s;
}
If the count is zero to start with, then it will be 2n+3 on termination. So each invocation of sum
execute a total of 2n+3 steps.
2. The second method to determine the step count of an algorithm is to build a table in
which we list the total number of steps contributes by each statement. First determine the
number of steps per execution (s/e) of the statement and the total number of times (ie.,
frequency) each statement is executed. By combining these two quantities, the total
contribution of all statements, the
step count for the entire algorithm is obtained.
Statement S/e Frequency Total

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

Total 2n+3

\
AVERAGE –CASE ANALYSIS
Most of the time, average-case analysis are performed under the more or less realistic
assumption that all instances of any given size are equally likely. For sorting problems, it is
simple to assume also that all the elements to be sorted are distinct.
Suppose we have „n‟ distinct elements to sort by insertion and all n! Permutation of these
elements is equally likely. To determine the time taken on a average by the algorithm, we
could add the times required to sort each of the possible permutations, and then divide by n!.
The answer thus obtained. An alternative approach, easier in this case is to analyze directly
the time required by the algorithm, reasoning probabilistically as we proceed. For any 1 ,2
consider the sub array, T[1….i].
The partial rank of T[I] is defined as the position it would occupy if the sub array were sorted.
For Example, the partial rank of T[4] in [3,6,2,5,1,7,4] in 3 because T[1….4] once sorted is [2,3,5,6].
Clearly the partial rank of T[I] does not depend on the order of the element in Sub array T[1…I-1].
Analysis
Best case: This analysis constrains on the input, other than size. Resulting in the fasters possible run
time
Worst case: This analysis constrains on the input, other than size. Resulting in the fasters possible
run time.
Average case: This type of analysis results in average running time over every type of input.
Complexity: Complexity refers to the rate at which the storage time grows as a function of the
problem size
Asymptotic analysis:Expressing the complexity in term of its relationship to know function. This
type analysis is called asymptotic analysis.
Asymptotic notation:
Big ‘oh’: the function f(n)=O(g(n)) iff there exist positive constants c and no such that f(n)≤c*g(n) for
all n, n ≥ no.
Omega: the function f(n)=Ω(g(n)) iff there exist positive constants c and no such that f(n) ≥ c*g(n) for
all n, n ≥ no.
Theta: the function f(n)=ө(g(n)) iff there exist positive constants c1,c2 and no such that c1 g(n) ≤ f(n)
≤ c2 g(n) for all n, n ≥ no.

SOLVING RECURRENCES :-( Happen again (or) repeatedly)


The indispensable last step when analyzing an algorithm is often to solve a
recurrence equation. With a little experience and intention, most recurrence can be
solved by intelligent guesswork. However, there exists a powerful technique that can be
used to solve certain classes of recurrence almost automatically. This is a main topic of this
section the technique of the characteristic equation.
1. Intelligent guess work:
Then this form is correct. Consider the following recurrence,
0 if n=0
T(n) = 3T(n /2)+n otherwise

It is tempting to restrict n to being ever since in that case n = n/2, but recursively
dividing an even no. by 2, may produce an odd no. larger than 1. Therefore, it is a better
idea to restrict n to being an exact power of 2. First, we tabulate the value of the recurrence on
the first few powers of 2.
Probabilistic Analysis
Probabilistic analysis is the use of probability in the analysis of problems.
Example: Suppose that you need to hire a new office assistant. Your previous attempts at hiring have
been unsuccessful, and you decide to use an employment agency. The employment agency sends you
one candidate each day. You interview that person and then decide either to hire that person or not.
You must pay the employment agency a small fee to interview an applicant. To actually hire an
applicant is more costly, however, since you must fire your current office assistant and pay a
substantial hiring fee to the employment agency. You are committed to having, at all times, the best
possible person for the job. Therefore, you decide that, after interviewing each applicant, if that
applicant is better qualified than the current office assistant, you will fire the current office assistant
and hire the new applicant. You are willing to pay the resulting price of this strategy, but you wish to
estimate what that price will be.
HIRE –ASSISTANT (n)
// It assumes that the candidates for the office assistant job are numbered 1 through n.
// The procedure assumes that you are able to, after interviewing candidate i, determine whether //
candidate I is the best candidate you have seen so far.
{
best :=0; //candidate 0 is a least-qualified dummy candidate
for i:=1 to n do
{
interview candidate I;
if (candidate I is better than candidate best) then
{ best:=i;
hire candidate i;
}
}
}
The cost model for this problem differs from the other problems. We focus not on the running time of
HIRE-ASSISTANT, but instead on the costs incurred by interviewing and hiring.
Interviewing has a low cost, say ci, whereas hiring is expensive, costing ch. Letting m be the number
of people hired, the total cost associated with this algorithm is O(ci n + chm). No matter how many
people we hire, we always interview n candidates and thus always incur the cost ci n associated with
interviewing. We therefore concentrate on analyzing chm, the hiring cost. This quantity varies with
each run of the algorithm.
Analysis of the hiring problem using indicator random variables
We now wish to compute the expected number of times that we hire a new office assistant. In order to
use a probabilistic analysis, we assume that the candidates arrive in a random order. Let X is the
random variable whose value equals the number of times we hire a new office assistant.

but this calculation would be cumbersome. We shall instead use indicator random
variables to greatly simplify the calculation.

To use indicator random variables, instead of computing E[X], by defining one variable associated
with the number of times we hire a new office assistant, we define n variables related to whether or
not each particular candidate is hired. In particular, we let Xi be the indicator random variable
associated with the event in which the ith candidate is hired. Thus,
Xi = I { candidate I is hired}
=
and X= X1+X2+ …. +Xn.
E[Xi]= Pr{candidate I is hired } =1/i;
Now, we compute E[X]
E[X] = = = = ln n+O(1)
Even though we interview n people, we actually hire only approximately ln n of them, on average.
Assuming that the candidates are presented in a random order, algorithm HIRE-ASSISTANT has an
average-case total hiring cost of O(ch ln n).
Amortized analysis:
It is a method of analyzing algorithms that considers the entire sequence of operations of the
program. It allows for the establishment of a worst-case bound for the performance of an algorithm
irrespective of the inputs by looking at all of the operations. This analysis is most commonly
discussed using Big O Notation. At the heart of the method is the idea that while certain operations
may be extremely costly in resources, they cannot occur at a high enough frequency to weigh down
the entire program because the number of less costly operations will far outnumbers the costly ones
in the long run, "paying back" the program over a number of iterations. It is particularly useful
because it guarantees worst-case performance rather than making assumptions about the state of the
program.
There are generally three methods for performing amortized analysis:
▪ The aggregate method
▪ The accounting method, and
▪ The potential method.

All of these give the same answers, and their usage difference is primarily circumstantial and due to
individual preference. Aggregate analysis determines the upper bound T(n) on the total cost of a
sequence of n operations, then calculates the amortized cost to be T(n) / n.
The accounting method determines the individual cost of each operation, combining its immediate
execution time and its influence on the running time of future operations. Usually, many short-running
operations accumulate a "debt" of unfavorable state in small increments, while rare long-running
operations decrease it drastically.
The potential method is like the accounting method, but overcharges operations early to
compensate for undercharges later.

DIVIDE AND CONQUER:


General method:Given a function to compute on ‘n’ inputs the divide-and-conquer strategy suggests
splitting the inputs into ‘k’ distinct subsets, 1<k<=n, yielding ‘k’ sub problems.These sub problems
must be solved, and then a method must be found to combine sub solutions into a solution of the
whole.If the sub problems are still relatively large, then the divide-and-conquer strategy can possibly
be reapplied.Often the sub problems resulting from a divide-and-conquer design are of the same type
as the original problem.For those cases the re application of the divide-and-conquer principle is
naturally expressed by a recursive algorithm.
• DAndC (Algorithm) is initially invoked as DAndC(P), where ‘p’ is the problem to be
solved.
• Small (P) is a Boolean-valued function that determines whether the i/p size is small
enough that the answer can be computed without splitting.
• If this so, the function ‘S’ is invoked.
• Otherwise, the problem P is divided into smaller sub problems.
• These sub problems P1, P2 …Pk are solved by recursive application of DAndC.
• Combine is a function that determines the solution to p using the solutions to the ‘k’
sub problems.
• If the size of p is n and the sizes of the ‘k’ sub problems are n1, n2 ….nk, respectively,
then the computing time of DAndC is described by the recurrence relation.

T(n) = g(n) n small


T(n1)+T(n2)+……………+T(nk)+f(n); otherwise.

Where T(n) : is the time for DAndC on any i/p of size ‘n’.
g(n) : is the time of compute the answer directly for small i/ps.
f(n) : is the time for dividing P & combining the solution tosub problems.
Algorithm DAndC(P) {
if small(P) then return S(P);
else {
divide P into smaller instances
P1, P2… Pk, k>=1;
5. Apply DAndC to each of these sub problems;
6. return combine (DAndC(P1), DAndC(P2),…….,DAndC(Pk));
7. } }
The complexity of many divide-and-conquer algorithms is given by recurrences of the form

T(n) = T(1) n=1


aT(n/b)+f(n) n>1
Where a & b are known constants. We assume that T(1) is known & „n‟ is a power of b(i.e.,
k
n=b )

One of the methods for solving any such recurrence relation is called the substitution method. This
method repeatedly makes substitution for each occurrence of the function. T is the Right-hand side
until all such occurrences disappear.
Example:
1) Consider the case in which a=2 and b=2. Let T(1)=2 & f(n)=n. We have,
T(n) = 2T(n/2)+n
= 2[2T(n/2/2)+n/2]+n
= [4T(n/4)+n]+n
= 4T(n/4)+2n
= 4[2T(n/4/2)+n/4]+2n
= 4[2T(n/8)+n/4]+2n
= 8T(n/8)+n+2n
= 8T(n/8)+3n
*
*
*
Let n=2k, then k=log n
= 2n T(n/n) +k n
= n. T(1) + n log n = 2n + n log n = O(n log n)

BINARY SEARCH
1. Algorithm BinSrch(a,i,l,x)
a. // Given an array a[i:l] of elements in non-decreasing
b. //order, i≤ i ≤ l,determine whether „x‟ is present and
c. // if so, return „j‟ such that x=a[j]; else return 0.
2. {
3. if(l=i) then
4. {
a. if (x=a[i]) then return I;
b. else return 0;
5. }
6. else
7. { mid= [(i+l)/2];
8. if(x=a[mid]) then return id;
9. else if( x< a[mid]) then
10. return BinSrch(a,i,mid-1,x);
11. else return BinSrch(a,mid+1,l,x);
12. }
13. }
Algorithm, describes this binary search method, where Binsrch has 4 i/ps a[], i , l & x.
It is initially invoked as Binsrch (a,1,n,x)
A non-recursive version of Binsrch is given below.
1. Algorithm BinSearch(a,n,x)
2. // Given an array a[1:n] of elements in non-decreasing
3. //order, n>=0,determine whether „x‟ is present and
4. // if so, return „j‟ such that x=a[j]; else return 0.
5. {
6. low:=1; high:=n;
7. while (low<=high) do
8. {
9. mid:=[(low+high)/2];
10. if (x<a[mid]) then high;
11. else if(x>a[mid]) then
i. low=mid+1;
12. else return mid;
13. }
14. return 0;
15. }

Iterative Binary Search


This Binsearch has 3 i/ps a,n, & x.
The while loop continues processing as long as there are more elements left to check.
At the conclusion of the procedure 0 is returned if x is not present, or ‘j’ is returned, such that
a[j]=x.We observe that low & high are integer variables such that each time through the loop
either x is found or low is increased by at least one or high is decreased at least one. Thus we have
2 sequences of integers approaching each other and eventually low becomes > than high & causes
termination in a finite no. of steps if x-is not present.

Example:
1) Let us select the 14 entries. -
15,6,0,7,9,23,54,82,101,112,125,131,142,151.
Place them in a[1:14], and simulate the steps Bin-search goes through as it searches for different
values of ‘x’
Only the variables, low, high & mid need to be traced as we simulate the algorithm.
We try the following values for x: 151, -14 and 9.
for 2 successful searches
1unsuccessful search.

Table. Shows the traces of Bin search on these 3 steps.

X=151 low high mid


1 14 7
8 14 11
12 14 13
14 14 14
Found

x=-14 low high mid


1 14 7
1 6 3
1 2 1
2 2 2
2 1 Not found

x=9 low high mid


1 14 7
1 6 3
4 6 5
Found

Theorem: Algorithm Bin-search(a,n,x) works correctly.


Proof:
We assume that all statements work as expected and that comparisons such as x>a[mid] are
appropriately carried out.
Initially low =1, high= n,n>=0, and a[1]<=a[2]<=……..<=a[n].
If n=0, the while loop is not entered and is returned.

Otherwise we observe that each time thro‟ the loop the possible elements to be checked of or
equality with x and a[low], a[low+1],……..,a[mid],……a[high].If x=a[mid], then the algorithm
terminates successfully. Otherwise, the range is narrowed by either increasing low to (mid+1) or
decreasing high to (mid-1). Clearly, this narrowing of the range does not affect the outcome of the
search. If low becomes > than high, then ‘x’ is not present & hence the loop is exited.

MERGE SORT
As another example divide-and-conquer, we investigate a sorting algorithm that has the nice
property that is the worst case its complexity is O(n log n)
This algorithm is called merge sort
We assume throughout that the elements are to be sorted in non-decreasing order. Given a
sequence of „n‟ elements a[1],…,a[n] the general idea is to imagine then split into 2 sets
a[1],…..,a[n/2] and a[[n/2]+1],….a[n].
Each set is individually sorted, and the resulting sorted sequences are merged to produce a single
sorted sequence of ‘n’ elements.
Thus, we have another ideal example of the divide-and-conquer strategy in which the splitting is
into 2 equal-sized sets & the combining operation is the merging of 2 sorted sets into one.
Algorithm For Merge Sort:
1. Algorithm MergeSort (low,high)
2. //a[low:high] is a global array to be sorted
3. //Small(P) is true if there is only one element
4. //to sort. In this case the list is already sorted.
5. {
6. if (low<high) then //if there are more than one element
7. {
8. //Divide P into subproblems
9. //find where to split the set
10. mid = [(low+high)/2];
11. //solve the subproblems.
12. mergesort (low,mid);
13. mergesort(mid+1,high);
14. //combine the solutions .
15. merge(low,mid,high);
16. } }
Algorithm: Merging 2 sorted subarrays using auxiliary storage.
1. Algorithm merge(low,mid,high)
2. //a[low:high] is a global array containing
3. //two sorted subsets in a[low:mid]
4. //and in a[mid+1:high].The goal is to merge these 2 sets into
5. //a single set residing in a[low:high].b[] is an auxiliary global array.
6. {
7. h=low; I=low; j=mid+1;
8. while ((h<=mid) and (j<=high)) do
9. {
10. if (a[h]<=a[j]) then
11. {
12. b[I]=a[h];
13. h = h+1;
14. }
15. else
16. {
17. b[I]= a[j];
18. j=j+1;
19. }
20. I=I+1;
21. }
22. if (h>mid) then
23. for k=j to high do
24. {
25. b[I]=a[k];
26. I=I+1;
27. }
28. else
29. for k=h to mid do
30. {
31. b[I]=a[k];
32. I=I+1;
33. }
34. for k=low to high do a[k] = b[k];
35. }
Consider the array of 10 elements a[1:10] =(310, 285, 179, 652, 351, 423, 861, 254, 450,
520). Algorithm Merge sort begins by splitting a[] into 2 sub arrays each of size five (a[1:5]
and a[6:10]). The elements in a[1:5] are then split into 2 sub arrays of size 3 (a[1:3] ) and
2(a[4:5]) Then the items in a a[1:3] are split into sub arrays of size 2 a[1:2] & one(a[3:3]) The 2
values in a[1:2} are split to find time into one-element sub arrays, and now the merging begins.(310|
285| 179| 652, 351| 423, 861, 254, 450, 520), Where vertical bars indicate the boundaries of sub
arrays. Elements a[I] and a[2] are merged to yield, (285, 310|179|652, 351| 423, 861, 254, 450, 520)
Then a[3] is merged with a[1:2] and (179, 285, 310| 652, 351| 423, 861, 254, 450, 520) Next,
elements a[4] & a[5] are merged.(179, 285, 310| 351, 652 | 423, 861, 254, 450, 520), And then a[1:3]
& a[4:5] (179, 285, 310, 351, 652| 423, 861, 254, 450, 520), Repeated recursive calls are invoked
producing the following sub arrays. (179, 285, 310, 351, 652| 423| 861| 254| 450, 520), Elements a[6]
&a[7] are merged. Then a[8] is merged with a[6:7],(179, 285, 310, 351, 652| 254,423, 861| 450,
520), Next a[9] &a[10] are merged, and then a[6:8] & a[9:10] (179, 285, 310, 351, 652| 254, 423, 450,
520, 861 )
At this point there are 2 sorted sub arrays & the final merge produces the fully sorted result. (179,
254, 285, 310, 351, 423, 450, 520, 652, 861)

If the time for the merging operations is proportional to n, then the computing time for merge
sort is described by the recurrence relation.
T(n) = a n=1,‟a‟ a constant
2T(n/2)+cn n>1,‟c‟ a constant.
When ‘n’ is a power of 2, n= 2^k, we can solve this equation by successive substitution.
T(n) =2(2T(n/4) +cn/2) +cn
= 4T(n/4)+2cn
= 4(2T(n/8)+cn/4)+2cn
*
*
*
= 2k T(1)+kCn.
= an + cn log n.
It is easy to see that if sk<n<=2k+1, then T(n)<=T(2k+1).
Therefore,
T(n)=O(n log n)

QUICK SORT
1. The divide-and-conquer approach can be used to arrive at an efficient sorting method
different from merge sort.
2. In merge sort, the file a[1:n] was divided at its midpoint into sub arrays which were
independently sorted & later merged.
3. In Quick sort, the division into 2 sub arrays is made so that the sorted sub arrays do not
need to be merged later.
4. This is accomplished by rearranging the elements in a[1:n] such that a[I]<=a[j] for all I
between 1 & n and all j between (m+1) & n for some m, 1<=m<=n.
5. Thus the elements in a[1:m] & a[m+1:n] can be independently sorted.
6. No merge is needed. This rearranging is referred to as partitioning.
7. Function partition of Algorithm accomplishes an in-place partitioning of the elements
of a[m:p-1]
8. It is assumed that a[p]>=a[m] and that a[m] is the partitioning element. If m=1 & p-
1=n, then a[n+1] must be defined and must be greater than or equal to all elements in
a[1:n]
9. The assumption that a[m] is the partition element is merely for convenience, other
choices for the partitioning element than the first item in the set are better in practice.
10. The function interchange (a,I,j) exchanges a[I] with a[j].
Algorithm: Partition the array a[m:p-1] about a[m]
1. Algorithm Partition(a,m,p)
2. //within a[m],a[m+1],…..,a[p-1] the elements
3. // are rearranged in such a manner that if
4. //initially t=a[m],then after completion
5. //a[q]=t for some q between m and
6. //p-1,a[k]<=t for m<=k<q, and
7. //a[k]>=t for q<k<p. q is returned
8. //Set a[p]=infinite.
9. {
10. v=a[m];I=m;j=p;
11. repeat
12. {
13. repeat
14. I=I+1;
15. until(a[I]>=v);
16. repeat
17. j=j-1;
18. until(a[j]<=v);
19. if (I<j) then interchange(a,i.j);
20. }until(I>=j);
21. a[m]=a[j]; a[j]=v;
22. retun j;
23. }
1. Algorithm Interchange(a,I,j)
2. //Exchange a[I] with a[j]
3. {
4. p=a[I];
5. a[I]=a[j];
6. a[j]=p;
7. }

Algorithm: Sorting by Partitioning

1. Algorithm Quicksort(p,q)
2. //Sort the elements a[p],….a[q] which resides
3. //is the global array a[1:n] into ascending
4. //order; a[n+1] is considered to be defined
5. // and must be >= all the elements in a[1:n]
6. {
7. if(p<q) then // If there are more than one element
8. {
9. // divide p into 2 subproblems
10. j=partition(a,p,q+1);
11. //‟j‟ is the position of the partitioning element.
12. //solve the subproblems.
13. quicksort(p,j-1);
14. quicksort(j+1,q);
15. //There is no need for combining solution.
16. }
17. }

STRASSEN’S MATRIX MULTIPLICAION


Let A and B be the 2 n*n Matrix. The product matrix C=AB is calculated by using the formula,
C (i ,j )= A(i,k) B(k,j) for all „i‟ and and j between 1 and n.
The time complexity for the matrix Multiplication is O(n^3)
Divide and conquer method suggest another way to compute the product of n*n matrix.
We assume that N is a power of 2 .In the case N is not a power of 2 ,then enough rows and
columns of zero can be added to both A and B .SO that the resulting dimension are the powers of
two.
If n=2 then the following formula as a computed using a matrix multiplication operation for the
elements of A & B. If n>2,Then the elements are partitioned into sub matrix n/2*n/2..since ‘n’ is a
power of 2 these product can be recursively computed using the same formula .This Algorithm
will continue applying itself to smaller sub matrix until ‘n’ become suitable small(n=2) so that the
product is computed directly .
The formula are
C11 = A11 B11 + A12 B21
C12 = A11 B12 + A12 B22
C21 = A21 B11 + A22 B21
C22 = A21 B12 + A22 B22

• To compute AB using the equation we need to perform 8 multiplication of n/2*n/2 matrix and
from 4 addition of n/2*n/2 matrix. Ci,j are computed using the formula in equation 4 As can
be sum P, Q, R, S, T, U, and V can be computed using 7 Matrix multiplication and 10 addition
or subtraction.
• The Cij are required addition 8 addition or subtraction.
P=(4*4)+(4+4)=64,Q=(4+4)4=32,R=4(4-4)=0, S=4(4-4)=0, T=(4+4)4=32,
U=(4-4)(4+4)=0, V=(4-4)(4+4)=0, C11=(64+0-32+0)=32, C12=0+32=32
C21=32+0=32, C22=64+0-32+0=32
So the answer c(i,j) is 32 32

32 32
since n/2 n/2 &matrix can be can be added in Cn for some constant C, The overall computing time
T(n) of the resulting divide and conquer algorithm is given by the sequence.
T(n)= b n<=2 a &b are
8T(n/2)+cn2 n>2 constant

That is T(n)=O(nlog8)=O(n3)
* Matrix multiplication are more expensive then the matrix addition O(n^3).We can attempt to
reformulate the equation for Cij so as to have fewer multiplication and possibly more addition .
Strassen has discovered a way to compute the Cij of equation (2) using only 7 multiplication
and 18 addition or subtraction. Strassen’s formula are
P= (A11+A12)(B11+B22)
Q= (A12+A22)B11
R= A11(B12-B22)
S= A22(B21-B11)
T= (A11+A12)B22
U= (A21-A11)(B11+B12)
V= (A12-A22)(B21+B22).
C11=P+S-T+V
C12=R+t
C21=Q+T and C22=P+R-Q+V
Time Complexity
T(n)= b n<=2 a &b are
7T(n/2)+an2 n>2 constant

That is T(n)= O(nlog7)=O(n2.81)

UNIT-II
Searching and Traversal Techniques
Traversal: It is a search that necessarily involves the examination of every vertex in the given object
instance. This is applicable only to binary trees.
Searching: It is a search that may not examine all vertices. This is applicable to Graphs.
Techniques for Binary Trees
When traversing a binary tree we want to treat each node and its sub trees in the same fashion. If we
let L, D, R stand for moving left, printing the data, and moving right when at a node then there are six
possible combinations of traversal: LDR, LRD, DLR, DRL, RDL, and RLD. If we adopt the convention
that we traverse left before right then only three traversals remain: LDR, LRD and DLR. To these we
assign the names inorder, postorder and pre-order.
Figure 2.1 Binary Tree
For the above binary tree, its inorder traversal is :FDHGIBEAC
postorder traversal is :FHIGDEBCA
and preorder traversal is :ABDFGHIEC
Recursive algorithms for binary tree traversals:
Treenode = record
{
Type daa; // Type is the data type of data.
Treenode *lchild; treenode * rchild;
}
Inorder:
Algorithm InOrder(t)
// t is abinary tree. Each node of t has three fields:lchild, data, and rchild.
{
if(t≠ 0) then
{
InOrder(t lchild);
Visit(t);
InOrder(t rchild);
}
}
Preorder
Algorithm PreOrder(t)
// t is abinary tree. Each node of t has three fields:lchild, data, and rchild.
{
if(t≠ 0) then
{
Visit(t);
PreOrder(t lchild);

PreOrder(t rchild);
}
}
Postorder
Algorithm PostOrder(t)
// t is abinary tree. Each node of t has three fields:lchild, data, and rchild.
{
if(t≠ 0) then
{
PostOrder(t lchild);
PostOrder(t rchild);
Visit(t);
}
}
Theorem 2.1 Let t(n) and s(n) respectively represent the time and space needed by any one of the
traversal algorithms when the input tree T has n ≥0 nodes. If the time and space needed to visit a node
is ϴ(1) then t(n) = ϴ(n) and s(n) = O(n).
Efficient non-recursive algorithms for binary tree traversals:
Inorder
Algorithm INORDER (T)
// a nonrecursive version using a stack of size m
{
If (T = 0) then return //T empty.
P := T; i:= 0 //P traverses T; i is top of stack.
while(1)
{
while ( P lchild ≠ 0) //traverse left subtree
{
i:=i+l;
if (i > m) then
{
write (“stack overflow”);
exit;
}
STACK[i] := P; P :=P lchild;
}
VISIT(P); //left subtree of P has been traversed
if(i≠0)
{
P := P rchild;
if (P =0) then ; //traverse right subtree
{
P := STACK(i); i :=i – 1;}
}
}
}

Nonrecursive algorithm for inorder traversal


Preorder
Algorithm PREORDER (T)
// a nonrecursive version using a stack of size m
{
If (T = 0) then return //T empty.
P := T; i:= 0 //P traverses T; i is top of stack.
While(1)
{
while ( P lchild ≠ 0) //traverse left subtree
{
i:=i+l;
if (i > m) then
{
write (“stack overflow”);
exit;
}
VISIT(P);
STACK(i) := P; P :=P lchild;
}
if (i≠0)
{
P := p rchild;
if (P =0) then //traverse right subtree
{ P := STACK(i); i :=i – 1} } } }
Nonrecursive algorithm for preorder traversal

Search And Traversal Techniques For Graphs


A fundamental problem concerning graphs is the path problem. In its simplest form it requires us to
determine whether or not there exists a path in the given graph G = (V, E) such that this path starts at
vertex v and ends at u. A more general form would be to determine for a given starting. vertex v
subset of V all vertices u such that there is a path from v to u. This latter problem can be solved by
starting at vertex v and systematically searching the graph G for vertices that can be reached from v.
We have two search methods known as
1. Breadth First Search(BFS),and
2. Depth First Search (DFS).
Breadth First Search and Traversal
In breadth first search we start at a vertex v and mark it as having been reached (visited). The
vertex v will at this time be said to be unexplored. A vertex will be said to have been explored by an
algorithm when the algorithm has visited all vertices adjacent from it. All unvisited vertices adjacent
from v are visited next. These are new unexplored vertices. Vertex v has now been explored. The
newly visited vertices haven't been explored and are put onto the end of a list of unexplored vertices.
The first vertex on this list is the next to be explored. Exploration continues until no unexplored vertex
is left. The list of unexplored vertices operates as a queue and may be represented using any of the
standard queue representations.

Algorithm BFS(v)
A breadth first search of G is carried out beginning a vertex v. For any node i, visited[i]=1 if i
has already been visited. The graph G and array visited[] are global. Visited[] is initialized to 0
{ u:=v; // q is a queue of unexplored vertices.
visited[v]=1;
repeat {
for all vertices w adjacent from u do
{
if( visited[w]=0) then
{
Add w to q; //w is unexplored.
visited[w]=1;
}
}
if q is empty then return; // No unexplored vertex.
Delete the next element, u, from q; //Get first unexplored vertex.
} until(false);
}
Pseudocode for breadth first search
Theorem 2.2: Algorithm BFS visits all vertices’ reachable from v.
Theorem 2.3 Let t (n, e) and s(n, e) be the maximum time and maximum additional space taken by
algorithm BFS on any graph G with n vertices’ and e edges. t(n, e) = (n + e) and s(n, e) = (n) if G is
represented by its adjacency lists. If G is represented by its adjacency matrix then t(n, e) = (n 2) and
s(n, e) = (n).

Figure 2.2: Undirected Graph


For the above graph, its BFS order is 12345678.
If BFS is used on a connected undirected graph G then all vertices in G get visited and the
graph is traversed. However, if G is not connected then at least one vertex of G is not visited. A
complete traversal of the graph can be made by repeatedly calling BFS each time with a new unvisited
starting vertex. The resulting traversal algorithm is known as breadth first traversal (BFT).
Algorithm BFT(G,n)
// Breadth first traversal of G
{
for i:=1 to n do //Mark all vertices unvisited.
visited[i]=0;
for i:=1 to n do
if( visited[i]=0) then BFS(i);
}
Breadth first graph traversal

Depth First Search and Traversal


A depth first search of a graph differs from a breadth first search in that the exploration of a vertex v is
suspended as soon as a new vertex is reached. At this time the exploration of the new vertex u begins.
When this new vertex has been explored, we continue to explore v. The search terminates when all
reached vertices have been fully explored.
The DFS order for above graph is: 12485637
Algorithm DFS(v)
// given an undirected (directed) graph G= (V, E) with n vertices and an array visited [] initially set //to
zero, this algorithm visits all vertices reachable from v. G and visited [] are global.
{ Visited[v]=1;
for each vertex w adjacent from v do
{
If (visited[w]=0) then DFS(w);
}
}
Depth first search of a graph
Disjoint set operations: Union and Find algorithms
Here we see the use of trees for the representation of sets. We shall assume that the elements of the
sets are the numbers 1, 2, 3, ... , n. These numbers might, in practice, be indices into a symbol table
where the actual names of the elements are stored. We shall assume that the sets being represented are
pair wise disjoint; i.e. if Si and Sj i ≠ j, are two sets then there is no element which is in both Si and S j·
For example, if we have 10 elements numbered 1 through 10, they may be partitioned into three
disjoint sets Si= {l, 7, 8, 9}; S2 = {2, 5, 10} and S3 ={3, 4, 6}.
The operations we wish to perform on these sets are:
(a) Disjoint set union: If Si and Sj are two disjoint sets, then their union Si U S j = { all
elements x such that x is in S; or S j}. Thus, S 1 U S 2 = {1, 7, 8, 9, 2, 5, 10}. Since we have assumed
that all sets are disjoint, following the union of S; and Sj we can assume that the sets Si and Sj no
longer exist independently, i.e. they are replaced by Si U Sj in the collection of sets.
(b) Find (i): Find the set containing element i. Thus, 4 is in set S3 and 9 is in set S1.
The sets will be represented by trees. One possible representation for the sets S1, S 2 and S 3 is given
below:

Possible representation of S1US2

Array representation of S1, S2 and S3


Simple Algorithms for Union and Find

Degenerated Tree for Union(1), Union(2),...Unino(n-1),Union(n)


Since the time taken for a union is constant, all the n - 1 unions can be processed in time O(n).
However, each FIND requires following a chain of PARENT links from node 1 to the root. The time
required to process a FIND for an element at level i of a tree is O(i). Hence, the total time needed to
process the n - 2 finds is O(n2).

Weighting Rule for UNION(i j): If the number of nodes in tree i is less than the number in tree j,
then make j the parent of i, otherwise make i the parent of j.

Using this rule on the sequence of set unions given before we obtain the trees as shown below
Example: Consider performing the following sequence of Unions performed using weighted union

A Worst case tree using weighting rule


Now process the following 8 finds:
FIND(8), FIND(8), FIND(8), FIND(8)
FIND(8), FIND(8), FIND(8), FIND(8)
Using the old version of find, namely procedure F(8), requires going up 3 parent link fields for a total
of 24 moves to process all 8 finds. In algorithm FIND, the first FIND(8) requires going up 3 links and
then resetting 3 links. Each of remaining 7 finds requires going up only 1 link field. The total cost is
now only 13 moves.
Collapsing Rule: If j is a node on the path from i to its root then set PARENT(j) to root (i).

Connected Components and Bi-connected components

A vertex v in a connected graph G is an articulation point iff the deletion of vertex v together with all
edges incident to v disconnects the graph into two or more nonempty components. In the connected
graph of Figure 2.6(a) vertex 2 is an articulation point as the deletion of vertex 2 and edges (1, 2), (2,
3), (2, 5), (2, 7) and (2, 8) leaves behind two disconnected nonempty components (Figure 2.6(b)).
Graph G of Figure 2.6(a) has only two other articulation points: vertex 5 and vertex 3.

Figure 2.7(a) Graph G Figure 2.7(b) Result of deleting vertex 2

A graph G is biconnected iff it contains no articulation point. The graph of Figure 2.7(a) is not
biconnected. The graph of Figure 2.8 is biconnected.

Figure 2.8 A Biconnected graph

The presence of articulation points in a connected graph can be an undesirable feature in many cases.
For example, if G represents a communication network with the vertices representing communication
stations and the edges communication lines then the failure of a communication station i which is an
articulation point would result in loss of communication to points other than i too. On the other hand,
if G has no articulation point then if any station i fail, we can still communicate between every pair of
stations not including station i.
Here we discuss an efficient algorithm to test if a connected graph is biconnected. For the case of
graphs that are not biconnected, this algorithm will identify all the articulation points. Once it has been
determined that a connected graph G is not biconnected, it may be desirable to determine a set of
edges whose inclusion will make the graph biconnected.
Now, we discuss identifying the articulation points and biconnected components of a connected
graph G with n ≥2 vertices's. The problem is efficiently solved by considering a depth first spanning
tree of G.

Figures 2.9(a) and (b) show a depth first spanning tree of the graph of Figure 2.6(a). In each figure
there is a number outside each vertex. These numbers correspond to the order in which a depth first
search visits these vertices. This number will be referred to as the depth first number (DFN) of the
vertex. Thus, DFN(l) = 1, DFN(4) = 2 and DFN(6) = 8. In Figure 2.8(b) solid edges form the depth
first spanning tree. These edges are called tree edges. Broken edges (i.e. all remaining edges) are
called back edges.

(a) (b)
Figure 2.9 A depth first spanning tree of the graph of figure 2.7 (a)
Depth first spanning trees have a property that is very useful in identifying articulation points and
biconnected components. This property is that if (u, v) is any edge in G then relative to the depth first
spanning tree T either u is an ancestor of v or v is an ancestor of u. So, there are no cross edges relative
to a depth first spanning tree ((u, v) is a cross edge relative to T iff neither u is an ancestor of v nor v
an ancestor of u). To see this, assume that (u, v) E E(G) and (u, v) is a cross edge. (u, v) cannot be a
tree edge as otherwise u is the parent of v or vice versa. So, (u, v) must be a back edge. Without loss of
generality, we may assume DFN(u) < DFN(v). Since vertex u is visited first, its exploration cannot be
complete until vertex v is visited. From the definition of depth first search, it follows that u is an
ancestor of all vertices visited until u is completely explored. Hence u is an ancestor of v in T and (u,
v) cannot be a cross edge.
The root node of a depth first spanning tree is an articulation point iff it has at least two children.
Furthermore, if u is any other vertex then it is not an articulation point iff from every child w of u
it is possible to reach an ancestor of u using only a path made up of descendents of w and a back edge.
A simple rule to identify articulation points is defined. For each vertex u define L(u) as follows:
L(u) = min{ DFN(u), min{L(w) I w is a child of u }, min{ DFN(w) I (u, w)is a back edge } }
For the spanning tree of Figure 2.8(b) the L values are L(l:l0) = (1, 1, 1, 1, 6, 8, 6, 6, 5, 4). Vertex 3 is
an articulation point as child 10 has L(l0) = 4 while DFN(3) = 3. Vertex 2 is an articulation point as
child 5 has L(5) = 6 and DFN(2) = 6. The only other articulation point is vertex 5; child 6 has L(6) = 8
while DFN(5) = 7.
L(u) can be easily computed if the vertices's of the depth first spanning tree are visited in postorder.
Thus, to determine the articulation points, it will be necessary to perform a depth first search of the
graph G and visit the nodes in the resulting depth first spanning tree in postorder. It is possible to do
both these functions in parallel.
Pseudo code to compute DFN and L

Procedure ART carries out a depth first search of G. During this search each newly visited vertex gets
assigned its depth first number. At the same time, L(i) is computed for each vertex in the tree.

Pseudo code to determine Bi components


UNIT-III
GREEDY METHOD
Greedy method is the most straightforward designed technique.
As the name suggest they are short sighted in their approach taking decision on the basis of the
information immediately at the hand without worrying about the effect these decision may
have in the future.
DEFINITION:
A problem with N inputs will have some constraints .any subsets that satisfy these constraints
are called a feasible solution.
A feasible solution that either maximize can minimize a given objectives function is called an
optimal solution.
Control algorithm for Greedy Method:
1. Algorithm Greedy (a,n)

2. //a[1:n] contain the „n‟ inputs


3. {
4. solution =0;//Initialise the solution.
5. For i=1 to n do
6. {
7. x=select(a);
8. if(feasible(solution,x))then
9. solution=union(solution,x);
10. }

11 .return solution;
12. }

The function select an input from a[] and removes it. The select input value is assigned to X.
1. Feasible is a Boolean value function that determines whether X can be included into
the solution vector.

2. The function Union combines X with The solution and updates the objective function.
3. The function Greedy describes the essential way that a greedy algorithm will once a
particular problem is chosen and s the function subset, feasible & union are properly
implemented.
KNAPSACK PROBLEM
1. We are given n objects and knapsack or bags with capacity m object i has a weight wi
where i varies from 1 to n.
2. The problem is we have to fill the bag with the help of n objects and the resulting profit
has to be maximum.
3. Formally the problem can be stated as Maximize ∑xi pi subject to xiwi<=m
where xi is the fraction of object and it lies between 0 to 1.There are so many ways to solve this
problem, which will give many feasible solutions for which we have to find the optimal solution. But
in this algorithm, it will generate only one solution which is going to be feasible as well as optimal.

4 First, we find the profit & weight rates of each and every object and sort it according to
the descending order of the ratios. Select an object with highest p/w ratio and check
whether its height is lesser than the capacity of the bag. If so place 1 unit of the first object
and decrement the capacity of the bag by the weight of the object you have placed.
• Repeat the above steps until the capacity of the bag becomes less than the weight of the
object you have selected.
• In this case place a fraction of the object and come out of the loop whenever you
selected.
ALGORITHM:
1. Algorityhm Greedy knapsack (m,n)
2 //P[1:n] and the W[1:n]contain the profit & weight respectively of n objects //ordered
such that P[i]/W[i] >=P[i+1]/W[i+1] 5 m is the Knapsack size and
// x[1:n] is the solutionvertex.
6. {
7. f or i:=1 to n do a[i]=0.0;
8. U:=m;
9. for i:=1 to n do
10. {
11. if (w[i]>u)then break; 13.
x[i]:=1.0;U:=U-w[i]
14. }
15. if(i<=n)then x[i]:=U/w[i]; 16. }
Example:
Capacity=20 N=3 ,M=20 Wi=18,15,10; Pi=25,24,15
Pi/Wi=25/18=1.36,24/15=1.6,15/10=1.5

Descending Order Pi/Wi 1.6 1.5 1.36


Pi = 24 15 25
Wi = 15 10 18
Xi = 1 5/10 0
PiXi=1*24+0.5*15=31.5

The optimal solution is 31.5


X1 X2 X3 WiXi PiXi
½ 1/3 ¼ 16.6 24.25
1 2/5 0 20 18.2
0 2/3 1 20 31

Of these feasible solution Solution 4 yield the Max profit .As we shall soon see this solution is optimal
for the given problem instance.

JOB SCHEDULING WITH DEAD LINES


The problem is the number of jobs, their profit and deadlines will be given and we have to find a
sequence of job, which will be completed within its deadlines, and it should yield a maximum profit.

Points To remember:
To complete a job, one has to process the job or a action for one unit of time. Only one
machine is available for processing jobs. A feasible solution for this problem is a subset
of j of jobs such that each job in this subject can be completed by this deadline. If we
select a job at that time , Since one job can be processed in a single m/c. The other job
has to be in its waiting state until the job is completed and the machine becomes free. So
the waiting time and the processing time should be less than or equal to the dead line of
the job.

ALGORITHM:
High level description of job sequencing algorithm
Algorithm GREEDY _JOB(D, J, n)
/ /J is an output variable. It is the set of jobs to be completed by their deadlines
{ J={1}
for i = 2 to n do
{
if (all jobs in J U { i} can be completed by their deadlines) thenJ=- J U {i};
}
}
Greedy algorithm for sequencing unit time jobs with deadlines and profits
Algorithm JS(d,j,n)
//The job are ordered such that p[1]>p[2]…>p[n]
//j[i] is the ith job in the optimal solution
// Also at terminal d [ J[ i]<=d[ J {i+1],1<i<k
{ d[0]=J[0]=0;
}
J[1]=1; k=1; For I =1 to n do
{ // consider jobs in non increasing order of P[I];find the position for I and check feasibility insertion
r=k;
while((d[J[r]]>d[i] )and (d[J[r]]! = r)do r =r-1; if
(d[J[r]]<d[I])and (d[I]>r))then
{
for q=k to (r+1) step –1 do J [q+1]=j[q] J[r+1]=i;
K=k+1;
}
} return k;
}
Example :
1. n=5 (P1,P2,…P5)=(20,15,10,5,1)
(d1,d2….d3)=(2,2,1,3,3)

Feasible solution Processing Sequence Value

(1) (1) 20
(2) (2) 15
(3) (3) 10
(4) (4) 5
(5) (5) 1
(1,2) (2,1) 35
(1,3) (3,1) 30
(1,4) (1,4) 25
(1,5) (1,5) 21
(2,3) (3,2) 25
(2,4) (2,4) 20
(2,5) (2,5) 16
(1,2,3) (3,2,1) 45
(1,2,4) (1,2,4) 40
The Solution (1,2,3) is optimal
2. n=4
(P1,P2,…P4)=(100,1
0,15,27)
(d1,d2….d4)=(2,1,2,
1)

Feasible solution Processing Sequence Value

(1,2) (2,1) 110


(1,3) (1,3) 115
(1,4) (4,1) 127
(2,3) (9,3) 25
(2,4) (4,2) 37
(3,4) (4,3) 42
(1) (1) 100
(2) (2) 10
(3) (3) 15
(4) (4) 27

The solution (1,4) is optimal.

MINIMUM SPANNING TREE


Let G(V,E) be an undirected connected graph with vertices's ‘v’ and edge
‘E’. A sub-graph t=(V,E’’) of the G is a Spanning tree of G iff ‘t’ is a tree. The problem is to
generate a graph G‟= (V,E’’) where E‟ is the subset of E,G‟ is a Minimum spanning tree. Each
and every edge will contain the given non-negative length .connect all the nodes with edge
present in set E‟ and weight has to be minimum.
NOTE:
We have to visit all the nodes.
The subset tree (i.e) any connected graph with „N‟ vertices must have at least N-1 edges and
also it does not form a cycle.
Definition:
A spanning tree of a graph is an undirected tree consisting of only those edges that are
necessary to connect all the vertices in the original graph.
A Spanning tree has a property that for any pair of vertices there exist only one path between
them and the insertion of an edge to a spanning tree form a unique cycle.

Application of the spanning tree:


1. Analysis of electrical circuit.
2. Shortest route problems.

Minimum cost spanning tree:


The cost of a spanning tree is the sum of cost of the edges in that tree.
There are 2 methods to determine a minimum cost spanning tree
1. Kruskal’s Algorithm
2. Prom’s Algorithm.

KRUSKAL’S ALGORITHM:
In Kruskal's algorithm the selection function chooses edges in increasing order of length without
worrying too much about their connection to previously chosen edges, except that never to form a
cycle. The result is a forest of trees that grows until all the trees in a forest (all the components) merge
in a single tree. In this algorithm, a minimum cost-spanning tree ‘T’ is built edge by edge. Edges are
considered for inclusion in ‘T’ in increasing order of their cost. An edge is included in ‘T’ if it doesn’t
form a cycle with edge already in T. To find the minimum cost spanning tree the edge are inserted to
tree in increasing order of their cost
Algorithm:Algorithm kruskal(E,cost,n,t)
//E is the set of edges in G has ‘n’ vertices. cost[u,v]cost of edge (u,v).t is the set of edge // in
minimum cost spanning tree. The final cost is returned.
{
for i=1 to n do
parent[i]:=-1;
i:=0;
mincost:=0.0;
while((i<n-1)and (heap not empty)) do
{ j:=find(n);
k:=find(v);
if(j≠ k) than
{
i:=i+1
t[i,1]:=u;
t[i,2]:=v;
mincost:=mincost+cost[u,v];
union(j,k);
}}
if(i ≠ n-1) then write(“No spanning tree”) else return
minimum cost; }
Analysis The time complexity of minimum cost spanning tree algorithm in worst case is O(|E|log|E|),
where E is the edge set of G.

Example: Step by Step operation of Kurskal algorithm.

PRIM'S ALGORITHM
Start from an arbitrary vertex (root). At each stage, add a new branch (edge) to the tree already
constructed; the algorithm halts when all the vertices in the graph have been reached

Algorithm prims(e,cost,n,t)
{ Let (k, l) be an edge of minimum cost in E; Mincost :=cost[k,l];
T[1,1]:=k; t[1,2]:=l; for i:=1 to n
do
if (cost[i,l]<cost[i,k]) then near[i]:=l; else
near[i]:=k;
Near[k]:=near[l]:=0; for i:=2 to n-
1 do {
Let j be an index such that near[j]≠0 and
Cost[j,near[j]] is minimum; T[i,1]:=j;
T[i,2]:=near[j]; Mincost:=mincost+
Cost[j,near[j]]; Near[j]:=0;
for k:=0 to n do
if near((near[k]≠0) and (Cost[k,near[k]]>cost[k,j])) then
Near[k]:=j;
}
Return mincost;
}
The Prim’s algorithm will start with a tree that includes only a minimum cost edge of G. Then, edges
are added to the tree one by one. The next edge (i,j) to be added in such that I is a vertex included in
the tree, j is a vertex not yet included, and cost of (i,j), cost[i,j] is minimum among all the edges. The
working of Prim’s will be explained by following diagram
Step 1: Step 2:

Step 3: Step 4:

Step 5: Step 6:

SINGLE SOURCE SHORTEST PATH


Single-source shortest path:
Graphs can be used to represent the highway structure of a state or country with vertices
representing cities and edges representing sections of highway. The edges can then be assigned
weights which may be either the distance between the two cities connected by the edge or the average
time to drive along that section of highway. A motorist wishing to drive from city A to B would be
interested in answers to the following questions:
1. Is there a path from A to B?
2. If there is more than one path from A to B? Which is the shortest path?

The problems defined by these questions are special case of the path problem we study in this
section. The length of a path is now defined to be the sum of the weights of the edges on that path.
The starting vertex of the path is referred to as the source and the last vertex the destination. The
graphs are digraphs representing streets. Consider a digraph G=(V,E), with the distance to be
traveled as weights on the edges. The problem is to determine the shortest path from v0 to all the
remaining vertices of G. It is assumed that all the weights associated with the edges are positive.
The shortest path between v0 and some other node v is an ordering among a subset of the edges.
Hence this problem fits the ordering paradigm.
Example: Find the shortest path from Boston to all other cities in the given graph

Directed graph from map

Cost adjacency matrix for the above directed graph, all entries not shown are

Actions in determining the shortest path


DYNAMIC PROGRAMMING
THE GENERAL METHOD
Dynamic Programming is an algorithm design method that can be used when the solution to a
problem may be viewed as the result of a sequence of decisions.
In dynamic programming an optimal sequence of decisions is arrived at by making explicit appeal to
the Principle of Optimality. This principle 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.
Thus, the essential difference between the greedy method and dynamic programming is that in the
greedy method only one decision sequence is ever generated. In dynamic programming, many
decision sequences may be generated. However, sequences containing suboptimal subsequences
cannot be optimal (if the principal of optimality holds) and so will not (as far as possible) be
generated.
Matrix-Chain Multiplication(or Matrix Chain Ordering Problem, MCOP) is an optimization
problem that can be solved using dynamic programming. Given a sequence of matrices, the goal is to
find the most efficient way to multi these matrices. The problem is not actually to perform the
multiplications, but merely to decide the sequence of the matrix multiplications involved.
Here are many options because matrix multiplication is associative. In other words, no matter how the
product is parenthesized, the result obtained will remain the same. For example, for four matrices A,
B, C, and D, we would have:

((AB)C)D = (A(BC))D = (AB)(CD) = A((BC)D) = A(B(CD)).

However, the order in which the product is parenthesized affects the number of simple
arithmetic operations needed to compute the product, or the efficiency. For example, if A is a 10 × 30
matrix, B is a 30 × 5 matrix, and C is a 5 × 60 matrix, then computing (AB)C needs (10×30×5) +
(10×5×60) = 1500 + 3000 = 4500 operations, while computing A(BC) needs (30×5×60) + (10×30×60)
= 9000 + 18000 = 27000 operations. Clearly the first method is more efficient. With this information,
the problem statement can be refined as "how to determine the optimal parenthesization of a product
of n matrices?" Checking each possible parenthesization (brute force) would require a run-time that is
exponential in the number of matrices, which is very slow and impractical for large n. A quicker
solution to this problem can be achieved by breaking up the problem into a set of related subproblems.
By solving subproblems once and reusing the solutions, the required run-time can drastically reduced.
This concept is known as dynamic programming.

A dynamic programming algorithm

To begin, let us assume that all we really want to know is the minimum cost, or minimum number of
arithmetic operations needed to multiply out the matrices. If we are only multiplying two matrices,
there is only one way to multiply them, so the minimum cost is the cost of doing this. In general, we
can find the minimum cost using the following recursive algorithm:

• 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 cost 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.
For example, if we have four matrices ABCD, we compute the cost required to find each of (A)(BCD),
(AB)(CD), and (ABC)(D), making recursive calls to find the minimum cost to compute ABC, AB, CD,
and BCD. We then choose the best one. Better still, this yields not only the minimum cost, but also
demonstrates the best way of doing the multiplication: group it the way that yields the lowest total
cost, and do the same for each factor.

However, if we implement this algorithm we discover that it is just as slow as the naive way of trying
all permutations! What went wrong? The answer is that we're doing a lot of redundant work. For
example, above we made a recursive call to find the best cost for computing both ABC and AB. But
finding the best cost for computing ABC also requires finding the best cost for AB. As the recursion
grows deeper, more and more of this type of unnecessary repetition occurs.

One simple solution is called memoization: each time we compute the minimum cost needed to
multiply out a specific subsequence, we save it. If we are ever asked to compute it again, we simply
give the saved answer, and do not recompute it. Since there are about n2/2 different subsequences,
where n is the number of matrices, the space required to do this is reasonable. It can be shown that this
simple trick brings the runtime down to O(n3) from O(2n), which is more than efficient enough for
real applications. This is top-down dynamic programming. The following bottom-up approach
computes, for each 2 ≤ k ≤ n, the minimum costs of all subsequences of length k using the costs of
smaller subsequences already computed. It has the same asymptotic runtime and requires no recursion.

pseudocode:
// Matrix A[i] has dimension dims[i-1] x dims[i] for i = 1..n
MatrixChainOrder(int dims[])
{
// length[dims] = n + 1
n = dims.length - 1;
// m[i,j] = Minimum number of scalar multiplications (i.e., cost)
// needed to compute the matrix A[i]A[i+1]...A[j] = A[i..j]
// The cost is zero when multiplying one matrix
for (i = 1; i <= n; i++)
m[i, i] = 0;
for (len = 2; len <= n; len++) { // Subsequence lengths
for (i = 1; i <= n - len + 1; i++) {
j = i + len - 1;
m[i, j] = MAXINT;
for (k = i; k <= j - 1; k++) {
cost = m[i, k] + m[k+1, j] + dims[i-1]*dims[k]*dims[j];
if (cost < m[i, j]) {
m[i, j] = cost;
s[i, j] = k; // Index of the subsequence split that achieved minimal cost
}
}
}
}
} Note: The first index for dims is 0 and the first index for m and s is 1.
OPTIMAL BINARY SEARCH TREES
Definition A binary search tree T is a binary tree; either it is empty or each node in the tree contains
an identifier and
(i) all identifiers in the left subtree of T are less (numerically or alphabetically) than the identifier in
the root node T;
(ii) all identifiers in the right subtree are greater than the identifier in the root node T;
(iii) the left and right subtrees of Tare also binary search trees.
To determine whether an identifier X is present in a binary search tree, X is compared with the
root. If X is less than the identifier in the root, then the search continues in the left subtree; if X equals
the identifier in the root, the search terminates.

Two possible for binary search tree

In obtaining a cost function for binary search trees, it is useful to add a fictitious node in place
of every empty subtree in the search tree. Such nodes are called external nodes and are drawn square.
All other nodes are internal nodes. If a binary search tree represents n identifiers then there will be
exactly n internal nodes and n + 1 (fictitious) external nodes. Every internal node represents a point
where a successful search may terminate. Every external node represents a point where an
unsuccessful search may terminate.
If a successful search terminates at an internal node at level l then l iterations are required
according to binary search algorithm. Hence, the expected cost contribution from the internal node for
ai is P(i)*level (ai).Unsuccessful searches terminate with i = 0 (i.e. at an external node) in algorithm
SEARCH. The identifiers not in the binary search tree may be partitioned into n + 1 equivalence
classes Ei, 0 ≤ I ≤n. En contains all identifiers X such that X < a 1. Ei contains all identifiers X such
that a; < X < ai+1, 1 ≤ i < n. En contains all identifiers X, X > an. It is easy to see that for all identifiers
in the same class E;, the search terminates at the same external node. For identifiers in different E; the
search terminates at different external nodes. If the failure node for Ei is at level l then only l - 1
iterations of the while loop are made. Hence, the cost contribution of this node is Q (i) * (level(Ei) -
1).The preceding discussion leads to the following formula for the expected cost of a binary search
tree: ∑P(i)*level(ai) + ∑ Q(i)*(level(Ei) - 1).
The preceding discussion leads to the following formula for the expected cost of a binary search tree:
∑1≤i≤n P(i)*level(ai) + ∑ 0≤i≤n Q(i)*(level(Ei) - 1)
The possible binary search trees for the identifier set (a1,a2, a3) = (do, if, stop) are:
With equal probabilities P(i) = Q(j) = 1/7 for all i andj, we have cost(tree a) = 15/7; cost(tree b) =
13/7cost(tree c) = 15/7; cost(tree d) = 15/7 cost(tree e) = 15/7.As expected, tree b is optimal.
With P(l) = .5, P(2) = .1, P(3) .05,Q(0) = .15, Q(l) = .1, Q(2) = .05 and Q(3) = .OS we have
cost(tree a) = 2.65; cost(tree b) = 1.9 cost( tree c) = 1.5; cost( treed) = 2.05 cost(tree e) = 1.6
In order to apply dynamic programming to the problem of obtaining an optimal binary search
tree we need to view the construction of such a tree as the result of a sequence of decisions and then
observe that the principle of optimality holds when applied to the problem state resulting from a
decision.

Then cost of the left tree is :


COST(L) = ∑1≤i≤k P(i)*level(ai) + ∑ 0≤i≤k Q(i)*(level(Ei) - 1)
And right tree
COST(R) = ∑k<i≤n P(i)*level(ai) + ∑ k≤i≤n Q(i)*(level(Ei) - 1)

P(k) + COST(L) + COST(R) + W(0, k - 1) + W(k, n)


In addition, k must be chosen such that P(k) + C(0, k - 1) + C(k, n) + W(0, k - 1) + W(k, n)
is minimum. Hence, for C(0, n) we obtain:
C(0, n) = min1≤k≤n { C(0, k - 1) + C(k, n) + P(k) + W(0, k - 1) + W(k, n)}
We may generalize (5.11) to obtain for any C(i,j) Optimal Binary Search Trees as shown in fig.
C(i,j) = mini<k≤j {C(i, k - 1) + C(k, J) + P(k) + W(l, k - 1) + W(k,J)}
= mini<k≤j { C(i. k - 1) + C(k. j)} + W(i. j)

Example Let n = 4 and (a1, a1, a3, a4) = (do, if, read, while).Let P(1:4) = (3, 3, 1, 1) and Q(0:4) =
(2, 3, 1, 1, 1). The Ps and Qs have been multiplied by 16 for convenience. Initially, we have W(i, i) =
Q(i), C(i, i) = 0 and R(i, i) = 0, 0 ::5 i ::5 4. Using eq (5.12) and the observation
W(i,J) = P(j) + Q(j) + W(i,j - 1) we get:
W(O, 1) = P(l) + Q(l) + W(O, 0) = 8
C(O, 1) = W(O, 1) + min{ C(O, 0) + C(l, 1)} = 8
R(O, 1) = 1
W(l, 2) = P(2) + Q(2) + W(l, 1) = 7
C(l,2) = W(l,2) + min{C(l, 1) + C(2,2)} = 7
R(O, 2) = 2
W(2, 3) = P(3) + Q(3) + W(2, 2) = 3
C(2, 3) = W(2, 3) + min{ C(2, 2) + C(3, 3)} = 3
R(2, 3) = 3
W(3, 4) = P(4) + Q(4) + W(3, 3) = 3
C(3, 4) = W(3, 4) + min{ C(3, 3) + C(4, 4)} = 3
R(3, 4) = 4

Computation of C(0, 4), W(0, 4) and R(0, 4)

0/1-KNAPSACK
A solution to the knapsack problem may be obtained by making a sequence of decisions on the
variables xi, x2, ... , Xn. A decision on variable x; involves deciding which of the values 0 or 1 is to be
assigned to it.
Let fi(X) be the value of an optimal solution to KNAP(l, j, X). Since the principle of optimality
holds, we obtain For arbitrary f;(X), i > 0, Equation generalizes to
fi(X) = max{f i-1(X), f i-1 (X - wi) + pi}.
If S is the set of all pairs for f i-1 (including(0, 0)) then the set Si1 of all pairs for
i-1
gi(X) = f i-1 (X - wi) + p; is obtained by adding to each pair in Si- 1 the pair (pi;, Wi).

Si1 = {(P,W)/(P-Pi,W- wi)€ Si }


Si may now be obtained by merging together Si- 1.
Example: Consider the knapsack instance n=3,(w1,w2,w3)=(2,3,4) and (p1,p2,p3)=(1,2,5) and m=6
For the data of Example, we have
s0 = {(0,0)}; S11 = {(1,2)} s1 = {(0,0),(1,2)}; S21 = {(2,3),(3,5)}
S2 = {(0, 0), (1, 2), (2, 3), (3, 5)}; S31 = {(5, 4), (6, 6), (7, 7), (8, 9)}
S3 = {(0, 0), (1, 2), (2, 3), (5, 4), (6, 6), (7, 7), (8, 9)}.
Note that the pair (3, 5) has been eliminated from S3 as a result of the purging rule stated
above. Hence, the solution is (x1,x2,x3)=(1,0,1)
knapsack algorithm
Algorithm for 0/ 1 knapsack problem

THE TRAVELING SALESPERSON PROBLEM


The traveling salesperson problem finds application in a variety of situations. Suppose we have to
route a postal van to pick up mail from mail boxes located at n different sites. An n + 1 vertex graph
may be used to represent the situation. One vertex represents the post office from which the postal van
starts and to which it must return. Edge (i, j) is assigned a cost equal to the distance from site i to site j.
The route taken by the postal van is a tour and we are interested in finding a tour of minimum length.
Tour to be a simple path that starts and ends at vertex 1. Every tour consists of an edge < 1, k > for
some k € V - { 1} and a path from vertex k to vertex 1. The path from vertex k to vertex 1 goes through
each vertex in V - { 1, k} exactly once. It is easy to see that if the tour is optimal then
the path from k to 1 must be a shortest k to 1 path going through all vertices's in V - { 1, k }. Hence,
the principle of optimality holds. Let g(i, S) be the length of a shortest path starting at vertex i, going
through all vertices's in S and terminating at vertex 1. g ( 1, V - { 1 } ) is the length of an optimal
salesperson tour.
From the principal of optimality it follows that:

The problem solved for g(l, V - { 1}) if we know g(k, V - { 1, k }) for all choices of k. The g values
may be obtained by the above equation. Clearly, g(i, ø) = Ci,1, 1 ≤i≤ n.
Then we can obtain g(i, S) for S with ⌈S⌉= 2 etc. When ⌈S⌉ < n - 1, the values of i and S for which g (i,
S) is needed are such that i ≠1; 1 Sand i S.
Example Directed graph and edge length matrix

g(2, ø)= c21 = 5;g(3, ø)= C31 = 6 andg(4, ø)= C41 = 8 .


Using g(I,S)Equation, we obtain
g(2, { 3}) = C23 + g(3, ø) = 15; g(3, {2}) = 18; g(4, {2}) = 13; g(2, {4}) = 18 , g(3,{4}) = 20;
g(4, {3}) = 15. Next, we compute g(i, S) with ⌈S⌉= 2, i ≠1; 1 Sand i S.
g(2, {3, 4}) = min{c23 + g(3, {4}),c24 + g(4, {3})} = 25
g(3, {2, 4}) = min{c32 + g(2, { 4}), C34 + g(4, {2})} = 25
g(4, {2, 3}) = min{c42 + g(2, {3}), C43 + g(3, {2})} = 23
Finally, from g(1, V- {1}),we obtain g(l,{2,3,4}) = min{c12 +g(2,{3,4}),c13 +g(3,{2,4}),c14
+g(4,{2,3})} = min{ 35, 40, 43}= 35. An optimal tour of the graph of Figure (a) has length 35. A tour
of this length may be constructed if we retain with each g(i, S) the value of J that minimizes the right
hand side of (5.20). Let j(i, S) be this value. Then, j(1, {2, 3, 4}) = 2. Thus the tour starts from 1 and
goes to 2. The remaining tour may be obtained from g(2, {3, 4}). j(2, {3, 4}) = 4. Thus the next edge
is (2, 4). The remaining tour is for g(4, {3}). j(4, {3}) = 3.
The optimal tour is 1, 2, 4, 3, 1.

RELIABILITY DESIGN
In this section we look at an example of how to use dynamic programming to solve a problem
with a multiplicative optimization function. The problem is to design a system which is composed of
several devices connected in series .
Let ri be the reliability of device Di. Then, the reliability of the entire system is Πri. Even if
the individual devices are very reliable (the ri 's are very close to one), the reliability of the system
may not be very good. For example, if n = 10 and ri = .99, 1 ≤i ≤10 then Πri = .904. Hence, it is
desirable to duplicate devices. Multiple copies of the same device type are connected in parallel
through the use of switching circuits.
If stage i contains mi copies of device Di then the probability that all mi have a malfunction is
(1 - ri ) mi · Hence the reliability of stage i becomes 1 - (1 - ri ) mi. Thus, if ri = . 99 and mi = 2 the
stage reliability becomes .9999. In any practical situation, the stage reliability will be a little less than
1 - (1 - ri ) mi because the switching circuits themselves are not fully reliable.

Multiple devices connected in parallel in each stage.


Our problem is to use device duplication to maximize reliability. This maximization is to be
carried out under a cost constraint. Let ci be the cost of each unit of device i and let c be the
maximum allowable cost of the system being designed. We wish to solve the following maximization
problem:
maximize II 1≤i≤n Qi (mi)
subject to ∑ 1≤i≤m mi ci ≤c

mi≥1 and integer, 1≤i≤n

Since, we may assume each ci > 0,each mi must be in the range 1≤ mi≤ui where

Example: We are to design a three stage system with device types D1, D2 and D3. The costs are $30,
$15 and $20 respectively. The cost of the system is to be no more than $105. The reliability of each
device type is .9, .8 and .5 respectively. We shall assume that if stage i has mi devices of type i in
parallel then Qi (mi)= 1 - (1 - ri ) mi. In terms of the notation used earlier c1 = 30; c2 = 15; C3 = 20; c
= 105; r1 = .9; ri = .8; r3 = .5; U1 = 2; U2 = 3 and U3 = 3.
Using to represent all tuples of obtainable from by choosing mi = .i we obtain: =
{(.9, 30)} and = {(.9, 30), (.99, 60)}. = {(.72, 45), (.792, 75)}; = {(.864, 60)}. Note that the
tuple (.9504, 90) which comes from (.99, 60) has been eliminated from this leaves only $10. This
is not enough to allow m3 = 1. 2
= { (.8928, 75)}. Combining, we get S = { (.72, 45), (.864, 60),
(.8928, 75)} as the tuple (.792, 75) is dominated by (.864, 60). = {(.36, 65), (.432, 80), (.4464,95)};
= {(.54, 85), (.648, 100)}; = {(.63, 105)}. Combining, we get S3 = { (.36, 65); (.432, 80); (.54,
85); (.648, 100)}.The best design has a reliability of .648 and a cost of 100. Tracing back through the
Si’s we determine that m 1 = 1, m2 = 2 and m3 = 2.
UNIT 4
BACKTRACKING

Introdution:-Backtracking is a general algorithm for finding all (or some) solutions to some
computational problems, notably constraint satisfaction problems, that incrementally builds candidates
to the solutions. The classic example of the use of backtracking is the eight queens puzzle, that asks
for all arrangements of eight queens on a standard chessboard so that no queen attacks any other.
Backtracking can be applied only for problems which admit the concept of a "partial candidate
solution" and a relatively quick test of whether it can possibly be completed to a valid solution.
Many of the problems we shall solve using backtracking require that all the solutions satisfy a
complex set of constraints. For any problem these constraints may be divided into two categories:
explicit and implicit.
Explicit constraints are rules which restrict each xi to take on values only from a given set:-
xi>=0 or Si={all nonnegative real numbers}
xi = 0 or 1 or Si={0,1}
li<=xi<=ui or Si={a1: li<=ai<=ui}

The explicit constraints may or not depend on the particular instance I of the problem being
solved. All tuples that satisfy the explicit constraints define a possible solution space for I. The
implicit constraints determine which of the tuples in the solution space of I actually satisfy the
criterion function. Thus implicit constraints describe the way in which the xi must relate to each other.

A recursive Backtracking algorithm


Applications of backtracking:- Backtracking is an important tool for solving constraint satisfaction
problems, such as n-Queens problem, SumOfSubset, crosswords, verbal arithmetic, Sudoku, and
many other puzzles. etc..,
Example:[8-queens problem]:-A classic combinatorial problem is to place eight queens on an 8 x 8
chessboard so that no two "attack", that is so that no two of them are on the same row, column or
diagonal. Let us number the rows and columns of the chessboard 1 through 8 (as in figure). The
queens may also be numbered 1 through 8. Since each queen must be on a different row, we can
without loss of generality assume queen i is to be placed on row i. All solutions to the g.queens
problem can therefore be represented as 8-tuples (x1, ....... , x8) where x; is the column on which queen i
is placed. The explicit constraints using this formulation are Si = { 1, 2, 3, 4, 5, 6, 7, 8}, 1≤i≤n.
Therefore the solution space consists of gs g-tuples. The implicit constraints for this problem are that
no two xi' can be the same (i.e. all queens must be on different columns) and no two queens can be on
the same diagonal. The first of these two constraints implies that all solutions are permutations of the
8 tuple (1, 2, 3, 4, 5, 6, 7, 8). This realization reduces the size of the solution space from 8 8 8 tuples to
8! tuples. We shall see later how to formulate the second constraint in terms of the x i. Expressed as an
8-tuple, the solution in figure is (4, 6, 8, 2, 7, 1, 3, 5).
One solution to the 8-queen proplem.

At this point it is useful to develop some terminology regarding tree organizations of solution
spaces. Each node in this tree defines a problem state. All paths from the root to other nodes define
the state space of the problem. Solution states are those problem states S for which the path from the
root to S defines a tuple in the solution space. Answer states are those solution states S for which the
path from the root to S defines a tuple which is a member of the set of solutions (i.e., it satisfies the
implicit constraints) of the problem. The tree organization of the solution space will be referred to as
the state space tree.
Example-1: (n-queens) The n-queens problem is a generalization of the 8-queens problem. n queens
are to be placed on a nxn chessboard so that no two attack, i.e., no two queens are on the same
row,column or diagonal. Generalizing our earlier discussion, the solution space consists of all n!
permutations of the n-tuple (1, 2, ... , n).
Figure shows a possible tree organization for the case n = 4. A tree such as this is called a
permutation tree. The edges are labelled by possible values of xi' Edges from level 1 to level 2 nodes
specify the values for x1 . Thus, the leftmost subtree contains all solutions with x 1 = 1; its leftmost
subtree contains all solutions with x 1 = 1 and x2 = 2, etc. Edges from level i to level i + 1 are labelled
with the values of x;. The solution space is defined by all paths from the root node to a leaf node.
There are 4! = 24 leaf
0 nodes in the tree of
the figure.
Example-2: Sum of subsets , we gave two possible formulations of the solution space for the sum of
subsets problem. Figures show a possible tree organization for each of these formulations for the case
n = 4. The tree of Figure corresponds to the variable tuple size formulation. The edges are labeled
such that an edge from a level i node to a level i + 1 node represents a value for xi. At each node, the
solution space is partitioned into subsolution spaces. The solution space is defined by all paths from
the root node to any node in the tree. The possible paths are ( ) (this corresponds to the empty path
from the root to itself); (1); (1,2); (1, 2, 3); (1, 2, 3, 4); (1, 2, 4); (1, 3, 4); (2); (2, 3); etc. Thus, the
left-most subtree defines all subsets containing w 1, the next subtree defines all subsets containing w2
but not w 1; etc. The tree of Figure corresponds to the fixed tuple size formulation. Edges from level i
nodes to level i + 1 nodes are labelled with the value of xi which is either zero or one. All paths from
the root to a leaf node define the solution space. The left subtree of the root defines all subsets
containing w 1 while the right subtree defines all subsets not containing w 1 etc. Now there are 2 4
leaf nodes which represent 16 possible tuples.

Figure.A possible solution space organization for the sum of subsets. Nodes are numbered as in BFS

figur
e .
Anot
her
possi
ble
solut
ion
space organization for the sum of subsets. Nodes are numbered as in D-Search.

Example 1A:Let us see how backtracking works on the 4- queens problem . As a


bounding function we will use the obvious criteria that if (x1, x 2 , ..., xi) is the path to the current E-
node then all children nodes with parent-child labelings xi + 1 are such that (xi, ... , Xi+t) represents a
chessboard configuration in which no two queens are attacking. We start with the root node as the
only live node. This be- comes the E-node and the path is ().We generate one child. Let us assume that
children are generated in ascending order. Thus, node number 2 is generated and the path is now (1).
This corresponds to placing queen 1 on column 1. Node 2 becomes the E-node. Node 3 is generated
and immediately killed. The next node generated is node 8 and the path becomes (1, 3). Node 8
becomes the E-node. However, it gets killed as all of its children represent board configurations that
cannot lead to an answer node. We backtrack to node 2 and generate another child, node 13. The path
is now (1, 4). Figure shows the board configurations as backtracking proceeds. The below figure
shows graphically the steps that the backtracking algorithm goes through as it tries to find a solution.
The dots indicate placements of a queen which were tried and rejected because another queen was
attacking. In (b) the second queen is placed on columns 1, 2 and finally settles on column 3. In (c) the
algorithm tries all four columns and is unable to place the next queen on a square. Backtracking now
takes place. In (d) the second queen is moved to the next possible column, column 4 and the third
queen is placed on column 2. The boards in Figure (e, f, g, h) show the remaining steps that the
algorithm goes through until a solution is found. Figure 7.6 shows the part of the tree of Figure that is
actually generated. Nodes are numbered in the order in which they are generated. A node that gets
killed as a result of the bounding function has a B under it. Contrast this tree which contains 31 nodes.
Example of a backtrack solution to the four queens problem
General
backtracking
algorithm
The 8-Queens
Probelm
If we imagine the
squares of the
chessboard being
numbered as the
indices of the two
dimensional
array A(1:n, l:n)
then we observe
that for every element on the same diagonal which runs from the upper left to the lower right, each
element has the same "row - column" value. Also, every element
on the same diagonal which goes from the upper right to the lower left has the same "row + column"
value. Suppose two queens are placed at positions (i,j) and (k, /).Then by the above they are on the
same diagonal only if
i - j = k - l or i + j = k + l.
The first equation implies j-l=i-k
while the second implies j-l=k-i.
Therefore two queens lie on the same diagonal if and only if |j-l| = |i-k|.

The
below
algorith
m is for :
All
Solutions
to the n-
queens
problem
Example-2A SUM OF SUBSETS
Suppose we are given n distinct positive numbers (usually called weights) and we desire to find all
combinations of these numbers whose sum is M. This is called the sum of subsets problem.
According to the above constrains the algorithm is given below;
Algorithm for Sum of subset

Example2B:The above figure shows the portion of the state space tree generated by procedure
SUMOFSUB while working on the instance n = 6, M = 30 and W(l :6) = (5, 10, 12, 13, 15, 18). The
rectangular nodes list the values of s, k, r on each of the calls to SUMOFSUB. Circular nodes
represent points at which a subset with sum Mis printed out. At nodes A, Band C the output is
respectively (1, 1, 0, 0, 1), (1, 0, 1, 1) and (0, 0, 1, 0, 0, 1). Note that the tree of below Figure
contains only 23 rectangular nodes. The full state space tree for n = 6 contains 26 - 1 = 63 nodes from
which calls could be made (this count excludes the 64 leaf nodes as no call need be made from a leaf).
Example 3: GRAPH COLORING
Let G be a graph and m be a given positive integer. We want to discover if 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 . 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.
The chromatic number of the below graph is 3.

A graph is said to be planar iff 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.
A graph and its planer graph
An algorithm to generate a next colour.
Example 4: HAMILTONIAN CYCLES
Let G = ( V, E) be a connected graph with n vertices. A Hamiltonian cycle is a round trip path along n
edges of G which visits every vertex once and returns to its starting position. In other words if a
Hamiltonian cycle begins at some vertex v 1 E G and the vertices of G are visited in the order v 1, v 2,
••• , Vn+ 1 then the edges (vi… Vi+1) are in E, 1 ::5 i ::5 n and the v; are distinct except for v1 and
Vn+1which are equal.

G1 contains a Hamiltonian cycle and G2is not contains the Hamiltonian cycle

An algorithm for generating a next vertex

An algorithm for finding all Hamiltonian cycle


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.
Let us see how a FIFO branch-and-bound algorithm would search the state space tree
(Exapmle1A) for the 4-queens problem.. The only live nodes now are nodes 2, 18, 34 and 50.
According to the FIFO nodes are explored
The below Figure shows the portion of the tree of 4-Queens problem as in stated(Exapmle1A) that is
generated by a FIFO branch-and-bound search. Nodes that get killed as a result of the bounding
functions have a B under them. At the time the answer node, node 31, is reached the only live nodes
remaining are nodes 38 and 54.
Fig: Portion of 4-queens state space tree generated by FIFO branch-and-bound
LC-Search
In both LIFO and FIFO branch -and-bound the selection rule for the next E-node is rather rigid and in
a sense "blind". The selection rule for the next E-node does not give any preference to a node that has
a very good chance of getting the search to an answer node quickly. However, the rigid FIFO rule
requires first the expansion of all live nodes generated before node 30 was generated. The search for
an answer node can often be seeded by using an "intelligent" ranking function, c(. )(it is also know as
C-cap function ), for live nodes. The next E-node is selected on the basis of this ranking function.
A search strategy that uses a cost function c(X) = f(h(X)) + g(X) to select the next E-node would
always choose for its next £-node a live node with least c(.). Hence, such a search strategy is called an
LC-search (Least Cost search). It is interesting to note that BFS and D-search are special cases of LC-
search. Let T be a state space tree and c( ) a cost function for the nodes in T. If X is a node in T then
c(X) is the minimum cost of any answer node in the subtree with root X. Thus, c(T) is the cost of a
minimum cost answer node in T.
An algorithm for control abstraction for LC search.

Bounding
A branch-and-bound method searches a state space tree using any search mechanism in which all the
children of the E-node are generated before another node becomes the E-node.
A cost function c(-) such that c(X) <=c(X) is used to provide lower bounds on solutions
obtainable from any node X. If U is an upper bound on the cost of a minimum cost solution then all
live nodes X with c(X) > U may be killed as all answer nodes reachable from X have cost c(X) >=c(X)
> U. In case an answer node with cost U has already been reached then all live nodes with c(X) >=U
may be killed. The starting value for U may be obtained by some heuristic or may be set to ∞. Clearly,
so long as the initial value for U is no less than the cost of a minimum cost answer node, the above
rules to kill live nodes will not result in the killing of a live node that can reach a minimum cost
answer node. Each time a new answer node is found, the value of U may be updated.
Let us see how these ideas may be used to arrive at branch-and-bound algorithms for optimization
problems. As an example optimization problem, consider the job sequencing with deadlines problem.
Consider the following instance: n = 4; (pi, di. t1) = (5, 1, 1); (p2, di, t2) = (10, 3, 2); (p3, dJ, t3) = (6,
2, 1) and (p4, d4, t4) = (3, 1, 1). The solution space for this instance consists of all possible subsets of
the job index set { 1, 2, 3, 4 }. The solution space may be organized into a tree using
either of the two formulations used for the sum of subsets problem.
The below Figures shows corresponds to the variable tuple size formulation and corresponds
to the fixed tuple size formulation respectively.
LC Branch-and-Bound
An LC branch-and-bound search of the tree of above first figure will begin with U = ∞ and node 1 as
the first E-node. When node 1 is expanded, nodes 2, 3, 4 and 5 are generated in that order. As in the
case of FIFO branch-and-bound, U is updated to 14 when node 3 is generated and nodes 4 and
5 are killed as c(4) > U and c(5) > U. Node 2 is the next E-node as c(2) = 0 while c(3) = 5. Nodes 6, 7
and 8 are generated. U is updated to 9 when node 6 is generated. So, node 7 is killed as c(7) = 10 > U.
Node 8 is infeasible and so killed. The only live nodes now are nodes 3 and 6. Node 6 is the next £-
node as c(6) = 0 < c(3). Both its children are infeasible. Node 3 becomes the next E-node. When node
9 is generated U is updated to 8 as u(9) = 8. So, node 10 with c(10) = 11 is killed upon
generation. Node 9 becomes the next E-node. Its only child is infeasible. No live nodes remain. The
search terminates with node 9 representing the minimum cost answer node. An LC branch-and-bound
algorithm may also terminate when the next E-node E has c(E) >= U. The control abstraction for LC
branch-and-bound is LCBB. It operates under the same assumptions as FIFOBB.
0/1 knapsack problem
In order to use the branch-and-bound technique to solve any problem, it is first necessary to conceive of a state
space tree for the problem. We have already seen two possible state space tree organizations for the knapsack
problem (Section 7.6). Still, we cannot directly apply the techniques of Section 8.1 since these were discussed
with respect to minimization problems whereas the knapsack problem is a maximization problem. This
difficulty is easily overcome by replacing the objective function ∑pixi by the function
- ∑pixi. Clearly, ∑pixi is maximized iff - ∑pixi is minimized. This modified knapsack problem is stated as
n
minimize - ∑pixi
i=l
n
subject to ∑pixi <=M
i=l
Xi = 0 or 1, 1<=i<=n
Algorithm Function u( ·) for knapsack problem
LC Branch-and-Bound Solution
Example (LCBB) Consider the knapsack instance: n = 4; (pi, p2,p3, p4) = (10, 10, 12, 18); (wi. w2, w
3, w4) = (2, 4, 6, 9) and M = 15. For this node, node 1 of the below figure , we have c(l) = -38 and
u(l) = -32. Since this is not a solution node, procedure LCBB sets ans = 0 and U = - 32 .The E-node is
expanded and its two children, nodes 2 and 3 generated. C (2) = -38, c(3) = -32, u(2) = -32 and u(3) =
-27.

Figure LC Branch-and-bound tree the above example problem

FIFO branch-and-bound tree


TRAVELING SALESPERSON
An O(n2 2n) dynamic programming algorithm for the traveling salesperson problem We shall now
investigate branch-and-bound algorithms for this problem. While the worst case complexity of these
algorithms will not be any better than O(n2 2n) the use of good bounding functions will enable these
branch-and-bound algorithms to solve some problem instances in much less time than required by the
dynamic programming algorithm. Let G = (V, E) be a directed graph defining an instance of the
traveling salesperson problem. Let Cij be the cost of edge (i, j), Cij = ∞ if (i, j) ₡ E and let ̸ V ̸ = n.
Figure shows the tree organization for the case of a complete graph with ̸ V ̸ = 4. Node 14 represents
the tour io = l, i1 = 3, i2 = 4, i3 = 2and i4= 1.

Let us consider an example problem and solutions are according to FIFOBB and LCBB
Example cost matrix is as given below:

For the above cost matrix State space tree for the traveling salesperson problem with n = 4
and io = i 4 = 1
State space tree generated by procedure LCBB.

Reduced cost matrices corresponding to nodes to the above tree


UNIT-V
NP-Hard and NP-Complete Problems

In this we try to distinguish problems whose solution is by a polynomial time algorithm and
problems for which no polynomial time algorithm is known. It is an unexplained phenomenon that for
many of the problems we know and study, the best algorithms for their solution have computing times
which cluster into two groups.
The first group consists of problems whose solution is bounded by a polynomial of small
degree. Examples are ordered searching which is O(log n), polynomial evaluation is O(n), sorting is
O(n log n), and matrix multiplication which is O(n 2·81).
The second group contains problems whose best known algorithms are non polynomial.
Examples include the traveling salesperson and the knapsack problem for which the best algorithms
given in this text have a complexity O(n22n) and O(2n/2) respectively.
In the quest to develop efficient algorithms, no one has been able to develop a polynomial
time algorithm for any problem in the second group. This is very important because algorithms whose
computing time is greater than polynomial very quickly require such vast amounts of time to execute
that even moderate size problems cannot be solved.
The theory of NP-completeness does not provide a method of obtaining polynomial time
algorithms for problems in the second group. Nor does it say that algorithms of this complexity do not
exist. Instead, we show that many of the problems for which there is no known polynomial time
algorithm are computationally related.
In fact, we shall establish two classes of problems. They are NP-hard and NP-complete. A
problem which is NP complete will have the property that it can be solved in polynomial time iff 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 NP complete problems can be solved
in polynomial time. So, all NP-complete problems are NP-hard but all NP-hard problems are not NP-
complete.
Nondeterministic Algorithms
Deterministic Algorithms: The result of every operation is uniquely defined. These agree with the
way programs are executed on a computer.
Nondeterministic Algorithms: These algorithms contain operations whose outcome are not uniquely
defined but are limited to a specified set of possibilities. The machine executing such operations is
allowed to choose any one of these outcomes subject to a termination condition. To specify such
algorithms, we use the following new functions:
1. Choice (S) -arbitrarily chooses one of the elements of set S
2. Failure () -signals an unsuccessful completion
3. Success () -signals a successful completion.
Example 1: Consider the problem of searching for an element x in a given set of elements A[1: n] , n
≥ 1. We are required to determine an index j such that A [j] = x or j = 0 if x is not in A.
A nondeterministic algorithm for this is
Algorithm NSearch( A,n,x)
{
j :=choice(l,n);
if A[j] = x then {write(j); Success();}
write(0); Failure();
}
Nondeterministic Search
Example 2: [Sorting] Let A[i], 1≤ i≤ n be an unsorted set of positive integers. The non deterministic
algorithm NSort (A, n) sorts the numbers into non decreasing order and then outputs them in this
order. An auxiliary array B[l:n)] is used for convenience.
Algorithm NSort(A, n)
// sort n positive integers
{
for i:=1 to n do B[i]=0; //Initialize B[].
For i:=1 to n do
{ j :=Choice(l,n)
if B [j]≠ 0 then Failure();
B[j]:=A[i];
}
for i :=1 to n - 1 do //verify order
if B[i] > B[i + 1] then Failure();
write(B[1:n]);
Success();
}

Nondeterministic sorting
Decision Problem: Any problem for which the answer is either zero or one is called a decision
problem. Any problem that involves the identification of an optimal value of a given cost function is
called as an optimization problem.
Example 3 [Max Clique] A maximal complete sub graph of a graph G = (V, E) is a clique. The size of
the clique is the number of vertices in it. The max clique problem is to determine the size of a largest
clique in G. The corresponding decision problem is to determine if G has a clique of size at least k for
some given k.
Example 4 [0/l-Knapsack] The knapsack decision problem is to determine if there is a 0/ 1
assignment of values to xi 1 ≤ i ≤n such that ∑ pi xi ≥ Rand ∑ wi xi ≤ M. R is a given number. The pi’s
and wi‘s are nonnegative numbers.
The complexity of a nondeterministic algorithm
Definition: The time re1uired by a nondeterministic algorithm performing on any given input is the
minimum number of steps needed to reach a successful completion if there exists a sequence of
choices leading to such a completion. In case successful completion is not possible then the time
required is O(1). A nondeterministic algorithm is of complexity O(f(n)) if for all inputs of size, n, n
≥n0, that result in a successful completion the time required is at most cf(n) for some constants c and
n0.
Algorithm SUM(A, n, M)
{
S :=1 //S is an M + 1 bit word. Bit zero is 1.
for i :=1 to n do
S :=S or Shift(S, A[i])
if Mth bit in S is 1 then
write ('A subset sums to M);
else
write ('no subset sums to M)
}
Deterministic sum of subsets

Knapsack decision problem: Procedure DKP is a nondeterministic polynomial time algorithm for
the knapsack decision problem.
Algorithm DKP(P, W, n, M, R, X)
{
W :=0; P:=0;
for i := l to n do
{
X[i]:=Choice (0, 1)
W := W+x[i]*w[i];
P := P+x[i]*p[i];
}
if ((W<M) or(P<R)) then Failure();
else Success();
}
Nondeterministic Knapsack problem

The time complexity of DKP() is O(n). If q is the input length using binary representation , the time is
O(q).
Max clique

Algorithm DCK ( G, n, k)
{
S := //S is an initially empty set
for i :=1 to k do //select k distinct vertices
{
t :=Choice (1,n);
if t S then Failure();
S - S U {t } //add t to set S
}
// At this point S contains k distinct vertex indices
for all pairs (i, j) such that i S, j S and i ≠ j do
if (i, j) is not an edge of the graph G then Failure();
Success();
}
Nondeterministic clique

Satisfiability : Let x 1 , x2 , … , denote boolean variables (their value is either true or false). Let xi
denote the negation of xi. A literal is either a variable or its negation. A formula in the propositional
calculus is an expression that can be constructed using literals and the operations and and or.
Examples of such formulas are (x1 /\ x2 ) V (x3 /\ x4 ); (x3 V X4 ) /\ (x 1 V x 2). V denotes or and /\
denotes and. A formula is in conjunctive normal form (CNF) iff it is represented as /\ ci, where the
ci’s are clauses each represented as V lij. The lij are literals. It is in disjunctive normal form (DNF) iff
it is represented as Vci, and each clause ci is represented as /\ lij. Thus (x1 /\ x2 ) V (x3 /\ x4 ) is in
DNF while (x3 V x4 ) /\ (x1 V x2 ) is in CNF. The satisfiability problem is to determine if a formula is
true for some assignment of truth values to the variables. CFN-satisfiability is the satisfiability
problem for CNF formulas. It is easy to obtain a polynomial time nondeterministic algorithm that
terminates successfully if and only if a given propositional formula E(x1 , ... , xn) is satisfiable. Such
an algorithm could proceed by simply choosing one of the 2n possible assignments of truth values to
(x1 , ..., Xn) and verifying that E(x1 , ..., Xn) is true for that assignment.
The following Algorithm EVAL does this. The nondeterministic time required by the algorithm is
O(n) to choose the value of (x1 , … , xn) plus the time needed to deterministically evaluate E for that
assignment. This time is proportional to the length of E.
Algorithm EVAL(E, n)
//Determine if the propositional formula E is satisfiable. The variables are x1,x2,…xn
{
for i :=1 to n do //choose a truth value assignment
xi:= Choice (true, false);
if E(x1 , …, xn) is true then Success(); //satisfiable
else Failure();
}
Nondeterministic satisfiability

The Classes NP-hard and NP-complete


An algorithm A is of polynomial complexity if there exists a polynomial p( ) such that the computing
time of A is O(p(n)) for every input of size n.
Definition: P is the set of all decision problems solvable by a deterministic algorithm in polynomial
time. NP is the set of all decision problems solvable by a nondeterministic algorithm in polynomial
time.
Since deterministic algorithms are just a special case of nondeterministic ones, we can conclude that P
~ NP. What we do not know, and what has become perhaps the most famous unsolved problem in
computer science is whether P = NP or P ≠ NP.
Definition: Let L 1 and L2 be problems. L1 reduces to L2 (also written L1 L2) if and only if there is
a way to solve L1 by a deterministic polynomial time algorithm using a deterministic algorithm that
solves L2 in polynomial time.
This definition implies that if we have a polynomial time algorithm for L2 then we can solve L1 in
polynomial time. One may readily verify that is a transitive relation (i.e. if L1 L2 and L2 L3
then L1 L3).
Definition A problem L is NP-hard if and only if satisfiability reduces L (satisfiability L). A
problem L is NP-complete if and only if L is NP-hard and L NP.
It is easy to see that there are NP-hard problems that are not NP-complete. Only a decision problem
can be NP-complete. However, an optimization problem may be NP-hard. Furthermore if L 1 is a
decision problem and Li an optimization problem, it is quite possible that L1 L2.
One may trivially show that the knapsack decision problem reduces to the knapsack optimization
problem. For the clique problem one may easily show that the clique decision problem reduces to the
clique optimization problem. In fact, we can also show that these optimization problems reduce to
their corresponding decision problems (see exercises). Yet, optimization problems cannot be NP-
complete while decision problems can.
There also exist NP-hard decision problems that are not NP-complete.
Example 5: As an extreme example of an NP-hard decision problem that is not NP-complete consider
the halting problem for deterministic algorithms. The halting problem is to determine for an arbitrary
deterministic algorithm A and an input I whether algorithm A with input I ever terminates (or enters
an infinite loop). It is well known that this problem is undecidable. Hence, there exists no algorithm
(of any complexity) to solve this problem. So, it clearly cannot be in NP. To show satisfiability
halting problem simply construct an algorithm A whose input is a propositional formula X. If X has n
variables then A tries out all 2n possible truth assignments and verifies if X is satisfiable. If it is then A
stops. If X is not satisfiable then A enters an infinite loop. Hence, A halts on input X iff X is
satisfiable. If we had a polynomial time algorithm for the halting problem then we could solve the
satisfiability problem in polynomial time using A and X as input to the algorithm for the halting
problem. Hence, the halting problem is an NP-hard problem which is not in NP.
Definition Two problems L1and L2 are said to be polynomially equivalent iff L1 L2 and L2 L1 .
In order to show that a problem, L2 is NP-hard it is adequate to show L1 L2 where L 1 is some
problem already known to be NP-hard. Since is a transitive relation, it follows that if satisfiability
oc L1 and L1 L2 then satisfiability L2. To show an NP-hard decision problem NP-complete we
have just to exhibit a polynomial time nondeterministic algorithm for it. Later sections will show
many problems to be NP-hard. While we shall restrict ourselves to decision problems, it should be
clear that corresponding optimization problems are also NP-hard.

COOK'S THEOREM
Cook's theorem states that satisfiability is in P iff P = NP.
Proof: We have already seen that satisfiability is in NP. Hence, if P = NP then satisfiability is in P. It
remains to be shown that if satisfiability is in P then P = NP. In order to prove this latter statement, we
shall show how to obtain from any polynomial time nondeterministic decision algorithm A and input I
a formula Q(A, I) such that Q is satisfiable iff A has a successful termination with input I. If the length
of I is n and the time complexity of A is p(n) for some polynomial p( ) then the length of Q will be
O(p3(n) log n) = O(p4(n)). The time needed to construct Q will also be O(p3(n) log n). A deterministic
algorithm Z to determine the outcome of A on any input I may be easily obtained. Z simply computes
Q and then uses a deterministic algorithm for the satisfiability problem to determine whether or not Q
is satisfiable. If O(q(m)) is the time needed to determine if a formula of length m is satisfiable then
the complexity of Z is O(p3(n) log n + q(p3(n) log n)). If satisfiability is in P then q(m) is a polynomial
function of m and the complexity of Z becomes O(r(n)) for some polynomial r( ). Hence, if
satisfiability is in P then for every nondeterministic algorithm A in NP we can obtain a deterministic Z
in P. So, the above construction will show that if satisfiability is in P then P = NP.

15. Additional Topics


1. Multiplication of two large integers using Divide and Conquer.
2. Greedy solution to Optimal Storage on tapes.
3. Solution to Knapsack problem using Backtracking.
4. Discussion of NP-Complete problems.
16. University question papers
II B.Tech I-Semester Regular Examinations, November 2017
DESIGN AND ANALYSIS OF ALGORITHMS
(Common to Computer Science and Engineering, Information Technology and Computer Science
and Systems Engineering)
Time: 3 hours Max. Marks: 80
Answer any FIVE questions
All questions carry equal marks
---

1. Define space complexity. Explain the same with an example.

2. a) Compare Merge sort & Quick sort for the given data sets.
10, 30, 15, 45, 25, 30, 35, 20, 30, 40, 50
b) Compare their time complexities.

3. Explain the Kruskal’s algorithm with an example and analyze its time complexity.

4. a) Write a procedure DIVIDE(b,T) to implement SPLIT(b,s) instruction which partitions a 2-3 tree T
about a leaf ‘b’ so that all leaves to the left of ‘b’ and ‘b’ itself is in one 2-3 tree and leaves to the
right of ‘b’ are in a second 2-3 tree.
b) Prove that the above procedure takes time O(height(T)) and the order of the tree is preserved.

5. Discuss the dynamic programming solutions for the problems of reliability design and traveling
sales person problem.

6. a) Show that the in-order and post order sequences of a binary tree uniquely define the binary tree.
b) Write a detailed note on depth-first traversal.

7. Explain in detail how the technique of backtracking can be applied to solve the 8 queen’s
problem. Present the required algorithms.

8. Write a program schema for a LIFO Branch & Bound for a Least-Cost answer node.

###
II B.Tech I-Semester Regular Examinations, November 2011
DESIGN AND ANALYSIS OF ALGORITHMS
(Common to Computer Science and Engineering, Information Technology and Computer Science
and Systems Engineering)
Time: 3 hours Max. Marks: 80
Answer any FIVE questions
All questions carry equal marks
---
1. A complex valued matrix X is represented by a pair of matrices (A,B) where A and B contain real
values. Write an algorithm that computes the product of two complex valued matrices (A,B) and
(C,D) where (A,B) * (C,D) = (A+iB) * (C+iD) = (AC-BD) + i (AD+BC). Determine the number
of additions and multiplications if the matrices are all n×n.
2. a) Devise a version of Merge sort algorithm which performs sorting in-place.
b) Devise a ternary search algorithm which first tests the element at position n/3 for equality with
some value x and then possibly checks the element at 2n/3 and either discovers x or reduces the
set size to one third of the original. Compare this with the binary search.
3. Explain the Job sequencing with dead line algorithm and also find the solution for the instance
n=7, (P1,P2,…. ,P7)=(3,5,20,18,1,6,30) and (D1,D2,…., D7)=(1,3,4,3,2,1,2).
4. a) Write an algorithm for checking whether an array H [1,2,….,n] is a heap or not.
b) Determine the time efficiency of the above algorithm.
5. Using the algorithm OBST, compute W(i,j), R(i,j) and C(i,j), 0<=i<j<=4 for the identifier set
(a1,a2,a3,a4)=(end, goto, print, stop) with p(1)=1/20, p(2)=1/5, p(3)=1/10, p(4)=1/20; q(0)=1/5,
q(1)=1/10, q(2)=1/5, q(3)=1/20 and q(4)=1/20. Using the R(i,j)’s construct the OBST.
6. Write a non-recursive algorithm for the inorder traversal of binary tree T. Each node has 4 fields:
LCHILD, DATA, PARENT, RCHILD. Your algorithm should take no more than O(1) additional
space and O(n) time for an n-node tree. Show that this is the time taken by your algorithm.
7. Compare and contrast
a) Brute force approach Vs Backtracking
b) Fixed Vs variable tuple size formulation.

8. Consider the LCBB traveling sales person algorithm described using the dynamic state space tree
formulation. Let A and B be nodes. Let B be the child of A. If the edge (A,B) represents the
inclusion of edge <i,j> in the tour, then in the reduced matrix for B all entries in row i and column
j are set to ∞. In addition, one more entry is set to ∞. Obtain an efficient way to determine this
entry.

###
II B.Tech I-Semester Regular Examinations, November 2010
DESIGN AND ANALYSIS OF ALGORITHMS
(Common to Computer Science and Engineering, Information Technology and Computer Science
and Systems Engineering)
Time: 3 hours Max. Marks: 80
Answer any FIVE questions
All questions carry equal marks
---

1. Define time complexity. Describe different notations used to represent these complexities.
Illustrate.

2. a) Analyze the average case time complexity of Quick sort.


b) If k is a non-negative constant, then show that the solution to the given recurrence equation, for n
a power of 2 is T(n) =3knlog3 – 2kn.
T(n) = k, n=1
3T(n/2)+kn, n>1

3. a) Write Prim’s algorithm under the assumption that the graphs are represented by adjacency lists.
b) Analyze precisely the computing time and space requirements of this new version of Prim’s
algorithm using adjacency lists.

4. a) What are Dictionaries? Explain.


b) What is a balanced tree? Differentiate between 2-3 trees and AVL trees.

5. a) What do you mean by forward and backward approach of problem solving in Dynamic
programming?
b) What are the differences between the Greedy and Dynamic programming methods of problem
solving?

6. a) Present an algorithm for depth first search traversal. Explain with an example.
b) Write a detailed note on breadth first traversal.

7. a) What is graph coloring? Present an algorithm which finds all m-colorings of a graph.
b) Draw the state space tree for m-closing graph using a suitable graph.

8. Present a program schema for a FIFO Branch & Bound search for a Least-Cost answer node.

###

Code No: RR-210504


II B.Tech I-Semester Regular Examinations, November 2010
DESIGN AND ANALYSIS OF ALGORITHMS
(Common to Computer Science and Engineering, Information Technology and Computer Science
and Systems Engineering)
Time: 3 hours Max. Marks: 80
Answer any FIVE questions
All questions carry equal marks
1. Define space complexity. Explain the same with an example.

2. a) Compare Merge sort & Quick sort for the given data sets.
10, 30, 15, 45, 25, 30, 35, 20, 30, 40, 50
c) Compare their time complexities.

3. Explain the Kruskal’s algorithm with an example and analyze its time complexity.
4. a) Write a procedure DIVIDE(b,T) to implement SPLIT(b,s) instruction which partitions a 2-3 tree T
about a leaf ‘b’ so that all leaves to the left of ‘b’ and ‘b’ itself is in one 2-3 tree and leaves to the
right of ‘b’ are in a second 2-3 tree.
b) Prove that the above procedure takes time O(height(T)) and the order of the tree is preserved.

5. Discuss the dynamic programming solutions for the problems of reliability design and traveling
sales person problem.

6. a) Show that the inorder and post order sequences of a binary tree uniquely define the binary tree.
c) Write a detailed note on depth-first traversal.

7. Explain in detail how the technique of backtracking can be applied to solve the 8 queen’s
problem. Present the required algorithms.

8. Write a program schema for a LIFO Branch & Bound for a Least-Cost answer node.

Code No: RR-210504


II B.Tech I-Semester Regular Examinations, November 2010
DESIGN AND ANALYSIS OF ALGORITHMS
(Common to Computer Science and Engineering, Information Technology and Computer Science
and Systems Engineering)
Time: 3 hours Max. Marks: 80
Answer any FIVE questions
All questions carry equal marks
---
1. Write the non-recursive algorithm for finding the Fibonacci sequence and derive its time
complexity.

2. a) Design a Divide and Conquer algorithm for computing the number of levels in a binary tree.
b) Compute the efficiency of the above algorithm.

3. Formulate an algorithm for a spanning tree problem in terms of a sequence of set operations in
which take G as the undirected graph ; S as the undirected tree; V as the number of vertices; E as
the number of edges T as a set used to collect the edges of the final minimum spanning tree; C as
the cost function for the graph G given by { e Σ ε E1 C(e) for the sub-graph G1 = (V1,E1) of G.
Use set VS for the vertex set of the forest to write the minimum cost spanning tree algorithm.

4. a) Write a pseudo code for constructing 2-3 trees for a given list of n integers.
b) Explain the above algorithm for a list of 10 integers.

5. a) Using Divide and Conquer approach coupled with the set generation approach, show how to
obtain an O(2n/2) algorithm for 0/1 Knapsack problem.
b) Develop an algorithm that uses this approach to solve the 0/1 Knapsack problem.
c) Compare the run time and storage requirements of this approach.

6. a) Write a non-recursive algorithm for the pre-order traversal of a binary tree T, using stacks.
b) What are the time and space requirements of your algorithm?
7. Define the following terms: state space, explicit constraints, implicit constraints, problem state,
solution states, answer states, live nod, E-node, dead node, bounding functions.

8. Draw the portion of a state space tree generated by FIFOBB, LCBB and LIFOBB for the job
sequencing with deadlines instance n=5, (p1,p2,…,p5)=(6,3,4,8,5), (t1,t2,….,t5)=(2,1,2,1,1) and
(d1,d2,…,d5)=(3,1,4,2,4). What is the penalty corresponding to an optimal solution? Use a variable
tuple size formulation and ĉ (.) and u (.).
###

Code No: RR-210504


II B.Tech I-Semester Regular Examinations, November 2010
DESIGN AND ANALYSIS OF ALGORITHMS
(Common to Computer Science and Engineering, Information Technology and Computer Science
and Systems Engineering)
Time: 3 hours Max. Marks: 80
Answer any FIVE questions
All questions carry equal marks
---
1. A complex valued matrix X is represented by a pair of matrices (A,B) where A and B contain real
values. Write an algorithm that computes the product of two complex valued matrices (A,B) and
(C,D) where (A,B) * (C,D) = (A+iB) * (C+iD) = (AC-BD) + i (AD+BC). Determine the number
of additions and multiplications if the matrices are all n×n.
2. a) Devise a version of Merge sort algorithm which performs sorting in-place.
b) Devise a ternary search algorithm which first tests the element at position n/3 for equality with
some value x and then possibly checks the element at 2n/3 and either discovers x or reduces the
set size to one third of the original. Compare this with the binary search.
3. Explain the Job sequencing with dead line algorithm and also find the solution for the instance
n=7, (P1,P2,…. ,P7)=(3,5,20,18,1,6,30) and (D1,D2,…., D7)=(1,3,4,3,2,1,2).
4. a) Write an algorithm for checking whether an array H [1,2,….,n] is a heap or not.
b) Determine the time efficiency of the above algorithm.
5. Using the algorithm OBST, compute W(i,j), R(i,j) and C(i,j), 0<=i<j<=4 for the identifier set
(a1,a2,a3,a4)=(end, goto, print, stop) with p(1)=1/20, p(2)=1/5, p(3)=1/10, p(4)=1/20; q(0)=1/5,
q(1)=1/10, q(2)=1/5, q(3)=1/20 and q(4)=1/20. Using the R(i,j)’s construct the OBST.
6. Write a non-recursive algorithm for the inorder traversal of binary tree T. Each node has 4 fields:
LCHILD, DATA, PARENT, RCHILD. Your algorithm should take no more than O(1) additional
space and O(n) time for an n-node tree. Show that this is the time taken by your algorithm.
7. Compare and contrast
c) Brute force approach Vs Backtracking
d) Fixed Vs variable tuple size formulation.

8. Consider the LCBB traveling sales person algorithm described using the dynamic state space tree
formulation. Let A and B be nodes. Let B be the child of A. If the edge (A,B) represents the
inclusion of edge <i,j> in the tour, then in the reduced matrix for B all entries in row i and column
j are set to ∞. In addition, one more entry is set to ∞. Obtain an efficient way to determine this
entry.

17. Question Bank


Unit-1
1. a) Define an algorithm. Describe the Characteristics of the algorithm.
b) What do you mean by the input size of a problem? Explain its significance.
2. Describe the Performance analysis in detail.
3. a) Define time Complexity. Describe different notations used to represent these complexities.
b) Write the non-recursive algorithm for finding the Fibonacci sequence and derive its time
complexity.
4.a) Write an algorithm to find a largest of given ‘n’ numbers. Derive its time complexity using
big ‘oh’
b) Explain about amortization.
5. a) Show that f(n)+g(n) = o(n2) where f(n)=3n2-n+4 and g(n)= n log n+5
b) Show that f (n) =4n2 – 64n + 288 =Ω (n2).
6. a) Write and explain the control abstraction for divide and conquer.
b) Explain Binary Search and give its algorithm.

7. a) Sort the following elements using Merge Sort: 10, 30, 15, 45, 25, 30, 35, 20, 30, 40, 50
b) Write the Algorithm for Merge sort and derive its time complexity.
8. a) Write the algorithm for quick sort.
b) Derive its time complexity for Best, Worst and Average cases.
9. a) Trace the quick sort algorithm to sort the list C, O, L, L, E, G, E in alphabetical order.
b) Give an instance, where the quick sort algorithm has worst case time complexity.
10. a) If K is a non negative constant, then prove that the recurrence

T(n)= K n=1
3T (n/2) + kn n>1

has the following solution (for n a power of 2); T (n) = 3kn log3 – 2kh.

b) Solve the following recurrence relation using substitution method.

T(n) = 1 n≤4
2T ( sqrt of n ) + log n n>4

Unit-II
1. a) List various tree traversal techniques. Explain with an example.
b) Write non-recursive algorithm for in-order binary tree traversal.
2. Develop algorithms for UNION and FIND using weighting rule and collapsing rule respectively.
3. a) Write and explain BFS and DFS Algorithms for Graphs.
b) Write BFS and DFS order for the following Graph.
4. a) What are biconnected components? Explain.
b) Write an algorithm to determine the articulation points and the biconnected components of a
graph G.
5. a) Explain in detail AND/OR Graphs.
b) Explain in detail about Game Trees.

Unit-III
1. a) Explain the terms feasible solution, optimal solution and objective function.
b) Find an optimal solution to the knapsack instance n=7 m=15.
(P1, P2…..P7)= (10,5,15,7,6,18,3), and (W1, W2,…..W7) = (2,3,5,7,1,4,1).

2. a) Explain the prims algorithm with the appropriate example.


b) Write the prims algorithm to find the minimum cost spanning tree.
3. What is a spanning tree? What are the applications of it.
4. a) Explain in the control abstraction for greedy method.
b) Write kruskal’s algorithm to find the maximum spanning tree.
5. a) Explain the algorithm for Job sequencing with deadlines. Applying the same find the solution
for the instance n=4, (P1, P2…..P4) = (100.10, 15.27), and (d1, d2….d4) = (2, 1, 2, 1).
6. a) What is Principle of optimality? Explain its significance. b) Multistage Graphs
7. a) Define an Optimal Binary Search Tree (OBST)
b) Construct an OBST when n=4, such that a1< a2< a3< a4. With (q0, q1, q2, q4) =
(1/8, 3/16, 1/16, 1/16, 1/16) and (p1, p2, p3, p4) = (1/4, 1/8, 1/16, 1/16).
8. a) Define merge and purge rules.
b) Consider the Knapsack instance n=6,
m=165, (p1, p2…..p6) = (w1, w2…w6) = (100, 50, 20, 10, 7, 3) generate the Si sets containing
the pair (pi, wi) and thus find the optimal solution.
9 a) Explain the OBST algorithm.
b) Write an Algorithm for 0/1 Knapsack problem using Dynamic programming.
10. a) What is Travelling Sales Person problem and what are its applications?
b) Find the shortest tour of a TSP for following instance using Dynamic programming.
A B C D
A 0 10 15 20
B 5 0 9 10
6 13 0 12
D 8 8 9 0
11. a) Explain in detail the Reliability Design problem.
b) Design three stage system with device types D1, D2, D3. The costs are Rs. 30, Rs. 15 and Rs.
20 respectively. The Costs of the system into be not more than Rs. 105. The reliability of each
device type is 0.9, 0.8 and 0.5 respectively.
UNIT-IV
1. a) Compare and contrast Brute Force approach and Back tracking.
b) Write the control abstraction of Backtracking.
2. a) Define the following terms. Explicit constraints, Implicit constraints, state space tree, problem
state, Answer state, live node, E-node, dead node. Back tracking and Branch and Bound.
3. a) Compare and contrast fixed vs variable tuple size formulation.
b) Draw the tree organization of the 4-queens solution space.Number the nodes using
i) BFS ii) DFS iii) D-Search
4. a) Explain in detail how Backtracking can be applied to solve the 8-queens problem.
b) Write an algorithm for n-queens problem.
5. a) Explain in detail the Sum of Subsets problem with an example.
b) Write recursive Backtracking algorithm for Sum of Subsets problem.
6. Consider the following instance of Sum of Subsets problem:
W={15, 7, 20, 5, 18, 10, 12} and m=35. Find all possible Sub Sets of w that sum to m. Draw the
portion of the state space tree that is generated.
7. a) Draw the state space tree for the following graph when m=3(m-Coluring graph problem).
1 2

4 3
b) Devise a Backtracking algorithm for m-Coloring graph problem.

8. a) Define Hamiltonian cycle. Give an example.


b) Write a Backtracking algorithm fro finding all Hamiltonian cycles in a given graph.

9. a) Define the term Branch and Bound and Explain it with an example.
b) Write an algorithm for LC-Search.
10. Draw the portion of the State Space Tree generated by FIFO BB, LCBB and LIFOBB for the job
sequencing with deadlines instance, n=4, (p1, d1, t1) = (5, 1, 1), (p2, d2, t2) = (10, 3, 2), (p3, d3, t3) =
(6, 2, 1) and (p4, d4, t4) = (3, 1, 1). What is the penalty corresponding to an optional solution.

11. Draw the portion the State Space Tree generated by LCBB and FIFOBB for the following
Knapsack instance.
N=4, (p1, p2, p3, p4) = (10, 10, 12, 18), (w1, w2, w3, w4) = (2, 4, 6, 9) and m=15.

12. Consider the Traveling Salesperson instance defined by the cost matrix.

∞ 20 30 10 11
15 ∞ 16 4 2
3 5 ∞ 2 4
19 6 18 ∞ 3
16 4 7 16 ∞

a) Obtain the reduced cost matrix.


b) Using a State Space Tree formulation, obtain the portion of the State Space tree that will be
generated by LCBB. Label each node by its Ĉ value, write out the reduced matrices
corresponding to each of these nodes.
13. Solve the above problem using reduced matrix method and the Dynamic State Space Tree
approach.

UNIT – V
1. Write about the theory of NP-Completeness.
2. Write short notes on Deterministic and Non-Deterministic algorithm with an example.
3. Explain Non Deterministic search and sorting algorithms.
4. Write about Decision and Optimization problems with examples.
5. a) Explain the Clique problem and write the algorithm for the same.
b) Write a Non-Deterministic Knapsack algorithm.
6. Explain the Satisfiability problem and write the algorithm for the same.
7. a) With a neat diagram, explain the relevance of NP-hard and NP-complete problems.
b) What is meant by Halting problem. Explain with an example.
8. State and explain Cook’s Theorem.
1 8. Assignment sheets
Assignment – I
-----------------------------------------------------------------------------------------------------------
1 . a) Define Recursion.
b) Write Recursive and iterative algorithms for the following:
i) Factorial of a number ii) Printing nth Fibonacci number iii) Towers of Hanoi
c) Compute Time complexity for the above algorithms.

2. Solve the recurrence relation T(n) = T(1) n=1


aT (n/b) + f(n) n>1.

for the following choice of a,b and f(n). Using Master Method & Substitution Method.
a)
a=1, b=2 and f(n)=cn
b)
a=5, b=4 and f(n) = cn2
c)
a=28, b=3 and f(n)=cn3
3 . a) Explain the following sorting techniques:
i) Selection sort ii) insertion sort iii) Quick sort iv) Merge sort
b) Sort the following set of numbers using above sorting techniques:
15 8 45 14 43 70 49 25 35 17 28 12 52 20 22

4. a) Define min and max Heap.


b) Construct min and max heap for: 15 9 19 20 35 40 12 10 8 16
c) Explain how sorting can be done using heaps.

5. a) Explain Probabilistic Analysis.


b) Explain Amortized complexity in detail.

Assignment – II
-----------------------------------------------------------------------------------------------------------
1. a) For the following Binary Tree write in-order, pre-order and postorder traversals.

b) Construct the binary tree from the following


in-order: EICFBGDJHK
pre-order : BCEIFDGHJK
c) Write and trace non recursive algorithms for all tree traversal techniques.

2. a) Explain the tree representation of Disjoint sets.


b) Write simple Union and Find algorithms. Discuss merits and demerits of these algorithms.
c) Write and explain algorithms for Weighted Union and Collapsing Find.

3. a) Define spanning tree, connected components and articulation points.


b) Identify articulation points and associated biconnected components for the graph given below
c) Find articulation points through L[].

4. Write and explain algorithms for determining L[] and biconnected components.
5. a) Explain AND/OR Graphs.
b) Explain Game Tree.

Assignment – III
-----------------------------------------------------------------------------------------------------------
1. Find an optimal solution to the Knapsack instance n=7, m=15, (p1, p2,….p7) = (10, 5, 15, 7, 6,
18, 3) and (w1, w2, w3…..w7) = (2,3,5,7,1,4,1).

2. Find an optimal solution to the following Job sequencing with deadlines instance when n=7, (p1,
p2,….p7) = (3, 5, 20, 18, 1, 6, 30) and (d1, d2, …..d7) = (1, 3, 4, 3, 2, 1, 2).

3. Compute a minimum cost spanning tree for the following graph using
a) Prim’s Algorithm b) Kruskal’s Algorithm.
6
2 7 21
11 8 10 6
15
12 4 11
1
17 7
2 14
13 8
5
3 5

4. Find a minimum cost path from s to t in the following multistage graph. Do this first using forward
approach and then using backward approach

5. Find an optimal sequence of parenthesization of a matrix chain product whose sequence of


dimensions I (10, 20, 50, 1, 100).

6. Construct the Optimal Binary Search Tree when n=4 such that a1<a2<a3<a4 with
(q0, q1, q2, q3, q4) = (¼, 3/16, 1/16, 1/16, 1/16) and
(p1, p2, p3, p4) = (1/4, 1/8, 1/16, 1/16)

7. Find the optimal solution for the 0/1 knapsack (p) instance
when m=165, n=6, (p1,….p6) = (w1,…..w6) = (100, 50, 20, 10, 7, 3)

8. Find the shortest tour of a TSP for following instance using dynamic programming.

A B C D
A ∞ 12 5 7
B 11 ∞ 13 6
C 4 9 ∞ 18
D 10 3 2 ∞

10. a) Explain in detail the Reliability Design problem.


b) Design a three stage system with device types D1, D2, D3. The costs are Rs. 30, Rs. 15 and Rs. 20
respectively. The Cost of the system is to be not more than Rs. 105. The reliability of each device type is
0.9, 0.8 and 0.5 respectively.
Assignment – IV
-----------------------------------------------------------------------------------------------------------
1. Consider the following Sum of Subsets problem instance.
n=6, m=30, and w[1:6] ={5, 10, 12, 13, 15, 18}.
Find all possible subsets of w that sum to m. Draw the portion of the state space tree that is generated.
2. a). Draw the tree organization of the 4-queens solution space. Number the nodes using
i) BFS ii) DFS iii) D-Search
b) Write a Backtracking algorithm for finding all Hamiltonian cycles in a given graph.
3. Draw the portion of the state space tree generated by FIFOBB, LCBB and LIFOBB for the job
sequencing with deadlines instance: N=5, (p1,p2,….p5) = (6, 3, 4, 8, 5). (t1, t2…..t5) = (2, 1, 2, 1, 1) and
(d1, d2,….d5) = (3, 1, 4, 2, 4).
What is the penalty corresponding to an optimal solution. Use a variable tuple size formulation.
4. Draw the portion of the state space tree generated by LCBB for the following Knapsack instances.
a) n=5 (p1, p2….p5) = (10, 15, 6, 8, 4) (w1, w2,……w5) = (4, 6, 3, 4,) and m=12
b) n=5 (p1, p2,…p5) = (w1, w2,….w5) = (4, 4, 5, 8, 9) and m=15.

5. Consider the traveling sales person instance defined by the cost matrix.
∞ 7 3 12 8
3 ∞ 6 14 9
5 8 ∞ 6 18
9 3 5 ∞ 11
18 14 9 8 ∞
a) Obtain the reduced cost matrix
b) Using state space tree formulation, obtain the portion of state space tree that will be generated by
LCBB. Label each node by its Ĉ value. Write out the reduced matrices corresponding to each of
these nodes.
c) Do part (b) using the reduced matrix method and Dynamic state space tree approach.
Assignment – V
-----------------------------------------------------------------------------------------------------------
1. Differentiate tractable and intractable problems.
2. a) Explain nondeterministic algorithms.
b) Write and explain non deterministic algorithms for searching and sorting.
3. Explain the relationship between P and NP Classes with a neat diagram.
4. a) Define NP Complete and NP Hard problem.
b) Explain some NP Complete problems.
5. State and prove Cook’s Theorem.
19. Objective Questions
Unit-I
1. The time complexity of Strassen’s Matrix multiplication algorithm is__
[ ]
a) O(log n 7 ) b) O(log 7 n ) c) O(7 l o g n ) d) None

2. If algorithm takes O(n 2 ), it is faster for sufficiently larger n than if it had taken
___________ [ ]
a)O(log n) b) O(n) c) O(n log n) d) O(2 n )

3. The worst case time complexity of Quick sort is__________


[ ]
a) O(n log n) b) O(n 2 ) c) O(log n) d) None

4. The Average Time Complexity of Binary Search is______


[ ]
a) O(n log n) b) O(n 2 ) c) O(log n) d) None

5. The Average Time Complexity of Merge Sor t is ______


[ ]
a) O(n log n) b) O(n 2 ) c) O(log n) d) None

6. Which notation gives t he lower bound of the function f(n).________


[ ]
a) O b) o c) w d)NONE
7. In Quick sort what is t he characteristic of the pivot element_______

8. Time Complexity, T(p), of a program is the sum of _____________ and


_____________

9. The f(n)= w(g(n)), if _____________

10. Sort the following in ascending order.


O(n), O( 1) , O(2 n ), (log n) , O(n 2 ), O(n log n), O(n 3 ), O(3 n )

11. If P the actual problem is divided into k sub problems P 1 ,P 2 ,….P k then the
computing time of P, T(n) is given by
______________________________________________________________ where n i
is the number of input present in P i .

12. Step by step procedure to solve a problem is known as____________

13. Define profiling.

14. The maximum stack space needed by Quick sort is ________

15. The data structure used to execute the recursive procedures is _____________

16. The condition for mer ge sort is_________________________


17. The time Complexity of conventional matrix multiplication algorithm
is___________

18. Define Algorithm

19. The amount of comput er memory required to execute a program is known


as________

20. Write the recurrence relation to find the time complexity of recursive fibonacci
series program__________________________.

Unit-II
1. The maximum no. of nodes in a 2-3 tree of Height k is _______________

2. Write Weighted rule for Union.

3. Write the Collapsing rule for Find.

4. What is the time complexity of Weighted Union algorithm._____________

5 In Reliability design problem Ui is called as [ ]


a. The upper bound on no. of copies
b. The lower bound on no. of copies
c. Reliability d) none

6 The objective function of Traveling sales person problem is [ ]


a. Cost of the tour must be maximum
b. Time of the tour must be minimum
c. Time of the tour must be maximum
d. Cost of the tour must be minimum
7 The node for which the children is currently being generated is known as
a) Dead node b)E-node [ ]
c) Live node d) None

8 The node for which all the children are generated is known as
a)Dead node b)E-node [ ]
c)Live node d)None

9 The node for which the children is yet to be generated is known as


a)Dead node b)E-node [ ]
c)Live node d)None

10 ------------- Constraints are rules that restrict each xi to take on values only from a given set
[ ]
a)Explicit Constraints b)Implicit Constraints
c)Implied Constraints d)None
11 _____ determines which of the tuples (sets) in the solution space actually satisfy the criterion function
[ ]
a) Explicit Constraints b)Implicit Constraints
c) Implied Constraints d)None
12 Depth First node generation with bounding functions is called___[ ]
a) Dynamic Programming b) Branch and Bound
c) Back Tracking d) None
13 _______ functions are used to kill the nodes of a state space tree [ ]
a) Explicit Functions b)Implicit Functions c) Bounding Functions d)None
14 Define Problem state.

15 Write the formula to test whether the two queens are in the same diagonal or not.
16 place(k, i) returns true if ________________________________

17 Define Chromatic Number.

18 Number of nodes generated in the State Space Tree of a Graph Coloring problem with n vertices and with
chromatic number m is _______________

19 g(i , S)= min { }

20 g( i , ) = ___________________ [ ]
a) Ci b) C i c) Cii d) C
i
21 In reliability design problem S1 = _________________________.

Unit-III
In greedy method only one decision sequence is ever generated. (True/False)
1. Multi-stage graph problem is classified under …………………. category of problem.
a) permutation selection problem
b) subset selection problem
c) both a and b
d) none
2. If you construct a Binary search tree for the given identifier set {for,do,while,int,if}.
Then the average no of comparisions required to search an identifier in the worst case is ……………..
3. The time complexity of Heap Sort algorithm is [ ]
a) O(n log n)
b) O( log n)
c) O(n2)
d) O(1)

4. In AVL Tree the Balancing factor of any node can be [ ]


a) 1,2,3
b) 1,0
c) 1,0,-1
d) None

5. In a 2-3 Tree each node should have __________ no of keys. [ ]


a) 2 b)3 c)5 d)None

6. In Sets the FIND operation returns [ ]


a) Parent node
b) Root node
c) Child node
d) None

7. In a Maximum Heap the value of a node is at least as large as its ____[ ]


a) Parent
b) Root
c) Sibling
d) Children

8. In _____________ trees all the terminal nodes are at same level [ ]


a) 2-3 Trees
b) AVL Trees
c) Binary Trees
d) None
9. In Weighted Union Algorithm the set with minimum no. of nodes becomes
a) Root of the Resultant Tree [ ]
b) Child node at Level Two
c) AVL Tree
d) 2-3 Tree

10. The minimum no. of nodes in an AVL tree of Height ‘ h’ is given by n(h)=________________

11. The minimum no. of nodes in a 2-3 tree of Height k is ______________

12. Write the Objective function of Knapsack problem.


13. Define Optimal & feasible Solutions
14. Write the Control abstraction of Greedy Method.
Unit -IV
1. The time complexity of In-order Tree traversal algorithm is __________ [ ]
a)(n) b) log n) c) (n2) d) (n3)

2. The Space complexity of In-order Tree traversal algorithm is __________ [ ]


a)O(n) b)O(log n) c)O(n2) d)O(n3)

3. The Space complexity of BFS algorithm is __________ [ ]


a)(n) b) (log n) c) (n2) d) (n3)
4. The Time complexity of BFS algorithm(if adjacency matrix is used to represent the graph) is __________
[ ]
2 3
a) (n+E) b) (log n) c) (n ) d) (n )
5. The Time complexity of DFS algorithm(if adjacency list is used to represent the graph) is __________
[ ]
a) (n+E) b) (log n) c) (n2) d) (n3)
6. The data structure used in BFS algorithm is [ ]
a) Stack b)Queue c)Linked list d)None
7. The data structure used in DFS algorithm is [ ]
a) Stack b)Queue c)Linked list d)None
8. ___________ game is one in which there are no valid sequences of infinite length
a)Infinite b)Finite c)Cricket d)None [ ]
9. Define the Instance of a Game Tree
10. The Problem reduction in computer can be represented by using [ ]
a. Multistage graph
b. Tree
c. AND/OR graph
d. OR/OR graph
11. In LC search the next E-node is selected based on _______ [ ]
a) FIRST IN FIRST OUT b)LAST IN FIRST OUT
c) RANKING FUNCTION d)All the ABOVE.
12. In LC search, the function of Least( ) is________________ [ ]
a. To find the node with least rank
b. To find least number of nodes
c. To find the least value
d. None
13. In 15-puzzle problem, g(x)= ___________________________ [ ]
a. The total number of Tiles.
b. The number of non blank Tiles with even number
c. The number of Tiles with ODD number
d. The number of non blank Tiles not in their Goal Position.

14. Intelligent ranking function c(x)=___________ [ ]


a. F(h(x)-g|(x)
b. F(h(x))
c. F(h(x))+g|(x)
d. None

NIT-V

1. The solution to the recurrence H(n) = n +root(n )· H([ ]) is [ ]

A. O(n).

B. O( ).
C. O(n log log n).
D. O(log n).
E. O(log* n).

2. Given two graphs G1 and G2, deciding if one can delete k edges from G1 and get
the graph G2 (we consider two graphs to be the same if one can rename the vertices of one graph to get the
second graph) is
A. NP-complete.
B. Solvable in polynomial time.
C. NP-hard
D. None of the above.
3. Any problem that can be solved by an algorithm that uses only O(log n) bits of memory (in addition to the input,
which resides in a read-only memory) can be solved using polynomial time." This statement is:
A. False.
B. True.
C. False only if P = NP.
D. True only if P = NP.

4.The problem Triple 2Coloring (deciding if the vertices of a graph G can be


partitioned into three sets S, T, V , such that the induced subgraphs GS, GT and GV are each
colorable by two colors) is
A. NP-Complete.
B. Solvable in polynomial time.
C. NP-Hard.
D. None of the above.

5 . The solution to the recurrence A(n) = A(log n) + 1 is:


A. O(1)
B. O(log log log n)
C. O(n log n).
D. O(n log n).

6). Given a boolean formula F of length n defined over 100 variables, deciding if F is satisfiable can be done in:
A. O(2n) time, and there is no faster algorithm.
B. O(log log n) time.
C. Polynomial time.
D. This is an NP-complete problem, and it cannot be solved.

20. Tutorial problems


Tutorial-1

1. Write an algorithm to evaluate a polynomial using Horne r's Rule.


2. Given n Boolean variables x1, x2….xn. We wish to print all possible combinations of truth values they can
assume. Write an algorithm to accomplish this.
3. Give both a Recursive and an Iterative algorithm to compute the binomial coefficient n
m
4. Ackermann’s function A(m,n) is defined as follows.
n+1 if m=0
A (m,n) = A (m-1, 1) if n=0
A (m-1, A (m, n-1)) otherwise
Write both Recursive and Non Recursive algorithm for computing above function.
5. Determine the frequency counts for all statements in the following two algorithm segments.

a) b)
1. for i: = 1 to n do 1. i: = 1;
2. for j: = 1 to i do 2. while (i≤n) do
3. for k: = 1 to j do 3. {
4. x: = x+1; 4. x: = x+1;
5. i: = i+1;
6. }

6. Obtain the step count for the following algorithm using the Frequency method. Clearly show the step
count table.
Algorithm mult (a, b, c, n)
{
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];
}
}

Tutorial-2
1. Show how Quicksort sorts the following sequence of keys: 5, 5, 8, 3, 4, 3, 2

2. Sort a file of n records which consists of scanning the file, merging consecutive pairs of size one, then
merge pairs of size two, and so on. Write an algorithm that carries out this process. Show how your
algorithm works on the data set (100, 300, 150, 450, 250, 350, 200, 400, 500).

3. Show how Quick sort sorts the following sequence of keys: 1, 1, 1, 1, 1, 1, 1 and 5, 5, 8, 3, 4, 3, 2

4. On which input data does the algorithm Quick Sort exhibit its worst case behavior?

5. Perform Matrix multiplication using Divide and Conquer and Strassen’s Matrix Multiplication on matrices
compatible for multiplication of your choice.

Tutorial-3
1. Present an algorithm Height union that uses the height rule for union operations instead of the
weighting rule.
2. For the following graphs indentify the articulation points and draw the biconnected components.

1 1 5
4
8
2 3 4 6
2
7
a) b)
Tutorial-4
1. Prove that if p1/w1≥, p2/w2≥,……≥pn/wn, then Greedy-Knapsack generates an optimal solution
to the given instance of the Knapsack problem.

2. Find an optimal solution to the Knapsack instance:


n=7, m=15, (p1, p2,….p7) = (10, 5, 15, 7, 6, 18, 3) and (w1, w2, w3…..w7) = (2,3,5,7,1,4,1).

3. Compute a minimum cost spanning tree for the following graph using
a) Prim’s Algorithm b) Kruskal’s Algorithm.
6
2 7 21
11 8 10 6
15
12 4 11
1
17 7
2 14
13 8
5
3 5

4. Find the shortest path between all pairs of nodes in the following graph
A 5 B

1 2
4 3

C D
6

Tutorial-5

1. Construct the Optimal Binary Search Tree when n=4 such that a1<a2<a3<a4 with
(q0, q1, q2, q3, q4) = (¼, 3/16, 1/16, 1/16, 1/16) and (p1, p2, p3, p4) = (1/4, 1/8, 1/16, 1/16)

2. Find the optimal solution for the knapsack (p) instance when m=165, n=6, (p1,….p6) =
(w1,…..w6) = (100, 50, 20, 10, 7, 3)

3. Consider the following Sum of Subsets problem instance. n=6, m=30, and w[1:6] ={5, 10, 12, 13,
15, 18}. Find all possible subsets of w that sum to m. Draw the portion of the state space tree that is
generated.
4 Draw the portion of the state space tree generated by LCBB for the following Knapsack instances.
n=5 (p1, p2….p5) = (10, 15, 6, 8, 4)
(w1, w2,……w5) = (4, 6, 3, 4,) and m=12
c) n=5 (p1, p2,…p5) = (w1, w2,….w5) = (4, 4, 5, 8, 9) and m=15.

21. Known gaps ,if any and inclusion of the same in lecture schedule : NO

22. Discussion topics

1. Time and Space complexity


2. Different Sorting algorithms
3. Applications of Divide and Conquer
4. Greedy technique applications
5. Dynamic programming
6. Comparison of Dynamic programming and Greedy technique
7. Application of Trees
8. Application of graphs
9. Tree and graph traversals
10. Applications of backtracking technique
11. Difference between LC Branch & Bound and FIFO branch and Bound
12. Deterministic and Non Deterministic algorithms

23. References , Journals, Websites and E-links


http://www.personal.kent.edu/~rmuhamma/Algorithms/algorithm.html
http://nptel.ac.in/courses/106101060/
http://freevideolectures.com/Course/2281/Design-and-Analysis-of-Algorithms#
http://staff.ustc.edu.cn/~csli/graduate/algorithms/book6/toc.htm
http://www.cs.princeton.edu/courses/archive/spr11/cos423/Lectures/NP-completeness.pdf
https://www.ics.uci.edu/~eppstein/161/syl.html

You might also like