Вы находитесь на странице: 1из 18

Chapter 1

Introduction
Brooks/Cole,
2001

Pseudocode
Part English, part structured code
Used to describe an algorithm
In this text, algorithms consist of:

Header (purpose, conditions, and return)


Statement numbers
Meaningful variables
Statement constructs (sequence, selection, and
loop)
Brooks/Cole,
2001

Data Structure
Combination of either a data type or another
data structure, into a set with defined
relationships
Examples:
Array
Homogeneous sequence of data elements
Position association among elements

Record
Heterogeneous combination of data
No association among elements
Brooks/Cole,
2001

Abstraction
We know what a data type can do
Details of how this is done, is hidden
Hiding details of how operations are
performed, allows a programmer to focus on
what needs to be done

Brooks/Cole,
2001

Figure 1-1

Brooks/Cole,
2001

Abstract Data Type Model


User is not able to access the data structure
and operational functions inside the model
All functions within the model are within
scope of each other
Users access the ADT through the external
interface (operation name and parameters)

Brooks/Cole,
2001

Figure 1-2

Brooks/Cole,
2001

ADT Data Structure


Hides implementation from users
Data about the structure is stored inside the
ADT
Implemented using a C++ class
ADT Class Templates are used to provide
flexibility in handling different data types

Brooks/Cole,
2001

Figure 1-3

Brooks/Cole,
2001

Algorithm Efficiency
Efficiency is a function of the number of elements to
be processed (n):
f(n) = efficiency
Standard measures:

Linear
Logarithmic
Linear log
Quadratic
Polynomial
Exponential
Factorial

f(n) = n
f(n) = log2n
f(n) = nlog2n
f(n) = n2
f(n) = nk (k is largest term exponent)
f(n) = cn (c is a constant)
f(n) = n!
Brooks/Cole,
2001

Figure 1-4

Brooks/Cole,
2001

Matrix Addition Example


Add corresponding elements (first element of
first matrix, to first element of second matrix)
Put result of the addition into their
corresponding locations in a third matrix.

Brooks/Cole,
2001

Figure 1-5

Brooks/Cole,
2001

Algorithm 1-3
algorithm addMatrix (val matrix1 <matrix>, val matrix2 <matrix>, val size
<integer>, ref matrix3 <matrix>)
1.
r=0
2.
loop (r < size)
1. c = 0
2. loop (c < size)
1. matrix3[r, c] = matrix1[r, c] + matrix2[r, c]
2. c = c + 1
3. end loop
4. r = r + 1
3.
end loop
4.
return
end addMatrix
Brooks/Cole,
2001

Matrix Multiplication Example


Multiply each element in a row of the first matrix, by its
corresponding element in a column of the second matrix
Value of the matrix is the sum of the products:
matrix3 [r,c] =
matrix1[r, 0] x matrix2[0, c]
+ matrix1[r, 1] x matrix2[1, c]
+ matrix1[r, 2] x matrix2[2, c]

+ matrix1[r, s-1] x matrix2[s-1, c]


where s = size of matrix
Brooks/Cole,
2001

Figure 1-6

Brooks/Cole,
2001

Algorithm 1-4
algorithm multiplyMatrix(val matrix1 <matrix>, val matrix2 <matrix>, val size <integer>, ref
matrix3 <matrix>)
1.
r=0
2.
loop (r < size)
1.
c=0
2.
loop (c < size)
1.
matrix3[r, c] = 0
2.
m=0
3.
loop (m < size)
1. matrix3[r, c] = matrix3[r,c] + matrix1[r,m] x matrix2[m,c]
2. m = m + 1
4.
end loop
5.
c=c+1
3.
end loop
4.
r=r+1
3.
end loop
4.
return
end multiplyMatrix
Brooks/Cole,
2001

Algorithm Analysis
Algorithm 1-4 is composed of three nested
loops
Big-O efficiency of O(size3), or O(n3)

Brooks/Cole,
2001

Вам также может понравиться