Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Dayschool 2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

EEX4465 –

Data Structures and Algorithms

Day School – 2
by
Shalini Rajasingham
Adapted from
Dr Gehan Anthonys

Bachelor of Software Engineering

Department of Electrical and Computer Engineering


Faculty of Engineering
The Open University of Sri Lanka
Algorithms
• Algorithm is not the complete code or program, it is just the core
logic of a problem, which can be expressed either as an informal
high-level description as pseudocode or using a flowchart.
Properties of Algorithms

Every Algorithm must satisfy the following properties:


1. Input - there should be zero or more inputs supplied externally
to the algorithm.
2. Output - there should be at least one output obtained.
3. Definiteness - every step of the algorithm should be clear and
well defined.
4. Finiteness - the algorithm should have finite number of steps.
5. Feasibility − it should be feasible with the available resources.
6. Independent − it should have step-by-step directions and
should be independent from the programming language.
August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 2
Performance of an Algorithm

• An algorithm is said to be efficient and fast, if it takes less time to


execute and consumes less memory space, and its performance is
measured based on:
o Time Complexity --- is a way to represent the amount of time required by the
program to run till its completion. Always try to keep the time required minimum.

o Space Complexity --- is the amount of memory space required by the


algorithm, during its execution. Space complexity must be taken seriously for
multi-user systems and in situations where limited memory is available.

Asymptotic Analysis:
• When analyzing in terms of time and space, we cannot provide an
exact number to define the time and space required by the algorithm;
• Thus, it is expressed using some standard notations, known
as asymptotic notations. The term ”Asymptotic“ means approaching
a value or curve arbitrarily closely.

August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 3


Time Complexity
• Using asymptotic analysis, we can very well conclude the best-case,
average-case, and worst-case scenario of an algorithm.
• Best Case − Minimum time required for program execution.
• Average Case − Average time required for program execution.
• Worst Case − Maximum time required for program execution.

Types of Asymptotic Notations:


• Three types of asymptotic notations are used to represent the
“growth of any algorithm”, as input increases:

1) Ω - Omega - low 2) Θ – Theta - mid 3) O – Oh -up

T(n)

n
where f(n) and g(n) are operations of an algorithm with the number of input data n and k is a positive constant.

August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 4


Ω (Omega) Θ (Theta) O(Big O)
• Now, when we talk about the speed of an algorithm, we use different notations
to describe how fast it grows as n gets bigger.
• Ω (Omega) notation talks about how fast the algorithm will run at least. It's like
saying, the algorithm won't be slower than this." It gives us a lower bound.
• Θ (Theta) notation gives us a precise idea of how fast the algorithm will run. It's
like saying, "The algorithm will be somewhere around this speed, not slower, not
faster." It gives us both upper and lower bounds, so it's a tight range.
• When we write things like Ω(g(n)) or Θ(g(n)), the g(n) is just some function that
helps us compare. It's like a benchmark. We're saying, “The algorithm will run at
least as fast as g(n)" or “The algorithm will run about as fast as g(n).“
• Big O notation When we say f(n) is O(g(n)), it means that g(n) is an upper bound
on the growth rate of f(n). In other words, as the input size n increases, the
function f(n) will not grow faster than g(n) times some constant factor.

August 13, 2024 EEX4465 -- Data Structures and Algorithms 5


Algorithm Analysis
• In algorithm analysis, g(n) typically represents a function that acts as a reference or
benchmark for comparing the growth rate of another function, f(n).
• When we say f(n) is O(g(n)), Ω(g(n)), or Θ(g(n)), we're essentially comparing the
growth rate of f(n) to the growth rate of g(n).
• If we say f(n) is O(g(n)), we're saying that g(n) is an upper bound on the growth rate of
f(n).
• If we say f(n) is Ω(g(n)), we're saying that g(n) is a lower bound on the growth rate of
f(n).
• If we say f(n) is Θ(g(n)), we're saying that g(n) provides a tight bound on the growth
rate of f(n), meaning f(n) grows at the same rate as g(n).
• In essence, g(n) helps us understand how fast f(n) grows or behaves as the input size
n increases. It serves as a point of comparison to analyze and describe the
performance characteristics of algorithms.

6
August 13, 2024 EEX4465 -- Data Structures and Algorithms
Big O - Notation

• Introduced by Paul Bechmann in 1894, who was a


mathematician, worked on analytic number theory;
• Big O-notation gives an upper bound on a function.

Definition:

O(g(n)) = { f(n): there exist positive constants c and k


such that 0  f(n)  c*g(n) for all n  k }.

August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 7


Rules for calculating Big Oh – value

• For common operations:

Assignment (s  1)
Addition (s + 1) O(1)
Multiplication (s*2)
Comparison (s < 10)

August 13, 2024 EEX4465 -- Data Structures and Algorithms 8


Rules for calculating Big Oh – value
• Ignore constant: Ignore constant factors when determining Big O notation. If an
algorithm's time complexity is 5n2 + 3n + 2 you would simplify it to O(n2), ignoring the
constant factors 5 and 3.
• Drop Non-Dominant Terms: Drop lower-order terms and coefficients of terms that are
less significant as n grows large. For example, if you have 3n3 + 5n2 + 10n + 2 the Big O
notation would be O(n3 ) as the n3 term dominates the growth rate.
• Sum of Multiple Functions: If an algorithm has multiple operations, each with its own
time complexity represented by a function, you consider the function with the highest
growth rate. For example, if one part of the algorithm is O(n) and another part is O(n2),
the overall time complexity would be O(n2)
• Product of Nested Operations: For nested loops or recursive algorithms, you multiply
the time complexities of each operation. For example, if you have two nested loops, one
with O(n) operations and the other with O(n 2) operations, the overall time complexity
would be O(n⋅n2)=O(n3).
• Worst-Case Analysis: Big O notation typically represents the worst-case scenario for
an algorithm's time complexity. It provides an upper bound on the running time of the
algorithm for any input size.

August 13, 2024 EEX4465 -- Data Structures and Algorithms 9


• For sums:
If T1(n) and T2(n) are the running times of two algorithms P1 and P2
and that T1(n) is O(f(n)) and T2(n) is O(g(n)). Then T1(n) + T2(n), the
running time of P1 followed by P2 is O(max(f(n), g(n))).

E.g., Consider steps of running times O(f(n)) and O(g(n)), with


and
Calculate the time complexity if f(n) and g(n) placed sequentially.

Solution:

If n is even If n is odd
f(n)  O(n4) f(n)  O(n2)
g(n)  O(n2) g(n)  O(n3)
Thus,
T1(n) + T2(n)  O(n4) T1(n) + T2(n)  O(n3)

August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 10


• For products:

If T1(n) and T2(n) are O(f(n)) and O(g(n)) respectively,


then T1(n)T2(n) is O(f(n))O(g(n)).

E.g., State the efficiency of the following algorithm w.r.t “printf” statement.

scanf("%d",&n);
for ( i = 0; i < n; i++)
for ( j = 0; j < n; j++)
printf("(%d,%d) \n", i, j);

Solution:

Each for loop iterates O(n) times.


Therefore, the running time is = O(n)*O(n)*O(1) = O(n*n) = O(n2).

August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 11


Fastest to Slowest

August 13, 2024 EEX4465 -- Data Structures and Algorithms 12


E.g., Consider the following expressions of time required for execution
of two algorithms as below and which algorithm takes more time?
T1  (20n2 + 3n - 4) and T2  (n3 + 100n - 2)

When the number of input n increases, the running time is entirely


dependent on n2 and n3, respectively. We can conclude the
running time of second algorithm will grow faster than the first
one.
Meaning: The size of the input (n) increases, the time it takes for the
second algorithm to run will increase at a faster rate compared
to the first algorithm.
Note, we are ignoring the other constants and insignificant parts of the
expressions. Common Asymptotic Notations
constant Ο(1) quadratic Ο(n2)
logarithmic Ο(log n) cubic Ο(n3)
linear Ο(n) polynomial nΟ(1)
n log n Ο(n log n) exponential 2Ο(n)

August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 13


Some advanced examples:

August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 14


:

Exercises

August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 15


Ex 5: The factorial of a given number n can be found as below. Find the
corresponding running time T(n).

Solution:
We can find the running time as follow:

where and are arbitrary constants.

Note:
• Best, worst, and average are difficult to deal with precisely because
the details are very complicated;
• It easier to talk about upper and lower bounds of the function;
• Asymptotic notations (Ω; Ѳ; O) are also can be used for practically
deal with complexity functions.

August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 16


Space Complexity

Whenever a solution to a problem is written some memory is required to


complete. An algorithm generally requires space for the components:
• Instruction Space: is the memory space required to store the
executable version of the instructions.
• Data Space: is the space required to store all the constants and
variables (including temporary variables) value.
• Environment Space: is the space required to store the environment
information needed to resume the suspended function.
E.g., if a function A() inside calls function B() then all variables in A() will get
stored on the system stack temporarily, while B() is executed inside
A().

Note: when calculating the Space Complexity, we usually consider only


Data Space and neglect the other two types.

August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 17


Do you have any questions/clarifications?

… few minutes.

August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 18


More on Algorithms
Now let's try to learn algorithms-writing:
• Design an algorithm to add two numbers and display the result.
Method 1: Method 2:

• There are many solutions for a given problem. How can we


decide the better one?
- you can perform the time and space complexity analyses.

Any algorithm can be presented by


using a pseudocode or a flow chart.
August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 19
Pseudocode Representation

• One of the popular representation of Algorithm;


• A description of a computer programming algorithm that
uses the structural conventions of programming languages
but omits detailed subroutines or language-specific syntax.
• Widely choosen because:
– Easy to read and write;
– Allow the programmer to concentrate on the logic of the
problem;
– Structured in English language.

August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 20


Rules for Pseudocode: (you should follow these rules)
• Write only one statement per line;
• Capitalize initial keyword;
• Multiline structural elements come in pairs;
e.g., for every BEGIN there is an END, for every IF there is an ENDIF.
• Indent to show hierarchy (i.e., identify of control structures);
e.g., BEGIN
IF
...
ENDIF
END
• Groups of statements may be formed into modules, and
that group given a name;
• Keep statements language independent.

August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 21


Keywords
• Input: READ, OBTAIN, GET
• Output: PRINT, DISPLAY, SHOW
• Compute: COMPUTE, CALCULATE, DETERMINE
• Initialize: SET, INIT
• Add one: INCREMENT
• Subtract one: DECREMENT, etc.

Structures
• For each procedure or subroutine
BEGIN name
END name
• For single selection
IF condition THEN
statements
ELSE
statements
ENDIF
August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 22
Structures
• For multiway selection
CASEWHERE expression evaluates to
A: process A
B: process B
………
OTHERWISE process alternative
ENDCASE
• For pre test repetition • For post test repetition

WHILE condition REPEAT


statements statements
ENDWHILE UNTIL condition

• For Loop
FOR variable = start TO finish STEP increments
statements
NEXT variable

August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 23


Flowcharts representation
• This is a type of diagram representing a process using different
symbols containing information about steps or a sequence of events.
• Each of these symbols is linked with arrows to illustrate the flow
direction of the process.

August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 24


Now, lets see how pseducodes and flowcharts are used in applications.

Example 1: Adding two numbers

August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 25


Example 2: Single selection

or

August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 26


Example 3: Write a pseudocode and draw the flowchart to decide whether it
is eligible to sit the final exam based on CA mark.

Solution:
IF CA mark >= 40 THEN
Display “Eligible to sit the final exam”
ELSE
Display “Not eligible to sit the final exam”
ENDIF

August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 27


Example 4: Multi-way selection

August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 28


Do you have any questions/clarifications?

August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 29

You might also like