1 welcome to data structures spring 2009 the slides in this course includes slides by t. tamir, a....

61
1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 6 5 4 2

Upload: gregory-payne

Post on 16-Jan-2016

245 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

1

Welcome to Data StructuresSpring 2009

The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses.

Thanks

null

7

65

42

Page 2: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

2

Welcome to Data StructuresSpring 2008

InstructorYael Moses ([email protected])

office hours: Thu. 10:30-11:30 or by appointment

TARan Eshel

Page 3: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

3

Textbook

Cormen, Leiserson, Rivest and Stein Introduction to Algorithms. (CLRS)

You will need this book also next year (for Algorithms) and maybe for additional elective courses.

A Hebrew translation exists (by Open univ.)

You will really need to read!!

Page 4: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

4

Student's Responsibilities:

During the semester you will need to submit:

6-7 theoretical assignments3 programming projects

Page 5: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

5

Final Grade

If final exam < 60 then

grade = fail

else grade = 0.2 *assignmnets +

0.8 * final exam.

Warning: DS is (considered) a difficult course.

Page 6: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

6

Today

What is data structure?What is it good for?Getting startedAn overview of the course

Page 7: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

7

Solving a problem

Input

manipulation

output

Page 8: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

8

input

algorithm

output

Page 10: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

10

Solving a problem

Data: information for analysis.

e.g., numbers, words, songs, movies

Algorithm: A well-defined procedure to solve a problem

Data structure: the way the data is organized

Page 11: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

11

The Sorting Problem

Input: A sequence of n numbers <a1,a2,..,an>

Output: A permutation (reordering) of the input sequence <b1,b2,..,bn> such that b1≤b2… ≤bn

Note: the sets {a1,a2,..,an}= {b1,b2,..,bn}

Example: input: <31,41,59,26,41,58>output: <26,31,41,41,58,59>

Page 12: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

12

Insertion Sort

Picking the elements 1 by 1 from the sequence

For each element insert it into correct place in the sequence of already sorted elements

Until last element inserted into place

Page 13: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

13

Insertion Sort

5 2 4 6 1 3

2 5 4 6 1 3

2 4 5 6 1 3

2 4 5 6 1 3

1 2 4 5 6 3

1 2 3 4 5 6

Page 14: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

14

What is missing?

How the data (numbers) are represented:Each number: binaryThe sequence of numbers: ?

The operations required in the chosen representation?

Picking the elements 1 by 1 from the sequence For each element insert it into correct place in

the sequence of already sorted elements Until last element inserted into place

Page 15: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

15

Insertion Sort (use array)

Insertion-Sort(A) for i 2 to length[A]

key A[i]j i-1while j>0 and A[j]>key

A[j+1] A[j]j j-1

end A[j+1] key

end

5 2 4 6 1 3

What is the worse case?

Page 16: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

16

Pseudocode

In the lectures I will be presenting algorithms in pseudocode.This is very common in the computer

science literaturePseudocode is usually easily translated to

real code.

Pseudocode should also be used for homework

Page 17: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

17

Pseudocode

The algorithms you design in homework will be read by a person, not a computer

The No Code Rule:Do not turn in Java or C code when asked

for pseudocodeExplain algorithm precisely, but without all

the details needed for a computer code

Page 18: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

18

Pseudocode example (good)

Insertion-Sort(A) for i 2 to length[A]

key A[i] j i-1 while j>0 and A[j]>key

A[j+1] A[j]j j-1

end A[j+1] key

end

Page 19: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

19

Java code

public static void insertionSort (int[] array){ for (int j = 1; j < array.length; j++) {

int key = array[j]; int k = j - 1;

while(k >= 0 && array[k] > key) { array[k+1] = array[k]; k--;

} array[k+1] = key;

} }

Page 20: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

20

What is a Data Structure?

The way the data is organized

What do we mean by “way”?The organization should allow an

efficient use of the data

Page 21: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

21

Abstract Data Type

An abstract mathematical definition of objects, with operations defined on them

Page 22: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

22

Elementary Data Structures

ArraysListsStacksQueues Trees

RF

12

3 4

5 67 8

In some languages these are basic data types – in others they need to be implemented

head

Page 24: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

24

A mapping from an index set, such as {0,1,2,…,n}, into a cell type

Objects: set of cells

Operations:create(A,n)put(A,v,i) or A[i] vvalue(A,i)

ADT: Array

Page 25: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

25

Simple Linked List

Group data together in a linear and flexible way.

Example:

A B A CR

Add an element

(Firsy in the list)

Page 26: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

26

Examples of Lists

List of numbersList of namesList of listsList of arraysRepresenting a sparse high order

polynomial

Page 27: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

27

Objects: Sets of elements (e.g., integers)Operations:Create a set Insert an elementRemove an elementCheck membershipMinimum Maximum

{5,1,2,2…,3}

ADT: Finite Dynamic Sets

Page 28: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

28

Data Structures

Representation of objects of an Abstract Data Type (ADT)

Algorithms manipulate the data structures to implement the operations of the ADT

ADT: An abstract mathematical definition of objects, with operations defined on them

Page 29: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

29

Objects: Finite sequences of nodesOperations:CreateOn a list

Get the first node On a node

Get dataGet next nodeAssign value to data Assign next node

ADT: Basic Linked List

A B A nil

Page 30: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

Examples of using linked list Operations

30

Page 31: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

31

Basic List Operations: in pseudo code

head[L] - the first node of L (in Java: L.head)

next[x] - the next node in L or NIL if x is the tail of L

(in Java: x.next)

data[x] – the node data

(in Java: x.data)

Page 32: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

32

More list operations

Low level:List-Search(L,k)List-head-Insert(L,x) Insert-after-key(L,k,x)List-tail-Insert(L,x)List-Delete(L,k)Higher level:Sort(L)Reverse(L) Can be implemented using the

basic list operations

Page 33: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

33

Searching

List-Search(L,k)

x head[L]

while (x NIL) and (data[x] k)

do x next[x]

return(x)

Page 34: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

34

Inserting into the head

List-head-Insert(L,x)

next[x] head[L]

head[L] x

Page 35: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

35

Insert after a key k

Value

NULL

L

nodeValue Next

node

k

Insert the value v after P

Next

Valuev

Next

Page 36: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

36

Insert after a key

Insert-after-key(L,k,x)

y List-Search(L,k)

if y NIL

next[x] next[y]

next[y] x

end

Page 37: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

37

Linked List Delete a key

L

Value Next

node

Value Next

node

kNULL

Value Next

Page 38: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

38

Linked List Delete a key

L

Value Next

node

To delete the node pointed to by Q,need a pointer to the previous node;

Value Next

node

QNULL

Page 39: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

39

Linked List Delete

L

Value Next

node

Value Next

node

NULL

kprev_x

Page 40: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

40

Deleting a key

List-Delete(L,k)x head[L]

prev_x= head[L]while (x NIL) and (data[x] k) do

prev_x xx next[x]

endif prev_x NIL

next[prev_x] next[x]end

Page 41: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

41

Doubly Linked Lists

As mentioned, for delete we need ‘findPrevious’. This is a slow [O(N)] function - because we cannot go directly to previous node

Solution: Keep a "previous" pointer at each node.

head prev prev prev

Page 42: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

42

Double Link Pros and Cons

AdvantageDelete (not DeleteAfter) and FindPrev are faster

Disadvantages:More space used up (double the number of

pointers at each node)More book-keeping for updating the two pointers

at each node (pretty negligible overhead)

Page 43: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

43

Insertion Sort Using Linked Lists

9 8 10 6

9 8 10 6

9 8 10 6

Page 44: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

44

Data Structure: linked list implementation

Using records and pointersUsing arrays

Page 45: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

45

Pointer Implementation

Basic IdeaData: For each node allocate memory Pointer: Keep a pointer to the memory

