Skip to content
Merged
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
331 changes: 331 additions & 0 deletions notebooks/lib_examples.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,331 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# NetGraph Library Examples\n",
"\n",
"This notebook contains examples of using the NetGraph library to create and manipulate graphs, calculate maximum flow, and place traffic demands."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Required imports\n",
"from ngraph.lib.graph import MultiDiGraph\n",
"from ngraph.lib.max_flow import calc_max_flow\n",
"\n",
"# Create a graph with parallel edges\n",
"# Metric:\n",
"# [1,1] [1,1]\n",
"# ┌────────►B─────────┐\n",
"# │ │\n",
"# │ ▼\n",
"# A C\n",
"# │ ▲\n",
"# │ [2] [2] │\n",
"# └────────►D─────────┘\n",
"#\n",
"# Capacity:\n",
"# [1,2] [1,2]\n",
"# ┌────────►B─────────┐\n",
"# │ │\n",
"# │ ▼\n",
"# A C\n",
"# │ ▲\n",
"# │ [3] [3] │\n",
"# └────────►D─────────┘\n",
"\n",
"g = MultiDiGraph()\n",
"g.add_edge(\"A\", \"B\", metric=1, capacity=1)\n",
"g.add_edge(\"B\", \"C\", metric=1, capacity=1)\n",
"g.add_edge(\"A\", \"B\", metric=1, capacity=2)\n",
"g.add_edge(\"B\", \"C\", metric=1, capacity=2)\n",
"g.add_edge(\"A\", \"D\", metric=2, capacity=3)\n",
"g.add_edge(\"D\", \"C\", metric=2, capacity=3)\n",
"\n",
"# Calculate MaxFlow between the source and destination nodes\n",
"max_flow = calc_max_flow(g, \"A\", \"C\")\n",
"\n",
"# We can verify that the result is as expected\n",
"assert max_flow == 6.0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Required imports\n",
"from ngraph.lib.graph import MultiDiGraph\n",
"from ngraph.lib.max_flow import calc_max_flow\n",
"\n",
"# Create a graph with parallel edges\n",
"# Metric:\n",
"# [1,1] [1,1]\n",
"# ┌────────►B─────────┐\n",
"# │ │\n",
"# │ ▼\n",
"# A C\n",
"# │ ▲\n",
"# │ [2] [2] │\n",
"# └────────►D─────────┘\n",
"#\n",
"# Capacity:\n",
"# [1,2] [1,2]\n",
"# ┌────────►B─────────┐\n",
"# │ │\n",
"# │ ▼\n",
"# A C\n",
"# │ ▲\n",
"# │ [3] [3] │\n",
"# └────────►D─────────┘\n",
"g = MultiDiGraph()\n",
"g.add_edge(\"A\", \"B\", metric=1, capacity=1)\n",
"g.add_edge(\"B\", \"C\", metric=1, capacity=1)\n",
"g.add_edge(\"A\", \"B\", metric=1, capacity=2)\n",
"g.add_edge(\"B\", \"C\", metric=1, capacity=2)\n",
"g.add_edge(\"A\", \"D\", metric=2, capacity=3)\n",
"g.add_edge(\"D\", \"C\", metric=2, capacity=3)\n",
"\n",
"# Calculate MaxFlow between the source and destination nodes\n",
"# Flows will be placed only on the shortest paths\n",
"max_flow = calc_max_flow(g, \"A\", \"C\", shortest_path=True)\n",
"\n",
"# We can verify that the result is as expected\n",
"assert max_flow == 3.0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Required imports\n",
"from ngraph.lib.graph import MultiDiGraph\n",
"from ngraph.lib.max_flow import calc_max_flow\n",
"from ngraph.lib.common import FlowPlacement\n",
"\n",
"# Create a graph with parallel edges\n",
"# Metric:\n",
"# [1,1] [1,1]\n",
"# ┌────────►B─────────┐\n",
"# │ │\n",
"# │ ▼\n",
"# A C\n",
"# │ ▲\n",
"# │ [2] [2] │\n",
"# └────────►D─────────┘\n",
"#\n",
"# Capacity:\n",
"# [1,2] [1,2]\n",
"# ┌────────►B─────────┐\n",
"# │ │\n",
"# │ ▼\n",
"# A C\n",
"# │ ▲\n",
"# │ [3] [3] │\n",
"# └────────►D─────────┘\n",
"g = MultiDiGraph()\n",
"g.add_edge(\"A\", \"B\", metric=1, capacity=1)\n",
"g.add_edge(\"B\", \"C\", metric=1, capacity=1)\n",
"g.add_edge(\"A\", \"B\", metric=1, capacity=2)\n",
"g.add_edge(\"B\", \"C\", metric=1, capacity=2)\n",
"g.add_edge(\"A\", \"D\", metric=2, capacity=3)\n",
"g.add_edge(\"D\", \"C\", metric=2, capacity=3)\n",
"\n",
"# Calculate MaxFlow between the source and destination nodes\n",
"# Flows will be equally balanced across the shortest paths\n",
"max_flow = calc_max_flow(\n",
" g, \"A\", \"C\", shortest_path=True, flow_placement=FlowPlacement.EQUAL_BALANCED\n",
")\n",
"\n",
"# We can verify that the result is as expected\n",
"assert max_flow == 2.0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Required imports\n",
"from ngraph.lib.graph import MultiDiGraph\n",
"from ngraph.lib.common import init_flow_graph\n",
"from ngraph.lib.demand import FlowPolicyConfig, Demand, get_flow_policy\n",
"from ngraph.lib.flow import FlowIndex\n",
"\n",
"# Create a graph\n",
"# Metric:\n",
"# [1] [1]\n",
"# ┌──────►B◄──────┐\n",
"# │ │\n",
"# │ │\n",
"# │ │\n",
"# ▼ [1] ▼\n",
"# A◄─────────────►C\n",
"#\n",
"# Capacity:\n",
"# [15] [15]\n",
"# ┌──────►B◄──────┐\n",
"# │ │\n",
"# │ │\n",
"# │ │\n",
"# ▼ [5] ▼\n",
"# A◄─────────────►C\n",
"g = MultiDiGraph()\n",
"g.add_edge(\"A\", \"B\", metric=1, capacity=15, label=\"1\")\n",
"g.add_edge(\"B\", \"A\", metric=1, capacity=15, label=\"1\")\n",
"g.add_edge(\"B\", \"C\", metric=1, capacity=15, label=\"2\")\n",
"g.add_edge(\"C\", \"B\", metric=1, capacity=15, label=\"2\")\n",
"g.add_edge(\"A\", \"C\", metric=1, capacity=5, label=\"3\")\n",
"g.add_edge(\"C\", \"A\", metric=1, capacity=5, label=\"3\")\n",
"\n",
"# Initialize a flow graph\n",
"r = init_flow_graph(g)\n",
"\n",
"# Create traffic demands\n",
"demands = [\n",
" Demand(\n",
" \"A\",\n",
" \"C\",\n",
" 20,\n",
" ),\n",
" Demand(\n",
" \"C\",\n",
" \"A\",\n",
" 20,\n",
" ),\n",
"]\n",
"\n",
"# Place traffic demands onto the flow graph\n",
"for demand in demands:\n",
" # Create a flow policy with required parameters or\n",
" # use one of the predefined policies from FlowPolicyConfig\n",
" flow_policy = get_flow_policy(FlowPolicyConfig.TE_UCMP_UNLIM)\n",
"\n",
" # Place demand using the flow policy\n",
" demand.place(r, flow_policy)\n",
"\n",
"# We can verify that all demands were placed as expected\n",
"for demand in demands:\n",
" assert demand.placed_demand == 20\n",
"\n",
"assert r.get_edges() == {\n",
" 0: (\n",
" \"A\",\n",
" \"B\",\n",
" 0,\n",
" {\n",
" \"capacity\": 15,\n",
" \"flow\": 15.0,\n",
" \"flows\": {\n",
" FlowIndex(src_node=\"A\", dst_node=\"C\", flow_class=0, flow_id=1): 15.0\n",
" },\n",
" \"label\": \"1\",\n",
" \"metric\": 1,\n",
" },\n",
" ),\n",
" 1: (\n",
" \"B\",\n",
" \"A\",\n",
" 1,\n",
" {\n",
" \"capacity\": 15,\n",
" \"flow\": 15.0,\n",
" \"flows\": {\n",
" FlowIndex(src_node=\"C\", dst_node=\"A\", flow_class=0, flow_id=1): 15.0\n",
" },\n",
" \"label\": \"1\",\n",
" \"metric\": 1,\n",
" },\n",
" ),\n",
" 2: (\n",
" \"B\",\n",
" \"C\",\n",
" 2,\n",
" {\n",
" \"capacity\": 15,\n",
" \"flow\": 15.0,\n",
" \"flows\": {\n",
" FlowIndex(src_node=\"A\", dst_node=\"C\", flow_class=0, flow_id=1): 15.0\n",
" },\n",
" \"label\": \"2\",\n",
" \"metric\": 1,\n",
" },\n",
" ),\n",
" 3: (\n",
" \"C\",\n",
" \"B\",\n",
" 3,\n",
" {\n",
" \"capacity\": 15,\n",
" \"flow\": 15.0,\n",
" \"flows\": {\n",
" FlowIndex(src_node=\"C\", dst_node=\"A\", flow_class=0, flow_id=1): 15.0\n",
" },\n",
" \"label\": \"2\",\n",
" \"metric\": 1,\n",
" },\n",
" ),\n",
" 4: (\n",
" \"A\",\n",
" \"C\",\n",
" 4,\n",
" {\n",
" \"capacity\": 5,\n",
" \"flow\": 5.0,\n",
" \"flows\": {\n",
" FlowIndex(src_node=\"A\", dst_node=\"C\", flow_class=0, flow_id=0): 5.0\n",
" },\n",
" \"label\": \"3\",\n",
" \"metric\": 1,\n",
" },\n",
" ),\n",
" 5: (\n",
" \"C\",\n",
" \"A\",\n",
" 5,\n",
" {\n",
" \"capacity\": 5,\n",
" \"flow\": 5.0,\n",
" \"flows\": {\n",
" FlowIndex(src_node=\"C\", dst_node=\"A\", flow_class=0, flow_id=0): 5.0\n",
" },\n",
" \"label\": \"3\",\n",
" \"metric\": 1,\n",
" },\n",
" ),\n",
"}"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading
Loading