Date post: | 15-Dec-2015 |
Category: |
Documents |
Upload: | citlali-buntin |
View: | 218 times |
Download: | 1 times |
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
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
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 = {s1, s2, s3, … sn}
• 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
10
12
9
610
12
3
8
2
15
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) 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
Greedy Algorithms
• Earliest finishing time to maximise the number of participants• Maximise the remaining time
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)
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
• 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)
Dynamic programming
• Items = {i1, i2, i3 … in}
• Weight = {w1, w2, w3 … wn}
• Value = {v1, v2, v3 … vn}
• W = max weight we can take
Dynamic programming
• Optimal structure?
• w = {w1, w2 … wj}
• w = {w1, w2 … wj-1}
• Overlapping subproblems• Compare solution with item to solution without item
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
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)
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) + Q(n)
• Master method
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))
Divide and Conquer
• Find 90
33 45 53 82 90 120 150
Divide and Conquer
• Find 90
• Divide into two
33 45 53 82 90 120 150
Divide and Conquer
• Find 90
• Divide into two
• And again!
90 120 150
Divide and Conquer
• Find 90
• Divide into two
• And again!
90
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)
Problem solving
• Top down• greedy
• Bottom up• dynamic
Questions?