Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find length of the longest path in an n-ary tree in Python
In this problem, we need to find the longest path in an n-ary tree represented by an edge list. Each edge (u, v) indicates that u is the parent of v. The path length is calculated as 1 + number of nodes in the path.
For the given tree structure:
The longest path is [1, 4, 5, 7] with 4 nodes, so the path length is 1 + 4 = 5.
Algorithm Approach
We use a two-step BFS approach to find the diameter of the tree:
- Build an adjacency list representation of the tree
- Use BFS to find the farthest node from any starting point
- Use BFS again from that farthest node to find the actual diameter
Implementation
def solve(edges):
# Build adjacency list
graph = {}
for u, v in edges:
if u not in graph:
graph[u] = []
graph[u].append(v)
if v not in graph:
graph[v] = []
graph[v].append(u)
def bfs(start):
distances = {start: 1}
farthest = start
queue = [start]
for node in queue:
for neighbor in graph[node]:
if neighbor not in distances:
distances[neighbor] = distances[node] + 1
if distances[neighbor] > distances[farthest]:
farthest = neighbor
queue.append(neighbor)
return farthest, distances[farthest]
# Find any node to start BFS
if not graph:
return 0
start_node = next(iter(graph))
# First BFS to find one end of the diameter
farthest_node, _ = bfs(start_node)
# Second BFS from the farthest node to find diameter
_, diameter = bfs(farthest_node)
return diameter
# Test with the example
edges = [(1, 2), (1, 3), (1, 4), (4, 5), (5, 7), (1, 6), (4, 8)]
print(solve(edges))
5
How It Works
The algorithm works in two phases:
- First BFS: Start from any node and find the farthest node from it. This gives us one endpoint of the longest path.
- Second BFS: Start from the node found in step 1 and find the farthest node from it. The distance to this node is the diameter.
This approach works because in a tree, the longest path (diameter) will always be between two leaf nodes or nodes with minimal connections.
Time and Space Complexity
- Time Complexity: O(n) where n is the number of nodes, as we perform two BFS traversals
- Space Complexity: O(n) for storing the adjacency list and BFS queue
Conclusion
The two-BFS approach efficiently finds the longest path in an n-ary tree by first identifying one endpoint of the diameter, then measuring the distance to the opposite endpoint. This method guarantees finding the true diameter of the tree structure.
