networkx

How to split tuple with parentheses in Python?

会有一股神秘感。 提交于 2020-02-06 06:29:05
问题 I have built-in tuple which looks like (u,v) . They are generated by Networkx and they show links in a graph. I make a list out of the called link_list . I have to split the tuple such that the outcome would be: u , v I tried divmod but it doesn't give the right answer. for link in link_list: u,v = divmod(*link) print u,v 回答1: Simple: for link in link_list: u, v = link print u, v It's called sequence unpacking. 回答2: you can get the tuple into individual variables in the for statement as

networkx(二)

夙愿已清 提交于 2020-02-06 00:34:56
Graph Creation 有三种方式来创建networkx的图形对象: 1.图生成器-创建网络拓扑的标准算法 2.从源文件中导入数据 3.自己添加边和节点 import networkx as nx G = nx . Graph ( ) G . add_edge ( 1 , 2 ) #默认边的值为1 G . add_edge ( 2 , 3 , weight = 0.9 ) #指定边的值为0.9 边的属性可以是任何东西 import math G . add_edge ( 'y' , 'x' , function = math . cos ) G . add_node ( math . cos ) #节点可以是任何可hash的值 你可以一次性添加很多条边: elist = [ ( 1 , 2 ) , ( 2 , 3 ) , ( 1 , 4 ) , ( 4 , 2 ) ] G . add_edges_from ( elist ) #通过定义好的边的list来添加边 elist = [ ( 'a' , 'b' , 5.0 ) , ( 'b' , 'c' , 3.0 ) , ( 'a' , 'c' , 1.0 ) , ( 'c' , 'd' , 7.3 ) ] #通过list定义边和边的权值 G . add_weighted_edges_from ( elist )

is this betweenness calculation correct?

若如初见. 提交于 2020-02-02 03:05:33
问题 I try to calculate betweenness for all nodes for the path from 2 to 6 in this simple graph. G=nx.Graph() edge=[(1,5),(2,5),(3,5),(4,5),(4,6),(5,7),(7,6)] G.add_edges_from(edge) btw=nx.betweenness_centrality_subset(G,[2],[6]) However the result is: {1: 0.0, 5: 0.5, 2: 0.0, 3: 0.0, 4: 0.25, 6: 0.0, 7: 0.25} I was wondering why the betweenness for node 5 is 0.5 while it should be 1 since the number of total shortest path is 2 and both of them include 5 and node 4 and 7 should be 0.5 回答1: It

is this betweenness calculation correct?

冷暖自知 提交于 2020-02-02 03:05:22
问题 I try to calculate betweenness for all nodes for the path from 2 to 6 in this simple graph. G=nx.Graph() edge=[(1,5),(2,5),(3,5),(4,5),(4,6),(5,7),(7,6)] G.add_edges_from(edge) btw=nx.betweenness_centrality_subset(G,[2],[6]) However the result is: {1: 0.0, 5: 0.5, 2: 0.0, 3: 0.0, 4: 0.25, 6: 0.0, 7: 0.25} I was wondering why the betweenness for node 5 is 0.5 while it should be 1 since the number of total shortest path is 2 and both of them include 5 and node 4 and 7 should be 0.5 回答1: It

How can I get the number of nodes of a Neo4j graph database from Python?

血红的双手。 提交于 2020-02-01 11:27:06
问题 I'm trying to get the number of nodes of a Neo4j graph database using Python, but I don't find any method or property to do that. Does anybody how can I get this information? Other Python packages like NetworkX has a method to get this information. >>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc >>> G.add_path([0,1,2]) >>> len(G) 3 回答1: Update: Since I first wrote this, the answer has changed. The database now keeps exact counts of total nodes, as well as counts by label.

Get node list from random walk in networkX

自作多情 提交于 2020-01-31 18:14:28
问题 I am new to networkX. I created a graph as follows: G = nx.read_edgelist(filename, nodetype=int, delimiter=',', data=(('weight', float),)) where the edges are positive, but do not sum up to one. Is there a built-in method that makes a random walk of k steps from a certain node and return the node list? If not, what is the easiest way of doing it (nodes can repeat)? Pseudo-code: node = random res = [node] for i in range(0, k) read edge weights from this node an edge from this node has

Get node list from random walk in networkX

痞子三分冷 提交于 2020-01-31 18:13:10
问题 I am new to networkX. I created a graph as follows: G = nx.read_edgelist(filename, nodetype=int, delimiter=',', data=(('weight', float),)) where the edges are positive, but do not sum up to one. Is there a built-in method that makes a random walk of k steps from a certain node and return the node list? If not, what is the easiest way of doing it (nodes can repeat)? Pseudo-code: node = random res = [node] for i in range(0, k) read edge weights from this node an edge from this node has

How do I enumerate all *maximal* cliques in a graph using networkx + python?

纵然是瞬间 提交于 2020-01-30 12:56:48
问题 If you look at https://en.wikipedia.org/wiki/Clique_problem, you'll notice there is a distinction between cliques and maximal cliques. A maximal clique is contained in no other clique but itself. So I want those clique, but networkx seems to only provide: networkx.algorithms.clique.enumerate_all_cliques(G) So I tried a simple for loop filtering mechanism (see below). def filter_cliques(self, cliques): # TODO: why do we need this? Post in forum... res = [] for C in cliques: C = set(C) for D in

Errors caused by write statements when running f2py-wrapped, PETSc-based fortran code in OpenMDAO

无人久伴 提交于 2020-01-25 22:05:51
问题 I am using f2py to wrap my PETSc-based fortran analysis code for use in OpenMDAO (as suggested in this post). Rather than use f2py directly, I'm instead using it to generate the relevant .c, .pyc, etc. files and then linking them myself using mpif90. In a simple python environment, I can import my .so and run the code without any problems: >>> import module_name >>> module_name.execute() expected code output... However, when trying to do the same thing in an OpenMDAO component, I get the

Errors caused by write statements when running f2py-wrapped, PETSc-based fortran code in OpenMDAO

佐手、 提交于 2020-01-25 22:05:32
问题 I am using f2py to wrap my PETSc-based fortran analysis code for use in OpenMDAO (as suggested in this post). Rather than use f2py directly, I'm instead using it to generate the relevant .c, .pyc, etc. files and then linking them myself using mpif90. In a simple python environment, I can import my .so and run the code without any problems: >>> import module_name >>> module_name.execute() expected code output... However, when trying to do the same thing in an OpenMDAO component, I get the