Exemple #1
0
 /// <summary>
 /// Adds the elements of another VertexCollection to the end of this VertexCollection.
 /// </summary>
 /// <param name="items">
 /// The VertexCollection whose elements are to be added to the end of this VertexCollection.
 /// </param>
 public virtual void AddRange(VertexCollection items)
 {
     foreach (IVertex item in items)
     {
         this.List.Add(item);
     }
 }
 public VertexRecorderVisitor(VertexCollection vertices)
 {
     if (vertices == null)
     {
         throw new ArgumentNullException("vertices");
     }
     this.vertices = vertices;
 }
 public PredecessorRecorderVisitor(VertexEdgeDictionary predecessors)
 {
     if (predecessors == null)
     {
         throw new ArgumentNullException("predecessors");
     }
     this.predecessors = predecessors;
     this.endPathVertices = new VertexCollection();
 }
 public SinkRecorderVisitor(IIncidenceGraph g)
 {
     this.sinks = new VertexCollection();
     if (g == null)
     {
         throw new ArgumentNullException("g");
     }
     this.visitedGraph = g;
 }
        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="ec">base collection</param>
        /// <param name="ep">predicate</param>
        /// <exception cref="ArgumentNullException">ec or ep null</exception>
        public FilteredVertexEnumerable(VertexCollection ec, IVertexPredicate ep)
        {
            if (ec == null)
                throw new ArgumentNullException("Vertex collection");
            if (ep == null)
                throw new ArgumentNullException("Vertex predicate");

            m_VertexCollection = ec;
            m_VertexPredicate = ep;
        }
 /// <summary>
 /// Create a <see cref="sinkRecorderVisitor"/> instance.
 /// </summary>
 /// <param name="g">visited graph</param>
 /// <param name="sinks">collection that will hold the sinks</param>
 /// <exception cref="ArgumentNullException">g is a null reference</exception>
 public SinkRecorderVisitor(
     IIncidenceGraph g,
     VertexCollection sinks)
 {
     if (g==null)
         throw new ArgumentNullException("g");
     if (sinks==null)
         throw new ArgumentNullException("sinks");
     this.visitedGraph = g;
     this.sinks = sinks;
 }
 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);
     }
 }
Exemple #8
0
        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="ec">base collection</param>
        /// <param name="ep">predicate</param>
        /// <exception cref="ArgumentNullException">ec or ep null</exception>
        public FilteredVertexEnumerable(VertexCollection ec, IVertexPredicate ep)
        {
            if (ec == null)
            {
                throw new ArgumentNullException("Vertex collection");
            }
            if (ep == null)
            {
                throw new ArgumentNullException("Vertex predicate");
            }

            m_VertexCollection = ec;
            m_VertexPredicate  = ep;
        }
        public void Calculate(int moveCount)
        {
            Initialize();

            for(int i = 1;i<moveCount;++i)
            {
                PropagateChanges(i);

                foreach(IEdge e in EdgesWidthTargetInFront )
                {
                    TraverseEdge(e,i);
                }

                front = newFront;
                newFront = new VertexCollection();
            }
        }
        protected void Initialize()
        {
            this.front = new VertexCollection();
            this.front.AddRange(this.goals);
            this.newFront = new VertexCollection();
            this.probs = new VertexDoublesDictionary();
            this.costs = new VertexDoublesDictionary();

            foreach(IVertex v in this.TestGraph.Graph.Vertices)
            {
                // setting probs
                DoubleCollection col =new DoubleCollection();
                if (this.Goals.Contains(v))
                {
                    col.Add(0);
                }
                else
                {
                    col.Add(1);
                }

                this.probs.Add(v,col);

                // setting costs
                col = new DoubleCollection();
                col.Add(0);
                this.costs.Add(v,col);
            }

            foreach(IVertex v in this.TestGraph.States)
            {
                this.Strategy.SetChooseEdge(v,0,null);
            }
        }
 internal void ComputeNoInit(IVertex s)
 {
     VertexCollection vertices = new VertexCollection();
     new TopologicalSortAlgorithm(this.VisitedGraph, vertices).Compute();
     this.OnDiscoverVertex(s);
     VertexCollection.Enumerator enumerator = vertices.GetEnumerator();
     while (enumerator.MoveNext())
     {
         IVertex v = enumerator.get_Current();
         this.OnExamineVertex(v);
         IEdgeEnumerator enumerator2 = this.VisitedGraph.OutEdges(v).GetEnumerator();
         while (enumerator2.MoveNext())
         {
             IEdge e = enumerator2.get_Current();
             this.OnDiscoverVertex(e.get_Target());
             if (this.Relax(e))
             {
                 this.OnEdgeRelaxed(e);
             }
             else
             {
                 this.OnEdgeNotRelaxed(e);
             }
         }
         this.OnFinishVertex(v);
     }
 }
