Objec&ves. Review. Data structure: Heaps Data structure: Graphs. What is a priority queue? What is a heap?

Similar documents
Priority queues implemented via heaps

Heaps and Priority Queues

Review Asympto&c Bounds

Divide-and-Conquer Algorithms Part Two

CS Data Structures and Algorithm Analysis

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

Objec&ves. Review. Divide and conquer algorithms

Review Of Topics. Review: Induction

CS213d Data Structures and Algorithms

Algorithms and Theory of Computation. Lecture 2: Big-O Notation Graph Algorithms

Advanced Implementations of Tables: Balanced Search Trees and Hashing

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

CPSC 320 Sample Final Examination December 2013

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

Part IA Algorithms Notes

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

Dictionary: an abstract data type

Assignment 5: Solutions

CS361 Homework #3 Solutions

Search Trees. EECS 2011 Prof. J. Elder Last Updated: 24 March 2015

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

CS 240 Data Structures and Data Management. Module 4: Dictionaries

Insertion Sort. We take the first element: 34 is sorted

Search Trees. Chapter 10. CSE 2011 Prof. J. Elder Last Updated: :52 AM

25. Minimum Spanning Trees

25. Minimum Spanning Trees

CSE 373: Data Structures and Algorithms Pep Talk; Algorithm Analysis. Riley Porter Winter 2017

The Greedy Method. Design and analysis of algorithms Cs The Greedy Method

Fundamental Algorithms

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

2-INF-237 Vybrané partie z dátových štruktúr 2-INF-237 Selected Topics in Data Structures

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

ENS Lyon Camp. Day 2. Basic group. Cartesian Tree. 26 October

CS60007 Algorithm Design and Analysis 2018 Assignment 1

INF2220: algorithms and data structures Series 1

Algorithm Design and Analysis

Shortest Path Algorithms

Greedy. Outline CS141. Stefano Lonardi, UCR 1. Activity selection Fractional knapsack Huffman encoding Later:

Data Structures and Algorithms " Search Trees!!

Sorting Algorithms. We have already seen: Selection-sort Insertion-sort Heap-sort. We will see: Bubble-sort Merge-sort Quick-sort

Mon Tue Wed Thurs Fri

Slides for CIS 675. Huffman Encoding, 1. Huffman Encoding, 2. Huffman Encoding, 3. Encoding 1. DPV Chapter 5, Part 2. Encoding 2

Algorithms: Lecture 12. Chalmers University of Technology

Tree Heap Priority Queue

Algorithm runtime analysis and computational tractability

Dictionary: an abstract data type

Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.

IS 709/809: Computational Methods in IS Research Fall Exam Review

L feb abhi shelat

Algorithm Design and Analysis

Binary Search Trees. Motivation

Data Structures 1 NTIN066

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

CS 580: Algorithm Design and Analysis

2 GRAPH AND NETWORK OPTIMIZATION. E. Amaldi Introduction to Operations Research Politecnico Milano 1

Data Structures and and Algorithm Xiaoqing Zheng

Sorting. Chapter 11. CSE 2011 Prof. J. Elder Last Updated: :11 AM

Directed Graphs (Digraphs) and Graphs

Algorithms. Algorithms 2.4 PRIORITY QUEUES. Pro tip: Sit somewhere where you can work in a group of 2 or 3

Objec&ves. Review. Dynamic Programming. What is the knapsack problem? What is our solu&on? Ø Review Knapsack Ø Sequence Alignment 3/28/18

4.8 Huffman Codes. These lecture slides are supplied by Mathijs de Weerd

Algorithm Design CS 515 Fall 2015 Sample Final Exam Solutions

Premaster Course Algorithms 1 Chapter 3: Elementary Data Structures

Optimal Tree-decomposition Balancing and Reachability on Low Treewidth Graphs

Friday Four Square! Today at 4:15PM, Outside Gates

Overlapping Communities

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes.

CSCI 3110 Assignment 6 Solutions

Optimal Color Range Reporting in One Dimension

Chapter 8 The Disjoint Sets Class

CS781 Lecture 3 January 27, 2011

FIBONACCI HEAPS. preliminaries insert extract the minimum decrease key bounding the rank meld and delete. Copyright 2013 Kevin Wayne

Solution suggestions for examination of Logic, Algorithms and Data Structures,

CS 161: Design and Analysis of Algorithms

Recitation 7. Treaps and Combining BSTs. 7.1 Announcements. FingerLab is due Friday afternoon. It s worth 125 points.

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

Chapter 9: Relations Relations

Fibonacci (Min-)Heap. (I draw dashed lines in place of of circular lists.) 1 / 17

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

BFS Dijkstra. Oct abhi shelat

Dominating Set Counting in Graph Classes

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2006

Algorithm Design and Analysis

Math 391: Midterm 1.0 Spring 2016

Discrete Wiskunde II. Lecture 5: Shortest Paths & Spanning Trees

Greedy Alg: Huffman abhi shelat

Data Structures and Algorithms

Algorithm Design and Analysis

The null-pointers in a binary search tree are replaced by pointers to special null-vertices, that do not carry any object-data

21. Dynamic Programming III. FPTAS [Ottman/Widmayer, Kap. 7.2, 7.3, Cormen et al, Kap. 15,35.5]

Randomization in Algorithms and Data Structures

Chapter 10 Search Structures

CS 161 Summer 2009 Homework #2 Sample Solutions

Efficient Spatial Data Structure for Multiversion Management of Engineering Drawings

Design of discrete-event simulations

Problem-Solving via Search Lecture 3

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

Single Source Shortest Paths

ECE608 Midterm 1 Spring 2009 THEN PUT YOUR ADDRESS ON EVERY PAGE OF THE EXAM! NAME: Neatness counts. We will not grade what we cannot read.

Transcription:

Objec&ves Data structure: Heaps Data structure: Graphs Submit problem set 2 1 Review What is a priority queue? What is a heap? Ø Proper&es Ø Implementa&on What is the process for finding the smallest element in a heap? What is the process for adding to a heap? 2 1

Review: Heap Defined Combines benefits of sorted array and list Balanced binary tree root Each node has at most 2 children Node value is its key Heap order: each node s key is at least as large as its parent s Note: not a binary search tree 3 Review: Implemen&ng a Heap Op&on 1: Use pointers Ø Each node keeps Element it stores (key) 3 pointers: 2 children, parent Op&on 2: No pointers Ø Requires knowing upper bound on n Ø For node at posi&on i ley child is at 2i right child is at 2i+1 4 2

Review: Implemen&ng a Heap Finding the minimal element Ø First element Ø O(1) 5 Review: Heapify-Up Heap Position where node added Heapify-up(H, i): if i > 1 then j=parent(i)=floor(i/2) if key[h[i]] < key[h[j]] then swap array entries H[i] and H[j] Heapify-up(H, j) O(log i) 6 3

Dele&ng an Element Delete at position 3 w 7 Dele&ng an Element Delete at posi&on i Removing an element: Ø Messes up heap order Ø Leaves a hole in the heap Not as straighaorward as Heapify-Up Algorithm 1. Fill in element where hole was Patch hole: move n th element into i th spot 2. Adjust heap to be in order At posi&on i because moved n th item up to i 8 4

Dele&ng an Element Delete at position 3 w Example of OK: 11 deleted, replaced by 16 Two bad possibili&es: element w is Ø Too small: viola&on is between it and parent à Heapify-Up Ø Too big: with one or both children à Heapify-Down (example: w becomes 12) 9 Dele&ng an Element Example where new key is too small 2 3 6 4 7 5 10 Delete 9 Replace with 5 (from other side of heap) But 5 < 6, so need to Heapify-Up 10 5

Heapify-Down Heapify-down(H, i): n = length(h) if 2i > n then Why can we stop? Terminate with H unchanged else if 2i < n then left=2i and right=2i+1 j be index that minimizes key[h[left]] and key[[h[right]] else if 2i = n then j=2i if key[h[j]] < key[h[i]] then swap array entries H[i] and H[j] Heapify-down(H, j) 11 Heapify-Down Heapify-down(H, i): n = length(h) if 2i > n then i is a leaf nowhere to go Terminate with H unchanged else if 2i < n then left=2i and right=2i+1 j be index that minimizes key[h[left]] and key[[h[right]] else if 2i = n then j=2i if key[h[j]] < key[h[i]] then swap array entries H[i] and H[j] Heapify-down(H, j) 12 6

Prac&ce: Heapify-Down 21 Moved 21 to where element was removed 13 Prac&ce: Heapify-Down 21 7 21 14 7

Prac&ce: Heapify-Down 7 7 21 8 21 15 Run&me of Heapify-Down? Heapify-down(H, i): n = length(h) if 2i > n then Terminate with H unchanged else if 2i < n then left=2i and right=2i+1 j be index that minimizes O(1) key[h[left]] and key[[h[right]] else if 2i = n then j=2i if key[h[j]] < key[h[i]] then swap array entries H[i] and H[j] Heapify-down(H, j) Num swaps: O(log n) O(1) 16 8

Implemen&ng Priority Queues with Heaps Opera&on Descrip&on Run Time StartHeap(N) Insert(v) FindMin() Delete(i) ExtractMin() Creates an empty heap that can hold N elements Inserts item v into heap Iden&fies minimum element in heap but does not remove it Deletes element in heap at posi&on i Iden&fies and deletes an element with minimum key from heap 17 Implemen&ng Priority Queues with Heaps Opera&on Descrip&on Run Time StartHeap(N) Creates an empty heap that can hold N elements O(N) Insert(v) Inserts item v into heap O(log n) FindMin() Delete(i) ExtractMin() Iden&fies minimum element in heap but does not remove it Deletes element in heap at posi&on i Iden&fies and deletes an element with minimum key from heap O(1) O(log n) O(log n) 18 9

Puing It All Together 1. Add elements into PQ with the number s value as its priority 2. Then extract the smallest number un&l done Ø Come out in sorted order What is the running time of sorting numbers using a PQ implemented with a heap? O(n log n) 19 Comparing Data Structures Opera&on Heap Unsorted List Sorted List Start(N) O(1) O(1) Insert(v) O(1) O(n) FindMin() O(1) O(1) Delete(i) O(n) O(1) ExtractMin() O(n) O(1) 20 10

Comparing Data Structures Opera&on Heap Unsorted List Sorted List Start(N) O(N) O(1) O(1) Insert(v) O(log n) O(1) O(n) FindMin() O(1) O(1) O(1) Delete(i) O(log n) O(n) O(1) ExtractMin() O(log n) O(n) O(1) 21 Addi&onal Heap Opera&ons Access elements in PQ by name Key 2 4 5 6 9 20 Priority Value 3542 5143 8712 1264 9123 5954 Process id Ø Maintain addi&onal array Position that stores current posi&on of each element in heap Opera&ons: Ø Delete(Posi&on[v]) Does not increase overall running &me Ø ChangeKey(v, α) Changes key of element v to α Iden&fy posi&on of element v in array (Position array) Change key, heapify 22 11

GRAPHS 23 Undirected Graphs G = (V, E) V = nodes (ver&ces) E = edges between pairs of nodes Captures pairwise rela&onship between objects Graph size parameters: n = V, m = E V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11 Jan 26, 2018 CSCI211 - Sprenkle 24 12

1/26/18 Social Networks Node: people; Edge: rela&onship between 2 people Everything Bad Is Good for You: How Today's Popular Culture Is Actually Making Us Smarter Television shows have complex plots, complex social networks Social network of 24's Jack Bauer http://www.cs.duke.edu/csed/harambeenet/ modules.html Jan 26, 2018 CSCI211 - Sprenkle 25 Facebook: Visualizing Friends http://www.facebook.com/notes/facebook-engineering/ visualizing-friendships/469716398919 Jan 26, 2018 CSCI211 - Sprenkle 26 13

World Wide Web Web graph Ø Node: web page Ø Edge: hyperlink from one page to another Directed Graph: cnn.com netscape.aol.com sportsillustrated.cnn.com people.com &me.com hbo.com boardwalkempire.com Jan 26, 2018 CSCI211 - Sprenkle 27 Graph of Web Page www.wlu.edu 28 14

Ecological Food Web Food web graph Ø Node = species Ø Edge = from prey to predator Directed Graph: Reference: https://www.msu.edu/course/isb/202/ ebertmay/images/foodweb.jpg Jan 26, 2018 CSCI211 - Sprenkle 29 Graph Applica&ons Graph Nodes Edges transportation street intersections highways communication computers fiber optic cables World Wide Web web pages hyperlinks social people relationships food web species predator-prey software systems functions function calls scheduling tasks precedence constraints circuits gates wires Jan 26, 2018 CSCI211 - Sprenkle 30 15

Graph Representa&on: Adjacency Matrix n n matrix with A uv = 1 if (u, v) is an edge Ø Two representa&ons of each edge (symmetric matrix) Ø Space? Ø Checking if (u, v) is an edge? Ø Iden&fying all edges? 1 2 3 4 5 6 7 8 1 0 1 1 0 0 0 0 0 2 1 0 1 1 1 0 0 0 3 1 1 0 0 1 0 1 1 4 0 1 0 0 1 0 0 0 5 0 1 1 1 0 1 0 0 6 0 0 0 0 1 0 0 0 7 0 0 1 0 0 0 0 1 8 0 0 1 0 0 0 1 0 Jan 26, 2018 CSCI211 - Sprenkle 31 Graph Representa&on: Adjacency Matrix n n matrix with A uv = 1 if (u, v) is an edge Ø Two representa&ons of each edge (symmetric matrix) Ø Space: Θ(n 2 ) Ø Checking if (u, v) is an edge: Θ(1) &me Ø Iden&fying all edges: Θ(n 2 ) &me Jan 26, 2018 CSCI211 - Sprenkle 1 2 3 4 5 6 7 8 1 0 1 1 0 0 0 0 0 2 1 0 1 1 1 0 0 0 3 1 1 0 0 1 0 1 1 4 0 1 0 0 1 0 0 0 5 0 1 1 1 0 1 0 0 6 0 0 0 0 1 0 0 0 7 0 0 1 0 0 0 0 1 8 0 0 1 0 0 0 1 0 32 16

Looking Ahead Problem Set 3 due next Friday Wiki due Monday Ø 2.4, 2.5 Ø 3.1 33 17