COP 3530: Computer Science III - PowerPoint PPT Presentation

About This Presentation
Title:

COP 3530: Computer Science III

Description:

Rather than defining one vertex as a starting vertex, suppose that we would like ... Q new priority queue. Let s be any vertex. for all v G.vertices() if v = s ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 45
Provided by: marklle
Learn more at: http://www.cs.ucf.edu
Category:

less

Transcript and Presenter's Notes

Title: COP 3530: Computer Science III


1
COP 3530 Computer Science III Summer
2005 Graphs and Graph Algorithms Part 5
Instructor Mark Llewellyn
markl_at_cs.ucf.edu CSB 242, 823-2790 http//ww
w.cs.ucf.edu/courses/cop3530/summer05
School of Computer Science University of Central
Florida
2
All-Pairs Shortest Paths
  • Dijkstras algorithm was a single source shortest
    path algorithm.
  • Rather than defining one vertex as a starting
    vertex, suppose that we would like to find the
    distance between every pair of vertices in a
    weighted graph G. In other words, the shortest
    path from every vertex to every other vertex in
    the graph.
  • One option would be to run Dijkstras algorithm
    in a loop considering each vertex once as the
    starting point. A better option would be to use
    the Floyd-Warshall dynamic programming algorithm
    (typically referred to as Floyds algorithm).

3
  • Dijkstras Algorithm Single Source Shortest
    Path
  • Label Path length to each vertex as ? (or 0 for
    the start vertex)
  • Loop while there is a vertex
  • Remove up the vertex with minimum path length
  • Check and if required update the path length of
    its adjacent neighbors
  • end loop

Update rule Let a be the vertex removed
and b be its adjacent vertex Let e be
the edge connecting a to b. if
(a.pathLength e.weight lt b.pathLength) b.pathLe
ngth?a.PathLength e.weight b.parent ? a
4
  • DAG based Algorithm - Single Source Shortest
    Path
  • May have negative weight
  • Label Path length to each vertex as ? (or 0 for
    the start vertex)
  • Compute Topological Ordering
  • loop for i ? 1 to n-1
  • Check and if required update the path length of
    adjacent neighbors of vi
  • end loop

-2
D
C
3
-5
8
E
5
6
9
B
2
A
5
All-Pairs Shortest Paths
6
All-Pairs Shortest Paths
  • Dynamic Programming approach
  • Initialize a n?n table with 0 or ? or edge
    length.
  • Check and Update the table bottom up in nested
    loops
  • for k,
  • for i,
  • for j

j?
i?
B
7
Minimum Spanning Tree
  • Spanning subgraph
  • Subgraph of a graph G containing all the vertices
    of G
  • Spanning tree
  • Spanning subgraph that is itself a (free) tree
  • Minimum spanning tree (MST)
  • Spanning tree of a weighted graph with minimum
    total edge weight
  • Applications
  • Communications networks
  • Transportation networks

ORD
10
1
PIT
DEN
6
7
9
3
DCA
STL
4
5
8
2
DFW
ATL
8
Cycle Property
  • Cycle Property
  • Let T be a minimum spanning tree of a weighted
    graph G
  • Let e be an edge of G that is not in T
  • Let C let be the cycle formed by e with T
  • For every edge f of C, weight(f) ? weight(e)
  • Proof
  • By contradiction
  • If weight(f) gt weight(e) we can get a spanning
    tree of smaller weight by replacing e with f

9
Partition Property
  • Partition Property
  • Consider a partition of the vertices of G into
    subsets U and V
  • Let e be an edge of minimum weight across the
    partition
  • There is a minimum spanning tree of G containing
    edge e
  • Proof
  • Let T be an MST of G
  • If T does not contain e, consider the cycle C
    formed by e with T and let f be an edge of C
    across the partition
  • By the cycle property, weight(f) ? weight(e)
  • Thus, weight(f) weight(e)
  • We obtain another MST by replacing f with e

10
Minimum Spanning Tree
  • Prims Algorithm (Prim-Jarnik Algorithm)
  • Label cost of each vertex as ? (or 0 for the
    start vertex)
  • Loop while there is a vertex
  • Remove a vertex that will extend the tree with
    minimum additional cost
  • Check and if required update the path length of
    its adjacent neighbors (Update rule different
    from Dijkstras algorithm)
  • end loop

