📌  相关文章
📜  来自源节点的树中每个节点的级别(使用 BFS)

📅  最后修改于: 2022-05-13 01:57:54.254000             🧑  作者: Mango

来自源节点的树中每个节点的级别(使用 BFS)

给定一棵具有 v 个顶点的树,从源节点开始计算树中每个节点的级别。
例子:

Input :   

使用 BFS 的源节点树中每个节点的级别

Output :  Node      Level
           0          0
           1          1
           2          1
           3          2
           4          2
           5          2
           6          2
           7          3

Explanation : 

使用 BFS 的源节点树中每个节点的级别

Input:

使用 BFS 的源节点树中每个节点的级别

Output :  Node      Level
           0          0
           1          1
           2          1
           3          2
           4          2
Explanation:

使用 BFS 的源节点树中每个节点的级别

方法:
BFS(广度优先搜索)是一种图遍历技术,首先访问节点及其邻居,然后访问邻居的邻居。简单来说,就是从源头逐层遍历。首先遍历一级节点(源节点的直接邻居),然后遍历二级节点(源节点的邻居),以此类推。 BFS 可用于确定来自给定源节点的每个节点的级别。
算法:

  1. 创建树,一个存储节点的队列,并在队列中插入根节点或起始节点。创建一个大小为 v(顶点数)的额外数组级别并创建一个已访问数组。
  2. 当队列大小大于 0 时运行循环。
  3. 将当前节点标记为已访问。
  4. 从队列中弹出一个节点并插入其子节点(如果存在)并将插入节点的大小更新为level[child] = level[node] + 1
  5. 打印所有节点及其级别。

执行:

C++
// CPP Program to determine level of each node
// and print level
#include 
using namespace std;
  
// function to determine level of each node starting
// from x using BFS
void printLevels(vector graph[], int V, int x)
{
    // array to store level of each node
    int level[V];
    bool marked[V];
  
    // create a queue
    queue que;
  
    // enqueue element x
    que.push(x);
  
    // initialize level of source node to 0
    level[x] = 0;
  
    // marked it as visited
    marked[x] = true;
  
    // do until queue is empty
    while (!que.empty()) {
  
        // get the first element of queue
        x = que.front();
  
        // dequeue element
        que.pop();
  
        // traverse neighbors of node x
        for (int i = 0; i < graph[x].size(); i++) {
            // b is neighbor of node x
            int b = graph[x][i];
  
            // if b is not marked already
            if (!marked[b]) {
  
                // enqueue b in queue
                que.push(b);
  
                // level of b is level of x + 1
                level[b] = level[x] + 1;
  
                // mark b
                marked[b] = true;
            }
        }
    }
  
    // display all nodes and their levels
    cout << "Nodes"
         << "    "
         << "Level" << endl;
    for (int i = 0; i < V; i++)
        cout << " " << i << "   -->   " << level[i] << endl;
}
  
// Driver Code
int main()
{
    // adjacency graph for tree
    int V = 8;
    vector graph[V];
  
    graph[0].push_back(1);
    graph[0].push_back(2);
    graph[1].push_back(3);
    graph[1].push_back(4);
    graph[1].push_back(5);
    graph[2].push_back(5);
    graph[2].push_back(6);
    graph[6].push_back(7);
  
    // call levels function with source as 0
    printLevels(graph, V, 0);
  
    return 0;
}


Java
// Java Program to determine level of each node 
// and print level 
import java.util.*;
  
class GFG
{
      
// function to determine level of each node starting 
// from x using BFS 
static void printLevels(Vector> graph, int V, int x) 
{ 
    // array to store level of each node 
    int level[] = new int[V]; 
    boolean marked[] = new boolean[V]; 
  
    // create a queue 
    Queue que = new LinkedList(); 
  
    // enqueue element x 
    que.add(x); 
  
    // initialize level of source node to 0 
    level[x] = 0; 
  
    // marked it as visited 
    marked[x] = true; 
  
    // do until queue is empty 
    while (que.size() > 0) 
    { 
  
        // get the first element of queue 
        x = que.peek(); 
  
        // dequeue element 
        que.remove(); 
  
        // traverse neighbors of node x 
        for (int i = 0; i < graph.get(x).size(); i++) 
        { 
            // b is neighbor of node x 
            int b = graph.get(x).get(i); 
  
            // if b is not marked already 
            if (!marked[b])
            { 
  
                // enqueue b in queue 
                que.add(b); 
  
                // level of b is level of x + 1 
                level[b] = level[x] + 1; 
  
                // mark b 
                marked[b] = true; 
            } 
        } 
    } 
  
    // display all nodes and their levels 
    System.out.println( "Nodes"
                        + " "
                        + "Level"); 
    for (int i = 0; i < V; i++) 
        System.out.println(" " + i +" --> " + level[i] ); 
} 
  
// Driver Code 
public static void main(String args[])
{ 
    // adjacency graph for tree 
    int V = 8; 
    Vector> graph=new Vector>(); 
      
    for(int i = 0; i < V + 1; i++)
    graph.add(new Vector());
  
    graph.get(0).add(1); 
    graph.get(0).add(2); 
    graph.get(1).add(3); 
    graph.get(1).add(4); 
    graph.get(1).add(5); 
    graph.get(2).add(5); 
    graph.get(2).add(6); 
    graph.get(6).add(7); 
  
    // call levels function with source as 0 
    printLevels(graph, V, 0); 
} 
} 
  
// This code is contributed by Arnab Kundu


Python3
# Python3 Program to determine level 
# of each node and print level 
import queue 
  
# function to determine level of 
# each node starting from x using BFS 
def printLevels(graph, V, x):
      
    # array to store level of each node 
    level = [None] * V 
    marked = [False] * V 
  
    # create a queue 
    que = queue.Queue()
  
    # enqueue element x 
    que.put(x) 
  
    # initialize level of source 
    # node to 0 
    level[x] = 0
  
    # marked it as visited 
    marked[x] = True
  
    # do until queue is empty 
    while (not que.empty()):
  
        # get the first element of queue 
        x = que.get() 
  
        # traverse neighbors of node x
        for i in range(len(graph[x])):
              
            # b is neighbor of node x 
            b = graph[x][i] 
  
            # if b is not marked already 
            if (not marked[b]): 
  
                # enqueue b in queue 
                que.put(b) 
  
                # level of b is level of x + 1 
                level[b] = level[x] + 1
  
                # mark b 
                marked[b] = True
  
    # display all nodes and their levels 
    print("Nodes", " ", "Level")
    for i in range(V):
        print(" ",i,  " --> ", level[i])
  
# Driver Code 
if __name__ == '__main__':
  
    # adjacency graph for tree 
    V = 8
    graph = [[] for i in range(V)]
  
    graph[0].append(1) 
    graph[0].append(2) 
    graph[1].append(3) 
    graph[1].append(4) 
    graph[1].append(5) 
    graph[2].append(5) 
    graph[2].append(6) 
    graph[6].append(7) 
  
    # call levels function with source as 0 
    printLevels(graph, V, 0)
  
# This code is contributed by PranchalK


C#
// C# Program to determine level of each node 
// and print level 
using System;
using System.Collections.Generic;
  
class GFG
{
      
// function to determine level of each node starting 
// from x using BFS 
static void printLevels(List> graph, 
                                  int V, int x) 
{ 
    // array to store level of each node 
    int []level = new int[V]; 
    Boolean []marked = new Boolean[V]; 
  
    // create a queue 
    Queue que = new Queue(); 
  
    // enqueue element x 
    que.Enqueue(x); 
  
    // initialize level of source node to 0 
    level[x] = 0; 
  
    // marked it as visited 
    marked[x] = true; 
  
    // do until queue is empty 
    while (que.Count > 0) 
    { 
  
        // get the first element of queue 
        x = que.Peek(); 
  
        // dequeue element 
        que.Dequeue(); 
  
        // traverse neighbors of node x 
        for (int i = 0; i < graph[x].Count; i++) 
        { 
            // b is neighbor of node x 
            int b = graph[x][i]; 
  
            // if b is not marked already 
            if (!marked[b])
            { 
  
                // enqueue b in queue 
                que.Enqueue(b); 
  
                // level of b is level of x + 1 
                level[b] = level[x] + 1; 
  
                // mark b 
                marked[b] = true; 
            } 
        } 
    } 
  
    // display all nodes and their levels 
    Console.WriteLine("Nodes" + " " + "Level"); 
    for (int i = 0; i < V; i++) 
        Console.WriteLine(" " + i +" --> " + level[i]); 
} 
  
// Driver Code 
public static void Main(String []args)
{ 
    // adjacency graph for tree 
    int V = 8; 
    List> graph = new List>(); 
      
    for(int i = 0; i < V + 1; i++)
        graph.Add(new List());
  
    graph[0].Add(1); 
    graph[0].Add(2); 
    graph[1].Add(3); 
    graph[1].Add(4); 
    graph[1].Add(5); 
    graph[2].Add(5); 
    graph[2].Add(6); 
    graph[6].Add(7); 
  
    // call levels function with source as 0 
    printLevels(graph, V, 0); 
} 
}
  
// This code is contributed by Princi Singh


Javascript


输出:

Nodes    Level
 0   -->   0
 1   -->   1
 2   -->   1
 3   -->   2
 4   -->   2
 5   -->   2
 6   -->   2
 7   -->   3

复杂性分析:

  • 时间复杂度: O(n)。
    在 BFS 遍历中,每个节点只被访问一次,因此时间复杂度为 O(n)。
  • 空间复杂度: O(n)。
    需要空间来将节点存储在队列中。