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)

PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PPT
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
PDF
NTPC PATRATU Summer internship report.pdf
hemant03701
 
PDF
SERVERLESS PERSONAL TO-DO LIST APPLICATION
anushaashraf20
 
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
PDF
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
PDF
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PPTX
Alan Turing - life and importance for all of us now
Pedro Concejero
 
PDF
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
PPTX
Final Major project a b c d e f g h i j k l m
bharathpsnab
 
PPTX
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PPTX
UNIT 1 - INTRODUCTION TO AI and AI tools and basic concept
gokuld13012005
 
PPTX
仿制LethbridgeOffer加拿大莱斯桥大学毕业证范本,Lethbridge成绩单
Taqyea
 
PPTX
darshai cross section and river section analysis
muk7971
 
PDF
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PPTX
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
NTPC PATRATU Summer internship report.pdf
hemant03701
 
SERVERLESS PERSONAL TO-DO LIST APPLICATION
anushaashraf20
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Alan Turing - life and importance for all of us now
Pedro Concejero
 
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
Final Major project a b c d e f g h i j k l m
bharathpsnab
 
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
UNIT 1 - INTRODUCTION TO AI and AI tools and basic concept
gokuld13012005
 
仿制LethbridgeOffer加拿大莱斯桥大学毕业证范本,Lethbridge成绩单
Taqyea
 
darshai cross section and river section analysis
muk7971
 
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
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