SlideShare a Scribd company logo
ADT(Algorithm Design Technique Backtracking algorithm).ppt
General Concepts
 Algorithm strategy
 Approach to solving a problem
 May combine several approaches
 Algorithm structure
 Iterative  execute action in loop
 Recursive  reapply action to subproblem(s)
 Problem type
 Satisfying  find any satisfactory solution
 Optimization  find best solutions (vs. cost
metric)
Some Algorithm Strategies
 Recursive algorithms
 Backtracking algorithms
 Divide and conquer algorithms
 Dynamic programming algorithms
 Greedy algorithms
Recursive Algorithm
 Based on reapplying algorithm to subproblem
 Approach
1. Solves base case(s) directly
2. Recurs with a simpler subproblem
3. May need to convert solution(s) to subproblems
Recursive Algorithm –
Examples
 To count elements in list
 If list is empty, return 0
 Else skip 1st
element and recur on remainder of list
 Add 1 to result
 To find element in list
 If list is empty, return false
 Else if first element in list is given value, return
true
 Else skip 1st
element and recur on remainder of list
Backtracking Algorithm
 Based on depth-first recursive search
 Approach
1. Tests whether solution has been found
2. If found solution, return it
3. Else for each choice that can be made
a) Make that choice
b) Recur
c) If recursion returns a solution, return it
4. If no choices remain, return failure
 Some times called “search tree”
 Basically it is exhaustive search using divide and conquer.
 Sometimes the best algorithm for a problem is to try all possibilities.
 This is always slow.
 Backtracking speeds the exhaustive search by pruning.
Backtracking Algorithm Application
 Application to:
 The knapsack problem
 The Hamiltonian cycle problem
 The travelling salesperson problem
 The eight queen problem
Eight Queen Problem
Backtracking Algorithm – Example
 Find path through maze
 Start at beginning of maze
 If at exit, return true
 Else for each step from current location
 Recursively find path
 Return with first successful step
 Return false if all steps fail
Backtracking Algorithm – Example
 Color a map with no more than four colors
 If all countries have been colored return success
 Else for each color c of four colors and country n
 If country n is not adjacent to a country that has been
colored c
 Color country n with color c
 Recursively color country n+1
 If successful, return success
 Return failure
Divide and Conquer
 Based on dividing problem into subproblems
 Approach
1. Divide problem into smaller subproblems
Subproblems must be of same type
Subproblems do not need to overlap
2. Solve each subproblem recursively
3. Combine solutions to solve original problem
 Usually contains two or more recursive calls
Divide and Conquer – Examples
 Quicksort
 Partition array into two parts around pivot
 Recursively quicksort each part of array
 Concatenate solutions
Average Case Analysis of Quick Sort
Divide and Conquer – Examples
Average Case Analysis of Quick Sort
Divide and Conquer – Examples
Divide and Conquer – Examples
Divide and Conquer – Examples
 Mergesort
 Partition array into two parts
 Recursively mergesort each half
 Merge two sorted arrays into single sorted array
Dynamic Programming Algorithm
 Based on remembering past results
 Approach
1.Divide problem into smaller subproblems
Subproblems must be of same type
Subproblems must overlap
2.Solve each subproblem recursively
May simply look up solution
3.Combine solutions into to solve original problem
4.Store solution to problem
 Generally applied to optimization problems
Fibonacci Algorithm
 Fibonacci numbers
 fibonacci(0) = 1
 fibonacci(1) = 1
 fibonacci(n) = fibonacci(n-1) +
fibonacci(n-2)
 Recursive algorithm to calculate
fibonacci(n)
 If n is 0 or 1, return 1
 Else compute fibonacci(n-1) and
fibonacci(n-2)
 Return their sum
 Simple algorithm  exponential time
O(2n
)
BY USING DP
Dynamic programming version of fibonacci(n)
If n is 0 or 1, return 1
Else solve fibonacci(n-1) and fibonacci(n-2)
Look up value if previously computed
Else recursively compute
Find their sum and store
Return result
Dynamic programming algorithm  O(n) time
Since solving fibonacci(n-2) is just looking up value
Dynamic Programming – Example
 Combinations
 Knapsack problem
 Matrix product
 Dijkstra Algorithm
 Floyds Algorithm
