You are on page 1of 5

# CS340 - Problem Set 9

Alex Wang
November 17, 2016

Problem A

1.1

Description

First, for connect an edge from each store to any house within a one hour radius. Each
edge will have a capacity of 1. Next, connect edges from a super-source s to each store and
assign each edge to have a capacity of d nk e. Then connect all the houses to a super-sink
t each with an edge capacity of 1. Once the graph is created, run the Ford-Fulkerson
algorithm and check to see if the max-flow is equal to n, the number of houses. If max-flow
does equal n then that means its possible for each parent to get a toy without driving for
more than an hour to the store.

1.2

Pseudocode

Function buildGraph()
Initialize n houses and k stores Connect edges from each store to all houses
within an hour drive. Initiate all of these edges with capacity c(e) = 1
Initialize super-source s. Connect edges from s to each store each with a
capacity c(e) = d nk e
Initialize sink t. Connect edges from each house to the super sink each with a
capacity c(e) = 1

## Function determinePossibility(n houses, k stores)

Let G = buildGraph()
if Ford-Fulkerson max-flow is equal to n then
return True
end
else
return False
end

1.3

Time Analysis

We assume that connecting a single edge takes O(1) time. Connecting each store to all
of the houses within its vicinity, in the worst-case scenario, O(kn) time. We also have to
connect the super-source to all the supply nodes( O(k) ) and each house to the sink ( O(n) ).
And lastly, running the Ford-Fulkerson algorithm will take O(mF ) where m is the number
of edges and F is the maximum flow. In this problem, the number of edges is kn + k + n
and the maximum possible flow is n. So the total runtime for running the Ford-Fulkerson
algorithm is O(kn + k + n) = O(kn2 + kn + n2 ) = O(n2 ). Now the total runtime of building
the graph then running the Ford-Fulkerson algorithm on it is O(kn + n + k + n2 = O(n2 ).

1.4

Proof of Correctness

Proof. We first need to show that if there is a max-flow value of n, then there is a valid
matching between the parents and a store and there cannot be a store with more than
d nk e parents. Since there is a max-flow of n, that means the total capacity of all the edges
coming from the parent nodes going into the sink is n. Since there are n parents total,
this means that capacity of all of these edges must be n/n = 1. Because 1 unit of flow is
coming out of each parent, 1 unit of flow must be going into each parent and each of these
edges is coming from a store which means there is a matching between stores and parents.
Now we need to show that a store cannot be connected to more than d nk e parents. If there
are more than d nk e parents connected to a given store, then that means there are more than
d nk e units of flow going into that store. But we are given that each store has a maximum
of d nk e units flowing into them from the source. So, there cannot be more than d nk e parents
connected to any given store.
Therefore, if there is a max-flow value of n, then there is a valid matching between the
parents and a store.
Proof. Next, we need to show that if there is a valid matching between the parents and a
store, then there is a max-flow value of n. For any valid matching between some store ri
2

and some parent pj , we send one unit of flow between the store and the parent. This unit
of flow ultimately comes from the source node s. Then from the parent to the sink t, we
send one unit of flow. And now we have a single path starting from source s to ri to pj to
t. Because there are n parent nodes, there n such paths going into the sink. Therefore,
the maximum flow is n.

Problem B

2.1

Description

The problem we are trying to solve is to see if we can find k vertex disjoint paths starting
at one of the k nodes and ending at a border node. First take the current graph G and
split each node v such that there is a vin node and a vout node; set the edge capacity
between them to be 1. Connect a source s to all the k nodes kin nodes and set their edge
capacity to be 1. Let u be any other node within vs vicinity. Create an edge (v, u) by
connecting vout and uin and set this edge capacity to 1. If v is a border node, then connect
vout to a sink t and once again set this edge capacity to be 1.
Now that we have built a graph, run the Ford-Fulkerson algorithm on it. If the max-flow is
equal to k, then it is possible to evacuate people from those k locations to the city border.

2.2

Pseudocode

Function buildGraph(G)
Let G0 = splitted version graph G with edge capacity of 1 between vin and vout
Let s and t be super-source and sink respectively
Connect source s to all the kin nodes with edge capacity of 1
Let u be any node within vs vicinity
if v is a border node then
Connect vout to sink t with edge capacity of 1
end
else
Connect vout to uin with edge capacity of 1
end
return G0

Function determinePossibility()
Let G = buildGraph()
if Ford-Fulkerson max-flow is equal to k then
return True
end
else
return False
end

2.3

Time Analysis

We will assume adding an edge or splitting a node takes O(1) time. The total number of
nodes in a graph of m n is (n + 1)(m + 1) so the runtime is O(mn + n + m). All of these
nodes will then all be connected to either another node or the sink but the runtime will
remain O(mn + n + m). Since there are k starting locations, connecting the source to each
one of these will take O(k) time and since there will be k border nodes there are k nodes to
connect to the sink. The total running time of building this graph is O(mn + n + m + 2k)
And finally, running the Ford-Fulkerson algorithm will take O(EF ) where E = m(n + 1) +
n(m + 1) and F = k. Therefore, the total running time is O(EF ) + O(mn + n + m + 2k) =
O([mn + n + m]k + mn + n + m + k) = O([mn + n + m]k)

2.4

Proof of Correctness

Proof. We need to show that if the max-flow = k then there are k vertex disjoint paths.
There are k units of flow leaving the source and that is being distributed among k vertices
so each edge has a capacity of 1. And because we added an edge with capacity of 1 between
vin and vout , that vertex cannot be visited again. From this point onward, every edge added
will have a capacity of 1 so there is no possibility of any vertex being visited more than
once. The result is there are k vertex-disjoint paths because the edge capacities throughout
the path are all equal to 1.
Proof. Now we need to show that if there are k vertex disjoint paths then the max-flow is
k. A vertex disjoint path is a path where they do not share any nodes except the last one.
We know that there are k paths going into the sink and throughout each path all the edge
capacities are equal to 1. Therefore, there must be k units of flow leaving the source and
the max-flow is k. If the max-flow does not equal to k, then that means that units of flow
were either lost or created while going from the source to the sink.

Collaboration
Daniel Washburn, Kevin Liao, Erin Lipman, Audrey Lin