private void GenerateSpanningTree() { _spanningTree = new BidirectionalGraph <TVertex, Edge <TVertex> >(false); _spanningTree.AddVertexRange(VisitedGraph.Vertices); IQueue <TVertex> vb = new QuickGraph.Collections.Queue <TVertex>(); if (VisitedGraph.VertexCount > 0) { vb.Enqueue(VisitedGraph.Vertices.OrderBy(v => VisitedGraph.InDegree(v)).First()); } switch (Parameters.SpanningTreeGeneration) { case SpanningTreeGeneration.BFS: var bfsAlgo = new BreadthFirstSearchAlgorithm <TVertex, TEdge>(VisitedGraph, vb, new Dictionary <TVertex, GraphColor>()); bfsAlgo.TreeEdge += e => _spanningTree.AddEdge(new Edge <TVertex>(e.Source, e.Target)); bfsAlgo.Compute(); break; case SpanningTreeGeneration.DFS: var dfsAlgo = new DepthFirstSearchAlgorithm <TVertex, TEdge>(VisitedGraph); dfsAlgo.TreeEdge += e => _spanningTree.AddEdge(new Edge <TVertex>(e.Source, e.Target)); dfsAlgo.Compute(); break; } }
/// <summary> /// Computes the maximum flow between Source and Sink. /// </summary> /// <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"); } if (this.Services.CancelManager.IsCancelling) { return; } var g = this.VisitedGraph; foreach (var u in g.Vertices) { foreach (var e in g.OutEdges(u)) { var capacity = this.Capacities(e); if (capacity < 0) { throw new InvalidOperationException("negative edge capacity"); } this.ResidualCapacities[e] = capacity; } } this.VertexColors[Sink] = GraphColor.Gray; while (this.VertexColors[Sink] != GraphColor.White) { var vis = new VertexPredecessorRecorderObserver <TVertex, TEdge>( this.Predecessors ); var queue = new QuickGraph.Collections.Queue <TVertex>(); var bfs = new BreadthFirstSearchAlgorithm <TVertex, TEdge>( this.ResidualGraph, queue, this.VertexColors ); using (vis.Attach(bfs)) bfs.Compute(this.Source); if (this.VertexColors[this.Sink] != GraphColor.White) { this.Augment(this.Source, this.Sink); } } // while this.MaxFlow = 0; foreach (var e in g.OutEdges(Source)) { this.MaxFlow += (this.Capacities(e) - this.ResidualCapacities[e]); } }
/// <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 ); var Q = new QuickGraph.Collections.Queue <TVertex>(); var 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]); } }
private BidirectionalGraph <FrameworkElement, Edge> GenerateSpanningTree(BidirectionalGraph <FrameworkElement, Edge> graph) { var spanningTree = new BidirectionalGraph <FrameworkElement, Edge>(false); spanningTree.AddVertexRange(graph.Vertices.ToList()); IQueue <FrameworkElement> vb = new QuickGraph.Collections.Queue <FrameworkElement>(); vb.Enqueue(graph.Vertices.OrderBy(graph.InDegree).First()); //switch (Parameters.SpanningTreeGeneration) //{ // case SpanningTreeGeneration.BFS: var bfsAlgo = new DepthFirstSearchAlgorithm <FrameworkElement, Edge>(graph); bfsAlgo.TreeEdge += e => spanningTree.AddEdge(e); bfsAlgo.Compute(); // break; // case SpanningTreeGeneration.DFS: // var dfsAlgo = new DepthFirstSearchAlgorithm<TVertex, TEdge>(VisitedGraph); // dfsAlgo.TreeEdge += e => spanningTree.AddEdge(new Edge<TVertex>(e.Source, e.Target)); // dfsAlgo.Compute(); // break; //} return(spanningTree); }