Data Structures and Algorithms Lecture 3 Instructor: Quratulain Date: 8 th September, 2009.
-
Upload
josephine-griffith -
Category
Documents
-
view
216 -
download
2
Transcript of Data Structures and Algorithms Lecture 3 Instructor: Quratulain Date: 8 th September, 2009.
Data Structures and Algorithms
Lecture 3
Instructor: Quratulain
Date: 8th September, 2009
CSE 246 Data Structures and Algorithms Fall2009 Quratulain
2
IntroductionAlgorithms
◦Outline the computational procedure, step by step instructions. Include search, delete, sort etc.
Program◦Implementation of algorithm in some
programming languageData Structures
◦Organization of data need to solve the problem
3
Data StructuresThere are a number of facets to good
programs: they must run correctly run efficiently be easy to read and understand be easy to debug and be easy to modify.
This course will focus on solving problems efficiently: you will be introduced to a number of fundamental data structures and algorithms for manipulating them.
CSE 246 Data Structures and Algorithms Fall2009 Quratulain
CSE 246 Data Structures and Algorithms Fall2009 Quratulain
4
Information and Meaning
5
Abstract Data TypeA data type whose properties (domain
and operations) are specified independently of any particular implementation.
ADT is a mathematical model which gives the a set of utilities available to the user but never states the details of its implementation.
Focusing on what it does and ignoring how it does its job
In OO-Programming a Class is an ADTCSE 246 Data Structures and Algorithms Fall2009 Quratulain
6
A Real Life Example: why data structures?
Electronic Phone Book
Contains different DATA:
- names
- phone number
- addresses
Need to perform certain OPERATIONS:
- add
- delete
- look for a phone number
- look for an address
How to organize the data so to optimize the efficiency of the operations.
ADT is Phone BookCSE 246 Data Structures and Algorithms Fall2009 Quratulain
7
ADT
CSE 246 Data Structures and Algorithms Fall2009 Quratulain
•When we use abstract datatypes, our programs divide into two pieces:
•The Application: The part that uses the abstract datatype.
•The Implementation: The part that implements the abstract datatype.
8
Abstract Data Type A type is a collection of values. For example, the
boolean type consists of two values, true and false; the byte type consists of all integers between -127 and +128.
A data type is a type + a set of operations on data of the type. For example, integers of type byte plus {+, -, *, /} comprise the data type byte.
An integer array is a data type which has assign and search operations associated with it.
An abstract data type is a data type solely defined in terms of a type and a set of operations on that type. Each operation is defined in terms of its input and output without specifying how the data type is implemented.
A stack is an example of an ADT, defined in terms of push and pop operations.
A data structure is an implementation of an ADT . The Stack ADT, for example, an be implemented by means of an array.
CSE 246 Data Structures and Algorithms Fall2009 Quratulain
9
Levels of abstraction in data specification
Any data can be defined in two formats: logical and physical. An ADT defines
data in a logical format -- as a conceptual model, which is completely
independent of its implementation. Consider, for example, a matrix of integers.
At a logical level, we are not interested in how this matrix is implemented. The
only items of interest are what type of data this matrix consists of, and what
operations will be performed on this data.
At a physical level, the associated data structure representing the Matrix ADT
can be implemented with a different degree of abstraction by means of:
◦ a high level language, in which case the Matrix ADT can be defined as an array of integers.
◦ in assembly language (or byte code) it is represented as a sequence of words in computer memory.
◦ in machine language it is represented as a sequence of electronic bits.
CSE 246 Data Structures and Algorithms Fall2009 Quratulain
10
The Matrix example (cont.)The ADT Matrix, in more formal terms, can de defined as a
collection of data
items of type MatrixData arranged as a rectangular grid, and associated with
the following set of operations:
◦ Retrieve (M, row#, col#)◦ Assign (M, row#, col#, value)
To implement the ADT matrix, we can use a two-dimensional array. For example:
int[ ][ ] M = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}}
The retrieve and assign operations can be implemented as follows:
item = M[1][2]
M[2][3] = item
CSE 246 Data Structures and Algorithms Fall2009 Quratulain
11
Representation of Multi-dimensional arrays in computer memory
Computer memory is a linear sequence of memory cells. There are two ways
to translate a two-dimensional array into a linear sequence:
1. Row by row (this is called the row-major form). To implement assign and retrieve operations, we must be able to find a particular entry. This can be done by means of the following formula (called a mapping function):
(# of columns * (i - 1) + j),
where i is the specified row#, and j is the specified col#.
2. Column by column (this is called the column-major form) in which case the mapping function is
(# of rows * (j - 1) + i).CSE 246 Data Structures and Algorithms Fall2009 Quratulain
12
Row Major Ordering
CSE 246 Data Structures and Algorithms Fall2009 Quratulain
Another view
For a three-dimensional array, the formula to compute the offset into memory is the following: Address = Base + ((depthindex*col_size+colindex) * row_size + rowindex) * Element_Size
For a two-dimension row major ordered array declared in Pascal as "A:array [0..3,0..3] of integer" is Element_Address = Base_Address + (colindex * row_size + rowindex) * Element_Size
For a four dimensional array, the formula for computing the address of an array element is Address = Base + (((LeftIndex * depth_size + depthindex)*col_size+colindex) * row_size + rowindex) * Element_Size
13
Column Major Ordering
CSE 246 Data Structures and Algorithms Fall2009 Quratulain
For a two-dimension column major array: Element_Address = Base_Address + (rowindex * col_size + colindex) * Element_Size
For a three-dimension column major array: Address = Base + ((rowindex*col_size+colindex) * depth_size + depthindex) * Element_Size
For a four-dimension column major array: Address = Base + (((rowindex * col_size + colindex)*depth_size + depthindex) * Left_size + Leftindex) * Element_Size