1 / 49

CPE261702 ALGORITHM ANALYSIS & DESIGN PRESENTATION TOPIC IN MAP

CPE261702 ALGORITHM ANALYSIS & DESIGN PRESENTATION TOPIC IN MAP. 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

rachel
Download Presentation

CPE261702 ALGORITHM ANALYSIS & DESIGN PRESENTATION TOPIC IN MAP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CPE261702 ALGORITHM ANALYSIS & DESIGN PRESENTATION TOPIC IN MAP Master of Computer Engineering Chiangmai University

  2. 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 Master of Computer Engineering Chiangmai University

  3. Basic Array index 0 1 2 3 4 Array salary We can build to platform ( php code ) $salary[i] = ? EX. $salary[1] = $salary[3] = 11000 20000 But we can’t know. Who’s salary ? Master of Computer Engineering Chiangmai University

  4. Associative Array We can replace index by name. name Sara Bob Smith John Steve Array salary We can build to platform ( php code ) $salary[‘name’] = ? EX. $salary[‘Smith’] = 15000 Master of Computer Engineering Chiangmai University

  5. 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. Master of Computer Engineering Chiangmai University

  6. 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). Master of Computer Engineering Chiangmai University

  7. Example Master of Computer Engineering Chiangmai University

  8. Example (Cont.) Master of Computer Engineering Chiangmai University

  9. 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 ). put ( Sara , 11000 ) nodes/positions header trailer entries Sara 11000 Master of Computer Engineering Chiangmai University

  10. Simple List-Based Map put ( Bob , 12000 ) nodes/positions header trailer entries Sara 11000 Bob 12000 Master of Computer Engineering Chiangmai University

  11. Simple List-Based Map put ( Smith , 15000 ) nodes/positions header trailer entries Sara 11000 Smith 15000 Bob 12000 Master of Computer Engineering Chiangmai University

  12. Simple List-Based Map put ( Steve , 18000 ) nodes/positions header trailer entries Sara 11000 Smith 15000 Steve 18000 Bob 12000 Master of Computer Engineering Chiangmai University

  13. Simple List-Based Map put ( Smith , 20000 ) nodes/positions header trailer 15000 20000 entries Sara 11000 Smith Return value = 15000 Steve 18000 Bob 12000 Master of Computer Engineering Chiangmai University

  14. Simple List-Based Map get ( Steve ) nodes/positions header trailer entries Sara 11000 Smith 20000 Return value = 18000 Steve 18000 Bob 12000 Master of Computer Engineering Chiangmai University

  15. Simple List-Based Map get ( John ) nodes/positions header trailer entries Sara 11000 Smith 20000 Return value = null Steve 18000 Bob 12000 Master of Computer Engineering Chiangmai University

  16. Simple List-Based Map remove ( Bob ) nodes/positions header trailer entries Sara 11000 Smith 20000 Return value = 12000 Steve 18000 Bob 12000 Master of Computer Engineering Chiangmai University

  17. Simple List-Based Map remove ( Stephen ) nodes/positions header trailer entries Sara 11000 Steve 18000 Return value = null Smith 20000 Master of Computer Engineering Chiangmai University

  18. 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 ifp.element () .getkey () = kthen t p.element () .getValue () B.set ( p, ( k , v )) returnt S.addLast (( k , v )) n  n + 1 return null Master of Computer Engineering Chiangmai University

  19. 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 ifp.element () .getkey () = kthen returnp.element () .getvalue () return null Algorithm 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 ifp.element () .getkey () = kthen t p.element () .getValue () S.remove ( p ) n  n - 1 returnt return null Master of Computer Engineering Chiangmai University

  20. 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 Master of Computer Engineering Chiangmai University

  21. Hash Table Architecture Hash Function String Hash Table Addr. Key Data Hash Code Compression Function Master of Computer Engineering Chiangmai University

  22. Example Implement A small phone book as a hash table. Figure Reference : http://th.wikipedia.org/ Master of Computer Engineering Chiangmai University

  23. Hash Table Algorithm Map Methods with Separate Chaining used for Collisions. Delegate operations to a list-based map at each cell. 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 Master of Computer Engineering Chiangmai University

  24. Hash Table Algorithm 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 Master of Computer Engineering Chiangmai University

  25. 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. Master of Computer Engineering Chiangmai University

  26. 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. Master of Computer Engineering Chiangmai University

  27. 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. Master of Computer Engineering Chiangmai University

  28. Example • Tries Figure Reference : http://en.wikipedia.org Master of Computer Engineering Chiangmai University

  29. Trie Algorithm Pseudo-code for Insert: Master of Computer Engineering Chiangmai University

  30. Trie Algorithm Trie searching method used same way as search tree structure. In this case, we use preorder traversal. AlgorithmpreOrder(v) visit(v) foreachchild w of v preorder (w) Master of Computer Engineering Chiangmai University

  31. 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. Master of Computer Engineering Chiangmai University

  32. 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. Master of Computer Engineering Chiangmai University

  33. 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. Master of Computer Engineering Chiangmai University

  34. 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. Master of Computer Engineering Chiangmai University

  35. AVL Tree Architecture Master of Computer Engineering Chiangmai University

  36. Example Implement • AVL Tree 44,A 78,D 17,B 88,H 32,C 50,E 48,F 63,G Master of Computer Engineering Chiangmai University

  37. Insertion AVL Tree • Insert (15,Q) 44,A 78,D 17,B 88,H 15,Q 32,C 50,E 48,F 63,G Master of Computer Engineering Chiangmai University

  38. Insertion AVL Tree • Insert (50,Z) 44,A 78,D 17,B 50,Z 88,H 15,Q 32,C 50,E 48,F 63,G Master of Computer Engineering Chiangmai University

  39. Searching AVL Tree • Search (48) 44,A 78,D 17,B 50,Z 88,H 15,Q 32,C 48,F 63,G Return Value = F Master of Computer Engineering Chiangmai University

  40. Searching AVL Tree • Search (60) 44,A 78,D 17,B 50,Z 88,H 15,Q 32,C 48,F 63,G Return Value = null Master of Computer Engineering Chiangmai University

  41. Deletion AVL Tree • Delete (63) 44,A 78,D 17,B 50,Z 88,H 15,Q 32,C 48,F 63,G Master of Computer Engineering Chiangmai University

  42. Implement AVL Tree Algorithm • Insertion Algorithm AlgorithmInsert ( 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) ifT.isExternal (v) T.put(data,v) return null if key.data = key(v) old value = T.getvalue() • T.put(data,v) • return(old value) Master of Computer Engineering Chiangmai University

  43. Implement AVL Tree Algorithm Else if key.data< key(v) returnInsert (k,T.left(v)) elseif k.data>key(v) returnInsert (k,T.right(v)) • ifT is balancce • Return () • else • Rebalancing Tree Master of Computer Engineering Chiangmai University

  44. Implement AVL Tree Algorithm • Searching Algorithm AlgorithmSearch ( k ,v ) Input : A key k and position v Output : Return value of key in position v, else return null ifT.isExternal(v) returnnull if k<key(v) returnremove(k,T.left(v)) else ifk=key(v) returnT.getValue(v) else{ k>key(v) } returnSearch(k,T.right(v)) Master of Computer Engineering Chiangmai University

  45. Implement AVL Tree Algorithm • Deletion Algorithm Algorithmdelete ( k ) Input : searching by inorder traversal with key k Output : return value (remove) at position v ifT.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 Master of Computer Engineering Chiangmai University

  46. Implement AVL Tree Algorithm 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 ifT.Right(v) = null temp = node(v) node(v) = T.Left(v) Remove temp Master of Computer Engineering Chiangmai University

  47. Implement AVL Tree Algorithm else tempnode = findmin(T.Right(v)) node.data = tempnode.data delete(node.data,T.Right(v)) } ifT is balancce Return () else Rebalancing Tree Master of Computer Engineering Chiangmai University

  48. Performance & Complexity * d = size of alphabet m = size of the string parameter of the operation Master of Computer Engineering Chiangmai University

  49. Thank you Master of Computer Engineering Chiangmai University

More Related