My Profile

I, Das ShrikKrishna J. MCA III IMSCD&R, Ahmednagar.
Showing posts with label Perform Operations on Graph. Show all posts
Showing posts with label Perform Operations on Graph. Show all posts

Monday, 15 August 2011

Perform Operations on Graph

Write a Program in C to Perform Operations on Graph
#include<stdio.h>
#include<conio.h>
void create();
void display();
void iedge(int t,int h);
void inode();
void dedge(int t,int h);
void dnode(int d);
int adj[10][10];
int n;
void main()
{
int node,tail,head,action;
char choice='y';
clrscr();
create();
display();
while(1)
{
printf("\n\n1) Insert a node\n");
printf("2) Insert an edge\n");
printf("3) Delete a node\n");
printf("4) Delete an edge\n");
printf("5) Dispaly\n");
printf("6) Exit\n");
printf("Enter your choice : ");
scanf("%d",&action);
switch(action)
{
case 1:
choice='y';
while(choice=='y'||choice=='Y')
{
inode();
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
display();
break;
case 2:
choice='y';
while(choice=='y'||choice=='Y')
{
printf("\nEnter an edge to be inserted : ");
scanf("%d %d",&tail,&head);
iedge(tail,head);
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
display();
break;
case 3:
choice='y';
while(choice=='y'||choice=='Y')
{
printf("\nEnter a node to be deleted : ");
scanf("%d",&node);
dnode(node);
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
display();
break;
case 4:
choice='y';
while(choice=='y'||choice=='Y')
{
printf("\nEnter an edge to be deleted : ");
scanf("%d %d",&tail,&head);
dedge(tail,head);
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
display();
break;
case 5:
display();
break;
case 6:
exit();
break;
}
}
}
void create()
{
int i,j,edges,tail,head;
printf("Enter number of nodes : ");
scanf("%d",&n);
edges=n*(n-1);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
adj[i][j]=0;
}
}
printf("\nEnter -1 -1 to exit \n\n");
for(i=1;i<=edges;i++)
{
printf("Edge %d : ",i);
scanf("%d %d",&tail,&head);
if(tail==-1&&head==-1)
break;
if(tail>n||head>n||tail<=0||head<=0)
{
printf("Invalid edge!\n");
i--;
}
else
adj[tail][head]=1;
}
}
void display()
{
int i,j;
printf("\n");
for(i=1;i<=n;i++)
{
printf("\t%d",i);
}
printf("\n---------------------------------\n");
for(i=1;i<=n;i++)
{
printf("%d |",i);
for(j=1;j<=n;j++)
{
printf("\t%d",adj[i][j]);
}
printf(" | \n");
}
getch();
}
void inode() //insert new node by increasing size of 2d array
{
int i;
n++;
printf("\nNode %d is inserted successfully\n",n);
for(i=1;i<=n;i++)
{
adj[i][n]=0;
adj[n][i]=0;
}
}
void dnode(int d) //delete the node from graph
{
int i,j;
if(n==0)
{
printf("Graph is empty\n");
return;
}
if(d>n)
{
printf("Invalid Node\n");
return;
}
for(i=d;i<=n-1;i++)
{
for(j=1;j<=n;j++)
{
adj[i][j]=adj[i+1][j]; //move rows upward
adj[j][i]=adj[j][i+1]; //move columns left
}
}
n--;
}
void iedge(int t,int h) //insert or add link between 2 nodes
{
if(t>n||h>n||t<=0||h<=0)
{
printf("Invalid edge!\n");
return;
}
else
{
adj[t][h]=1;
}
}
void dedge(int t,int h) //delete or remove link between 2 nodes
{
if(t>n||h>n||t<=0||h<=0||adj[t][h]==0)
{
printf("Invalid edge or doesnot exist\n");
return;
}
else
{
adj[t][h]=0;
}
}

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | cheap international calls