private static int[] BFS_Dijkstra1(GraphWeithedAdj graph, int s) { int[] distTo = new int[graph.Vertexes.Length]; for (int v = 0; v < graph.Vertexes.Length; v++) { distTo[v] = Int32.MaxValue; } distTo[s] = 0; // puth in priority queue key(distance) & value(list of index of vertex) List<int> l = new List<int>(); l.Add(s); pq.Add(distTo[s], l); while (pq.Count != 0) { List<int> listU = pq[pq.Keys.First()]; pq.Remove(pq.Keys.First()); foreach (int u in listU) { foreach (WeightEdge0 e in graph.Vertexes[u]) { int uFrom = u; int vTo = e.VertexTo; int distSum = distTo[uFrom] + e.Weight; if (distTo[vTo] <= distSum) { continue; } distTo[vTo] = distSum; if (pq.ContainsKey(distTo[vTo])) { pq[distTo[vTo]].Add(vTo); } else { List<int> ll = new List<int>(); ll.Add(vTo); pq.Add(distTo[vTo], ll); } } } } for (int i = 0; i < distTo.Length; i++) { distTo[i] = (distTo[i] == Int32.MaxValue ? -1 : distTo[i]); } return distTo; }
public DataWeightedGraphs(string fileName) { string filePath = Utils.GetFullFilePath(fileName); try { using (StreamReader reader = new StreamReader(filePath)) { string line; int iEdge = 0; int iGraph = -1; int countVerticles; int countEdges = 0; line = reader.ReadLine(); _count = int.Parse(line); _GraphsWeithedAdj = new GraphWeithedAdj[_count]; while ((line = reader.ReadLine()) != null) { if (line == "") { continue; } if (countEdges == 0) { iGraph++; iEdge = 0; string[] args = line.Split(' '); countVerticles = int.Parse(args[0]); countEdges = int.Parse(args[1]); _GraphsWeithedAdj[iGraph] = new GraphWeithedAdj(); _GraphsWeithedAdj[iGraph].Vertexes = new List<WeightEdge0>[countVerticles]; for (int i = 0; i < _GraphsWeithedAdj[iGraph].Vertexes.Length; i++) { _GraphsWeithedAdj[iGraph].Vertexes[i] = new List<WeightEdge0>(); } continue; } string[] edge = line.Split(' '); int v0 = int.Parse(edge[0]); int v1 = int.Parse(edge[1]); int w = int.Parse(edge[2]); if (iEdge == 0) { _GraphsWeithedAdj[iGraph].start = v1 - 1; _GraphsWeithedAdj[iGraph].finish = v0 - 1; _GraphsWeithedAdj[iGraph].weight = w; } _GraphsWeithedAdj[iGraph].Vertexes[v0 - 1].Add( new WeightEdge0() { VertexTo = v1 - 1, Weight = w }); countEdges--; iEdge++; } } } catch (Exception e) { Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } }