Data Structures Final Review

131 flashcardsby Jackson
Study these flashcardsFree · spaced repetition · iPhone, Android, Mac & web

All cards

1
O(1)
Constant Time
2
O(logN)
Logarithmic Time
3
O(sqrtN)
Logarithmic Time
4
O(N)
Linear Time
5
O(N log N)
Linear Time
6
O(N^2)
Quadratic Time
7
O(N^3)
Quadratic Time
8
O(2^n)
Exponential Time
9
O(e^n)
Exponential Time
10
Abstract Data Type (ADT)
A set of operations that should be available to a data structure.
11
List ADT Methods
Insert, Find, Modify, Delete
12
Stack ADT Methods
Push, Pop, Peek
13
Queue ADT Methods
Enqueue, Dequeue, Peek
14
Hash Table ADT Methods
Insert, Find, Delete
15
Graph ADT Methods
Insert, Find, Delete
16
List Times (Array, Unordered, Shifting)
Insert: * O(1), Find: O(N), Modify: O(N), Delete: O(N)
17
List Times (Array, Unordered, Lazy)
Insert: * O(1), Find: O(N), Modify: O(N), Delete: O(N)
18
List Times (Linked List, Unordered, Tail, No Double)
Insert: O(1), Find: O(N), Modify: O(N), Delete: O(N)
19
List Times (Linked List, Unordered, Tail, Doubly Linked)
Insert: O(1), Find: O(N), Modify: O(N), Delete: O(N)
20
List Times (Array, Ordered, Linear Search, Shifting)
Insert: O(N), Find: O(N), Modify: O(N), Delete: O(N)
21
List Times (Array, Ordered, Binary Search, Shifting)
Insert: O(N), Find: O(log N), Modify: O(log N), Delete: O(N)
22
List Times (Array, Ordered, Binary Search, Lazy Deletion)
Insert: O(N), Find: O(log N), Modify: O(log N), Delete: O(log N)
23
List Times (Linked List, Ordered, Linear Search, No Double)
Insert: O(N), Find: O(N), Modify: O(N), Delete: O(N)
24
List Times (Linked List, Ordered, Linear Search, Doubly Linked)
Insert: O(N), Find: O(N), Modify: O(N), Delete: O(N)
25
Stack Times (Array)
Push: * O(1), Pop: O(1), Peek: O(1)
26
Stack Times (Linked List, No Double)
Push: O(1), Pop: O(1), Peek: O(1)
27
Stack Times (Linked List, Doubly Linked)
Push: O(1), Pop: O(1), Peek: O(1)
28
Queue Times (Array, Lazy Deletion)
Enqueue: * O(1), Dequeue: O(1), Peek: O(1)
29
Queue Times (Array, Shifting)
Enqueue: * O(1), Dequeue: O(N), Peek: O(1)
30
Queue Times (Circular Array)
Enqueue: * O(1), Dequeue: O(1), Peek: O(1)
31
Queue Times (Linked List, No Tail, No Double)
Enqueue: O(1), Dequeue: O(N), Peek: O(N)
32
Queue Times (Linked List, Tail, No Double)
Enqueue: O(1), Dequeue: O(1), Peek: O(1)
33
Queue Times (Linked List, Tail, Doubly Linked)
Enqueue: O(1), Dequeue: O(1), Peek: O(1)
34
Hash Table Times (Array)
Insert: O(N), Find: O(N), Delete: O(N)
35
Graph
A set of vertices and edges that connect said vertices
36
Binary Tree Times
Insert: O(N), Find: O(N), Delete: O(N)
37
Binary Search Tree Times
Insert: O(N), Find: O(N), Delete: O(N)
38
Complete Tree Times
Insert: O(1), Find: O(N), Delete: O(N)
39
Heap Times
Insert: O(1) + O(logN), Find: O(N), Delete: O(N), DeleteMin: O(1) + O(logN)
40
AVL Times
Insert: O(log N) + O(1), Find: O(log N), Delete: O(log N) + O(1)
41
Splay Trees
Insert: O(N) & * O(log N), Find: O(N) & * O(log N), Delete: O(N) & * O(log N)
42
Master Theorem
A formula for calculating recurrence relations
43
Master Theorem Formula
T(n) = a * T(n) + (n^k * log^p(n))
44
Master Theorem (a > b^k)
T(n) = Omega(n^(log_b(a)))
45
Master Theorem (a = b^k, p >= 0)
T(n) = Omega(n^(log_b(a)) * log^(p+1)(n))
46
Master Theorem (a = b^k, p = -1)
T(n) = Omega(n^(log_b(a)) * log(log(n)))
47
Master Theorem (a = b^k, p < -1)
T(n) = Omega(n^(log_b(a)))
48
Master Theorem (a < b^k, p >= 0)
T(n) = Omega(n^k * log^p(n))
49
Master Theorem (a < b^k, p < 0)
T(n) = Omega(n^k)
50
Algorithm
A high level, language-independent description of a step-by-step process
51
Data Structure
A specific organization of data and family of algorithms for implementing an ADT
52
Priority Queue
A data structure whose first element is the element with the lowest priority. Has operations insert, deleteMin, and is_empty.
53
Queue
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
54
Stack
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.
55
Heap 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.
56
Heap 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
57
Balance Condition
A rearrangement function for a BST that keeps the average height at O(log N)
58
AVL Tree
A BST whose nodes adhere to the AVL property, and implement the AVL balance condition.
59
AVL 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
60
Right 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
61
Left 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
62
AVL 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.
63
Splay
An operation in Splay Trees that move the most recently access nodes to the top, with the side effect of balancing the whole tree.
64
Blind Rebalance
When a tree rebalances itself without using height information.
65
Zig-Zig
Perform two right rotations on the target node.
66
Zag-Zag
Perform two left rotations on the target node.
67
Zig-Zag
Perform a right rotation then a left rotation on the target node.
68
Zag-Zig
Perform a left rotation then a right rotation on the target node
69
Splay Tree Join (T1 < T2)
Splay the maximum element of T1 to the root, then make T2 a right child of T1
70
Splay Tree Split
Splay the specified node up to the root, and then cut the link from the root to the left child.
71
Splay Tree Delete
Splay the specified node up to the root, delete it, then join the resulting subtrees.
72
Red-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.
73
Collision Resolution
A method for handling collisions in a hash table
74
Open Addressing
A collision resolution strategy that resolves collisions y choosing a different location when the natural choice is full.
75
Linear Probing
Find the index and keep incrementing by 1 until an open spot is found.
h(key) + i % T
76
Quadratic Probing
Find the index and keep incrementing polynomially until an open spot is found.
h(key) + i^2 % T
77
Double Hashing
Find the index using a first hashing function, and use a second hashing function to handle collisions.
(h(key) + i*g(key)) % T
78
Separate Chaining
A collision resolution strategy that places colliding elements into their own separate collections, usually a linked list.
79
Load factor
The average amount of elements per bucket in a separate chaining hash table. Equal to N/TableSize.
80
2-3-4 Tree Times
Insert: O(log N), Find: O(log N), Delete: O(log N)
81
2-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.
82
2-3-4 Tree
A bottom-up search tree whose nodes can have 1, 2, or 3 elements, and can have n elements + 1 children.
83
2-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.
84
2-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.
85
Undirected Graph
A graph in which the edges have no direction.
86
Directed Graph
A graph in which the edges have a direction.
87
Self Edge
An edge that originates and ends at the same node
88
Cycle
A path that originates and ends at the same node.
89
Path
A sequence of edges that start at one node and ends at another node, connected via intermediary edges between nodes.
90
Connected Graph
A graph such that there is a path going from any node to any other node
91
Disconnected Graph
A graph that is not connected (there exist nodes with no paths between them)
92
Tree
A graph with no cycles.
93
Adjacency Matrix
A |V| X |V| matrix M, where if M[u][v] is true, then there is an edge from u to v.
94
Topological 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.
95
Topological Sort Requirements
A directed graph with no cycles.
96
Depth-First Search
A search in which children of a node are considered before siblings are considered.
97
Breadth-First Search
A search in which siblings of a node are considered before children are considered.
98
Dijkstra'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
99
Topological 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.
100
Greedy Algorithm
An algorithm that makes the best choice at each step.
101
Spanning 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).
102
Weighted Graph
A graph where each edge has an associated cost.
103
Prim'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.
104
Kruskal's Algorithm
1. Pick the lowest cost edge that does not cause a cycle
2. Repeat until all vertices are connected.
105
All Pairs Shortest Paths
A table describing the shortest path from any vertex to any other vertex.
106
Floyd-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|
107
Floyd-Warshall All Pairs Shortest Path (Find Distance a -> b)
Look up D[a][b].
108
Floyd-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
109
Single Source Shortest Path
Two arrays describing the distances and sequences from a single source node to every other node.
110
Bellman-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|
111
Union-Find ADT
A collection of sets with the operations union and find.
112
Union-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.
113
Topological Sorting Algorithm Naive Initialization Time
O(|V| + |E|)
114
Topological Sorting Algorithm Naive Sum Of All Find-New-Vertex Time
O(|V|^2)
115
Topological Sorting Algorithm Naive Decrements Time
O(|E|)
116
Topological Sorting Algorithm Naive Total Time
O(|V|^2)
117
Topological Sorting Algorithm Improved Initialization Time
O(|V| + |E|)
118
Topological Sorting Algorithm Improved Enqueues + Dequeues Time
O(|V|)
119
Topological Sorting Algorithm Improved Sum Of All Decrements Time
O(|E|)
120
Topological Sorting Algorithm Improved Total Time
O(|V| + |E|)
121
Dijkstra's Algorithm Best Running Time
O((|V| + |E|) log |V|)
122
Bellman-Ford Shortest Path Algorithm Initialization Time
O(|V|)
123
Bellman-Ford Shortest Path Algorithm Relaxation Time
O(|V||E|)
124
Bellman-Ford Shortest Path Algorithm Negative Cycles Time
O(|E|)
125
Bellman-Ford Shortest Path Algorithm Total Time (General)
O(|V||E|)
126
Bellman-Ford Shortest Path Algorithm Total Time (Complete Graph)
O(|V|^3)
127
Prim's Algorithm Running Time
O(|E| log |V|)
128
Kruskal's Algorithm Running Time
O(|E| log |V|)
129
Prim's Algorithm Running Time (Fibonacci Heap)
O(|E| + log |V|)
130
Union Find Running Time
O(log |N|)
131
Floyd-Warshall All Pairs Shortest Path Algorithm
O(|V|^3)
Study all 131 cards for free