Vera Sacristan data structure Using the...

33
Discrete and Algorithmic Geometry Facultat de Matem` atiques i Estad´ ıstica Universitat Polit` ecnica de Catalunya Using the appropriate data structure Vera Sacrist´ an

Transcript of Vera Sacristan data structure Using the...

Page 1: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Discrete and Algorithmic GeometryFacultat de Matematiques i Estadıstica

Universitat Politecnica de Catalunya

Using the appropriatedata structure

Vera Sacristan

Page 2: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Depending on what we want to do with our data,it may be convenient to store them in different ways

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

USING THE APPROPRIATE DATA STRUCTURE

Page 3: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Depending on what we want to do with our data,it may be convenient to store them in different ways

Example

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

USING THE APPROPRIATE DATA STRUCTURE

Page 4: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Depending on what we want to do with our data,it may be convenient to store them in different ways

Example

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Consider the following graph G:

1 2

34 5

USING THE APPROPRIATE DATA STRUCTURE

Page 5: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Depending on what we want to do with our data,it may be convenient to store them in different ways

Example

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Consider the following graph G:

1 2

34 5

Incidence matrix0 1 1 1 01 0 1 0 11 1 0 0 01 0 0 0 00 1 0 0 0

USING THE APPROPRIATE DATA STRUCTURE

Page 6: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Depending on what we want to do with our data,it may be convenient to store them in different ways

Example

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Consider the following graph G:

1 2

34 5

Incidence matrix0 1 1 1 01 0 1 0 11 1 0 0 01 0 0 0 00 1 0 0 0

Adjacency list

((2, 3, 4), (1, 3, 5), (1, 2), (1), (2))

USING THE APPROPRIATE DATA STRUCTURE

Page 7: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Depending on what we want to do with our data,it may be convenient to store them in different ways

Example

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Consider the following graph G:

1 2

34 5

Incidence matrix0 1 1 1 01 0 1 0 11 1 0 0 01 0 0 0 00 1 0 0 0

Adjacency list

((2, 3, 4), (1, 3, 5), (1, 2), (1), (2))

Are vi and vjconnected?

USING THE APPROPRIATE DATA STRUCTURE

Page 8: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Depending on what we want to do with our data,it may be convenient to store them in different ways

Example

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Consider the following graph G:

1 2

34 5

Incidence matrix0 1 1 1 01 0 1 0 11 1 0 0 01 0 0 0 00 1 0 0 0

Adjacency list

((2, 3, 4), (1, 3, 5), (1, 2), (1), (2))

Are vi and vjconnected?

O(1)

O(n)

USING THE APPROPRIATE DATA STRUCTURE

Page 9: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Depending on what we want to do with our data,it may be convenient to store them in different ways

Example

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Consider the following graph G:

1 2

34 5

Incidence matrix0 1 1 1 01 0 1 0 11 1 0 0 01 0 0 0 00 1 0 0 0

Adjacency list

((2, 3, 4), (1, 3, 5), (1, 2), (1), (2))

Are vi and vjconnected?

O(1)

O(n)

What isdegree(vi)?

USING THE APPROPRIATE DATA STRUCTURE

Page 10: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Depending on what we want to do with our data,it may be convenient to store them in different ways

Example

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Consider the following graph G:

1 2

34 5

Incidence matrix0 1 1 1 01 0 1 0 11 1 0 0 01 0 0 0 00 1 0 0 0

Adjacency list

((2, 3, 4), (1, 3, 5), (1, 2), (1), (2))

Are vi and vjconnected?

O(1)

O(n)

What isdegree(vi)?

O(n)

O(degree)

USING THE APPROPRIATE DATA STRUCTURE

Page 11: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Depending on what we want to do with our data,it may be convenient to store them in different ways

Example

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Consider the following graph G:

1 2

34 5

Incidence matrix0 1 1 1 01 0 1 0 11 1 0 0 01 0 0 0 00 1 0 0 0

