Classification

ruralrompSoftware and s/w Development

Dec 2, 2013 (3 years and 9 months ago)

82 views

Data Structure


Class notes and Handouts











CS 241

Dr. Chung Lee

Computer Science

Calif. State Polytechnic Univ.

Pomona






1

CS
240/241 CONCEPT OVERVIEW



In the beginning !!


industrial revolution


First and second


Computer


information proc
essing system




IPO (Input
-
Processing


Output) cycle


Information


facts and ideas


Data


Information in processable form.





"a representation of facts, concepts or instructions in a formalized manner suitable for
communication, interpretation or pro
cessing by human or automated means".

(ISO definition)


System
-

Collection of multiple items(components) integrated for
a
common purpose


Components



Hardware



Processors


CPU, PPU, DPU GPU




Storage devices



Primary (Main memory)





Secondary (Secondary storage device)




I/O devices



Input, output (Transput)


Software




System software


access tool. Provides efficient and user f
riendly

access

of resources




OS, Programming language (Low Level, Medium Level, High Le
vel)




Application software


productivity software tool to achieve some purpose




Office, Graphics, CAD/CAM,
D
atabase,




U
ser


Persons motivated to access computer system for a purpose

Access is done via
proper interface (Human Computer Interaction)



1
st

Gen : Command 2
nd

gen: Menu, 3
rd

:GUI (WIMP), 4
th

:Multimedia 5
th

:intelligent system


Then what is data structure?



Gotlieb &

Gotlieb : CS view of data structure







Def :
A particular way to store and organize data

so that

it can be



M
ath




used
___________

function



representation





DS



SW HW






__________



2

Software


p牯杲rm







data element








Domain






ADT




organizati
on




D
ata

S
tructure




Function
:Operational component














Axiom

(Invariant)


Program





Implementation






Al
gorithm








Simple



Data elements







Compound




Organization





Restricted access

Stack
,
Q
ueue,
D
eque



Linear







Unrestricted access



list, array, file







Tree




Non
-
linear


Graph







Access function (non
-
destructive, non
-
mutable, probing)



Operations





Modification function (Destructive, mutable, changi
ng)







Algorithms


Procedure or sequence of instructions for solving a problem


(P4 at textbook)



3

Abstract Data Type
(ADT)


Definition



Mathematical model of information.





Components

--

ADT = {D,F,A}



D (Domain)


Information components



F (Function)
--

Operation (Usage ) components



A (Axiom)


Information rules, invariant




Steps of building ADT

--
>
data structure


1)

Understand and clarify the nature of
the target
information unit.

2)

Identify and

determine which data objects and operations to include in the module.

3)

Express this property somewhat formally so that it can be understood
and communicate
well.

4)

Translate this formal specification into proper language. In C++, this becomes a .h file.

In J
ava this is
called "user interface".

5)

Upon fi
nalized specification,
write necessary
implementation
. This
includes storage scheme

and
operational detail
. Operational detail is expressed as separate functions

(methods)
.


Example
s

1.

ADT String


(1) Understand
the concept


linear collection of characters


Null string






(2) Identify domain and Operations (functions)
--



Domain
--

Character, position, string




Operation : length , create, charAt, concat




(3) Formalize


Domain (Data Object)


Type




Variables


String





String




S
t
, S
t
1, S
t
2, S
t
3,




character



char




c, c1, c2, c3,



index




N




i,j,k



other primitives



integer, float, boolean




4

Operations

(methods
)


--

two step process




Generic form (model)





Language speci
fic form

Object oriented


<function_name> ( <Argument_list> ) : <return_type>


<return_type> <function_name> ( <Argument_list> )


createString (

) : String

or

createString (c1,c2,

) : String






isEmpty(S
t
) : boolean


length(
St

) :
natural



identi
c
al(S
t
1,
Str2



) :
boolean


concatenate (S1, S2): string, concat(St
, c): string
,


s
ubstring(S
t
,
i
, j

) :
string


search(S
t
1,
St2

) :
integer


insert( Str1, i str2) : string insert(St, i, c ): string



(4) (5)


translate and implement.


2.

ADT Stack


(1) Understand the concept
--



LIFO





(2) Identify domain and Operations (functions)
--



Domain
--

data element , sstack




Operation


create, push, pop, peek, size, isEmpty, isFull





(3) Formalize

Domain (Data Object)


Type




Variables

St
ack






St
ack




S, S1, S2, S3,



element




el_type




x
,
x
1,
x
2,
x
3,



other primitives



integer, float, boolean




Operations

(methods
)



Generic form (model)





Language specific

form

Object oriented




Create () : stack

Push(stack, element): Stack

Pop(Stack) : stack

Size(stack): Ntutal

isEmpty(Stack): Boolean

peekstack): element





(4) (5)


translate and implement.


5

LINKED LISTS

Concept/Definitions :

Pointer
--

a valu
e interpreted as an address location. This can be a single location which contains a
necessary information or a “Starting Address” of a structure.


Referencing


Process of accessing the location. Based on “Fetch

Access“ cycle of CPU action.


Scattered s
torage


logically related data components are stored “all over the places”. Contrasts to
Contiguous storage where logically related data components are stored in “one place”.

Classification


Singly linked list


Chain(Linear list)

Pointer based LL(Addr
ess is an actual MM location.)


Doubly linked list






Multi
-
list


Ring(Circular list)

Cursor based LL (Array index is used as location.)


Comparisons with Contiguous list





Contiguous



Linked List

Physical vs Logical order


Same




Different


Accessed by




index, bias(offset, displacement)

pointer


Order(sequence) indicator


External (can see the value)


Internal(can’t see)



Nature of order indicator



Implicit



Explicit


Advantages

:


1) Efficient sto
rage usage
--

dynamic allocation. Runtime deallocation(G C )


2) Ease of insert/delete)


3) Multi
-
path access
--

more than one way to access the element (Graph)


4) Data sharing
--

one data object can be access
ed by many other variable or list.


5) Late binding


Actual variable, object, function or file can be determined at the time of running program.


Disadvantages

: 1) Overhead in pointer field(s) (storage space)



2) Overhead in Insert/dele
te operations (run time)



3) Speed of access (retrieval)
--

efficient algorithm can not be accommodated.



4) Operational anomalies, difficulty of understanding


Components of Linked lists







Header starting address








Singly linked node

data
-



Node
--

data element, node pointer










Doubly linked node


Singly linked list






Doub
ly linked list



Linear Linked List





Circular(ly) Linked List



6

(quick)
Review of L
inear Structures


1. Unrestricted linear list (
Called

general list
or just

list
)



Concept diagram:


Apple





Bread





Tomato








Modeling (A
bstract data type)


ADT List = { D, F, Axiom

}


D = {List, element, location}



L


x


k


F = ( {Destructive operations}


{Non
-
destructive operations} )



Create( ) : List




IsEmpty( L) : boolean





Insert_After( x, k, L) : List


Locate(L, k) : x




D
el
ete_after(k, L): List




Front(L) : x



Update ( x, k, L) : List



T
raverse(L) : seq of x



Implementations

A) Storage scheme


Contiguous







Linked















B) Operational detail


C
reate ( )

1.
Allocate an empty array of size N





1. C
reate (allocate) a header node and point null



Insert (L,

x, k)

0.

check if there is room




0. create a node and store x

1.

shift k
-
th through last element one position


1. locate k
-
th position

2.

store x at k.






2. update pointers

3.

update size value




-
1 : new node points next node












-
2 : previous node points this node




7

Restricted

Linear Lists







2.

Stack


LIFO

Concept (diagram)



Modeling (A





Implementations

A) Storage scheme

Contiguous







Linked








B) Operational detail








Usage




2.

Queue


LIFO

Concept (diagram)


waiting line



Modeling (A






Implementations

A) Storage scheme

Contiguous







Linked



B) Operational detail





Usage


8

Software Engineering


What is




Engineering approach to the life cycle of software.


Steps

1.

Plan
ning

2.

Requirement engineering

3.

Design

4.

Coding

5.

Testing

6.

Release

7.

Maintenance

Or generically,


1.

Definition phase

2.

Development phase

3.

Maintenance phase


Different
process
models

0.

Code and fix

1.

Waterfall

2.

Spiral

3.

Prototype

4.

Formal

5.

Synchronize and stabilize

6.

Agile


XP, S
crum,
Crystal, FDD, ASD, DSDM


9

Java
F
acts

Java : Object Oriented Programming Language (OOPL)


What is OOPL?



Requirements for OOPL :

1.

Encapsulation

2.

Inheritance

3.

Polymorphism



Strength
s

of Java



WORA concept




API’s





Version
s :



Open source



JDK vs JRE



Version History




Java 1.0

-

JDK 1.0 (jan 1996
)
, JDK 1.1 (Feb 1997)



J2SE 1.2 (Dec 1998)


