20181110 1K 1 0 0
Description
Manolis. . Koubarakis. Data Structures and Programming Techniques. 1. Weighted Graphs. Weighted graphs . are directed graphs in which numbers called . weights. are attached to the directed edges.. Example. ID: 727188
Embed code:
Download this presentation
DownloadNote  The PPT/PDF document "Weighted Graphs ( Γράφοι με Βά..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, noncommercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Presentations text content in Weighted Graphs ( Γράφοι με Βάρη)
Weighted Graphs (Γράφοι με Βάρη)
Manolis Koubarakis
Data Structures and Programming Techniques
1
Slide2Weighted Graphs
Weighted graphs are directed graphs in which numbers called weights are attached to the directed edges.
Example: Let the vertices of a graph represent cities on a map. The weight on an edge connecting city A to city B can be the travel distance from A to B, the cost of an airline ticket to go from A to B, or the time required to travel from A to B.
Data Structures and Programming Techniques
2
Slide3Weighted Graphs (cont’d)
To represent a weighted directed graph
, we can use an adjacency matrix
in which:
if there exists an edge
of weight
if there is no edge from
to
.We will assume that all weights are nonnegative numbers.
Data Structures and Programming Techniques
3
Slide4Example Weighted Graph
Data Structures and Programming Techniques
4
2
1
6
5
4
3
10
3
7
5
6
7
2
8
1
5
Slide5Adjacency Matrix for the Example Graph
1
2
3
4
5
6
1
0
35207
10
3
0
5
1
4
0
6
5
0
7
6
8
2
0
1
2
3
4
5
6
1
0
3
5
2
0
7
10
3
0
5
1
4
0
6
5
0
7
6
8
20
Data Structures and Programming Techniques
5
Slide6Shortest Paths (Συντομότερα Μονοπάτια)
The length of a path P is the sum of the weights of the edges of P.
A very interesting problem in a directed weighted graph is to find the shortest path from a vertex A to a vertex B.
Data Structures and Programming Techniques
6
Slide7The Shortest Path from Vertex 1 to Vertex 5
Data Structures and Programming Techniques
7
2
1
6
5
4
3
10
3
7
5
6
7
2
8
1
5
Slide8The Single Source Shortest Paths Problem
Let
be a directed graph in which each edge has a nonnegative weight, and one vertex is specified as the
source
(
αφετηρία).
The
single source shortest paths
problem (το πρόβλημα των συντομότερων μονοπατιών κοινής αφετηρίας) is to determine the length of the shortest path from the source to each vertex in Data Structures and Programming Techniques8
Slide9Dijkstra’s Greedy Algorithm for the Single Source Shortest Paths Problem
Let
our graph.
We
start
with a vertex set
containing only the source.
We will
progressively enlarge by adding one new vertex at a time, until includes all vertices of .The vertex we add at each stage is the vertex in , which is at a minimum distance from the source among all vertices in that have not been added to (this is a greedy choice). Data Structures and Programming Techniques9
Slide10Dijkstra’s Algorithm (cont’d)
We keep track of the
minimum distance from the source
at each stage by using an array
which keeps track of the shortest distance from
to each vertex
in
and also each vertex
in
using a path starting at , such that all vertices of path lie in , except the last vertex which lies outside . Data Structures and Programming Techniques10
Slide11Dijkstra’s Algorithm (cont’d)
Every time we add a new vertex to
, we update the array
. This distance is updated in case it is bigger than the length of the path from the source to
going through
which is
. This operation is called
relaxation
(χαλάρωση). Data Structures and Programming Techniques11
Slide12Example Graph
Data Structures and Programming Techniques
12
2
1
6
5
4
3
10
3
7
5
6
7
2
8
1
5
Slide13Expanding the Vertex Set W in Stages
Stage
W
VW
w
Δ(
w)
Δ
(1)
Δ(2)Δ(3)Δ(4)Δ(5)Δ(6)Start{1}{2,3,4,5,6}035
Stage
W
VW
w
Δ(
w)
Δ
(1)
Δ
(2)
Δ
(3)
Δ
(4)
Δ
(5)
Δ
(6)
Start
{1}
{2,3,4,5,6}


0
3
5
Data Structures and Programming Techniques
13
2
1
6
5
4
3
10
3
7
5
6
7
2
815
Slide14Expanding the Vertex Set W in Stages (cont’d)
Stage
W
VW
w
Δ(
w)
Δ
(1)
Δ(2)Δ(3)Δ(4)Δ(5)Δ(6)Start{1}{2,3,4,5,6}035
Stage
W
VW
w
Δ(
w)
Δ
(1)
Δ
(2)
Δ
(3)
Δ
(4)
Δ
(5)
Δ
(6)
Start
{1}
{2,3,4,5,6}


0
3
5
Data Structures and Programming Techniques
14
W=2
is chosen for the second stage.
2
1
6
5
4
3
10
3
7
5
6
72815
Slide15Expanding the Vertex Set W in Stages (cont’d)
Stage
W
VW
w
Δ(
w)
Δ
(1)
Δ(2)Δ(3)Δ(4)Δ(5)Δ(6)Start{1}{2,3,4,5,6}0352{1,2}{3,4,5,6}
230310
5
Stage
W
VW
w
Δ(
w)
Δ
(1)
Δ
(2)
Δ
(3)
Δ
(4)
Δ
(5)
Δ
(6)
Start
{1}
{2,3,4,5,6}


0
3
5
2
{1,2}
{3,4,5,6}
2
3
0
3
10
5
Data Structures and Programming Techniques
15
2
1
654310375672
8
1
5
Slide16Expanding the Vertex Set W in Stages (cont’d)
Stage
W
VW
w
Δ(
w)
Δ
(1)
Δ(2)Δ(3)Δ(4)Δ(5)Δ(6)Start{1}{2,3,4,5,6}0352{1,2}{3,4,5,6}
230310
5
Stage
W
VW
w
Δ(
w)
Δ
(1)
Δ
(2)
Δ
(3)
Δ
(4)
Δ
(5)
Δ
(6)
Start
{1}
{2,3,4,5,6}


0
3
5
2
{1,2}
{3,4,5,6}
2
3
0
3
10
5
Data Structures and Programming Techniques
16
W=6
is chosen for the third stage.
216543103756
7
2
8
1
5
Slide17Expanding the Vertex Set W in Stages (cont’d)
Stage
W
VW
w
Δ(
w)
Δ
(1)
Δ(2)Δ(3)Δ(4)Δ(5)Δ(6)Start{1}{2,3,4,5,6}0352{1,2}{3,4,5,6}
230310
5
3
{1,2,6}
{3,4,5}
6
5
0
3
10
7
5
Stage
W
VW
w
Δ(
w)
Δ
(1)
Δ
(2)
Δ
(3)
Δ
(4)
Δ
(5)
Δ
(6)
Start
{1}
{2,3,4,5,6}


0
3
5
2
{1,2}
{3,4,5,6}23031053{1,2,6}{3,4,5}6503107
5Data Structures and Programming Techniques172
1
6
5
4
3
10
3
7
5
6
7
2
8
1
5
Slide18Expanding the Vertex Set W in Stages (cont’d)
Stage
W
VW
w
Δ(
w)
Δ
(1)
Δ(2)Δ(3)Δ(4)Δ(5)Δ(6)Start{1}{2,3,4,5,6}0352{1,2}{3,4,5,6}
230310
5
3
{1,2,6}
{3,4,5}
6
5
0
3
10
7
5
Stage
W
VW
w
Δ(
w)
Δ
(1)
Δ
(2)
Δ
(3)
Δ
(4)
Δ
(5)
Δ
(6)
Start
{1}
{2,3,4,5,6}


