CSE 373: Data Structures and Algorithms

Similar documents
CSE 373: Data Structures and Algorithms

CSE 373: Data Structures and Algorithms

Queues 4/11/18. Many of these slides based on ones by Cynthia Lee

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

CSE 373: Data Structures and Algorithms

CSE 373: Data Structures and Algorithms

CSE 373: Data Structures and Algorithms

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

Today s Outline. CSE 326: Data Structures Asymptotic Analysis. Analyzing Algorithms. Analyzing Algorithms: Why Bother? Hannah Takes a Break

CS 206 Introduction to Computer Science II

CSE 332: Data Abstractions. Ruth Anderson Spring 2014 Lecture 1

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

CSE 373: Data Structures and Algorithms

CSE 373: Data Structures and Algorithms

0.1 Welcome. 0.2 Insertion sort. Jessica Su (some portions copied from CLRS)

COURSE MECHANICS. Welcome! CSE 332 Data Abstractions: Introduction and ADTs. Concise to-do list. Today in Class. Instructor: Kate Deibel

Sorting Algorithms. CSE21 Winter 2017, Day 2 (B00), Day 1-2 (A00) January 11, 2017

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

CS 206 Introduction to Computer Science II

APCS-AB: Java. Recursion in Java December 12, week14 1

CSE 373: Data Structures and Algorithms

CSE373: Data Structures & Algorithms Lecture 12: Amortized Analysis and Memory Locality. Lauren Milne Spring 2015

Searching Algorithms/Time Analysis

CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up. Nicki Dell Spring 2014

CSE 373: Data Structures and Algorithms

COMP251: Algorithms and Data Structures. Jérôme Waldispühl School of Computer Science McGill University

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Lecture 3: Queues, Testing, and Working with Code

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims

CSE373: Data Structures and Algorithms Lecture 1: Introduc<on; ADTs; Stacks/Queues

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

UNIVERSITY REGULATIONS

CSE373: Data Structures & Algorithms Lecture 12: Amortized Analysis and Memory Locality. Linda Shapiro Winter 2015

1KOd17RMoURxjn2 CSE 20 DISCRETE MATH Fall

FINAL EXAMINATION. COMP-250: Introduction to Computer Science - Fall 2010

Homework 3. OPTIONAL PROBLEMS (No due date)

CSE 373: Data Structures and Algorithms

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Lauren Milne Summer 2015

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

Adam Blank Lecture 1 Winter 2017 CSE 332. Data Abstractions

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 10 Notes. Class URL:

Lecture 2: Implementing ADTs

CSE 332: Data Structures. Spring 2016 Richard Anderson Lecture 1

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues. Aaron Bauer Winter 2014

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Adam Blank Lecture 2 Winter 2017 CSE 332. Data Structures and Parallelism

Assume you are given a Simple Linked List (i.e. not a doubly linked list) containing an even number of elements. For example L = [A B C D E F].

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

CS103 Spring 2018 Mathematical Vocabulary

CSE 20 DISCRETE MATH WINTER

CSE 373: Data Structures and Algorithms

Standard ADTs. Lecture 19 CS2110 Summer 2009

Lecture Notes for Chapter 2: Getting Started

ECE Fall st Midterm Examination (120 Minutes, closed book)

Complexity, Induction, and Recurrence Relations. CSE 373 Help Session 4/7/2016

Data Structures Lecture 8

FINAL EXAMINATION. COMP-250: Introduction to Computer Science - Fall 2011

CSE 20 DISCRETE MATH. Winter

CS-141 Exam 2 Review November 10, 2017 Presented by the RIT Computer Science Community

CSE 373: Data Structures and Algorithms

Lecture 2: Stacks and Queues

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

cs Java: lecture #6

COMP Data Structures

Computer Science 210: Data Structures

CMPS 182: Introduction to Database Management Systems. Instructor: David Martin TA: Avi Kaushik. Syllabus

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Linda Shapiro Spring 2016

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Algorithm Design and Analysis Homework #4

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

CSE 20 DISCRETE MATH. Fall

Institute of Operating Systems and Computer Networks Algorithms Group. Network Algorithms. Tutorial 1: Flashback and Fast Forward

