Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 170 additions & 1 deletion ex-1-2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,82 @@
"# Part 1: Reading graphs from files / writing graphs to files + graph basics"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## NetworkX Tutorial: Graph I/O and Basics\n",
"\n",
"Before starting the exercises, let's review the key NetworkX concepts and functions you'll need.\n",
"\n",
"### Reading and Writing Graphs\n",
"\n",
"NetworkX supports various graph file formats:\n",
"\n",
"**Reading graphs:**\n",
"```python\n",
"# Read edge list formats\n",
"G = nx.read_edgelist('file.txt') # Simple edge list\n",
"G = nx.read_weighted_edgelist('file.txt') # With weights\n",
"\n",
"# Read other formats\n",
"G = nx.read_pajek('file.net') # Pajek format\n",
"G = nx.read_gml('file.gml') # GML format\n",
"G = nx.read_graphml('file.graphml') # GraphML format\n",
"```\n",
"\n",
"**Writing graphs:**\n",
"```python\n",
"nx.write_edgelist(G, 'output.txt')\n",
"nx.write_pajek(G, 'output.net')\n",
"nx.write_gml(G, 'output.gml')\n",
"```\n",
"\n",
"### Graph Types\n",
"\n",
"- `nx.Graph()` - Undirected graph\n",
"- `nx.DiGraph()` - Directed graph\n",
"- `nx.MultiGraph()` - Undirected multigraph (multiple edges between nodes)\n",
"- `nx.MultiDiGraph()` - Directed multigraph\n",
"\n",
"### Basic Graph Operations\n",
"\n",
"```python\n",
"# Graph information\n",
"G.number_of_nodes() # Count nodes\n",
"G.number_of_edges() # Count edges\n",
"G.nodes() # Get all nodes\n",
"G.edges() # Get all edges\n",
"\n",
"# Adding/removing nodes and edges\n",
"G.add_node(1)\n",
"G.add_edge(1, 2)\n",
"G.remove_node(1)\n",
"G.remove_edge(1, 2)\n",
"\n",
"# Checking existence\n",
"G.has_node(1)\n",
"G.has_edge(1, 2)\n",
"\n",
"# Graph comparison\n",
"nx.is_isomorphic(G1, G2) # Check if graphs are structurally identical\n",
"```\n",
"\n",
"### Directed vs Undirected Graphs\n",
"\n",
"When reading a graph as directed vs undirected:\n",
"- **Undirected**: Each edge (u,v) is bidirectional\n",
"- **Directed**: Each edge has a specific direction from u to v\n",
"- Converting directed to undirected typically doubles edge count (each directed edge becomes one undirected edge)\n",
"\n",
"**\ud83d\udcda References:**\n",
"- [NetworkX I/O Documentation](https://networkx.org/documentation/stable/reference/readwrite/index.html)\n",
"- [NetworkX Graph Types](https://networkx.org/documentation/stable/reference/classes/index.html)\n",
"- [NetworkX Tutorial](https://networkx.org/documentation/stable/tutorial.html)\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -235,6 +311,99 @@
"# Part 2: Connected components, Giant Component & Subgraphs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## NetworkX Tutorial: Connected Components and Subgraphs\n",
"\n",
"### Connected Components\n",
"\n",
"A connected component is a maximal set of nodes where every node is reachable from every other node.\n",
"\n",
"**For undirected graphs:**\n",
"```python\n",
"# Get all connected components\n",
"components = nx.connected_components(G) # Returns generator of sets\n",
"component_list = list(nx.connected_components(G))\n",
"\n",
"# Number of connected components\n",
"num_components = nx.number_connected_components(G)\n",
"\n",
"# Get the largest connected component (Giant Component)\n",
"largest_cc = max(nx.connected_components(G), key=len)\n",
"giant_component = G.subgraph(largest_cc).copy()\n",
"\n",
"# Check if graph is connected\n",
"nx.is_connected(G)\n",
"```\n",
"\n",
"**For directed graphs:**\n",
"```python\n",
"# Strongly connected components (path in both directions)\n",
"scc = nx.strongly_connected_components(G)\n",
"\n",
"# Weakly connected components (treating edges as undirected)\n",
"wcc = nx.weakly_connected_components(G)\n",
"```\n",
"\n",
"### Subgraphs\n",
"\n",
"```python\n",
"# Create a subgraph from a set of nodes\n",
"nodes_subset = [1, 2, 3, 4, 5]\n",
"subG = G.subgraph(nodes_subset) # Returns a view\n",
"subG_copy = G.subgraph(nodes_subset).copy() # Create independent copy\n",
"\n",
"# Induced subgraph (includes all edges between nodes in subset)\n",
"induced_subG = G.subgraph(nodes_subset)\n",
"\n",
"# Edge-based subgraph\n",
"edges_subset = [(1,2), (2,3)]\n",
"edge_subG = G.edge_subgraph(edges_subset)\n",
"```\n",
"\n",
"### Giant Component\n",
"\n",
"The giant component is the largest connected component in a graph. It's particularly important in network analysis:\n",
"\n",
"```python\n",
"# Extract giant component\n",
"largest_cc = max(nx.connected_components(G), key=len)\n",
"giant = G.subgraph(largest_cc).copy()\n",
"\n",
"# Size comparison\n",
"print(f\"Original graph: {G.number_of_nodes()} nodes\")\n",
"print(f\"Giant component: {giant.number_of_nodes()} nodes\")\n",
"print(f\"Fraction in giant: {giant.number_of_nodes()/G.number_of_nodes():.2%}\")\n",
"```\n",
"\n",
"### Network Resilience\n",
"\n",
"Studying how networks respond to node/edge removal:\n",
"\n",
"```python\n",
"# Random failures - remove random nodes\n",
"import random\n",
"nodes_to_remove = random.sample(list(G.nodes()), k=10)\n",
"G_failed = G.copy()\n",
"G_failed.remove_nodes_from(nodes_to_remove)\n",
"\n",
"# Targeted attacks - remove high-degree nodes\n",
"degree_sorted = sorted(G.degree(), key=lambda x: x[1], reverse=True)\n",
"top_nodes = [node for node, deg in degree_sorted[:10]]\n",
"G_attacked = G.copy()\n",
"G_attacked.remove_nodes_from(top_nodes)\n",
"```\n",
"\n",
"**\ud83d\udcda References:**\n",
"- [NetworkX Components](https://networkx.org/documentation/stable/reference/algorithms/component.html)\n",
"- [NetworkX Subgraphs](https://networkx.org/documentation/stable/reference/classes/generated/networkx.Graph.subgraph.html)\n",
"- [Network Resilience Analysis](https://networkx.org/documentation/stable/reference/algorithms/connectivity.html)\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "94a45b3e",
Expand Down Expand Up @@ -377,4 +546,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}
Loading