Midterm Review. EECS 2011 Prof. J. Elder - 1 -

Similar documents
Midterm Review. EECS 2011 Prof. J. Elder - 1 -

Priority Queues & Heaps

Priority Queues & Heaps

Priority Queues & Heaps

Search Trees. Chapter 10. CSE 2011 Prof. J. Elder Last Updated: :51 PM

ECE250: Algorithms and Data Structures Trees

Comparison Sorts. EECS 2011 Prof. J. Elder - 1 -

Maps and Hash Tables. EECS 2011 Prof. J. Elder - 1 -

Maps, Hash Tables and Dictionaries

COMP : DATA STRUCTURES 2/27/14. Are binary trees satisfying two additional properties:

Estimating the Margin of Victory for Instant-Runoff Voting

TAFTW (Take Aways for the Week) APT Quiz and Markov Overview. Comparing objects and tradeoffs. From Comparable to TreeMap/Sort

File Systems: Fundamentals

Chapter 8: Recursion

Proving correctness of Stable Matching algorithm Analyzing algorithms Asymptotic running times

NEW YORK CITY COLLEGE OF TECHNOLOGY The City University of New York

Uninformed search. Lirong Xia

CS 5523: Operating Systems

Complexity of Manipulating Elections with Few Candidates

Constraint satisfaction problems. Lirong Xia

Event Based Sequential Program Development: Application to Constructing a Pointer Program

A Calculus for End-to-end Statistical Service Guarantees

Estimating the Margin of Victory for an IRV Election Part 1 by David Cary November 6, 2010

Contents. Bibliography 121. Index 123

Title: Adverserial Search AIMA: Chapter 5 (Sections 5.1, 5.2 and 5.3)

Optimization Strategies

Text UI. Data Store Ø Example of a backend to a real Could add a different user interface. Good judgment comes from experience

CS 4407 Algorithms Greedy Algorithms and Minimum Spanning Trees

Bribery in voting with CP-nets

Data 100. Lecture 9: Scraping Web Technologies. Slides by: Joseph E. Gonzalez, Deb Nolan

Mathematics and Social Choice Theory. Topic 4 Voting methods with more than 2 alternatives. 4.1 Social choice procedures

Title: Solving Problems by Searching AIMA: Chapter 3 (Sections 3.1, 3.2 and 3.3)

Cluster Analysis. (see also: Segmentation)

CS 5523 Operating Systems: Synchronization in Distributed Systems

Random Forests. Gradient Boosting. and. Bagging and Boosting

A Bloom Filter Based Scalable Data Integrity Check Tool for Large-scale Dataset

Chapter 11. Weighted Voting Systems. For All Practical Purposes: Effective Teaching

Notes for Session 7 Basic Voting Theory and Arrow s Theorem

Subreddit Recommendations within Reddit Communities

Support Vector Machines

Introduction to Game Theory. Lirong Xia

Hoboken Public Schools. Algebra II Honors Curriculum

HASHGRAPH CONSENSUS: DETAILED EXAMPLES

Minimum Spanning Tree Union-Find Data Structure. Feb 28, 2018 CSCI211 - Sprenkle. Comcast wants to lay cable in a neighborhood. Neighborhood Layout

Manipulating Two Stage Voting Rules

Hoboken Public Schools. Project Lead The Way Curriculum Grade 8

VOTING DYNAMICS IN INNOVATION SYSTEMS

Dimension Reduction. Why and How

Report to 175 th WP.29 session from the 27 th IWVTA Informal Group meeting (Phase 2)

The Effectiveness of Receipt-Based Attacks on ThreeBallot

The Australian Society for Operations Research

Hoboken Public Schools. AP Statistics Curriculum

Aggregating Dependency Graphs into Voting Agendas in Multi-Issue Elections

Designing a Social Network Prep for Lab 10. March 26, 2018 Sprenkle - CSCI Why classes and objects? How do we create new data types?

Electrical Engineering and Computer Science Department

MATH4999 Capstone Projects in Mathematics and Economics Topic 3 Voting methods and social choice theory

Hoboken Public Schools. College Algebra Curriculum

Lecture 6 Cryptographic Hash Functions

LVWME Recommendations for Recount Procedures in Ranked Choice contests.

Introduction to Computational Social Choice. Yann Chevaleyre. LAMSADE, Université Paris-Dauphine

Louis M. Edwards Mathematics Super Bowl Valencia Community College -- April 30, 2004

Complexity of Strategic Behavior in Multi-Winner Elections

Tengyu Ma Facebook AI Research. Based on joint work with Yuanzhi Li (Princeton) and Hongyang Zhang (Stanford)

An Integer Linear Programming Approach for Coalitional Weighted Manipulation under Scoring Rules

Computational Social Choice: Spring 2007

This situation where each voter is not equal in the number of votes they control is called:

Year 1 Mental mathematics and fluency in rapid recall of number facts are one of the main aims of the new Mathematics Curriculum.

Iowa Voting Series, Paper 6: An Examination of Iowa Absentee Voting Since 2000

Processes. Criteria for Comparing Scheduling Algorithms

Hat problem on a graph

Political Districting for Elections to the German Bundestag: An Optimization-Based Multi-Stage Heuristic Respecting Administrative Boundaries

A comparative analysis of subreddit recommenders for Reddit

Voting on combinatorial domains. LAMSADE, CNRS Université Paris-Dauphine. FET-11, session on Computational Social Choice

Fall 2016 COP 3223H Program #5: Election Season Nears an End Due date: Please consult WebCourses for your section

AGENDAS AND SINCERITY: A SECOND RESPONSE TO SCHWARTZ

Social welfare functions

Batch binary Edwards. D. J. Bernstein University of Illinois at Chicago NSF ITR

Estimating the Margin of Victory for Instant-Runoff Voting*

Fuzzy Mathematical Approach for Selecting Candidate For Election by a Political Party

Polydisciplinary Faculty of Larache Abdelmalek Essaadi University, MOROCCO 3 Department of Mathematics and Informatics

Complexity of Terminating Preference Elicitation

Agendas and sincerity: a second response to Schwartz

OPEN SOURCE CRYPTOCURRENCY

Federated Decision Making

CSCI211: Intro Objectives

Randomized Pursuit-Evasion in Graphs

CELL PROPOSALS FROM THE ORGANIZERS (Samples 1 to 8)

Approval Voting Theory with Multiple Levels of Approval

ForeScout Extended Module for McAfee epolicy Orchestrator

A Critical Review of the Triple Ballot Voting System. Part 2:

arxiv: v2 [math.ho] 12 Oct 2018

Fertility, Income Distribution, and Growth

Cyber-Physical Systems Scheduling

16. How to Structure Large Models and Programs with Graph Structurings

Combating Friend Spam Using Social Rejections

Dictatorships Are Not the Only Option: An Exploration of Voting Theory

The HeLIx + inversion code Genetic algorithms. A. Lagg - Abisko Winter School 1

Adapting the Social Network to Affect Elections

Document Approval Process. SDR Forum Policy 001

How hard is it to control sequential elections via the agenda?

Transcription:

Midterm Review - 1 -

Topics on the Midterm Ø Data Structures & Object-Oriented Design Ø Run-Time Analysis Ø Linear Data Structures Ø The Java Collections Framework Ø Recursion Ø Trees Ø Priority Queues & Heaps - 2 -

Data Structures So Far Ø Array List q (Extendable) Array Ø Node List q Singly or Doubly Linked List Ø Stack q Array q Singly Linked List Ø Queue q Array q Singly or Doubly Linked List Ø Priority Queue q Unsorted doubly-linked list q Sorted doubly-linked list q Heap (array-based) Ø Adaptable Priority Queue Ø Tree q Sorted doubly-linked list with locationaware entries q Heap with location-aware entries q Linked Structure Ø Binary Tree q Linked Structure q Array - 3 -

Topics on the Midterm Ø Data Structures & Object-Oriented Design Ø Run-Time Analysis Ø Linear Data Structures Ø The Java Collections Framework Ø Recursion Ø Trees Ø Priority Queues & Heaps - 4 -

Data Structures & Object-Oriented Design Ø Definitions Ø Principles of Object-Oriented Design Ø Hierarchical Design in Java Ø Abstract Data Types & Interfaces Ø Casting Ø Generics Ø Pseudo-Code - 5 -

Topics on the Midterm Ø Data Structures & Object-Oriented Design Ø Run-Time Analysis Ø Linear Data Structures Ø The Java Collections Framework Ø Recursion Ø Trees Ø Priority Queues & Heaps - 6 -

Seven Important Functions Ø Seven functions that often appear in algorithm analysis: q Constant 1 q Logarithmic log n q Linear n q N-Log-N n log n q Quadratic n 2 q Cubic n 3 q Exponential 2 n Ø In a log-log chart, the slope of the line corresponds to the growth rate of the function. - 7 -

Definition of Big Oh cg( n) fn ( ) fn ( ) ÎOgn ( ( )) gn ( ) $ > " ³ cn, 0 0: n n0, f( n) cgn ( ) n - 8 -

Relatives of Big-Oh big-omega n f(n) is Ω(g(n)) if there is a constant c > 0 and an integer constant n 0 1 such that f(n) c g(n) for n n 0 big-theta n f(n) is Θ(g(n)) if there are constants c 1 > 0 and c 2 > 0 and an integer constant n 0 1 such that c 1 g(n) f(n) c 2 g(n) for n n 0-9 -

Time Complexity of an Algorithm The time complexity of an algorithm is the largest time required on any input of size n. (Worst case analysis.) Ø O(n 2 ): For any input size n n 0, the algorithm takes no more than cn 2 time on every input. Ø Ω(n 2 ): For any input size n n 0, the algorithm takes at least cn 2 time on at least one input. Ø θ (n 2 ): Do both. - 10 -

Time Complexity of a Problem The time complexity of a problem is the time complexity of the fastest algorithm that solves the problem. Ø O(n 2 ): Provide an algorithm that solves the problem in no more than this time. q Remember: for every input, i.e. worst case analysis! Ø Ω(n 2 ): Prove that no algorithm can solve it faster. q Remember: only need one input that takes at least this long! Ø θ (n 2 ): Do both. - 11 -

Topics on the Midterm Ø Data Structures & Object-Oriented Design Ø Run-Time Analysis Ø Linear Data Structures Ø The Java Collections Framework Ø Recursion Ø Trees Ø Priority Queues & Heaps - 12 -

Linear Data Structures Ø Fundamental Data Structures q Arrays q Singly-Linked Lists q Doubly-Linked Lists Ø Abstract Data Types q Array Lists q Stacks q Queues - 13 -

Topics on the Midterm Ø Data Structures & Object-Oriented Design Ø Run-Time Analysis Ø Linear Data Structures Ø The Java Collections Framework Ø Recursion Ø Trees Ø Priority Queues & Heaps - 14 -

Iterators Ø An Iterator is an object that enables you to traverse through a collection and to remove elements from the collection selectively, if desired. Ø You get an Iterator for a collection by calling its iterator method. Ø Suppose collection is an instance of a Collection. Then to print out each element on a separate line: Iterator<E> it = collection.iterator(); while (it.hasnext()) System.out.println(it.next()); - 15 -

The Java Collections Framework (Ordered Data Types) Interface Abstract Class Class Iterable Collection Queue Abstract Collection List Abstract Queue Abstract List Priority Queue Abstract Sequential List Array List Vector Stack Linked List - 16 -

Topics on the Midterm Ø Data Structures & Object-Oriented Design Ø Run-Time Analysis Ø Linear Data Structures Ø The Java Collections Framework Ø Recursion Ø Trees Ø Priority Queues & Heaps - 17 -

Linear Recursion Design Pattern Ø Test for base cases q Begin by testing for a set of base cases (there should be at least one). q Every possible chain of recursive calls must eventually reach a base case, and the handling of each base case should not use recursion. Ø Recurse once q Perform a single recursive call. (This recursive step may involve a test that decides which of several possible recursive calls to make, but it should ultimately choose to make just one of these calls each time we perform this step.) q Define each possible recursive call so that it makes progress towards a base case. - 18 -

Binary Recursion Ø Binary recursion occurs whenever there are two recursive calls for each non-base case. Ø Example 1: The Fibonacci Sequence - 19 -

Topics on the Midterm Ø Data Structures & Object-Oriented Design Ø Run-Time Analysis Ø Linear Data Structures Ø The Java Collections Framework Ø Recursion Ø Trees Ø Priority Queues & Heaps - 20 -

Ø Root: node without parent (A) Tree Terminology Ø Internal node: node with at least one child (A, B, C, F) Ø External node (a.k.a. leaf ): node without children (E, I, J, K, G, H, D) Ø Ancestors of a node: parent, grandparent, grand-grandparent, etc. A Ø Descendant of a node: child, grandchild, grand-grandchild, etc. Ø Siblings: two nodes having the same parent B C D Ø Depth of a node: number of ancestors (excluding self) E F G H Ø Height of a tree: maximum depth of any node (3) Ø Subtree: tree consisting of a node and its descendants I J K subtree - 21 -

Position ADT Ø The Position ADT models the notion of place within a data structure where a single object is stored Ø It gives a unified view of diverse ways of storing data, such as qa cell of an array qa node of a linked list qa node of a tree Ø Just one method: qobject element(): returns the element stored at the position - 22 -

Tree ADT Ø We use positions to abstract nodes Ø Generic methods: q integer size() q boolean isempty() q Iterator iterator() q Iterable positions() Ø Accessor methods: q position root() q position parent(p) q positioniterator children(p) Ø Query methods: q boolean isinternal(p) q boolean isexternal(p) q boolean isroot(p) Ø Update method: q object replace(p, o) q Additional update methods may be defined by data structures implementing the Tree ADT - 23 -

Preorder Traversal Ø A traversal visits the nodes of a tree in a systematic manner Ø In a preorder traversal, a node is visited before its descendants Algorithm preorder(v) visit(v) for each child w of v preorder (w) 1 Make Money Fast! 2 1. Motivations 2. Methods References 5 9 3 4 1.1 Greed 1.2 Avidity 6 7 8 2.1 Stock Fraud 2.2 Ponzi Scheme 2.3 Bank Robbery - 24 -

Postorder Traversal Ø In a postorder traversal, a node is visited after its descendants Algorithm postorder(v) for each child w of v postorder (w) visit(v) 9 cs16/ 3 homeworks/ 7 programs/ 8 todo.txt 1K 1 2 4 5 6 h1c.doc 3K h1nc.doc 2K DDR.java 10K Stocks.java 25K Robot.java 20K - 25 -

Properties of Proper Binary Trees Ø Notation n number of nodes e number of external nodes Ø Properties: q e = i + 1 q n = 2e - 1 i number of internal nodes q h i h height q h (n - 1)/2 q e 2 h q h log 2 e q h log 2 (n + 1) - 1-26 -

Topics on the Midterm Ø Data Structures & Object-Oriented Design Ø Run-Time Analysis Ø Linear Data Structures Ø The Java Collections Framework Ø Recursion Ø Trees Ø Priority Queues & Heaps - 27 -

Priority Queue ADT Ø A priority queue stores a collection of entries Ø Each entry is a pair (key, value) Ø Main methods of the Priority Queue ADT q insert(k, x) inserts an entry with key k and value x q removemin() removes and returns the entry with smallest key Ø Additional methods q min() returns, but does not remove, an entry with smallest key q size(), isempty() Ø Applications: q Process scheduling q Standby flyers - 28 -

