Computer Science 385 Design and Analysis of Algorithms Siena College Spring Lab 8: Greedy Algorithms Due: Start of your next lab session

Similar documents
Computer Science 501 Data Structures & Algorithms The College of Saint Rose Fall 2014

CS106X Programming Abstractions in C++ Dr. Cynthia Bailey Lee

Sorting and Searching

Sorting and Searching

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms

CS350: Data Structures Heaps and Priority Queues

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

CSci 231 Final Review

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

Programming Abstractions

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Binary heaps (chapters ) Leftist heaps

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

HEAPS & PRIORITY QUEUES

Collection of priority-job pairs; priorities are comparable.

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Heaps. Heaps Priority Queue Revisit HeapSort

Properties of a heap (represented by an array A)

Minimum Spanning Trees

BM267 - Introduction to Data Structures

Heapsort. Heap data structure

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree

1. O(log n), because at worst you will only need to downheap the height of the heap.

Computer Science E-22 Practice Final Exam

Priority Queues Heaps Heapsort

Topics Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Alg. using a Heap making use of the heap properties STL Sorting F

CS165: Priority Queues, Heaps

1 Interlude: Is keeping the data sorted worth it? 2 Tree Heap and Priority queue

Algorithms and Theory of Computation. Lecture 7: Priority Queue

CSE 100 Minimum Spanning Trees Prim s and Kruskal

The heap is essentially an array-based binary tree with either the biggest or smallest element at the root.

Heaps and Priority Queues

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

Priority Queues. Priority Queues Trees and Heaps Representations of Heaps Algorithms on Heaps Building a Heap Heapsort.

Heaps. Heapsort. [Reading: CLRS 6] Laura Toma, csci2000, Bowdoin College

CSCI2100B Data Structures Heaps

ECE250: Algorithms and Data Structures Final Review Course

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

CH 8. HEAPS AND PRIORITY QUEUES

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

CH. 8 PRIORITY QUEUES AND HEAPS

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Brute-Force Algorithms

Module 2: Priority Queues

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

Algorithms and Data Structures

Stores a collection of elements each with an associated key value

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Announcements HEAPS & PRIORITY QUEUES. Abstract vs concrete data structures. Concrete data structures. Abstract data structures

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn

Problem Pts Score Grader Problem Pts Score Grader

Dictionaries. Priority Queues

What is a minimal spanning tree (MST) and how to find one

9. Heap : Priority Queue

CS 216 Fall 2007 Final Exam Page 1 of 10 Name: ID:

11 Stress Tests; Priority Queue; Heapsort

Chapter 6 Heapsort 1

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

tree follows. Game Trees

CS2013 Course Syllabus Spring 2018 Lecture: Mon/Wed 2:00 P.M. 2:50 P.M. SH C259 Lab: Mon/Wed 2:50 P.M. 4:00 P.M. SH C259

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

Lecture 18 Heaps and Priority Queues

Heaps. Heaps. A heap is a complete binary tree.

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

Heapsort. Why study Heapsort?

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

Heaps Goodrich, Tamassia. Heaps 1

CSCE 2014 Final Exam Spring Version A

22 Elementary Graph Algorithms. There are two standard ways to represent a

looking ahead to see the optimum

Heaps. A heap is a certain kind of complete binary tree.

Trees & Tree-Based Data Structures. Part 4: Heaps. Definition. Example. Properties. Example Min-Heap. Definition

Tables and Priority Queues

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid

CSC 1700 Analysis of Algorithms: Heaps

COS 226 Midterm Exam, Spring 2009

22 Elementary Graph Algorithms. There are two standard ways to represent a

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

COMP 250 Fall priority queues, heaps 1 Nov. 9, 2018

You must include this cover sheet. Either type up the assignment using theory5.tex, or print out this PDF.

Implementations. Priority Queues. Heaps and Heap Order. The Insert Operation CS206 CS206

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

EST Solutions. Ans 1(a): KMP Algorithm for Preprocessing: KMP Algorithm for Searching:

CS 206 Introduction to Computer Science II

Adding a Node to (Min) Heap. Lecture16: Heap Sort. Priority Queue Sort. Delete a Node from (Min) Heap. Step 1: Add node at the end

LECTURE 26 PRIM S ALGORITHM

CS 206 Introduction to Computer Science II

Algorithms and Data Structures 2014 Exercises and Solutions Week 9

