Saturday, November 24, 2012

Very Simple My Own Shell in C

Do you want to publish source codes in your blog or web site as follows.
Visit Source Code Formatter
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<string.h>  
 int main(){  
       char buf[256]={0};  
       printf("\n\n\n");  
       while(1){  
            printf("\nMOSH : ");  
            if (fgets(buf, sizeof(buf), stdin) == NULL){  
                fprintf(stderr, "Invalid input command!\n");  
                exit(-1);  
              }  
              buf[strlen(buf)-1]='\0';  
            system(buf);  
       }       
       return 0;  
 }  

Saturday, November 10, 2012

Maximum matching in bipartite graph using Hopcroft Karp algorithm in C ++

Do you want to publish source codes in your blog or web site as follows.
Visit Source Code Formatter
 /*******************************************************/  
 /*       2nd Takehome Assignment         */  
 /*Instructions :                    */  
 /*   Data should be input to the program using text */  
 /*     file (EX: test.txt)            */  
 /*   INPUT FORMAT:                 */  
 /*     4 5 5                   */  
 /*     1 8                    */  
 /*     1 5                    */  
 /*     2 6                    */  
 /*     3 9                    */  
 /*     4 7                    */  
 /*   first three integers (n,m and e)        */  
 /*     n: number of left hand nodes        */  
 /*     m: number of right hand node        */  
 /*     e: number of incedent edges        */  
 /*    next integer(g[u][v])            */  
 /*      g[u][v]: adjecent verteces        */  
 /*******************************************************/  
 #include <iostream>  
 #include <vector>  
 #include <stdio.h>  
 #include <stdlib.h>  
 #include <string.h>  
 #include <queue>  
 #include <fstream>  
 using namespace std;  
 #define MAX 10000 //maximum allowed couples  
 #define NIL 0  
 #define INF (1<<28) //infinity  
 vector<int> G[MAX]; // G[0]= NIL u G1[G[1---n]] u G2[G[n+1---n+m]]  
 int n,m,e,match[MAX],dist[MAX];  
 //Breadth first search  
 bool bfs() {  
   int i, u, v, len;  
   queue<int> Q;  //an integer queue  
   for(i=1; i<=n; i++) {  
     if(match[i]==NIL) { //i is not matched  
       dist[i] = 0;  
       Q.push(i);  
     }  
     else dist[i] = INF;  
   }  
   dist[NIL] = INF;  
   while(!Q.empty()) {  
     u = Q.front();  
     Q.pop();  
     if(u!=NIL) {  
       len = G[u].size();  
       for(i=0; i<len; i++) {  
         v = G[u][i];  
         if(dist[match[v]]==INF) {  
           dist[match[v]] = dist[u] + 1;  
           Q.push(match[v]);  
         }  
       }  
     }  
   }  
   return (dist[NIL]!=INF);  
 }  
 //depth first search  
 bool dfs(int u) {  
   int i, v, len;  
   if(u!=NIL) {  
     len = G[u].size();  
     for(i=0; i<len; i++) {  
       v = G[u][i];  
       if(dist[match[v]]==dist[u]+1) {  
         if(dfs(match[v])) {  
           match[v] = u;  
           match[u] = v;  
           return true;  
         }  
       }  
     }  
     dist[u] = INF;  
     return false;  
   }  
   return true;  
 }  
 int hopcroft_karp() {  
   int matching = 0, i;  
   // match[] is assumed NIL for all vertex in G  
   while(bfs())  
     for(i=1; i<=n; i++)  
       if(match[i]==NIL && dfs(i))  
         matching++;  
   return matching;  
 }  
 //read data file in to an array  
 int openDataFile(char *filename){  
   int A, B;  
   ifstream infile(filename);  
   if (!infile) { //fill not found  
     printf("There was a problem opening file %s for reading.\n",filename);  
     return 0;  
   }  
   printf("Opened %s for reading.\n", filename); //file opened  
   infile >> n >> m >>e; // for perfect matching n=m  
   while (infile >> A >> B) { //read adjecent verteces  
     G[A].push_back(B);  
   }  
 }  
 //main method starts here  
 int main() {  
   printf("\n*******************************\n\tFind Stable Cuples\n*******************************\n");  
   char filename[256] = {0};  
   int NumOfMatches,i;  
   printf("Enter data file name : ");  
   scanf("%s",&filename);  
   openDataFile(filename);  
   while(1){  
     int choice;  
     printf("\n*******************************\n\tFind Stable Cuples\n*******************************\n");  
     printf("Enter 1 : Find Optimal Solution\n");  
     printf("Enter 2 : Add New Data File\n");  
     printf("Enter 0 : Exit\n");  
     printf("\nEnter :");  
     scanf("%d",&choice);  
     if(choice==1){  
       openDataFile(filename);  
       NumOfMatches=hopcroft_karp(); //find matching perfect matching couples  
       printf("Number of couples matched : %d\n",NumOfMatches);  
       for(i=1;i<=n;i++){  
         printf("%d matched with %d\n",i,match[i]);  
       }  
     }else if(choice==2){  
       printf("Enter data file name : ");  
       scanf("%s",&filename);  
     }  
     else if(choice==0){  
       printf("Program Terminated\n");  
       exit(0);  
     }  
     else{  
       printf("Error : Wrong Input\n");  
     }  
   }  
   return 0;  
 }  

