public static void Loop(IMutableVertexAndEdgeListGraph<string,Edge<string>> g) { string x = "x"; g.AddVertex(x); string y = "y"; g.AddVertex(y); string z = "z"; g.AddVertex(z); g.AddEdge(new Edge<string>(x, y)); g.AddEdge(new Edge<string>(y, z)); g.AddEdge(new Edge<string>(z, x)); }
public static void Simple(IMutableVertexAndEdgeListGraph<string, Edge<string>> g) { string x = "x"; g.AddVertex(x); string y = "y"; g.AddVertex(y); string z = "z"; g.AddVertex(z); string w = "w"; g.AddVertex(w); string u = "u"; g.AddVertex(u); string v = "v"; g.AddVertex(v); g.AddEdge(new Edge<string>(u, x)); g.AddEdge(new Edge<string>(u, v)); g.AddEdge(new Edge<string>(w, z)); g.AddEdge(new Edge<string>(w, y)); g.AddEdge(new Edge<string>(w, u)); g.AddEdge(new Edge<string>(x, v)); g.AddEdge(new Edge<string>(v, y)); g.AddEdge(new Edge<string>(y, x)); g.AddEdge(new Edge<string>(z, y)); }
public static void UnBalancedFlow(IMutableVertexAndEdgeListGraph<string, Edge<string>> g) { string x = "x"; g.AddVertex(x); string y = "y"; g.AddVertex(y); string z = "z"; g.AddVertex(z); string w = "w"; g.AddVertex(w); g.AddEdge(new Edge<string>(x, y)); g.AddEdge(new Edge<string>(x, z)); g.AddEdge(new Edge<string>(y, z)); g.AddEdge(new Edge<string>(x, w)); g.AddEdge(new Edge<string>(w, y)); g.AddEdge(new Edge<string>(w, z)); }
public void Add(T source, T target) { _graph.AddVertex(source); _graph.AddVertex(target); _graph.AddEdge(new Edge <T>(source, target)); }
/// <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; }
/// <summary> /// Compute the transitive closure and store it in the supplied graph 'tc' /// </summary> /// <param name="tc"> /// Mutable Graph instance to store the transitive closure /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="tc"/> is a <null/>. /// </exception> public void Create( IMutableVertexAndEdgeListGraph tc ) { if (tc==null) throw new ArgumentNullException("tc"); CondensationGraphAlgorithm cgalgo = new CondensationGraphAlgorithm(VisitedGraph); cgalgo.Create(cg); ArrayList topo_order = new ArrayList(cg.VerticesCount); TopologicalSortAlgorithm topo = new TopologicalSortAlgorithm(cg, topo_order); topo.Compute(); VertexIntDictionary in_a_chain= new VertexIntDictionary(); VertexIntDictionary topo_number = new VertexIntDictionary(); for( int order=0; order<topo_order.Count; order++ ) { IVertex v = (IVertex)topo_order[order]; topo_number.Add( v, order ); if(!in_a_chain.Contains(v)) // Initially no vertex is present in a chain in_a_chain.Add(v, 0); } VertexListMatrix chains = new VertexListMatrix(); int position = -1; foreach( IVertex v in topo_order ) { if(in_a_chain[v]==0) { // Start a new chain position = chains.AddRow(); IVertex next = v; for(;;) { chains[position].Add(next); in_a_chain[next] = 1; // Get adjacent vertices ordered by topological number // Extend the chain by choosing the adj vertex with lowest topo# ArrayList adj = TopoSortAdjVertices(next, cg, topo_number); if( (next = FirstNotInChain(adj, in_a_chain)) == null ) break; } } } VertexIntDictionary chain_number = new VertexIntDictionary(); VertexIntDictionary pos_in_chain = new VertexIntDictionary(); // Record chain positions of vertices SetChainPositions(chains, chain_number, pos_in_chain); VertexListMatrix successors = new VertexListMatrix(); successors.CreateObjectMatrix(cg.VerticesCount, chains.RowCount, int.MaxValue); if(topo_order.Count > 0) { for( int rtopo=topo_order.Count-1; rtopo>-1; rtopo--) { IVertex u = (IVertex) topo_order[rtopo]; foreach( IVertex v in TopoSortAdjVertices(u, cg, topo_number) ) { if(topo_number[v] < (int)successors[u.ID][chain_number[v]]) { // {succ(u)} = {succ(u)} U {succ(v)} LeftUnion(successors[u.ID], successors[v.ID]); // {succ(u)} = {succ(u)} U {v} successors[u.ID][chain_number[v]] = topo_number[v]; } } } } // Create transitive closure of condensation graph // Remove existing edges in CG & rebuild edges for TC from // successor set (to avoid duplicating parallel edges) ArrayList edges = new ArrayList(); foreach( IEdge e in cg.Edges ) edges.Add(e); foreach(IEdge e in edges) cg.RemoveEdge(e); foreach(IVertex u in cg.Vertices) { int i = u.ID; for(int j=0; j<chains.RowCount; j++) { int tnumber = (int) successors[i][j]; if(tnumber < int.MaxValue) { IVertex v = (IVertex) topo_order[tnumber]; for(int k=pos_in_chain[v]; k<chains[j].Count; k++) { cg.AddEdge( u, (IVertex)chains[j][k]); } } } } // Maps a vertex in input graph to it's transitive closure graph graphTransitiveClosures = new VertexVertexDictionary(); // Add vertices to transitive closure graph foreach(IVertex v in visitedGraph.Vertices) { if(!graphTransitiveClosures.Contains(v)) { IVertex vTransform = tc.AddVertex(); OnInitTransitiveClosureVertex( new TransitiveClosureVertexEventArgs( v, vTransform) ); // Fire the TC Vertex Event graphTransitiveClosures.Add(v, vTransform); } } //Add edges connecting vertices within SCC & adjacent // SCC (strongly connected component) IVertexCollection scc_vertices = null; foreach(IVertex s_tccg in cg.Vertices) { scc_vertices = (IVertexCollection)cgalgo.SCCVerticesMap[s_tccg.ID]; if(scc_vertices.Count > 1) { foreach(IVertex u in scc_vertices) foreach(IVertex v in scc_vertices) OnExamineEdge(tc.AddEdge(graphTransitiveClosures[u], graphTransitiveClosures[v])); } foreach(IEdge adj_edge in cg.OutEdges(s_tccg)) { IVertex t_tccg = adj_edge.Target; foreach(IVertex s in (IVertexCollection)cgalgo.SCCVerticesMap[s_tccg.ID]) { foreach(IVertex t in (IVertexCollection)cgalgo.SCCVerticesMap[t_tccg.ID]) OnExamineEdge(tc.AddEdge(graphTransitiveClosures[s], graphTransitiveClosures[t])); } } } }
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> /// /// </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; } } }
public static void RegularLattice(int rows, int columns, IMutableVertexAndEdgeListGraph<string, Edge<string>> g) { string[,] latice = new string[rows, columns]; // adding vertices for (int i = 0; i < rows; ++i) { for (int j = 0; j < columns; ++j) { latice[i, j] = String.Format("{0},{1}", i.ToString(), j.ToString()); g.AddVertex(latice[i, j]); } } // adding edges for (int i = 0; i < rows - 1; ++i) { for (int j = 0; j < columns - 1; ++j) { g.AddEdge(new Edge<string>(latice[i, j], latice[i, j + 1])); g.AddEdge(new Edge<string>(latice[i, j + 1], latice[i, j])); g.AddEdge(new Edge<string>(latice[i, j], latice[i + 1, j])); g.AddEdge(new Edge<string>(latice[i + 1, j], latice[i, j])); } } for (int j = 0; j < columns - 1; ++j) { g.AddEdge(new Edge<string>(latice[rows - 1, j], latice[rows - 1, j + 1])); g.AddEdge(new Edge<string>(latice[rows - 1, j + 1], latice[rows - 1, j])); } for (int i = 0; i < rows - 1; ++i) { g.AddEdge(new Edge<string>(latice[i, columns - 1], latice[i + 1, columns - 1])); g.AddEdge(new Edge<string>(latice[i + 1, columns - 1], latice[i, columns - 1])); } }
/// <summary> /// Compute the transitive closure and store it in the supplied graph 'tc' /// </summary> /// <param name="tc"> /// Mutable Graph instance to store the transitive closure /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="tc"/> is a <null/>. /// </exception> public void Create(IMutableVertexAndEdgeListGraph tc) { if (tc == null) { throw new ArgumentNullException("tc"); } CondensationGraphAlgorithm cgalgo = new CondensationGraphAlgorithm(VisitedGraph); cgalgo.Create(cg); ArrayList topo_order = new ArrayList(cg.VerticesCount); TopologicalSortAlgorithm topo = new TopologicalSortAlgorithm(cg, topo_order); topo.Compute(); VertexIntDictionary in_a_chain = new VertexIntDictionary(); VertexIntDictionary topo_number = new VertexIntDictionary(); for (int order = 0; order < topo_order.Count; order++) { IVertex v = (IVertex)topo_order[order]; topo_number.Add(v, order); if (!in_a_chain.Contains(v)) // Initially no vertex is present in a chain { in_a_chain.Add(v, 0); } } VertexListMatrix chains = new VertexListMatrix(); int position = -1; foreach (IVertex v in topo_order) { if (in_a_chain[v] == 0) { // Start a new chain position = chains.AddRow(); IVertex next = v; for (;;) { chains[position].Add(next); in_a_chain[next] = 1; // Get adjacent vertices ordered by topological number // Extend the chain by choosing the adj vertex with lowest topo# ArrayList adj = TopoSortAdjVertices(next, cg, topo_number); if ((next = FirstNotInChain(adj, in_a_chain)) == null) { break; } } } } VertexIntDictionary chain_number = new VertexIntDictionary(); VertexIntDictionary pos_in_chain = new VertexIntDictionary(); // Record chain positions of vertices SetChainPositions(chains, chain_number, pos_in_chain); VertexListMatrix successors = new VertexListMatrix(); successors.CreateObjectMatrix(cg.VerticesCount, chains.RowCount, int.MaxValue); if (topo_order.Count > 0) { for (int rtopo = topo_order.Count - 1; rtopo > -1; rtopo--) { IVertex u = (IVertex)topo_order[rtopo]; foreach (IVertex v in TopoSortAdjVertices(u, cg, topo_number)) { if (topo_number[v] < (int)successors[u.ID][chain_number[v]]) { // {succ(u)} = {succ(u)} U {succ(v)} LeftUnion(successors[u.ID], successors[v.ID]); // {succ(u)} = {succ(u)} U {v} successors[u.ID][chain_number[v]] = topo_number[v]; } } } } // Create transitive closure of condensation graph // Remove existing edges in CG & rebuild edges for TC from // successor set (to avoid duplicating parallel edges) ArrayList edges = new ArrayList(); foreach (IEdge e in cg.Edges) { edges.Add(e); } foreach (IEdge e in edges) { cg.RemoveEdge(e); } foreach (IVertex u in cg.Vertices) { int i = u.ID; for (int j = 0; j < chains.RowCount; j++) { int tnumber = (int)successors[i][j]; if (tnumber < int.MaxValue) { IVertex v = (IVertex)topo_order[tnumber]; for (int k = pos_in_chain[v]; k < chains[j].Count; k++) { cg.AddEdge(u, (IVertex)chains[j][k]); } } } } // Maps a vertex in input graph to it's transitive closure graph graphTransitiveClosures = new VertexVertexDictionary(); // Add vertices to transitive closure graph foreach (IVertex v in visitedGraph.Vertices) { if (!graphTransitiveClosures.Contains(v)) { IVertex vTransform = tc.AddVertex(); OnInitTransitiveClosureVertex( new TransitiveClosureVertexEventArgs( v, vTransform) ); // Fire the TC Vertex Event graphTransitiveClosures.Add(v, vTransform); } } //Add edges connecting vertices within SCC & adjacent // SCC (strongly connected component) IVertexCollection scc_vertices = null; foreach (IVertex s_tccg in cg.Vertices) { scc_vertices = (IVertexCollection)cgalgo.SCCVerticesMap[s_tccg.ID]; if (scc_vertices.Count > 1) { foreach (IVertex u in scc_vertices) { foreach (IVertex v in scc_vertices) { OnExamineEdge(tc.AddEdge(graphTransitiveClosures[u], graphTransitiveClosures[v])); } } } foreach (IEdge adj_edge in cg.OutEdges(s_tccg)) { IVertex t_tccg = adj_edge.Target; foreach (IVertex s in (IVertexCollection)cgalgo.SCCVerticesMap[s_tccg.ID]) { foreach (IVertex t in (IVertexCollection)cgalgo.SCCVerticesMap[t_tccg.ID]) { OnExamineEdge(tc.AddEdge(graphTransitiveClosures[s], graphTransitiveClosures[t])); } } } } }
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; }
public static void FileDependency(IMutableVertexAndEdgeListGraph<string, Edge<string>> g) { // adding files and storing names string zig_cpp = "zip.cpp"; g.AddVertex(zig_cpp); string boz_h = "boz.h"; g.AddVertex(boz_h); string zag_cpp = "zag.cpp"; g.AddVertex(zag_cpp); string yow_h = "yow.h"; g.AddVertex(yow_h); string dax_h = "dax.h"; g.AddVertex(dax_h); string bar_cpp = "bar.cpp"; g.AddVertex(bar_cpp); string zow_h = "zow.h"; g.AddVertex(zow_h); string foo_cpp = "foo.cpp"; g.AddVertex(foo_cpp); string zig_o = "zig.o"; g.AddVertex(zig_o); string zag_o = "zago"; g.AddVertex(zag_o); string bar_o = "bar.o"; g.AddVertex(bar_o); string foo_o = "foo.o"; g.AddVertex(foo_o); string libzigzag_a = "libzigzig.a"; g.AddVertex(libzigzag_a); string libfoobar_a = "libfoobar.a"; g.AddVertex(libfoobar_a); string killerapp = "killerapp.exe"; g.AddVertex(killerapp); // adding dependencies g.AddEdge(new Edge<string>(dax_h, foo_cpp)); g.AddEdge(new Edge<string>(dax_h, bar_cpp)); g.AddEdge(new Edge<string>(dax_h, yow_h)); g.AddEdge(new Edge<string>(yow_h, bar_cpp)); g.AddEdge(new Edge<string>(yow_h, zag_cpp)); g.AddEdge(new Edge<string>(boz_h, bar_cpp)); g.AddEdge(new Edge<string>(boz_h, zig_cpp)); g.AddEdge(new Edge<string>(boz_h, zag_cpp)); g.AddEdge(new Edge<string>(zow_h, foo_cpp)); g.AddEdge(new Edge<string>(foo_cpp, foo_o)); g.AddEdge(new Edge<string>(foo_o, libfoobar_a)); g.AddEdge(new Edge<string>(bar_cpp, bar_o)); g.AddEdge(new Edge<string>(bar_o, libfoobar_a)); g.AddEdge(new Edge<string>(libfoobar_a, libzigzag_a)); g.AddEdge(new Edge<string>(zig_cpp, zig_o)); g.AddEdge(new Edge<string>(zig_o, libzigzag_a)); g.AddEdge(new Edge<string>(zag_cpp, zag_o)); g.AddEdge(new Edge<string>(zag_o, libzigzag_a)); g.AddEdge(new Edge<string>(libzigzag_a, killerapp)); }
public static void Fsm(IMutableVertexAndEdgeListGraph<string,NamedEdge<string>> g) { string s0 = "S0"; g.AddVertex(s0); string s1 = "S1"; g.AddVertex(s1); string s2 = "S2"; g.AddVertex(s2); string s3 = "S3"; g.AddVertex(s3); string s4 = "S4"; g.AddVertex(s4); string s5 = "S5"; g.AddVertex(s5); g.AddEdge(new NamedEdge<string>(s0, s1,"StartCalc")); g.AddEdge(new NamedEdge<string>(s1, s0,"StopCalc")); g.AddEdge(new NamedEdge<string>(s1, s1,"SelectStandard")); g.AddEdge(new NamedEdge<string>(s1, s1,"ClearDisplay")); g.AddEdge(new NamedEdge<string>(s1, s2,"SelectScientific")); g.AddEdge(new NamedEdge<string>(s1, s3,"EnterDecNumber")); g.AddEdge(new NamedEdge<string>(s2, s1,"SelectStandard")); g.AddEdge(new NamedEdge<string>(s2, s2,"SelectScientific")); g.AddEdge(new NamedEdge<string>(s2, s2,"ClearDisplay")); g.AddEdge(new NamedEdge<string>(s2, s4,"EnterDecNumber")); g.AddEdge(new NamedEdge<string>(s2, s5,"StopCalc")); g.AddEdge(new NamedEdge<string>(s3, s0,"StopCalc")); g.AddEdge(new NamedEdge<string>(s3, s1,"ClearDisplay")); g.AddEdge(new NamedEdge<string>(s3, s3,"SelectStandard")); g.AddEdge(new NamedEdge<string>(s3, s3,"EnterDecNumber")); g.AddEdge(new NamedEdge<string>(s3, s4,"SelectScientific")); g.AddEdge(new NamedEdge<string>(s4, s2,"ClearDisplay")); g.AddEdge(new NamedEdge<string>(s4, s3,"SelectStandard")); g.AddEdge(new NamedEdge<string>(s4, s4,"SelectScientific")); g.AddEdge(new NamedEdge<string>(s4, s4,"EnterDecNumber")); g.AddEdge(new NamedEdge<string>(s4, s5,"StopCalc")); g.AddEdge(new NamedEdge<string>(s5, s2, "StartCalc")); }
public static void Fsm(IMutableVertexAndEdgeListGraph <string, NamedEdge <string> > g) { string s0 = "S0"; g.AddVertex(s0); string s1 = "S1"; g.AddVertex(s1); string s2 = "S2"; g.AddVertex(s2); string s3 = "S3"; g.AddVertex(s3); string s4 = "S4"; g.AddVertex(s4); string s5 = "S5"; g.AddVertex(s5); g.AddEdge(new NamedEdge <string>(s0, s1, "StartCalc")); g.AddEdge(new NamedEdge <string>(s1, s0, "StopCalc")); g.AddEdge(new NamedEdge <string>(s1, s1, "SelectStandard")); g.AddEdge(new NamedEdge <string>(s1, s1, "ClearDisplay")); g.AddEdge(new NamedEdge <string>(s1, s2, "SelectScientific")); g.AddEdge(new NamedEdge <string>(s1, s3, "EnterDecNumber")); g.AddEdge(new NamedEdge <string>(s2, s1, "SelectStandard")); g.AddEdge(new NamedEdge <string>(s2, s2, "SelectScientific")); g.AddEdge(new NamedEdge <string>(s2, s2, "ClearDisplay")); g.AddEdge(new NamedEdge <string>(s2, s4, "EnterDecNumber")); g.AddEdge(new NamedEdge <string>(s2, s5, "StopCalc")); g.AddEdge(new NamedEdge <string>(s3, s0, "StopCalc")); g.AddEdge(new NamedEdge <string>(s3, s1, "ClearDisplay")); g.AddEdge(new NamedEdge <string>(s3, s3, "SelectStandard")); g.AddEdge(new NamedEdge <string>(s3, s3, "EnterDecNumber")); g.AddEdge(new NamedEdge <string>(s3, s4, "SelectScientific")); g.AddEdge(new NamedEdge <string>(s4, s2, "ClearDisplay")); g.AddEdge(new NamedEdge <string>(s4, s3, "SelectStandard")); g.AddEdge(new NamedEdge <string>(s4, s4, "SelectScientific")); g.AddEdge(new NamedEdge <string>(s4, s4, "EnterDecNumber")); g.AddEdge(new NamedEdge <string>(s4, s5, "StopCalc")); g.AddEdge(new NamedEdge <string>(s5, s2, "StartCalc")); }
public static void Create <TEdge>( IMutableVertexAndEdgeListGraph <MazeGraphVertex, TEdge> g, VertexFactory <MazeGraphVertex> vertexFactory, EdgeFactory <MazeGraphVertex, TEdge> edgeFactory, Random rnd, MazeGraphOptions opts ) where TEdge : IEdge <MazeGraphVertex> { Contract.Requires(g != null); Contract.Requires(vertexFactory != null); Contract.Requires(edgeFactory != null); Contract.Requires(rnd != null); Contract.Requires(opts.vertexCount > 0); Contract.Requires(opts.edgeCount >= 0); Contract.Requires( !(!g.AllowParallelEdges && !opts.selfEdges) || opts.edgeCount <= opts.vertexCount * (opts.vertexCount - 1) // directed graph ); var vertices = new MazeGraphVertex[opts.vertexCount]; for (int i = 0; i < opts.vertexCount; ++i) { g.AddVertex(vertices[i] = vertexFactory()); } var freeVertices = new List <MazeGraphVertex> (vertices); MazeGraphVertex a; MazeGraphVertex b; int j, k; j = k = 0; while (j < opts.edgeCount) { a = freeVertices[rnd.Next(opts.vertexCount - k)]; do { b = vertices[rnd.Next(opts.vertexCount)]; }while (opts.selfEdges == false && a.Equals(b)); if (g.AddEdge(edgeFactory(a, b))) { if (g.OutDegree(a) >= opts.branchingFactor) { freeVertices.Remove(a); ++k; } ++j; } } j = k = 0; while (j < opts.treasures) { a = MazeGraphFactory.GetVertex(g, rnd); if (!(a.HasTreasure)) { a.HasTreasure = true; ++j; } } while (k < opts.startingPoints) { b = MazeGraphFactory.GetVertex(g, rnd); if (!(b.StartingPoint)) { b.StartingPoint = true; ++k; } } }
public static void FileDependency(IMutableVertexAndEdgeListGraph <string, Edge <string> > g) { // adding files and storing names string zig_cpp = "zip.cpp"; g.AddVertex(zig_cpp); string boz_h = "boz.h"; g.AddVertex(boz_h); string zag_cpp = "zag.cpp"; g.AddVertex(zag_cpp); string yow_h = "yow.h"; g.AddVertex(yow_h); string dax_h = "dax.h"; g.AddVertex(dax_h); string bar_cpp = "bar.cpp"; g.AddVertex(bar_cpp); string zow_h = "zow.h"; g.AddVertex(zow_h); string foo_cpp = "foo.cpp"; g.AddVertex(foo_cpp); string zig_o = "zig.o"; g.AddVertex(zig_o); string zag_o = "zago"; g.AddVertex(zag_o); string bar_o = "bar.o"; g.AddVertex(bar_o); string foo_o = "foo.o"; g.AddVertex(foo_o); string libzigzag_a = "libzigzig.a"; g.AddVertex(libzigzag_a); string libfoobar_a = "libfoobar.a"; g.AddVertex(libfoobar_a); string killerapp = "killerapp.exe"; g.AddVertex(killerapp); // adding dependencies g.AddEdge(new Edge <string>(dax_h, foo_cpp)); g.AddEdge(new Edge <string>(dax_h, bar_cpp)); g.AddEdge(new Edge <string>(dax_h, yow_h)); g.AddEdge(new Edge <string>(yow_h, bar_cpp)); g.AddEdge(new Edge <string>(yow_h, zag_cpp)); g.AddEdge(new Edge <string>(boz_h, bar_cpp)); g.AddEdge(new Edge <string>(boz_h, zig_cpp)); g.AddEdge(new Edge <string>(boz_h, zag_cpp)); g.AddEdge(new Edge <string>(zow_h, foo_cpp)); g.AddEdge(new Edge <string>(foo_cpp, foo_o)); g.AddEdge(new Edge <string>(foo_o, libfoobar_a)); g.AddEdge(new Edge <string>(bar_cpp, bar_o)); g.AddEdge(new Edge <string>(bar_o, libfoobar_a)); g.AddEdge(new Edge <string>(libfoobar_a, libzigzag_a)); g.AddEdge(new Edge <string>(zig_cpp, zig_o)); g.AddEdge(new Edge <string>(zig_o, libzigzag_a)); g.AddEdge(new Edge <string>(zag_cpp, zag_o)); g.AddEdge(new Edge <string>(zag_o, libzigzag_a)); g.AddEdge(new Edge <string>(libzigzag_a, killerapp)); }
public void Create(IMutableVertexAndEdgeListGraph tc) { if (tc == null) { throw new ArgumentNullException("tc"); } CondensationGraphAlgorithm algorithm = new CondensationGraphAlgorithm(this.VisitedGraph); algorithm.Create(this.cg); ArrayList vertices = new ArrayList(this.cg.get_VerticesCount()); new TopologicalSortAlgorithm(this.cg, vertices).Compute(); VertexIntDictionary dictionary = new VertexIntDictionary(); VertexIntDictionary dictionary2 = new VertexIntDictionary(); for (int i = 0; i < vertices.Count; i++) { IVertex vertex = (IVertex) vertices[i]; dictionary2.Add(vertex, i); if (!dictionary.Contains(vertex)) { dictionary.Add(vertex, 0); } } VertexListMatrix chains = new VertexListMatrix(); int num2 = -1; Label_0112: foreach (IVertex vertex2 in vertices) { if (dictionary.get_Item(vertex2) == 0) { num2 = chains.AddRow(); IVertex vertex3 = vertex2; while (true) { chains[num2].Add(vertex3); dictionary.set_Item(vertex3, 1); ArrayList list2 = this.TopoSortAdjVertices(vertex3, this.cg, dictionary2); vertex3 = this.FirstNotInChain(list2, dictionary); if (vertex3 == null) { goto Label_0112; } } } } VertexIntDictionary dictionary3 = new VertexIntDictionary(); VertexIntDictionary dictionary4 = new VertexIntDictionary(); this.SetChainPositions(chains, dictionary3, dictionary4); VertexListMatrix matrix2 = new VertexListMatrix(); matrix2.CreateObjectMatrix(this.cg.get_VerticesCount(), chains.RowCount, 0x7fffffff); if (vertices.Count > 0) { for (int j = vertices.Count - 1; j > -1; j--) { IVertex v = (IVertex) vertices[j]; foreach (IVertex vertex5 in this.TopoSortAdjVertices(v, this.cg, dictionary2)) { if (dictionary2.get_Item(vertex5) < ((int) matrix2[v.get_ID()][dictionary3.get_Item(vertex5)])) { this.LeftUnion(matrix2[v.get_ID()], matrix2[vertex5.get_ID()]); matrix2[v.get_ID()][dictionary3.get_Item(vertex5)] = dictionary2.get_Item(vertex5); } } } } ArrayList list3 = new ArrayList(); IEdgeEnumerator enumerator = this.cg.get_Edges().GetEnumerator(); while (enumerator.MoveNext()) { IEdge edge = enumerator.get_Current(); list3.Add(edge); } foreach (IEdge edge2 in list3) { this.cg.RemoveEdge(edge2); } IVertexEnumerator enumerator5 = this.cg.get_Vertices().GetEnumerator(); while (enumerator5.MoveNext()) { IVertex vertex6 = enumerator5.get_Current(); int num4 = vertex6.get_ID(); for (int k = 0; k < chains.RowCount; k++) { int num6 = (int) matrix2[num4][k]; if (num6 < 0x7fffffff) { IVertex vertex7 = (IVertex) vertices[num6]; for (int m = dictionary4.get_Item(vertex7); m < chains[k].Count; m++) { this.cg.AddEdge(vertex6, (IVertex) chains[k][m]); } } } } this.graphTransitiveClosures = new VertexVertexDictionary(); IVertexEnumerator enumerator6 = this.visitedGraph.get_Vertices().GetEnumerator(); while (enumerator6.MoveNext()) { IVertex vertex8 = enumerator6.get_Current(); if (!this.graphTransitiveClosures.Contains(vertex8)) { IVertex vertex9 = tc.AddVertex(); this.OnInitTransitiveClosureVertex(new TransitiveClosureVertexEventArgs(vertex8, vertex9)); this.graphTransitiveClosures.Add(vertex8, vertex9); } } IVertexCollection vertexs = null; IVertexEnumerator enumerator7 = this.cg.get_Vertices().GetEnumerator(); while (enumerator7.MoveNext()) { IVertex vertex10 = enumerator7.get_Current(); vertexs = (IVertexCollection) algorithm.SCCVerticesMap[vertex10.get_ID()]; if (vertexs.Count > 1) { IVertexEnumerator enumerator8 = vertexs.GetEnumerator(); while (enumerator8.MoveNext()) { IVertex vertex11 = enumerator8.get_Current(); IVertexEnumerator enumerator9 = vertexs.GetEnumerator(); while (enumerator9.MoveNext()) { IVertex vertex12 = enumerator9.get_Current(); this.OnExamineEdge(tc.AddEdge(this.graphTransitiveClosures.get_Item(vertex11), this.graphTransitiveClosures.get_Item(vertex12))); } } } IEdgeEnumerator enumerator10 = this.cg.OutEdges(vertex10).GetEnumerator(); while (enumerator10.MoveNext()) { IVertex vertex13 = enumerator10.get_Current().get_Target(); IVertexEnumerator enumerator11 = ((IVertexCollection) algorithm.SCCVerticesMap[vertex10.get_ID()]).GetEnumerator(); while (enumerator11.MoveNext()) { IVertex vertex14 = enumerator11.get_Current(); IVertexEnumerator enumerator12 = ((IVertexCollection) algorithm.SCCVerticesMap[vertex13.get_ID()]).GetEnumerator(); while (enumerator12.MoveNext()) { IVertex vertex15 = enumerator12.get_Current(); this.OnExamineEdge(tc.AddEdge(this.graphTransitiveClosures.get_Item(vertex14), this.graphTransitiveClosures.get_Item(vertex15))); } } } } }
/// <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); } }
/// <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); }