Greedy Algorithm
 Based on trying best current (local) choice
 Approach
 At each step of algorithm
 Choose best local solution
 Avoid backtracking, exponential time O(2n
)
 Hope local optimum lead to global optimum
Greedy Algorithm – Example
Kruskal’s Minimal Spanning Tree
Algorithm
sort edges by weight (from least to most)
tree = 
for each edge (X,Y) in order
if it does not create a cycle
add (X,Y) to tree
stop when tree has N–1 edgesPicks best
local solution
at each step
ADT(Algorithm Design Technique Backtracking algorithm).ppt

More Related Content

Similar to ADT(Algorithm Design Technique Backtracking algorithm).ppt (20)

PPTX
Types of algorithms
Amelita Martinez
 
PPTX
algo classification.pptx
ShivaniSharma335055
 
PPTX
mmmmmmm
Kawsar Ahmed
 
PPTX
algorithm design.pptx
ssuserd11e4a
 
PPT
Optimization problems
Ruchika Sinha
 
PPT
35 algorithm-types
Kislay Bhardwaj L|PT,ECSA,C|EH
 
PDF
Disign and Analysis for algorithm in computer science and technology
ritikkumarchaudhury7
 
PPT
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
dakccse
 
PPTX
Algorithms Design Patterns
Ashwin Shiv
 
PPT
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
AmitBhola17
 
PPT
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
ZeelGoyani
 
PPT
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
BinayakMukherjee4
 
PPT
32 algorithm-types
ashish bansal
 
PDF
Algo Strategies and explaination ppt.pdf
sayalishivarkar1
 
PPT
Dynamic pgmming
Dr. C.V. Suresh Babu
 
PPTX
CH-1.1 Introduction (1).pptx
satvikkushwaha1
 
PPTX
Design and Analysis of Algorithm-Lecture.pptx
bani30122004
 
PDF
Data Analysis and Algorithms Lecture 1: Introduction
TayyabSattar5
 
PDF
Cs6402 scad-msm
Jai Ram
 
PPT
graph coloring.ppt
chetanvchaudhari
 
Types of algorithms
Amelita Martinez
 
algo classification.pptx
ShivaniSharma335055
 
mmmmmmm
Kawsar Ahmed
 
algorithm design.pptx
ssuserd11e4a
 
Optimization problems
Ruchika Sinha
 
35 algorithm-types
Kislay Bhardwaj L|PT,ECSA,C|EH
 
Disign and Analysis for algorithm in computer science and technology
ritikkumarchaudhury7
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
dakccse
 
Algorithms Design Patterns
Ashwin Shiv
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
AmitBhola17
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
ZeelGoyani
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
BinayakMukherjee4
 
32 algorithm-types
ashish bansal
 
Algo Strategies and explaination ppt.pdf
sayalishivarkar1
 
Dynamic pgmming
Dr. C.V. Suresh Babu
 
CH-1.1 Introduction (1).pptx
satvikkushwaha1
 
Design and Analysis of Algorithm-Lecture.pptx
bani30122004
 
Data Analysis and Algorithms Lecture 1: Introduction
TayyabSattar5
 
Cs6402 scad-msm
Jai Ram
 
graph coloring.ppt
chetanvchaudhari
 

Recently uploaded (20)

PPT
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PPTX
MATLAB : Introduction , Features , Display Windows, Syntax, Operators, Graph...
Amity University, Patna
 
PDF
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PDF
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
PPTX
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PPT
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
MRRS Strength and Durability of Concrete
CivilMythili
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
MATLAB : Introduction , Features , Display Windows, Syntax, Operators, Graph...
Amity University, Patna
 
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
Ad

