Is it possible to add weight / probability to node in graph theory (using networkx)

I am using networkx (a python library for processing graphs). I basically have nodes with different edges, but I want to see what the path would look like if it used the nodes that were the most connected.

I can use this command to view the number of connections:

len(G.edges(CurrentNode))

and I can get the number of edges, but I'm not sure how to apply this for a list as a path. For example, I can add this number as an attribute, but I don’t think that the attributes are taken into account when searching for the path and because I add it after the edges are connected, I can not add weights to the edges themselves. Another problem is that the higher the score, the more I want to track the path, but with the edges, I think it follows the lowest weighted edge.

I am wondering what approach other people are taking to find paths based on certain node characteristics? If someone knows how to do this for networkx, great! but I think networkx has many functions, so if I can get a theory or a general approach, I'm sure I can find a way to do this in python.

UPDATE: Sorry, I may explain this incorrectly. I understand that I can add attributes to nodes, but I'm not sure how to make decisions based on these attributes. Therefore, in my case, based on certain conditions, I add edges between nodes. Each group of nodes represents a different day (day1data .., day2data .., day3data ..), so I connect several nodes from day1 to nodes in day2 only if certain rules are met. As soon as my bound edges are connected, I want those that were considered more strongly to choose the path. So I added the weight attribute to each node of the current day, which basically represents the total number of edges connecting this node. My problem is that the weight attribute is not used in any of the path decision decisions, because its attribute,which I created and designated myself (I could create a label named "abc" = "hello world" and apply this attribute to node). How can I take this weight into account when creating the path (the ribs are already created, so I don’t think I can go back and recreate them)?

+5
3

NetworkX. , , dict.

In [30]: import networkx as nx

In [31]: G = nx.Graph()

In [32]: G.add_edge(1, 2, weight=3, type="green")

In [33]: G[1][2]
Out[33]: {'type': 'green', 'weight': 3}

In [34]: G[1][2]["weight"]
Out[34]: 3

, ( ) .

In [35]: G[1][2]["weight"] = 5

In [36]: del G[1][2]["type"]

In [37]: G[1][2]["color"] = "green"

In [38]: G[1][2]
Out[38]: {'color': 'green', 'weight': 5}

, , ( , ).

In [39]: G.add_edge(1, 3, weight=1)

In [40]: G.add_edge(2, 3, weight=2)

In [41]: G.edges()
Out[41]: [(1, 2), (1, 3), (2, 3)]

In [42]: nx.shortest_path(G, source=1, target=2, weight="weight")
Out[42]: [1, 3, 2]

. , , . . 1/max(k_i,k_j) (i,j), k_i, k_j - i j.

, , : .. . , . , -, , . , .

+6

NetworkX Tutorial

>>> G.add_edge(1, 2, weight=4.7 )
>>> G.add_edges_from([(3,4),(4,5)], color='red')
>>> G.add_edges_from([(1,2,{'color':'blue'}), (2,3,{'weight':8})])
>>> G[1][2]['weight'] = 4.7
>>> G.edge[1][2]['weight'] = 4

, .

0

attribute, .

, , networkx/algorithms/shortest_paths/weighted.py

It will have a lambda declaration get_weight functionsimilar to this:

if G.is_multigraph():
    get_weight = lambda u, v, data: min(
        eattr.get(weight, 1) for eattr in data.values())
else:
    get_weight = lambda u, v, data: data.get(weight, 1)

I wanted to give my nodespecific weight, so I changed it as follows:

if G.is_multigraph():
    get_weight = lambda u, v, data: min(
        eattr.get(weight, 1) for eattr in data.values())
else:
    get_weight = lambda u, v, data: (data.get(weight,0) + nx.get_node_attributes(G, "node_weight").get(v,0)) 

I set my default weight to 0: data: data.get(weight,0)and added the value of my own node_weight attribute (default 0).

data: (data.get(weight,0) + nx.get_node_attributes(G, "node_weight").get(v,0))

vis the next achievable nodein the schedule.

Now you can install attributeafter creating the schedule.

nx.set_node_attributes(G, "node_weight", {1:3.5, 2:56})
0
source

All Articles