0% found this document useful (0 votes)
6 views2 pages

Exp 4-2

The document contains a C program that implements a graph data structure using adjacency lists. It includes functions for creating nodes and graphs, adding edges, and performing breadth-first and depth-first traversals. The main function demonstrates these functionalities by creating a graph with 8 vertices and displaying the results of both traversal methods starting from vertex 1.

Uploaded by

lagova8513
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Exp 4-2

The document contains a C program that implements a graph data structure using adjacency lists. It includes functions for creating nodes and graphs, adding edges, and performing breadth-first and depth-first traversals. The main function demonstrates these functionalities by creating a graph with 8 vertices and displaying the results of both traversal methods starting from vertex 1.

Uploaded by

lagova8513
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <stdlib.h>
#include <stdbool.h>
struct Node{
int data;
struct Node* next;
};
struct Graph {
int numVertices;
struct Node** adjLists;
};
struct Node* createNode(int data);
struct Graph* createGraph(int vertices);
void addEdge (struct Graph* graph,int src,int dest);
void breadthFirstTraversal(struct Graph* graph,int start);
void depthFirstTraversal(int vertex,struct Graph* graph,bool visited[]);
struct Node* createNode(int data){
struct Node* newNode=(struct Node*) malloc (sizeof(struct Node));
newNode->data=data;
newNode->next=NULL;
return newNode;
}
struct Graph* createGraph(int vertices){
struct Graph* graph=(struct Graph*) malloc (sizeof(struct Graph));
graph->numVertices = vertices;
graph->adjLists=(struct Node**) malloc (vertices*sizeof(struct Node*));
for(int i=0;i<vertices;i++) {
graph->adjLists[i] = NULL;
}
return graph;
}
void addEdge(struct Graph* graph,int src,int dest){
struct Node*newNode = createNode(dest);
newNode->next=graph->adjLists[src];
graph->adjLists[src]=newNode;
}
void breadthFirstTraversal(struct Graph* graph,int start){
bool visited[graph->numVertices];
for(int i=0;i<graph->numVertices;i++){
visited[i]=false;
}
int queue[graph->numVertices],front=0,rear=0;
visited[start]=true;
queue[rear++]=start;
while(front < rear){
int current = queue[front++];
printf("%d ", current);
struct Node*temp = graph->adjLists[current];
while(temp!=NULL){
int adjVertex = temp->data;
if(!visited[adjVertex]){
visited[adjVertex]=true;
queue[rear++]=adjVertex;
}
temp = temp->next;
}
}
printf("\n");
}
void depthFirstTraversal(int vertex,struct Graph* graph,bool visited[]){
visited[vertex]=true;
printf("%d ",vertex);
struct Node*temp = graph->adjLists[vertex];
while(temp != NULL){
int adjVertex=temp->data;
if(!visited[adjVertex]){
depthFirstTraversal(adjVertex,graph,visited);
}
temp = temp->next;
}
}
int main() {
int numVertices = 8;
struct Graph* graph = createGraph(numVertices);
addEdge(graph,1,2);
addEdge(graph,1,3);
addEdge(graph,2,4);
addEdge(graph,2,5);
addEdge(graph,3,6);
addEdge(graph,3,7);
addEdge(graph,4,8);
addEdge(graph,5,8);
addEdge(graph,6,8);
addEdge(graph,7,8);
printf("Breadth-First Traversal(BFT) starting from vertex 1: ");
breadthFirstTraversal(graph,1);
bool visited[numVertices];
for(int i=0;i<numVertices;i++){
visited[i]=false;
}
printf("Depth-First Traversal(DFT) strating from vertex 1: ");
depthFirstTraversal(1,graph,visited);
printf("\n");
return 0;
}

You might also like