This preview shows pages 1–11. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: AMCS 260/CS 260 Design and Analysis of Algorithms 5. Greedy Algorithms Mikhail Moshkov Division of Mathematical and Computer Sciences and Engineering King Abdullah University of Science and Technology Spring 2010 Greedy Algorithms Greedy algorithms are algorithms that use the following metaheuristic: to make the locally optimal choice at each stage with the hope of finding the global optimum. Binary Heaps To make algorithms considered in this section more efficient we will use a priority queue (implemented via binary heap) which maintains a set of elements (nodes) with associated numeric key values and supports the following operations: I Insert . Add a new element to the set. I Decreasekey . Accommodate the decrease in key value of an element. I Deletemin . Return the element with the smallest key, and remove it from the set. I Makequeue . Build a priority queue for the given elements, with the given key values. Binary Heaps In a binary heap elements are stored in a complete binary tree, namely, a binary tree in which each level is filled from the left to the right, and must be full before the next level is started. In addition, the key value of any node of the tree is less than or equal to key values of its children. In particular, the root of the tree always contains an element with the smallest key. Binary Heaps To Insert a new element, we should place it at the bottom of the tree (in the first available position). If the key of new element is less than the key of its parent, then we should swap these elements, etc. The number of swaps is at most the depth of the tree, which is b log 2 n c where n is the number of elements. Binary Heaps The Decreasekey is similar to Insert with the only difference that the element is already in the tree. The Makequeue takes at most n Insert operations. Binary Heaps To Deletemin we should return the element attached to the root. Then we should remove this element from the heap, take the element attached to the last node in the tree (in the rightmost position in the bottom level) and place it at the root. If the key of this element is bigger than for a child, we should swap these elements, etc. The number of swaps is at most b log 2 n c . Binary Heaps Thus, Insert , Decreasekey and Deletemin require O (log n ) time, and Makequeue takes O ( n log n ) time. Binary Heaps The considered binary tree can be easily represented as an array. The nodes of tree have a natural ordering: row by row, starting at the root and moving from the left to the right within each level. The array has positions 1 ,..., n corresponding to the considered nodes. One can show that the node number j has parent with number j j 2 k and children with numbers 2 j and 2 j + 1 . Dijkstra’s Algorithm Let G = ( V , E ) be a directed graph in which each edge e has a length l e ≥ . For a path P , the length of P (denoted by l ( P ) ) is the sum of lengths of all edges in P ....
View
Full
Document
This note was uploaded on 05/23/2010 for the course CS 245 taught by Professor Dunno during the Spring '10 term at Aarhus Universitet.
 Spring '10
 DUNNO
 Algorithms, Machine Learning

Click to edit the document details