0% found this document useful (0 votes)
418 views9 pages

CS330 Homework 6

This document contains the solutions to three algorithm questions. Question 1 asks for an algorithm to determine if a floor plan is ergonomic in polynomial time. The solution constructs a flow network and uses the Ford-Fulkerson algorithm to check if maximum flow equals the number of lights. Question 2 asks how to find maximum flow in a graph after reducing an edge's capacity by 1, in O(m+n) time. The solution runs one iteration of Ford-Fulkerson on the residual graph if the edge was at capacity. Question 3 asks how to match website visitors to ads to satisfy advertiser contracts. The solution constructs a flow network and checks if maximum flow equals the sum of advertiser requirements.

Uploaded by

ADITYA TRIPATHI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
418 views9 pages

CS330 Homework 6

This document contains the solutions to three algorithm questions. Question 1 asks for an algorithm to determine if a floor plan is ergonomic in polynomial time. The solution constructs a flow network and uses the Ford-Fulkerson algorithm to check if maximum flow equals the number of lights. Question 2 asks how to find maximum flow in a graph after reducing an edge's capacity by 1, in O(m+n) time. The solution runs one iteration of Ford-Fulkerson on the residual graph if the edge was at capacity. Question 3 asks how to match website visitors to ads to satisfy advertiser contracts. The solution constructs a flow network and checks if maximum flow equals the sum of advertiser requirements.

Uploaded by

ADITYA TRIPATHI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CS330 Homework 6

Name: Yichen Anthea Li


U-ID: 56620966

Question 1 Chapter 7, Exercise 6.


Give an algorithm to decide if a given floor plan is ergonomic. The running time should be
polynomial in m and n. You may assume that you have a subroutine with O(1) running time that
takes two line segments as input and decides whether or not they cross in the plane.

Graph: We can use maximal matching to solve the ergonomic problem. To build the
following graph that condenses the floor plan:
- Generate nodes ui ∈ U to represent the light switches and nodes wi ∈ W to represent the
light fixture.
- Connect (ui, wi) with an edge if the line drawn from ui to wi is not
intersected by any walls in the floor plan. Each edge has capacity of 1.
- Create 2 nodes starting node s and destination nodes. Connect s to all the
light switch nodes ui and all of the fixture nodes wi to t. Assign capacity of
1 to all these edges.

Run Algorithm: Run Ford-Fulkerson on the graph constructed as above, if the max flow
returned equals to n, then the floor plan is ergonomic, otherwise, return false.

Claim: If we run the Ford-Fulkerson algorithm on the graph constructed as above.


There is a possible ergonomic floorplan iff the max flow through the graph equals n.
If the max flow equals n, then there is a total value of n flowing out to t,
because of the edge capacity of 1, there must be exactly n of these edges are equal
to 1 so it allows the integral amount n to flow across the system. Thus, there must n
matching ergonomic light fixture and switches in the floor plan. Conversely, if there
are n lights and fixtures and switches that are ergonomic in the floor plan, then
these edges would all be connected, forming a maximum total capacity of n in the
system allowing through maximum flow of n.

Runtime analysis:
O(n3). To generate the graph, it takes O(n2) to fill in all the edges between lights
and switches, and generating the nodes has basic operation of O(1) for all the lights
and switches. Running the Ford-Fulkerson takes O(mC). Our setup of the graph allows
the maximum capacity of n, and there are, in the worst case, n2 edges between the
switch nodes and fixture nodes. Running the Ford-Fulkerson would takes O(n2) * O(n) =
O(n3), in the worst case scenario. The Algorithm, thus, takes O(n2) + O(n3) = O(n3).
Proof of correctness:
Proof by contradiction: Assume given a floor plan that is ergonomic and our return
value from the algorithm is false. Since, each switch send out only one unit of flow to the
fixture it is wired/connected to, it must be that the max flow is less than n (the capacity of of the
graph ≤ n) which means the graph is not saturated. It must be that some fixtures and switches
are not connected, because the fixtures and switches are not ergonomic to each other. The
algorithm will not return True unless all the fixture and switches are connected. Thus, there is a
contradiction in our assumption. Our construction of graph and evaluation of the return results
solves the problem correctly.

Question 2 Chapter 7, question 10.


Suppose you are given a directed graph G=(V,E),with a positive integer capacity ce on each
edge e, a source s ∈ V, and a sink t ∈ V. You are also given a maximum s-t
flow in G, defined by a flow value fe on each edge e. The flow f is acyclic: There is no
cycle in G on which all edges carry positive flow. The flow f is also integer-valued.
Now suppose we pick a specific edge e∗ ∈ E and reduce its capacity by 1 unit.
Show how to find a maximum flow in the resulting capacitated graph in time
O(m + n), where m is the number of edges in G and n is the number of nodes.

Algorithm:
Input: graph with a specific edge e with capacity deducted by 1
Output: max flow

If capacity of e* is not saturated under the maxflow


Return original max flow ( max flow does not change )
Else // the edge e* is saturated that f(e*) = c(e*), run one iteration of
Ford-Fulkerson on the residual graph
- Reduce the flow by 1, which creates imbalances on the two ends of the
edge u and v.
- Find path from t to v and path from u to s and decrease the flow value
by 1 on all edges composing these two paths in the residual graph
- Find augmenting path pa with flow up to 1 from s to t
If there is no such path
Return (original max flow - 1)
- Else
Increase flow value of pa by 1
Return (original max flow)
Proof of correctness:
When the capacity of e* is not saturated f(e*) < c(e*), decreasing the capacity by 1
does not change the max flow, because the flow value on this edge is not changed and
the flow graph stay the same. When the capacity of e* is saturated, there are two
cases that could happen. First is that the one unit of flow can be redirected
somewhere else going through a different path. In this case, we run one iteration of
Ford-Fulkerson on the residual graph to find such an augmenting path that has flow of
1. Then we redirect the exceeding 1 unit flow by increasing the flow value of this
path by 1. And the maximum flow of the new graph would stay the same as the original
graph. The second case is when the 1 unit cannot be rerouted. Then, the max flow does
not change. In this case, Ford-Fulkerson augmentation would not be able to find such
an augmenting path and thus the max flow of the new graph would be the Original max
flow - 1.

Running time:
O(m+n) First, to identify whether e* is saturated we traverse through the max flow
path which takes O(m). The worst case is that we found out that e* is saturated, we
first look for a path from t to v and u from u to s, which is achievable if we
traverse the graph, and it takes O(m+n). Then, to finding an augmenting path to
redirect the 1 unit flow, we run one iteration of Ford-Fulkerson alg on the residual
graph, which is O(m+n). Thus, the total running time of our algorithm is O(m+n)

Question 3 Chapter 7, Problem 16 on p. 312.


We know that user j (for j = 1, 2, . . . , n) belongs to a subset Uj ⊆ {G1, . . . ,
Gk} of the demographic groups. The problem: Is there a way to show a single
ad to each user so that the site’s contracts with each of the m advertisers
is satisfied for the minute? (That is, for each i=1,...,m, can at least ri of the
n users). Give an efficient algorithm to decide if this is possible, and if so,
to actually choose an ad to show each user.

Input: a set of site visitors and a set of advertisers


Output: whether possible, if yes, then match ads with visitor

Graph: Generate a graph G = (V, E) to represent a flow network to match the


site visitors to advertisers:
- A set of vertices v1, … , vn for each site visitor, and a set of
advertisers w1, … , wm.
- Starting node s that connected by esum of ges of capacity 1 pointing at
each site visitor nodes and destination node t connected by edges of
capacity ri coming from each of the advertiser nodes.
- If a visitor vi is in the demographic group in the contract with
advertiser wj, add an edge of capacity 1 from vi to wj for each of the
advertiser compatible with vi.
Run Algorithm: Run Ford-Fulkerson on the above graph, If (max flow == ∑ri), return the
graph, else, return “cannot be achieved”
Claim: there is a way to meet all advertising contracts at the same time if and only if
(max flow = ∑ri). Because the flow on all the edges from the site visitor to advertisers have only
maximum capacity of 1, for the maximum flow through the graph system to be ∑ri , there must
be exactly ∑ri edges with flow value of 1 between the two groups. For the system to have ∑ri
amount of flow, there has to be ∑ri .number of edges going from the source node S to the site
visitors, which means there has to be ∑ri number of visitors, so the flow out of the source node
would saturate the flow to the destination node t. Thus, there must exist a way to match all the
visitors to advertisers’ contract goal. Conversely, if all contracts are achievable, there must be
∑ri edges constructed between the users to the matching demographic groups to the
advertisers such that each advertiser have ri edges coming in. The flow from the source node
would saturate the flow from advertisers to destination node t. So,(max flow = ∑ri).

Running time:
The algorithm has two major parts, building the graph and running Ford-Fulkerson
- Building the graph would take O(m*n*k), generating nodes for the the
advertisers and the site visitors takes O(m) + O(n) times with base operation
of O(1). To fill in the edges, we need to traverse through all the users, then
for each user, we need to traverse through all the demographic groups, and
then traverse through all the advertisers to see if their contract includes
the current demographic group. Thus it take O(n)*O(k)*O(m) = O(nkm)
- Running Ford Fulkerson takes O(mC), which in the worst case of our graph would
be O(m*n) to go through all the edges. The maximum capacity coming out from
the source node s would be n, going to n number of site visitors, which would
be O(m*n)*O(n) = O(n2m)
Thus, if we assume that there are less demographic groups than there site visitors,
the running time of our algorithm comes down to O(n2m). Without such assumption, the
aggregate running time of our algorithm would be O(nmk) + O(n2m).

Proof of correctness:
Proof by contradiction: Assume that it is possible to arrange ads so that all the contracts are
satisfied and our algorithm returns false on the graph constructed as specified. This means that
the max flow returned by the Ford-Fulkerson is less than ∑ri . Because all the edges between
visitor and advertiser have flow value of 1, it must be that there are less than ∑ri number of
edges with the flow value 1 between the visitor and advertisers. This can only happen if the
number of site visitors does not match the ri for each of the advertisers. Thus, there is a
contradiction in our assumption. Conversely, if (max flow = ∑ri), there must be ∑ri constructed
between the advertisers and users. Therefore there is a flow value ∑ri which is the max flow
because it saturates the max capacity.

Question 4. Chapter 8, Exercise 4


Consider the following list of problems, and for each problem either give a polynomial
time algorithm or prove that the problem is NP- complete.
(a) The general Resource Reservation Problem defined above.

Definition of Resource Reservation Problem: given a set of processes and


resources, and number k, is it possible to allocate the resources so that at least k
processes do not fight for resource it requests with each other. The Resource
Reservation Problem is at least as hard as the independent set problem, Independent
set ≤p Resource Reservation

Consider a graph G = (V, E) where the vertices corresponds to the processes while the
edges corresponds to the resources. The requested resourced by a process would the be
edges connected to that process node. If two nodes are requesting the same resource,
then the edge would connect with two requesting process nodes. To solve the Resource
Reservation problem, we are just simply finding a independent set size of k. If there
is such a set, the k nodes in the set would give us processes that do not fight for
resources with each other.

Conversely, if we have a set of nodes that can be active at the same time.
Then there shouldn’t be any resource edge connecting these nodes. For any of the two
nodes in this set, if they are joint by an edge, then they are fighting for the same
resources, so they cannot be active at the same time and the set is not an
independent set for these two nodes connected by an edge.

Because we know that the independent set problem is NP-complete, we can say
that the Resource Reservation is also NP-complete.

(b) The special case of the problem when k = 2.

There is a polynomial time solution to the problem when k = 2


Algorithm:
Input: set of resources and set of processes
Output: yes or no ( whether or not there are 2 coexisting processes )

Generate a graph with the vertices corresponding to processes and edges


connecting the vertices that request the same resource.
For v = 1 to |v|
For u = i to |v|
If (v, u) ∉ E
Return “yes there are two non-conflicting resources”
Return False

Proof of running time:


O(n2) generating the graph takes O(n2) of basic O(1) operations, because we are
generating 1 node for each process, which takes O(n) and then we are traversing all
nodes to add resource edges to the process nodes. The worst case scenario takes O(m)
* O(n) = O(mn) To traverse through all pairs of processes, it takes O(n)*O(n) with
base operation of O(1), which gives O(n2) running time. Because the resources are
shared and assuming there is no idle resources, there are at least as many processes
as there are resources O(mn) < O(n2), and thus, the total running time of our
algorithm is O(mn) + O(n2) = O(n2).