Exemple #12
0
 /// <summary>
 /// Initializes a new instance of the VertexCollection class, containing elements
 /// copied from another instance of VertexCollection
 /// </summary>
 /// <param name="items">
 /// The VertexCollection whose elements are to be added to the new VertexCollection.
 /// </param>
 public VertexCollection(VertexCollection items)
 {
     this.AddRange(items);
 }
 public void RemoveDanglingLinks()
 {
     VertexCollection vertexs = new VertexCollection();
     do
     {
         vertexs.Clear();
         IVertexListGraph graph = new FilteredVertexListGraph(this.VisitedGraph, new InDictionaryVertexPredicate(this.ranks));
         foreach (IVertex vertex in this.ranks.get_Keys())
         {
             if (graph.OutDegree(vertex) == 0)
             {
                 vertexs.Add(vertex);
             }
         }
         VertexCollection.Enumerator enumerator = vertexs.GetEnumerator();
         while (enumerator.MoveNext())
         {
             IVertex vertex2 = enumerator.get_Current();
             this.ranks.Remove(vertex2);
         }
     }
     while (vertexs.Count != 0);
 }
 public VertexRecorderVisitor()
 {
     this.vertices = new VertexCollection();
 }
		private SortedList BuildSCCVertexMap( VertexIntDictionary vSccMap )	
        {
			//	Construct a map of SCC ID as key & IVertexCollection of vertices contained within the SCC as value
			SortedList h = new SortedList();
			VertexCollection vertices = null;
			foreach( DictionaryEntry de in vSccMap )	
            {
				IVertex v = (IVertex) de.Key;
				int scc_id = (int) de.Value;
				if( h.ContainsKey(scc_id) )
					((VertexCollection) h[scc_id]).Add(v);
				else	
                {
					vertices = new VertexCollection();
					vertices.Add(v);
					h.Add(scc_id, vertices);
				}
			}
			return h;
		}
 protected void Initialize()
 {
     this.front = new VertexCollection();
     this.front.AddRange(this.goals);
     this.newFront = new VertexCollection();
     this.probs = new VertexDoublesDictionary();
     this.costs = new VertexDoublesDictionary();
     IVertexEnumerator enumerator = this.TestGraph.Graph.get_Vertices().GetEnumerator();
     while (enumerator.MoveNext())
     {
         IVertex vertex = enumerator.get_Current();
         DoubleCollection doubles = new DoubleCollection();
         if (this.Goals.Contains(vertex))
         {
             doubles.Add(0.0);
         }
         else
         {
             doubles.Add(1.0);
         }
         this.probs.Add(vertex, doubles);
         doubles = new DoubleCollection();
         doubles.Add(0.0);
         this.costs.Add(vertex, doubles);
     }
     IVertexEnumerator enumerator2 = this.TestGraph.States.GetEnumerator();
     while (enumerator2.MoveNext())
     {
         IVertex v = enumerator2.get_Current();
         this.Strategy.SetChooseEdge(v, 0, null);
     }
 }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="e">Base enumerator</param>
 /// <param name="p">predicate</param>
 public Enumerator(VertexCollection.Enumerator e, IVertexPredicate p)
 {
     m_Enumerator = e;
     m_Predicate = p;
 }
