public class SequentialSolver
extends java.util.concurrent.RecursiveTask<java.util.List<java.lang.Integer>>
SequentialSolver
implements a solver for
Maze
objects using a single-thread depth-first search.
Even though SequentialSolver
is implemented as a
RecursiveTask
, it is purely sequential. Method
compute
returns a solution consisting of a list of
node identifiers in the maze that lead from the start node to a
goal.
Depth-first search is implemented using a stack of
frontier
nodes — giving the nodes to be explored
next in depth-first order. Visited nodes are added to a set
visited
. For each visited node,
predecessor
keeps track of the other node adjacent to
the visited node that has been visited just before it. Method
pathFromTo
reconstructs a path by following the
precedessor
relation backwards.
Modifier and Type | Field and Description |
---|---|
protected int |
forkAfter
Number of steps (nodes to be visited) before forking.
|
protected java.util.Stack<java.lang.Integer> |
frontier
The nodes in the maze to be visited next.
|
protected Maze |
maze
The maze being searched.
|
protected java.util.Map<java.lang.Integer,java.lang.Integer> |
predecessor
If
(m -> n) is in precedessor , then
the node with identifier n has been first visited
from its neighbor node with identifier m during
the search. |
protected int |
start
The identifier of the node in the maze from where the search
starts.
|
protected java.util.Set<java.lang.Integer> |
visited
Set of identifiers of all nodes visited so far during the
search.
|
Constructor and Description |
---|
SequentialSolver(Maze maze)
Creates a solver that searches in
maze from the
start node to a goal. |
Modifier and Type | Method and Description |
---|---|
java.util.List<java.lang.Integer> |
compute()
Searches for and returns the path, as a list of node
identifiers, that goes from the start node to a goal node in
the maze.
|
protected void |
initStructures()
Initializes
visited , predecessor , and
frontier with empty data structures for sequential
access. |
protected java.util.List<java.lang.Integer> |
pathFromTo(int from,
int to)
Returns the connected path, as a list of node identifiers, that
goes from node
from to node to
following the inverse of relation predecessor . |
adapt, adapt, adapt, cancel, compareAndSetForkJoinTaskTag, complete, completeExceptionally, fork, get, get, getException, getForkJoinTaskTag, getPool, getQueuedTaskCount, getSurplusQueuedTaskCount, helpQuiesce, inForkJoinPool, invoke, invokeAll, invokeAll, invokeAll, isCancelled, isCompletedAbnormally, isCompletedNormally, isDone, join, peekNextLocalTask, pollNextLocalTask, pollTask, quietlyComplete, quietlyInvoke, quietlyJoin, reinitialize, setForkJoinTaskTag, tryUnfork
protected Maze maze
protected int forkAfter
0
in SequentialSolver
, which
means no forking.protected java.util.Set<java.lang.Integer> visited
protected java.util.Map<java.lang.Integer,java.lang.Integer> predecessor
(m -> n)
is in precedessor
, then
the node with identifier n
has been first visited
from its neighbor node with identifier m
during
the search.protected java.util.Stack<java.lang.Integer> frontier
protected int start
public SequentialSolver(Maze maze)
maze
from the
start node to a goal.maze
- the maze to be searchedprotected void initStructures()
visited
, predecessor
, and
frontier
with empty data structures for sequential
access.public java.util.List<java.lang.Integer> compute()
null
.compute
in class java.util.concurrent.RecursiveTask<java.util.List<java.lang.Integer>>
null
if such a path cannot
be foundprotected java.util.List<java.lang.Integer> pathFromTo(int from, int to)
from
to node to
following the inverse of relation predecessor
. If
such a path cannot be reconstructed from
predecessor
, the method returns null
.from
- the identifier of the initial node on the pathto
- the identifier of the final node on the pathfrom
to
to
if such a path can be reconstructed from
predecessor
; null
otherwise