在BFS算法中,嵌套循环嵌套的时间复杂性是什么?

发布于 2025-02-06 22:40:31 字数 1992 浏览 4 评论 0原文

因此,我编写了一种算法,该算法返回连接到一个给出节点的节点列表,并且在无方向图中的最大跃点内。

注意:无向图表示如果A连接到B,则B也将B连接到A。

基本上,EDGES如下:

"A", "B"
"A", "C"
"B", "D"
"C", "E"
"E", "F"
"E", "G"

边缘存储在Hashmap中如下:

{"A": ["B","C"]}, {"B", ["A", "D"]}... goes on.

findNodes(“ C”,2)返回[“ a”,“ b”,“ e”,“ f”,“ f”,“ g”]findnodes(“ A”,1)返回[“ B”,“ C”]

我应用了BFS算法如下:

  • 将第一个节点添加到队列。
  • 创建循环直到队列变为nil或 counter 变得与 maxhop 相同。
  • 获取队列中的节点数量。
  • 轮询节点并将其标记为 CurrentNode
  • 根据队列数量创建循环。
  • 然后创建另一个循环以获取 CurrentNode 的邻居,

因此,该算法如下。我对算法的时间复杂性感到非常困惑。有什么建议吗?

我不像具有o(n^3)时间复杂性的算法。达到正确时间复杂的正确方法是什么?因为每个循环和循环都取决于某种条件(应该是),但是时间复杂性是多少?

// FindNodes returns list of nodes connected to a given node and within a max number of hops
// e.g. "A", 2 -> ["B", "C", "D", "E"]
func (ug *UndirectedGraph) FindNodes(node string, maxHops int) []string {
    // if hop number is 0 then it returns the root, node itself.
    if maxHops == 0 {
        return []string{node}
    }

    result := []string{}
    counter := 0
    queue := Queue{Nodes: []string{}}
    queue.add(node)

    for len(queue.Nodes) != 0 && counter != maxHops {

        cnt := len(queue.Nodes)
        for i := 0; i < cnt; i++ {
            currentNode := queue.poll()
            if _, ok := ug.Visited[currentNode]; ok {
                continue
            }

            ug.Visited[currentNode] = true
            neighbours := ug.Edges[currentNode]
            for _, neighbour := range neighbours {
                if _, ok := ug.Visited[neighbour]; !ok {
                    result = append(result, neighbour)
                    queue.add(neighbour)
                }
            }
        }

        counter += 1
    }
    return result
}

So, I've written an algorithm that returns list of nodes connected to a give node and within a max number of hops in an undirected graph.

Note: Undirected graph means if A is connected to B then B is also connected to A.

Basically, let's say edges are as follows:

"A", "B"
"A", "C"
"B", "D"
"C", "E"
"E", "F"
"E", "G"

Edges are stored in a hashmap like map[string][]string as follows:

{"A": ["B","C"]}, {"B", ["A", "D"]}... goes on.

and FindNodes("C", 2) returns ["A", "B", "E", "F", "G"]
or FindNodes("A", 1) returns ["B", "C"].

I applied BFS algorithm as follows:

  • Add the first node to queue.
  • Creates while loop until queue becomes nil or counter becomes same as maxHop.
  • Get the number of nodes in the queue.
  • Poll the node and mark it as currentNode
  • Create for loop according to the number of queue.
  • And then create another loop to get neighbours of currentNode

So, the algorithm is as follows. And I'm quite confused about the time complexity of the algorithm. Any recommendations?

I don't it looks like an algorithm that has O(n^3) time complexity. What's the right way to reach correct time complexity? Because every for loop and the while loop depends on some condition (as it should be) but what's the time complexity?

// FindNodes returns list of nodes connected to a given node and within a max number of hops
// e.g. "A", 2 -> ["B", "C", "D", "E"]
func (ug *UndirectedGraph) FindNodes(node string, maxHops int) []string {
    // if hop number is 0 then it returns the root, node itself.
    if maxHops == 0 {
        return []string{node}
    }

    result := []string{}
    counter := 0
    queue := Queue{Nodes: []string{}}
    queue.add(node)

    for len(queue.Nodes) != 0 && counter != maxHops {

        cnt := len(queue.Nodes)
        for i := 0; i < cnt; i++ {
            currentNode := queue.poll()
            if _, ok := ug.Visited[currentNode]; ok {
                continue
            }

            ug.Visited[currentNode] = true
            neighbours := ug.Edges[currentNode]
            for _, neighbour := range neighbours {
                if _, ok := ug.Visited[neighbour]; !ok {
                    result = append(result, neighbour)
                    queue.add(neighbour)
                }
            }
        }

        counter += 1
    }
    return result
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文