0% found this document useful (0 votes)
53 views10 pages

T22 WeightedGraphs

The document discusses weighted graphs and algorithms for finding shortest paths and minimum spanning trees in weighted graphs. It describes Dijkstra's single-source shortest path algorithm and Prim's minimum spanning tree algorithm, providing examples of their application to a sample weighted graph.

Uploaded by

Naveen Jafer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views10 pages

T22 WeightedGraphs

The document discusses weighted graphs and algorithms for finding shortest paths and minimum spanning trees in weighted graphs. It describes Dijkstra's single-source shortest path algorithm and Prim's minimum spanning tree algorithm, providing examples of their application to a sample weighted graph.

Uploaded by

Naveen Jafer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Weighted Graphs

Data Structures & Algorithms


1
CS@VT 2000-2009 McQuain
Weighted Graphs
In many applications, each edge of a
graph has an associated numerical
value, called a weight.
Usually, the edge weights are non-
negative integers.
Weighted graphs may be either
directed or undirected.
a
i
g
f
e
d
c
b
h
25
15
10
5
10
20
15
5
25
10
The weight of an edge is often referred to as the "cost" of the edge.
In applications, the weight may be a measure of the length of a route, the
capacity of a line, the energy required to move between locations along a
route, etc.
Weighted Graphs
Data Structures & Algorithms
2
CS@VT 2000-2009 McQuain
Shortest Paths (SSAD)
Given a weighted graph, and a
designated node S, we would like to
find a path of least total weight from
S to each of the other vertices in the
graph.
The total weight of a path is the sum
of the weights of its edges.
a
i
g
f
e
d
c
b
h
25
15
10
5
10
20
15
5
25
10
We have seen that performing a DFS or BFS on the graph will produce a
spanning tree, but neither of those algorithms takes edge weights into account.
There is a simple, greedy algorithm that will solve this problem.
Weighted Graphs
Data Structures & Algorithms
3
CS@VT 2000-2009 McQuain
Dijkstra's SSAD Algorithm*
We assume that there is a path from
the source vertex s to every other
vertex in the graph.
Let S be the set of vertices whose
minimum distance from the source
vertex has been found. Initially S
contains only the source vertex.
The algorithm is iterative, adding one
vertex to S on each pass.
a
i
g
f
e
d
c
b
h
25
15
10
5
10
20
15
5
25
10
*1959
We maintain a table D such that for each vertex v, D(v) is the minimum
distance from the source vertex to v via vertices that are already in S (aside
possibly from v itself).
Greed: on each iteration, add to S the vertex v not already in S for which
D(v) is minimal.
Weighted Graphs
Data Structures & Algorithms
4
CS@VT 2000-2009 McQuain
Dijkstra's Algorithm Trace
Let the source vertex be a.
a
i
g
f
e
d
c
b
h
25
15
10
5
10
20
15
5
25
10
S = {a}

25 15 0
i h g f e d c b a
D
S = {a,b}
40 20

25

25 15 0
i h g f e d c b a
D
Weighted Graphs
Data Structures & Algorithms
5
CS@VT 2000-2009 McQuain
Dijkstra's Algorithm Trace
35 20

25 35 25 15 0
i h g f e d c b a
Continuing:
S = {a, b, h}
D
S = {a, b, h, c}
D
S = {a, b, h, c, e}
D
S = {a, b, h, c, e, g}
D
S = {a, b, h, c, e, g, f}
D
35 20

25

25 15 0
i h g f e d c b a
35 20 30 35 25 35 25 15 0
i h g f e d c b a
35 20 30 35 25 35 25 15 0
i h g f e d c b a
35 20 30 35 25 35 25 15 0
i h g f e d c b a
Weighted Graphs
Data Structures & Algorithms
6
CS@VT 2000-2009 McQuain
Dijkstra's Algorithm Trace
Continuing:
S = {a, b, h, c, e, g, f, d}
D
S = {a, b, h, c, e, g, f, d, i}
D
a
i
g
f
e
d
c
b
h
25
15
10
5
10
20
15
5
25
10
The corresponding tree is shown
at left. As described, the
algorithm does not maintain a
record of the edges that were used,
but that can easily be remedied.
35 20 30 35 25 35 25 15 0
i h g f e d c b a
35 20 30 35 25 35 25 15 0
i h g f e d c b a
Weighted Graphs
Data Structures & Algorithms
7
CS@VT 2000-2009 McQuain
Limitations
Dijkstra'sSSAD Algorithm only works for graphs with non-negative weights.
See the Bellman-Ford Algorithm, which works even if the weights are negative,
provided there is no negative cycle (a cycle whose total weight is negative).
Weighted Graphs
Data Structures & Algorithms
8
CS@VT 2000-2009 McQuain
Minimal Spanning Tree
Given a weighted graph, we would
like to find a spanning tree for the
graph that has minimal total weight.
The total weight of a spanning tree is
the sum of the weights of its edges.
We want to find a spanning tree T,
such that if T' is any other spanning
tree for the graph then the total
weight of T is less than or equal to
that of T'.
a
i
g
f
e
d
c
b
h
25
15
10
5
10
20
15
5
25
10
Weighted Graphs
Data Structures & Algorithms
9
CS@VT 2000-2009 McQuain
J arnik-Prim MST Algorithm
By modifying DijkstrasSSAD Algorithm to build a list of the edges that are
used as vertices are added, and storing the distance from nodes to the current
tree (rather than from nodes to the source) we obtain Prims Algorithm (V
J arnik, 1930 and R C Prim, 1957).
It turns out that this algorithm does, in fact, create a spanning tree of minimal
weight if the graph to which it is applied is connected.
Since the complex steps in Prims algorithm are the same as Dijkstras, no
detailed example is traced here.
QTP: why does Dijkstra's SSAD Algorithm not necessarily
find a minimum-weight spanning tree?
Weighted Graphs
Data Structures & Algorithms
10
CS@VT 2000-2009 McQuain
J arnik-Prim MST Algorithm Trace
a
i
g
f
e
d
c
b
h
25
15
10
5
10
20
15
5
25
10
A B C D E F G H I
----------------------------------
0 15 25 inf inf inf inf inf inf
A 25 inf 10 inf inf 5 25
25 inf 10 inf inf B 15
20 inf B 10 5 15
20 inf 10 E 15
20 inf E 15
20 inf H
E 10
C
a
i
g
f
e
d
c
b
h
25
15
10
5
10
20
15
5
25
10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy