private void VerifyCounts(IMutableVertexAndEdgeListGraph <string, Edge <string> > g)
        {
            int i = 0;

            foreach (string v in g.Vertices)
            {
                i++;
            }
            Assert.Equal(g.VertexCount, i);

            i = 0;
            foreach (string v in g.Vertices)
            {
                foreach (Edge <string> e in g.OutEdges(v))
                {
                    i++;
                }
            }
            Assert.Equal(g.EdgeCount, i);

            i = 0;
            foreach (Edge <string> e in g.Edges)
            {
                i++;
            }
            Assert.Equal(g.EdgeCount, i);
        }
Esempio n. 2
0
        /// <summary>
        /// Computes the maximum flow between <see cref="MaximumFlowAlgorithm{TVertex,TEdge}.Source"/>
        /// and <see cref="MaximumFlowAlgorithm{TVertex,TEdge}.Sink"/>.
        /// </summary>
        protected override void InternalCompute()
        {
            ThrowIfCancellationRequested();

            IMutableVertexAndEdgeListGraph <TVertex, TEdge> graph = VisitedGraph;

            foreach (TVertex vertex in graph.Vertices)
            {
                foreach (TEdge edge in graph.OutEdges(vertex))
                {
                    double capacity = Capacities(edge);
                    if (capacity < 0)
                    {
                        throw new NegativeCapacityException();
                    }
                    ResidualCapacities[edge] = capacity;
                }
            }

            VerticesColors[Sink] = GraphColor.Gray;
            while (VerticesColors[Sink] != GraphColor.White)
            {
                var verticesPredecessors = new VertexPredecessorRecorderObserver <TVertex, TEdge>(Predecessors);
                var queue = new Collections.Queue <TVertex>();
                var bfs   = new BreadthFirstSearchAlgorithm <TVertex, TEdge>(
                    ResidualGraph,
                    queue,
                    VerticesColors);

                using (verticesPredecessors.Attach(bfs))
                    bfs.Compute(Source);

                if (VerticesColors[Sink] != GraphColor.White)
                {
                    Augment(Source, Sink);
                }
            }

            MaxFlow = 0;
            foreach (TEdge edge in graph.OutEdges(Source))
            {
                MaxFlow += (Capacities(edge) - ResidualCapacities[edge]);
            }
        }
Esempio n. 3
0
		/// <summary>
		/// Adds temporary edges to the graph to make all vertex even.
		/// </summary>
		/// <param name="g"></param>
		/// <returns></returns>
		public EdgeCollection AddTemporaryEdges(IMutableVertexAndEdgeListGraph g)
		{
			if (g==null)
				throw new ArgumentNullException("g");

			// first gather odd edges.
			VertexCollection oddVertices = AlgoUtility.OddVertices(g);

			// check that there are an even number of them
			if (oddVertices.Count%2!=0)
				throw new Exception("number of odd vertices in not even!");

			// add temporary edges to create even edges:
			EdgeCollection ec = new EdgeCollection();

			bool found,foundbe,foundadjacent;
			while (oddVertices.Count > 0)
			{
				IVertex u = oddVertices[0];
				// find adjacent odd vertex.
				found = false;
				foundadjacent = false;
				foreach(IEdge e in g.OutEdges(u))
				{
					IVertex v = e.Target;
					if (v!=u && oddVertices.Contains(v))
					{
						foundadjacent=true;
						// check that v does not have an out-edge towards u
						foundbe = false;
						foreach(IEdge be in g.OutEdges(v))
						{
							if (be.Target==u)
							{
								foundbe = true;
								break;
							}
						}
						if (foundbe)
							continue;
						// add temporary edge
						IEdge tempEdge = g.AddEdge(v,u);
						// add to collection
						ec.Add(tempEdge);
						// remove u,v from oddVertices
						oddVertices.Remove(u);
						oddVertices.Remove(v);
						// set u to null
						found = true;
						break;
					}
				}

				if (!foundadjacent)
				{
					// pick another vertex
					if (oddVertices.Count<2)
						throw new Exception("Eulerian trail failure");
					IVertex v = oddVertices[1];
					IEdge tempEdge = g.AddEdge(u,v);
					// add to collection
					ec.Add(tempEdge);
					// remove u,v from oddVertices
					oddVertices.Remove(u);
					oddVertices.Remove(v);
					// set u to null
					found = true;
					
				}

				if (!found)
				{
					oddVertices.Remove(u);
					oddVertices.Add(u);
				}
			}

			temporaryEdges = ec;

			return ec;			
		}
        private void VerifyCounts(IMutableVertexAndEdgeListGraph<string, Edge<string>> g)
        {
            int i = 0;
            foreach (string v in g.Vertices)
                i++;
            Assert.AreEqual(g.VertexCount, i);

            i = 0;
            foreach (string v in g.Vertices)
                foreach (Edge<string> e in g.OutEdges(v))
                    i++;
            Assert.AreEqual(g.EdgeCount, i);

            i = 0;
            foreach (Edge<string> e in g.Edges)
                i++;
            Assert.AreEqual(g.EdgeCount, i);
        }
 public EdgeCollection AddTemporaryEdges(IMutableVertexAndEdgeListGraph g)
 {
     if (g == null)
     {
         throw new ArgumentNullException("g");
     }
     VertexCollection vertexs = QuickGraph.Algorithms.AlgoUtility.OddVertices(g);
     if ((vertexs.Count % 2) != 0)
     {
         throw new Exception("number of odd vertices in not even!");
     }
     EdgeCollection edges = new EdgeCollection();
     while (vertexs.Count > 0)
     {
         IVertex vertex = vertexs.get_Item(0);
         bool flag = false;
         bool flag3 = false;
         IEdgeEnumerator enumerator = g.OutEdges(vertex).GetEnumerator();
         while (enumerator.MoveNext())
         {
             IVertex vertex2 = enumerator.get_Current().get_Target();
             if ((vertex2 != vertex) && vertexs.Contains(vertex2))
             {
                 flag3 = true;
                 bool flag2 = false;
                 IEdgeEnumerator enumerator2 = g.OutEdges(vertex2).GetEnumerator();
                 while (enumerator2.MoveNext())
                 {
                     if (enumerator2.get_Current().get_Target() == vertex)
                     {
                         flag2 = true;
                         break;
                     }
                 }
                 if (!flag2)
                 {
                     IEdge edge3 = g.AddEdge(vertex2, vertex);
                     edges.Add(edge3);
                     vertexs.Remove(vertex);
                     vertexs.Remove(vertex2);
                     flag = true;
                     break;
                 }
             }
         }
         if (!flag3)
         {
             if (vertexs.Count < 2)
             {
                 throw new Exception("Eulerian trail failure");
             }
             IVertex vertex3 = vertexs.get_Item(1);
             IEdge edge4 = g.AddEdge(vertex, vertex3);
             edges.Add(edge4);
             vertexs.Remove(vertex);
             vertexs.Remove(vertex3);
             flag = true;
         }
         if (!flag)
         {
             vertexs.Remove(vertex);
             vertexs.Add(vertex);
         }
     }
     this.temporaryEdges = edges;
     return edges;
 }
