public void BiggerSituation() { SimpleGraph graph = new SimpleGraph(); Vertex A = graph.CreateVertex( "A" ); Vertex B = graph.CreateVertex( "B" ); Vertex C = graph.CreateVertex( "C" ); Vertex D = graph.CreateVertex( "D" ); Vertex E = graph.CreateVertex( "E" ); Vertex F = graph.CreateVertex( "F" ); Vertex G = graph.CreateVertex( "G" ); graph.CreateEdge( B, C ); graph.CreateEdge( A, C ); graph.CreateEdge( C, E ); graph.CreateEdge( B, E ); graph.CreateEdge( A, E ); graph.CreateEdge( E, F ); graph.CreateEdge( F, G ); Vertex[] vertices = TopologicalSort.Perform( graph ); Vertex[] expected = new Vertex[] { A, B, D, C, E, F, G }; AssertNotNull( vertices ); AssertEquals( expected, vertices ); }
public void AddVertex() { SimpleGraph graph = new SimpleGraph(); Vertex A = graph.CreateVertex( "A" ); AssertNotNull( A ); AssertEquals( "A", A.Content ); AssertEquals( 1, graph.Vertices.Length ); }
public void AddEdge() { SimpleGraph graph = new SimpleGraph(); Vertex A = graph.CreateVertex( "A" ); Vertex B = graph.CreateVertex( "B" ); Edge edge = graph.CreateEdge( A, B ); AssertNotNull( edge ); AssertEquals( A, edge.Source ); AssertEquals( B, edge.Target ); AssertEquals( 1, A.Successors.Length ); AssertEquals( 0, B.Successors.Length ); }
public void SimpleUse() { SimpleGraph graph = new SimpleGraph(); Vertex A = graph.CreateVertex( "A" ); Vertex B = graph.CreateVertex( "B" ); Vertex C = graph.CreateVertex( "C" ); Vertex D = graph.CreateVertex( "D" ); Vertex E = graph.CreateVertex( "E" ); graph.CreateEdge( A, B ); graph.CreateEdge( A, C ); graph.CreateEdge( C, D ); Vertex[] vertices = TopologicalSort.Perform( graph ); Vertex[] expected = new Vertex[] { A, E, B, C, D }; AssertNotNull( vertices ); AssertEquals( expected, vertices ); }
/// <summary> /// Arrange the Vertex making the less accessible /// vertex coming before than others. Returns the /// Vertex array with the result. /// </summary> /// <param name="graph"></param> /// <returns></returns> public static Vertex[] Perform(SimpleGraph graph) { ArrayList result = new ArrayList(); Hashtable inDegree = new Hashtable(); Queue queue = new Queue(); foreach(Edge edge in graph.Edges) { IncrementDegree(edge.Target, inDegree); } foreach(Vertex vertex in graph.Vertices) { if (ObtainInDegree(vertex, inDegree) == 0) { queue.Enqueue(vertex); } } while(queue.Count != 0) { Vertex v = (Vertex) queue.Dequeue(); result.Add(v); foreach(Vertex to in v.Successors) { if (DecrementDegree(to, inDegree) == 0) { queue.Enqueue(to); } } } return (Vertex[]) result.ToArray(typeof (Vertex)); }
protected virtual SimpleGraph BuildHandlerGraph() { SimpleGraph graph = new SimpleGraph(); foreach( IHandler handler in m_key2Handler.Values ) { graph.CreateVertex( handler.ComponentModel.Service ); } foreach( IHandler handler in m_key2Handler.Values ) { Type service = handler.ComponentModel.Service; IDependencyModel[] dependencies = handler.ComponentModel.Dependencies; foreach(IDependencyModel dependency in dependencies) { graph.CreateEdge( graph[service], graph[dependency.Service] ); } } return graph; }