Shortest Path Problems• Single source single destination.. Single Source Single Destination Possible greedy algorithm: – Leave source vertex using cheapest/shortest edge.. – Leave new v
Trang 1Course Notes Set:
Graphs 2
COMP 2210
Dr HendrixComputer Science and Software Engineering
Auburn University
Trang 4Spanning Tree
• Subgraph that includes all vertices
of the original graph.
• Subgraph is a tree.
– If original graph has n vertices, the
spanning tree has n vertices and n-1
edges
Trang 52
Trang 6Spanning Tree
• Start a breadth-first search at any
vertex of the graph.
• If graph is connected, the n-1
edges used to get to unvisited
vertices define a spanning tree.
• Time
– O(n^2) when adjacency matrix used
– O(n+e) when adjacency lists used (e
is number of edges)
Trang 9Minimum Cost Spanning
2
Trang 10Minimum Cost Spanning
2
Trang 11Minimum-Cost Spanning
Tree
• Network has 10 edges
• Spanning tree has only n - 1 = 7 edges
• Need to either select 7 edges or discard
9
Trang 12Possible Greedy Strategies
• Start with an n vertex forest Consider
edges in ascending order of cost Select
edge if it does not form a cycle together
with already selected edges
– Kruskal’s method.
• Start with a 1 vertex tree and grow it
into an n vertex tree by repeatedly
adding a vertex and an edge When
there is a choice, add a least cost edge
– Prim’s method.
Trang 13• Pick an arbitrary starting vertex.
• Add the incident edge with minimum weight to
the spanning tree provided it doesn’t create a
cycle.
• Repeat until there are no more edges to
consider or until n-1 edges have been added.
Trang 16Kruskal’s Method
• Edge (2,4) is considered next and rejected
because it creates a cycle.
• Edge (3,6) is considered next and rejected.
7
• Edge (5,7) is considered next and added
14
Trang 17• Min-cost spanning tree is unique when
all edge costs are different
14
Trang 18Shortest Path Problems
• Directed weighted graph.
• Path length is sum of weights of
edges on path.
• The vertex at which the path begins
is the source vertex.
• The vertex at which the path ends is
the destination vertex.
Trang 21Shortest Path Problems
• Single source single
destination.
• Single source all
destinations.
• All pairs (every vertex is a
source and destination).
Trang 22Single Source Single
Destination
Possible greedy algorithm:
– Leave source vertex using
cheapest/shortest edge
– Leave new vertex using cheapest edge
subject to the constraint that a new
vertex is reached
– Continue until destination is reached
Trang 24Single Source All
Destinations
Need to generate n (n is number of vertices)
paths (including path from source to itself)
Greedy method:
– Select destinations in increasing order of length
of shortest path.
– Assume edge costs (lengths) are >= 0.
– So, no path has length < 0.
– First shortest path is from source vertex to itself The length of this path is 0.
Trang 25Greedy Single Source All Destinations
Trang 26Greedy Single Source All
•Next shortest path is the shortest one edge extension of an already generated shortest path
Trang 27Greedy Single Source All
Destinations
• Let d(i) (distanceFromSource(i))be the
length of a shortest one edge extension of
an already generated shortest path, the one edge extension ends at vertex i
• The next shortest path is to an as yet
unreached vertex for which the d() value is least
• Let p(i) (predecessor(i)) be the vertex just
before vertex i on the shortest one edge
extension to i
Trang 28Greedy Single Source All
p
0-
21
161
-
-
-1412
Trang 29Greedy Single Source All
p
1
0-
61
21
161
-
-
-141
1035
Trang 30Greedy Single Source All
p
1
0-
61
21
161
-
-
-141
53
103
956
Trang 31Greedy Single Source All
p
1
0-
61
21
95
-
-
-141
53
103
4
9
Trang 32Greedy Single Source All
p
1
0-
61
21
95
-
-
-141
53
103
7
12410
Trang 33Greedy Single Source All
p
0-
61
21
95
-
-
-141
53
103
124
7
116
Trang 34Greedy Single Source All
-61
21
95
-
-
-141
53
103
124116
Trang 35Single Source Single
Destination
Terminate single source all
destinations greedy algorithm as
soon as shortest path to desired
vertex has been generated.
Trang 36Data Structures For Dijkstra’s
Algorithm
• The greedy single source all destinations
algorithm is known as Dijkstra’s algorithm
• Implement d() and p() as 1D arrays
• Keep a chain c of reachable vertices to
which shortest path is yet to be generated
• Select and remove vertex v in c that has
smallest d() value
• Update d() and p() values of vertices
adjacent to v
Trang 37• O(n) to select next destination vertex
• O(out-degree) to update d() and p()
values when adjacency lists are used
• O(n) to update d() and p() values when
adjacency matrix is used
• Selection and update done once for
each vertex to which a shortest path is
found
• Total time is O(n2 + e) = O(n2)
Trang 38• When a min heap of d() values is used
in place of the chain c of reachable
vertices, total time is O((n+e) log n),
because O(n) remove min operations
and O(e) change key (d() value)
operations are done
• When e is O(n2), using a min heap is
worse than using a chain
• When a Fibonacci heap is used, the total