Você está na página 1de 44

CS13 - Data Structures

Summer 2010-2011

Data Structures
Data Type refers to the kind of data that variables may hold or take on in a programming language and for which operations are automatically provided Example: PASCAL: Integer, real, boolean and character C: character, integer, floating point, double floating point FORTRAN: integer, real, complex, logical and character LISP: list or S-expression
CS13 - Data Structures Summer 2010-2011 2

Data Structures (cont)


Abstract Data Type (ADT) a set of data elements with a set of operations defined on the data elements Data Structure the implementation, using specific programming language, of an ADT in terms of language data types or other data structures such that operations on the data structure are expressible in terms of directly executable procedures
CS13 - Data Structures Summer 2010-2011 3

Algorithms
- A finite set of instructions which, if followed will accomplish a particular task 5 Important Properties: 1. Finiteness 2. Definiteness 3. Input 4. Output 5. Effectiveness
CS13 - Data Structures Summer 2010-2011 4

Finiteness: An algorithm must terminate after a finite number of steps Definiteness: Each step of an algorithm must be precisely defined Input: An algorithm has 0 or more input quantities from a set called domain of the algorithm

CS13 - Data Structures

Summer 2010-2011

Output: An algorithm has 1 or more output quantities which generate a set called range of the algorithm Effectiveness: All of the operations to be performed are sufficiently basic that they can, in principle be done exactly and in finite time by a person using only pencil and paper.

CS13 - Data Structures

Summer 2010-2011

Qualities of a good algorithm


simple but powerful and general solutions can be easily understood by others can be easily modified if necessary correct for clearly defined situation understood on a number of levels

CS13 - Data Structures

Summer 2010-2011

Sample Algorithm - How to change your motor oil (Plain English)


First, place the oil pan underneath the oil plug of your car. Next, unscrew the oil plug and drain the oil. Now, replace the oil plug. Once the old oil is drained, remove the oil cap from the engine and pour in 4 quarts of oil. Finally, replace the oil cap on the engine.

CS13 - Data Structures

Summer 2010-2011

Sample Algorithm - How to change your motor oil (Structured English)


1. Place the oil pan underneath the oil plug of your car. 2. Unscrew the oil plug. 3. Drain oil. 4. Replace the oil plug. 5. Remove the oil cap from the engine. 6. Pour in 4 quarts of oil. 7. Replace the oil cap.
CS13 - Data Structures Summer 2010-2011 9

Methods of Accessing Elements in Data Structure


1. The computed address method - used to access the elements of a structure that is pre-allocated and essentially static.
2. The link-addressing method - a mechanism for manipulating dynamic structures which change in shape and size at run time
CS13 - Data Structures Summer 2010-2011 10

Node
- A sequence of one or more contiguous addressable units - The address of a node is the address of its leftmost addressable unit - Usually divided into named segments called fields: info and link Node P
INFO(P) = CS LINK(P) =
CS13 - Data Structures

Info Link CS
1000

1000
Summer 2010-2011 11

Info Link 3000 1000 CS


1000

IT
BS

4000

4000

^
BS

Equivalent linked list:


P CS IT

INFO (P) = CS INFO(LINK(P)) = IT INFO(LINK(LINK(P))) = BS


CS13 - Data Structures Summer 2010-2011 12

What is the result of the following?: Q LINK(P) LINK(P) LINK(Q) LINK(Q)


CS P Q

^
IT

BS

CS13 - Data Structures

Summer 2010-2011

13

EASY Notation
Features: 1. assignment statement 2. unconditional statement 3. conditional statement 4. iteration statement 5. input/output statement 6. Program structure

CS13 - Data Structures

Summer 2010-2011

14

EASY assignment statement


General form:

variable expression
where expression may be of type arithmetic, boolean or character to construct this expression, the ff. type of operators are provided:

a) arithmetic operators +, - *, / ^ b) logical oeprators AND, OR, NOT c) relational operators <. <. =. <>, > >
CS13 - Data Structures Summer 2010-2011 15

EASY assignment statement (cont)


other mathematical notations , mod, log2, , , etc.. Examples: |(lower+upper)/2|, n<m AND i<>j, ( Additionally, expression may take the form fieldname(pointer variable) like LINK(p) or INFO(LINK(p)

CS13 - Data Structures

Summer 2010-2011

16

EASY unconditional transfer statements


a. the go to statement - causes a transfer to the statement (not recommended) b. the exit statement - causes transfer to the statement immediately following the loop which contains the exit statement

CS13 - Data Structures

Summer 2010-2011

17

EASY conditional transfer statements


a. the if statement if condition then s1 else s2 s1 and s2 consists of one or more EASY statements the group of statements is enclosed in square brackets the else clause is optional

CS13 - Data Structures

Summer 2010-2011

18

EASY conditional transfer statements


b. the case statement
case : cond1: S1 : cond2: S2 : condn: Sn else Sn+1 end case

alternative to sequence of if statements


CS13 - Data Structures Summer 2010-2011 19

EASY iteration statements


a. the while statement
while cond do S end while
the statement or group of statements S is repeatedly executed for as long as cond is true some statement in S must be either change cond to false or cause transfer to a statement outside the loop if cond is initially false, S is not executed at all
CS13 - Data Structures Summer 2010-2011 20

EASY iteration statements


b. the repeat-until statement
repeat S until cond
the statement of group of statements S is repeatedly executed for as long as cond is false Some statement in S must either change cond to true or cause transfer to a statement outside the loop S is executed at least once regardless of the initial value of cond
CS13 - Data Structures Summer 2010-2011 21

EASY iteration statements


c. the loop forever statement
loop s forever some statements in S must cause an exit from this, otherwise infinite loop

d. the for statement


for var start to finish by incr do S end for
CS13 - Data Structures Summer 2010-2011 22

EASY iteration statements

var is a variable while start, finish and incr are arithmetic expressions var is initially set to the value of incr after every execution of S looping continues until var exceeds the value of finish or some statement in S causes transfer to a statement outside the for-loop

EASY input/output statements


input list output list list is a sequence of one or more variables of quoted strings (for output)
CS13 - Data Structures Summer 2010-2011 23

EASY declaration statements


a. The array element
array name (I1:U1, I2:U2, Ii:Ui, In:Un), name is the name given to the array: Ii and

Ui are the lower and upper bounds for the ith dimension b. The node statement
node (f1, f2, fi,fn)

fi is a field name. The node statement is used

to specify the node structure of the nodes used in a procedure


CS13 - Data Structures Summer 2010-2011 24

EASY control statements


a. The call statement
call name (ap1, ap2,..,api,apn)

The call statement transfers control to the procedure named name b. The return statement
return or return (expression)

- transfers control back to the procedure containing the statement


CS13 - Data Structures Summer 2010-2011 25

EASY control statements


c. The stop statement stop terminates execution of the procedure and returns control to the operating system d. The end statement end marks the physical end of a procedure. In the absence of a return statement, reaching the end statement implies a return
CS13 - Data Structures Summer 2010-2011 26

EASY program structure


- a collection of one or more EASY procedures - form: procedure name(fp1, fp2, .., fpi, ..., fpn) S end name where name is the name given to the procedure and fp1, fp2,... are formal parameters
CS13 - Data Structures Summer 2010-2011 27

EASY program structure


- A procedure is invoked using the call statement
call name(ap1, ap2, .., api, ..., apn)

where name is the name of the procedure and ap1, ap2,... are the actual parameters, which correspond on a one-to-one basis by type and size, with the formal parameters fp1, fp2, ... in the called procedure.
CS13 - Data Structures Summer 2010-2011 28

EASY program structure


- A procedure can be used as a function by using the statement
return (expression)
where the value of expression is delivered as the value of the procedure.

- A function-procedure is called implicitly, for instance, by using an assignment statement, var name(ap1, ap2, .., api, ..., apn) - A procedure may call itself (recursion). - Finally, EASY comments are enclosed in double slashes:
// Calculate allocation factors a and B.//
CS13 - Data Structures Summer 2010-2011 29

Sample EASY procedures


1. Factorial function: Find n! given n> 0.
procedure FACTORIAL(n) if n < 1 then value 1 else value n*FACTORIAL(n-1) return(value) end FACTORIAL

2. List inversion: Given a singly-linked linear list, invert the list in-place.
CS13 - Data Structures Summer 2010-2011 30

Mathematical Functions Frequently Used in Describing Algorithms


1. Floor of x - |_ x _| The greatest integer less than or equal to x, where x is any real number.
Example: |_ 6.15 _| = 6, |_ 1/2 _| = 0, |_ -1/2 _| = -1

2. Ceiling of x - | x | The smallest integer greater than or equal to x, where x is any real number.
Example: | 6.15 | = 7, | 1/2| = 1, | -1/2 | = 0
CS13 - Data Structures Summer 2010-2011 31

Example: X Floor Ceiling

-1.1 0 1.01 2.9 3

CS13 - Data Structures

Summer 2010-2011

32

Mathematical Functions Frequently Used in Describing Algorithms


3. Given any two real numbers x and y, x mod y is defined as: X mod y = x if y = 0 = x y . |_ x/y _| if y <> 0 For example : 10 mod 3 = 24 mod 8 = -5 mod 7 =
CS13 - Data Structures Summer 2010-2011 33

Verify the following formulas: 1. | x | = |_ x _| if and only if x is an integer 2. | x | = |_ x _| + 1 if and only if x is not an integer 3. |_ -x _| = - | x | 4. |_ x _| + |_ y _| |_ x + y _| 5. x = |_ x _| + x mod 1 6. z(x mod y) = zx mod zy

CS13 - Data Structures

Summer 2010-2011

34

The O-notation (big-oh)


used to describe the time or space complexity of an algorithm an algorithm has time complexity O(f(n)) if the actual number of steps executed to completion is no more than some constant times f(n) for sufficiently large n an algorithm has space complexity O(f(n)) if the actual amount of space it uses to execute is no more than some constant times f(n) for sufficiently large n
CS13 - Data Structures Summer 2010-2011 35

The O-notation (big-oh)


More formally, we say that g(n) = O(f(n)) if there exists two constants K and n0 such that |g(n)| k . |_f(n)_| for all n n0. We do not specify the actual values of K and n0; these constants may in fact be different for every appearance of O

CS13 - Data Structures

Summer 2010-2011

36

The O-notation (big-oh)


Example: g(n)= 13 + 23 + 33 + +n3 steps. We assert that the algorithm runs in, or has time complexity O(n4). To prove, let K=1 and n0 = 1. Then we have, g(n)= 13 + 23 + 33 + +n3 = = [n(n+1)/2]2 1 .n4 = n2 + n 2n2 = n n2 which is true for all n 1.
CS13 - Data Structures Summer 2010-2011 37

The O-notation (big-oh)


We would simply write the previous example as: g(n) = [n(n+1)/2]2 = n4/4 + n3/2 + n2/4 = O(n4) since for sufficiently large n, the dominant term will be n4/4.

CS13 - Data Structures

Summer 2010-2011

38

Complexity, or performance, classes of the various algorithms we will encounter in this course:
Time Complexity O(1) O(log2n) O(n) O(n log2n) O(n2) O(n3) O(2n)
CS13 - Data Structures

Description Constant Logarithmic Linear Quadratic Cubic Exponential


Summer 2010-2011

Example Insert into d-list Binary search Linear search Heapsort Dijkstras algo/ bubble sort Floyds algorithm Algorithm for TSP
39

The table below shows the results for t=1 usec and n=1,000,000

CS13 - Data Structures

Summer 2010-2011

40

The table below shows the results for t=1 usec for five values of T

CS13 - Data Structures

Summer 2010-2011

41

Operations On The O-Notation


Rule for sums: Suppose that T1(n) = O(f(n)) and
T2(n) = O(g(n)). Then, T(n) = T1(n) + T2(n) = O(max[f(n), g(n)])

Examples:
(a) T(n) = 3n3 + 5n2 = O(n3) (b) T(n) = 2n + n4 + n log2n = O(2n)

CS13 - Data Structures

Summer 2010-2011

42

Operations On The O-Notation


2. Rule for products:
Suppose that T1(n) = O(f(n)) and T2(n) = O(g(n)). Then, T(n) = T1(n) . T2(n) = O(f(n). g(n)) Example: The time complexity of the following segment of code is O(n2).

CS13 - Data Structures

Summer 2010-2011

43

Operations On The O-Notation


The steps in the inner loop (which take constant time) are executed n + n-1 + n-2 + ...+ 2 + 1 times. This is the sum of the first n integers which is n(n+1)/2 = n2/2
+ n/2 = O(n2)

CS13 - Data Structures

Summer 2010-2011

44

Você também pode gostar