Title: Weighted Graphs
1Weighted Graphs
- In this model, weights or costs are associated
with edges. - Examples
- Airline maps (distance, fare)
- Electric circuits (length, cost of wire)
- Job-scheduling chart (time, cost of task)
- We are interested in ways to
- Find the lowest-cost way to connect all the
points - Find the lowest-cost path between two given
points
2Weighted, undirected graphs
3Internal representations
Adjacency matrices use a weight value instead of
a boolean value. Adjacency lists use a weight
field for each list node.
4Minimum Spanning Tree
A minimum spanning tree of a weighted graph is a
collection of edges connecting all the vertices
such that the sum of the weights of the edges is
at least as small as the sum of the weights of
any other collection of edges connecting all the
vertices. Note that this need not be unique!
5Minimum Spanning Tree
These edges form spanning trees because if
theres any cycle, some edges in the cycle can be
deleted to give a collection of edges that still
connect the vertices but has a smaller weight.
6Minimum Spanning Tree General Property
Given any division of the vertices of a graph
into two sets, the minimum spanning tree contains
the shortest of the edges connecting a vertex in
one of the sets to a vertex in the other set.
A B C D E F G H I J K L M gt DF must be
in any minimum spanning tree.
7General Property Proof
Given any division of the vertices of a graph
into two sets, the minimum spanning tree contains
the shortest of the edges connecting a vertex in
one of the sets to a vertex in the other
set. Proof by contradiction Call the shortest
edge connecting two sets s and assume it is not
in the minimum spanning tree. Then consider the
graph formed by adding s to the purported minimum
spanning tree. This graph has a cycle in that
cycle some other edge besides s must connect the
two sets. Deleting this edge and adding s gives a
shorter spanning tree, and this contradicts the
assumption that s is not in the minimum spanning
tree.
8Building Minimum Spanning Tree
Based on the general property proved earlier, we
can build a minimum spanning tree by starting
with any vertex and always taking next the vertex
closest to the vertices already taken. In case
of a tie, any vertex among those tied will do.
9Building Minimum Spanning Tree
10Implementation
- Recall that in a traversal, there are 3 types of
vertices - tree vertices, whose edges have all been examined
- fringe vertices, on a data structure, waiting to
be processed - unseen vertices, whose which havent been touched
at all. - Fundamentally, graph searching does
- Move one vertex x from the fringe to the tree
- Put on the fringe any unseen vertices adjacent
to x - Depending on the data structure (stack,
queue,...) the traversal ends up being DFS, BFS,
...
11Implementation
We need a special kind of data structure that
prioritizes vertices on the fringes of our
current set based on the weights. As it turns
out, the now-famous priority queue does the
job. To compute the minimal spanning tree, the
priority of each vertex on the fringe should be
the length of the shortest edge connecting it to
the tree.
12Building Minimum Spanning Tree
Based on the general property proved earlier, we
can build a minimum spanning tree by starting
with any vertex and always taking next the vertex
closest to the vertices already taken. In case
of a tie, any vertex among those tied will do.
B 1 F 2 G 6
State of priority queue
13Building Minimum Spanning Tree
C 1 D 2 F 2 E 4 G 6
A
2
H
6
I
D 2 F 2 E 4 G 6
1
3
1
B
C
1
G
1
4
4
2
2
1
K
J
1
2
D
E
5
3
1
2
4
2
L
M
F
1
2
14Building Minimum Spanning Tree
F 1 E 2 G 6
Note priority update!
L 2 E 2 G 6
15Building Minimum Spanning Tree
M 1 E 2 J 3 G 5
E 2 J 2 G 5
16Building Minimum Spanning Tree
G 1 J 2
J 1 H 3
17Building Minimum Spanning Tree
K 1 H 3
I 1 H 3
18Building Minimum Spanning Tree
H 2
19Building Minimum Spanning Tree - Performance
Priority-first search on sparse graphs computes
the minimum-spanning tree in O( (EV) log V)
steps. At each step, we pick the shortest edge
from a visited node to a fringe node (there are
no edges from visited nodes to unseen nodes),
this by the property discussed previously, every
edge that is picked is on the minimum spanning
tree. The priority queue contains only vertices,
and, if implemented as a heap then each operation
requires O(logV) steps. Each vertex leads to an
insertion and each edge leads to a pqueue change
operation.
20Building Minimum Spanning Tree Kruskals Method
A completely different method is to simply add
edges one at a time, at each step using the
shortest edge that does not form a cycle. That
is, we start with an N-tree forest, and for N
steps, we combine two trees (using the shortest
edge possible) until there is just one tree left.
(Kruskal, 1956).
21Building Minimum Spanning Tree Kruskals Method
A
2
H
6
I
1
3
1
B
C
1
G
1
4
4
2
2
1
K
J
1
2
D
E
5
3
1
2
4
2
L
M
F
1
2
22Building Minimum Spanning Tree Kruskals Method
A
2
H
6
I
1
3
1
B
C
1
G
1
4
4
2
2
K
1
J
1
2
D
E
5
3
1
2
4
2
L
M
F
1
2
23Building Minimum Spanning Tree Kruskals Method
24Building Minimum Spanning Tree Kruskals Method
25Building Minimum Spanning Tree Kruskals Method
26Building Minimum Spanning Tree Kruskals Method
27Building Minimum Spanning Tree Kruskals Method
- How to know when to stop? Two ways.
- If we find V-1 edges, we have a tree and we can
stop. - If the pqueue runs out first, then we have
examined all the edges without finding a spanning
tree (i.e. the given graph is not connected)
28Kruskals Method - Performance
The running time of this method is dominated by
the time spent processing edges in the priority
queue. Kruskals algorithm computes the minimum
spanning tree of a graph in O(E log E)
steps. The correctness of the algorithm follows
from the basic property proved earlier the two
sets of vertices are those attached to edges
chosen for the tree and those not yet touched.
Each edge added is the shortest between vertices
in the two sets.
29Kruskals Method - Performance
The worst case is a graph that is not connected,
so that all the edges must be examined. Even for
connected graphs, the worst case is the same,
because a graph might consist of two clusters of
vertices all connected together by very short
edges, and only one edge that is very long
connecting the two clusters. In that case, the
longest edge in the graph is in the minimum
spanning tree, but it will be the last edge out
of the priority queue.
30Kruskals Method - Notes
Cycle-testing can be done using the union-find
algorithm discussed earlier in the semester -
(how?)
31Shortest Path Algorithms
The shortest-path problem is to find the path in
a weighted graph connecting two given vertices x
and y with the property that the weights of all
the edges is minimized over all such path. If
the weights are all 1, the problem is still
interesting if finds the path with the minimum
number of edges that connects x and y.
32Shortest Path Algorithms
We have already considered an algorithm that
solves this problem BFS. A BFS starting at x
will first visit all vertices that can be reached
from x with one edge, then all vertices that can
be reached from x with two edges, and so on,
visiting all vertices that can be reached with k
edges before encountering any that require k1
edges. Thus, when y is first encountered, the
shortest path from x has been found because no
shorter paths reached y.
33Shortest Path Algorithms
In general, we can consider the shortest path
from a given vertex to all the other vertices. If
we draw the shortest path from x to every other
vertex in the graph, then we get no cycles
(why?), and we have a spanning tree. Each vertex
leads to a different spanning tree.
34Shortest Path Algorithms
Shortest path can be trivially implemented using
the same priority-first solution as for finding a
minimum spanning tree. We build the tree for
vertex x by adding, at each step, the vertex on
the fringe that is closest to x (before, we added
the one closest to the tree). Implementation
details To find which fringe vertex is closest
to x, we use the val array for each tree vertex
k, valk is the distance from that vertex to x,
using the shortest path (which must be comprised
of tree nodes). When k is added to the tree, we
update the fringe by going through ks adjacency
list. For each node t on the list, the shortest
distance to x through k from t-gtv is valk
t-gtw (where w is the associated weight). We use
valk t-gtw for priority in the priority-first
graph traversal program.
35Shortest Path For Vertex A
Based on the general property proved earlier, we
can build a shortest path tree for vertex A by
starting with vertex and always taking next the
vertex closest to A so far.
B 1 F 2 G 6
State of priority queue
36Shortest Path For Vertex A
Based on the general property proved earlier, we
can build a shortest path tree for vertex A by
starting with vertex and always taking next the
vertex closest to A so far.
C 2 F 2 D 3 E 5 G 6
State of priority queue
37Shortest Path For Vertex A
Based on the general property proved earlier, we
can build a shortest path tree for vertex A by
starting with vertex and always taking next the
vertex closest to A so far.
A
2
H
6
I
F 2 D 3 E 5 G 6
1
3
1
B
C
1
G
1
4
4
2
2
1
K
J
1
2
D
E
5
3
1
2
4
2
L
M
F
1
2
State of priority queue
38Shortest Path For Vertex A
Based on the general property proved earlier, we
can build a shortest path tree for vertex A by
starting with vertex and always taking next the
vertex closest to A so far.
Note how value changes from 5 to 4
A
2
H
6
I
D 3 L 4 E 4 G 6
1
3
1
B
C
1
G
1
4
4
2
2
1
K
J
1
2
D
E
5
3
1
2
4
2
L
F
M
1
2
State of priority queue
39Shortest Path For Vertex A
Based on the general property proved earlier, we
can build a shortest path tree for vertex A by
starting with vertex and always taking next the
vertex closest to A so far.
A
2
H
6
I
1
3
1
B
C
L 4 E 4 G 6
1
G
1
4
4
2
2
1
K
J
1
2
D
E
5
3
1
2
4
2
L
F
M
1
2
State of priority queue
40Shortest Path For Vertex A
Based on the general property proved earlier, we
can build a shortest path tree for vertex A by
starting with vertex and always taking next the
vertex closest to A so far.
A
2
H
6
I
E 4 M 5 G 6 J 7
1
3
1
B
C
1
G
1
4
4
2
2
1
K
J
1
2
D
E
5
3
1
2
4
2
L
F
M
1
2
State of priority queue
41Shortest Path For Vertex A
Based on the general property proved earlier, we
can build a shortest path tree for vertex A by
starting with vertex and always taking next the
vertex closest to A so far.
A
2
H
6
I
1
3
1
B
C
1
G
M 5 G 5 J 7
1
4
4
2
2
1
K
J
1
2
D
E
5
3
1
2
4
2
L
F
M
1
2
State of priority queue
42Shortest Path For Vertex A
Based on the general property proved earlier, we
can build a shortest path tree for vertex A by
starting with vertex and always taking next the
vertex closest to A so far.
A
2
H
6
I
1
3
1
B
C
1
G
1
4
4
2
2
1
K
G 5 J 7
J
1
2
D
E
5
3
1
2
4
2
L
M
F
1
2
State of priority queue
43Shortest Path For Vertex A
Based on the general property proved earlier, we
can build a shortest path tree for vertex A by
starting with vertex and always taking next the
vertex closest to A so far.
A
2
H
6
I
1
3
1
B
C
1
G
1
4
4
2
2
1
K
J 6 H 8
J
1
2
D
E
5
3
1
2
4
2
L
M
F
1
2
State of priority queue
44Shortest Path For Vertex A
Based on the general property proved earlier, we
can build a shortest path tree for vertex A by
starting with vertex and always taking next the
vertex closest to A so far.
A
2
H
6
I
1
3
1
B
C
1
G
1
4
4
2
2
1
K
J
K 7 H 8
1
2
D
E
5
3
1
2
4
2
L
M
F
1
2
State of priority queue
45Shortest Path For Vertex A
Based on the general property proved earlier, we
can build a shortest path tree for vertex A by
starting with vertex and always taking next the
vertex closest to A so far.
A
2
H
6
I
1
3
1
B
C
1
G
1
4
4
2
2
K
1
J
I 8 H 8
1
2
D
E
5
3
1
2
4
2
L
M
F
1
2
State of priority queue
46Shortest Path For Vertex A
Based on the general property proved earlier, we
can build a shortest path tree for vertex A by
starting with vertex and always taking next the
vertex closest to A so far.
A
2
H
I
6
1
3
1
B
C
1
G
1
4
4
2
2
K
1
J
1
2
D
E
5
3
H 8
1
2
4
2
L
M
F
1
2
State of priority queue
47Shortest Path For Vertex A
Based on the general property proved earlier, we
can build a shortest path tree for vertex A by
starting with vertex and always taking next the
vertex closest to A so far.
A
2
H
I
6
1
3
1
B
C
1
G
1
4
4
2
2
K
1
J
1
2
D
E
5
3
1
2
4
2
L
M
F
1
2
State of priority queue