Weighted directed graph Python

The project implements a Weighted and directed graph model. The graph contains a data structure of a dictionary in a dictionary: the keys in the external dict are sources nodes keys, Every value is a pair (tuple) of (dest: weight), of an edge.

Graph is an important data structure studied in Computer Science. is an equally important topic in both mathematics and Computer Science.

Representing a graph in a program means finding a way to store the graph in a computer’s memory. Which is a prerequisite to working with graphs in…

Note

Click to download the full example code

Weighted Graph

An example using Graph as a weighted network.

Weighted directed graph Python

import matplotlib.pyplot as plt
import networkx as nx

G = nx.Graph()

G.add_edge("a", "b", weight=0.6)
G.add_edge("a", "c", weight=0.2)
G.add_edge("c", "d", weight=0.1)
G.add_edge("c", "e", weight=0.7)
G.add_edge("c", "f", weight=0.9)
G.add_edge("a", "d", weight=0.3)

elarge = [(u, v) for (u, v, d) in G.edges(data=True) if d["weight"] > 0.5]
esmall = [(u, v) for (u, v, d) in G.edges(data=True) if d["weight"] <= 0.5]

pos = nx.spring_layout(G, seed=7)  # positions for all nodes - seed for reproducibility

# nodes
nx.draw_networkx_nodes(G, pos, node_size=700)

# edges
nx.draw_networkx_edges(G, pos, edgelist=elarge, width=6)
nx.draw_networkx_edges(
    G, pos, edgelist=esmall, width=6, alpha=0.5, edge_color="b", style="dashed"
)

# node labels
nx.draw_networkx_labels(G, pos, font_size=20, font_family="sans-serif")
# edge weight labels
edge_labels = nx.get_edge_attributes(G, "weight")
nx.draw_networkx_edge_labels(G, pos, edge_labels)

ax = plt.gca()
ax.margins(0.08)
plt.axis("off")
plt.tight_layout()
plt.show()

Total running time of the script: ( 0 minutes 0.081 seconds)

Download Python source code: plot_weighted_graph.py

Download Jupyter notebook: plot_weighted_graph.ipynb

Gallery generated by Sphinx-Gallery

How do you implement a weighted directed graph in Python?

If there is an edge between vertex 0 and 1, then the graph can be traversed from 0 to 1 and from 1 to 0. Some graphs have weights associated with edges between two vertices. These graphs are called Weighted Graphs. Implementation of the Graph can be done by using either an adjacency list or an adjacency matrix.

Can directed graphs be weighted?

Directed and undirected graphs can also be weighted. A weighted directed graph is the same as a directed graph except that each edge has a weight or cost associated with it.

Is weighted graph a directed graph?

Weighted directed graphs (also known as directed networks) are (simple) directed graphs with weights assigned to their arrows, similarly to weighted graphs (which are also known as undirected networks or weighted networks).

How do you make a weighted graph in NetworkX?

Weighted graphs using NetworkX.
NOTE: The approach outlined here works well for a small set of nodes. ... .
a) Iterate through the graph nodes to gather all the weights..
b) Get unique weights..
c) Loop through the unique weights and plot any edges that match the weight..
d) Normalize the weights. ... .
e) Make changes to the weighting..