DSA1 | Week 2

Data Structures: Just Some Structured Data

Sets & Sequential Data

The atomic types have served us well,

but it is now time to build on them.

Say you are a real estate agent &

need to reserve some houses in a new

apartment complex.

The builder can block any contiguous chunk

of N houses for you.

Your list of clients may evolve over time.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

⛔️

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

⛔️

👇

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

🔑

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

🔑

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

🔑

🔑

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

🔑

🔑

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

🔑

🔑

🔑

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

🔑

🔑

🔑

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

🔑

🔑

🔑

🔑

Arrays

Linked Lists

Looking up the i-th element 

Adding a new element at the start

Adding a new element at the end

Removing the i-th element

instant

instant

instant*

\(\approx\) n

\(\approx\) n

\(\approx\) n-i

\(\approx\) i

Adding a new element at the i-th location

\(\approx\) n-i

\(\approx\) min( i,n-i)**

\(\approx\) min( i,n-i)**

* Assuming we have a tail pointer.

** Assuming we have backward pointers.

Why can't everything be fast?

DYNAMIC ARRAYS

Whenever the array overflows, double its size.

Total cost = total #insertions + total #copy operations

Total cost = total #insertions + total #copy operations

\(\leqslant\) N + N = 2N

...since each copy can be associated with a distinct insert,

& the #inserts = N; so #copy operations \(\leqslant\) N

Why can't everything be fast?

DYNAMIC ARRAYS

Whenever the array overflows, double its size.

Graphs

Model relationships between entities.

Graphs

Common graphical representation: points & lines

Graphs: Terminology

Vertices & Edges

Vertices are adjacent if there is an edge between them; and they are the endpoints of said edge.

An edge is incident to its endpoints.

The (out)-neighbors of a vertex X are all the vertices Y such that (X,Y) is an edge.

The degree of a vertex is the number of neighbors it has.

Representing Graphs

1. Adjacency Matrix

2. Adjacency Lists

3. Edge Lists

Three popular styles.

a

c

e

b

d

f

Adjacency Matrix

a

c

e

b

d

f

a

a

b

c

d

e

f

b

c

d

e

f

Adjacency Matrix

a

c

b

f

a

a

b

c

d

f

b

c

e

f

e

d

e

d

Adjacency Matrix

a

a

b

c

d

f

b

c

e

f

e

d

0

a

c

e

b

d

f

Adjacency Matrix

c

e

d

f

a

c

d

e

f

b

c

d

e

f

a

b

0

a

b

Adjacency Matrix

a

c

e

b

d

f

a

c

d

e

f

b

c

d

e

f

a

b

0

1

Adjacency Matrix

a

c

e

b

d

f

a

c

d

e

f

b

c

d

e

f

a

b

1

1

1

1

1

1

1

1

1

1

Adjacency Matrix

a

c

e

b

d

f

a

c

d

e

f

b

c

d

e

f

a

b

1

1

1

1

1

1

1

1

1

1

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

Adjacency Matrix

a

c

e

b

d

f

a

c

d

e

f

b

c

d

e

f

a

b

1

1

1

1

1

1

1

1

1

1

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

Adjacency Matrix

a

c

e

b

d

f

a

c

d

e

f

b

c

d

e

f

a

b

1

1

1

1

1

1

1

1

1

1

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

Adjacency List

a

c

e

b

d

f

Adjacency List

a

c

e

b

d

f

a

c

d

e

f

b

Adjacency List

e

d

f

c

d

e

f

b

c

b

b

c

a

a

Adjacency List

a

e

d

f

a

c

d

e

f

b

c

b

b

c

a

Adjacency List

a

d

e

f

b

c

b

b

c

a

a

a

d

e

e

d

f

c

Adjacency List

a

c

e

f

b

c

b

b

c

a

a

a

d

e

e

d

f

c

d

Adjacency List

a

c

d

f

b

c

b

b

c

a

a

a

d

e

e

d

f

c

c

f

e

Adjacency List

a

c

d

b

c

b

b

c

a

a

a

d

e

e

d

f

c

c

f

e

e

f

Adjacency List

a

c

d

b

c

b

b

c

a

a

a

d

e

e

d

f

c

c

f

e

e

f

Edge List

a

e

d

f

c

b

Edge List

a

e

d

f

c

b

a

b

1.

Edge List

a

e

d

f

c

b

a

b

1.

c

d

2.

Edge List

a

e

d

f

c

b

a

b

1.

c

d

2.

e

f

3.

Edge List

a

e

d

f

c

b

a

b

1.

c

d

2.

e

f

3.

a

c

4.

Edge List

a

e

d

f

c

b

a

b

1.

c

d

2.

e

f

3.

a

c

4.

c

e

5.

Edge List

a

e

d

f

c

b

a

b

1.

c

d

2.

e

f

3.

a

c

4.

c

e

5.

Official Running Times

Brilliant.

When the procedure only needs constant time.

Decent.

When the procedure always wraps up in, and sometimes needs,
time proportional to the maximum degree of the graph.

(n/m)-tolerable.

When the procedure always wraps up in, and sometimes needs,

time proportional to the number of vertices/edges in the graph.

(n/m)-painful.

When the procedure always wraps up in, and sometimes needs,

time proportional to the number of vertices/edges in the graph squared.

Adj. Matrix Adj. List Edge List
Adding a vertex
Deleting a vertex
Adding an edge
Deleting an edge
Finding degree(v)
Check if (u,v) is an edge
Adj. Matrix Adj. List Edge List
Adding a vertex Painful
Deleting a vertex Painful
Adding an edge Brilliant
Deleting an edge Brilliant
Finding degree(v) Tolerable
Check if (u,v) is an edge Brilliant
Adj. Matrix Adj. List Edge List
Adding a vertex Painful Tolerable
Deleting a vertex Painful Tolerable
Adding an edge Brilliant Brilliant
Deleting an edge Brilliant Decent
Finding degree(v) Tolerable Decent
Check if (u,v) is an edge Brilliant Decent
Adj. Matrix Adj. List Edge List
Adding a vertex Painful Tolerable Decent
Deleting a vertex Painful Tolerable Tolerable
Adding an edge Brilliant Brilliant Brilliant
Deleting an edge Brilliant Decent Tolerable
Finding degree(v) Tolerable Decent Tolerable
Check if (u,v) is an edge Brilliant Decent Tolerable

2023 DSA1 | Week 2

By Neeldhara Misra

2023 DSA1 | Week 2

Data Structures: Just some Structured Data

  • 579