Sunteți pe pagina 1din 23

ECE 3060 – Data Structures and

Algorithms
Lecture #1

Dec 4, 2019 Myanmar Institute of Information Technology 1


Introduction to Data Structures
• Data Structure is a way of collecting and organizing data in
such a way that can perform operations on these data in an
effective way.
• Data Structure is a systematic way to organize data in order to
use it efficiently.
• A data structure is a collection (grouping) of simple or
structured data types and a set of rules (operations) for
organizing and accessing the collection. Some examples of
data structures would be: an array, lists, stacks, queues, trees.
• The foundation terms of a data structure:
• Interface: Interface represents the set of operations that a data
structure supports.
• Implementation: Implementation provides the definition of the
algorithms used in the operations of the data structure.

Dec 4, 2019 Myanmar Institute of Information Technology 2


Characteristics of a Data Structure
• Correctness − Data structure implementation
should implement its interface correctly.
• Time Complexity − Running time or the execution
time of operations of data structure must be as
small as possible.
• Space Complexity − Memory usage of a data
structure operation should be as little as possible.

Dec 4, 2019 Myanmar Institute of Information Technology 3


Need for Data Structure
• As applications are getting complex and data rich,
there are three common problems:
• Data Search
• Processor speed
• Multiple requests

Dec 4, 2019 Myanmar Institute of Information Technology 4


Two types of Data Structures

1. Linear data structure


2. Non linear data structure

Dec 4, 2019 Myanmar Institute of Information Technology 5


Linear data structure

• A data structure is said to be linear if the elements


form a sequence.
Array, Stack, Linked list, Queue

Dec 4, 2019 Myanmar Institute of Information Technology 6


Non Linear data structure

• A data structure is said to be nonlinear if the


elements in a nonlinear data structure do not form
a sequence.
Trees and Graphs

Dec 4, 2019 Myanmar Institute of Information Technology 7


What is an Algorithm ?
• An algorithm is a finite set of instructions or logic, written in
order, to accomplish a certain predefined task. Algorithm is
not the complete code or program, it is just the core
logic(solution) of a problem, which can be expressed either as
an informal high level description as pseudocode or using a
flowchart.
• Every Algorithm must satisfy the following properties:
• Input- There should be inputs supplied externally to the algorithm.
• Output- There should be at least 1 output obtained.
• Definiteness- Every step of the algorithm should be clear and well
defined.
• Finiteness- The algorithm must terminate after a finite number of
steps.
• Correctness- Every step of the algorithm must generate a correct
output.

Dec 4, 2019 Myanmar Institute of Information Technology 8


What is an Algorithm ?
• An algorithm is said to be efficient and fast, if it
takes less time to execute and consumes less
memory space. The performance of an algorithm is
measured on the basis of following properties :
• Space Complexity
• Time Complexity

Dec 4, 2019 Myanmar Institute of Information Technology 9


Space Complexity
• Its the amount of memory space required by the
algorithm, during the course of its execution. Space
complexity must be taken seriously for multi-user systems
and in situations where limited memory is available.
• An algorithm generally requires space for following
components :
• Instruction Space: Its the space required to store the executable
version of the program. This space is fixed, but varies depending
upon the number of lines of code in the program.
• Data Space: Its the space required to store all the constants and
variables(including temporary variables) value.
• Environment Space: Its the space required to store the
environment information needed to resume the suspended
function.

Dec 4, 2019 Myanmar Institute of Information Technology 10


Space Complexity of Algorithms
• Whenever a solution to a problem is written some memory
is required to complete. For any algorithm memory may be
used for the following:
 Variables (This include the constant values,
temporary values)
 Program Instruction
 Execution
• Space complexity is the amount of memory used by the
algorithm (including the input values to the algorithm) to
execute and produce the result.
• Auxiliary Space is confused with Space Complexity. Auxiliary
Space is the extra space or the temporary space used by the
algorithm during it's execution.
• Space Complexity = Auxiliary Space + Input space
Dec 4, 2019 Myanmar Institute of Information Technology 11
Calculating Space Complexity
Type Size
bool, char 1 byte
short 2 bytes
int, float 4 bytes
double 8 bytes
int sum( int a[], int n )
{
{ int x = 0;
int z = a + b + c; for(int i = 0; i < n; i++)
return(z); {
} x = x + a[i];
}
return(x);
Space Complexity: 20 bytes }
Constant Space Complexity
Space Complexity: 4n + 12 bytes
Linear Space Complexity: increasing
linearly with the increase in the input
Dec 4, 2019 valueTechnology
Myanmar Institute of Information n 12
Time Complexity of Algorithms

• Time complexity of an algorithm signifies the total time


required by the program to run till its completion.
• The time complexity of algorithms is most commonly
expressed using the big O notation. It's an asymptotic
notation to represent the time complexity.
• Time Complexity is most commonly estimated by counting
the number of elementary steps performed by any
algorithm to finish execution.
• The algorithm's performance may vary with different types
of input data, hence for an algorithm we usually use the
worst-case Time complexity of an algorithm because that is
the maximum time taken for any input size.

Dec 4, 2019 Myanmar Institute of Information Technology 13


Types of Notations for Time
Complexity

• Big Oh denotes "fewer than or the same as"


<expression> iterations.
• Big Omega denotes "more than or the same as"
<expression> iterations.
• Big Theta denotes "the same as" <expression>
iterations.
• Little Oh denotes "fewer than" <expression>
iterations.
• Little Omega denotes "more than" <expression>
iterations.
Dec 4, 2019 Myanmar Institute of Information Technology 14
Types of Notations for Time
Complexity
• O(expression) is the set of functions that grow slower than
or at the same rate as expression. It indicates the maximum
required by an algorithm for all input values. It represents
the worst case of an algorithm's time complexity.
• Omega(expression) is the set of functions that grow faster
than or at the same rate as expression. It indicates the
minimum time required by an algorithm for all input values.
It represents the best case of an algorithm's time
complexity.
• Theta(expression) consist of all the functions that lie in
both O(expression) and Omega(expression). It indicates the
average bound of an algorithm. It represents the average
case of an algorithm's time complexity.

Dec 4, 2019 Myanmar Institute of Information Technology 15


Calculating Time Complexity
statement;
Time Complexity will be Constant.

for(i=0; i < N; i++)


{
statement;
}
Time Complexity will be Linear. The running time of the loop is directly proportional to N.
When N doubles, so does the running time.

for(i=0; i < N; i++)


{
for(j=0; j < N; j++)
{
statement;
}
}
Time Complexity will be Quadratic. The running time of the two loops is proportional to the
square of N. When N doubles, the running time increases by N * N.
Dec 4, 2019 Myanmar Institute of Information Technology 16
Calculating Time Complexity
while(low <= high)
{
mid = (low + high) / 2;
if (target < list[mid])
high = mid - 1;
else if (target > list[mid])
low = mid + 1;
else break;
}
Time Complexity will be Logarithmic. The running time of the algorithm is proportional to
the number of times N can be divided by 2(N is high-low here). This is because the algorithm
divides the working area in half with each iteration.

void quicksort(int list[], int left, int right)


{
int pivot = partition(list, left, right);
quicksort(list, left, pivot - 1);
quicksort(list, pivot + 1, right);
}
Time Complexity will be N*log( N ). The running time consists of N loops (iterative or
recursive) that are logarithmic, Myanmar
Dec 4, 2019
thus the algorithm is a combination of linear and
Institute of Information Technology 17
logarithmic.
Common Asymptotic Notations
constant − Ο(1)

logarithmic − Ο(log n)

linear − Ο(n)

Combination of Linear,
− Ο(n log n)
logarithmic

quadratic − Ο(n2)

cubic − Ο(n3)

exponential − O(2n)

Dec 4, 2019 Myanmar Institute of Information Technology 18


Analysis of Algorithms
• O(1): Time complexity of a function (or set of
statements) is considered as O(1) if it doesn’t contain
loop, recursion and call to any other non-constant time
function.
// set of non-recursive and non-loop statements
swap() function has O(1) time complexity.
A loop or recursion that runs a constant number of times
is also considered as O(1). For example the following
loop is O(1).
// Here c is a constant
for (int i = 1; i <= c; i++)
{
// some O(1) expressions
}
Dec 4, 2019 Myanmar Institute of Information Technology 19
Analysis of Algorithms
• O(n): Time Complexity of a loop is considered as O(n) if the
loop variables is incremented / decremented by a constant
amount. For example following functions have O(n) time
complexity.
// Here c is a positive integer constant
for (int i = 1; i <= n; i += c)
{
// some O(1) expressions
}
for (int i = n; i > 0; i -= c)
{
// some O(1) expressions
}
Dec 4, 2019 Myanmar Institute of Information Technology 20
Analysis of Algorithms
•O(n2): Time complexity of nested loops is equal to the
number of times the innermost statement is
executed. For example the following sample loops
have O(n2) time complexity.
for (int i = 1; i <=n; i += c)
{
for (int j = 1; j <=n; j += c)
{
// some O(1) expressions
}
}

for (int i = n; i > 0; i -= c)


{
for (int j = i+1; j <=n; j += c)
{
// some O(1) expressions
}
} Dec 4, 2019 Myanmar Institute of Information Technology 21
Analysis of Algorithms
• O(log n) Time Complexity of a loop is
considered as O(log n) if the loop variables
is divided / multiplied by a constant
amount.
for (int i = 1; i <=n; i *= c)
{
// some O(1) expressions
}
for (int i = n; i > 0; i /= c)
{
// some O(1) expressions
} Dec 4, 2019 Myanmar Institute of Information Technology 22
How to combine time complexities
of consecutive loops?
for (int i = 1; i <=m; i += c)
{
// some O(1) expressions
}
for (int i = 1; i <=n; i += c)
{
// some O(1) expressions
}
Time complexity of above code is O(m) + O(n) which is
O(m+n).
If m == n, the time complexity becomes O(2n) which is
O(n).
Dec 4, 2019 Myanmar Institute of Information Technology 23

S-ar putea să vă placă și