3O(sqrtN)
Logarithmic Time
10Abstract Data Type (ADT)
A set of operations that should be available to a data structure.
11List ADT Methods
Insert, Find, Modify, Delete
12Stack ADT Methods
Push, Pop, Peek
13Queue ADT Methods
Enqueue, Dequeue, Peek
14Hash Table ADT Methods
Insert, Find, Delete
15Graph ADT Methods
Insert, Find, Delete
16List Times (Array, Unordered, Shifting)
Insert: * O(1), Find: O(N), Modify: O(N), Delete: O(N)
17List Times (Array, Unordered, Lazy)
Insert: * O(1), Find: O(N), Modify: O(N), Delete: O(N)
18List Times (Linked List, Unordered, Tail, No Double)
Insert: O(1), Find: O(N), Modify: O(N), Delete: O(N)
19List Times (Linked List, Unordered, Tail, Doubly Linked)
Insert: O(1), Find: O(N), Modify: O(N), Delete: O(N)
20List Times (Array, Ordered, Linear Search, Shifting)
Insert: O(N), Find: O(N), Modify: O(N), Delete: O(N)
21List Times (Array, Ordered, Binary Search, Shifting)
Insert: O(N), Find: O(log N), Modify: O(log N), Delete: O(N)
22List Times (Array, Ordered, Binary Search, Lazy Deletion)
Insert: O(N), Find: O(log N), Modify: O(log N), Delete: O(log N)
23List Times (Linked List, Ordered, Linear Search, No Double)
Insert: O(N), Find: O(N), Modify: O(N), Delete: O(N)
24List Times (Linked List, Ordered, Linear Search, Doubly Linked)
Insert: O(N), Find: O(N), Modify: O(N), Delete: O(N)
25Stack Times (Array)
Push: * O(1), Pop: O(1), Peek: O(1)
26Stack Times (Linked List, No Double)
Push: O(1), Pop: O(1), Peek: O(1)
27Stack Times (Linked List, Doubly Linked)
Push: O(1), Pop: O(1), Peek: O(1)
28Queue Times (Array, Lazy Deletion)
Enqueue: * O(1), Dequeue: O(1), Peek: O(1)
29Queue Times (Array, Shifting)
Enqueue: * O(1), Dequeue: O(N), Peek: O(1)
30Queue Times (Circular Array)
Enqueue: * O(1), Dequeue: O(1), Peek: O(1)
31Queue Times (Linked List, No Tail, No Double)
Enqueue: O(1), Dequeue: O(N), Peek: O(N)
32Queue Times (Linked List, Tail, No Double)
Enqueue: O(1), Dequeue: O(1), Peek: O(1)
33Queue Times (Linked List, Tail, Doubly Linked)
Enqueue: O(1), Dequeue: O(1), Peek: O(1)
34Hash Table Times (Array)
Insert: O(N), Find: O(N), Delete: O(N)
35Graph
A set of vertices and edges that connect said vertices
36Binary Tree Times
Insert: O(N), Find: O(N), Delete: O(N)
37Binary Search Tree Times
Insert: O(N), Find: O(N), Delete: O(N)
38Complete Tree Times
Insert: O(1), Find: O(N), Delete: O(N)
39Heap Times
Insert: O(1) + O(logN), Find: O(N), Delete: O(N), DeleteMin: O(1) + O(logN)
40AVL Times
Insert: O(log N) + O(1), Find: O(log N), Delete: O(log N) + O(1)
41Splay Trees
Insert: O(N) & * O(log N), Find: O(N) & * O(log N), Delete: O(N) & * O(log N)
42Master Theorem
A formula for calculating recurrence relations
43Master Theorem Formula
T(n) = a * T(n) + (n^k * log^p(n))
44Master Theorem (a > b^k)
T(n) = Omega(n^(log_b(a)))
45Master Theorem (a = b^k, p >= 0)
T(n) = Omega(n^(log_b(a)) * log^(p+1)(n))
46Master Theorem (a = b^k, p = -1)
T(n) = Omega(n^(log_b(a)) * log(log(n)))
47Master Theorem (a = b^k, p < -1)
T(n) = Omega(n^(log_b(a)))
48Master Theorem (a < b^k, p >= 0)
T(n) = Omega(n^k * log^p(n))
49Master Theorem (a < b^k, p < 0)
T(n) = Omega(n^k)
50Algorithm
A high level, language-independent description of a step-by-step process
51Data Structure
A specific organization of data and family of algorithms for implementing an ADT
52Priority Queue
A data structure whose first element is the element with the lowest priority. Has operations insert, deleteMin, and is_empty.
53Queue
A data structure whose elements have First-In First-Out insertion and removal. The first element inserted is the first element removed. Has operations enqueue, dequeue, and peek
54Stack
A data structure whose elements have Last-In First-Out insertion and removal. The last element inserted is the first element removed. Has push, pop, and peek.
55Heap Insertion
1. Place the element in the next available slot of the complete tree
2. Shift the element upward, swapping with parents, until each parent's priority is less than the priority of its children.
56Heap deleteMin
1. Swap the root of the heap with the last element of the heap
2. Delete the last element of the heap
3. Shift the root downward, swapping with children, until each child's priority is larger than the priority of its parents
57Balance Condition
A rearrangement function for a BST that keeps the average height at O(log N)
58AVL Tree
A BST whose nodes adhere to the AVL property, and implement the AVL balance condition.
59AVL Property
The left and right subtrees of a node differ by -1, 0, or 1. They do not differ from a greater amount. If they do, the balance condition is activated
60Right Rotation
Swaps a parent for its left child, making the parent its right child. If the left child already has right children, it becomes the left children of the parent node instead.
Available to AVL Trees, Splay Trees
61Left Rotation
Swaps a parent for its right child, making the parent its left child. If the right child already has left children, it becomes the right children of the parent node instead.
Available to AVL Trees, Splay Trees
62AVL Balance Condition
On a LL insertion, perform a right rotation on the imbalanced node.
On a RR insertion, perform a left rotation on the imbalanced node.
On a LR insertion, perform a left rotation between the problematic grandchild and child, and then perform a right rotation between the new child and the parent.
On a RL insertion, perform a right rotation between the problematic grandchild and child, and then perform a left rotation between the new child and the parent.
63Splay
An operation in Splay Trees that move the most recently access nodes to the top, with the side effect of balancing the whole tree.
64Blind Rebalance
When a tree rebalances itself without using height information.
65Zig-Zig
Perform two right rotations on the target node.
66Zag-Zag
Perform two left rotations on the target node.
67Zig-Zag
Perform a right rotation then a left rotation on the target node.
68Zag-Zig
Perform a left rotation then a right rotation on the target node
69Splay Tree Join (T1 < T2)
Splay the maximum element of T1 to the root, then make T2 a right child of T1
70Splay Tree Split
Splay the specified node up to the root, and then cut the link from the root to the left child.
71Splay Tree Delete
Splay the specified node up to the root, delete it, then join the resulting subtrees.
72Red-Black Tree Properties
1) Every node has a color either red or black.
2) Root of tree is always black.
3) There are no two adjacent red nodes (A red node cannot have a red parent or red child).
4) Every path from a node (including root) to any of its descendant NULL node has the same number of black nodes.
73Collision Resolution
A method for handling collisions in a hash table
74Open Addressing
A collision resolution strategy that resolves collisions y choosing a different location when the natural choice is full.
75Linear Probing
Find the index and keep incrementing by 1 until an open spot is found.
h(key) + i % T
76Quadratic Probing
Find the index and keep incrementing polynomially until an open spot is found.
h(key) + i^2 % T
77Double Hashing
Find the index using a first hashing function, and use a second hashing function to handle collisions.
(h(key) + i*g(key)) % T
78Separate Chaining
A collision resolution strategy that places colliding elements into their own separate collections, usually a linked list.
79Load factor
The average amount of elements per bucket in a separate chaining hash table. Equal to N/TableSize.
802-3-4 Tree Times
Insert: O(log N), Find: O(log N), Delete: O(log N)
812-3-4 Tree Insertion
If any bottom nodes are a 4 node (has three elements), preemptively split it upwards.
Search for the location to place the node and then insert it.
822-3-4 Tree
A bottom-up search tree whose nodes can have 1, 2, or 3 elements, and can have n elements + 1 children.
832-3-4 Preemptive Split
If a node has 3 elements, upon insertion, the node must be split upwards. The middle node becomes the parent of a left 2-node and a right 2-node, both holding a single element. If there are no parents, a new parent is formed, otherwise it joins the already existing parent. Splits may cascade upwards, upon which this is repeated.
842-3-4 Removal
Start at the root, searching for the node to delete. Along the way own, turn 2-nodes into three nodes (except for the root).
If an adjacent sibling is a 3 or 4 nodes, redistribute items from the sibling and adopt the child.
If the adjacent sibling is a 2-node, redistribute items from the parent (which will always have at least 2 items, unless it's the root), then merge the nodes.
Once the node is found, move it downward to a leaf node, shifting nodes along the way, then delete it.
85Undirected Graph
A graph in which the edges have no direction.
86Directed Graph
A graph in which the edges have a direction.
87Self Edge
An edge that originates and ends at the same node
88Cycle
A path that originates and ends at the same node.
89Path
A sequence of edges that start at one node and ends at another node, connected via intermediary edges between nodes.
90Connected Graph
A graph such that there is a path going from any node to any other node
91Disconnected Graph
A graph that is not connected (there exist nodes with no paths between them)
92Tree
A graph with no cycles.
93Adjacency Matrix
A |V| X |V| matrix M, where if M[u][v] is true, then there is an edge from u to v.
94Topological Sort
A linear ordering of its vertices such that for every directed edge uv from vertex u to vertex v, u comes before v in the ordering.
95Topological Sort Requirements
A directed graph with no cycles.
96Depth-First Search
A search in which children of a node are considered before siblings are considered.
97Breadth-First Search
A search in which siblings of a node are considered before children are considered.
98Dijkstra's algorithm
An algorithm for finding the shortest paths from an origin node to every other node.
1. Pick the closest unknown vertex v
2. Add it to the known vertices
3. Update distances for nodes with edges from v
99Topological Sort Algorithm
1. Label each vertex with its in-degree
2. Enqueue each vertex with a degree of 0
3. Dequeue the first vertex, add it to the ordering, and then decrease the in-degree of all the vertices they point to.
4. Repeat Steps 2-4 until all vertices are ordered.
100Greedy Algorithm
An algorithm that makes the best choice at each step.
101Spanning Tree
A subgraph of a graph that includes all the vertices of the graph while retaining the minimum amount of edges (or minimum cost in the case of a weighted graph).
102Weighted Graph
A graph where each edge has an associated cost.
103Prim's Algorithm
1. Pick a node and mark it known
2. Select the lowest edge that is connected to our already known nodes, and is connected to an unknown node.
3. Add the edge to our spanning tree, and mark both it and the vertex its connected to as known.
4. Repeat 2-4 until all vertices are known.
104Kruskal's Algorithm
1. Pick the lowest cost edge that does not cause a cycle
2. Repeat until all vertices are connected.
105All Pairs Shortest Paths
A table describing the shortest path from any vertex to any other vertex.
106Floyd-Warshall All Pairs Shortest Path Algorithm
k = iteration number
D_k = |V| x |V| Distance table in kth iteration
S_k = |V| x |V| Sequence table in kth iteration
d_ij = Distance between vertex i and j
1. Remove self loops and resolve multiedges by picking the lowest node.
2. Fill the S_0 table by filling each column with the column number
3. Fill the D_0 table by filling dij with the cost of the edge between i and j if it exists, and infinity if it doesn't exist. If i == j, mark it as nil.
4. For each cell C_ij in D_k, if in D_k-1 d_ij > d_ik + d_kj, then C_ij = d_ik + d_kj and S_ij = k. Otherwise, C_ij = d_ij and S_ij copies the value from the previous table.
5. Increment k by 1. Loop 4-5 until k > |V|
107Floyd-Warshall All Pairs Shortest Path (Find Distance a -> b)
Look up D[a][b].
108Floyd-Warshall All Pairs Shortest Path (Find Sequence a -> b)
Seq = []
"Seq top" is the topmost value of the sequence stack
1. Push vertex a onto Seq
2. Push S[Seq top][b] onto Seq
3. Repeat 2 until S[Seq top][b] == b
109Single Source Shortest Path
Two arrays describing the distances and sequences from a single source node to every other node.
110Bellman-Ford Single Source Shortest Path Algorithm
D = Distance array
P = Predecessor Array
k = Iteration count
1. Fill the distance array with 0 at the source node, and infinity at every other node
2. Fill the predecessor array with nil
3. For each edge u -> v, determine if v.d > u.d + w(u, v). If so, set v.d = u.d + w(u, v), and set v.p = u.
4. Increment k, and repeat until k = |V|
111Union-Find ADT
A collection of sets with the operations union and find.
112Union-Find Implementation
Start with a forest. Each forest has a weight corresponding to the amount of nodes in their uptree.
This can be represented using a parent array and a weight array.
Union(a, b): Find the roots of a and b. If a.w < b.w, have a point to b. Otherwise, have b point to a. Update the weight of b to be |a| + |b|.
Find(a): Find the root of a. If path compression is on, shift a upwards to directly point to the root to be accessed earlier.
113Topological Sorting Algorithm Naive Initialization Time
O(|V| + |E|)
114Topological Sorting Algorithm Naive Sum Of All Find-New-Vertex Time
O(|V|^2)
115Topological Sorting Algorithm Naive Decrements Time
O(|E|)
116Topological Sorting Algorithm Naive Total Time
O(|V|^2)
117Topological Sorting Algorithm Improved Initialization Time
O(|V| + |E|)
118Topological Sorting Algorithm Improved Enqueues + Dequeues Time
O(|V|)
119Topological Sorting Algorithm Improved Sum Of All Decrements Time
O(|E|)
120Topological Sorting Algorithm Improved Total Time
O(|V| + |E|)
121Dijkstra's Algorithm Best Running Time
O((|V| + |E|) log |V|)
122Bellman-Ford Shortest Path Algorithm Initialization Time
O(|V|)
123Bellman-Ford Shortest Path Algorithm Relaxation Time
O(|V||E|)
124Bellman-Ford Shortest Path Algorithm Negative Cycles Time
O(|E|)
125Bellman-Ford Shortest Path Algorithm Total Time (General)
O(|V||E|)
126Bellman-Ford Shortest Path Algorithm Total Time (Complete Graph)
O(|V|^3)
127Prim's Algorithm Running Time
O(|E| log |V|)
128Kruskal's Algorithm Running Time
O(|E| log |V|)
129Prim's Algorithm Running Time (Fibonacci Heap)
O(|E| + log |V|)
130Union Find Running Time
O(log |N|)
131Floyd-Warshall All Pairs Shortest Path Algorithm
O(|V|^3)