Place a "root" node in an undirected graph - python

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?

Related

Extending/scaling graph network based on independent nodes using NetworkX

I have two separate lists of nodes and stations. Each list consists of Node (or station) ID, x-coordinate, and y-coordinate. I pick up a random set of nodes and connect them to generate a graph network using NetworkX based on a certain distance between them. I select a random set of stations. I have shared the data and code at Data and Code
Now, I want to extend the graph network in a way that each edge should be within a certain distance from the station. I want to keep the shape (topology) of the initial network as it is but want to zoom (or scale/extend) the network to make it close to the stations. I am not sure how can I extend the graph to meet the aforementioned conditions/constraints. The stations can be regarded as forming an overlay network and the base network is being extended based on this overlay network. Here is a figure to show the desired network. Any help in this regard is very much appreciated.
Network Diagram

How do I let a user delete a node or edge in a PyVis network graph?

Our network graph is noisy and needs to be understood, broken up in to separate clusters, and generally cleaned up. How do people do that is my general question and my specific question regards simply deleting an object.
I'd like to design a mini-app to hand a PyVis network graph to a user and let them delete ( drop, remove ) edges or nodes somehow, preferably with a clean button or keyboard-shortcut to affect the currently selected objects.
I can't find in the PyVis documentation a command or example to drop/delete/remove a node or edge. I can't find any question let alone answer on StackOverflow tagged [pyvis] for how to do this. ( I can manage lists and queues etc in Python, this is about altering a visualized graph in real-time without having to secretly rebuild the whole thing.
Am I missing something obvious? Didn't anyone else ever want to do this?
But, I can't find any documentation on how to delete an existing node or edge from a visualized network. I'm looking at having to capture input, figure out the X, Y coordinates and current list of objects, back way up and remove what I want gone from the lists, and regenerating the whole display. Seriously?
That's right dropping nodes and edges doesn't exist in pyvis. I used Networkx for removing nodes and edges. This package is in my opinion better suited for playing around with the graph, although it contains minimal support for visualizations.
Pyvis has a from_nx function for importing it from Networkx. Since you want to do it in realtime however, I guess the problem is that there is an absence of this feature within pyvis. In a pyvis approach you could destroy the node object (part of network). If you want to do it in the interface by clicking I suggest combining it with networkx as it can help you check conditions and feed these node ids.
I hope that my answers helps you. I also posted some code below for the people interested in converting networkx to pyvis.
import networkx as nx
from pyvis.network import Network
foo = nx.DiGraph()
foo.add_nodes_from(list_with_node_ids)
foo.add_edge(child_node_id), parent_node_id)
foo.remove_nodes_from(list_with_nodes_to_remove)
bar = Network('500px', '500px'
Network.from_nx(bar, foo)
bar.show('bar.html')

Positioning networkx nodes by shared node attributes

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)

An algorithm to add nodes in a wireless network to obtain connected coverage

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

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.
I suggest looking at Minimum Spanning Tree algorithms like Prim's algorithm.
The networkx module will do this for you - see the documentation.

Categories