120 likes | 136 Views
This lecture note explores the combination of basic data structures like arrays, linked lists, and trees to implement different data structures tailored for specific applications. Topics include array of linked lists, adjacency list for representing graphs, hash tables with separate chaining, and adjacency matrix for directed graphs. Learn how to efficiently search and delete elements using these mixed data structures.
E N D
CS1020 Data Structures and Algorithms ILecture Note #17 Mix-and-Match Data Structures with Multiple Organisation
Basic Data Structures We cancombine them to implement different data structures for different applications. Arrays Linked Lists Trees (to be covered in CS2010) [CS1020 Lecture 17: Mix-and-Match]
Mix-and-Match 1 2 3 • Array of Linked Lists • E.g.: Adjacent list for representing graph • E.g.: Hash table with separate chaining [CS1020 Lecture 17: Mix-and-Match]
v2 v3 v1 v5 v4 G Adjacency List for Directed Graph 1 2 4 2 3 2 4 4 3 4 5 [CS1020 Lecture 17: Mix-and-Match]
v2 v3 v1 v5 v4 G Adjacency Matrix for Directed Graph Matrix[i][j] = 1 if (vi, vj)E 0 if (vi, vj)E [CS1020 Lecture 17: Mix-and-Match]
CS1102 AY2003 [CS1020 Lecture 17: Mix-and-Match]
CS1102 AY2003: Use Adjacency Matrix O(1) O(1) O(1) O(n) [CS1020 Lecture 17: Mix-and-Match]
CS1102 AY2003: Use Adjacency List 1 O(1) 2 3 O(n) 4 O(n) O(ni) [CS1020 Lecture 17: Mix-and-Match]
Problem Use hashing. (i, j) as key and the hash value returned by hash function to be index to a hash table where (i, j) is stored together with the reference to the node in the linked list. Searching on an unsorted linked list is O(n) How to improve it to O(1)? [CS1020 Lecture 17: Mix-and-Match]
Use Adjacency List 1 2 3 4 Is delete (i, j) O(1)? [CS1020 Lecture 17: Mix-and-Match]
CS1102 AY2003 Build an adjacency list of the graph, where the lists are doubly linked. Build a hash table with (i, j) as key, and a reference to the node representing (i, j) in the adjacency list as value. [CS1020 Lecture 17: Mix-and-Match]