0% found this document useful (0 votes)
9 views25 pages

03 Lecture Three

Uploaded by

kkmma283
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)
9 views25 pages

03 Lecture Three

Uploaded by

kkmma283
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/ 25

By

Dr. Ahmed Taha


Lecturer, Computer Science Department,
Faculty of Computers & Informatics,
Benha University Lecture 3
1
Lecture Three

Preliminaries II

2
3
4
Book Title:
First Course in Algorithms Through Puzzles

Authors:
Ryuhei Uehara

Publisher:
Springer

Edition:
2019
5
Efficiency of
Algorithm
• The efficiency of a computation is the resources
required to perform the computation.

• More precisely, time and space required to compute.

• These terms are known as time complexity and space


complexity in the area of computational complexity.
6
Evaluation of
Algorithms on Turing
Machine Model

7
Evaluation of
Algorithms on Turing
Machine Model
• We assume that each basic operation (reading/writing a
letter on the tape, moving the head, and changing its
state) takes one unit of time.

• Therefore, for a given input, its time complexity is defined


by the number of times the loop is repeated, and its space
complexity is defined by the number of checked cells on
the tape.
8
Evaluation of
Algorithms on RAM
Model

9
Evaluation of
Algorithms on RAM
Model
• The time complexity is defined by the number of times the program counter
PC is updated.

• Time to access memory:


▪ In the RAM model, one data element in any memory cell can be read/
written in one unit of time. This is why it is known as “Random Access”
▪ In a Turing machine, if it is necessary to read a data element located far
from the current head position, it takes time because the machine has to
move the head one by one.
▪ On the other hand, for the RAM model machine, When the address of the
data is given, the machine can read or write to the address in one step.

10
Evaluation of
Algorithms on RAM
Model
• One unit of memory:
▪ The RAM model deals with the data in one word in one step.
▪ If it has n words in memory, to access each of them requires
log n bits to access it uniquely.
▪ In the RAM model, “one word” means “log n bits,” where n is
the number of memory cells.
▪ On the Turing machine, one bit is a unit.
▪ Therefore, some algorithms seem to run log n times faster on
the RAM model than on the Turing machine model.

11
Computational
Complexity in the
Worst Case
• An algorithm computes some output from its input in
general.
• The computational complexity is the resources required
for the computation.
• Usually, we consider two computational complexities:
▪ Time complexity is the number of steps required for the
computation.
▪ Space complexity is the number of words (or bits in
some case) required for the computation
12
Computational
Complexity in the
Worst Case
• Example:
▪ If We consider the computational complexity of a program
running on a computer for some specific input x.

▪ We first fix the machine model and a program that runs on the
machine, and then give some input x.

▪ Then, its time complexity is given by the number of steps, and


space complexity is given by the number of memory cells.

13
Computational
Complexity in the
Worst Case
• Suppose we want to compare two algorithms A and B.
• Then, we decide which machine model to use, and
implement the two algorithms on it as programs, say PA
and PB.
• It is not a good idea to compare them for every input x.
• Sometimes A runs faster than B, but B may run faster
more often.
14
Computational
Complexity in the
Worst Case
• Therefore, we measure the computational complexity
of an algorithm by the worst case scenario for each
length n of input.
• For each length n, we have 2n different inputs from
000 . . . 0 to 111 . . . 1.
• Then we take the worst computation time and the
worst computation space for all of the inputs of length
n.
15
Computational
Complexity in the
Worst Case
• Time complexity of an algorithm A:
Let PA be a program that realizes algorithm A. Then, the time
complexity t(n) of the algorithm A is defined by the longest
computation steps of PA under all possible inputs x of length n.

Example For the time complexities of two algorithms A and B, say


tA(n) and tB(n), if we have tA(n) < tB(n) for every n, we say that
algorithm A is faster than B.

16
Data Structures
• Data structures continue to be one of the major active research
areas.
• In theoretical computer science, many researchers investigate
and develop fine data structures to solve some problems more
efficiently, smarter, and simpler.
• Both Data structure and Algorithm are fundamental building
blocks of any software application. They are two side of the
same coin.
• Niklaus Wirth wrote a book titled
“Algorithms + Data Structures = Programs”
17
Data Structures

18
Data Structures

• Array

19
Data Structures

• Array

20
Data Structures

• Subroutines and functions

21
Data Structures

• Subroutines and functions

22
Data Structures

• Multi-dimensional Array

23
Data Structures

• Multi-dimensional Array

24
25

You might also like