What is NetworkX?

A Python library used to create, manipulate, and study graphs and networks (social, transportation, web, etc.)

๐Ÿ“– Key Definitions


Create and Inspect a Graph

๐Ÿ“Œ Code: Create an Undirected Graph

import networkx as nx 

# Create an undirected graph
G = nx.Graph()

# Add a single node labeled "A" to the graph
G.add_node("A")

# Add multiple nodes: "B" and "C" to the graph
G.add_nodes_from(["B", "C"])

# Add an edge (connection) between nodes "A" and "B"
G.add_edge("A", "B")

# Add multiple edges to the graph: one between "B" and "C", another between "C" and "A"
G.add_edges_from([("B", "C"), ("C", "A")])

# Print all nodes in the graph
print("Nodes:", G.nodes)

# Print all edges in the graph
print("Edges:", G.edges)

๐Ÿง  Exam Concept: Undirected vs Directed Graph


โžฐ Directed Graph Example