/// <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);
			}
		}
Exemplo n.º 2
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;
		}
		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;
		}
 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);
         }
     }
 }
 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;
 }
        /// <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);
        }
 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);
 }