CSE373: Data Structures & Algorithms Lecture 23: Course Victory Lap. Kevin Quinn Fall 2015

Where we are going (today)

4. Java language basics: Function. Minhaeng Lee

3137 Data Structures and Algorithms in C++

CS 240 Fall 2015 Section 004. Alvin Chao, Professor

COMP Data Structures

UNIVERSITY REGULATIONS

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Unit #3: Recursion, Induction, and Loop Invariants

CS 61B Midterm 2 Guerrilla Section Spring 2018 March 17, 2018

CMPSCI 250: Introduction to Computation. Lecture #1: Things, Sets and Strings David Mix Barrington 22 January 2014

Greedy algorithms is another useful way for solving optimization problems.

Class Note #02. [Overall Information] [During the Lecture]

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

CSE 332: Data Abstractions Lecture 15: Topological Sort / Graph Traversals. Ruth Anderson Winter 2013

CSc 225 Algorithms and Data Structures I Case Studies

Kernel Implementations I. 27 June 2013 OSU CSE 1

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

Where we are going (today)

Overview. CSE 101: Design and Analysis of Algorithms Lecture 1

LIFO : Last In First Out

Activity Guide - Public Key Cryptography

Recursive Data and Recursive Functions

CS 4349 Lecture August 21st, 2017

ANALYSIS OF ALGORITHMS

Transcription:

CSE 373: Data Structures and Algorithms Lecture 2: Wrap up Queues, Asymptotic Analysis, Proof by Induction Instructor: Lilian de Greef Quarter: Summer 2017

Today: Announcements Wrap up Queues Begin Asymptotic Analysis: Big-O Proof by Induction

Announcement: Office Hours Announced! See course webpage for times Most held in 3 rd floor breakouts in CSE (whiteboards near stairs) Lilian s additional actual office office hours CSE 220 (a more private environment) During listed times And by appointment! (email me >24 hours ahead of time with several times that work for you) Come talk to me about anything! (feedback, grad school, Ultimate Frisbee, life problems, whatever)

Announcement: Sections When & where: listed on course webpage What: TA-led Review sessions of course material Practice problems Question-answering Optional, but highly encouraged! I wouldn't have passed 332 (Data Structures and Parallelism) without regularly going to section! Vlad (TA)

Other Announcements Homework 1 is out On material covered in Lecture 1 Go forth! or at least get Eclipse set up today. Only required course reading: 10 pages, easy read on commenting style Due beginning of class on Monday July 3 rd Not an official UW holiday (sorry guys) But I m declaring it an unofficial holiday! Go enjoy a 4-day July 4 th weekend!

Finishing up Queues Let s resolve that cliff-hanger!

If we can assume the queue is not empty, how can we implement size-1 dequeue()? 0 1 Public E dequeue() { size--; E e = array[front]; <Your code here!> return e; } rear i h g f e front 2 A) B) front++; if (front == array.length) front = 0; rear = rear-1; if (rear < 0) rear = array.length-1; C) D) for (int i = 0; i < rear; i++) { array[i] = array[i+1] } front++; if (front == array.length) front = 0; None of these are correct

(Notes for yourself)

If we can assume the array is not full, how can we implement size-1 0 enqueue(e e)? 1 Public enqueue(e e) { <Your code here!> size++; } rear i h g f e 2 front A) B) rear++; if (rear == array.length) rear = 0; array[rear] = e; rear++; array[rear] = e; C) D) for (int i=front; i<rear; i++) { array[i] = array[i+1] } array[rear] = e; rear++; None of these are correct

(Notes for yourself)

Between arrays and linked-lists which one *always* is the fastest at enqueue, dequeue, and seekthelement operations? (where seekthelement lets you peek at the kth element in the stack) Fastest: enqueue dequeue seekthelement A) Arrays Linked-Lists Neither B) Linked-lists Neither Neither C) Linked-lists Neither Arrays D) They re all the same

(Notes for yourself)

Which one s better? Arrays Linked-lists

Trade-offs! The ability to choose wisely between trade-offs is why it s important to understand underlying data structures. Common Trade-offs Time vs space One operation s efficiency vs another Generality vs simplicity vs performance