Exemple #18
0
 /// <summary>
 /// Initializes a new instance of the VertexCollection class, containing elements
 /// copied from another instance of VertexCollection
 /// </summary>
 /// <param name="items">
 /// The VertexCollection whose elements are to be added to the new VertexCollection.
 /// </param>
 public VertexCollection(VertexCollection items)
 {
     this.AddRange(items);
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="avs"></param>
 /// <param name="e"></param>
 /// <returns></returns>
 protected CharacteristicVertex FindTargetVertex(VertexCollection avs,IEdge e)
 {
     foreach(CharacteristicVertex avtarget in avs)
     {
         if (avtarget.IncomingEdge==e)
             return avtarget;
     }
     throw new Exception("could not find target vertex");
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="g"></param>
        /// <param name="assg"></param>
        public void Transform(IBidirectionalGraph g, IMutableVertexAndEdgeListGraph assg)
        {
            VertexCollection avs = new VertexCollection();
            // adding vertices
            foreach(IEdge e in g.Edges)
            {
                // xi_-(L) = g(xi_-(0), xi_+(L))
                CharacteristicVertex avm = (CharacteristicVertex)assg.AddVertex();
                avm.IncomingEdge = e;
                avm.Vertex = e.Target;
                avs.Add(avm);

                // xi_+(0) = g(xi_-(0), xi_+(L))
                CharacteristicVertex avp = (CharacteristicVertex)assg.AddVertex();
                avp.IncomingEdge = e;
                avp.Vertex = e.Source;
                avs.Add(avp);
            }

            // adding out edges
            foreach(CharacteristicVertex av in avs)
            {
                foreach(IEdge e in g.OutEdges(av.Vertex))
                {
                    // find target vertex:
                    CharacteristicVertex avtarget = FindTargetVertex(e);
                    // add xi_-
                    CharacteristicEdge aem = (CharacteristicEdge)assg.AddEdge(av,avtarget);
                    aem.Positive = false;
                    aem.Edge = e;
                }
                foreach(IEdge e in g.InEdges(av.Vertex))
                {
                    // find target vertex:
                    CharacteristicVertex avtarget = FindTargetVertex(e);
                    // add xi_-
                    CharacteristicEdge aem = (CharacteristicEdge)assg.AddEdge(av,avtarget);
                    aem.Positive = true;
                    aem.Edge = e;
                }
            }
        }
        /// <summary>
        /// Iteratively removes the dangling links from the rank map
        /// </summary>
        public void RemoveDanglingLinks()
        {
            VertexCollection danglings = new VertexCollection();
            do
            {
                danglings.Clear();

                // create filtered graph
                IVertexListGraph fg = new FilteredVertexListGraph(
                    this.VisitedGraph,
                    new InDictionaryVertexPredicate(this.ranks)
                    );

                // iterate over of the vertices in the rank map
                foreach(IVertex v in this.ranks.Keys)
                {
                    // if v does not have out-edge in the filtered graph, remove
                    if ( fg.OutDegree(v) == 0)
                        danglings.Add(v);
                }

                // remove from ranks
                foreach(IVertex v in danglings)
                    this.ranks.Remove(v);
                // iterate until no dangling was removed
            }while(danglings.Count != 0);
        }
 /// <summary>
 /// Default constructor
 /// </summary>
 public PredecessorRecorderVisitor()
 {
     this.predecessors = new VertexEdgeDictionary();
     this.endPathVertices = new VertexCollection();
 }
Exemple #23
0
		/// <summary>
		/// Create a collection of odd vertices
		/// </summary>
		/// <param name="g">graph to visit</param>
		/// <returns>colleciton of odd vertices</returns>
		/// <exception cref="ArgumentNullException">g is a null reference</exception>
		public static VertexCollection OddVertices(IVertexAndEdgeListGraph g)
		{
			if (g==null)
				throw new ArgumentNullException("g");

			VertexIntDictionary counts = new VertexIntDictionary();
			foreach(IVertex v in g.Vertices)
			{
				counts[v]=0;
			}
	
			foreach(IEdge e in g.Edges)
			{
				++counts[e.Source];
				--counts[e.Target];
			}

			VertexCollection odds= new VertexCollection();
			foreach(DictionaryEntry de in counts)
			{
				if ((int)de.Value%2!=0)
					odds.Add((IVertex)de.Key);
			}

			return odds;
		}
 public void Calculate(int moveCount)
 {
     this.Initialize();
     for (int i = 1; i < moveCount; i++)
     {
         this.PropagateChanges(i);
         IEdgeEnumerator enumerator = this.EdgesWidthTargetInFront.GetEnumerator();
         while (enumerator.MoveNext())
         {
             IEdge e = enumerator.get_Current();
             this.TraverseEdge(e, i);
         }
         this.front = this.newFront;
         this.newFront = new VertexCollection();
     }
 }
		internal void ComputeNoInit(IVertex s)
		{
			VertexCollection orderedVertices = new VertexCollection();
			TopologicalSortAlgorithm topoSorter = new TopologicalSortAlgorithm(VisitedGraph,orderedVertices);
			topoSorter.Compute();

			OnDiscoverVertex(s);

			foreach(IVertex v in orderedVertices) 
			{
				OnExamineVertex(v);

				foreach(IEdge e in VisitedGraph.OutEdges(v))
				{
					OnDiscoverVertex(e.Target);
					bool decreased = Relax(e);
					if (decreased)
						OnEdgeRelaxed(e);
					else
						OnEdgeNotRelaxed(e);
				}
				OnFinishVertex(v);      
			}

		}
Exemple #26
0
 /// <summary>
 /// Adds the elements of another VertexCollection to the end of this VertexCollection.
 /// </summary>
 /// <param name="items">
 /// The VertexCollection whose elements are to be added to the end of this VertexCollection.
 /// </param>
 public virtual void AddRange(VertexCollection items)
 {
     foreach (IVertex item in items)
     {
         this.List.Add(item);
     }
 }
Exemple #27
0
			/// <summary>
			/// Builds a enumerator on the collection
			/// </summary>
			/// <param name="collection"></param>
			public Enumerator(VertexCollection collection)
			{
				this.wrapped = ((System.Collections.CollectionBase)collection).GetEnumerator();
			}
Exemple #28
0
 /// <summary>
 /// Builds a enumerator on the collection
 /// </summary>
 /// <param name="collection"></param>
 public Enumerator(VertexCollection collection)
 {
     this.wrapped = ((System.Collections.CollectionBase)collection).GetEnumerator();
 }
 public PreflowLayer()
 {
     this.activeVertices = new VertexCollection();
     this.inactiveVertices = new VertexCollection();
 }
 private SortedList BuildSCCVertexMap(VertexIntDictionary vSccMap)
 {
     SortedList list = new SortedList();
     VertexCollection vertexs = null;
     IDictionaryEnumerator enumerator = vSccMap.GetEnumerator();
     while (enumerator.MoveNext())
     {
         DictionaryEntry current = (DictionaryEntry) enumerator.Current;
         IVertex key = (IVertex) current.Key;
         int num = (int) current.Value;
         if (list.ContainsKey(num))
         {
             ((VertexCollection) list[num]).Add(key);
         }
         else
         {
             vertexs = new VertexCollection();
             vertexs.Add(key);
             list.Add(num, vertexs);
         }
     }
     return list;
 }
 public void Create(IMutableVertexAndEdgeListGraph cg)
 {
     if (cg == null)
     {
         throw new ArgumentNullException("cg");
     }
     if (this.components == null)
     {
         this.ComputeComponents();
     }
     this.sccVertexMap = this.BuildSCCVertexMap(this.components);
     VertexCollection vertexs = new VertexCollection();
     IDictionaryEnumerator enumerator = this.sccVertexMap.GetEnumerator();
     while (enumerator.MoveNext())
     {
         IVertex cgVertex = cg.AddVertex();
         this.OnInitCondensationGraphVertex(new CondensationGraphVertexEventArgs(cgVertex, (IVertexCollection) enumerator.Value));
         vertexs.Add(cgVertex);
     }
     for (int i = 0; i < this.sccVertexMap.Keys.Count; i++)
     {
         VertexCollection vertexs2 = new VertexCollection();
         IVertexEnumerator enumerator2 = ((IVertexCollection) this.sccVertexMap[i]).GetEnumerator();
         while (enumerator2.MoveNext())
         {
             IVertex vertex2 = enumerator2.get_Current();
             IEdgeEnumerator enumerator3 = this.VisitedGraph.OutEdges(vertex2).GetEnumerator();
             while (enumerator3.MoveNext())
             {
                 IVertex vertex3 = enumerator3.get_Current().get_Target();
                 int num2 = this.components.get_Item(vertex3);
                 if (i != num2)
                 {
                     IVertex vertex4 = vertexs.get_Item(num2);
                     if (!vertexs2.Contains(vertex4))
                     {
                         vertexs2.Add(vertex4);
                     }
                 }
             }
         }
         IVertex vertex5 = vertexs.get_Item(i);
         VertexCollection.Enumerator enumerator4 = vertexs2.GetEnumerator();
         while (enumerator4.MoveNext())
         {
             IVertex vertex6 = enumerator4.get_Current();
             cg.AddEdge(vertex5, vertex6);
         }
     }
 }
		/// <summary>
		/// Compute the condensation graph and store it in the supplied graph 'cg'
		/// </summary>
		/// <param name="cg">
		/// Instance of mutable graph in which the condensation graph 
		/// transformation is stored
		/// </param>
		public void Create( IMutableVertexAndEdgeListGraph cg )	
        {
			if (cg==null)
				throw new ArgumentNullException("cg");			

			if (components==null)
				ComputeComponents();			
					
			//  components list contains collection of 
            // input graph Vertex for each SCC Vertex_ID 
            // (i.e Vector< Vector<Vertex> > )
			//	Key = SCC Vertex ID 
			sccVertexMap = BuildSCCVertexMap(components);
			
			//	Lsit of SCC vertices
			VertexCollection toCgVertices = new VertexCollection();
			IDictionaryEnumerator it = sccVertexMap.GetEnumerator();
			while( it.MoveNext() )	
				//	as scc_vertex_map is a sorted list, order of SCC IDs will match CG vertices
			{
				IVertex curr = cg.AddVertex();
				OnInitCondensationGraphVertex(new CondensationGraphVertexEventArgs(curr, (IVertexCollection)it.Value));
				toCgVertices.Add(curr);
			}
			
			for( int srcSccId=0; srcSccId<sccVertexMap.Keys.Count; srcSccId++ )	
            {   
				VertexCollection adj = new VertexCollection();
				foreach( IVertex u in (IVertexCollection)sccVertexMap[srcSccId] )	
                {					
					foreach(IEdge e in VisitedGraph.OutEdges(u))	{
						IVertex v = e.Target;        			
						int targetSccId = components[v];
						if (srcSccId != targetSccId)	
                        {
							// Avoid loops in the condensation graph
							IVertex sccV = toCgVertices[targetSccId];
							if( !adj.Contains(sccV) )		// Avoid parallel edges
								adj.Add(sccV);
						}
					}
				}
				IVertex s = toCgVertices[srcSccId];
				foreach( IVertex t in adj )
					cg.AddEdge(s, t);
			}
		}
        internal void FindFeasible()
        {
            this.negativeVertices = new VertexCollection();
            this.positiveVertices = new VertexCollection();
            int nn = 0, np = 0;
            foreach(IVertex u in VisitedGraph.Vertices)
            {
                int degree=Degree(u);
                if( degree < 0 )
                    NegativeVertices.Add(u);
                else if( degree > 0 )
                    PositiveVertices.Add(u);
            }

            int du,dv,f;
            foreach(IVertex u in NegativeVertices)
            {
                du = Degrees[u];
                foreach(IVertex v in PositiveVertices)
                {
                    dv = Degrees[v];
                    if (-du<dv)
                        Adjoins[u,v] = f = -du;
                    else
                        Adjoins[u,v] = f = dv;

                    Degrees[u] += f;
                    Degrees[v] -= f;
                }
            }
        }