Weighted Graphs - PowerPoint PPT Presentation

1 / 47
About This Presentation
Title:

Weighted Graphs

Description:

Airline maps (distance, fare) Electric circuits ... 'Find the lowest-cost path between two given points' J. L. K. M. 5. 3. 2. 6. Weighted, undirected graphs ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 48
Provided by: greenc4
Category:

less

Transcript and Presenter's Notes

Title: Weighted Graphs


1
Weighted 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

2
Weighted, undirected graphs
3
Internal representations
Adjacency matrices use a weight value instead of
a boolean value. Adjacency lists use a weight
field for each list node.
4
Minimum 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!
5
Minimum 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.
6
Minimum 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.
7
General 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.
8
Building 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.
9
Building Minimum Spanning Tree
10
Implementation
  • 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,
    ...

11
Implementation
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.

12
Building 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
13
Building 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
14
Building Minimum Spanning Tree
F 1 E 2 G 6
Note priority update!
L 2 E 2 G 6
15
Building Minimum Spanning Tree
M 1 E 2 J 3 G 5
E 2 J 2 G 5
16
Building Minimum Spanning Tree
G 1 J 2
J 1 H 3
17
Building Minimum Spanning Tree
K 1 H 3
I 1 H 3
18
Building Minimum Spanning Tree
H 2
19
Building 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.
20
Building 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).
21
Building 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
22
Building 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
23
Building Minimum Spanning Tree Kruskals Method
24
Building Minimum Spanning Tree Kruskals Method
25
Building Minimum Spanning Tree Kruskals Method
26
Building Minimum Spanning Tree Kruskals Method
27
Building 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)

28
Kruskals 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.
29
Kruskals 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.
30
Kruskals Method - Notes
Cycle-testing can be done using the union-find
algorithm discussed earlier in the semester -
(how?)
31
Shortest 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.
32
Shortest 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.
33
Shortest 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.
34
Shortest 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.
35
Shortest 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
36
Shortest 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
37
Shortest 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
38
Shortest 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
39
Shortest 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
40
Shortest 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
41
Shortest 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
42
Shortest 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
43
Shortest 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
44
Shortest 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
45
Shortest 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
46
Shortest 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
47
Shortest 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
Write a Comment
User Comments (0)
About PowerShow.com