๐ธ๏ธ NetworkX Graphs
NetworkX is a Python package for the creation, manipulation, and study of the structure of complex networks.
Mastering this concept will significantly boost your Python data science skills!
๐ป Code Example:
import networkx as nx import json # 1. Build a weighted graph โ pynfinity topic dependency network G = nx.DiGraph() # Nodes โ python learning topics topics = ["Variables", "Functions", "Classes", "Decorators", "Generators", "AsyncIO", "Pydantic", "FastAPI"] G.add_nodes_from(topics, category="python") # Directed edges (prerequisite โ advanced) edges = [ ("Variables", "Functions", {"weight": 1, "difficulty": "easy"}), ("Functions", "Classes", {"weight": 2, "difficulty": "medium"}), ("Classes", "Decorators", {"weight": 3, "difficulty": "medium"}), ("Decorators", "AsyncIO", {"weight": 4, "difficulty": "hard"}), ("Classes", "Pydantic", {"weight": 3, "difficulty": "medium"}), ("Pydantic", "FastAPI", {"weight": 4, "difficulty": "hard"}), ("AsyncIO", "FastAPI", {"weight": 4, "difficulty": "hard"}), ("Generators", "AsyncIO", {"weight": 3, "difficulty": "hard"}), ("Functions", "Generators", {"weight": 2, "difficulty": "medium"}), ] G.add_edges_from([(u, v, d) for u, v, d in edges]) print(f"Nodes: {G.number_of_nodes()} | Edges: {G.number_of_edges()}") # 2. Shortest learning path โ Variables โ FastAPI path = nx.shortest_path(G, "Variables", "FastAPI") print(f"\nShortest path to FastAPI:") print(" โ ".join(path)) # 3. Centrality โ which topic is most connected? centrality = nx.in_degree_centrality(G) sorted_topics = sorted(centrality.items(), key=lambda x: x[1], reverse=True) print("\nMost depended-upon topics (in-degree centrality):") for topic, score in sorted_topics[:4]: print(f" {topic:<15} {score:.3f}") # 4. Topological sort (valid learning order) topo_order = list(nx.topological_sort(G)) print("\nRecommended learning order:") print(" โ ".join(topo_order)) # 5. Find all paths from Functions to FastAPI all_paths = list(nx.all_simple_paths(G, "Functions", "FastAPI")) print(f"\nTotal paths: Functions โ FastAPI: {len(all_paths)}")
Keep exploring and happy coding! ๐ป