Set1 Introduction
Set1 Introduction
COURSE INTRODUCTION
1
Objective
2
Prerequisite
3
Main Questions
4
What are Algorithms?
5
Example: Sorting
• An algorithm for the sorting problem is a sequence of computational steps with the above
input/output specifications.
6
Example: Traveling
7
Example: Majority
• Problem: Find the element in a list that occurs the most number of times
• Input: A multi-set of numbers 𝑆 = 𝑎 , … , 𝑎
• Output: A number 𝑥 such that number of #𝑥 ∈ 𝑆 > (#𝑦 ∈ 𝑆) for any 𝑦 ≠ 𝑥
• Ex: 1, 7, 7, 1, 7, 7, 4 ⇒ 7
8
Example: Majority
• How fast is this algorithm – in terms of the number of items in the list?
9
Example: Majority
10
Internet: Web search, Biology: Human Computers: Circuit Computer graphics:
packet routing, genome project, layout, file system, Movies, video games,
in Real Life
Security: Cell phones, Multimedia: MP3, JPG, Social networks: Physics: N-body
e-commerce, voting DivX, HDTV, face Recommendations, simulation, particle
machines, ... recognition, ... news feeds, collision simulation, ...
advertisements, ...
12
Why Study Algorithms?
• Correctness
• Running-time
• Scalability
• Composability
• Space/Memory
• Elegance and Simplicity
• Reliability
13
Algorithms as Technology
• If computers have infinite power, infinite memory, and infinite speed, then
any algorithm would be fine as long as it solves a problem correctly.
• Speed and memory are limited
• It is important to have efficient algorithms (shorter time and less
memory)
• Example: Insertion sort takes 𝑐 𝑛 , while Merge sort takes 𝑐 𝑛𝑙𝑔𝑛
• Machine A: 1 billion instructions/sec, B: 10 million instructions/sec,
• A use Insertion sort: 2𝑛 , B use Merge sort: 50𝑛𝑙𝑔𝑛
n A B
Million (10 ) ) 2000 sec 100 sec
10 million 2.3 days 20 min
14
How to Design and Analyze Algorithms?
15
Pseudocode Conventions
• Indentation indicates block structure.
• The looping constructs while, for, and repeat-until and the if-else conditional construct have interpretations similar
to those in C, C++, Java, Python, and Pascal.
• The symbol “//” indicates that the remainder of the line is a comment.
• A multiple assignment of the form 𝑖 = 𝑗 = 𝑒 assigns to both variables i and j the value of expression e.
• Array indices start at 1 not 0.
• Compound data are organized into objects, which are composed of attributes. We access a particular attribute using
the dot operator.
• Parameters are passed to a procedure by value. Arrays and Objects are passed by reference (pointer).
• A return statement immediately transfers control back to the point of call in the calling procedure. Multiple values
to be returned in a single return statement are allowed.
• The boolean operators “and” and “or” are short circuiting.
• Error handling is omitted.
16