- Rotation (cyclic shift)
- Longest increasing subsequence
- Maximum subsequence
- Majority element
- Sqrt decomposition
A
k-rotation (or ak-cyclic shift) of a sequence{a0a2...an-1}is a sequence{aP(1)aP(2)...aP(n-1)}, where the index permutation isP(i) = (i + k) % n.
📝
- Any non-zero rotation has no trivial cycles. The number of (non-trivial) cycles is
gcd(k, n).
🔗
📖
- Sec. 11.3: Rotation – A.A.Stepanov, D.E.Rose. From mathematics to generic programming (2014)
- Sec. 10.4: Rotate algorithms – A.A.Stepanov, P.McJones. Elements of programming (2009)
- Sec. 2.3: The power of primitives – J.Bentley. Programming pearls (1999)
📄
- C.A.Furia. Rotation of sequences: Algorithms and proofs. Preprint (2014).
Full text - D.Gries, H.Mills. Swapping sections. Technical report 81-452, Department of computer science, Cornell University, 1981.
Full text
Gist of the algorithm: reverse the subsequences
{a0...ak-1}and{ak...an-1}, then reverse the whole sequence{ak-1...a0an-1...ak}.
📝
- The total number of swaps is
⌊n/2⌋ + ⌊k/2⌋ + ⌊(n-k)/2⌋ ∼ n. With 3 assignments per swap, the total number of assignments is∼ 3n. - This algorithm is typically used to implement
std::rotatefor bidirectional iterators.
Gist of the algorithm: if
k = n - k, swap the subsequences{a0...ak-1}and{ak...an-1}; ifk < n - k, swap the subsequences{a0...ak-1}and{ak...a2k-1}, then proceed recursively for the suffix subsequence{a0...ak-1a2k...an-1}withk' = k; ifk > n - k, swap the subsequences{a0...an-k-1}and{ak...an-1}, then proceed recursively for the suffix subsequence{an-k...ak-1a0...an-k-1}withk' = 2k - n.
📝
- The section sizes
(k, n - k)form the same sequence as that obtained if the subtraction-based Euclidean algorithm is employed to calculategcd(k, n). - The total number of swaps is
n - gcd(k, n). With 3 assignments per swap, the total number of assignments is3[n - gcd(k, n)]. - The Gries–Mills algorithm can be implemented such that it only requires to move one step forward, so this algorithm is typically used to implement
std::rotatefor forward and random access iterators.
Gist of the algorithm: compute the number of cycles,
nc = gcd(k, n - k); for each cycle{aCi(0)aCi(1)aCi(2)...}withCi(j) = (i + jk) % n, i = 0, ..., nc - 1, make a cyclic shift of all the elements by one position to obtain{aCi(1)aCi(2)...aCi(0)}.
📝
- This algorithm is also known as the juggling algorithm.
- The total number of assignments is
n + gcd(k, n). However, this algorithm is not cache-friendly and can have poor performance in practice, although it makes much fewer (~2-3 times) memory accesses. - This algorithm is sometimes used to implement
std::rotatefor random access iterators.
📄
W.Fletcher, R.Silver. Algorithm 284: Interchange of two blocks of data – Communications of the ACM 9, 326 (1966)
Problem: find the longest monotonically increasing subsequence (not necessarily contiguous) within a given sequence.
📝
- The dynamic programming solution (without additional tricks) has running time
O(n2), and is not the most efficient one. The problem can be solved inO(n log n)time using an algorithm based on binary search. This algorithm is output-sensitive: if the size of the output, the lengthkof a subsequence, is taken into account, it requiresO(n log k)time. - Any comparison-based algorithm requires at least
n log2 n - n log2 log2 n + O(n)comparisons in the worst case.
🔗
- Longest increasing subsequence – Wikipedia
- Longest increasing subsequence – CP-Algorithms
🎥
- [Dynamic programming]https://www.youtube.com/watch?v=1ivFSH0ijOM&t=2570 – MIT OCW 6.006: Introduction to algorithms (2011)
📖
- Sec. 8.3: Longest increasing sequence – S.S.Skiena. The algorithm design manual (2008)
- Sec. 3.5.2 Classical examples – S.Halim, F.Halim. Competitive programming (2013)
- Sec. 6.5.2: Finding the kth smallest element, Sec. 6.11.1: Longest increasing subsequence – U.Manber. Introduction to algorithms: A creative approach (1989)
📄
- M.L.Fredman. On computing the length of longest increasing subsequences – Discrete Mathematics 11, 29 (1975)
Problem: find a contiguous subsequence with the largest sum within a given one-dimensional sequence.
📄
- J.Bentley. Programming pearls: Algorithm design techniques – Communications of the ACM 27, 865 (1984)
- D.Gries. A note on a standard strategy for developing loop invariants and loops – Science of computer programming 2, 207 (1982)
🔗
- Boyer–Moore majority vote algorithm – Wikipedia
🔗
- Sqrt decomposition – CP-Algorithms
- S.Kopeliovich. Sqrt decomposition – MIPT (2016)