Monday, June 04, 2012

Kruskal's Algorithm, Simplified

Note : This article is intended to the readers  who can implement Kruskal's algorithm manually at the least.


Kruskal's algorithm is an algorithm for building Minimum Spanning Tree (MST) from an undirected graph.  The algorithm can be summarized as "Go on building the MST by adding the edges in their ascending order of their cost such that they do not create cycles".This can be explained in steps as:-
1. Identify next minimum edge
2. See whether that edge creates a cycle,if added to the MST
3. If it doesn't, add it to MST ,else discard it and repeat step 1.
4. Go to step 1 if MST is not complete.


Consider that we are using the Adjacency Matrix for  representing  the graph. Then Step 1 is the easiest to carry out and Step 2 is the hardest. It is the Step 2 that I want to discuss in detail. Step 1 can be carried out simply by constructing nested loops. At the end of step 1, we have the next minimum cost and the name of the nodes connected by the minimum cost edge.


Now comes the trickiest part. Given an edge,how will we find whether that edge creates a cycle or not when added to the MST?This requires a clearer understanding of what cycles is.


Identifying Cycles

Cycles occurs when there are multiple paths from any node to any other node. So before adding an edge (x,y) to the MST, we have to see whether there is already a path that exists between nodes x and y. A path between nodes x and y can take  number of forms. Examples are:-
1. (x,z) (z,y)
    x is connected to z and z is in turn  connected to y,thus creating a path between x and y
2. (x,z) (z,a)(a,y)
    This can be similarly be explained as  example 1.

Explanation with an Example

Consider the below graph
The adjacency matrix representation of the above graph is given as follows:-
Note: The blank spaces can be regarded as infinity which denotes that the corresponding nodes are not related.Also the above matrix is a symmetric matrix.

Now we start applying the Kruskal's algorithm manually.
The first minimum cost edge is (1,3) or (3,1). We will add it to MST since it won't create any cycles for obvious reasons. Similarly by manual evaluation (4,6) , (2,5) and (3,6) are added to the MST. Now our the adjacency Matrix of the graph would look like as below:-
Note: For each edge added to the MST, the corresponding column is made blank(or infinity)

Now our MST graph would look like as below:-
The adjacency matrix for the same is given by:-
The next minimum cost edge is (1,4) with a cost of 5. But by manual evaluation we know that this edge will result in cyclic graph when added to MST. Now we will see how to infer whether an edge will result in cyclic property, from the adjacency matrix of the MST. Now we need to see whether there exists a path between 1 and 4.We follow the below steps:-
A.Take the columns of the first row. Name them as (1,k) where k=1 to n (n is the number of nodes). 
B.Select the non blank columns among (1,k)and name them as(1,nb)  . If any of the nb=4,then we can straight forward say that there is a path between 1 and 4.If non among (1,nb) has nb=4,then we will see whether there exists, a path between (nb,4) by applying same procedure as Step A.Thus this goes on recursively.
Based on this we develop a noPath() procedure,which will take the nodes,and MST matrix as arguments and returns 1 if there is no path between them else it will return 0.

procedure noPath(int node1,int node2,int nodePrev,int n,int MST[][])

begin

         int k,ret=1;

        for k in range 1 to n do

        begin

                 if(MST[node1][k]!=blank)

                 then

                       if k equal node2

                       then

                              ret=0

                      else if k not equal node1 and k not equal to nodePrev 

                                              then    
                                                                     ret=noPath(k,node2,node1,n,MST)
                                                                 end
                                             end
                       end
       return ret
end {noPath}

Note:The variable nodePrev has been used to avoid the reverse direction flow. For example
after having a search through edge (1,6),we do not want to turn back and search (6,1).


With the above algorithm I hope I have covered the most important aspect of Kruskal algorithm .You may see the real C program implementing Kruskal algo here

Sunday, January 22, 2012

Command line tool for Internet Usage Monitoring in Linux

