cse202: design and analysis of algorithmsrajaiswal/winter2020/cse... · patriots: 10 giants: 10...

26
CSE202: Design and Analysis of Algorithms Ragesh Jaiswal, CSE, UCSD Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Upload: others

Post on 21-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

CSE202: Design and Analysis of Algorithms

Ragesh Jaiswal, CSE, UCSD

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 2: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Applications of Network Flow

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 3: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowTeam Elimination

Suppose there are four teams in NFL with their currentnumber of wins:

Patriots: 10Giants: 10Steelers: 10Chargers: 8

There are 7 more games to be played. These are as follows:

Chargers plays all other 3 teams.Patriots Vs Giants, Giants Vs Steelers, Patriots Vs Steelers,Giants Vs Patriots

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 4: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowTeam Elimination

Suppose there are four teams in NFL with their currentnumber of wins:

Patriots: 10Giants: 10Steelers: 10Chargers: 8

There are 7 more games to be played. These are as follows:

Chargers plays all other 3 teams.Patriots Vs Giants, Giants Vs Steelers, Patriots Vs Steelers,Giants Vs Patriots

A team is said to be eliminated if it cannot end withmaximum number of wins.

Can we say that Chargers have been eliminated give thecurrent scenario?

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 5: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowTeam Elimination

Suppose there are four teams in NFL with their currentnumber of wins:

Patriots: 10Giants: 10Steelers: 9Chargers: 8

There are 7 more games to be played. These are as follows:

Chargers plays all other 3 teams.4 games between Patriots and Giants.

Can we say that Chargers have been eliminated give thecurrent scenario?

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 6: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowTeam Elimination

Problem

There are n teams. Each team i has a current number of winsdenoted by w(i).There are G (i , j) games yet to be played betweenteam i and j . Design an algorithm to determine whether a giventeam x has been eliminated.

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 7: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowTeam Elimination

Problem

There are n teams. Each team i has a current number of wins denotedby w(i).There are G (i , j) games yet to be played between team i andj . Design an algorithm to determine whether a given team x has beeneliminated.

Consider the following flow network

Figure: Team x can end with at most m wins, i.e., m = w(x) +∑

j G (x , j)

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 8: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowTeam Elimination

Problem

There are n teams. Each team i has a current number of wins denotedby w(i).There are G (i , j) games yet to be played between team i andj . Design an algorithm to determine whether a given team x has beeneliminated.

Claim 1: Team x has been eliminated iff the maximum flow inthe network is < g∗, where g∗ =

∑i ,j s.t. x /∈{i ,j} G (i , j).

Figure: Team x can end with at most m wins, i.e., m = w(x) +∑

j G (x , j)

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 9: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowTeam Elimination

Problem

There are n teams. Each team i has a current number of wins denotedby w(i).There are G (i , j) games yet to be played between team i andj . Design an algorithm to determine whether a given team x has beeneliminated.

Claim 1: Team x has been eliminated iff the maximum flow inthe network is < g∗, where g∗ =

∑i ,j s.t. x /∈{i ,j} G (i , j).

Comment: If we can somehow find a subset T of teams (notincluding x) such that∑

i∈T w(i) +∑

i<j and i ,j∈T G (i , j) > m · |T |. Then we have awitness to the fact that x has been eliminated.

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 10: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowTeam Elimination

Problem

There are n teams. Each team i has a current number of wins denotedby w(i).There are G (i , j) games yet to be played between team i andj . Design an algorithm to determine whether a given team x has beeneliminated.

Claim 1: Team x has been eliminated iff the maximum flow inthe network is < g∗, where g∗ =

∑i ,j s.t. x /∈{i ,j} G (i , j).

Comment: If we can somehow find a subset T of teams (notincluding x) such that∑

i∈T w(i) +∑

i<j and i ,j∈T G (i , j) > m · |T |. Then we have awitness to the fact that x has been eliminated.Can we find such a subset T?

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 11: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowTeam Elimination

