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
 
PPT
32 algorithm-types
ashish bansal
 
PDF
Daa chapter 2
B.Kirron Reddi
 
PDF
Solution 3.
sansaristic
 
PPTX
Algorithm_Dynamic Programming
Im Rafid
 
PPTX
Algorithms Design Patterns
Ashwin Shiv
 
PPTX
dinosourrrrrrrrrrrrrrrrrrrrrr formula .pptx
ShohidulIslamSovon
 
PPT
Kk20503 1 introduction
Low Ying Hao
 
PPTX
DAA UNIT 3
Dr. SURBHI SAROHA
 
PDF
Analysis and design of algorithms part 4
Deepak John
 
PPT
Backtracking
Vikas Sharma
 
PDF
Lecture 5 6_7 - divide and conquer and method of solving recurrences
jayavignesh86
 
PPTX
DAA-Module-5.pptx
smithashetty24
 
PPTX
Brute Force and Divide & Conquer Technique
ssusered62011
 
PPTX
Module 2ppt.pptx divid and conquer method
JyoReddy9
 
PPTX
Dynamic programming
Yıldırım Tam
 
PPTX
TRAVELING SALESMAN PROBLEM, Divide and Conquer
DrSMeenakshiSundaram1
 
PDF
Algorithms practice and problem solving - dynamic programming
Xochitl Watts
 
PDF
Daa chapter4
B.Kirron Reddi
 
PPTX
Divide and Conquer / Greedy Techniques
Nirmalavenkatachalam
 
Types of algorithms
Amelita Martinez
 
32 algorithm-types
ashish bansal
 
Daa chapter 2
B.Kirron Reddi
 
Solution 3.
sansaristic
 
Algorithm_Dynamic Programming
Im Rafid
 
Algorithms Design Patterns
Ashwin Shiv
 
dinosourrrrrrrrrrrrrrrrrrrrrr formula .pptx
ShohidulIslamSovon
 
Kk20503 1 introduction
Low Ying Hao
 
DAA UNIT 3
Dr. SURBHI SAROHA
 
Analysis and design of algorithms part 4
Deepak John
 
Backtracking
Vikas Sharma
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
jayavignesh86
 
DAA-Module-5.pptx
smithashetty24
 
Brute Force and Divide & Conquer Technique
ssusered62011
 
Module 2ppt.pptx divid and conquer method
JyoReddy9
 
Dynamic programming
Yıldırım Tam
 
TRAVELING SALESMAN PROBLEM, Divide and Conquer
DrSMeenakshiSundaram1
 
Algorithms practice and problem solving - dynamic programming
Xochitl Watts
 
Daa chapter4
B.Kirron Reddi
 
Divide and Conquer / Greedy Techniques
Nirmalavenkatachalam
 

Recently uploaded (20)

PDF
bs-en-12390-3 testing hardened concrete.pdf
ADVANCEDCONSTRUCTION
 
PDF
Plant Control_EST_85520-01_en_AllChanges_20220127.pdf
DarshanaChathuranga4
 
PPTX
Explore USA’s Best Structural And Non Structural Steel Detailing
Silicon Engineering Consultants LLC
 
PPTX
Computer network Computer network Computer network Computer network
Shrikant317689
 
PPTX
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
PPTX
Work at Height training for workers .pptx
cecos12
 
PDF
01-introduction to the ProcessDesign.pdf
StiveBrack
 
PDF
NFPA 10 - Estandar para extintores de incendios portatiles (ed.22 ENG).pdf
Oscar Orozco
 
PPTX
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
 
PDF
Artificial Neural Network-Types,Perceptron,Problems
Sharmila Chidaravalli
 
PPTX
Functions in Python Programming Language
BeulahS2
 
PDF
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
PPTX
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
ssuser307730
 
PPTX
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
 
PDF
LLC CM NCP1399 SIMPLIS MODEL MANUAL.PDF
ssuser1be9ce
 
PDF
PRIZ Academy - Process functional modelling
PRIZ Guru
 
DOCX
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
PDF
CLIP_Internals_and_Architecture.pdf sdvsdv sdv
JoseLuisCahuanaRamos3
 
PDF
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
cl144
 
PDF
Authentication Devices in Fog-mobile Edge Computing Environments through a Wi...
ijujournal
 
bs-en-12390-3 testing hardened concrete.pdf
ADVANCEDCONSTRUCTION
 
Plant Control_EST_85520-01_en_AllChanges_20220127.pdf
DarshanaChathuranga4
 
Explore USA’s Best Structural And Non Structural Steel Detailing
Silicon Engineering Consultants LLC
 
Computer network Computer network Computer network Computer network
Shrikant317689
 
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
Work at Height training for workers .pptx
cecos12
 
01-introduction to the ProcessDesign.pdf
StiveBrack
 
NFPA 10 - Estandar para extintores de incendios portatiles (ed.22 ENG).pdf
Oscar Orozco
 
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
 
Artificial Neural Network-Types,Perceptron,Problems
Sharmila Chidaravalli
 
Functions in Python Programming Language
BeulahS2
 
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
ssuser307730
 
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
 
LLC CM NCP1399 SIMPLIS MODEL MANUAL.PDF
ssuser1be9ce
 
PRIZ Academy - Process functional modelling
PRIZ Guru
 
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
CLIP_Internals_and_Architecture.pdf sdvsdv sdv
JoseLuisCahuanaRamos3
 
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
cl144
 
Authentication Devices in Fog-mobile Edge Computing Environments through a Wi...
ijujournal
 
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