Last active
August 29, 2015 13:58
-
-
Save pablormier/10107318 to your computer and use it in GitHub Desktop.
Shortest path in a directed weighted graph with Hipster
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a simple weighted directed graph with Hipster where | |
// vertices are Strings and edge values are just doubles | |
HipsterDirectedGraph<String,Double> graph = GraphBuilder.create() | |
.connect("A").to("B").withEdge(4d) | |
.connect("A").to("C").withEdge(2d) | |
.connect("B").to("C").withEdge(5d) | |
.connect("B").to("D").withEdge(10d) | |
.connect("C").to("E").withEdge(3d) | |
.connect("D").to("F").withEdge(11d) | |
.connect("E").to("D").withEdge(4d) | |
.buildDirectedGraph(); | |
// Create the search problem. For graph problems, just use | |
// the GraphSearchProblem util class to generate the problem with ease. | |
SearchProblem p = GraphSearchProblem | |
.startingFrom("A") | |
.in(graph) | |
.takeCostsFromEdges() | |
.build(); | |
// Search the shortest path from "A" to "F" | |
System.out.println(Hipster.createDijkstra(p).search("F")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment