Week 01 Introduction to algorithms course - Shohag Barman
Week 01 Introduction to algorithms course - Shohag Barman
INTRODUCTION
1
Course Objectives
2
AIUB::CSC2105::Algorithm
Course Prerequisites
• Programming
– Data types, operations
– Conditional statements
– Loops
– Procedures and functions
– C/ C++/ Java
• Discrete Mathematics (proof theorems)
• Data Structures (array, structure, pointer, file, etc...)
• Computer lab (edit, compile, execute, debug)
• If you lack of any of the above please refine yourselves.
3
AIUB::CSC2105::Algorithm
Importance of the course
4
AIUB::CSC2105::Algorithm
Course Contents
5
Resources & References
6
7
Course Evaluation
Midterm Quiz (Best 1 out of 2) 20
Laboratory Performance 20
Assignment 10
Class Attendance 10
Midterm Written Exam 40
Midterm Total 100 40%
Final term Quiz (Best 1 out of 2) 20
Laboratory Performance 20
Assignment 10
Class Attendance 10
Final term Written Exam 40
Final Term Total 100 60%
Grand Total Final Grade of the Course 100
8
Course Evaluation ( Online Semester )
Marking system for Theory and Lab Marking system for Theory and Lab (Final
(Midterm) Term)
Quizzes 20% Quizzes 20%
Lab Performance 20% Lab Performance 20%
Attendances 10% Attendances 10%
Theory Quizzes (MCQ) 10% Theory Quizzes (MCQ) 10%
Lab Quizzes (MCQ) 10% Lab Quizzes (MCQ) 10%
Assignments 10% Assignments 10%
9
Welcome to the
course
Algorithms
AIUB::CSC2105::Algorithm Lecture01-Introduction 10
??????
11
The Goals of this Course
• To think algorithmically
• To understand and learn the idea behind algorithm
design techniques
• To get to know a toolbox of classical algorithms.
• To reason (in a precise and formal way) about the
efficiency and the correctness of algorithms.
12
AIUB::CSC2105::Algorithm
??????
What is
Algorithm?
13
AIUB::CSC2105::Algorithm
Informally
produces
takes some some value
value or set Algorithm: any well- or set of
of values as defined values, as
input computational output
procedure
• An algorithm is thus a sequence of computational steps that transform the input into the
output.
• Solving a given problem:
– Data structure: Organization of data to solve the problem at hand.
– Algorithm: Outline, the essence of a computational procedure, step-by-step instructions.
– Program: Implementation of an algorithm in some programming language.
14
AIUB::CSC2105::Algorithm
Kinds of Problem to be solved
• Sorting and Searching are the basic and most common computational
problem.
• Clever algorithms are employed for the Internet
– to manage large volume of data transfer.
– Finding good routes on which the data will travel.
– Search engine to quickly find requested pages.
– Etc…
15
AIUB::CSC2105::Algorithm
How to Develop an Algorithm?
16
AIUB::CSC2105::Algorithm
??????
17
AIUB::CSC2105::Algorithm
Algorithm
Analysis
18
AIUB::CSC2105::Algorithm
Analysis of Algorithms
• Efficiency:
– Running time
– Space used
• Efficiency as a function of the input size:
– Number of data elements (numbers, points).
– The number of bits of an input number .
19
AIUB::CSC2105::Algorithm
The RAM Model
20
AIUB::CSC2105::Algorithm
The RAM model (cont.)
21
AIUB::CSC2105::Algorithm
Example
Algorithm arrayMax(A, n)
#
operations
currentMax A[0] 2
for (i =1; i<n; i++) 2n
(i=1 once, i<n n times, i++ (n-1) times)
if A[i] currentMax then 2(n 1)
currentMax A[i] 2(n 1)
return currentMax 1
Total 6n
1
22
AIUB::CSC2105::Algorithm
Insertion Sort
23
AIUB::CSC2105::Algorithm
Loop invariants and the correctness of insertion sort
• Loop Invariant: is a property of a program loop that is true before (and after)
each iteration.
• Initialization: It is true prior to the first iteration of the loop.
24
AIUB::CSC2105::Algorithm
Analysis of Insertion Sort
• Time to compute the running time as a function of the input size (exact
analysis).
cost times
for j := 2 to n do c1 n
key := A[j] c2 n-1
// Insert A[j] into A[1..j-1] 0 n-1
i := j-1 c3 n-1
n
while i>0 and A[i]>key do j 2
tj
c4
n
A[i+1]:=A[i] j 2
(t j 1)
c5
n
i-- j 2
(t j 1)
A[i+1]:=key c6 n-1
c7
25
Analysis of Insertion Sort
+ c7 (n-1)
26
…Analysis of Insertion Sort
• Performance often draws the line between what is feasible and what is impossible.
• Often it is sufficient to count the number of iterations of the core (innermost) part.
– No distinction between comparisons, assignments, etc (that means roughly the same cost for all of them).
– Gives precise enough results.
• In some cases the cost of selected operations dominates all other costs.
– Disk I/O versus RAM operations.
– Database systems.
28
AIUB::CSC2105::Algorithm
Best/ Worst/ Average Case
• Best case: works fast on some input.
• Worst case: (usually) maximum time of algorithm on any input of size.
• Average case: (sometimes) expected time of algorithm over all inputs of size. Need
assumption of statistical distribution of inputs.
AIUB::CSC2105::Algorithm Introduction29
…Best/ Worst/ Average Case
5n
4n best-case
3n
2n
1n
1 2 3 4 5 6 7 8 9 10 11 12 …..
30
AIUB::CSC2105::Algorithm
Best/ Worst/ Average Case
31
AIUB::CSC2105::Algorithm
Asymptotic Notation
• Asymptotic Notations are languages that allow us to analyze an algorithm's
running time by identifying its behavior as the input size for the algorithm
increases.
• This is also known as an algorithm's growth rate.(Wiki)
32
AIUB::CSC2105::Algorithm
Asymptotic Notation
Running Time
• Used for worst-case analysis
n0 Input Size
We write f(n) = O(g(n)), If there are positive constants n0 and c such that, to the
right of n0 the f(n) always lies on or below c*g(n).
33
AIUB::CSC2105::Algorithm
• Example 1: f(n)=2n+3 , g(n)=n
• Now, we have to find Is f(n)=O(g(n))?
• To check f(n)=O(g(n)), it must satisfy the given
condition:
• f(n)<=c.g(n)
• 2n+3 <= c.n
• Let's assume c=5, n=1 then
• 2*1+3<=5*1
• 5<=5
34
Asymptotic Notation
• The “big-Omega” W-Notation
– asymptotic lower bound
– f(n) = W(g(n)) if there exists constants c>0 and n0>0, s.t. f(n) ≥ c g(n) for n ³ n0
Running Time
f (n )
c g ( n )
n0 Input Size
We write f(n) = Ω(g(n)), If there are positive constants n0 and c such that, to the right
of n0 the f(n) always lies on or above c*g(n).
Ω(g(n)) = { f(n) : There exist positive constant c and n0 such that 0 ≤ c g(n) ≤ f(n), for all
n ≥ n0}
35
...Asymptotic Notation
• The “big-Theta” Q-Notation
– asymptoticly tight bound
– f(n) = Q(g(n)) if there exists constants c1>0, c2>0, and n0>0, s.t. for n ³ n0 c1 g(n) £ f(n) £ c2 g(n)
Running Time
f (n )
c1 g (n )
n0 Input Size
We write f(n) = Θ(g(n)), If there are positive constants n0 and c1 and c2 such that, to
the right of n0 the f(n) always lies between c1*g(n) and c2*g(n) inclusive. Θ(g(n)) = {f(n) :
There exist positive constant c1, c2 and n0 such that 0 ≤ c1 g(n) ≤ f(n) ≤ c2 g(n), for all n
≥ n0}
36
AIUB::CSC2105::Algorithm
Asymptotic Notation
37
AIUB::CSC2105::Algorithm
Time complexity
• Int n0, n1, c;
n0 = 10;
n1 = 10;
C = n0+n1;
O(1)
Int i, n, r;
r=0;
for(i=1; i<=n;i++){
r=r+1;
}
1- 2 (+ and = )
2- 4
• O(2n)-> O(n)
38
Time complexity(Cont.)
int i, j, n;
c=0;
for(i=0;i<n;i++){
for(j=0; j<n;j++){
c =c+1;
}
}
1-1
2-4
O(n2)
39
Space Complexity
40
Time complexity familiar tasks
41
AIUB::CSC2105::Algorithm
Correctness of Algorithms
42
AIUB::CSC2105::Algorithm
Partial and Total Correctness
– Partial correctness
IF this point is reached, THEN this is the desired output
Total correctness
INDEED this point is reached, AND this is the desired output
43
AIUB::CSC2105::Algorithm
Assertions
subroutine (INPUT).
subroutine (OUTPUT).
44
AIUB::CSC2105::Algorithm
Pre/post-conditions
• Example:
– Write a pseudocode algorithm to find the two smallest numbers in a sequence
of numbers (given as an array).
45
AIUB::CSC2105::Algorithm