NetworkX: Multigraphs














































NetworkX: Multigraphs



Python: NetworkX

NetworkX: Multigraphs

A multigraph is a graph which is permitted to have multiple edges, also called parallel edges, that is, edges that have the same end nodes. Thus, two vertices may be connected by more than one edge.

Multigraphs can further be divided into two categories:

  • Undirected Multigraphs

  • Directed Mutligraphs

NetworkX provides classes for both of the above categories.

MultiGraph class for undirected multigraphs.

MultiDiGraph class for directed multigraphs.

In the code below we have created an undirected multigraph and printed the graph degree to see how it behaves.

import networkx as nx MG = nx.MultiGraph() MG.add_weighted_edges_from([(1, 2, 0.5), (1, 2, 0.75), (2, 3, 0.5)]) print(dict(MG.degree(weight='weight')))

The output is like this

{1: 1.25, 2: 1.75, 3: 0.5}

Similarly, we will create a directed multigraph and print its indegrees and outdegrees.

import networkx as nx MG = nx.MultiDiGraph() MG.add_weighted_edges_from( [(1, 2, 0.5), (1, 2, 0.3), (2, 1, 0.75), (2, 3, 0.5)]) print(dict(MG.in_degree(weight='weight'))) print(dict(MG.out_degree(weight='weight')))

The output is like this

{1: 0.75, 2: 0.8, 3: 0.5} {1: 0.8, 2: 1.25, 3: 0}

We can also convert a MultiGraph object to a Graph or a MutliDiGraph object to a DiGraph object.

The code below creates the same undirected multigraph we created in a previous example and converts it to an undirected graph.

import networkx as nx MG = nx.MultiGraph() MG.add_weighted_edges_from([(1, 2, 0.5), (1, 2, 0.75), (2, 3, 0.5)]) print(dict(MG.degree(weight='weight'))) G = nx.Graph(MG) print(dict(G.degree(weight='weight')))

The output of the above code is like this

{1: 1.25, 2: 1.75, 3: 0.5} {1: 0.75, 2: 1.25, 3: 0.5}

 


More Articles of Aniket Sharma:

Name Views Likes
Pyperclip: Installation and Working 990 2
Number Guessing Game using Python 683 2
Pyperclip: Not Implemented Error 1027 2
Hangman Game using Python 16785 2
Using Databases with CherryPy application 1673 2
nose: Working 507 2
pytest: Working 511 2
Open Source and Hacktoberfest 867 2
Managing Logs of CherryPy applications 1001 2
Top 20 Data Science Tools 684 2
Ajax application using CherryPy 799 2
REST application using CherryPy 664 2
On Screen Keyboard using Python 5508 2
Elastic Net Regression 815 2
US Presidential Election 2020 Prediction using Python 794 2
Sound Source Separation 1164 2
URLs with Parameters in CherryPy 1633 2
Testing CherryPy application 635 2
Handling HTML Forms with CherryPy 1448 2
Applications of Natural Language Processing in Businesses 509 2
NetworkX: Multigraphs 649 2
Tracking User Activity with CherryPy 1397 2
CherryPy: Handling Cookies 820 2
Introduction to NetworkX 633 2
TorchServe - Serving PyTorch Models 1302 2
Fake News Detection Model using Python 734 2
Keeping Home Routers secure while working remotely 484 2
Email Slicer using Python 2996 2
NetworkX: Creating a Graph 1108 2
Best Mathematics Courses for Machine Learning 551 2
Hello World in CherryPy 680 2
Building dependencies as Meson subprojects 978 2
Vehicle Detection System 1081 2
NetworkX: Examining and Removing Graph Elements 608 2
Handling URLs with CherryPy 537 2
PEP 8 - Guide to Beautiful Python Code 757 2
NetworkX: Drawing Graphs 624 2
Mad Libs Game using Python 643 2
Hosting Cherry applications 613 2
Top 5 Free Online IDEs of 2020 867 2
pytest: Introduction 534 2
Preventing Pwned and Reused Passwords 582 2
Contact Book using Python 2095 2
Introduction to CherryPy 547 2
nose: Introduction 505 2
Text-based Adventure Game using Python 3000 2
NetworkX: Adding Attributes 2279 2
NetworkX: Directed Graphs 1021 2
Dice Simulator using Python 560 2
Decorating CherryPy applications using CSS 833 2

Comments