BFS和DFS的时空复杂性,在AdjacencyMatrix和AdjacencyList中的图上

时间:2019-05-22 23:13:31

标签: time graph complexity-theory space breadth-first-search

我已经执行了BFS和DFS(用于打印图形)->具有多种实现。 但是我对它们的时间复杂度感到困惑,同时在网上阅读一些文章时说它的O(V ^ 2)。有人说它的O(| V + E |) 我现在真的很困惑。

我将附加我的不同实现。 [附加为链接以更好地突出显示语法] 1. Graph_AdjMat --> graph with matrix implementation

  1. Graph_AdjList --> graph with list< list> implementation

  2. Graph_AdjList_Weighted_Map graph for weighted scenario, weights imipllements using Map, to map node -> node_weight

  3. Graph_AdjList_Weighted_Vertex graph for weighted scenario, weights imipllements using Vertex class

主要需要检查代码(1)(2)&(3)

as in code (1) : implementation using matrix.       [[[ CONSTANT time  for checking if two node's are connected ]]]
in code(2) . : implementation using List of Lists     [[[ --> here it takes O(N) for checking if two node's are connected ]]]
in code(3) . : implementation using List of Maps     [[[ --> CONSTANT time  for checking if two node's are connected ]]]

因此,如果我在所有这些中都实现dfs和bfs,那么DFS和BFS的时间和空间复杂度将是多少?

               TIME (DFS)         TIME (BFS)      SPACE (DFS)     SPACE (BFS)
--------------------------------------------------------------------------------
Code 1  :   |                  |                  |             |              |
------------|------------------|------------------|-------------|--------------|
Code 2  :   |                  |                  |             |              |
------------|------------------|------------------|-------------|--------------|
Code 3  :   |                  |                  |             |              |
--------------------------------------------------------------------------------

0 个答案:

没有答案