Solutions

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Solutions

Question 1:

Question 2:
Create a new adjacency-list Adj of size |V| and an empty matrix M of size |V|^2 first.

For each vertex u in the multigraph G, we iterably examine each vertex v of Adj[u].
•If M(u, v) == ∅, mark M(u, v) true, then add v to Adj[u]
•If M(u, v) == true, do nothing.
Since we lookup in the adjacency-list Adj for |V + E| times, the time complexity is O(V + E

EQUIVALENT-UNDIRECTED-GRAPH
let Adj'[1..|V|] be a new adjacency-list
let M be |V| × |V|
for each vertex u ∈ G.V
for each v ∈ Adj[u]
if M[u, v] == Ø && u != v
M[u, v] = true
INSERT(Adj'[u], v)
Question 3:
Proof by contradiction:

Let T and T’ be two minimum spanning trees which differ by at least one edge.

Let this edge be (u, v). Therefore, edge (u, v) is an element of T, but not an element of T’

Let (u, v) cross some arbitrary cut in G.

Since (u, v) is not an element of T’, there exists another element of T’, mainly (x, y) which
crosses some cut in G.

Now suppose either (u, v) or (x, y) is the light edge crossing the cut.

Assumption: Let (u, v) be the light edge.

Since (u, v) is not an element of T’, there exists some path in T’ from u to v. This path must
cross the cut somewhere at (x, y).

Since (u, v) is a unique minimum edge, weight (x, y) > (u, v)

Therefore, cut (x, y) from T’ and add (u, v) to reduce the weight of the tree.

Therefore T’ was not the minimum spanning tree.

Proof by counterexample for the converse:


T = {u, v, w, x}
A = {(u, v, 1), (v, w, 1), (w, x, 1)}
B = {u, x}
There exist two light edges crossing cut (B, T-B): (u, v) and (w, x)

However, there can exist only one minimum spanning tree.


Question 4:
Approach: The idea is to use Bellman-Ford Algorithm which is used to detect a
negative cycle or not. To print the negative cycles, perform the Nth iteration of
Bellman-Ford and pick a vertex from any edge which is relaxed in this iteration.
Using this vertex and its ancestors, the negative cycle can be printed. Below are the
steps:
• Perform N-1 iterations of Bellman-Ford algorithm and relax each edge (u,
v). Keep track of parent of each vertex and store in an array parent[].
• Now, do one more iteration and if no edge relaxation take place in
this Nth iteration, then there is no cycle of negative weight exists in the
graph.
• Otherwise take a variable C and store the vertex v from any edge (u, v),
which is relaxed in the Nth iteration.
• Now, starting from the C vertex go towards its ancestors until a cycle is
found and finally print it.
• This cycle will be the desired cycle of negative weight.

You might also like