} // ensure non-instantiability. public static IList findPathBetween(Graph graph, Object startVertex, Object endVertex) { MyBreadthFirstIterator iter = new MyBreadthFirstIterator(graph, startVertex); while (iter.MoveNext()) { object vertex = iter.Current; if (vertex.Equals(endVertex)) { return(createPath(iter, endVertex)); } } return(null); }
private BFSShortestPath() { } // ensure non-instantiability. public static IList findPathBetween(Graph graph, Object startVertex, Object endVertex) { MyBreadthFirstIterator iter = new MyBreadthFirstIterator(graph, startVertex); while (iter.MoveNext()) { object vertex = iter.Current; if (vertex.Equals(endVertex)) { return createPath(iter, endVertex); } } return null; }
private static IList createPath(MyBreadthFirstIterator iter, Object endVertex) { ArrayList path = new ArrayList(); while (true) { Edge edge = iter.getSpanningTreeEdge(endVertex); if (edge == null) { break; } path.Add(edge); endVertex = edge.oppositeVertex(endVertex); } path.Reverse(); return(path); }
private static IList createPath(MyBreadthFirstIterator iter, Object endVertex) { ArrayList path = new ArrayList(); while(true) { Edge edge = iter.getSpanningTreeEdge(endVertex); if(edge == null) { break; } path.Add(edge); endVertex = edge.oppositeVertex( endVertex ); } path.Reverse(); return path; }
private void createShortestPathGraph() { /* shortestPathGraph = new DefaultDirectedGraph(); * //shortestPathGraph.addAllVertices(g.vertexSet()); * * LinkedList queue = new LinkedList(); * * //encounter target vertex * queue.addLast(targetVertex); * shortestPathGraph.addVertex(targetVertex); * * int distance = 0; * * Object firstVertexOfNextLevel = targetVertex; * Collection verticesOfNextLevel = new ArrayList(); * * while (!queue.isEmpty()) { * //provide next vertex * Object vertex = queue.removeFirst(); * * if (vertex == firstVertexOfNextLevel) { * distance++; * firstVertexOfNextLevel = null; * verticesOfNextLevel.clear(); * } * * //add unseen children of next vertex * List edges = g.edgesOf(vertex); * * for(Iterator i = edges.iterator(); i.hasNext();) { * Edge e = (Edge) i.next( ); * Object opposite = e.oppositeVertex(vertex); * * if (!shortestPathGraph.containsVertex(opposite)) { * //encounter vertex * queue.addLast(opposite); * shortestPathGraph.addVertex(opposite); * * verticesOfNextLevel.add(opposite); * * if (firstVertexOfNextLevel == null) { * firstVertexOfNextLevel = opposite; * } * } * * * if (verticesOfNextLevel.contains(opposite)) { * shortestPathGraph.addEdge(opposite, vertex); * } * } * }*/ shortestPathGraph = new DefaultDirectedGraph(); shortestPathGraph.addVertex(targetVertex); // This map gives the distance of a vertex to the target vertex //UPGRADE_TODO: Class 'java.util.HashMap' was converted to 'System.Collections.Hashtable' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashMap'" System.Collections.IDictionary distanceMap = new System.Collections.Hashtable(); for (MyBreadthFirstIterator iter = new MyBreadthFirstIterator(g, targetVertex); iter.MoveNext();) { System.Object vertex = iter.Current; shortestPathGraph.addVertex(vertex); int distance = iter.level; distanceMap[vertex] = (System.Int32)distance; //UPGRADE_TODO: Method 'java.util.Iterator.hasNext' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratorhasNext'" for (System.Collections.IEnumerator edges = g.edgesOf(vertex).GetEnumerator(); edges.MoveNext();) { //UPGRADE_TODO: Method 'java.util.Iterator.next' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratornext'" Edge edge = (Edge)edges.Current; System.Object opposite = edge.oppositeVertex(vertex); if (distanceMap[opposite] != null) { if (((System.Int32)distanceMap[opposite]) + 1 == distance) { shortestPathGraph.addVertex(opposite); shortestPathGraph.addEdge(vertex, opposite); } } } if (vertex == sourceVertex) { break; } } System.Collections.IEnumerator edgeIterator = shortestPathGraph.outgoingEdgesOf(sourceVertex).GetEnumerator(); edgeIteratorStack = new System.Collections.ArrayList(); edgeIteratorStack.Add(edgeIterator); vertexStack = new System.Collections.ArrayList(); vertexStack.Add(sourceVertex); }