0
3
5
2
{1,2}
{3,4,5,6}23031053{1,2,6}{3,4,5}6503107
5Data Structures and Programming Techniques18W=4 is chosen for the fourth stage.
2
1
6
5
4
3
10
3
7
5
6
7
2
8
1
5
Slide19Expanding the Vertex Set W in Stages (cont’d)
Stage
W
VW
w
Δ(
w)
Δ
(1)
Δ(2)Δ(3)Δ(4)Δ(5)Δ(6)Start{1}{2,3,4,5,6}0352{1,2}{3,4,5,6}
230310
5
3
{1,2,6}
{3,4,5}
6
5
0
3
10
7
5
4
{1,2,6,4}
{3,5}
4
7
0
3
10
7
13
5
Stage
W
VW
w
Δ(
w)
Δ
(1)
Δ
(2)
Δ
(3)
Δ
(4)
Δ
(5)
Δ
(6)Start{1}{2,3,4,5,6}0352{1,2}{3,4,5,6}230310
53{1,2,6}{3,4,5}650310754{1,2,6,4}{3,5}4703107135Data Structures and Programming Techniques19
2
1
6
5
4
3
10
3
7
5
6
7
2
8
1
5
Slide20Expanding the Vertex Set W in Stages (cont’d)
Stage
W
VW
w
Δ(
w)
Δ
(1)
Δ(2)Δ(3)Δ(4)Δ(5)Δ(6)Start{1}{2,3,4,5,6}0352{1,2}{3,4,5,6}
230310
5
3
{1,2,6}
{3,4,5}
6
5
0
3
10
7
5
4
{1,2,6,4}
{3,5}
4
7
0
3
10
7
13
5
Stage
W
VW
w
Δ(
w)
Δ
(1)
Δ
(2)
Δ
(3)
Δ
(4)
Δ
(5)
Δ
(6)Start{1}{2,3,4,5,6}0352{1,2}{3,4,5,6}230310
53{1,2,6}{3,4,5}650310754{1,2,6,4}{3,5}4703107135Data Structures and Programming Techniques20W=3 is chosen for the fifth stage.
2
1
6
5
4
3
10
3
7
5
6
7
2
8
1
5
Slide21Expanding the Vertex Set W in Stages (cont’d)
Stage
W
VW
w
Δ(
w)
Δ
(1)
Δ(2)Δ(3)Δ(4)Δ(5)Δ(6)Start{1}{2,3,4,5,6}0352{1,2}{3,4,5,6}
230310
5
3
{1,2,6}
{3,4,5}
6
5
0
3
10
7
5
4
{1,2,6,4}
{3,5}
4
7
0
3
10
7
13
5
5
{1,2,6,4,3}
{5}
3
10
0
3
10
7
11
5
Stage
W
VW
w
Δ(
w)
Δ(1)Δ(2)Δ(3)Δ(4)Δ(5)Δ(6)Start{1}{2,3,4,5,6}03
52{1,2}{3,4,5,6}23031053{1,2,6}{3,4,5}650310754{1,2,6,4}{3,5}47031071355{1,2,6,4,3}{5}31003107115Data Structures and Programming Techniques21
2
1
6
5
4
3
10
3
7
5
6
7
2
8
1
5
Slide22Expanding the Vertex Set W in Stages (cont’d)
Stage
W
VW
w
Δ(
w)
Δ
(1)
Δ(2)Δ(3)Δ(4)Δ(5)Δ(6)Start{1}{2,3,4,5,6}0352{1,2}{3,4,5,6}
230310
5
3
{1,2,6}
{3,4,5}
6
5
0
3
10
7
5
4
{1,2,6,4}
{3,5}
4
7
0
3
10
7
13
5
5
{1,2,6,4,3}
{5}
3
10
0
3
10
7
11
5
Stage
W
VW
w
Δ(
w)
Δ(1)Δ(2)Δ(3)Δ(4)Δ(5)Δ(6)Start{1}{2,3,4,5,6}03
52{1,2}{3,4,5,6}23031053{1,2,6}{3,4,5}650310754{1,2,6,4}{3,5}47031071355{1,2,6,4,3}{5}31003107115Data Structures and Programming Techniques22W=5 is chosen for the sixth stage.
2
1
6
5
4
3
10
3
7
5
6
7
2
8
1
5
Slide23Expanding the Vertex Set W in Stages (cont’d)
Stage
W
VW
w
Δ(
w)
Δ
(1)
Δ(2)Δ(3)Δ(4)Δ(5)Δ(6)Start{1}{2,3,4,5,6}0352{1,2}{3,4,5,6}
230310
5
3
{1,2,6}
{3,4,5}
6
5
0
3
10
7
5
4
{1,2,6,4}
{3,5}
4
7
0
3
10
7
13
5
5
{1,2,6,4,3}
{5}
3
10
0
3
10
7
11
5
6
{1,2,6,4,3,5}
{}
5
11
0
3107115StageWVWwΔ(w)Δ(1)Δ(2)Δ(3)Δ(4)
Δ(5)Δ(6)Start{1}{2,3,4,5,6}0352{1,2}{3,4,5,6}23031053{1,2,6}{3,4,5}650310754{1,2,6,4}{3,5}47031071355{1,2,6,4,3}{5}31003107
11
5
6
{1,2,6,4,3,5}
{}
5
11
0
3
10
7
11
5
Data Structures and Programming Techniques
23
2
1
6
5
4
3
10
3
7
5
6
7
2
8
1
5
Slide24Dijkstra’s Algorithm in Pseudocode
void
ShortestPath
(void)
{
Let
MinDistance
be a variable that takes edge
weights as values.
Let Minimum(x,y) be a function whose value is the lesser of x and y. /* Let s in V be the source vertex at which the shortest paths starts. */ /* Initialize W and ShortestDistance[u] as follows: */ W={s}; ShortestDistance[s]=0; for (each u in V{s}) ShortestDistance[u]=T[s][u]; Data Structures and Programming Techniques24
Slide25Dijkstra’s Algorithm (cont’d)
/*Now repeatedly enlarge W until W includes all vertices in V */
while (W!=V){
/* find the vertex w in VW at the minimum distance from s */
MinDistance
=
for (each v in VW){
if (ShortestDistance[v] < MinDistance){ MinDistance=ShortestDistance[v]; w=v; } } /* add w to W */ W=W {w}; /* relaxation step: update the shortest distance to vertices in VW */ for (each u in VW){ ShortestDistance[u]=Minimum(ShortestDistance
[u],
ShortestDistance[w]+T[w][u]);
}
}
}
Data Structures and Programming Techniques
25
Slide26Proof of Correctness
We will first prove that when
is selected,
gives us the length of the shortest path from the source to
.
We will also prove that, at each stage, after
is enlarged by the addition of
and shortest distances updated,
gives the distance of the shortest path from
to every vertex in via intermediaries lying wholly in . Data Structures and Programming Techniques26
Slide27Proof (cont’d)
So first consider when we are ready to enlarge the vertex set
by choosing a new vertex
to add to it.
We will prove that
gives us the length of the shortest path from
to
.
Data Structures and Programming Techniques27
Slide28Proof (cont’d)
Let us assume that this is not the case i.e.,
is
not
the length of the shortest path from
to
.
Then, there must exist some shorter path
, which starts at
and contains a vertex in other than We can start at the source and proceed along path , passing through vertices in , until we come to the first vertex , that is not in .Now notice that the length of the initial portion of the path from to is shorter than the length of the entire path from to . Data Structures and Programming Techniques28
Slide29Hypothetical Shorter Path to
Data Structures and Programming Techniques
29
Proof (cont’d)
Since we assumed that the length of path
was shorter than
, the length of the path from
to
is shorter than
also.
Moreover, the path from
to
has all its vertices except for lying in .Thus we would have when was chosen as the next vertex to add to .But this contradicts the choice of and would have meant that we would have chosen instead.Since we reached a contradiction, is the length of the shortest path from to . Data Structures and Programming Techniques
30
Slide31Proof (cont’d)
Now we need to verify that
gives the shortest distance from
to every vertex
in
traveling via intermediaries in
after the new vertex
has been added to
.
Observe that when we add a new vertex to , we adjust the shortest distances to take into account of the possibility that there is now a shorter path to going through .If that path goes through the old to and then immediately to , its length will be compared with and will be reduced if the new path is shorter.The only other possibility for a shorter path is shown on the next slide where the path travels to , then back into the old , to some member of the old , then to . Data Structures and Programming Techniques
31
Slide32Impossible Shortest Path
Data Structures and Programming Techniques
32
Proof (cont’d)
But there really cannot be such a path. Since
was placed in
before
, the shortest of all paths from the source to
runs through the old
alone. Therefore, the path to
through
shown on the figure is no shorter than the path directly to
through . As a result, the length of the path from the source to and is no less from the old value of . Thus, cannot be reduced by the algorithm due to a path through and , and we need not consider the length of such paths. Data Structures and Programming Techniques33
Slide34Time Complexity
If we use an adjacency matrix to represent the digraph,
Dijkstra’s algorithm runs in
time
where
is the number of vertices of the graph.
The initialization stage runs through
vertices and takes time
The whileloop runs through the
vertices of one at a time, and for each such vertex, the selection of the new vertex at minimum distance, as well as the updating of the distances takes time proportional to the number of vertices in . Therefore, the loop takes time. Data Structures and Programming Techniques34
Slide35Time Complexity (cont’d)
If
is much less than
it is better to use the
adjacency list
representation of the graph and a
priority queue
to organize the vertices in
.Then, the updating of the array can be done by going down the adjacency list of and updating the distances in the priority queue. A total of updates will be made, each at cost if the priority queue is implemented as a heap, so the total time for updates is . Data Structures and Programming Techniques35
Slide36Time Complexity (cont’d)
The time to initialize the priority queue is
The time needed to select
is
since it involves finding and removing the
minimum element in a heap.
Thus, the total time of the algorithm is
which is considerably better than
for sparse graphs. Data Structures and Programming Techniques36
Slide37The AllPairs Shortest Path Problem
Suppose we have a weighted digraph that gives the flying time on certain routes containing cities, and we wish to construct a table that gives the shortest time required to fly from any one city to any other.This is an instance of the
allpairs shortest path problem.
Data Structures and Programming Techniques
37
Slide38The AllPairs Shortest Path Problem (cont’d)
More formally, let
be a weighted directed graph in which each edge (
has a nonnegative weight
The
allpairs shortest path problem
is to find for each pair of vertices
, the shortest path from
to
We could solve this problem by running Dijkstra’s algorithm with each vertex in turn as a source.We will present a more direct way of solving the problem due to R. W. Floyd. Data Structures and Programming Techniques38
Slide39Floyd’s Algorithm
Let us assume that vertices in
are numbered with
. The algorithm uses an
matrix
in which to compute the lengths of the shortest paths.
We initially set
for all
If there is no edge from
to , we assume Each diagonal element of is set to 0. Data Structures and Programming Techniques39
Slide40Floyd’s Algorithm (cont’d)
The algorithm makes
iterations over the matrix
After the

th
iteration
,
will have as value the smallest length of any path from vertex
to vertex that does not pass through a vertex numbered higher than .In the th iteration, we use the following formulas to compute
Data Structures and Programming Techniques
40
Slide41The
th Iteration Graphically
Data Structures and Programming Techniques
41
Floyd’s Algorithm (cont’d)
void APSP(void)
{
int
i,j,k
;
int A[MAX][MAX], C[MAX][MAX]; for (i=0; i<=MAX1; i++) for (j=0; j<=MAX1; j++) A[i][j]=C[i][j]; for (i=0; i<=MAX1; i++) A[i][i]=0; for (k=0; k<=MAX1; k++) for (
i=0; i
<=MAX1; i
++)
for (j=0; j<=MAX1; j++)
if (A[
i
][k]+A[k][j] < A[
i
][j])
A[
i
][j]=A[
i
][k]+A[k][j];
}
Data Structures and Programming Techniques
42
Slide43Time Complexity
The running time of Floyd’s algorithm is
where
is the number of vertices.
Data Structures and Programming Techniques
43
Slide44Transitive Closure (Μεταβατική Κλειστότητα)
In some problems we may be interested in determining only
whether there exists a path of length one or more from vertex
to vertex
of directed graph
The algorithm for this problem is a modification of Floyd’s algorithm, which historically predates Floyd’s algorithm, called
Warshall’s
algorithm.
Data Structures and Programming Techniques44
Slide45Transitive Closure (cont’d)
Suppose our weight matrix
is just the adjacency matrix of graph
. That is,
if there is an edge from
to
, and 0 otherwise.
We wish to compute the matrix
such that
if there is a path of length one or more from to , and 0 otherwise. is often called the transitive closure of the adjacency matrix. Data Structures and Programming Techniques45
Slide46Transitive Closure (cont’d)
The transitive closure can be computed using a procedure similar to the one we used for the allpairs shortest path problem.
We apply the following formula in the

th
pass over the Boolean matrix
The formula states that there is a path from
to
not passing through a vertex numbered higher than
ifthere is already a path from to
not passing through a vertex number higher than orthere is a path from
to
not passing through a vertex numbered higher than
and a path from
to
not passing through a vertex numbered higher than
Data Structures and Programming Techniques
46
Slide47Transitive Closure (cont’d)
void
TransitiveClosure(void)
{
int
i,j,k
;
int A[MAX][MAX], C[MAX][MAX]; for (i=0; i<=MAX1; i++) for (j=0; j<=MAX1; j++) A[i][j]=C[i][j]; for (k=0; k<=MAX1; k++) for (i=0; i<=MAX1; i++) for (j=0; j<=MAX1; j++) if (!A[i][j]) A[i
][j]=A[i][k] && A[k
][j];
}
Data Structures and Programming Techniques
47
Slide48Time Complexity
The running time of Warshall’s algorithm is
where
is the number of vertices.
Data Structures and Programming Techniques
48
Slide49Readings
T. A. Standish. Data Structures , Algorithms and Software Principles in C.Chapter 10
A. V. Aho, J. E. Hopcroft and J. D. Ullman.
Data Structures and Algorithms.
Chapters 6 and 7
Data Structures and Programming Techniques
49
Slide50