DSC 40B

Quiz 08

Practice problems for topics on Quiz 08.

Tags in this problem set:

Problem #298

Tags: shortest paths

True or False: It is possible for two arbitrary nodes to have a negative shortest path distance between them without the presence of a negative cycle in the graph.

Solution

True.

For example, if there is a single edge \((u, v)\) with weight \(-1\) and no other edges, the shortest path distance from \(u\) to \(v\) is \(-1\), and there is no cycle at all.

Problem #299

Tags: bellman-ford

Suppose the Bellman-Ford algorithm is run on a graph \(G\) that does not contain negative cycles. Let \(u\) be an arbitrary node in the graph, and assume that \(u\) is reachable from the source.

True or False: \(u\)'s estimated distance can change multiple times during the algorithm's execution.

Solution

True.

Suppose there are edges \((s, u)\) with weight 10, \((s, a)\) with weight 1, and \((a, u)\) with weight 1. If \((s, u)\) is updated first, \(u\)'s estimate drops from \(\infty\) to 10. It drops again to 2 once \((s, a)\) and then \((a, u)\) are updated.

Problem #300

Tags: bellman-ford

Recall that Bellman-Ford with early stopping updates edges iteratively until no estimated distances change, at which point the algorithm terminates early. True or False: the number of iterations needed before early termination may depend on the order in which the edges are updated.

Solution

True.

Consider the path graph \(s \to a \to b \to c\). If the edges are updated in order along the path, every distance is correct after one iteration, and the second iteration changes nothing. If they are updated in reverse order, each iteration only fixes one more node, so it takes all three iterations. (With \(|V| = 4\), the outer loop runs at most \(|V| - 1 = 3\) times.)

Problem #301

Tags: bellman-ford

Suppose you have a directed graph with 8 nodes and 7 edges. If you were to run the Bellman-Ford algorithm on this graph with early stopping implemented, what is the minimum number of times the outer for loop could possibly run? You should assume that all 8 nodes are reachable from the source node.

Solution

Answer: 2.

In the best situation, the edges would be updated in just the right order so that all of the correct shortest paths are found on the first iteration. On the second iteration, B-F will detect that nothing changed, and it will terminate.

Problem #302

Tags: bellman-ford

Recall that the outer loop of the Bellman-Ford algorithm without early stopping loops for a fixed number of iterations, while the inner loop iterates over each edge of the graph.

Suppose Bellman-Ford with early stopping is run on the graph below using node \(s\) as the source:

What is the fewest number of iterations of the outer loop that can possibly be performed?

Solution

2.

If the edges happen to come in the order of the shortest paths (e.g., \((s, u_3)\), \((u_3, u_4)\), \((u_4, u_1)\), \((u_3, u_5)\), \((u_5, u_2)\) and \((u_5, u_6)\) first), one iteration gets every estimate right. But the algorithm only stops after an iteration that changes nothing, so it needs a second iteration to see that. It can't stop after one, since the first iteration always changes the estimates of \(s\)'s neighbors.

Problem #303

Tags: bellman-ford

Suppose Bellman-Ford with early stopping is run on the weighted graph below whose edge weights are unknown to us (but known to the algorithm). The algorithm uses node \(a\) as the source node.

How many iterations of the outer loop will be run in the worst case? Your answer should be a number.

Solution

4.

No path in this graph has more than 3 edges, so after 3 iterations every estimate is correct, whatever the weights are. In the worst case, a shortest path has 3 edges (e.g., \(a \to b \to d \to f\)) and the edges come in an unlucky order, so that all 3 iterations are needed, and then a 4th iteration is needed to see that nothing changes.

Problem #304 🌶️

Tags: bellman-ford

Suppose the Bellman-Ford algorithm with early stopping is run on the weighted graph shown below using node \(s\) as the source (the edge weights are intentionally not shown). In the worst case, how many iterations of Bellman-Ford will be performed? That is, how many iterations of the outer for-loop will occur?

Solution

7.

After \(k\) iterations, Bellman-Ford has the correct distance to every node whose shortest path from \(s\) uses at most \(k\) edges. With early stopping, it stops after the first iteration in which no estimate changes. So if every shortest path has at most \(k\) edges, the estimates are all correct after \(k\) iterations, and one more iteration confirms it, for \(k + 1\) iterations in all.

We don't know the weights, so in the worst case a shortest path is the longest path in the graph. The longest path from \(s\) goes from \(s\) to the next node, then up and around the top square to the center node (3 edges), and then right along the last two edges: \(1 + 3 + 2 = 6\) edges. The graph has no cycles, so no path is longer. If the edges are processed in an unlucky order, each iteration gets only one more edge of this path right, so it takes 6 iterations to get every estimate right and a 7th to see that nothing changed.

Problem #305

Tags: bellman-ford

Consider a “2-chain” graph: a graph with \(n\) nodes that is constructed by taking \(n\) nodes labeled 1, 2, \(\ldots\), \(n\), and, for each node \(u\), making an edge to nodes \(u + 1\) and \(u + 2\) (if they are in the graph).

