edited by
10,224 views
23 23 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.

6 Answers

26 26 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)

1 flag:
✌ Edit necessary (Anurag Prasad “Incorrect solution as the question requires O(cycle length) solution but the given solution is O(V).”)
9 9 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.

1 1 vote

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
0 0 votes
The answers here are not considering that we only have a parent array for our DFS tree. We cannot perform a dfs lookup from u and reach node v that easily. We would have to build a graph from the dfs tree. Which is not optimal.

We can just find the Least common Ancestor of u and v, following the parent pointers.
The edges involved will also be the starting elements of the LCA arrays.
Position:
Show:

Related questions

74 74 votes
9 answers 9 answers
39.3k
39.3k views
Kathleen asked Sep 13, 2014
39,348 views
The access times of the main memory and the Cache memory, in a computer system, are $500$ n sec and $50$ nsec, respectively. It is estimated that $80\%$ of the main memor...
16 16 votes
2 answers 2 answers
4.6k
4.6k views
go_editor asked Apr 24, 2016
4,570 views
Consider the function $F(n)$ for which the pseudocode is given below :Function F(n) begin F1 ← 1 if(n=1) then F ← 3 else For i = 1 to n do begin C ← 0 For j = 1 to n – 1 ...
21 21 votes
4 answers 4 answers
6.6k
6.6k views
Kathleen asked Sep 13, 2014
6,585 views
Consider the function $F(n)$ for which the pseudocode is given below :Function F(n) begin F1 ← 1 if(n=1) then F ← 3 else For i = 1 to n do begin C ← 0 For j = 1 to n – 1 ...
32 32 votes
3 answers 3 answers
7.6k
7.6k views
Kathleen asked Sep 13, 2014
7,603 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...