Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng
Overview Breaking and entry How to get away? Which items to steal? Stash the loot
Brute Force Simple Start at the beginning Keep working till.. You find a solution! Bang you head against a brick wall long enough, sooner or later the brick wall gives out
Brute Force
Searching a list Travelling salesman problem Deoxyribonucleic acid Heuristics!
Brute Force It can work! Some times it’s the only solution! Problem: Inefficient Can become time consuming
Optimal subproblems Optimal solution has optimal subproblem solutions S = {s 1, s 2, s 3, … s n } Optimal sub-structure Greedy algorithms Else Overlapping substructures Dynamic programming
Greedy Algorithms Local optimum results in global optimum Problem: Shortest path from A to B Museum Safe House
Greedy Algorithms Not all local optimal solutions can lead to global optimal solutions Can end up with the worse case If globally optimal can be fast
Greedy Algorithms Set of candidate solutions Paths Selection function Which path to choose? End function Are we there yet?
Greedy Algorithms A resource Set of agents who want to use the resource No overlaps in time allowed Agents (a)abcdefg Start (s) Finish (f) a, d, e
Greedy Algorithms Earliest finishing time to maximise the number of participants Maximise the remaining time
Greedy Algorithms O(n)
Greedy Algorithms No back tracking Choices dependent on passed choices but not future choices
Dynamic programming Overlapping sub problems Once calculated, save and reuse Dynamic Updates and changes things but not as in dynamic programming languages Programming Filling tables not computer programming
Dynamic programming Items Weight Value But we have … Max weight!
Dynamic programming Fibonacci sequence F n = F n-1 + F n-2 F 0 = 0, F 1 = 1 and F 2 = 1 F(6) F(4) F(5) F(3) F(4) F(3) F(1)F(2) F(0)F(1) F(2) F(0) F(1) F(2) F(0)F(1) F(2) F(0)F(1) F(2) F(1)F(0) F(1)
Dynamic programming Items = {i 1, i 2, i 3 … i n } Weight = {w 1, w 2, w 3 … w n } Value = {v 1, v 2, v 3 … v n } W = max weight we can take
Dynamic programming Optimal structure? w = {w 1, w 2 … w j } w = {w 1, w 2 … w j-1 } Overlapping subproblems Compare solution with item to solution without item
Dynamic programming If i = 0 or w= 0 If w i > w If i > 0 or w >= w i
Dynamic programming O(n)
Divide and Conquer Recursion Break the problem reclusively into small problems Preferably evenly Solve the smaller problems Combine together to produce the overall solution
Divide and Conquer T(n) = 2T(n/2) + (n) Master method
Divide and Conquer
Find
Divide and Conquer Find 90 Divide into two
Divide and Conquer Find 90 Divide into two And again!
Divide and Conquer Find 90 Divide into two And again! 90
Divide and Conquer
Problem solving Top down greedy Bottom up dynamic
Questions?