Graphs
What are graphs?
• Yes, this is a graph….
• But we are interested in a different kind of “graph”
Graphs
• Graphs are composed of
– Nodes (vertices)
– Edges (arcs) node
edge
Graphs
• Graphs are composed of
– Nodes (vertices)
– Edges (arcs) node
edge
Varieties
• Nodes
– Labeled or unlabeled
• Edges
– Directed or undirected
– Labeled or unlabeled
Motivation for Graphs
• Consider the data structures we have node node
looked at so far… Value Next Value Next
• Linked list: nodes with 1 incoming edge +
1 outgoing edge 94
• Binary trees/heaps: nodes with 1
incoming edge + 2 outgoing edges
10 97
• B-trees: nodes with 1 incoming edge +
multiple outgoing edges 3 5
96 99
Graphs
are a powerful way to model
relationships in various contexts
Relationships
six relationships:
– Linear orderings
– Hierarchical orderings
– Partial orderings
– Equivalence relations
– Weak orderings
– Adjacency relations
Representing Partial Orders
reserve
check in flight call
airport taxi
pack
take bags
flight
locate
taxi to gate
airport
Partial Orders
reserve check in
flight airport
call
taxi take
taxi to flight
airport locate
pack gate
bags
Motivation for Graphs
• How can you generalize these data structures?
• Consider data structures for representing the
following problems…
CSE Course Prerequisites
461
322
1201 1101
321
326
410 415 142
370 341
413 417
378
Nodes = courses 421
Directed edge = prerequisite 401
Representing a Maze
S
S
E
E
Nodes = rooms
Edge = door or passage
Representing Electrical Circuits
Battery Switch
Nodes = battery, switch, resistor, etc. Resistor
Edges = connections
Program statements
x1 x2
x1=q+y*z + -
x2=y*z-q
Naive:
q * * q
y*z calculated twice y z
x1 x2
common
subexpression + -
eliminated:
q * q
Nodes = symbols/operators
y z
Edges = relationships
Precedence
S1 a=0; 6
S2 b=1;
S3 c=a+1
5
S4 d=b+a;
S5 e=d+1;
S6 e=c+d;
4
Which statements must execute before S6? 3
S1 , S 2 , S 3 , S 4
Nodes = statements
Edges = precedence requirements 1 2
Traffic Flow on Highways
UW
Nodes = cities
Edges = # vehicles on
connecting highway
([Link])
Computer Networks
56
128
16 181
30 140
Nodes = computers
Edges = transmission rates
Airline Routes
Nodes = cities
Edges = direct flights
Graphs
• Important way of modelling and representing the
organization of many systems and problems
– Road networks
– Electronic circuits
– Telecommunication networks
– Human interaction
– Social networks
– Eco-system networks
– Robot navigation paths
– Any relationship …
Graphs
• graph: a data structure containing
– a set of vertices V
– a set of edges E, where an edge
represents a connection between 2 vertices
– G = (V, E)
– edge is a pair (v, w) where v, w in V
• the graph at right: V = {a, b, c} and E = {(a, b), (b, c), (c, a)}
– Assuming that a graph can only have one edge between a pair of
vertices and cannot have an edge to itself, what is the maximum
number of edges a graph can contain, relative to the size of the vertex
set V?
Paths
• path: a path from vertex A to B is a sequence of edges that
can be followed starting from A to reach B
– can be represented as vertices visited or edges taken
– example: path from V to Z: {b, h} or {V, X, Z}
• reachability: v1 is reachable from v2 if a V
a b
path exists from V1 to V2 P
U d X1 Z
P h
• connected graph: one in which it's c 2 e
W g
possible to reach any node from any other
– is this graph connected? f
Y
Cycles
• cycle: path from one node back to itself
– example: {b, g, f, c, a} or {V, X, Y, W, U, V}
• loop: edge directly from node to
V
itself a b
– many graphs don't allow loops U d X Z
C h
c 2 e C
W g1
f Y
Weighted graphs
• weight: (optional) cost associated with a given edge
• example: graph of airline flights
– if we were programming this graph, what information
would we have to store for each vertex / edge?
MAA
DEL
PAT
BLR
AMD
lXA BOM
COK
Directed graphs
• directed graph (digraph): edges are one-way
connections between vertices
– if graph is directed, a vertex has a separate in/out
degree
Graphs-Cyclic-Acyclic
Connectivity
Undirected graphs are connected if there is a path between
any two vertices
Directed graphs are strongly connected if there is a path from
any one vertex to any other
Directed graphs are weakly connected if there is a path
between any two vertices, ignoring direction
A complete graph has an edge between every pair of vertices
Trees as Graphs
• Every tree is a graph with A
some restrictions:
B C
–the tree is directed
–there is exactly one D E F
directed path from the
root to every node G H
More terminology
• degree: number of edges touching a vertex
– example: W has degree 4
– what is the degree of X? of Z?
V
a b
• adjacent vertices: connected h j
directly by an edge U d X Z
c e i
W g
f
Y
Directed Acyclic Graphs (DAGs)
DAGs are directed main()
graphs with no
cycles.
mult()
if program call graph
is a DAG, then all add()
procedure calls can
be in-lined
read()
access()
Trees DAGs Graphs
Graphs Implementation
Implementing a graph
• If we wanted to program an actual data structure to represent a graph,
what information would we need to store?
– for each vertex? 2
– for each edge? 1 3
• What kinds of questions 7
would we want to be able to
answer quickly:
4
– about a vertex?
– about its edges / neighbors? 6
– about paths? 5
– about what edges exist in the graph?
• We'll explore three common graph implementation strategies:
– edge list, adjacency list, adjacency matrix
Edge list
• edge list: an unordered list of all edges in the graph
• advantages
– easy to loop/iterate over all edges
2
• disadvantages 1 3
– hard to tell if an edge
exists from A to B 7
– hard to tell how many edges
a vertex touches (its degree) 4
1 1 1 2 2 3 5 5 5 7 6
5
2 5 6 7 3 4 6 7 4 4
Adjacency matrix
• adjacency matrix: an n × n matrix where:
– the nondiagonal entry aij is the number of edges joining vertex i and
vertex j (or the weight of the edge joining vertex i and vertex j)
– the diagonal entry aii corresponds to the number of loops (self-
connecting edges) at vertex i
[Link]
Pros/cons of Adj. matrix
• advantage: fast to tell whether edge exists between
any two vertices i and j (and to get its weight)
• disadvantage: consumes a lot of memory on sparse
graphs (ones with few edges)
Adjacency matrix example
• The graph at right has the following adjacency matrix:
– How do we figure out the degree of a given vertex?
– How do we find out whether an edge exists from A to B?
– How could we look for loops in the graph?
2
1 2 3 4 5 6 7
1 3
1 0 1 0 0 1 1 0
2 1 0 1 0 0 0 1
3 0 1 0 1 0 0 0 7
4
0 0 1 0 1 0 1
5
1 0 0 1 0 1 1 4
6
7 1 0 0 0 1 0 0
0 1 0 1 1 0 0
6
5
Adjacency lists
• adjacency list: stores edges as individual linked lists of
references to each vertex's neighbors
– generally, no information needs to be stored in the edges,
only in nodes, these arrays can simply be pointers to other
nodes and thus represent edges with little memory
requirement
1 2 5 6
2 3 1 7
3
2 4
4
3 7 5
5
6 6 1 7 4
7 1 5
4 5 2
Pros/cons of adjacency list
• advantage: new nodes can be added to the graph easily, and they can be
connected with existing nodes simply by adding elements to the
appropriate arrays; "who are my neighbors" easily answered
• disadvantage: determining whether an edge exists between two nodes
requires O(n) time, where n is the average number of incident edges per
node
Adjacency list example
• The graph at right has the following adjacency list:
– How do we figure out the degree of a given vertex?
– How do we find out whether an edge exists from A to B?
– How could we look for loops in the graph?
2
1 3
1 2 5 6
2 7
3 1 7
3
2 4 4
4
3 7 5
5
6 6 1 7 4 6
7 1 5 5
4 5 2
Runtime table
n vertices, m edges
no parallel edges Edge Adjacency Adjacency
no self-loops List List Matrix
Space n+m n+m n2
Finding all adjacent
m deg(v) n
vertices to v
Determining if v is
m deg(v) 1
adjacent to w
inserting a vertex 1 1 n2
inserting an edge 1 1 1
removing vertex v m 1 n2
removing an edge m deg(v) 1
Graph traversals
Breadth-first traversal
Consider implementing a breadth-first traversal on a graph:
– Choose any vertex, mark it as visited and push it onto queue
– While the queue is not empty:
• Pop to top vertex v from the queue
• For each vertex adjacent to v that has not been visited:
– Mark it visited, and
– Push it onto the queue
This continues until the queue is empty
– Note: if there are no unvisited vertices, the graph is connected,
Breadth-first traversal
The size of the queue is O(V)
– The size depends both on:
• The number of edges, and
• The out-degree of the vertices
Depth-first traversal
Consider implementing a depth-first traversal on a graph:
– Choose any vertex, mark it as visited
– From that vertex:
• If there is another adjacent vertex not yet visited, go to it
• Otherwise, go back to the most previous vertex that has not yet had all of its
adjacent vertices visited and continue from there
– Continue until no visited vertices have unvisited adjacent vertices
Two implementations:
– Recursive
– Iterative
Example
Consider this graph
Example
Performing a breadth-first traversal
– Push the first vertex onto the queue
A
Example
Performing a breadth-first traversal
– Pop A and push B, C and E
A
B C E
Example
Performing a breadth-first traversal:
– Pop B and push D
A, B
C E D
Example
Performing a breadth-first traversal:
– Pop C and push F
A, B, C
E D F
Example
Performing a breadth-first traversal:
– Pop E and push G and H
A, B, C, E
D F G H
Example
Performing a breadth-first traversal:
– Pop D
A, B, C, E, D
F G H
Example
Performing a breadth-first traversal:
– Pop F
A, B, C, E, D, F
G H
Example
Performing a breadth-first traversal:
– Pop G and push I
A, B, C, E, D, F, G
H I
Example
Performing a breadth-first traversal:
– Pop H
A, B, C, E, D, F, G, H
I
Example
Performing a breadth-first traversal:
– Pop I
A, B, C, E, D, F, G, H, I
Example
Performing a breadth-first traversal:
– The queue is empty: we are finished
A, B, C, E, D, F, G, H, I
Example
Perform a recursive depth-first traversal on this same graph
Example
Performing a recursive depth-first traversal:
– Visit the first node
A
Example
Performing a recursive depth-first traversal:
– A has an unvisited neighbor
A, B
Example
Performing a recursive depth-first traversal:
– B has an unvisited neighbor
A, B, C
Example
Performing a recursive depth-first traversal:
– C has an unvisited neighbor
A, B, C, D
Example
Performing a recursive depth-first traversal:
– D has no unvisited neighbors, so we return to C
A, B, C, D, E
Example
Performing a recursive depth-first traversal:
– E has an unvisited neighbor
A, B, C, D, E, G
Example
Performing a recursive depth-first traversal:
– F has an unvisited neighbor
A, B, C, D, E, G, I
Example
Performing a recursive depth-first traversal:
– H has an unvisited neighbor
A, B, C, D, E, G, I, H
Example
Performing a recursive depth-first traversal:
– We recurse back to C which has an unvisited neighbour
A, B, C, D, E, G, I, H, F
Example
Performing a recursive depth-first traversal:
– We recurse finding that no other nodes have unvisited neighbours
A, B, C, D, E, G, I, H, F
Comparison
Performing a recursive depth-first traversal:
– We recurse finding that no other nodes have unvisited neighbours
A, B, C, D, E, G, I, H, F
Comparison
The order in which vertices can differ greatly
A, B, C, E, D, F, G, H, I A, B, C, D, E, G, I, H, F
Applications
Applications of tree traversals include:
– Determining connectiveness and finding connected sub-graphs
– Determining the path length from one vertex to all others
– Testing if a graph is bipartite
– Determining maximum flow
DFS example
• depth-first search (DFS): finds a path between two vertices by exploring each
possible path as many steps as possible before backtracking
DFS example
• All DFS paths from A to others (assumes ABC edge order)
– A
– A -> B
– A -> B -> D
– A -> B -> F
– A -> B -> F -> E
– A -> C
– A -> C -> G
• What are the paths that DFS did not find?
DFS observations
• guaranteed to find a path if one exists
• easy to retrieve exactly what the path
is (to remember the sequence of edges
taken) if we find it
• optimality: not optimal. DFS is guaranteed to
find a path, not necessarily the best/shortest
path
– Example: DFS(A, E) may return
A -> B -> F -> E
DFS, BFS runtime
• What is the expected runtime of DFS, in terms of
the number of vertices V and the number of
edges E ?
• What is the expected runtime of BFS, in terms of
the number of vertices V and the number of
edges E ?
• Answer: O(V + E)
– each algorithm must potentially visit every node
and/or examine every edge once.
Dijkstra’s algorithm
Dijkstra’s algorithm solves the single-source shortest path problem
– Assumption: all the weights are positive
Strategy
Suppose you are at vertex A
– You are aware of all vertices adjacent to it
– This information is either in an adjacency list or adjacency matrix
Strategy
Is 5 the shortest distance to B via the edge (A, B)?
– Why or why not?
Strategy
Are you guaranteed that the shortest path to C is (A, C), or
that (A, D) is the shortest path to vertex D?
Strategy
We accept that (A, B) is the shortest path to vertex B from A
– Let’s see where we can go from B
Strategy
By some simple arithmetic, we can determine that
– There is a path (A, B, E) of length 5 + 7 = 12
– There is a path (A, B, F) of length 5 + 3 = 8
Strategy
Is (A, B, F) is the shortest path from vertex A to F?
– Why or why not?
Strategy
Are we guaranteed that any other path we are currently aware of is
also going to be the shortest path?
Strategy
Okay, let’s visit vertex F
– We know the shortest path is (A, B, F) and it’s of length 8
Strategy
There are three edges exiting vertex F, so we have paths:
– (A, B, F, E) of length 8 + 6 = 14
– (A, B, F, G) of length 8 + 4 = 12
– (A, B, F, C) of length 8 + 2 = 10
Strategy
By observation:
– The path (A, B, F, E) is longer than (A, B, E)
– The path (A, B, F, C) is shorter than the path (A, C)
Strategy
At this point, we’ve discovered the shortest paths to:
– Vertex B: (A, B) of length 5
– Vertex F: (A, B, F) of length 8
Strategy
At this point, we have the shortest distances to B and F
– Which remaining vertex are we currently guaranteed to have the
shortest distance to?
Dijkstra’s algorithm
We initially don’t know the distance to any vertex except the initial
vertex
– We require an array of distances, all initialized to infinity except for the
source vertex, which is initialized to 0
– Each time we visit a vertex, we will examine all adjacent vertices
• We need to track visited vertices—a Boolean table of size V
– Do we need to track the shortest path to each vertex?
• That is, do I have to store (A, B, F) as the shortest path to vertex F?
– We really only have to record that the shortest path
to vertex F came from vertex B
• We would then determine that the shortest
path to vertex B came from vertex A
• Thus, we need an array of previous vertices,
all initialized to null
Dijkstra’s algorithm
Thus, we will iterate V times:
– Find that unvisited vertex v that has a minimum distance to it
– Mark it as having been visited
– Consider every adjacent vertex w that is unvisited:
• Is the distance to v plus the weight of the edge (v, w) less than our currently
known shortest distance to w
• If so, update the shortest distance to w and record v as the previous pointer
– Continue iterating until all vertices are visited or all remaining vertices
have a distance to them of infinity
Example
Consider this graph
Example
Let us give a weight to each of the edges
Example
Find the shortest distance from K to every other nodes
Example
We set up our table
– Which unvisited vertex has the minimum distance to it?
Vertex Visited Distance Previous
A F ∞ Ø
B F ∞ Ø
C F ∞ Ø
D F ∞ Ø
E F ∞ Ø
F F ∞ Ø
G F ∞ Ø
H F ∞ Ø
I F ∞ Ø
J F ∞ Ø
K F 0 Ø
L F ∞ Ø
Example
We visit vertex K
Vertex Visited Distance Previous
A F ∞ Ø
B F ∞ Ø
C F ∞ Ø
D F ∞ Ø
E F ∞ Ø
F F ∞ Ø
G F ∞ Ø
H F ∞ Ø
I F ∞ Ø
J F ∞ Ø
K T 0 Ø
L F ∞ Ø
Example
Vertex K has four neighbors: H, I, J and L
Vertex Visited Distance Previous
A F ∞ Ø
B F ∞ Ø
C F ∞ Ø
D F ∞ Ø
E F ∞ Ø
F F ∞ Ø
G F ∞ Ø
H F ∞ Ø
I F ∞ Ø
J F ∞ Ø
K T 0 Ø
L F ∞ Ø
Example
We have now found at least one path to each of these vertices
Vertex Visited Distance Previous
A F ∞ Ø
B F ∞ Ø
C F ∞ Ø
D F ∞ Ø
E F ∞ Ø
F F ∞ Ø
G F ∞ Ø
H F 8 K
I F 12 K
J F 17 K
K T 0 Ø
L F 16 K
Example
We’re finished with vertex K
– To which vertex are we now guaranteed we have the shortest path?
Vertex Visited Distance Previous
A F ∞ Ø
B F ∞ Ø
C F ∞ Ø
D F ∞ Ø
E F ∞ Ø
F F ∞ Ø
G F ∞ Ø
H F 8 K
I F 12 K
J F 17 K
K T 0 Ø
L F 16 K
Example
We visit vertex H: the shortest path is (K, H) of length 8
– Vertex H has four unvisited neighbors: E, G, I, L
Vertex Visited Distance Previous
A F ∞ Ø
B F ∞ Ø
C F ∞ Ø
D F ∞ Ø
E F ∞ Ø
F F ∞ Ø
G F ∞ Ø
H T 8 K
I F 12 K
J F 17 K
K T 0 Ø
L F 16 K
Example
Consider these paths:
(K, H, E) of length 8 + 6 = 14 (K, H, G) of length 8 + 11 = 19
(K, H, I) of length 8 + 2 = 10 (K, H, L) of length 8 + 9 = 17
– Which of these are
shorter than any Vertex Visited Distance Previous
known path? A F ∞ Ø
B F ∞ Ø
C F ∞ Ø
D F ∞ Ø
E F ∞ Ø
F F ∞ Ø
G F ∞ Ø
H T 8 K
I F 12 K
J F 17 K
K T 0 Ø
L F 16 K
Example
We already have a shorter path (K, L), but we update the other three
Vertex Visited Distance Previous
A F ∞ Ø
B F ∞ Ø
C F ∞ Ø
D F ∞ Ø
E F 14 H
F F ∞ Ø
G F 19 H
H T 8 K
I F 10 H
J F 17 K
K T 0 Ø
L F 16 K
Example
We are finished with vertex H
– Which vertex do we visit next?
Vertex Visited Distance Previous
A F ∞ Ø
B F ∞ Ø
C F ∞ Ø
D F ∞ Ø
E F 14 H
F F ∞ Ø
G F 19 H
H T 8 K
I F 10 H
J F 17 K
K T 0 Ø
L F 16 K
Example
The path (K, H, I) is the shortest path from K to I of length 10
– Vertex I has two unvisited neighbors: G and J
Vertex Visited Distance Previous
A F ∞ Ø
B F ∞ Ø
C F ∞ Ø
D F ∞ Ø
E F 14 H
F F ∞ Ø
G F 19 H
H T 8 K
I T 10 H
J F 17 K
K T 0 Ø
L F 16 K
Example
Consider these paths:
(K, H, I, G) of length 10 + 3 = 13 (K, H, I, J) of length 10 + 18 = 28
Vertex Visited Distance Previous
A F ∞ Ø
B F ∞ Ø
C F ∞ Ø
D F ∞ Ø
E F 14 H
F F ∞ Ø
G F 19 H
H T 8 K
I T 10 H
J F 17 K
K T 0 Ø
L F 16 K
Example
We have discovered a shorter path to vertex G, but (K, J) is still the
shortest known path to vertex J
Vertex Visited Distance Previous
A F ∞ Ø
B F ∞ Ø
C F ∞ Ø
D F ∞ Ø
E F 14 H
F F ∞ Ø
G F 13 I
H T 8 K
I T 10 H
J F 17 K
K T 0 Ø
L F 16 K
Example
Which vertex can we visit next?
Vertex Visited Distance Previous
A F ∞ Ø
B F ∞ Ø
C F ∞ Ø
D F ∞ Ø
E F 14 H
F F ∞ Ø
G F 13 I
H T 8 K
I T 10 H
J F 17 K
K T 0 Ø
L F 16 K
Example
The path (K, H, I, G) is the shortest path from K to G of length 13
– Vertex G has three unvisited neighbors: E, F and J
Vertex Visited Distance Previous
A F ∞ Ø
B F ∞ Ø
C F ∞ Ø
D F ∞ Ø
E F 14 H
F F ∞ Ø
G T 13 I
H T 8 K
I T 10 H
J F 17 K
K T 0 Ø
L F 16 K
Example
Consider these paths:
(K, H, I, G, E) of length 13 + 15 = 28 (K, H, I, G, F) of length 13 + 4 = 17
(K, H, I, G, J) of length 13 + 19 = 32
– Which do we Vertex Visited Distance Previous
update? A F ∞ Ø
B F ∞ Ø
C F ∞ Ø
D F ∞ Ø
E F 14 H
F F ∞ Ø
G T 13 I
H T 8 K
I T 10 H
J F 17 K
K T 0 Ø
L F 16 K
Example
We have now found a path to vertex F
Vertex Visited Distance Previous
A F ∞ Ø
B F ∞ Ø
C F ∞ Ø
D F ∞ Ø
E F 14 H
F F 17 G
G T 13 I
H T 8 K
I T 10 H
J F 17 K
K T 0 Ø
L F 16 K
Example
Where do we visit next?
Vertex Visited Distance Previous
A F ∞ Ø
B F ∞ Ø
C F ∞ Ø
D F ∞ Ø
E F 14 H
F F 17 G
G T 13 I
H T 8 K
I T 10 H
J F 17 K
K T 0 Ø
L F 16 K
Example
The path (K, H, E) is the shortest path from K to E of length 14
– Vertex G has four unvisited neighbors: B, C, D and F
Vertex Visited Distance Previous
A F ∞ Ø
B F ∞ Ø
C F ∞ Ø
D F ∞ Ø
E T 14 H
F F 17 G
G T 13 I
H T 8 K
I T 10 H
J F 17 K
K T 0 Ø
L F 16 K
Example
The path (K, H, E) is the shortest path from K to E of length 14
– Vertex G has four unvisited neighbors: B, C, D and F
Vertex Visited Distance Previous
A F ∞ Ø
B F ∞ Ø
C F ∞ Ø
D F ∞ Ø
E T 14 H
F F 17 G
G T 13 I
H T 8 K
I T 10 H
J F 17 K
K T 0 Ø
L F 16 K
Example
Consider these paths:
(K, H, E, B) of length 14 + 5 = 19 (K, H, E, C) of length 14 + 1 = 15
(K, H, E, D) of length 14 + 10 = 24 (K, H, E, F) of length 14 + 22 = 36
– Which do we Vertex Visited Distance Previous
update? A F ∞ Ø
B F ∞ Ø
C F ∞ Ø
D F ∞ Ø
E T 14 H
F F 17 G
G T 13 I
H T 8 K
I T 10 H
J F 17 K
K T 0 Ø
L F 16 K
Example
We’ve discovered paths to vertices B, C, D
Vertex Visited Distance Previous
A F ∞ Ø
B F 19 E
C F 15 E
D F 24 E
E T 14 H
F F 17 G
G T 13 I
H T 8 K
I T 10 H
J F 17 K
K T 0 Ø
L F 16 K
Example
Which vertex is next?
Vertex Visited Distance Previous
A F ∞ Ø
B F 19 E
C F 15 E
D F 24 E
E T 14 H
F F 17 G
G T 13 I
H T 8 K
I T 10 H
J F 17 K
K T 0 Ø
L F 16 K
Example
We’ve found that the path (K, H, E, C) of length 15 is the shortest
path from K to C
– Vertex C has one unvisited neighbor, B
Vertex Visited Distance Previous
A F ∞ Ø
B F 19 E
C T 15 E
D F 24 E
E T 14 H
F F 17 G
G T 13 I
H T 8 K
I T 10 H
J F 17 K
K T 0 Ø
L F 16 K
Example
The path (K, H, E, C, B) is of length 15 + 7 = 22
– We have already discovered a shorter path through vertex E
Vertex Visited Distance Previous
A F ∞ Ø
B F 19 E
C T 15 E
D F 24 E
E T 14 H
F F 17 G
G T 13 I
H T 8 K
I T 10 H
J F 17 K
K T 0 Ø
L F 16 K
Example
Where to next?
Vertex Visited Distance Previous
A F ∞ Ø
B F 19 E
C T 15 E
D F 24 E
E T 14 H
F F 17 G
G T 13 I
H T 8 K
I T 10 H
J F 17 K
K T 0 Ø
L F 16 K
Example
We now know that (K, L) is the shortest path between these
two points
– Vertex L has no unvisited neighbors
Vertex Visited Distance Previous
A F ∞ Ø
B F 19 E
C T 15 E
D F 24 E
E T 14 H
F F 17 G
G T 13 I
H T 8 K
I T 10 H
J F 17 K
K T 0 Ø
L T 16 K
Example
Where to next?
– Does it matter if we visit vertex F first or vertex J first?
Vertex Visited Distance Previous
A F ∞ Ø
B F 19 E
C T 15 E
D F 24 E
E T 14 H
F F 17 G
G T 13 I
H T 8 K
I T 10 H
J F 17 K
K T 0 Ø
L T 16 K
Example
Let’s visit vertex F first
– It has one unvisited neighbor, vertex D
Vertex Visited Distance Previous
A F ∞ Ø
B F 19 E
C T 15 E
D F 24 E
E T 14 H
F T 17 G
G T 13 I
H T 8 K
I T 10 H
J F 17 K
K T 0 Ø
L T 16 K
Example
The path (K, H, I, G, F, D) is of length 17 + 14 = 31
– This is longer than the path we’ve already discovered
Vertex Visited Distance Previous
A F ∞ Ø
B F 19 E
C T 15 E
D F 24 E
E T 14 H
F T 17 G
G T 13 I
H T 8 K
I T 10 H
J F 17 K
K T 0 Ø
L T 16 K
Example
Now we visit vertex J
– It has no unvisited neighbors
Vertex Visited Distance Previous
A F ∞ Ø
B F 19 E
C T 15 E
D F 24 E
E T 14 H
F T 17 G
G T 13 I
H T 8 K
I T 10 H
J T 17 K
K T 0 Ø
L T 16 K
Example
Next we visit vertex B, which has two unvisited neighbors:
(K, H, E, B, A) of length 19 + 20 = 39 (K, H, E, B, D) of length 19 + 13 = 32
– We update the path length to A
Vertex Visited Distance Previous
A F 39 B
B T 19 E
C T 15 E
D F 24 E
E T 14 H
F T 17 G
G T 13 I
H T 8 K
I T 10 H
J T 17 K
K T 0 Ø
L T 16 K
Example
Next we visit vertex D
– The path (K, H, E, D, A) is of length 24 + 21 = 45
– We don’t update A
Vertex Visited Distance Previous
A F 39 B
B T 19 E
C T 15 E
D T 24 E
E T 14 H
F T 17 G
G T 13 I
H T 8 K
I T 10 H
J T 17 K
K T 0 Ø
L T 16 K
Example
Finally, we visit vertex A
– It has no unvisited neighbors and there are no unvisited vertices left
– We are done
Vertex Visited Distance Previous
A T 39 B
B T 19 E
C T 15 E
D T 24 E
E T 14 H
F T 17 G
G T 13 I
H T 8 K
I T 10 H
J T 17 K
K T 0 Ø
L T 16 K
Example
Thus, we have found the shortest path from vertex K to each of
the other vertices
Vertex Visited Distance Previous
A T 39 B
B T 19 E
C T 15 E
D T 24 E
E T 14 H
F T 17 G
G T 13 I
H T 8 K
I T 10 H
J T 17 K
K T 0 Ø
L T 16 K
Example
Using the previous pointers, we can reconstruct the paths
Vertex Visited Distance Previous
A T 39 B
B T 19 E
C T 15 E
D T 24 E
E T 14 H
F T 17 G
G T 13 I
H T 8 K
I T 10 H
J T 17 K
K T 0 Ø
L T 16 K
Example
Note that this table defines a rooted parental tree
– The source vertex K is at the root
– The previous pointer is the parent of the vertex in the tree
Vertex Previous
A B
B E
C E
D E
E H
F G
G I
H K
I H
J K
K Ø
L K
Implementation and analysis
The initialization requires O(V) memory and run time
We iterate V – 1 times, each time finding next closest vertex to the source
– Iterating through the table requires is O (V) time
– Each time we find a vertex, we must check all of its neighbors
– With an adjacency matrix, the run time is O (V(V + V)) = O(V2)
– With an adjacency list, the run time is O (V2 + E) = O (V2) as E = O(V2)