protected override void InternalCompute() { if (this.VisitedGraph.VertexCount == 0) { return; } this.Initialize(); TVertex rootVertex; if (!this.TryGetRootVertex(out rootVertex)) { // enqueue roots foreach (var root in AlgoUtility.Roots(this.VisitedGraph)) { this.EnqueueRoot(root); } } else // enqueue select root only { this.Visit(rootVertex); } this.FlushVisitQueue(); }
public void Compute(IVertexListGraph <string, Edge <string> > g) { // is this a dag ? bool isDag = AlgoUtility.IsDirectedAcyclicGraph(g); IDistanceRelaxer relaxer = new ShortestDistanceRelaxer(); List <string> vertices = new List <string>(g.Vertices); foreach (string root in vertices) { if (isDag) { Search(g, root, relaxer); } else { try { Search(g, root, relaxer); } catch (NonAcyclicGraphException) { Console.WriteLine("NonAcyclicGraphException caught (as expected)"); } } } }
public void ComputeCriticalPath(IVertexListGraph <string, Edge <string> > g) { // is this a dag ? bool isDag = AlgoUtility.IsDirectedAcyclicGraph(g); var relaxer = new CriticalDistanceRelaxer(); var vertices = new List <string>(g.Vertices); foreach (string root in vertices) { if (isDag) { Search(g, root, relaxer); } else { try { Search(g, root, relaxer); Assert.Fail("should have found the acyclic graph"); } catch (NonAcyclicGraphException) { Console.WriteLine("NonAcyclicGraphException caught (as expected)"); } } } }
private void CheckComponentCount(IVertexAndEdgeListGraph <string, Edge <string> > g) { // check number of vertices = number of storngly connected components int components = AlgoUtility.StronglyConnectedComponents <string, Edge <string> >(g, new Dictionary <string, int>()); Assert.AreEqual(components, algo.CondensatedGraph.VertexCount, "ComponentCount does not match"); }
/// <summary> /// Computes the number of eulerian trail in the graph. If negative, /// there is an eulerian circuit. /// </summary> /// <param name="g"></param> /// <returns>number of eulerian trails</returns> public static int ComputeEulerianPathCount(IVertexAndEdgeListGraph g) { if (g == null) { throw new ArgumentNullException("g"); } int odd = AlgoUtility.OddVertices(g).Count; if (odd == 0) { return(-1); } if (odd % 2 != 0) { return(0); } else if (odd == 0) { return(1); } else { return(odd / 2); } }
private void CheckDAG(IVertexAndEdgeListGraph <string, Edge <string> > g) { // check it's a dag try { AlgoUtility.TopologicalSort(this.algo.CondensatedGraph); } catch (NonAcyclicGraphException) { Assert.Fail("Graph is not a DAG."); } }
/// <summary> /// Adds a child vertex to the tree /// </summary> /// <param name="parent">parent vertex</param> /// <returns>created vertex</returns> /// <exception cref="ArgumentNullException">parent is a null reference</exception> /// <exception cref="NonAcyclicGraphException"> /// if <c>AllowCycles</c> is false and the edge creates a cycle /// </exception> public virtual IVertex AddChild(IVertex parent) { IVertex v = this.MutableWrapped.AddVertex(); this.MutableWrapped.AddEdge(parent, v); // check non acyclic if (!this.allowCycles) { AlgoUtility.CheckAcyclic(this.MutableWrapped, parent); } return(v); }
/// <summary> /// Generate file containing command lines to run ikvmc including names of referenced dlls. /// </summary> /// <param name="g"></param> /// <param name="path"></param> private static void GenerateIkvmcRunScript(BidirectionalGraph <string, Edge <string> > g, string path) { StreamWriter sw = new StreamWriter(path); foreach (string vertex in AlgoUtility.TopologicalSort <string, Edge <string> >(g)) { IEnumerable <string> names = g.InEdges(vertex).Select <Edge <string>, string>(item => item.Source); string references = ""; foreach (string name in names) { references += " -r:" + name.Replace(".jar", ".dll"); } string commandLine = "ikvmc " + vertex + " -target:library" + references; sw.WriteLine(commandLine); } sw.Close(); }
protected override void InternalCompute() { this.Initialize(); TVertex rootVertex; if (!this.TryGetRootVertex(out rootVertex)) { // enqueue all roots foreach (var root in AlgoUtility.Roots(this.VisitedGraph)) { this.EnqueueRoot(root); } } else { this.EnqueueRoot(rootVertex); } this.FlushVisitQueue(); }
public void ComputeNoInit(TVertex s) { ICollection <TVertex> orderedVertices = AlgoUtility.TopologicalSort <TVertex, TEdge>(this.VisitedGraph); OnDiscoverVertex(s); foreach (TVertex v in orderedVertices) { OnExamineVertex(v); foreach (TEdge e in VisitedGraph.OutEdges(v)) { OnDiscoverVertex(e.Target); bool decreased = Relax(e); if (decreased) { OnTreeEdge(e); } else { OnEdgeNotRelaxed(e); } } OnFinishVertex(v); } }
public void Scenario() { AdjacencyGraph <string, Edge <string> > graph = new AdjacencyGraph <string, Edge <string> >(true); // Add some vertices to the graph graph.AddVertex("A"); graph.AddVertex("B"); graph.AddVertex("C"); graph.AddVertex("D"); graph.AddVertex("E"); graph.AddVertex("F"); graph.AddVertex("G"); graph.AddVertex("H"); graph.AddVertex("I"); graph.AddVertex("J"); // Create the edges Edge <string> a_b = new Edge <string>("A", "B"); Edge <string> a_d = new Edge <string>("A", "D"); Edge <string> b_a = new Edge <string>("B", "A"); Edge <string> b_c = new Edge <string>("B", "C"); Edge <string> b_e = new Edge <string>("B", "E"); Edge <string> c_b = new Edge <string>("C", "B"); Edge <string> c_f = new Edge <string>("C", "F"); Edge <string> c_j = new Edge <string>("C", "J"); Edge <string> d_e = new Edge <string>("D", "E"); Edge <string> d_g = new Edge <string>("D", "G"); Edge <string> e_d = new Edge <string>("E", "D"); Edge <string> e_f = new Edge <string>("E", "F"); Edge <string> e_h = new Edge <string>("E", "H"); Edge <string> f_i = new Edge <string>("F", "I"); Edge <string> f_j = new Edge <string>("F", "J"); Edge <string> g_d = new Edge <string>("G", "D"); Edge <string> g_h = new Edge <string>("G", "H"); Edge <string> h_g = new Edge <string>("H", "G"); Edge <string> h_i = new Edge <string>("H", "I"); Edge <string> i_f = new Edge <string>("I", "F"); Edge <string> i_j = new Edge <string>("I", "J"); Edge <string> i_h = new Edge <string>("I", "H"); Edge <string> j_f = new Edge <string>("J", "F"); // Add the edges graph.AddEdge(a_b); graph.AddEdge(a_d); graph.AddEdge(b_a); graph.AddEdge(b_c); graph.AddEdge(b_e); graph.AddEdge(c_b); graph.AddEdge(c_f); graph.AddEdge(c_j); graph.AddEdge(d_e); graph.AddEdge(d_g); graph.AddEdge(e_d); graph.AddEdge(e_f); graph.AddEdge(e_h); graph.AddEdge(f_i); graph.AddEdge(f_j); graph.AddEdge(g_d); graph.AddEdge(g_h); graph.AddEdge(h_g); graph.AddEdge(h_i); graph.AddEdge(i_f); graph.AddEdge(i_h); graph.AddEdge(i_j); graph.AddEdge(j_f); // Define some weights to the edges Dictionary <Edge <string>, double> edgeCost = new Dictionary <Edge <string>, double>(graph.EdgeCount); edgeCost.Add(a_b, 4); edgeCost.Add(a_d, 1); edgeCost.Add(b_a, 74); edgeCost.Add(b_c, 2); edgeCost.Add(b_e, 12); edgeCost.Add(c_b, 12); edgeCost.Add(c_f, 74); edgeCost.Add(c_j, 12); edgeCost.Add(d_e, 32); edgeCost.Add(d_g, 22); edgeCost.Add(e_d, 66); edgeCost.Add(e_f, 76); edgeCost.Add(e_h, 33); edgeCost.Add(f_i, 11); edgeCost.Add(f_j, 21); edgeCost.Add(g_d, 12); edgeCost.Add(g_h, 10); edgeCost.Add(h_g, 2); edgeCost.Add(h_i, 72); edgeCost.Add(i_f, 31); edgeCost.Add(i_h, 18); edgeCost.Add(i_j, 7); edgeCost.Add(j_f, 8); // We want to use Dijkstra on this graph DijkstraShortestPathAlgorithm <string, Edge <string> > dijkstra = new DijkstraShortestPathAlgorithm <string, Edge <string> >(graph, edgeCost); // Attach a Vertex Predecessor Recorder Observer to give us the paths VertexPredecessorRecorderObserver <string, Edge <string> > predecessorObserver = new VertexPredecessorRecorderObserver <string, Edge <string> >(); using (ObserverScope.Create <IVertexPredecessorRecorderAlgorithm <string, Edge <string> > >(dijkstra, predecessorObserver)) { // Run the algorithm with A set to be the source dijkstra.Compute("A"); } foreach (KeyValuePair <string, Edge <string> > kvp in predecessorObserver.VertexPredecessors) { Console.WriteLine("If you want to get to {0} you have to enter through the in edge {1}", kvp.Key, kvp.Value); } foreach (string v in graph.Vertices) { double distance = AlgoUtility.ComputePredecessorCost( predecessorObserver.VertexPredecessors, edgeCost, v); Console.WriteLine("A -> {0}: {1}", v, distance); } }
public IVertexEnumerable Leaves(RunInvokerVertex v) { return(AlgoUtility.Sinks(this.graph, v)); }
public void Test() { // create a new adjacency graph AdjacencyGraph g = new AdjacencyGraph(false); // adding files and storing names IVertex boz_h = g.AddVertex(); Names[boz_h] = "boz.h"; IVertex zag_cpp = g.AddVertex(); Names[zag_cpp] = "zag.cpp"; IVertex yow_h = g.AddVertex(); Names[yow_h] = "yow.h"; IVertex dax_h = g.AddVertex(); Names[dax_h] = "dax.h"; IVertex bar_cpp = g.AddVertex(); Names[bar_cpp] = "bar.cpp"; IVertex zow_h = g.AddVertex(); Names[zow_h] = "zow.h"; IVertex foo_cpp = g.AddVertex(); Names[foo_cpp] = "foo.cpp"; IVertex zig_o = g.AddVertex(); Names[zig_o] = "zig.o"; IVertex zag_o = g.AddVertex(); Names[zag_o] = "zago"; IVertex bar_o = g.AddVertex(); Names[bar_o] = "bar.o"; IVertex foo_o = g.AddVertex(); Names[foo_o] = "foo.o"; IVertex libzigzag_a = g.AddVertex(); Names[libzigzag_a] = "libzigzig.a"; IVertex libfoobar_a = g.AddVertex(); Names[libfoobar_a] = "libfoobar.a"; IVertex killerapp = g.AddVertex(); Names[killerapp] = "killerapp"; IVertex zig_cpp = g.AddVertex(); Names[zig_cpp] = "zip.cpp"; // adding dependencies g.AddEdge(dax_h, foo_cpp); g.AddEdge(dax_h, bar_cpp); g.AddEdge(dax_h, yow_h); g.AddEdge(yow_h, bar_cpp); g.AddEdge(yow_h, zag_cpp); g.AddEdge(boz_h, bar_cpp); g.AddEdge(boz_h, zig_cpp); g.AddEdge(boz_h, zag_cpp); g.AddEdge(zow_h, foo_cpp); g.AddEdge(foo_cpp, foo_o); g.AddEdge(foo_o, libfoobar_a); g.AddEdge(bar_cpp, bar_o); g.AddEdge(bar_o, libfoobar_a); g.AddEdge(libfoobar_a, libzigzag_a); g.AddEdge(zig_cpp, zig_o); g.AddEdge(zig_o, libzigzag_a); g.AddEdge(zag_cpp, zag_o); g.AddEdge(zag_o, libzigzag_a); g.AddEdge(libzigzag_a, killerapp); Console.WriteLine("Leaves"); foreach (IVertex v in AlgoUtility.Sinks(g)) { Console.WriteLine(Names[v]); } Console.WriteLine("Leaves of zag_o"); foreach (IVertex v in AlgoUtility.Sinks(g, zag_o)) { Console.WriteLine(Names[v]); } // outputing graph to png GraphvizAlgorithm gw = new GraphvizAlgorithm( g, // graph to draw ".", // output file path GraphvizImageType.Png // output file type ); // outputing to graph. gw.Write("filedependency"); // adding custom vertex settings gw.FormatVertex += new FormatVertexEventHandler(gw_FormatVertex); gw.Write("fp"); }
/// <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); }