📜  给定图形成的森林中最大树的大小

📅  最后修改于: 2021-10-25 04:40:06             🧑  作者: Mango

给定一个具有N 个节点和M 个边的无向无环图,任务是找到该图形成的森林中最大树的大小。

例子:

方法:这个想法是首先计算每个森林中可达节点的数量。所以:

  • 在每个节点上应用 DFS 并获得该节点形成的树的大小,并检查是否从一个源访问了每个连接的节点。
  • 如果当前树的大小大于答案,则将答案更新为当前树的大小。
  • 如果尚未访问某些节点集,则再次执行 DFS 遍历。
  • 最后,当所有节点都被访问时,所有答案的最大值就是最终答案。

下面是上述方法的实现:

C++
0
 /   \
1     2


Java
3
 \
  4


Python3
// C++ program to find the size
// of the largest tree in the forest
 
#include 
using namespace std;
 
// A utility function to add
// an edge in an undirected graph.
void addEdge(vector adj[],
             int u, int v)
{
    adj[u].push_back(v);
    adj[v].push_back(u);
}
 
// A utility function to perform DFS of a
// graph recursively from a given vertex u
// and returns the size of the tree formed by u
int DFSUtil(int u, vector adj[],
            vector& visited)
{
    visited[u] = true;
    int sz = 1;
 
    // Iterating through all the nodes
    for (int i = 0; i < adj[u].size(); i++)
        if (visited[adj[u][i]] == false)
 
            // Perform DFS if the node is
            // not yet visited
            sz += DFSUtil(
                adj[u][i], adj, visited);
    return sz;
}
 
// Function to return the  size of the
// largest tree in the forest given as
// the adjacency list
int largestTree(vector adj[], int V)
{
    vector visited(V, false);
    int answer = 0;
 
    // Iterating through all the vertices
    for (int u = 0; u < V; u++) {
        if (visited[u] == false) {
 
            // Find the answer
            answer
                = max(answer,
                      DFSUtil(u, adj, visited));
        }
    }
    return answer;
}
 
// Driver code
int main()
{
    int V = 5;
    vector adj[V];
    addEdge(adj, 0, 1);
    addEdge(adj, 0, 2);
    addEdge(adj, 3, 4);
    cout << largestTree(adj, V);
    return 0;
}


C#
// Java program to find the size
// of the largest tree in the forest
import java.util.*;
class GFG{
 
// A utility function to add
// an edge in an undirected graph.
static void addEdge(Vector adj[],
                    int u, int v)
{
  adj[u].add(v);
  adj[v].add(u);
}
 
// A utility function to perform DFS of a
// graph recursively from a given vertex u
// and returns the size of the tree formed by u
static int DFSUtil(int u, Vector adj[],
                   Vector visited)
{
  visited.add(u, true);
  int sz = 1;
 
  // Iterating through all the nodes
  for (int i = 0; i < adj[u].size(); i++)
    if (visited.get(adj[u].get(i)) == false)
 
      // Perform DFS if the node is
      // not yet visited
      sz += DFSUtil(adj[u].get(i),
                    adj, visited);
  return sz;
}
 
// Function to return the  size of the
// largest tree in the forest given as
// the adjacency list
static int largestTree(Vector adj[],
                       int V)
{
  Vector visited = new Vector<>();
  for(int i = 0; i < V; i++)
  {
    visited.add(false);
  }
  int answer = 0;
 
  // Iterating through all the vertices
  for (int u = 0; u < V; u++)
  {
    if (visited.get(u) == false)
    {
      // Find the answer
      answer = Math.max(answer,
               DFSUtil(u, adj, visited));
    }
  }
  return answer;
}
 
// Driver code
public static void main(String[] args)
{
  int V = 5;
  Vector adj[] = new Vector[V];
  for (int i = 0; i < adj.length; i++)
    adj[i] = new Vector();
  addEdge(adj, 0, 1);
  addEdge(adj, 0, 2);
  addEdge(adj, 3, 4);
  System.out.print(largestTree(adj, V));
}
}
 
// This code is contributed by Rajput-Ji


输出:
# Python3 program to find the size
# of the largest tree in the forest
  
# A utility function to add
# an edge in an undirected graph.
def addEdge(adj, u, v):
 
    adj[u].append(v)
    adj[v].append(u)
 
# A utility function to perform DFS of a
# graph recursively from a given vertex u
# and returns the size of the tree formed by u
def DFSUtil(u, adj, visited):
     
    visited[u] = True
    sz = 1
  
    # Iterating through all the nodes
    for i in range(0, len(adj[u])):
        if (visited[adj[u][i]] == False):
  
            # Perform DFS if the node is
            # not yet visited
            sz += DFSUtil(adj[u][i], adj, visited)
             
    return sz
 
# Function to return the  size of the
# largest tree in the forest given as
# the adjacency list
def largestTree(adj, V):
     
    visited = [False for i in range(V)]
     
    answer = 0
  
    # Iterating through all the vertices
    for u in range(V):
        if (visited[u] == False):
  
            # Find the answer
            answer = max(answer,DFSUtil(
                u, adj, visited))
         
    return answer
 
# Driver code
if __name__=="__main__":
 
    V = 5
     
    adj = [[] for i in range(V)]
     
    addEdge(adj, 0, 1)
    addEdge(adj, 0, 2)
    addEdge(adj, 3, 4)
     
    print(largestTree(adj, V))
     
# This code is contributed by rutvik_56

时间复杂度: O(V + E) ,其中 V 是顶点数,E 是边数。

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程