COMP3010: Algorithm Theory and Design

Daniel Sutantyo, Department of Computing, Macquarie University

8.1 - Single-Source Shortest Path

Intro

8.1 - Single-Source Shortest Path

  • Single-Source Shortest Path (SSSP) is the problem of finding the shortest path from one vertex to every other vertices in the graph.
  • You have done this in COMP225/COMP2010 using Dijkstra's Algorithm
    • and I expect you to already know it, so please revise the topic if you need to refresh your memory (we're going to mention it a bit later)

Single-Source Shortest Path

8.1 - Single-Source Shortest Path

  • Some food for thought:
    • do you think it would be more efficient if we find all the shortest paths from every vertex to every other vertex?
      • this is what we call "all-pairs shortest path", and we'll do this in the next lecture
    • do you think it's faster to find just the shortest path from just one vertex to another vertex?

Notation

8.1 - Single-Source Shortest Path

  • Let \(G = \langle V,E \rangle\) be a weighted, directed graph where:
    • \(V\) is the set of vertices in \(G\), and
    • \(E\) is the set of edges in \(G\)
  • Let \(w(u,v)\) be the weight of the edge connecting vertex \(u\) and vertex \(v\)
  • Let \(p_{uv} = \{x_1 = u, x_2, x_3, \dots, x_{k-1}, x_k = v\}\), \(x_i \in V\), be the shortest path between vertex \(u\) and vertex \(v\)
  • The main focus is to find the weight of the path, i.e. the sum of the weights of the edges in the path, and not the path itself

Problem Decomposition 

8.1 - Single-Source Shortest Path

ShortestPath(\(u\),\(v\))

ShortestPath(\(y_1\),\(v\))

ShortestPath(\(y_2\),\(v\))

ShortestPath(\(y_\ell\),\(v\))

\(\dots\)

where the vertices \(y_i\), \(1 \le i \le \ell\), are all the other vertices in the graph

  • Note that we're using \(\{x_1 = u, x_2, \dots, x_{k-1},x_k = v\}\) to represent the shortest path, so for example, the shortest path can be \(\{u,y_1, y_4,y_ 7, v\}\)

Problem Decomposition 

8.1 - Single-Source Shortest Path

ShortestPath(\(u\),\(v\))

\(w(u,y_1)\) + ShortestPath(\(y_1\),\(v\))

\(w(u,v)\) 

\(\dots\)

\[\text{ShortestPath}(u,v) = \begin{cases} \displaystyle \min\left(w(u,v),\min_{1 \le i \le \ell} \left(w(u,y_i) + \text{ShortestPath}(y_i,v)\right)\right) & \text{if $u \ne v$} \\ 0 &\text{if $u = v$}  \end{cases}\]

\(w(u,y_2)\) +ShortestPath(\(y_2\),\(v\))

\(w(u,y_\ell)\) +ShortestPath(\(y_\ell\),\(v\))

Optimal Substructure

8.1 - Single-Source Shortest Path

ShortestPath(\(u\),\(v\))

\(w(u,y_1)\) + ShortestPath(\(y_1\),\(v\))

\(w(u,v)\) 

\(\dots\)

\[\text{ShortestPath}(u,v) = \begin{cases} \displaystyle \min\left(w(u,v),\min_{1 \le i \le \ell} \left(w(u,y_i) + \text{ShortestPath}(y_i,v)\right)\right) & \text{if $u \ne v$} \\ 0 &\text{if $u = v$}  \end{cases}\]

\(w(u,y_2)\) +ShortestPath(\(y_2\),\(v\))

\(w(u,y_\ell)\) +ShortestPath(\(y_\ell\),\(v\))

Optimal Substructure

8.1 - Single-Source Shortest Path

ShortestPath(\(u\),\(v\))

\(w(u,y_1)\) + ShortestPath(\(y_1\),\(v\))

\(w(u,v)\) 

\(\dots\)

\(w(u,y_2)\) +ShortestPath(\(y_2\),\(v\))

\(w(u,y_\ell)\) +ShortestPath(\(y_\ell\),\(v\))

  • Let \(p_{uv} = \{x_1 = u, x_2, \dots, x_{k-1},x_k = v\}\) be the shortest path from \(u\) to \(v\).
    • let's suppose that \(x_2 = y_m\), so the path given by ShortestPath\((y_m,v)\) is in \(p_{uv}\)
    • then ShortestPath\((y_m,v)\) must return the shortest path from \(y_m\) to \(v\), because if it is not, then we can find a shorter path from \(y_m\) to \(v\) and add \(w(u,y_m\) to it, to have a shorter path than \(p_{uv}\), a contradiction!

Optimal Substructure

8.1 - Single-Source Shortest Path

  • CLRS page 645 is a bit more general:
  • If \(p_{uv} = \{x_1 = u, x_2, \dots, x_i, \dots, x_j, \dots, x_{k-1},x_k = v\}\) is the shortest path from \(u\) to \(v\) and let \(p_{ij}\) be the subpath from \(x_i\) to \(x_j\), \(1 < i < j < k\), then \(p_{ij}\) is the shortest path from \(x_i\) to \(x_j\) 
    • because again, if it's not, then we can find a shorter path between \(x_i\) and \(x_j\), then replace the path we have in \(p_{uv}\) and end up with a shorter path between \(u\) and \(v\), which is a contradiction!

\(u\)

\(v\)

\(x_i\)

\(x_j\)

\(\cdots\)

\(\cdots\)

\(\cdots\)

\(\cdots\)

Dijkstra's Algorithm

8.1 - Single-Source Shortest Path

  • In COMP225/2010, you encountered Dijkstra's Algorithm for finding the shortest path from one vertex to all the other vertices
  • The algorithm is quite simple:
    • Starting from vertex \(u\), we find the distance to all other vertices

5

2

1

4

3

4

8

3

1

2

5

9

1 2 3 4 5
0 4 inf inf 8

Dijkstra's Algorithm

8.1 - Single-Source Shortest Path

5

2

1

4

3

4

8

3

1

2

5

9

1 2 3 4 5
0 4 inf inf 8
  • The algorithm is quite simple:
    • Starting from vertex \(u\), we find the distance to all other vertices
    • We then choose the nearest vertex \(v\), put that in a set of visited vertices \(S\), and look at all edges leaving \(v\) to see if we can improve the shortest path from \(u\) to all other vertices

\(S = \{1,2\}\)

Dijkstra's Algorithm

8.1 - Single-Source Shortest Path

5

2

1

4

3

4

8

3

1

2

5

9

1 2 3 4 5
0 4 inf 13 7
  • The algorithm is quite simple:
    • Starting from vertex \(u\), we find the distance to all other vertices
    • We then choose the nearest vertex \(v\), put that in a set of visited vertices \(S\), and look at all edges leaving \(v\) to see if we can improve the shortest path from \(u\) to all other vertices

\(S = \{1,2\}\)

Dijkstra's Algorithm

8.1 - Single-Source Shortest Path

5

2

1

4

3

4

8

3

1

2

5

9

1 2 3 4 5
0 4 inf 8 7
  • The algorithm is quite simple:
    • Starting from vertex \(u\), we find the distance to all other vertices
    • We then choose the nearest vertex \(v\), put that in a set of visited vertices \(S\), and look at all edges leaving \(v\) to see if we can improve the shortest path from \(u\) to all other vertices

\(S = \{1,2,5\}\)

Dijkstra's Algorithm

8.1 - Single-Source Shortest Path

5

2

1

4

3

4

8

3

1

2

5

9

1 2 3 4 5
0 4 13 8 7
  • The algorithm is quite simple:
    • Starting from vertex \(u\), we find the distance to all other vertices
    • We then choose the nearest vertex \(v\), put that in a set of visited vertices \(S\), and look at all edges leaving \(v\) to see if we can improve the shortest path from \(u\) to all other vertices

\(S = \{1,2,5,4\}\)

Dijkstra's Algorithm

8.1 - Single-Source Shortest Path

  • You can find the proof of correctness of Dijkstra's algorithm in CLRS page 659-661
    • the correctness hinges on the loop invariant: the weight of the paths to the vertices in \(S\) is the shortest paths to those vertices
    • it is not examinable
  • The complexity of a naive implementation is \(O(V^2+E)\)
    • we have to visit all vertices and finding the next vertex to visit is \(O(V)\), and we also need to consider every edge
    • if you can find the next vertex faster, then you can push this down to \(O(V\log V + E)\)

Negative Weights

8.1 - Single-Source Shortest Path

  • Can we find the shortest path of a graph that contains negative weights?

5

2

1

4

3

4

2

-5

6

7

-5

9

Negative Weights

8.1 - Single-Source Shortest Path

  • Can we find the shortest path of a graph that contains negative weights?
1 2 3 4 5
0 4 inf inf 2

\(S = \{1,5\}\)

5

2

1

4

3

4

2

-5

6

7

-5

9

Negative Weights

8.1 - Single-Source Shortest Path

  • Can we find the shortest path of a graph that contains negative weights?
1 2 3 4 5
0 4 inf 8 2

\(S = \{1,5\}\)

5

2

1

4

3

4

2

-5

6

7

-5

9

Negative Weights

8.1 - Single-Source Shortest Path

  • Can we find the shortest path of a graph that contains negative weights?
1 2 3 4 5
0 4 inf 8 2

\(S = \{1,5,2\}\)

5

2

1

4

3

4

2

-5

6

7

-5

9

Negative Weights

8.1 - Single-Source Shortest Path

  • Can we find the shortest path of a graph that contains negative weights?
1 2 3 4 5
0 4 3 8 2

\(S = \{1,5,2,4\}\)

5

2

1

4

3

4

2

-5

6

7

-5

9

Negative Weights

8.1 - Single-Source Shortest Path

  • Can we find the shortest path of a graph that contains negative weights?
1 2 3 4 5
0 4 0 5 -1

5

2

1

4

3

4

2

-5

6

7

-5

9

1 2 3 4 5
0 4 3 8 2

\(S = \{1,5,2,4\}\)

Negative Weights

8.1 - Single-Source Shortest Path

  • Can we find the shortest path of a graph that contains negative weights?
1 2 3 4 5
0 ? ? ? ?

5

2

1

4

3

4

2

-5

-6

7

-5

9

Negative Weights

8.1 - Single-Source Shortest Path

  • Can we find the shortest path of a graph that contains negative weights?
1 2 3 4 5
0 -5 ? ? ?

5

2

1

4

3

4

2

-5

-6

7

-5

9

Negative Weights

8.1 - Single-Source Shortest Path

  • If the graph contains negative weights, then we can still find the shortest path from one vertex to every other vertices provided that there are no negative-weight cycles
    • because then the shortest-path won't be well-defined, as we can always find a path that is shorter by repeating the cycle

Cycles in the Shortest Path

  • Can the shortest path contains a cycle?

8.1 - Single-Source Shortest Path

\(u\)

\(v\)

\(x_i\)

\(\cdots\)

\(\cdots\)

\(\cdots\)

\(\cdots\)

\(\cdots\)

  • if the weight of the cycle is positive, then we can remove it and end up with a shorter path
  • if the weight of the cycle is negative, then the shortest path is not well-defined
  • if the weight of the cycle is 0, then removing if gives us a path with the same weight
  • so the shortest path should not contain any cycle

Relaxation

  • In finding the shortest path, we often start with an upper bound for the distances between one vertex to another
    • in Dijkstra's we assume the distance to every vertex is infinity at the start
  • In subsequent steps, we include more edges to try and reduce this upper bound
  • This process is called relaxation: relaxing an edge means that we are using that edge to see if we can find a shorter path

8.1 - Single-Source Shortest Path

Relaxation

  • More formally, let \(d(u,m)\) be the distance between \(u\) and \(m\) and \(d(u,n)\) be the distance between \(u\) and \(n\)
  • If \(d(u,m) > d(u,n) + w(n,m)\), then we can relax the edge \((n,m)\) to improve the shortest path from \(u\) to \(m\)

8.1 - Single-Source Shortest Path

m

u

4

10

1

n

  • in the above example, initially \(d(u,m) = 10\) and \(d(u,n) = 4\), then we relax the edge \((n,m\)), to improve \(d(u,m)\) to \(5\) 

End

  • In this lecture, we're just going through the basics of finding the shortest path in a graph
  • In the next lectures, we're going to see more algorithms for finding the shortest path in a graph, but make sure you understand the concepts that we've discussed here first

8.1 - Single-Source Shortest Path