0% found this document useful (0 votes)
17 views18 pages

Final 3333

Uploaded by

Prasad Dhumale
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)
17 views18 pages

Final 3333

Uploaded by

Prasad Dhumale
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/ 18

School of Computing Science and Engineering

Academic Year:2024
Course Code: B22EF0305 Algorithms Lab Project Report
Semester & Batch: 4th A-2

Project Details:

Project Title: PATH FINDING

Place of Project: REVA UNIVERSITY, BENGALURU

Student Details:
D.DHINESH KUMAAR REDDY
Name: Sign:
9392026464
Mobile No:
[email protected]
Email-ID:
R22EF047
SRN:

Guide and Lab Faculty Members Details


Guide Name:
(The Faculty Sign:
Member Date:
Assigned)
Grade by Guide:
Name of Lab Co- Sign:
RFaculty 1 Date:
Name of Lab Co- Sign:
Faculty 2 Date:
Grade by Lab
Faculty Members
(combined)

SEE Examiners
Name of Sign:
Examiner 1: Date:
Name of Sign:
Examiner 2: Date:
Contents
1. Abstract 3

2. Introduction 3

3. Problem Statement. 4

4. Project overview. 4

4.1.Objectives

4.2.Goals

5. Implementation. 4

5.1.Problem analysis and description.

5.2.Modules identified.

5.3.Code with comments. 5-11

6. Output and results 12

7. Conclusions 13

8. References 13-14

School of Computer Science and Engineering Page 2


1. Abstract:
This program implements three fundamental graph algorithms in the C programming language:
Dijkstra's Algorithm, Depth-First Search (DFS), and the A* Algorithm. The program begins by
soliciting user input for the adjacency matrix of a graph, then applies Dijkstra's Algorithm to
compute the shortest paths from a specified source vertex to all other vertices. Subsequently, it
performs a Depth-First Traversal of the graph to visit all vertices. Lastly, the program allows the
user to input a grid representing a maze with obstacles, followed by specifying start and goal
nodes, upon which the A* Algorithm is applied to find the shortest path through the maze from the
start to the goal node. Each algorithm's execution time is recorded and displayed alongside the
corresponding output.

2. Introduction:
Graph algorithms form the backbone of many computational processes and have broad applications
across various fields such as network routing, geographical mapping, game development, and
artificial intelligence. These algorithms help in solving complex problems by providing efficient
methods to traverse, search, and find optimal paths in graphs and grids. This project aims to
develop a robust C program that implements three fundamental graph algorithms: Dijkstra's
Algorithm, Depth First Search (DFS), and the A* Algorithm.

Dijkstra's Algorithm is a widely-used algorithm designed to find the shortest path between nodes in
a weighted graph, where all edge weights are non-negative. This algorithm is particularly useful in
scenarios such as network routing, where it ensures that data packets travel the shortest possible
route between routers. Depth First Search (DFS) is another essential algorithm, used primarily for
graph traversal. It explores as far along a branch as possible before backtracking, making it useful
for applications such as finding connected components, performing topological sorting, and solving
puzzles where all potential solutions must be explored.
The A* Algorithm is an advanced pathfinding and graph traversal algorithm that combines the
benefits of Dijkstra's Algorithm with heuristic methods to enhance efficiency. By using heuristics,
A* can often find the shortest path more quickly than Dijkstra’s Algorithm alone, especially in
large grids or graphs. This algorithm is extensively used in artificial intelligence for games, where
characters need to navigate through complex environments, and in robotics, where efficient
pathfinding is crucial for navigation and obstacle avoidance.
The primary objective of this project is to provide a comprehensive and practical implementation
of these algorithms. Users will be able to input custom graphs and grids, execute the algorithms,
and observe the results along with the execution time. This hands-on approach will not only
demonstrate the practical utility of these algorithms but also provide insights into their performance
and efficiency. Through this project, users can better understand the intricacies of graph algorithms
and their real-world applications, thereby enhancing their problem-solving and computational
skills.