Yeah, a command line tool for monitoring the internet usage so that I can avoid over usage of the internet. This was something that I have been trying to develop for quite sometime and the breakthrough happened just yesterday.  Initially I was trying to develop it using  shell scripts. The whole script was centered at the ifconfig command. When I tried installing it to the crontab, it won't work for some reason which I don't know yet. Then I turned to Python. Here too the script was centered on the ifconfig. But to my disappointment that too didn't work when installed to crontab. it was at this point that I thought about a different strategy. In order to get the details of Internet usage in current session I decided to rely on the file, the ifconfig command is relying rather than the relying on ifconfig command itself.
                       So the next challenge was finding out the file ifconfig is relying. Then I decided to analyse the system calls used by ifconfig . Here I took the help of strace command and arrived at the conclusion /proc/net/dev is the file that is used by the ifconfig for the details of internet usage in current session. And with this file things become lot easier.
              As of now this application will monitor the usage for one month. Then it is reset to zero automatically. You can change the day of the month in which the monitoring begins. The application  also has a feature wherein  you can set free usage time, so that the internet usage is not monitored between the specified time.It can only monitor the internet communication taking place through eth0 (wired) eth1 (wireless) interfaces. 


            You may track the development here You can install it by running Installer.sh script (Don't change the file names and all files should be in the same directory as of Installer.sh ). After installing you can use the tool by using the command Eusage along with the switches.
Note : Scripts won't work without running Installer.sh. Don't try to run Eusage without the switches
Eusage -u :- used to give the details of usage so far, from the beginning of the month 
Eusage -r :- To clear the memory (i.e. everything        will be set to zero)
Eusage -h :- for help
Eusage -c :- for changing free usage time and to set the day of month to begin the monitoring



Saturday, December 17, 2011

Twitter for System Administration



How about shutting down/restarting your system via SMS? 
Is that something that is possible? Yes , With twitter SMS service that is of course possible. This can come handy for system administrators. Here I will show you how to control your system  on the move via SMS, using the services provided by the twitter.
          Initially we will create two twitter accounts , call them Master and Slave. Master account will be a normal account, on the other hand Slave will be a private account, which has connection only with Master(i.e Slave will be only following the Master and the only follower that Slave has, will be Master). This helps preventing users other than Master sending messages to the Slave.
           The next step will be a program that monitors the message inbox of the Slave for the messages from the Master. This program have to be run on the system that has to be controlled. The program should be able to log in to the account of Slave and to check out its inbox. That's where twitter API comes to rescue. For various reasons twitter prevents earlier Basic authentication. So we have to rely on OAuth . OAuth is a trickier authentication method , whose explanation is beyond the scope of this article. Using twitter api resources we will be checking out the message inbox of Slave. Remember Master is the only user in twitter who can send messages to the Slave . Also message to a twitter user can be sent via SMS .Messages are monitored by the program and depending on the message, necessary actions are taken. Below is simple program that will Shut down/restart the system depending on the message. 


Though the sample program can only be used for Shutting down/restarting the system ,System Administrators can add the functionality like auto replying to the queries issued by the Master, thus achieving a greater control over the system via SMS.
Note : Python tweepy module is a module that helps in communication with twitter. It can be installed in linux system by the command easy_install tweepy. Also the program is installed in crontab ,so that it runs every minute.

Thursday, December 01, 2011

The Notifier

Have you ever been in a situation where you were forced to check out a web page again and again to see whether a particular information appeared there or not? Well, I have been in such a situation where I wanted to see whether my university results were released or not. Checking out the University site every time using a browser was an option. But I decided to automate the process. That's how the following script was born.










After writing this script , I have to automate the running of this script. This was achieved using crontab . I scheduled it in a way that it gets executed every minute. So when the in formation appears in the web page I get notified automatically.
Note : By changing variables URL,Lookfor and Displaymessage any one can customize it for their own use.
Steps to setting up this in a Linux system
Step1 : Save the script with executable permissions. This can be achieved by the command chmod 777 filename
Step 2 : Type in the command crontab -e .Now a file gets opened up ,to which you need to add the following  * * * * *  absolute_path_to_the_script