Comparator ADT Ø A comparator encapsulates the action of comparing two objects according to a given total order relation Ø A generic priority queue uses an auxiliary comparator Ø The comparator is external to the keys being compared Ø When the priority queue needs to compare two keys, it uses its comparator Ø The primary method of the Comparator ADT: q compare(a, b): ²Returns an integer i such that v i < 0 if a < b v i = 0 if a = b v i > 0 if a > b v an error occurs if a and b cannot be compared. - 29 -

Heaps Ø Goal: q O(log n) insertion q O(log n) removal Ø Remember that O(log n) is almost as good as O(1)! q e.g., n = 1,000,000,000 à log n 30 Ø There are min heaps and max heaps. We will assume min heaps. - 30 -

Min Heaps Ø A min heap is a binary tree storing keys at its nodes and satisfying the following properties: q Heap-order: for every internal node v other than the root ² key(v) key(parent(v)) q (Almost) complete binary tree: let h be the height of the heap ²for i = 0,, h - 1, there are 2 i nodes of depth i ²at depth h 1 v the internal nodes are to the left of the external nodes v Only the rightmost internal node may have a single child 2 5 6 9 7-31 - q The last node of a heap is the rightmost node of depth h

Upheap Ø After the insertion of a new key k, the heap-order property may be violated Ø Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node Ø Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k Ø Since a heap has height O(log n), upheap runs in O(log n) time 2 1 5 1 5 2 9 7 6 9 7 6-32 -

Downheap Ø After replacing the root key with the key k of the last node, the heap-order property may be violated Ø Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root Ø Note that there are, in general, many possible downward paths which one do we choose??? 7 5 w 6 9-33 -

Downheap Ø We select the downward path through the minimum-key nodes. Ø Downheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k Ø Since a heap has height O(log n), downheap runs in O(log n) time 7 5 5 w 6 7 w 6 9 9-34 -

Array-based Heap Implementation Ø We can represent a heap with n keys by means of an array of length n + 1 Ø Links between nodes are not explicitly stored 2 Ø The cell at rank 0 is not used Ø The root is stored at rank 1. Ø For the node at rank i q the left child is at rank 2i q the right child is at rank 2i + 1 9 5 7 6 q the parent is at rank floor(i/2) q if 2i + 1 > n, the node has no right child q if 2i > n, the node is a leaf 0 2 5 6 9 7 1 2 3 4 5-35 -

Bottom-up Heap Construction Ø We can construct a heap storing n keys using a bottom-up construction with log n phases 2 i -1 2 i -1 Ø In phase i, pairs of heaps with 2 i -1 keys are merged into heaps with 2 i+1-1 keys Ø Run time for construction is O(n). 2 i+1-1 - 36 -

Adaptable 3 a Priority Queues 5 g 4 e - 37 -

Additional Methods of the Adaptable Priority Queue ADT Ø remove(e): Remove from P and return entry e. Ø replacekey(e,k): Replace with k and return the old key; an error condition occurs if k is invalid (that is, k cannot be compared with other keys). Ø replacevalue(e,x): Replace with x and return the old value. - 38 -

Location-Aware Entries Ø A locator-aware entry identifies and tracks the location of its (key, value) object within a data structure - 39 -

Heap Implementation Ø A location-aware heap entry is an object storing q key 2 d q value q position of the entry in the underlying heap Ø In turn, each heap position stores an entry Ø Back pointers are updated during entry swaps 4 a 6 b 8 g 5 e 9 c - 40 -

Performance Ø Times better than those achievable without location-aware entries are highlighted in red: Method Unsorted List Sorted List Heap size, isempty O(1) O(1) O(1) insert O(1) O(n) O(log n) min O(n) O(1) O(1) removemin O(n) O(1) O(log n) remove O(1) O(1) O(log n) replacekey O(1) O(n) O(log n) replacevalue O(1) O(1) O(1) - 41 -

Topics on the Midterm Ø Data Structures & Object-Oriented Design Ø Run-Time Analysis Ø Linear Data Structures Ø The Java Collections Framework Ø Recursion Ø Trees Ø Priority Queues & Heaps - 42 -