CSE 373: Data Structures and Algorithms

Similar documents
CSE 373: Data Structures and Algorithms

CSE373: Data Structure & Algorithms Lecture 23: More Sorting and Other Classes of Algorithms. Catie Baker Spring 2015

CSE 373: Data Structures and Algorithms

CSE 373: Data Structures and Algorithms

CSE 373: Data Structures and Algorithms

CSE 326: Data Structures Sorting Conclusion

Virtual University of Pakistan

Sorting. Riley Porter. CSE373: Data Structures & Algorithms 1

Chapter 9 Graph Algorithms

Chapter 9 Graph Algorithms

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

CSE 373: Data Structures & Algorithms More Sor9ng and Beyond Comparison Sor9ng

Model Answer. Section A Q.1 - (20 1=10) B.Tech. (Fifth Semester) Examination Analysis and Design of Algorithm (IT3105N) (Information Technology)

Chapter 9 Graph Algorithms

Design and Analysis of Algorithms. Comp 271. Mordecai Golin. Department of Computer Science, HKUST

CSE 100: B+ TREE, 2-3 TREE, NP- COMPLETNESS

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 25: Review and Open Problems

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

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

CSE 417 Branch & Bound (pt 4) Branch & Bound

Sorting. Dr. Baldassano Yu s Elite Education

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

CSE 21 Summer 2017 Homework 4

Introduction to Computer Science

Best known solution time is Ω(V!) Check every permutation of vertices to see if there is a graph edge between adjacent vertices

Review of course COMP-251B winter 2010

Announcements. HW4: Due tomorrow! Final in EXACTLY 2 weeks. Start studying. Summer 2016 CSE373: Data Structures & Algorithms 1

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

CSE 373 Lecture 19: Wrap-Up of Sorting

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

Design and Analysis of Algorithms

PSD1A. DESIGN AND ANALYSIS OF ALGORITHMS Unit : I-V

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

CSE 373: Data Structures and Algorithms

CSC 505, Fall 2000: Week 12

Introduction to Algorithms May 14, 2003 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser.

NP-Complete Problems

CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION

Lecture 24: More Reductions (1997) Steven Skiena. skiena

Algorithms and Data Structures, or

CS1 Lecture 30 Apr. 2, 2018

Partha Sarathi Mandal

Contents. 1 Introduction. 2 Searching and Traversal Techniques. Preface... (vii) Acknowledgements... (ix)

DESIGN AND ANALYSIS OF ALGORITHMS

CSE 332 Winter 2018 Final Exam (closed book, closed notes, no calculators)

CS 440 Theory of Algorithms /

Time Complexity of an Algorithm

1 5,9,2,7,6,10,4,3,8,1 The first number (5) is automatically the first number of the sorted list

Data Structures and Algorithm Analysis in C++

CSE 373: Data Structures and Algorithms

CSC263 Week 12. Larry Zhang

INSTITUTE OF AERONAUTICAL ENGINEERING

R13 SET - 1. ''' '' ''' ' blog: anilkumarprathipati.wordpress.com. Code No: RT32054

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

Some Hardness Proofs

CMPSCI611: The SUBSET-SUM Problem Lecture 18

P and NP CISC5835, Algorithms for Big Data CIS, Fordham Univ. Instructor: X. Zhang

Week 12: Running Time and Performance

Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest. Introduction to Algorithms

P and NP (Millenium problem)

CSE332 Summer 2010: Final Exam

Introduction to Algorithms

CSE 373: Data Structures and Algorithms. Memory and Locality. Autumn Shrirang (Shri) Mare

CS 373: Combinatorial Algorithms, Spring 1999

P and NP CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang

UNIVERSITY OF TORONTO Department of Computer Science April 2014 Final Exam CSC373H1S Robert Robere Duration - 3 hours No Aids Allowed.

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

CS2351 Data Structures. Lecture 5: Sorting in Linear Time

5105 BHARATHIDASAN ENGINEERING COLLEGE

CS261: A Second Course in Algorithms Lecture #16: The Traveling Salesman Problem

Algorithms and Data Structures (INF1) Lecture 15/15 Hua Lu

CSE332 Summer 2012 Final Exam, August 15, 2012

The Algorithm Design Manual

Final Examination CSE 100 UCSD (Practice)

6. Algorithm Design Techniques

COS 226 Final Exam, Spring 2009

Analysis of Algorithms

CSCE 350: Chin-Tser Huang. University of South Carolina

EECS 203 Lecture 20. More Graphs

CS 4349 Lecture October 18th, 2017

CS61BL. Lecture 5: Graphs Sorting

CSE 373: Data Structures and Algorithms

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

CSE 373: Practice Final

Lecture 11. Lecture 11: External Sorting

Review for Midterm Exam

Algorithms and Data Structures

CMPSCI611: Approximating SET-COVER Lecture 21

Algorithms and Data Structures. Algorithms and Data Structures. Algorithms and Data Structures. Algorithms and Data Structures

Introduction to Algorithms Third Edition

Vertex Cover Approximations

Coping with the Limitations of Algorithm Power Exact Solution Strategies Backtracking Backtracking : A Scenario

CSE 373 NOVEMBER 8 TH COMPARISON SORTS

Graphs. Data Structures and Algorithms CSE 373 SU 18 BEN JONES 1

1 Introduction. 1. Prove the problem lies in the class NP. 2. Find an NP-complete problem that reduces to it.

Notebook Assignments

NP Completeness. Andreas Klappenecker [partially based on slides by Jennifer Welch]

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

Transcription:

CSE 373: Data Structures and Algorithms Lecture 21: Finish Sorting, P vs NP Instructor: Lilian de Greef Quarter: Summer 2017

Today Announcements Finish up sorting Radix Sort Final comments on sorting Complexity Theory: P =? NP

Announcements Final Exam: Next week During usual lecture time (10:50am - 11:50am) Cumulative (so all material we ve covered in class is fair game)... but with emphasis on material covered after the midterm Date?

The Big Picture Surprising amount of juicy computer science: 2-3 lectures Simple algorithms: O(n 2 ) Fancier algorithms: O(n log n) Comparison lower bound: W(n log n) Specialized algorithms: O(n) Handling huge data sets Insertion sort Selection sort Shell sort Heap sort Merge sort Quick sort (avg) Bucket sort Radix sort How??? Change the model assume more than compare(a,b) External sorting

Radix sort Radix = the base of a number system Examples will use 10 because we are used to that In implementations use larger numbers For example, for ASCII strings, might use 128 Idea: Bucket sort on one digit at a time Number of buckets = radix Starting with least significant digit Keeping sort stable Do one pass per digit Invariant: After k passes (digits), the last k digits are sorted Aside: Origins go back to the 1890 U.S. census

Radix Sort: Example Input: 478 537 9 721 3 38 143 67 First pass: bucket sort by one s digit 0 1 2 3 4 5 6 7 8 9 Second pass: stable bucket sort by ten s digit 0 1 2 3 4 5 6 7 8 9 Third pass: stable bucket sort by hundred s digit 0 1 2 3 4 5 6 7 8 9 Output:

(extra space for scratch work / notes)

Analysis Input size: n Number of buckets = Radix: B Number of passes = Digits : P Work per pass is 1 bucket sort: Total work is Compared to comparison sorts, sometimes a win, but often not Example: Strings of English letters up to length 15 Run-time proportional to: 15*(52 + n) This is less than n log n only if n > 33,000 Of course, cross-over point depends on constant factors of the implementations And radix sort can have poor locality properties

Comments on Sorting Algorithms

Sorting massive data Need sorting algorithms that minimize disk/tape access time: Quicksort and Heapsort both jump all over the array, leading to random disk accesses Merge sort scans linearly through arrays, leading to (relatively) sequential disk access Merge sort is the basis of massive sorting Merge sort can leverage multiple disks

External Merge Sort Sort 900 MB using 100 MB RAM Read 100 MB of data into memory Sort using conventional method (e.g. quicksort) Write sorted 100MB to temp file Repeat until all data in sorted chunks (900/100 = 9 total) Read first 10 MB of each sorted chuck, merge into remaining 10 MB writing and reading as necessary Single merge pass instead of log n Additional pass helpful if data much larger than memory Parallelism and better hardware can improve performance Distribution sorts (similar to bucket sort) are also used

