Network Flow Part II

Download Report

Transcript Network Flow Part II

Announcements

Finish up Network Flow today

Then Review for Final on Monday
◦ HW#5 is due on Monday, let me or the TA’s know if you
have trouble starting.

The Final is one week from Monday 12/13 at 4pm –
6:50pm in this room.

Review Material has been posted
◦ Topics, Questions and Answers (some of the answers are
unfinished – I will try to finish these up and re-post soon)
◦ Remo’s Review Notes – very detailed, very good resource.
Network Flow
The Maximum Flow Problem
and
The Ford-Fulkerson Algorithm
Network Flow

The network flow problem is
as follows:
◦ Given a connected directed
graph G
 with non-negative integer weights,
 (where each edge stands for the
capacity of that edge),
◦ 2 different vertices, s and t,
called the source and the sink,
 such that the source only has outedges and the sink only has inedges,
◦ Find the maximum amount of
some commodity that can flow
through the network from
source to sink.
12
a
b
20
16
s
4
10
9
7
t
4
c
14
d
Each edge stands for the
capacity of that edge.
Network Flow

One way to imagine the
situation is imagining each edge
as a pipe that allows a certain
flow of a liquid per second.
◦ The source is where the liquid is
pouring from, and the sink is
where it ends up.
◦ Each edge weight specifies the
maximal amount of liquid that
can flow through that pipe per
second.
◦ Given that information, what is
the most liquid that can flow
from source to sink per second,
in the steady state?
12
a
b
20
16
s
4
10
9
7
t
4
c
14
d
Each edge stands for the
capacity of that edge.
Network Flow
◦ Residual capacity – is simply an edge’s unused
capacity.
◦ Augmenting path – defined as one where you have a
path from the source to the sink where every edge
has a non-zero residual capacity.
a
0/12
b
0/20
0/16
Using the notation: used / capacity.
0/9
s
0/4
0/10
t
0/7
0/4
0/13
c
0/14
d
Residual Capacity: capacity - used.
Network Flow
◦ Find the Maximum Flow if the Network:
a
12
b
20
16
9
4 10
s
t
7
3
4
c
14
d
Ford-Fulkerson Algorithm
While there exists an augmenting path
Add the appropriate flow to that augmenting path

So we’re going to arbitrarily choose an the
augmenting path s,c,d,t in the graph below:
◦ And add the flow to that path.
a
0/12
b
0/20
0/16
0/9
s
0/4
0/10
t
0/7
4/4
0/4
4/13
0/13
c
4/14
0/14
d
◦ Residual capacity of a path –
the minimum of the residual
capacities of the edges on that
path.
◦ 4 in this case, which is the
limiting factor for this path’s
flow.
Ford-Fulkerson Algorithm

We can represent the flow taken with back-edges in a Residual Graph.
◦ This will allow us to “undo” or redistribute the flow we have already
taken.
◦ So flow taken is a back edge, and any residual flow is a forward edge.
Residual Graph:
12
a
b
16
Next choice for
augmenting path:
s, a, b, t
20
10
9
7
s
4
t
4
9
c
10
4
d
Ford-Fulkerson Algorithm
While there exists an augmenting path
Add the appropriate flow to that augmenting path

Choose another augmenting path (one where you have a path from the
source to the sink where every edge has a non-zero residual capacity.)
◦ s,a,b,t
Residual Graph:
a
12
b
12
4
8
10
4
s
9
7
4
t
4
9
c
10
4
d
12
Next choice for
augmenting path:
s, c, d, b, t
Ford-Fulkerson Algorithm
While there exists an augmenting path
Add the appropriate flow to that augmenting path

Choose another augmenting path (one where you have a path from the
source to the sink where every edge has a non-zero residual capacity.)
◦ s,c, d, b, t
Residual Graph:
a
12
b
12
4
1
10
4
s
9
7
11
t
4
2
c
3
11
d
19
Are there any more
augmenting paths?
No! We’re done
The maximum flow =
19 + 4 = 23
Examples on the Board…
Example




In a department there are n courses and m instructors.
Every instructor has a list of courses he or she can teach.
Every instructor can teach at most 3 courses during a year.
The goal: find an allocation of courses to the instructors subject to
these constraints.
Network Flow – Optional Homework
Problem

An orchestra is trying to fill all of its positions. For different
instruments, it needs a different number of players.
◦ For example, it might need 20 violinists but only 3 trumpet players.

