/// <summary> /// Tworzenie macierzy odleglosci pomiedzy wszystkimi parami wierzcholkow w grafie spojnym /// </summary> /// <param name="from"></param> Graf, w ktorym liczymy odleglosci miedzy wszystkimi parami wierzcholkow /// <returns></returns> Macierz odleglosci miedzy wszystkimi parami wierzcholkow public static int[,] distancesMatrix(GraphMatrix from) { GraphMatrix graph = CreateBiggestCoherent(from); int nodes = graph.NodesNr; int[,] distances = new int[nodes, nodes]; List <int> path = new List <int>(); int total_dist = 0; int dist = 0; for (int i = 0; i < nodes; ++i) { for (int j = 0; j < nodes; ++j) { distances[i, j] = 0; } } for (int i = 0; i < nodes; ++i) { for (int j = 0; j < nodes; ++j) { if (i == j) { distances[i, j] = 0; } else if (distances[i, j] != 0) { continue; } else { path = PathFinding.Dijkstra(graph, i, j); if (path.Count == 0) { continue; } if (path.Count == 1) { distances[i, j] = distances[j, i] = graph.getWeight(i, path[0]); } else { for (int k = 0; k < path.Count - 1; ++k) { dist += graph.getWeight(path[k], path[k + 1]); } total_dist = dist + graph.getWeight(i, path[0]); distances[i, j] = distances[j, i] = total_dist; total_dist = dist = 0; path.Clear(); } } } } return(distances); }
/// <summary> /// Tworzenie macierzy odleglosci pomiedzy wszystkimi parami wierzcholkow w grafie spojnym skierowanym, wykorzystuje algorytm Dijkstry /// </summary> /// <param name="from"></param> Graf skierowany, w ktorym liczymy odleglosci miedzy wszystkimi parami wierzcholkow /// <returns></returns> distances - Macierz odleglosci miedzy wszystkimi parami wierzcholkow public static int[,] distancesDirectedMatrix(DirectedGraphMatrix graph) { int nodes = graph.NodesNr; int[,] distances = new int[nodes, nodes]; List <int> path = new List <int>(); int total_dist = 0; int dist = 0; const int INF = int.MaxValue - 10000; for (int i = 0; i < nodes; ++i) { for (int j = 0; j < nodes; ++j) { if (i == j) { distances[i, j] = 0; } else { distances[i, j] = INF; } } } for (int i = 0; i < nodes; ++i) { for (int j = 0; j < nodes; ++j) { if (i == j) { continue; } else { path = PathFinding.Dijkstra(graph, i, j); if (path.Count == 0) { continue; } else if (path.Count == 1) { distances[i, j] = graph.getWeight(i, path[0]); path.Clear(); } else { for (int k = 0; k < path.Count - 1; ++k) { dist += graph.getWeight(path[k], path[k + 1]); } total_dist = dist + graph.getWeight(i, path[0]); distances[i, j] = total_dist; total_dist = dist = 0; path.Clear(); } } } } return(distances); }