CS350: Data Structures Dijkstra s Shortest Path Alg.

ECE 250 Algorithms and Data Structures

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Homework Set 3 Due: Start of Class, Monday, April 10, 2017

Technical University of Denmark

Transcription:

Computer Science 385 Design and Analysis of Algorithms Siena College Spring 2017 Lab 8: Greedy Algorithms Due: Start of your next lab session You will be assigned groups of size 2 or 3 for this lab. Only one submission per group is needed. Please have your instructor initial in each box as you complete each task. Names: Question 1: Hashing Practice: Complete Levitin Exercises 7.3.1 and 7.3.2 on p. 274-275. (10 points)

If you have not studied or do not recall the details of heaps and heapsort, please review Levitin Section 6.4 and/or the topic notes on Heaps linked from the schedule page. Question 2: Consider the construction of a min-heap by starting with an empty heap, then inserting values in a given order. Show the min-heap after each of the values 2, 8, 97, 31, 12, 3, and 39 are added to an initially empty heap. (6 points) 2

Question 3: Consider the construction of a min-heap as done in the first phase of a heapsort. Start with an array containing the values 2, 8, 97, 31, 12, 3, and 39. Show the contents of the array (in array or tree form, your choice) after each non-trivial iteration of the first phase of the heapsort (which goes from an unsorted array to a min-heap). (7 points) 3

Question 4: Finally, show the transformation from the min-heap you just constructed to a sorted array, completing the heapsort procedure. (7 points) 4

Question 5: Consider a binary min-heap like the ones in Levitin Section 6.4. a. Which locations in a binary min-heap of n elements could possibly contain the thirdsmallest element? (3 points) b. Which locations in a binary min-heap of n elements could possibly contain the largest element? (2 points) 5

The heaps we have seen here, where the heap is represented by a binary tree stored in an array, are one specific case of a more general structure called a d-heap. In a d-heap, each node has up to d children. So the binary heaps you would have seen before would be 2-heaps. For the questions below, assume that the minimum value is stored at the root node (i.e., that it is a min-heap). Question 6: Show the construction of a 2-heap that results from the following values being inserted: 18, 9, 23, 17, 1, 43, 65, 12. How many comparisons are needed? (8 points) Question 7: Show the construction of a 3-heap that results from the following values being inserted: 18, 9, 23, 17, 1, 43, 65, 12. How many comparisons are needed? (8 points) 6

Question 8: For the heap element at position i in the underlying array of a 3-heap, what are the positions of its immediate chidren and its parent? (Give formulas in terms of i.) (2 points) Question 9: For the heap element at position i in the underlying array of a d-heap, what are the positions of its immediate chidren and its parent? (Give formulas in terms of i andd.) (2 points) Question 10: Show the construction of a 1-heap that results from the following values being inserted: 18, 9, 23, 17, 1, 43, 65, 12. How many comparisons are needed? (5 points) 7

Question 11: Show the construction of a 7-heap that results from the following values being inserted: 18, 9, 23, 17, 1, 43, 65, 12. How many comparisons are needed? (5 points) 8

Read Levitin Section 9.2 about Kruskal s Algorithm, and then complete the problem below. Question 12: Using the style as shown in Levitin Figure 9.5, p. 326, demonstrate the steps of Kruskal s Algorithm on the graphs in Levitin Exercise 9.2.1, p. 331-332. (15 points, 6 for part a, 9 for part b) 9

Question 13: Using the graph below, (credit: Bailey Problem 16.7, p. 436), use Dijkstra s Algorithm to compute the shortest distance from Dover to Phoenix by filling in the tables below, using the algorithm and notation as shown in the example from class. (25 points) Fill in the table in Table 1 version of this document or create an equivalent table of your own. which is a map of cities as keys to the shortest distance from Dover to the last edge traversed on that shortest route as values. It is easiest to specify edges by the labels of their endpoints rather than the edge label itself, which might not be unique. Also, use the table in Table 2 to keep track of your priority queue. Remember, don t erase entries when you remove them from the queue, just cross them out and mark them with a number in the Seq column of the table entry to indicate the sequence in which the values were removed from the queue. 10

City Dover (distance,last-edge) (0,null) Table 1: Map of Dijkstra s algorithm results for last edge traversed to find each city for the first time. 11

(distance,last-edge) Seq Table 2: Table showing the progress of the priority queue in Dijkstra s algorithm. 12