School of Computer Science and Engineering Page 3


3. Problem statement:
You are tasked with developing a C program that implements three different graph algorithms:
Dijkstra's algorithm for finding the shortest path in a weighted graph, Depth First Search (DFS)
for traversing a graph, and the A* algorithm for pathfinding in a grid. The program should take
various inputs from the user to configure the graph and grid, and then it should execute the
algorithms, displaying the results and timing the execution
4. Project overview:
4.1. Objectives:
• A Implement Dijkstra's Algorithm: To find the shortest path from a source vertex to all
other vertices in a weighted graph.
• Implement Depth First Search (DFS): To traverse a graph and visit all the vertices
reachable from a given starting vertex.
• Implement A Algorithm:* To find the shortest path in a grid from a start node to a goal
node using heuristics.
• User Input Handling: To allow users to input the graph and grid configurations.
• Result Display: To output the results of the algorithms, including the paths found and the
distances calculated.
• Performance Measurement: To measure and display the execution time of each
algorithm.
4.2.Goals: The goal of a project overview is to lay out the details of a project in a concise,
easy-to-understand manner that can be presented to clients, team members, and key
stakeholders.

5. Project Implementation.
5.1. Problem analysis and description.
This project addresses the problem of graph traversal and pathfinding in different scenarios:
• Shortest Path in Weighted Graph: Using Dijkstra's algorithm to find the shortest path
from a source vertex to all other vertices.
• Graph Traversal: Using DFS to explore all vertices of a graph from a given starting point.
• Pathfinding in Grid: Using A* algorithm to find the shortest path in a grid, considering
obstacles.
• These problems are common in network routing, game development, and various
optimization tasks
5.2. Modules identified:
Input Module: Handles user input for graph and grid configurations.
Dijkstra Module: Implements Dijkstra's algorithm.
School of Computer Science and Engineering Page 4
DFS Module: Implements the Depth First Search algorithm.
A Module:* Implements the A* algorithm.
Output Module: Displays the results and execution time.
5.3. Code with comments.
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <limits.h>

#define MAX_VERTICES 100

typedef struct {
int x, y;
} Node;

typedef struct {
Node parent;
double f, g, h;
} NodeData;

int graph[MAX_VERTICES][MAX_VERTICES];
bool visited[MAX_VERTICES];
int numVertices;

int minDistance(int dist[], bool sptSet[]) {


int min = INT_MAX, minIndex;

for (int v = 0; v < numVertices; v++) {


if (!sptSet[v] && dist[v] <= min) {
min = dist[v];
minIndex = v;
}
}
School of Computer Science and Engineering Page 5
return minIndex;
}

void printSolution(int dist[]) {


printf("Vertex Distance from Source\n");
for (int i = 0; i < numVertices; i++) {
printf("%d\t%d\n", i, dist[i]);
}
}

void dijkstra(int src) {


int dist[MAX_VERTICES];
bool sptSet[MAX_VERTICES];

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


dist[i] = INT_MAX;
sptSet[i] = false;
}

dist[src] = 0;

clock_t start, end;


start = clock();

for (int count = 0; count < numVertices - 1; count++) {


int u = minDistance(dist, sptSet);
sptSet[u] = true;

for (int v = 0; v < numVertices; v++) {


if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] <
dist[v]) {
dist[v] = dist[u] + graph[u][v];
}
}
}
School of Computer Science and Engineering Page 6
end = clock();

printSolution(dist);
printf(“Time Complexity:O(v2)”);
printf("Time taken: %f seconds\n", ((double)(end - start)) / CLOCKS_PER_SEC);
}

void dfs(int vertex) {


visited[vertex] = true;
printf("%d ", vertex);

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


if (graph[vertex][i] && !visited[i]) {
dfs(i);
}
}
}

double heuristic(Node current, Node goal) {


return sqrt((current.x - goal.x) * (current.x - goal.x) + (current.y - goal.y) * (current.y -
goal.y));
}