ADT(Algorithm Design Technique Backtracking algorithm).ppt

  • 2. General Concepts  Algorithm strategy  Approach to solving a problem  May combine several approaches  Algorithm structure  Iterative  execute action in loop  Recursive  reapply action to subproblem(s)  Problem type  Satisfying  find any satisfactory solution  Optimization  find best solutions (vs. cost metric)
  • 3. Some Algorithm Strategies  Recursive algorithms  Backtracking algorithms  Divide and conquer algorithms  Dynamic programming algorithms  Greedy algorithms
  • 4. Recursive Algorithm  Based on reapplying algorithm to subproblem  Approach 1. Solves base case(s) directly 2. Recurs with a simpler subproblem 3. May need to convert solution(s) to subproblems
  • 5. Recursive Algorithm – Examples  To count elements in list  If list is empty, return 0  Else skip 1st element and recur on remainder of list  Add 1 to result  To find element in list  If list is empty, return false  Else if first element in list is given value, return true  Else skip 1st element and recur on remainder of list
  • 6. Backtracking Algorithm  Based on depth-first recursive search  Approach 1. Tests whether solution has been found 2. If found solution, return it 3. Else for each choice that can be made a) Make that choice b) Recur c) If recursion returns a solution, return it 4. If no choices remain, return failure  Some times called “search tree”  Basically it is exhaustive search using divide and conquer.  Sometimes the best algorithm for a problem is to try all possibilities.  This is always slow.  Backtracking speeds the exhaustive search by pruning.
  • 7. Backtracking Algorithm Application  Application to:  The knapsack problem  The Hamiltonian cycle problem  The travelling salesperson problem  The eight queen problem Eight Queen Problem
  • 8. Backtracking Algorithm – Example  Find path through maze  Start at beginning of maze  If at exit, return true  Else for each step from current location  Recursively find path  Return with first successful step  Return false if all steps fail
  • 9. Backtracking Algorithm – Example  Color a map with no more than four colors  If all countries have been colored return success  Else for each color c of four colors and country n  If country n is not adjacent to a country that has been colored c  Color country n with color c  Recursively color country n+1  If successful, return success  Return failure
  • 10. Divide and Conquer  Based on dividing problem into subproblems  Approach 1. Divide problem into smaller subproblems Subproblems must be of same type Subproblems do not need to overlap 2. Solve each subproblem recursively 3. Combine solutions to solve original problem  Usually contains two or more recursive calls
  • 11. Divide and Conquer – Examples  Quicksort  Partition array into two parts around pivot  Recursively quicksort each part of array  Concatenate solutions Average Case Analysis of Quick Sort
  • 12. Divide and Conquer – Examples Average Case Analysis of Quick Sort
  • 13. Divide and Conquer – Examples
  • 14. Divide and Conquer – Examples
  • 15. Divide and Conquer – Examples  Mergesort  Partition array into two parts  Recursively mergesort each half  Merge two sorted arrays into single sorted array
  • 16. Dynamic Programming Algorithm  Based on remembering past results  Approach 1.Divide problem into smaller subproblems Subproblems must be of same type Subproblems must overlap 2.Solve each subproblem recursively May simply look up solution 3.Combine solutions into to solve original problem 4.Store solution to problem  Generally applied to optimization problems
  • 17. Fibonacci Algorithm  Fibonacci numbers  fibonacci(0) = 1  fibonacci(1) = 1  fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)  Recursive algorithm to calculate fibonacci(n)  If n is 0 or 1, return 1  Else compute fibonacci(n-1) and fibonacci(n-2)  Return their sum  Simple algorithm  exponential time O(2n ) BY USING DP Dynamic programming version of fibonacci(n) If n is 0 or 1, return 1 Else solve fibonacci(n-1) and fibonacci(n-2) Look up value if previously computed Else recursively compute Find their sum and store Return result Dynamic programming algorithm  O(n) time Since solving fibonacci(n-2) is just looking up value
  • 18. Dynamic Programming – Example  Combinations  Knapsack problem  Matrix product  Dijkstra Algorithm  Floyds Algorithm
  • 19. Greedy Algorithm  Based on trying best current (local) choice  Approach  At each step of algorithm  Choose best local solution  Avoid backtracking, exponential time O(2n )  Hope local optimum lead to global optimum
  • 20. Greedy Algorithm – Example Kruskal’s Minimal Spanning Tree Algorithm sort edges by weight (from least to most) tree =  for each edge (X,Y) in order if it does not create a cycle add (X,Y) to tree stop when tree has N–1 edgesPicks best local solution at each step