Algorithm 深度优先搜索(DFS)与广度优先搜索(BFS)伪码和复杂性

Algorithm 深度优先搜索(DFS)与广度优先搜索(BFS)伪码和复杂性,algorithm,time-complexity,depth-first-search,breadth-first-search,pseudocode,Algorithm,Time Complexity,Depth First Search,Breadth First Search,Pseudocode,我必须为一个算法开发伪代码,该算法计算连接的数据的数量 图G=(V,E)中给定顶点V和边E的分量 我知道我可以使用深度优先搜索或广度优先搜索来计算连接组件的数量 然而,我想使用最有效的算法来解决这个问题,但我不确定每个算法的复杂性 下面是以伪代码形式编写DFS的尝试 function DFS((V,E)) mark each node in V with 0 count ← 0 for each vertex in V do if vertex

我必须为一个算法开发伪代码,该算法计算连接的数据的数量 图G=(V,E)中给定顶点V和边E的分量

我知道我可以使用深度优先搜索或广度优先搜索来计算连接组件的数量

然而,我想使用最有效的算法来解决这个问题,但我不确定每个算法的复杂性

下面是以伪代码形式编写DFS的尝试

function DFS((V,E))
     mark each node in V with 0
     count ← 0
     for each vertex in V do
          if vertex is marked then
               DFSExplore(vertex)

function DFSExplore(vertex)
     count ← count + 1
     mark vertex with count
     for each edge (vertex, neighbour) do
          if neighbour is marked with 0 then
               DFSExplore(neighbour)
function BFS((V, E))
     mark each node in V with 0
     count ← 0, init(queue)     #create empty queue 
     for each vertex in V do
          if vertex is marked 0 then
               count ← count + 1
               mark vertex with count
               inject(queue, vertex)             #queue containing just vertex
               while queue is non-empty do
                    u ← eject(queue)                          #dequeues u
                    for each edge (u, w) adjacent to u do
                         if w is marked with 0 then
                              count ← count + 1
                              mark w with count
                              inject(queue, w)     #enqueues w
下面是以伪代码形式编写BFS的尝试

function DFS((V,E))
     mark each node in V with 0
     count ← 0
     for each vertex in V do
          if vertex is marked then
               DFSExplore(vertex)

function DFSExplore(vertex)
     count ← count + 1
     mark vertex with count
     for each edge (vertex, neighbour) do
          if neighbour is marked with 0 then
               DFSExplore(neighbour)
function BFS((V, E))
     mark each node in V with 0
     count ← 0, init(queue)     #create empty queue 
     for each vertex in V do
          if vertex is marked 0 then
               count ← count + 1
               mark vertex with count
               inject(queue, vertex)             #queue containing just vertex
               while queue is non-empty do
                    u ← eject(queue)                          #dequeues u
                    for each edge (u, w) adjacent to u do
                         if w is marked with 0 then
                              count ← count + 1
                              mark w with count
                              inject(queue, w)     #enqueues w
我的讲师说BFS与DFS具有相同的复杂性

然而,当我向上搜索深度优先搜索的复杂度时,它是O(V^2),而当使用邻接列表时,宽度优先搜索的复杂度是O(V+E),当使用邻接矩阵时,它是O(V^2)


我想知道如何计算DFS/BFS的复杂度,我想知道如何调整伪代码来解决问题。

如果使用邻接列表,DFS和BFS的时间复杂度是相同的,即O(V+e)。所以如果你问,哪一个更好,那完全取决于你要解决的问题的类型。假设你想解决一个问题,如果你的目标接近起始顶点,那么BFS将是一个更好的选择。另外,如果考虑内存,那么DFS是更好的选择,因为不需要存储子指针。


图像礼貌-Narasimha Karumachi简化了DSA

具有V顶点的图形可以有多达
V*(V-1)/2的边。所以O(V+E)和O(V^2)之间没有区别,除非问题陈述在E上加了一个较小的上界。哪个参考文献告诉你DFS的时间复杂度是O(V^2)?你的讲师是正确的。在DFS代码中,每个边(顶点、相邻点)都有
。在BFS代码中,与u do相邻的每条边(u,w)都有
。这两行可以通过邻接列表或邻接矩阵来实现。如果使用邻接列表实现,则可以说这两种算法都在O(V+E)时间内运行。如果用邻接矩阵实现,这两种算法都可以说是在O(V^2)时间内运行的。但这是一个没有区别的区别,因为E可以是O(V^2),在这种情况下,O(V+E)是O(V+V^2),这就是O(V^2)。