void astar(int grid[][MAX_VERTICES], Node start, Node goal, int rows, int cols) {
NodeData nodes[rows][cols];
bool closedList[rows][cols];

for (int i = 0; i < rows; i++) {


for (int j = 0; j < cols; j++) {
nodes[i][j].parent = (Node){-1, -1};
nodes[i][j].f = nodes[i][j].g = nodes[i][j].h = INFINITY;
closedList[i][j] = false;
}
}

School of Computer Science and Engineering Page 7


nodes[start.x][start.y].f = nodes[start.x][start.y].g = nodes[start.x][start.y].h = 0;

clock_t start_time = clock();

while (true) {
double minF = INFINITY;
Node current = {-1, -1};

for (int i = 0; i < rows; i++) {


for (int j = 0; j < cols; j++) {
if (!closedList[i][j] && nodes[i][j].f < minF) {
minF = nodes[i][j].f;
current = (Node){i, j};
}
}
}

if (minF == INFINITY) {
printf("No path found!\n");
return;
}

if (current.x == goal.x && current.y == goal.y) {


printf("Path found!\n");
Node path[rows * cols];
int pathLength = 0;

while (!(current.x == start.x && current.y == start.y)) {


path[pathLength++] = current;
current = nodes[current.x][current.y].parent;
}

printf("Path:\n");
for (int i = pathLength - 1; i >= 0; i--) {
printf("(%d,%d)\n", path[i].x, path[i].y);

School of Computer Science and Engineering Page 8


}

clock_t end_time = clock();


double elapsed_time = ((double)(end_time - start_time)) / CLOCKS_PER_SEC;
printf("Time taken: %.6f seconds\n", elapsed_time);

return;
}

closedList[current.x][current.y] = true;

for (int i = -1; i <= 1; i++) {


for (int j = -1; j <= 1; j++) {
if (i == 0 && j == 0) continue;
int newX = current.x + i;
int newY = current.y + j;
if (newX < 0 || newX >= rows || newY < 0 || newY >= cols || grid[newX][newY] == 1)
continue;

Node neighbor = {newX, newY};


double tentativeG = nodes[current.x][current.y].g + sqrt(i * i + j * j);
double tentativeF = tentativeG + heuristic(neighbor, goal);

if (tentativeF < nodes[neighbor.x][neighbor.y].f) {


nodes[neighbor.x][neighbor.y].f = tentativeF;
nodes[neighbor.x][neighbor.y].g = tentativeG;
nodes[neighbor.x][neighbor.y].h = heuristic(neighbor, goal);
nodes[neighbor.x][neighbor.y].parent = current;
}
}
}
}
}

int main() {
int rows, cols;
School of Computer Science and Engineering Page 9
printf("Enter the number of rows and columns for the adjacency matrix : ");
scanf("%d %d", &rows, &cols);
numVertices = rows;

printf("Enter the adjacency matrix for the graph (%d x %d):\n", rows, cols);
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numVertices; j++) {
scanf("%d", &graph[i][j]);
}
}

int srcVertex;
printf("Enter the source vertex for Dijkstra's algorithm: ");
scanf("%d", &srcVertex);

dijkstra(srcVertex);

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


visited[i] = false;
}

clock_t start_time, end_time;


double elapsed_time;

start_time = clock();

printf("\n\nDepth First Traversal:\n\n ");


for (int i = 0; i < numVertices; i++) {
if (!visited[i]) {
dfs(i);
}
}
printf("\n");

end_time = clock();

School of Computer Science and Engineering Page 10


elapsed_time = ((double)(end_time - start_time)) / CLOCKS_PER_SEC;
printf("Time taken: %.6f seconds\n", elapsed_time);