Adjacency list

((2, 3, 4), (1, 3, 5), (1, 2), (1), (2))

Are vi and vjconnected?

O(1)

O(n)

What isdegree(vi)?

O(n)

O(degree)

Storagespace

USING THE APPROPRIATE DATA STRUCTURE

Page 12: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Depending on what we want to do with our data,it may be convenient to store them in different ways

Example

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Consider the following graph G:

1 2

34 5

Incidence matrix0 1 1 1 01 0 1 0 11 1 0 0 01 0 0 0 00 1 0 0 0

Adjacency list

((2, 3, 4), (1, 3, 5), (1, 2), (1), (2))

Are vi and vjconnected?

O(1)

O(n)

What isdegree(vi)?

O(n)

O(degree)

Storagespace

O(n2)

O(edges)

USING THE APPROPRIATE DATA STRUCTURE

Page 13: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

USING THE APPROPRIATE DATA STRUCTURE

Terminology

Let S be a set in a given universe U .

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Page 14: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

USING THE APPROPRIATE DATA STRUCTURE

Terminology

Let S be a set in a given universe U .

Any data structure representing S and allowing the following operations:

• query• add• delete

is called a dictionary

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Page 15: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

USING THE APPROPRIATE DATA STRUCTURE

Terminology

Let S be a set in a given universe U .

Any data structure representing S and allowing the following operations:

• query• add• delete

is called a dictionary

If the universe U is totally sorted:

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Page 16: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

USING THE APPROPRIATE DATA STRUCTURE

Terminology

Let S be a set in a given universe U .

Any data structure representing S and allowing the following operations:

• query• add• delete

is called a dictionary

If the universe U is totally sorted:

• locate• insert• delete

dictionary

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Page 17: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

USING THE APPROPRIATE DATA STRUCTURE

Terminology

Let S be a set in a given universe U .

Any data structure representing S and allowing the following operations:

• query• add• delete

is called a dictionary

If the universe U is totally sorted:

• locate• insert• delete

dictionary

• minimum

priority queue

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Page 18: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

USING THE APPROPRIATE DATA STRUCTURE

Terminology

Let S be a set in a given universe U .

Any data structure representing S and allowing the following operations:

• query• add• delete

is called a dictionary

If the universe U is totally sorted:

• locate• insert• delete

dictionary

• minimum

priority queue

• maximum• next• previous

augmented dictionary

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Page 19: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Data structures to implement these operations

USING THE APPROPRIATE DATA STRUCTURE

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Page 20: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Data structures to implement these operations

Linked list

start x1 x2 . . . xn end

USING THE APPROPRIATE DATA STRUCTURE

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Page 21: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Data structures to implement these operations

Linked list

start x1 x2 . . . xn end

Doubly linked list

x1 x2 . . . xn start/endstart/end

USING THE APPROPRIATE DATA STRUCTURE

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Page 22: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Data structures to implement these operations

Linked list

start x1 x2 . . . xn end

Doubly linked list

x1 x2 . . . xn start/endstart/end

How are the operations done?

• Locate: explore the list in O(i) time• Insert/ delete: Once located, change pointers in O(1) time• Min/max: O(1) time• Next/previous: O(1) time

USING THE APPROPRIATE DATA STRUCTURE

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Page 23: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Data structures to implement these operations

Linked list

start x1 x2 . . . xn end

Doubly linked list

x1 x2 . . . xn start/endstart/end

How are the operations done?

• Locate: explore the list in O(i) time• Insert/ delete: Once located, change pointers in O(1) time• Min/max: O(1) time• Next/previous: O(1) time

Two particular lists are:

Stack: LIFO list (insert/delete only at the end)

Queue: LILO list (insert at the end, delete at the beginning)

USING THE APPROPRIATE DATA STRUCTURE

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Page 24: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Data structures to implement these operations

