Module 5
Module 5
(CSE 2007)
Module 5
Limitations of
Algorithm
1
Module 5-Content
1. Introduction, Lower Bound Arguments
2. Decision Trees
P, NP ,NP complete problems
3. Backtracking- Introduction
N Queens Problem
N Queens Algorithm
4. Travelling Salesman Problem
2
Lower Bounds
Lower bound: an estimate on a minimum amount of work
needed to solve a given problem
Examples:
• number of comparisons needed to find the largest element in
a set of n numbers
• number of comparisons needed to sort an array of size n
• number of comparisons necessary for searching in a sorted
array
• number of multiplications needed to multiply two n-by-n
matrices
3
Lower Bounds (cont.)
Lower bound can be
• an exact count
• an efficiency class ()
Tight lower bound: there exists an algorithm with the
same efficiency as the lower bound
4
Decision Trees
Decision tree — a convenient model of algorithms
involving comparisons in which:
• internal nodes represent comparisons
• leaves represent outcomes (or input cases)
Decision tree for 3-element insertion sort
abc
yes no
a<b
abc bac
yes no yes no
b< c a<c
5
Decision Trees and Sorting Algorithms
• Any comparison-based sorting algorithm can be
represented by a decision tree (for each fixed n)
• Number of leaves (outcomes) n!
• Height of binary tree with n! leaves log2n!
• Minimum number of comparisons in the worst case
log2n! for any comparison-based sorting algorithm,
since the longest path represents the worst case and its
length is the height
6
Types of Problems
• Deterministic Problem
• Non Deterministic Problem
• Polynomial Problem
• Exponential Problem
Deterministic problem
• In deterministic algorithm, for a given particular
input, algorithm will always produce the same
output going through same set of states.
Particular input -- computer -- always produce the
same output
Can determine next step of solution.
NP-complete
problem
15
NP-Complete Problems (cont.)
Other NP-complete problems obtained through
polynomial-time reductions from a known NP-complete
problem
NP problems
know n
NP-complete
problem
candidate
f or NP -
completeness
16
NP Hard Problem
• A problem H is NP hard , if every problem L in NP
can be reducible to H in polynomial time. H’s
solution can be used to solve L in polynomial
time.
17
Backtracking
• A given problem has a set of constraints and possibly an
objective function
• The solution optimizes an objective function, and/or is
feasible.
• We can represent the solution space for the problem
using a state space tree
• The root of the tree represents 0 choices,
• Nodes at depth 1 represent first choice
• Nodes at depth 2 represent the second choice, etc.
• In this tree a path from a root to a leaf represents a
candidate solution
• An example for Backtracking is N Queens
problems.
18
N Queens Problem
1. The n-queens problem defines to place n queens
on an n×n chessboard so that no two queens
attack each other by being in the same row or in
the same column or on the same diagonal.
2. For n=1, the problem has a trivial solution, and
it is easy to see that there is no solution for n=2
and n=3.
3. So let us consider the four-queens problem and
solve it by the backtracking technique.
19
• We start with the empty board and then place queen 1 in the
first possible position of its row, which is in column 1 of row
1.
• Then we place queen 2, after trying unsuccessfully columns1
and 2,in the first acceptable position for it,which is square (2,
3), the square in row 2 and column 3.
• This proves to be a dead end because there is no acceptable
position for queen 3. So, the algorithm backtracks and puts
queen 2 in the next possible position at (2, 4).
• Then queen 3 is placed at (3, 2), which proves to be another
dead end.
• The algorithm then backtracks all the way to queen 1 and
moves it to (1, 2).
• Queen 2 then goes to (2, 4), queen 3 to (3, 1), and queen 4 to
(4, 3), which is a solution to the problem
20
Queens Algorithm
AlGORITHM Backtrack(X[l..i])
//Gives a template of a generic backtracking algorithm
//Input: X[Li] specifies first i promising components of a solution
//Output: All the tuples representing the problem's solutions
if X[Li] is a solution write X[l..i]
else
for each element x E S;+J consistent with X[Li] and the constraints do
X[i + 1] *-X
Backtrack(X[1..i + 1 ]J
21
4*4 Queen Problem using State space tress
22
• For TSP pls refer textbook
23
Thank You