/// <summary> /// Checks whether there is a path from one vertex to another. /// </summary> private static bool HasPath <T>(IVertexAndEdgeListGraph <T, Edge <T> > graph, T source, T target) { var dfs = graph.ShortestPathsDijkstra(edge => 1, source); IEnumerable <Edge <T> > path; bool result = dfs.Invoke(target, out path); return(result); }
public void PDFCreatorTest() { Matrix matrix = new Matrix(new int[, ] { { 0, 1, 2, 3 }, { 1, 0, 1, 2 }, { 2, 1, 0, 4 }, { 3, 2, 4, 0 } }); (this.graph, this.edgeCost) = GraphCreator.Create(matrix); TryFunc <int, IEnumerable <Edge <int> > > tryGetPath = graph.ShortestPathsDijkstra(this.edgeCost, 0); PdfGenerator.Generate(DotGenerator <int, Edge <int> > .GetDotCode(this.graph, tryGetPath), "test"); Assert.IsTrue(File.Exists("test.pdf")); File.Delete("test.pdf"); }
public static IEnumerable <TEdge> FindShortestPath <TVertex, TEdge>( IVertexAndEdgeListGraph <TVertex, TEdge> graph, TVertex startVertex, TVertex endVertex) where TEdge : class, IEdge <TVertex> { Func <TEdge, double> edgeCost = e => 1; // extension build arround algorithm 'command pattern': var tryGetPaths = graph.ShortestPathsDijkstra(edgeWeights: edgeCost, source: startVertex); // query path for given vertices IEnumerable <TEdge> edgePath; if (!tryGetPaths(endVertex, out edgePath)) { return(Enumerable.Empty <TEdge>()); } return(edgePath); }
private TryFunc <int, IEnumerable <Edge <int> > > createTryGetPath(Matrix matrix) { (IVertexAndEdgeListGraph <int, Edge <int> > graph, Func <Edge <int>, double> edgeCost) = new GraphCreator(matrix).GetResult(); return(graph.ShortestPathsDijkstra(edgeCost, 0)); }
private void calculatePath(IVertexAndEdgeListGraph<string, Edge<string>> graph, string root, string target, int index) { if (root != target) { if (_nodesInPath.Count != index + 1) { while (_nodesInPath.Count != index + 1) { _nodesInPath.Add(new List<string>()); } } _nodesInPath[index] = new List<string>(); //IVertexAndEdgeListGraph<string, Edge<string>> graph = networkGraph; Func<Edge<String>, double> edgeCost = e => 1; // constant cost Address targ = Address.Parse(target); if (targ.subnet != myAddr.subnet) target = targ.network + "." + targ.subnet + ".*"; //string root = _CCmsg[1]; TryFunc<string, System.Collections.Generic.IEnumerable<QuickGraph.Edge<string>>> tryGetPaths = graph.ShortestPathsDijkstra(edgeCost, root); //string target = _CCmsg[2]; IEnumerable<Edge<string>> path; if (tryGetPaths(target, out path)) { lock (_nodesInPath) { SetText("Wyznaczona trasa od " + root + " do " + target + ":"); if (nodesInPath.Count != index + 1) { while (nodesInPath.Count != index + 1) { nodesInPath.Add(new List<string>()); } } nodesInPath[index] = new List<string>(); nodesInPath[index].Add(path.First().Source); foreach (Edge<string> edge in path) { Address src; if (Address.TryParse(edge.Source, out src)) { Address tempdest; if (!Address.TryParse(edge.Target, out tempdest)) { String[] srcArr = edge.Source.Split('.'); String[] destArr = edge.Target.Split('.'); if (destArr[1] != srcArr[1] && int.Parse(srcArr[1]) == myAddr.subnet) { foreach (KeyValuePair<Address, Address> kvp in subnetConnections) { if (kvp.Key.ToString() == src.ToString()) { nodesInPath[index].Add(kvp.Value.ToString()); } } } } } SetText(edge.ToString()); nodesInPath[index].Add(edge.Target); String[] _arr = edge.Target.Split('.'); if (_arr[2] != "*") _nodesInPath[index].Add(edge.Target); } } //pyta każdego LRM o to, czy jest wolne łącze do LRM następnego w kolejce //nie pyta się ostatniego LRM w ścieżce, zakładam że jak w jedną stronę jest połączenie to i w drugą jest for (int i = 0; i < nodesInPath[index].Count - 1; i++) { string[] srcArr = nodesInPath[index][i].Split('.'); if (int.Parse(srcArr[1]) == myAddr.subnet) { List<String> _msg = new List<String>(); _msg.Add("IS_LINK_AVAILABLE"); _msg.Add(nodesInPath[index][i + 1]); _msg.Add(String.Empty + index); SPacket _pck = new SPacket(myAddr.ToString(), nodesInPath[index][i], _msg); whatToSendQueue.Enqueue(_pck); } } } else { //gdy nie ma ścieżki string ccAddr = myAddr.network + "." + myAddr.subnet + ".1"; string _routeMsg = "NO_ROUTE"; SPacket packet = new SPacket(myAddr.ToString(), ccAddr, _routeMsg); whatToSendQueue.Enqueue(packet); } } else { List<string> _msg = new List<string>(); _msg.Add("ROUTE"); _msg.Add(root); SPacket pck = new SPacket(myAddr.ToString(), myAddr.network + "." + myAddr.subnet + ".1", _msg); whatToSendQueue.Enqueue(pck); } }