BFS-DFS
BFS-DFS
Sample Solution:
#include <stdio.h>
#include <stdlib.h>
graph[start][end] = 1;
graph[end][start] = 1; // For undirected graph
int queue[MAX_VERTICES];
queue[++rear] = startVertex;
visited[i] = 1;
queue[++rear] = i;
}
}
}
printf("\n");
int main() {
int vertices, edges;
// Input the number of vertices
scanf("%d", &vertices);
scanf("%d", &edges);
return 1;
}
if (start < 0 || start >= vertices || end < 0 || end >= vertices) {
printf("Invalid vertices. Try again.\n");
i--;
continue;
scanf("%d", &startVertex);
Output:
Input the number of vertices: 5
Input the number of edges: 6
Input edge 1 (start end): 0 1
Input edge 2 (start end): 1 2
Input edge 3 (start end): 2 3
Input edge 4 (start end): 3 4
Input edge 5 (start end): 4 0
Input edge 6 (start end): 2 4
Input the starting vertex for BFS traversal: 0
BFS Traversal Order: 0 1 4 2 3
Explanation:
Header Files:
o #include <stdio.h>: Provides input and output functions.
o #include <stdlib.h>: Provides memory allocation and other utility
functions.
Macro Definition:
o #define MAX_VERTICES 100: Defines the maximum number of
vertices in the graph.
Function to Add an Edge:
o void addEdge(int graph[MAX_VERTICES][MAX_VERTICES], int
start, int end): Adds an edge between two vertices in the adjacency
matrix, indicating a connection in the graph.
Function to Perform BFS Traversal:
o void BFS(int graph[MAX_VERTICES][MAX_VERTICES], int
vertices, int startVertex): Performs BFS traversal on the graph starting
from the specified vertex.
o Uses a queue to keep track of visited vertices and their neighbors.
o Prints the order of visited vertices.
Main Function:
o Reads the number of vertices (vertices) and edges (edges) from the
user.
o Constructs an adjacency matrix (graph) to represent the graph.
o Takes input for edges, validates input vertices, and adds edges to the
graph.
o Takes input for the starting vertex for BFS traversal (startVertex).
o Calls the BFS function to perform BFS traversal and prints the order
of visited vertices.
Sample Solution:
#include <stdio.h>
}
}
int main() {
int vertices, edges;
scanf("%d", &vertices);
return 1;
}
return 1;
}
if (start < 0 || start >= vertices || end < 0 || end >= vertices) {
printf("Invalid vertices. Try again.\n");
i--;
continue;
graph[start][end] = 1;
scanf("%d", &startVertex);
return 1;
Output:
Enter the number of vertices: 5
Enter the number of edges: 6
Enter edge 1 (start end): 0 1
Enter edge 2 (start end): 1 2
Enter edge 3 (start end): 2 3
Enter edge 4 (start end): 3 4
Enter edge 5 (start end): 4 0
Enter edge 6 (start end): 2 4
Enter the starting vertex for DFS traversal: 2
DFS Traversal Order: 2 3 4 0 1
Explanation:
DFS Function:
o The "DFS()" function takes the graph, an array to track visited
vertices, the total number of vertices, and the starting vertex as
parameters.
o It prints the current vertex, marks it as visited, and then recursively
explores all unvisited adjacent vertices.
Main Function:
o It takes user input for the number of vertices and edges.
o Constructs the adjacency matrix based on user input for edges.
o Takes the starting vertex for DFS traversal.
o Calls the "DFS()" function with the provided parameters.
o Outputs the DFS traversal order.
User Input:
o The user is prompted to input the number of vertices and edges.
o For each edge, the user inputs the starting and ending vertices.
Output:
o The program outputs the DFS traversal order starting at the specified
vertex.