Skip to content

Commit 54bc41f

Browse files
committed
finish ch1
1 parent 15535f5 commit 54bc41f

File tree

15 files changed

+818
-22
lines changed

15 files changed

+818
-22
lines changed

README.md

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,24 @@ Algorithms, 4th edition textbook code (using c++)
1111
1212
## ch1. Fundamentals
1313

14-
| REF | PROGRAM | DESCRIPTION / JAVADOC | REF | PROGRAM | DESCRIPTION / JAVADOC |
15-
| :----------------------------------------------------------: | :---------------------------------------------------: | :------------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: | :-----------------------------: |
16-
| [-](https://algs4.cs.princeton.edu/11model/index.php#-) | [BinarySearch.h](ch1/head/BinarySearch.h) | binary search | [-](https://algs4.cs.princeton.edu/11model/index.php#-) | [RandomSeq.cpp](ch1/2_RandomSeq/main.cpp) | random numbers in a given range |
17-
| [-](https://algs4.cs.princeton.edu/11model/index.php#-) | [Average.cpp](ch1/3_Average/main.cpp) | average of a sequence of numbers | [-](https://algs4.cs.princeton.edu/11model/index.php#-) | [Cat.cpp](ch1/4_Cat/main.cpp) | concatenate files |
18-
| [-](https://algs4.cs.princeton.edu/11model/index.php#-) | [Knuth.h](ch1/head/Knuth.h) | Knuth shuffle | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Counter.h](ch1/head/Counter.h) | counter |
19-
| [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [StaticSETofInts.h](ch1/head/StaticSETofInts.h) | set of integers | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Whitelist.cpp](ch1/8_Whitelist/main.cpp) | whitelist client |
20-
| [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Vector.h](ch1/head/Vector.h) | Euclidean vector | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Date.h](ch1/head/Date.h) | date |
21-
| [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Transaction.h](ch1/head/Transaction.h) | transaction | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Point2D.h](ch1/head/Point2D.h) | point |
22-
| [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [RectHV.h](ch1/head/RectHV.h) | axis-aligned rectangle | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Interval1D.h](ch1/head/Interval1D.h) | 1d interval |
23-
| [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Interval2D.h](ch1/head/Interval2D.h) | 2d interval | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Accumulator.h](ch1/head/Accumulator.h) | running average and stddev |
24-
| [1.1](https://algs4.cs.princeton.edu/13stacks/index.php#1.1) | [ResizingArrayStack.h](ch1/head/ResizingArrayStack.h) | LIFO stack (resizing array) | [1.2](https://algs4.cs.princeton.edu/13stacks/index.php#1.2) | [LinkedStack.h](ch1/head/LinkedStack.h) | LIFO stack (linked list) |
25-
| [-](https://algs4.cs.princeton.edu/13stacks/index.php#-) | [Stack.h](ch1/head/Stack.h) | LIFO stack | [-](https://algs4.cs.princeton.edu/13stacks/index.php#-) | [ResizingArrayQueue.h](ch1/head/ResizingArrayQueue.h) | FIFO queue (resizing array) |
26-
| [1.3](https://algs4.cs.princeton.edu/13stacks/index.php#1.3) | [LinkedQueue.h](ch1/head/LinkedQueue.h) | FIFO queue (linked list) | [-](https://algs4.cs.princeton.edu/13stacks/index.php#-) | [Queue.h](ch1/head/Queue.h) | FIFO queue |
27-
| [-](https://algs4.cs.princeton.edu/13stacks/index.php#-) | [ResizingArrayBag.h](ch1/head/ResizingArrayBag.h) | multiset (resizing array) | [1.4](https://algs4.cs.princeton.edu/13stacks/index.php#1.4) | [LinkedBag.h](ch1/head/LinkedBag.h) | multiset (linked list) |
28-
| [-](https://algs4.cs.princeton.edu/13stacks/index.php#-) | [Bag.h](ch1/head/Bag.h) | multiset | [-](https://algs4.cs.princeton.edu/14analysis/index.php#-) | [Stopwatch.h](ch1/head/Stopwatch.h) | timer (wall time) |
29-
| [-](https://algs4.cs.princeton.edu/14analysis/index.php#-) | [StopwatchCPU.h]() | timer (CPU time) | [-](https://algs4.cs.princeton.edu/14analysis/index.php#-) | [LinearRegression.java](https://algs4.cs.princeton.edu/14analysis/LinearRegression.java.html) | simple linear regression |
30-
| [-](https://algs4.cs.princeton.edu/14analysis/index.php#-) | [ThreeSum.h](ch1/head/ThreeSum.h) | brute-force three sum | [-](https://algs4.cs.princeton.edu/14analysis/index.php#-) | [ThreeSumFast.h](ch1/head/ThreeSumFast.h) | faster three sum |
31-
| [-](https://algs4.cs.princeton.edu/14analysis/index.php#-) | [DoublingTest.h](ch1/head/DoublingTest.h) | doubling test | [-](https://algs4.cs.princeton.edu/14analysis/index.php#-) | [DoublingRatio.cpp](ch1/32_DoublingRatio/main.cpp) | doubling ratio |
32-
| [-](https://algs4.cs.princeton.edu/15uf/index.php#-) | [QuickFindUF.h](ch1/head/QuickFindUF.h) | quick find | | | |
14+
| REF | PROGRAM | DESCRIPTION / JAVADOC | REF | PROGRAM | DESCRIPTION / JAVADOC |
15+
| :----------------------------------------------------------: | :-------------------------------------------------------: | :------------------------------: | :----------------------------------------------------------: | :---------------------------------------------------: | :-----------------------------: |
16+
| [-](https://algs4.cs.princeton.edu/11model/index.php#-) | [BinarySearch.h](ch1/head/BinarySearch.h) | binary search | [-](https://algs4.cs.princeton.edu/11model/index.php#-) | [RandomSeq.cpp](ch1/2_RandomSeq/main.cpp) | random numbers in a given range |
17+
| [-](https://algs4.cs.princeton.edu/11model/index.php#-) | [Average.cpp](ch1/3_Average/main.cpp) | average of a sequence of numbers | [-](https://algs4.cs.princeton.edu/11model/index.php#-) | [Cat.cpp](ch1/4_Cat/main.cpp) | concatenate files |
18+
| [-](https://algs4.cs.princeton.edu/11model/index.php#-) | [Knuth.h](ch1/head/Knuth.h) | Knuth shuffle | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Counter.h](ch1/head/Counter.h) | counter |
19+
| [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [StaticSETofInts.h](ch1/head/StaticSETofInts.h) | set of integers | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Whitelist.cpp](ch1/8_Whitelist/main.cpp) | whitelist client |
20+
| [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Vector.h](ch1/head/Vector.h) | Euclidean vector | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Date.h](ch1/head/Date.h) | date |
21+
| [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Transaction.h](ch1/head/Transaction.h) | transaction | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Point2D.h](ch1/head/Point2D.h) | point |
22+
| [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [RectHV.h](ch1/head/RectHV.h) | axis-aligned rectangle | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Interval1D.h](ch1/head/Interval1D.h) | 1d interval |
23+
| [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Interval2D.h](ch1/head/Interval2D.h) | 2d interval | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Accumulator.h](ch1/head/Accumulator.h) | running average and stddev |
24+
| [1.1](https://algs4.cs.princeton.edu/13stacks/index.php#1.1) | [ResizingArrayStack.h](ch1/head/ResizingArrayStack.h) | LIFO stack (resizing array) | [1.2](https://algs4.cs.princeton.edu/13stacks/index.php#1.2) | [LinkedStack.h](ch1/head/LinkedStack.h) | LIFO stack (linked list) |
25+
| [-](https://algs4.cs.princeton.edu/13stacks/index.php#-) | [Stack.h](ch1/head/Stack.h) | LIFO stack | [-](https://algs4.cs.princeton.edu/13stacks/index.php#-) | [ResizingArrayQueue.h](ch1/head/ResizingArrayQueue.h) | FIFO queue (resizing array) |
26+
| [1.3](https://algs4.cs.princeton.edu/13stacks/index.php#1.3) | [LinkedQueue.h](ch1/head/LinkedQueue.h) | FIFO queue (linked list) | [-](https://algs4.cs.princeton.edu/13stacks/index.php#-) | [Queue.h](ch1/head/Queue.h) | FIFO queue |
27+
| [-](https://algs4.cs.princeton.edu/13stacks/index.php#-) | [ResizingArrayBag.h](ch1/head/ResizingArrayBag.h) | multiset (resizing array) | [1.4](https://algs4.cs.princeton.edu/13stacks/index.php#1.4) | [LinkedBag.h](ch1/head/LinkedBag.h) | multiset (linked list) |
28+
| [-](https://algs4.cs.princeton.edu/13stacks/index.php#-) | [Bag.h](ch1/head/Bag.h) | multiset | [-](https://algs4.cs.princeton.edu/14analysis/index.php#-) | [Stopwatch.h](ch1/head/Stopwatch.h) | timer (wall time) |
29+
| [-](https://algs4.cs.princeton.edu/14analysis/index.php#-) | [StopwatchCPU.h]() | timer (CPU time) | [-](https://algs4.cs.princeton.edu/14analysis/index.php#-) | [LinearRegression.h](ch1/head/LinearRegression.h) | simple linear regression |
30+
| [-](https://algs4.cs.princeton.edu/14analysis/index.php#-) | [ThreeSum.h](ch1/head/ThreeSum.h) | brute-force three sum | [-](https://algs4.cs.princeton.edu/14analysis/index.php#-) | [ThreeSumFast.h](ch1/head/ThreeSumFast.h) | faster three sum |
31+
| [-](https://algs4.cs.princeton.edu/14analysis/index.php#-) | [DoublingTest.h](ch1/head/DoublingTest.h) | doubling test | [-](https://algs4.cs.princeton.edu/14analysis/index.php#-) | [DoublingRatio.cpp](ch1/32_DoublingRatio/main.cpp) | doubling ratio |
32+
| [-](https://algs4.cs.princeton.edu/15uf/index.php#-) | [QuickFindUF.h](ch1/head/QuickFindUF.h) | quick find | [-](https://algs4.cs.princeton.edu/15uf/index.php#-) | [QuickUnionUF.h](ch1/head/QuickUnionUF.h) | quick union |
33+
| [1.5](https://algs4.cs.princeton.edu/15uf/index.php#1.5) | [WeightedQuickUnionUF.h](ch1/head/WeightedQuickUnionUF.h) | weighted quick union | [-](https://algs4.cs.princeton.edu/15uf/index.php#-) | [UF.h](ch1/head/UF.h) | union-by-rank with path halving |
3334

ch1/10_Date/CMakeLists.txt

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

44
set(CMAKE_CXX_STANDARD 14)
55

6-
set(SOURCE_FILES main.cpp ../head/BinarySearch.h ../head/Knuth.h ../head/Counter.h ../head/StaticSetofInts.h ../head/Vector.h ../head/Date.h ../head/Transaction.h ../head/Point2D.h ../head/Interval1D.h ../head/Interval2D.h ../head/RectHV.h ../head/Accumulator.h ../head/ResizingArrayStack.h ../head/LinkedStack.h ../head/Stack.h ../head/ResizingArrayQueue.h ../head/LinkedQueue.h ../head/Queue.h ../head/ResizingArrayBag.h ../head/LinkedBag.h ../head/Bag.h)
6+
set(SOURCE_FILES main.cpp ../head/BinarySearch.h ../head/Knuth.h ../head/Counter.h ../head/StaticSetofInts.h ../head/Vector.h ../head/Date.h ../head/Transaction.h ../head/Point2D.h ../head/Interval1D.h ../head/Interval2D.h ../head/RectHV.h ../head/Accumulator.h ../head/ResizingArrayStack.h ../head/LinkedStack.h ../head/Stack.h ../head/ResizingArrayQueue.h ../head/LinkedQueue.h ../head/Queue.h ../head/ResizingArrayBag.h ../head/LinkedBag.h ../head/Bag.h ../head/Stopwatch.h ../head/ThreeSum.h ../head/ThreeSumFast.h ../head/DoublingTest.h ../head/QuickFindUF.h ../head/QuickUnionUF.h ../head/WeightedQuickUnionUF.h ../head/UF.h ../head/LinearRegression.h)
77
add_executable(10_Date ${SOURCE_FILES})
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_LinearRegression)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(28_LinearRegression ${SOURCE_FILES})

ch1/28_LinearRegression/main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <iostream>
2+
#include "../head/LinearRegression.h"
3+
4+
using namespace std;
5+
6+
7+
int main() {
8+
vector<double> a{1.0, 2.0, 3.0, 4.1}, b{1.01, 2.01, 2.99, 4.0};
9+
LinearRegression lr(a, b);
10+
cout << lr << endl;
11+
}

ch1/34_QuickUnionUF/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(34_QuickUnionUF)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(34_QuickUnionUF ${SOURCE_FILES})

ch1/34_QuickUnionUF/main.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include "../head/QuickUnionUF.h"
4+
5+
using namespace std;
6+
7+
/**
8+
* Reads in a sequence of pairs of integers (between 0 and n-1) from standard input,
9+
* where each integer represents some object;
10+
* if the sites are in different components, merge the two components
11+
* and print the pair to standard output.
12+
*
13+
* @param args the command-line arguments
14+
*/
15+
int main() {
16+
fstream file("/home/ace/AceDev/C++/algorithm/ch1/data/tinyUF.txt");
17+
int n, p, q;
18+
file >> n;
19+
QuickUnionUF uf(n);
20+
while (file >> p >> q) {
21+
if (uf.connected(p, q)) continue;
22+
uf.union_op(p, q);
23+
cout << p << " " << q << endl;
24+
}
25+
cout << uf.getcount() << " components" << endl;
26+
}
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(35_WeightedQuickUnionUF)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(35_WeightedQuickUnionUF ${SOURCE_FILES})

ch1/35_WeightedQuickUnionUF/main.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include "../head/WeightedQuickUnionUF.h"
4+
5+
using namespace std;
6+
7+
/**
8+
* Reads in a sequence of pairs of integers (between 0 and n-1) from standard input,
9+
* where each integer represents some object;
10+
* if the sites are in different components, merge the two components
11+
* and print the pair to standard output.
12+
*
13+
* @param args the command-line arguments
14+
*/
15+
int main() {
16+
fstream file("/home/ace/AceDev/C++/algorithm/ch1/data/largeUF.txt");
17+
int n, p, q;
18+
file >> n;
19+
WeightedQuickUnionUF uf(n);
20+
while (file >> p >> q) {
21+
if (uf.connected(p, q)) continue;
22+
uf.union_op(p, q);
23+
cout << p << " " << q << endl;
24+
}
25+
cout << uf.getcount() << " components" << endl;
26+
}

ch1/36_UF/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(36_UF)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(36_UF ${SOURCE_FILES})

ch1/36_UF/main.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include "../head/UF.h"
4+
5+
using namespace std;
6+
7+
/**
8+
* Reads in a sequence of pairs of integers (between 0 and n-1) from standard input,
9+
* where each integer represents some object;
10+
* if the sites are in different components, merge the two components
11+
* and print the pair to standard output.
12+
*
13+
* @param args the command-line arguments
14+
*/
15+
int main() {
16+
fstream file("/home/ace/AceDev/C++/algorithm/ch1/data/largeUF.txt");
17+
int n, p, q;
18+
file >> n;
19+
UF uf(n);
20+
while (file >> p >> q) {
21+
if (uf.connected(p, q)) continue;
22+
uf.union_op(p, q);
23+
cout << p << " " << q << endl;
24+
}
25+
cout << uf.getcount() << " components" << endl;
26+
}

ch1/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ add_subdirectory(24_LinkedBag)
3434
add_subdirectory(25_Bag)
3535
add_subdirectory(26_Stopwatch)
3636
add_subdirectory(27_StopwatchCPU)
37-
37+
add_subdirectory(28_LinearRegression)
3838
add_subdirectory(29_ThreeSum)
3939
add_subdirectory(30_ThreeSumFast)
4040
add_subdirectory(31_DoublingTest)
4141
add_subdirectory(32_DoublingRatio)
42-
add_subdirectory(33_QuickFindUF)
42+
add_subdirectory(33_QuickFindUF)
43+
add_subdirectory(34_QuickUnionUF)
44+
add_subdirectory(35_WeightedQuickUnionUF)
45+
add_subdirectory(36_UF)

0 commit comments

Comments
 (0)