Professional Documents
Culture Documents
CS13 - Data Structures Summer 2010-2011 1
CS13 - Data Structures Summer 2010-2011 1
CS13 - Data Structures Summer 2010-2011 1
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
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
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.
Summer 2010-2011
Summer 2010-2011
Summer 2010-2011
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
IT
BS
4000
4000
^
BS
^
IT
BS
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
Summer 2010-2011
14
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
Summer 2010-2011
16
Summer 2010-2011
17
Summer 2010-2011
18
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
Ui are the lower and upper bounds for the ith dimension b. The node statement
node (f1, f2, fi,fn)
The call statement transfers control to the procedure named name b. The return statement
return or return (expression)
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
- 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
2. List inversion: Given a singly-linked linear list, invert the list in-place.
CS13 - Data Structures Summer 2010-2011 30
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
Summer 2010-2011
32
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
Summer 2010-2011
34
Summer 2010-2011
36
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
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
Summer 2010-2011
40
The table below shows the results for t=1 usec for five values of T
Summer 2010-2011
41
Examples:
(a) T(n) = 3n3 + 5n2 = O(n3) (b) T(n) = 2n + n4 + n log2n = O(2n)
Summer 2010-2011
42
Summer 2010-2011
43
Summer 2010-2011
44