Foundations of Data Structures Practical Session 4 Recurrence

  • Slides: 22
Download presentation
Foundations of Data Structures Practical Session #4 Recurrence ADT 08/04/2013 Amihai Savir & Ilya

Foundations of Data Structures Practical Session #4 Recurrence ADT 08/04/2013 Amihai Savir & Ilya Mirsky - 2013

Recurrence solution methods 2

Recurrence solution methods 2

The master method 3

The master method 3

Master method example 4

Master method example 4

Example cont’d 5

Example cont’d 5

Example cont’d 6

Example cont’d 6

ADT From Wikipedia: • In computer science, an abstract data type (ADT) is a

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. 7

Vector (Array) 8

Vector (Array) 8

Queue FIFO - First In First Out ADT that supports the following operations: •

Queue FIFO - First In First Out ADT 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() eek – (optional) returns an element from the head of the queue, without removing it. • is. Empty() – returns true if the queue is empty, 9

Stack LIFO - Last In First Out ADT that supports the following operations: •

Stack LIFO - Last In First Out ADT 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. • is. Empty – returns true if the stack is empty. 10

Linked List • A data structure that supports linear access to its elements. •

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. 11

Question 1 Suggest 2 ways to implement a Queue using 2 Stacks. Solution •

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(), is. Empty() 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 is. Empty(). 12

Question 1 cont’d enqueue (x) { move. Elements(A, B) A. push(x) move. Elements(B, A)

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

Question 1 cont’d Another solution enqueue (x) { move. Elements(A, B) A. push(x) }

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

Question 2* Suggest a way to implement a Stack using Linked List. Solution We

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. 15

Question 2 cont’d pop() { element ← L. item. At(0) L. remove. Item. At(0)

Question 2 cont’d pop() { element ← L. item. At(0) L. remove. Item. At(0) return element } push(x) { L. add. To. Head(x) } top() { element ← L. item. At(0) return element } is. Empty() if L. size()=0 then return true else return false } 16

Question 3* Operation init(n) is. Element(k) insert(x, k) remove(k) is. Empty() has. All ()

Question 3* Operation init(n) is. Element(k) insert(x, k) remove(k) is. Empty() has. All () Time O(n) O(1) O(1) 17

Question 3 cont’d • Insert (x, k) { A[k] = x count++ } remove

Question 3 cont’d • Insert (x, k) { A[k] = x count++ } remove (k) { A[k] = null count-} is. Empty () { return (count == 0) } has. All () { return (count == n) } 18

Question 4 init(n) flip(i, j) has. Row. Of 1() has. Row. Of 0() Operation

Question 4 init(n) flip(i, j) has. Row. Of 1() has. Row. Of 0() Operation Initialize A with the value 1 Flip the value in A[i, j], i. e. , A[i, j] = !A[i, j] Return true iff A has a row that contains only 1 -s Return true iff A has a row that contains only 0 -s Time O(n 2) O(1) 19

Question 4 cont’d • flip(i, j) if (A[i][j] == 0) { if (Sum[i] ==

Question 4 cont’d • flip(i, j) if (A[i][j] == 0) { if (Sum[i] == 0) count 0 - Sum[i]++ if (Sum[i] == n) count 1++ A[i][j] = 1 } else { if (Sum[i] == n) count 1 - Sum[i]-if (Sum[i] == 0) count 0++ A[i][j] = 0 } 20

Question 5 Operation Time add(k, is. Special) O(n) remove. Min() remove. Max() O(1) get.

Question 5 Operation Time add(k, is. Special) O(n) remove. Min() remove. Max() O(1) get. Oldest() Returns the oldest special number, according to insertion order O(1) 21

Question 5 cont’d 22

Question 5 cont’d 22