Minimum Spanning Trees
Review union-find
(See slides)
Priority Queues
New data structure: Priority Queue
import heapq
priority_queue: list[int] = []
print(priority_queue) # []
heapq.heappush(priority_queue, 3)
heapq.heappush(priority_queue, 1)
heapq.heappush(priority_queue, 4)
heapq.heappush(priority_queue, 2)
print(priority_queue) # [1, 2, 4, 3]
smallest = heapq.heappop(priority_queue) # Remove and return the smallest item
print(smallest) # 1
print(priority_queue) # [2, 3, 4]
if priority_queue: # if it's not empty
smallest = priority_queue[0] # Peek at the smallest item without removing it
print(smallest) # 2
print(priority_queue) # [2, 3, 4]
Poll: What is output?
priority_queue: list[int] = []
heapq.heappush(priority_queue, 5)
heapq.heappush(priority_queue, 3)
heapq.heappush(priority_queue, 6)
print(heapq.heappop(priority_queue))
- 3
- 5
- 6
Minimum Spanning Trees
Poll: A tree is a type of graph. What are the differences between a tree and a graph?
- Each node in a tree must have exactly one parent (unless it is the root)
- Each node in a tree must have at least one child
- Trees are required to have at least one cycle
- Trees are not allowed to have cycles
- Edges in a tree must be weighted
- Edges in a tree cannot be weighted
(See slides)
Kruskal's Algorithm
(See slides)
Poll: How can we get the edge with the smallest weight at each step?
- Store the edges in a Priority Queue, and remove the front item one by one
- Store the edges in a regular list, and search for and remove the smallest item one by one
- Store the edges in a Binary Search Tree (SortedSet) and remove the leftmost node one by one
- Store the edges in a Hash Table and remove the leftmost node one by on
Poll: How can we tell if an edge (between node1 and node2) would cause a cycle?
- Union-find: If the find operation returns the same root for both node1 and node2, then that edge would cause a cycle
- Union-find: If the find operation for node1 returns node2, or vice versa, then that edge would cause a cycle
- Recursive backtracking: starting at node1, search for all nodes that can be reached. If node2 is in that set, then that edge would cause a cycle
(See slides)
Poll: Which of the following are true?
- To iterate through the edges, smallest to largest, we can store them in a Priority Queue
- To determine whether adding an edge would cause a cycle, we can use union-find to see if they are already in the same tree
- We complete Kruskal's Algorithm when each node has at least one edge connected to it in the tree
- The weighted graph edges chosen for the MST are different from the directed parent edges used for union-fin