Mini Project AP

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Solving Mazes with Shortest Paths Using Depth-First Search

and Breadth-First Search

Project Report

Submitted by

Hardik Nitin Barhate (21BCS3183)


Yash Dubey (21BCS3064)
In partial fulfillment for the award of the degree of

Bachelor of Engineering

IN

Computer Science and Engineering

Chandigarh University

May 2023
BONAFIDE CERTIFICATE

Certified that this project report “Solving Mazes with Shortest Paths Using
Depth-First Search and Breadth-First Search” is the bonafide work of “Hardik
Nitin Barhate (21BCS3183), Yash Dubey (21BCS3064)” who carried out the
project work under my/our supervision.

SIGNATURE SIGNATURE

ASSOCIATE DIRECTOR SUPERVISOR

Submitted for the project viva-voce examination held on

INTERNAL EXAMINER EXTERNAL EXAMINER


TABLE OF CONTENTS

CHAPTER 1. INTRODUCTION .................................................................1


1. Client identification .....................................................................................................1

2. Identification of Problem .............................................................................................2

1. Identification of Tasks ..................................................................................................2

1. Timeline .......................................................................................................................3

2. Organization of the Report ..........................................................................................4

CHAPTER 2. DESIGN FLOW/PROCES .................................................5


1. Evaluation & Selection of Specifications/Features......................................................5

2. Design Constraints .......................................................................................................6

3. Analysis & features finalization subject to constraint ..................................................7

4. Design Section .............................................................................................................8

5. Implementation plan/methodology ..............................................................................8

CHAPTER 3. RESULTS ANALYSIS AND VALIDATION ......................9


1. Implementation of solution ...........................................................................................9

CHAPTER 4. CONCLUSION AND FUTURE WORK ............................12


1. Conclusion..................................................................................................................12

2. Future Work ................................................................................................................13

REFERENCE
CHAPTER 1.
INTRODUCTION

1. Client Identification/Need Identification/Identification of relevant


Contemporary issue

A solution that can effectively solve mazes with the shortest paths is needed for Pathfinding
Prowess. They need an algorithm or tool that can find the quickest path from a starting point
to a destination while navigating through intricate maze structures. This solution should be
flexible enough to accommodate many kinds of mazes in addition to offering the shortest
path, making it an all-purpose tool for their requirements.

The identification of a relevant contemporary issue for Pathfinding Prowess in solving mazes
with the shortest paths using Depth-First Search (DFS) and Breadth-First Search (BFS) is the
demand for efficient and scalable pathfinding algorithms in modern applications. As
technology advances and becomes an integral part of many industries, the need for
optimization and speed in pathfinding algorithms becomes increasingly important.

2. Identification of Problem
The problem is to develop a maze solver algorithm that can be used to solve mazes of any
size or complexity. The algorithm must be able to find the shortest path from the starting
point to the ending point of the maze.
To develop a maze solver algorithm that can solve mazes of any size or complexity and
find the shortest path from the starting point to the ending point of the maze, we can use a
combination of the following techniques:
I. Graph traversal algorithms: Graph traversal algorithms turn mazes into graphs for
navigation, using nodes for cells and edges for movements. Depth-first search and
breadth-first search explore paths to the end.
II. Dynamic programming: Dynamic programming breaks complex problems into smaller
ones, creating efficient maze solvers by reusing solutions of past subproblems.
3. Identification of Tasks
The following are some of the tasks involved in maze solving:
1. Maze representation: The maze must be represented in a way that can be processed
by the maze solver algorithm. This can be done by representing the maze as a graph,
where each node in the graph represents a cell in the maze and each edge in the graph
represents a possible movement between two cells.
2. Pathfinding: Use graph traversal algorithms like depth-first search or breadth-first
search to navigate from start to end.
3. Heuristic evaluation: Implement heuristics to estimate distances to the end, guiding
the search for shorter paths.
4. Path validation: Ensure the found path is valid, with each node properly connected
and no obstacles blocking the way
5. Path optimization: The maze solver algorithm may be able to optimize the path found
to make it shorter or more efficient. This can be done by using techniques such as
dynamic programming.
In addition to these tasks, maze solving algorithms may also need to perform other tasks,
such as:
1. Dealing with obstacles: The maze solver algorithm may need to be able to handle
obstacles in the maze. This can be done by representing obstacles as nodes in the
graph and assigning them a high cost. The algorithm will then avoid taking paths
that go through obstacles.
2. Dealing with multiple paths: The maze solver algorithm may need to be able to find
the shortest path to the ending point, even if there are multiple paths to the ending
point. This can be done by using a heuristic function that estimates the distance to

the ending point from each node. The algorithm will then prefer to take paths that
are estimated to be shorter.
3. Dealing with uncertainty: Utilize probabilistic reasoning for navigating with
incomplete or uncertain maze information.
Pathfinding Prowess provides a set of algorithms that can be used to perform all of the
tasks involved in maze solving, including dealing with obstacles, multiple paths, and
uncertainty.
4. Timeline
The following is a tentative timeline for developing the maze solver algorithm:
1. Week 1: Research existing maze solver algorithms.
2. Week 2: Design a new maze solver algorithm.
3. Week 3: Implement the maze solver algorithm in a programming language.
4. Week 4: Test the maze solver algorithm to ensure that it works correctly.

5. Organization of the Report


This report is organized as follows:
Section 1: Introduction
Highlights Pathfinding Prowess's need for efficient maze-solving, emphasizing real-
time navigation and scalability for a thorough exploration within a timeline.
Section 2: Design Flow
Discusses concept generation, specification selection despite constraints, analysis of
alternatives, and finalization of the best design with an execution plan.
Section 3: Results analysis and validation
Focuses on implementing the chosen design with modern tools for documentation
and testing, underlining the role of project management and communication.
Section 4:Conclusion and future work
Reflects on the project outcomes, discussing deviations and potential improvements.
Future directions are suggested, supported by references and a user manual that
includes visual guides. Highlights the project's achievements and contributions.
CHAPTER 2.

DESIGN FLOW/PROCESS

1. Evaluation & Selection of Specifications/Features


The selected specifications for the maze solver algorithm prioritize versatility, efficiency,
and user-friendliness. It is designed to tackle mazes of varying sizes and complexities,
ensuring it can be widely applied. Central to its functionality is finding the shortest path
quickly, avoiding unnecessary computational costs. Its ease of implementation and use
ensures accessibility for a broad user base, aligning with the project's goals and the
client's requirements.

2. Design Constraints
Several design constraints were taken into account during the development process:
1. Platform Compatibility: The algorithm must be implemented in a programming
language that is compatible with the chosen software or hardware platform to ensure
seamless integration.
2. Resource Efficiency: Efficiency is crucial, necessitating a design that minimizes
resource usage, be it processing power, memory, or energy consumption.
3. Maintenance and Updates: The algorithm should be designed with maintainability
in mind, allowing for easy updates and modifications as needed to adapt to changing
requirements and technological advancements.
These design constraints ensure that the final solution is not only technically viable
but also adaptable and sustainable in the long term, aligning with the project's
objectives and client's needs.
3. Analysis and Feature finalization subject to constraints
Analysis and feature finalization, taking design constraints into account, led to the
following decisions :
1. The algorithm will be implemented using the Python programming language, chosen
for its compatibility with the intended application.
2. The algorithm will utilize a depth-first search (DFS) or breadth-first search (BFS)
approach to solve mazes, tailored to the specific requirements and constraints of the
project.
3. The algorithm will undergo optimization to minimize resource consumption, ensuring
it operates efficiently within the defined constraints, such as memory and processing
power.

4. Design Flow
Design Flow using DFS and BFS Algorithms

1. Maze Initialization: Start by selecting a maze and defining the start and end points.
2. Parallel Exploration: Implement a hybrid approach where both DFS and BFS algorithms
work in parallel. DFS explores deeply while BFS progresses level by level.
3. Path Integration: Combine the results from both algorithms to determine the shortest path
to the destination.
4. Dynamic Selection: Depending on the maze's characteristics, dynamically choose which
algorithm to give priority to.
5. Optimization: Fine-tune the hybrid approach and add heuristics to enhance adaptability
and efficiency.
6. Validation: Rigorously test the algorithm on various maze types, analyzing resource
usage and performance.
7. Documentation: Create comprehensive design drawings and documentation.
Design Flow with A* Algorithm

1. Maze Initialization: Choose a maze and designate the start and end points.
2. A* Algorithm: Implement the A* algorithm, which combines elements of both BFS and
DFS, utilizing heuristics to navigate efficiently.
3. Pathfinding: Apply A* to find the shortest path, consistently maintaining real-time
adaptability to the maze's structure.
4. Optimization: Fine-tune the A* algorithm with various heuristic functions and
performance-enhancing techniques.
5. Validation: Thoroughly test the A* algorithm across different mazes to ensure optimal
resource usage and speed.
6. Documentation: Develop detailed design drawings and documentation.

The first design flow combines the DFS and BFS algorithms to create a dynamic, hybrid
approach, offering adaptability to varying maze structures. In contrast, the second design
flow employs the A* algorithm, incorporating heuristic elements to efficiently navigate
mazes and optimize the shortest path. Both design approaches aim to meet the project's
objectives and constraints while offering diverse solutions for maze solving.
5. Design selection
The choice of Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms
for the maze-solving project is a strategic decision that leverages their combined
strengths. DFS is valued for its memory efficiency, suitable for limited resources,
while BFS ensures the identification of the shortest path, crucial for effectiveness.
Employing these algorithms together allows for a versatile and adaptive maze solver,
capable of handling various maze configurations. This hybrid approach optimally
balances resource management and operational speed, aligning with the project's goals
for an efficient, adaptable, and resource-conscious solution.

