public void ComputeWithRootAndTarget_Throws() { const int start1 = 1; const int end1 = 2; var graph1 = new BidirectionalGraph <int, Edge <int> >(); var algorithm1 = new HoffmanPavleyRankedShortestPathAlgorithm <int, Edge <int> >(graph1, edge => 1.0); Assert.Throws <ArgumentException>(() => algorithm1.Compute(start1)); graph1.AddVertex(start1); Assert.Throws <InvalidOperationException>(() => algorithm1.Compute(start1)); Assert.Throws <ArgumentException>(() => algorithm1.Compute(start1, end1)); var start2 = new TestVertex("1"); var end2 = new TestVertex("2"); var graph2 = new BidirectionalGraph <TestVertex, Edge <TestVertex> >(); var algorithm2 = new HoffmanPavleyRankedShortestPathAlgorithm <TestVertex, Edge <TestVertex> >(graph2, edge => 1.0); // ReSharper disable AssignNullToNotNullAttribute Assert.Throws <ArgumentNullException>(() => algorithm2.Compute(null)); Assert.Throws <ArgumentNullException>(() => algorithm2.Compute(start2, null)); Assert.Throws <ArgumentNullException>(() => algorithm2.Compute(null, end2)); Assert.Throws <ArgumentNullException>(() => algorithm2.Compute(null, null)); // ReSharper restore AssignNullToNotNullAttribute }
public IEnumerable <IEnumerable <TEdge> > HoffmanPavleyRankedShortestPath <TVertex, TEdge>( [PexAssumeNotNull] IBidirectionalGraph <TVertex, TEdge> g, [PexAssumeNotNull] Dictionary <TEdge, double> edgeWeights, TVertex rootVertex, TVertex goalVertex, int pathCount ) where TEdge : IEdge <TVertex> { //GraphConsoleSerializer.DisplayGraph((IEdgeListGraph<TVertex, TEdge>)g); PexAssert.TrueForAll(g.Edges, edgeWeights.ContainsKey); var target = new HoffmanPavleyRankedShortestPathAlgorithm <TVertex, TEdge>(g, e => edgeWeights[e]); target.ShortestPathCount = pathCount; target.Compute(rootVertex, goalVertex); double lastWeight = double.MinValue; foreach (var path in target.ComputedShortestPaths) { TestConsole.WriteLine("path: {0}", Enumerable.Sum(path, e => edgeWeights[e])); double weight = Enumerable.Sum(path, e => edgeWeights[e]); Assert.IsTrue(lastWeight <= weight, "{0} <= {1}", lastWeight, weight); Assert.AreEqual(rootVertex, Enumerable.First(path).Source); Assert.AreEqual(goalVertex, Enumerable.Last(path).Target); Assert.IsTrue(EdgeExtensions.IsPathWithoutCycles <TVertex, TEdge>(path)); lastWeight = weight; } return(target.ComputedShortestPaths); }
private static void RunHoffmanPavleyRankedShortestPathAndCheck <TVertex, TEdge>( IBidirectionalGraph <TVertex, TEdge> graph, Dictionary <TEdge, double> edgeWeights, TVertex rootVertex, TVertex targetVertex, int pathCount) where TEdge : IEdge <TVertex> { QuikGraphAssert.TrueForAll(graph.Edges, edgeWeights.ContainsKey); var target = new HoffmanPavleyRankedShortestPathAlgorithm <TVertex, TEdge>(graph, e => edgeWeights[e]) { ShortestPathCount = pathCount }; target.Compute(rootVertex, targetVertex); double lastWeight = double.MinValue; foreach (TEdge[] path in target.ComputedShortestPaths.Select(p => p.ToArray())) { double weight = path.Sum(e => edgeWeights[e]); Assert.IsTrue(lastWeight <= weight, $"{lastWeight} <= {weight}"); Assert.AreEqual(rootVertex, path.First().Source); Assert.AreEqual(targetVertex, path.Last().Target); Assert.IsTrue(path.IsPathWithoutCycles <TVertex, TEdge>()); lastWeight = weight; } }
public bool IsSubTypeOf(TypeIdentity parent, TypeIdentity child) { var algorithm = new HoffmanPavleyRankedShortestPathAlgorithm <TypeIdentity, Edge <TypeIdentity> >( m_TypeGraph, e => 1.0); algorithm.ShortestPathCount = 10; algorithm.Compute(child, parent); return(algorithm.ComputedShortestPathCount > 0); }
public void InfiniteLoop() { BidirectionalGraph <int, TaggedEdge <int, int> > graph = CreateGraph(); var algorithm = new HoffmanPavleyRankedShortestPathAlgorithm <int, TaggedEdge <int, int> >(graph, e => 1.0) { ShortestPathCount = 5 }; algorithm.Compute(5, 2); Console.WriteLine($"path: {algorithm.ComputedShortestPathCount}"); foreach (IEnumerable <TaggedEdge <int, int> > path in algorithm.ComputedShortestPaths) { foreach (TaggedEdge <int, int> edge in path) { Console.Write($"{edge}:"); } Console.WriteLine(); } #region Local function BidirectionalGraph <int, TaggedEdge <int, int> > CreateGraph() { int ii = 0; var g = new BidirectionalGraph <int, TaggedEdge <int, int> >(); g.AddVerticesAndEdge(new TaggedEdge <int, int>(0, 1, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1, 2, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2, 3, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(3, 4, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(4, 5, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(5, 0, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1, 5, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(5, 1, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2, 5, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1, 0, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2, 1, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(3, 2, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(4, 3, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(5, 4, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(0, 5, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(5, 2, ii)); return(g); } #endregion }
public void ComputeWithRootAndTarget() { const int start = 0; const int end = 1; var graph = new BidirectionalGraph <int, Edge <int> >(); graph.AddVertexRange(new[] { start, end }); var algorithm = new HoffmanPavleyRankedShortestPathAlgorithm <int, Edge <int> >(graph, edge => 1.0); Assert.DoesNotThrow(() => algorithm.Compute(start, end)); Assert.IsTrue(algorithm.TryGetRootVertex(out int root)); Assert.IsTrue(algorithm.TryGetTargetVertex(out int target)); AssertEqual(start, root); AssertEqual(end, target); }
private static void MainAction(HoffmanPavleyRankedShortestPathAlgorithm <long, TaggedEdge <long, double> > alg, IReadOnlyList <Way> ways, Dictionary <long, int> nodeWages, Random r) { var r1 = ways[r.Next(ways.Count)]; var r2 = ways[r.Next(ways.Count)]; if (r1.Nodes.Contains(r2.Start)) { return; } alg.Compute(r1.Start, r2.Start); foreach (var shortestPath in alg.ComputedShortestPaths) { foreach (var edge in shortestPath) { nodeWages[edge.Source]++; } } }
/// <summary> /// Computes the k-shortest path from <paramref name="source"/> /// <paramref name="target"/> using Hoffman-Pavley algorithm. /// </summary> /// <typeparam name="TVertex">type of the vertices</typeparam> /// <typeparam name="TEdge">type of the edges</typeparam> /// <param name="visitedGraph"></param> /// <param name="edgeWeights"></param> /// <param name="source"></param> /// <param name="target"></param> /// <param name="pathCount"></param> /// <returns></returns> public static IEnumerable <IEnumerable <TEdge> > RankedShortestPathHoffmanPavley <TVertex, TEdge>( this IBidirectionalGraph <TVertex, TEdge> visitedGraph, Func <TEdge, double> edgeWeights, TVertex source, TVertex target, int pathCount) where TEdge : IEdge <TVertex> { Contract.Requires(visitedGraph != null); Contract.Requires(edgeWeights != null); Contract.Requires(source != null && visitedGraph.ContainsVertex(source)); Contract.Requires(target != null && visitedGraph.ContainsVertex(target)); Contract.Requires(pathCount > 1); var algo = new HoffmanPavleyRankedShortestPathAlgorithm <TVertex, TEdge>(visitedGraph, edgeWeights); algo.ShortestPathCount = pathCount; algo.Compute(source, target); return(algo.ComputedShortestPaths); }
public void InfiniteLoop() { BidirectionalGraph <int, TaggedEdge <int, int> > graph = CreateGraph(); var algorithm = new HoffmanPavleyRankedShortestPathAlgorithm <int, TaggedEdge <int, int> >(graph, e => 1.0) { ShortestPathCount = 5 }; Assert.DoesNotThrow(() => algorithm.Compute(5, 2)); #region Local function BidirectionalGraph <int, TaggedEdge <int, int> > CreateGraph() { int ii = 0; var g = new BidirectionalGraph <int, TaggedEdge <int, int> >(); g.AddVerticesAndEdge(new TaggedEdge <int, int>(0, 1, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1, 2, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2, 3, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(3, 4, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(4, 5, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(5, 0, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1, 5, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(5, 1, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2, 5, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1, 0, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2, 1, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(3, 2, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(4, 3, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(5, 4, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(0, 5, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(5, 2, ii)); return(g); } #endregion }
public static List <CalculatedRoute> calculateKRoutes(string origin, string destination, List <string> cities, List <CachedSection> cachedSections, bool fastest, int k) { var graphWeightTuple = CreateBidirectionalGraph(cities, cachedSections, fastest); var pathAlgorithm = new HoffmanPavleyRankedShortestPathAlgorithm <string, TaggedEdge <string> >(graphWeightTuple.Item1, e => graphWeightTuple.Item2[e]); pathAlgorithm.ShortestPathCount = k; pathAlgorithm.Compute(origin, destination); var result = new List <CalculatedRoute>(); foreach (IEnumerable <TaggedEdge <string> > path in pathAlgorithm.ComputedShortestPaths) { var route = new CalculatedRoute(); route.Route = new List <CachedSection>(); decimal totalPrice = 0; float totalDuration = 0; foreach (TaggedEdge <string> section in path) { var cachedSection = new CachedSection(); cachedSection.Price = section.Price; totalPrice += section.Price; cachedSection.Duration = section.Duration; totalDuration += section.Duration; cachedSection.Provider = section.Provider; cachedSection.Category = section.Category; cachedSection.Weight = section.Weight; cachedSection.From = new City(section.Source); cachedSection.To = new City(section.Target); route.Route.Add(cachedSection); } route.Price = totalPrice; route.Duration = totalDuration; result.Add(route); } return(result); }
public void InfiniteLoop13111() { int ii = 0; var mvGraph2 = new BidirectionalGraph <int, TaggedEdge <int, int> >(); mvGraph2.AddVerticesAndEdge(new TaggedEdge <int, int>(0, 1, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge <int, int>(1, 2, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge <int, int>(2, 3, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge <int, int>(3, 4, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge <int, int>(4, 5, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge <int, int>(5, 0, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge <int, int>(1, 5, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge <int, int>(5, 1, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge <int, int>(2, 5, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge <int, int>(1, 0, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge <int, int>(2, 1, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge <int, int>(3, 2, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge <int, int>(4, 3, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge <int, int>(5, 4, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge <int, int>(0, 5, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge <int, int>(5, 2, ii++)); var test1 = new HoffmanPavleyRankedShortestPathAlgorithm <int, TaggedEdge <int, int> >(mvGraph2, E => 1.0); test1.ShortestPathCount = 5; test1.Compute(5, 2); TestConsole.WriteLine("path: {0}", test1.ComputedShortestPathCount); foreach (var path in test1.ComputedShortestPaths) { foreach (var edge in path) { Console.Write(edge + ":"); } TestConsole.WriteLine(); } }
public void NotEnoughPaths() { BidirectionalGraph <int, TaggedEdge <int, int> > graph = CreateGraph(); var algorithm = new HoffmanPavleyRankedShortestPathAlgorithm <int, TaggedEdge <int, int> >(graph, e => 1.0) { ShortestPathCount = 5 }; algorithm.Compute(1626, 1965); Assert.AreEqual(4, algorithm.ComputedShortestPathCount); #region Local function BidirectionalGraph <int, TaggedEdge <int, int> > CreateGraph() { int ii = 0; var g = new BidirectionalGraph <int, TaggedEdge <int, int> >(); g.AddVerticesAndEdge(new TaggedEdge <int, int>(493, 495, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(495, 493, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(497, 499, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(499, 497, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(499, 501, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(501, 499, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(501, 503, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(503, 501, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(503, 505, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(505, 503, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(505, 507, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(507, 505, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(507, 509, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(509, 507, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(509, 511, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(511, 509, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2747, 2749, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2749, 2747, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2749, 2751, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2751, 2749, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2751, 2753, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2753, 2751, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2753, 2755, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2755, 2753, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2755, 2757, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2757, 2755, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2757, 2759, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2759, 2757, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2761, 2763, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2763, 2761, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2765, 2767, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2767, 2765, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2763, 2765, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2765, 2763, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(654, 978, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(978, 654, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(978, 1302, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1302, 978, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1302, 1626, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1626, 1302, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1626, 1950, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1950, 1626, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1950, 2274, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2274, 1950, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2274, 2598, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2598, 2274, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(513, 676, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(676, 513, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2767, 2608, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2608, 2767, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2287, 2608, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2608, 2287, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(676, 999, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(999, 676, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1321, 1643, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1643, 1321, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1643, 1965, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1965, 1643, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1965, 2287, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2287, 1965, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(999, 1321, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1321, 999, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2745, 2747, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2747, 2745, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(650, 491, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(491, 650, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(650, 970, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(970, 650, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2258, 2582, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2582, 2258, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(970, 1291, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1291, 970, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1935, 2258, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2258, 1935, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1291, 1613, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1613, 1291, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1613, 1935, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(1935, 1613, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2582, 2745, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2745, 2582, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(495, 497, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(497, 495, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(511, 513, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(513, 511, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(491, 493, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(493, 491, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(491, 654, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(654, 491, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2761, 2598, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2598, 2761, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2761, 2759, ii++)); g.AddVerticesAndEdge(new TaggedEdge <int, int>(2759, 2761, ii)); return(g); } #endregion }
public void NotEnoughPaths() { int ii = 0; var graph = new BidirectionalGraph <int, TaggedEdge <int, int> >(); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(493, 495, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(495, 493, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(497, 499, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(499, 497, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(499, 501, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(501, 499, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(501, 503, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(503, 501, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(503, 505, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(505, 503, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(505, 507, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(507, 505, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(507, 509, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(509, 507, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(509, 511, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(511, 509, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2747, 2749, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2749, 2747, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2749, 2751, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2751, 2749, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2751, 2753, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2753, 2751, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2753, 2755, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2755, 2753, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2755, 2757, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2757, 2755, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2757, 2759, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2759, 2757, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2761, 2763, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2763, 2761, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2765, 2767, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2767, 2765, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2763, 2765, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2765, 2763, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(654, 978, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(978, 654, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(978, 1302, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(1302, 978, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(1302, 1626, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(1626, 1302, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(1626, 1950, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(1950, 1626, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(1950, 2274, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2274, 1950, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2274, 2598, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2598, 2274, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(513, 676, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(676, 513, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2767, 2608, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2608, 2767, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2287, 2608, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2608, 2287, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(676, 999, ii++));; graph.AddVerticesAndEdge(new TaggedEdge <int, int>(999, 676, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(1321, 1643, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(1643, 1321, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(1643, 1965, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(1965, 1643, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(1965, 2287, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2287, 1965, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(999, 1321, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(1321, 999, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2745, 2747, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2747, 2745, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(650, 491, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(491, 650, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(650, 970, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(970, 650, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2258, 2582, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2582, 2258, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(970, 1291, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(1291, 970, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(1935, 2258, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2258, 1935, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(1291, 1613, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(1613, 1291, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(1613, 1935, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(1935, 1613, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2582, 2745, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2745, 2582, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(495, 497, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(497, 495, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(511, 513, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(513, 511, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(491, 493, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(493, 491, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(491, 654, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(654, 491, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2761, 2598, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2598, 2761, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2761, 2759, ii++)); graph.AddVerticesAndEdge(new TaggedEdge <int, int>(2759, 2761, ii++)); var test1 = new HoffmanPavleyRankedShortestPathAlgorithm <int, TaggedEdge <int, int> >(graph, e => 1.0); test1.ShortestPathCount = 5; test1.Compute(1626, 1965); Assert.Equal(4, test1.ComputedShortestPathCount); TestConsole.WriteLine("path: {0}", test1.ComputedShortestPathCount); foreach (var path in test1.ComputedShortestPaths) { foreach (var edge in path) { Console.Write(edge + ":"); } TestConsole.WriteLine(); } }
public void NotEnoughPaths() { int ii = 0; var graph = new BidirectionalGraph<int, TaggedEdge<int, int>>(); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(493, 495, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(495, 493, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(497, 499, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(499, 497, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(499, 501, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(501, 499, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(501, 503, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(503, 501, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(503, 505, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(505, 503, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(505, 507, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(507, 505, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(507, 509, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(509, 507, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(509, 511, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(511, 509, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2747, 2749, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2749, 2747, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2749, 2751, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2751, 2749, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2751, 2753, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2753, 2751, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2753, 2755, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2755, 2753, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2755, 2757, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2757, 2755, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2757, 2759, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2759, 2757, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2761, 2763, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2763, 2761, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2765, 2767, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2767, 2765, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2763, 2765, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2765, 2763, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(654, 978, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(978, 654, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(978, 1302, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(1302, 978, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(1302, 1626, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(1626, 1302, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(1626, 1950, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(1950, 1626, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(1950, 2274, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2274, 1950, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2274, 2598, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2598, 2274, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(513, 676, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(676, 513, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2767, 2608, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2608, 2767, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2287, 2608, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2608, 2287, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(676, 999, ii++)); ; graph.AddVerticesAndEdge(new TaggedEdge<int, int>(999, 676, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(1321, 1643, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(1643, 1321, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(1643, 1965, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(1965, 1643, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(1965, 2287, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2287, 1965, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(999, 1321, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(1321, 999, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2745, 2747, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2747, 2745, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(650, 491, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(491, 650, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(650, 970, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(970, 650, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2258, 2582, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2582, 2258, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(970, 1291, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(1291, 970, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(1935, 2258, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2258, 1935, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(1291, 1613, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(1613, 1291, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(1613, 1935, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(1935, 1613, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2582, 2745, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2745, 2582, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(495, 497, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(497, 495, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(511, 513, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(513, 511, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(491, 493, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(493, 491, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(491, 654, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(654, 491, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2761, 2598, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2598, 2761, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2761, 2759, ii++)); graph.AddVerticesAndEdge(new TaggedEdge<int, int>(2759, 2761, ii++)); var test1 = new HoffmanPavleyRankedShortestPathAlgorithm<int, TaggedEdge<int, int>>(graph, e => 1.0); test1.ShortestPathCount = 5; test1.Compute(1626, 1965); Assert.AreEqual(4, test1.ComputedShortestPathCount); Console.WriteLine("path: {0}", test1.ComputedShortestPathCount); foreach (var path in test1.ComputedShortestPaths) { foreach (var edge in path) Console.Write(edge + ":"); Console.WriteLine(); } }
public void InfiniteLoop13111() { int ii = 0; var mvGraph2 = new BidirectionalGraph<int, TaggedEdge<int, int>>(); mvGraph2.AddVerticesAndEdge(new TaggedEdge<int, int>(0, 1, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge<int, int>(1, 2, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge<int, int>(2, 3, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge<int, int>(3, 4, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge<int, int>(4, 5, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge<int, int>(5, 0, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge<int, int>(1, 5, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge<int, int>(5, 1, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge<int, int>(2, 5, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge<int, int>(1, 0, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge<int, int>(2, 1, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge<int, int>(3, 2, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge<int, int>(4, 3, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge<int, int>(5, 4, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge<int, int>(0, 5, ii++)); mvGraph2.AddVerticesAndEdge(new TaggedEdge<int, int>(5, 2, ii++)); var test1 = new HoffmanPavleyRankedShortestPathAlgorithm<int, TaggedEdge<int, int>>(mvGraph2, E => 1.0); test1.ShortestPathCount = 5; test1.Compute(5, 2); Console.WriteLine("path: {0}", test1.ComputedShortestPathCount); foreach (var path in test1.ComputedShortestPaths) { foreach(var edge in path) Console.Write(edge + ":"); Console.WriteLine(); } }
public Graph GetSubGraph(string group) { // Extract the header vertices var subGraph = new Graph(); // Add the vertices foreach (var vertex in this.Vertices) { if (vertex.Groups.Contains(group)) { subGraph.Vertices.Add(vertex); if (!vertex.PositionForGroup.ContainsKey(group)) { vertex.PositionForGroup[group] = vertex.Position; } vertex.SelectedGroup = group; vertex.DisplayPosition = vertex.PositionForGroup[group]; } } foreach (var vertex in subGraph.Vertices) { vertex.DisplayPosition = vertex.PositionForGroup[group]; } // Process for each pair and add edges foreach (var srcVertex in subGraph.Vertices) { foreach (var dstVertex in subGraph.Vertices) { var algorithm = new HoffmanPavleyRankedShortestPathAlgorithm <Vertex, Edge>(this, edge => 1); algorithm.Compute(srcVertex, dstVertex); foreach (var path in algorithm.ComputedShortestPaths) { // Convert to list to avoid multiple enumerations. var pathList = path as IList <Edge> ?? path.ToList(); var src = pathList.First().Source; foreach (var edge in pathList) { if (subGraph.Vertices.Contains(edge.Target)) { var connectorPostions = srcVertex.ConnectorPositions[group][dstVertex.Key]; var newEdge = new Edge(src, edge.Target) { ConnectorPositions = connectorPostions ?? new EdgeConnectorPositions() }; subGraph.edges.AddUnique(newEdge); src = edge.Target; } } } } } subGraph.DefaultGroup = group; return(subGraph); }
public bool IsSubTypeOf(TypeIdentity parent, TypeIdentity child) { var algorithm = new HoffmanPavleyRankedShortestPathAlgorithm<TypeIdentity, Edge<TypeIdentity>>( m_TypeGraph, e => 1.0); algorithm.ShortestPathCount = 10; algorithm.Compute(child, parent); return algorithm.ComputedShortestPathCount > 0; }
static void Main(string[] args) { //Dictionary to hold all the airports, using dictionary so we can retrieve the value with the string id in flight Dictionary <String, Airports> airports = new Dictionary <String, Airports>(); //List of all the flights List <Route> flights = new List <Route>(); //Data for airports String s = "OSL,Oslo Gardermoen;BCN, Barcelona El Prat;LGW,London Gatwick;CDG,Paris Charles De Gaulle;ARN,Stockholm Arland;CPH,Copenhagen Kastrup;AGP,Malaga Costa del Sol Airport;AMS,Amsterdam Airport Schipol;FRA,Frankfurt Airport;JFK,New York John F. Kennedy International Airport;LAX,Los Angeles International Airport;FCO,Roma Leonardo da Vinci - Fiumicino Airport;TRD,Trondheim Værnes;BGO,Bergen Flesland;TXL,Berlin Tegel Airport;BKK,Suvarnabhumi Airport;JNB,Johanesburg O. R. Tambo International Airport;BNE,Brisbane Airport;GIG,Rio de Janeiro - Antonio Carlos Jobim International Airport;KEF,Reykjavik Keflavík International Airport"; //Splitting the string and creating airports objects, and putting them into the list String[] data = s.Split(';'); foreach (string a in data) { var airport = new Airports(); String[] b = a.Split(','); airport.AirportID = b[0]; airport.AirportName = b[1]; airports.Add(b[0], airport); } //Flight data String x = "30.09.2017 09:00:00,30.09.2017 10:40:00,499,OSL,LGW,1;30.09.2017 09:30:00,30.09.2017 12:30:00,599,OSL,BCN,2;30.09.2017 10:00:00,30.09.2017 12:00:00,299,OSL,FRA,3;30.09.2017 07:00:00,30.09.2017 08:00:00,199,OSL,TRD,4;30.09.2017 08:00:00,30.09.2017 09:00:00,199,OSL,BGO,5;30.09.2017 12:00:00,30.09.2017 15:00:00,549,OSL,CDG,6;30.09.2017 10:00:00,30.09.2017 11:00:00,199,BGO,OSL,5;30.09.2017 09:00:00,30.09.2017 10:00:00,149,BGO,TRD,7;30.09.2017 09:00:00,30.09.2017 10:00:00,199,TRD,OSL,4;30.09.2017 11:00:00,30.09.2017 12:00:00,149,TRD,BGO,7;30.09.2017 12:00:00,30.09.2017 13:40:00,499,LGW,OSL,1;30.09.2017 11:00:00,30.09.2017 13:00:00,299,LGW,FRA,8;30.09.2017 13:00:00,30.09.2017 16:00:00,449,LGW,BCN,9;30.09.2017 14:30:00,30.09.2017 17:30:00,599,BCN,OSL,2;30.09.2017 17:00:00,30.09.2017 20:00:00,449,BCN,LGW,9;30.09.2017 19:00:00,30.09.2017 21:00:00,349,BCN,FRA,10;30.09.2017 13:30:00,30.09.2017 15:30:00,299,FRA,OSL,3;30.09.2017 14:30:00,30.09.2017 16:30:00,299,FRA,LGW,8;30.09.2017 23:00:00,01.10.2017 01:00:00,349,FRA,BCN,10;30.09.2017 16:00:00,30.09.2017 19:00:00,549,CDG,OSL,6"; //Splitting the string and creating flight objects and adding them to the list String[] arrayOfFlights = x.Split(';'); foreach (String a in arrayOfFlights) { String[] b = a.Split(','); var f = new Route(); var source = new Airports(); var target = new Airports(); String[] departureDateTime = b[0].Split(' '); String[] departureDate = departureDateTime[0].Split('.'); String[] departureTime = departureDateTime[1].Split(':'); String[] arrivalDateTime = b[1].Split(' '); String[] arrivalDate = arrivalDateTime[0].Split('.'); String[] arrivalTime = arrivalDateTime[1].Split(':'); f.DepartureTime = new DateTime(int.Parse(departureDate[2]), int.Parse(departureDate[1]), int.Parse(departureDate[0]), int.Parse(departureTime[0]), int.Parse(departureTime[1]), int.Parse(departureTime[2])); f.ArrivalTime = new DateTime(int.Parse(arrivalDate[2]), int.Parse(arrivalDate[1]), int.Parse(arrivalDate[0]), int.Parse(arrivalTime[0]), int.Parse(arrivalTime[1]), int.Parse(arrivalTime[2])); airports.TryGetValue(b[3], out source); f.Source = source; airports.TryGetValue(b[4], out target); f.Target = target; flights.Add(f);//Adding to list } //Graph where the edges go both ways var g = new BidirectionalGraph <Airports, Route>(); //Adding airports and routes to graph foreach (var airport in airports) { g.AddVertex(airport.Value); } foreach (Route r in flights) { g.AddEdge(r); } //Func that returns the traveltime of the route arg Func <Route, double> travelTime = t => (t.ArrivalTime - t.DepartureTime).TotalMinutes; //Source airport and target Airports sourceA; airports.TryGetValue("OSL", out sourceA); Airports targetA; airports.TryGetValue("BCN", out targetA); DateTime start = DateTime.Now; //Using HoffmanPavleyRankedShortestPath to find the 2 shortest paths HoffmanPavleyRankedShortestPathAlgorithm <Airports, Route> hoffmanAlgorithm = new HoffmanPavleyRankedShortestPathAlgorithm <Airports, Route>(g, travelTime); //List to hold all the possible routes List <IEnumerable <Route> > listOfAvailableFlights = new List <IEnumerable <Route> >(); try { //Getting the 5 shortest paths, does not take into account waiting time at the airports hoffmanAlgorithm.ShortestPathCount = 10; hoffmanAlgorithm.SetRootVertex(sourceA); hoffmanAlgorithm.Compute(sourceA, targetA); //Adding the path to the list if it is viable foreach (IEnumerable <Route> path in hoffmanAlgorithm.ComputedShortestPaths) { Boolean viable = true; //Helper variable Route p = null; //Helper variable to hold the previous flight foreach (var e in path) { if (p != null) { if (e.DepartureTime < p.ArrivalTime.AddMinutes(30)) { viable = false; //Not adding to list if a flight leaves before the previous one lands + 30 minutes } } p = e;//Setting the helper variable } if (viable) { listOfAvailableFlights.Add(path); //Adding to list } } } catch (Exception e) { Debug.WriteLine(e); } Debug.WriteLine((DateTime.Now - start).TotalMilliseconds); //Traversing the list foreach (var path in listOfAvailableFlights) { //Retrieving the first and last route in the path Route last = new Route(); last = path.LastOrDefault(); Route first = new Route(); first = path.FirstOrDefault(); double timespan = 0; //Calculating the total travel time, including downtime at the airports if (!last.Equals(first)) { timespan = (last.ArrivalTime - first.DepartureTime).TotalHours; } else { timespan = (first.ArrivalTime - first.DepartureTime).TotalHours; } //Printing the path foreach (var e in path) { Debug.WriteLine(e); } //Printing the total waiting time Debug.WriteLine(timespan); Debug.WriteLine("--------------------------------"); } }