/** * Build the shortest path from the target/destination node, following the * parent links. * * @param target * The destination node. * @return The path. */ public Path buildPath(AStarNode target) { Path path = new Path(); ArrayList<AStarNode> thePath = new ArrayList<AStarNode>(); AStarNode node = target; while (node != null) { thePath.add(node); node = node.parent; } int n = thePath.size(); if (n > 1) { AStarNode current = thePath.get(n - 1); AStarNode follow = thePath.get(n - 2); path.add(current.node, follow.edge); current = follow; for (int i = n - 3; i >= 0; i--) { follow = thePath.get(i); path.add(follow.edge); current = follow; } } return path; }
/** * A synonym for {@link #add(Edge)}. */ public void push(Node from, Edge edge) { add(from, edge); }
@SuppressWarnings("unchecked") private void pathSetShortestPath_facilitate(Node current, Path path, List<Path> paths) { Node source = graph.getNode(this.source_id); if (current != source) { Node next = null; ArrayList<? extends Edge> predecessors = (ArrayList<? extends Edge>) current .getAttribute(identifier+".predecessors"); while (current != source && predecessors.size() == 1) { Edge e = predecessors.get(0); next = e.getOpposite(current); path.add(current, e); current = next; predecessors = (ArrayList<? extends Edge>) current .getAttribute(identifier+".predecessors"); } if (current != source) { for (Edge e : predecessors) { Path p = path.getACopy(); p.add(current, e); pathSetShortestPath_facilitate(e.getOpposite(current), p, paths); } } } if (current == source) { paths.add(path); } }
/** * Constructs all the possible shortest paths from the source node to the * destination (end). Warning: this construction is VERY HEAVY ! * * @param end * The destination to which shortest paths are computed. * @return a list of shortest paths given with * {@link org.graphstream.graph.Path} objects. */ public List<Path> getPathSetShortestPaths(Node end) { ArrayList<Path> paths = new ArrayList<Path>(); pathSetShortestPath_facilitate(end, new Path(), paths); return paths; }
throw new IllegalArgumentException("From node cannot be null."); } else { setRoot(from);
/** * Build the shortest path from the target/destination node, following the * parent links. * * @param target * The destination node. * @return The path. */ public Path buildPath(AStarNode target) { Path path = new Path(); ArrayList<AStarNode> thePath = new ArrayList<AStarNode>(); AStarNode node = target; while (node != null) { thePath.add(node); node = node.parent; } int n = thePath.size(); if (n > 1) { AStarNode current = thePath.get(n - 1); AStarNode follow = thePath.get(n - 2); path.add(current.node, follow.edge); current = follow; for (int i = n - 3; i >= 0; i--) { follow = thePath.get(i); path.add(follow.edge); current = follow; } } return path; }
/** * A synonym for {@link #add(Edge)}. */ public void push(Edge edge) { add(edge); }
@SuppressWarnings("unchecked") private void pathSetShortestPath_facilitate(Node current, Path path, List<Path> paths) { Node source = graph.getNode(this.source_id); if (current != source) { Node next = null; ArrayList<? extends Edge> predecessors = (ArrayList<? extends Edge>) current .getAttribute(identifier+".predecessors"); while (current != source && predecessors.size() == 1) { Edge e = predecessors.get(0); next = e.getOpposite(current); path.add(current, e); current = next; predecessors = (ArrayList<? extends Edge>) current .getAttribute(identifier+".predecessors"); } if (current != source) { for (Edge e : predecessors) { Path p = path.getACopy(); p.add(current, e); pathSetShortestPath_facilitate(e.getOpposite(current), p, paths); } } } if (current == source) { paths.add(path); } }
/** * Constructs all the possible shortest paths from the source node to the * destination (end). Warning: this construction is VERY HEAVY ! * * @param end * The destination to which shortest paths are computed. * @return a list of shortest paths given with * {@link org.graphstream.graph.Path} objects. */ public List<Path> getPathSetShortestPaths(Node end) { ArrayList<Path> paths = new ArrayList<Path>(); pathSetShortestPath_facilitate(end, new Path(), paths); return paths; }
throw new IllegalArgumentException("From node cannot be null."); } else { setRoot(from);
public Path getShortestPathTo(String other) { TargetPath tpath = targets.get(other); // XXX Probably a bug here in the Path class usage. // TODO update this to create an edge path to be compatible with // multi-graphs. if (tpath != null) { Path path = new Path(); // XXX use the Path object directly. ArrayList<Node> nodePath = new ArrayList<Node>(); nodePath.add(source); nodePath.add(tpath.target); // Recursively build the path between the source and target node // by exploring pass-by nodes. expandPath(1, this, tpath, nodePath); // Build a Path object. for (int i = 0; i < nodePath.size() - 1; ++i) { // XXX XXX complicated ? path.add( nodePath.get(i), nodePath.get(i).getEdgeToward( nodePath.get(i + 1).getId())); } return path; } return null; }
/** * A synonym for {@link #add(Edge)}. */ public void push(Node from, Edge edge) { add(from, edge); }
/** * Get a copy of this path * * @return A copy of this path. */ @SuppressWarnings("unchecked") public Path getACopy() { Path newPath = new Path(); newPath.root = this.root; newPath.edgePath = (Stack<Edge>) edgePath.clone(); newPath.nodePath = (Stack<Node>) nodePath.clone(); return newPath; }
/** * Returns the shortest path from the source node to a given target node. If * there is no path from the source to the target returns an empty path. * This method constructs a {@link org.graphstream.graph.Path} object which * consumes heap memory proportional to the number of edges and nodes in the * path. When possible, prefer using {@link #getPathNodes(Node)} and * {@link #getPathEdges(Node)} which are more memory- and time-efficient. * * @param target * a node * @return the shortest path from the source to the target * @complexity O(<em>p</em>) where <em>p</em> is the number of the nodes in * the path */ public Path getPath(Node target) { Path path = new Path(); if (Double.isInfinite(getPathLength(target))) return path; Stack<Edge> stack = new Stack<Edge>(); for (Edge e : getPathEdges(target)) stack.push(e); path.setRoot(source); while (!stack.isEmpty()) path.add(stack.pop()); return path; } }
/** * A synonym for {@link #add(Edge)}. */ public void push(Edge edge) { add(edge); }
/** * Get a copy of this path * * @return A copy of this path. */ @SuppressWarnings("unchecked") public Path getACopy() { Path newPath = new Path(); newPath.root = this.root; newPath.edgePath = (Stack<Edge>) edgePath.clone(); newPath.nodePath = (Stack<Node>) nodePath.clone(); return newPath; }
/** * Returns the shortest path from the source node to a given target node. If * there is no path from the source to the target returns an empty path. * This method constructs a {@link org.graphstream.graph.Path} object which * consumes heap memory proportional to the number of edges and nodes in the * path. When possible, prefer using {@link #getPathNodes(Node)} and * {@link #getPathEdges(Node)} which are more memory- and time-efficient. * * @param target * a node * @return the shortest path from the source to the target * @complexity O(<em>p</em>) where <em>p</em> is the number of the nodes in * the path */ public Path getPath(Node target) { Path path = new Path(); if (Double.isInfinite(getPathLength(target))) return path; Stack<Edge> stack = new Stack<Edge>(); for (Edge e : getPathEdges(target)) stack.push(e); path.setRoot(source); while (!stack.isEmpty()) path.add(stack.pop()); return path; } }