Suppose the Bellman-Ford algorithm without early stopping is run on a 2-chain graph with \(n\) nodes, using node 1 as the source. What is the time taken as a function of \(n\)? State your answer using asymptotic notation.

Solution

\(\Theta(n^2)\).

Each node has edges to at most two others, so there are about \(2n\) edges (exactly \(2n - 3\)). Bellman-Ford without early stopping runs \(|V| - 1 = n - 1\) iterations, each looping over every edge, for \(\Theta(VE) = \Theta(n \cdot n) = \Theta(n^2)\) time.

Problem #306

Tags: bellman-ford

Suppose the Bellman-Ford algorithm is run on the following graph using node \(a\) as the source node:

Suppose when graph.edges is called, the edges are returned in the order:

\( (a, b), (c, e), (d, f), (c, d), (b, d), (b, e), (a, c), (e, f) \).

After one iteration of the outer loop of Bellman-Ford, what is the estimated distance to node \(d\)?

Solution

9.

Going through the edges in the given order: \((a, b)\) sets \(b\)'s estimate to 1; \((c, e)\), \((d, f)\) and \((c, d)\) change nothing, since \(c\) and \(d\) are still at \(\infty\); then \((b, d)\) sets \(d\)'s estimate to \(1 + 8 = 9\). The shorter path through \(c\) isn't found yet, because \((a, c)\) comes after \((c, d)\) in the order.

Problem #307 🌶️

Tags: bellman-ford

Recall that the Bellman-Ford algorithm keeps track of the estimated shortest path distance from the source to each node in the graph. These estimates are updated, and eventually become correct, provided that the graph has no negative cycles.

Suppose the Bellman-Ford algorithm is run on the graph below using node \(s\) as the source.

After 2 iterations of the outer loop, which nodes are guaranteed to have correct estimated shortest path distances (no matter the order in which graph.edges produces the graph's edges)? Name all such nodes.

Solution

\(s\), \(u_3\), \(u_4\), and \(u_5\).

\(u_3\), \(u_4\), and \(u_5\) are guaranteed to have correct estimated shortest path distances after 2 iterations of the outer loop. \(s\)'s estimated distance is correct from the start, since it is initialized to \(0\).

Problem #308 🌶️

Tags: bellman-ford

Suppose Bellman-Ford with early stopping is run on the weighted graph below. The algorithm uses node \(a\) as the source node.

After the third iteration, name all nodes whose shortest path distances from \(a\) are guaranteed to be estimated correctly.

Solution

\(\{a,b,c,e,f\}\).

After \(k\) iterations of the outer loop, Bellman-Ford is guaranteed to have the correct distance to every node whose shortest path from the source uses at most \(k\) edges. Other estimates might happen to be correct too, depending on the order of the edges, but they aren't guaranteed to be. So we need each node's shortest path, and how many edges it uses:

\[ \begin{aligned} b&: a \to b && \text{distance } 1 && \text{1 edge}\\ c&: a \to c && \text{distance } 10 && \text{1 edge}\\ e&: a \to c \to e && \text{distance } 10 - 9 = 1 && \text{2 edges}\\ f&: a \to c \to e \to f && \text{distance } 1 + 1 = 2 && \text{3 edges}\\ g&: a \to c \to e \to f \to g && \text{distance } 2 - 5 = -3 && \text{4 edges}\\ d&: a \to c \to e \to f \to g \to d && \text{distance } -3 - 4 = -7 && \text{5 edges} \end{aligned} \]

These beat the other paths: \(a \to b \to e\) has distance 2, \(a \to c \to e \to g\) has distance 2, and \(a \to b \to d\) has distance 4.

The source \(a\) is correct (distance 0) from the start. So after the third iteration, \(a\), \(b\), \(c\), \(e\) and \(f\) are guaranteed to be correct, but \(g\) (4 edges) and \(d\) (5 edges) aren't.

Problem #309 🌶️

Tags: breadth first search

Suppose that every edge in the weighted graph \(G\) has weight \(-5\). True or False: breadth first search on this graph will compute a longest path from the source to every node in the graph (that is, a path with greatest possible total edge weight).

You may assume that every node in the graph is reachable from the source.

Solution

True.

A path with \(k\) edges has total weight \(-5k\), so the path with the greatest total weight is the one with the fewest edges. BFS finds exactly this path: the path with the fewest edges from the source to each node.

Problem #310

Tags: bellman-ford

Consider running Bellman-Ford on the graph below using node \(u\) as the source.

Part 1)

Suppose that graph.edges produces edges in the following order:

\[ (u,c), (a, b), (a, d), (u, a), (b, e), (c, d), (e, b), (d, e) \]

After two iterations of the outer for-loop, what is the estimated shortest path distance for node \(b\)?

Solution

7.

After iteration 1, the estimates are \(a = 2\), \(c = 7\), \(d = 9\), \(e = 13\) (\(b\) is still \(\infty\)). In iteration 2, \(b\) becomes \(16\) via \((a, b)\), \(d\) becomes \(5\), then \(b\) becomes \(13 - 6 = 7\) via \((e, b)\), and \(e\) becomes \(9\). So after two iterations, the estimate for \(b\) is 7.

