public void BuildGraphAndSearchShortestPathUsingGraphBuilder() { // Build algorithm GraphBuilder builder = new GraphBuilder(); builder.Add(a); builder.Add(b, c); builder.Add(d); DijkstraShortestPathAlgorithm <IPoint, IEdge <IPoint> > algorithm = builder.PrepareAlgorithm(); // Attach a distance observer to give us the shortest path distances VertexDistanceRecorderObserver <IPoint, IEdge <IPoint> > distObserver = new VertexDistanceRecorderObserver <IPoint, IEdge <IPoint> >(); distObserver.Attach(algorithm); // Attach a Vertex Predecessor Recorder Observer to give us the paths VertexPredecessorRecorderObserver <IPoint, IEdge <IPoint> > predecessorObserver = new VertexPredecessorRecorderObserver <IPoint, IEdge <IPoint> >(); predecessorObserver.Attach(algorithm); // Run algorithm algorithm.Compute(start); // Check results int distance = distObserver.Distances[end]; Assert.AreEqual(2, distance); IDictionary <IPoint, IEdge <IPoint> > predecessors = predecessorObserver.VertexPredecessors; for (int i = 0; i < distance; i++) { IEdge <IPoint> edge = predecessors[end]; if (i == 0) { Assert.AreEqual(d.GetPointN(d.NumPoints - 2), edge.Source); Assert.AreEqual(d.EndPoint, edge.Target); } else if (i == 1) { Assert.AreEqual(a.StartPoint, edge.Source); Assert.AreEqual(d.GetPointN(d.NumPoints - 2), edge.Target); } end = edge.Source; } // Detach the observers distObserver.Detach(algorithm); predecessorObserver.Detach(algorithm); }
/// <summary> /// Computes the maximum flow between <paramref name="src"/> and /// <paramref name="sink"/> /// </summary> /// <param name="src"></param> /// <param name="sink"></param> /// <returns></returns> protected override void InternalCompute() { if (this.Source == null) { throw new InvalidOperationException("Source is not specified"); } if (this.Sink == null) { throw new InvalidOperationException("Sink is not specified"); } foreach (TVertex u in VisitedGraph.Vertices) { foreach (TEdge e in VisitedGraph.OutEdges(u)) { ResidualCapacities[e] = Capacities[e]; } } VertexColors[Sink] = GraphColor.Gray; while (VertexColors[Sink] != GraphColor.White) { VertexPredecessorRecorderObserver <TVertex, TEdge> vis = new VertexPredecessorRecorderObserver <TVertex, TEdge>( Predecessors ); VertexBuffer <TVertex> Q = new VertexBuffer <TVertex>(); BreadthFirstSearchAlgorithm <TVertex, TEdge> bfs = new BreadthFirstSearchAlgorithm <TVertex, TEdge>( ResidualGraph, Q, VertexColors ); vis.Attach(bfs); bfs.Compute(this.Source); vis.Detach(bfs); if (VertexColors[this.Sink] != GraphColor.White) { Augment(this.Source, this.Sink); } } // while this.MaxFlow = 0; foreach (TEdge e in VisitedGraph.OutEdges(Source)) { this.MaxFlow += (Capacities[e] - ResidualCapacities[e]); } }
public void BuildGraphAndSearchShortestPathUsingGeometryUnion() { IGeometry edges = a.Union(b).Union(c).Union(d).Union(e); Assert.IsNotNull(edges); Assert.IsTrue(edges.GetType() == typeof(MultiLineString)); Assert.Greater(edges.NumGeometries, 0); foreach (IGeometry edge in ((GeometryCollection)edges).Geometries) { Assert.IsNotNull(edge); Assert.IsTrue(edge.GetType() == typeof(LineString)); Debug.WriteLine(edge); } // Build graph IDictionary <IEdge <IGeometry>, double> consts = new Dictionary <IEdge <IGeometry>, double>(edges.NumGeometries); AdjacencyGraph <IGeometry, IEdge <IGeometry> > graph = new AdjacencyGraph <IGeometry, IEdge <IGeometry> >(true); foreach (ILineString str in ((GeometryCollection)edges).Geometries) { // Add vertex 1 IGeometry vertex1 = str.StartPoint; Assert.IsNotNull(vertex1); if (!graph.ContainsVertex(vertex1)) { Debug.WriteLine(String.Format("Adding vertex {0} to the list", vertex1)); graph.AddVertex(vertex1); } else { Debug.WriteLine(String.Format("Vertex {0} already present", vertex1)); } // Add vertex 2 IGeometry vertex2 = str.EndPoint; Assert.IsNotNull(vertex2); if (!graph.ContainsVertex(vertex2)) { Debug.WriteLine(String.Format("Adding vertex {0} to the list", vertex2)); graph.AddVertex(vertex2); } else { Debug.WriteLine(String.Format("Vertex {0} already present", vertex2)); } // Compute weight double weight = weightComputer(str); Assert.Greater(weight, 0.0); // Add edge for 1 => 2 IEdge <IGeometry> edge1 = new Edge <IGeometry>(vertex1, vertex2); Assert.IsNotNull(edge1); graph.AddEdge(edge1); consts.Add(edge1, weight); // Add edge for 2 => 1 IEdge <IGeometry> edge2 = new Edge <IGeometry>(vertex2, vertex1); Assert.IsNotNull(edge2); graph.AddEdge(edge2); consts.Add(edge2, weight); } // Perform DijkstraShortestPathAlgorithm DijkstraShortestPathAlgorithm <IGeometry, IEdge <IGeometry> > dijkstra = new DijkstraShortestPathAlgorithm <IGeometry, IEdge <IGeometry> >(graph, consts); // attach a distance observer to give us the shortest path distances VertexDistanceRecorderObserver <IGeometry, IEdge <IGeometry> > distObserver = new VertexDistanceRecorderObserver <IGeometry, IEdge <IGeometry> >(); distObserver.Attach(dijkstra); // Attach a Vertex Predecessor Recorder Observer to give us the paths VertexPredecessorRecorderObserver <IGeometry, IEdge <IGeometry> > predecessorObserver = new VertexPredecessorRecorderObserver <IGeometry, IEdge <IGeometry> >(); predecessorObserver.Attach(dijkstra); // Run the algorithm Debug.WriteLine(String.Format("Starting algorithm from root vertex {0}", start)); dijkstra.Compute(start); foreach (KeyValuePair <IGeometry, int> kvp in distObserver.Distances) { Debug.WriteLine(String.Format("Distance from root to node {0} is {1}", kvp.Key, kvp.Value)); } foreach (KeyValuePair <IGeometry, IEdge <IGeometry> > kvp in predecessorObserver.VertexPredecessors) { Debug.WriteLine(String.Format( "If you want to get to {0} you have to enter through the IN edge {1}", kvp.Key, kvp.Value)); } Check(graph, consts, predecessorObserver); // Detach the observers distObserver.Detach(dijkstra); predecessorObserver.Detach(dijkstra); }
public void BuildGraphFromMinimalGraphShapefile() { string path = "minimalgraph.shp"; int count = 15; Assert.IsTrue(File.Exists(path)); ShapefileReader reader = new ShapefileReader(path); IGeometryCollection edges = reader.ReadAll(); Assert.IsNotNull(edges); Assert.IsInstanceOfType(typeof(GeometryCollection), edges); Assert.AreEqual(count, edges.NumGeometries); ILineString startls = null; // Build graph Dictionary <IEdge <IGeometry>, double> consts = new Dictionary <IEdge <IGeometry>, double>(edges.NumGeometries); AdjacencyGraph <IGeometry, IEdge <IGeometry> > graph = new AdjacencyGraph <IGeometry, IEdge <IGeometry> >(true); foreach (IMultiLineString mlstr in edges.Geometries) { Assert.AreEqual(1, mlstr.NumGeometries); ILineString str = (ILineString)mlstr.GetGeometryN(0); if (startls == null) { startls = str; } // Add vertex 1 IGeometry vertex1 = str.StartPoint; Assert.IsNotNull(vertex1); if (!graph.ContainsVertex(vertex1)) { Debug.WriteLine(String.Format("Adding vertex {0} to the list", vertex1)); graph.AddVertex(vertex1); } else { Debug.WriteLine(String.Format("Vertex {0} already present", vertex1)); } // Add vertex 2 IGeometry vertex2 = str.EndPoint; Assert.IsNotNull(vertex2); if (!graph.ContainsVertex(vertex2)) { Debug.WriteLine(String.Format("Adding vertex {0} to the list", vertex2)); graph.AddVertex(vertex2); } else { Debug.WriteLine(String.Format("Vertex {0} already present", vertex2)); } // Compute weight double weight = weightComputer(str); Assert.Greater(weight, 0.0); // Add edge 1 => 2 IEdge <IGeometry> edge1 = new Edge <IGeometry>(vertex1, vertex2); Assert.IsNotNull(edge1); graph.AddEdge(edge1); consts.Add(edge1, weight); // Add edge 2 => 1 IEdge <IGeometry> edge2 = new Edge <IGeometry>(vertex2, vertex1); Assert.IsNotNull(edge2); graph.AddEdge(edge2); consts.Add(edge2, weight); } // Perform DijkstraShortestPathAlgorithm DijkstraShortestPathAlgorithm <IGeometry, IEdge <IGeometry> > dijkstra = new DijkstraShortestPathAlgorithm <IGeometry, IEdge <IGeometry> >(graph, consts); // attach a distance observer to give us the shortest path distances VertexDistanceRecorderObserver <IGeometry, IEdge <IGeometry> > distObserver = new VertexDistanceRecorderObserver <IGeometry, IEdge <IGeometry> >(); distObserver.Attach(dijkstra); // Attach a Vertex Predecessor Recorder Observer to give us the paths VertexPredecessorRecorderObserver <IGeometry, IEdge <IGeometry> > predecessorObserver = new VertexPredecessorRecorderObserver <IGeometry, IEdge <IGeometry> >(); predecessorObserver.Attach(dijkstra); // Run the algorithm Assert.IsNotNull(startls); IGeometry startPoint = startls.StartPoint; Debug.WriteLine(String.Format("Starting algorithm from root vertex {0}", startPoint)); dijkstra.Compute(startPoint); foreach (KeyValuePair <IGeometry, int> kvp in distObserver.Distances) { Debug.WriteLine(String.Format("Distance from root to node {0} is {1}", kvp.Key, kvp.Value)); } foreach (KeyValuePair <IGeometry, IEdge <IGeometry> > kvp in predecessorObserver.VertexPredecessors) { Debug.WriteLine(String.Format( "If you want to get to {0} you have to enter through the IN edge {1}", kvp.Key, kvp.Value)); } Check(graph, consts, predecessorObserver); // Detach the observers distObserver.Detach(dijkstra); predecessorObserver.Detach(dijkstra); }
public void BuildGraphAndSearchShortestPathUsingGeometryUnion() { IGeometry edges = a.Union(b).Union(c).Union(d).Union(e); Assert.IsNotNull(edges); Assert.IsTrue(edges.GetType() == typeof(MultiLineString)); Assert.Greater(edges.NumGeometries, 0); foreach (IGeometry edge in ((GeometryCollection) edges).Geometries) { Assert.IsNotNull(edge); Assert.IsTrue(edge.GetType() == typeof(LineString)); Debug.WriteLine(edge); } // Build graph IDictionary<IEdge<IGeometry>, double> consts = new Dictionary<IEdge<IGeometry>, double>(edges.NumGeometries); AdjacencyGraph<IGeometry, IEdge<IGeometry>> graph = new AdjacencyGraph<IGeometry, IEdge<IGeometry>>(true); foreach (ILineString str in ((GeometryCollection) edges).Geometries) { // Add vertex 1 IGeometry vertex1 = str.StartPoint; Assert.IsNotNull(vertex1); if (!graph.ContainsVertex(vertex1)) { Debug.WriteLine(String.Format("Adding vertex {0} to the list", vertex1)); graph.AddVertex(vertex1); } else Debug.WriteLine(String.Format("Vertex {0} already present", vertex1)); // Add vertex 2 IGeometry vertex2 = str.EndPoint; Assert.IsNotNull(vertex2); if (!graph.ContainsVertex(vertex2)) { Debug.WriteLine(String.Format("Adding vertex {0} to the list", vertex2)); graph.AddVertex(vertex2); } else Debug.WriteLine(String.Format("Vertex {0} already present", vertex2)); // Compute weight double weight = weightComputer(str); Assert.Greater(weight, 0.0); // Add edge for 1 => 2 IEdge<IGeometry> edge1 = new Edge<IGeometry>(vertex1, vertex2); Assert.IsNotNull(edge1); graph.AddEdge(edge1); consts.Add(edge1, weight); // Add edge for 2 => 1 IEdge<IGeometry> edge2 = new Edge<IGeometry>(vertex2, vertex1); Assert.IsNotNull(edge2); graph.AddEdge(edge2); consts.Add(edge2, weight); } // Perform DijkstraShortestPathAlgorithm DijkstraShortestPathAlgorithm<IGeometry, IEdge<IGeometry>> dijkstra = new DijkstraShortestPathAlgorithm<IGeometry, IEdge<IGeometry>>(graph, consts); // attach a distance observer to give us the shortest path distances VertexDistanceRecorderObserver<IGeometry, IEdge<IGeometry>> distObserver = new VertexDistanceRecorderObserver<IGeometry, IEdge<IGeometry>>(); distObserver.Attach(dijkstra); // Attach a Vertex Predecessor Recorder Observer to give us the paths VertexPredecessorRecorderObserver<IGeometry, IEdge<IGeometry>> predecessorObserver = new VertexPredecessorRecorderObserver<IGeometry, IEdge<IGeometry>>(); predecessorObserver.Attach(dijkstra); // Run the algorithm Debug.WriteLine(String.Format("Starting algorithm from root vertex {0}", start)); dijkstra.Compute(start); foreach (KeyValuePair<IGeometry, int> kvp in distObserver.Distances) Debug.WriteLine(String.Format("Distance from root to node {0} is {1}", kvp.Key, kvp.Value)); foreach (KeyValuePair<IGeometry, IEdge<IGeometry>> kvp in predecessorObserver.VertexPredecessors) Debug.WriteLine(String.Format( "If you want to get to {0} you have to enter through the IN edge {1}", kvp.Key, kvp.Value)); Check(graph, consts, predecessorObserver); // Detach the observers distObserver.Detach(dijkstra); predecessorObserver.Detach(dijkstra); }
public void BuildGraphFromMinimalGraphShapefile() { string path = "minimalgraph.shp"; int count = 15; Assert.IsTrue(File.Exists(path)); ShapefileReader reader = new ShapefileReader(path); IGeometryCollection edges = reader.ReadAll(); Assert.IsNotNull(edges); Assert.IsInstanceOfType(typeof(GeometryCollection), edges); Assert.AreEqual(count, edges.NumGeometries); ILineString startls = null; // Build graph Dictionary<IEdge<IGeometry>, double> consts = new Dictionary<IEdge<IGeometry>, double>(edges.NumGeometries); AdjacencyGraph<IGeometry, IEdge<IGeometry>> graph = new AdjacencyGraph<IGeometry, IEdge<IGeometry>>(true); foreach (IMultiLineString mlstr in edges.Geometries) { Assert.AreEqual(1, mlstr.NumGeometries); ILineString str = (ILineString) mlstr.GetGeometryN(0); if (startls == null) startls = str; // Add vertex 1 IGeometry vertex1 = str.StartPoint; Assert.IsNotNull(vertex1); if (!graph.ContainsVertex(vertex1)) { Debug.WriteLine(String.Format("Adding vertex {0} to the list", vertex1)); graph.AddVertex(vertex1); } else Debug.WriteLine(String.Format("Vertex {0} already present", vertex1)); // Add vertex 2 IGeometry vertex2 = str.EndPoint; Assert.IsNotNull(vertex2); if (!graph.ContainsVertex(vertex2)) { Debug.WriteLine(String.Format("Adding vertex {0} to the list", vertex2)); graph.AddVertex(vertex2); } else Debug.WriteLine(String.Format("Vertex {0} already present", vertex2)); // Compute weight double weight = weightComputer(str); Assert.Greater(weight, 0.0); // Add edge 1 => 2 IEdge<IGeometry> edge1 = new Edge<IGeometry>(vertex1, vertex2); Assert.IsNotNull(edge1); graph.AddEdge(edge1); consts.Add(edge1, weight); // Add edge 2 => 1 IEdge<IGeometry> edge2 = new Edge<IGeometry>(vertex2, vertex1); Assert.IsNotNull(edge2); graph.AddEdge(edge2); consts.Add(edge2, weight); } // Perform DijkstraShortestPathAlgorithm DijkstraShortestPathAlgorithm<IGeometry, IEdge<IGeometry>> dijkstra = new DijkstraShortestPathAlgorithm<IGeometry, IEdge<IGeometry>>(graph, consts); // attach a distance observer to give us the shortest path distances VertexDistanceRecorderObserver<IGeometry, IEdge<IGeometry>> distObserver = new VertexDistanceRecorderObserver<IGeometry, IEdge<IGeometry>>(); distObserver.Attach(dijkstra); // Attach a Vertex Predecessor Recorder Observer to give us the paths VertexPredecessorRecorderObserver<IGeometry, IEdge<IGeometry>> predecessorObserver = new VertexPredecessorRecorderObserver<IGeometry, IEdge<IGeometry>>(); predecessorObserver.Attach(dijkstra); // Run the algorithm Assert.IsNotNull(startls); IGeometry startPoint = startls.StartPoint; Debug.WriteLine(String.Format("Starting algorithm from root vertex {0}", startPoint)); dijkstra.Compute(startPoint); foreach (KeyValuePair<IGeometry, int> kvp in distObserver.Distances) Debug.WriteLine(String.Format("Distance from root to node {0} is {1}", kvp.Key, kvp.Value)); foreach (KeyValuePair<IGeometry, IEdge<IGeometry>> kvp in predecessorObserver.VertexPredecessors) Debug.WriteLine(String.Format( "If you want to get to {0} you have to enter through the IN edge {1}", kvp.Key, kvp.Value)); Check(graph, consts, predecessorObserver); // Detach the observers distObserver.Detach(dijkstra); predecessorObserver.Detach(dijkstra); }
public void BuildGraphAndSearchShortestPathUsingGraphBuilder() { // Build algorithm GraphBuilder builder = new GraphBuilder(); builder.Add(a); builder.Add(b, c); builder.Add(d); DijkstraShortestPathAlgorithm<IPoint, IEdge<IPoint>> algorithm = builder.PrepareAlgorithm(); // Attach a distance observer to give us the shortest path distances VertexDistanceRecorderObserver<IPoint, IEdge<IPoint>> distObserver = new VertexDistanceRecorderObserver<IPoint, IEdge<IPoint>>(); distObserver.Attach(algorithm); // Attach a Vertex Predecessor Recorder Observer to give us the paths VertexPredecessorRecorderObserver<IPoint, IEdge<IPoint>> predecessorObserver = new VertexPredecessorRecorderObserver<IPoint, IEdge<IPoint>>(); predecessorObserver.Attach(algorithm); // Run algorithm algorithm.Compute(start); // Check results int distance = distObserver.Distances[end]; Assert.AreEqual(2, distance); IDictionary<IPoint, IEdge<IPoint>> predecessors = predecessorObserver.VertexPredecessors; for (int i = 0; i < distance; i++) { IEdge<IPoint> edge = predecessors[end]; if (i == 0) { Assert.AreEqual(d.GetPointN(d.NumPoints - 2), edge.Source); Assert.AreEqual(d.EndPoint, edge.Target); } else if (i == 1) { Assert.AreEqual(a.StartPoint, edge.Source); Assert.AreEqual(d.GetPointN(d.NumPoints - 2), edge.Target); } end = edge.Source; } // Detach the observers distObserver.Detach(algorithm); predecessorObserver.Detach(algorithm); }