CS3401 Algorithms Unit IV
CS3401 Algorithms Unit IV
Mr.C.Karthikeyan
Assistant Professor, Department of CSE
Karpagam Institute of Technology
CS3401 Algorithms | Unit IV 1
Course Objectives
• Understand and apply the algorithm analysis techniques on
searching and sorting algorithms
• Critically analyze the efficiency of graph algorithms
• Understand different algorithm design techniques
• Solve programming problems using state space tree
• Understand the concepts behind NP Completeness,
Approximation algorithms and randomized algorithms.
CS3401 Algorithms | Unit IV 2
Course Outcomes
• Develop algorithms for various computing problems and time and space
complexity analysis.
• Apply graph algorithms to solve problems and analyze their efficiency.
• Analyze the different design techniques like divide and conquer, dynamic
programming and Greedy techniques to solve problems.
• Make use of the state space tree method for solving computational
problems.
• Solve problems using approximation algorithms and randomized
algorithms.
CS3401 Algorithms | Unit IV 3
Course Syllabus
COURSE SYLLABUS
Mr.C.Karthikeyan
Assistant Professor, Department of CSE
Karpagam Institute of Technology
CS3401 Algorithms | Unit IV 7
Backtracking: N-Queens problem - Hamiltonian
Circuit Problem - Subset Sum Problem – Graph
colouring problem Branch and Bound: Solving 15-
Puzzle problem - Assignment problem - Knapsack
Problem - Travelling Salesman Problem
Solution: Start our search with vertex 'a'. This vertex 'a' becomes the root of our implicit tree.
• Complete state-space tree of the backtracking algorithm applied to the instance A = {3, 5,
6, 7} and d = 15 of the subset-sum problem.
• The number inside a node is the sum of the elements already included in the subsets
represented by the node.
• The inequality below a leaf indicates CS3401
the reason forIV its termination.
Algorithms | Unit 28
Subset Sum Problem : Example
29
Example 2: There are 5 distinct numbers {1,2,5,6,8}. Find the combinations of these numbers
such that the sum is 9. Use the backtracking model to arrive at the solution.
30
Graph colouring problem
34
Graph colouring problem
35
Step 3
Backtracking traverses the state space tree Branch-and-Bound traverse the tree in any
Traversal
by DFS (Depth First Search) manner. manner, DFS or BFS.
• We have to find a lower bound on the cost of an optimal selection without actually solving
the problem. We can do this by several methods. For example, it is clear that the cost of any
solution, including an optimal one, cannot be smaller than the sum of the smallest elements
in each of the matrix’s rows.
• For the instance here, this sum is 2 + 3+ 1+ 4 = 10. It is important to stress that this is not the
cost of any legitimate selection (3 and 1 came from the same column of the matrix); it is just
a lower bound on the cost of any legitimate selection.
• We can and will apply the same thinking to partially constructed solutions. For example, for
any legitimate selection that selects 9 from the first row, the lower bound will be 9 + 3 + 1+ 4
44
= 17.
Assignment Problem : Introduction
• It is sensible to consider a node with the best bound as most promising,45 although this does
not, of course, preclude the possibility that an optimal solution will ultimately belong to a
different branch of the state-space tree. This variation of the strategy is called the best-first
branch-and bound.
• The lower-bound value for the root, denoted lb, is 10. The nodes on the first level of the
tree correspond to selections of an element in the first row of the matrix, i.e., a job for
person a as Shown below.
In the above Figure, Levels 0 and 1 of the state-space tree for the instance of the assignment
problem being solved with the best-first branch-and-bound algorithm. The number above a
node shows the order in which the node was generated. A node’s fields indicate the job
number assigned to person a and the lower bound value, lb, for this node. 45
Assignment Problem : Introduction
46
In the above figure, Levels 0, 1, and 2 of the state-space tree for the instance of the assignment problem being
solved with the best-first branch-and-bound algorithm.
46
Assignment Problem : Introduction
47
A Simple way to compute the upper bound ub is to add to v, the total value of the items
already selected, the product of the remaining capacity of the knapsack W − w and the best
per unit payoff among the remaining items, which is vi+1/wi+1:
ub = v + (W − w)(vi+1/wi+1).
= 0+(10-0) (10)
= 100
50
Knapsack Problem : Introduction
51
Example: