Вы находитесь на странице: 1из 7

Homework 5

Raj Korpally(rk2277), Rithvik Gambhir(rg2974)


Problem 1:
Part a:
Idea
The problem can be solved by using the Dijkstra's algorithm by finding the shortest distances
from each of the vertices taking the vertex v as the source, where (u, v) is the negative weighted
edge in the input graph. A negative cycle is detected if sum of the distance from the vertex v to
vertex u and the negative weight is negative.
Pseudo code
runDijstra()
1. for every vertex v in the graph
2.
dist[v] = 0 and H[v] = 0
3. dist[source] = 0 and H[source] = -1
4. update[source]
5. for i = 1 to n-1
6.
u = vertex from H with the smallest distance (H is set to -1
after it is "extracted")
7.
update(u)
8. return distance[destination]
update(u)
1. for every neighbor v of update
2.
if dist[v] > dist[u] + weight(u, v)
3.
dist[v] = dist[u] + weight(u, v)
4.
if v is not set to 1 in H
5.
H[v] = 1
Running Time Estimate
Dijkstra() and update(u)
As discussed in class, the running time of Dijkstra and update, if H is an
array, is O(n^2). If H were a heap, the update method takes O(deg(u).log(n))
time and Dijkstra takes an overall of O( m.log(n) ). All the steps in the
findNegativeCycles method take only constant number of steps. Therefore, the
algorithm has a running time of O(n^2).
Proof of correctness
The proof of correctness for both Dijkstra and update follow form what was
discussed in class. Although Dijkstra's algorithm does not give the correct
result for negative weighted edges, we are guaranteed that the input graph
contains only one such edge. So the algorithm can be run taking taking the
vertex v as the source, where (u, v) is the negative weighted edge in the
input graph. The only modification is the return value of the runDijkstra method
which is just the distance of u from the source vertex v and not the complete

array of distances as discussed in class.

Part b:
1. First we remove one negative edge are run the Part A algorithm to check for negative cycle.
2. Return true if negative cycle exists.
3. Now Remove the other negative weight edge and run the Part A algorithm to check for negative
cycle.
4. Return true if negative cycle exists.
5. If no negative cycle is detected following steps 1 to 3 then find the shortest path between the two
vertices's of one negative weight edge but including the other negative weight edge in the graph.
6. Add the negative weight of edge to the shortest distance achieved. If negative then negative cycle
exists else no negative cycle.
7. Repeat step 5,6 selecting the other to negative vertices's this time but including the others negative
edge in the graph. If negative cycle is detected output true.
Running Time:
O(n^2). As it is running the part A algorithm at most 4 times.
Proof of Correctness:
First we check whether negative cycle can exist independent of the other negative weighted edge. If it
can exist we output true. Then we check if negative cycle can exist including both the negative edges.
When we check this part, the chance of forming negative cycle without the support of other edge is
eliminated and we can run our algorithm smoothly even if there is negative edge in the graph. And we
add the negative weight of the edge we choose to shortest distance to see if the overall is negative. If
its negative then negative cycle exists.

Problem 2:
Algorithm:
The problem is solved by using the application of Ford-Fulkerson Maximum Network Flow Algorithm
extending it to Bipartite Matching problem and modifying it to achieve our goal.
1. Construct 3 directed graphs with edges directed from the set of children to their preferred clothing in
the respective graph.
2. Add vertices's 's' and 't' to the 3 graphs separately.
3. Connect 's' to each child from the set of children with a directed graph from s to set of children(n) in
each of 3 graphs. And connect all clothing objects in each of 3 graphs to corresponding 't' directed from
clothing to 't'.
4. Assign all connected edge weights to 1 in all three graphs.
5.Using Ford-Fulkerson Algorithm we find the Maximum flow in each of 3 graphs from 's' to 't'
separately.

