Skip to content
Snippets Groups Projects
Commit 9506c18a authored by Kevin Lin's avatar Kevin Lin
Browse files

Add and link to Shortest Paths module slides

parent daf06219
No related branches found
No related tags found
No related merge requests found
Pipeline #527998 passed with stages
in 46 seconds
......@@ -9,6 +9,7 @@ title: Shortest Paths
2/22
: [**Shortest Paths Trees**]({{ site.baseurl }}{% link lessons/graph-algorithms.md %}#shortest-paths-trees)
: [Slides]({{ site.baseurl }}{% post_url 2023-02-22-shortest-paths-trees %})
: {% include learning_objectives.md lesson="Shortest Paths Trees" %}
: **Prj**{: .label .label-yellow }[**Shortest Paths**]({{ site.baseurl }}{% link projects/shortest-paths.md %})
......@@ -17,6 +18,7 @@ title: Shortest Paths
2/24
: [**Dynamic Programming**]({{ site.baseurl }}{% link lessons/graph-algorithms.md %}#dynamic-programming)
: [Slides]({{ site.baseurl }}{% post_url 2023-02-24-dynamic-programming %})
: {% include learning_objectives.md lesson="Dynamic Programming" %}
: **Asm**{: .label .label-green }[**Assessment 3**]({{ site.baseurl }}{% link assessments/3.md %})
......@@ -24,6 +26,7 @@ title: Shortest Paths
2/27
: [**Disjoint Sets**]({{ site.baseurl }}{% link lessons/graph-algorithms.md %}#disjoint-sets)
: [Slides]({{ site.baseurl }}{% post_url 2023-02-27-disjoint-sets %})
: {% include learning_objectives.md lesson="Disjoint Sets" %}
3/1
......
---
title: &title Shortest Paths Trees
description: *title
summary: *title
redirect_to: &redirect https://docs.google.com/presentation/d/1d5PlGbPhxL2oORaUsbVM5P2JgEbhiDvp2kSrEgUuULU/edit?usp=sharing
canonical_url: *redirect
---
Dijkstra’s order
Let’s find a weighted shortest paths tree (SPT) starting from A.
What is the cost of the shortest path from A to G, i.e. distTo.get(G)?
Give the order in which Dijkstra's algorithm removes vertices from the perimeter.
1
A
B
C
F
E
H
D
G
5
1
6
4
4
4
5
2
2
3
Negative edge weights
Using the previous graph, let’s think about how negative weights affects Dijkstra’s.
Which edge, if replaced with the weight -100, would cause Dijkstra’s algorithm to compute an incorrect shortest paths tree from A?
Explain in your own words why Dijkstra’s algorithm might not work if there are negative edge weights in the graph.
True or false: If the only negative-weight edges in a directed graph are outgoing edges from the start node, then Dijkstra's algorithm will compute a correct shortest paths tree. Assume that the start node is not part of a cycle.
2
ToposortDAGSolver
Change one edge weight to make Dijkstra’s fail to find a valid shortest paths tree.
Give a topological sorting for the above graph. (Many possible answers!)
Why does ToposortDAGSolver find a correct SPT in DAGs with negative weights?
3
A
B
C
F
E
H
D
G
5
1
6
4
4
4
5
2
2
3
Dijkstra’s runtime
Give the worst-case number of calls to add, removeMin, and changePriority.
perimeter.add(start, 0.0);
while (!perimeter.isEmpty()) {
Vertex from = perimeter.removeMin();
for (Edge edge : graph.neighbors(from)) {
Vertex to = edge.to;
double oldDist = distTo.getOrDefault(to, ∞);
double newDist = distTo.get(from) + edge.weight;
if (newDist < oldDist) {
edgeTo.put(to, edge);
distTo.put(to, newDist);
if (perimeter.contains(to)) {
perimeter.changePriority(to, newDist);
} else {
perimeter.add(to, newDist);
} } } }
4
A* search
perimeter.add(start, 0.0);
while (!perimeter.isEmpty()) {
Vertex from = perimeter.removeMin();
for (Edge edge : graph.neighbors(from)) {
Vertex to = edge.to;
double oldDist = distTo.getOrDefault(to, ∞);
double newDist = distTo.get(from) + edge.weight;
if (newDist < oldDist) {
edgeTo.put(to, edge);
distTo.put(to, newDist);
double priority = newDist + graph.est(to, goal);
if (perimeter.contains(to)) {
perimeter.changePriority(to, priority);
} else {
perimeter.add(to, priority);
} } } }
5
Bellman–Ford algorithm
Negative-weight shortcuts are no problem for the Bellman–Ford algorithm.
for (int i = 1; i < graph.vertices().size(); i += 1) {
for (Vertex from : graph.vertices()) {
for (Edge edge : graph.neighbors(from)) {
Vertex to = edge.to;
double oldDist = distTo.getOrDefault(to, ∞);
double newDist = distTo.get(from) + edge.weight;
if (newDist < oldDist) {
edgeTo.put(to, edge);
distTo.put(to, newDist);
}
}
}
}
Our graphs lack the vertices() affordance so Bellman–Ford is very slow.
6
---
title: &title Dynamic Programming
description: *title
summary: *title
redirect_to: &redirect https://docs.google.com/presentation/d/1YYRECF9h2HLY40XQzLZqQpbPMx4N594JnQDBMBHnaec/edit?usp=sharing
canonical_url: *redirect
---
Reductions
The following sentences incorrectly apply the idea of reduction. Explain each issue.
Seam finding reduces to Dijkstra's algorithm.
Topological sorting reduces to depth-first search.
Topological sorting reduces to graph traversal.
1
Dynamic programming
Can merge sort be written as a dynamic programming algorithm? Why or why not?
2
Andrea Muljono.
Robot pathfinding
Let’s help a robot find its way on an w x h grid. The robot starts at the top-left corner (0, 0) and wants to go to the bottom-right corner (w - 1, h - 1). The robot can only take one step down or one step right at a time.
How many unique paths can the robot take to reach the bottom-right corner?
How can we express this as a recursive problem?
3
h
w
Andrea Muljono.
numUniquePaths
Fill in the assignment statements to complete the dynamic programming solution.
public static int numUniquePaths(int w, int h) {
int[][] grid = new int[w][h];
for (int x = 0; x < w; x += 1) {
grid[x][0] =
}
for (int y = 0; y < h; y += 1) {
grid[0][y] =
}
for (int x = 1; x < w; x += 1) {
for (int y = 1; y < h; y += 1) {
grid[ ][ ] =
} }
return grid[x - 1][y - 1];
}
4
Andrea Muljono.
---
title: &title Disjoint Sets
description: *title
summary: *title
redirect_to: &redirect https://docs.google.com/presentation/d/1snljlDSDj1pXerjG7B3DdWO-chS_w0Dl_CxGGqrVSCs/edit?usp=sharing
canonical_url: *redirect
---
Kruskal’s algorithm
Give the order in which Kruskal’s algorithm selects edges.
After considering the edge with weight 2, circle the connected components above.
1
d
a
b
c
e
f
9
1
2
8
3
6
5
7
4
Kruskal’s runtime
Prim’s algorithm is in Θ(|E| log |V|) time overall.
Fill in the blanks with the greatest big-O bounds for isConnected and connect if we know that Kruskal’s algorithm is in Θ(|E| log |E|) time overall.
Merge sort the list of edges in the graph. Θ(|E| log |E|)
While the size of the MST < |V| - 1…
Determine if isConnected(e.from, e.to). ________________
If false, connect(e.from, e.to) and add e. ________________
Then, fill in the table with the worst-case runtime for each implementation.
2
Disjoint Sets
find runtime
union runtime
Quick Find (QF)
Quick Union (QU)
Weighted QU
Path compression
Path compression reassigns the parent of each node visited by find to the root.
Analogous to balancing a search tree by rotations or color flips. Θ(𝛂(N)) amortized.
3
15
11
5
12
13
6
1
7
14
8
2
9
10
3
0
4
Bounded weights
We are trying to find the minimum spanning tree of a graph where edge weights are double (decimal) values bounded between 0 and 255. Is it possible to find an MST in runtime faster than Θ(|E| log |E|)? If so, explain how. Else, explain why not.
4
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment