Algorithm Lec1
Algorithm Lec1
Algorithm Lec1
2
Overall aims of the course
3
Prerequisites
4
Course Objectives
5
References
6
Basis of Grading
quizzes --- 5%
Assignments --- 10%
Midterm Exam --- 15%
Practical Exam- --- 20%
Final Exam --- 50%
7
Class-taking technique
9
Introduction to Algorithms
LECTURE1
• Introduction
• Quick Mathematical Overview
• Analysis of Algorithms
• Examples
10
Introduction
11
Why algorithms?
12
Algorithms are the heart of computer science, and their
essential nature is to automate some aspect of collecting,
organizing and processing information.
4. Iterate over L and see which number occurs twice, L[i] > 1.
18
In fact, this solution is not efficient: it wastes both
time and space. How much better can we do?
19
A better solution
What is a solution?
22
Problems and Their Solutions
Problem:
Input Set Output Set
Problem
Instances
Solution:
Algorithm
Computing
Device
Domain Range
23
The Solution is the Algorithm
Problem:
Input Set Output Set
Problem
Instances
Solution:
Algorithm
Computing
Device
Domain Range
24
Designing good algorithms matters.
As computer scientists, we aim to design correct solutions to
problems (does not fail on any input),
design elegant solutions to problems
design efficient solutions (use as few resources as possible),
and
provide performance guarantees where possible (you have
thought carefully about the worst possible behavior).
25
Achieving these goals is not always easy.
Many of the algorithms we’ll see in this
course are clever,
but very few are the most efficient solution
possible.
26
What is an algorithm?
Required Properties?
Finite Domain
Finite Range
Must Terminate
Correctness
Relative to a Computation Device
Including Elementary Operations
28
Properties of an Algorithm
Definiteness:Each instruction is clear and unambiguous.
29
Correctness
31
How to Measure Efficiency
Machine-independent way:
analyze "pseudocode" version of algorithm
assume idealized machine model
one instruction takes one time unit
"Big-Oh" notation
order of magnitude as problem size increases
Worst-case analyses
consider the algorithm’s worst possible behavior, in terms of
resource usage (time & space).
safe, often occurs most often, average case often just as bad
32
How to Measure Efficiency
33
Efficiency
Efficiency is how cost grows with the difficulty of the
instance
“Difficulty” means “size” of the instance
i.e., the number of parameters needed to completely characterize
the instance
Size of a list, matrix, etc.
Example: we expect the cost of sorting a list of 100
numbers to be worse than sorting 10 numbers
34
Modeling the Real World
Cast your application in terms of well-studied abstract data
structures
Concrete Abstract
arrangement, tour, ordering, sequence permutation
cluster, collection, committee, group, packaging, selection subsets
hierarchy, ancestor/descendants, taxonomy trees
network, circuit, web, relationship graph
sites, positions, locations points
shapes, regions, boundaries polygons
text, characters, patterns strings
35
Real-World Applications
36
Some Important Problem Types
Sorting Combinatorial
a set of items find desired permutation,
Searching combination or subset
among a set of items Geometric
String processing graphics, imaging,
robotics
text, bit strings, gene
sequences Numerical
Graphs continuous math: solving
equations, evaluating
model objects and their functions
relationships
37
Algorithm Design Techniques
Brute Force & Dynamic Programming
Exhaustive Search break problem into overlapping
follow definition / try all subproblems
possibilities Greedy
Divide & Conquer repeatedly do what is best now
39
A Quick Mathematical Review
40
A Quick Mathematical Review
f (i) f (a) f (a 1)
i a
f (a 2) ... f (b)
41
A Quick Mathematical Review
Theorem: For any integer n > 0, and any real number, consider
n
a
i 0
i
1 a a 2 ... a n
42
A Quick Mathematical Review
43
A Quick Mathematical Review
44
A Quick Mathematical Review
45
A Quick Mathematical Review
46
A Quick Mathematical Review
Mathematical Induction:
We using induction to prove that some statement concerning the
positive integer is true, we use the following terminology:
Induction base is the proof that the statement is true for
n = 1 (or some other initial value).
Induction hypothesis is the assumption that the
statement is true for an arbitrary n >= 1 (or some other
initial value).
Induction step is the proof that if the statement is true
for n, it must also be true for n +1
47
A Quick Mathematical Review
Combinations
The binomial Theorem states that for any nonnegative
integer n and real numbers a and b,
n
n!
( a b)
n
k 0 k! (n k )!
a k bn k
48
Algorithms Performance Analysis
49
Performance Analysis
Algorithm
Definition: An algorithm is a finite step-by-step list of
well-defined instructions for solving a particular problem.
Algorithm complexity is a key task in comparing the
efficiency of algorithms.
Two factors are considered:
1- the (running) time Time Complexity
2- the space (usage) Space Complexity
50
Performance Analysis
51
Performance Analysis
Key step
The most costly step of the algorithm
Takes most of the running time
In worse-case analysis:
We find the maximum value of the number of the
key steps.
In Average-case analysis:
We find the average value of the number of the key
steps.
53
Performance Analysis
Analyzing An Algorithm
Three steps in algorithm analysis
1- Describe the algorithm precisely
2- Define the input size n.
3- Count the number of key steps in
function of the input size n, I,e., f (n).
54
Performance Analysis
Example#1
Algorithm:
// Search for value in the list of n items x[0], x[1], …, x[n-1]
for (int i=0; i<n; i++)
if (value = = x[i] ) // key step
return true;
else
return false
Input Size n
Key Step Comparison
Worse-case n
Average case n/2 55
Performance Analysis
Example#2
Algorithm
// Finding a minimum of the list of n items x[0], x[1], …, x[n-1]
int min = x[0];
min = x[i];
Input Size n
Key Step Comparison
Worse-case n -1
Average-case n -1
56
Performance Analysis
Example#3
Algorithm
// Summation of n integers
int sum = 0;
sum = sum + x;
return sum
Input Size n
Key Step Addition
Worse-case n
Average-case n
57
Test Yourself
58
Thank you
59