Elementary Graph Algorithms - Breadth First Search
Treasure Island - Problem Definition
$$\begin{bmatrix}
O & O & O & O \\
D & O & D & O \\
O & O & O & O \\
X & D & D & O \\
\end{bmatrix}$$
$$\begin{bmatrix}
O & O & O & O \\
D & O & D & O \\
O & O & O & O \\
O & D & D & O \\
O & D & X & O \\
\end{bmatrix}$$
Treasure Island - Approach
We use a variant of the BFS algorithm to find the minimum distance to reach the treasure. We maintain a FIFO queue data structure to keep discovered vertices whose adjacency list is yet to be scanned. We use an array named $m$ to get hold of the $x$ and $y$ coordinate changes for each move: up, down, left and right. We have two procedures, $FIND-TREASURE$ first calculates shortest path distances in an $m \cdot n$ matrix named $d$ and the procedure $PRINT-PATH$ uses this matrix in turn to print the blocks in an optimal solution. Also note that the procedure $PRINT-PATH$ starts its job from the block where the treasure is buried, hence the treasure coordinates are passed in as an argument to the routine. The algorithms that follow describe our approach of finding the shortest path more precisely.
**Input:** Map area represented by an $m \cdot n$ matrix
**Output:** Minimum number of steps to reach the treasure along with the corresponding route.
$FIND-TREASURE(G)$
$\phantom{{}++{}}$ $rl \gets G.length$
$\phantom{{}++{}}$ $cl \gets G[0].length$
$\phantom{{}++{}}$ let $d$ be a $rl \cdot cl$ matrix, each entry initialized to $\infty$
$\phantom{{}++{}}$ let $q$ be an empty FIFO queue
$\phantom{{}++{}}$ let $m$ be an array containing the finite set of moves
$\phantom{{}++{}}$ let $r$ be the treasures row position initialized to $-1$
$\phantom{{}++{}}$ let $c$ be the treasures column position initialized to $-1$
$\phantom{{}++{}}$ $q.enqueue(\{0, 0\})$
$\phantom{{}++{}}$ $d[0][0] = 0$
$\phantom{{}++{}}$ `while` $q$ is not empty
$\phantom{{}++{}}$ $\phantom{{}++{}}$ $u \gets q.dequeue()$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ `for` `each` $p \in m$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ $i \gets u[0] + p[0]$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ $j \gets u[1] + p[1]$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ `if` $i \ge 0$ and $i \lt rl$ and $j \ge 0$ and $j \lt cl$ and $d[i][j] == \infty$ and $G[i][j] \ne D$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ `if` $G[i][j] == X$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ $r \gets i$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ $c = j$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ $d[i][j] \gets d[u[0]][u[1]] + 1$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ $q.enqueue(\{i, j\})$
$\phantom{{}++{}}$ `if` $r == -1$ and $c == -1$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ throw error
$\phantom{{}++{}}$ `print` "The minimum route takes " d[r][c] " steps."
$\phantom{{}++{}}$ $PRINT-PATH(d, r, c, m)$
**Input:** An $m \cdot n$ matrix containing shortest path distances calculated at step 1, $x$ and $y$ coordinates of the treasure and the finite set of moves $m$.
**Output:** Shortest route to reach the treasure.
$PRINT-PATH(d, i, j, m)$
$\phantom{{}++{}}$ `if` $i == 0$ and $j == 0$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ `print` "(" i ", " j ")"
$\phantom{{}++{}}$ `else`
$\phantom{{}++{}}$ $\phantom{{}++{}}$ $r \gets -1$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ $c \gets -1$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ $s \gets \infty$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ `for` `each` $p \in m$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ $k \gets i + p[0]$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ $l \gets j + p[1]$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ `if` $k \ge 0$ and $k \lt d.length$ and $l \ge 0$ and $l \lt d[0].length$ and $d[k][l] \lt s$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ $r \gets k$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ $c \gets l$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ $s \gets d[k][l]$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ `if` $r == -1$ and $c == -1$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ `print` "path doesn't exist"
$\phantom{{}++{}}$ $\phantom{{}++{}}$ `else`
$\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ $PRINT-PATH(d, r, c, m)$
$\phantom{{}++{}}$ $\phantom{{}++{}}$ $\phantom{{}++{}}$ `print` "(" i ", " j ")"
Comments
Post a Comment