Greedy algorithm problems [00NR]
- October 3, 2026
Dijkstra's algorithm problems [00NS]
- October 3, 2026
[D1] Dijkstra with negative weight (1pt) [00NT]
- October 3, 2026
Why doesn't Dijkstra's algorithm work if edges in the graph can have negative weights (even if there are no directed cycles)? Give an example of a directed graph with no directed cycles where Dijkstra's algorithm fails to find the minimum-weight path between a pair of vertices. Be sure to demonstrate that you understand why your graph is a counterexample; that is, show what Dijkstra's algorithm does on your example graph, and explain why the path it finds is not the minimum-weight path.
[D2] Dijkstra for longest paths (1pt) [00NU]
- October 3, 2026
What happens if we replace the word “smallest” in Dijkstra’s algorithm with the word “biggest”—that is, we use a max priority queue so we pull out the vertex u with the maximum distance on each iteration, and for each edge (u,v) we update d[v] to be the maximum of d[v] and the sum d[u] + w_{uv}. Can we use this modified Dijkstra’s algorithm to find longest paths bewteen nodes in any graph without directed cycles? Prove that this works, or give a counterexample (with explanation) where it doesn’t.
[K9] Kattis: Dijkstra problem (3 pts) [00OS]
- October 9, 2026
Solve one of the following Kattis problems requiring Dijkstra's Algorithm:
[D3] The Roving Photographer (2 pts) [00OT]
- October 9, 2026
Phoebe the Photographer is just leaving for work at the small-town newspaper where she works as a staff photographer, when she suddenly remembers that she was supposed to take a photograph to decorate page C17. She will just have to take a photograph on the way, but she is worried about getting to work late!
Phoebe's town can be represented by a directed, weighted graph with V vertices and E edges, where the weight on each edge represents the time needed to travel along that street in the given direction. (As usual, two-way streets are represented by one edge in each direction, possibly with different weights.) One particular vertex represents Pheobe's house, and another vertex represents her work. There are various locations (i.e. vertices) at which Pheobe could take a photograph; each such vertex has a number representing the amount of time Pheobe would need to spend taking a photograph there (some photographs can be taken quickly, while others need a while to set up).
Pheobe needs to travel to some location where she can take a photograph, pause long enough to take the photo, and then travel to her workplace. Explain how Pheobe could compute such a path which is as short as possible, and analyze the running time of your algorithm.
Hint.
- October 9, 2026
You may come up with a \Theta (VE\lg V) algorithm, but it is possible to do better: it can be done in \Theta (E \lg V) time.
Hint.
- October 9, 2026
How can you transform the graph so that the problem is solved by a single call to Dijkstra's Algorithm?
Hint.
- October 9, 2026
Consider duplicating each vertex, one to represent visiting that location with no photograph taken, and another to represent visiting that location with a photograph already taken.
Minimum Spanning Tree problems [00OR]
- October 9, 2026
[M1] Kruskal's Algorithm works with repeated weights (3 pts) [00OO]
- October 9, 2026
On an activity, we proved that Kruskal's Algorithm is correct when every edge has a distinct weight. Prove that Kruskal's Algorithm works even when there are edges in the graph with the same weight.
Hint.
- October 9, 2026
Generalize the Cut Property to the situation when there are multiple edges with the same minimum weight crossing a cut. What can we say about the relationship of any MST to such edges?
[M2] Prim's Algorithm is correct (2 pts) [00OQ]
- October 9, 2026
Prove that Prim's Algorithm is correct.
[M3] Min-max spanning trees (3 pts) [00OU]
- October 9, 2026
Let G=(V,E) be an undirected, connected graph with non-negative edge lengths. We say that T is a min-max spanning tree on G if T is a spanning tree whose longest edge is as short as possible. That is, if we imagine taking all the possible spanning trees of G and sorting them by the length of their longest edge, in increasing order, T would be first (or tied for first) on the list. Put yet another way, T is a min-max spanning tree if and only if every other spanning tree T' has some edge which is longer than (or equal to) every edge in T.
Prove or disprove each of the following claims. (Hint: try lots of small examples!)
- If T is a minimum spanning tree for G, then T is a min-max spanning tree for G.
- If T is a min-max spanning tree for G, then T is a minimum spanning tree for G.
[K10] Kattis: MST problem (3 pts) [00P7]
- October 9, 2026
Solve one of the following Kattis problems requiring the computation of a minimum spanning tree:
[M4] Percolation (6 pts) [00P8]
- October 14, 2026
In this implementation challenge you will explore an interesting application of a union-find structure.
Introduction [00P9]
- October 14, 2026
Percolating on 25x25 grid...
. . . X . . . . . . .
. . . . X . . . . . .
. . . . . X X . . . . . . .
. . X X . . . . .
. . . . . . X . . . . . .
. . . . X . . . . . . . . . .
. . . . . . X . . . . . .
. . . . . X . . . . .
. . . . . . X X X . . . X X X . .
. . . . . . . . . . X . X X . X . . .
. . . . . . . X X . . X . . X . .
. . . . . . . . . X X X X . X . .
. . . . . . . . . X
. . . . . . . . . . . . X X .
. . . . . . . . . . . . . X
. . . . . . . X
. . . . . . . . X X .
. . . . . . . . . . X X X X .
. . . . X . . .
. . . . . . . . . . X . .
. . . . . . X . . . .
. . . . . . . . . X . . . . .
. . . . . . . . . X X . .
. . . . . . . . . X . .
. . . . . . . X .
Percolation achieved with 352 cells opened (56.32%)
Shortest path length = 48Imagine an n \times n square grid, where each cell is connected to its four neighbors to the north, east, south, and west. Each cell can be either “open” or “blocked”. Paths through the grid can only travel through open cells. We say the grid percolates if there is a path from some open cell in the top row all the way to some open cell in the bottom row.
Now, carry out the following process: the cells all start out blocked; at each iteration, randomly pick a blocked cell and open it. Keep iterating until the grid percolates. It is a curious fact that on average, the grid will percolate after 59.2% of the cells are opened—no matter what size the grid is. (The larger the grid, the more precise the percentage will be.) Moreover, this percentage is a sort of “phase transition”: if you take a large grid and randomly open fewer than 59.2% of its cells, then it probably will not percolate; open any more than 59.2%, and it probably will.
Requirements [00PA]
- October 14, 2026
Write a program (using a language of your choice) which will explore this percolation model, producing output like the above. That is, your program should carry out the above random cell-opening procedure until the grid percolates, and then display the resulting grid along with the shortest percolating path.
Note that in the given sample output shown above, open cells are displayed as a space, blocked cells as a dot, and open cells through which the shortest percolating path passes as an X. Alternate columns are left blank just to make the grid look relatively square. Your output does not have to look exactly like this.
To receive credit for this problem:
- Your program runs correctly and can produce output like the above.
- Your program runs within a few seconds for grids up to 500 \times 500 in size.
- Your implementation of a generic union-find data structure is in a separate file from the rest of your code (or a separate class within the same file).
Hints [00PB]
- October 14, 2026
- You’ll want to implement your own union-find data structure, as discussed in class. Make a generic union-find data structure which supports the following operations:
new(n: int): create a new union-find structure with n nodes, numbered 0 \dots n-1, with each node conceptually starting out in its own singleton set.find(u: int) -> int: return the “representative” or root node of the set containing node u.union(u: int, v: int): union the sets containing nodes u and v, if they are not already in the same set.connected(u: int, v: int) -> bool: test whether two nodes are already in the same set or not. This can be implemented simply in terms offind.
- If you want to assign a unique node number to each cell in an n \times n grid, it is easiest to number the first row 0 \dots n-1, then the second row n \dots 2n-1, and so on. In general, coordinates (r,c) will be node nr + c. Conversely, node k will correspond to the row and column coordinates (k \mathbin {\mathbf {div}} n, k \mathbin {\mathbf {mod}} n), i.e. the quotient and remainder when dividing k by n.
- We have discussed in class how to find a shortest path between two specific nodes. Since we want to find a path from any node in the top row to any node in the bottom row, try adding a new “virtual” start node connecting to every node in the top row, and a virtual end node connecting to every node in the bottom row. For example, these virtual nodes could be given node numbers n^2 and n^2 + 1 in a union-find structure of size n^2 + 2. This reduces the problem back to finding a path between two specific nodes (the virtual start and end nodes).