0% found this document useful (0 votes)
90 views6 pages

Content Beyond Syllabus

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views6 pages

Content Beyond Syllabus

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Ex. No.

Content Beyond Syllabus


Date: Applications of Queue ADTs – Graph Traversal - BFS

AIM:

To implement Breadth-First Search (BFS) traversal of a graph using C language.

ALGORITHM:

1. Start the Program:


o Include necessary header files.
o Define constants and global variables for the queue, adjacency matrix,
visited array, and number of vertices.
2. Initialize the Queue:
o Define enqueue function to add a vertex to the queue.
o Define dequeue function to remove a vertex from the queue.
3. Breadth-First Search (BFS):
o Define bfs function:
 Initialize the queue with the start vertex.
 Mark the start vertex as visited.
 While the queue is not empty:
 Dequeue a vertex and print it.
 For each adjacent vertex of the dequeued vertex:
 If the vertex is not visited, enqueue it and mark it as
visited.
4. Main Function:
o Input the number of vertices.
o Input the adjacency matrix.
o Initialize the visited array to 0.
o Input the starting vertex.
o Call the bfs function with the starting vertex.
5. Stop the Program.

THEORY:

Breadth-First Search (BFS):

Breadth-First Search (BFS) is an algorithm used for traversing or searching tree or graph
data structures. It starts at a selected node (known as the "start vertex") and explores all
its neighboring nodes at the present depth before moving on to nodes at the next depth
level. BFS is particularly useful for finding the shortest path in an unweighted graph.

Key Concepts:

 Queue-Based Traversal: BFS uses a queue to keep track of vertices to be


explored. Nodes are enqueued when they are discovered and dequeued when they
are processed.
 Level Order Traversal: BFS visits nodes level by level. This makes it ideal for
problems where finding the shortest path or the minimum number of steps is
required.
 Visited Array: This array keeps track of which vertices have been visited to
prevent reprocessing and infinite loops.

Steps in BFS:

1. Initialization:
o Start by marking the start vertex as visited and enqueue it.
2. Processing:
o Dequeue a vertex and process it (typically involves printing or performing
some operations).
o For each adjacent vertex of the dequeued vertex, if it has not been visited,
mark it as visited and enqueue it.
3. Termination:
o Continue this process until the queue is empty, meaning all reachable
vertices from the start vertex have been visited.

Advantages of BFS:

 Shortest Path: In an unweighted graph, BFS finds the shortest path from the
start vertex to any other vertex.
 Complete and Optimal: BFS is complete (finds a solution if one exists) and
optimal (finds the shortest path) in unweighted graphs.

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

#define MAX 100

int queue[MAX], front = -1, rear = -1;


int adj[MAX][MAX], visited[MAX];
int n; // Number of vertices in the graph

void enqueue(int vertex) {


if (rear == MAX - 1)
printf("Queue Overflow\n");
else {
if (front == -1)
front = 0;
queue[++rear] = vertex;
}
}

int dequeue() {
int vertex;
if (front == -1 || front > rear) {
printf("Queue Underflow\n");
return -1;
} else {
vertex = queue[front++];
if (front > rear)
front = rear = -1;
return vertex;
}
}
void bfs(int startVertex) {
int i;
enqueue(startVertex);
visited[startVertex] = 1;

while (front != -1) {


int currentVertex = dequeue();
printf("Visited %d\n", currentVertex);

for (i = 1; i <= n; i++) {


if (adj[currentVertex][i] == 1 && !visited[i]) {
enqueue(i);
visited[i] = 1;
}
}
}
}

int main() {
int i, j, startVertex;

printf("Enter number of vertices: ");


scanf("%d", &n);

printf("Enter adjacency matrix:\n");


for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
scanf("%d", &adj[i][j]);
}
}

for (i = 1; i <= n; i++) {


visited[i] = 0;
}

printf("Enter the starting vertex: ");


scanf("%d", &startVertex);
bfs(startVertex);
return 0;
}
Ex. No. Content Beyond Syllabus
Date: Applications of Stack ADTs – Graph Traversal - DFS

AIM:

To implement Depth-First Search (DFS) traversal of a graph using C language.

ALGORITHM:

1. Start the Program:


o Include necessary header files.
o Define constants and global variables for the stack, adjacency matrix,
visited array, and number of vertices.
2. Initialize the Stack:
o Define push function to add a vertex to the stack.
o Define pop function to remove a vertex from the stack.
3. Depth-First Search (DFS):
o Define dfs function:
 Initialize the stack with the start vertex.
 Mark the start vertex as visited and print it.
 While the stack is not empty:
 Peek at the top vertex of the stack.
 For each adjacent vertex of the top vertex:
 If the vertex is not visited, push it onto the stack,
mark it as visited, and print it.
 If no adjacent unvisited vertex is found, pop the stack.
4. Main Function:
o Input the number of vertices.
o Input the adjacency matrix.
o Initialize the visited array to 0.
o Input the starting vertex.
o Call the dfs function with the starting vertex.
5. Stop the Program.

THEORY:

Depth-First Search (DFS):

Depth-First Search (DFS) is an algorithm used for traversing or searching tree or graph
data structures. It starts at a selected node (known as the "start vertex") and explores as
far as possible along each branch before backtracking. DFS uses a stack-based approach
for traversal.
Key Concepts:

 Stack-Based Traversal: DFS uses a stack to keep track of vertices to be


explored. Nodes are pushed onto the stack when they are discovered and popped
off when they are processed.
 Exploration: DFS explores as deeply as possible before backtracking. This
makes it suitable for problems where you need to explore all possibilities, such
as finding connected components.
 Visited Array: This array keeps track of which vertices have been visited to
avoid reprocessing and infinite loops.

Steps in DFS:

1. Initialization:
o Start by marking the start vertex as visited and push it onto the stack.
2. Processing:
o Peek at the top vertex of the stack.
o For each adjacent vertex of the top vertex, if it has not been visited, push it
onto the stack, mark it as visited, and print it.
3. Backtracking:
o If no adjacent unvisited vertex is found, pop the stack to backtrack and
continue the search.

Advantages of DFS:

 Space Efficiency: DFS can be more space-efficient compared to BFS, especially in


large graphs, as it stores only the path from the start node to the current node.
 Complete Search: DFS can be used to explore all nodes and edges in a graph. It
can be useful for solving problems that require exhaustive exploration.

PROGRAM

#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int stack[MAX], top = -1;
int adj[MAX][MAX], visited[MAX];
int n; // Number of vertices in the graph
void push(int vertex) {
if (top == MAX - 1)
printf("Stack Overflow\n");
else
stack[++top] = vertex;
}

int pop() {
if (top == -1) {
printf("Stack Underflow\n");
return -1;
} else {
return stack[top--];
}
}

void dfs(int startVertex) {


int i;
push(startVertex);
visited[startVertex] = 1;
printf("Visited %d\n", startVertex);
while (top != -1) {
int currentVertex = stack[top];
int found = 0;

for (i = 1; i <= n; i++) {


if (adj[currentVertex][i] == 1 && !visited[i]) {
push(i);
visited[i] = 1;
printf("Visited %d\n", i);
found = 1;
break;
}
}

if (!found)
pop();
}
}

int main() {
int i, j, startVertex;

printf("Enter number of vertices: ");


scanf("%d", &n);

printf("Enter adjacency matrix:\n");


for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
scanf("%d", &adj[i][j]);
}
}

for (i = 1; i <= n; i++) {


visited[i] = 0;
}
printf("Enter the starting vertex: ");
scanf("%d", &startVertex);
dfs(startVertex);
return 0;
}

You might also like