0% found this document useful (0 votes)
3 views32 pages

Lecture 2.2

The document discusses Data Structures and Abstract Data Types (ADTs), focusing on their definitions, operations, and the importance of abstraction in programming. It emphasizes the distinction between data types and data structures, illustrating concepts with examples such as seat reservations on an airplane. Additionally, it covers algorithm efficiency and methods for measuring it, highlighting the significance of time complexity in evaluating algorithms.

Uploaded by

noobscaferen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views32 pages

Lecture 2.2

The document discusses Data Structures and Abstract Data Types (ADTs), focusing on their definitions, operations, and the importance of abstraction in programming. It emphasizes the distinction between data types and data structures, illustrating concepts with examples such as seat reservations on an airplane. Additionally, it covers algorithm efficiency and methods for measuring it, highlighting the significance of time complexity in evaluating algorithms.

Uploaded by

noobscaferen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

University of Management &

Technology

Data Structures
Abstract Data Types

Mr. Shoaib Khan


[email protected]

Lecture No. 2.1


Some basic questions
• What is an integer?
– What are the operations that can be performed
on Integers?

• What is a character?
– What are the operations that can be performed
on characters?
Some more basic questions
• How an integer is represented in memory?
– How the integer based operations are
performed?

• How a rational number is represented?


– How different operations are performed?
Abstraction levels
• What are different kinds of programming
Languages?
– Low-level/Assembly/High-level languages

• Programming in a certain type of language


involves abstraction
– In order to simplify the representation
– In order to make it easier to program
Abstraction levels
• While programming in a high-level
language we are not that bothered about
low level representation
Abstract Data Types
• Abstract data-types allow us to take our
abstraction level, one-step further

• Instead of focusing on how a particular


data-structure can be implemented, focus
is on what should be chosen to solve the
problem
Abstract Data Types
Abstract Data Type (ADT): a definition for a data type
solely in terms of a set of values and a set of operations
on that data type.

Each ADT operation is defined by its inputs and outputs.


ENCAPSULATION: Hide Implementation details

7
Abstract Data Types
Def. a collection of related data
items
together with
an associated
e.g. whole setand
numbers (integers) ofarithmetic
operationsoperators for
addition, subtraction, multiplication and division.

e.g. Flight reservation


Basic operations: find empty seat, reserve a seat,
cancel a seat assignment

Why "abstract?"
Data, operations, and relations are studied independent of
implementation.

What not how is the focus.

8
Abstract Data Types
Def.Consists of
storage structures (data structures)
to store the data items
and
algorithms for the basic operations.

The storage structures/data structures used in


implementations are provided in a language (primitive
or built-in) or are built from the language constructs
(user-defined).

In either case, successful software design uses data


abstraction:
Separating the definition of a data type from its
9
implementation.
Data Structure
• A data structure is the physical implementation of an ADT.
– Each operation associated with the ADT is implemented by one or
more subroutines in the implementation.

• Data structure usually refers to an organization of data in


main memory.

• File structure is an organization for data on peripheral


storage, such as a disk drive.

10
Data Type

ADT:
Data Items:
Type
Logical Form
Operations

Data Structure: Data Items:


Storage Space Physical Form
Subroutines

11
ADT example
Data Structures, Abstract Data Types,
and Implementations

• Consider example of an airplane flight with


10 seats to be assigned
• Tasks
– List available seats
– Reserve a seat
• How to store, access data?
Data Structures, Abstract Data Types,
and Implementations

• Consider example of an airplane flight with


10 seats to be assigned
• Tasks
– List available seats
– Reserve a seat
• How to store, access data?
– 10 individual variables
Use 10 individual variables
• Algorithm to List available • Algorithm to Reserve a
seats seat

1. If seat1 == ‘ ’: 1. Set DONE to false


2. If seat1 ==‘ ’:
display 1 print “do you want seat #1??”
2. If seat2 == ‘ ’: Get answer
display 2 if answer==‘Y’:
. set seat1 to ‘X’
set Done to True
. 3. If seat2 ==‘ ’:
. print “do you want seat #2??”
10. If seat10 == ‘ ’: Get answer
if answer==‘Y’:
display 10 set seat2 to ‘X’
set Done to True
.
.
.
Data Structures, Abstract Data Types,
and Implementations

