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

Ass3 setb

The document contains two C programs for graph traversal. The first program implements Depth First Search (DFS) using an adjacency matrix to represent the graph, while the second program implements Breadth First Search (BFS) with a similar adjacency matrix structure. Both programs allow users to input the number of vertices and edges, display the adjacency matrix, and specify a starting vertex for traversal.

Uploaded by

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

Ass3 setb

The document contains two C programs for graph traversal. The first program implements Depth First Search (DFS) using an adjacency matrix to represent the graph, while the second program implements Breadth First Search (BFS) with a similar adjacency matrix structure. Both programs allow users to input the number of vertices and edges, display the adjacency matrix, and specify a starting vertex for traversal.

Uploaded by

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

/*Set B 2) Write a C program that accepts the vertices and edges of a graph and

store it as an adjacency matrix. Implement function to traverse the graph using


Depth First Search (DFS) traversal.*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAX_VERTICES 100

void dfs(int adjacencyMatrix[MAX_VERTICES][MAX_VERTICES], int numVertices, int


startVertex, bool visited[]);

int main()
{
int adjacencyMatrix[MAX_VERTICES][MAX_VERTICES] = {0};
int numVertices, numEdges, i, j, source, destination, startVertex;

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


scanf("%d", &numVertices);

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


scanf("%d", &numEdges);

// Accept edges and update the adjacency matrix


for (i = 0; i < numEdges; i++)
{
printf("Enter the source and destination vertices for edge %d: ", i +
1);
scanf("%d %d", &source, &destination);

// Since the graph is undirected, add edges in both directions


adjacencyMatrix[source - 1][destination - 1] = 1;
adjacencyMatrix[destination - 1][source - 1] = 1;
}

// Display the adjacency matrix


printf("\nAdjacency Matrix:\n");
for (i = 0; i < numVertices; i++)
{
for (j = 0; j < numVertices; j++)
{
printf("%d ", adjacencyMatrix[i][j]);
}
printf("\n");
}

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


scanf("%d", &startVertex);

printf("\nDFS traversal order: ");

bool visited[MAX_VERTICES] = {false};


dfs(adjacencyMatrix, numVertices, startVertex - 1, visited);
printf("\n");

return 0;
}

void dfs(int adjacencyMatrix[MAX_VERTICES][MAX_VERTICES], int numVertices, int


startVertex, bool visited[]) {
visited[startVertex] = true;
printf("%d ", startVertex + 1);

for (int i = 0; i < numVertices; i++)


{
if (adjacencyMatrix[startVertex][i] && !visited[i])
{
dfs(adjacencyMatrix, numVertices, i, visited);
}
}
}

/*Set B
a) Write a C program that accepts the vertices and edges of a graph and store it
as an adjacency matrix. Implement function to traverse the graph using Breadth
First Search (BFS) traversal.*/

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

#define MAX_VERTICES 100

void bfs(int adjacencyMatrix[MAX_VERTICES][MAX_VERTICES], int numVertices, int


startVertex);

int main()
{
int adjacencyMatrix[MAX_VERTICES][MAX_VERTICES] = {0};
int numVertices, numEdges, i, j, source, destination, startVertex;

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


scanf("%d", &numVertices);

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


scanf("%d", &numEdges);

// Accept edges and update the adjacency matrix


for (i = 0; i < numEdges; i++)
{
printf("Enter the source and destination vertices for edge %d: ", i +
1);
scanf("%d %d", &source, &destination);

// Since the graph is undirected, add edges in both directions


adjacencyMatrix[source - 1][destination - 1] = 1;
adjacencyMatrix[destination - 1][source - 1] = 1;
}

// Display the adjacency matrix


printf("\nAdjacency Matrix:\n");
for (i = 0; i < numVertices; i++)
{
for (j = 0; j < numVertices; j++)
{
printf("%d ", adjacencyMatrix[i][j]);
}
printf("\n");
}

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


scanf("%d", &startVertex);

printf("\nBFS traversal order: ");


bfs(adjacencyMatrix, numVertices, startVertex - 1);
printf("\n");

return 0;
}

void bfs(int adjacencyMatrix[MAX_VERTICES][MAX_VERTICES], int numVertices, int


startVertex)
{
bool visited[MAX_VERTICES] = {false};
int queue[MAX_VERTICES];
int front = 0, rear = 0;

visited[startVertex] = true;
printf("%d ", startVertex + 1);
queue[rear++] = startVertex;

while (front != rear)


{
int currentVertex = queue[front++];
for (int i = 0; i < numVertices; i++)
{
if (adjacencyMatrix[currentVertex][i] && !visited[i]) {
visited[i] = true;
printf("%d ", i + 1);
queue[rear++] = i;
}
}
}
}

You might also like