03 Lecture Three
03 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.
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.
9
Evaluation of
Algorithms on RAM
Model
• The time complexity is defined by the number of times the program counter
PC is updated.
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.
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.
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
21
Data Structures
22
Data Structures
• Multi-dimensional Array
23
Data Structures
• Multi-dimensional Array
24
25