The Java Cheat Sheet can be run using a command-line window and running the command as different interrelated methods. From a programmatic perspective, Java provides a very rich and extensive set of library methods than its competitors and thus it has become a much preferable language for the programmers. The Java Graph class relies on an underlying Vertex class with the following behaviors: a constructor that sets data to the passed in inputData and sets edges to an empty ArrayList; an.addEdge method that takes a vertex and weight and adds an edge to edges; a.removeEdge method that takes a vertex and removes it from edges. THE JAVA LANGUAGE CHEAT SHEET Primitive Types: INSTANTIATION INTEGER: byte(8bit),short(16bit),int(32bit), long(64bit),DECIM:float(32bit),double(64bit),OTHER: boolean.
We summarize the performance characteristics of classic algorithms anddata structures for sorting, priority queues, symbol tables, and graph processing.
We also summarize some of the mathematics useful in the analysis of algorithms, including commonly encountered functions;useful formulas and appoximations; properties of logarithms;asymptotic notations; and solutions to divide-and-conquer recurrences.
ALGORITHM | CODE | STABLE | BEST | AVERAGE | WORST | REMARKS | |
---|---|---|---|---|---|---|---|
selection sort | Selection.java | ✔ | ½ n 2 | ½ n 2 | ½ n 2 | n exchanges; quadratic in best case | |
insertion sort | Insertion.java | ✔ | ✔ | n | ¼ n 2 | ½ n 2 | use for small or partially-sorted arrays |
bubble sort | Bubble.java | ✔ | ✔ | n | ½ n 2 | ½ n 2 | rarely useful; use insertion sort instead |
shellsort | Shell.java | ✔ | n log3n | unknown | c n 3/2 | tight code; subquadratic | |
mergesort | Merge.java | ✔ | ½ n lg n | n lg n | n lg n | n log n guarantee; stable | |
quicksort | Quick.java | ✔ | n lg n | 2 n ln n | ½ n 2 | n log n probabilistic guarantee; fastest in practice | |
heapsort | Heap.java | ✔ | n† | 2 n lg n | 2 n lg n | n log n guarantee; in place | |
†n lg n if all keys are distinct |
DATA STRUCTURE | CODE | INSERT | MIN | DELETE | MERGE | ||
---|---|---|---|---|---|---|---|
array | BruteIndexMinPQ.java | 1 | n | n | 1 | 1 | n |
binary heap | IndexMinPQ.java | log n | log n | 1 | log n | log n | n |
d-way heap | IndexMultiwayMinPQ.java | logdn | d logdn | 1 | logdn | d logdn | n |
binomial heap | IndexBinomialMinPQ.java | 1 | log n | 1 | log n | log n | log n |
Fibonacci heap | IndexFibonacciMinPQ.java | 1 | log n† | 1 | 1 † | log n† | 1 |
† amortized guarantee |
worst case | average case | ||||||
---|---|---|---|---|---|---|---|
DATA STRUCTURE | CODE | SEARCH | INSERT | DELETE | SEARCH | INSERT | DELETE |
sequential search (in an unordered list) | SequentialSearchST.java | n | n | n | n | n | n |
binary search (in a sorted array) | BinarySearchST.java | log n | n | n | log n | n | n |
binary search tree (unbalanced) | BST.java | n | n | n | log n | log n | sqrt(n) |
red-black BST (left-leaning) | RedBlackBST.java | log n | log n | log n | log n | log n | log n |
AVL | AVLTreeST.java | log n | log n | log n | log n | log n | log n |
hash table (separate-chaining) | SeparateChainingHashST.java | n | n | n | 1 † | 1 † | 1 † |
hash table (linear-probing) | LinearProbingHashST.java | n | n | n | 1 † | 1 † | 1 † |
† uniform hashing assumption |
PROBLEM | ALGORITHM | CODE | TIME | SPACE |
---|---|---|---|---|
path | DFS | DepthFirstPaths.java | E + V | V |
shortest path (fewest edges) | BFS | BreadthFirstPaths.java | E + V | V |
cycle | DFS | Cycle.java | E + V | V |
directed path | DFS | DepthFirstDirectedPaths.java | E + V | V |
shortest directed path (fewest edges) | BFS | BreadthFirstDirectedPaths.java | E + V | V |
directed cycle | DFS | DirectedCycle.java | E + V | V |
topological sort | DFS | Topological.java | E + V | V |
bipartiteness / odd cycle | DFS | Bipartite.java | E + V | V |
connected components | DFS | CC.java | E + V | V |
strong components | Kosaraju–Sharir | KosarajuSharirSCC.java | E + V | V |
strong components | Tarjan | TarjanSCC.java | E + V | V |
strong components | Gabow | GabowSCC.java | E + V | V |
Eulerian cycle | DFS | EulerianCycle.java | E + V | E + V |
directed Eulerian cycle | DFS | DirectedEulerianCycle.java | E + V | V |
transitive closure | DFS | TransitiveClosure.java | V (E + V) | V 2 |
minimum spanning tree | Kruskal | KruskalMST.java | E log E | E + V |
minimum spanning tree | Prim | PrimMST.java | E log V | V |
minimum spanning tree | Boruvka | BoruvkaMST.java | E log V | V |
shortest paths (nonnegative weights) | Dijkstra | DijkstraSP.java | E log V | V |
shortest paths (no negative cycles) | Bellman–Ford | BellmanFordSP.java | V (V + E) | V |
shortest paths (no cycles) | topological sort | AcyclicSP.java | V + E | V |
all-pairs shortest paths | Floyd–Warshall | FloydWarshall.java | V 3 | V 2 |
maxflow–mincut | Ford–Fulkerson | FordFulkerson.java | EV (E + V) | V |
bipartite matching | Hopcroft–Karp | HopcroftKarp.java | V ½ (E + V) | V |
assignment problem | successive shortest paths | AssignmentProblem.java | n 3 log n | n 2 |
FUNCTION | NOTATION | DEFINITION |
---|---|---|
floor | ( lfloor x rfloor ) | greatest integer (; le ; x) |
ceiling | ( lceil x rceil ) | smallest integer (; ge ; x) |
binary logarithm | ( lg x) or (log_2 x) | (y) such that (2^{,y} = x) |
natural logarithm | ( ln x) or (log_e x ) | (y) such that (e^{,y} = x) |
common logarithm | ( log_{10} x ) | (y) such that (10^{,y} = x) |
iterated binary logarithm | ( lg^* x ) | (0) if (x le 1;; 1 + lg^*(lg x)) otherwise |
harmonic number | ( H_n ) | (1 + 1/2 + 1/3 + ldots + 1/n) |
factorial | ( n! ) | (1 times 2 times 3 times ldots times n) |
binomial coefficient | ( n choose k ) | ( frac{n!}{k! ; (n-k)!}) |
NAME | NOTATION | DESCRIPTION | DEFINITION |
---|---|---|---|
Tilde | (f(n) sim g(n); ) | (f(n)) is equal to (g(n)) asymptotically (including constant factors) | ( ; displaystyle lim_{n to infty} frac{f(n)}{g(n)} = 1) |
Big Oh | (f(n)) is (O(g(n))) | (f(n)) is bounded above by (g(n)) asymptotically (ignoring constant factors) | there exist constants (c > 0) and (n_0 ge 0) such that (0 le f(n) le c cdot g(n)) forall (n ge n_0) |
Big Omega | (f(n)) is (Omega(g(n))) | (f(n)) is bounded below by (g(n)) asymptotically (ignoring constant factors) | ( g(n) ) is (O(f(n))) |
Big Theta | (f(n)) is (Theta(g(n))) | (f(n)) is bounded above and below by (g(n)) asymptotically (ignoring constant factors) | ( f(n) ) is both (O(g(n))) and (Omega(g(n))) |
Little oh | (f(n)) is (o(g(n))) | (f(n)) is dominated by (g(n)) asymptotically (ignoring constant factors) | ( ; displaystyle lim_{n to infty} frac{f(n)}{g(n)} = 0) |
Little omega | (f(n)) is (omega(g(n))) | (f(n)) dominates (g(n)) asymptotically (ignoring constant factors) | ( g(n) ) is (o(f(n))) |
NAME | NOTATION | EXAMPLE | CODE FRAGMENT |
---|---|---|---|
Constant | (O(1)) | array access arithmetic operation function call | |
Logarithmic | (O(log n)) | binary search in a sorted array insert in a binary heap search in a red–black tree | |
Linear | (O(n)) | sequential search grade-school addition BFPRT median finding | |
Linearithmic | (O(n log n)) | mergesort heapsort fast Fourier transform | |
Quadratic | (O(n^2)) | enumerate all pairs insertion sort grade-school multiplication | |
Cubic | (O(n^3)) | enumerate all triples Floyd–Warshall grade-school matrix multiplication | |
Polynomial | (O(n^c)) | ellipsoid algorithm for LP AKS primality algorithm Edmond's matching algorithm | |
Exponential | (2^{O(n^c)}) | enumerating all subsets enumerating all permutations backtracing search |
Here are some examples.
FUNCTION | (o(n^2)) | (O(n^2)) | (Theta(n^2)) | (Omega(n^2)) | (omega(n^2)) | (sim 2 n^2) | (sim 4 n^2) |
---|---|---|---|---|---|---|---|
(log_2 n) | ✔ | ✔ | |||||
(10n + 45) | ✔ | ✔ | |||||
(2n^2 + 45n + 12) | ✔ | ✔ | ✔ | ✔ | |||
(4n^2 - 2 sqrt{n}) | ✔ | ✔ | ✔ | ✔ | |||
(3n^3) | ✔ | ✔ | |||||
(2^n) | ✔ | ✔ |
RECURRENCE | (T(n)) | EXAMPLE |
---|---|---|
(T(n) = T(n,/,2) + 1) | (sim lg n) | binary search |
(T(n) = 2 T(n,/,2) + n) | (sim n lg n) | mergesort |
(T(n) = T(n-1) + n) | (sim frac{1}{2} n^2) | insertion sort |
(T(n) = 2 T(n,/,2) + 1) | (sim n) | tree traversal |
(T(n) = 2 T(n-1) + 1) | (sim 2^n) | towers of Hanoi |
(T(n) = 3 T(n,/,2) + Theta(n)) | (Theta(n^{log_2 3}) = Theta(n^{1.58...})) | Karatsuba multiplication |
(T(n) = 7 T(n,/,2) + Theta(n^2)) | (Theta(n^{log_2 7}) = Theta(n^{2.81...})) | Strassen multiplication |
(T(n) = 2 T(n,/,2) + Theta(n log n)) | (Theta(n log^2 n)) | closest pair |
Last modified on September 12, 2020.
Copyright © 2000–2019Robert SedgewickandKevin Wayne.All rights reserved.