Claim 1: Team x has been eliminated iff the maximum flow inthe network is < g∗, where g∗ =

∑i ,j s.t. x /∈{i ,j} G (i , j).

Proof.

Claim 1.1: If x has been eliminated, then the max flow in thenetwork is < g∗.

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 12: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowTeam Elimination

Claim 1: Team x has been eliminated iff the maximum flow inthe network is < g∗, where g∗ =

∑i ,j s.t. x /∈{i ,j} G (i , j).

Proof of Claim 1

Claim 1.1: If x has been eliminated, then the max flow in thenetwork is < g∗.Claim 1.2: If the max flow is < g∗, then team x has beeneliminated.

Proof of Claim 1.2

Consider any s-t min-cut (A,B) in the graph.

Claim 1.2.1: If vij is in A, then both vi and vj are in A.

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 13: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowTeam Elimination

Claim 1: Team x has been eliminated iff the maximum flow inthe network is < g∗, where g∗ =

∑i ,j s.t. x /∈{i ,j} G (i , j).

Proof of Claim 1

Claim 1.1: If x has been eliminated, then the max flow in thenetwork is < g∗.Claim 1.2: If the max flow is < g∗, then team x has beeneliminated.

Proof of Claim 1.2

Consider any s-t min-cut (A,B) in the graph.

Claim 1.2.1: If vij is in A, then both vi and vj are in A.

Claim 1.2.2: If both vi and vj are in A, then vij is in A.

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 14: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowTeam Elimination

Claim 1: Team x has been eliminated iff the maximum flow inthe network is < g∗, where g∗ =

∑i ,j s.t. x /∈{i ,j} G (i , j).

Proof of Claim 1

Claim 1.1: If x has been eliminated, then the max flow in thenetwork is < g∗.Claim 1.2: If the max flow is < g∗, then team x has beeneliminated.

Proof of Claim 1.2

Consider any s-t min-cut (A,B) in the graph.

Claim 1.2.1: If vij is in A, then both vi and vj are in A.

Claim 1.2.2: If both vi and vj are in A, then vij is in A.

Let T be the set of teams such that i ∈ T iff vi ∈ A.

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 15: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowTeam Elimination

Claim 1: Team x has been eliminated iff the maximum flow inthe network is < g∗, where g∗ =

∑i ,j s.t. x /∈{i ,j} G (i , j).

Proof of Claim 1

Claim 1.1: If x has been eliminated, then the max flow in thenetwork is < g∗.Claim 1.2: If the max flow is < g∗, then team x has beeneliminated.

Proof of Claim 1.2

Consider any s-t min-cut (A,B) in the graph.

Claim 1.2.1: If vij is in A, then both vi and vj are in A.

Claim 1.2.2: If both vi and vj are in A, then vij is in A.

Let T be the set of teams such that i ∈ T iff vi ∈ A. Then we have:

C (A,B) =∑i∈T

(m − w(i)) +∑

{i,j}6⊂T ,i<j

G (i , j) < g∗

⇒ m · |T | −∑i∈T

w(i) +

g∗ −∑

{i,j}⊂T ,i<j

G (i , j)

< g∗

⇒∑i∈T

w(i) +∑

{i,j}⊂T ,i<j

G (i , j) > m · |T |

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 16: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowFeasible Circulation

Given a weighted directed graph representing a transportationnetwork.

There are multiple supply nodes in the graph denoting theplaces that has a factory for some product.

There are multiple demand nodes denoting the consumptionpoints.

Each supply node v has an associated supply value s(v)denoting the amount the product it can supply.

Each demand node v has a similar demand value d(v).

Question: Is there a way to ship product such that all demandand supply goals are met?

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 17: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowFeasible Circulation

Problem

Given a directed graph G with integer edge capacities. For each nodev , there is an associated demand value t(v) denoting the demand atthe node (for supply nodes this is −s(v), for demand nodes d(v), forother nodes 0). Find whether there exists a flow f such that for allnodes v :

