What is time complexity of BFS depending on the representation of the graph?

被刻印的时光 ゝ 提交于 2019-12-20 15:42:07

问题


I was wondering what is the time complexity of BFS, if I use:

  • an adjacency matrix
  • adjacency list
  • edge list

Is it same as their space complexity?


回答1:


The complexity of BFS implemented using an Adjacency Matrix will be O(|V|2). And that when implemented by an Adjacency List is O(|V| + |E|).

Why is it more in the case of Adjacency Matrix?

This is mainly because every time we want to find what are the edges adjacent to a given vertex 'U', we would have to traverse the whole array AdjacencyMatrix[U], which is off course of length |V|.

Imagine the BFS progressing as frontiers. You take a starting vertex S, which is at level 0. All the adjacent vertices are at level 1. Then, we mark all the adjacent vertices of all vertices at level 1, which don't have a level, to level 2. So, every vertex will belong to one frontier (or level) only. And when an element is in a frontier, we check once for its adjacent vertices, which takes O(|V|) time. As, the frontier covers |V| elements over the course of the algorithm, the total time would become O(|V| * |V|) which is O(|V|2).

The complexity difference in BFS when implemented by Adjacency Lists and Matrix occurs due to this fact that in Adjacency Matrix, to tell which nodes are adjacent to a given vertex, we take O(|V|) time, irrespective of edges. Whereas, in Adjacency List, it is immediately available to us, takes time proportional to adjacent vertices itself, which on summation over all vertices |V| is |E|. So, BFS by Adjacency List gives O(|V| + |E|).




回答2:


Time complexity necessarily depends on the representation.

As this link suggests, the time complexity with and adjacency list is O(V + E), and with an adjacency matrix is O(V2).




回答3:


First let's look at the time complexity. If an adjacency matrix can be stored as a sparse matrix, the space complexity would be the same . A sparse matrix essentially stores only the nonzero values of the adjacency matrix, hence has the same space complexity as an adjacency list representation, i.e. O(|V| + |E|)

Now on to time complexity. One way of doing a BFS search is to simply use a sparse adjacency matrix as one would normally use an adjacency list representation, i.e. O(|V| + |E|).

Another way of doing a BFS on an adjacency matrix is by using sparse matrix-vector multiplications by repeatedly applying Y=G X, where G is a sparse adjacency matrix and X is a sparse vector with 1s on the frontier. This operation is basically a combination of columns of G. If this operation is implemented such that each multiplication takes time proportional to the size of the data accessed in each column of G and the number of nonzeros in the vector X, then the time complexity would also be O(|V| + |E|). An advantage of this approach is that it is straight forward to extend it to multiple BFS searches by replacing X with a sparse matrix representing the frontiers of several BFS searches. More details can be seen in this paper on implementations of graph algorithms using sparse matrices.

An edge list is not typically used for BFS, since it is expensive to find the neighbors of a vertex.



来源:https://stackoverflow.com/questions/19553676/what-is-time-complexity-of-bfs-depending-on-the-representation-of-the-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!