data structures and algorithms - columbia universitybert/courses/3137/lecture18.pdfgrand central...
TRANSCRIPT
![Page 1: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/1.jpg)
Data Structures and AlgorithmsSession 18. April 1, 2009
Instructor: Bert Huanghttp://www.cs.columbia.edu/~bert/courses/3137
![Page 2: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/2.jpg)
Announcements
Homework 4 due next class
Huffman compression must handle any characters in dictionary.txt
Spell checker can ignore case
![Page 3: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/3.jpg)
Review
Rehashing
String hash function example
Graphs
Terminology and properties
Implementation
![Page 4: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/4.jpg)
Today’s Plan
Topological Sort
Shortest Path
Unweighted version
Weighted version
![Page 5: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/5.jpg)
Implementation
Option 1:
Store all nodes in an indexed list
Represent edges with adjacency matrix
Option 2:
Explicitly store adjacency lists
![Page 6: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/6.jpg)
Adjacency Matrices
2d-array A of boolean variables
A[i][j] is true when node i is adjacent to node j
If graph is undirected, A is symmetric 1
2 3
4
5
1 2 3 4 5
1
2
3
4
5
0 1 1 0 01 0 0 1 01 0 0 1 00 1 1 0 10 0 0 1 0
![Page 7: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/7.jpg)
Adjacency Lists
Each node stores references to its neighbors
1
2 3
4
5
1 2 3
2 1 4
3 1 4
4 2 3 5
5 4
![Page 8: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/8.jpg)
Math Notation for GraphsSet Notation:
(v is in V)
(union)
(intersection)
(U is a subset of V)
G = {V, E}
G is the graph
V is set of vertices
E is set of edges
|V| = N = size of V
v ! V
U ! V
U ! V
U ! V(vi, vj) ! E
![Page 9: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/9.jpg)
Topological Sort
Problem definition:
Given a directed acyclic graph G, order the nodes such that for each edge , is before in the ordering.
e.g., scheduling errands when some tasks depend on other tasks being completed.
(vi, vj) ! E vi
vj
![Page 10: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/10.jpg)
Topological Sort Ex.
Buy Groceries
Cook Dinner
TaxesBuy
Stamps
Mail Tax Form
Mail Postcard
Go to ATMFix
Computer
Look up recipe online
Mail recipe to
Grandma
![Page 11: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/11.jpg)
Topological Sort Naïve AlgorithmDegree means # of edges, indegree means # of incoming edges
1. Compute the indegree of all nodes
2. Print any node with indegree 0
3. Remove the node we just printed. Go to 1.
Which nodes’ indegrees change?
![Page 12: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/12.jpg)
Topological SortBetter Algorithm1. Compute all indegrees
2. Put all indegree 0 nodes into a Collection
3. Print and remove a node from Collection
4. Decrement indegrees of the node’s neighbors.
5. If any neighbor has indegree 0, place in Collection. Go to 3.
![Page 13: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/13.jpg)
Buy Groceries
Cook Dinner
TaxesBuy
Stamps
Mail Tax Form
Mail Postcard
Go to ATMFix
Computer
Look up recipe online
Mail recipe to
Grandma
ATM compgrocer-
iesrecipe stamps taxes cook
grand-ma
post-card
mail taxes
0 0 2 1 1 1 2 2 1 2
![Page 14: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/14.jpg)
Buy Groceries
Cook Dinner
TaxesBuy
Stamps
Mail Tax Form
Mail Postcard
Go to ATMFix
Computer
Look up recipe online
Mail recipe to
Grandma
ATM compgrocer-
iesrecipe stamps taxes cook
grand-ma
post-card
mail taxes
- - 2 1 1 1 2 2 1 2
Queue
comp
ATM
![Page 15: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/15.jpg)
Buy Groceries
Cook Dinner
TaxesBuy
Stamps
Mail Tax Form
Mail Postcard
Go to ATMFix
Computer
Look up recipe online
Mail recipe to
Grandma
ATM compgrocer-
iesrecipe stamps taxes cook
grand-ma
post-card
mail taxes
0 - 2 1 1 1 0 1 2 2 1 2
Queue
comp
![Page 16: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/16.jpg)
Buy Groceries
Cook Dinner
TaxesBuy
Stamps
Mail Tax Form
Mail Postcard
Go to ATMFix
Computer
Look up recipe online
Mail recipe to
Grandma
ATM compgrocer-
iesrecipe stamps taxes cook
grand-ma
post-card
mail taxes
0 - 2 1 1 - 1 2 2 1 2
Queue
stamps
comp
![Page 17: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/17.jpg)
Buy Groceries
Cook Dinner
TaxesBuy
Stamps
Mail Tax Form
Mail Postcard
Go to ATMFix
Computer
Look up recipe online
Mail recipe to
Grandma
ATM compgrocer-
iesrecipe stamps taxes cook
grand-ma
post-card
mail taxes
0 1 2 1 1 0 - 1 0 2 2 1 2
Queue
stamps
![Page 18: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/18.jpg)
Buy Groceries
Cook Dinner
TaxesBuy
Stamps
Mail Tax Form
Mail Postcard
Go to ATMFix
Computer
Look up recipe online
Mail recipe to
Grandma
ATM compgrocer-
iesrecipe stamps taxes cook
grand-ma
post-card
mail taxes
0 1 2 1 - - - 2 2 1 2
Queue
recipe
taxes
stamps
![Page 19: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/19.jpg)
Buy Groceries
Cook Dinner
TaxesBuy
Stamps
Mail Tax Form
Mail Postcard
Go to ATMFix
Computer
Look up recipe online
Mail recipe to
Grandma
ATM compgrocer-
iesrecipe stamps taxes cook
grand-ma
post-card
mail taxes
0 1 2 1 - 2 - 2 2 1 1 0 2 1
Queue
recipe
taxes
![Page 20: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/20.jpg)
Buy Groceries
Cook Dinner
TaxesBuy
Stamps
Mail Tax Form
Mail Postcard
Go to ATMFix
Computer
Look up recipe online
Mail recipe to
Grandma
ATM compgrocer-
iesrecipe stamps taxes cook
grand-ma
post-card
mail taxes
0 1 2 1 - 2 - 2 2 1 - 2 1
Queue
postcard
recipe
taxes
![Page 21: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/21.jpg)
Buy Groceries
Cook Dinner
TaxesBuy
Stamps
Mail Tax Form
Mail Postcard
Go to ATMFix
Computer
Look up recipe online
Mail recipe to
Grandma
ATM compgrocer-
iesrecipe stamps taxes cook
grand-ma
post-card
mail taxes
0 1 2 1 - 2 3 2 2 1 - 2 1 0
Queue
postcard
recipe
![Page 22: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/22.jpg)
Buy Groceries
Cook Dinner
TaxesBuy
Stamps
Mail Tax Form
Mail Postcard
Go to ATMFix
Computer
Look up recipe online
Mail recipe to
Grandma
ATM compgrocer-
iesrecipe stamps taxes cook
grand-ma
post-card
mail taxes
0 1 2 1 - 2 3 2 2 1 - -
Queue
mail taxes
postcard
recipe
![Page 23: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/23.jpg)
Buy Groceries
Cook Dinner
TaxesBuy
Stamps
Mail Tax Form
Mail Postcard
Go to ATMFix
Computer
Look up recipe online
Mail recipe to
Grandma
ATM compgrocer-
iesrecipe stamps taxes cook
grand-ma
post-card
mail taxes
0 1 2 1 0 4 2 3 2 1 2 1 0 - -
Queue
mail taxes
postcard
![Page 24: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/24.jpg)
Buy Groceries
Cook Dinner
TaxesBuy
Stamps
Mail Tax Form
Mail Postcard
Go to ATMFix
Computer
Look up recipe online
Mail recipe to
Grandma
ATM compgrocer-
iesrecipe stamps taxes cook
grand-ma
post-card
mail taxes
0 1 - 4 2 3 2 1 - - -
Queuegroceries
grandma
mail taxes
postcard
![Page 25: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/25.jpg)
Buy Groceries
Cook Dinner
TaxesBuy
Stamps
Mail Tax Form
Mail Postcard
Go to ATMFix
Computer
Look up recipe online
Mail recipe to
Grandma
ATM compgrocer-
iesrecipe stamps taxes cook
grand-ma
post-card
mail taxes
0 1 8 4 2 3 2 1 0 7 5 6
Queue
![Page 26: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/26.jpg)
Buy Groceries
Cook Dinner
TaxesBuy
Stamps
Mail Tax Form
Mail Postcard
Go to ATMFix
Computer
Look up recipe online
Mail recipe to
Grandma
ATM compgrocer-
iesrecipe stamps taxes cook
grand-ma
post-card
mail taxes
0 1 8 4 2 3 9 7 5 6
Queue
![Page 27: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/27.jpg)
Topological Sort Running timeInitial indegree computation: O(|E|)
Unless we update indegree as we build graph
|V| nodes must be enqueued/dequeued
Dequeue requires operation for outgoing edges
Each edge is used, but never repeated
Total running time O(|V| + |E|)
![Page 28: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/28.jpg)
Shortest Path
Given G = (V,E), and a node s V, find the shortest (weighted) path from s to every other vertex in G.
Motivating example: subway travel
Nodes are junctions, transfer locations
Edge weights are estimated time of travel
!
![Page 29: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/29.jpg)
Approximate MTA Express Stop SubgraphA few inaccuracies (don’t use this to plan any trips)
116th Broad.
96th Broad.
72nd Broad.
Times Square
Grand Central
59thLex.
86thLex.
Penn Station
Port Auth.
59th Broad.
125th and 8th
145th and 8th
168th Broad.
![Page 30: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/30.jpg)
Breadth First Search
Like a level-order traversal
Find all adjacent nodes (level 1)
Find new nodes adjacent to level 1 nodes (level 2)
... and so on
We can implement this with a queue
![Page 31: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/31.jpg)
Unweighted Shortest Path AlgorithmSet node s’ distance to 0 and enqueue s.
Then repeat the following:
Dequeue node v. For unset neighbor u:
set neighbor u’s distance to v’s distance +1
mark that we reached v from u
enqueue u
![Page 32: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/32.jpg)
116th Broad.
96th Broad.
72nd Broad.
Times Square
Grand Central
59thLex.
86thLex.
Penn Station
Port Auth.
59th Broad.
125th and 8th
145th and 8th
168th Broad.
168th Broad.
145th Broad.
125th 8th
59th Broad.
Port Auth.
116th Broad.
96th Broad.
72nd Broad.
Times Sq.
Penn St.
86th Lex.
59th Lex.
Grand Centr.
dist
prev
0
source
![Page 33: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/33.jpg)
116th Broad.
96th Broad.
72nd Broad.
Times Square
Grand Central
59thLex.
86thLex.
Penn Station
Port Auth.
59th Broad.
125th and 8th
145th and 8th
168th Broad.
Port Auth.Penn Station72nd Broad.
Grand Central59th Broad.
168th Broad.
145th Broad.
125th 8th
59th Broad.
Port Auth.
116th Broad.
96th Broad.
72nd Broad.
Times Sq.
Penn St.
86th Lex.
59th Lex.
Grand Centr.
dist
prev
1 1 1 0 1 1Times
Sq.Times
Sq.Times
Sq.source
Times Sq.
Times Sq.
![Page 34: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/34.jpg)
116th Broad.
96th Broad.
72nd Broad.
Times Square
Grand Central
59thLex.
86thLex.
Penn Station
Port Auth.
59th Broad.
125th and 8th
145th and 8th
168th Broad.
Port Auth.Penn Station72nd Broad.
Grand Central
168th Broad.
145th Broad.
125th 8th
59th Broad.
Port Auth.
116th Broad.
96th Broad.
72nd Broad.
Times Sq.
Penn St.
86th Lex.
59th Lex.
Grand Centr.
dist
prev
1 1 1 0 1 1Times
Sq.Times
Sq.Times
Sq.source
Times Sq.
Times Sq.
![Page 35: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/35.jpg)
116th Broad.
96th Broad.
72nd Broad.
Times Square
Grand Central
59thLex.
86thLex.
Penn Station
Port Auth.
59th Broad.
125th and 8th
145th and 8th
168th Broad.
125th and 8thPort Auth.
Penn Station72nd Broad.
Grand Central
168th Broad.
145th Broad.
125th 8th
59th Broad.
Port Auth.
116th Broad.
96th Broad.
72nd Broad.
Times Sq.
Penn St.
86th Lex.
59th Lex.
Grand Centr.
dist
prev
2 1 1 1 0 1 159th
Broad.Times
Sq.Times
Sq.Times
Sq.source
Times Sq.
Times Sq.
![Page 36: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/36.jpg)
116th Broad.
96th Broad.
72nd Broad.
Times Square
Grand Central
59thLex.
86thLex.
Penn Station
Port Auth.
59th Broad.
125th and 8th
145th and 8th
168th Broad.
125th and 8thPort Auth.
Penn Station72nd Broad.
168th Broad.
145th Broad.
125th 8th
59th Broad.
Port Auth.
116th Broad.
96th Broad.
72nd Broad.
Times Sq.
Penn St.
86th Lex.
59th Lex.
Grand Centr.
dist
prev
2 1 1 1 0 1 159th
Broad.Times
Sq.Times
Sq.Times
Sq.source
Times Sq.
Times Sq.
![Page 37: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/37.jpg)
116th Broad.
96th Broad.
72nd Broad.
Times Square
Grand Central
59thLex.
86thLex.
Penn Station
Port Auth.
59th Broad.
125th and 8th
145th and 8th
168th Broad.
59th Lex.125th and 8th
Port Auth.Penn Station72nd Broad.
168th Broad.
145th Broad.
125th 8th
59th Broad.
Port Auth.
116th Broad.
96th Broad.
72nd Broad.
Times Sq.
Penn St.
86th Lex.
59th Lex.
Grand Centr.
dist
prev
2 1 1 1 0 1 2 159th
Broad.Times
Sq.Times
Sq.Times
Sq.source
Times Sq.
Grand Centr.
Times Sq.
![Page 38: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/38.jpg)
116th Broad.
96th Broad.
72nd Broad.
Times Square
Grand Central
59thLex.
86thLex.
Penn Station
Port Auth.
59th Broad.
125th and 8th
145th and 8th
168th Broad.
59th Lex.125th and 8th
Port Auth.Penn Station
168th Broad.
145th Broad.
125th 8th
59th Broad.
Port Auth.
116th Broad.
96th Broad.
72nd Broad.
Times Sq.
Penn St.
86th Lex.
59th Lex.
Grand Centr.
dist
prev
2 1 1 1 0 1 2 159th
Broad.Times
Sq.Times
Sq.Times
Sq.source
Times Sq.
Grand Centr.
Times Sq.
![Page 39: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/39.jpg)
116th Broad.
96th Broad.
72nd Broad.
Times Square
Grand Central
59thLex.
86thLex.
Penn Station
Port Auth.
59th Broad.
125th and 8th
145th and 8th
168th Broad.
96th Broad.59th Lex.
125th and 8thPort Auth.
Penn Station
168th Broad.
145th Broad.
125th 8th
59th Broad.
Port Auth.
116th Broad.
96th Broad.
72nd Broad.
Times Sq.
Penn St.
86th Lex.
59th Lex.
Grand Centr.
dist
prev
2 1 1 2 1 0 1 2 159th
Broad.Times
Sq.Times
Sq.72nd
Broad.Times
Sq.source
Times Sq.
Grand Centr.
Times Sq.
![Page 40: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/40.jpg)
116th Broad.
96th Broad.
72nd Broad.
Times Square
Grand Central
59thLex.
86thLex.
Penn Station
Port Auth.
59th Broad.
125th and 8th
145th and 8th
168th Broad.
96th Broad.59th Lex.
125th and 8thPort Auth.
168th Broad.
145th Broad.
125th 8th
59th Broad.
Port Auth.
116th Broad.
96th Broad.
72nd Broad.
Times Sq.
Penn St.
86th Lex.
59th Lex.
Grand Centr.
dist
prev
2 1 1 2 1 0 1 2 159th
Broad.Times
Sq.Times
Sq.72nd
Broad.Times
Sq.source
Times Sq.
Grand Centr.
Times Sq.
![Page 41: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/41.jpg)
116th Broad.
96th Broad.
72nd Broad.
Times Square
Grand Central
59thLex.
86thLex.
Penn Station
Port Auth.
59th Broad.
125th and 8th
145th and 8th
168th Broad.
96th Broad.59th Lex.
125th and 8th
168th Broad.
145th Broad.
125th 8th
59th Broad.
Port Auth.
116th Broad.
96th Broad.
72nd Broad.
Times Sq.
Penn St.
86th Lex.
59th Lex.
Grand Centr.
dist
prev
2 1 1 2 1 0 1 2 159th
Broad.Times
Sq.Times
Sq.72nd
Broad.Times
Sq.source
Times Sq.
Grand Centr.
Times Sq.
![Page 42: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/42.jpg)
116th Broad.
96th Broad.
72nd Broad.
Times Square
Grand Central
59thLex.
86thLex.
Penn Station
Port Auth.
59th Broad.
125th and 8th
145th and 8th
168th Broad.
96th Broad.59th Lex.
168th Broad.
145th Broad.
125th 8th
59th Broad.
Port Auth.
116th Broad.
96th Broad.
72nd Broad.
Times Sq.
Penn St.
86th Lex.
59th Lex.
Grand Centr.
dist
prev
2 1 1 2 1 0 1 2 159th
Broad.Times
Sq.Times
Sq.72nd
Broad.Times
Sq.source
Times Sq.
Grand Centr.
Times Sq.
![Page 43: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/43.jpg)
116th Broad.
96th Broad.
72nd Broad.
Times Square
Grand Central
59thLex.
86thLex.
Penn Station
Port Auth.
59th Broad.
125th and 8th
145th and 8th
168th Broad.
145th and 8th96th Broad.59th Lex.
168th Broad.
145th Broad.
125th 8th
59th Broad.
Port Auth.
116th Broad.
96th Broad.
72nd Broad.
Times Sq.
Penn St.
86th Lex.
59th Lex.
Grand Centr.
dist
prev
3 2 1 1 2 1 0 1 2 1125th 8th
59th Broad.
Times Sq.
Times Sq.
72nd Broad.
Times Sq.
sourceTimes
Sq.Grand Centr.
Times Sq.
![Page 44: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/44.jpg)
116th Broad.
96th Broad.
72nd Broad.
Times Square
Grand Central
59thLex.
86thLex.
Penn Station
Port Auth.
59th Broad.
125th and 8th
145th and 8th
168th Broad.
145th and 8th96th Broad.
168th Broad.
145th Broad.
125th 8th
59th Broad.
Port Auth.
116th Broad.
96th Broad.
72nd Broad.
Times Sq.
Penn St.
86th Lex.
59th Lex.
Grand Centr.
dist
prev
3 2 1 1 2 1 0 1 2 1125th 8th
59th Broad.
Times Sq.
Times Sq.
72nd Broad.
Times Sq.
sourceTimes
Sq.Grand Centr.
Times Sq.
![Page 45: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/45.jpg)
116th Broad.
96th Broad.
72nd Broad.
Times Square
Grand Central
59thLex.
86thLex.
Penn Station
Port Auth.
59th Broad.
125th and 8th
145th and 8th
168th Broad.
86th Lex.145th and 8th96th Broad.
168th Broad.
145th Broad.
125th 8th
59th Broad.
Port Auth.
116th Broad.
96th Broad.
72nd Broad.
Times Sq.
Penn St.
86th Lex.
59th Lex.
Grand Centr.
dist
prev
3 2 1 1 2 1 0 1 3 2 1125th 8th
59th Broad.
Times Sq.
Times Sq.
72nd Broad.
Times Sq.
sourceTimes
Sq.59th Lex.
Grand Centr.
Times Sq.
![Page 46: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/46.jpg)
116th Broad.
96th Broad.
72nd Broad.
Times Square
Grand Central
59thLex.
86thLex.
Penn Station
Port Auth.
59th Broad.
125th and 8th
145th and 8th
168th Broad.
86th Lex.145th and 8th
168th Broad.
145th Broad.
125th 8th
59th Broad.
Port Auth.
116th Broad.
96th Broad.
72nd Broad.
Times Sq.
Penn St.
86th Lex.
59th Lex.
Grand Centr.
dist
prev
3 2 1 1 2 1 0 1 3 2 1125th 8th
59th Broad.
Times Sq.
Times Sq.
72nd Broad.
Times Sq.
sourceTimes
Sq.59th Lex.
Grand Centr.
Times Sq.
![Page 47: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/47.jpg)
116th Broad.
96th Broad.
72nd Broad.
Times Square
Grand Central
59thLex.
86thLex.
Penn Station
Port Auth.
59th Broad.
125th and 8th
145th and 8th
168th Broad.
116th Broad.86th Lex.
145th and 8th
168th Broad.
145th Broad.
125th 8th
59th Broad.
Port Auth.
116th Broad.
96th Broad.
72nd Broad.
Times Sq.
Penn St.
86th Lex.
59th Lex.
Grand Centr.
dist
prev
3 2 1 1 3 2 1 0 1 3 2 1125th 8th
59th Broad.
Times Sq.
Times Sq.
96th Broad.
72nd Broad.
Times Sq.
sourceTimes
Sq.59th Lex.
Grand Centr.
Times Sq.
![Page 48: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/48.jpg)
116th Broad.
96th Broad.
72nd Broad.
Times Square
Grand Central
59thLex.
86thLex.
Penn Station
Port Auth.
59th Broad.
125th and 8th
145th and 8th
168th Broad.
116th Broad.86th Lex.
168th Broad.
145th Broad.
125th 8th
59th Broad.
Port Auth.
116th Broad.
96th Broad.
72nd Broad.
Times Sq.
Penn St.
86th Lex.
59th Lex.
Grand Centr.
dist
prev
3 2 1 1 3 2 1 0 1 3 2 1125th 8th
59th Broad.
Times Sq.
Times Sq.
96th Broad.
72nd Broad.
Times Sq.
sourceTimes
Sq.59th Lex.
Grand Centr.
Times Sq.
![Page 49: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/49.jpg)
116th Broad.
96th Broad.
72nd Broad.
Times Square
Grand Central
59thLex.
86thLex.
Penn Station
Port Auth.
59th Broad.
125th and 8th
145th and 8th
168th Broad.
168th Broad.116th Broad.
86th Lex.
168th Broad.
145th Broad.
125th 8th
59th Broad.
Port Auth.
116th Broad.
96th Broad.
72nd Broad.
Times Sq.
Penn St.
86th Lex.
59th Lex.
Grand Centr.
dist
prev
4 3 2 1 1 3 2 1 0 1 3 2 1145th 8th
125th 8th
59th Broad.
Times Sq.
Times Sq.
96th Broad.
72nd Broad.
Times Sq.
sourceTimes
Sq.59th Lex.
Grand Centr.
Times Sq.
![Page 50: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/50.jpg)
Weighted Shortest Path
The problem becomes more difficult when edges have different weights
Weights represent different costs on using that edge
Standard algorithm is Dijkstra’s Algorithm
![Page 51: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/51.jpg)
Dijkstra’s Algorithm
Keep distance overestimates D(v) for each node v (all non-source nodes are initially infinite)
1. Choose node v with smallest unknown distance
2. Declare that v’s shortest distance is known
3. Update distance estimates for neighbors
![Page 52: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/52.jpg)
Updating Distances
For each of v’s neighbors, w,
if min(D(v)+ weight(v,w), D(w))
i.e., update D(w) if the path going through v is cheaper than the best path so far to w
![Page 53: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/53.jpg)
72nd Broad.
Times Square
Penn Station
Port Auth.59th
Broad.
5
1210
4
7
2
6
59th Broad. Port Auth. 72nd Broad Times Sq. Penn St.
inf inf inf inf 0
? ? ? ? home
![Page 54: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/54.jpg)
72nd Broad.
Times Square
Penn Station
Port Auth.59th
Broad.
5
1210
4
7
2
6
59th Broad. Port Auth. 72nd Broad Times Sq. Penn St.
inf inf inf inf 0
? ? ? ? home
![Page 55: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/55.jpg)
72nd Broad.
Times Square
Penn Station
Port Auth.59th
Broad.
5
1210
4
7
2
6
59th Broad. Port Auth. 72nd Broad Times Sq. Penn St.
inf 6 inf 2 0
? Penn St.? ? Penn St.? home
![Page 56: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/56.jpg)
72nd Broad.
Times Square
Penn Station
Port Auth.59th
Broad.
5
1210
4
7
2
6
59th Broad. Port Auth. 72nd Broad Times Sq. Penn St.
inf 6 inf 2 0
? Penn St.? ? Penn St. home
![Page 57: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/57.jpg)
72nd Broad.
Times Square
Penn Station
Port Auth.59th
Broad.
5
1210
4
7
2
6
59th Broad. Port Auth. 72nd Broad Times Sq. Penn St.
2+12=14 6 2+4=6 2 0
Times Sq? Penn St.? Times Sq? Penn St. home
![Page 58: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/58.jpg)
72nd Broad.
Times Square
Penn Station
Port Auth.59th
Broad.
5
1210
4
7
2
6
59th Broad. Port Auth. 72nd Broad Times Sq. Penn St.
14 6 6 2 0
Times Sq? Penn St. Times Sq? Penn St. home
![Page 59: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/59.jpg)
72nd Broad.
Times Square
Penn Station
Port Auth.59th
Broad.
5
1210
4
7
2
6
59th Broad. Port Auth. 72nd Broad Times Sq. Penn St.
5+6=11 6 6 2 0
Port Auth? Penn St. Times Sq? Penn St. home
![Page 60: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/60.jpg)
72nd Broad.
Times Square
Penn Station
Port Auth.59th
Broad.
5
1210
4
7
2
6
59th Broad. Port Auth. 72nd Broad Times Sq. Penn St.
11 6 6 2 0
Port Auth? Penn St. Times Sq Penn St. home
![Page 61: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/61.jpg)
72nd Broad.
Times Square
Penn Station
Port Auth.59th
Broad.
5
1210
4
7
2
6
59th Broad. Port Auth. 72nd Broad Times Sq. Penn St.
11 6 6 2 0
Port Auth Penn St. Times Sq Penn St. home
![Page 62: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/62.jpg)
Dijkstra’s Algorithm AnalysisFirst, convince ourselves that the algorithm works.
At each stage, we have a set of nodes whose shortest paths we know
In the base case, the set is the source node.
Inductive step: if we have a correct set, is greedily adding the shortest neighbor correct?
![Page 63: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/63.jpg)
Proof by Contradiction (Sketch)
Contradiction: Dijkstra’s finds a shortest path to node w through v, but there exists an even shorter path
This shorter path must pass from inside our known set to outside.
Call the 1st node in cheaper path outside our set u
The path to u must be shorter than the path to w
But then we would have chosen u instead
s
u
v w
?
...
...
![Page 64: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/64.jpg)
Computational Cost
Keep a priority queue of all unknown nodes
Each stage requires a deleteMin, and then some decreaseKeys (the # of neighbors of node)
We call decreaseKey once per edge, we call deleteMin once per vertex
Both operations are O(log |V|)
Total cost: O(|E| log |V| + |V| log |V|) = O(|E| log |V|)
![Page 65: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture18.pdfGrand Central 59th Lex. 86th Lex. Penn Station Port Auth. 59th Broad. 125th and 8th 145th and 8th](https://reader036.vdocuments.us/reader036/viewer/2022071113/5fe9d9e21ff1b772f218e565/html5/thumbnails/65.jpg)
Reading
Weiss Section 9.3 (today’s material)
Weiss Section 9.4 (Monday’s material)