dsj2

Transkript

dsj2
Datové struktury a algoritmy
Část 5
Abstraktní datové typy
Petr Felkel
Data structures and algorithms
Part 5
Abstract data types
Petr Felkel
Abstraktní datové typy
• Abstrahují od jak?
•
•
Jak jsou data zobrazena v paměti…
Jak jsou prováděny operace…
• Zdůrazňují co?
•
DSA
Co s nimi operace s daty provádějí
nezávisle na konkrétní implementaci
3 / 61
Abstract data types
• Abstract from: How?
•
•
How is the data stored in memory?
How are the operations executed?
• Emphasize: What?
•
DSA
What do the operations do with the data
(independent of any particular
implementation)?
4 / 61
Abstraktní datové typy
9
9
9
9
Fronta (Queue)
Zásobník (Stack)
Pole (Array)
Tabulka (Table)
• Seznam (List)
• Strom (Tree)
• Množina (Set)
DSA
5 / 61
Seznam (List)
• Posloupnost údajů
• Ukazovátko
• Pouze v místě ukazovátka lze přidat /
zrušit / aktualizovat prvek
• Homogenní, lineární, dynamická
DSA
6 / 61
List (Seznam)
• Sequence of items
• Marker
• Insert, delete, update only where
points the marker
• Homogeneous, linear, dynamic
DSA
7 / 61
List (Seznam)
init
Nat
length
Bool
DSA
read
List
Elem
empty,
insert
full delete,
first,last
next,prev
8 / 61
List (Seznam)
init: -> List
insert(_,_): Elem, List -> List
read(_): List -> Elem
delete(_),
first(_), last(_)
next(_), prev(_): List -> List
length(_): List -> Nat
empty(_), full(_)
atbeg(_), atend(_): List -> Bool
DSA
9 / 61
List (Seznam)
Pomocný druh
Seq = posloupnost bez ukazovátka
Pomocné operace:
new: -> Seq
... prázdná posloupnost
cons(_,_): Elem, Seq -> Seq
cons2(_,_): Elem, List -> List
... vlož do čela
mark(_): Seq -> List ... ukaz. do čela
DSA
10 / 61
List (Seznam)
Example:
a b c d
cons2( a, cons2( b, mark(
cons( c, cons( d, new )))))
DSA
11 / 61
List – insert
a b c d
Where to insert?
Where to leave the marker?
a b
a b
DSA
x
x
c d
c d
a b c
a b c
x
x
d
d
12 / 61
List – delete
a b c d
DSA
Where to leave the marker?
a b d
On the next
a b d
On the previous
13 / 61
List (Seznam)
var: x,y:Elem, s:Seq, l:List
init = mark( new )
x
insert( x, mark(s) )
= cons2( x, mark(s))
... Insert BEFORE the pointer
x
x
insert( x, cons2( y, l ))= y
cons2( y, insert( x, l )) y x
DSA
14 / 61
List (Seznam)
delete( init ) = init
delete( mark( cons( x, s)))
= mark( s )
x
... delete by the pointer
delete( cons2( x, l )) = x
cons2( x, delete( l ))
x
... delete by the pointer
DSA
15 / 61
List (Seznam)
next( init ) = init
next( mark( cons( x, s ) ) ) =
cons2( x, mark( s ) )
... pointer move
x
x
next( cons2( x, l ) ) =
cons2( x, next( l ) )
..no change before x
DSA
x
16 / 61
List (Seznam)
prev( mark(s) ) = mark(s)
prev( cons2( x, mark( s ) ) ) =
mark( cons( x, s ))
... move by head
x
x
prev( cons2( x, cons2( y, l ))) =
cons2( x, prev( cons2( y, l )))
x y
x y
... move inside
DSA
17 / 61
List (Seznam)
first( mark(s) ) = mark(s)
... no move by head
first( cons2( x, l ) ) =
first( prev( cons2( x, l ) ) )
... move step by
step to front
DSA
x
x
18 / 61
List (Seznam)
last( init ) = init
last( mark( cons( x, s ) ) ) =
cons2( x, last( mark( s ) ) )
... move back
x
x
last( cons2( x, l ) ) =
cons2( x, last( l ) )
…no change before x
DSA
x
19 / 61
List (Seznam)
read( init ) = error_elem
read( cons2( x, l ) = read( l )
read( mark( cons( x, s ))) = x
DSA
20 / 61
List (Seznam)
length( init ) = 0
length( cons2( x, l )) =
succ( length( l ) )
... elements before the pointer
length( mark( cons( x, s ))) =
succ( length( mark( s )))
... elements after the pointer
DSA
21 / 61
List (Seznam)
empty( l ) = ( length( l ) == 0 )
full( l ) = ( length( l ) == max )
atbeg( mark( s )) = true
atbeg( cons2( x, l )) = false
atend( mark( new)) = true
atend( mark( cons( x, s )))= false
atend( cons2( x, l )) = atend( l )
DSA
22 / 61
List implementation
In Array
O(n) insert, delete
0 1
2
3
4
5
O(1) first, last, prev, next
A B C D
(head) point
tail
Stacks in Array
0 1
2
3
A B
(head) point
DSA
O(1) insert, delete, prev, next
4
5
O(n) first, last, …
A B C
C D
(tail)
point
23 / 61
D
List implementation
In Dynamic memory
O(1) insert, O(1)..O(n) delete
O(1) first, last, prev, next
head
tail
point
len
3
head
tail
point
len
3
head
tail
point
len
DSA
A
B
- memory for pointers
C
Linked list
A
B
Double linked list
C
Circular double linked list
3
A
B
C
24 / 61
List implementation
Linked list
head
tail
point
len
A
B
C
3
help
head
tail
point
len
DSA
A
3
x
B
C
list::insert( elem x ) {
item *help = new item;
if( point == NULL ){ //point behind
help->next = NULL;
help->val = x;
if( tail == NULL ) //empty list
head = help;
else
//add at end
tail->next = help;
tail = help;
} //point still points behind list!
else { //point in the list - trick
help->val = point->val;
help->next = point->next;
point->next = help;
point->val = x;
point = help;
}
len++;
}
25 / 61
List implementation
Linked list
help
head
tail
point
len
A
B
C
3
help
head
tail
point
len
A
B
C
D
4
help
head
tail
point
len
DSA
A
3
B
D
D
list::delete( ) {
item *help;
if( point != NULL ){ //behind ignore
if( point->next == NULL ) { //last
help = head; //find predecessor
while( help->next != point )
help = help->next;
}
help->next = NULL;
point = NULL;
delete( tail ); tail = help;
}
else {// trick: skip predec.search
help = point->next;
*point = *help;
if( help == tail )
tail = point;
delete( help );
}
len--;
}
}
26 / 61
List implementation
list::prev( ) {
Linked list
head
tail
point
len
A
B
item *help;
if( point != head){//point can move
help = head;
while( help->next != point )
help = help->next;
point = help;
}
C
3
}
help
head
tail
point
len
DSA
A
x
B
C
4
27 / 61
List implementation
list::prev( ) {
Double linked list
head
tail
point
len
3
A
B
C
item *help;
if( point != head){//point can move
point = point->prev;
}
}
Prev is the only place to save!
DSA
28 / 61
List implementation
Double linked list
head
tail
point
len
3
A
B
C
list::delete( ) {
item *help;
if( point != NULL ){ //behind ignore
help = point->next ;
if( head == point ) //first elem
head = help;
if( tail == point ) //last elem
tail = tail->prev;
if( help != NULL ) //update prev
help->prev = point->prev;
//update next
if( point->prev != NULL );
point->prev->next = help;
delete( point );
point = help;
len--;
}
}
DSA
29 / 61
List implementation
Circular double linked list
head
tail
list
point
len
A
B
list::delete( ) {
item *help;
if( point != list ){ //not at end
point->prev->next = point->next;
point->next->prev = point->prev;
help = point;
point = point->next;
C
delete( help );
len--;
3
help
}
}
list::prev( ) {
if( !atBegin() ) //point!=list->head
point = point->prev;
}
DSA
30 / 61
Abstraktní datové typy
9
9
9
9
Fronta (Queue)
Zásobník (Stack)
Pole (Array)
Tabulka (Table)
9Seznam (List)
• Strom (Tree)
• Množina (Set)
DSA
31 / 61
Strom (Tree)
• Kdy?
•
•
•
DSA
řazení, vyhledávání, vyhodnocování
výrazů, ...
acyklický souvislý graf
kořenový strom
•
•
•
•
orientovaný strom, zvláštní uzel - kořen
kořen spojen se všemi uzly orient. cestou
binární strom - má 0, (1), 2 následníky
uspořádaný binární strom - dvojice <u, u1>
a <u, u2> jsou uspořádány
32 / 61
Tree (Strom)
• When?
•
•
•
DSA
sorting, searching, evaluation of
expressions, ...
acyclic connected graph
root tree
•
•
•
•
oriented tree, special node - root
root connected to all nodes by oriented path
binary tree - has 0, (1), 2 successors
ordered binary tree - pairs <u, u1> and <u,
u2> are ordered
33 / 61
Strom (Tree)
• uzel - více následníků
• binární strom - 2 následníci
• následník - opět strom
• homogenní, nelineární, dynamická
DSA
34 / 61
Tree (Strom)
• node - more successors
• binary tree - 2 successors
• successor - also a tree
• homogeneous, nonlinear, dynamic
DSA
35 / 61
Binární strom (Tree)
empty
null
info
Tree
leaf
Elem
Bool
left,
right
DSA
cons
setleft,
setright
36 / 61
Binární strom (Tree)
empty: -> Tree
leaf(_): Elem -> Tree
cons(_,_,_): Elem, Tree, Tree
->Tree
left(_), right(_): Tree -> Tree
null(_): Tree -> Bool
setleft, setright(_,_): Tree,
Tree -> Tree
setinfo( Tree, Elem ): -> Tree
info(_): Tree -> Elem
DSA
37 / 61
Binární strom (Tree)
var x: Elem; a,b,t: tree
leaf( x ) = cons( x, empty, empty)
left( empty ) = error_tree
left( cons( x, a, b)) = a
right( empty ) = error_tree
right( cons( x, a, b)) = b
DSA
38 / 61
Binární strom (Tree)
null( empty ) = true
null( cons( x, a, b)) = false
setleft( empty, t ) = error_tree
setleft( cons( x, a, b ), t ) =
cons( x, t, b )
setright( empty, t ) = error_tree
setright( cons( x, a, b ), t ) =
cons( x, a, t )
DSA
39 / 61
Binární strom (Tree)
setinfo( empty, x ) = error_tree
setinfo( cons( x, a, b ), y ) =
cons( y, a, b )
info( empty ) = error_elem
info( cons( x, a, b ) ) = x
DSA
40 / 61
Abstraktní datové typy
9
9
9
9
Fronta (Queue)
Zásobník (Stack)
Pole (Array)
Tabulka (Table)
9Seznam (List)
9Strom (Tree)
• Množina (Set)
DSA
41 / 61
Parametrické datové typy
• Nové typy
•
•
•
obohacováním původních o druhy a
operace
často stejné, jen nad jinými základy
liší se jen některými prvky
=> parametrický datový typ
odlišné prvky - parametry
DSA
42 / 61
Množina
typ: MNOŽINA( ELEMENT )
parametr: typ prvků ELEMENT
požadavky na parametr:
druhy: Elem
operace: eq(_,_): Elem, Elem -> Bool
použité typy: ELEMENT, přirozená čísla,
logické hodnoty
druhy: Elem, Bool, Set, Nat
DSA
43 / 61
Množina
operace:
[]: Set
(prázdná množina)
ins(_,_): Elem,Set ->Set (vložení prvku)
del(_,_): Elem,Set ->Set (zrušení -“in(_,_) : Elem,Set ->Bool(test přísluš.)
card(_) : Set->Nat
DSA
(počet prvků)
44 / 61
)
Množina bez opakování
proměnné:
s: Set, x,y: Elem
bez opakování prvků
axiomy:
ins(x,s) = if in(x,s) then s
del(x,[])=[]
del(x,ins(y,s))= if eq(x,y)
then del(x,s)
else ins(y,del(x,s))
nezáleží na pořadí vkládání
DSA
45 / 61
Množina bez opakování
axiomy: (pokračování)
in(x,[]) = false
in(x,ins(y,s))= if eq(x,y)then true
else in(x,s)
card([]) = 0
card(ins(x,s)) = if(in(x,s)) card(s)
else succ(card(s))
DSA
46 / 61
Množina bez opakování
axiomy: (pokračování)
eq([],[]) = true
eq([], ins(x, t)) = false
eq(s,t) = eq(t,s)
bez opakování prvků
eq(ins(x,s),t) = if in(x,s) then eq(s,t)
else if in(x,t)
then eq(s,del(x,t))
else false
DSA
47 / 61
Abstraktní datové typy
9
9
9
9
Fronta (Queue)
Zásobník (Stack)
Pole (Array)
Tabulka (Table)
9Seznam (List)
9Strom (Tree)
9Množina (Set)
DSA
48 / 61
Appendix A
Bool example
DSA
49 / 61
Bool data type - signature
• Logical value - Bool
• Diagram: true,false
Bool
and,eq
not
Symbolic: see next slide
DSA
50 / 61
Bool data type - signature
Logical value - Bool
Symbolic:
druhy: Bool
operace: true,false: Bool (constants, nulary)
not: Bool -> Bool (unary operation)
and: Bool,Bool -> Bool
(binary)
eq: Bool,Bool -> Bool
DSA
51 / 61
Bool data type - signature
Prefix notation
and(_,_): Bool,Bool -> Bool
Infixový notation
_&_:
Bool,Bool -> Bool
priority: not,and ... Must be stated explicitly
DSA
52 / 61
Bool data type - axioms
var x,y: Bool
not(true) = false
not(false) = true
and(x, true) = x
and(x,false) = false
and(x,y) = and(y,x)
or(x, true) = true
or(x,false) = x
or(x,y) = or(y,x)
DSA
negation
logical AND
logical OR
53 / 61
Bool data type - axioms
Equality - ver. 1
eq(true, true) = true
eq(true,false) = false
eq(false, true) = false
eq(false,false) = true
DSA
54 / 61
Bool data type - axioms
Equality - ver. 2
eq(x, true) = x
eq(x, false) = not(x)
eq(x, y) = eq(y, x)
DSA
55 / 61
Appendix B - Expressions
a) constant: f
(where f is nulary operation declared: f:d)
b) variable: x
(where f is a variable declared var x:d)
c) operation – prefix notation: f(t1,t2, ... tn)
(where f is n-ary operation declared:
f: d1, d2, ..., dn -> d
and t1, t2, ..., tn are expressions of sorts
d1, d2, ..., dn)
d) operace – infix notation : t1 f1 t2 f2 ...fn tn+1
(where f (n+1)-ary operation declared
_f1_f2_..._fn : d1, d2, ..., dn+1 -> d
and t1, t2, ..., tn+1 expressions of sorts d1, d2, ...,
dn+1)
e) brackets: (t)
(where t is an expression of sort d)
f) other notations are not expressions.
DSA
56 / 61
Expressions (cont.):
Herbrand’s universum for a given signature
= set of all expressions on given signature
Example:
{true, false, not(true), not(false),
not(not(true)),...}
Two classes of equivalence (describe the same value)
• {true, not(false), not(not(true)),...}
... [true]
• {false, not(true), not(not(false)),...}
... [false]
DSA
57 / 61
Bool - třídy ekvivalence
• Množina všech výrazů nad signaturou
(Herbrandovo univerzum)
{true, false, not(true), not(false),
not(not(true)),...}
DSA
•
Bool: lze rozdělit na 2 třídy ekvivalence:
•
Výrazy třídy popisují různý způsob
konstrukce stejné hodnoty
•
•
{true, not(false), not(not(true)),...} ... [true]
{false, not(true), not(not(false)),...}... [false]
58 / 61
Bool - třídy ekvivalence
Doplníme axiomy
true = [true]
false = [false]
not([x])=[not(x)]
and([x],[y]) = [and(x, y)]
Axiomy s třídami ekvivalence
=> tvoří model datového typu
DSA
59 / 61
Prameny / References
• Jan Honzík: Programovací techniky, skripta,
VUT Brno, 19xx
• Karel Richta: Datové struktury, skripta pro
postgraduální studium, ČVUT Praha, 1990
• Bohuslav Hudec: Programovací techniky,
skripta, ČVUT Praha, 1993
DSA
60 / 61
Prameny / References
• Steven Skiena: The Algorithm Design Manual,
Springer-Verlag New York, 1998
http://www.cs.sunysb.edu/~algorith
• Gang of four (Cormen, Leiserson, Rivest, Stein):
Introduction to Algorithms, MIT Press, 1990
• Code exapmples: M.A.Weiss: Data Structures and
Problem Solving using JAVA, Addison Wesley,
2001, code web page:
http://www.cs.fiu.edu/~weiss/dsj2/
code/code.html
DSA
61 / 61

Podobné dokumenty

Experimentální analýza algoritmů je empirickým protějškem

Experimentální analýza algoritmů je empirickým protějškem Stony Brook Algorithm Repository http://www.cs.sunysb.edu/~algorith/ spojeno s knihou S. Skiena: Algorithm Design Manual, Springer 1997, 2. vydání 2008 http://www.springer.com/computer/theoretical+...

Více

Experimentální analýza algoritmů je empirickým protějškem

Experimentální analýza algoritmů je empirickým protějškem Stony Brook Algorithm Repository http://www.cs.sunysb.edu/~algorith/ spojeno s knihou S. Skiena: Algorithm Design Manual, Springer 1997, 2. vydání 2008 http://www.springer.com/computer/theoretical+...

Více

Základy algoritmizace

Základy algoritmizace 3. FOWLER, M. Patterns of Enterprise Application Architecture. Addison-Wesley Professional, 2002. ISBN 0321127420. 4. FOWLER, M. Domain-Specific Languages. Addison-Wesley Signature Series, 2010. I...

Více

Dokazování v predikátové logice

Dokazování v predikátové logice Definition ((jednoduchý) sémantický strom pro S, částečná realizace) Nechť S je množina klauzulı́, AS Herbrandova báze pro S. Sémantický strom pro S je kořenový binárnı́ strom T , k...

Více

AMX Mod X

AMX Mod X www.n1ce.cz – GAMEHOSTING & WEBHOSTING Statická IP adresa: otevřeme si soubor usere.ini, který najdeme na FTP svého serveru cstrike/addons/amxmodx/configs, tento soubor si otevřeme v poznámkovém b...

Více

ADS1-ZFS

ADS1-ZFS Vzhledem ke specifikaci zásobníku jako posloupnosti prvků se přímo nabízí implementace zásobníku pomocí pole. Tak např. v jazyku PASCAL je pak možná deklarace záznamu obsahujícího dvě položky : pol...

Více

Inicializační rozdělení do shluků a jeho vliv na konečné shlukování v

Inicializační rozdělení do shluků a jeho vliv na konečné shlukování v Cambridge University Press, New York 2007. [5] Řezanková, H., Húsek, D., Snášel, V.: Shluková analýza dat. Professional Publishing, Praha 2007. [6] Zhang, T., Ramakrishnan, R., Livny, M.: BIRCH: An...

Více

Umělá inteligence I Roman Barták, KTIML

Umělá inteligence I Roman Barták, KTIML Co třeba programovací jazyky (C++, Java,…)?

Více