Part 2)

With the same edge order as in the previous part, how many iterations does the outer for-loop take? The algorithm includes early stopping.

Solution

4.

Continuing from the previous part: in iteration 3, \(b\) becomes \(9 - 6 = 3\). Iteration 4 changes nothing, so the algorithm stops after 4 iterations.

Part 3)

Now suppose that graph.edges produces edges in a different order:

\[ (a, b), (a, d), (b, e), (c, d), (d, e), (e, b), (u, a), (u, c) \]

How many iterations does the outer for-loop take? The algorithm includes early stopping.

Solution

3.

Iteration 1 only sets \(a = 2\) and \(c = 7\), since the edges out of \(u\) come last. In iteration 2, \(b = 16\), \(d = 5\), \(e = 24\) then \(9\), and \(b = 3\). Iteration 3 changes nothing, so the algorithm stops after 3 iterations.

Problem #311

Tags: bellman-ford, dijkstra, shortest paths

Suppose you are planning a trip, and are intending to travel by air from San Diego to another city, with potentially multiple stops in between. You would like to schedule flights that take the least amount of time to get to your destination. In order to do this, you are thinking about these flights as a graph, where each airport is a node, and the weight of an edge connecting two airports corresponds to the amount of time it takes to travel between them.

Which algorithm should you choose to compute the fastest route from San Diego to each of the other cities? If two algorithms both find the fastest routes, pick the one which has a better time complexity.

Solution

Dijkstra's Algorithm.

The graph is weighted, so BFS and DFS won't find shortest paths. Travel times are never negative, so both Bellman-Ford and Dijkstra's algorithm work, but Dijkstra's algorithm has the better time complexity.

Problem #312

Tags: bellman-ford, dijkstra, shortest paths

Suppose you are planning a trip by air from San Diego to another city, with potentially multiple stops in between. You are thinking about the flights as a graph, where each airport is a node and each flight is an edge. You don't care about the travel time, and instead care about the cost, so the weight of an edge is the cost of that flight. Also suppose that the airline has a special deal, where flights between certain cities will give you frequent flyer miles, or points towards free flights that you can use later in your trip. In other words, some flights have a negative cost. If you want to find the lowest cost route, which algorithm would be the best choice?

Solution

Bellman-Ford.

Some edge weights are negative, and Dijkstra's algorithm is not guaranteed to find shortest paths when there are negative edge weights. Bellman-Ford works with negative weights (as long as there are no negative cycles), and BFS and DFS don't find shortest paths in weighted graphs.

Problem #313

Tags: dijkstra

Consider running Dijkstra's Algorithm implemented using a priority queue on the graph below using node \(a\) as the source.

You may assume that graph.nodes produces nodes in alphabetical order.

(a) On which iteration of the while loop is est[b] updated for the first time?

(b) After six iterations of the while loop, what is the value of est[b]?

Solution

(a) 1. (b) 3.

(a) On the first iteration, \(a\) is popped from the priority queue and the edge \((a, b)\) is updated, setting est[b] to 3.

(b) The only edge into \(b\) is \((a, b)\), so est[b] never changes again and is still 3 after six iterations.

Problem #314

Tags: dijkstra

Consider the following graph. If the set \(C\) of correct nodes (shown in green) contains nodes \(s\), \(u_1\), and \(u_2\), how many exit paths from \(s\) through \(C\) exist?

Solution

Answer: 4.

We defined an exit path to be any path starting at the source where all but the last node is in the correct set, and the last node is outside of the correct set. More informally: it's a path where the last node is gray and all of the other nodes are green. The four exit paths are \((s, u_1, u_6)\), \((s, u_1, u_3)\), \((s, u_1, u_2, u_4)\), and \((s, u_2, u_4)\).

Problem #315

Tags: dijkstra

Suppose Dijkstra's algorithm is run on the graph shown below using node \(a\) as the source.

What will be the fourth node popped from the priority queue? (The first node popped is the source, \(a\).)

Solution

\(f\).

After popping \(a\), the estimates are \(b = 1\) and \(c = 4\), so \(b\) is popped second. Its edges lower \(e\) to 2 (and set \(d\) to 9), so \(e\) is popped third. Then \((e, f)\) lowers \(f\) to 3, which is less than \(c\)'s 4, so \(f\) is popped fourth.

Problem #316

Tags: dijkstra

Suppose Dijkstra's algorithm is run on the graph shown below using node \(a\) as the source.

Suppose \(C\) is the set of “correct” nodes: that is, \(C\) is the set of nodes whose estimated distances are known to be correct.

What will be the fifth node added to \(C\) by Dijkstra's algorithm? (The first node added to \(C\) is the source, \(a\).)

Solution

\(b\).

Popping \(a\) sets \(g = 1\), \(b = 4\) and \(c = 5\). Then \(g\) is added (1), and it sets \(d = 9\) and \(e = 2\). Then \(e\) is added (2), and it sets \(f = 3\). Then \(f\) is added (3). The smallest remaining estimate is now \(b\)'s 4, so \(b\) is fifth.