300 likes | 512 Views
CS1020 Data Structures and Algorithms I Lecture Note # 12. Mix and Match Combining data structures to solve problems. Objective. Using a combination of data structures to solve problems. . 1 Data Structures with Multiple Organisations. 1.1 Combining Data Structures.
E N D
CS1020 Data Structures and Algorithms ILecture Note #12 Mix and Match Combining data structures to solve problems
Objective Using a combination of data structures to solve problems. [CS1020 Lecture 12 AY2013/4 S2]
1.1 Combining Data Structures • We have learned a number of data structures in this module: • Arrays • Linked Lists • Stacks • Queues • Hash tables • For some applications, sometimes we combine some of these data structures to solve the problems [CS1020 Lecture 12 AY2013/4 S2]
1.2 Introducing Graphs (1/2) d c b a f e d c b a f e An undirected graph of 6 vertices and 7 edges. A directed graph of 6 vertices and 7 edges. • In computer science, a graph is a data structure with: • V: A set of vertices (or nodes) • E: A set of edges (x, y), where x and y V. • Edges may be directed or undirected. A graph with directed (undirected) edges is called a directed (undirected) graph. • Sometimes an edge may be associated with a value. [CS1020 Lecture 12 AY2013/4 S2]
1.2 Introducing Graphs (2/2) Module a has prerequisite modules b, c and d. a b c d e g h f i j • A Directed Acyclic Graph (DAG) is a directed graph with no cycle. • Example: Module prerequisite graph • Directed edge (x, y) means module x has prerequisite module y [CS1020 Lecture 12 AY2013/4 S2]
1.3 Array of Linked Lists (1/2) k1,data 0 k2,data k4,data k3,data m-1 • An array of linked lists is a useful data structure where each element of the array references a linked list of objects • Applications: • Hash table with separate chaining • Lecture #11 slide 37: Use a linked list to store the collided keys [CS1020 Lecture 12 AY2013/4 S2]
1.3 Array of Linked Lists (2/2) Module prerequisite graph Sorted array of modules g a b b c d e g j i h f j f d c h e e f i h j i f g j i h a d c b e • Notes: • Modules are sorted and stored in an array • Find = O(log n) because of sorted array of modules • PrintInOrder = O(n) • Print all modules in order. • Applications (continued): • Adjacent list for representing graph [CS1020 Lecture 12 AY2013/4 S2]
1.4 Hash Table + Linked Lists (1/2) Module prerequisite graph Hash table g b b d c d a h e i f e c a b c d e h f i j g • Notes: • Find = O(1) • PrintInOrder = O(n log n) • Modules in hash table are not in order, • need to sort them before printing : Use a hash function h(m) to hash a module m into the hash table and use a linked list to store its pre-requisite modules. [CS1020 Lecture 12 AY2013/4 S2]
1.4 Hash Table + Linked Lists (2/2) Module prerequisite graph Head of module linked list Hash table a g b d c d b h e i f e c a b c d e h f i j g • Notes: • Find = O(1) • PrintInOrder = O(n) • Use the sorted linked list • to print all modules. : Augment the previous arrangement with a sorted linked list of modules in hash table. [CS1020 Lecture 12 AY2013/4 S2]
1.5 1:1 Mapping (1/3) • Very often, we need to create a 1:1 mapping of objects to their unique identifiers. The identifiers are usually chosen to be integers owing to their ease of use, especially as indices to an array. • Example: Flight information with departure cities and arrival cities. • To facilitate searching, each city’s name is associated with a unique identifier (an integer). • The vertices in the graph that represents the flight information now contain the city identifiers instead of city names. • As a result, we need a table lookup between a city name and its identifier. [CS1020 Lecture 12 AY2013/4 S2]
1.5 1:1 Mapping (2/3) 0 • Let n be the number of cities • Find city identifier given city name: O(log n) • Find city name given city identifier: O(1) • Add/delete a city: O(n) • Note: On average, we need to change half of the city identifiers. Also, for the flight information application, we need to change half of the nodes and edges of the graph that represents the flight information. • The worst case for the changes is O(n2), which is very bad! Why O(n2)? Beijing 1 British Columbia 2 : : Chicago 12 13 London Jakarta Singapore Sydney 14 Osaka : : 20 21 : : • Method 1: • Sort the city names and store them in an array. • Index of the array is the city identifier [CS1020 Lecture 12 AY2013/4 S2]
1.5 1:1 Mapping (3/3) Hash table: Beijing 5, Osaka 0 • Let n be the number of cities • Find city identifier given city name: O(1) • Find city name given city identifier: O(1) • Add/delete a city: O(1) • Why? British Columbia 1 4, Chicago : 2 Chicago 3 : 4 5 Singapore Sydney 1, British Columbia 0, Singapore Jakarta 7, Sydney 2, London 3, Jakarta London 6, Beijing Osaka 6 7 : : : • Method 2: • Store city names (unsorted) in an array; index of the array is the city identifier • Add a hash table: Hash the city name to the city identifier [CS1020 Lecture 12 AY2013/4 S2]
1.6 More Examples • Suppose we need an ADT that supports the following operations • enqueue(item) i.e. offer(item) • dequeue() i.e. poll() • peek() • printInOrder() [CS1020 Lecture 12 AY2013/4 S2]
1.6.1 Use a Queue • If we use a queue, we can support the queue operations efficiently in O(1). • But to print the items in order, we need to first sort the items in the queue, which is O(N log N) time (assuming N items in the queue). [CS1020 Lecture 12 AY2013/4 S2]
1.6.2 Use a Sorted Linked List • We can reduce printInOrder() to O(N) using a sorted linked list instead. • But the queue operations are not supported. [CS1020 Lecture 12 AY2013/4 S2]
8 3 3 5 9 8 5 9 1.6.3 Use both Queue + Sorted List? List head Sorted List Queue back front Trivial problem: Need to duplicate the data. [CS1020 Lecture 12 AY2013/4 S2]
1.6.3 Enqueue(6) 5 3 8 9 Sorted List 6 6 3 3 8 8 9 9 5 5 Queue [CS1020 Lecture 12 AY2013/4 S2]
1.6.3 Dequeue() 5 6 3 8 9 Sorted List 6 3 3 8 8 9 9 5 Queue [CS1020 Lecture 12 AY2013/4 S2]
1.6.3 Using both Queue + Sorted List Q:Can we improve them? But then enqueue and dequeue would now take linear time O(N), because we have to look for the position of the item in the linked list to insert/delete.Tooslow. [CS1020 Lecture 12 AY2013/4 S2]
1.6.4 Improvement: Queue + DLinked List List Head 3 8 9 5 Queue front back Queue Pointer Succ/Pred Pointer • Only store one copy of each item. • Each node has 2 sets of pointers: • One for queue and other for a doubly linked list [CS1020 Lecture 12 AY2013/4 S2]
1.6.4 Using both Queue + DLinked List Q:Can we improve the complexity of enqueue O(N)? A: Yes, by using a Binary Search Tree (BST) but this is not in the CS1020 syllabus. Enqueuebecomes O(log N). Dequeue of a doubly linked list can be done in O(1) time. Q: How? However, enqueue is still O(N). Why? E.g.: enqueue 4. [CS1020 Lecture 12 AY2013/4 S2]
2 Final Exam (1/2) • Date: 5 May 2014, Monday • Time: 5 – 7pm • Venue: To be announced by Registrar’s Office • Weightage: 45% • Scope: Entire semester with emphasis on the materials from Linked List onwards • All materials are tested, including • Lecture notes • Tutorials • Take-home and sit-in labs [CS1020 Lecture 12 AY2013/4 S2]
2 Final Exam (2/2) Closed book. You are allowed to bring in one A4 handwritten reference sheet. Read through all questions first before answering them. Refer to CS1020 website, “Exams” page: http://www.comp.nus.edu.sg/~cs1020/3_ca/exams.html [CS1020 Lecture 12 AY2013/4 S2]
2 CS1020 Objectives (1/2) Give an introduction to OO Programming (OOP) Model using Java programming language, linear data structures, and algorithms for constructing efficient programs. Emphasize on data abstraction issues (through ADTs) in the code development. Emphasize on efficient implementations of chosen data structures and algorithms. [CS1020 Lecture 12 AY2013/4 S2]
2 CS1020 Objectives (2/2) Linear data structures include arrays, lists, stacks, queues, and hash tables; together with their algorithms (insert, delete, find, and updates). Simple algorithmic paradigms, such as sorting and search algorithms, and divide-and-conquer algorithms were introduced. Recursion and elementary analysis of algorithms were taught. [CS1020 Lecture 12 AY2013/4 S2]
3 Announcements • Our consultations hours are given on the IVLE announcement • Please check out the IVLE as we will still be making announcements • We will post your CA marks for your checking • Please check your CA marks and report any discrepancy before the stated deadline [CS1020 Lecture 12 AY2013/4 S2]