Vera Sacristan data structure Using the...

Post on 18-Oct-2020

1 views 0 download

Transcript of Vera Sacristan data structure Using the...

Discrete and Algorithmic GeometryFacultat de Matematiques i Estadıstica

Universitat Politecnica de Catalunya

Using the appropriatedata structure

Vera Sacristan

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Data structures to implement these operations

USING THE APPROPRIATE DATA STRUCTURE

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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