java 2

with different versions
--

Java ME/SE/EE





J2SE 1.3 (May 2000), J2SE 1.4 (Feb 2002)



J2SE 5.0 (Sept 2004)



Java SE 6 (Dec. 2006)



Java SE 6 update 10 (Oct. 2008) ….. update 24 (Feb. 2011)



Java 7 (Dol
phin)

Currently in Beta version.



Java 8 : Expected in 2012




JavaFX : RIA(ich Internet Application) version (Runnable in Netbean or Eclipse)


10

A
LGORITHM


Defi
nition



A step by step instruction to complete a task in a finite steps



Background


Mohamed ibn ………. Al’kwarizmi Turing




Classification




Recursive, Greedy


local
-
optima


Brute
-
force Branch
-
and
-
bound


Divide
-
conquer


Depth
-
first


Breadth
-
firs


Backtracking




Method of algorithm Expression

A.

Verbal description

B.

Pseudocode

C.

Visual chart

D.

Algorithmic Language(s)

E.

Programming Language(s)


Requirements


1.

Unambiguous


detail
, specific


2.

Finite


non
-
infinity (ending condition)


3.

Feasible (Effective)


doable, executable by machine Turing machine


4.

Has Input


beginning value


5.

Has Output
--
result



Missing ?
--

correctness, efficiency




Program efficiency



Time efficienc
y vs space efficiency




--

Time
-
space trade off


minimum running time


Factors

(1)

Hardware

(2)

System software

(3)

Data size

(4)

Algorithm logic


instruction arrangement



11


Running time Measurement (Expression)


Processor clock time
--



Size of instructions to

execute





Absolute number
--





Equation
--

complexity function


Running time Notation Definitions


Big
-
Oh : Running time, T(n) = O(g(n)) iff


c, n0




n


n0, T(n)


c

g(n)

where g(n)=n
0
, n
1
, n
2
, log(n), n log(n), 2
n

, m+n NP
-
complete, NP
-
incomplete


Big
-
Omega : Running time, T(n) =

(g(n)) iff


c, n0




n


n0, T(n)


c


g(n)


Theta : Running time, T(n) =


(g(n)) iff T(n) = O(g(n)
)


T(n) =

(g(n))



Little
-
oh : Running time, T(n) = o (g(n)) iff T(n) = O(g(n))


T(n)




(g(n))




What does this mean?
first,

T(n).


A program segment:


version 1

Version 2




Version 3



public class DoThisAndThat {


public static void main(String [ ] argv) {


int aNum=0;


aNum = Termio.readInt();


for (int i=0;i<aNum<i++){


if (aNum >

0) {





System.out.print("Series is ");

for (int j=0;j<aNum, j++){


System.out.print("Square is ");



System.out.println ( i*aNum);


System.out.println ( j*i);


System.out.println( aNum*aNum);


}





System.out.println
( x*i*aNum);

}









}

else {


System.out.print("Negative square is ");


System.out.println(
-

aNum*aNum);

}


}

}





T1=T(N) = 5




T2=T(N) = 3N+6



T3= T(N) = 3N
2

+ 4N+6







For T2, g(N)=N
0



g(N) = g(N)=N
1

g(N)=N
2

N

T1

T2

T3



c=7, 10, 100, 10000


c=3 7 10



0

5


6


6




1

5


9


13



2

5

12


26


10


36



20

5


1286


100


306

30406









For T3,







g(N)=N
0



N
1


N
2


N
3


For T1
, c=6, g(N)=N
0
, N
1
, N
2
, …




12



Recur
sive algorithms (Recursion)


Definition : A method calling itself



Direct recursion


calling within method



Indirect recusion


A


B _
--
>C


A

What is
?


another form of loop.



Loop (Iteration, repetition)




Recursion


LCV (Loop co
ntrol Variiable)



In
itializer



Mod
ifier



Term
inator



For (int i=0; i<10; i++)


Body




e.g. Calculating factorial



public long factorial (int n) {



if (n <0)




throw new IllegalArgumentException( );



if (n <= 1)




return 1;



return n * factorial(n
-
1);


}


Hand tracing for
val = factorial(3);










Tower of Hanoi



void hanoi(Disk n, Peg A, Peg B, Peg C) {






if (n == 1)




move (n, A, C);



else {




hanoi (n
-
1, A, C, B);




move (n, A, C);




hanoi (n
-
1, B, A, C);



}


}


www.
mazeworks.com
/
hanoi
/
hanoi
.htm