Each potential candidate for the orchestra can play some set of
instruments.
◦ For example, one player might be able to play either a violin or a viola,
while another might be able to play a trumpet, trombone or tuba.

Given the following information:
a)
b)
A list of how many of each position the orchestra needs.
A list of each candidate and which instruments they can play.
Determine whether or not the orchestra can fill all its
positions.
 Describe how to solve this problem using network flow.

Ford-Fulkerson Algorithm - Runtime
While there exists an augmenting path
Add the appropriate flow to that augmenting path

We can check the existence of an augmenting path by doing a
graph traversal on the network (with all full capacity edges
removed.)
◦ This graph, a subgraph with all edges of full capacity removed is called a
residual graph.

It is difficult to analyze the true running time of this algorithm
because it is unclear exactly how many augmenting paths can
be found in an arbitrary flow network.
◦ In the worst case, each augmenting path adds 1 to the flow of a
network, and each search for an augmenting path takes O(E) time,
where E is the number of edges in the graph.
◦ Thus, at worst-case, the algorithm takes O(|f|E) time, where |f| is the
maximal flow of the network.
Edmonds-Karp Algorithm

This algorithm is a variation on the FordFulkerson method which is intended to
increase the speed of the first algorithm.

The idea is to try to choose good augmenting
paths.
◦ In this algorithm, the augmenting path suggested is
the augmenting path with the minimal number of
edges.
 (We can find this using BFS, since this finds all paths of a
certain length before moving on to longer paths.)
◦ The total number of iterations of the algorithm
using this strategy is O(VE). Thus, its total running
time is O(VE2).
Maximum Bipartite Matching



A bipartite graph – one
whose vertices can be split
in two sets such that there
is no edge connecting
vertices in the same set.
A matching in a graph is a
set of edges such that no
vertex is touched by more
than one edge.
A maximum matching is a
matching with max
cardinality (the most
edges).
Rob
A
Joe
B
Bob
C
Mark
D
Tom
E
Maximum Bipartite Matching



A bipartite graph – one
whose vertices can be split
in two sets such that there
is no edge connecting
vertices in the same set.
A matching in a graph is a
set of edges such that no
vertex is touched by more
than one edge.
A maximum matching is a
matching with max
cardinality (the most
edges).
Rob
A
Joe
B
Bob
C
Mark
D
Tom
E
Maximum Bipartite Matching


A bipartite graph can
be built with one set
of vertices for
employees and the
other for jobs to be
done.
Then the problem of s
matching employees
to jobs can be solved
by assigning a source
and sink, and
determining the max
flow.
Rob
A
Joe
B
Bob
C
Mark
D
Tom
E
t
Each employee
is able to do only
certain jobs:
1
Rob
Each employee
can do 1 job:
A
1
1
1
1
B
1
1
1
Bob
1
1
Joe
s
Each job can only
be filled by at most
1 employee:
1
C
1
1
t
1
1
1
1
1
Mark
D
1
1
1
Tom
1
E
Network Flow

Consider a network flow problem where
the goal was to figure out how to change
the capacities to increase the maximum
flow in the network.
◦ This might be applicable if you wanted to get
a greater throughput of data in a network and
you wanted to figure out where in the
network you needed to add a router that
could handle more data per unit of time.
Network Flow

How to increase the maximum flow of a network.
◦ Let’s say you restrict your search in your network
(that is already at max flow) to paths that have at most
one edge that is being used to capacity.
◦ For example, s v1  v2  …  vn  t, where each
edge has extra capacity EXCEPT for the edge vi 
vi+1.
What is a simple fix to add extra capacity to one
of the edges that will add maximum flow to the
network?
 How much will this simple fix add?

Network Flow

Take a look at the extra capacity available
through each of the other edges
◦
◦
◦
◦

s → v1, v1 → v2, …, vi-1 → vi, vi+1 → vi+2, …, vn → t.
Take the minimum of these values.
Then, add this much capacity to the edge vi → vi+1.
Adding more than this would do no good, because then
some other edge would become the bottleneck.
This just adds enough capacity so that the original
edge is no more of a bottleneck than the “second
worst” edge in this particular path.
References
Slides adapted from Arup Guha’s Computer
Science II Lecture notes:
http://www.cs.ucf.edu/~dmarino/ucf/cop3503/le
ctures/
 Additional material from the textbook:

Data Structures and Algorithm Analysis in Java (Second
Edition) by Mark Allen Weiss

J Elder’s Network Flow slides,York University