problem solving dr. andrew wallace phd beng(hons) euring [email protected]

31
Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng [email protected]

Upload: citlali-buntin

Post on 15-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Problem SolvingDr. Andrew Wallace PhD BEng(hons) EurIng

[email protected]

Page 2: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Overview

• Breaking and entry

• How to get away?

• Which items to steal?

• Stash the loot

Page 3: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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

Page 4: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Brute Force

BruteForce()

c first(P)

while c != Invalid

do if valid(P, c)

then output(P, c)

c next(P, c)

P = problem

c = candidate solution

Page 5: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Brute Force

• Searching a list

• Travelling salesman problem

• Deoxyribonucleic acid• Heuristics!

Page 6: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Brute Force

• It can work!• Some times it’s the only solution!

• Problem:• Inefficient• Can become time consuming

Page 7: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Optimal subproblems

• Optimal solution has optimal subproblem solutions

• S = {s1, s2, s3, … sn}

• Optimal sub-structure

• Greedy algorithms

• Else• Overlapping substructures• Dynamic programming

Page 8: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Greedy Algorithms

• Local optimum results in global optimum

• Problem: Shortest path from A to B

Museum

Safe House

10

12

9

610

12

3

8

2

15

Page 9: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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

Page 10: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Greedy Algorithms

• Set of candidate solutions• Paths

• Selection function• Which path to choose?

• End function• Are we there yet?

Page 11: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Greedy Algorithms

• A resource

• Set of agents who want to use the resource

• No overlaps in time allowed

Agents (a) a b c d e f g

Start (s) 1 3 2 4 6 7 5

Finish (f) 4 5 6 6 8 10 10

a, d, e

Page 12: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Greedy Algorithms

• Earliest finishing time to maximise the number of participants• Maximise the remaining time

Page 13: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Greedy Algorithms

Resource Allocation(s, f, n)

A {a1}

i 1

for m 2 to n

do if sm fi

then A A {am}

i m

return AO(n)

Page 14: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Greedy Algorithms

• No back tracking• Choices dependent on passed choices but not future choices

Page 15: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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

Page 16: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Dynamic programming

• Items

• Weight

• Value

• But we have …• Max weight!

Page 17: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Dynamic programming

• Fibonacci sequence

• Fn = Fn-1 + Fn-2

• F0 = 0, F1 = 1 and F2 = 1

F(6)

F(4)

F(5)

F(3)

F(4)

F(3)

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(1)

F(2)

F(1) F(0)

F(1)

Page 18: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Dynamic programming

• Items = {i1, i2, i3 … in}

• Weight = {w1, w2, w3 … wn}

• Value = {v1, v2, v3 … vn}

• W = max weight we can take

Page 19: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Dynamic programming

• Optimal structure?

• w = {w1, w2 … wj}

• w = {w1, w2 … wj-1}

• Overlapping subproblems• Compare solution with item to solution without item

Page 20: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Dynamic programming

c[i, w] =

c[i, w] = maximum value with weights less than or equal to W

v = values (stored in an array)

w = weights (stored in an array)

W = max capacity

If i = 0 or w= 0

If wi > w

If i > 0 or w >= wi

Page 21: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Dynamic programming

Dynamic 1-0 Knapsack(v, w, n, W)

for w 0 to W

do c[0, w] 0

for i 1 to n

do c[i, 0] 0

for w 1 to W

do if wi ≤ w

then if vi + c[i-1, w-wi] > c[i-1, w]

c[i, w] vi + c[i-1, w – wi]

else c[i, w] c[i-1, w]

else c[i, w] c[i-1, w]O(n)

Page 22: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Divide and Conquer

• Recursion• Break the problem reclusively into small problems• Preferably evenly

• Solve the smaller problems

• Combine together to produce the overall solution

Page 23: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Divide and Conquer

• T(n) = 2T(n/2) + Q(n)

• Master method

Page 24: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Divide and Conquer

• Master method:

• T(n) = aT(n/b) + f(nc)

• IF f(n) = O(), where c < logba then T(n) = Q()

• IF f(n) = Q(logkn), where c = logba, then T(n) = Q(logk+1n)

• IF f(n) = W(), where c > logab and af(n/b) kf(n) where k < 1 and n is sufficiently large, then T(n) = Q(f(n))

Page 25: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Divide and Conquer

• Find 90

33 45 53 82 90 120 150

Page 26: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Divide and Conquer

• Find 90

• Divide into two

33 45 53 82 90 120 150

Page 27: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Divide and Conquer

• Find 90

• Divide into two

• And again!

90 120 150

Page 28: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Divide and Conquer

• Find 90

• Divide into two

• And again!

90

Page 29: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Divide and Conquer

• T(n) = 1T(n/2) + Q(1)

• logba = log21 = 0

• nc = 1, therefore c = 0

• Therefore c = logba

• Therefore case 2

• f(n) = Q(logkn), then T(n) = Q(logk+1n)

• k = 0, therefore

• f(n) = Q(log1n) = Q(logn)

Page 30: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Problem solving

• Top down• greedy

• Bottom up• dynamic

Page 31: Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Questions?