Saturday, August 9, 2014

Another program for DFS and BFS

Another implementation of DFS and BFS. Comment if you need help.

#include<stdio.h> #include<stdlib.h> //Structure for Vertex Nodes struct vertexNode { int element; int visited; struct edgeNode *edge; struct vertexNode *next; }; typedef struct vertexNode vertexNode; vertexNode *start = NULL; //Structure for Edge Nodes struct edgeNode { struct vertexNode *connectsTo; struct edgeNode *next; }; typedef struct edgeNode edgeNode; //Function for creating new vertex node void addVertex(int element) { vertexNode *newVertex,*temp; //Allocating memory for new vertex node and initializing the values newVertex = (vertexNode *) malloc(sizeof(vertexNode)); newVertex->element = element; newVertex->visited =0; newVertex->edge=NULL; newVertex->next=NULL; if(start==NULL) //If the graph is empty { start=newVertex; } else //If the graph is not empty { temp= start; while(temp->next!=NULL) { temp = temp->next; } temp->next = newVertex; } } //Function for creating new edge node void addEdge(int v1, int v2) { edgeNode *newEdge,*temp; vertexNode *temp1,*v,*u; temp1 = start; /*Finding the source and destination vertex v is the source vertex and u is the destination vertex*/ while(temp1) { if(temp1->element == v1) v=temp1; if(temp1->element == v2) u=temp1; temp1=temp1->next; } newEdge = (edgeNode *) malloc(sizeof(edgeNode)); newEdge->connectsTo = u; newEdge->next = NULL; if(v->edge==NULL) { v->edge = newEdge; } else { temp=v->edge; while(temp->next!=NULL) { temp=temp->next; } temp->next=newEdge; } } //Function for displaying the graph void displayGraph() { vertexNode *temp1; edgeNode *temp2; temp1=start; while(temp1) { printf("%d->",temp1->element); if(temp1->edge!=NULL) { temp2=temp1->edge; while(temp2) { printf("%d->",temp2->connectsTo->element); temp2=temp2->next; } } printf("\n|\n"); printf("v\n"); temp1=temp1->next; } } struct stackNode { vertexNode *vElement; struct stackNode *next; }; typedef struct stackNode stackNode; stackNode *top = NULL; //Stack push function void push(vertexNode *v) { stackNode *newStackNode; newStackNode = (stackNode *) malloc(sizeof(stackNode)); newStackNode->vElement = v; newStackNode->next = NULL; if(top==NULL) { top=newStackNode; } else { newStackNode->next = top; top = newStackNode; } } //Stack pop function vertexNode * pop() { vertexNode *temp=NULL; if(top==NULL) { printf("\nStack is Empty"); } else { temp = top->vElement; top = top->next; } return(temp); } void dfs() { vertexNode *temp; edgeNode *temp2; push(start); while(top) { temp = pop(); if(temp->visited==0) { printf("%d\t",temp->element); temp->visited = 1; } temp2=temp->edge; while(temp2) { if(temp2->connectsTo->visited==0) push(temp2->connectsTo); temp2=temp2->next; } } } //Structure for Queue Nodes struct queueNode { vertexNode *vElement; struct queueNode *next; }; typedef struct queueNode queueNode; queueNode *qFront=NULL,*qRear=NULL; //Queue Insert function void enQueue(vertexNode *v) { queueNode *tmp_ptr=NULL; tmp_ptr=(queueNode *)malloc(sizeof(queueNode)); tmp_ptr->vElement = v; tmp_ptr->next = NULL; if(qFront == NULL) { qFront=tmp_ptr; qRear=tmp_ptr; } else { qRear->next=tmp_ptr; qRear=tmp_ptr; } } //Queue Delete function vertexNode * deQueue() { /*Delete element from Queue*/ vertexNode *temp=NULL; temp = qFront->vElement; if(qFront==NULL) printf("\nThe Queue is Empty"); else if(qFront==qRear) qFront=qRear=NULL; else qFront=qFront->next; return(temp); } //Function for Breadth First Search void bfs() { vertexNode *temp; edgeNode *temp2; enQueue(start); while(qFront) { temp =deQueue(); if(temp->visited==0) { printf("%d\t",temp->element); temp->visited = 1; } temp2=temp->edge; while(temp2) { enQueue(temp2->connectsTo); temp2=temp2->next; } } } int main() { int i,num,v1,v2; do { printf("\n1.INSERT\n2.ADD EDGE\n3.BFS traversal\n4.DFS traversal\n5.DISplay\n6.exit\nenter your choice:"); scanf("%d",&i); switch(i) { case 1: printf("\nEnter vertex:\t"); scanf("%d",&num); addVertex(num); break; case 4: dfs(); break; case 2: printf("\nEnter vertices to be connected:\t"); scanf("%d%d",&v1,&v2); addEdge(v1,v2); break; case 3: bfs(); break; case 5: displayGraph(); break; case 6: break; default: printf("ERROR! Invalid Choice..\n"); } } while(i!=6); }

No comments:

Post a Comment