Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Which implementation is more efficient to accomplish the following task? test

ID: 3571807 • Letter: 1

Question

1. Which implementation is more efficient to accomplish the following task?

test whether an edge exists between two vertices

Select one:

adjacency list

adjacency matrix

they are equally as efficient

2. Which implementation is more efficient to accomplish the following task?

determine whether a graph is complete

Select one:

adjacency list

adjacency matrix

they are equally as efficient

3. Which implementation is more efficient to accomplish the following task?

obtain a list of all vertices adjacent to a particular vertex

Select one:

adjacency list

adjacency matrix

they are equally as efficient

Explanation / Answer

Adjacency Matrix:
Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j. Adjacency matrix for undirected graph is always symmetric. Adjacency Matrix is also used to represent weighted graphs. If adj[i][j] = w, then there is an edge from vertex i to vertex j with weight w.

Pros: Representation is easier to implement and follow. Removing an edge takes O(1) time. Queries like whether there is an edge from vertex ‘u’ to vertex ‘v’ are efficient and can be done O(1).

Cons: Consumes more space O(V^2). Even if the graph is sparse(contains less number of edges), it consumes the same space. Adding a vertex is O(V^2) time.

Adjacency List:
An array of linked lists is used. Size of the array is equal to number of vertices. Let the array be array[]. An entry array[i] represents the linked list of vertices adjacent to the ith vertex. This representation can also be used to represent a weighted graph. The weights of edges can be stored in nodes of linked lists.

Pros: Saves space O(|V|+|E|) . In the worst case, there can be C(V, 2) number of edges in a graph thus consuming O(V^2) space. Adding a vertex is easier.

Cons: Queries like whether there is an edge from vertex u to vertex v are not efficient and can be done O(V).


1. Which implementation is more efficient to accomplish the following task?
test whether an edge exists between two vertices

   Ans: adjacency matrix

2. Which implementation is more efficient to accomplish the following task?
   determine whether a graph is complete

   Ans: they are equally as efficient, because you have to go each vertex and fine wether path exist to all other vertex from this or not

3. Which implementation is more efficient to accomplish the following task?
obtain a list of all vertices adjacent to a particular vertex

   Ans: adjacency list