This repository has been archived on 2022-06-22. You can view files and clone it, but cannot push or open issues or pull requests.
usaco-guide/content/5_Gold/SP.mdx

112 lines
3.9 KiB
Text
Raw Normal View History

2020-06-25 01:38:05 +00:00
---
id: sp
title: "Shortest Paths with Non-Negative Edge Weights"
author: Benjamin Qi
prerequisites:
- Gold - Breadth First Search
description: Introduces Dijkstra's Algorithm for a single source as well as Floyd-Warshall for All-Pairs Shortest Path.
2020-06-26 18:00:32 +00:00
frequency: 3
2020-06-25 01:38:05 +00:00
---
import { Problem } from "../models";
export const metadata = {
problems: {
sample: [
new Problem("CSES", "Shortest Routes I", "1671", "Easy", false, ["SP"], "equivalent to [Kattis SSSP Non-Negative](https://open.kattis.com/problems/shortestpath1)"),
],
dijk: [
new Problem("CSES", "Flight Discount", "1195", "Easy", false, ["SP"], "one edge modified"),
new Problem("CSES", "Flight Routes", "1196", "Easy", false, ["SP"], "$k$ smallest paths"),
new Problem("CSES", "Investigation", "1202", "Easy", false, ["SP"], ""),
new Problem("Gold", "Milk Pumping", "969", "Easy", false, ["SP"], ""),
new Problem("Gold", "Visit FJ", "717", "Easy", false, ["SP"], ""),
new Problem("Gold", "Shortcut", "899", "Easy", false, ["SP"], ""),
new Problem("Gold", "Fine Dining", "861", "Easy", false, ["SP"], ""),
new Problem("Kattis", "Robot Turtles", "robotturtles", "Easy", false, ["SP"], ""),
new Problem("Kat", "Lane Switching", "https://open.kattis.com/contests/acpc17open/problems/laneswitching", "Normal", false, ["SP"], ""),
],
apspSam: [
new Problem("CSES", "Shortest Routes II", "1672", "Easy", false, ["APSP"], ""),
],
apsp: [
2020-07-03 20:29:32 +00:00
new Problem("Gold", "Moortal Cowmbat", "971", "Hard", false, ["APSP", "DP"], ""),
2020-06-25 01:38:05 +00:00
],
}
};
2020-07-06 18:14:41 +00:00
## Single-Source Shortest Path
2020-06-25 01:38:05 +00:00
2020-07-06 18:14:41 +00:00
<problems-list problems={metadata.problems.sample} />
2020-06-25 01:38:05 +00:00
2020-07-06 18:14:41 +00:00
### Tutorial
2020-07-03 20:29:32 +00:00
Use *Dijkstra's Algorithm*.
2020-06-25 01:38:05 +00:00
2020-06-27 03:07:31 +00:00
<resources>
<resource source="CPH" title="13.2 - Dijkstra" starred>code</resource>
<resource source="cp-algo" title="Dijkstra (Dense Graphs)" url="graph/dijkstra_dense.html"></resource>
<resource source="cp-algo" title="Dijkstra (Sparse Graphs)" url="graph/dijkstra_sparse.html"></resource>
<resource source="CPC" title="8 - Graphs 2" url="08_graphs_2"></resource>
</resources>
2020-06-25 01:38:05 +00:00
2020-06-26 18:00:32 +00:00
2020-07-06 18:14:41 +00:00
### Implementation
2020-06-26 18:00:32 +00:00
2020-07-06 18:14:41 +00:00
#### Part 1: $O(N^2)$
2020-07-03 20:29:32 +00:00
The USACO training pages present a $O(N^2)$ version, although this is rarely used nowadays. (but describe anyways?)
2020-07-06 18:14:41 +00:00
#### Part 2: $O(M\log N)$
2020-06-26 18:00:32 +00:00
2020-07-03 20:29:32 +00:00
<resources>
<resource source="Benq" title="Dijkstra" url="https://github.com/bqi343/USACO/blob/master/Implementations/content/graphs%20(12)/Basics/Dijkstra%20(7.3).h"> </resource>
</resources>
(comments in code?)
<optional-content title="Faster Dijkstra">
2020-06-26 18:00:32 +00:00
2020-07-03 20:29:32 +00:00
Can be done in $O(M+N\log N)$ with [Fibonacci heap](https://en.wikipedia.org/wiki/Fibonacci_heap).
2020-06-26 18:00:32 +00:00
</optional-content>
2020-07-06 18:14:41 +00:00
### Problems
2020-06-25 01:38:05 +00:00
<problems-list problems={metadata.problems.dijk} />
## All Pairs Shortest Path (APSP)
<problems-list problems={metadata.problems.apspSam} />
Use the *Floyd-Warshall* algorithm.
2020-06-27 03:07:31 +00:00
### Tutorial
2020-06-25 01:38:05 +00:00
2020-06-27 03:07:31 +00:00
<resources>
<resource source="CPH" title="13.3 - Floyd-Warshall" starred>example calculation, code</resource>
<resource source="PAPS" title="12.3.3"></resource>
<resource source="cp-algo" title="Floyd-Warshall" url="graph/all-pair-shortest-path-floyd-warshall.html"></resource>
</resources>
2020-06-25 01:38:05 +00:00
2020-06-27 03:07:31 +00:00
<optional-content title="Incorrect Floyd-Warshall">
2020-06-25 01:38:05 +00:00
2020-06-27 03:07:31 +00:00
[Paper](https://arxiv.org/pdf/1904.01210.pdf)
2020-06-25 01:38:05 +00:00
2020-06-27 03:07:31 +00:00
> A common mistake in implementing the FloydWarshall algorithm is to
misorder the triply nested loops (The correct order is `KIJ`). The incorrect
`IJK` and `IKJ` algorithms do not give correct solutions for some instance. However, we can prove that if these are repeated three times, we obtain the correct solutions.
>
> It would be emphasized that these fixes (repeating incorrect algorithms three
times) have the same time complexity as the correct FloydWarshall algorithm
up to constant factors. Therefore, our results suggest that, if one is confused by
the order of the triply nested loops, one can repeat the procedure three times
just to be safe.
2020-06-25 01:38:05 +00:00
2020-06-27 03:07:31 +00:00
</optional-content>
### Problems
2020-06-25 01:38:05 +00:00
2020-06-28 16:03:46 +00:00
<problems-list problems={metadata.problems.apsp} />
(more?)