Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Algorithms1 Graphs 3 Adapted From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University:

Similar presentations


Presentation on theme: "Data Structures and Algorithms1 Graphs 3 Adapted From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University:"— Presentation transcript:

1 Data Structures and Algorithms1 Graphs 3 Adapted From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University: Harper Collins Publishers)

2 Data Structures and Algorithms2 Analysis of Dijkstra’s Least Cost Paths Algorithm

3 Data Structures and Algorithms3 Given directed graph G and start vertex S Part 1. make an empty set U for each vertex v in G do dist(v) = infinity insert v into U dist(S) = 0

4 Data Structures and Algorithms4 Part 2. repeat |G| times v = any member of U with minimum distance delete v from U for each neighbor w of v do if w is a member of U do dist(w) = min(dist(w), dist(v) + cost(v,w))

5 Data Structures and Algorithms5 Two Implementatations Adjacency Matrix a matrix is used to hold the costs an array of boolean implements the set U an array of ints holds the distance values Adjacency list An array of lists holds the graph Use a heap to hold the set U

6 Data Structures and Algorithms6 Two Implementatations Adjacency Matrix search costs  (n) each iteration of the main loop consists of one search and at most n-1 updates of distance fields the total time is  (n 2 ) Adjacency list An array of lists holds the graph Use a heap to hold the set U

7 Data Structures and Algorithms7 Adjacency list time to initialize U  (n) n deletemin operations O(n * lg n) modify distance in heap for every edge in the graph O(e Lg n) Overall : O((n + e) lg n)

8 Data Structures and Algorithms8 Which is better? For dense graphs e is about n 2 which makes the adjacency matrix representation better. For sparse graphs, e is small so choose the heap implementation.


Download ppt "Data Structures and Algorithms1 Graphs 3 Adapted From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University:"

Similar presentations


Ads by Google