|
ADS
Algorithms and Data Structures
|
The graph of a grid map, beyond the book. More...
Files | |
| file | ADS/graph/GridGraph.h |
| The graph of a grid map, beyond the book [see Graph.h and Grid.h]: the cells of a \( \mathit{rows} \times \mathit{cols} \) map, the cell \( ( r , c ) \) the node \( u = r \cdot \mathit{cols} + c \), each cell free or blocked, and the moves to the four sides of a cell or also to its four corners. | |
Classes | |
| class | ADS::GridGraph< W, C, Policy > |
| the implicit graph of a grid map, the cells of cost \( C \) and the weights of the moves of type \( W \), whose changes of the costs are told through the subject policy Policy [see Modification.h] More... | |
Enumerations | |
| enum class | ADS::GridMoves { ADS::GridMoves::FOUR , ADS::GridMoves::EIGHT } |
| the moves of a GridGraph More... | |
| enum class | ADS::Corners { ADS::Corners::CUT , ADS::Corners::NO_SQUEEZE , ADS::Corners::NO_CUT } |
| when a move to a corner of a cell is allowed, the corner free More... | |
| enum class | ADS::MoveCost { ADS::MoveCost::ENTERED , ADS::MoveCost::LEFT } |
| which cell of a move of a GridGraph weighs it More... | |
Functions | |
| template<class W, class C, class P> | |
| std::vector< int > | ADS::gridDistances (const GridGraph< W, C, P > &g, const std::vector< int > &S) |
| the distance in moves of each cell of the grid \( g \) from the nearest of the nodes \( S \), INF if none reaches it or if the cell is blocked; \( D[ u ] \) for the cell \( u \) | |
| template<class W = int> | |
| W | ADS::manhattan (GridCell a, GridCell b, W straight=W(1)) |
| the distance of Manhattan between the cells \( a \) and \( b \), \( \mathit{straight} \cdot ( | \Delta r | + | \Delta c | ) \), the least cost of a path of moves to the sides of cost \( \mathit{straight} \) | |
| template<class W> | |
| W | ADS::octile (GridCell a, GridCell b, W straight, W diagonal) |
| the octile distance between the cells \( a \) and \( b \), \( \mathit{straight} \cdot ( M - m ) + \mathit{diagonal} \cdot m \) with \( M \) and \( m \) the larger and the smaller of \( | \Delta r | \) and \( | \Delta c | \), the least cost of a path of moves to the sides and to the corners | |
| template<class W = int> | |
| W | ADS::chebyshev (GridCell a, GridCell b, W straight=W(1)) |
| the distance of Chebyshev between the cells \( a \) and \( b \), \( \mathit{straight} \cdot \max\{ | \Delta r | , | \Delta c | \} \), the octile distance when a move to a corner costs as one to a side (the king) | |
| template<class W, class C, class P> | |
| W | ADS::gridHeuristic (const GridGraph< W, C, P > &g, int u, int t) |
| a lower bound on the cost of a path from \( u \) to \( t \) in \( g \), consistent, for the A* of Hart, Nilsson and Raphael: the least cost \( \gamma_{\mathrm{min}} \) of a cell times manhattan() for four moves and times octile() for eight, the latter with \( \min\{ \mathit{diagonal} , 2 \, \mathit{straight} \} \) as the cost of a corner; the same bound under both values of MoveCost, since a move weighs at least \( \gamma_{\mathrm{min}} \) times its unit cost whichever of its two cells pays (see Graphs on grid maps) | |
| template<class W = double, class C = unsigned char> | |
| GridGraph< W, C > | ADS::readGridMap (std::istream &in, GridMoves moves=GridMoves::EIGHT, Corners corners=Corners::NO_CUT) |
| the grid of a map in the format of the benchmarks of Sturtevant: the lines type octile, height \( h \), width \( w \) and map, then \( h \) lines of \( w \) characters, of which the dot, G and S are free cells of cost 1 and the others (O, T, W and the at sign) are blocked | |
The graph of a grid map, beyond the book.
The definitions, the invariants and the costs of the area are in Theory of graphs.
|
strong |
|
strong |
the moves of a GridGraph
| Enumerator | |
|---|---|
| FOUR | to the four sides of a cell |
| EIGHT | to the sides and to the four corners |
|
strong |
which cell of a move of a GridGraph weighs it
| Enumerator | |
|---|---|
| ENTERED | the cell entered: \( ( u , v ) \) weighed by \( v \) |
| LEFT | the cell left: \( ( u , v ) \) weighed by \( u \) |
the distance of Chebyshev between the cells \( a \) and \( b \), \( \mathit{straight} \cdot \max\{ | \Delta r | , | \Delta c | \} \), the octile distance when a move to a corner costs as one to a side (the king)
| std::vector< int > ADS::gridDistances | ( | const GridGraph< W, C, P > & | g, |
| const std::vector< int > & | S ) |
the distance in moves of each cell of the grid \( g \) from the nearest of the nodes \( S \), INF if none reaches it or if the cell is blocked; \( D[ u ] \) for the cell \( u \)
In pseudocode (the visit in breadth of section 9.5, from all the sources at once, as gridDistances() of Grid.h):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{gridDistances}( G , S ) \cr 2 & \quad D[ u ] \gets \infty \text{ for every } u \in [ 0 , \mathrm{bound}() );\ Q \gets () \cr 3 & \quad \textbf{foreach}\ s \in S \text{ with } D[ s ] = \infty \ \textbf{do}\ D[ s ] \gets 0;\ \mathrm{enqueue}( Q , s )\quad \triangleright\ \text{a source given twice counts once} \cr 4 & \quad \textbf{while}\ Q \ne \emptyset\ \textbf{do} \cr 5 & \qquad u \gets \mathrm{dequeue}( Q ) \cr 6 & \qquad \textbf{foreach}\ v \in G.\mathrm{adj}( u ) \text{ with } D[ v ] = \infty\ \textbf{do}\ D[ v ] \gets D[ u ] + 1;\ \mathrm{enqueue}( Q , v ) \cr 7 & \quad \textbf{return}\ D \end{array}} \]
| W ADS::gridHeuristic | ( | const GridGraph< W, C, P > & | g, |
| int | u, | ||
| int | t ) |
a lower bound on the cost of a path from \( u \) to \( t \) in \( g \), consistent, for the A* of Hart, Nilsson and Raphael: the least cost \( \gamma_{\mathrm{min}} \) of a cell times manhattan() for four moves and times octile() for eight, the latter with \( \min\{ \mathit{diagonal} , 2 \, \mathit{straight} \} \) as the cost of a corner; the same bound under both values of MoveCost, since a move weighs at least \( \gamma_{\mathrm{min}} \) times its unit cost whichever of its two cells pays (see Graphs on grid maps)
In pseudocode:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{gridHeuristic}( G , u , t ) \cr 2 & \quad \textbf{if}\ G \text{ has four moves }\ \textbf{then return} \ \gamma_{\mathrm{min}} \cdot \mathrm{manhattan}( u , t , \mathit{straight} ) \cr 3 & \quad \textbf{return}\ \gamma_{\mathrm{min}} \cdot \mathrm{octile}( u , t , \mathit{straight} , \min\{ \mathit{diagonal} , 2 \, \mathit{straight} \} ) \end{array}} \]
the distance of Manhattan between the cells \( a \) and \( b \), \( \mathit{straight} \cdot ( | \Delta r | + | \Delta c | ) \), the least cost of a path of moves to the sides of cost \( \mathit{straight} \)
the octile distance between the cells \( a \) and \( b \), \( \mathit{straight} \cdot ( M - m ) + \mathit{diagonal} \cdot m \) with \( M \) and \( m \) the larger and the smaller of \( | \Delta r | \) and \( | \Delta c | \), the least cost of a path of moves to the sides and to the corners
| GridGraph< W, C > ADS::readGridMap | ( | std::istream & | in, |
| GridMoves | moves = GridMoves::EIGHT, | ||
| Corners | corners = Corners::NO_CUT ) |
the grid of a map in the format of the benchmarks of Sturtevant: the lines type octile, height \( h \), width \( w \) and map, then \( h \) lines of \( w \) characters, of which the dot, G and S are free cells of cost 1 and the others (O, T, W and the at sign) are blocked
In pseudocode:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{readGridMap}( \mathit{in} ) \cr 2 & \quad \text{read the header: } h \text{ and } w \cr 3 & \quad \textbf{for}\ r \gets 0\ \textbf{to}\ h - 1\ \textbf{do} \cr 4 & \qquad \textbf{for}\ c \gets 0\ \textbf{to}\ w - 1\ \textbf{do} \ \mathit{cost}[ r w + c ] \gets 1\ \textbf{if}\ \text{the } \text{character is free, else } 0 \cr 5 & \quad \textbf{return}\ \mathrm{GridGraph}( h , w , \mathit{cost} ) \end{array}} \]