You are on page 1of 9

10/13/2020

+ +
TOPICS

DATA STRUCTURES VS ALGORITHMS

ROLE OF ALGORITHMS IN COMPUTING

BIG-O NOTATION
Introduction to Data Structures and Algorithms
Data Structures PSEUDOCODES

+ +
TOPICS DATA STRUCTURES VS ALGORITHMS

DATA STRUCTURES VS ALGORITHMS Data Structure

ROLE OF ALGORITHMS IN COMPUTING  Is a way to store and organize data in order to


facilitate access and modifications.
BIG-O NOTATION  Is used to denote a particular way of
organizing data for types of operations.
PSEUDOCODES

+ +
Example of Data Structure DATA STRUCTURES VS ALGORITHMS

Animal Classification Book Composition


Algorithm

A finite sequence of instructions, each of which


has a clear meaning and can be performed with
a finite amount of effort in a finite length of
time.
 A simple set of instructions to be followed to
solve a problem.

1
10/13/2020

+ +
DATA STRUCTURES VS ALGORITHMS Example of Algorithm

Algorithm Email Checking/ Validation Baking a Cake

A sequence of computational steps that


transforms the input into output.
 A tool for solving a well-specified
computational problem.

+ +
TOPICS ROLE OF ALGORITHMS IN COMPUTING

DATA STRUCTURES VS ALGORITHMS Kinds of Problems solved by Algorithms

ROLE OF ALGORITHMS IN COMPUTING  The Human Genome Project


i. Identify all the 100,000 genes in human DNA.
BIG-O NOTATION ii. Determine the sequences of the 3 billion
chemical base pairs that make up human DNA.
PSEUDOCODES

+ +
ROLE OF ALGORITHMS IN COMPUTING ROLE OF ALGORITHMS IN COMPUTING

Kinds of Problems solved by Algorithms Kinds of Problems solved by Algorithms

 The Internet  Electronic Commerce


i. Enables people all around the world to quickly i. The core technologies used in electronic
access and retrieve large amounts of information commerce include public-key cryptography and
. digital signatures which are based on numerical
ii. Sites on the Internet can manage and manipulate algorithms and number theory.
this large volume of data.

2
10/13/2020

+ +
ROLE OF ALGORITHMS IN COMPUTING ROLE OF ALGORITHMS IN COMPUTING

Kinds of Problems solved by Algorithms Show how to solve specific problems:

 Manufacturing and other Commercial  Determine shortest route


Enterprises i. Given a roadmap on which the distance is
i. allocate scarce resources in the most beneficial between each pair of adjacent intersection is
way. marked.
 Finding the common subsequence
i. X = {x1; x2; : : : ; xm) and Y = (y1; y2; : : : ; yn).

+ +
ROLE OF ALGORITHMS IN COMPUTING ROLE OF ALGORITHMS IN COMPUTING

Algorithms as Technology Insertion Sort Merge Sort

Takes time roughly equal to Takes time roughly equal to


c1n2 to sort n items, where c1 is c2n lg n, where lg n stands for
a constant that does not log2 n and c2 is another
 Efficiency depend on n. That is, it takes constant that does not depend
i. Different algorithms devised to solve the same time roughly proportional to on n.
n2.
problem often differ dramatically in their
efficiency.
ii. These differences can be much more significant
than differences due to hardware and software.

+ +
ROLE OF ALGORITHMS IN COMPUTING ROLE OF ALGORITHMS IN COMPUTING

For a concrete example, let us pit a faster  Suppose that computer A executes 10
computer (computer A) running insertion billion instructions per second (faster than
sort against a slower computer (computer any single sequential computer at the time
B) running merge sort. They each must of this writing) and computer B executes
sort an array of 10 million numbers. only 10 million instructions per second, so
that computer A is 1000 times faster than
computer B in raw computing power.

3
10/13/2020

+ +
ROLE OF ALGORITHMS IN COMPUTING ROLE OF ALGORITHMS IN COMPUTING

To make the difference even more Suppose further that just an average
dramatic, suppose that the world’s programmer implements merge sort, using
craftiest programmer codes insertion sort a high-level language with an inefficient
in machine language for computer A, and compiler, with the resulting code taking
the resulting code requires 2n2 instructions 50n lg n instructions.
to sort n numbers.

+ +
ROLE OF ALGORITHMS IN COMPUTING ROLE OF ALGORITHMS IN COMPUTING

To sort 10 million numbers, computer A To sort 10 million numbers, computer B
takes takes

+ +
ROLE OF ALGORITHMS IN COMPUTING ROLE OF ALGORITHMS IN COMPUTING

By using an algorithm whose running time The advantage of merge sort is even more
grows more slowly, even with a poor pronounced when we sort 100 million
compiler, computer B runs more than 17 numbers: where insertion sort takes more
times faster than computer A! than 23 days, merge sort takes under four
hours. In general, as the problem size
increases, so does the relative advantage
of merge sort.

4
10/13/2020

+ +
TOPICS BIG-O NOTATION

DATA STRUCTURES VS ALGORITHMS Asymptotic Notation


 describes the behavior of the time or space
ROLE OF ALGORITHMS IN COMPUTING complexity for large instance characteristics.

BIG-O NOTATION The notation f(n) = O(g(n)) read as “f(n) is big oh of


g(n)” means that f(n) is asymptotically smaller than or
PSEUDOCODES equal to g(n).

+ +
BIG-O NOTATION BIG-O NOTATION

Big-Oh Notation Most common Big-Oh Notation


 used for run-time complexity analysis.
 describes the worst-case scenario and can
 O(1) Constant
be used to describe the execution time i. Describes an algorithm that will always execute in
required or the space used by an algorithm. the same time (or space) regardless of the size of
the input data set.
ii. The operation doesn’t depend on the size of its
input.

+ +
BIG-O NOTATION BIG-O NOTATION

Most common Big-Oh Notation Most common Big-Oh Notation

 O(1) Constant  O(n) Linear


A = b + c; i. Describes an algorithm whose performance will
grow linearly and in direct proportion to the size
of the input data set.
ii. The run time complexity is proportionate to the
size of n.

5
10/13/2020

+ +
BIG-O NOTATION BIG-O NOTATION

Most common Big-Oh Notation Most common Big-Oh Notation

 O(n) Linear  O(log n) Logarithmic


double max = a[0]; i. Barely slower than a constant-time program.
for (int i = 1; i < N; i++) ii. Normally associated with algorithms that break the
if (a[i] > max) max = a[i]; problem into smaller chunks per each invocation.

+ +
NOTATION BIG-O NOTATION

Most common Big- Most common Big-Oh Notation


Oh Notation
 O(n log n) Linearithmic
 O(log n) Logarithmic
i. Describe programs whose running time for a
problem of size N has order of growth N log N.
ii. Taking up to time proportional to n log(n) to run
on inputs of size n.

+ +
BIG-O NOTATION BIG-O NOTATION

Most common Big-Oh Notation Most common Big-Oh Notation

 O(n log n) Linearithmic  O(n2) Quadratic


i. Represents an algorithm whose performance is
directly proportional to the square of the size of
the input data set.

6
10/13/2020

+ +
BIG-O Notation BIG-O Notation

Most common Big-Oh Notation Most common Big-Oh Notation

 O(n2) Quadratic  O(n3) Cubic


for (int i = 0; i < N; i++) i. A typical program whose running time has order of
for (int j = i+1; j < N; j++) growth N3 has three nested for loops, used for
if (a[i] + a[j] == 0) some calculation involving all triples of N
elements.
cnt++;

+ +
BIG-O NOTATION BIG-O NOTATION

Most common Big-Oh Notation Most common Big-Oh Notation

 O(n3) Cubic  O(2n) Exponential


for (int i = 0; i < N; i++) i. Exponential algorithms are extremely slow—you
for (int j = i+1; j < N; j++) will never run one of them to completion for a
for (int k = j+1; k < N; k++) large problem.
if (a[i] + a[j] + a[k] == 0)
cnt++;

+ +
BIG-O NOTATION TOPICS

DATA STRUCTURES VS ALGORITHMS

ROLE OF ALGORITHMS IN COMPUTING

BIG-O NOTATION

PSEUDOCODES

7
10/13/2020

+ +
PSEUDOCODES PSEUDOCODES

Is a plain language description of steps in For the most part interpreting the
an algorithm or another system. pseudocode is trivial as it looks very much
like a more abstract C++, or C#:
Often uses structural conventions of
normal programming language, but is
intended for human reading rather than
 Pre-conditions should always be enforced.
machine reading.

+ +
PSEUDOCODES PSEUDOCODES

For the most part interpreting the For the most part interpreting the
pseudocode is trivial as it looks very much pseudocode is trivial as it looks very much
like a more abstract C++, or C#: like a more abstract C++, or C#:

 Post-conditions represent the result of  The type of parameters is inferred.


applying algorithm a to data structured.

+ +
PSEUDOCODES PSEUDOCODES

For the most part interpreting the All algorithms start with a simple algorithm
pseudocode is trivial as it looks very much signature, e.g.
like a more abstract C++, or C#:
1) algorithm AlgorithmName(arg1, arg2, ...,
argN)
 All primitive language constructs are 2) ...
explicitly begun and ended. n) end AlgorithmName

8
10/13/2020

+ +
PSEUDOCODES PSEUDOCODES

Immediately after the algorithm signature we list any Pre algorithm Add(value)
Pre: value is the value to add to the list
or Post conditions. Post: value has been placed at the tail of the list
1) algorithm AlgorithmName(n) n ← node(value)
if head = ∅
2) Pre: n is the value to compute the factorial of head ← n
3) n≥0 tail ← n
else
4) Post: the factorial of n has been computed tail.Next ← n
tail ← n
5) // ... end if
n) end AlgorithmName end Add

+
PSEUDOCODES

PROGRAM CleanTheFridge
for EACH item in the fridge DO
IF(the item is rotten) THEN
throw away the item
ELSE
put the item back in the fridge
END
END
Make Dinner
END

You might also like