mugetsu
mugetsu

Reputation: 4398

Creating a tree in python given nodes and their positions

I have an XML file which contains different nodes of data that I randomly generated. What I want to do is run through each node and create a tree out of it. My customized software uses the XML data to draw these nodes and their connections visually.

There is no criteria for which node connects to which; given 500 nodes, I want the ability to generate a tree with a decently complex breadth and depth.

I'm coding this in python using a customized library that draws diagrams using JgraphX so there's no point for me to show the exact code. But assume that I have the following 3 functions:

getXY_coord(a), get the XY coord of the node on the diagram
connectNodes(a,b), connects node a with b
getAllNodes(), returns list of all nodes on diagram

How would I approach making this complex tree? It doesn't even have to be visually organized, a node can connect to another node on the opposite side of the diagram, as long as the connections themselves are complex.

The only thing I was able to pull off was to randomize the list of nodes and connect the nodes adjacent in the list. This doesn't get what I want however.

Upvotes: 1

Views: 539

Answers (1)

Hugh Bothwell
Hugh Bothwell

Reputation: 56634

I suggest looking at Minimum Spanning Tree algorithms like Prim's algorithm.

The networkx module will do this for you - see the documentation.

Upvotes: 1

Related Questions