Wrap-up on Sorting Simple O(n 2 ) sorts can be fastest for small n Insertion sort (latter linear for mostly-sorted) Good below a cut-off for divide-and-conquer sorts O(n log n) sorts Heap sort, in-place, not stable, not parallelizable Merge sort, not in place but stable and works as external sort Quick sort, in place, not stable and O(n 2 ) in worst-case Often fastest, but depends on costs of comparisons/copies W (n log n) is worst-case and average lower-bound for sorting by comparisons Non-comparison sorts Bucket sort good for small number of possible key values Radix sort uses fewer buckets and more phases Best way to sort?

Source: https://xkcd.com/1185/

Complexity Theory: P vs NP Just a small taste of Complexity Theory

Easy Problems for the Computer Sorting a list of n numbers Multiplying two n x n matrices n 3 5 2 7 1 6 8 9 2 4 6 10 9 3 2 12 1 5 5 4 5 12 8 6 7 6 1 5 9 23 5 8 = n n n n

Easy Problems for the Computer Shortest Path Algorithm Minimum Spanning Tree Algorithms 4 D A 2 9 2 2 B 3 F 1 5 10 2 3 G C 11 1 E 7 1 H C A 2 F D B 1 2 5 2 1 6 10 1 E 1 5 3 G Edsgar Dijkstra

Hard Problems for the Computer The Knapsack Problem I want to carry as much money s worth as I can that still fits in my bag! What do I pack?

Hard Problems for the Computer The Traveling Salesperson Problem I ll leave Seattle to sell goods, visiting each city only once, and return to Seattle. What s the shortest route? Beijing 5411 Seattle 6775 5217 4583 6685 5058 3608 Berlin 1004 Moscow 10 933 6356 7325 2185 1533 São Paulo 7577 3488 Tehran

Hard Problems for the Computer Find a Hamiltonian path (a path that visits each vertex exactly once) (never mind weights or even returning to our starting point!)

Comparing n 2 vs 2 n The alien s computer performs 10 9 operations/sec n = 10 n = 30 n = 50 n = 70 n 2 100 < 1 sec 900 < 1 sec 2500 < 1 sec 4900 < sec 2 n 1024 < 1 sec 10 9 1 sec 10 15 11.6 days 10 21 31,688 years n! 3628800 < 1 sec 10 16 years 10 48 years 10 83 years (10 5 x age of the universe!)

Easy vs Hard Problems for the Computer Polynomial Time = Efficient Is an algorithm efficient with O(n)? O(n 2 )? O(n 10 )? O(n log n)? O(n log n )? O(2 n )? O(n!)?

Polynomial Time? So we know there are polynomial time algorithms to Sort numbers Multiply n x n matrices Find the shortest path in a graph Find the minimum spanning tree and more But the million dollar question is are there polynomial time algorithms to solve The Knapsack Problem? The Traveling Salesperson Problem? Finding Hamiltonian Paths? and thousands more!

P = NP? SAT NP (Nondeterministic Polynomial Time) Hamiltonian Path Traveling Salesperson n x n x n Sudoku P (Polynomial Time) Dijkstra s sort search Prim s Knapsack Kruskal s And there are problems even harder than NP!

Relevance of P = NP NP contains lots of problems we don t know to be in P Classroom Scheduling Packing objects into bins Scheduling jobs on machines Finding cheap tours visiting a subset of cities Finding good packet routings in networks Decryption...

With this knowledge, we can avoid saying I can t find an efficient algorithm. I guess I m too dumb. Cartoon courtesy of Computers and Intractability: A Guide to the Theory of NP-Completeness by M. Garey and D. Johnson

But know it isn t wise to say I can t find an efficient algorithm because no such algorithm is possible! Cartoon courtesy of Computers and Intractability: A Guide to the Theory of NP-Completeness by M. Garey and D. Johnson

And, instead, prove it s in NP to then say I can t find an efficient algorithm, but neither can all these famous people. Cartoon courtesy of Computers and Intractability: A Guide to the Theory of NP-Completeness by M. Garey and D. Johnson

Source: https://xkcd.com/287/

Preparing for Final Exam

Final Exam Study Strategies

Practice Problem Given a value x and an array of integers, determine whether two of the numbers add up to x