Skip to content

Commit 1c27e80

Browse files
committed
finish ch4
1 parent 9a5c9e4 commit 1c27e80

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1818
-26
lines changed

README.md

+28-22
Large diffs are not rendered by default.

ch4/10_Cycle/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ project(10_Cycle)
33

44
set(CMAKE_CXX_STANDARD 14)
55

6-
set(SOURCE_FILES main.cpp ../head/Graph.h ../head/GraphGenerator.h ../head/DepthFirstSearch.h ../head/EulerianCycle.h ../head/EulerianPath.h ../head/SymbolGraph.h ../head/Digraph.h ../head/DirectedDFS.h ../head/NonrecursiveDirectedDFS.h ../head/DepthFirstDirectedPaths.h ../head/BreadthFirstDirectedPaths.h ../head/DirectedCycle.h ../head/DirectedCycleX.h ../head/DigraphGenerator.h ../head/DirectedEulerianCycle.h ../head/DirectedEulerianPath.h ../head/DepthFirstOrder.h ../head/Topological.h ../head/TopologicalX.h ../head/TransitiveClosure.h ../head/SymbolDigraph.h ../head/KosarajuSharirSCC.h ../head/Edge.h ../head/EdgeWeightedGraph.h ../head/LazyPrimMST.h ../head/PrimMST.h ../head/KruskalMST.h ../head/BoruvkaMST.h ../head/DirectedEdge.h ../head/EdgeWeightedDigraph.h ../head/TarjanSCC.h ../head/GabowSCC.h)
6+
set(SOURCE_FILES main.cpp ../head/Graph.h ../head/GraphGenerator.h ../head/DepthFirstSearch.h ../head/EulerianCycle.h ../head/EulerianPath.h ../head/SymbolGraph.h ../head/Digraph.h ../head/DirectedDFS.h ../head/NonrecursiveDirectedDFS.h ../head/DepthFirstDirectedPaths.h ../head/BreadthFirstDirectedPaths.h ../head/DirectedCycle.h ../head/DirectedCycleX.h ../head/DigraphGenerator.h ../head/DirectedEulerianCycle.h ../head/DirectedEulerianPath.h ../head/DepthFirstOrder.h ../head/Topological.h ../head/TopologicalX.h ../head/TransitiveClosure.h ../head/SymbolDigraph.h ../head/KosarajuSharirSCC.h ../head/Edge.h ../head/EdgeWeightedGraph.h ../head/LazyPrimMST.h ../head/PrimMST.h ../head/KruskalMST.h ../head/BoruvkaMST.h ../head/DirectedEdge.h ../head/EdgeWeightedDigraph.h ../head/TarjanSCC.h ../head/GabowSCC.h ../head/DijkstraSP.h ../head/DijkstraUndirectedSP.h ../head/DijkstraAllPairsSP.h ../head/AcyclicSP.h ../head/EdgeWeightedDirectedCycle.h ../head/AcyclicLP.h ../head/BellmanFordSP.h ../head/AdjMatrixEdgeWeightedDigraph.h ../head/FloydWarshall.h)
77
add_executable(10_Cycle ${SOURCE_FILES})

ch4/41_DijkstraSP/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(41_DijkstraSP)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/Graph.h ../head/GraphGenerator.h ../head/DepthFirstSearch.h ../head/EulerianCycle.h ../head/EulerianPath.h ../head/SymbolGraph.h)
7+
add_executable(41_DijkstraSP ${SOURCE_FILES})

ch4/41_DijkstraSP/main.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <iostream>
2+
#include "../head/DijkstraSP.h"
3+
4+
using namespace std;
5+
6+
/**
7+
* Unit tests the {@code DijkstraSP} data type.
8+
*
9+
* @param args the command-line arguments
10+
*/
11+
int main() {
12+
string file = "/home/ace/AceDev/C++/algorithm/ch4/data/tinyEWD.txt";
13+
EdgeWeightedDigraph G(file);
14+
int s = 0;
15+
16+
DijkstraSP sp(G, s);
17+
18+
for (int t = 0; t < G.V_(); ++t) {
19+
if (sp.hasPathTo(t)) {
20+
cout << s << " to " << t << " (" << sp.distTo_(t) << " ) ";
21+
for (auto e: sp.pathTo(t))
22+
cout << e << " ";
23+
cout << endl;
24+
} else
25+
cout << s << " to " << t << " no path" << endl;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(42_DijkstraUndirectedSP)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/Graph.h ../head/GraphGenerator.h ../head/DepthFirstSearch.h ../head/EulerianCycle.h ../head/EulerianPath.h ../head/SymbolGraph.h)
7+
add_executable(42_DijkstraUndirectedSP ${SOURCE_FILES})

ch4/42_DijkstraUndirectedSP/main.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <iostream>
2+
#include "../head/DijkstraUndirectedSP.h"
3+
4+
using namespace std;
5+
6+
/**
7+
* Unit tests the {@code DijkstraSP} data type.
8+
*
9+
* @param args the command-line arguments
10+
*/
11+
int main() {
12+
string file = "/home/ace/AceDev/C++/algorithm/ch4/data/tinyEWG.txt";
13+
EdgeWeightedGraph G(file);
14+
int s = 6;
15+
16+
DijkstraUndirectedSP sp(G, s);
17+
18+
for (int t = 0; t < G.V_(); ++t) {
19+
if (sp.hasPathTo(t)) {
20+
cout << s << " to " << t << " (" << fixed << setprecision(2) << sp.distTo_(t) << " ) ";
21+
for (auto e: sp.pathTo(t))
22+
cout << e << " ";
23+
cout << endl;
24+
} else
25+
cout << s << " to " << t << " no path" << endl;
26+
}
27+
}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(43_DijkstraAllPairsSP)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/Graph.h ../head/GraphGenerator.h ../head/DepthFirstSearch.h ../head/EulerianCycle.h ../head/EulerianPath.h ../head/SymbolGraph.h)
7+
add_executable(43_DijkstraAllPairsSP ${SOURCE_FILES})

ch4/43_DijkstraAllPairsSP/main.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
#include "../head/DijkstraAllPairsSP.h"
3+
4+
using namespace std;
5+
6+
/**
7+
* Unit tests the {@code DijkstraSP} data type.
8+
*
9+
* @param args the command-line arguments
10+
*/
11+
int main() {
12+
string file = "/home/ace/AceDev/C++/algorithm/ch4/data/tinyEWD.txt";
13+
EdgeWeightedDigraph G(file);
14+
15+
DijkstraAllPairsSP dsp(G);
16+
17+
int s = 1, t = 2;
18+
if (dsp.hasPath(s, t)) {
19+
cout << s << " to " << t << " : ";
20+
for (auto e: dsp.path(s, t))
21+
cout << e << " ";
22+
cout << endl;
23+
}
24+
}

ch4/44_AcyclicSP/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(44_AcyclicSP)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/Graph.h ../head/GraphGenerator.h ../head/DepthFirstSearch.h ../head/EulerianCycle.h ../head/EulerianPath.h ../head/SymbolGraph.h)
7+
add_executable(44_AcyclicSP ${SOURCE_FILES})

ch4/44_AcyclicSP/main.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <iostream>
2+
#include "../head/AcyclicSP.h"
3+
4+
using namespace std;
5+
6+
/**
7+
* Unit tests the {@code AcyclicSP} data type.
8+
*
9+
* @param args the command-line arguments
10+
*/
11+
int main() {
12+
string file = "/home/ace/AceDev/C++/algorithm/ch4/data/tinyEWDAG.txt";
13+
EdgeWeightedDigraph G(file);
14+
15+
int s = 5;
16+
AcyclicSP asp(G, s);
17+
18+
for (int v = 0; v < G.V_(); ++v) {
19+
if (asp.hasPathTo(v)) {
20+
cout << s << " to " << v << " (" << asp.distTo_(v) << ") ";
21+
for (auto e: asp.pathTo(v))
22+
cout << e << " ";
23+
cout << endl;
24+
} else
25+
cout << s << " to " << v << " no path" << endl;
26+
}
27+
}

