Doubts about complexity bfs

3

In some texts (many), where the algorithm BFS is explained (Search in width - < em> Breadth First Search ) is a section that says that the complexity of this is O(v+e) , or failing O(v2) . That is, a problem P, but in some AI texts, when they explain this algorithm, they come out that the complexity is O(XV) . That is, exponential, which makes it an NP problem which makes the explanation they give much more meaningful.

My question is where the complexity of O(v+e) u O(v2) comes from.

    
asked by Kevin AB 18.06.2016 в 00:19
source

1 answer

1

The answer is, they are both. Because everything depends on how you are analyzing things.

The questions you should ask yourself are, what am I going through? How big is the tree that forms the algorithm? I know how big it is? For the algorithm at some point?

To traverse the entire graph I must go through all the nodes passing through all the axes (in the worst case we have n2 axes, with n the number of nodes).

Now, BFS forms a tree, assuming that the number of axes per nodes is x and x is maximum, to go from one level to another of the tree I go through x-1 axes for each node of the level.

Well, if I'm node v and I get to it using the axis (u,v) I will not go back that axis (I'll ignore it). That is, when you reach the leaves of the tree you will have traveled xn axes, where n is the distance from the leaves to the root.

Let's see that this is fulfilled in a finite and complete graph (that of n2 axes).

From the root to the next level the algorithm will run n axes.

Then in the next level, which has n nodes, it will travel n-1 axes per level node, that is, it will travel (n-1)*n axes.

That is, in total n+(n-1)*n . Which gives n2 .

    
answered by 08.09.2016 в 05:14