CSC 380: Design and Analysis of Algorithms

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Heaps and heapsort COMP171 Fall Sorting III / Slide 2 Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. Sizes: Job.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
1 TCSS 342, Winter 2005 Lecture Notes Priority Queues and Heaps Weiss Ch. 21, pp
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Chapter 21 Binary Heap.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Dan Grossman Fall 2013.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
CSE 373: Data Structures and Algorithms Lecture 11: Priority Queues (Heaps) 1.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
Lecture on Data Structures(Trees). Prepared by, Jesmin Akhter, Lecturer, IIT,JU 2 Properties of Heaps ◈ Heaps are binary trees that are ordered.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
Heaps and Heap Sort. Sorting III / Slide 2 Background: Complete Binary Trees * A complete binary tree is the tree n Where a node can have 0 (for the leaves)
Sorting With Priority Queue In-place Extra O(N) space
Priority Queues A priority queue is an ADT where:
CSE373: Data Structures & Algorithms Priority Queues
CSE373: Data Structures & Algorithms
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues and Heaps Suggested reading from Weiss book: Ch. 21
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Hashing Exercises.
Source: Muangsin / Weiss
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
March 31 – Priority Queues and the heap
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Cinda Heeren / Geoffrey Tien
Heaps, Heap Sort, and Priority Queues
Chapter 8 – Binary Search Tree
Priority Queue & Heap CSCI 3110 Nan Chen.
Part-D1 Priority Queues
i206: Lecture 14: Heaps, Graphs intro.
CSE 373: Data Structures and Algorithms
CMSC 341 Lecture 14 Priority Queues & Heaps
B-Tree Insertions, Intro to Heaps
Heaps and the Heapsort Heaps and priority queues
Tree Representation Heap.
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Computer Science 2 Heaps.
CE 221 Data Structures and Algorithms
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Priority Queues & Heaps
CSC 380: Design and Analysis of Algorithms
CSE 373 Data Structures and Algorithms
CSE 373 Priority queue implementation; Intro to heaps
Priority Queues CSE 373 Data Structures.
Heaps & Multi-way Search Trees
Priority Queues Binary Heaps
Heaps Chapter 6 Section 6.9.
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

CSC 380: Design and Analysis of Algorithms Dr. Curry Guinn

Quick Info Dr. Curry Guinn CIS 2015 guinnc@uncw.edu www.uncw.edu/people/guinnc 962-7937 Office Hours: MWF: 10:00am-11:00m and by appointment

Outline of today Heaps (Priority Queues) Greedy Algorithms Shortest path algorithms Dijkstra

Priority Queue A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value (key) of each object (smaller value higher priority, or higher value higher priority) Example Applications: printer -> print (dequeue) the shortest document first operating system -> run (dequeue) the shortest job first

Priority Queue (Heap) Operations deleteMin Priority Queue insert insert (enqueue) deleteMin (dequeue) smaller value higher priority find, return, and remove the minimum element peek Look at the smallest value

Implementation using Linked List Unsorted linked list insert takes O(1) time deleteMin takes O(N) time Sorted linked list insert takes O(N) time deleteMin takes O(1) time

Heap properties heap: a tree with the following two properties: 1. completeness complete tree: every level is full except possibly the lowest level, which must be filled from left to right with no leaves to the right of a missing node (i.e., a node may not have any children until all of its possible siblings exist) Heap shape:

Heap properties 2 2. heap ordering a tree has heap ordering if P < X for every element X with parent P In other words, in heaps, parents' element values are always smaller than those of their children Implies that minimum element is always the root Is every a heap a BST? Are any heaps BSTs? Are any BSTs heaps? Answer: no, it is not a BST.

Which are min-heaps? wrong! 10 20 10 20 80 10 80 20 80 40 60 85 99 40 30 15 50 700 50 700 10 10 20 80 10 40 60 85 99 20 80 20 80 50 700 40 60 99 40 60

Heap height and runtime Height of a complete tree is always log n, because it is always balanced This suggests that searches, adds, and removes will have O(log n) worst-case runtime Because of this, if we implement a priority queue using a heap, we can provide the O(log n) runtime required for the add and remove operations n-node complete tree of height h: h = log n 2h  n  2h+1 - 1

Adding to a heap When an element is added to a heap, it should be initially placed as the rightmost leaf (to maintain the completeness property) heap ordering property becomes broken! 10 10 20 80 20 80 40 60 85 99 40 60 85 99 50 700 65 50 700 65 15

Adding to a heap, cont'd. To restore heap ordering property, the newly added element must be shifted upward ("bubbled up") until it reaches its proper place Bubble up (book: "percolate up") by swapping with parent How many bubble-ups could be necessary, at most? 10 10 20 80 15 80 40 60 85 99 40 20 85 99 50 700 65 15 50 700 65 60

Heap practice problem Draw the state of the min-heap tree after adding the following elements to it: 6, 50, 11, 25, 42, 20, 104, 76, 19, 55, 88, 2

The peek operation peek on a min-heap is trivial; because of the heap properties, the minimum element is always the root peek is O(1) 10 20 80 40 60 85 99 50 700 65

Removing from a min-heap Min-heaps only support remove of the min element (the root) Must remove the root while maintaining heap completeness and ordering properties Intuitively, the last leaf must disappear to keep it a heap Initially, just swap root with last leaf (we'll fix it) 10 65 20 80 20 80 40 60 85 99 40 60 85 99 700 50 65 700 50 65

Removing from heap, cont'd. Must fix heap-ordering property; root is out of order Shift the root downward ("bubble down") until it's in place Swap it with its smaller child each time What happens if we don't always swap with the smaller child? 65 20 20 80 40 80 40 60 85 99 50 60 85 99 700 50 700 65

Implementation with a list!

Array Implementation If Parent is i, If Child is i, Left child is 2*i Right child is 2*i + 1 If Child is i, Parent is Floor(i/2) =

Optimization problems An optimization problem is one in which you want to find, not just a solution, but the best solution A “greedy algorithm” sometimes works well for optimization problems A greedy algorithm works in phases. At each phase: You take the best you can get right now, without regard for future consequences You hope that by choosing a local optimum at each step, you will end up at a global optimum 2

Shortest Path Algorithms Single-Source Shortest Path Problem Given an input a weighted graph, G = (V, E), and a distinguished vertex, s, find the shortest weighted path from s to every other vertex in G. Assume there is no edge with a negative cost (which could create negative cost cycles).

Shortest Path Algorithms Dijkstra’s Algorithm (for single-source weighted graphs) A greedy algorithm, solving a problem by stages by doing what appears to be the best thing at each stage. Select a vertex v, which has the smallest dv among all the unknown vertices, and declare that the shortest path from s to v is known.

Shortest Path Algorithms For each adjacent vertex, w, update dw = dv + cv,w if this new value for dw is an improvement.

Stages of Dijkstra's algorithm

Stages of Dijkstra's algorithm

An Animation https://www.cs.usfca.edu/~galles/visualization/Dijkstra.html

Pseudocode (not Levitin)

Runtime of Dijkstra's alg. The simplest implementation of the Dijkstra's algorithm stores vertices of set Q in an ordinary linked list or array Operation Extract-Min(Q) is simply a linear search through all vertices in Q In this case, the total running time is O(V2) For sparse graphs, that is, graphs with much less than V2 edges: Dijkstra's algorithm can be implemented more efficiently by using a priority queue to implement the removeMin function algorithm improves to O((E+V)logV) time

For Next Class, Monday (after Spring Break) Huffman coding! A compression technique Homework 5 due Tuesday after Spring Break