ch4/45_AcyclicLP/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(45_AcyclicLP)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/Graph.h ../head/GraphGenerator.h ../head/DepthFirstSearch.h ../head/EulerianCycle.h ../head/EulerianPath.h ../head/SymbolGraph.h)
7+
add_executable(45_AcyclicLP ${SOURCE_FILES})

ch4/45_AcyclicLP/main.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <iostream>
2+
#include "../head/AcyclicLP.h"
3+
4+
using namespace std;
5+
6+
/**
7+
* Unit tests the {@code AcyclicLP} data type.
8+
*
9+
* @param args the command-line arguments
10+
*/
11+
int main() {
12+
string file = "/home/ace/AceDev/C++/algorithm/ch4/data/tinyEWDAG.txt";
13+
EdgeWeightedDigraph G(file);
14+
15+
int s = 5;
16+
AcyclicLP alp(G, s);
17+
18+
for (int v = 0; v < G.V_(); ++v) {
19+
if (alp.hasPathTo(v)) {
20+
cout << s << " to " << v << " (" << alp.distTo_(v) << ") ";
21+
for (auto e: alp.pathTo(v))
22+
cout << e << " ";
23+
cout << endl;
24+
} else
25+
cout << s << " to " << v << " no path" << endl;
26+
}
27+
}

ch4/46_CPM/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(46_CPM)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/Graph.h ../head/GraphGenerator.h ../head/DepthFirstSearch.h ../head/EulerianCycle.h ../head/EulerianPath.h ../head/SymbolGraph.h)
7+
add_executable(46_CPM ${SOURCE_FILES})

ch4/46_CPM/main.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <iostream>
2+
#include "../head/AcyclicLP.h"
3+
4+
using namespace std;
5+
6+
/**
7+
* Reads the precedence constraints from standard input
8+
* and prints a feasible schedule to standard output.
9+
*
10+
* @param args the command-line arguments
11+
*/
12+
int main() {
13+
string file = "/home/ace/AceDev/C++/algorithm/ch4/data/jobsPC.txt";
14+
fstream in(file);
15+
int m, n, precedent;
16+
double duration;
17+
// number of jobs
18+
in >> n;
19+
// source and sink
20+
int source = 2 * n, sink = 2 * n + 1;
21+
22+
// build network
23+
EdgeWeightedDigraph G(2 * n + 2);
24+
for (int i = 0; i < n; ++i) {
25+
in >> duration;
26+
G.addEdge(DirectedEdge(source, i, 0.0));
27+
G.addEdge(DirectedEdge(i + n, sink, 0.0));
28+
G.addEdge(DirectedEdge(i, i + n, duration));
29+
30+
// precedence constraints
31+
in >> m;
32+
for (int j = 0; j < m; ++j) {
33+
in >> precedent;
34+
G.addEdge(DirectedEdge(n + i, precedent, 0.0));
35+
}
36+
}
37+
38+
// compute longest path
39+
AcyclicLP lp(G, source);
40+
41+
// print results
42+
cout << "job start finish" << endl;
43+
cout << "--------------------" << endl;
44+
for (int i = 0; i < n; ++i)
45+
cout << i << " " << fixed << setprecision(1) << lp.distTo_(i)
46+
<< " " << lp.distTo_(i + n) << endl;
47+
cout << "Finish tims: " << lp.distTo_(sink);
48+
49+
}

