Professional Documents
Culture Documents
• Greedy
– Shortest path, minimum spanning tree, …
• Divide and Conquer
CSE373: Data Structures and Algorithms – Divide the problem into smaller subproblems,
solve them, and combine into the overall solution
Algorithm Design Paradigms – Often done recursively
– Quick sort, merge sort are great examples
• Dynamic Programming
– Brute force through all possible solutions, storing solutions to
Steve Tanimoto subproblems to avoid repeat computation
Winter 2016 • Backtracking
This lecture material represents the work of multiple instructors at the University of Washington. – A clever form of exhaustive search
Thank you to all who have contributed!
• Divide a bigger problem into many smaller subproblems • The fibonacci sequence is a very famous number sequence
• 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
• If the number of subproblems grows exponentially, a recursive • The next number is found by adding up the two numbers before it.
solution may have an exponential running time • Recursive solution:
fib(int n) {
• Dynamic programming to the rescue! if (n == 1 || n == 2) {
return 1
• Often an individual subproblem may occur many times! }
return fib(n – 2) + fib(n – 1)
– Store the results of subproblems in a table and re-use them
}
instead of recomputing them
– Technique called memoization • Exponential running time!
– A lot of repeated computation
Winter 2016 CSE 373: Data Structures & Algorithms 3 Winter 2016 CSE 373: Data Structures & Algorithms 4
Winter 2016 CSE 373: Data Structures & Algorithms 5 Winter 2016 CSE 373: Data Structures & Algorithms 6
1
3/3/2016
7
Winter 2016 CSE 373: Data Structures & Algorithms Winter 2016 CSE 373: Data Structures & Algorithms 8
Portion A
Junction At this point in time you know that
Portion B
Winter 2016 CSE 373: Data Structures & Algorithms 9 Winter 2016 CSE 373: Data Structures & Algorithms 10
Winter 2016 CSE 373: Data Structures & Algorithms 11 12 CSE 373: Data Structures & Algorithms Winter 2016
2
3/3/2016
Backtracking Backtracking
• Dealing with the maze:
The neat thing about coding up backtracking is that it can be done
– From your start point, you will iterate through each possible
recursively, without having to do all the bookkeeping at once.
starting move.
– Instead, the stack of recursive calls does most of the
– From there, you recursively move forward.
bookkeeping
– If you ever get stuck, the recursion takes you back to where
– (i.e., keeps track of which locations we’ve tried so far.)
you were, and you try the next possible move.
Winter 2016 CSE 373: Data Structures & Algorithms 13 Winter 2016 CSE 373: Data Structures & Algorithms 14
Winter 2016 CSE 373: Data Structures & Algorithms 17 Winter 2016 CSE 373: Data Structures & Algorithms 18