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

后端 未结 4 1243
野性不改
野性不改 2021-02-04 15:15

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 spa

相关标签:
4条回答
  • 2021-02-04 15:24

    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.

    0 讨论(0)
  • 2021-02-04 15:26

    Time complexities for different representations of Graph:

    1. Edge List:

    Edge list consists of all the edges in a list. In order to do the BFS time complexity is O(E^2).Because for every edge u->v, you have to traverse through entire edge list and find the edges whose source vertex is u and explore them, then explore the vertices 'v' which are in u->v to do the BFS.

    Where E is the number of edges.

    If you sort the edges based on source index and destination index, then the sorted list will be in BFS order. Just traverse the list, you will get the BFS.

    Time complexity is O(E*log(E)) for sorting the edge list.

    2. Adjacency List

    Adjacency is a map of keys, where every vertex is a key and points to a list of vertices which are incident from or adjacent to that key vertex.

    In order to perform BFS, put any vertex in the queue and make it as visited, pop the queue[0], pick the starting vertex, explore all its adjacent vertexes, make them as visited and put them in the queue and similarly pop the queue[0] and explore all the non-visited vertices until the queue becomes empty. For every vertex, you are traversing through only its adjacent non-visited vertexes(nothing but edges).

    So, the time complexity is O(V+E)

    3. Matrix

    In matrix representation, for every vertex, you have to traverse through all the vertices and check whether there is a non-visited vertex. Since, for every vertex, we are traversing through all the vertices,

    The time complexity is O(V^2)

    0 讨论(0)
  • 2021-02-04 15:28

    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).

    0 讨论(0)
  • 2021-02-04 15:38

    The complexity of BFS implemented using an Adjacency Matrix will be O(|V|²). 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 ofcourse of length |V|.

    Imagine the BFS progressing as frontiers. You take a starting vertex S, which is at level 0. All the vertices adjacent to S will be 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 only one frontier. Each of these frontiers correspond to different levels. 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|²).

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

    0 讨论(0)
提交回复
热议问题