Using the networkx library I build and find the critical path of a network from a data frame.
The network graph created below can be found here.
# Create a NetworkX DiGraph
G = nx.DiGraph()
# Iterate through the DataFrame and add edges to the graph
for index, row in df.iterrows():
G.add_edge(row['predecessor'], row['successor'], duration=row['duration'])
# Find the critical path
critical_path = nx.dag_longest_path(G, weight='duration')
print(f'Critical Path: {critical_path}')
Critical Path: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
Total Duration of Critical Path: 35
Networkx makes it relatively simple to find the path. I manually colored the nodes to highlight the identified critical path.