About the list of all nodes in graph(python) - python

I meet this question when I read about the python script of SCI2(Science of Science)software. Maybe what I want to ask is a simple python question.
I will show the SCI2 script first in the picture below. After that,I will write some codes. My question is why the codes in the picture can get list of all nodes by slicing method [:] but I can't.
codes from SCI2 python script Maybe the picture can not be shown, I write the important codes here. (the language is python)
# make a copy of the list of all nodes
nodesbynumworks = g.nodes[:]
What I wirte is:
import networkx as nx
g =nx.Graph()
g.add_node(1,size=11)
g.add_node(2,size=12)
a = g.nodes[:]
And this is my result:
my result
I just want to get the list of all nodes so that I can take some nodes from this list. And maybe I can change some attributes of some nodes.But I can't do that now. What I can think about is the reason that python2 and python3 are different in some place. I know this a a very pale explanation.
Hope someone can help me.

If you want the list of keys, then try:
g.nodes().keys()
If you want the list of values, then try:
g.nodes().values()

For SCI2, it appears to me that they have made a choice about how to represent graphs. For them graph.nodes is a list (or perhaps it's a numpy array or something similar). Thus graph.nodes[:] is a perfectly well-defined command.
You're using networkx for your graphs. For networkx graph.nodes is something different. In earlier versions of networkx, it is a function that returns a list of nodes, so graph.nodes()[:] will do what you want, but graph.nodes[:] doesn't make since because you're asking for a slice of the function, rather than a slice of the list it returns. In later versions it is a NodeView. I think your code might work in this version. If not, then I'm fairly confident that graph.nodes()[:] still works.
[I don't have the newer version on the computer I'm using right now, so I'm not 100% sure.]

Thank you for everyone.I have used a method to know something.
I used print type(g)and print type(g.nodes)and found something like the picture.
the type of g and g.nodes
So this is created by Jython which combine python and Java and it is not an object of networkx.
Okay,now I can convince myself.
Thanks for everyone again!

Related

Trying to find sets of nodes that disconnects graph, with networkx in python

So i'm trying to solve: "Find a graph algorithm that will identify set(s) of nodes that, if removed, causes the graph to shatter into at least two connected components. Use it to identify such set(s) for your network."
I found this function here: https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.algorithms.connectivity.kcutsets.all_node_cuts.html which looks exactly like what i'm looking for.
So i try to run this:
pg_all_node_cuts = len(nx.all_node_cuts(pg))
Problem is, this takes forever to run. I haven't been able to finish running this, after running for maybe 6 hours.
So im wondering, does anyone have a alternative python library that is faster or something else in networkx that helps my problem?, or am I misunderstanding what I should find?
Thanks
pg_all_node_cuts = nx.all_node_cuts(pg)
for b in pg_all_node_cuts:
print(b)

matplotlib to visualize linked lists and decisions trees

are there any tools or examples for how to visualize things like linked lists and decisions trees using matplotlib?
I ask because I wrote a linked list type of class (each node can have multiple inputs/outputs, and there's a class variable that stores node names), and want to visualize it. Unfortunately, my computer at work is locked down so much that I can't download other packages, so I've got to use whatever is on hand- which is matplotlib
I've started reading it, and if I do it by hand, I can probably make something that visualizes one-directional linked lists (just give it the root node, and plop down a square with text for each operation). But if there's branching, or multiple inputs into a node things get a bit more complicated- for example is it possible to expand a figure after creating it?
Yes, you can use networkx library and the draw_networkx method. There are plenty of examples on Stack Overflow. Here is one example: https://stackoverflow.com/a/52683100/6361531

Numpy reapeat array attached to array?! what is the syntax here?

So, I currently need to understand the following code properly:
J = p['M'].repeat(p['N'],1).T
p is a dictionary in which the entry under key M is simply an array, the T transposes, that much is clear.
But, the only version I can find for the repeat function is syntax in the form of
numpy.repeat(array , repeats [,axis])
This leaves me wondering what the meaning of a syntax of type array.repeat(something) actually means and I can neither find an answer in my head or the internet for now. This is numpy though, isnt it? It is imported, without being tagged with an 'as' clause.
So currently am on a machine without a python/numpy shell installed to simply try it, so I thought I give this a shot: What is repeated how many times?
My first interpretation would be p['M'] is repeated p['N'] times along the first axis, then transposed, but every example specifying an axis I find uses something like axis=1.
Thanks a lot =)
There is another version of repeat in numpy: numpy.ndarray.repeat
Please see the documentation here
Hope this helps

Depth First Search in Python

I'm having problems in implementing search algorithms in Python. I need a generic depth first search code which can return the path from the start state to end state.
Start with this essay on implementing Graphs in Python and then understand that DFS may use a stack to pop in and pop out nodes of the graph. Having this idea may help you proceed with implementing DFS in Python. And if you have specific questions, please post here and folks are ready to help.
You can find a detailed implementation explanation at literateprograms.org.
(Actually, it's pretty much the first Google hit, trying that before posting questions on SO might help next time)
Use the adjacency list representation to implement the graph. Code to implement it is as follows. (this code is specific if you use Python to read a text file on graph and implement the adjecency list)
(Python 3.3)
def ajlist(nameofgraph,start,goal):
x=input("enter the name of the file you want to implement adjecency list: ")##file.txt##
text_file=open(x,"r")
nameofgraph={}##use a dictionary##
for line in text_file:
(vertex,val)=line.split()
if vertex not in nameofgraph:
nameofgraph[vertex]=set9[val])
else:
nameofgraph.get[key].add(val)
stack=[(stack,[start])]
while stack:
(vertex,path)=stack.pop()
for next in graph[vertex]-set(path):
if next==goal:
yield (path+[next])
else:
stack.append((next,path+[next]))
When you want to run this use this syntax list(DFS(nameofgraph,start,goal))
Above is the easiest way of doing DFS and find the path in graph. If you have implemented a graph in python then you do not need the (input) function. Then you have to do is take away the implementing adjecency list part and use the real traversal part.

Matlab function equivalent for Python (Flood Fill)

Quick question, I'm looking for a python function that performs the equivalent job that matlab's imfill.m does. I realize that python has openCV but I have been unable to get that to work properly and am trying to find a substitute for it. The part of imfill that I'm trying to replicate is the 'holes' part of it.
I have a mask that I've generated but I'm trying to fill in all regions that are surrounded by 'land' and leave only the water regions unfilled in.
If this isn't clear enough please let me know and I can try and be more specific. Thank you for your time.
I was able to find a function within scipy that performed similar to what imfill does. It's called binary_fill_holes and it can be found here for anyone that is having the same problem as myself.
Although I can't take full/any real credit for finding it since it was answered here to one of my other questions PIL Plus/imToolkit replacements by unutbu.

Categories

Resources