2. Data Structures
■ A data structure is a way to organize data to enable
efficient computation over that data
■ A data structure supports certain operations, each with a:
– Meaning: what does the operation do/return
– Performance: how efficient is the operation
■ Examples:
– List with operations insert and delete
– Stack with operations push and pop
3. Organizing Data
■ Any organization for a collection of records that can be
easily 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.
4. Efficiency
■ A solution is said to be efficient if it solves the problem
within its resource constraints.
– Time
– Space
■ The cost of a solution is the amount of resources that the
solution consumes
5. Need for Data Structure
■ Data Search − Consider an inventory of 1 million(106) items of
a store. If the application is to search an item, it has to search
an item in 1 million(106) items every time slowing down the
search. As data grows, search will become slower.
■ Processor Speed − Processor speed although being very high,
falls limited if the data grows to billion records.
■ Multiple Requests − As thousands of users can search data
simultaneously on a web server, even the fast server fails
while searching the data.
6. Data Structures
■ The domain of data structures studies how we can store
and access data.
■ A data structure can be:
– Static: the size of the data structure is fixed. Such data
structures are suitable if it is known that a fixed
number of elements need to be stored.
– Dynamic: the size of the data structure can grow or
shrink as needed by the number of elements.
7. Classification of Data
Structure
■ Data structure are normally divided into two broad
categories:
– Primitive Data Structure
– Non-Primitive Data Structure
9. Primitive Data Structure
■ Primitive data structures are the fundamental data types
which are supported by a programming language.
■ Some basic data types are integer, real, character, float.
■ The terms ‘data type’, ‘basic data type’, and ‘primitive data
type’ are often used interchangeably
11. Non-Primitive Data Structure
■ Non-primitive data structures are those data structures
which are created using primitive data structures.
■ Examples of such data structures include linked lists,
stacks, trees, and graphs.
■ Non-primitive data structures can further be classified
into two categories: linear and non-linear data structures.
12. Non-Primitive Data Structure
■ Linear: In Linear data structure, values are arrange in a
linear or sequential order.
– Array: Fixed-size
– Linked-list: Variable-size
– Stack: Add to top and remove from top
– Queue: Add to back and remove from front
– Priority queue: Add anywhere, remove the highest
priority
13. Non-Primitive Data Structure
■ Non-Linear: The data values in this structure are not
arranged in sequential order.
– Hash tables: Unordered lists which use a ‘hash
function’ to insert and search
– Tree: Data is organized in branches.
– Graph: A more general branching structure, with less
strict connection conditions than for a tree
14. Data Types
■ A data type is a set of values and a set of operations on
those values. For example:
■ int
– set of values are integer numbers from a given interval
– possible operations: add, subtract, multiply, etc.
■ boolean
– set of values: true, false
– possible operations: negation, and, or, xor, etc.
15. Data Types
■ String
– ”abc”, ”text”, etc.
– possible operations: get the length, get a character
from a position, get a substring, etc.
■ etc.
16. Abstract Data Types
■ An Abstract Data Type (ADT) is a data type having the
following two properties:
– the objects from the domain of the ADT are specified
independently of their representation
– the operations of the ADT are specified independently
of their implementation
17. Abstract Data Types - Domain
■ The domain of an ADT describes what elements belong to
this ADT.
■ If the domain is finite, we can simply enumerate them.
■ If the domain is not finite, we will use a rule that describes
the elements belonging to the ADT.
18. Abstract Data Types -
Interface
■ After specifying the domain of an ADT, we need to specify
its operations.
■ The set of all operations for an ADT is called its interface.
■ The interface of an ADT contains the signature of the
operations, together with their input data, results,
preconditions and post conditions (but no detail regarding
the implementation of the method).
■ When talking about ADT we focus on the WHAT (it is, it
does), not on the HOW.
19. ADT - Example
■ Consider the Date ADT (made of three numbers: year,
month and day)
– Elements belonging to this ADT are all the valid dates
(year is positive, maybe less than 3000, month is
between 1 and 12, day is between 1 and maximum
number of possible days for the month)
20. ADT - Example
■ One possible operation for the Data ADT could be:
difference(Date d1, Date d2)
– Descr: computed the difference in number of days
between two dates
– Pre: d1 and d2 have to be valid Dates, d1 ≤ d2
– Post: difference ← d, d is a natural number and
represents the number of days between d1 and d2.
– Throws: and exception if d1 is greater than d2
21. ADT - Example
■ This information specifies everything we need to use the
Date ADT, even if we know nothing about how it is
represented or how the operation difference is
implemented.
22. Why Abstract Data Types?
■ There are several different Abstract Data Types, so
choosing the most suitable one is an important step
during application design.
■ When choosing the suitable ADT we are not interested in
the implementation details of the ADT.
■ Most high-level programming languages usually provide
implementations for different Abstract Data Types.
– In order to be able to use the right ADT for a specific
problem, we need to know their domain and interface.
23. Why Abstract Data Types?
■ Why do we need to implement our own Abstract Data
Types if they are already implemented in most
programming languages?
– Implementing these ADT will help us understand
better how they work (we cannot use them, if we do
not know what they are doing)
24. Why Abstract Data Types?
– To learn to create, implement and use ADT for
situations when:
■ we work in a programming language where they are
not readily implemented.
■ we need an ADT which is not part of the standard ones,
but might be similar to them.
25. Advantages of working with
ADTs
■ Abstraction is defined as the separation between the
specification of an object (its domain and interface) and
its implementation.
■ Encapsulation - abstraction provides a promise that any
implementation of an ADT will belong to its domain and
will respect its interface. And this is all that is needed to
use an ADT.
26. Advantages of working with
ADTs
■ Localization of change - any code that uses an ADT is still
valid if the ADT changes (because no matter how it
changes, it still has to respect the domain and interface).
■ Flexibility - an ADT can be implemented in different ways,
but all these implementation have the same interface.
Switching from one implementation to another can be
done with minimal changes in the code.
27. Advantages of working with
ADTs
■ Consider again our example with the Date ADT:
– Assume you used variables of type Date and the
difference operation in your code.
– If the implementation of the Date changes, these
changes will not affect your code, because even if the
representation changes and even if the
implementation of the operations changes, they still
have to respect the interface (the signature and
behavior of the difference operation is still the same).
28. Advantages of working with
ADTs
– If a new implementation for the Date will appear and
you want to switch to that implementation, it can be
done with minimal modifications in your code
29. Algorithm
■ Algorithm is a step by step procedure, which defines a set
of instructions to be executed in certain order to get the
desired output.
■ Algorithms are generally created independent of
underlying languages
30. Algorithm
■ From data structure point of view, following are some
important categories of algorithms :
– Search − Algorithm to search an item in a data structure.
– Sort − Algorithm to sort items in certain order
– Insert − Algorithm to insert item in a data structure
– Update − Algorithm to update an existing item in a data
structure
– Delete − Algorithm to delete an existing item from a data
structure
31. Characteristics of an
Algorithm
■ Unambiguous − Algorithm should be clear and
unambiguous. Each of its steps or phases, and their
input/outputs should be clear and must lead to only one
meaning.
■ Input − An algorithm should have 0 or more well defined
inputs.
■ Output − An algorithm should have 1 or more well
defined outputs, and should match the desired output.
32. Characteristics of an
Algorithm
■ Finiteness − Algorithms must terminate after a finite
number of steps.
■ Feasibility − Should be feasible with the available
resources.
■ Independent − An algorithm should have step-by-step
directions which should be independent of any
programming code.
33. How to write an algorithm?
■ There are no well-defined standards for writing
algorithms. Rather, it is problem and resource dependent.
– Algorithms are never written to support a particular
programming code.
– As we know that all programming languages share
basic code constructs like loops (do, for, while), flow-
control (if-else), etc. These common constructs can be
used to write an algorithm.
34. Algorithm Complexity
■ Time and space used by the Algorithm determines the
efficiency of an algorithm.
■ Time Factor − The time is measured by counting the
number of key operations such as comparisons in sorting
algorithm
■ Space Factor − The space is measured by counting the
maximum memory space required by the algorithm.