So, its been over an year since I was introduced to traversal in graphs/trees and the basic stuff which comes with it like detecting cycles and stuff.
So let's begin with basic BFS and DFS traversing and see what are the main points which we need to consider.
DFS traversal:
The main thing I remembered whenever someone says DFS is a stack. Now the basic traversal for me goes as follows(btw its same for directed and undirected doesn't matter much):
DFS cycle detection:
Ok so all the resources that one consults about graphs would not fail to emphasize the fact that DFS is used to find cycles in graph. Lets see how can it be done.
Here we would have to distinguish between undirected and directed graphs... why you ask? my reply ... wait for it child....
DFS cycle detection in undirected graphs:
So its so easy to see how if while traversing we see a vertex which we had already encountered we just raise an alarm.
All nice and done. On the top of it we were maintaing a boolean array before.... so we just use it to see if the the vertex being checked isVisited or not and just detect it from there without changing much of our strategy which we used in traversal. All good..... there is more than what meets the eye!
So the problem arises here.... how do you strore the graph... the undirectional graph??
By an adjacency list or matrix which is symmetrical.
So take the graph as shown in the fig1.
You are on 1 ->2->3 . now when you are on three you try to look for outgoing edges of three. On outgoing edge of three connects to 2. Buy hey we had already marked vertex 2 as visited (we mark it as soon as we pop it remember???)
and Voila we have a cycle!!!!
So clearly as you see we failed.
Yes failed.
I failed.
Was trying this question .... just had to detect cycle and couldn't do it. Swept the floor underneath my feet. And from there I did an inrospection whose result is this article.
So where does the problem arise? Clearly our algo while processing the vertex failed to see that this is the same edge which its parent used to push the vertex. In a sense it failed to see that it was the same edge that detected it.
So what do we do?? we maintain an parent.. For each vertex maintain an parent. so while processing if you see that the vertex in the adjacency list is your parent then you take lite.
But the problem still persist. See the figure 2. Basicallly a triangle.
The problem is that it correctly detects a cycle in the first go but detects it again... SO clearly the parent thing is not sufficient (or lets say we need to generalize it). So whats the generalization of an parent?? what is generalization of parent. Ancestor of course...
So how do you detect ancestor... you detect all the vertex that are on your recursion stack. So how do you maintain it??
I don't know....
Atleast for the iterative procedure that i gave you before.. I don't know?
So what do i know ? that if using recursion it can be easily done...
The major thing is that instead of keeping an array isVisited which is boolean, make it hold more than one value(more than just true and false). And then you realize why did that cormen book had all these colors while you were able to do it with just a single boolean thing... after all this time you realize how good the cormen was. Though i think that maintaing colorful array( storing more than one value) is overkill for just traversing.. why you ask ? because you are just listing them and it matters not if it is on recursion stack or not. But we need this fact now , the recursion stack technique. So we have three value or colors (0 == not visited, 1== currently in recursin stack 2 == done with.) ONe might thing that it is also an overkill... well thats the part of growing . I while writing this realized that i might actually be. One better way is that ,
while pushing we are seeing if the vertex was already visited or not. Now the thing is if we add a check while poping the vertex, I think we are done with. Thats all . No need to have all the colors and stuff...
So why does the cormen go this extra mile, of having colors and stuff?
Because he's genious. We need them, this recursion stack in directed graph.
Probably the checking of isVisited at the time of poping is just a workaround??
BTW here is the link to the "problem".
http://codeforces.com/problemset/problem/25/D
So let's begin with basic BFS and DFS traversing and see what are the main points which we need to consider.
DFS traversal:
The main thing I remembered whenever someone says DFS is a stack. Now the basic traversal for me goes as follows(btw its same for directed and undirected doesn't matter much):
- take the root (or starting vertex)
- push onto stack
- while stack is non empty
- pop the topmost
- mark it as visited (you have a isVisited array made of boolean)
- iterate through all the outgoing edges(it may be directed or undirected)
- if that particular one is not visited then push it onto the stack.
DFS cycle detection:
Ok so all the resources that one consults about graphs would not fail to emphasize the fact that DFS is used to find cycles in graph. Lets see how can it be done.
Here we would have to distinguish between undirected and directed graphs... why you ask? my reply ... wait for it child....
DFS cycle detection in undirected graphs:
So its so easy to see how if while traversing we see a vertex which we had already encountered we just raise an alarm.
All nice and done. On the top of it we were maintaing a boolean array before.... so we just use it to see if the the vertex being checked isVisited or not and just detect it from there without changing much of our strategy which we used in traversal. All good..... there is more than what meets the eye!
So the problem arises here.... how do you strore the graph... the undirectional graph??
By an adjacency list or matrix which is symmetrical.
So take the graph as shown in the fig1.
You are on 1 ->2->3 . now when you are on three you try to look for outgoing edges of three. On outgoing edge of three connects to 2. Buy hey we had already marked vertex 2 as visited (we mark it as soon as we pop it remember???)
and Voila we have a cycle!!!!
So clearly as you see we failed.
Yes failed.
I failed.
Was trying this question .... just had to detect cycle and couldn't do it. Swept the floor underneath my feet. And from there I did an inrospection whose result is this article.
So where does the problem arise? Clearly our algo while processing the vertex failed to see that this is the same edge which its parent used to push the vertex. In a sense it failed to see that it was the same edge that detected it.
So what do we do?? we maintain an parent.. For each vertex maintain an parent. so while processing if you see that the vertex in the adjacency list is your parent then you take lite.
But the problem still persist. See the figure 2. Basicallly a triangle.
The problem is that it correctly detects a cycle in the first go but detects it again... SO clearly the parent thing is not sufficient (or lets say we need to generalize it). So whats the generalization of an parent?? what is generalization of parent. Ancestor of course...
So how do you detect ancestor... you detect all the vertex that are on your recursion stack. So how do you maintain it??
I don't know....
Atleast for the iterative procedure that i gave you before.. I don't know?
So what do i know ? that if using recursion it can be easily done...
The major thing is that instead of keeping an array isVisited which is boolean, make it hold more than one value(more than just true and false). And then you realize why did that cormen book had all these colors while you were able to do it with just a single boolean thing... after all this time you realize how good the cormen was. Though i think that maintaing colorful array( storing more than one value) is overkill for just traversing.. why you ask ? because you are just listing them and it matters not if it is on recursion stack or not. But we need this fact now , the recursion stack technique. So we have three value or colors (0 == not visited, 1== currently in recursin stack 2 == done with.) ONe might thing that it is also an overkill... well thats the part of growing . I while writing this realized that i might actually be. One better way is that ,
while pushing we are seeing if the vertex was already visited or not. Now the thing is if we add a check while poping the vertex, I think we are done with. Thats all . No need to have all the colors and stuff...
So why does the cormen go this extra mile, of having colors and stuff?
Because he's genious. We need them, this recursion stack in directed graph.
Probably the checking of isVisited at the time of poping is just a workaround??
BTW here is the link to the "problem".
http://codeforces.com/problemset/problem/25/D