public static IUnweightedDirectedGraph <TVertex> ToGraph <TVertex>( this IEnumerable <IUnweightedEndpointPair <TVertex> > endpointPairs ) { var graph = new UnweightedDirectedGraph <TVertex>(); foreach (var endpointPair in endpointPairs) { graph.AddEdge(endpointPair.Origin, endpointPair.Destination); } return(graph); }
public async Task <IUnweightedDirectedGraph <string> > GetUnweightedDirectedGraphAsync() { var edges = await EdgeService.GetEdgesAsync(); var graph = new UnweightedDirectedGraph <string>(); foreach (var edge in edges) { graph.AddEdge(edge.Origin, edge.Destination); } return(graph); }
public static IUnweightedDirectedGraph <TVertex> DepthFirstSearch <TVertex>( this IUnweightedDirectedGraph <TVertex> graph, TVertex origin ) { if (graph == null) { throw new ArgumentNullException(nameof(graph)); } if (origin == null) { throw new ArgumentNullException(nameof(origin)); } if (!graph.Vertices.Contains(origin)) { throw new ArgumentException(); } var visited = new HashSet <TVertex>(); var output = new UnweightedDirectedGraph <TVertex>(); var stack = new Stack <TVertex>(); visited.Add(origin); stack.Push(origin); while (stack.Count > 0) { var vertex = stack.Pop(); bool Predicate(TVertex successorVertex) { return(!visited.Contains(successorVertex)); } foreach (var successorVertex in graph.SuccessorVertices(vertex).Where(Predicate)) { output.AddEdge(vertex, successorVertex); visited.Add(successorVertex); stack.Push(successorVertex); } } return(output); }
public void Test() { var g = new UnweightedDirectedGraph <string>(); g.AddVertex("a"); g.AddEdge(Edge <string> .Of("b", "c")); g.AddEdge(Edge <string> .Of("b", "e")); g.AddEdge(Edge <string> .Of("e", "f")); g.AddEdge(Edge <string> .Of("f", "d")); var node = g.NodeOf("c"); var node2 = g.NodeOf("f"); var allNodes = g.AllNodes(); var nbrs = g.Neighbors(Node <string> .Of("b")).ToList(); var incidentEdges = g.IncidentEdges(Node <string> .Of("b")).ToList(); var bfs = new BFS <string>(g); var res = bfs.Execute("b"); var tmp = res; }
private static void DoDebug() { #region Miscellaneous Algorithms WriteLine("All Simple Paths - Default"); Stopwatch.Restart(); var allSimplePaths = WeightedDirectedGraph.AllSimplePaths( "HARROW & WEALDSTONE", "ELEPHANT & CASTLE", EdgeAdder, 25 ); Stopwatch.Stop(); WriteLine($"Time: {Stopwatch.Elapsed}\n"); WriteLine("Breadth First Search - Default"); Stopwatch.Restart(); UnweightedDirectedGraph.BreadthFirstSearch( "HARROW & WEALDSTONE" ); Stopwatch.Stop(); WriteLine($"Time: {Stopwatch.Elapsed}\n"); WriteLine("Breadth First Search - Graph Enumerator"); Stopwatch.Restart(); new BreadthFirstSearchGraphEnumerator <string>( UnweightedDirectedGraph, "HARROW & WEALDSTONE" ).ToEnumerable(); Stopwatch.Stop(); WriteLine($"Time: {Stopwatch.Elapsed}\n"); WriteLine("Breadth First Search - Path Algorithm"); Stopwatch.Restart(); new BreadthFirstSearchUnweightedDirectedPathAlgorithm <string>( UnweightedDirectedGraph ).PathCollection("HARROW & WEALDSTONE"); Stopwatch.Stop(); WriteLine($"Time: {Stopwatch.Elapsed}\n"); WriteLine("Depth First Search - Default"); Stopwatch.Restart(); UnweightedDirectedGraph.DepthFirstSearch( "HARROW & WEALDSTONE" ); Stopwatch.Stop(); WriteLine($"Time: {Stopwatch.Elapsed}\n"); WriteLine("Depth First Search - Graph Enumerator"); Stopwatch.Restart(); new DepthFirstSearchGraphEnumerator <string>( UnweightedDirectedGraph, "HARROW & WEALDSTONE" ).ToEnumerable(); Stopwatch.Stop(); WriteLine($"Time: {Stopwatch.Elapsed}\n"); WriteLine("Depth First Search - Path Algorithm"); Stopwatch.Restart(); new DepthFirstSearchUnweightedDirectedPathAlgorithm <string>( UnweightedDirectedGraph ).PathCollection("HARROW & WEALDSTONE"); Stopwatch.Stop(); WriteLine($"Time: {Stopwatch.Elapsed}\n"); #endregion #region Weighted Sum Algorithm var allSimplePathsList = allSimplePaths.ToList(); var extremizeTypes = new Dictionary <ObjectiveType, ExtremizeType> { { ObjectiveType.Time, ExtremizeType.Minimize }, { ObjectiveType.Distance, ExtremizeType.Minimize }, { ObjectiveType.Comfortability, ExtremizeType.Maximize }, { ObjectiveType.Reliability, ExtremizeType.Maximize }, { ObjectiveType.Connections, ExtremizeType.Minimize } }; var weights = new Dictionary <ObjectiveType, double> { { ObjectiveType.Comfortability, 100 }, { ObjectiveType.Reliability, 50 } }; WriteLine("Weighted Sum Shortest Path - Default"); Stopwatch.Restart(); allSimplePathsList.WeightedSumShortestPath(weights, WeightedSumShortestPathObjectiveSelector); Stopwatch.Stop(); WriteLine($"Time: {Stopwatch.Elapsed}\n"); WriteLine("Weighted Sum Shortest Paths - Default"); Stopwatch.Restart(); allSimplePathsList.WeightedSumShortestPaths(extremizeTypes, WeightedSumShortestPathsObjectiveSelector); Stopwatch.Stop(); WriteLine($"Time: {Stopwatch.Elapsed}\n"); #endregion #region Dijkstra's Algorithm EdgeComparer.NoOfTotalComparisons = 0; EdgeComparer.NoOfInteractiveComparisons = 0; WriteLine("Dijkstra Shortest Paths - Default"); Stopwatch.Restart(); WeightedDirectedGraph.DijkstraShortestPaths( "HARROW & WEALDSTONE", EdgeAdder, EdgeComparer ); Stopwatch.Stop(); WriteLine( $"Time: {Stopwatch.Elapsed}, Interactive Comparisons: {EdgeComparer.NoOfInteractiveComparisons}, Total Comparisons: {EdgeComparer.NoOfTotalComparisons}\n" ); EdgeComparer.NoOfTotalComparisons = 0; EdgeComparer.NoOfInteractiveComparisons = 0; WriteLine("Dijkstra Shortest Paths - Path Algorithm"); Stopwatch.Restart(); new DijkstraShortestWeightedDirectedPathAlgorithm <string, Edge>( WeightedDirectedGraph, EdgeAdder, EdgeComparer ).PathCollection("HARROW & WEALDSTONE"); Stopwatch.Stop(); WriteLine( $"Time: {Stopwatch.Elapsed}, Interactive Comparisons: {EdgeComparer.NoOfInteractiveComparisons}, Total Comparisons: {EdgeComparer.NoOfTotalComparisons}\n" ); #endregion }