-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d10ceb0
commit 7dc8f1e
Showing
14 changed files
with
122 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include "BellmanFordSP.h" | ||
#include "DirectedCycle.h" | ||
|
||
void BellmanFordSP::onRelaxationSuccess(const EdgeWeightedDigraph &G, int v, const DirectedEdge &e, int w) { | ||
if (!onQ[w]) { | ||
queue.push_back(w); | ||
onQ[w] = true; | ||
} | ||
} | ||
|
||
void BellmanFordSP::afterRelaxation(const EdgeWeightedDigraph &G, int v, const DirectedEdge &e, int w) { | ||
if (cost++ % G.V() == 0) findNegativeCycle(); | ||
} | ||
|
||
void BellmanFordSP::findNegativeCycle() { | ||
int V = edgeTo.size(); | ||
EdgeWeightedDigraph spt(V); | ||
for (int v = 0; v < V; ++v) { | ||
if (edgeTo[v]) spt.addEdge(edgeTo[v]); | ||
} | ||
DirectedCycle cf(spt); | ||
cycle = cf.cycle(); | ||
} | ||
|
||
BellmanFordSP::BellmanFordSP(const EdgeWeightedDigraph &G, int s) : SP(G, s), onQ(G.V()) { | ||
queue.push_back(s); | ||
onQ[s] = true; | ||
while (!queue.empty() && !hasNegativeCycle()) { | ||
int v = queue.front(); | ||
queue.pop_front(); | ||
onQ[v] = false; | ||
relax(G, v); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#ifndef BELLMANFORDSP_H | ||
#define BELLMANFORDSP_H | ||
|
||
|
||
#include "SP.h" | ||
#include <vector> | ||
#include <list> | ||
|
||
class BellmanFordSP : public SP { | ||
private: | ||
std::vector<bool> onQ; // 顶点是否存在与队列中 | ||
std::list<int> queue; // 正在被放松的顶点 | ||
int cost = 0; // relax()的调用次数 | ||
std::list<DirectedEdge> cycle; // edgeTo[]表示的子图是否含有负权重环 | ||
|
||
void onRelaxationSuccess(const EdgeWeightedDigraph &G, int v, const DirectedEdge &e, int w) override; | ||
|
||
void afterRelaxation(const EdgeWeightedDigraph &G, int v, const DirectedEdge &e, int w) override; | ||
|
||
void findNegativeCycle(); | ||
|
||
public: | ||
BellmanFordSP(const EdgeWeightedDigraph &G, int s); | ||
|
||
bool hasNegativeCycle() const { return !cycle.empty(); } | ||
|
||
std::list<DirectedEdge> negativeCycle() const { return cycle; } | ||
}; | ||
|
||
|
||
#endif //BELLMANFORDSP_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,4 +83,6 @@ add_executable(algs4 | |
DijkstraSP.h | ||
AcyclicSP.cpp | ||
AcyclicSP.h | ||
BellmanFordSP.cpp | ||
BellmanFordSP.h | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
8 | ||
15 | ||
4 5 0.35 | ||
5 4 0.35 | ||
4 7 0.37 | ||
5 7 0.28 | ||
7 5 0.28 | ||
5 1 0.32 | ||
0 4 0.38 | ||
0 2 0.26 | ||
7 3 0.39 | ||
1 3 0.29 | ||
2 7 0.34 | ||
6 2 -1.20 | ||
3 6 0.52 | ||
6 0 -1.40 | ||
6 4 -1.25 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters