private IEnumerable <IDirectedEdge> cycle; //edgeTo中是否含有负权重环 public BellmanFordSP(IEdgeWeightedDIgraph G, int s) : base(G, s) { onQ = new bool[G.V]; queue = new Chapter1.Queue <int>(); queue.Enqueue(s); onQ[s] = true; while (!queue.IsEmpty && !HasNegativeCycle()) { int v = queue.Dequeue(); onQ[v] = false; Relax(G, v); } }
private void Bfs(IGraph g, int s) { var queue = new Chapter1.Queue <int>(); marked[s] = true; //起点被标记 queue.Enqueue(s); while (!queue.IsEmpty) { int v = queue.Dequeue(); //从队列中删除一个顶点 foreach (int w in g.Adj(v)) { if (!marked[w]) { edgeTo[w] = v; marked[w] = true; queue.Enqueue(w); } } } }