[Networkit] adding weights to a new created graph

Michelangelo Puliga michelangelo.puliga at imtlucca.it
Thu Jun 12 11:21:45 CEST 2014


Thanks a lot guys

michelangelo

On Thu, Jun 12, 2014 at 11:18 AM, Christian Staudt
<christian.staudt at kit.edu> wrote:
> Hi Michelangelo,
>
> A) the default constructor of Graph creates an unweighted graph, i.e. all weights are 1.0. If you want a weighted graph, set the constructor parameter „weighted“ to True. Example:
>
> In [1]: import NetworKit as nk
>
> In [2]: G = nk.Graph(weighted=True)
>
> In [3]: G.addNode(); G.addNode()
> Out[3]: 1
>
> In [4]: G.addEdge(0, 1, 0.1)
>
> In [5]: G.weight(0, 1)
> Out[5]: 0.1
>
>
> B) This is a bug in the last release which I have now fixed. Please update to the latest revision (of either the default or Dev branch) and try this:
>
> In [1]: from NetworKit import *
>
> In [2]: import networkx as nx
>
> In [3]: nxG = nx.Graph()
>
> In [5]: nxG.add_edge(0, 1, weight=42.0)
>
> In [6]: nxG[0][1]["weight"]
> Out[6]: 42.0
>
> In [7]: G = nx2nk(nxG, weightAttr="weight")
>
> In [8]: G.weight(0, 1)
> Out[8]: 42.0
>
>
> Thanks for your feedback, and I hope you will find NetworKit useful for your research. We are always interested in hearing about and improving the user experience.
>
> Chris
>
>
>
> Am 12.06.2014 um 10:39 schrieb Michelangelo Puliga <michelangelo.puliga at imtlucca.it>:
>
>> Dear NetworKit developers,
>>  I'm trying to use Networkit in the python realization to analyze a
>> network for community detection. Instead of reading the data from a
>> metis file I'm trying to create a graph (weidghted undirected)
>>
>> Here an example in ipython3
>>
>>
>> In[1]: import NetworKit as nk
>>
>> In [2]: G = nk.Graph()
>>
>> In [3]: G.addNode()
>> Out[3]: 0
>>
>> In [4]: G.addNode()
>> Out[4]: 1
>>
>> In [5]: G.addNode()
>> Out[5]: 2
>>
>> In [6]: G.addEdge(0,1,0.1)
>>
>> In [7]: G.edges()
>> Out[7]: [(1, 0)]
>>
>> In [8]: G.weight(0,1)
>> Out[8]: 1.0
>>
>> In [9]: G.weight(1,0)
>> Out[9]: 1.0
>>
>> the weight associated to the edge (0,1) is always 1. while I defined
>> in addEdge(0,1,0.1)  weight = 0.1
>>
>> finally if I try to convert an weighted graph from NetworkX to
>> NetworKit I got the error that MarkAsWeighted function is not defined
>> (there is a todo in the source code )...
>>
>> Any help/suggestion to load a weighted network programmatically from
>> the code (addnode, addedge ?)
>>
>> thanks
>>
>> michelangelo
>>
>> _______________________________________________
>> NetworKit mailing list
>> NetworKit at ira.uni-karlsruhe.de
>> https://lists.ira.uni-karlsruhe.de/mailman/listinfo/networkit
>



More information about the NetworKit mailing list