You are on page 1of 10

Provide the following operations for a graph using the adjacency matrix (i) Add a Vertex (ii) Add

an Edge (iii) Remove a Vertex (iv) Traverse the graph in breadth first order (v) Traverse the graph in depth first order #include<stdio.h> # define MAX 6 struct queue { int data[MAX]; int front,rear; }; void initq(struct queue *q) { q->front=q->rear=-1; } int isemptyq(struct queue q) { if(q.front==-1) return 1; return 0; } int isfullq(struct queue q) { if((q.front==1 && q.rear==MAX-1) || (q.rear==q.front-1)) return 1; return 0; } int enque(struct queue *q,char label) { if(isfullq(*q)) return 0; if(isemptyq(*q)) { q->front++; } if(q->rear==MAX-1) q->rear=0; else q->rear++;

representation:

Roll No. 05225304413

q->data[(q->rear)]=label; return 1; } int deque(struct queue *q,char *label) { if(isemptyq(*q)) return 0; *label=q->data[q->front]; if(q->front==q->rear) initq(q); else if(q->front==MAX-1) q->front=0; else q->front++; return 1; }

struct graph { char labels[MAX]; int adj[MAX][MAX]; int count; }; struct stack { char labels[MAX]; int top; }; int init(struct graph *g) { int i,j; g->count= -1; for(i=0;i<MAX;i++) for(j=0;j<MAX;j++) g->adj[i][j]=0; return 0; } int initstk(struct stack * s) { s->top=-1; return 0; } int isfull(struct stack const * s) {

Roll No. 05225304413

if(s->top == MAX-1) return 1; else return 0; } int isempty(struct stack const * s) { if(s->top == -1) return 1; else return 0; } int push(struct stack * s,char data) { if(!isfull(s)) { s->top++; s->labels[s->top]=data; return 0; } return 1; } int pop(struct stack * s,char * data) { if(!isempty(s)) { *data=s->labels[s->top]; s->top--; return 1; } return 0; }

int addnode(struct graph *g,char label) { if(g->count+1 == MAX) return 0; else g->labels[++(g->count)]=label; return 1; } int indexcal(struct graph g,char l) {

Roll No. 05225304413

int i=0; for(i=0;i<MAX;i++) { if(g.labels[i] == l) return i; } return -1; } int addedge(struct graph *g,char stlabel,char edlabel) { int st,en; st=indexcal(*g, stlabel); en=indexcal(*g, edlabel); if(st==-1||en==-1) return 0; else g->adj[st][en]++; return 1; } void display(struct graph g) { int i,j; for(i=0;i<=g.count;i++) { printf("\t%c",g.labels[i]); } for(i=0;i<=g.count;i++) { printf("\n\n%c",g.labels[i]); for(j=0;j<=g.count;j++) { printf("\t%d",g.adj[i][j]); } printf("\n"); } } void adjslist(struct graph g,char label) { int i,lb; lb=indexcal(g,label); for(i=0;i<=g.count;i++) { printf("\t%c",g.labels[i]); }

Roll No. 05225304413

printf("\n"); printf("%c",g.labels[lb]); for(i=0;i<=g.count;i++) { printf("\t%d ",g.adj[lb][i]); } } int indegree(struct graph g,char c) { int index,i,count=0; index=indexcal(g,c); if(index==-1) return -1; for(i=0;i<MAX;i++) { if(g.adj[i][index]>0) { count+=g.adj[i][index]; } } return count; } int outdegree(struct graph g,char c) { int index,i,count=0; index=indexcal(g,c); if(index==-1) return -1; for(i=0;i<MAX;i++) { if(g.adj[index][i]>0) { count+=g.adj[index][i]; } } return count; }

int dfs(struct graph g,char strvertex) { struct stack s; int st,i; char vtemp; initstk(&s); if(indexcal(g,strvertex)==-1) return 0;

Roll No. 05225304413

push(&s,strvertex); while(!isempty(&s)) { pop(&s,&vtemp); printf("%c",vtemp); st=indexcal(g,vtemp); for(i=0;i<=g.count;i++) { if(g.adj[st][i] > 0) { push(&s,g.labels[i]); g.adj[st][i]=0; } } } return 0; } int bfs(struct graph g,char strvertex) { struct queue s; int st,i; char vtemp; initq(&s); if(indexcal(g,strvertex)==-1) return 0; enque(&s,strvertex); while(!isemptyq(s)) { deque(&s,&vtemp); printf("%c",vtemp); st=indexcal(g,vtemp); for(i=0;i<=g.count;i++) { if(g.adj[st][i] > 0) { enque(&s,g.labels[i]); g.adj[st][i]=0; } } } return 0; }

Roll No. 05225304413

int main() { struct graph g; int choice,ch,sc; char label,s,d,label1; init(&g); do { printf("Select from following!"); printf("\n1.Add a Vertex "); printf("\n2.Add an Edge"); printf("\n3.Display Adjacency Matrix"); printf("\n4.Display Adjacency List"); printf("\n5.Calculate In-Degree"); printf("\n6.Calculate Out-Degree"); printf("\n7.Depth First Search"); printf("\n8.Breadth First Search"); printf("\n9.Exit"); printf("\nEnter your choice: "); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter The Label: "); while(getchar()!='\n'); scanf("%c",&label); sc=addnode(&g,label); if(sc) printf("\nVertex Added."); else printf("\nAdding of vertex failed"); break; case 2: printf("\nEnter the Source vertex: "); while(getchar()!='\n'); scanf("%c",&s); printf("\nEnter The destination vertex: "); while(getchar()!='\n'); scanf("%c",&d); sc=addedge(&g,s,d); if(sc) printf("\nEdge added !!!"); else printf("\nNo such Edge Found !!!"); break;

Roll No. 05225304413

case 3: display(g); break; case 4: printf("\nEnter The Label: "); while(getchar()!='\n'); scanf("%c",&label1); adjslist(g,label1); break; case 5: printf("\nEnter The Node: "); while(getchar()!='\n'); scanf("%c",&label); sc=indegree(g,label); if(sc==-1) printf("\nLabel Not Found !!!"); else printf("\nIn-degree: %d",sc); break; case 6: printf("\n\nEnter The Node: "); while(getchar()!='\n'); scanf("%c",&label); sc=outdegree(g,label); if(sc==-1) printf("\nLabel Not Found !!!"); else printf("\nOut-degree: %d",sc); break; case 7: printf("\nEnter The Source Node: "); while(getchar()!='\n'); scanf("%c",&label); printf("\nDepth - First Search: "); dfs(g,label); break; case 8: printf("\nEnter The Source Node: "); while(getchar()!='\n'); scanf("%c",&label); printf("\nBreadth - First Search: "); bfs(g,label);

Roll No. 05225304413

break; case 9: exit(1); break; }; printf("\nPress 1 to continue"); scanf("%d",&choice); }while(choice!=0); return 0; } OUTPUT :

Roll No. 05225304413

Roll No. 05225304413

You might also like