1 / 22

Foundations of Data Structures

Foundations of Data Structures. Practical Session #4 Recurrence ADT. Recurrence solution methods. Substitution method Guess the form of the solution and prove it by induction. Iteration method Convert the recurrence into a summation and solve it Master method

magda
Download Presentation

Foundations of Data Structures

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. Foundations of Data Structures Practical Session #4 Recurrence ADT Amihai Savir & Ilya Mirsky - 2013

  2. Recurrence solution methods • Substitution method • Guess the form of the solution and prove it by induction. • Iteration method • Convert the recurrence into a summation and solve it • Mastermethod • A “cookbook” method for solving recurrences of the form • Which describe an algorithm that divides a problem of size into sub-problems, each of size and solved in time . • The cost of dividing the problem and combining the results of the sub-problems is described by the function .

  3. The master method • If • If • If and • Intuition: in each of the 3 cases we compare to . The former describe the cost of each step of the recurrence, the latter describe the cost of solving sub-problems, each of size . • The greater of the two functions dominate the running time.

  4. Master method example • Use the Master method to solve the following recurrences:

  5. Example cont’d • b) • and for

  6. Example cont’d • Not all recurrences that match the “right form” are solvable using the master method. • For instance, • Where does it fail? • It seems that case 3 might apply, since isasymptotically larger than . • The problem is that it’s not polynomially larger. • The ratio is asymptotically smaller than for any .

  7. ADT • From Wikipedia: • In computer science, an abstract data type (ADT) is a mathematical model for a certain class of data structures that have similar behavior. • An abstract data type is defined indirectly, only by the operations that may be performed on it and by mathematical constraints on the effects (and possibly cost) of those operations.

  8. Vector (Array) • A linear sequence of elements that supports access to its elements by their indexes. • A vector ADT supports the following basic methods: • elementAt(i) – returns the element at index • replaceAt(i, element) – replaces and returns the element at index with the new given element. • insertAt(i, element) – inserts a new element at index • removeAt(i) – removes the element in index • size() – returns the size of the vector • isEmpty() - returns true if the vector is empty

  9. Queue • FIFO - First In First OutADT that supports the following operations: • enqueue(element) – inserts new element at the tail of the queue. • dequeue() – removes and returns an element from the head of the queue. • peek() – (optional) returns an element from the head of the queue, without removing it. • isEmpty() –returns true if the queue is empty,

  10. Stack • LIFO - Last In First OutADT that supports the following operations: • push(element) – adds an element to the head of the stack. • pop() – removes and returns an element from the head of the stack. • top() – (optional) returns the element from the head of the stack, without removing it. • isEmpty–returns true if the stack is empty.

  11. Linked List • A data structure that supports linear access to its elements. • Each element points to the next element in the list. • In a doubly-linked list: each element has 2 pointers, one to the next element in the list and the other to the previous element.

  12. Question 1 • Suggest 2 ways to implement a Queue using 2 Stacks. • Solution • Notice that the queue/stack is abstract, meaning the implementation isn't known, only the interface. • In order to implement a queue we need to implement the following methods:enqueue(x), dequeue(), isEmpty() • We will use stack A to hold the queue's elements and stack B as a temporary stack. We will use the Stack ADT operations: push(x), pop() and isEmpty().

  13. Question 1 cont’d enqueue(x) { moveElements(A, B) A.push(x) moveElements(B, A) } dequeue( ) { element = A.pop() return element } isEmpty ( ) { return A.isEmpty() } moveElements(X, Y) { while (! X.isEmpty()){ temp = X.pop() Y.push(temp) } } * In a very similar manner one can implement stack using 2 queues.

  14. Question 1 cont’d Another solution isEmpty ( ) { return (A.isEmpty() && B.isEmpty()) } enqueue(x) { moveElements(A, B) A.push(x) } dequeue( ) { moveElements(B,A) element = A.pop() return element }

  15. Question 2* • Suggest a way to implement a Stack using Linked List. • Solution • We will use an inner List L in order to hold the stack’s elements.

  16. Question 2 cont’d pop() { element ← L.itemAt(0) L.removeItemAt(0) return element } push(x) { L.addToHead(x) } top() { element ← L.itemAt(0) return element } isEmpty() if L.size()=0 then return true else return false }

  17. Question 3* • is a set of at most elements, where is given. • Each element has a unique key in the range (i.e., no 2 different elements can have the same key). • Find a data structure of size , that supports the following operations in the given times:

  18. Question 3 cont’d We will use: • Array A of size that will hold pointers to elements. • A counter, , of number of occupied indexes in A. init(n) { for (i=0 ; i<n ; i++) A[i] = null count = 0 } isElement (k) { return (A[k] != null) } Insert (x, k) { A[k] = x count++ } remove (k) { A[k] = null count-- } isEmpty() { return (count == 0) } hasAll() { return (count == n) }

  19. Question 4 • A is a Boolean (0 or 1 values) matrix of size .Propose a data structure that supports the following operations in the given times: • We will use the following data structures: • A[n][n] – the matrix • Sum[n] – An array containing sums of the rows in A. Sum[i] holds the sum of the ’s row in A. • count1 = how many cells in Sum contain n • count0 = how many cells in Sum contain 0

  20. Question 4 cont’d init() { count1 = n count0 = 0 } hasRowOf1() { return count1 > 0 } hasRowOf0() { return count0 > 0 } flip(i,j) if (A[i][j] == 0) { if (Sum[i] == 0) count0- - Sum[i]++ if (Sum[i] == n) count1++ A[i][j] = 1 } else { if (Sum[i] == n) count1- - Sum[i]-- if (Sum[i] == 0) count0++ A[i][j] = 0 }

  21. Question 5 • Propose a data structure that supports the following operations in the given times on a set of different numbers, of which are “special” (. • Space complexity should be

  22. Question 5 cont’d • We will use the following data structures: • Doubly-Linked list that will store all the numbers in a sorted manner. • Queue that will store just the “special” numbers (in a LIFO order). • Each entry in the list points to its copy in the queue. • Description of the operations • add(k) – insert to the list in the right place according to its value, if it is “special” insert to the queue as well. • removeMin() – remove the first item from the list, if it is “special” then remove it from the queue as well (reachable through the pointer). (removeMax() is similar). • getOldest() – return (without removing) the “oldest” item in the queue.

More Related