Binary-search trees

Balanced binary trees

USING THE APPROPRIATE DATA STRUCTURE

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Page 25: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Data structures to implement these operations

Binary-search trees

Balanced binary trees

acyclic connected graphs

USING THE APPROPRIATE DATA STRUCTURE

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Page 26: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Data structures to implement these operations

Binary-search trees

Balanced binary trees

acyclic connected graphs

each node has at most two children

USING THE APPROPRIATE DATA STRUCTURE

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Page 27: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Data structures to implement these operations

Binary-search trees

Balanced binary trees

acyclic connected graphs

each node has at most two children

all branches have approximately the same length

USING THE APPROPRIATE DATA STRUCTURE

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Page 28: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Data structures to implement these operations

Binary-search trees

Balanced binary trees

acyclic connected graphs

each node has at most two children

all branches have approximately the same length

Example

S = {1, 2, 3, 4, 5, 6, 7, 8} in U = (R ≤)

USING THE APPROPRIATE DATA STRUCTURE

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Page 29: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Data structures to implement these operations

Binary-search trees

Balanced binary trees

acyclic connected graphs

each node has at most two children

all branches have approximately the same length

Example

S = {1, 2, 3, 4, 5, 6, 7, 8} in U = (R ≤)

≤ 4

≤ 1

≤ 6≤ 2

≤ 3 ≤ 5 ≤ 7

1 2 3 4 5 6 7 8

USING THE APPROPRIATE DATA STRUCTURE

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Page 30: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Data structures to implement these operations

Binary-search trees

Balanced binary trees

acyclic connected graphs

each node has at most two children

all branches have approximately the same length

Example

S = {1, 2, 3, 4, 5, 6, 7, 8} in U = (R ≤)

≤ 4

≤ 1

≤ 6≤ 2

≤ 3 ≤ 5 ≤ 7

1 2 3 4 5 6 7 8

• Locate(3)• Locate(3.5)• Min(S)• Insert(3.5)• Delete(3)

USING THE APPROPRIATE DATA STRUCTURE

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Page 31: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

Data structures to implement these operations

Binary-search trees

Balanced binary trees

acyclic connected graphs

each node has at most two children

all branches have approximately the same length

Theorem

Given a set S of n elements ot a totally ordered univers U , it is possible to build abalanced binary search tree representing S using O(n) space and O(n log n) time.

Then: • memberQ• locate• min/max• insert/delete

run in O(log n) time

• next/previous runs in O(1) time

USING THE APPROPRIATE DATA STRUCTURE

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

Page 32: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

INITIAL READING

F. P.Preparata and M. I ShamosComputational Geometry: An IntroductionSpringer-Verlag, 1985, pp. 6-15 and 26-35.

J-D. Boissonnat and M. YvinecAlgorithmic GeometryCambridge University Press, 1997, pp. 3-31.

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

USING THE APPROPRIATE DATA STRUCTURE

Page 33: Vera Sacristan data structure Using the appropriatedccg.upc.edu/people/vera/wp-content/uploads/2012/10/DAG-Using... · Vera Sacristan Depending on what we want to do with our data,

INITIAL READING

F. P.Preparata and M. I ShamosComputational Geometry: An IntroductionSpringer-Verlag, 1985, pp. 6-15 and 26-35.

J-D. Boissonnat and M. YvinecAlgorithmic GeometryCambridge University Press, 1997, pp. 3-31.

Discrete and Algorithmic Geometry, Facultat de Matematiques i Estadıstica, UPC

FURTHER READING

T. H. Cormen, C. E. Leiserson, and R. L. RivestIntroduction to AlgorithmsThe MIT Press - McGraw Hill Book Company, 1990 (3rd ed. 2009)

A. V. Aho, J. E. Hopcroft, and J. D. UllmanData structures and algorithmsAddison-Wesley, 1983

USING THE APPROPRIATE DATA STRUCTURE