Dijkstra's Algorithm for Single Source Shortest Path Problem, Lecture notes of Algorithms and Programming

Dijkstra's algorithm for solving the Single Source Shortest Path (SSSP) problem in graphs with non-negative edge weights. It reviews relevant definitions such as shortest path and negative weight cycle, and provides the pseudocode for the algorithm. The document also includes a runtime analysis of the algorithm and discusses the concept of greedy algorithms. It is likely related to university topics such as data structures and algorithms, graph theory, and computer science.

Typology: Lecture notes

2021/2022

Uploaded on 05/11/2023

millyx
millyx 🇺🇸

4.7

(9)

249 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CIS 121—Data Structures and Algorithms—Fall 2022
Dijkstra’s—Monday, October 31 / Tuesday, November 1
Readings
Lecture Notes Chapter 20: Dijkstra’s Algorithm
Review: Single Source Shortest Path (SSSP) Problem
In the Single Source Shortest Path (SSSP) Problem, we are given a graph G= (V, E), and we want to find a
shortest path from a given source vertex sVto each vertex vV. From Recitation 6, we proved that BFS
solves this problem for unweighted graphs. However, for weighted graphs, we need something a little more
robust. Thus, Dijkstra’s algorithm is an algorithm that solves this problem for graphs with non-negative
edge weights; some relevant definitions are as follows:
Shortest Path: A path from vertex sto vertex twith the property that no other such path has a lower
total edge weight.
Negative Weight Cycle: A cycle with weights that sum to a negative number.
Review: Dijkstra’s Algorithm
From Recitation 5, we proved that BFS solves the “Single Source Shortest Path” problem for unweighted
graphs. However, for weighted graphs, we need something a little more robust. Dijkstra’s algorithm finds
the shortest path between two given vertices in a weighted graph, assuming that the graph’s edge weights
are non-negative. The running time of the algorithm is O(|E|log |V|+|V|log |V|) when the graph is
implemented using adjacency lists. The pseudocode for the algorithm can be found here.
Runtime Analysis
The running time of Dijkstra’s algorithm has two terms: |E|log |V|and |V|log |V|. We first consider the
|V|log |V|term: each Extract-Min operation takes O(log |V|) time, and this operation is called |V|times
because there are |V|vertices.
The |E|log |V|term has to do with the relaxation step of Dijkstra’s algorithm. Each edge examined may re-
sult in a relaxation of the neighboring node in the heap a Decrease-Key operation that takes O(log |V|)
time. The number of vertices examined in the for loop is bounded by the total degree of all vertices, as each
vertex is added and popped exactly once from the min-heap. This value is 2|E|by the Handshaking Lemma,
so in the worst-case we have 2|E|decrease-key operations, for a total of O(|E|log |V|).
This analysis works for easily proving our runtime, but we can actually do a better analysis. Each edge
(u, v)can only cause one relaxation, not 2 as the Handshaking Lemma suggests. This is because (u, v) is
explored only when node uis popped from the min-heap. This means that when (u, v) is explored from no de
v, we know node uhas already been removed, so its key cannot be decreased. Hence, the O(|E|log |V|) term
comes from the O(log |V|) cost of a Decrease-Key operation, which is called at most |E|times overall.
Greedy Algorithms
A greedy algorithm is an algorithm that always makes the locally optimal choice the best available
choice at that moment—in order to find the best globally optimal solution. Greedy algorithms do not
1
pf2

Partial preview of the text

Download Dijkstra's Algorithm for Single Source Shortest Path Problem and more Lecture notes Algorithms and Programming in PDF only on Docsity!

CIS 121—Data Structures and Algorithms—Fall 2022

Dijkstra’s—Monday, October 31 / Tuesday, November 1

Readings

  • Lecture Notes Chapter 20: Dijkstra’s Algorithm

Review: Single Source Shortest Path (SSSP) Problem

In the Single Source Shortest Path (SSSP) Problem, we are given a graph G = (V, E), and we want to find a shortest path from a given source vertex s ∈ V to each vertex v ∈ V. From Recitation 6, we proved that BFS solves this problem for unweighted graphs. However, for weighted graphs, we need something a little more robust. Thus, Dijkstra’s algorithm is an algorithm that solves this problem for graphs with non-negative edge weights; some relevant definitions are as follows:

Shortest Path: A path from vertex s to vertex t with the property that no other such path has a lower total edge weight.

Negative Weight Cycle: A cycle with weights that sum to a negative number.

Review: Dijkstra’s Algorithm

From Recitation 5, we proved that BFS solves the “Single Source Shortest Path” problem for unweighted graphs. However, for weighted graphs, we need something a little more robust. Dijkstra’s algorithm finds the shortest path between two given vertices in a weighted graph, assuming that the graph’s edge weights are non-negative. The running time of the algorithm is O(|E| log |V | + |V | log |V |) when the graph is implemented using adjacency lists. The pseudocode for the algorithm can be found here.

Runtime Analysis

The running time of Dijkstra’s algorithm has two terms: |E| log |V | and |V | log |V |. We first consider the |V | log |V | term: each Extract-Min operation takes O(log |V |) time, and this operation is called |V | times because there are |V | vertices.

The |E| log |V | term has to do with the relaxation step of Dijkstra’s algorithm. Each edge examined may re- sult in a relaxation of the neighboring node in the heap — a Decrease-Key operation that takes O(log |V |) time. The number of vertices examined in the for loop is bounded by the total degree of all vertices, as each vertex is added and popped exactly once from the min-heap. This value is 2|E| by the Handshaking Lemma, so in the worst-case we have 2|E| decrease-key operations, for a total of O(|E| log |V |).

This analysis works for easily proving our runtime, but we can actually do a better analysis. Each edge (u, v) can only cause one relaxation, not 2 as the Handshaking Lemma suggests. This is because (u, v) is explored only when node u is popped from the min-heap. This means that when (u, v) is explored from node v, we know node u has already been removed, so its key cannot be decreased. Hence, the O(|E| log |V |) term comes from the O(log |V |) cost of a Decrease-Key operation, which is called at most |E| times overall.

Greedy Algorithms

A greedy algorithm is an algorithm that always makes the locally optimal choice — the best available choice at that moment—in order to find the best globally optimal solution. Greedy algorithms do not

always yield optimal solutions, but for many problems they do. Note that Dijkstra’s algorithm solves the “Single Source Shortest Path” problem by following this paradigm — it uses a priority queue structure that always yields the node with the shortest distance from the source node when polled. Consider the set S of vertices in Dijkstra’s whose final shortest-path weights from the source have already been determined. At each step in the algorithm, since we ultimately want to find the shortest path from our source, we use our priority queue to make the locally optimal choice by adding the node with the current shortest distance from the source node to the set S.

Problems

Problem 1

Find the shortest paths from A:

Problem 2: True or False

  1. Provided there are no negative weight cycles, Dijkstra’s algorithm will work with negative edge weights.
  2. Dijkstra’s algorithm will not terminate when run on a graph with negative edge weights.
  3. If we double the weights of all edges, then Dijkstra’s algorithm produces the same shortest path.
  4. If we square the weights of all edges, then Dijkstra’s algorithm produces the same shortest path.

Problem 3

(From CLRS 24.3-6) We are given a directed graph G = (V, E) where each edge (u, v) ∈ E has an associated value r(u, v) ∈ [0, 1] that represents the reliability of a communication channel from u to v. We thus interpret r(u, v) as the probability that the channel from u to v will not fail, and we assume that these probabilities are independent. Design an efficient algorithm to find the most reliable path (lowest failure probability) between a vertex s and any other vertex in the graph.