public void BuildInducedGraph(List <int> verticesIndexes) { foreach (var index in verticesIndexes) { foreach (var incidentEdge in VerticesList[index - 1].AdjacencyList) { var adjacencyVertex = incidentEdge.IncidentTo.AdjacencyList; adjacencyVertex.Remove(adjacencyVertex.First(edge => edge.IncidentTo.Index == index)); } for (int i = 0; i < EdgesList.Count; i++) { if (EdgesList[i].First.Index == index || EdgesList[i].Second.Index == index) { EdgesList.RemoveAt(i); } } } foreach (var vertexToDeleteIndex in verticesIndexes) { VerticesList.RemoveAt(vertexToDeleteIndex - 1); } Size -= 1; }
/// <summary> /// Алгоритм Джонсона. Сложность 0(V^2*lgV + VE), если Фибоначчиевая куча, или 0(VE*lgV). /// Вычисление кратчайших путей между всеми парами вершин /// </summary> public string JohnsonAlgorithm() { Size += 1; VerticesList.Add(new Vertex(Size)); Vertex curVertexFrom = VerticesList[Size - 1]; for (int i = 0; i < Size - 1; i++) { Vertex curVertexTo = VerticesList[i]; IncidentEdge curEdge = new IncidentEdge(curVertexTo, 0); curVertexFrom.AdjacencyList.Add(curEdge); } if (BellmanFordAlgorithm(curVertexFrom) == false) { Console.WriteLine("Входной граф содержит цикл с отрицательным весом"); } else { foreach (var vertex in VerticesList) { foreach (var incidentEdge in vertex.AdjacencyList) { incidentEdge.Weight = incidentEdge.Weight + vertex.Distance - incidentEdge.IncidentTo.Distance; } } VerticesList.RemoveAt(Size - 1); Size -= 1; int[] Distances = new int[Size]; for (int i = 0; i < Size; i++) { Distances[i] = VerticesList[i].Distance; } int[,] matrix = new int[Size, Size]; for (int i = 0; i < Size; i++) { Dijkstra(VerticesList[i]); for (int j = 0; j < Size; j++) { matrix[i, j] = VerticesList[j].Distance + Distances[j] - Distances[i]; } } StringBuilder output = new StringBuilder(); for (int i = 0; i < Size; i++) { for (int j = 0; j < Size; j++) { output.Append(matrix[i, j] + " "); } output.Append("\n"); } Console.WriteLine(output); return(output.ToString()); } return(null); }