Skip to content

Commit 9a5c9e4

Browse files
committed
update ch4
1 parent c072b38 commit 9a5c9e4

Some content is hidden

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

49 files changed

+2731
-17
lines changed

README.md

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ Algorithms, 4th edition textbook code (using c++)
33

44
**Note:**
55

6+
**(the code can run, but may not well --- I will modify these in the next cycle)**
7+
68
1. based on STL Library
79
2. using C++14
810
3. **Not support** drawing
9-
4. **Bug: ** const object (many member function forget to add const)
11+
4. **Bug: ** const object (many member function forget to add const) ② memory leak ③ point or reference ?
1012
4. Welcome to point out the error, and pull better code
1113

1214
> the code is writed and debug in CLion IDE,and not test in terminal(I will check it after finish more code)
@@ -64,19 +66,26 @@ Algorithms, 4th edition textbook code (using c++)
6466

6567
## ch4. Graphs
6668

67-
| REF | PROGRAM | DESCRIPTION / C++DOC | REF | PROGRAM | DESCRIPTION / C++DOC |
68-
| :----------------------------------------------------------: | :----------------------------------------------------------: | :-----------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: | :-------------------------------: |
69-
| [-](https://algs4.cs.princeton.edu/41graph/index.php#-) | [Graph.h](ch4/head/Graph.h) | undirected graph | [-](https://algs4.cs.princeton.edu/41graph/index.php#-) | [GraphGenerator.java](https://algs4.cs.princeton.edu/41graph/GraphGenerator.java.html) | generate random graphs |
70-
| [-](https://algs4.cs.princeton.edu/41graph/index.php#-) | [DepthFirstSearch.h](ch4/head/DepthFirstSearch.h) | depth-first search in a graph | [-](https://algs4.cs.princeton.edu/41graph/index.php#-) | [NonrecursiveDFS.h](ch4/head/NonrecursiveDFS.h) | DFS in a graph (nonrecursive) |
71-
| [4.1](https://algs4.cs.princeton.edu/41graph/index.php#4.1) | [DepthFirstPaths.h](ch4/head/DepthFirstPaths.h) | paths in a graph (DFS) | [4.2](https://algs4.cs.princeton.edu/41graph/index.php#4.2) | [BreadthFirstPaths.h](ch4/head/BreadthFirstPaths.h) | paths in a graph (BFS) |
72-
| [4.3](https://algs4.cs.princeton.edu/41graph/index.php#4.3) | [CC.h](ch4/head/CC.h) | connected components of a graph | [-](https://algs4.cs.princeton.edu/41graph/index.php#-) | [Bipartite.h](ch4/head/Bipartite.h) | bipartite or odd cycle (DFS) |
73-
| [-](https://algs4.cs.princeton.edu/41graph/index.php#-) | [BipartiteX.h](ch4/head/BipartiteX.h) | bipartite or odd cycle (BFS) | [-](https://algs4.cs.princeton.edu/41graph/index.php#-) | [Cycle.h](ch4/head/Cycle.h) | cycle in a graph |
74-
| [-](https://algs4.cs.princeton.edu/41graph/index.php#-) | [EulerianCycle.h](ch4/head/EulerianCycle.h) | Eulerian cycle in a graph | [-](https://algs4.cs.princeton.edu/41graph/index.php#-) | [EulerianPath.h](ch4/head/EulerianPath.h) | Eulerian path in a graph |
75-
| [-](https://algs4.cs.princeton.edu/41graph/index.php#-) | [SymbolGraph.h](ch4/head/SymbolGraph.h) | symbol graph | [-](https://algs4.cs.princeton.edu/41graph/index.php#-) | [DegreesOfSeparation.cpp](ch4/14_DegreesOfSeparation/main.cpp) | degrees of separation |
76-
| [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [Digraph.h](ch4/head/Digraph.h) | directed graph | [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [DigraphGenerator.h](ch4/head/DigraphGenerator.h) | generate random digraphs |
77-
| [4.4](https://algs4.cs.princeton.edu/42digraph/index.php#4.4) | [DirectedDFS.h](ch4/head/DirectedDFS.h) | depth-first search in a digraph | [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [NonrecursiveDirectedDFS.h](ch4/head/NonrecursiveDirectedDFS.h) | DFS in a digraph (nonrecursive) |
78-
| [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [DepthFirstDirectedPaths.h](ch4/head/DepthFirstDirectedPaths.h) | paths in a digraph (DFS) | [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [BreadthFirstDirectedPaths.h](ch4/head/BreadthFirstDirectedPaths.h) | paths in a digraph (BFS) |
79-
| [-](https://algs4.cs.princeton.edu/44sp/index.php#-) | [DirectedCycle.h](ch4/head/DirectedCycle.h) | cycle in a digraph | [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [DirectedCycleX.h](ch4/head/DirectedCycleX.h) | cycle in a digraph (nonrecursive) |
80-
| [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [DirectedEulerianCycle.h](ch4/head/DirectedEulerianCycle.h) | Eulerian cycle in a digraph | [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [DirectedEulerianPath.h](ch4/head/DirectedEulerianPath.h) | Eulerian path in a digraph |
81-
| [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [DepthFirstOrder.h](ch4/head/DepthFirstOrder.h) | depth-first order in a digraph | [4.5](https://algs4.cs.princeton.edu/42digraph/index.php#4.5) | [Topological.h](ch4/head/Topological.h) | topological order in a DAG |
69+
| REF | PROGRAM | DESCRIPTION / C++DOC | REF | PROGRAM | DESCRIPTION / C++DOC |
70+
| :----------------------------------------------------------: | :----------------------------------------------------------: | :------------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: | :---------------------------------: |
71+
| [-](https://algs4.cs.princeton.edu/41graph/index.php#-) | [Graph.h](ch4/head/Graph.h) | undirected graph | [-](https://algs4.cs.princeton.edu/41graph/index.php#-) | [GraphGenerator.java](https://algs4.cs.princeton.edu/41graph/GraphGenerator.java.html) | generate random graphs |
72+
| [-](https://algs4.cs.princeton.edu/41graph/index.php#-) | [DepthFirstSearch.h](ch4/head/DepthFirstSearch.h) | depth-first search in a graph | [-](https://algs4.cs.princeton.edu/41graph/index.php#-) | [NonrecursiveDFS.h](ch4/head/NonrecursiveDFS.h) | DFS in a graph (nonrecursive) |
73+
| [4.1](https://algs4.cs.princeton.edu/41graph/index.php#4.1) | [DepthFirstPaths.h](ch4/head/DepthFirstPaths.h) | paths in a graph (DFS) | [4.2](https://algs4.cs.princeton.edu/41graph/index.php#4.2) | [BreadthFirstPaths.h](ch4/head/BreadthFirstPaths.h) | paths in a graph (BFS) |
74+
| [4.3](https://algs4.cs.princeton.edu/41graph/index.php#4.3) | [CC.h](ch4/head/CC.h) | connected components of a graph | [-](https://algs4.cs.princeton.edu/41graph/index.php#-) | [Bipartite.h](ch4/head/Bipartite.h) | bipartite or odd cycle (DFS) |
75+
| [-](https://algs4.cs.princeton.edu/41graph/index.php#-) | [BipartiteX.h](ch4/head/BipartiteX.h) | bipartite or odd cycle (BFS) | [-](https://algs4.cs.princeton.edu/41graph/index.php#-) | [Cycle.h](ch4/head/Cycle.h) | cycle in a graph |
76+
| [-](https://algs4.cs.princeton.edu/41graph/index.php#-) | [EulerianCycle.h](ch4/head/EulerianCycle.h) | Eulerian cycle in a graph | [-](https://algs4.cs.princeton.edu/41graph/index.php#-) | [EulerianPath.h](ch4/head/EulerianPath.h) | Eulerian path in a graph |
77+
| [-](https://algs4.cs.princeton.edu/41graph/index.php#-) | [SymbolGraph.h](ch4/head/SymbolGraph.h) | symbol graph | [-](https://algs4.cs.princeton.edu/41graph/index.php#-) | [DegreesOfSeparation.cpp](ch4/14_DegreesOfSeparation/main.cpp) | degrees of separation |
78+
| [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [Digraph.h](ch4/head/Digraph.h) | directed graph | [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [DigraphGenerator.h](ch4/head/DigraphGenerator.h) | generate random digraphs |
79+
| [4.4](https://algs4.cs.princeton.edu/42digraph/index.php#4.4) | [DirectedDFS.h](ch4/head/DirectedDFS.h) | depth-first search in a digraph | [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [NonrecursiveDirectedDFS.h](ch4/head/NonrecursiveDirectedDFS.h) | DFS in a digraph (nonrecursive) |
80+
| [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [DepthFirstDirectedPaths.h](ch4/head/DepthFirstDirectedPaths.h) | paths in a digraph (DFS) | [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [BreadthFirstDirectedPaths.h](ch4/head/BreadthFirstDirectedPaths.h) | paths in a digraph (BFS) |
81+
| [-](https://algs4.cs.princeton.edu/44sp/index.php#-) | [DirectedCycle.h](ch4/head/DirectedCycle.h) | cycle in a digraph | [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [DirectedCycleX.h](ch4/head/DirectedCycleX.h) | cycle in a digraph (nonrecursive) |
82+
| [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [DirectedEulerianCycle.h](ch4/head/DirectedEulerianCycle.h) | Eulerian cycle in a digraph | [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [DirectedEulerianPath.h](ch4/head/DirectedEulerianPath.h) | Eulerian path in a digraph |
83+
| [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [DepthFirstOrder.h](ch4/head/DepthFirstOrder.h) | depth-first order in a digraph | [4.5](https://algs4.cs.princeton.edu/42digraph/index.php#4.5) | [Topological.h](ch4/head/Topological.h) | topological order in a DAG |
84+
| [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [TopologicalX.h](ch4/head/TopologicalX.h) | topological order (nonrecursive) | [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [TransitiveClosure.h](ch4/head/TransitiveClosure.h) | transitive closure |
85+
| [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [SymbolDigraph.h](ch4/head/SymbolDigraph.h) | symbol digraph | [4.6](https://algs4.cs.princeton.edu/42digraph/index.php#4.6) | [KosarajuSharirSCC.h](ch4/head/KosarajuSharirSCC.h) | strong components (Kosaraju–Sharir) |
86+
| [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [TarjanSCC.h](ch4/head/TarjanSCC.h) | strong components (Tarjan) | [-](https://algs4.cs.princeton.edu/42digraph/index.php#-) | [GabowSCC.h](ch4/head/GabowSCC.h) | strong components (Gabow) |
87+
| [-](https://algs4.cs.princeton.edu/43mst/index.php#-) | [EdgeWeightedGraph.h](ch4/head/EdgeWeightedGraph.h) | edge-weighted graph | [-](https://algs4.cs.princeton.edu/43mst/index.php#-) | [Edge.h](ch4/head/Edge.h) | weighted edge |
88+
| [-](https://algs4.cs.princeton.edu/43mst/index.php#-) | [LazyPrimMST.h](ch4/head/LazyPrimMST.h) | MST (lazy Prim) | [4.7](https://algs4.cs.princeton.edu/43mst/index.php#4.7) | [PrimMST.h](ch4/head/PrimMST.h) | MST (Prim) |
89+
| [4.8](https://algs4.cs.princeton.edu/43mst/index.php#4.8) | [KruskalMST.h](ch4/head/KruskalMST.h) | MST (Kruskal) | [-](https://algs4.cs.princeton.edu/43mst/index.php#-) | [BoruvkaMST.h](ch4/head/BoruvkaMST.h) | MST (Boruvka) |
90+
| [-](https://algs4.cs.princeton.edu/44sp/index.php#-) | [EdgeWeightedDigraph.h](ch4/head/EdgeWeightedDigraph.h) | edge-weighted digraph | [-](https://algs4.cs.princeton.edu/44sp/index.php#-) | [DirectedEdge.h](ch4/head/DirectedEdge.h) | weighted, directed edge |
8291

ch4/10_Cycle/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
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)
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)
77
add_executable(10_Cycle ${SOURCE_FILES})

ch4/27_TopologicalX/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(27_TopologicalX)
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(27_TopologicalX ${SOURCE_FILES})

ch4/27_TopologicalX/main.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <iostream>
2+
#include "../head/Digraph.h"
3+
#include "../head/DepthFirstOrder.h"
4+
5+
using namespace std;
6+
7+
// TODO: add main test
8+
9+
/**
10+
* Unit tests the {@code DirectedEulerianCycle} data type.
11+
*
12+
* @param args the command-line arguments
13+
*/
14+
int main() {
15+
string file = "/home/ace/AceDev/C++/algorithm/ch4/data/tinyDG.txt";
16+
char delimiter = ' ';
17+
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(28_TransitiveClosure)
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(28_TransitiveClosure ${SOURCE_FILES})

ch4/28_TransitiveClosure/main.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <iostream>
2+
#include "../head/TransitiveClosure.h"
3+
4+
using namespace std;
5+
6+
7+
/**
8+
* Unit tests the {@code TransitiveClosure} data type.
9+
*
10+
* @param args the command-line arguments
11+
*/
12+
int main() {
13+
string file = "/home/ace/AceDev/C++/algorithm/ch4/data/tinyDG.txt";
14+
Digraph G(file);
15+
16+
TransitiveClosure tc(G);
17+
18+
// print header
19+
cout << " ";
20+
for (int v = 0; v < G.getV(); ++v)
21+
cout << " " << v;
22+
cout << endl;
23+
cout << "--------------------------------------------" << endl;
24+
25+
// print transitive closure
26+
for (int v = 0; v < G.getV(); v++) {
27+
cout << " " << v;
28+
for (int w = 0; w < G.getV(); w++) {
29+
if (tc.reachable(v, w)) cout << " T";
30+
else cout << " ";
31+
}
32+
cout << endl;
33+
}
34+
}

ch4/29_SymbolDigraph/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(29_SymbolDigraph)
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(29_SymbolDigraph ${SOURCE_FILES})

ch4/29_SymbolDigraph/main.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <iostream>
2+
#include "../head/SymbolDigraph.h"
3+
4+
using namespace std;
5+
6+
7+
/**
8+
* Unit tests the {@code SymbolDigraph} data type.
9+
*
10+
* @param args the command-line arguments
11+
*/
12+
int main() {
13+
string file = "/home/ace/AceDev/C++/algorithm/ch4/data/routes.txt";
14+
char delimiter = ' ';
15+
SymbolDigraph sg(file, delimiter);
16+
17+
Digraph *graph = sg.digraph();
18+
string tmp;
19+
cout << "please input search file (-1 means break)" << endl;
20+
while (cin >> tmp) {
21+
if (tmp == "-1")
22+
break;
23+
for (int v: graph->getadj(sg.indexOf(tmp))) {
24+
cout << " " << sg.nameOf(v) << endl;
25+
}
26+
}
27+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(30_KosarajuSharirSCC)
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(30_KosarajuSharirSCC ${SOURCE_FILES})

ch4/30_KosarajuSharirSCC/main.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <iostream>
2+
#include "../head/KosarajuSharirSCC.h"
3+
4+
using namespace std;
5+
6+
7+
/**
8+
* Unit tests the {@code SymbolDigraph} data type.
9+
*
10+
* @param args the command-line arguments
11+
*/
12+
int main() {
13+
string file = "/home/ace/AceDev/C++/algorithm/ch4/data/tinyDG.txt";
14+
Digraph G(file);
15+
KosarajuSharirSCC scc(G);
16+
17+
// number of connected components
18+
int m = scc.getcount();
19+
cout << m << " strong components" << endl;
20+
21+
// compute list of vertices in each strong component
22+
vector<queue<int>> components(m);
23+
for (int v = 0; v < G.getV(); ++v) {
24+
components[scc.getid(v)].push(v);
25+
}
26+
27+
// print results
28+
for (int i = 0; i < m; ++i) {
29+
while (!components[i].empty()) {
30+
cout << components[i].front() << " ";
31+
components[i].pop();
32+
}
33+
cout << endl;
34+
}
35+
}

0 commit comments

Comments
 (0)