Proof of correctness:
Proof by contradiction: As proved above any independent set of the graph would
represent the non conflicting set of processes. Suppose that we returned yes on a
graph that does not any any pair of non-conflicting nodes. Our algorithm traverses
all pairs of vertices and if they are independent of each other then we return these
two as the coexisting processes. Because these two processes have no edge connecting
the two, then it must be that they are not requesting the same resources, which is a
conflict with our assumption, thus the algorithm does find the non-conflicting pair
if there is one.

(c) The special case of the problem when there are two types of resources—say, people and
equipment—and each process requires at most one resource of each type.

In this problem, we redefine the graph so that the nodes are the resources for each
person and equipment, and the processes are the edges jointing the person nodes and
the equipment nodes. Then we are looking for a set of processes with non-conflicting
resources requirements, which means that the set of requests must not have the same
human and machine nodes as end point, which is then a bipartite matching.

Algorithm:
Input: set of human and machine resources and the process requests
Output: maximum bipartite matching

Graph:
- Generate nodes ui for all the people that the process can request and nodes wi
for all the machines resources
- Use edges of capacity 1 to represent processes to connect specific human to
machine resources that the same process request.
- Generate source node s and connect it to all the human resources with edges of
capacity 1 and destination node t and connect all of them from machine nodes
to t with edges of capacity 1.

Run Algorithm: Run Ford-Fulkerson on the above graph, and if max flow (f) ≥ k, return
True, else return False. This means that there are f number of maximum flow edges,
which are also the maximum bipartite matching for the graph. In our setup, these are
also the processes that can be satisfied at the same time.

Claim: Requests can be satisfied at the same time if and only if they are in the
maximum bipartite matching of the graph
If the max flow = f, then there are maximum of n edges in the bipartite
matching between the people and machine nodes. Because all edges in the graph have
capacity 1, only 1 unit of flow is being let into each human nodes. Thus, each
machine node would only have one valid edge coming in from only one of the human
nodes because only 1 unit of flow is being let out. Thus, the f edges are maximum
bipartite matching of the graph, which have different human and machine nodes as
endpoints. They are the maximum non-conflicting processes. Conversely, if there are n
numbers of non-conflicting processes, it means that there would be n number of
bipartite-matched edges from human nodes to the machine nodes. Our running of Ford-
Fulkerson would pick the maximum flow edges, which equivalents the maximum bipartite
matching in our setup of the graph.

Proof of running time:


O(mn) To set up the graph, it takes O(m) to set up all the resource nodes and O(n) to
fill in all the edges and edge capacity. Thus, generating the graph takes O(m) + O(n)
running time. Ford Fulkerson runs in O(mC), for m being the number of edges. In our
set up of the graph, the maximum capacity would be the number of human source nodes,
which is O(m) in the worst case, Thus, Running time of Ford-Fulkerson would take
O(m)*O(n). The total running time of the algorithm would be O(m) + O(n) + O(mn) =
O(mn)

Proof of correctness:
First, we need to prove that Ford-Fulkerson would give us maximum bipartite matching:
Proof by contradiction: Assume that the maximum flow returned by Ford-Fulkerson is
not a Bipartite matching given our graph construction. Because all edges in the graph
have capacity 1, and only 1 unit of flow is being let out of each machine node, each
machine node would only have one valid edge coming in from only one of the human
nodes because only 1 unit of flow is being let out. Thus, the all max flow edges
between human nodes and machine nodes must have distinct ends. Thus, we found
contradiction in our assumption, and Ford-Fulkerson would find maximum bipartite
matching in the graph.

Then, we need to prove that bipartite matching solves this case of the problem.
Proof by contradiction: Assume that our algorithm returns n number of max flow edges
and there are less than or more than n number of non-conflicting processes existing
in the system. If the maxflow = n, there must be maximum of n edges in the bipartite
matching between the people and machine nodes, which means there are maximum number
of n edges with different human and machine nodes as endpoints. Thus, there must be
maximum number of n non-conflicting processes, which forms a conflict with our
assumption. Conversely, if there are n numbers of non-conflicting processes, it means
that there would be n number of edges with flow value of 1 with different end points.
Our running of Ford-Fulkerson find the maximum amount of these edge. Thus, algorithm
is correct.

(d) The special case of the problem when each resource is requested by at most two processes.

This special case is still NP-complete, Resource Reservation ≤p Special case


Resource Reservation.

