static public Path[] runAlgorithm(Graph graph_, Vertex begin, Vertex end) { graph = graph_; Path[] widestPath = new Path[graph.Vertices.Length]; PriorityQueue.Heap<Vertex> verticesHeap = new PriorityQueue.Heap<Vertex>(); initialize(begin); verticesHeap.initialise(graph.Vertices.Length); for (int i = 0; i < graph.Vertices.Length; i++) { verticesHeap.insertElement(new PriorityQueue.Element<Vertex>(graph.Vertices[i].CumulatedWeight, graph.Vertices[i])); } while (verticesHeap.NumberOfElements != 0) { Vertex currentVertex = verticesHeap.deleteMax().Data; if (currentVertex.CumulatedWeight == 0) break; foreach (Edge e in currentVertex.EdgesOut) { Vertex neighbor = e.End; double alternate = Math.Max(neighbor.CumulatedWeight, Math.Min(currentVertex.CumulatedWeight, e.Weight)); if (alternate > neighbor.CumulatedWeight) { neighbor.CumulatedWeight = alternate; neighbor.Prev = currentVertex; } } sortHeap(ref verticesHeap); } widestPath[0] = generatePath(begin, end); return widestPath; }
public void load(List<string> textFile) { vertices = new Vertex[int.Parse(getDataFromLine(textFile[0], 1))]; if (vertices.Length == 0) throw new Exception("Zerowa liczba wierzchołków!"); for (int i = 0; i < vertices.Length; i++) { vertices[i] = new Vertex(i+1); } edges = new Edge[int.Parse(getDataFromLine(textFile[1], 1))]; if (edges.Length == 0) throw new Exception("Zerowa liczba krawędzi!"); for (int i = 0; i < edges.Length; i++) { int edge_id = int.Parse(getDataFromLine(textFile[2 + i], 0)); int begin_id = int.Parse(getDataFromLine(textFile[2 + i], 1)); int end_id = int.Parse(getDataFromLine(textFile[2 + i], 2)); edges[i] = new Edge(edge_id, vertices[begin_id - 1], vertices[end_id - 1]); edges[i].Begin.addEdgeOut(edges[i]); } }
static private Path generatePath(Vertex begin, Vertex end) { Path path = new Path(graph.Vertices.Length); Vertex currentVertex = end; while (currentVertex != begin) { if (currentVertex == null) { return null; } path.push(currentVertex); currentVertex = currentVertex.Prev; } path.push(begin); return path; }
static private void initialize(Vertex begin) { for (int i = 0; i < graph.Vertices.Length; i++) { graph.Vertices[i].CumulatedWeight = 0; graph.Vertices[i].Prev = null; } graph.Vertices[begin.Id - 1].CumulatedWeight = infinity; begin.Prev = begin; }
//static private void print(double[,] arr) //{ // var rowCount = arr.GetLength(0); // var colCount = arr.GetLength(1); // for (int row = 0; row < rowCount; row++) // { // for (int col = 0; col < colCount; col++) // Console.Write(String.Format("{0}\t", arr[row, col])); // Console.WriteLine(); // } //} //static private void print(Vertex[,] arr) //{ // var rowCount = arr.GetLength(0); // var colCount = arr.GetLength(1); // for (int row = 0; row < rowCount; row++) // { // for (int col = 0; col < colCount; col++) // if(arr[row,col] != null) // { // Console.Write(String.Format("{0}\t", arr[row, col].Id)); // }else // { // Console.Write(String.Format("{0}\t", "-")); // } // Console.WriteLine(); // } //} static public Path[] runAlgorithm(Graph graph_, Vertex begin) { graph = graph_; Path[] shortestPaths = new Path[graph.Vertices.Length]; initialize(); algorithmLogic(); int len = graph.Vertices.Length; for (int i = 0; i < len; i++) { shortestPaths[i] = generatePath(begin, graph.Vertices[i]); } return shortestPaths; }
static public Path[] runAlgorithm(Graph graph_, Vertex begin, Vertex end) { graph = graph_; Path[] shortestPaths = new Path[graph.Vertices.Length]; initialize(); algorithmLogic(); int len = graph.Vertices.Length; shortestPaths[0] = generatePath(begin, end); return shortestPaths; }