Lecture 18: Greedy Algorithms + Midterm Review
Tim LaRock larock.t@northeastern.edu bit.ly/cs3000syllabus
Lecture 18: Greedy Algorithms + Midterm Review Tim LaRock - - PowerPoint PPT Presentation
Lecture 18: Greedy Algorithms + Midterm Review Tim LaRock larock.t@northeastern.edu bit.ly/cs3000syllabus Business Homework 5 due tonight at midnight Boston time, solutions will be released tomorrow morning No class tomorrow, midterm review
Tim LaRock larock.t@northeastern.edu bit.ly/cs3000syllabus
Homework 5 due tonight at midnight Boston time, solutions will be released tomorrow morning No class tomorrow, midterm review moved to today Extra credit assignment available as of yesterday
Midterm 2 to be released tomorrow night, due Friday night
intuitively guide us towards a solution
edges that can be included in these solutions!
that the algorithm will get stuck if it is not able to modify the flow!
eventually reach an optimal global solution are called greedy algorithms
,
1 1 1 2 2 3 3 3 4 4
5
,
5 ,./
1 1 1 2 2 3 3 3 4 4
5
,
5 ,./
1 1 1 2 2 3 3 3 4 4 = 1 4 β 3 + 5 + 8 + 10 = 26 4 π½ πππ‘π’ = 1 4 β πππ‘π’(1) + πππ‘π’(2) + πππ‘π’(3) + πππ‘π’(4)
We can modify the order of the files on the tape, resulting in a permutation π where π(π) returns the index of the file in the πth block. We can then rewrite the expected (average) cost of accessing file k as π½ πππ‘π’(π) = 1 π + + π[π(π)]
,
5 ,./
Intuitively: To minimize average cost, we should store the smallest files first,
smaller ones! But how do we prove that this is the optimal strategy?
1 1 1 2 2 3 3 3 4 4 2 2 4 4 1 1 1 3 3 3 π½ πππ‘π’ = 26 4
We can modify the order of the files on the tape, resulting in a permutation π where π(π) returns the index of the file in the πth block. We can then rewrite the expected (average) cost of accessing file k as π½ πππ‘π’(π) = 1 π + + π[π(π)]
,
5 ,./
Intuitively: To minimize average cost, we should store the smallest files first,
smaller ones! But how do we prove that this is the optimal strategy?
1 1 1 2 2 3 3 3 4 4 2 2 4 4 1 1 1 3 3 3 π½ πππ‘π’ = 26 4 π½ πππ‘π’(π) = 2 + 4 + 7 + 10 4 = 23 4
1. Find the unwritten file with minimum length (break ties arbitrarily) 2. Write that file to the tape
1. Find the unwritten file with minimum length (break ties arbitrarily) 2. Write that file to the tape
Claim: π½ πππ‘π’ π is minimized when π π π β€ π[π π + 1 ] for all π. Proof: Let a = π π and π = π(π + 1) and suppose π π > π[π] for some index π. If we swap the files π and π on the tape, then the cost of accessing π increases by π[π] and the cost of accessing π decreases by π[π]. Overall, the swap changes the expected cost by
H I JH[K] 5
. This change represents an improvement because π π < π[π]. Thus, if the files are out of order, we can decrease expected cost by swapping pairs to put them in order.
1 1 1 2 2 3 3 3 4 4
Claim: π½ πππ‘π’ π is minimized when π π π β€ π[π π + 1 ] for all π. Proof: Let a = π π and π = π(π + 1) and suppose π π > π[π] for some index π. If we swap the files π and π on the tape, then the cost of accessing π increases by π[π] and the cost of accessing π decreases by π[π]. Overall, the swap changes the expected cost by
H I JH[K] 5
. This change represents an improvement because π π < π[π]. Thus, if the files are out of order, we can decrease expected cost by swapping pairs to put them in order.
1 1 1 2 2 3 3 3 4 4
Claim: π½ πππ‘π’ π is minimized when π π π β€ π[π π + 1 ] for all π. Proof: Let a = π π and π = π(π + 1) and suppose π π > π[π] for some index π. If we swap the files π and π on the tape, then the cost of accessing π increases by π[π] and the cost of accessing π decreases by π[π]. Overall, the swap changes the expected cost by
H I JH[K] 5
. This change represents an improvement because π π < π[π]. Thus, if the files are out of order, we can decrease expected cost by swapping pairs to put them in order.
1 1 1 2 2 3 3 3 4 4
Claim: π½ πππ‘π’ π is minimized when π π π β€ π[π π + 1 ] for all π. Proof: Let a = π π and π = π(π + 1) and suppose π π > π[π] for some index π. If we swap the files π and π on the tape, then the cost of accessing π increases by π[π] and the cost of accessing π decreases by π[π]. Overall, the swap changes the expected cost by
H K JH[I] 5
. This change represents an improvement because π π < π[π]. Thus, if the files are out of order, we can decrease expected cost by swapping pairs to put them in order.
1 1 1 2 2 3 3 3 4 4
Claim: π½ πππ‘π’ π is minimized when π π π β€ π[π π + 1 ] for all π. Proof: Let a = π π and π = π(π + 1) and suppose π π > π[π] for some index π. If we swap the files π and π on the tape, then the cost of accessing π increases by π[π] and the cost of accessing π decreases by π[π]. Overall, the swap changes the expected cost by
H K JH[I] 5
. This change represents an improvement because π π < π[π]. Thus, if the files are out of order, we can decrease expected cost by swapping pairs to put them in order.
1 1 1 2 2 3 3 3 4 4
Claim: π½ πππ‘π’ π is minimized when π π π β€ π[π π + 1 ] for all π. Proof: Let a = π π and π = π(π + 1) and suppose π π > π[π] for some index π. If we swap the files π and π on the tape, then the cost of accessing π increases by π[π] and the cost of accessing π decreases by π[π]. Overall, the swap changes the expected cost by
H K JH[I] 5
. This change represents an improvement because π π < π[π]. Thus, if the files are out of order, we can decrease expected cost by swapping pairs to put them in order.
1 1 1 2 2 3 3 3 4 4
Average cost for example above:
MN O
Average cost after swapping files 1 and 2:
/ O 2 + 5 + 8 + 10 = MP O
26 4 + 2 β 3 4 = 26 β 1 4 = 25 4
Claim: π½ πππ‘π’ π is minimized when π π π β€ π[π π + 1 ] for all π. Proof: Let a = π π and π = π(π + 1) and suppose π π > π[π] for some index π. If we swap the files π and π on the tape, then the cost of accessing π increases by π[π] and the cost of accessing π decreases by π[π]. Overall, the swap changes the expected cost by
H K JH[I] 5
. This change represents an improvement because π π < π[π]. Thus, if the files are out of length-order, we can decrease expected cost by swapping pairs to put them in order.
1 1 1 2 2 3 3 3 4 4
Greedy algorithms repeatedly apply a simple rule to eventually find an
Inductive Exchange Arguments are strategies for proving correctness of some greedy algorithms Next Week: Data Compression with Huffman Codes Proof strategies for greedy algorithms Inductive exchange Greedy-stays-ahead
BFS DFS Best 2 4 5 5 1 3 3 1
BFS DFS Best 2 4 5 5 1 3 Currently visiting neighbors Visited 3 1 In the (priority) queue/on the stack
BFS DFS Best 2 4 5 5 1 3 Currently visiting neighbors Visited 3 1 In the (priority) queue/on the stack
BFS DFS Best 2 4 5 5 1 3 Currently visiting neighbors Visited 3 1 In the (priority) queue/on the stack
BFS DFS Best 2 4 3 1 5 5 1 3 Currently visiting neighbors Visited In the (priority) queue/on the stack
BFS DFS Best 2 4 3 1 5 5 1 3 Currently visiting neighbors Visited In the (priority) queue/on the stack
BFS DFS Best 2 4 5 5 1 3 Currently visiting neighbors Visited 3 1 In the (priority) queue/on the stack
BFS DFS Best 2 4 5 5 1 3 Currently visiting neighbors Visited 3 1 In the (priority) queue/on the stack
BFS DFS Best 2 4 5 5 1 3 Currently visiting neighbors Visited 3 1 In the (priority) queue/on the stack
BFS DFS Best 2 4 5 5 1 3 Currently visiting neighbors Visited 3 1 In the (priority) queue/on the stack
BFS DFS Best 2 4 5 5 1 3 In the (priority) queue/on the stack Currently visiting neighbors Visited 3 1
BFS DFS Best 2 4 5 5 1 3 Currently visiting neighbors Visited 3 1 In the (priority) queue/on the stack
π» = (π, πΉ) is a graph visited[π£] = 0 for all π£ β π clock = 1 πΈπΊπ π£ : visited[π£] = 1 For π€ β ππππβπππ π‘(π£): If visited[π€] = 0: parent[π€] = π£ DFS(π€) post-visit(π£) post-visit(π£): set postorder[π£] = clock clock β clock + 1 Recursive DFS with post-ordering
π€M π€j π€O π€P π€/ π€N π€k π€M π€j π€O π€P π€/ π€N π€k
u c b a
Vertex
u a b c
Postorder
4 1 3 2 u c b a 4 3 2 1
1 2 6 3 5 4 7 8
Label Connected Components
1 2 6 3 5 4 7 8
Add Safe Edges
1 1 1 2 1 2 1 1
Label Connected Components
1 1 1 2 1 2 1 1
Add Safe Edges
1 1 1 1 1 1 1 1
Done!
add π€ to π
) and a flow π, an augmenting path π is an π‘ β π’ path such that π(π) < π(π) for every edge π β π
s 1 2 t 10 10 10 10 10 10 20 20 30
Adding uniform flow
path results in a new valid s-t flow!
x
βͺ ππ βΆ π π > 0 .
u v
π(π) π(π)
u v
π π β π(π) π(π) πw π
Augment(Gf, P) b Β¬ the minimum capacity of an edge in P for e Γ P if e Γ E: f(e) Β¬ f(e) + b else: f(e) Β¬ f(e) - b return f Note: This is the same process as the recurrence in Erickson 10.3!
Augment(Gf, P) b Β¬ the minimum capacity of an edge in P for e Γ P if e Γ E: f(e) Β¬ f(e) + b else: f(e) Β¬ f(e) - b return f FordFulkerson(G,s,t,{c(e)}) for e Γ E: f(e) Β¬ 0 Gf is the residual graph while (there is an s-t path P in Gf) f Β¬ Augment(Gf,P) update Gf return f
s 1 2 t 10 10 20 20 30 s 1 2 t
s 1 2 t 10 10 20 20 30 s 1 2 t 10 10 20 20 30
π β πΉ πw β πΉ
s 1 2 t 10 10 20 20 30 s 1 2 t 10 10 20 20 30
π β πΉ πw β πΉ
s 1 2 t 10 10 20 20 30 s 1 2 t 10 10 20 20 20 20 20 30
π β πΉ πw β πΉ
s 1 2 t 10 10 20 20 20 20 20 30 s 1 2 t 10 10 20
π β πΉ πw β πΉ
10 20 20
s 1 2 t 10 10 20 20 20 20 20 30 s 1 2 t 10 10 20
π β πΉ πw β πΉ
10 20 20
s 1 2 t 10 10 20 10 10 20 10 20 20 30 s 1 2 t 10 10 20
π β πΉ πw β πΉ
10 20 20
s t 10 10 s 1 2 t 10 10 20 10 10 20 10 20 20 30 1 2 10 20 20 20
in networks with integer capacities
minimum s-t cut
in π»xβ gives a minimum cut
maximum flow