• Consider example of an airplane flight with


10 seats to be assigned
• Tasks
– List available seats
– Reserve a seat
• How to store, access data?
– 10 individual variables
– An array of variables
Use Array
• Algorithm to List available seats
For number ranging from 0 to max_seats-1, do:
If seat[number] == ‘ ’:
Display number

• Algorithm to Reserve a seat


Reading number of seat to be reserved
If seat[number] is equal to ‘ ’:
set seat[number] to ‘X’
Else
Display a message that the seat having this
number is occupied
• This simple example does illustrate the concept
of an Abstract Data Type

• ADT consists of
The collection of data items
Basic operation that must be performed on
them

• In the example, a collection of data is a list of


seats
• The basic operations are (1) Scan the list to
determine which seats are occupied (2) change
seat’s status
ADT’s for Primitive Data Types

19
Boolean data
Data values: {false, true}

Operations: and &&


or ||
not !
&& 0 1 | | 0 1
0 0 0 x !x
0 0 1
1 0 1
0 1
1 1 1 1 0
Character Data
Store numeric codes (ASCII, EBCDIC, Unicode)
1 byte for ASCII and EBCDIC,
2 bytes for Unicode
ASCII/EBCDIC

Unicode

Basic operation: comparison to determine if Equal, Less than ,Greater


than, etc. use their numeric codes (i.e. use ordinal value)
Integer Data

Non-negative (unsigned) integer:


Store its base-two representation in a fixed number w
of bits
(e.g., w = 16 or w = 32)

00000000010110002
88 =

Signed integer:
Store in a fixed number w of bits using either of complement
representations:
Operations: arithmetic operators for addition,
subtraction, multiplication and division

22
Algorithm Review
• An algorithm is a definite procedure for solving a
problem in finite number of steps

• Algorithm is a well defined computational


procedure that takes some value (s) as input,
and produces some value (s) as output.

• Algorithm is finite number of computational


statements that transform input into the output
• Algorithm
– Finite sequence of instructions.
– Each instruction having a clear meaning.
– Each instruction requiring finite amount of
effort.
– Each instruction requiring finite time to
complete.
Good Algorithms?
• Run in less time
• Consume less memory

But computational resources (time


complexity) is usually more important
Measuring Efficiency
• The efficiency of an algorithm is a measure of the
amount of resources consumed in solving a
problem of size n.
• The resource we are most interested in is time
• We can use the same techniques to analyze the
consumption of other resources, such as memory
space.

• How to measure the efficiency of the algorithm?


Ways of measuring efficiency:
• Run the program and see how long it takes
• Run the program and see how much memory it uses

•Lots of variables to control:


• What is the input data?
• What is the hardware platform?
• What is the programming language/compiler?

Just because one program is faster than another


right now, means it will always be faster?

27
Want to achieve platform-independence

• Use an abstract machine that uses steps of time


and units of memory, instead of seconds or bytes

•each elementary operation takes 1 step

•each elementary instance occupies 1 unit of memory

11
Running Time of an Algorithm
• Running time is measured in terms of number of
steps/primitive operations performed

• Generally time grows with size of input, so running time of


an algorithm is usually measured as function of input size.

• Independent from machine, OS


Simple Example (1)
// Input: int A[N], array of N integers
// Output: Sum of all numbers in array A

int Sum(int A[], int N)


{
int s=0;
for (int i=0; i< N; i++)
s = s + A[i];
return s;
}
How should we analyse this?
Simple Example (2)
// Input: int A[N], array of N integers
// Output: Sum of all numbers in array A

int Sum(int A[], int N){


int s=0; 1
for (int i=0; i< N; i++)
2 3 4
s = s + A[i];
5 6 7 1,2,8: Once
return s; 3,4,5,6,7: Once per each iteration
}
8 of for loop, N iteration
Total: 5N + 3
The complexity function of the
algorithm is : f(N) = 5N +3
References
• Example taken from lecture slides by Dr.
Bernard Chen Ph.D. at University of Central
Arkansas

You might also like