Download code

Jump to: navigation, search

Back to Depth-first_search_(Java)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

Vertex.java

  1 /* The authors of this work have released all rights to it and placed it
  2 in the public domain under the Creative Commons CC0 1.0 waiver
  3 (http://creativecommons.org/publicdomain/zero/1.0/).
  4 
  5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 12 
 13 Retrieved from: http://en.literateprograms.org/Depth-first_search_(Java)?oldid=11979
 14 */
 15 
 16 import java.util.List;
 17 import java.util.ArrayList;
 18 import java.util.Stack;
 19 
 20 
 21 interface GoalFunction<T>
 22 {
 23     boolean evaluate(Vertex<T> o);
 24 }
 25 
 26 public class Vertex<T>
 27 {
 28     private final T data;
 29     private final List<Vertex<T>> _successors = new ArrayList<Vertex<T>>();
 30 
 31     Vertex(T data) { this.data = data; }
 32     T getData() { return data; }
 33     List<Vertex<T>> successors() { return _successors; }
 34 
 35     public static <T> boolean depthFirstSearch(Vertex<T> start,
 36                                                GoalFunction<T> isGoal,
 37                                                Stack<Vertex<T>> result)
 38     {
 39         if (result.contains(start))
 40 	{
 41 	    return false;
 42 	}
 43 
 44         result.push(start);
 45 
 46         if (isGoal.evaluate(start))
 47 	{
 48 	    return true;
 49 	}
 50         for (Vertex<T> v : start.successors()) {
 51 	    if (depthFirstSearch(v, isGoal, result))
 52 	    {
 53 	        return true;
 54 	    }
 55 	}
 56 
 57         // No path was found
 58         result.pop();
 59         return false; 
 60     }
 61 
 62     public static List<Vertex<Integer>> petersenGraph()
 63     {
 64         List<Vertex<Integer>> v = new ArrayList<Vertex<Integer>>();
 65         for (int i = 0; i < 10; i++)
 66         {
 67             v.add(new Vertex<Integer>(i));
 68         }
 69         int[][] edges =
 70 	    {{0,1}, {1,0}, {1,2}, {2,1}, {2,3}, {3,2}, {3,4}, {4,3}, {4,0}, {0,4},
 71 	     {5,6}, {6,5}, {6,7}, {7,6}, {7,8}, {8,7}, {8,9}, {9,8}, {9,5}, {5,9},
 72 	     {5,0}, {0,5}, {6,2}, {2,6}, {7,4}, {4,7}, {8,1}, {1,8}, {9,3}, {3,9}};
 73 	for (int[] e : edges)
 74 	{
 75 	    v.get(e[0]).successors().add(v.get(e[1]));
 76 	}
 77         return v;
 78     }
 79 
 80     public static void main(String[] args)
 81     {
 82         List<Vertex<Integer>> v = petersenGraph();
 83         Stack<Vertex<Integer>> path = new Stack<Vertex<Integer>>();
 84         if (depthFirstSearch(v.get(0), new GoalFunction<Integer>() {
 85             public boolean evaluate(Vertex<Integer> v)
 86             {
 87                 return v.getData() == 7;
 88             }
 89         }, path))
 90         {
 91             System.out.print("Found path: ");
 92             for (Vertex<Integer> u : path)
 93             {
 94                 System.out.print(u.getData() + " ");
 95             }
 96             System.out.println();
 97         }
 98         else
 99         {
100             System.out.println("No path found");
101         }
102     }
103 
104 }
105 


hijacker
hijacker
hijacker
hijacker