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

Chapter 9 Graph Algorithms

Chapter 9 Graph Algorithms

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

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

Chapter 9 Graph Algorithms

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

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)

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

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

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

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

CSE 21 Summer 2017 Homework 4

Introduction to Computer Science

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

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

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

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

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

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

Review of course COMP-251B winter 2010

Sorting. Dr. Baldassano Yu s Elite Education

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

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

CSE 373: Data Structures and Algorithms

Design and Analysis of Algorithms

CSC263 Week 12. Larry Zhang

Algorithms and Data Structures, or

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

Lecture 11. Lecture 11: External Sorting

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

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

CSE 373: Data Structures and Algorithms

CSE 373 Lecture 19: Wrap-Up of Sorting

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

NP-Complete Problems

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

CS61BL. Lecture 5: Graphs Sorting

EECS 203 Lecture 20. More Graphs

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

CS1 Lecture 30 Apr. 2, 2018

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

Partha Sarathi Mandal

CSE 373: Data Structures and Algorithms

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

Some Hardness Proofs

DESIGN AND ANALYSIS OF ALGORITHMS

CMPSCI611: Approximating SET-COVER Lecture 21

CS 440 Theory of Algorithms /

Algorithms and Data Structures

Time Complexity of an Algorithm

DESIGN AND ANALYSIS OF ALGORITHMS GREEDY METHOD

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

INSTITUTE OF AERONAUTICAL ENGINEERING

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

CSC 505, Fall 2000: Week 12

5105 BHARATHIDASAN ENGINEERING COLLEGE

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

Design and Analysis of Algorithms

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

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

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

Week 12: Running Time and Performance

CSE332 Summer 2010: Final Exam

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

CS 4349 Lecture October 18th, 2017

P and NP (Millenium problem)

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

CSE 417 Practical Algorithms. (a.k.a. Algorithms & Computational Complexity)

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

Data Structures and Algorithm Analysis in C++

Introduction to Algorithms

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

COMP Analysis of Algorithms & Data Structures

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2

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

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

The Algorithm Design Manual

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

CMPSCI611: The SUBSET-SUM Problem Lecture 18

COS 226 Final Exam, Spring 2009

9. Which situation is true regarding a cascading cut that produces c trees for a Fibonacci heap?

Final Examination CSE 100 UCSD (Practice)

Analysis of Algorithms

6. Algorithm Design Techniques

n 2 C. Θ n ( ) Ο f ( n) B. n 2 Ω( n logn)

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

CS599: Convex and Combinatorial Optimization Fall 2013 Lecture 14: Combinatorial Problems as Linear Programs I. Instructor: Shaddin Dughmi

( ). Which of ( ) ( ) " #& ( ) " # g( n) ( ) " # f ( n) Test 1

Review for Midterm Exam

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

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

COS 226 Midterm Exam, Spring 2009

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: Friday

Back to Sorting Algorithms

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:

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?

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

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 Questions you should have asked me: 1) Is the array in any particular order? 2) Should I consider the case where adding two large numbers could cause an overflow? 3) Is space a factor, in other words, can I use an additional structure(s)? 4) Is this method going to be called frequently with different/the same value of x? 5) About how many values should I expect to see in the array, or is that unspecified? 6) Will x always be a positive value? 7) Can I assume the array won t always be empty, what if its null?

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