DFT & BFT
DFT & BFT
Implement BFT and DFT for given graph, when graph is represented by
Descrition:-
Breadth-First Traversal (BFT), also known as Breadth-First Search (BFS), is an algorithm for
traversing or searching tree or graph data structures. It starts at a given node (often called the 'root' in
the context of trees or the 'starting vertex' in graphs) and explores all its neighboring nodes at the
present depth level before moving on to nodes at the next depth level.
Depth-First Traversal (DFT), also known as Depth-First Search (DFS), is an algorithm for
traversing or searching tree or graph data structures. It starts at a given node and explores as far as
possible along each branch before backtracking.
a) Adjacency Matrix
BFT:-
Program
#include <stdio.h>
#include <stdlib.h>
int currentNode;
queue[++rear] = start;
visited[start] = 1;
currentNode = queue[front++];
visited[i] = 1;
int main() {
scanf("%d", &n);
scanf("%d", &graph[i][j]);
scanf("%d", &start);
BFT(graph, n, start);
return 0;
OUTPUT:-
01100
10110
11001
01000
00100
Result:-
This code implements Breadth-First Traversal for a graph represented by adjacency matrix.
DFT:-
Program:-
#include <stdio.h>
#include <stdlib.h>
visited[v] = 1;
DFTUtil(graph, n, i, visited);
int main() {
scanf("%d", &n);
scanf("%d", &graph[i][j]);
scanf("%d", &start);
DFT(graph, n, start);
return 0;
OUTPUT:-
01100
10110
11001
01000
00100
Result:-
This code implements Depth-First Traversal for a graph represented by adjacency matrix.
b) Adjacency Lists
BFT Program:-
#include <stdio.h>
#include <stdlib.h>
int vertex;
} Node;
int numVertices;
Node** adjLists;
} Graph;
int items[MAX];
int front;
int rear;
} Queue;
Node* createNode(int v) {
newNode->vertex = v;
newNode->next = NULL;
return newNode;
graph->numVertices = vertices;
graph->adjLists[i] = NULL;
return graph;
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
Queue* createQueue() {
Queue* q = malloc(sizeof(Queue));
q->front = -1;
q->rear = -1;
return q;
int isEmpty(Queue* q) {
printf("\nQueue is full!");
} else {
if (q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
int dequeue(Queue* q) {
int item;
if (isEmpty(q)) {
printf("\nQueue is empty!");
item = -1;
} else {
item = q->items[q->front];
q->front++;
return item;
Queue* q = createQueue();
visited[i] = 0;
visited[startVertex] = 1;
enqueue(q, startVertex);
while (!isEmpty(q)) {
while (temp) {
if (!visited[adjVertex]) {
visited[adjVertex] = 1;
enqueue(q, adjVertex);
temp = temp->next;
free(visited);
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
BFT(graph, start);
printf("\n");
return 0;
OUTPUT:-
Result:-
This code implements Breadth-First Traversal for a graph represented by adjacency lists. .
DFT Program;-
#include <stdio.h>
#include <stdlib.h>
int vertex;
} Node;
Node** adjLists;
} Graph;
Node* createNode(int v) {
newNode->vertex = v;
newNode->next = NULL;
return newNode;
graph->numVertices = vertices;
graph->adjLists[i] = NULL;
return graph;
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
while (temp) {
if (!visited[adjVertex]) {
temp = temp->next;
visited[i] = 0;
free(visited);
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
scanf("%d", &start);
DFT(graph, start);
printf("\n");
return 0;
OUTPUT:-
Result:-
This code implements Depth-First Traversal for a graph represented by adjacency lists.