design5sl

design5sl - AMCS 260/CS 260 Design and Analysis of...

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

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

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

View Full Document
Greedy Algorithms Greedy algorithms are algorithms that use the following meta-heuristic: 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 Decrease-key . Accommodate the decrease in key value of an element. I Delete-min . Return the element with the smallest key, and remove it from the set. I Make-queue . Build a priority queue for the given elements, with the given key values.

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

View Full Document
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.

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

View Full Document
Binary Heaps The Decrease-key is similar to Insert with the only difference that the element is already in the tree. The Make-queue takes at most n Insert operations.
Binary Heaps To Delete-min 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 .

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

View Full Document
Binary Heaps Thus, Insert , Decrease-key and Delete-min require O (log n ) time, and Make-queue 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 .

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

View Full Document
Dijkstra’s Algorithm Let G = ( V , E ) be a directed graph in which each edge e has a length l e 0 . For a path P , the length of P (denoted by l ( P ) ) is the sum of lengths of all edges in P .
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern