0% found this document useful (0 votes)
14 views22 pages

Lec - 1 Intro

Uploaded by

amrayyad728
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views22 pages

Lec - 1 Intro

Uploaded by

amrayyad728
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

CS313

Dr. Sondos Fadl


 What is an Algorithm?
It is a method or process (some steps) adopted to solve a problem.

 What is the Problem in CS?


It is a task to be performed, it consist of inputs and outputs.
 What is a program?
It is an instance of an algorithm in a certain programming language.
Problem

Quality
Solution = Program
Time
𝑺𝟏 𝑺𝟐 𝑺𝟑 𝑺𝟒
𝒕𝟏 𝒕𝟐 𝒕𝟑 𝒕𝟒 Storage Space
𝟐𝟑𝒔 𝟏𝟓𝒔 𝟏𝟎𝒔 𝟏𝟗𝒔
Before & After!

 Before, 𝑺𝟏  𝑪𝟏 𝑺𝟐  𝑪𝟐 𝑺𝟑  𝑪𝟑 𝑺𝟒  𝑪𝟒

 Algorithm Analysis
Some methods are able to make analysis without coding and without the
need for PC.
Before algorithm: Write code to solve problem Save resources
After algorithm: Write EFFICIENT code to solve problem Save time
Save money
importance
Think Design Analyze

By study By study By study


different different how to
algorithms design analyze
/ methods the
problems methods
Analysis & Design of Algorithms

Analysis Design

Statements, Recursion Divide & Greedy


Dynamic
Conditions & Conquer Method
(2 methods) Programming
Loops
Solve problems

Discuss other
Sections
algorithms
Our course

Assignments
on the time,
1 no Late!
Assignments -1
5

10
Quizzes 10
Evaluations
Mid-term 15

Final 60
 The running time of an algorithm on a particular input is
the number of primitive operations or “steps” executed.

 It is convenient to define the notion of “step” so that it is as


machine-independent as possible.
 For example: a constant amount of time is required to execute each line
of our Algorithm. One line may take a different amount of time than
another line, but we shall assume that each execution of the 𝑖th line
takes time 𝑡, where 𝑡 is a constant.
 This method reflects how the algorithm would be implemented on most
actual computers.
 This simpler notation will also make it easy to determine whether one
algorithm is more efficient than another.
 Worst case
 Asymptotic analysis (with large inputs)
 Let two independent consecutive statements are P1 and P2. Let t1 be the cost of
running P1 and t2 be the cost of running P2. The total cost of the program is the
addition of cost of individual statement i.e. t1+t2

 Assume that statement 2 is independent of statement 1 and statement 1 executes


first followed by statement 2. The total running time is 𝑇 𝑛 = 𝑛 + 𝑛2
 The loop body is executed 10 times. If it takes m operations to run the
body, the total number of operations is 10×m=10m. In general, if the
loop iterates n times and the running time of the loop body are m, the
total cost of the program is n∗m.
 Please note that we are ignoring the time taken by expression i<10
and statement i++. If we include these, the total time becomes

𝑇 𝑛 = 1 + 𝑛 + 𝑛 + 1 + 𝑚𝑛 = 2 + 2𝑛 + 𝑚𝑛
 In this analysis, we made one important assumption. We assumed that the
body of the loop doesn’t depend on i. Sometimes the runtime of the body
does depend on i. In that case, our calculation becomes a little bit difficult.
 In the for loop above, the control goes inside the if condition only when i is
an even number. That means the body of if condition gets executed n/2
times. The total cost is therefore

𝑇 𝑛 = 𝑛2 × 𝑛 2 = 𝑛3 2
 There are two for loops, each goes n times. So the total cost is

𝑇 𝑛 = 𝑛 × 𝑛 × 𝑛 = 𝑛3
 while loops are usually harder to analyze than for loops because there is no
obvious a priori way to know how many times we shall have to go round the
loop. One way of analyzing while loops is to find a variable that goes
increasing or decreasing until the terminating condition is met.
 How many times the loop repeats? In every iteration, the value of i gets
halved. If the initial value of i is 16, after 4 iterations it becomes 1
(𝐥𝐨𝐠 𝟐 𝟏𝟔 = 𝟒 ) and the loop terminates. The implies that the loop repeats
𝐥𝐨𝐠 𝟐 𝒊 times. In each iteration, it does the n work. Therefore the total cost is
𝑇 𝑛 = nlog 2 𝑖
int sum(int a, int b) {
int c = a + b; 1
return c 1
}

𝑇 𝑛 =1+1=2
int array_sum(int a, int n) {
int i; 1
(variable declaration and assignment) int sum = 0; 2 Initial 1
for (i = 0; i < n; i++) { Increment n
sum = sum + a[i] n Condition n+1
}
return sum; 1
}

𝑇 𝑛 = 1 + 2 + 1 + 𝑛 + 𝑛 + 1 + 𝑛 + 1 = 5 + 3𝑛
int sum = 0; 1
for (i = 0; i < n; i++) { n
for (j = 0; j < n; j++) { n
for (k = 0; k < n; k++) { n
if (i == j == k) {
𝒏𝟒 𝒏𝟑 for (l = 0; l < n; l++) { n
𝒏𝟐
n sum = i + j + k + l; 𝟏
}
}
}
}
}
𝑇 𝑛 = 1 + 𝑛4 = 𝑛4
max=-∞ 1
Initial 1
for i=1 to n Increment n
Condition n+1
if A(i)>max
1 1
4n Indexing 1
max=A(i)
Condition 1
end if
end for
return max 1

𝑇 𝑛 = 1 + 1 + 𝑛 + 𝑛 + 1 + 4𝑛 + 1 = 6𝑛 + 4
Initial 1
for i=2 to i<=𝒏𝟐 Increment size  (𝒏𝟐 -1)
Condition size+1  (𝒏𝟐 -1) +1
(𝒏 -1)
𝟐
print “hello”
(𝒏𝟐 -1) print (i)
end for

𝑇 𝑛 = 1 + 𝑛2 − 1 + 𝑛2 − 1 + 1 + 𝑛2 − 1 + 𝑛2 − 1

𝑇 𝑛 = 4𝑛2 -2

You might also like