1.03k likes | 1.22k Views
Discrete Optimization Lecture 5. M. Pawan Kumar pawan.kumar@ecp.fr. Exam Question Type 1. Q. Find the distance of the shortest path from s=v 0 to all vertices in the graph using Dijkstra’s method. Show the estimate of the distance at each iteration. s. v 0. 4. 2. 1. v 1. v 4. 3. 1.
E N D
Discrete OptimizationLecture 5 M. Pawan Kumar pawan.kumar@ecp.fr
Exam Question Type 1 Q. Find the distance of the shortest path from s=v0 to all vertices in the graph using Dijkstra’s method. Show the estimate of the distance at each iteration. s v0 4 2 1 v1 v4 3 1 Iteration = 1 6 v2 v5 3 5 Iteration = 2 v3 2 v6 …….. 1 7 Iteration = 8 v7
Exam Question Type 2 • Q. Provide the code for the following function that implements the Floyd-Warshall algorithm in O(n3) time. • void floyd_warshall(int n, double **A, double **D); • n = number of vertices in the graph • A = nxn array to store the input, • A[i][j] = length of arc (i,j), 0 ≤ i < n, 0 ≤ j < n • D = nxn array to store the output • Use the following assumptions. • The graph has no negative length directed circuit. • If (i,j) is not an arc, A[i][j] = infinity • double **allocate_memory(int n, int m) returns an nxm array • free_memory(double **D, int n, int m) frees an nxm array • …..
void floyd_warshall(int n, double **A, double **D) { double **D_prev = allocate_memory(n,n); inti, j, k; for(i=0;i<n;i++) { for(j=0;j<n;j++) { D[i][j] = A[i][j]; } } for(k=0;k<n;k++) { for(i=0;i<n;i++) { for(j=0;j<n;j++) { D_prev[i][j] = D[i][j]; } } for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(D_prev[i][k] + D_prev[k][j] < D[i][j]) { D[i][j] = D_prev[i][k] + D_prev[k][j]; } } } } free_memory(D_prev,n,n); }
Exam Question Type 3 Q. Prove that Dijkstra’s algorithm is correct when the length of the arcs is non-negative. Let l(a,b) be the length of the arc from vertex a to vertex b. Let d(a) be the current estimate of the distance from the source vertex s to the vertex a. Let dist(a) be the distance of the shortest path from the source vertex s to the vertex a. By definition, d(a) ≥ dist(a). We initialize U = V, where V is the set of all vertices. At each iteration, we choose u = argminaU d(a). We set U = U – {u}, and d(v) = min{d(v),d(u)+l(u,v)} We will show that d(u) = dist(u). At the first iteration, u = s and d(u) = 0. Since all arc lengths are non-negative, it follows that d(s) = dist(s) = 0. At iteration t, let us assume that d(u) > dist(u). There exists a shortest path s, v1,v2, … vk, u from s to u. Let i be the smallest index such that i U. d(vi) ≤ d(vi-1) + l(vi-1,vi) = dist(vi-1)+l(vi-1,vi) = dist(vi) Therefore, d(vi) = dist(vi), which implies that d(vi) < d(u). This implies that u cannot be chosen at the current iteration. Hence, by contradiction d(u) = dist(u).
Outline • Interactive Image Segmentation • Minimum-Cut • From MAP Estimation to Minimum-Cut • Solving Minimum-Cut
Image Segmentation How ? Pose the problem as MAP estimation.
v1 v2 v3 .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. vn Random Variables Each random variable va corresponds to a pixel Set of all variables = V = {v1,v2,…,vn}
v1 v2 v3 .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. vn Edges Edge (va,vb) connects two neighboring pixels. Neighbors: top, down, left and right Set of all edges = E
v1 v2 v3 .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. vn Labels Label set L = {l0,l1} Label l0 corresponds to “object” Label l1 corresponds to “background”
v1 v2 v3 .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. vn Labeling Labeling f: V L We say va is assigned a label lf(a) 2n Number of possible labelings?
Unary Potentials θa;1 θa;0 va Create a histogram of RGB values H0(R,G,B) = number of pixels with color = (R,G,B) number of pixels
Unary Potentials θa;1 θa;0 va θa;0 = -log H0(R(a),G(a),B(a)) H0(R,G,B) = number of pixels with color = (R,G,B) number of pixels
Unary Potentials θa;1 θa;0 va θa;0 will be low/high?
Unary Potentials θa;1 θa;0 va θa;0 will be low/high?
Unary Potentials θa;1 θa;0 va Create a histogram of RGB values H1(R,G,B) = number of pixels with color = (R,G,B) number of pixels
Unary Potentials θa;1 θa;0 va θa;1 = -log H1(R(a),G(a),B(a)) H0(R,G,B) = number of pixels with color = (R,G,B) number of pixels
Unary Potentials θa;1 θa;0 va θa;1 will be low/high?
Unary Potentials θa;1 θa;0 va θa;1 will be low/high?
Pairwise Potentials θab;11 θab;01 θab;10 θab;00 va vb Neighboring pixels tend to have the same label.
Pairwise Potentials 0 θab;01 θab;10 0 va vb Neighboring pixels tend to have the same label.
Pairwise Potentials 0 θab;01 θab;10 0 va vb Similar color tends to imply same label. D(a,b) = difference in RGB values of a and b θab;01 = θab;10 = exp(-D(a,b)).
Pairwise Potentials 0 θab;01 θab;10 0 va vb θab;01 and θab;10 will be low/high?
Pairwise Potentials 0 θab;01 θab;10 0 va vb θab;01 and θab;10 will be low/high?
v1 v2 v3 .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. vn Energy Function Labeling f: V L We say va is assigned a label lf(a) Q(f) = ΣaVθa;f(a) + Σ(a,b)Eθab;f(a)f(b)
v1 v2 v3 .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. vn MAP Estimation Labeling f: V L We say va is assigned a label lf(a) minf Q(f) = ΣaVθa;f(a) + Σ(a,b)Eθab;f(a)f(b)
Outline • Interactive Image Segmentation • Minimum-Cut (A Special Case) • From MAP Estimation to Minimum-Cut • Solving Minimum-Cut
Directed Graph D = (V, A) 10 v1 v2 Two important restrictions 3 2 (1) Rational arc lengths (2) Positive arc lengths v3 v4 5
Cut D = (V, A) • Let V1 and V2 such that • V1 “union” V2 = V • V1 “intersection” V2 = Φ 10 v1 v2 3 2 • C is a set of arcs such that • (u,v) A • u V1 • v V2 v3 v4 5 C is a cut in the digraph D
Cut D = (V, A) V1 What is C? 10 v1 v2 {(v1,v2),(v1,v4)} ? 3 2 {(v1,v4),(v3,v2)} ? ✓ v3 v4 {(v1,v4)} ? 5 V2
Cut D = (V, A) V1 V2 What is C? 10 v1 v2 {(v1,v2),(v1,v4),(v3,v2)} ? 3 2 ✓ {(v4,v3)} ? v3 v4 {(v1,v4),(v3,v2)} ? 5
Cut D = (V, A) V2 V1 What is C? 10 v1 v2 ✓ {(v1,v2),(v1,v4),(v3,v2)} ? 3 2 {(v3,v2)} ? v3 v4 {(v1,v4),(v3,v2)} ? 5
Cut D = (V, A) • Let V1 and V2 such that • V1 “union” V2 = V • V1 “intersection” V2 = Φ 10 v1 v2 3 2 • C is a set of arcs such that • (u,v) A • u V1 • v V2 v3 v4 5 C is a cut in the digraph D
Weight of a Cut D = (V, A) 10 v1 v2 3 Sum of length of all arcs in C 2 v3 v4 5
Weight of a Cut D = (V, A) 10 v1 v2 3 w(C) = Σ(u,v) C l(u,v) 2 v3 v4 5
Weight of a Cut D = (V, A) V1 What is w(C)? 10 v1 v2 3 3 2 v3 v4 5 V2
Weight of a Cut D = (V, A) V1 V2 What is w(C)? 10 v1 v2 5 3 2 v3 v4 5
Weight of a Cut D = (V, A) V2 V1 What is w(C)? 10 v1 v2 15 3 2 v3 v4 5
st-Cut s D = (V, A) 1 2 A source vertex “s” 10 v1 v2 A sink vertex “t” 3 2 • C is a cut such that • s V1 • t V2 v3 v4 5 7 t 3 C is an st-cut
Weight of an st-Cut s D = (V, A) 1 2 10 v1 v2 3 w(C) = Σ(u,v) C l(u,v) 2 v3 v4 5 7 t 3
Weight of an st-Cut s D = (V, A) 1 2 What is w(C)? 10 v1 v2 3 3 2 v3 v4 5 7 t 3
Weight of an st-Cut s D = (V, A) 1 2 What is w(C)? 10 v1 v2 15 3 2 v3 v4 5 7 t 3
Minimum Cut Problem s D = (V, A) 1 2 Find a cut with the minimum weight !! 10 v1 v2 3 2 C* = argminC w(C) v3 v4 5 7 t 3
Solvers for the Minimum-Cut Problem Augmenting Path and Push-Relabel n: #nodes m: #edges U: maximum arc length [Slide credit: Andrew Goldberg]
Remember … Two important restrictions (1) Rational arc lengths (2) Positive arc lengths
Cut D = (V, A) • Let V1 and V2 such that • V1 “union” V2 = V • V1 “intersection” V2 = Φ 10 v1 v2 3 2 • C is a set of arcs such that • (u,v) A • u V1 • v V2 v3 v4 5 C is a cut in the digraph D
st-Cut s D = (V, A) 1 2 A “source” vertex s 10 v1 v2 A “sink” vertex t 3 2 • C is a cut such that • s V1 • t V2 v3 v4 5 7 t 3 C is an st-cut
Minimum Cut Problem s D = (V, A) 1 2 Find a cut with the minimum weight !! 10 v1 v2 3 2 C* = argminC w(C) v3 v4 5 w(C) = Σ(u,v) C l(u,v) 7 t 3
Outline • Interactive Image Segmentation • Minimum-Cut • From MAP Estimation to Minimum-Cut • Solving Minimum-Cut
Overview Digraph D Energy Q One vertex per random variable + Additional vertices “s” and “t” Compute Minimum Cut va V1 implies f(a) = 0 Labeling f* V = V1 U V2 va V2 implies f(a) = 1