📜  从 1 开始打印图的字典顺序最小的 BFS

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

从 1 开始打印图的字典顺序最小的 BFS

给定一个有N个顶点和M个边的连通图。任务是打印从 1 开始的图的字典顺序最小的 BFS 遍历。
注意:顶点从 1 到 N 编号。
例子:

Input: N = 5, M = 5 
       Edges: 
       1 4
       3 4
       5 4
       3 2
       1 5 
Output: 1 4 3 2 5 
Start from 1, go to 4, then to 3 and then to 2 and to 5. 

Input: N = 3, M = 2 
       Edges: 
       1 2 
       1 3 
Output: 1 2 3 

方法:我们可以使用优先级队列(最小堆)代替简单的队列,而不是在图上进行正常的 BFS 遍历。当访问节点时,将其相邻节点添加到优先级队列中。每次,我们访问一个新节点,它将是优先级队列中索引最小的节点。每次我们从 1 开始访问它们时打印节点。
下面是上述方法的实现:

CPP
// C++ program to print the lexcicographically
// smallest path starting from 1
 
#include 
using namespace std;
 
// Function to print the smallest lexicographically
// BFS path starting from 1
void printLexoSmall(vector adj[], int n)
{
    // Visited array
    bool vis[n + 1];
    memset(vis, 0, sizeof vis);
 
    // Minimum Heap
    priority_queue, greater > Q;
 
    // First one visited
    vis[1] = true;
    Q.push(1);
 
    // Iterate till all nodes are visited
    while (!Q.empty()) {
 
        // Get the top element
        int now = Q.top();
 
        // Pop the element
        Q.pop();
 
        // Print the current node
        cout << now << " ";
 
        // Find adjacent nodes
        for (auto p : adj[now]) {
 
            // If not visited
            if (!vis[p]) {
 
                // Push
                Q.push(p);
 
                // Mark as visited
                vis[p] = true;
            }
        }
    }
}
 
// Function to insert edges in the graph
void insertEdges(int u, int v, vector adj[])
{
    adj[u].push_back(v);
    adj[v].push_back(u);
}
 
// Driver Code
int main()
{
    int n = 5, m = 5;
    vector adj[n + 1];
 
    // Insert edges
    insertEdges(1, 4, adj);
    insertEdges(3, 4, adj);
    insertEdges(5, 4, adj);
    insertEdges(3, 2, adj);
    insertEdges(1, 5, adj);
 
    // Function call
    printLexoSmall(adj, n);
 
    return 0;
}


Java
// Java program to print the lexcicographically
// smallest path starting from 1
import java.util.*;
public class GFG
{
   
  // Function to print the smallest lexicographically
  // BFS path starting from 1
  static void printLexoSmall(Vector> adj, int n)
  {
    // Visited array
    boolean[] vis = new boolean[n + 1];
 
    // Minimum Heap
    Vector Q = new Vector();
 
    // First one visited
    vis[1] = true;
    Q.add(1);
 
    // Iterate till all nodes are visited
    while (Q.size() > 0) {
 
      // Get the top element
      int now = Q.get(0);
 
      // Pop the element
      Q.remove(0);
 
      // Print the current node
      System.out.print(now + " ");
 
      // Find adjacent nodes
      for(int p : adj.get(now)) {
 
        // If not visited
        if (!vis[p]) {
 
          // Push
          Q.add(p);
          Collections.sort(Q);
 
          // Mark as visited
          vis[p] = true;
        }
      }
    }
  }
 
  // Function to insert edges in the graph
  static void insertEdges(int u, int v, Vector> adj)
  {
    adj.get(u).add(v);
    adj.get(v).add(u);
  }
 
  // Driver code
  public static void main(String[] args) {
    int n = 5;
    Vector> adj = new Vector>();    
    for(int i = 0; i < n + 1; i++)
    {
      adj.add(new Vector());
    }
 
    // Insert edges
    insertEdges(1, 4, adj);
    insertEdges(3, 4, adj);
    insertEdges(5, 4, adj);
    insertEdges(3, 2, adj);
    insertEdges(1, 5, adj);
 
    // Function call
    printLexoSmall(adj, n);
  }
}
 
// This code is contributed by divyeshrabadiya07.


Python3
# Python program to print the lexcicographically
# smallest path starting from 1
 
# Function to print the smallest lexicographically
# BFS path starting from 1
def printLexoSmall(adj, n):
     
    # Visited array
    vis = [False for i in range(n + 1)]
     
    # Minimum Heap
    Q = []
     
    # First one visited
    vis[1] = True;
    Q.append(1)
     
    # Iterate till all nodes are visited
    while(len(Q) != 0):
         
        # Get the top element
        now = Q[0]
         
        # Pop the element
        Q.pop(0)
         
        # Print the current node
        print(now, end = " ")
         
        # Find adjacent nodes
        for p in adj[now]:
             
            # If not visited
            if(not vis[p]):
                 
                # Push
                Q.append(p)
                Q.sort()
                 
                # Mark as visited
                vis[p] = True
 
# Function to insert edges in the graph
def insertEdges(u, v, adj):
    adj[u].append(v)
    adj[v].append(u)
 
# Driver code
n = 5
m = 5
adj = [[] for i in range(n + 1)]
 
# Insert edges
insertEdges(1, 4, adj)
insertEdges(3, 4, adj)
insertEdges(5, 4, adj)
insertEdges(3, 2, adj)
insertEdges(1, 5, adj)
 
# Function call
printLexoSmall(adj, n)
 
# This code is contributed by avanitrachhadiya2155


C#
// C# program to print the lexcicographically
// smallest path starting from 1
using System;
using System.Collections.Generic;
class GFG {
     
    // Function to print the smallest lexicographically
    // BFS path starting from 1
    static void printLexoSmall(List> adj, int n)
    {
        // Visited array
        bool[] vis = new bool[n + 1];
       
        // Minimum Heap
        List Q = new List();
       
        // First one visited
        vis[1] = true;
        Q.Add(1);
       
        // Iterate till all nodes are visited
        while (Q.Count > 0) {
       
            // Get the top element
            int now = Q[0];
       
            // Pop the element
            Q.RemoveAt(0);
       
            // Print the current node
            Console.Write(now + " ");
       
            // Find adjacent nodes
            foreach (int p in adj[now]) {
       
                // If not visited
                if (!vis[p]) {
       
                    // Push
                    Q.Add(p);
                    Q.Sort();
       
                    // Mark as visited
                    vis[p] = true;
                }
            }
        }
    }
     
    // Function to insert edges in the graph
    static void insertEdges(int u, int v, List> adj)
    {
        adj[u].Add(v);
        adj[v].Add(u);
    }
 
  // Driver code
  static void Main()
  {
    int n = 5;
    List> adj = new List>();    
    for(int i = 0; i < n + 1; i++)
    {
        adj.Add(new List());
    }
   
    // Insert edges
    insertEdges(1, 4, adj);
    insertEdges(3, 4, adj);
    insertEdges(5, 4, adj);
    insertEdges(3, 2, adj);
    insertEdges(1, 5, adj);
   
    // Function call
    printLexoSmall(adj, n);
  }
}
 
// This code is contributed by divyesh072019.


Javascript


输出:
1 4 3 2 5