Skip to content

Commit a9cf3dd

Browse files
committed
update ch5
1 parent b98af12 commit a9cf3dd

File tree

14 files changed

+580
-10
lines changed

14 files changed

+580
-10
lines changed

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,15 @@ Working directory: youroot/algs4cplusplus/ch1
112112

113113
## ch5. Strings
114114

115-
| REF | PROGRAM | DESCRIPTION / C++DOC | REF | PROGRAM | DESCRIPTION / C++DOC |
116-
| :---------------------------------------------------------: | :----------------------------------------------------------: | :------------------------: | :---------------------------------------------------------: | :----------------------------------------------------------: | :-------------------------------: |
117-
| [-](https://algs4.cs.princeton.edu/50strings/index.php#-) | [Alphabet.java](https://algs4.cs.princeton.edu/50strings/Alphabet.java.html) | alphabet | [-](https://algs4.cs.princeton.edu/50strings/index.php#-) | [Count.java](https://algs4.cs.princeton.edu/50strings/Count.java.html) | alphabet client |
118-
| [5.1](https://algs4.cs.princeton.edu/51radix/index.php#5.1) | [LSD.h](ch5/head/LSD.h) | LSD radix sort | [5.2](https://algs4.cs.princeton.edu/51radix/index.php#5.2) | [MSD.h](ch5/head/MSD.h) | MSD radix sort |
119-
| [-](https://algs4.cs.princeton.edu/51radix/index.php#-) | [InplaceMSD.h](ch5/head/InplaceMSD.h) | In-place MSD radix sort1 | [5.3](https://algs4.cs.princeton.edu/51radix/index.php#5.3) | [Quick3string.h](ch5/head/Quick3string.h) | 3-way string quicksort |
120-
| [-](https://algs4.cs.princeton.edu/51radix/index.php#-) | [AmericanFlag.h](ch5/head/AmericanFlag.h) | American flag sort1 | [-](https://algs4.cs.princeton.edu/51radix/index.php#-) | [AmericanFlagX.h](ch5/head/AmericanFlagX.h) | non-recursive American flag sort1 |
121-
| [5.4](https://algs4.cs.princeton.edu/52trie/index.php#5.4) | [TrieST.h](ch5/head/TrieST.h) | multiway trie symbol table | [-](https://algs4.cs.princeton.edu/52trie/index.php#-) | [TrieSET.h](ch5/head/TrieSET.h) | multiway trie set |
122-
| [5.5](https://algs4.cs.princeton.edu/52trie/index.php#5.5) | [TST.h](ch5/head/TrieSET.h) | ternary search trie | | | |
115+
| REF | PROGRAM | DESCRIPTION / C++DOC | REF | PROGRAM | DESCRIPTION / C++DOC |
116+
| :----------------------------------------------------------: | :----------------------------------------------------------: | :----------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: | :-----------------------------------: |
117+
| [-](https://algs4.cs.princeton.edu/50strings/index.php#-) | [Alphabet.java](https://algs4.cs.princeton.edu/50strings/Alphabet.java.html) | alphabet | [-](https://algs4.cs.princeton.edu/50strings/index.php#-) | [Count.java](https://algs4.cs.princeton.edu/50strings/Count.java.html) | alphabet client |
118+
| [5.1](https://algs4.cs.princeton.edu/51radix/index.php#5.1) | [LSD.h](ch5/head/LSD.h) | LSD radix sort | [5.2](https://algs4.cs.princeton.edu/51radix/index.php#5.2) | [MSD.h](ch5/head/MSD.h) | MSD radix sort |
119+
| [-](https://algs4.cs.princeton.edu/51radix/index.php#-) | [InplaceMSD.h](ch5/head/InplaceMSD.h) | In-place MSD radix sort1 | [5.3](https://algs4.cs.princeton.edu/51radix/index.php#5.3) | [Quick3string.h](ch5/head/Quick3string.h) | 3-way string quicksort |
120+
| [-](https://algs4.cs.princeton.edu/51radix/index.php#-) | [AmericanFlag.h](ch5/head/AmericanFlag.h) | American flag sort1 | [-](https://algs4.cs.princeton.edu/51radix/index.php#-) | [AmericanFlagX.h](ch5/head/AmericanFlagX.h) | non-recursive American flag sort1 |
121+
| [5.4](https://algs4.cs.princeton.edu/52trie/index.php#5.4) | [TrieST.h](ch5/head/TrieST.h) | multiway trie symbol table | [-](https://algs4.cs.princeton.edu/52trie/index.php#-) | [TrieSET.h](ch5/head/TrieSET.h) | multiway trie set |
122+
| [5.5](https://algs4.cs.princeton.edu/52trie/index.php#5.5) | [TST.h](ch5/head/TrieSET.h) | ternary search trie | [5.6](https://algs4.cs.princeton.edu/53substring/index.php#5.6) | [KMP.h](ch5/head/KMP.h) | substring search (Knuth–Morris–Pratt) |
123+
| [5.7](https://algs4.cs.princeton.edu/53substring/index.php#5.7) | [BoyerMoore.h](ch5/head/BoyerMoore.h) | substring search (Boyer–Moore) | [5.8](https://algs4.cs.princeton.edu/53substring/index.php#5.8) | [RabinKarp.h](ch5/head/RabinKarp.h) | substring search (Rabin–Karp) |
124+
| [5.9](https://algs4.cs.princeton.edu/54regexp/index.php#5.9) | [NFA.h](ch5/head/NFA.h) | NFA for regular expressions | [-](https://algs4.cs.princeton.edu/54regexp/index.php#-) | [GREP.cpp](ch5/16_GREP/main.cpp) | grep |
125+
| [-](https://algs4.cs.princeton.edu/55compression/index.php#-) | [BinaryDump.cpp](ch5/17_BinaryDump/main.cpp) | binary dump | | | |
123126

ch5/10_TrieSET/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ project(10_TrieSET)
33

44
set(CMAKE_CXX_STANDARD 14)
55

6-
set(SOURCE_FILES main.cpp ../head/LSD.h ../head/MSD.h ../head/TST.h ../head/AmericanFlag.h ../head/AmericanFlagX.h ../head/KMP.h ../head/BoyerMoore.h ../head/RabinKarp.h)
6+
set(SOURCE_FILES main.cpp ../head/LSD.h ../head/MSD.h ../head/TST.h ../head/AmericanFlag.h ../head/AmericanFlagX.h ../head/KMP.h ../head/BoyerMoore.h ../head/RabinKarp.h ../head/NFA.h)
77
add_executable(10_TrieSET ${SOURCE_FILES})

ch5/15_NFA/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(15_NFA)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/LSD.h ../head/MSD.h ../head/TST.h)
7+
add_executable(15_NFA ${SOURCE_FILES})

ch5/15_NFA/main.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "../head/NFA.h"
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
/**
7+
* Unit tests the {@code NFA} data type.
8+
*
9+
* @param args the command-line arguments
10+
*/
11+
int main() {
12+
string regexp = "(A*B|AC)D";
13+
string txt = "AAAACD";
14+
15+
NFA nfa(regexp);
16+
cout << nfa.recognizes(txt) << endl;
17+
}

ch5/16_GREP/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(16_GREP)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/LSD.h ../head/MSD.h ../head/TST.h)
7+
add_executable(16_GREP ${SOURCE_FILES})

ch5/16_GREP/main.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "../head/NFA.h"
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
/**
7+
* Interprets the command-line argument as a regular expression
8+
* (supporting closure, binary or, parentheses, and wildcard)
9+
* reads in lines from standard input; writes to standard output
10+
* those lines that contain a substring matching the regular
11+
* expression.
12+
*
13+
* @param args the command-line arguments
14+
*/
15+
int main() {
16+
ifstream file("./data/tinyL.txt");
17+
string regexp = "(.*(A*B|AC)D.*)";
18+
string line;
19+
NFA nfa(regexp);
20+
while (file >> line) {
21+
if (nfa.recognizes(line))
22+
cout << line << endl;
23+
}
24+
}

ch5/17_BinaryDump/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(17_BinaryDump)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/LSD.h ../head/MSD.h ../head/TST.h)
7+
add_executable(17_BinaryDump ${SOURCE_FILES})

ch5/17_BinaryDump/main.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
#include <fstream>
3+
4+
using namespace std;
5+
6+
/**
7+
* Reads in a sequence of bytes from standard input and writes
8+
* them to standard output in binary, k bits per line,
9+
* where k is given as a command-line integer (defaults
10+
* to 16 if no integer is specified); also writes the number
11+
* of bits.
12+
*
13+
* @param args the command-line arguments
14+
*/
15+
int main() {
16+
ifstream file("./data/abra.txt", ios::binary);
17+
int bitsPerLine = 16;
18+
char tmp;
19+
int cnt = 0;
20+
while (file >> tmp) {
21+
for (int i = 7; i >= 0; --i) {
22+
cout << ((tmp >> i) & 1);
23+
cnt++;
24+
if (cnt % bitsPerLine == 0 && cnt != 0)cout << endl;
25+
}
26+
}
27+
if (bitsPerLine != 0) cout << endl;
28+
cout << cnt << " bits" << endl;
29+
}

ch5/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ add_subdirectory(11_TST)
1818
add_subdirectory(12_KMP)
1919
add_subdirectory(13_BoyerMoore)
2020
add_subdirectory(14_RabinKarp)
21+
add_subdirectory(15_NFA)
22+
add_subdirectory(16_GREP)
23+
add_subdirectory(17_BinaryDump)
2124

2225

23-
add_subdirectory(temp)
26+
# add_subdirectory(temp)

ch5/data/abra.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ABRACADABRA!

ch5/data/tinyL.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
AC
2+
AD
3+
AAA
4+
ABD
5+
ADD
6+
BCD
7+
ABCCBD
8+
BABAAA
9+
BABBAAA

ch5/head/Digraph.h

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
#ifndef CH4_DIGRAPH_H
2+
#define CH4_DIGRAPH_H
3+
4+
#include <forward_list>
5+
#include <vector>
6+
#include <stdexcept>
7+
#include <fstream>
8+
#include <string>
9+
#include <stack>
10+
#include <ostream>
11+
#include <sstream>
12+
13+
using std::forward_list;
14+
using std::vector;
15+
using std::runtime_error;
16+
using std::string;
17+
using std::fstream;
18+
using std::to_string;
19+
using std::stack;
20+
using std::distance;
21+
using std::ostream;
22+
using std::stringstream;
23+
24+
/**
25+
* The {@code Digraph} class represents a directed graph of vertices
26+
* named 0 through <em>V</em> - 1.
27+
* It supports the following two primary operations: add an edge to the digraph,
28+
* iterate over all of the vertices adjacent from a given vertex.
29+
* Parallel edges and self-loops are permitted.
30+
* <p>
31+
* This implementation uses an adjacency-lists representation, which
32+
* is a vertex-indexed array of {@link Bag} objects.
33+
* All operations take constant time (in the worst case) except
34+
* iterating over the vertices adjacent from a given vertex, which takes
35+
* time proportional to the number of such vertices.
36+
* <p>
37+
* For additional documentation,
38+
* see <a href="https://algs4.cs.princeton.edu/42digraph">Section 4.2</a> of
39+
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
40+
*
41+
* @author Robert Sedgewick
42+
* @author Kevin Wayne
43+
*/
44+
class Digraph {
45+
public:
46+
/**
47+
* Initializes an empty digraph with <em>V</em> vertices.
48+
*
49+
* @param V the number of vertices
50+
* @throws IllegalArgumentException if {@code V < 0}
51+
*/
52+
Digraph(int V) : V(V), E(0), indegree(V), adj(V) {
53+
if (V < 0) throw runtime_error("Number of vertices in a Digraph must be nonnegative");
54+
}
55+
56+
/**
57+
* Initializes a digraph from the specified input stream.
58+
* The format is the number of vertices <em>V</em>,
59+
* followed by the number of edges <em>E</em>,
60+
* followed by <em>E</em> pairs of vertices, with each entry separated by whitespace.
61+
*
62+
* @param in the input stream
63+
* @throws IllegalArgumentException if the endpoints of any edge are not in prescribed range
64+
* @throws IllegalArgumentException if the number of vertices or edges is negative
65+
* @throws IllegalArgumentException if the input stream is in the wrong format
66+
*/
67+
Digraph(string in): E(0) {
68+
fstream file(in);
69+
file >> V;
70+
if (V < 0) throw runtime_error("number of vertices in a Digraph must be nonnegative");
71+
indegree.resize(V);
72+
adj.resize(V);
73+
int tmpE;
74+
file >> tmpE;
75+
if (tmpE < 0) throw runtime_error("number of edges in a Digraph must be nonnegative");
76+
int v, w;
77+
for (int i = 0; i < tmpE; i++) {
78+
file >> v >> w;
79+
addEdge(v, w);
80+
}
81+
}
82+
83+
/**
84+
* Initializes a new digraph that is a deep copy of the specified digraph.
85+
*
86+
* @param G the digraph to copy
87+
*/
88+
Digraph(const Digraph &G) : Digraph(G.getV()) {
89+
E = G.getE();
90+
for (int v = 0; v < V; v++)
91+
indegree[v] = G.getindegree(v);
92+
for (int v = 0; v < G.getV(); v++) {
93+
// reverse so that adjacency list is in same order as original
94+
stack<int> reverse;
95+
for (int w : G.adj[v]) {
96+
reverse.push(w);
97+
}
98+
while (!reverse.empty()) {
99+
auto w = reverse.top();
100+
reverse.pop();
101+
adj[v].push_front(w);
102+
}
103+
}
104+
}
105+
106+
/**
107+
* Returns the number of vertices in this digraph.
108+
*
109+
* @return the number of vertices in this digraph
110+
*/
111+
int getV() const {
112+
return V;
113+
}
114+
115+
/**
116+
* Returns the number of edges in this digraph.
117+
*
118+
* @return the number of edges in this digraph
119+
*/
120+
int getE() const {
121+
return E;
122+
}
123+
124+
/**
125+
* Adds the directed edge v→w to this digraph.
126+
*
127+
* @param v the tail vertex
128+
* @param w the head vertex
129+
* @throws IllegalArgumentException unless both {@code 0 <= v < V} and {@code 0 <= w < V}
130+
*/
131+
void addEdge(int v, int w) {
132+
validateVertex(v);
133+
validateVertex(w);
134+
adj[v].push_front(w);
135+
indegree[w]++;
136+
E++;
137+
}
138+
139+
/**
140+
* Returns the vertices adjacent from vertex {@code v} in this digraph.
141+
*
142+
* @param v the vertex
143+
* @return the vertices adjacent from vertex {@code v} in this digraph, as an iterable
144+
* @throws IllegalArgumentException unless {@code 0 <= v < V}
145+
*/
146+
forward_list<int> getadj(int v) const {
147+
validateVertex(v);
148+
return adj[v];
149+
}
150+
151+
/**
152+
* Returns the number of directed edges incident from vertex {@code v}.
153+
* This is known as the <em>outdegree</em> of vertex {@code v}.
154+
*
155+
* @param v the vertex
156+
* @return the outdegree of vertex {@code v}
157+
* @throws IllegalArgumentException unless {@code 0 <= v < V}
158+
*/
159+
int outdegree(int v) const {
160+
validateVertex(v);
161+
return distance(adj[v].begin(), adj[v].end());
162+
}
163+
164+
/**
165+
* Returns the number of directed edges incident to vertex {@code v}.
166+
* This is known as the <em>indegree</em> of vertex {@code v}.
167+
*
168+
* @param v the vertex
169+
* @return the indegree of vertex {@code v}
170+
* @throws IllegalArgumentException unless {@code 0 <= v < V}
171+
*/
172+
int getindegree(int v) const {
173+
validateVertex(v);
174+
return indegree[v];
175+
}
176+
177+
/**
178+
* Returns the reverse of the digraph.
179+
*
180+
* @return the reverse of the digraph
181+
*/
182+
Digraph *reverse() {
183+
Digraph *reverse = new Digraph(V);
184+
for (int v = 0; v < V; v++) {
185+
for (int w : getadj(v)) {
186+
reverse->addEdge(w, v);
187+
}
188+
}
189+
return reverse;
190+
}
191+
192+
/**
193+
* Returns a string representation of the graph.
194+
*
195+
* @return the number of vertices <em>V</em>, followed by the number of edges <em>E</em>,
196+
* followed by the <em>V</em> adjacency lists
197+
*/
198+
friend ostream &operator<<(ostream &stream, const Digraph &dg) {
199+
stringstream ss;
200+
ss << dg.V << " vertices, " << dg.getE() << " edges" << std::endl;
201+
for (int v = 0; v < dg.V; ++v) {
202+
ss << v << ": ";
203+
for (auto w: dg.getadj(v))
204+
ss << w << " ";
205+
ss << std::endl;
206+
}
207+
stream << ss.str();
208+
return stream;
209+
}
210+
211+
private:
212+
// throw an IllegalArgumentException unless {@code 0 <= v < V}
213+
void validateVertex(int v) const {
214+
if (v < 0 || v >= V)
215+
throw runtime_error("vertex " + to_string(v) + " is not between 0 and " + to_string(V - 1));
216+
}
217+
218+
private:
219+
int V; // number of vertices in this digraph
220+
int E; // number of edges in this digraph
221+
vector<forward_list<int>> adj; // adj[v] = adjacency list for vertex v
222+
vector<int> indegree; // indegree[v] = indegree of vertex v
223+
};
224+
225+
#endif //CH4_DIGRAPH_H

0 commit comments

Comments
 (0)