# Notes84 - Monday Last time we talked about Dijkstra's Algorithm The shortest path from a source vertex to all other vertices(We saw an example in

This preview shows pages 1–2. Sign up to view the full content.

Monday, April 25, 2005 Last time we talked about Dijkstra's Algorithm: The shortest path from a source vertex to all other vertices. (We saw an example in the last class.) Now, we will give pseudocode for Dijkstra's Shortest Path Algorithm Algorithm shortestPath(G, S, D) Input: weighted graph, G; source vertex, S. Output: Array D[ ] such that D[U] will be the length of the shortest path from S to U in G. Initialize D[S] = 0; Initialize D[U] = ∞; // For each U ≠ S Initialize Q = {all vertices in G}; // Q = list or queue; keeps track of unvisited vertices while(Q not empty) { Get a vertex U from Q such that D[U] is the minimum among all vertices in Q. For each vertex Z adjacent to U such that Z is in Q { if(D[U] + W(U,Z) < D[Z]) { D[Z] = D[U] + W(U,Z) } } } W(U,Z) is the weight of the edge from U to Z m = # edges n = # vertices Complexity: The for loop is O(m) independent of the while loop. The while loop is O(n 2 ) or O(n log n). getMin( ) takes O(n) if we are using a list or queue, O(log n) if we are using a heap. O(n 2 + O(m) = O(n 2 + m) = O(n 2 ) for a simple list or queue. O(n log n) + O(m) = O(n log n + m) for a heap Minimum Spanning Tree - Spanning tree with minimum total weight - Example: connect all computers in the building with minimum amount of cable. What is the minimum weight necessary to connect all the vertices? Minimum Spanning Tree Property

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/02/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

### Page1 / 8

Notes84 - Monday Last time we talked about Dijkstra's Algorithm The shortest path from a source vertex to all other vertices(We saw an example in

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online