I have a Digraph constructed with networkx that has 37379 nodes and 61263 edges. I would like to extract for a node target, a subgraph that contain only the target node and the first nodes that are linked to it.
I tried the answers here and it only drawn me the target node. Have i missed something ? I checked that there were edges in my graph ( i just output as json and saw "source and target" items)
Please delete the question, it was a key type error as my keys are integers and tried to make a subgraph with string keys...
Related
I looking for a elegant way to find all nodes with a defined attribute. E.g. let's say I create a new network with two nodes
G.add_node('A', attr1='alpha')
G.add_node('B', attr1='beta')
Now, I would like to have a function that returns all nodes where the attribute "attr1" that matches "beta" something like
THX
Lazloo
Try
L = [node for node in G.nodes() if G.node[node]['attr1']=='beta']
to create a list (look at list comprehensions). You can also create other data types that contain all of these nodes.
Context
This is the first time I have to work with NetworkX so either I can't read correctly the documentation, or I simply do not use the right vocabulary.
Problem
I am working with a DiGraph, and I want to get a list of every nodes accessible starting from a specified node.
I thought of making a sub-graph containing the nodes I just described, and I would siply have to iterate over that specific sub-graph. Unfortunately, I didn't find a way to create automatically a sub-graph with the condition I mentioned.
It feels like an obvious feature. What am I missing ?
You are looking for the nx.descendants method:
descendants(G, source)
Return all nodes reachable from
(source) in G.
Parameters : G : NetworkX DiGraph
source : node in G
Returns : des : set()
The descendants of source in G
I am trying to apply brute-force method to find the shortest path between an origin and a destination node (OD pair). I create the network using networkX and call the permutations followed by the application of brute force. If all the nodes in the network are connected with all others, this is ok. But if some or many edges are not there, this method is not gonna work out.
To make it right, i should delete all the permuations which are containing the illegal edges.
For example if two permutation tuples are
[(1,2,3,4,5), (1,2,4,3,5)]
and in my network no edge exist between node 2 and 3, the first tuple in the mentioned list should be deleted.
First question: Is it an efficient way to first create permutations and then go in there and delete those containing illegal edges? if not what should I do?
Second question: If yes, my strategy is that I am first creating a list of tuples containing all illegal edges from networkx "G.has_edge(u,v)" command and then going into the permutations and looking if such an edge exist, delete that permutation and so on. Is it a good strategy? if no, what else do you suggest.
Thank you :)
Exact solution for general TSP is recognized as non-polynomial. Enumerating all permutations, being the most straight-forward approach, is valid despite of its O(n!) complexity. Refer to wikipedia page of TSP for more information.
As for your specific problem, generating valid permutation is possible using depth-first search over the graph.
Python-like pseudo code showing this algorithm is as following:
def dfs(vertex, visited):
if vertex == target_vertex:
visited.append(target_vertex)
if visited != graph.vertices:
return
do_it(visited) # visited is a valid permutation
for i in graph.vertices:
if not i in visited and graph.has_edge(vertex, i):
dfs(i, visited + [i])
dfs(start_vertex, [start_vertex])
I have this GraphML file that I've read into Networkx.
So I access all the nodes by:
g.nodes()
It gives me a list of strings. Say one of them is "123".
I then try to access a node as:
g["123"]
and it gives me a dictionary.
I then try to access the nodes using the nodes function as follows:
for n in g.nodes( data = True ):
print n
It then gives me a 2-tuple with the string node name as first element and a dictionary as a second element.
The thing is, it is a different dictionary from the first one. And it's confusing the heck out of me, so any help here is appreciated.
Are they supposed to be different? If so, why? If not, then what am I doing wrong? :)
I can post the actual data if it would help.
Have you considered reading the various pages of documentation?
nlist : list
A list of nodes. If data=True a list of two-tuples containing (node, node data dictionary).
and...
adj_dict : dictionary
The adjacency dictionary for nodes connected to n.
"Node data dictionary" and "adjacendy dictionary" are not the same thing.
I'd like to know the best way to read a disconected undirected graph using igraph for python. For instance, if I have the simple graph in which 0 is linked to 1 and 2 is a node not connected to any other. I couldn't get igraph to read it from a edgelist format(Graph.Read_Edgelist(...)), because every line must be an edge, so the following is not allowed:
0 1
2
I've been just wondering if adjacency matrix is my only/best option in this case (I could get it to work through this representation)? I'd rather a format in which I could understand the data by looking it (something really hard when it comes to matrix format).
Thanks in advance!
There's the LGL format which allows isolated vertices (see Graph.Read_LGL). The format looks like this:
# nodeID
nodeID2
nodeID3
# nodeID2
nodeID4
nodeID5
nodeID
# isolatedNode
# nodeID5
I think you get the basic idea; lines starting with a hash mark indicate that a new node is being defined. After this, the lines specify the neighbors of the node that has just been defined. If you need an isolated node, you just specify the node ID prepended by a hash mark in the line, then continue with the next node.
More information about the LGL format is to be found here.
Another fairly readable format that you might want to examine is the GML format which igraph also supports.