public double Compute(Vertex src, Vertex sink)
        {
            m_ResidualEdgeCapacities = new EdgeDoubleDictionary();

            // initializing
            foreach(Vertex u in VisitedGraph.Vertices)
                foreach(Edge e in u.OutEdges)
                    ResidualCapacities[e] = Capacities[e];

            Colors[sink] = GraphColor.Gray;
            while (Colors[sink] != GraphColor.White)
            {
                VertexBuffer Q = new VertexBuffer();
                ResidualEdgePredicate ep = new ResidualEdgePredicate(ResidualCapacities);

                BreadthFirstSearchAlgorithm bfs = new BreadthFirstSearchAlgorithm(resg,Q,Colors);
                PredecessorVisitor pred = new PredecessorVisitor(Predecessors);
                pred.RegisterHandlers(bfs);
                bfs.Compute(src);

                if (Colors[sink] != GraphColor.White)
                    Augment(src, sink, pred.Predecessors);
            } // while

            double flow = 0;
            foreach(Edge e in src.OutEdges)
                flow += (EdgeCapacities[e] - ResidualEdgeCapacities[e]);

            return flow;
        }
 /// <summary>
 /// Constructs a edge weight scaler
 /// </summary>
 /// <param name="weights">edge weight dictionary</param>
 /// <param name="factor">weight scale factor</param>
 public EdgeWeightScalerVisitor(EdgeDoubleDictionary weights, double factor)
 {
     if (weights==null)
         throw new ArgumentNullException("weights");
     this.weights=weights;
     this.factor = factor;
 }
        public GraphBalancerAlgorithm(
            IMutableBidirectionalVertexAndEdgeListGraph visitedGraph,
            IVertex source,
            IVertex sink,
            EdgeDoubleDictionary capacities)
        {
            if (visitedGraph == null)
                throw new ArgumentNullException("visitedGraph");
            if (source == null)
                throw new ArgumentNullException("source");
            if (!visitedGraph.ContainsVertex(source))
                throw new ArgumentException("source is not part of the graph");
            if (sink == null)
                throw new ArgumentNullException("sink");
            if (!visitedGraph.ContainsVertex(sink))
                throw new ArgumentException("sink is not part of the graph");
            if (capacities == null)
                throw new ArgumentNullException("capacities");

            this.visitedGraph = visitedGraph;
            this.source = source;
            this.sink = sink;
            this.capacities = capacities;

            // setting preflow = l(e) = 1
            foreach (IEdge edge in this.VisitedGraph.Edges)
                this.preFlow.Add(edge, 1);
        }
 public WeightedMarkovEdgeChain(EdgeDoubleDictionary weights)
 {
     if (weights == null)
     {
         throw new ArgumentNullException("weights");
     }
     this.weights = weights;
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="g"></param>
 /// <param name="capacities"></param>
 /// <param name="reversedEdges"></param>
 public EdmondsKarpMaximumFlowAlgorithm(
     IVertexListGraph g,
     EdgeDoubleDictionary capacities,
     EdgeEdgeDictionary reversedEdges
     )
     : base(g,capacities,reversedEdges)
 {
 }
 public PushRelabelMaximumFlowAlgorithm(IIndexedVertexListGraph g, EdgeDoubleDictionary capacities, EdgeEdgeDictionary reversedEdges)
     : base(g, capacities, reversedEdges)
 {
     this.visitedGraph = g;
     this.excessFlow = new VertexDoubleDictionary();
     this.current = new VertexIntDictionary();
     this.distances = new VertexIntDictionary();
 }
		public void Init()
		{
			this.capacities = new EdgeDoubleDictionary();
			this.reversedEdges = new EdgeEdgeDictionary();
			this.graph = new AdjacencyGraph();

			this.source = graph.AddVertex();
			this.sink = graph.AddVertex();

			BuildSimpleGraph(source, sink);
		}
        public EdmundsKarpMaximumFlow(
            Graph g,
            EdgeDoubleDictionary edgeCapacities
            )
            : base(g)
        {
            if (edgeCapacities == null)
                throw new ArgumentNullException("Edge capacities");

            m_EdgeCapacities = edgeCapacities;
            m_ResidualEdgeCapacities = null;
            m_Predecessors = new VertexEdgeDictionary();
        }
Example #9
0
        public MinimumFlowAlgorithm(BidirectionalGraph visitedGraph)
        {
            if (visitedGraph == null)
                throw new ArgumentNullException("visitedGraph");
            if (capacities == null)
                throw new ArgumentNullException("capacities");
            this.visitedGraph = visitedGraph;
            this.capacities = new EdgeDoubleDictionary();
            foreach (IEdge edge in this.visitedGraph.Edges)
                this.capacities.Add(edge, double.MaxValue);

            this.Initialize();
        }
Example #10
0
        public MinimumFlowAlgorithm(
            BidirectionalGraph visitedGraph, 
            EdgeDoubleDictionary capacities)
        {
            if (visitedGraph == null)
                throw new ArgumentNullException("visitedGraph");
            if (capacities == null)
                throw new ArgumentNullException("capacities");
            this.visitedGraph = visitedGraph;
            this.capacities = capacities;

            this.Initialize();
        }
 public GraphBalancerAlgorithm(IMutableBidirectionalVertexAndEdgeListGraph visitedGraph, IVertex source, IVertex sink)
 {
     this.source = null;
     this.sink = null;
     this.balancingSource = null;
     this.balancingSourceEdge = null;
     this.balancingSink = null;
     this.balancingSinkEdge = null;
     this.capacities = new EdgeDoubleDictionary();
     this.preFlow = new EdgeIntDictionary();
     this.surplusVertices = new VertexCollection();
     this.surplusEdges = new EdgeCollection();
     this.deficientVertices = new VertexCollection();
     this.deficientEdges = new EdgeCollection();
     this.balanced = false;
     if (visitedGraph == null)
     {
         throw new ArgumentNullException("visitedGraph");
     }
     if (source == null)
     {
         throw new ArgumentNullException("source");
     }
     if (!visitedGraph.ContainsVertex(source))
     {
         throw new ArgumentException("source is not part of the graph");
     }
     if (sink == null)
     {
         throw new ArgumentNullException("sink");
     }
     if (!visitedGraph.ContainsVertex(sink))
     {
         throw new ArgumentException("sink is not part of the graph");
     }
     this.visitedGraph = visitedGraph;
     this.source = source;
     this.sink = sink;
     IEdgeEnumerator enumerator = this.VisitedGraph.get_Edges().GetEnumerator();
     while (enumerator.MoveNext())
     {
         IEdge edge = enumerator.get_Current();
         this.capacities.Add(edge, double.MaxValue);
     }
     IEdgeEnumerator enumerator2 = this.VisitedGraph.get_Edges().GetEnumerator();
     while (enumerator2.MoveNext())
     {
         IEdge edge2 = enumerator2.get_Current();
         this.preFlow.Add(edge2, 1);
     }
 }
        public KruskalMinimumSpanningTreeAlgorithm(
            IVertexAndEdgeListGraph visitedGraph,
            EdgeDoubleDictionary weights
            )
        {
            if (visitedGraph==null)
                throw new ArgumentNullException("visitedGraph");
            if (weights==null)
                throw new ArgumentNullException("weights");

            this.visitedGraph = visitedGraph;
            this.weights=weights;
            this.spanningTreeEdges = new EdgeCollection();
        }
		/// <summary>
		/// Builds a new Bellman Ford searcher.
		/// </summary>
		/// <param name="g">The graph</param>
		/// <param name="weights">Edge weights</param>
		/// <exception cref="ArgumentNullException">Any argument is null</exception>
		/// <remarks>This algorithm uses the <seealso cref="BreadthFirstSearchAlgorithm"/>.</remarks>
		public BellmanFordShortestPathAlgorithm(
			IVertexAndEdgeListGraph g, 
			EdgeDoubleDictionary weights
			)
		{
			if (weights == null)
				throw new ArgumentNullException("Weights");

			m_VisitedGraph = g;
			m_Colors = new VertexColorDictionary();
			m_Weights = weights;
			m_Distances = new VertexDoubleDictionary();
			m_Predecessors = new VertexVertexDictionary();
		}
 public DagShortestPathAlgorithm(IVertexListGraph g, EdgeDoubleDictionary weights)
 {
     if (g == null)
     {
         throw new ArgumentNullException("g");
     }
     if (weights == null)
     {
         throw new ArgumentNullException("Weights");
     }
     this.m_VisitedGraph = g;
     this.m_Colors = new VertexColorDictionary();
     this.m_Distances = new VertexDoubleDictionary();
     this.m_Predecessors = new VertexVertexDictionary();
     this.m_Weights = weights;
 }
		/// <summary>
		/// Builds a new Dijsktra searcher.
		/// </summary>
		/// <param name="g">The graph</param>
		/// <param name="weights">Edge weights</param>
		/// <exception cref="ArgumentNullException">Any argument is null</exception>
		/// <remarks>This algorithm uses the <seealso cref="BreadthFirstSearchAlgorithm"/>.</remarks>
		public DijkstraShortestPathAlgorithm(
			IVertexListGraph g, 
			EdgeDoubleDictionary weights
			)
		{
			if (g == null)
				throw new ArgumentNullException("g");
			if (weights == null)
				throw new ArgumentNullException("Weights");

			this.visitedGraph = g;
			this.colors = new VertexColorDictionary();
			this.distances = new VertexDoubleDictionary();
			this.weights = weights;
			this.vertexQueue = null;
		}
        /// <summary>
        /// Builds a new Dijsktra searcher.
        /// </summary>
        /// <param name="g">The graph</param>
        /// <param name="weights">Edge weights</param>
        /// <exception cref="ArgumentNullException">Any argument is null</exception>
        /// <remarks>This algorithm uses the <seealso cref="BreadthFirstSearchAlgorithm"/>.</remarks>
        public DijkstraShortestPathAlgorithm(
            IVertexListGraph g,
            EdgeDoubleDictionary weights
            )
        {
            if (g == null)
                throw new ArgumentNullException("g");
            if (weights == null)
                throw new ArgumentNullException("Weights");

            m_VisitedGraph = g;
            m_Colors = new VertexColorDictionary();
            m_Distances = new VertexDoubleDictionary();
            m_Predecessors = new VertexVertexDictionary();
            m_Weights = weights;
            m_VertexQueue = null;
        }
 public MinimumFlowAlgorithm(BidirectionalGraph visitedGraph, EdgeDoubleDictionary capacities)
 {
     this.reverser = null;
     this.balancer = null;
     this.maxFlowF1 = null;
     this.maxFlowf2 = null;
     if (visitedGraph == null)
     {
         throw new ArgumentNullException("visitedGraph");
     }
     if (capacities == null)
     {
         throw new ArgumentNullException("capacities");
     }
     this.visitedGraph = visitedGraph;
     this.capacities = capacities;
     this.Initialize();
 }
Example #18
0
		/// <summary>Constructs a maximum flow algorithm.</summary>
		/// <param name="g">Graph to compute maximum flow on.</param>
		/// <param name="capacities">edge capacities</param>
		/// <param name="reversedEdges">reversed edge map</param>
		/// <exception cref="ArgumentNullException"><paramref name="g"/> or
		/// <paramref name="capacities"/> or <paramref name="reversedEdges"/> is a null
		/// reference.
		/// </exception>
		public MaximumFlowAlgorithm(
			IVertexListGraph g,
			EdgeDoubleDictionary capacities,
			EdgeEdgeDictionary reversedEdges
			)
		{
			if (g==null)
				throw new ArgumentNullException("g");
			if (capacities==null)
				throw new ArgumentNullException("capacities");
			if (reversedEdges==null)
				throw new ArgumentNullException("reversedEdges");
		
			this.visitedGraph=g;
			this.capacities=capacities;
			this.reversedEdges=reversedEdges;
		
			this.predecessors=new VertexEdgeDictionary();
			this.residualCapacities=new EdgeDoubleDictionary();
			this.colors = new VertexColorDictionary();
        }
 public MinimumFlowAlgorithm(BidirectionalGraph visitedGraph)
 {
     this.reverser = null;
     this.balancer = null;
     this.maxFlowF1 = null;
     this.maxFlowf2 = null;
     if (visitedGraph == null)
     {
         throw new ArgumentNullException("visitedGraph");
     }
     if (this.capacities == null)
     {
         throw new ArgumentNullException("capacities");
     }
     this.visitedGraph = visitedGraph;
     this.capacities = new EdgeDoubleDictionary();
     VertexEdgesEnumerator enumerator = this.visitedGraph.Edges.GetEnumerator();
     while (enumerator.MoveNext())
     {
         IEdge edge = enumerator.get_Current();
         this.capacities.Add(edge, double.MaxValue);
     }
     this.Initialize();
 }
Example #20
0
        private void menuItem8_Click(object sender, System.EventArgs e)
        {
            if (this.netronPanel.Graph==null)
                throw new Exception("Generate a graph first");
            if (this.netronPanel.Populator==null)
                throw new Exception("Populator should not be null.");

            ResetVertexAndEdgeColors();

            // create algorithm
            this.vertexCounts = new VertexIntDictionary();
            this.edgeCounts = new EdgeIntDictionary();
            foreach(IVertex vertex in this.netronPanel.Graph.Vertices)
                this.vertexCounts[vertex]=0;
            foreach(IEdge edge in this.netronPanel.Graph.Edges)
                this.edgeCounts[edge]=0;

            this.edgeWeights =new EdgeDoubleDictionary();
            foreach(IEdge edge in this.netronPanel.Graph.Edges)
                edgeWeights[edge]=1;
            WeightedMarkovEdgeChain chain = new WeightedMarkovEdgeChain(edgeWeights);
            RandomWalkAlgorithm walker = new RandomWalkAlgorithm(
                this.netronPanel.Graph
                );

            walker.TreeEdge+=new EdgeEventHandler(walker_WeightedTreeEdge);

            LayoutAlgorithmTraverVisitor tracer = new LayoutAlgorithmTraverVisitor(this.netronPanel.Populator);
            walker.TreeEdge +=new EdgeEventHandler(tracer.TreeEdge);

            Thread thread = new Thread(new ThreadStart(walker.Generate));
            thread.Start();
        }
 public static EdgeDoubleDictionary UnaryWeightsFromVertexList(IVertexListGraph graph)
 {
     if (graph == null)
     {
         throw new ArgumentNullException("graph");
     }
     EdgeDoubleDictionary dictionary = new EdgeDoubleDictionary();
     IVertexEnumerator enumerator = graph.get_Vertices().GetEnumerator();
     while (enumerator.MoveNext())
     {
         IVertex vertex = enumerator.get_Current();
         IEdgeEnumerator enumerator2 = graph.OutEdges(vertex).GetEnumerator();
         while (enumerator2.MoveNext())
         {
             IEdge edge = enumerator2.get_Current();
             dictionary.set_Item(edge, 1.0);
         }
     }
     return dictionary;
 }
 public VanishingWeightedMarkovEdgeChain(EdgeDoubleDictionary weights, double factor)
     : base(weights)
 {
     this.factor = factor;
 }
		/// <summary>
		/// Create a edge unary weight dictionary.
		/// </summary>
		/// <param name="graph">graph to map</param>
		/// <returns>Dictionary where each edge wheight is 1</returns>
		public static EdgeDoubleDictionary UnaryWeightsFromVertexList(IVertexListGraph graph)
		{
			if (graph==null)
				throw new ArgumentNullException("graph");

			EdgeDoubleDictionary weights=new EdgeDoubleDictionary();
			foreach(IVertex v in graph.Vertices)
			{
				foreach(IEdge e in graph.OutEdges(v))
				{
					weights[e]=1;
				}
			}
			return weights;
		}
Example #24
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="residualCapacities">Residual Edge capacities map</param>
 /// <exception cref="ArgumentNullException">residualCapacities is null</exception>
 public ResidualEdgePredicate(EdgeDoubleDictionary residualCapacities)
 {
     if (residualCapacities == null)
         throw new ArgumentNullException("residualCapacities");
     m_ResidualCapacities = residualCapacities;
 }
		/// <summary>
		/// Create a edge unary weight dictionary.
		/// </summary>
		/// <param name="graph">graph to map</param>
		/// <returns>Dictionary where each edge wheight is 1</returns>
		public static EdgeDoubleDictionary UnaryWeightsFromEdgeList(IEdgeListGraph graph)
		{
			if (graph==null)
				throw new ArgumentNullException("graph");
			EdgeDoubleDictionary weights=new EdgeDoubleDictionary();
			foreach(IEdge e in graph.Edges)
				weights[e]=1;
			return weights;
		}