{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Properties\n\nCompute some network properties for the lollipop graph.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\nimport networkx as nx\n\nG = nx.lollipop_graph(4, 6)\n\npathlengths = []\n\nprint(\"source vertex {target:length, }\")\nfor v in G.nodes():\n spl = dict(nx.single_source_shortest_path_length(G, v))\n print(f\"{v} {spl} \")\n for p in spl:\n pathlengths.append(spl[p])\n\nprint()\nprint(f\"average shortest path length {sum(pathlengths) / len(pathlengths)}\")\n\n# histogram of path lengths\ndist = {}\nfor p in pathlengths:\n if p in dist:\n dist[p] += 1\n else:\n dist[p] = 1\n\nprint()\nprint(\"length #paths\")\nverts = dist.keys()\nfor d in sorted(verts):\n print(f\"{d} {dist[d]}\")\n\nprint(f\"radius: {nx.radius(G)}\")\nprint(f\"diameter: {nx.diameter(G)}\")\nprint(f\"eccentricity: {nx.eccentricity(G)}\")\nprint(f\"center: {nx.center(G)}\")\nprint(f\"periphery: {nx.periphery(G)}\")\nprint(f\"density: {nx.density(G)}\")\n\npos = nx.spring_layout(G, seed=3068) # Seed layout for reproducibility\nnx.draw(G, pos=pos, with_labels=True)\nplt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Generated by dwww version 1.16 on Wed Apr 8 11:16:32 CEST 2026.