cpe261702 algorithm analysis & design presentation topic in map
Post on 09-Jan-2016
19 Views
Preview:
DESCRIPTION
TRANSCRIPT
CPE261702 ALGORITHM ANALYSIS & DESIGNPRESENTATION TOPIC IN
MAP
1Master of Computer Engineering Chiangmai University
Talking Outline
Introduction Basic Abstract Data Type (ADT) How do implementation ?
Double Linked List Hash Table
Bucket Array Hash Function
Trie AVL Tree
Performing & Complexity Demo Programmer
2Master of Computer Engineering Chiangmai University
Basic Array
3Master of Computer Engineering Chiangmai University
12000 11000 15000 20000 18000Array salary
0 1 2 3 4index
We can build to platform ( php code )
$salary[i] = ?EX.
$salary[1] =$salary[3] =
But we can’t know. Who’s salary ?
1100020000
Associative Array
4Master of Computer Engineering Chiangmai University
12000 11000 15000 20000 18000Array salary
Sara Bob Smith John Stevename
We can build to platform ( php code )
$salary[‘name’] = ?EX.
$salary[‘Smith’] =
We can replace index by name.
15000
Abstract Data Type
Since a map store a collection of object, it should be viewed as a collection of key-value pairs. As an ADT, a map M supports the following methods :
size () : Return the number of entries in M isEmpty () : Test whether M is empty. get (k) : If M contain an entry e with key equal to k, then return the value of e, else return null. put (k,v) : If M does not have an entry with key equal to k, then add entry (k,v) to M and return null; else, replace with v the existing value of the entry with key equal to k and return the old value. remove (k) : Remove from M the entry with key equal to k, and return its value; if M has no such entry, then return null.
5Master of Computer Engineering Chiangmai University
Abstract Data Type (Cont.)
keys () : Return an iterable collection containing all the keys stored in M (so keys().iterator() returns an iterator of keys). values () : Return an iterable collection containing all the values associated with keys stored in M (so values().iterator() returns an iterator of values). entries () : Return an iterable collection containing all the key- value entries in M (so entries().iterator() returns an iterator of entries).
6Master of Computer Engineering Chiangmai University
Example
7Master of Computer Engineering Chiangmai University
Operation Output Map
isEmpty() true Ø
put(5,A) null {(5,A)}
put(7,B) null {(5,A),(7,B)}
put(2,C) null {(5,A),(7,B),(2,C)}
put(8,D) null {(5,A),(7,B),(2,C),(8,D)}
put(2,E) C {(5,A),(7,B),(2,E),(8,D)}
get(7) B {(5,A),(7,B),(2,E),(8,D)}
get(4) null {(5,A),(7,B),(2,E),(8,D)}
get(2) E {(5,A),(7,B),(2,E),(8,D)}
size() 4 {(5,A),(7,B),(2,E),(8,D)}
Example (Cont.)
8Master of Computer Engineering Chiangmai University
Operation Output Map
remove(5) A {(7,B),(2,E),(8,D)}
remove(2) E {(7,B),(8,D)}
get(2) null {(7,B),(8,D)}
isEmply() false {(7,B),(8,D)}
Simple List-Based Map
A simple way of implementing a map is to store its n entries in list S,Implemented as an unsorted list ( base on doubly linked list ).
9Master of Computer Engineering Chiangmai University
trailerheader
nodes/positions
entries
Sara 11000
put ( Sara , 11000 )
Simple List-Based Map
put ( Bob , 12000 )
10Master of Computer Engineering Chiangmai University
trailerheader
nodes/positions
entries
Sara 11000 Bob 12000
Simple List-Based Map
put ( Smith , 15000 )
11Master of Computer Engineering Chiangmai University
trailerheader
nodes/positions
entries
Sara 11000 Bob 12000 Smith 15000
Simple List-Based Map
put ( Steve , 18000 )
12Master of Computer Engineering Chiangmai University
trailerheader
nodes/positions
entries
Sara 11000 Bob 12000 Smith 15000 Steve 18000
Simple List-Based Map
put ( Smith , 20000 )
13Master of Computer Engineering Chiangmai University
trailerheader
nodes/positions
entries
Sara 11000 Bob 12000 Smith Steve 180002000015000
Return value = 15000
Simple List-Based Map
get ( Steve )
14Master of Computer Engineering Chiangmai University
trailerheader
nodes/positions
entries
Sara 11000 Bob 12000 Smith 20000 Steve 18000
Return value = 18000
Simple List-Based Map
get ( John )
15Master of Computer Engineering Chiangmai University
trailerheader
nodes/positions
entries
Sara 11000 Bob 12000 Smith 20000 Steve 18000
Return value = null
Simple List-Based Map
remove ( Bob )
16Master of Computer Engineering Chiangmai University
trailerheader
nodes/positions
entries
Sara 11000 Bob 12000 Smith 20000 Steve 18000
Return value = 12000
Simple List-Based Map
remove ( Stephen )
17Master of Computer Engineering Chiangmai University
trailerheader
nodes/positions
entries
Sara 11000 Smith 20000 Steve 18000
Return value = null
Simple List-Based Map Algorithm
Algorithm put ( k , v ) : Input : A key-value pair ( k , v ) Output : The old value associated with key k in M, or null if k is new for each position p in S.positions () do if p.element () .getkey () = k then t p.element () .getValue () B.set ( p, ( k , v )) return t S.addLast (( k , v )) n n + 1 return null
18Master of Computer Engineering Chiangmai University
Simple List-Based Map Algorithm
Algorithm get ( k ) : Input : A key k Output : The value of key k in M, or null if there is no key k in M for each position p in S.positions () do if p.element () .getkey () = k then return p.element () .getvalue () return nullAlgorithm remove ( k ) : Input : A key k Output : The ( removed ) value of key k in M, or null if k is not in M for each position p in S.positions () do if p.element () .getkey () = k then t p.element () .getValue () S.remove ( p ) n n - 1 return t return null
19Master of Computer Engineering Chiangmai University
Hash Table
Hash Table is data structure use to map identifying value, know as key (e.g., a person’s name ), to their associated value (e.g., their telephone number).
Two components in Hash Table :
Bucket Array
Hash Function
20Master of Computer Engineering Chiangmai University
Hash Table Architecture
21Master of Computer Engineering Chiangmai University
String Key
String
Hash Code
Compression Function
Hash Function
Hash Table
Addr. Key Data
Example Implement
A small phone book as a hash table.
22Master of Computer Engineering Chiangmai University
Figure Reference : http://th.wikipedia.org/
Hash Table Algorithm
Map Methods with Separate Chaining used for Collisions.
Delegate operations to a list-based map at each cell.
23Master of Computer Engineering Chiangmai University
Algorithm get(k):
Output: The value associated with the key k in the map, or null if there is no
entry with key equal to k in the map
return B[h(k)].get(k) {delegate the get to the list-based map at B[h(k)]}
Algorithm put(k,v):
Output: If there is an existing entry in our map with key equal to k, then we
return its value (replacing it with v); otherwise, we return null
t = B[h(k)].put(k,v) {delegate the put to the list-based map at B[h(k)]}
if t = null then {k is a new key}
n = n + 1
return t
Hash Table Algorithm
24Master of Computer Engineering Chiangmai University
Algorithm remove(k):
Output: The (removed) value associated with key k in the map, or null if there
is no entry with key equal to k in the map
t = B[h(k)].remove(k) {delegate the remove to the list-based map at B[h(k)]}
if t ≠ null then {k was found}
n = n - 1
return t
The advantages of Hash Table
It has speed more than other table data
structures.
It has high performance when the number of entries
is large.
It has particularly efficient when the maximum
number of entries can be predicted in advance.
25Master of Computer Engineering Chiangmai University
The disadvantages of Hash Table
Can’t be more difficult implement than self-balancing
Binary Search Tree ( BST )
Consuming more resources. If the data is less than the
surface area of reservation.
Not effective when the number of entries is very small.
Can’t find minimum or maximum data value.
Can’t sorting in Hash Table.
26Master of Computer Engineering Chiangmai University
Trie
Ordered tree data structure.
Used to store an associative array.
The keys usually strings.
No node in the tree stores the key associated with
that node.
All the descendants of a node have a common prefix
of the string associated with that node.
The root is associated with the empty string.
Values are only associated with leaves and some inner
node that correspond to keys of interest.
27Master of Computer Engineering Chiangmai University
Example
Tries
28Master of Computer Engineering Chiangmai University
Figure Reference : http://en.wikipedia.org
Keys Values
to 7
tea 3
ted 4
ten 12
A 15
i 11
in 5
inn 9
Trie Algorithm
Pseudo-code for Insert:
29Master of Computer Engineering Chiangmai University
Trie Algorithm
30Master of Computer Engineering Chiangmai University
Trie searching method used same way as search tree structure.
In this case, we use preorder traversal.
Algorithm preOrder(v)visit(v)for each child w of v
preorder (w)
Tries Compare BST.
The main advantages of the tries over binary search trees(BSTs) :
Looking up keys is faster.
Tries can require less space when they contain a large number
of short string.
Tries facilitate longest-prefix matching.
31Master of Computer Engineering Chiangmai University
Tries Compare Hash Table.
The main advantages of the tries over hash tables:
Tries can perform a “closest fit ” find almost as quickly as an
exact find.
Tries tend to be faster on average at insertion than hash table.
Tries can be implemented in a way which avoids the need for
additional ( dynamic ) memory.
Looking up keys can be much faster if a hash function can be
avoided.
32Master of Computer Engineering Chiangmai University
Advantages of Tries
Looking up data in a trie is faster in the worst case, O(m) time,
compared to an imperfect hash table.
There are no collisions of different keys in a trie.
Bucket in a trie which are analogous to hash table bucket that
store key collisions are only necessary if a single key is
associated with more than one value.
There is no need to provide a hash function or to change hash
functions as more keys are added to a trie.
A trie can provide an alphabetical ordering of the entries by key.
33Master of Computer Engineering Chiangmai University
AVL Tree
AVL Tree is a self-balancing binary search tree. In an AVL tree,
The height of the two child subtrees of any node differ by at
most one.
Search, insertion, and deletion all take O( log n ) time.
Insertion and deletion may require the tree to be re balanced by
one or more tree rotation.
34Master of Computer Engineering Chiangmai University
AVL Tree Architecture
35Master of Computer Engineering Chiangmai University
String KeyMathematic
FunctionInteger Key AVL Tree
Example Implement
36Master of Computer Engineering Chiangmai University
44,A
17,B 78,D
48,F
88,H50,E32,C
63,G
AVL Tree
Insertion AVL Tree
37Master of Computer Engineering Chiangmai University
Insert (15,Q) 44,A
17,B 78,D
48,F
88,H50,E32,C
63,G
15,Q
Insertion AVL Tree
38Master of Computer Engineering Chiangmai University
Insert (50,Z) 44,A
17,B 78,D
48,F
88,H32,C
63,G
15,Q 50,E50,Z
Searching AVL Tree
39Master of Computer Engineering Chiangmai University
Search (48) 44,A
17,B 78,D
48,F
88,H50,Z32,C
63,G
15,Q
Return Value = F
Searching AVL Tree
40Master of Computer Engineering Chiangmai University
Search (60) 44,A
17,B 78,D
48,F
88,H50,Z32,C
63,G
15,Q
Return Value = null
Deletion AVL Tree
41Master of Computer Engineering Chiangmai University
Delete (63) 44,A
17,B 78,D
48,F
88,H50,Z32,C
63,G
15,Q
Implement AVL Tree Algorithm
42Master of Computer Engineering Chiangmai University
Insertion Algorithm
Algorithm Insert ( data ,v )
Input : Insert a data ( key-value pair ) in T at position v
Output : New node(data) or replace data at key.data = key(v)
if T.isExternal (v)
T.put(data,v)
return null
if key.data = key(v)
old value = T.getvalue()
T.put(data,v)
return (old value)
Implement AVL Tree Algorithm
43Master of Computer Engineering Chiangmai University
Else if key.data < key(v)
return Insert (k, T.left(v))
else if k.data > key(v)
return Insert (k, T.right(v))
if T is balancce
Return ()
else
Rebalancing Tree
Implement AVL Tree Algorithm
44Master of Computer Engineering Chiangmai University
Searching Algorithm
Algorithm Search ( k ,v )
Input : A key k and position v
Output : Return value of key in position v, else return null
if T.isExternal (v)
return null
if k < key(v)
return remove(k, T.left(v))
else if k = key(v)
return T.getValue(v)
else { k > key(v) }
return Search(k, T.right(v))
Implement AVL Tree Algorithm
45Master of Computer Engineering Chiangmai University
Deletion Algorithm
Algorithm delete ( k )
Input : searching by inorder traversal with key k
Output : return value (remove) at position v
if T.isExternal (v)
return null
if k < key(v)
delete (k , T.Left(v))
else if k > key (v)
delete (k , T.Right(v))
{
else
if T.Left(v) = null and T.Right(v) = null
Implement AVL Tree Algorithm
46Master of Computer Engineering Chiangmai University
value = T.getValue
T. Remove(k,v)
T.put (null,v)
Return (value)
else if T.Left(v) = null
temp = node(v)
node(v) = T.Right(v)
Remove temp
else if T.Right(v) = null
temp = node(v)
node(v) = T.Left(v)
Remove temp
Implement AVL Tree Algorithm
47Master of Computer Engineering Chiangmai University
else
tempnode = findmin(T.Right(v))
node.data = tempnode.data
delete(node.data,T.Right(v))
}
if T is balancce
Return ()
else
Rebalancing Tree
Performance & Complexity
48Master of Computer Engineering Chiangmai University
Structure Insertion Searching DeletionDoubly link-List O(1) O(n) O(n)
Hash Table O(1) O(1) O(1)Trie O(dm) O(dm) O(dm)
AVL Tree O(log n) O(log n) O(log n)
* d = size of alphabet m = size of the string parameter of the operation
Thank you
49Master of Computer Engineering Chiangmai University
top related