public static TryFunc <TVertex, IEnumerable <TEdge> > ShortestPathsDag <TVertex, TEdge>( #if !NET20 this #endif IVertexAndEdgeListGraph <TVertex, TEdge> visitedGraph, Func <TEdge, double> edgeWeights, TVertex source ) where TEdge : IEdge <TVertex> { Contract.Requires(visitedGraph != null); Contract.Requires(edgeWeights != null); Contract.Requires(source != null); var algorithm = new DagShortestPathAlgorithm <TVertex, TEdge>(visitedGraph, edgeWeights); var predecessorRecorder = new VertexPredecessorRecorderObserver <TVertex, TEdge>(); using (predecessorRecorder.Attach(algorithm)) algorithm.Compute(source); var predecessors = predecessorRecorder.VertexPredecessors; return(delegate(TVertex v, out IEnumerable <TEdge> edges) { return EdgeExtensions.TryGetPath(predecessors, v, out edges); }); }
public void GetVertexColor_Throws() { var graph = new AdjacencyGraph <int, Edge <int> >(); graph.AddVertex(0); var algorithm = new DagShortestPathAlgorithm <int, Edge <int> >(graph, edge => 1.0); algorithm.Compute(0); // ReSharper disable once ReturnValueOfPureMethodIsNotUsed Assert.Throws <VertexNotFoundException>(() => algorithm.GetVertexColor(1)); }
private static void RunDagShortestPathAndCheck <TVertex, TEdge>( [NotNull] IVertexListGraph <TVertex, TEdge> graph, [NotNull] TVertex root, [NotNull] IDistanceRelaxer relaxer) where TEdge : IEdge <TVertex> { var algorithm = new DagShortestPathAlgorithm <TVertex, TEdge>( graph, _ => 1.0, relaxer); algorithm.InitializeVertex += vertex => { Assert.AreEqual(GraphColor.White, algorithm.VerticesColors[vertex]); }; algorithm.DiscoverVertex += vertex => { Assert.AreEqual(GraphColor.Gray, algorithm.VerticesColors[vertex]); }; algorithm.StartVertex += vertex => { Assert.AreNotEqual(GraphColor.Black, algorithm.VerticesColors[vertex]); }; algorithm.ExamineVertex += vertex => { Assert.AreEqual(GraphColor.Gray, algorithm.VerticesColors[vertex]); }; algorithm.FinishVertex += vertex => { Assert.AreEqual(GraphColor.Black, algorithm.VerticesColors[vertex]); }; var predecessors = new VertexPredecessorRecorderObserver <TVertex, TEdge>(); using (predecessors.Attach(algorithm)) algorithm.Compute(root); Assert.AreEqual(graph.VertexCount, algorithm.VerticesColors.Count); foreach (TVertex vertex in graph.Vertices) { Assert.AreEqual(GraphColor.Black, algorithm.VerticesColors[vertex]); } CollectionAssert.IsNotEmpty(algorithm.GetDistances()); Assert.AreEqual(graph.VertexCount, algorithm.GetDistances().Count()); Verify(algorithm, predecessors); }
public static (bool, IDictionary <string, double>) Get(AdjacencyGraph <string, TaggedEdge <string, string> > graph, string root) { if (!graph.IsDirectedAcyclicGraph()) { return(false, new Dictionary <string, double>()); } Func <TaggedEdge <string, string>, double> Weights = e => double.Parse(e.Tag); var algorithm = new DagShortestPathAlgorithm <string, TaggedEdge <string, string> >(graph, Weights); algorithm.Compute(root); return(true, algorithm.Distances); }
public static (bool, IDictionary <int, double>) Get(AdjacencyGraph <int, Edge <int> > graph, int root) { if (!graph.IsDirectedAcyclicGraph()) { return(false, new Dictionary <int, double>()); } Func <Edge <int>, double> Weights = e => 1.0; var algorithm = new DagShortestPathAlgorithm <int, Edge <int> >(graph, Weights); algorithm.Compute(root); return(true, algorithm.Distances); }
public void GetVertexColor() { var graph = new AdjacencyGraph <int, Edge <int> >(); graph.AddVerticesAndEdge(new Edge <int>(1, 2)); var algorithm = new DagShortestPathAlgorithm <int, Edge <int> >(graph, _ => 1.0); algorithm.Compute(1); Assert.AreEqual(GraphColor.Black, algorithm.GetVertexColor(1)); Assert.AreEqual(GraphColor.Black, algorithm.GetVertexColor(2)); }
private static void Search <TVertex, TEdge>( [NotNull] IVertexListGraph <TVertex, TEdge> graph, [NotNull] TVertex root, [NotNull] IDistanceRelaxer relaxer) where TEdge : IEdge <TVertex> { var algorithm = new DagShortestPathAlgorithm <TVertex, TEdge>( graph, e => 1, relaxer); var predecessors = new VertexPredecessorRecorderObserver <TVertex, TEdge>(); using (predecessors.Attach(algorithm)) algorithm.Compute(root); Verify(algorithm, predecessors); }
public static DagShortestPathAlgorithm <T, Edge <T> > CreateAlgorithmAndMaybeDoComputation <T>( [NotNull] ContractScenario <T> scenario) { var graph = new AdjacencyGraph <T, Edge <T> >(); graph.AddVerticesAndEdgeRange(scenario.EdgesInGraph.Select(e => new Edge <T>(e.Source, e.Target))); graph.AddVertexRange(scenario.SingleVerticesInGraph); double Weights(Edge <T> e) => 1.0; var algorithm = new DagShortestPathAlgorithm <T, Edge <T> >(graph, Weights); if (scenario.DoComputation) { algorithm.Compute(scenario.Root); } return(algorithm); }