Python: NetworkX

Attributes such as weights, labels, colors or any other property can be attached to graphs, nodes or edges.

Each graph, node and edge can hold key-value pairs of attributes. By default these are empty. But attributes can be added or changed, using add_edge() and add_node() methods, or directly manipulated.

Graph attributes

Graph attributes can be assigned when creating a new graph.

import networkx as nx G = nx.Graph(day="Friday") print(G.graph)

The output looks like this

[(1, 2, {'weight': 4.7, 'color': 'blue'}), (2, 3, {'weight': 8}), (3, 4, {'color': 'red'}), (4, 5, {'color': 'red'})] [(1, 2, {'weight': 4.7, 'color': 'blue'}), (2, 3, {'weight': 8}), (3, 4, {'color': 'red', 'weight': 4.2}), (4, 5, {'color': 'red'})]

We can also modify attributes later.

import networkx as nx G = nx.Graph(day="Friday") print(G.graph) G.graph['day'] = 'Monday' print(G.graph)

The output looks like this

{'day': 'Friday'}

{'day': 'Monday'}

Node attributes

import networkx as nx G = nx.Graph() G.add_node(1, time='5pm') G.add_nodes_from([3], time='2pm') print(G.nodes[1]) G.nodes[1]['room'] = 714 print(G.nodes.data())

The output looks like this

{'time': '5pm'} [(1, {'time': '5pm', 'room': 714}), (3, {'time': '2pm'})]

Edge attributes

import networkx as nx G = nx.Graph() 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})]) print(G.edges.data()) G[1][2]['weight'] = 4.7 G.edges[3, 4]['weight'] = 4.2 print(G.edges.data())

The output looks like this

[(1, 2, {'weight': 4.7, 'color': 'blue'}), (2, 3, {'weight': 8}), (3, 4, {'color': 'red'}), (4, 5, {'color': 'red'})]

[(1, 2, {'weight': 4.7, 'color': 'blue'}), (2, 3, {'weight': 8}), (3, 4, {'color': 'red', 'weight': 4.2}), (4, 5, {'color': 'red'})]

More Articles of Aniket Sharma:

Name Views Likes
Pyperclip: Installation and Working 1088 2
Number Guessing Game using Python 752 2
Pyperclip: Not Implemented Error 1420 2
Hangman Game using Python 19229 2
Using Databases with CherryPy application 2164 2
nose: Working 585 2
pytest: Working 573 2
Open Source and Hacktoberfest 943 2
Managing Logs of CherryPy applications 1130 2
Top 20 Data Science Tools 760 2
Ajax application using CherryPy 908 2
REST application using CherryPy 752 2
On Screen Keyboard using Python 7571 2
Elastic Net Regression 958 2
US Presidential Election 2020 Prediction using Python 856 2
Sound Source Separation 1320 2
URLs with Parameters in CherryPy 2132 2
Testing CherryPy application 739 2
Handling HTML Forms with CherryPy 1779 2
Applications of Natural Language Processing in Businesses 586 2
NetworkX: Multigraphs 806 2
Tracking User Activity with CherryPy 1612 2
Introduction to NetworkX 690 2
TorchServe - Serving PyTorch Models 1496 2
Fake News Detection Model using Python 809 2
Keeping Home Routers secure while working remotely 549 2
Email Slicer using Python 3118 2
NetworkX: Creating a Graph 1181 2
Best Mathematics Courses for Machine Learning 617 2
Hello World in CherryPy 892 2
Building dependencies as Meson subprojects 1198 2
Vehicle Detection System 1197 2
NetworkX: Examining and Removing Graph Elements 680 2
Handling URLs with CherryPy 608 2
PEP 8 - Guide to Beautiful Python Code 825 2
NetworkX: Drawing Graphs 722 2
Mad Libs Game using Python 738 2
Hosting Cherry applications 692 2
Top 5 Free Online IDEs of 2020 971 2
pytest: Introduction 588 2
Preventing Pwned and Reused Passwords 640 2
Contact Book using Python 2284 2
Introduction to CherryPy 614 2
nose: Introduction 552 2
Text-based Adventure Game using Python 3365 2