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); }
private void LoadTXTFormatGraph(string graphFile) { FileStream ifs = new FileStream(graphFile, FileMode.Open); StreamReader sr = new StreamReader(ifs); string line = ""; string[] tokens = null; line = sr.ReadLine(); line = line.Trim(); while (line.StartsWith("c")) { line = sr.ReadLine(); line = line.Trim(); } tokens = line.Split(' '); int numVertices = int.Parse(tokens[1]); // Convert.ToInt32 int numEdges = int.Parse(tokens[2]); if (numVertices < 0 || numEdges < 0) { throw new Exception("Number nodes or edges is a negative"); } data = new VerticesList(numVertices); residualNetwork = new UMatrix(numVertices); while (!string.IsNullOrEmpty(line = sr.ReadLine())) { line = line.Trim(); tokens = line.Split(' '); int incidentFromIndex = int.Parse(tokens[0]); int incidentToIndex = int.Parse(tokens[1]); ushort capacity = ushort.Parse(tokens[2]); if (incidentFromIndex < 1 || incidentFromIndex > numVertices || incidentToIndex < 1 || incidentToIndex > numVertices) { throw new Exception("The vertex parameter is not in the range 0...this.numVertices"); } data.SetValue(incidentFromIndex, incidentToIndex, capacity); residualNetwork.SetValue(incidentFromIndex - 1, incidentToIndex - 1, capacity); } sr.Close(); ifs.Close(); NumberVertices = numVertices; NumberEdges = numEdges; }
/// <summary> Создаёт глубокую копию данного объекта </summary> public override object Clone() { var clone = new UndirectedGraph(); VerticesList.ForEach(v => clone.AddVertex(new Vertex(v.Name))); foreach (var edge in EdgesList) { var v1 = clone.Vertices.First(edge.Vertex1.Equals); var v2 = clone.Vertices.First(edge.Vertex2.Equals); clone.AddEdge(new UndirectedEdge(v1, v2)); } return(clone); }
/// <summary> Создаёт глубокую копию данного объекта </summary> public override object Clone() { var clone = new DirectedWeightedGraph(); VerticesList.ForEach(v => clone.AddVertex(new Vertex(v.Name))); foreach (var edge in EdgesList) { var v1 = clone.Vertices.Single(v => v.Equals(edge.Vertex1)); var v2 = clone.Vertices.First(v => v.Equals(edge.Vertex2)); clone.AddEdge(new DirectedWeightedEdge(v1, v2, edge.Weight)); } return(clone); }