Knowee
Questions
Features
Study Tools

If adjacency matrix is used, then what is the time complexity of BFS algorithm?a.O(e)b.O(n+e)c.Od.O(n2)

Question

If adjacency matrix is used, then what is the time complexity of BFS algorithm?a.O(e)b.O(n+e)c.Od.O(n2)

🧐 Not the exact question you are looking for?Go ask a question

Solution

The time complexity of BFS (Breadth-First Search) algorithm using an adjacency matrix is O(n^2). Here's why:

  1. Initialization: Before the BFS algorithm starts, it initializes all vertices as not visited, which takes O(n) time.

  2. Traversal: The BFS algorithm traverses all vertices of the graph. For each vertex, it needs to scan its entire row in the adjacency matrix to find its adjacent nodes. Since the adjacency matrix is a n*n matrix (where n is the number of vertices), this step takes O(n) time for each vertex.

  3. Total time complexity: Since the BFS algorithm needs to do the above step for all n vertices, the total time complexity is O(n*n) = O(n^2).

So, the correct answer is d. O(n^2).

This problem has been solved

Similar Questions

What is the worst time complexity of checking node u and v are directly connected by an edge in a graph G = |V, E| when adjacency list is used

Write an algorithm for Breadth First Search (BFS) traversal of a graph.

When will BFS traversal be complete for the given graph? BFS traversal will be complete when all the vertices are marked as visited and the queue is empty. BFS traversal will be complete when all the vertices are added to the queue.

Time Complexity of DFS is? (V – number of vertices, E – number of edges)Group of answer choicesO(V + E)O(E)O(V*E)O(V)

What is the time complexity of detecting a cycle in an undirected graph using DFS?O(V)O(V+E)O(ElogV)O(V^2)

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.