Java Code Examples for org.jgrapht.GraphPath#getVertexList()

The following examples show how to use org.jgrapht.GraphPath#getVertexList() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpleRoadMapPlanner.java    From coordination_oru with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Get the shortest path connecting given locations (two or more). The path between successive pairs of locations
 * is computed with Dijkstra's algorithm, where edge weights are path lengths.
 * @param locations At least two location names.
 * @return The shortest path connecting given locations.
 */
public PoseSteering[] getShortestPath(String[] locations) {
	if (locations.length < 2) throw new Error("Please provide at least two locations for path extraction!");
	DijkstraShortestPath<String, DefaultWeightedEdge> dijkstraShortestPath = new DijkstraShortestPath<String, DefaultWeightedEdge>(graph);
	ArrayList<PoseSteering> overallShortestPath = new ArrayList<PoseSteering>();
	for (int k = 0; k < locations.length-1; k++) {
	    GraphPath<String, DefaultWeightedEdge> gp = dijkstraShortestPath.getPath(locations[k], locations[k+1]);			
	    if (gp == null) return null;
	    List<String> oneShortestPath = gp.getVertexList();
	    ArrayList<PoseSteering> allPoses = new ArrayList<PoseSteering>();
	    for (int i = 0; i < oneShortestPath.size()-1; i++) {
	    	//PoseSteering[] onePath = loadKnownPath(oneShortestPath.get(i),oneShortestPath.get(i+1));
	    	PoseSteering[] onePath = paths.get(oneShortestPath.get(i)+"->"+oneShortestPath.get(i+1));
	    	if (i == 0) allPoses.add(onePath[0]);
	    	for (int j = 1; j < onePath.length-1; j++) {
	    		allPoses.add(onePath[j]);
	    	}
	    	if (i == oneShortestPath.size()-2) allPoses.add(onePath[onePath.length-1]);
	    }
	    if (k == 0) overallShortestPath.add(allPoses.get(0));
	    for (int i = 1; i < allPoses.size(); i++) {
	    	overallShortestPath.add(allPoses.get(i));
	    }
	}
	return overallShortestPath.toArray(new PoseSteering[overallShortestPath.size()]);
}
 
Example 2
Source File: Missions.java    From coordination_oru with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Get the shortest path connecting given locations (two or more). The path between successive pairs of locations
 * is computed with Dijkstra's algorithm, where edge weights are path lengths.
 * @param locations At least two location names.
 * @return The shortest path connecting given locations.
 */
public static PoseSteering[] getShortestPath(String ... locations) {
	if (locations.length < 2) throw new Error("Please provide at least two locations for path extraction!");
	DijkstraShortestPath<String, DefaultWeightedEdge> dijkstraShortestPath = new DijkstraShortestPath<String, DefaultWeightedEdge>(graph);
	ArrayList<PoseSteering> overallShortestPath = new ArrayList<PoseSteering>();
	for (int k = 0; k < locations.length-1; k++) {
	    GraphPath<String, DefaultWeightedEdge> gp = dijkstraShortestPath.getPath(locations[k], locations[k+1]);			
	    if (gp == null) return null;
	    List<String> oneShortestPath = gp.getVertexList();
	    ArrayList<PoseSteering> allPoses = new ArrayList<PoseSteering>();
	    for (int i = 0; i < oneShortestPath.size()-1; i++) {
	    	//PoseSteering[] onePath = loadKnownPath(oneShortestPath.get(i),oneShortestPath.get(i+1));
	    	PoseSteering[] onePath = paths.get(oneShortestPath.get(i)+"->"+oneShortestPath.get(i+1));
	    	if (i == 0) allPoses.add(onePath[0]);
	    	for (int j = 1; j < onePath.length-1; j++) {
	    		allPoses.add(onePath[j]);
	    	}
	    	if (i == oneShortestPath.size()-2) allPoses.add(onePath[onePath.length-1]);
	    }
	    if (k == 0) overallShortestPath.add(allPoses.get(0));
	    for (int i = 1; i < allPoses.size(); i++) {
	    	overallShortestPath.add(allPoses.get(i));
	    }
	}
	return overallShortestPath.toArray(new PoseSteering[overallShortestPath.size()]);
}
 
Example 3
Source File: GraphFunctions.java    From symja_android_library with GNU General Public License v3.0 5 votes vote down vote up
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
	try {
		if (ast.isAST1()) {
			GraphExpr<ExprEdge> gex = createGraph(ast.arg1());
			if (gex == null) {
				return F.NIL;
			}

			Graph<IExpr, ExprEdge> g = gex.toData();
			GraphPath<IExpr, ExprEdge> path = eulerianCycle(g);
			if (path == null) {
				// Graph is not Eulerian
				return F.CEmptyList;
			}
			List<IExpr> iList = path.getVertexList();
			IASTAppendable list = F.ListAlloc(iList.size());
			for (int i = 0; i < iList.size() - 1; i++) {
				list.append(F.DirectedEdge(iList.get(i), iList.get(i + 1)));
			}
			return list;
		}
	} catch (RuntimeException rex) {
		if (FEConfig.SHOW_STACKTRACE) {
			rex.printStackTrace();
		}
	}
	return F.NIL;
}
 
Example 4
Source File: GraphFunctions.java    From symja_android_library with GNU General Public License v3.0 5 votes vote down vote up
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
	try {
		if (ast.isAST1()) {
			GraphExpr<ExprEdge> gex = createGraph(ast.arg1());
			if (gex == null) {
				return F.NIL;
			}

			Graph<IExpr, ExprEdge> g = gex.toData();
			GraphPath<IExpr, ExprEdge> path = hamiltonianCycle(g);
			if (path == null) {
				// Graph is not Hamiltonian
				return F.CEmptyList;
			}
			List<IExpr> iList = path.getVertexList();
			IASTAppendable list = F.ListAlloc(iList.size());
			for (int i = 0; i < iList.size() - 1; i++) {
				list.append(F.DirectedEdge(iList.get(i), iList.get(i + 1)));
			}
			return list;
		}
	} catch (RuntimeException rex) {
		if (FEConfig.SHOW_STACKTRACE) {
			rex.printStackTrace();
		}
	}
	return F.NIL;
}