0

I started using python package 'networkx' today and I'm struggling to understand how you read input data from external files.

The examples shown in the documentation deal with small networks which can be read directly from the shell.

I have a legacy file which specifies a large electric network with this format:

'from_node' 'to_node' 'edge_name' 'edge_capacity' 'flow_cost' 

The next set of cards reads:

'node type' (source or sink), 'capacity', 'cost' 

I want to solve the max flow problem. How can I read such input data file?

1 Answer 1

1

You can read the edges using parse_edgelist:

In [1]: import networkx as nx In [2]: lines = ["from to name capacity cost", "from1 to1 name1 capacity1 cost1"] In [3]: G = nx.parse_edgelist(lines, data = (('name', str), ('capacity', str), ('cost', str))) In [5]: G.edges(data=True) Out[5]: [('from1', 'to1', {'capacity': 'capacity1', 'cost': 'cost1', 'name': 'name1'}), ('to', 'from', {'capacity': 'capacity', 'cost': 'cost', 'name': 'name'})] 

For the nodes, you can probably just iterate over your text file and annotate the graph. The documentation provides quite a few methods for reading graphs:

http://networkx.github.io/documentation/latest/reference/readwrite.html

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Juniper. It looks like is what I need to start using 'networkx'.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.