Update rule Let a be the vertex removed
and b be its adjacent vertex Let e be
the edge connecting a to b. if (e.weight lt
b.cost) b.cost?e.weight b.parent ? a
11
MST Prim-Jarniks Algorithm
?
6
D
C
?
1
6
8
E
6
2
?
7
2
B
A
?
0
12
MST Prim-Jarniks Algorithm
?
6
D
C
1
2,A
6
8
E
6
2
7,A
7
2
B
A
2,A
0
13
MST Prim-Jarniks Algorithm
?
6
D
C
1
2,A
6
8
E
6
2
7,A
7
2
B
A
2,A
0
14
MST Prim-Jarniks Algorithm
6
D
8,B
C
1
2,A
6
8
E
6
2
6,B
7
2
B
A
2,A
0
15
MST Prim-Jarniks Algorithm
6
D
8,B
C
1
2,A
6
8
E
6
2
6,B
7
2
B
A
2,A
0
16
MST Prim-Jarniks Algorithm
17
MST Prim-Jarniks Algorithm
6
D
6,D
C
1
2,A
6
8
E
6
2
1,D
7
2
B
A
2,A
0
18
MST Prim-Jarniks Algorithm
19
MST Prim-Jarniks Algorithm
6
D
6,D
C
1
2,A
6
8
E
6
2
1,D
7
2
B
A
2,A
0
20
Prim-Jarnik Algorithm
  • Algorithm MST (G)
  • Q ? new priority queue
  • Let s be any vertex
  • for all v ? G.vertices()
  • if v s
  • v.cost ? 0
  • else v.cost ? ?
  • v. parent ? null
  • Q.enQueue(v.cost, v)
  • while ?Q.isEmpty()
  • v ? Q .removeMin()
  • v.pathKnown ? true
  • for all e ? G.incidentEdges(v)
  • w ? opposite(v,e)
  • if ?w.pathKnown
  • if weight(e) lt w.cost
  • w.cost ? weight(e)
  • w. parent ? v
  • update key of w in Q

O(n)
O(n log n)
O((nm) log n)
21
Example
22
Example (cont.)
23
Minimum Spanning Tree
  • Kruskals Algorithm
  • Create a forest of n trees
  • Loop while (there is gt 1 tree in the forest)
  • Remove an edge with minimum weight
  • Accept the edge only if it connects 2 trees from
    the forest in to one.
  • end loop

The forest
24
MST Kruskals Algorithm
25
MST Kruskals Algorithm
26
MST Kruskals Algorithm
E
D
C
B
A
27
MST Kruskals Algorithm
E
D
C
B
A
28
Kruskals Algorithm
Algorithm KruskalMST(G) let Q be a priority
queue. Insert all edges into Q using their
weights as the key Create a forest of n trees
where each vertex is a tree
numberOfTrees ? n while numberOfTrees gt 1do
edge e ? Q.removeMin() Let u, v be the
endpoints of e if Tree(v) ? Tree(u)
then Combine Tree(v) and Tree(u)
using edge e decrement numberOfTrees
return T
O(m log m)
O(m log m)
29
Kruskal Example
2704
BOS
867
849
PVD
ORD
187
740
144
JFK
1846
621
1258
184
802
SFO
BWI
1391
1464
337
1090
DFW
946
LAX
1235
1121
MIA
2342
30
Example
31
Example
32
Example
33
Example
34
Example
35
Example
36
Example
37
Example
38
Example
39
Example
40
Example
41
Example
42
Example
43
Minimum Spanning Tree
  • Baruvkas Algorithm
  • Create a forest of n trees
  • Loop while (there is gt 1 tree in the forest)
  • For each tree Ti in the forest
  • Find the smallest edge e (u,v), in the edge
    list with u in Ti and v in Tj ?Ti
  • connects 2 trees from the forest in to one.
  • end loop

B
44
Baruvkas Algorithm
  • Like Kruskals Algorithm, Baruvkas algorithm
    grows many clouds at once.
  • Each iteration of the while-loop halves the
    number of connected components in T.
  • The running time is O(m log n).

Algorithm BaruvkaMST(G) T ? V just the
vertices of G while T has fewer than n-1 edges
do for each connected component C in T
do Let edge e be the smallest-weight edge from
C to another component in T. if e is not
already in T then Add edge e to T return T
Write a Comment
User Comments (0)
About PowerShow.com