location.

ValueNULL

L node

Value Next

node

Next

Page 46: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

46

Records

A record (also called a struct, similar to object)Group data together that are related

To access the fields we use “dot” notation.

Example: x: name and image

Example: x.name x.image

image name

Elton John

Page 47: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

47

PointerA pointer is a reference to a variable or record (or to an object in Java world).

In C, if X is of type pointer to Y then *X is of type Y – same for pseudocodeExample: x : record pointer

record*x

Page 48: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

48

A node: a record which contains a pointer

Group data and a pointer

To access the fields we use

Name: textImage: image

Example: x : complex number

Example: x.name x.image x.next

next: node reference (a pointer)

name[x]image [x]next[x]

OR

Page 49: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

49

Simple Linked List

A linked listGroup data together in a flexible, dynamic way.

4 9 13 20

L : node pointer

record node : ( data : integer (or any other structure) next : node pointer)

Page 50: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

50

Pointer Implementation Issues

Whenever you break a list, your code should fix the list up as soon as possibleDraw pictures of the list to visualize what needs to

be donePay special attention to boundary conditions:

Empty listSingle item – same item is both first and lastTwo items – first, last, but no middle itemsThree or more items – first, last, and middle items

Page 51: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

51

Implementing Pointers using Arrays

This is needed in languages like Fortran, Basic, and assembly language

Easiest when number of records is known ahead of time.

Each record field of a basic type is associated with an array.

A pointer field is an unsigned integer indicating an array index.

Page 52: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

52

Idea

data next

n nodes 012345..n-1

data : basic typenext : node pointer

D N• D[ ] : basic type array• N[ ] : integer array• Pointer is an integer• nil is -1• p.data is D[p]• p.next is N[p]• Two variables:

• L (head of list)• Free (used for nodeallocation)

Pointer World Nonpointer World

Page 53: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

53

Initialization

012345...

-1

0

1

2

3

n-2

D NFree = n-1

means

012345..n-1

D N nil

Free

Page 54: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

54

Example of Use

a b cL

null

c

a

2

-1

-1

5

7

D N

b 1

4

0

01234567

n = 8L = 3Free = 6

InsertFront(L, x) if (Free ≠ -1) then

q Freeelse

return “overflow”;

Free N[Free];

D[q] x;

N[q] L;

L q;

Page 55: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

55

Try DeleteFront

Define the cursor implementation of DeleteFront which removes the first member of the list when there is one.Remember to add garbage to free list.

DeleteFront(L) {???}

Page 56: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

56

DeleteFront Solution

DeleteFront(L) if L = -1 then

return “underflow” else q L; L N[L]; N[q] Free; Free q;

c

a

2

-1

-1

5

7

D N

b 1

4

0

01234567

n = 8L = 3Free = 6

a b cL

null

3

0

0

7

8

2

5

1

n = 8L = 5Free = 4

Page 57: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

57

Insert Running Time

List implementation by arrayWorst case is insert at position 0. Must move all

N items one position before the insertOn average, must move half the elements to

make room – assuming insertions at positions are equally likely

Probably too slowList implementation by Pointers:

Independent on the list length

Page 58: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

58

SummaryADT: An abstract mathematical definition of

objects, with operations defined on themData Structures:

Representation of objects of an Abstract Data Type (ADT)

Algorithms manipulate the data structures to implement the operations of the ADT

Examples

Page 59: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

59

Next Class

Application example of lists: Sparse Polynomial

Stacks Queues

Page 60: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

60

Course Overview

Introduction to many of the basic data structures used in computer softwareUnderstand the data structuresHow to implement themAnalyze the algorithms that use themKnow when to apply them

Practice design and analysis of data structures.Practice using these data structures by writing

programs.

Page 61: 1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42

61

Application example: Sparse Polynomials

10 + 4 x2 + 20 x40 + 8 x86

010

24

4020

868

P

record poly : (

exp : integer,

coef : integer,

next : poly pointer

)

exp

coef

next