Esempio n. 6
0
        /// <summary>
        /// Adds temporary edges to the graph to make all vertex even.
        /// </summary>
        /// <param name="g"></param>
        /// <returns></returns>
        public EdgeCollection AddTemporaryEdges(IMutableVertexAndEdgeListGraph g)
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }

            // first gather odd edges.
            VertexCollection oddVertices = AlgoUtility.OddVertices(g);

            // check that there are an even number of them
            if (oddVertices.Count % 2 != 0)
            {
                throw new Exception("number of odd vertices in not even!");
            }

            // add temporary edges to create even edges:
            EdgeCollection ec = new EdgeCollection();

            bool found, foundbe, foundadjacent;

            while (oddVertices.Count > 0)
            {
                IVertex u = oddVertices[0];
                // find adjacent odd vertex.
                found         = false;
                foundadjacent = false;
                foreach (IEdge e in g.OutEdges(u))
                {
                    IVertex v = e.Target;
                    if (v != u && oddVertices.Contains(v))
                    {
                        foundadjacent = true;
                        // check that v does not have an out-edge towards u
                        foundbe = false;
                        foreach (IEdge be in g.OutEdges(v))
                        {
                            if (be.Target == u)
                            {
                                foundbe = true;
                                break;
                            }
                        }
                        if (foundbe)
                        {
                            continue;
                        }
                        // add temporary edge
                        IEdge tempEdge = g.AddEdge(v, u);
                        // add to collection
                        ec.Add(tempEdge);
                        // remove u,v from oddVertices
                        oddVertices.Remove(u);
                        oddVertices.Remove(v);
                        // set u to null
                        found = true;
                        break;
                    }
                }

                if (!foundadjacent)
                {
                    // pick another vertex
                    if (oddVertices.Count < 2)
                    {
                        throw new Exception("Eulerian trail failure");
                    }
                    IVertex v        = oddVertices[1];
                    IEdge   tempEdge = g.AddEdge(u, v);
                    // add to collection
                    ec.Add(tempEdge);
                    // remove u,v from oddVertices
                    oddVertices.Remove(u);
                    oddVertices.Remove(v);
                    // set u to null
                    found = true;
                }

                if (!found)
                {
                    oddVertices.Remove(u);
                    oddVertices.Add(u);
                }
            }

            temporaryEdges = ec;

            return(ec);
        }