• Home
  • Textbooks
  • Algorithms
  • Paths in graphs

Algorithms

Sanjoy Dasgupta, Christos Papadimitriou, Umesh Vazirani

Chapter 4

Paths in graphs - all with Video Answers

Educators


Chapter Questions

01:57

Problem 1

Suppose Dijkstra's algorithm is run on the following graph, starting at node $A$.
(a) Draw a table showing the intermediate distance values of all the nodes at each iteration of the algorithm.
(b) Show the final shortest-path tree.

WZ
Wen Zheng
Numerade Educator
01:10

Problem 2

Just like the previous problem, but this time with the Bellman-Ford algorithm.

Clarissa Noh
Clarissa Noh
Numerade Educator
05:12

Problem 3

Squares. Design and analyze an algorithm that takes as input an undirected graph $G=(V, E)$
and determines whether $G$ contains a simple cycle (that is, a cycle which doesn't intersect itself) of length four. Its running time should be at most $O\left(|V|^{3}\right)$ You may assume that the input graph is represented either as an adjacency matrix or with adjacency lists, whichever makes your algorithm simpler.

Carson Merrill
Carson Merrill
Numerade Educator
01:57

Problem 4

Here's a proposal for how to find the length of the shortest cycle in an undirected graph with unit edge lengths.
When a back edge, say $(v, w),$ is encountered during a depth-first search, it forms a cycle with the tree edges from $w$ to $v$. The length of the cycle is level $[v]-$ level $[w]+1$ where the level of a vertex is its distance in the DFS tree from the root vertex. This suggests the following algorithm:
$\bullet$Do a depth-first search, keeping track of the level of each vertex.
Do a depth-first search, keeping track of the level of each vertex.
$\bullet$ Each time a back edge is encountered, compute the cycle length and save it if it is smaller than the shortest one previously seen.
Show that this strategy does not always work by providing a counterexample as well as a brief (one or two sentence) explanation.

WZ
Wen Zheng
Numerade Educator
01:57

Problem 5

Often there are multiple shortest paths between two nodes of a graph. Give a linear-time algorithm for the following task.
Input: Undirected graph $G=(V, E)$ with unit edge lengths; nodes $u, v \in V$ Output: The number of distinct shortest paths from $u$ to $v$

WZ
Wen Zheng
Numerade Educator
09:12

Problem 6

Prove that for the array prev computed by Dijkstra's algorithm, the edges $\{u, \text { prev }[u]\}$ (for all $u \in V)$ form a tree.

Chris Trentman
Chris Trentman
Numerade Educator
00:36

Problem 7

You are given a directed graph $G=(V, E)$ with (possibly negative) weighted edges, along with a specific node $s \in V$ and a tree $T=\left(V, E^{\prime}\right), E^{\prime} \subseteq E .$ Give an algorithm that checks whether $T$ is a shortest-path tree for $G$ with starting point $s$. Your algorithm should run in linear time.

WZ
Wen Zheng
Numerade Educator
00:42

Problem 8

Professor $\mathrm{F}$. Lake suggests the following algorithm for finding the shortest path from node $s$ to node $t$ in a directed graph with some negative edges: add a large constant to each edge weight so that all the weights become positive, then run Dijkstra's algorithm starting at node $s,$ and return the shortest path found to node $t$
Is this a valid method? Either prove that it works correctly, or give a counterexample.

WZ
Wen Zheng
Numerade Educator
00:42

Problem 9

Consider a directed graph in which the only negative edges are those that leave $s ;$ all other edges are positive. Can Dijkstra's algorithm, started at $s$, fail on such a graph? Prove your answer.

WZ
Wen Zheng
Numerade Educator
00:36

Problem 10

You are given a directed graph with (possibly negative) weighted edges, in which the shortest path between any two vertices is guaranteed to have at most $k$ edges. Give an algorithm that finds the shortest path between two vertices $u$ and $v$ in $O(k|E|)$ time.

WZ
Wen Zheng
Numerade Educator
01:57

Problem 11

Give an algorithm that takes as input a directed graph with positive edge lengths, and returns the length of the shortest cycle in the graph (if the graph is acyclic, it should say so). Your algorithm should take time at most $O\left(|V|^{3}\right)$

WZ
Wen Zheng
Numerade Educator
04:35

Problem 12

Give an $O\left(|V|^{2}\right)$ algorithm for the following task.
Input: An undirected graph $G=(V, E) ;$ edge lengths $l_{e}>0 ;$ an edge $e \in E$
Output: The length of the shortest cycle containing edge $e$

Gabrielle Schiavone
Gabrielle Schiavone
Numerade Educator
03:28

Problem 13

You are given a set of cities, along with the pattern of highways between them, in the form of an undirected graph $G=(V, E) .$ Each stretch of highway $e \in E$ connects two of the cities, and you know its length in miles, $l_{e} .$ You want to get from city $s$ to city $t .$ There's one problem: your car can only hold enough gas to cover $L$ miles. There are gas stations in each city, but not between cities. Therefore, you can only take a route if every one of its edges has length $l_{e} \leq L$
(a) Given the limitation on your car's fuel tank capacity, show how to determine in linear time whether there is a feasible route from $s$ to $t$
(b) You are now planning to buy a new car, and you want to know the minimum fuel tank capacity that is needed to travel from $s$ to $t .$ Give an $O((|V|+|E|) \log |V|)$ algorithm to determine this.

Stanley Enemuo
Stanley Enemuo
Numerade Educator
00:36

Problem 14

You are given a strongly connected directed graph $G=(V, E)$ with positive edge weights along with a particular node $v_{0} \in V$. Give an efficient algorithm for finding shortest paths between all pairs of nodes, with the one restriction that these paths must all pass through $v_{0}$

WZ
Wen Zheng
Numerade Educator
00:36

Problem 15

Shortest paths are not always unique: sometimes there are two or more different paths with the minimum possible length. Show how to solve the following problem in $O((|V|+|E|) \log |V|)$ time.

WZ
Wen Zheng
Numerade Educator