public GraphWeithedAdjAlt0(string fileName) { string filePath = Utils.GetFullFilePath(fileName); try { using (StreamReader reader = new StreamReader(filePath)) { string line; int iGraph = -1; int countVerticles; int countEdges = 0; line = reader.ReadLine(); Count = int.Parse(line); Graphs = new GraphAdjAlt1[Count]; while ((line = reader.ReadLine()) != null) { if (line == "") { continue; } if (countEdges == 0) { iGraph++; string[] args = line.Split(' '); countVerticles = int.Parse(args[0]); countEdges = int.Parse(args[1]); Graphs[iGraph] = new GraphAdjAlt1(); Graphs[iGraph].v = new List<WeightEdge1>[countVerticles]; for (int i = 0; i < Graphs[iGraph].v.Length; i++) { Graphs[iGraph].v[i] = new List<WeightEdge1>(); } continue; } string[] edge = line.Split(' '); int v0 = int.Parse(edge[0]) - 1; int v1 = int.Parse(edge[1]) - 1; int w = int.Parse(edge[2]); Graphs[iGraph].v[v0].Add(new WeightEdge1() { VertexTo = v1, Weight = w}); countEdges--; } } } catch (Exception e) { Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } }
private static int ShortestPathsExits(GraphAdjAlt1 g, int s) { int n = g.v.Length; dist = new int[n]; for (int i = 0; i < n; i++) { dist[i] = Int32.MaxValue; } dist[s] = 0; for (int i = 0; i < n - 1; i++) { haveChanging = false; for (int v = 0; v < n; v++) { if (g.v[v] == null) { continue; } foreach (WeightEdge1 e in g.v[v]) { Update(v, e); if (haveChanging == false) { break; } } } if (haveChanging == false) { break; } } for (int v = 0; v < n; v++) { if (g.v[v] == null) { continue; } foreach (WeightEdge1 e in g.v[v]) { Update(v, e); if (haveChanging == false) { return -1; } } } return 1; }
private static int BellmanFordAlgorithm(GraphAdjAlt1 g, int s) { return ShortestPathsExits(g, s); }