给定一个具有N个节点和M个边的无向无环图,任务是找到该图所形成的森林中最大的树的大小。
A forest is a collection of disjoint trees. In other words, we can also say that forest is a collection of an acyclic graph which is not connected.
例子:
Input: N = 5, edges[][] = {{0, 1}, {0, 2}, {3, 4}}
Output: 3
Explanation:
There are 2 trees, each having size 3 and 2 respectively.
and
Hence the size of the largest tree is 3.
Input: N = 5, edges[][] = {{0, 1}, {0, 2}, {3, 4}, {0, 4}, {3, 5}}
Output: 6
方法:想法是首先计算每个林中可到达节点的数量。所以:
- 在每个节点上应用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是边数。