edited by
4,861 views
16 votes
16 votes
Let $T$ be a Depth First Tree of a undirected graph $G$. An array $P$ indexed by the vertices of $G$ is given. $P[V]$ is the parent of vertex $V$, in $T$. Parent of the root is the root itself.

Give a method for finding and printing the cycle formed if the edge $(u,v)$ of $G$ not in $T$ (i.e., $e \in G-T$) is now added to $T$.

Time taken by your method must be proportional to the length of the cycle.

Describe the algorithm in a PASCAL $(C)$ – like language. Assume that the variables have been suitably declared.
edited by

5 Answers

20 votes
20 votes

Here we can run DFS on the DFS tree, to detect whether there is a cycle in O(V+E) time

Since, in a tree of n vertices, we have n-1 edges, adding a cycle will create n edges.

So, there will exist a cycle of exactly n edges when we add one edge to T.

So, now time complexity of DFS becomes, O(V+V)=O(V) which will be proportional to the length of the cycle.

Now, consider an example where we have a graph and DFS produces DFS tree for it.

And DFS tree will be

Now suppose I add a edge between 1-3 in the graph and now my algorithm would be

(1)Initialize all vertices of T to white.

Start with say 1

(2) Mark this vertex 1 with GREY color.

Now, push 1 onto stack and move to it's neighbor in T

(3)If 1's neighbor has color white(undiscovered vertex), push it onto the stack, mark it grey and now discover 2's neighbors.

(4)Then, we move to 5(white), marked grey, pushed onto stack and neighbor of 5 i.e. 4(white) being examined.

(5)4 marked greyed and pushed onto the stack.

(6) No more neighbor for 4, mark 4 as black and pop off 4 from the stack.

(7)Discovered 3 as white neighbor of 5, mark it grey and now examine all neighbor of 3.

(8)Now because of new edge added in T, vertex 1 which is grey is discovered as the neighbour of 3 and 3 is also gray.

Means we have encountered a back edge!!.

Now, return all the vertices which are in the stack and those will be the vertices which make up a cycle.

Time complexity-O(V)

7 votes
7 votes

Union find can take 0(logn) but we need time complexity proportional to cycle length.Lets see how we can do this:-

When we add a edge u-v ,which is in G but not int T,then cycle will come for sure as we have n edges now for n vertices.Now,if we clearly observe then we can see there are two paths now between u and v.

One is earlier path and one is the new path (by adding edge u-v).Now if we can find the earlier path between u and v ,then that path is the cycle and it will be our answer.So how to do that?

We are given with P array which is having parent of each node.Just start from node v and then get its parent say y,now goto to node y in P and get its parent .Do this until we get the node u,and keep on printing all the nodes we access.So,tis is how we get the cycle formed by adding edge u-v in o(m) time,where m is length of cycle.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

See the below example.I added 1-3 edges from G to T.And now start from 3 in P array and traverse till we find 1.

Note that mif the path goes from root,then we can do some modification here in constant time because if you try to get path that goes from root ,then when you reach root its parent is root only and progress cannot be made.

0 votes
0 votes

Here is what I think:

f(v){
    if(P[v] == u){  // Since cycle is gonna form everytime acc. to Q, so didn't define any base case for P[v] != u
        cout<<u;
    }
    else{
        f(P[v]);
        cout<<v;    // Print the path u -> v top down.
    }
}

Running Time = O(cycle length)

Is this correct?

edited by

Related questions

23 votes
23 votes
3 answers
3
Kathleen asked Sep 13, 2014
4,549 views
Assume that the last element of the set is used as partition element in Quicksort. If $n$ distinct elements from the set $\left[1\dots n\right]$ are to be sorted, give an...
9 votes
9 votes
3 answers
4
Kathleen asked Sep 12, 2014
7,300 views
Which of the following problems is not $\text{NP}$-hard?Hamiltonian circuit problemThe $0/1$ Knapsack problemFinding bi-connected components of a graphThe graph coloring ...