2

I have the following dataset:

firm_id_1 firm_id_2
1         2
1         4
1         5
2         1
2         3
3         2
3         6
4         1
4         5
4         6
5         4
5         7
6         3 ....

I would like to graph the network of firm_id = 1. In other words, I want to see a graph that shows that firm_id = 1 is directly connected to 2, 4, 5, and indirectly connected to 3 via firm 2, connected to 6 via firm 4 and indirectly connected to 7 via firm 5. In other words I graph the shortest distance to each node (firm_id) starting from firm_id=1. There is 3000 nodes in my data and I know that firm 1 reaches all nodes in less than 9 vertices. How can I graph this in Python?

1
  • 1
    it is expected you show us some efforts, this is not 'do my work for free' kind of place. Please see stackoverflow.com/help I can only hint, that not bad idea would be to build dictionary of lists for this data. Or find a package for graph manipulation. Commented Mar 25, 2014 at 5:22

2 Answers 2

1

I would start with a library called NetworkX. I'm not sure I understand everything that you are looking for, but I think this should be close enough for you to modify it.

This program will load the data in from a text file graphdata.txt, split by whitespace, and add the pair as an edge.

It will then calculate the shortest paths to all nodes from 1, and then print if the distance is larger than 9... see the documentation for more details.

Lastly, it will render the graph using a spring layout to a file called mynetwork.png and to the screen.

Some optimization may / may not be needed for 3000 nodes.

Hope this helps!

import networkx as nx
import matplotlib.pyplot as plt

graph = nx.Graph()
with open('graphdata.txt') as f:
    for line in f:
        firm_id_1, firm_id_2 = line.split()
        graph.add_edge(firm_id_1, firm_id_2)

paths_from_1 = nx.shortest_path(graph, "1")
for path in paths_from_1:
    if len(paths_from_1[node]) > 9:
        print "Shortest path from 1 to", node, "is longer than 9"


pos = nx.spring_layout(graph, iterations=200)
nx.draw(graph, pos)
plt.savefig("mynetwork.png")
plt.show()
Sign up to request clarification or add additional context in comments.

1 Comment

fantastic. It seems to be what I want. The graphs is really messy because of the 3000 nodes but now I now know which graph layout I need. When I was fooling around with networkx I couldn't find the correct layout of the network that I was visualizing in my mind...it is a spring_layout. I'll fool around with the options, etc. Thank you
1

You can try python-graph package. I am not sure about its scalability, but you can do something like...

from pygraph.classes.digraph import digraph
from pygraph.algorithms.minmax import shortest_path

gr= digraph()

gr.add_nodes(range(1,num_nodes))

for i in range(num_edges):
    gr.add_edge((edge_start, edge_end))

# shortest path from the node 1 to all others
shortest_path(gr,1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.