KLS, Vishwanathrao Deshpande Institute of Technology,
(Approved by AICTE, New Delhi, Affiliated to VTU, Belagavi)
(Recognized under Section 2(f) by UGC, New Delhi)
Udyog Vidya Nagar, Haliyal-581329, Dist: Uttara Kannada
Phone: 08284-220861, 220334, 221409, Fax: 08284-220813
Web:www.klsvdit.edu.in, Email:
[email protected]/
[email protected]Subject: Analysis and Design of Algorithms
Subject Code:BCS401
Faculty: Dr. Naveenkumar T. Rudrappa
Course outcome
1. Apply asymptotic notational method to analyze the
performance of the algorithms in terms of time complexity.
2. Demonstrate divide & conquer approaches and decrease &
conquer approaches to solve computational problems.
3. Make use of transform & conquer and dynamic
programming design approaches to solve the given real
world or complex computational problems.
4. Apply greedy and input enhancement methods to solve
graph & string based computational problems.
5. Analyze various classes (P,NP and NP Complete) of
problems.
6. Illustrate backtracking, branch & bound and approximation
methods.
2
Module 2
• Exhaustive Search: Travelling Salesman problem
• Exhaustive Search: Knapsack Problem
• Decrease and Conquer: Insertion Sort
• Decrease and Conquer: Topological Sorting
• Divide and Conquer: Merge Sort
• Divide and Conquer: Quick Sort
• Divide and Conquer: Binary Tree Traversals
• Divide and Conquer: Multiplications of large integers
• Divide and Conquer: Strassen’s Matrix multiplication
3
Exhaustive Search
Problems involving: 1. Permutation
2. Combination
3. Subset of a given set.
Exhaustive Search: Step 1: Generate every element from the problem domain.
Step 2: Select those that satisfy constraints.
Step 3: Find the desired element.
Travelling Salesman Problem (TSP)
Goal: To find the most economical way to travel all cities with the following constraints:
1. Visit each city only once.
2. Return to the original starting point.
Applications
1. Planning logistics
2. Manufacture of microchips
3. A sub-problem in many areas like:
1. City represents customers, soldering points, or DNA fragments.
2. Distance represents travelling times or cost, or a similarity measure between DNA
fragments.
4
Exhaustive Search: Travelling Salesman Problem
Hamiltonian Circuit: A cycle that passes through all vertices of
the graph exactly once
5
Exhaustive Search: Knapsack Problem
6
Exhaustive Search: Assignment Problem
Cost should be minimum.
7
Decrease and Conquer
Case 1: Decrease by a constant [Decrease by 1]
Size of instance reduced by constant 1
for each iteration
Types of input instances:
1. Odd count of inputs
2. Even count of inputs
Relationship between odd and even
an= an-1.a
f(n)= an
8
Decrease and Conquer
Case 2: Decrease by a constant factor
[Decrease by 2]
Size of instance reduced by constant
factor on each iteration. Decrease by
HALF.
𝑛
Relationship between an and 𝑎 2
an= (𝑎 2)2
Does not work for odd values of n.
Efficiency O(log n).
On each iteration size reduced by half at the cost of 2
MULTIPLICATIONS
9
Decrease and Conquer: Insertion Sort
10
Decrease and Conquer
Directed graph: Digraph
Direction specified for all its edges.
Difference between adjacency matrix and adjacency list
1. Adjacency matrix of directed graph is non-
symmetric.
2. An edge in directed graph has one
corresponding node in the digraphs
adjacency list.
3. Directed cycle: 3/more vertices a to b to c
to a.
Prerequisites:1, 2, 3, 4
Problem: In which order should the students take the courses?
Topological Sorting For every edge in the graph starting vertex
listed before the ending vertex.
11
Decrease and Conquer: Topological Sorting
Prerequisites:1, 2, 3, 4
Problem: In which order should the students take the courses?
Topological Sorting For every edge in the graph starting vertex listed
before the ending vertex. Graph should be DAG.
Note: No solution if there exits a cycle.
12
Divide and Conquer: Merge Sort
General plan for divide and conquer technique
13
Divide and Conquer: Merge Sort
14
Divide and Conquer: Quick Sort
Difference between Merge Sort: Divide using array index.
Quick sort: Divide using value.
15
Quick Sort contd....
16
Quick Sort contd....
17
Quick Sort contd....
18
Quick Sort contd....
19
Quick Sort contd....
20
Quick Sort Efficiency Analysis
21
Divide and Conquer: Binary Tree Traversals
22
Divide and Conquer: Multiplications of large integers
a=a1a0 a=a1.10n/2 + a0
b=b1b0 b=b1.10n/2 + b0
23
Divide and Conquer: Strassen’s Matrix multiplication
Normal Matrix Multiplication: Brute Force Technique
Multiplication of 2 (2 by 2) matrices= 8 Multiplications and 4 Additions
Strassen’s Matrix Multiplication: Divide and Conquer Technique
Multiplication of 2 (2 by 2) matrices= 7 Multiplications and 18(addition/subtractions).
24