Chapter One: Introduction To Data Structures & Algorithms
Chapter One: Introduction To Data Structures & Algorithms
Chapter one
1
outlines
To understand the concepts of
– Data structures
– Types of Data Structures
– Algorithms
– Properties of an Algorithm
– Complexity analysis
– ADTs
2
Computer Program
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
6
Complexity analysis
Why we should analyze algorithms?
Predict the resources that the algorithm requires
Computational time (CPU consumption)
Memory space (RAM consumption)
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
nn0.
g(n) is said to be an upper bound of f(n).
Example
18
Why algorithm analysis?
19
How to Measure Algorithm Performance
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
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
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