Asymptotic Analysis Oh ho! The Big-O!

Algorithm Analysis Why: to help choose the right algorithm or data structure for the job Often in asymptotic terms Most common way: Big-O Notation General idea: A common way to describe worst-case running time

Example #1: The barn is an array of Cows, excitement is an integer, and Cow.addHat()runs in constant time. Important! Always begin by specifying what n is! (or x or y or whatever letter) println("the alien is visiting!"); println("party time!"); excitement++; for (int i=0; i<barn.length; i++) { Cow cow = barn[i]; cow.addhat(); } Let's assume that one line of code takes 1 "unit of time" to run This is not always true, i.e. calls to non-constant-time methods)

Example #1: println("the alien is visiting!"); println("party time!"); excitement++; for (int i=0; i<barn.length; i++) { Cow cow = barn[i]; cow.addhat(); }

Example #2: Your turn! for (Person player: sportsteam) { player.smile(); for (Person teammate: sportsteam) { player.say( Good game!"); player.highfive(teammate); } Assume that the above Person method calls run in constant time

What s the asymptotic runtime of this (semi-)pseudocode? x := 0; for i=1 to N do for j=1 to i do x := x + 3; return x; A. O(n) B. O(n 2 ) C. O(n + n/2) D. None of the above

What s the asymptotic runtime of this (semi-)pseudocode? x := 0; for i=1 to N do for j=1 to i do x := x + 3; return x; A. O(n) B. O(n 2 ) C. O(n + n/2) D. None of the above How do we prove the right answer? Proof by Induction!

Inductive Proofs (Interlude from Asymptotic Analysis)

Steps to Inductive Proof 1. If not given, define n (or x or t or whatever letter you use) 2. Base Case 3. Inductive Hypothesis (IHOP): Assume what you want to prove is true for some arbitrary value k (or p or d or whatever letter you choose) 4. Inductive Step: Use the base case and IHOP to prove it's true for n = k+1

Example #0: Proof that I can climb any length ladder 1. Let n = number of rungs on a ladder. 2. Base Case: for n = 1 3. Inductive Hypothesis (IHOP): Assume true for some arbitrary integer n = k. 4. Inductive Step: (aiming to prove it's true for n = k+1) If I climb k steps of the ladder, then I have one step left to go. By IHOP, I can climb k steps of the ladder. By Base Case, I can climb the last step. So I can climb k+1 steps. I can climb forever!

Example #1 Prove that the number of n (n + 1) 2 x := 0; for i=1 to N do for j=1 to i do x := x + 3; return x; loop iterations is

(Extra room for notes)

Example #2: Prove that 1 + 2 + 4 + 8 + + 2 n = 2 n+1-1

(Extra room for notes)

Useful Mathematical Property! + ( 2 ) = 2 +./ 1 ),- You ll use it or see it again before the end of CSE 373.

Powers of 2 A bit is 0 or 1 (just two different letters or symbols ) A sequence of n bits can represent 2 n distinct things (For example, the numbers 0 through 2 n -1) 2 10 is 1024 ( about a thousand, kilo in CSE speak) 2 20 is about a million, mega in CSE speak 2 30 is about a billion, giga in CSE speak Java: an int is 32 bits and signed, so max int is about 2 billion a long is 64 bits and signed, so max long is 2 63-1

Which means You could give a unique id to Every person in the U.S. with 29 bits Every person in the world with 33 bits Every person to have ever lived with 38 bits (estimate) Every atom in the universe with 250-300 bits So if a password is 128 bits long and randomly generated, do you think you could guess it?

Example #3: (Parody) Reverse Induction! Proof by Reverse Induction That You Can Always Cage a Lion: Let n = number of lions Base Case: There exists some countable, arbitrarily large value of M such that when n = M, the lions are so packed together that it's trivial to cage one. IHOP: Assume this is also true for n = k. Inductive Step: Then for n = k-1, release a lion to reduce the problem to the case of n = k, which by the IHOP is true. QED :) Fun fact: Reverse induction is a thing! The math part of the above is actually correct.