printf("\nEnter the number of rows and columns for the grid :\n ");
scanf("%d %d", &rows, &cols);
int grid[MAX_VERTICES][MAX_VERTICES];
printf("\nEnter the grid (0 for empty, 1 for obstacles):\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &grid[i][j]);
}
}
Node startNode, goalNode;
printf("Enter the start node coordinates (x y): ");
scanf("%d %d", &startNode.x, &startNode.y);
printf("Enter the goal node coordinates (x y): ");
scanf("%d %d", &goalNode.x, &goalNode.y);
astar(grid, startNode, goalNode, rows, cols);
return 0;
}

School of Computer Science and Engineering Page 11


6. Output and results

Algorithm Time Complexity Space Complexity

Dijkstra’s Algorithm O(E log V) O(V2)

Depth First Search(DFS) O(V+E) O(V2)

A* Algorithm O(E+VlogV) O(V)

School of Computer Science and Engineering Page 12


7. Conclusions:
In conclusion, this project offers a comprehensive implementation of three essential graph algorithms in
C: Dijkstra's Algorithm, Depth First Search (DFS), and the A* Algorithm. Each algorithm serves a
unique purpose and finds applications across various computational domains.
Dijkstra's Algorithm efficiently computes the shortest path in weighted graphs with non-negative edge
weights. Its applications range from network routing to geographical mapping, where finding the most
efficient route is crucial.
Depth First Search (DFS) is a fundamental graph traversal algorithm used to explore all vertices and
edges of a graph. Its versatility makes it valuable for tasks such as identifying connected components,
topological sorting, and solving puzzles.
The A* Algorithm combines the capabilities of Dijkstra's Algorithm with heuristic methods to efficiently
find the shortest path in grids. This makes it indispensable for AI applications in games and robotics,
where navigation through complex environments is essential.
By providing intuitive interfaces for custom graph and grid inputs and presenting results with execution
times, this project serves as a valuable resource for understanding and applying graph algorithms in
practical scenarios. It empowers users to explore the functionalities of these algorithms and their
significance in solving real-world problems

8. References:
G. Eason, B. Noble, and I. N. Sneddon, “On certain integrals of Lipschitz-Hankel type involving
products of Bessel functions,” Phil. Trans. Roy. Soc. London, vol. A247, pp. 529–551, April
1955.
J. Clerk Maxwell, A Treatise on Electricity and Magnetism, 3rd ed., vol. 2. Oxford: Clarendon,
1892, pp.68–73.
I. S. Jacobs and C. P. Bean, “Fine particles, thin films and exchange anisotropy,” in Magnetism,
vol. III, G. T. Rado and H. Suhl, Eds. New York: Academic, 1963, pp. 271–350.
K. Elissa, “Title of paper if known,” unpublished.
R. Nicole, “Title of paper with only first word capitalized,” J. Name Stand. Abbrev., in press.
Y. Yorozu, M. Hirano, K. Oka, and Y. Tagawa, “Electron spectroscopy studies on magneto-optical
media and plastic substrate interface,” IEEE Transl. J. Magn. Japan, vol. 2, pp. 740–741, August
1987 [Digests 9th Annual Conf. Magnetics Japan, p. 301, 1982].
M. Young, The Technical Writer’s Handbook. Mill Valley, CA: University Science, 1989.
NPTEL Course on “Design and Analysis of Algorithms”, Prof. Abhiram G Ranade, Prof. Ajit A
Diwan and Prof. Sundar Vishwanathan, https://nptel.ac.in/courses/106101060
“Introduction to Design and Analysis of Algorithms” by Anany Levitin,
2nd edition
http://160592857366.free.fr/joe/ebooks/ShareData/Anany%20Levitin%
20English
School of Computer Science and Engineering Page 13
https://www.researchgate.net/publication/276847633_A_Review_Report_on_Divide_and_Co
nquer_Sortin g_ Algorithm
https://www.ijsrp.org/research-paper-0813/ijsrp-p2014.pdf
https://iopscience.iop.org/article/10.1088/1742-6596/1566/1/012038/pdf

School of Computer Science and Engineering Page 14


School of Computer Science and Engineering Page 15
School of Computer Science and Engineering Page 16
Page 2
School of Computer Science and Engineering Page 3

You might also like