Thursday, 19 June 2014

BFS, DFS, cycles, traversal and stuff.....

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):
  • 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.
So cool it would give you a nice traversal.

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

Wednesday, 18 June 2014

Some motivational things for a change!

So, with almost certainty that no other human (on his own accord without me guiding him) is going to read this stuff, so i thought that i would share something about something which is more important than coding.

Failure

Yup as it happened, it took a second to realize that you have failed .... and everything else kind of automatically falls apart... on its own accord. Currently i am feeling depressed, dejected and alone. With no- one to talk to (i tried talking to few but with no help) i am talking this medium to talk to future mukul. Giving him what i currently have the least -motivation- and hoping that writing about motivation would somwhat inspire me too. God this article is starting to get boring although it has just started.

So couople of days back.... actually i guess it was triplet of couple or somewhat close i read this quote...
"
 You tried? You failed? You failed bad?
Doesn't matter....
Try again, fail again , fail better!
"

Hmm i failed... and that too on various aspects
1. Feeling lonely... no one to talk to much.. in short failed as a friend?
2. Have no one to look forward to (if you know what i mean).... failed again...
3. It has been two years since i did graphs, trees , dfs and bfs ... turns that i had a major conceptual block.... How and where to find cycles in a graph or a tree....

Now the realization of the third point although came through a failed attempt of a question, it raised a question mark on concepts... at a time which was very critical.

So i am itchy to get started and try again.... on the third point... try hard.....
won't look for the outcome if it is good or bad(failure again)... i would try to fail better if not succeed... actually ... I would try to win in the most spectacular way possible... but at the same time won't be afraid to fail.