Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pathfinding Algorithm

Pathfinding Algorithm

A graph's pathfinding algorithm can also be implemented via depth-first traversal dfs, finding paths from the starting point s to other points in the graph graph. In the implementation class from the previous section, add a global variable from array to record the path, where from[i] represents the previous node of i on the searched path.

First the constructor initializes the initial conditions of the pathfinding algorithm, from = new int[G.V()] and from = new int[G.V()], and sets default values in the loop: the visited array is all false, the from array is all -1. Then the starting node is processed recursively with dfs.

...

// Constructor, pathfinding algorithm, find paths from point s to other points in graph graph

public

Path

(

Graph graph,

int

s

)

{

   

// Algorithm initialization

    G

=

graph

;

   

assert

s

=

0

&&

s

<

G.

V

(

)

;

    visited

=

new

boolean

[

G.

V

(

)

]

;

    from

=

new

int

[

G.

V

(

)

]

;

   

for

(

int

i

=

0

;

i

<

G.

V

(

)

;

i

++

)

{

        visited

[

i

]

=

false

;

        from

[

i

]

=

1

;

   

}

   

this

.

s

=

s

;

   

// Pathfinding algorithm

    dfs

(

s

)

;

}

...

So to determine whether there is a path from point s to point w, just query the corresponding value in the visited array.

...

boolean

hasPath

(

int

w

)

{

   

assert

w

=

0

&&

w

<

G.

V

(

)

;

   

return

visited

[

w

]

;

}

...

To get the specific path from point s to point w, we use the path method. First determine connectivity by calling the hasPath method. As can be seen from the constructor, all paths can be found simply by tracing back through the from array.

...

Vector

<

Integer

path

(

int

w

)

{

assert

hasPath

(

w

)

;

    Stack

<

Integer

s

=

new

Stack

<

Integer

(

)

;

   

// Find the path from s to w by tracing back through the from array, store it in a stack

   

int

p

=

w

;

   

while

(

p

!=

1

)

{

        s.

push

(

p

)

;

        p

=

from

[

p

]

;

   

}

   

// Take elements out of the stack one by one to obtain the path from s to w in order

    Vector

<

Integer

res

=

new

Vector

<

Integer

(

)

;

   

while

(

!

s.

empty

(

)

)

        res.

add

(

s.

pop

(

)

)

;

   

return

res

;

}

...

Java Example Code

Source code package download:Download

src/runoob/graph/Path.java file code:

package

runoob.graph

;

import

runoob.graph.read.Graph

;

import

java.util.Stack

;

import

java.util.Vector

;

/**
 * Pathfinding
 */

public

class

Path

{

   

// Reference to the graph

   

private

Graph G

;

   

// Starting point

   

private

int

s

;

   

// Record whether a node is visited during dfs

   

private

boolean

[

]

visited

;

   

// Record the path, from[i] represents the previous node of i on the searched path

   

private

int

[

]

from

;

   

// Depth-first traversal of the graph

   

private

void

dfs

(

int

v

)

{

        visited

[

v

]

=

true

;

       

for

(

int

i

:

G.

adj

(

v

)

)

           

if

(

!

visited

[

i

]

)

{

                from

[

i

]

=

v

;

                dfs

(

i

)

;

           

}

   

}

   

// Constructor, pathfinding algorithm, find paths from point s to other points in graph graph

   

public

Path

(

Graph graph,

int

s

)

{

       

// Algorithm initialization

        G

=

graph

;

       

assert

s

=

0

&&

s

<

G.

V

(

)

;

        visited

=

new

boolean

[

G.

V

(

)

]

;

        from

=

new

int

[

G.

V

(

)

]

;

       

for

(

int

i

=

0

;

i

<

G.

V

(

)

;

i

++

)

{

            visited

[

i

]

=

false

;

            from

[

i

]

=

1

;

       

}

       

this

.

s

=

s

;

       

// Pathfinding algorithm

        dfs

(

s

)

;

   

}

   

// Query whether there is a path from point s to point w

   

boolean

hasPath

(

int

w

)

{

       

assert

w

=

0

&&

w

<

G.

V

(

)

;

       

return

visited

[

w

]

;

   

}

   

// Query the path from point s to point w, stored in vec

    Vector

<

Integer

path

(

int

w

)

{

assert

hasPath

(

w

)

;

        Stack

<

Integer

s

=

new

Stack

<

Integer

(

)

;

       

// Find the path from s to w by tracing back through the from array, store it in a stack

       

int

p

=

w

;

       

while

(

p

!=

1

)

{

            s.

push

(

p

)

;

            p

=

from

[

p

]

;

       

}

       

// Take elements out of the stack one by one to obtain the path from s to w in order

        Vector

<

Integer

res

=

new

Vector

<

Integer

(

)

;

       

while

(

!

s.

empty

(

)

)

            res.

add

(

s.

pop

(

)

)

;

       

return

res

;

   

}

   

// Print the path from point s to point w

   

void

showPath

(

int

w

)

{

       

assert

hasPath

(

w

)

;

        Vector

<

Integer

vec

=

path

(

w

)

;

       

for

(

int

i

=

0

;

i

<

vec.

size

(

)

;

i

++

)

{

           

System

.

out

.

print

(

vec.

elementAt

(

i

)

)

;

           

if

(

i

==

vec.

size

(

)

1

)

               

System

.

out

.

println

(

)

;

           

else

               

System

.

out

.

print

(

" -> "

)

;

       

}

   

}

}