CH 10
CH 10
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 1
Important Examples
simplex method
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2
Linear Programming
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 3
Example
maximize 3x + 5y
subject to x + y ≤ 4
x + 3y ≤ 6 y
x ≥ 0, y ≥ 0
x + 3y = 6
( 0, 2 )
Feasible region is the ( 3, 1 )
set of points defined
by the constraints x
( 0, 0 ) ( 4, 0 )
x+y=4
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 4
Geometric solution
y
maximize 3x + 5y
subject to x+ y≤4
x + 3y ≤ 6
x ≥ 0, y ≥ 0
( 0, 2 )
( 3, 1 )
x
( 0, 0 ) ( 4, 0 )
3x + 5y = 20
3x + 5y = 14
Optimal solution: x = 3, y = 1
3x + 5y = 10
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 6
The Simplex Method
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 7
Standard form of LP problem
must be a maximization problem
all constraints (except the nonnegativity constraints) must be
in the form of linear equations
all the variables must be required to be nonnegative
maximize c1 x1 + ...+ cn xn
subject to ai 1x1+ ...+ ain xn = bi , i = 1,...,m,
x1 ≥ 0, ... , xn ≥ 0
maximize 3x + 5y maximize 3x + 5y + 0u + 0v
subject to x + y ≤ 4 subject to x + y + u =4
x + 3y ≤ 6 x + 3y + v
=6
x≥0, y≥0 x≥0, y≥0, u≥0, v≥0
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 9
Basic feasible solutions
A basic solution to a system of m linear equations in n unknowns
(n ≥ m) is obtained by setting n – m variables to 0 and solving the
resulting system to get the values of the other m variables. The
variables set to 0 are called nonbasic; the variables obtained by
solving the system are called basic.
A basic solution is called feasible if all its (basic) variables are
nonnegative.
Example x + y + u =4
x + 3y +v =6
x y u v
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 11
Outline of the Simplex Method
Step 0 [Initialization] Present a given LP problem in standard form and
set up initial tableau.
Step 1 [Optimality test] If all entries in the objective row are nonnegative —
stop: the tableau represents an optimal
solution.
Step 2 [Find entering variable] Select (the most) negative entry in the
objective row. Mark its column to indicate the
entering variable and the pivot column.
Step 3 [Find departing variable] For each positive entry in the pivot column,
calculate the θ-ratio by dividing that row's entry in the rightmost column by
its entry in the pivot column. (If there are no positive entries in the pivot
column — stop: the problem is unbounded.) Find the row with the smallest
θ-ratio, mark this row to indicate the departing variable and the pivot row.
Step 4 [Form the next tableau] Divide all the entries in the pivot row by its
entry in the pivot column. Subtract from each of the other rows, including the
objective row, the new pivot row multiplied by the entry in the pivot column
of the row in “Introduction
A. Levitin question. Replace
to the the label
Design & Analysis of the
of Algorithms,” 3rdpivot row
ed., Ch. 10 byPearson
©2012 the variable's 12
Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Example of Simplex Method Application
maximize
maximize z =z3=x +3x5y++50yu +
+ 00vu + 0v
subject to x+ y+ u
subject to x+ y+ u =4 =4
x + 3y + v =6
x + 3y + v =6
x≥0, y≥0, u≥0, v≥0
x≥0, y≥0, u≥0, v≥0
x y u v x y u v x y u v
2 1 3 1
u 1 1 1 0 4 u 0 1 2 x 1 0 3
3 3 2 2
1 1 1 1
v 1 3 0 1 6 y 1 0 2 y 0 1 1
3 3 2 2
4 5
3 5 0 0 0 0 0 10 0 0 2 1 14
3 3
4
3
Source
2 5 2
1 2 3 6
3 Sink
1
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 16
Definition of a Flow
capacity constraints
0 ≤ xij ≤ uij for every edge (i,j) E
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 17
Flow value and Maximum Flow Problem
The value of the flow is defined as the total outflow from the
source (= the total inflow into the sink).
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 18
Maximum-Flow Problem as LP problem
Maximize v = ∑ x1j
j: (1,j) E
subject to
∑ xji - ∑ xij = 0 for i = 2, 3,…,n-1
j: (j,i) E j: (i,j) E
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 19
Augmenting Path (Ford-Fulkerson) Method
Start with the zero flow (xij = 0 for every edge)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 20
Example 1
0/3 0/4
0/3
0/1
xij/uij
4 Augmenting path:
1→2 →3 →6
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 21
Example 1 (cont.)
0/3 0/4
0/3
0/1
4
Augmenting path:
1 →4 →3←2 →5 →6
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 22
Example 1 (maximum flow)
1/3 1/4
1/3
1/1
max flow value = 3
4
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 23
Finding a flow-augmenting path
To find a flow-augmenting path for a flow x, consider paths from
source to sink in the underlying undirected graph in which any
two consecutive vertices i,j are either:
• connected by a directed edge (i to j) with some positive
unused capacity rij = uij – xij
– known as forward edge ( → )
OR
• connected by a directed edge (j to i) with positive flow xji
– known as backward edge ( ← )
If a flow-augmenting path is found, the current flow can be
increased by r units by increasing xij by r on each forward edge
and decreasing xji by r on each backward edge, where
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 24
Finding a flow-augmenting path (cont.)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 25
Performance degeneration of the method
The augmenting-path method doesn’t prescribe a specific
way for generating flow-augmenting paths
Selecting a bad sequence of augmenting paths could impact
the method’s efficiency
Example 2 2
0/U 0/U
0/U 0/U
4
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 26
Example 2 (cont.)
2 2
0/U 0/U 1/U 0/U
2 2
U/U U/U 1/U 1/U
V=2U
1 0/1 3 ●●● 1 0/1 3 V=2
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 28
Vertex labeling
The source is always labeled with ∞,-
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 30
Example: Shortest-Augmenting-Path
Algorithm
5
0/3 0/4
0/3 0/1
4 3,1+
0/3 0/1
4 3,1+
Augment the flow by 1 (the sink’s first
label) along the path 1→4→3←2→5→6
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 32
Example (cont.)
5
1/3 1/4
1/3 1/1 5
1/3 1/4
4
Queue: 1 4
↑↑ ∞,-
2/2 1/5 2/2
1 2 3 6
1/3 1/1
4 2,1+
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 35
Definition of a Cut
Let X be a set of vertices in a network that includes its source but
does not include its sink, and let X, the complement of X, be the
rest of the vertices including the sink. The cut induced by this
partition of the vertices is the set of all the edges with a tail in X
and a head in X.
Capacity of a cut is defined as the sum of capacities of the edges
that compose the cut.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 36
Examples of network cuts
5
3 4
1 2 2 5 3 2
6
3
1
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 37
Max-Flow Min-Cut Theorem
The value of maximum flow in a network is equal to the
capacity of its minimum cut
V 1 2 3 4 5
U 6 7 8 9 10
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 39
Bipartite Graphs (cont.)
A bipartite graph is 2-colorable: the vertices can be colored
in two colors so that every edge has its vertices colored
differently
V 1 2 3 4 5
U 6 7 8 9 10
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 40
Matching in a Graph
A matching in a graph is a subset of its edges with the property
that no two edges share a vertex
V 1 2 3 4 5
a matching
in this graph
M = {(4,8), (5,9)}
U 6 7 8 9 10
V 1 2 3 4 5 A matched
A matching vertex
in this graph (M)
A free
vertex
U 6 7 8 9 10
V 1 2 3 4 5 V 1 2 3 4 5
U 6 7 8 9 10 U 6 7 8 9 10
V 1 2 3 4 5 V 1 2 3 4 5
U 6 7 8 9 10 U 6 7 8 9 10
Augmentation along
3, 8, 4, 9, 5, 10
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 45
BFS-based Augmenting Path Algorithm
Initialize queue Q with all free vertices in one of the sets (say V)
V 1 2 3 4 5 V 1 2 3 4 5
U 6 7 8 9 10 U 6 7 8 9 10
1
Each vertex is labeled with the vertex it was reached from. Queue deletions are
indicated by arrows. The free vertex found in U is shaded and labeled for clarity;
the new matching obtained by the augmentation is shown on the next slide.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 47
Example (cont.)
Initial Graph Resulting Graph
6 8
V 1 2 3 4 5 V 1 2 3 4 5
U 6 7 8 9 10 U 6 7 8 9 10
2 1 3
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 48
Example (cont.)
Initial Graph Resulting Graph
6 8
V 1 2 3 4 5 V 1 2 3 4 5
U 6 7 8 9 10 U 6 7 8 9 10
3 3 4 4
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 49
Example: maximum matching found
V 1 2 3 4 5
U 6 7 8 9 10
maximum
matching
V 1 2 3 4 5
1 1
1 1
1 1 1 1
1 1
U 6 7 8 9 10
1 1 1 1
1
t
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 54
Instance of the Stable Marriage Problem
An instance of the stable marriage problem can be specified
either by two sets of preference lists or by a ranking matrix, as in
the example below.
men’s preferences women’s preferences
1st 2nd 3rd 1st 2nd 3rd
Bob: Lea Ann Sue Ann: Jim Tom Bob
Jim: Lea Sue Ann Lea: Tom Bob Jim
Tom: Sue Lea Ann Sue: Jim Tom Bob
ranking matrix
Ann Lea Sue
Bob 2,3 1,2 3,3 {(Bob, Ann) (Jim, Lea) (Tom, Sue)} is unstable
Jim 3,1 1,3 2,1
{(Bob, Ann) (Jim, Sue) (Tom, Lea)} is stable
TomA. 3,2 2,1 1,2
Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 55
Stable Marriage Algorithm (Gale-Shapley)
Step 0 Start with all the men and women being free
Step 1 While there are free men, arbitrarily select one of them
and do the following:
Proposal The selected free man m proposes to w, the
next woman on his preference list
Response If w is free, she accepts the proposal to be
matched with m. If she is not free, she compares m with
her current mate. If she prefers m to him, she accepts
m’s proposal, making her former mate free; otherwise,
she simply rejects m’s proposal, leaving m free
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 56
Example
Ann Lea Sue
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 57
Example (cont.)
Ann Lea Sue
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 58
Example (cont.)
Ann Lea Sue
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 59
Analysis of the Gale-Shapley Algorithm
The algorithm terminates after no more than n2 iterations with
a stable marriage output