6. Implementation plan/methodology
Algorithm-
For DFS maze solving, use a stack to explore paths: add the start position to the stack,
then loop through, popping positions off to explore directions. Mark positions visited
to avoid repeats. If the goal is reached, return true; if the stack empties without finding
the goal, return false.

For BFS maze solving, use a queue: start by enqueuing the starting position. Loop
through, dequeuing positions to explore surrounding areas, marking each as visited to
prevent revisiting. If the goal is found, return true; if the queue empties first, the maze
can't be solved.
CHAPTER 3.

RESULT ANALYSIS AND VALIDATION

1. Implementation of solution

Pathfinding is the process of finding the shortest or most optimal path between two points in a
graph. It is a fundamental problem in many fields, including robotics, navigation, and game
development.

There are many different pathfinding algorithms, but two of the most common are depth-first
search (DFS) and breadth-first search (BFS). Both of these algorithms can be used to solve
mazes, but they have different strengths and weaknesses.

1. Depth-First Search (DFS)

DFS is a recursive algorithm that works by exploring all possible paths from a starting node
until it reaches the goal node or finds that there are no more paths to explore. DFS is a relatively
simple algorithm to implement, but it can be inefficient for large mazes, as it can explore many
dead ends.

To solve a maze using DFS, the algorithm would start at the starting node and then
recursively explore all of its neighbors. If a neighbor leads to a dead end, the algorithm would
backtrack and try another neighbor. This process would continue until the goal node is
reached or all possible paths have been explored.

2. Breadth-First Search (BFS)


Actually, the Breadth-First Search (BFS) algorithm is not recursive by nature; it's an iterative
algorithm that explores a graph level by level, making it particularly efficient for finding the
shortest path in large mazes. To solve a maze using BFS, it starts at the initial node, adding all
adjacent nodes to a queue. It then dequeues the first node to explore its neighbors, adding them
to the queue in turn. Dead-end paths are ignored. This process repeats until the goal is found or
the queue empties, efficiently ensuring the shortest path to the destination.
The following figure shows how DFS and BFS would solve a simple maze:

DFS path: Start -> A -> B -> C -> D -> Goal

BFS path: Start -> A -> B -> C -> D -> Goal

Implementation

class Maze:

def init (self, grid):

self.grid = grid

self.start = (0, 0)

self.goal = (len(grid) - 1, len(grid[0]) - 1)


def is_valid_move(self, x, y):

return 0 <= x < len(self.grid) and 0 <= y < len(self.grid[0]) and self.grid[x][y] == 1

def solve_dfs(self):

stack = [self.start]

visited = set()

while stack:

x, y = stack.pop()

if (x, y) == self.goal:

return True

if (x, y) not in visited:

visited.add((x, y))

for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:

new_x = x + dx

new_y = y + dy

if self.is_valid_move(new_x, new_y):

stack.append((new_x, new_y))

return False

def solve_bfs(self):

queue = [self.start]
visited = set()

while queue:

x, y = queue.pop(0)

if (x, y) == self.goal:

return True

if (x, y) not in visited:

visited.add((x, y))

for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:

new_x = x + dx

new_y = y + dy

if self.is_valid_move(new_x, new_y):

queue.append((new_x, new_y))

return False

Results:
CHAPTER 4.

CONCLUSION AND FUTURE WORK

1. Conclusion
This report discusses solving mazes using Depth-First Search (DFS) and Breadth-First
Search (BFS). DFS, a recursive algorithm, explores paths fully before moving on, being
space-e cient but poten ally slow for large mazes. In contrast, BFS, an itera ve
algorithm, examines paths level by level, trading o space e ciency for faster
solu ons in larger mazes. Thus, DFS suits smaller mazes or when speed is crucial,
while BFS is preferred for larger mazes or nding the shortest path.

2. Future work
In future, we would like to work on the following topics:

1. Develop a more e cient maze solver algorithm.


2. Develop hybrid path- nding algorithms that combine the strengths of both BFS and
DFS.
3. Applying path- nding algorithms to real world problems like naviga on and routing.
4. Develop path nding algorithms that are scalable and efficient.

REFERENCES

https://en.wikipedia.org/wiki/Depth-first_search https://web.eecs.utk.edu/~jplank/
plank/classes/cs302/Labs/Lab7/ https://medium.com/swlh/solving-mazes-with-depth-
first-search-e315771317ae https://medium.com/@ben.lafferty/simple-sudoku-with-
backtracking-bb4813ddabb1
ti
ffi
fi
fi
ffi
fi
ti
fi
ff
ti
ffi
ti

You might also like