0% found this document useful (0 votes)
51 views

Depth First and Breadth First Traversal of Graphs Represented Using Adjacency

Uploaded by

Lakshya Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Depth First and Breadth First Traversal of Graphs Represented Using Adjacency

Uploaded by

Lakshya Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h>
#include <stdlib.h>
#include <stdbool.h>

#define MAX_VERTICES 100

// Adjacency Matrix representation of a graph


int adjacencyMatrix[MAX_VERTICES][MAX_VERTICES];

// Adjacency List representation of a graph


struct Node {
int data;
struct Node* next;
};

struct List {
struct Node* head;
};

struct List adjacencyList[MAX_VERTICES];

int vertices, edges;

// Function to initialize the graph with given vertices and edges


void initializeGraph() {
int i, j;

// Initialize adjacency matrix with zeros


for (i = 0; i < MAX_VERTICES; i++) {
for (j = 0; j < MAX_VERTICES; j++) {
adjacencyMatrix[i][j] = 0;
}
}

// Initialize adjacency list with NULL


for (i = 0; i < MAX_VERTICES; i++) {
adjacencyList[i].head = NULL;
}
}

// Function to add an edge to the graph


void addEdge(int start, int end) {
adjacencyMatrix[start][end] = 1;
adjacencyMatrix[end][start] = 1;

// Create nodes for adjacency list and add edges


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = end;
newNode->next = adjacencyList[start].head;
adjacencyList[start].head = newNode;

newNode = (struct Node*)malloc(sizeof(struct Node));


newNode->data = start;
newNode->next = adjacencyList[end].head;
adjacencyList[end].head = newNode;
}

// Depth First Traversal (DFS) using recursion for adjacency matrix


void DFSMatrix(int vertex, bool visited[]) {
int i;

printf("%d ", vertex);


visited[vertex] = true;

for (i = 0; i < vertices; i++) {


if (adjacencyMatrix[vertex][i] == 1 && !visited[i]) {
DFSMatrix(i, visited);
}
}
}

// Depth First Traversal (DFS) using recursion for adjacency list


void DFSList(int vertex, bool visited[]) {
printf("%d ", vertex);
visited[vertex] = true;

struct Node* temp = adjacencyList[vertex].head;


while (temp != NULL) {
if (!visited[temp->data]) {
DFSList(temp->data, visited);
}
temp = temp->next;
}
}

// Breadth First Traversal (BFS) for adjacency matrix


void BFSMatrix(int start, bool visited[]) {
int queue[MAX_VERTICES];
int front = -1, rear = -1;

printf("%d ", start);


visited[start] = true;
queue[++rear] = start;

while (front != rear) {


int current = queue[++front];

for (int i = 0; i < vertices; i++) {


if (adjacencyMatrix[current][i] == 1 && !visited[i]) {
printf("%d ", i);
visited[i] = true;
queue[++rear] = i;
}
}
}
}

// Breadth First Traversal (BFS) for adjacency list


void BFSList(int start, bool visited[]) {
int queue[MAX_VERTICES];
int front = -1, rear = -1;

printf("%d ", start);


visited[start] = true;
queue[++rear] = start;

while (front != rear) {


int current = queue[++front];
struct Node* temp = adjacencyList[current].head;
while (temp != NULL) {
if (!visited[temp->data]) {
printf("%d ", temp->data);
visited[temp->data] = true;
queue[++rear] = temp->data;
}
temp = temp->next;
}
}
}

int main() {
int i, startVertex;

printf("Enter the number of vertices: ");


scanf("%d", &vertices);

printf("Enter the number of edges: ");


scanf("%d", &edges);

initializeGraph();

printf("Enter the edges (format: start_vertex end_vertex):\n");


for (i = 0; i < edges; i++) {
int start, end;
scanf("%d %d", &start, &end);
addEdge(start, end);
}

printf("Enter the starting vertex for traversal: ");


scanf("%d", &startVertex);

bool visited[MAX_VERTICES];
for (i = 0; i < MAX_VERTICES; i++) {
visited[i] = false;
}

printf("\nDepth First Traversal (DFS) using adjacency matrix: ");


DFSMatrix(startVertex, visited);

for (i = 0; i < MAX_VERTICES; i++) {


visited[i] = false;
}

printf("\nDepth First Traversal (DFS) using adjacency list: ");


DFSList(startVertex, visited);

for (i = 0; i < MAX_VERTICES; i++) {


visited[i] = false;
}

printf("\nBreadth First Traversal (BFS) using adjacency matrix: ");


BFSMatrix(startVertex, visited);

for (i = 0; i < MAX_VERTICES; i++) {


visited[i] = false;
}
printf("\nBreadth First Traversal (BFS) using adjacency list: ");
BFSList(startVertex, visited);

printf("\n");

return 0;
}

You might also like