in Graph Theory recategorized by
3,797 views
1 vote
1 vote
Adacency list is preferred over adjacency matrix when the graph is?

A) planar

B)  Dense

C)  Clique

D)  none of these
in Graph Theory recategorized by
3.8k views

1 comment

(D)  None Of these              Because adjacency list is preferred when the matrix is sparse. In the sparse matrix less space are used so we go to adjacency list is better option
0
0

1 Answer

0 votes
0 votes

First of all note that Sparse means that you have very few edges, and Dense means many edges, or almost complete graph. In a complete graph you have $n(n−1)/2$ edges, where $n$is the number of nodes.

Now, when we use matrix representation we allocate $n×n$ matrix to store node-connectivity information, e.g., $M[i][j]=1$ if there is edge between nodes ii and jj, otherwise$ M[i][j]=0$.


But if we use adjacency list then we have an array of nodes and each node points to its adjacency list containing ONLY its neighboring nodes.

Now if a graph is sparse and we use matrix representation then most of the matrix cells remain unused which leads to the waste of memory. Thus we usually don't use matrix representation for sparse graphs. We prefer adjacency list.

But if the graph is dense then the number of edges is close to (the complete) $n(n−1)/2$, or to $(n^{2})$ if the graph is directed with self-loops. Then there is no advantage of using adjacency list over matrix.

n terms of space complexity
Adjacency matrix: $O(n^{2})$
Adjacency list: $O(n+m)$
where nn is the number nodes, mm is the number of edges.

When the graph is undirected tree then
Adjacency matrix: $O(n^{2})$
Adjacency list: $O(n+n)$ is $O(n)$ (better than $O(n^{2})$)

When the graph is directed, complete, with self-loops then
Adjacency matrix: $O(n^{2})$
Adjacency list: $O(n+(n^{2}))$ is $O(n^{2})$ (no difference)

And finally, when you implement using matrix, checking if there is an edge between two nodes takes $O(1)$ times, while with an adjacency list, it may take linear time in $O( n)$.

answer is none of these

1 comment

That's a really beautiful explanation. I have a query if anybody could help me out:

When the graph is directed, complete, with self-loops then
Adjacency matrix: O(n2)
Adjacency list: O(n+(n2)) is O(n2) (no difference)

I understood about Adjacency matrix to be O(n2) but in the case of Adjacency list why O(n+(n2)).

What do n and n2 represent in O(n+(n2))?

Thank you.
0
0

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true