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

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

View Full Document Right Arrow Icon
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
Image of page 1

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

View Full Document Right Arrow Icon
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.
Image of page 2
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.
Image of page 3

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

View Full Document Right Arrow Icon
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.
Image of page 4
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.
Image of page 5

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

View Full Document Right Arrow Icon
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.
Image of page 6
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 .
Image of page 7

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

View Full Document Right Arrow Icon
Binary Heaps Thus, Insert , Decrease-key and Delete-min require O (log n ) time, and Make-queue takes O ( n log n ) time.
Image of page 8
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 .
Image of page 9

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

View Full Document Right Arrow Icon
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 .
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern