0% found this document useful (0 votes)
19 views

Data Structure - Algorithms - 01

The document discusses key concepts in data structures and algorithms including data types, data structures, time complexity analysis, and asymptotic notation. It defines data as a set of values, entities as objects with attributes and values, and information as processed data. Common data types and structures like arrays, stacks, queues, trees and graphs are introduced. The importance of algorithms and their properties for problem solving is also covered.

Uploaded by

Oishee Ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Data Structure - Algorithms - 01

The document discusses key concepts in data structures and algorithms including data types, data structures, time complexity analysis, and asymptotic notation. It defines data as a set of values, entities as objects with attributes and values, and information as processed data. Common data types and structures like arrays, stacks, queues, trees and graphs are introduced. The importance of algorithms and their properties for problem solving is also covered.

Uploaded by

Oishee Ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 33

UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

Course Name : Data Structure & Algorithms

Click to edit Master subtitle style


UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

• Data: Means set of values.


Ex: 4,AB,09/01/17
• Entity: An entity is one that has certain attributes
and which may be assigned values.
Ex: All the students constitute an entity set. Their
Name, Age,DOB etc are the attributes. Assigning
name,age and all other values to the attributes
gives complete information about a particuler
student
• Information: Can be defined as processed data.
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

• Data type: Refers to the kind of data that may appear in a


computation.
• Kind of data: Numeric, Alphanumeric etc.
Two types:
Built-in: Primitive data types.
User defined: Programmer’s own kind of data
Another name is Abstract Data Type.(ADT)
Programmer has to specify how to store the data and what
are the operations that can manipulate the data.
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

Data Structure Definition


UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

Manipulation of real-life data requires….


• Storage representation of user data
• Retrieval of stored data
Data Structure is the domain where we can
get the idea how data can be stored in
memory in an organised way so that data
can be searched , retrieved, modified in an
efficient manner.
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

Introduction to Data Structure


Data
Structure

Non-
Linear
Linear

Linked
Array Stack Queue Tree Graph
lists
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

Data Structures: A systematic way of organizing and accessing data.


-- No single data structure works well for ALL purposes.

Input Algorithm Output

An algorithm is a step-by-step procedure for solving a


problem in a finite amount of time.
Program = algorithms + data structures
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

Properties of a good Algorithm


UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

Properties of a good Algorithm


For each problem or class of problems, there may be many different algorithms .

For each algorithm, there may be many different implementations (programs).


UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

Analysis involves –
Tracing the algorithm for logical correctness, implementing
the algorithm and testing it on some data.
Checking the algorithm for simplicity

However, the simplest way of solving a problem is


sometimes not the best one, specially when, the simplest
approach involves the use of too much computer time or
space!!
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

• Estimate the running time – Time Complexity T(n)

• Estimate the memory space required – Space Complexity S(n)

Both depends on the input size n


UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

Definition of Time
• # of seconds (machine, implementation dependent).

• # lines of code executed.

• # of times a specific operation is performed (e.g.,


addition).
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

Theoretical analysis of time efficiency


 Time efficiency is analyzed by determining the number of repetitions of the basic operation as a
function of input size

• Basic operation: the operation that contributes most towards the running time of the algorithm.

Theoretical analysis of time efficiency


Input size

T(n) ≈ copC(n)
Number of times
Running time the basic operation
Execution time is executed
for one basic operation
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

Best-case, Average-case, Worst-case

• Worst case: W(n) – “maximum” over inputs of size n


– Provides an upper bound on running time
– An absolute guarantee that the algorithm would not run longer, no
matter what the inputs are

• Best case: B(n) – “minimum” over inputs of size n


– Provides a lower bound on running time
– Input is the one for which the algorithm runs the fastest
Lower bound ≤ Running time ≤ Upper bound
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

Best-case, Average-case, Worst-case (Contd..)


• Average case: A(n) – “average” over inputs of size n
– Provides a prediction about the running time
– Assumes that the input is random
– Under some assumption about the probability distribution of all
possible inputs of size n, calculates the weighted sum of expected C(n)
(numbers of basic operation repetitions) over all possible inputs of
size n.
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

Asymptotic Complexity
• Running time of an algorithm as a function of
input size n for large n.
• Expressed using only the highest-order term
in the expression for the exact running time.
– Instead of exact running time, say O(n2).
• Describes behavior of function in the limit.
• Written using Asymptotic Notation.
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

• The rate of change of execution time T(n) with the change in the
number of inputs is called growth of function.

• T(n) can be approximately equal to:


 nn
 n! Exponential functions
 2n
 n3
 n2
 nlog2 n Linear/polynomial functions
 n
 log2 n
 1 Logarithmic function
Constant
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

Asymptotic Notation
• ɵ, O, W, o, w
• Defined for functions over the natural numbers.
– Ex: f(n) = ɵ(n2).
– Describes how f(n) grows in comparison to n2.
• Define a set of functions; in practice used to
compare two function sizes.
• The notations describe different rate-of-growth
relations between the defining function and the
defined set of functions.
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

For function g(n), we define O(g(n)),


big-O of n, as the set:
O(g(n)) = {f(n) :
 positive constants c and n0, such
that n  n0,we have 0  f(n)  cg(n) }
Intuitively: Set of all functions whose
rate of growth is the same as or lower
than that of g(n).

g(n) is an asymptotic upper bound for f(n).


f(n) = (g(n))  f(n) = O(g(n)).
(g(n))  O(g(n)).
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

• 100n + 5 is O(n^2)
• 100n + 5
• ≤ 100n + n, for n ≥ 5
• = 101n ≤ 101n^2, so n0 = 5, c = 101
• Alternatively
• 100n + 5
• ≤ 100n + 5n, for n ≥1
• = 105n ≤ 105n^2, so n0 = 1, c = 105
• n0 and c are not unique!
• Of course, by the same argument, 100n+5 is also O(n)
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

• 100n^2 + 20n + 5 is O(n^2)


• 100n^2 + 20n + 5
• ≤ 100n^2 + 20n^2 + 5n^2, for n ≥ 1
• ≤ 125n^2
• n0 = 1, c = 125
• What matters is the highest term
• 20n + 5 dominated by 100n^2
• Is n^3 O(n^2)?
• No matter what c we choose, cn^2 will be
dominated by n3 for n ≥ c
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

For function g(n), we define


(g(n)), big-Omega of n, as the set:

(g(n)) = {f(n) :
 positive constants c and n0, such
that n  n0,
we have 0  cg(n)  f(n)}
Intuitively: Set of all functions whose rate
of growth is the same as or higher than that
of g(n).
g(n) is an asymptotic lower bound for f(n).
f(n) = (g(n))  f(n) = (g(n)).
(g(n))  (g(n)).
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

For function g(n), we define


(g(n)), big-Theta of n, as the set:
(g(n)) = {f(n) :
 positive constants c1, c2, and n0,
such that n  n0,
we have 0  c1g(n)  f(n)  c2g(n)
}
Intuitively: Set of all functions that
have the same rate of growth as g(n).

g(n) is an asymptotically tight bound for f(n).


UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

• n(n-1)/2 is Θ(n^2) // Tight bounds


Upper bound
• n(n-1)/2 = n^2/2 - n/2 ≤ n^2/2, for n ≥ 0
Lower bound
• n(n-1)/2 = n^2/2 - n/2 ≥ n^2/2 - (n/2 x n/2) ≥
n^2/4,
for n ≥ 2
• Choose n0 = 2, c1 = 1/2 and c2 = 1/4
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

EXAMPLES:
• int sum(a,n)
• { int s=0;
• for(i=0;i<n;i++)
• s=s+a[i];
• return s;
• }
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
Continued..
• void Add(A,B,n)
• { for(i=0;i<n;i++)
• { for(j=0;j<n;j++)
• Sum[i,j]=A[i][j]+B[i][j];
• }
• }
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

• void multiply(a,b,n)
• { for(i=0;i<n;i++)
• {for(j=0;j<n;j++)
• { c[i][j]=0;
• for(k=0;k<n;k++)
• { c[i][j]=c[i][j]+a[i][k]*b[k][j];}
• }}}
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
Continued
QUESTIONS
1. for(i=0;i<n;i=i+2)
{ stmt;}
2. for(i=0;i<n;i++)
{ for(j=0;j<i;j++)
{ stmt;}
}
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
Continued…
3. int p=0;
for(i=1; p<=n; i++)
{ p=p+i;}
4. for(i=1;i<n; i=i*2)
{ stmt;}
5.for(i=n;i>=1;i=i/2)
{ stmt;}
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
Continued…
6. for(i=1; i*i< n; i++)
{ stmt;}
7. for(i=0;i<n;i++)
{stmt;}
for(j=0;j<n;j++)
{ stmt;}
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
Continued..
8. p=0;
for(i=1; i<n; i=i*2)
{ p++;}
for(j=1;j< p; j=j*2)
{ stmt;}
9. for(i=0;i<n;i++)
{ for (j=1;j<n;j=j*2)
{ stmt;}
}
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
Continued..
Solutions:
1. O(n)
2. O(n^2)
3. O(sqrt(n))
4. O(log n)
5. O(log n)
6. O(sqrt(n))
7. O(n)
8. O(log (log n))
9. O(n log n)
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA

Thank You

33

You might also like