Bilal Dsa Lab
Bilal Dsa Lab
Description:
• PR_ENQUEUE
• PR_DEQUEUE
• Removes the element at the front of the queue and returns it.
• If the queue is empty, it returns a placeholder element and prints an error message.
• Handles the case where the last element is removed by setting both front and rear to null.
• PR_ISEMPTY
Task 02: Find the Shortest Path in Graphs Using BFS and Queues.
Code:
Description:
The function find_shortest_path implements a Breadth-First Search (BFS) algorithm to find the
shortest path in a grid from a source cell (src_x, src_y) to a destination cell (dst_x, dst_y), ensuring
traversal is restricted to cells with a value of 0. It first validates the source and destination
coordinates, returning -1 if they are invalid or inaccessible. Using two arrays (dx and dy), it defines
four possible movement directions (up, down, left, and right). A `visited` array ensures cells are
not revisited, and a queue is used to explore cells layer by layer, where each queue entry contains
the cell's coordinates and the path cost to reach it. The algorithm explores neighboring cells and
stops once the destination is reached, returning the path cost. If no path exists, it returns -1. This
function is efficient for finding the shortest path in unweighted grid-based scenarios.
Output:
Conclusion:
In this lab, we implemented a breadth-first search (BFS) algorithm to find the shortest path in a
grid, where only cells marked with 0 are traversable. The program successfully navigated the
grid, using a queue to explore neighboring cells and determining the shortest path or returning -1
if no path existed. The lab highlighted the importance of input validation and the use of auxiliary
arrays to track visited cells. Overall, it demonstrated BFS as an effective method for solving
pathfinding problems in grid-based environments.