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.
I suggest looking at Minimum Spanning Tree algorithms like Prim's algorithm.
The networkx module will do this for you - see the documentation.
Related
I need to implement in python a data structure that each node in the data structure represents a rectangle on a plane.
the operation that I need from the data structure is:
1) split a node, that split a rectangle into 4 rectangles with the same
size(in the end I suppose to get something like from A to B in this example)
2) get all neighbor rectangles(for some computation)
Up to now, I thought about two options both of them not optimal, the first one is to use some kind of octree/quadtree which make the splitting very easy but I'm not sure about finding all the neighbor rectangles. the second is a Graph which enables me to find the neighbors very easy but makes it difficult to split a node.
I didn't succeed to think about an elegant solution for doing both things, and I will appreciate suggestions, even better if they are implemented in a python library.
I'm trying to model computer networks in Python using Networkx with the built-in Barabasi-Albert graph generator. My understanding is that this better represents a real network.
I want to randomly select a node as a entry/exist point for an attacker and visualize it with matplotlib. However, this "random" node can be place pretty much anywhere in the layout (I'm currently using the spring layout). This looks a little weird sometimes as the selected node could be right in the middle of the visualization, as if the entry/exit node is from within the network vs the network edge.
While a case could be made that this could represent insider threats, that's not something I would like to show at this time.
Is there an easy way I can position the node so that it's on the "exterior" of the graph? Or perhaps "pick" one of the nodes on the "exterior" as this entry/exit?
Is it possible to position nodes in a networkx graph so that nodes sharing a certain (single) attribute are clustered near each other?
For example, if the nodes represent people and each has an attribute 'age', how can I make it so that people of the same age are near each other when I draw the graph? Is this possible?
You can specify the x,y coordinates of each node. So if you have some idea on how you want it to look it can be programmed. You could try a spring layout but this isn't going to be hit or miss, it's going to be more misses. The way to attempt it is by connecting the nodes of the same data to each other. (people of the same age have one or more edges between them)
The only way I see this working well with large amounts of data is using a tool called Gephi to manipulate by hand based on node data etc... it's like a photoshop of network graphs.
I would suggest yet another approach. Create an extra attribute for your nodes that corresponds to a range of values of the attribute you want to use for the grouping. For example, if your attribute is age, then create ranges 18-30, 31-40, etc. Save the result in GraphML format and load the network with NodeXL (which is not free anymore but you could buy it for a small fee)
In NodeXL you can group the nodes by some attribute and it lays out the different groups so that nodes belonging to the same group are laid out close to each other. You can also choose how nodes in a group are laid out, from a list of layout options)
I basically have already put Nodes with a specific location and power transmitted from each in a 50m x 50m room and one of the nodes being the Server node that I want every node to connect to in both ways(Directions).
Each node has a circle radius that represents it's coverage around it.
I want to create an algorithm to place extra nodes to the room so that I can obtain a 1 Strongly connected component between all the already existing nodes and the added extra nodes.
I've searched the spanning tree algorithm and new the implementation of them in python but I don't yet know an algorithm or a pseudo code that I can implement to achieve that.
Does anyone know an algorithm or a name of one or anything that can help me implement it?
Thanks in advance
I'm trying to write a script which will let me mangle (edit, cut, change) some big network files from a command line interface. One of the things I'm trying to do is isolate a subnetwork from a larger network based on searching for matches in node labels.
So basically I'd have a networkx graph with maybe 7000 nodes and corresponding edges with various labels. Then I'd match a string, eg "Smith" to the nodes. I'd get a match of maybe 30 nodes (label:"John Smith", label:"Peter Smith", etc). I'd then like to make a new networkx network containing those 30 nodes, and the edges they have, and the nodes those edges connect to, up to a depth of n, or optionally until all the nodes and edges are found.
My current code is rubbish, so maybe I'll try to write some pseudocode:
for node in networkx_network:
if searched_string in node:
new_network.add(node.subnetwork(depth=n))
I've spent days googling for a solution, and maybe subgraph, or neighbors, or connected_components is the right thing to do, but I can't wrap my head around how to do it.
single_source_shortest_path has an optional cutoff argument. Including it you can tell networkx to basically find paths to nodes within a certain distance of a given node. It's a bit of overkill because there's a lot of other information in those paths you don't need. If you then just take the keys of the resulting set of paths, you have all nodes reachable within that distance, and networkx has ways to find the graphs containing all of these nodes and the edges between them.
By looking at the source code for this, and removing the effort taken to track the actual paths, you can make it much more efficient if needed. But as it stands, the following works:
import networkx as nx
G=nx.fast_gnp_random_graph(100000,0.00002) #sample graph.
base = range(3) #arbitrarily choose to start from nodes 0, 1, and 2
depth = 3 #look for those within length 3.
foundset = {key for source in base for key in nx.single_source_shortest_path(G,source,cutoff=depth).keys()}
H=G.subgraph(foundset)
nx.draw_networkx(H)
import pylab as py
py.savefig('tmp.png')
try snowball sampling?
so for the set of nodes you have searched that contains your keyword.
look for all their neighbors, add then to the set.
look for all the neighbors' neighbors, add the new ones to the set.
iterate this process for n times.
at the end you will get a set of all the nodes you want, then use the subgraph function to get a subgraph of all the nodes in your final set.
this may not be the most efficient solution but should work.