Tuesday, April 7, 2009

My former object oriented programing lecturer Mr.Nandhakumar wanted me to help with his project in which path generation was a part. For a given sequence directed graph I had to generate all possible paths from the start node to the end nodes and I tried it out. It took nearly an hour to finish this program. It's quite understandable from the comments I have added to the program!


#include<stdio.h>
#include<conio.h>

void findpath(int);//function to find the path
void printpath();//function to print the path

int graph[13][13]=

//0 1 2 3 4 5 6 7 8 9 10 11 12

{0,1,0,0,0,0,0,0,0,0,0 ,0 ,0,//0
0,0,1,1,0,0,0,0,0,0,0 ,0 ,0,//1
0,0,0,0,0,0,0,0,0,0,0 ,1 ,0,//2
0,0,0,0,1,1,1,0,0,0,0 ,0 ,0,//3
0,0,0,0,0,0,0,0,0,0,0 ,1 ,0,//4
0,0,0,0,0,0,0,0,0,0,0 ,1 ,0,//5
0,0,0,0,0,0,0,1,0,0,0 ,0 ,0,//6
0,0,0,0,0,0,0,0,1,0,0 ,0 ,0,//7
0,0,0,0,0,0,1,0,0,1,1 ,0 ,0,//8
0,0,1,0,0,0,0,0,0,0,0 ,1 ,0,//9
0,0,0,0,0,0,0,0,0,0,0 ,0 ,1,//10
0,0,0,0,0,0,0,0,0,0,0 ,0 ,0,//11
0,0,0,0,0,0,0,0,0,0,0 ,0 ,0};//12

int i=0,j=0,k=0,temp=0;//k--->acts as a pointer to the queue cur path
int start_node=0;//stores the start node
int path[13][13];//stores the paths available
int curpath[13]={0,0,0,0,0,0,0,0,0,0,0,0,0};//queue of the path formed
int final[13]={0,0,0,0,0,0,0,0,0,0,0,1,1};//final state or not
int tot_paths[13]={0,0,0,0,0,0,0,0,0,0,0,0,0};//total paths available from a state
int path_count[13]={0,0,0,0,0,0,0,0,0,0,0,0,0};//paths that have been traversed
int flag_cycle;//it is a flag which is set to 1 if the current path has a cycle
int previ;
void main()
{
clrscr();
for(i=0;i<13;i++)
{
printf("\nPath %d:",i);
k=0;
for(j=0;j<13;j++)
{
path[i][j]=0;
if(graph[i][j]!=0)
{
printf("%d,",j);
tot_paths[i]++;
path[i][k++]=j;
}
}
}
printf("\n\n\nThe paths available are:\n");
findpath(start_node);
printpath();
for(temp=k-1;temp>=0;temp--)
{
if(tot_paths[curpath[temp]]<=path_count[curpath[temp]])
{
path_count[curpath[temp]]=0;
curpath[temp]=0;
k--;
}
else
{
k--;
findpath(curpath[temp]);
printpath();
}
}
getch();
}

void findpath(int start)
{
int count;
i=start;
count=path_count[i];
while(!final[i])//while final state is not reached
{
//check whether there is a cycle in the graph,if so skip it

if(checkpath(i))
{
curpath[k]=i;
i=previ;
return;
}
curpath[k++]=i;//add the node in the queue
path_count[i]++;//increment the path count
previ=i;
i=path[i][count];//move to the next node
count=path_count[i];//update count
}
//the same process is repeated for the final state

curpath[k++]=i;
path_count[i]++;
i=path[i][count];
count=path_count[i];
k--;
}

void printpath()//when this fn. ends temp should be equal to k
{
int temp1;
//check whether this path contains a cycle
//if so skip it
for(temp1=0;temp1<k;temp1++)
{
if(curpath[k]==curpath[temp1])
{
temp=k;
return;
}
}
printf("\n");
for(temp=0;temp<k;temp++)//don't change this variable temp,it is a global
variable!
{
printf("%d--->",curpath[tem]);
}
printf("%d",curpath[temp]);

}

int checkpath(int value)//checks whether there is a cycle
{
int temp1;
for(temp1=0;temp1<k;temp1++)
{
if(value==curpath[temp1])
return 1;
}
return 0;
}

0 comments: