public BellmanFord(WeightedGraph graph, int s) { this.G = graph; G.ValidateVertex(s); this.s = s; dis = new int[G.V]; Pre = new int[G.V]; for (int i = 0; i < dis.Length; i++) { dis[i] = int.MaxValue; Pre[i] = -1; } dis[0] = 0; Pre[s] = 0; //进行V-1次松弛操作 for (int j = 1; j < G.V; j++) { for (int i = 0; i < G.V; i++) { foreach (var item in G.GetAdj(i)) { if (dis[i] != int.MaxValue) { int temp = dis[i] + G.GetWeight(i, item); if (temp < dis[item]) { dis[item] = temp; Pre[item] = i; } } } } } //再进行一次松弛操作,判断是否有负权边 for (int i = 0; i < G.V; i++) { foreach (var item in G.GetAdj(i)) { if (dis[i] != int.MaxValue) { int temp = dis[i] + G.GetWeight(i, item); if (temp < dis[item]) { hasNegCycle = true; break; } } } } }
public Folyed(WeightedGraph graph) { this.G = graph; dis = new int[G.V, G.V]; Pre = new int[G.V]; for (int i = 0; i < dis.GetLength(0); i++) { for (int j = 0; j < dis.GetLength(1); j++) { dis[i, j] = int.MaxValue; } dis[i, i] = 0; foreach (var j in G.GetAdj(i)) { dis[i, j] = G.GetWeight(i, j); } } //进行V-1次松弛操作 for (int j = 1; j < G.V; j++) { //以k为原点 for (int k = 0; k < G.V; k++) { dis[k, k] = 0; //k为原点,每条边的松弛操作 for (int i = 0; i < G.V; i++) { foreach (var item in G.GetAdj(i)) { if (dis[k, i] != int.MaxValue && dis[i, item] != int.MaxValue) { int temp = dis[k, i] + dis[i, item]; if (temp < dis[k, item]) { dis[k, item] = temp; } } } } } } //判断是否有负权环 for (int i = 0; i < G.V; i++) { if (dis[i, i] < 0) { hasNegCycle = true; } } }
public Dijkstra(WeightedGraph graph, int s) { this.G = graph; G.ValidateVertex(s); this.s = s; dis = new int[G.V]; Pre = new int[G.V]; for (int i = 0; i < dis.Length; i++) { dis[i] = int.MaxValue; Pre[i] = -1; } dis[0] = 0; Pre[s] = 0; visited = new bool[G.V]; while (true) { int curDis = int.MaxValue, cur = -1; //1.遍历dis数组,找到未被定下的最小的节点 for (int i = 0; i < G.V; i++) { if (!visited[i] && dis[i] < curDis) { cur = i; curDis = dis[i]; } } if (cur == -1) { break; } //2.确定过这个最小节点的最短路径 visited[cur] = true; //3.根据这个节点的最短路的大小,更新其他节点路径的长度 foreach (var item in G.GetAdj(cur)) { if (!visited[item]) { int temp = dis[cur] + G.GetWeight(cur, item); if (temp < dis[item]) { dis[item] = temp; Pre[item] = cur; } } } } }
public void GetAllPaths(WeightedGraph graph, string startVertex) { List <List <string> > paths = new List <List <string> >(); List <List <int> > pathWeights = new List <List <int> >(); List <string> path = new List <string>(); List <int> pathWeight = new List <int>(); foreach (string v in graph.Vertices) { if (v.Equals(startVertex)) { continue; } path.Clear(); if (predecessorDict[v] == null) { continue; } string v2 = v; while (predecessorDict[v2] != null) { path.Add(predecessorDict[v2]); v2 = predecessorDict[v2]; } path.Reverse(); for (int i = 0; i < path.Count; i++) { string v3 = path[i]; int w = i != path.Count - 1 ? graph.GetWeight(v3, path[i + 1]) : graph.GetWeight(v3, v); pathWeight.Add(w); } paths.Add(path); pathWeights.Add(pathWeight); } }
public static void CalcShortestPath(WeightedGraph graph) { Dictionary <string, int> verticesDict = new Dictionary <string, int>(); List <string> chosenPath = new List <string>(); Dictionary <string, string> predecessorDict = new Dictionary <string, string>(); foreach (string vertix in graph.Vertices) { if (!vertix.Equals(graph.startVertex)) { verticesDict.Add(vertix, int.MaxValue); } } verticesDict.Add(graph.startVertex, 0); predecessorDict.Add(graph.startVertex, null); List <string> queue = new List <string>(); queue.AddRange(graph.Vertices); //queue.Remove(graph.startVertex); while (queue.Count != 0) { string u = null; int minimum = int.MaxValue; foreach (string v in queue) { if (verticesDict[v] < minimum) { minimum = verticesDict[v]; u = v; } } queue.Remove(u); foreach (string v in graph.adjacentVertices[u]) { if (verticesDict[u] + graph.GetWeight(u, v) < verticesDict[v]) { verticesDict[v] = verticesDict[u] + graph.GetWeight(u, v); //verticesDict[u] = verticesDict[v] predecessorDict[v] = u; } chosenPath.Add(u); } } List <string> path = new List <string>(); foreach (string v in graph.Vertices) { if (v.Equals(graph.startVertex)) { continue; } path.Clear(); if (predecessorDict[v] == null) { Console.WriteLine("Do mjesta {0} se ne može doći iz mjesta {1}!", v, graph.startVertex); continue; } string v2 = v; while (predecessorDict[v2] != null) { path.Add(predecessorDict[v2]); v2 = predecessorDict[v2]; } string line = "{0} [{1}]: "; int wSum = 0; //Console.Write("{0} [{1}]: ", v); path.Reverse(); for (int i = 0; i < path.Count; i++) { string v3 = path[i]; int w = i != path.Count - 1 ? graph.GetWeight(v3, path[i + 1]) : graph.GetWeight(v3, v); line += string.Format("{0} --[{1}]--> ", v3, w); wSum += w; //Console.Write("{0} --[{1}]-->", v3, w); } line += v; //Console.WriteLine(v); Console.WriteLine(line, v, wSum); } }
public void CalcShortestPath(WeightedGraph graph, string startVertex, string endVertex) { verticesDict.Clear(); predecessorDict.Clear(); foreach (string vertix in graph.Vertices) { if (!vertix.Equals(startVertex)) { verticesDict.Add(vertix, int.MaxValue); } } verticesDict.Add(startVertex, 0); predecessorDict.Add(startVertex, null); List <string> queue = new List <string>(); queue.AddRange(graph.Vertices); while (queue.Count != 0) { string u = null; int minimum = int.MaxValue; foreach (string v in queue) { if (verticesDict[v] < minimum) { minimum = verticesDict[v]; u = v; } } if (u != null) { queue.Remove(u); foreach (string v in graph.adjacentVertices[u]) { if ((graph.Connected(u, v)) && (verticesDict[u] + graph.GetWeight(u, v) < verticesDict[v])) { verticesDict[v] = verticesDict[u] + graph.GetWeight(u, v); predecessorDict[v] = u; } } } else { break; } } path.Clear(); pathWeights.Clear(); string v2 = endVertex; if (predecessorDict.ContainsKey(v2)) { while (predecessorDict[v2] != null) { path.Add(predecessorDict[v2]); v2 = predecessorDict[v2]; } path.Reverse(); for (int i = 0; i < path.Count; i++) { string v3 = path[i]; int w = i != path.Count - 1 ? graph.GetWeight(v3, path[i + 1]) : graph.GetWeight(v3, endVertex); pathWeights.Add(w); } } else { path.Add(null); PathWeights.Add(int.MaxValue); } }