recategorized by
3,807 views
1 votes
1 votes
Adacency list is preferred over adjacency matrix when the graph is?

A) planar

B)  Dense

C)  Clique

D)  none of these
recategorized by

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

Related questions

1 votes
1 votes
0 answers
2
admin asked Dec 15, 2022
272 views
Consider the following graph.Find closeness centrality of $\text{‘A’}$ node.
1 votes
1 votes
0 answers
3
admin asked Dec 15, 2022
276 views
Consider the following graph.Which nodes form the cliques of size $3?$
43 votes
43 votes
6 answers
4
Ishrat Jahan asked Oct 28, 2014
14,097 views
$G$ is a simple undirected graph. Some vertices of $G$ are of odd degree. Add a node $v$ to $G$ and make it adjacent to each odd degree vertex of $G$. The resultant graph...