DSA1 | Week 3

Representing Graphs

Graphs

Model relationships between entities.

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

e

b

d

f

a

a

b

c

d

f

b

c

e

f

e

d

Adjacency Matrix

a

c

e

b

d

f

a

a

b

c

d

f

b

c

e

f

e

d

0

Adjacency Matrix

a

c

e

b

d

f

a

c

d

e

f

b

c

d

e

f

a

b

0

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

0

1

& so on.

Adjacency List

a

c

e

b

d

f

Adjacency List

a

c

e

b

d

f

a

c

d

e

f

b

Adjacency List

a

e

d

f

a

c

d

e

f

b

c

b

b

c

Adjacency List

a

e

d

f

a

c

d

e

f

b

c

b

b

c

& so on.

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.

& so on.

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

Recap: what we have seen so far

Arrays v/s Lists

Lookups Addition
(at start)
Addition
(at end)
Deletions
Arrays Easy Hard Depends Prop to location of element being deleted
Lists Hard Easy Depends Prop to location of element being deleted
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

1

1

2

1

2

3

1

2

3

4

1

2

3

4

5

1

2

3

4

5

6

1

2

3

4

5

6

7

1

2

3

4

5

6

7

8

1

2

3

4

5

6

7

8

9

1

2

3

4

5

6

7

8

9

1

2

3

4

5

6

7

8

9

Try them all

Explore one thoroughly

What do you do when you have many different options?

Navigating Graphs

Navigating Graphs

Try them all

Explore one thoroughly

What do you do when you have many different options?

Try them all

Explore one thoroughly

What do you do when you have many different options?

Navigating Graphs

DSA1 | Week 3

By Neeldhara Misra