Daa 5
Daa 5
PROBLEM STATEMENT
Write a program in C to perform Breadth First Search and Depth First Search.
ALGORITHM
BFS
1. Start from a selected node (source).
2. Mark the node as visited and enqueue it.
3. While the queue is not empty:
Dequeue a node from the front of the queue.
Process the node.
Enqueue all unvisited neighbours of the node and mark them as visited.
DFS
1. Start from a selected node (source).
2. Mark the node as visited.
3. Process the node.
4. For each unvisited neighbour, recursively perform DFS.
CODE
#include <stdio.h>
#include <stdlib.h>
struct Graph {
int numVertices;
int adj[MAX][MAX];
};
visited[startVertex] = 1;
queue[rear++] = startVertex;
int main() {
int vertices, edges, src, dest;
int startVertex;
printf("Enter the starting vertex for DFS and BFS: ");
scanf("%d", &startVertex);
DFS(graph, startVertex);
BFS(graph, startVertex);
free(graph);
return 0;
}
OUTPUT
COMPLEXITY ANALYSIS
BFS:
Time Complexity: (O(V + E))
Space Complexity: (O(V))
DFS:
Time Complexity: (O(V + E))
Space Complexity: (O(V))