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.

Thursday, 22 May 2014

using const & varName in some function ....just some useful coding stuff

Okay lets keep this short and sweet....

while doing custom sorting using the sort function present in <algorithm> I saw a construct (or rather a way of programming) which i rarely (read never) use. This was this:

//normal part

typdef struct
{

}CoolStruct;

bool mySort(CoolStruct const &lhs, CoolStruct const &rhs )
{...}

Now why are we actually using const &varName....???

So i stumbled upon this article
http://duramecho.com/ComputerInformation/WhyHowCppConst.html

Now the thing related to using const while parameter passing is this:
passing structure would incur making copy of it. Now this might take a lot of time depending upon the size of the structure. One way to bypass this is passing a refernce to it using ( & symbol) in C++. But then you can make changes to it.... so while working on libraries if you want to make sure that nobody alters the passed refernce and that you are doing it for optimization purposes only "const" is added. So its saying that pass a refence so that no copying is involved ...  and i want to protect the data too so don't alter it.

Further the article is very nicely written and worth a read.

Enjoy ...future Mukul while reading it.

without wax
Mukul on 23 may 2014


Monday, 27 January 2014

Touchpad not registering clicks on ubuntu...here might be a solution....

So it happened again...while playing a video in vlc...touch pad stopped registering clicks. No, wait it was more than that...it stopped registering click on certain areas of the screen only.
Hmm...now here's an interesting problem for Sherlock to solve.

Well it turned out that some random guy had faced it before and had done quite a bit analysis...(sherlock of the open source world I say). And once again I was awestruck by this community ....the community of open source users who are so good at what they do.

So here's the link with detailed analysis.

http://looselytyped.blogspot.in/2012/10/fixing-mousetouchpad-freeze-and-hanging.html

And if in future you can't find this page..then here's the summary:

1)To restart the touchpad here are the commands:
  • pen terminal (ctrl + alt + t) or press alt + f2 to bring up unity's command lens.
  • Execute the following commands (recommended to run using gksudo but can be run using sudo as well) - 
    • gksudo modprobe -r psmouse (removes the mouse interface from the linux kernel)
    • gksudo modprobe psmouse (adds the mouse interface to the linux kernel)
     To restart a USB mouse -
  • Open terminal (ctrl + alt + t) or press alt + f2 to bring up unity's command lens.
  • Execute this command - 
 sudo /etc/init.d/hotplug restart

2) As a general practice Uncheck the 'Disable touchpad while typing' option.

3) It might be possible that unity lens(or some other stuff) might be open in the background...hence press esc. two -three times.

DONE!

Hmmm..don't know why* but this page is looking well formatted. Lite...who cares for formatting anyway.


-Cheers


*(actually i know why ;) )

 

Cool battery tweak...to show battery in percentage.

Its been quite some time. I would update it continuously now(...promise!). So here's one.

Its a tweak to display percentage instead of time remaining in ubuntu. I tried it on 12.10...works fine.

I didn't do it...so here's the link.

http://www.hecticgeek.com/2012/10/make-ubuntu-show-remaining-battery-life-as-a-percentage-tip/

In short... add repository...install...enjoy.

If you are future Mukul reading this: then have fun...you would bless your old self for keeping note of the link.

else if you  are someone else.....hope the above method helped you.



Saturday, 17 August 2013

Introduction.....

"Hello World"... the basic program which is described for anyone who starts a new language. So probably I should also start with a "Hello Blog" post. But naaa....that is just too plain and simple. Let me discuss something new...something inspiring. Let me describe you my theory.

1. Necessity is the mother of all inventions (heard it before right?)
2. Laziness is the mother of all shortcuts.
3. Boredom is the mother of everything else.
(the last two points being mine original)

So as i was getting a bit bored on this august evening I decided to do something new (read make a new blog). And thus this blog falls in the third category mentioned above.
So here i am...probably not knowing if anyone else would ever read these sentences or not...but still burning my eyes and typing my heart(or actually brain) out....So enough of intro..lets get down to work.

Who am I ?
A human. Yup no kidding.


What this blog is all about?
I will share every tech related knowledge that i have with you all. Since i learned a lot from people (searching problems on internet and finding some helpful blog...) I want to give back to the world in very similar manner.

This blog will just talk about science and tech stuff in the most humanly and simple way possible so that maximum possible number of people can understand stuff and use it.
The blog will also contain my day to day learning experiences so that anyone can learn with me.
(Hmm you caught me...the purpose of the last point is just to maintain a repository where i can keep my day to day learning so that I can access it anywhere ..anytime. Thanks Google!)

So let the game begin!