您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

广度优先搜索时间复杂度分析

广度优先搜索时间复杂度分析

我希望这对任何难以理解“广度优先搜索”(又称为BFS)的计算时间复杂性的人都有帮助。

Queue graphTraversal.add(firstVertex);

// This while loop will run V times, where V is total number of vertices in graph.
while(graphTraversal.isEmpty == false)

    currentVertex = graphTraversal.getVertex();

    // This while loop will run Eaj times, where Eaj is number of adjacent edges to current vertex.
    while(currentVertex.hasAdjacentVertices)
        graphTraversal.add(adjacentVertex);

    graphTraversal.remove(currentVertex);

时间复杂度如下:

V * (O(1) + O(Eaj) + O(1))
V + V * Eaj + V
2V + E(total number of edges in graph)
V + E

我试图简化代码和复杂度计算,但是如果您有任何问题,请告诉我。

其他 2022/1/1 18:21:51 有548人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