Daa Unit4 PDF
Daa Unit4 PDF
Example
maximize 3x + 5y
subject to x+ y≤4
x + 3y ≤ 6
x ≥ 0, y ≥ 0
Feasible region is the set of points defined by the constraints
y
x + 3y = 6
( 0, 2 )
( 3, 1 )
x
( 0, 0 ) ( 4, 0 )
x+y=4
Geometric solution
y
( 0, 2 )
( 3, 1 )
x
( 0, 0 ) ( 4, 0 )
3x + 5y = 20
3x + 5y = 14
3x + 5y = 10
Optimal solution: x = 3, y = 1
CS6402 __ Design and Analysis of Algorithms _ Unit IV_ ____4.2
Extreme Point Theorem Any LP problem with a nonempty bounded feasible region has an
optimal solution; moreover, an optimal solution can always be found at an extreme point of the
problem's feasible region.
Example
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
Variables u and v, transforming inequality constraints into equality constrains, are called slack
variables
Simplex Tableau
maximize z = 3x + 5y + 0u + 0v
subject to x+ y+ u =4
x + 3y + v =6
x≥0, y≥0, u≥0, v≥0
x y u v
u 1 1 1 0 4
v 1 3 0 1 6
3 5 0 0 0
objective row
basic variables = u,v
basic feasible solution = (0, 0, 4, 6)
value of z at (0, 0, 4, 6) =0
x y u v
u 1 1 1 0 4
v 1 3 0 1 6
3 5 0 0 0
x y u v
x 1 0 3/2 1/3 3
y 0 1 1/2 1/2 1
0 0 2 1 14
basic feasible sol. (3, 1, 0, 0) z = 14
Example 1:
Use Simplex method to solve the formers problem given below.
A farmer has a 320 acre farm on which she plants two crops: corn and soybeans. For each
acre of corn planted, her expenses are $50 and for each acre of soybeans planted, her expenses are
$100. Each acre of corn requires 100 bushels of storage and yields a profit of $60; each acre of
CS6402 __ Design and Analysis of Algorithms _ Unit IV_ ____4.5
soybeans requires 40 bushels of storage and yields a profit of $90. If the total amount of storage
space available is 19,200 bushels and the farmer has only $20,000 on hand, how many acres of
each crop should she plant in order to maximize her profit? What will her profit be if she follows
this strategy?
Solution
Linear Programming Problem Formulation
Corn Soybean Total
Expenses $50 $100 $20,000
Storage(bushels) 100 40 19,200
Profit 60 90 Maximize profit
A farmer has a 320 acre farm is unwanted data but c+s<=320.
Pivot row:
Replace the leaving variable in basic column with the entering variable.
New Pivot Row = Current Pivot Row / Pivot Element
All other rows including z:
New Row = Current Row – (Its Pivot column coefficient)* New Pivot Row
CS6402 __ Design and Analysis of Algorithms _ Unit IV_ ____4.7
Row y
New Pivot Row = Current Pivot Row / Pivot Element
= (0, 50, 100, 1, 0, 20000) / 100
= (0, , 1, , 0, 200)
Row s2
New Row = Current Row – (Its Pivot column coefficient)* New Pivot Row
= (0, 100, 40, 0, 1, 19200) - (40)*( 0, , 1, , 0, 200)
−
= (0, 80, 0, , 1, 96)
Row z
New Row = Current Row – (Its Pivot column coefficient)* New Pivot Row
= (1, -60, -90, 0, 0, 0) - (-90)*( 0, , 1, , 0, 200)
= (1, -60, -90, 0, 0, 0) + (90)*( 0, , 1, , 0, 200)
= (1, -15, 0, , 0, 18000)
Iteration II
Basic z x y s1 s2 Solution
NPR y 0 1/2 1 1/100 0 200
s2 0 80 0 -4/10 1 96
z 1 -15 0 9/10 0 18000
Row x
New Pivot Row = Current Pivot Row / Pivot Element
−
= (0, 80, 0, , 1, 11200) / 80
−
= (0, , 0, , , 140)
Row y
CS6402 __ Design and Analysis of Algorithms _ Unit IV_ ____4.8
New Row = Current Row – (Its Pivot column coefficient)* New Pivot Row
−
= (0, 1/2, 1, 1/100, 0, 200) - ( )*( 0, , 0, , , 140)
−
= (0, 0, 1, , , 130)
Row z
New Row = Current Row – (Its Pivot column coefficient)* New Pivot Row
−
= (1, -15, 0, , 0, 18000) - (-15)*( 0, , 0, , , 140)
−
= (1, -15, 0, , 0, 18000) + (15)*( 0, , 0, , , 140)
= (1, 0, 0, , , 20100)
Iteration III
Basic z x y s1 s2 Solution
y 0 0 1 1/80 -1/160 130
x 0 1 0 -1/200 1/80 140
z 1 0 0 33/40 15/80 20100
= ∑ ,
=
subject to:
∑ i = , ,...,m ,
=
j = , ,...,n .
Dual
Minimize
′ = ∑ ,
=
subject to:
∑ j = , ,...,n ,
=
i = , ,...,m .
Example:
CS6402 __ Design and Analysis of Algorithms _ Unit IV_ ____4.9
Definition of a Flow
A flow is an assignment of real numbers xij to edges (i,j) of a given network that satisfy the
following:
flow-conservation requirements
The total amount of material entering an intermediate vertex must be equal to the total
amount of the material leaving the vertex
capacity constraints
0 ≤ xij ≤ uij for every edge (i,j) E
Augmenting path: 1 2 3 6
xij/uij
Augmenting path: 1 4 3 2 5 6
Example 1 (maximum flow)
CS6402 __ Design and Analysis of Algorithms _ Unit IV_ ____4.12
Example 2
CS6402 __ Design and Analysis of Algorithms _ Unit IV_ ____4.13
1→2→4→3
1→4←2→3 V=1
V=2
…
V=2U
Requires 2U iterations to reach maximum flow of value 2U
Shortest-Augmenting-Path Algorithm
Generate augmenting path with the least number of edges by BFS as follows.
Starting at the source, perform BFS traversal by marking new (unlabeled) vertices with two labels:
• first label – indicates the amount of additional flow that can be brought from the
source to the vertex being labeled
• second label – indicates the vertex from which the vertex being labeled was reached,
with “+” or “–” added to the second label to indicate whether the vertex was
reached via a forward or backward edge
Vertex labeling
The source is always labeled with ∞,-
All other vertices are labeled as follows:
o If unlabeled vertex j is connected to the front vertex i of the traversal queue by a
directed edge from i to j with positive unused capacity rij = uij –xij (forward edge),
vertex j is labeled with lj,i+, where lj = min{li, rij}
CS6402 __ Design and Analysis of Algorithms _ Unit IV_ ____4.14
Queue: 1 2 4 3 5 6
Augment the flow by 2 (the sink’s first label) along the path 1 2 3 6
CS6402 __ Design and Analysis of Algorithms _ Unit IV_ ____4.15
Queue: 1 4 3 2 5 6
Augment the flow by 1 (the sink’s first label) along the path 1 4 3 2 5 6
Queue: 1 4
ALGORITHM ShortestAugmentingPath(G)
//Implements the shortest-augmenting-path algorithm
//Input: A network with single source 1, single sink n, and positive integer capacities uij on
// its edges (i, j )
//Output: A maximum flow x
assign xij= 0 to every edge (i, j ) in the network
label the source with ∞, − and add the source to the empty queue Q
while not Empty(Q) do
i Front(Q); Dequeue(Q)
for every edge from i to j do //forward edges
if j is unlabeled
rij uij− xij
if rij > 0
lj min{li, rij}; label j with lj, i +
Enqueue(Q, j )
for every edge from j to i do //backward edges
if j is unlabeled
if xji > 0
lj min{li, xji }; label j with lj, i−
Enqueue(Q, j )
if the sink has been labeled
//augment along the augmenting path found
j n //start at the sink and move backwards using second labels
while j ≠ 1 //the source hasn’t been reached
if the second label of vertex j is i+
xij xij+ ln
else //the second label of vertex j is i−
xij xij −ln
j i; i the vertex indicated by i’s second label
erase all vertex labels except the ones of the source
reinitialize Q with the source
return x //the current flow is maximum
CS6402 __ Design and Analysis of Algorithms _ Unit IV_ ____4.17
Time Efficiency
• The number of augmenting paths needed by the shortest-augmenting-path algorithm
never exceeds nm/2, where n and m are the number of vertices and edges, respectively.
• Since the time required to find shortest augmenting path by breadth-first search is in
O(n+m)=O(m) for networks represented by their adjacency lists, the time efficiency of
the shortest-augmenting-path algorithm is in O(nm2) for this representation.
• More efficient algorithms have been found that can run in close to O(nm) time, but these
algorithms don’t fall into the iterative-improvement paradigm.
A graph is bipartite if and only if it does not have a cycle of an odd length.
A bipartite graph is 2-colorable: the vertices can be colored in two colors so that every edge
has its vertices colored differently
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
A maximum (or maximum cardinality) matching is a matching with the largest number of edges
• always exists
• not always unique
Augmentation along 3, 8, 4, 9, 5, 10
Matching on the right is maximum (perfect matching).
Theorem: A matching M is maximum if and only if there exists no augmenting path with
respect to M.
Augmenting Path Method (template)
• Start with some initial matching . e.g., the empty set
• Find an augmenting path and augment the current matching along that path. e.g., using
breadth-first search like method
• When no augmenting path can be found, terminate and return the last matching, which is
maximum
ranking matrix
Ann Lea Sue
Bob 2,3 1,2 3,3
Jim 3,1 1,3 2,1
Tom 3,2 2,1 1,2
CS6402 __ Design and Analysis of Algorithms _ Unit IV_ ____4.20
Data for an instance of the stable marriage problem. (a) Men’s preference lists; (b) women’s preference lists. (c) Ranking
matrix (with the boxed cells composing an unstable matching).
Example
Free men: Bob, Jim, Tom
Ann Lea Sue