01a Programming 12 Introduction 4S
01a Programming 12 Introduction 4S
Chapter 1
Introduction
Sections 1.1, 1.2, 1.3
© 2019 Michel Pasquier - all rights reserved CMP 305 Data Structures and Algorithms 01a Programming - Introduction 3 © 2019 Michel Pasquier - all rights reserved CMP 305 Data Structures and Algorithms 01a Programming - Introduction 4
What is this course about? Find the kth-largest (or smallest) number in a set of size N
Examples: find
Why the choice of algorithms is critical - the 1000 best customers (who spend the most) in a database
- the most active users (who post the most) in a social network
when dealing with large inputs
How would you solve this?
Basic mathematical background ... ... ...
Algorithm 1:
Detailed introduction to recursion Sort the N numbers and Pick the kth one
Intuitive and easy to implement, but
Review of basic C++ concepts Sorting requires many comparisons
Introduction to modern C++ features Much work is done comparing elements that have no chance
of being in position k
To sort N elements need N log2(N) comparisons/swaps
Unsorted database with 10,000,000 elements and 1,000
swaps/sec will result in 2.7 days to finish the task !
© 2019 Michel Pasquier - all rights reserved CMP 305 Data Structures and Algorithms 01a Programming - Introduction 5 © 2019 Michel Pasquier - all rights reserved CMP 305 Data Structures and Algorithms 01a Programming - Introduction 6
© 2019 Michel Pasquier - all rights reserved CMP 305 Data Structures and Algorithms 01a Programming - Introduction 7 © 2019 Michel Pasquier - all rights reserved CMP 305 Data Structures and Algorithms 01a Programming - Introduction 8
Many approaches
Brute force search: can solve up to N=10
Using a smart formulation: up to N=30
Ad hoc algorithm (CSP): up to N=100
Good heuristic algorithm: up to N=1000
Best algorithm: up to N=1,000,000 !
© 2019 Michel Pasquier - all rights reserved CMP 305 Data Structures and Algorithms 01a Programming - Introduction 11 © 2019 Michel Pasquier - all rights reserved CMP 305 Data Structures and Algorithms 01a Programming - Introduction 12