C++ code common algorithms
Dijkstra
Assume N
nodes, source node is indexed by root
, using adjacency list
1 |
|
To track the actual shortest path, introduce an array int Predecessor[N]
,
update whenever dist[neighbor.second]
changes,
simply set to the new predecessor current.second
.
Union Find
1 |
|
Catalan number
The first Catalan numbers for n = 0, 1, 2, 3, … are
1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420, 24466267020, 91482563640, 343059613650, 1289904147324, 4861946401452, …
Fibonacci number
The first Fibonacci numbers for n = 0, 1, 2, 3, … are
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, …
KMP
1 |
|
Binary search
1 |
|
argsort in C++
1 |
|