ch4/47_BellmanFordSP/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(47_BellmanFordSP)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/Graph.h ../head/GraphGenerator.h ../head/DepthFirstSearch.h ../head/EulerianCycle.h ../head/EulerianPath.h ../head/SymbolGraph.h)
7+
add_executable(47_BellmanFordSP ${SOURCE_FILES})

ch4/47_BellmanFordSP/main.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <iostream>
2+
#include "../head/BellmanFordSP.h"
3+
4+
using namespace std;
5+
6+
/**
7+
* Unit tests the {@code BellmanFordSP} data type.
8+
*
9+
* @param args the command-line arguments
10+
*/
11+
int main() {
12+
string file = "/home/ace/AceDev/C++/algorithm/ch4/data/tinyEWDn.txt";
13+
EdgeWeightedDigraph G(file);
14+
15+
int s = 0;
16+
17+
BellmanFordSP sp(G, s);
18+
19+
if (sp.hasNegativeCycle()) {
20+
for (auto e: sp.negativeCycle())
21+
cout << e << endl;
22+
} else {
23+
for (int v = 0; v < G.V_(); ++v) {
24+
if (sp.hasPathTo(v)) {
25+
cout << s << " to " << v << fixed << setprecision(2) << " (" << sp.distTo_(v) << ") ";
26+
for (auto e: sp.pathTo(v))
27+
cout << e << " ";
28+
cout << endl;
29+
} else {
30+
cout << s << " to " << v << " no path" << endl;
31+
}
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(48_EdgeWeightedDirectedCycle)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/Graph.h ../head/GraphGenerator.h ../head/DepthFirstSearch.h ../head/EulerianCycle.h ../head/EulerianPath.h ../head/SymbolGraph.h)
7+
add_executable(48_EdgeWeightedDirectedCycle ${SOURCE_FILES})
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <iostream>
2+
#include "../head/EdgeWeightedDirectedCycle.h"
3+
#include <algorithm>
4+
5+
using namespace std;
6+
7+
/**
8+
* Unit tests the {@code DijkstraSP} data type.
9+
*
10+
* @param args the command-line arguments
11+
*/
12+
int main() {
13+
// create random DAG with V vertices and E edges; then add F random edges
14+
int V = 8;
15+
int E = 8;
16+
int F = 2;
17+
EdgeWeightedDigraph G(V);
18+
vector<int> vertices(V);
19+
for (int i = 0; i < V; i++)
20+
vertices[i] = i;
21+
shuffle(vertices.begin(), vertices.end(), g);
22+
uniform_int_distribution<> dis(0, V - 1);
23+
uniform_real_distribution<double> dis1(0, 1);
24+
for (int i = 0; i < E; i++) {
25+
int v, w;
26+
do {
27+
v = dis(g);
28+
w = dis(g);
29+
} while (v >= w);
30+
double weight = dis1(g);
31+
G.addEdge(DirectedEdge(v, w, weight));
32+
}
33+
34+
// add F extra edges
35+
for (int i = 0; i < F; i++) {
36+
int v = dis(g);
37+
int w = dis(g);
38+
double weight = dis1(g);
39+
G.addEdge(DirectedEdge(v, w, weight));
40+
}
41+
42+
cout << G << endl;
43+
44+
// find a directed cycle
45+
EdgeWeightedDirectedCycle finder(G);
46+
if (finder.hasCycle()) {
47+
cout << "Cycle: ";
48+
for (DirectedEdge e : finder.cycle_()) {
49+
cout << e << " ";
50+
}
51+
cout << endl;
52+
} else { // or give topologial sort
53+
cout << "No directed cycle" << endl;
54+
}
55+
}

ch4/49_Arbitrage/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(49_Arbitrage)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/Graph.h ../head/GraphGenerator.h ../head/DepthFirstSearch.h ../head/EulerianCycle.h ../head/EulerianPath.h ../head/SymbolGraph.h)
7+
add_executable(49_Arbitrage ${SOURCE_FILES})

0 commit comments

Comments
 (0)