Académique Documents
Professionnel Documents
Culture Documents
Post-Midterm 2 Material
Graph Traversals
All Traversals
● All graph traversals can be written
as variations of the baseline
traversal with a fringe
○ A fringe is any data structure that
supports insert(), poll(), remove()
○ This includes stacks and queues
BFS
● Replace the COLLECTION_TYPE with
a QUEUE
○ This search will traverse a graph “layer
by layer”, first the start node, then the
start node’s neighbors, then the start
node’s second degree neighbors, etc.
DFS
● Replace the COLLECTION_TYPE with
a STACK
○ This search will traverse a graph by
traversing the root, then picking a path
to follow down as long as it can (until it
hits a dead end).
○ After it hits a dead end, it find the most
recent node which has an unmarked
neighbor and continues searching from
there.
Orderings
● Pre Order
○ Parent then its children
○ First node will be the start node
● In Order
○ Left children then right children
○ Only really relevant in trees
○ Fun off topic fact: processes search trees in order
● Post Order
○ Children then the parent
○ Last node will be the start node
BFS Example
QUEUE VISITED
A A
B
D
A A
B B
D
C
E
A A
B B
D D
C
E
A A
B B
D D
C C
E
A A
B B
D D
C C
E E
F
A A
B B
D D
C C
E E
F F
A A A
B
D
A A A
B B B
C
E
D
A A A
B B B
C C C
E
D
A A A
B B B
C C C
E E E
D
F
A A A
B B B
C C C
E E E
D D D
F
A A A
B B B
C C C
E E E
D D D
F F F
A
B
C
E
D
F
C
D
F
E
B
A
We add each vertex to the fringe one time, and we check all edges two times,
so it runs in O(V+E) time. (also called linear time)
BFS, DFS generally traverses far vertices first while BFS generally traverses
close vertices first.
Rapidfire (3 / 3):
Say I represent a graph in real life with string,
glue, and blocks as shown. If I grab the light
green block and lift the whole graph ...
A E
6 D 3
Prim’s Visualized
﹍﹍﹍﹍﹍
9
B C
4
2
1 5
A E
6 D 3
Prim’s Visualized
﹍
9
B C
4
﹍
2
1
﹍
5
A
6 ﹍ D 3
E
﹍
Prim’s Visualized
﹍﹍﹍﹍
9
B C
4
2
1 5
A E
6 D 3
Prim’s Visualized
9
B C﹍
4
2 ﹍
1 5
﹍
A
﹍ E
6 D ﹍ 3
Prim’s Visualized
9
B C
4
2
1 5
A E
6 D 3
Prim’s Visualized
B C
4
2
1
A E
D 3
Kruskal’s Algorithm
● Construct an MST using Kruskal’s
● Sort the graph edges by increasing weight
● Starting with the min-weight edge, iteratively pick edges that won’t form a
cycle until we have N-1 edges in the MST
● Always produces the same MST assuming same sort and Weighted Quick
Union tie-breaking
○ WQU is used to make sure the MST is acyclic
Kruskal’s Visualized
9
B C Sorted Edges:
5 1. B - D, 1
2 2. C - D, 2
1 4 3. D - E, 3
4. C - E, 4
5. A - B, 5
A E 6. C - E, 6
6 D 3 7. B - C, 9
Kruskal’s Visualized
9
B C Sorted Edges:
5 1. B - D, 1
2 2. C - D, 2
1 4 3. D - E, 3
4. C - E, 4
5. A - B, 5
A E 6. C - E, 6
6 D 3 7. B - C, 9
Kruskal’s Visualized
9
B C Sorted Edges:
5 1. B - D, 1
2 2. C - D, 2
1 4 3. D - E, 3
4. C - E, 4
5. A - B, 5
A E 6. C - E, 6
6 D 3 7. B - C, 9
Kruskal’s Visualized
9
B C Sorted Edges:
5 1. B - D, 1
2 2. C - D, 2
1 4 3. D - E, 3
4. C - E, 4
5. A - B, 5
A E 6. C - E, 6
6 D 3 7. B - C, 9
Kruskal’s Visualized
9
B C Sorted Edges:
5 1. B - D, 1
2 2. C - D, 2
1 4 3. D - E, 3
4. C - E, 4
5. A - B, 5
A E 6. C - E, 6
6 D 3 7. B - C, 9
Kruskal’s Visualized
9
B C Sorted Edges:
5 1. B - D, 1
2 2. C - D, 2
1 4 3. D - E, 3
4. C - E, 4
5. A - B, 5
A E 6. C - E, 6
6 D 3 7. B - C, 9
Kruskal’s Visualized
B C Sorted Edges:
5 1. B - D, 1
2 2. C - D, 2
1 3. D - E, 3
4. C - E, 4
5. A - B, 5
A E 6. C - E, 6
D 3 7. B - C, 9
Practice MSTs
-------------
Solution for MST Practice
-----
Shortest Paths Trees
Shortest Paths Trees
● Graph (directed or undirected) has non-negative edge weights
● Compute shortest paths from a given source node s to all other vertices in
the graph
○ “Shortest” = sum of weights along path is smallest
Dijkstra’s Algorithm
● Find the shortest paths from the source node using Dijkstra’s
● Use a minimum priority queue to visit vertices in increasing path cost
○ Start at the source node (cost = 0)
○ Push fringe nodes onto the minPQ
■ Keep track of the distance to a node (cost)
■ Keep track of which node led to this node in the tree (prev)
○ Pop a node from the minPQ when visiting the node
● May give incorrect solution if the graph has negative edges
○ Once you pop a node off of the priority queue, it is not necessarily true that we have
found the shortest path to it (unlike in Dijkstra’s)
Prim’s vs. Dijkstra’s
● The two algorithms are exactly the same, except for the following
● Visit order
○ Dijkstra’s algorithm visits vertices in order of distance from the source.
○ Prim’s algorithm visits vertices in order of distance from the MST under construction.
● Visitation
○ Visiting a vertex in Dijkstra’s algorithm means to relax all of its edges.
○ Visiting a vertex in Prim’s algorithm means relaxing all of its edges, but under the metric
of distance from tree instead of distance from source.
Dijkstra’s Visualized
Vertex Visited Cost Prev
S F Inf NULL
A F Inf NULL
B F Inf NULL
C F Inf NULL
G F Inf NULL
Priority Queue : {}
Dijkstra’s Visualized
Vertex Visited Cost Prev
S F 0 NULL
A F Inf NULL
B F Inf NULL
C F Inf NULL
G F Inf NULL
Priority Queue : {(S,0)}
Dijkstra’s Visualized
Vertex Visited Cost Prev
S T 0 NULL
A F Inf NULL
B F Inf NULL
C F Inf NULL
G F Inf NULL
Priority Queue : {}
Dijkstra’s Visualized
Vertex Visited Cost Prev
S T 0 NULL
A F Inf NULL
B F 2 S
C F 2 S
G F Inf NULL
Priority Queue : {(B, 2), (C, 2)}
Dijkstra’s Visualized
Vertex Visited Cost Prev
S T 0 NULL
A F Inf NULL
B F 2 S
C T 2 S
G F Inf NULL
Priority Queue : {(B, 2)}
Dijkstra’s Visualized
Vertex Visited Cost Prev
S T 0 NULL
A F 4 C
B F 2 S
C T 2 S
G F Inf NULL
Priority Queue : {(B, 2), (A, 4)}
Dijkstra’s Visualized
Vertex Visited Cost Prev
S T 0 NULL
A F 4 C
B T 2 S
C T 2 S
G F Inf NULL
Priority Queue : {(A, 4)}
Dijkstra’s Visualized
Vertex Visited Cost Prev
S T 0 NULL
A F 3 B
B T 2 S
C T 2 S
G F Inf NULL
Priority Queue : {(A, 3)}
Dijkstra’s Visualized
Vertex Visited Cost Prev
S T 0 NULL
A T 3 B
B T 2 S
C T 2 S
G F Inf NULL
Priority Queue : {}
Dijkstra’s Visualized
Vertex Visited Cost Prev
S T 0 NULL
A T 3 B
B T 2 S
C T 2 S
G F 7 A
Priority Queue : {(G, 7)}
Dijkstra’s Visualized
Vertex Visited Cost Prev
S T 0 NULL
A T 3 B
B T 2 S
C T 2 S
G T 7 A
Priority Queue : {}
Dijkstra’s Visualized
Vertex Visited Cost Prev
S T 0 NULL
A T 3 B
B T 2 S
Following the Prev pointers for each vertex, reconstruct the shortest paths C T 2 S
from source S to the nodes B, C, A, G.
S → B = S-B
G T 7 A
S → C = S-C
S → A = S-B-A
S → G = S-B-A-G
Dijkstra’s Visualized
Vertex Visited Cost Prev
S T 0 NULL
A T 3 B
B T 2 S
C T 2 S
● Above is the shortests paths tree from source S
● Note: C-A is not in the shortest paths tree because we only popped A G T 7 A
from the queue after visiting it from B. (S-B-A offered a shorter path
than S-C-A)
A* Search Algorithm
● Given a starting vertex S and an ending vertex G, find the shortest path
between the two nodes S → G
● Optimization of Dijkstra’s
○ Break once you dequeue G
○ Visit nodes closer to G first
● Use a heuristic function h to estimate a node’s distance to G
○ minPQ priority = cost + h
○ priority(y) = minPathCost(S → y) + h(y)
○ priority(y) = [priority(x) - h(x) + cost(x → y)] + h(y)
Admissible and Consistent Heuristics
● Heuristic for A* search must be admissible and consistent
● An admissible heuristic function never overestimates the minimum path
distance from a given vertex A to destination G.
○ In other words, h(A) must never overestimate d(A, G).
● A consistent heuristic satisfies the following inequality
○ Given neighboring vertices A and B and a destination G
○ h(A) ≤ h(B) + d(A, B)
● All consistent heuristics are admissible
A* Visualized
Vertex Visited Cost Prev
S F Inf NULL
A F Inf NULL
B F Inf NULL
C F Inf NULL
G F Inf NULL
Priority Queue : {}
A* Visualized
Vertex Visited Cost Prev
S F 0 NULL
A F Inf NULL
B F Inf NULL
C F Inf NULL
G F Inf NULL
Priority Queue : {(S,4)}
A* Visualized
Vertex Visited Cost Prev
S T 0 NULL
A F Inf NULL
B F Inf NULL
C F Inf NULL
G F Inf NULL
Priority Queue : {}
A* Visualized
Vertex Visited Cost Prev
S T 0 NULL
A F Inf NULL
B F 2 S
C F 2 S
G F Inf NULL
Priority Queue : {(B, 4), (C, 8)}
A* Visualized
Vertex Visited Cost Prev
S T 0 NULL
A F Inf NULL
B T 2 S
C F 2 S
G F Inf NULL
Priority Queue : {(C, 8)}
A* Visualized
Vertex Visited Cost Prev
S T 0 NULL
A F 3 B
B T 2 S
C F 2 S
G F Inf NULL
Priority Queue : {(C, 8), (A, 4)}
A* Visualized
Vertex Visited Cost Prev
S T 0 NULL
A T 3 B
B T 2 S
C F 2 S
G F Inf NULL
Priority Queue : {(C, 8)}
A* Visualized
Vertex Visited Cost Prev
S T 0 NULL
A T 3 B
B T 2 S
C F 2 S
G F 7 A
Priority Queue : {(C, 8), (G, 7)}
A* Visualized
Vertex Visited Cost Prev
S T 0 NULL
A T 3 B
B T 2 S
C F 2 S
G T 7 A
Priority Queue : {(C, 8)}
A* Visualized
Vertex Visited Cost Prev
S T 0 NULL
A T 3 B
B T 2 S
1 2
Practice Question A
● True or false: If A and B are 2-4 trees with the exact same elements, they
must be identical. False! A: insert 1, 2, 3, 4, 5, 6. B: insert 2, 3, 4, 5, 6, 1
1 2 2 3
Practice Question A
● True or false: If A and B are 2-4 trees with the exact same elements, they
must be identical. False! A: insert 1, 2, 3, 4, 5, 6. B: insert 2, 3, 4, 5, 6, 1
1 2 3 2 3 4
Practice Question A
● True or false: If A and B are 2-4 trees with the exact same elements, they
must be identical. False! A: insert 1, 2, 3, 4, 5, 6. B: insert 2, 3, 4, 5, 6, 1
1 2 3 4 2 3 4 5
split! split!
Practice Question A
● True or false: If A and B are 2-4 trees with the exact same elements, they
must be identical. False! A: insert 1, 2, 3, 4, 5, 6. B: insert 2, 3, 4, 5, 6, 1
2 3
1 3 4 2 4 5
Practice Question A
● True or false: If A and B are 2-4 trees with the exact same elements, they
must be identical. False! A: insert 1, 2, 3, 4, 5, 6. B: insert 2, 3, 4, 5, 6, 1
2 3
1 3 4 5 2 4 5 6
Practice Question A
● True or false: If A and B are 2-4 trees with the exact same elements, they
must be identical. False! A: insert 1, 2, 3, 4, 5, 6. B: insert 2, 3, 4, 5, 6, 1
2 3
1 3 4 5 6 1 2 4 5 6
split!
Practice Question A
● True or false: If A and B are 2-4 trees with the exact same elements, they
must be identical. False! A: insert 1, 2, 3, 4, 5, 6. B: insert 2, 3, 4, 5, 6, 1
2 4 3
1 3 5 6 1 2 4 5 6
Practice Question A
● True or false: If A and B are 2-4 trees with the exact same elements, they
must be identical. False! A: insert 1, 2, 3, 4, 5, 6. B: insert 2, 3, 4, 5, 6, 1
2 4 3
1 3 5 6 1 2 4 5 6
● Nodes with a red right subtree and black left subtree need to be rotated
Red Black Tree Insertions
Fixup 2: Rotate linked red nodes into a normal 4-node (temporarily)
Red Black Tree Insertions
Fixup 3: Break up 4-nodes into 3-nodes or 2-nodes by recoloring
Red Black Tree Insertions
Fixup 4: As a result of other fixups, or of insertion into the empty tree, the root
may end up red, so color the root black after the rest of insertion and fixups
are finished
No fixups needed!
Red Black Tree Insertions
● Insert 85 into the Red Black Tree
Red Black Tree Insertions
● Insert 85 into the Red Black Tree
Apply fixup 1!
Apply fixup 2!
Apply fixup 3!
Apply fixup 3!
Apply fixup 1!
3 10
1 8
Practice Question B
● Draw the LLRB that results from adding 9 to the following LLRB:
7 7
3 10 3 10
1 8 1 8
3 10 3 10
1 8 1 9
9 8
Apply fixup 1!
Practice Question B
● Draw the LLRB that results from adding 9 to the following LLRB:
7 7
3 10 3 9
1 9 1 8 10
8
Apply fixup 2!
Practice Question B
● Draw the LLRB that results from adding 9 to the following LLRB:
7 7
3 9 3 9
1 8 10 1 8 10
Apply fixup 3!
Practice Question B
● Draw the LLRB that results from adding 9 to the following LLRB:
7 9
3 9 7 10
1 8 10 3 8
Apply fixup 1!
Balanced Search Structures
● A trie is a data structure that takes advantage of using multiple branches
for each node
● Each node inside a trie corresponds to a possible prefix
Searching
level 3
Balanced Search Structures
● Search for 125
Searching
level 3
Balanced Search Structures
● Search for 125
Searching
level 3
Balanced Search Structures
● Search for 125
Searching
level 2
Balanced Search Structures
● Search for 125
Searching
level 2
Balanced Search Structures
● Search for 125
Searching
level 2
Balanced Search Structures
● Search for 125
Searching
level 1
Balanced Search Structures
● Search for 125
Searching
level 1
Balanced Search Structures
● Search for 125
Searching
level 0
Balanced Search Structures
● Search for 125
Success!
Sorting
Sort Qualities
Stable Sort - A sort that does not change the relative order of equivalent
entries (compared to the input) is called stable.
Runtime: Θ(N2)
32 15 2 17 19 21 41 17 52
Inversions
Inversions - Number of pairs that are out of order.
C B A D F G H E I
1 + 2 + 3 = 6 inversions
Insertion Sort
In-place insertion sort:
Θ(N + Inversions)
Yes
Comparison-based
Heapsort
Algorithm HeapSort(List L):
https://docs.google.com/presentation/d/1z1lCiLSVLKoyUOIFspy1vxyEbe329ntLAVDQP3xj
mnU/pub?start=false&loop=false&delayms=3000&slide=id.g12a2a1b52f_0_1333
Heapsort
Algorithm HeapSort(List L):
https://docs.google.com/presentation/d/1z1lCiLSVLKoyUOIFspy1vxyEbe329ntLAVDQP3xj
mnU/pub?start=false&loop=false&delayms=3000&slide=id.g12a2a1b52f_0_1333
Heapsort Runtime
Runtime for removing an element from the
heap: Θ(logN)
Remove Θ(N) items from the heap
Runtime: Θ(NlogN)
Heapsort
1. Is heapsort stable?
No
Internal sort
Comparison-based
Heap Sort Practice
Suppose we want to sort the array [5, 6, 10, 17, 14, 12, 13] using in-place
heapsort. Give the array after heapification and a single remove-max
operation.
Heap Sort Practice
Suppose we want to sort the array [5, 6, 10, 17, 14, 12, 13] using in-place
heapsort. Give the array after heapification and a single remove-max
operation.
N/8 N/8 ….
Merge Sort
1. Is merge sort stable?
Yes
External
Comparison-based
Quicksort
Algorithm QuickSort(List L):
return L
Else:
https://docs.google.com/presentation/d/1QjAs-zx1i0_XWlLqsKtexb-iueao9jNLkN-gW9QxA
D0/edit#slide=id.g3655bd8207_1_60
Quick Sort
Quick sorting N items:
● Partition on leftmost item.
● Quicksort left half.
● Quicksort right half.
unsorted
Input: 32 15 2 17 19 26 41 17 17
Quick Sort
partition(32)
Quick sorting N items:
● Partition on leftmost item (32).
● Quicksort left half.
● Quicksort right half.
Input: 32 15 2 17 19 26 41 17 17
Quick Sort
partition(32)
Quick sorting N items:
● Partition on leftmost item (32).
● Quicksort left half.
● Quicksort right half.
in its
place
<= 32 >= 32
Input: 15 2 17 19 26 17 17 32 41
Quick Sort
partition(32)
Quick sorting N items:
● Partition on leftmost item (32) (done).
● Quicksort left half.
● Quicksort right half.
in its
place
Input: 15 2 17 19 26 17 17 32 41
Quick Sort
partition(32)
Quick sorting N items:
● Partition on leftmost item (32) (done). partition(15)
partition(17) partition(26)
x x x
partition(17)
in its in its in its in its in its in its in its in its x x
place place place place place place place place
Input: 2 15 17 17 17 19 26 32 41
Quick Sort
partition(32)
Quick sorting N items:
● Partition on leftmost item (32) (done). partition(15)
partition(17) partition(26)
x x x
partition(17)
x x
in its in its in its in its in its in its in its in its in its
place place place place place place place place place
Input: 2 15 17 17 17 19 26 32 41
Quick Sort
1. Is quick sort stable?
No
Depends on implementation
Comparison-based
Quicksort Runtime
datastructur.es
Best Case: Pivot Always Lands in the Middle
datastructur.es
Best Case Runtime?
≈ N/2 + ≈N/2 = ≈N
≈ N/4 * 4 = ≈N
datastructur.es
Worst Case: Pivot Always Lands at Beginning of Array
Give an example of an array that
would follow the pattern to the right.
● 123456
datastructur.es
Quicksort Performance
Theoretical analysis:
● Best case: Θ(N log N)
● Worst case: Θ(N2)
Recall that Θ(N log N) vs. Θ(N2) is a really big deal. So how can Quicksort be the
fastest sort empirically? Because on average it is Θ(N log N).
datastructur.es
Counting Sort
1. Count number of
occurrences of each entry
and allocate space.
2. Walk through the list and
add items to the new
sorted list in the correct
spaces.
Counting Sort Runtime
Total runtime on N keys with alphabet of size R: Θ(N+R)
● Create an array of size R to store counts: Θ(R)
● Counting number of each item: Θ(N)
● Calculating target positions of each item: Θ(R)
● Creating an array of size N to store ordered data: Θ(N)
● Copying items from original array to ordered array: Do N times:
○ Check target position: Θ(1)
○ Update target position: Θ(1)
● Copying items from ordered array back to original array: Θ(N)
Fib Table
0 1
Demo
Fib Table
0 1 2
Demo
Fib Table
0 1 2 3
Demo
Fib Table
0 1 2 3 5
Demo
Fib Table
0 1 2 3 5 8
Practice: Dynamic Programming
Find the length longest increasing subsequence out of a sequence of
numbers:
Ex: Given a list of numbers : [10, 22, 9, 33, 21, 50, 41, 60, 80], the longest
increasing subsequence is [10, 22, 33, 50, 60, 80] and the length is 6
Solution
First, we initialize a list to hold
our intermediate results
(longest subsequence up to
each integer of our list)
A .35
B .17
C .17
D .16 A B C E D
E .15
Huffman Encoding
Example:
Symbol Frequency
A .35
B .17
C .17
D .16
E .15
A B C E D
Huffman Encoding
Example:
Symbol Frequency
A .35
B .17
C .17
D .16
E .15 A B C D E
Huffman Encoding
Example:
Symbol Frequency
A .35
B .17
C .17
D .16
E .15
A B C D E
Practice
Solution
0 1
b
(.38) .62
0 1
e
(.25) .37
0 1
.15 .22
0 1 0 1
.09 c a
f (.06) (.10) (.12)
0 1
.04 d
(.05)
0 1
g h
(.01) (.03)
Pseudorandom Sequences
Pseudorandom Sequences
● Deterministic sequences that satisfy statistical criteria
● Linear congruential method used by Java: