dwww Home | Show directory contents | Find package

{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# SNAP Graph Summary\nAn example of summarizing a graph based on node attributes and edge attributes\nusing the Summarization by Grouping Nodes on Attributes and Pairwise\nedges (SNAP) algorithm (not to be confused with the Stanford Network\nAnalysis Project).  The algorithm groups nodes by their unique\ncombinations of node attribute values and edge types with other groups\nof nodes to produce a summary graph.  The summary graph can then be used to\ninfer how nodes with different attributes values relate to other nodes in the\ngraph.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import networkx as nx\nimport matplotlib.pyplot as plt\n\n\nnodes = {\n    \"A\": {\"color\": \"Red\"},\n    \"B\": {\"color\": \"Red\"},\n    \"C\": {\"color\": \"Red\"},\n    \"D\": {\"color\": \"Red\"},\n    \"E\": {\"color\": \"Blue\"},\n    \"F\": {\"color\": \"Blue\"},\n    \"G\": {\"color\": \"Blue\"},\n    \"H\": {\"color\": \"Blue\"},\n    \"I\": {\"color\": \"Yellow\"},\n    \"J\": {\"color\": \"Yellow\"},\n    \"K\": {\"color\": \"Yellow\"},\n    \"L\": {\"color\": \"Yellow\"},\n}\nedges = [\n    (\"A\", \"B\", \"Strong\"),\n    (\"A\", \"C\", \"Weak\"),\n    (\"A\", \"E\", \"Strong\"),\n    (\"A\", \"I\", \"Weak\"),\n    (\"B\", \"D\", \"Weak\"),\n    (\"B\", \"J\", \"Weak\"),\n    (\"B\", \"F\", \"Strong\"),\n    (\"C\", \"G\", \"Weak\"),\n    (\"D\", \"H\", \"Weak\"),\n    (\"I\", \"J\", \"Strong\"),\n    (\"J\", \"K\", \"Strong\"),\n    (\"I\", \"L\", \"Strong\"),\n]\noriginal_graph = nx.Graph()\noriginal_graph.add_nodes_from(n for n in nodes.items())\noriginal_graph.add_edges_from((u, v, {\"type\": label}) for u, v, label in edges)\n\n\nplt.suptitle(\"SNAP Summarization\")\n\nbase_options = {\"with_labels\": True, \"edgecolors\": \"black\", \"node_size\": 500}\n\nax1 = plt.subplot(1, 2, 1)\nplt.title(\n    \"Original ({} nodes, {} edges)\".format(\n        original_graph.number_of_nodes(), original_graph.number_of_edges()\n    )\n)\npos = nx.spring_layout(original_graph, seed=7482934)\nnode_colors = [d[\"color\"] for _, d in original_graph.nodes(data=True)]\n\nedge_type_visual_weight_lookup = {\"Weak\": 1.0, \"Strong\": 3.0}\nedge_weights = [\n    edge_type_visual_weight_lookup[d[\"type\"]]\n    for _, _, d in original_graph.edges(data=True)\n]\n\nnx.draw_networkx(\n    original_graph, pos=pos, node_color=node_colors, width=edge_weights, **base_options\n)\n\nnode_attributes = (\"color\",)\nedge_attributes = (\"type\",)\nsummary_graph = nx.snap_aggregation(\n    original_graph, node_attributes, edge_attributes, prefix=\"S-\"\n)\n\nplt.subplot(1, 2, 2)\n\nplt.title(\n    \"SNAP Aggregation ({} nodes, {} edges)\".format(\n        summary_graph.number_of_nodes(), summary_graph.number_of_edges()\n    )\n)\nsummary_pos = nx.spring_layout(summary_graph, seed=8375428)\nnode_colors = []\nfor node in summary_graph:\n    color = summary_graph.nodes[node][\"color\"]\n    node_colors.append(color)\n\nedge_weights = []\nfor edge in summary_graph.edges():\n    edge_types = summary_graph.get_edge_data(*edge)[\"types\"]\n    edge_weight = 0.0\n    for edge_type in edge_types:\n        edge_weight += edge_type_visual_weight_lookup[edge_type[\"type\"]]\n    edge_weights.append(edge_weight)\n\nnx.draw_networkx(\n    summary_graph,\n    pos=summary_pos,\n    node_color=node_colors,\n    width=edge_weights,\n    **base_options,\n)\n\nplt.tight_layout()\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:02:46 CEST 2026.