Maximum matching in bipartite graph using Ford Fulkerson algorithm in C

Do you want to publish source codes in your blog or web site as follows.
Visit Source Code Formatter
 /*******************************************************/  
 /*       2nd Takehome Assignment         */  
 /*    Maximum matching marriage couples using   */  
 /*      Ford Fulkerson Algorithm         */  
 /*Instructions :                    */  
 /*   Data should be input to the program using text */  
 /*     file (EX: test.txt)            */  
 /*   INPUT FORMAT:                 */  
 /*     10 10                   */  
 /*     1 6                    */  
 /*     1 7                    */  
 /*     2 7                    */  
 /*     3 6                    */  
 /*     3 8                    */  
 /*     3 10                    */  
 /*     4 7                    */  
 /*     4 10                    */  
 /*     5 7                    */  
 /*     5 9                    */  
 /*   first two integers (n and e)          */  
 /*     n: number of nodes in the graph      */  
 /*     e: number of incedent edges        */  
 /*    next integer(u,v)              */  
 /*      g[u][v]: adjecent verteces        */  
 /*******************************************************/  
 #include <stdio.h>  
 #include <stdlib.h>  
 // Basic Definitions  
 #define WHITE 0   //nodes status  
 #define GRAY 1  
 #define BLACK 2  
 #define MAX_NODES 1000   //maximum allowed nodes  
 #define INFINITY 1000000000  
 // variable Declarations  
 int n; // number of nodes  
 int e; // number of edges  
 int capacity[MAX_NODES][MAX_NODES]; // capacity matrix  
 int flow[MAX_NODES][MAX_NODES];   // flow matrix  
 int color[MAX_NODES]; // store node status  
 int parent[MAX_NODES]; // array to store augmenting path  
 int min (int x, int y) {  
   return x<y ? x : y; // returns minimum of x and y  
 }  
 // A Queue for Breadth-First Search  
 int head,tail;  
 int q[MAX_NODES+2];  
 void enqueue (int x) { //add to queue  
   q[tail] = x;  
   tail++;  
   color[x] = GRAY;  
 }  
 int dequeue () {    //remove from queue  
   int x = q[head];  
   head++;  
   color[x] = BLACK;  
   return x;  
 }  
 // Breadth-First Search for an augmenting path  
 int bfs (int start, int target) {  
   int u,v;  
   for (u=0; u<n+2; u++) { //initialize node status  
     color[u] = WHITE;  
   }  
   head = tail = 0;  
   enqueue(start);  
   parent[start] = -1;  
   while (head!=tail) {  
     u = dequeue();  
     // Search all adjacent white nodes v. If the capacity  
     // from u to v in the residual network is positive,  
     // enqueue v.  
     for (v=0; v<n+2; v++) {  
       if (color[v]==WHITE && capacity[u][v]-flow[u][v]>0) {  
         enqueue(v);  
         parent[v] = u;  
       }  
     }  
   }  
   // If the color of the target node is black now,  
   // it means that we reached it.  
   //printf("%d\n",color[target]);  
   return color[target]==BLACK;  
 }  
 // Ford-Fulkerson Algorithm  
 int max_flow (int source, int sink) {  
   int i,j,u;  
   // Initialize empty flow.  
   int max_flow = 0;  
   for (i=0; i<n+2; i++) {  
     for (j=0; j<n+2; j++) {  
       flow[i][j] = 0;  
     }  
   }  
   // While there exists an augmenting path,  
   // increment the flow along this path.  
   printf("\nAugmented Path :\n");  
   while (bfs(source,sink)) {  
     // Determine the amount by which we can increment the flow.  
     int increment = INFINITY;  
     for (u=sink; parent[u]!=(-1); u=parent[u]) {  
       increment = min(increment,capacity[parent[u]][u]-flow[parent[u]][u]);  
     }  
     //printf("\n%d\n",increment);  
     // Now increment the flow.  
     for (u=sink; parent[u]!=(-1); u=parent[u]) {  
       flow[parent[u]][u] += increment;  
       flow[u][parent[u]] -= increment; // Reverse in residual  
     }  
     printf("\t");  
     // Path trace  
     for (u=sink; parent[u]!=(-1); u=parent[u]) {  
       printf("%d<-",u);  
     }  
     printf("%d adds %d incremental flow\n",source,increment);  
     max_flow += increment;  
     //printf("\n\n%d %d\n\n",source,sink);  
   }  
   //printf("%d\n",s);  
   // No augmenting path anymore. We are done.  
   return max_flow;  
 }  
 // Reading the input file and the main program  
 int read_input_file() {  
   int a,b,i,j;  
   char filename[256] = {0};  
   printf("Enter data file name : ");  
   scanf("%s",&filename);  
   FILE* input = fopen(filename,"r");  
   if(!input){  
     printf("There was a problem opening file %s for reading.\n",filename);  
     return 0;  
   }  
   // read number of nodes and edges  
   fscanf(input,"%d %d",&n,&e);  
   // initialize empty capacity matrix  
   for (i=0; i<n+2; i++) {  
     for (j=0; j<n+2; j++) {  
       capacity[i][j] = 0;  
     }  
   }  
   // read edge capacities  
   for (i=0; i<e; i++) {  
     fscanf(input,"%d %d",&a,&b);  
     if(a==b){  
       printf("ERROR : Graph is not bipartite");  
       return 0;  
     }  
     capacity[a][b]= 1; // Could have parallel edges  
   }  
   fclose(input);  
   for(i=0;i<n/2;i++){  
     capacity[0][i+1]= 1;  
     capacity[i+6][n+1]= 1;  
   }  
   return 1;  
 }  
 //Extra works  
 /*void read_input_file_w() {  
   int a,b,c,i,j;  
   FILE* input = fopen(filename,"a+");  
   // read number of nodes and edges  
   fscanf(input,"%d %d",&n,&e);  
   // initialize empty capacity matrix  
   for (i=0; i<n+2; i++) {  
     for (j=0; j<n+2; j++) {  
       capacity[i][j] = 0;  
     }  
   }  
   // read edge capacities  
   for (i=0; i<e; i++) {  
     fscanf(input,"%d %d",&a,&b);  
     capacity[b][a]= 1; // Could have parallel edges  
   }  
   fclose(input);  
   for(i=0;i<n/2;i++){  
     capacity[0][i+6]= 1;  
     capacity[i+1][n+1]= 1;  
   }  
 }  
 void printMatrix(){  
   int i,j;  
   for (i=0; i<=n+1; i++) {  
     for (j=0; j<=n+1; j++) {  
       printf("%d ",capacity[i][j]);  
     }  
     printf("\n");  
   }  
 }*/  
 int start(){  
   int isBipartite=0;  
   while(!isBipartite){  
     printf("\n*******************************\n\tFind Stable Cuples\n*******************************\n");  
     isBipartite=read_input_file();  
     //printMatrix();  
   }  
   return isBipartite;  
 }  
 int main () {  
   int i,j;  
   int isBipartite=0;  
   isBipartite=start();  
   while(isBipartite){  
     int choice;  
     printf("\n*******************************\n\tFind Stable Cuples\n*******************************\n");  
     printf("Enter 1 : Find Optimal Solution\n");  
     printf("Enter 2 : Add New Data File\n");  
     printf("Enter 0 : Exit\n");  
     printf("\nEnter :");  
     scanf("%d",&choice);  
     if(choice==1){  
       printf("\ntotal flow is %d\n",max_flow(0,n+1));  
       printf("\nMatched couples:\n");  
       for (i=1; i<=n; i++)  
           //printf("%d ",parent[i]);  
         for (j=1; j<=n; j++)  
           if (flow[i][j]>0)  
             printf("\tMen:%d matched with Women:%d\n",i,j);  
     }else if(choice==2){  
       isBipartite=start();  
       //printMatrix();  
     }  
     else if(choice==0){  
       printf("Program Terminated\n");  
       exit(0);  
     }  
     else{  
       printf("Error : Wrong Input\n");  
     }  
   }  
   return 0;  
 }