598 views
0 votes
0 votes
adding an node in adjancy matrix and adjacancy list takes _________time?

expalin

2 Answers

2 votes
2 votes

Adding node in adjacancy matrix : You need O(n2) time. consider the case undirected complete graph u need to update entries on n-1 vertices ( which take O(1) to update but finding take O(n) time ) so O(n2) , one has to increase the storage for a n2 matrix to (n+1)² i.e. you will have to copy the whole content of the former smaller matrix to the newly allocated memory for the bigger matrix, because the mapping won't fit anymore. And that is obviously an O(n2) operation.

consider above 3 graph and assume d in 1st graph is added .you need to change the entries of a,b,c from d and from a,b,c to d,

Adding node in adjacancy list : It will take O(n) time in worst case when graph is dense , thats why these used when graph is sparse .

edited by
0 votes
0 votes

In adding a vertex , 

a) in case of adjacency matrix , it takes O(V2) time since adding a node requires that the dimension of the matrix needs to be changed and also the fact we know that in 1 D array of n elements , insertion of a new element somewhere in the middle takes O(n) time due to shift operations.So for each row , O(V) time.So for V rows = O(V2) time

b) In case of the adjacency linked list , we just add the node hence O(1) time.

Reference : https://en.wikipedia.org/wiki/Graph_(abstract_data_type)

Here you can find the comparision of running time complexities of operations of graph for adjacency matrix , adjacency list and incidence matrix representations of graph.

Related questions

–4 votes
–4 votes
2 answers
1
Souvik33 asked Oct 27, 2022
654 views
*MSQ*The following figure depicts a a. A tree and only treeb. A tree with 3 nodesc. A graph (Since every tree is a graph)d. A graph and only graph
1 votes
1 votes
0 answers
2
6 votes
6 votes
3 answers
3
rahul sharma 5 asked Dec 9, 2017
3,921 views
Consider the following graph:The minimum size of queue required when performing BFS on above graph is ________.(Size of queue is represented by maximum number of element ...
0 votes
0 votes
2 answers
4