6. If the the Maximum network flow 's' to 't 'achieved is equal to 'n' in each of the graph 3 graphs. Then
we can satisfy the condition that each child is properly clothed.
7. We look at the flow values of connected edges in the graph to get which child gets what clothing.
8. If the max network flow is less than 'n' in any of the three graphs we cannot satisfy the children.
Correctness:
The correctness of Bipartite Matching problem using Network Flow was proved in class. This
algorithm is the case of using Bipartite Matching Algorithm on 3 different sets of clothing with same
set of children. So, it is basically applying Bipartite Algorithm thrice. This algorithm works because
each bipartite problem solves the problem of matching all children with particular type of clothing. So
if the flow value is 'n' it implies that all children can be satisfied for that particular type of clothing.
In order to satisfy the children to get completely dressed it becomes necessary that each of separate
clothing is matched properly with the child. If the child can be satisfied in all 3-types of clothing then
only he can be completely clothed.
Running Time:
From class slides we know that running time of Ford-Fulkerson is O(n+m).F , where F is the maximum
flow value of the network and m,n are edges and vertices's of graph respectively.
We know that maximum number of edges in a Bipartite graph can be (n.m) edges.
Taking n as number of children, a, b, c as 3 types of clothing.
Total edges in each of three graphs will be (a.n ), (b.n), (c.n) respectively.
And number of vertices in each of 3 graphs will be order of (a+n), (b+n), (c+n).
So, edges+vertices(n+m) in three graphs will be O(a.n), O(b.n), O(c.n).
And as the Maximum Flow is bounded by 'n'. So, value of 'F' will be 'n'
The running time of modified Algorithm will be
O(a.n.n), O(b.n.n), O(c.n.n) for 3 graphs seperately.
This can be combined to have total running algorithm for finding the maximum flow in three graphs as
O(a.n^2) + O(b.n^2) + O(c.n^2) = O((n^2).Max(a, b,c)).
Other steps in the algorithm, that is adding vertices,edges, finding matches will be of lower order.
Total Running time is O((n^2).Max(a, b,c)).

Problem 4:
Problem P = Hamiltonian Path
Problem Q1 = Longest Path
Part a:
Aim: To reduce Hamiltonian Path Problem(P) to Longest Path Problem(Q1)
Given: G, s, t
Construct: G1 , s1, t1 pass as input to AQ1 to get output generated by AQ1
Constructed Inputs:
G1 = G
s1 = s
t1= t
Now, take input (G1, s1, t1) in send it into AQ1
We get output ' L' which is the output of Longest path problem when we pass (G1,s1,t1) to AQ1.
The output is the longest path between s1 and t1 in graph G1.
How can we say that Graph G has Hamiltonian Path looking at output 'L'?
We look at length of 'L', If the 'Length of L' is (|V| - 1), where |V| is the total number of vertices's of
input graph. We can confirm that that G has Hamiltonian Cycle.
Reason Why?
Definition of Hamiltonian path says that the path should cover all vertices's in the graph passing though
each vertex exactly once while traversing from s to t . When we pass our graph as input to longest path
problem it outputs the longest path between s and t where each vertex in the graph is traversed at-most
once. There are total 'V' vertices's in input graph. If the output length is (|V|-1) implies that all the
vertices's between s and t are traversed exactly once in the process of reaching s to t. This implies the
path we got is Hamiltonian path(from defn. of Hamiltonian path), we have solved Hamiltonian path
using Longest path problem. Also, the reduction time is polynomial because we are using the same
Graph and vertices's input. And just checking the value of output to match our goal.

Part b:
Problem P = Hamiltonian Path
Problem Q2 = Shortest Path with negative weights (zero, negative or positive weigths)
Aim: To reduce Hamiltonian Path Problem(P) to Shortest Path Problem(Q2)
Given: G, s, t
Construct: G2 , s2, t2, modify edge weights, pass as input to AQ2 to get output generated by AQ2
Constructed Inputs:
G2=G
s2=s
t2=t
Important: change edge weights
Change all edge weights of G2 to some negative constant value: K
So, each edge weight in Graph G2 will have Edge weight = K (negative constant)
Now, We take input (G2,s2,t2), (with modified edge weights) and send it into AQ2
We get output ' L', which is the output of Shortest path with negative weights when we pass (G2,s2,t2)
to AQ2.
The output is the shortest path with negative weights between s2 and t2 in graph G2.
How can we say that Graph G has Hamiltonian Path looking at output 'L'?
We look at length of 'L', If the 'Length of L' is ((|V| - 1)*K), where |V| is the total number of vertices's
of input graph and 'K' is each edge weight which is a negative constant We can confirm that that G has
Hamiltonian Cycle.
Reason Why?
Definition of Hamiltonian path says that the path should cover all vertices's in the graph passing though
each vertex exactly once while traversing from s to t. When we pass our inputs to the shortest path
with negative weights problem AQ2, it tries to get us the shortest path with negative edges. While
trying to finding the shortest path, the algorithm tries to get as negative as possible (Follows longest
path). As the definition of shortest path with negative weights allows us to traverse each vertex at most
once the maximum edges it can traverse is (|V|-1) so as to get as negative as possible while traversing
between s to t. If all vertices's are touched exactly once while traversing from s to t the maximum the
length it can achieve is ((|V| - 1)*K). If we get this value as length 'L', it implies all vertices's are
touched exactly once while traversing from s to t. This implies that our graph contains Hamiltonian
Path( from defn. of Hamiltonian path). So, we have solved the problem of Hamiltonian Path using
Shortest path with negative weights(AQ2). Also the reduction time is polynomial as its just changing
edge weights and looking at the value of output 'L' to match our goal.

Вам также может понравиться