# Critical Path

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](https://dtucker.xyz/projects/networks/criticalpath.html).

```python
# 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'])
```

```python
# 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.

[![network with critical path](https://cdn.hashnode.com/res/hashnode/image/upload/v1717796143565/09763226-642f-434a-8548-67393ccfc402.png align="center")](https://dtucker.xyz/projects/networks/criticalpath.html)
