NetworkX: Directed Graphs














































NetworkX: Directed Graphs



Python: NetworkX

NetworkX: Directed Graphs

A Directed Graph is a graph that is made up of a set of vertices connected by edges, where the edges have a direction associated with them. It differs from an ordinary or undirected graph, in that the latter is defined in terms of unordered pairs of vertices, which are usually called edges, arcs, or lines.

In NetworkX, we can make directed graphs using the DiGraph class.

The DiGraph class provides additional methods and properties specific to directed edges, e.g., DiGraph.out_edges, DiGraph.in_degree, DiGraph.predecessors(), DiGraph.successors() etc. To allow algorithms to work with both classes easily, the directed versions of neighbors() is equivalent to successors() while degree reports the sum of in_degree and out_degree.

In the below program we will create a directed graph with weighted edges. Then, we will print different properties to understand how the DiGraph() works.

import networkx as nx DG = nx.DiGraph() DG.add_weighted_edges_from([(1, 2, 0.5), (3, 1, 0.75)]) print(DG.out_degree(1, weight='weight')) print(DG.degree(1, weight='weight')) print(list(DG.successors(1))) print(list(DG.neighbors(1)))

The output looks like this

0.5 1.25 [2] [2]

The graph we created looks like this


Some algorithms work only for directed graphs and others are not well defined for directed graphs. So, we might need sometimes to convert a directed graph to undirected for some calculations. We can do this using

G = nx.Graph(DG)

or

G = DG.to_undirected()

The output is the same which is the below graph.




More Articles of Aniket Sharma:

Name Views Likes
Pyperclip: Installation and Working 992 2
Number Guessing Game using Python 683 2
Pyperclip: Not Implemented Error 1033 2
Hangman Game using Python 16821 2
Using Databases with CherryPy application 1676 2
nose: Working 509 2
pytest: Working 512 2
Open Source and Hacktoberfest 868 2
Managing Logs of CherryPy applications 1005 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 5532 2
Elastic Net Regression 816 2
US Presidential Election 2020 Prediction using Python 795 2
Sound Source Separation 1166 2
URLs with Parameters in CherryPy 1635 2
Testing CherryPy application 638 2
Handling HTML Forms with CherryPy 1450 2
Applications of Natural Language Processing in Businesses 511 2
NetworkX: Multigraphs 649 2
Tracking User Activity with CherryPy 1404 2
CherryPy: Handling Cookies 822 2
Introduction to NetworkX 633 2
TorchServe - Serving PyTorch Models 1306 2
Fake News Detection Model using Python 735 2
Keeping Home Routers secure while working remotely 484 2
Email Slicer using Python 2998 2
NetworkX: Creating a Graph 1111 2
Best Mathematics Courses for Machine Learning 551 2
Hello World in CherryPy 681 2
Building dependencies as Meson subprojects 979 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 759 2
NetworkX: Drawing Graphs 624 2
Mad Libs Game using Python 645 2
Hosting Cherry applications 613 2
Top 5 Free Online IDEs of 2020 868 2
pytest: Introduction 535 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 3002 2
NetworkX: Adding Attributes 2290 2
NetworkX: Directed Graphs 1022 2
Dice Simulator using Python 562 2
Decorating CherryPy applications using CSS 834 2

Comments