CSE 373: More on Dijkstra’s algorithm Michael Lee Wednesday, Feb 21, 2018 1
Dijkstra’s algorithm Initialization: 2. Set our starting node’s cost to 0 Core loop: 1. Get the next (unvisited) node that has the smallest cost 2. Update all adjacent vertices (if applicable) 3. Mark current node as “visited” Idea: Greedily pick node with smallest cost, then update everything possible. Repeat. 2 1. Assign each node an initial cost of ∞
Dijkstra’s algorithm Initialization: 2. Set our starting node’s cost to 0 Core loop: 1. Get the next (unvisited) node that has the smallest cost 2. Update all adjacent vertices (if applicable) 3. Mark current node as “visited” Idea: Greedily pick node with smallest cost, then update everything possible. Repeat. 2 1. Assign each node an initial cost of ∞
Dijkstra’s algorithm Initialization: 2. Set our starting node’s cost to 0 Core loop: 1. Get the next (unvisited) node that has the smallest cost 2. Update all adjacent vertices (if applicable) 3. Mark current node as “visited” Idea: Greedily pick node with smallest cost, then update everything possible. Repeat. 2 1. Assign each node an initial cost of ∞
Dijkstra’s algorithm Metaphor: Treat edges as canals and edge weights as distance. Imagine opening a dam at the starting node. How long does it take for the water to reach each vertex? Caveat: Dijkstra’s algorithm only guaranteed to work for graphs with no negative edge weights. Pronunciation: DYKE-struh (“dijk” rhymes with “bike”) 3
Dijkstra’s algorithm Metaphor: Treat edges as canals and edge weights as distance. Imagine opening a dam at the starting node. How long does it take for the water to reach each vertex? Caveat: Dijkstra’s algorithm only guaranteed to work for graphs with no negative edge weights. Pronunciation: DYKE-struh (“dijk” rhymes with “bike”) 3
Dijkstra’s algorithm Metaphor: Treat edges as canals and edge weights as distance. Imagine opening a dam at the starting node. How long does it take for the water to reach each vertex? Caveat: Dijkstra’s algorithm only guaranteed to work for graphs with no negative edge weights. Pronunciation: DYKE-struh (“dijk” rhymes with “bike”) 3
Dijkstra’s algorithm 2 list. start at the end, trace the red arrows backwards, and reverse the And we’re done! Now, to fjnd the shortest path, from a to a node, 1 3 2 3 1 7 2 11 9 10 Suppose we start at vertex “a”: 5 4 1 2 g e c d h f b a 4
Dijkstra’s algorithm 1 10 2 9 11 2 7 3 4 2 3 1 We initially assign all nodes a cost of infjnity. And we’re done! Now, to fjnd the shortest path, from a to a node, start at the end, trace the red arrows backwards, and reverse the list. 5 1 Suppose we start at vertex “a”: 2 a b f h d c e g 4 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
Dijkstra’s algorithm 1 10 2 9 11 2 7 3 4 2 3 1 Next, assign the starting node a cost of 0. And we’re done! Now, to fjnd the shortest path, from a to a node, start at the end, trace the red arrows backwards, and reverse the list. 5 1 Suppose we start at vertex “a”: 2 a b f h d c e g 4 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞
Dijkstra’s algorithm 1 10 2 9 11 2 7 3 4 2 3 1 Next, update all adjacent node costs as well as the backpointers. And we’re done! Now, to fjnd the shortest path, from a to a node, start at the end, trace the red arrows backwards, and reverse the list. 5 1 Suppose we start at vertex “a”: 2 a b f h d c e g 4 0 2 ∞ ∞ ∞ ∞ 4 1
Dijkstra’s algorithm 1 10 2 9 11 2 7 3 4 2 3 1 The pending node with the smallest cost is c , so we visit that next. And we’re done! Now, to fjnd the shortest path, from a to a node, start at the end, trace the red arrows backwards, and reverse the list. 5 1 Suppose we start at vertex “a”: 2 a b f h d c e g 4 2 ∞ ∞ 0 ∞ ∞ 4 1
Dijkstra’s algorithm 3 10 2 9 11 2 7 1 2 Suppose we start at vertex “a”: 3 1 We consider all adjacent nodes. a is fjxed, so we only need to update e . Note the new cost of e is the sum of the weights for And we’re done! Now, to fjnd the shortest path, from a to a node, start at the end, trace the red arrows backwards, and reverse the list. 5 4 1 d a b f h 2 4 c e g 2 ∞ ∞ 0 ∞ 4 1 12 a − c and c − e .
Dijkstra’s algorithm 1 10 2 9 11 2 7 3 4 2 3 1 b is the next pending node with smallest cost. And we’re done! Now, to fjnd the shortest path, from a to a node, start at the end, trace the red arrows backwards, and reverse the list. 5 1 Suppose we start at vertex “a”: 2 a b f h d c e g 4 2 ∞ ∞ 0 ∞ 1 12 4
Dijkstra’s algorithm 1 10 2 9 11 2 7 3 Suppose we start at vertex “a”: 2 3 1 The adjacent nodes are c , e , and f . The only node where we can And we’re done! Now, to fjnd the shortest path, from a to a node, start at the end, trace the red arrows backwards, and reverse the list. 5 4 1 d a b f h 2 4 c e g 2 4 ∞ 0 ∞ 1 12 4 update the cost is f . Note the route a − b − e has the same cost as a − c − e , so there’s no point in updating the backpointer to e .
Dijkstra’s algorithm 3 10 2 9 11 2 7 1 2 4 3 1 Both d and f have the same cost, so let’s (arbitrarily) pick d next. Note that we can’t adjust any of our neighbors. And we’re done! Now, to fjnd the shortest path, from a to a node, start at the end, trace the red arrows backwards, and reverse the list. 5 1 Suppose we start at vertex “a”: 2 a b f h d c e g 4 2 4 ∞ 0 ∞ 1 12 4
Dijkstra’s algorithm 1 10 2 9 11 2 7 3 4 2 3 1 Next up is f . And we’re done! Now, to fjnd the shortest path, from a to a node, start at the end, trace the red arrows backwards, and reverse the list. 5 1 Suppose we start at vertex “a”: 2 a b f h d c e g 4 2 4 ∞ 0 ∞ 1 12 4
Dijkstra’s algorithm 1 10 2 9 11 2 7 3 4 2 3 1 The only neighbor we is h . And we’re done! Now, to fjnd the shortest path, from a to a node, start at the end, trace the red arrows backwards, and reverse the list. 5 1 Suppose we start at vertex “a”: 2 a b f h d c e g 4 2 4 7 0 ∞ 1 12 4
Dijkstra’s algorithm 1 10 2 9 11 2 7 3 4 2 3 1 h has the smallest cost now. And we’re done! Now, to fjnd the shortest path, from a to a node, start at the end, trace the red arrows backwards, and reverse the list. 5 1 Suppose we start at vertex “a”: 2 a b f h d c e g 4 2 4 7 0 ∞ 1 12 4
Dijkstra’s algorithm 1 10 2 9 11 2 7 3 4 2 3 1 We update g . And we’re done! Now, to fjnd the shortest path, from a to a node, start at the end, trace the red arrows backwards, and reverse the list. 5 1 Suppose we start at vertex “a”: 2 a b f h d c e g 4 2 4 7 0 1 12 4 8
Dijkstra’s algorithm 1 10 2 9 11 2 7 3 4 2 3 1 Next up is g . And we’re done! Now, to fjnd the shortest path, from a to a node, start at the end, trace the red arrows backwards, and reverse the list. 5 1 Suppose we start at vertex “a”: 2 a b f h d c e g 4 2 4 7 0 4 1 12 8
Dijkstra’s algorithm 2 2 9 11 2 7 1 3 3 5 1 The two adjacent nodes are f and e . f is fjxed so we leave it alone. We however will update e : our current route is cheaper then the previous route, so we update both the cost and the backpointer. And we’re done! Now, to fjnd the shortest path, from a to a node, start at the end, trace the red arrows backwards, and reverse the list. 10 4 Suppose we start at vertex “a”: c a b f h 1 d g e 2 4 2 4 7 0 4 1 11 8
Dijkstra’s algorithm 3 10 2 9 11 2 7 1 2 4 3 1 The last pending node is e . We visit it, and check for any unfjxed adjacent nodes (there are none). And we’re done! Now, to fjnd the shortest path, from a to a node, start at the end, trace the red arrows backwards, and reverse the list. 5 1 Suppose we start at vertex “a”: 2 a b f h d c e g 4 2 4 7 0 4 1 11 8
Dijkstra’s algorithm 7 5 10 2 9 11 2 1 1 3 2 3 1 And we’re done! Now, to fjnd the shortest path, from a to a node, start at the end, trace the red arrows backwards, and reverse the list. 4 2 Suppose we start at vertex “a”: d a b f h 4 c e g 2 4 7 0 4 1 11 8
Dijkstra’s algorithm Core idea in simplifjed pseudocode: 5 def dijkstra(start): for (v : vertices): set cost(v) to infinity set cost(start) to 0 while (we still have unvisited nodes): current = get next smallest node for (edge : current.getOutEdges()): newCost = min(cost(current) + edge.cost, cost(edge.dest)) update cost(edge.dest) to newCost, update backpointers, etc return backpointers dictionary
Recommend
More recommend