Consider a case of the Resource Reservation problem, where each resource edge
now connect with up to 2 process nodes. If two nodes are requesting the same
resource, then the edge would connect the two requesting process nodes. To solve this
special case, we are finding a solution to the a general case resource reservation
problem by looking for an independent set size of k in the graph. Thus the special
case is a reduction of the Resource Reservation problem as we proved in part (a).
Thus, this special case is still an NP-Complete problem.
Question 5. Chapter 8. Exercise 10.
Given G, the paths {Pi}, and a number k, is it possible to place advertisements on at most k of
the nodes in G, so that each path Pi includes at least one node containing an advertisement, with
input G, {Pi : i = 1, . . . , t}, and k?

(a) Prove that Strategic Advertising is NP-complete.

Checking whether at least one of k nodes lies on path Pi takes polynomial time O(kn),
and thus, we can check the entire graph G { pi: i = 1… t } whether the given set of k
nodes is a valid set for it with polynomial time O(t*kn).

Strategic Advertising is NP-complete because it is polynomial time reducible to vertex cover,


which is also Vertex Cover ≤p Strategic Advertising. Given G = (V, E) and k, we
arbitrarily define a directed graph from G and paths {Pi} for all edges of E’. The
conversion from a normal graph to an arbitrary directed graph and assigning paths
takes polynomial time O(m). To find out whether the graph G; has a valid set of at
most k ads, we need to look for the subset size at most k that has at least one end
of each edge connected with the set to make sure that the set of ads covers all paths
in {Pi}. Thus, G’ would only contain valid ads if and only if there is a vertex cover
size of less than or equal to k in G. If G’ does have a valid set of ads, because the
set would have at least one end of all the edges attached, it is a vertex cover for
G. Conversely, if G has a vertex cover of size at most k; then the set would connect
with all path in {pi} and thus the cover set is a valid set of advertisement.
(b) Your friends wrote a pretty fast algorithm S that produces yes/no answers to arbitrary
instances of the Strategic Advertising Problem. Using the algorithm S as a black box, design an
algorithm that takes input G, {Pi}, and k as in part (a), and does one of the following two things:
– Outputs a set of at most k nodes in G so that each path Pi includes at least one of these nodes,
or
– Outputs (correctly) that no such set of at most k nodes exists. Your algorithm should use at
most a polynomial number of steps, together with at most a polynomial number of calls to the
algorithm S.

Algorithm:
Inputs: G of {Pi} and k
Outputs: a set of at most k nodes
ALG(G, k) {
Initialize set T to maintain the set ready to be returned
If (k==1) {
For each nodes v
Check if it lies on all paths,
if yes return {v}, if not, return “no”
}
Else { // k > 1
If (S(G, k) == True)
For each nodes v {
G’ ← Delete v and all paths through v from G,
Input into S so see if the new input have a valid set of
ads of size at most k-1.
Because S would return true on all vertices in a valid
set, add qualifying node to U
}
Else
Return “no”
}
Return U;
}

Proof of Correctness:
Proof by induction:
Base case: k = 1, If all paths cross at one node, if yes, return such node, if not,
return “no such node”.
Inductive case: k > 1, we input the Graph into Algorithm S, if S returns “no” we
return no such set of nodes” as well, if the Alg S returns “yes”, we traverse all
nodes v of {V}, delete v and all paths through it and input the new Graph into S
along with k-1. If S returns “yes”, then we add this node into the set of nodes U
that we maintain to be returned by our algorithm. whether, on this new input, there
is a valid set of ads of size at most k-1. There will be at least one node v that S
will return “yes” on namingly this node would be the node that all paths go through,
because we have already tested if such a node exist with S(G, k) test. Because U-{v}
is a valid set of at most k-1 advertisements on the new input, S will return yes on
any v of U. After a valid node has been identified, the problem is reduced to a set
of at most k-1 advertisements. By induction, when our algorithm finished, we have a
complete set of nodes T of valid advertisement that is size of at most k.

Proof of Running time:


O(n2+nt), We first check when k==1, if there is a path that lies on all path by
traversing through the path on each node which takes O(nt). When k > 1, for each
fixed k, we call alg S O(n+t) times for all the nodes and paths, Thus, the total
runtime of the algorithm would come down to be O(n) * O(n+t) + O(nt) = O(n2+nt).

You might also like