02-greedy

# Insertw wtvw parentw v 31 else if w is fringe

This preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: ks at every edge, so omega(n+m) Is there more? Yes, priority queue operations ExtractMin called n times descreaseKey could be called for each edge 32 How expensive? Depends on the size of the PQ How expensive is each call? Worst Case If all nodes connected to start, then size of PQ is n-1 right away. Decreases by 1 for each node selected Total cost is O(cost of extractMin for size n-1) Note use of Big-Oh (not Big-Theta) Could descreaseKey be called a lot? 33 Yes! Imagine an input that adds all nodes to the PQ at the first step, and then after that calls descreaseKey every possible time. (For you to do.) Priority Queue Costs and Prim’s Simplest choice: unordered list PQ.ExtractMin() is just a “findMin” Cost for one call is (n) Total cost for all n calls is (n2) PQ.decreaseKey() on a node finds it, changes it. Cost for one call is (n) But, if we can index an array by vertex number, the cost would be (1). If so, worst-case total cost is (m) Conclusion: Easy to get (n2) 34 Better PQ Implement...
View Full Document

## This note was uploaded on 02/25/2014 for the course CS 4102 taught by Professor Horton during the Spring '10 term at UVA.

Ask a homework question - tutors are online