f in(v)− f out(v) = t(v)

and the capacity constraints are met. Such a flow is called a feasiblecirculation.

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 18: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowFeasible Circulation

Problem

Given a directed graph G with integer edge capacities. For each nodev , there is an associated demand value t(v) denoting the demand atthe node (for supply nodes this is −s(v), for demand nodes d(v), forother nodes 0). Find whether there exists a flow f such that for allnodes v :

f in(v)− f out(v) = t(v)

and the capacity constraints are met. Such a flow is called a feasiblecirculation.

Claim 1: For a feasible circulation to exist,∑

v t(v) = 0. (Thatmeans supply equals the demand)

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 19: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowFeasible Circulation

Claim 1: For a feasible circulation to exist,∑

v t(v) = 0. (Thatmeans supply equals the demand)Consider the flow network as shown in the diagram below and letD =

∑demand node v d(v).

Figure: Connect source to supply nodes and demand nodes to sink.

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 20: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowFeasible Circulation

Claim 1: For a feasible circulation to exist,∑

v t(v) = 0. (Thatmeans supply equals the demand)Consider the flow network G ′ as shown in the diagram below andlet D =

∑demand node v d(v).

Claim 2: There is a feasible circulation in G iff the maximum flowin the network G ′ is D.

Figure: Connect source to supply nodes and demand nodes to sink.

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 21: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowFeasible Circulation

Claim 1: For a feasible circulation to exist,∑

v t(v) = 0. (Thatmeans supply equals the demand)Consider the flow network G ′ as shown in the diagram below andlet D =

∑demand node v d(v).

Claim 2: There is a feasible circulation in G iff the maximum flowin the network G ′ is D.

(if) Consider the max-flow and remove s, t.(only if) Extend the feasible circulation in the network.

Figure: Connect source to supply nodes and demand nodes to sink.

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 22: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowFeasible Circulation with Lower Bounds

Problem

Given a directed graph G with integer edge capacities c(e) and lowerbounds l(e). For each node v , there is an associated demand valuet(v) denoting the demand at the node (for supply nodes this is −s(v),for demand nodes d(v), for other nodes 0). Find whether there existsa flow f such that for all nodes v :

f in(v)− f out(v) = t(v)

and the following capacity constraints are met. For every edge e:

l(e) ≤ f (e) ≤ c(e)

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 23: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowFeasible Circulation with Lower Bounds

Consider a flow f such that for all edge e, f (e) = l(e).For each vertex v , let r(v) = f in(v)− f out(v).Construct a new graph G ′:

Each edge e in G ′ has capacity c(e)− l(e).Each vertex v in G ′ has a demand t(v)− r(v).

Idea: Solve the feasible circulation problem without lower boundson G ′.

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 24: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowFeasible Circulation with Lower Bounds

Consider a flow f such that for all edge e, f (e) = l(e).For each vertex v , let r(v) = f in(v)− f out(v).Construct a new graph G ′:

Each edge e in G ′ has capacity c(e)− l(e).Each vertex v in G ′ has a demand t(v)− r(v).

Idea: Solve the feasible circulation problem without lower boundson G ′.Claim: There is a feasible circulation (with lower bounds) in G iffthere is a feasible circulation in G ′.

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 25: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

Network FlowFeasible Circulation with Lower Bounds

Claim: There is a feasible circulation (with lower bounds) in G iffthere is a feasible circulation in G ′.

(if) Let f ′ be a feasible circulation in G ′. Consider f wheref (e) = f ′(e) + l(e). Is f a feasible circulation in G?(only if) Let f be a feasible circulation in G . Consider f ′ wheref ′(e) = f (e)− l(e). Is f ′ a feasible circulation in G ′?

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

Page 26: CSE202: Design and Analysis of Algorithmsrajaiswal/Winter2020/cse... · Patriots: 10 Giants: 10 Steelers: 10 Chargers: 8 There are 7 more games to be played. These are as follows:

End

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms