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

Chapter One: Introduction To Data Structures & Algorithms

This document discusses data structures and algorithms. It outlines key concepts like data structures, algorithms, complexity analysis, and abstract data types. It also covers different types of data structures like linear and non-linear data structures.

Uploaded by

Tekle
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Chapter One: Introduction To Data Structures & Algorithms

This document discusses data structures and algorithms. It outlines key concepts like data structures, algorithms, complexity analysis, and abstract data types. It also covers different types of data structures like linear and non-linear data structures.

Uploaded by

Tekle
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

DSA

Chapter one

Introduction to Data Structures & Algorithms

1
outlines
 To understand the concepts of
– Data structures
– Types of Data Structures
– Algorithms
– Properties of an Algorithm
– Complexity analysis
– ADTs

2
Computer Program

 A computer program is a series of instructions to


carry out a particular task written in a language
that a computer can understand.
 The process of preparing and feeding the
instructions into the computer for execution is
referred as programming.

3
Good Computer Program
 Programs consists of two things: Algorithms and data
structures
 A Good Program is a combination of both algorithm and a
data structure
 An algorithm is a step by step recipe for solving an
instance of a problem
 A data structure A data structure is a way of organizing and
storing data in a computer so that it can be accessed and
manipulated efficiently.
 Data structures are fundamental to computer science and
are used in various applications to solve different types of
problems. They play a crucial role in software
development and are chosen based on the specific
requirements of a given task.
4
Algorithms
 An algorithm is a step by step recipe for solving an
instance of a problem.
 Every single procedure that a computer performs is an
algorithm.
 An algorithm is a precise procedure for solving a problem
in finite number of steps.
 An algorithm states the actions to be executed and the
order in which these actions are to be executed.
 An algorithm is a well ordered collection of clear and
simple instructions of definite and effectively computable
operations that when executed produces a result and stops
executing at some point in a finite amount of time rather
than just going on and on infinitely.

5
Algorithm Properties

An algorithm possesses the following properties:


– It must be correct.
– It must be composed of a series of concrete steps.
– There can be no ambiguity as to which step will
be performed next.
– It must be composed of a finite number of steps.
– It must terminate.
– It takes zero or more inputs
– It should be efficient and flexible
– It should use less memory space as much as
possible
– It results in one or more outputs

6
Complexity analysis
Why we should analyze algorithms?
 Predict the resources that the algorithm requires
 Computational time (CPU consumption)
 Memory space (RAM consumption)

 Communication bandwidth consumption

 The running time of an algorithm is:


 The total number of primitive operations executed
(machine independent steps)
 Also known as algorithm complexity
Time Complexity
Worst-case
An upper bound on the running time for any input
of given size
Average-case
 Assume all inputs of a given size are equally likely
 Best-case
 The lower bound on the running time
Time Complexity – Example

Sequential search in a list of size n


 Worst-case:
n comparisons
Best-case:
1 comparison
Average-case:
n/2 comparisons
Asymptotic notations
Algorithm complexity is rough estimation of the
number of steps performed by given
computation depending on the size of the input
data
 Measured through asymptotic notation
 O(g) where g is a function of the input data size

 Examples:
 Linear complexity O(n) – all elements are processed once (or
constant number of times)
 Quadratic complexity O( n2) – each of the
elementsis processed n times
O-notation
Asymptotic upper bound
f(n)=O(g(n)) iff there exist a positive constant c and
non-negative integer n0 such that
f(n)  cg(n) for all
nn0.
 g(n) is said to be an upper bound of f(n).
Example

The running time is O(n2) means there is a


function f(n) that is O(n2) such that for any
value of n, no matter what particular input
of size n is chosen, the running time of that
input is bounded from above by the value
f(n).
3 * n2 + n/2 + 12 ∈ O(n2)
 4*n*log 2 (3*n+1) + 2*n-1 ∈ O(n *
log n)
Ω notation
Asymptotic lower bound
Example

When we say that the running time (no modifier) of


an algorithm is Ω (g(n)).
 we mean that no matter what particular input of
size n is chosen for each value of n, the running
time on that input is at least a constant times g(n),
for sufficiently large n.
n3 + 20n ∈ Ω(n2 )
Θ notation

g(n) is an asymptotically tight bound of f(n)


Example
Iterative Factorial Example
fact ( long n)
{
for (i=1; i<=n; i++)
x=i*x;
return x;
}
 Space occupied is
 Data Space: i, n and x
 Environment Space: Almost nothing because the
function is called only once.
 The algorithm has a complexity of O(1) because it
does not depend on n. No matter how big the problem
becomes ,the space complexity remains the same since
the same variables are used , and the function is called
only once.

18
Why algorithm analysis?

As computers get faster and problem sizes get bigger,


analysis will become more important.

Why? The difference between good and bad algorithms


will get bigger.

19
How to Measure Algorithm Performance

 What metric should be used to judge algorithms?


– Length of the program (lines of code)
– Ease of programming (bugs, maintenance)
– Memory required
 Running time

 Running time is the dominant standard.


– Quantifiable and easy to compare
– Often the critical bottleneck

20
The Need for Data Structures
Data structures organize data
 more efficient programs.
More powerful computers  more complex applications.
More complex applications demand more calculations.
Complex computing tasks are unlike our everyday experience.
 More typically, a data structure is meant to be an organization for a
collection of data items.
 Any organization for a collection of records can be searched, processed
in any order, or modified.
 The choice of data structure and algorithm can make the difference
between a program running in a few seconds or many days. A data
structure requires a certain amount of:
 space for each data item it stores
 time to perform a single basic operation
 programming effort.

21
Selecting a Data Structure

Select a data structure as follows:


1. Analyze the problem to determine the resource constraints a
solution must meet.
2. Determine the basic operations that must be
supported. Quantify the resource constraints for each
operation.
3. Select the data structure that best meets these requirements.

22
Data Structures

DS includes
• Logical or mathematical description of the structure
and Implementation of the structure on a computer
• Quantitative analysis of the structure, which includes
determining the amount of memory needed to store
the structure and the time required to process the
structure.

23
Classification of Data Structures

“Data Structures ”deals with the study of how the data is


organized in the memory, how efficiently the data can be
retrieved and manipulated, and the possible ways in which
different data items are logically related.
Types:
Primitive Data Structure: Ex. int,float,char
Non-primitive Data Structures:
Ex.Arrays,Structures,stacks
Linear Data Structures: Ex.Stacks,queues,linked list
Non-Linear Data Structures: Ex.Trees,Graphs.

24
Classification of Data Structures

.
Classification of Data Structures
 Linear data structures have a linear relationship between
its adjacent elements. Linked lists are examples of linear
data structures.
 Non linear data structures don’t have a linear relationship
between its adjacent elements
 In a linear data structure , each node has a link which
points to another node, whereas in a non linear data
structure, each node may point to several other nodes

26
Data Structures
 Linear structures
– Array: Fixed-size
– Linked-list: Variable-size
– Stack: Add to top and remove from top
– Queue: Add to back and remove from front
 Non linear structure
 Tree: A branching structure with no loops
 Hash tables: Unordered lists which use a ‘hash
function’ to insert and search
 Graph: A more general branching structure, with less
stringent connection conditions than for a tree
Abstract data type (ADTs)
 A data type that is defined entirely by a set of operations is
referred to as Abstract data type or simply ADT
 Abstract data types are a way of separating the
specification and representation of data types
 An ADT is a black box, where users can only see the
syntax and semantics of its operations
 An ADT is a combination of interface and implementation
The interface defines the logical properties of the ADT and
especially the signatures of its operations
 The implementation defines the representation of data
structure and the algorithms that implement the operations
 An abstract data type encapsulates data and functions into
a named data type

28
Abstract data type (ADTs)
 It is similar to a structure in C, but can include functions in
it
 The basic difference between ADTs and primitive
data is that the latter allow us to look at
types
representation, whereas
the former hide the representation
from us
 An ADT consists of a collection of values and operations
with the values derive their meaning solely through the
operations that can be performed upon them
 Benefits of using ADTs:
Code is easier to understand
Implementations of ADTs can be changed without requiring changes
to the program that uses the ADTs

35
ADTs Collection
 ADT is a data structure and a set of
operations which can be performed on it.
– A class in object-oriented design is an
ADT

30

You might also like