data structures lectures: haim kaplan and uri zwick teaching assistants: yaron orenstein and yahav...

41
Data Structures Lectures: Haim Kaplan and Uri Zwick Teaching Assistants: Yaron Orenstein and Yahav Nussbaum http://moodle.tau.ac.il/course / view.php?id =368215899 Websit e: Exam: 80% Theoretical Assingnments: 10% Practical Assignments: 10% Cormen, Leiserson, Rivest and Stein Introduction to Algorithms (Second/Third Editions)

Upload: posy-hudson

Post on 02-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Data StructuresLectures: Haim Kaplan and Uri Zwick

Teaching Assistants:Yaron Orenstein and Yahav Nussbaum

http://moodle.tau.ac.il/course/view.php?id=368215899Website:

Exam: 80%Theoretical Assingnments: 10%

Practical Assignments: 10%

Cormen, Leiserson, Rivest and Stein Introduction to Algorithms (Second/Third Editions)

Data Structures

Lists Search Trees

Heaps/Priority QueuesHashing

Union-FindTries and Suffix Trees

“In computer science, a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.”

Sorting and Selection

Data Structures

Haim Kaplan and Uri ZwickOctober 2012

Lecture 1Abstract Data Types

Lists, Stacks, Queues, DequesArrays, Linked ListsAmortized Analysis

Lists (Sequences)

Insert b at position i

Delete the item at position i

[a0 a1 a2 a3 … ai-1 ai ai+1 … an-2 an-1 ]b

Retrieve the item at position i

When items are inserted or deleted,the indices of some other items change

5

Abstract Data Type (ADT)Lists (Sequences)

List() – Create an empty listLength(L) – Return the length of list L

Retrieve(L,i) – Return the i-th item of LInsert(L,i,b) – Insert b as the i-th item of L

Delete(L,i) – Delete and return the i-th item of L

( Search(L,b) – Return the position of b in L, or −1 )

Interesting special cases:

Retrieve-First(L), Insert-First(L,b), Delete-First(L)Retrieve-Last(L), Insert-Last(L,b), Delete-Last(L)

Concat(L1, L2) – Concatenate L1 and L2

Abstract Data Types (ADT)Stacks, Queues, Dequeues

Stacks – Implement only:

Insert-Last(L,b), Retrieve-Last(L), Delete-Last(L)Also known as: Push(L,b), Top(L), Pop(L)

Last In First Out (LIFO)

Queues – Implement only:

Insert-Last(L,b), Retrieve-First(L), Delete-First(L)First In First Out (FIFO)

Deques (double ended queues) – Implement only:

Insert-First(L,b), Retrieve-First(L), Delete-First(L)Insert-Last(L,b), Retrieve-Last(L), Delete-Last(L)

Implementing lists using arrays

L

arraymaxlenlength

a0 a1 … an−1

nM

M

n

* * *

We need to know the maximal length M in advance.

Retrieve(L,i) (of any element) takes O(1) time

Insert-Last(L,b) and Delete-Last(L) take O(1) time

Stack operations in O(1) time

Implementing lists using arrays

L

arraymaxlenlength

a0 a1 … an−1

nM

M

n

* * *

We need to know the maximal length M in advance.

Retrieve(L,i) (of any element) takes O(1) time

Insert-Last(L,b) and Delete-Last(L) take O(1) time

Insert(L,i,b) and Delete(L,i) take O(n−i+1) time

Implementing lists using arraysL

arraymaxlenlength n

M

M

nn−10 1

n+1

Delete-Last very similar

Implementing lists using arrays

L

arraymaxlenlength n

M

M

nn−10 1

n+1

i

We need to move n−i items, then insert. O(n−i+1) time.

Implementing lists using circular arrays

Implement queue and deque operations in O(1) time

L

arraymaxlenlength n

M

M

n

start 2

New field: start

0 1 2

Implementing lists using circular arrays

Implement queue and deque operations in O(1) time

L

arraymaxlenlength

M−4

M

Mstart7

Occupied region can wrap around!

M−40 1 M−12

Implementing lists using circular arrays

Implement queue and deque operations in O(1) time

L

arraymaxlenlength

0

M

M

n

startn−1

n−10

1

1

n

Code of other operations similar

14

Arrays vs. Circular Arrays

Arrays Circular ArraysInsert/Delete

Last O(1) O(1)

Insert/Delete

First O(n+1) O(1)

Insert/Delete(i) O(n−i+1) O(min{i+1,n−i+1})

Retrieve(i) O(1) O(1)

Main advantage: Constant access time

Main disadvantage: Inserting or deleting elements ‘in the middle’ is expensive

Implementing lists using singly linked lists

Lists of unbounded lengthSupport some additional operations

length nlast

L

first

a0 a1 an-1 a2 …

List object

List-Node object

item next

item

Insert-First with singly linked lists

L

firstlast

length

n

a0 a1 an-1 a2 …next

Insert-First(L,b)Generate a new List-Node

object B containing b

Increment L.length

b(Return B)

B

Add B to the list

n+1 Adjust L.last, if necessary

item

Insert-First with singly linked lists

L

firstlast

length

n

a0 a1 an-1 a2 …next

b

Bn+1

Insert-Last and Delete-First – very similar, also O(1) time

Unfortunately, Delete-Last requires O(n+1) time

item

Retrieve with singly linked lists

L

firstlast

length n

a0 a1 an-1 a2 …next

Retrieving the i-th item takes O(i+1) time

item

Insert with singly linked lists

L

firstlast

length n

a0 a1 an-1 a2 …next

Inserting an item into position i takes O(i+1) time

Inserting a node(After a given node)

Insert-After(A,B) – Insert B after A

ai−1 ai …

A…

b B

Assignments’ orderis important

Deleting a node(After a given node, not the last one)

ai−1 ai …

A

Delete-After(A) – Delete the node following A

ai+1

What happens to the node removed?

Concatenating listsConcat(L1,L2) – Attach L2 to the end of L1

L1

n a0 a1 an-1 a2 …

m b0 b1 bm-1 b2 …

L2

n+m

(What happened to L2?)

O(1) time!

23

Circular arrays vs. Linked Lists

Circular arrays Linked lists

Insert/Delete-FirstInsert-Last O(1) O(1)

Delete-Last O(1) O(n)

Insert/Delete(i) O(min{i+1,n−i+1}) O(i+1)

Retrieve(i) O(1) O(i+1)

Concat O(min{n1,n2}+1) O(1)

In linked lists we can insert or deleteelements ‘in the middle’ is O(1) time

24

More fun with linked lists

Circular singly linked lists

(Circular) Doubly linked lists

Doubly linked lists with a sentinel

item

Circular singly linked lists

L

firstlast

length n

a0 a1 an-1 a2 …next

If we make the list circular,we don’t need first

All capabilitiesremain the same

26

How do we implementDelete-Last(L) in O(1) time?

Doubly linked lists!

(Circular) Doubly linked lists

All previous benefits + Delete-Last(L) in O(1) time

a0 an-1 …a1 a2

L

first

nextitemprev

Each List-Node now has a prev field

lengthn

Inserting a nodeinto a Doubly Linked List

Insert-After(A,B) – Insert node B after node A

ai+1 ai

A

b

B

Deleting a nodefrom a Doubly Linked List

Delete-Node(A) – Delete node A from its list

ai−1 …ai+1

Each node now has a prev field

ai

A

Note: A itself not changed! Is that good?

Circular Doubly linked listswith a sentinel

L

sentinel

a2 a1 a0 an-1

No special treatment of first and last elements

Each node has a successor and a predecessor

No special treatment of empty lists

In some case we can identify a list with its sentinel

Empty list

L

Circular Doubly linked listswith a sentinel

L

a3 a2 a1 an-1

Circular Doubly linked listswith a sentinel

33

Abstraction barriers

ListInsert, Retrieve, Delete

Search

User

List-NodeRetrieve-Node

Insert-After, Delete-After, Delete-Node

34

With the current interfacewe need to do:

Can we do?

O(n)O(n) O(1)

Suppose we inserted a into L.After sometime, we want to delete a from L

Modified ADT for lists

The current specification does not allow us to utilize one of the main capabilities of linked lists:

Insert-After, Delete-Node, Nextin O(1) time

We next define a new ADT in which the user is allowed to call

List-Node, Insert-After, Delete-Node, Next

Lists – A modified abstraction

[ a0 a1 … ai … an-1 ]A

List-Node(b) – Create a List-Node containing item bItem(B) – Return the item contained in List-Node B

Insert(L,i,B) – Insert B as the i-th List-Node of LRetrieve(L,i) – Return the i-th List-Node of L

Delete(L,i) – Delete and return the i-th List-Node of L

Concat(L1, L2) – Concatenate L1 and L2

B

Lists are now composed of List-Nodes

List() – Create an empty listLength(L) – Return the length of list L

Next(A) – Return the List-Node following A (assuming there is one)

Insert-After(A,B) – Insert B after A

Delete-Node(A) – Delete A from its current list

Note: L is not an argument of these operations!

Lists – A modified abstraction

[ a0 a1 … ai … an-1 ]A B

We now allow the following additional operations:

These operations assume that A is contained insome list, while B is not contained in any list

The actual implementation using linked listsremains essentially the same

The user explicitly manipulates List-Nodes

Lists – A modified abstraction

The length field is removed from the implementationas it is hard to keep it up to date

Due to concatenations, hard to keep track of which list contains a given List-Node

Pitfalls of the modified ADTL1

a2 a1 a0 an-1

L2

b2 b1 b0 bm-1

A

B

Insert-After(A,B) L2 is not a valid list now

Should call Delete(B) before Insert-After(A,B)

Which-List?

Concatenations move List-Nodes for lists to lists

Which-List(A) – return the list currently containing A

Naïve implementation: scan the list from A until getting to the sentinel

Much more efficient implementation possible using a Union-Find data structure.

41

Implementation of lists

Circular arrays

DoublyLinked

lists

Balanced Trees

Insert/Delete-FirstInsert/Delete-Last O(1) O(1) O(log n)

Insert/Delete(i) O(i+1) O(i+1) O(log n)

Retrieve(i) O(1) O(i+1) O(log n)

Concat O(n+1) O(1) O(log n)