Skip to content

Instantly share code, notes, and snippets.

@Vini2
Last active October 20, 2023 20:06
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Vini2/7e674661f79d5829563ad60d1baaf505 to your computer and use it in GitHub Desktop.
Code for Matching of Bipartite Graphs.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Matching of Bipartite Graphs\n",
"## Create a bipartite graph"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"import networkx as nx\n",
"from networkx.algorithms import bipartite\n",
"\n",
"# Initialise graph\n",
"B = nx.Graph()\n",
"\n",
"# Add nodes with the node attribute \"bipartite\"\n",
"top_nodes = [1, 2, 3]\n",
"bottom_nodes = [\"A\", \"B\", \"C\"]\n",
"B.add_nodes_from(top_nodes, bipartite=0)\n",
"B.add_nodes_from(bottom_nodes, bipartite=1)\n",
"\n",
"# Add edges only between nodes of opposite node sets\n",
"B.add_edges_from([(1, \"A\"), (1, \"B\"), (2, \"B\"), (2, \"C\"), (3, \"A\"), (3, \"C\")])\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Maximum Cardinality Matching"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{1: 'A', 2: 'D', 'A': 1, 'D': 2}\n"
]
}
],
"source": [
"# Initialise graph\n",
"B = nx.Graph()\n",
"\n",
"# Add nodes with the node attribute \"bipartite\"\n",
"top_nodes = [1, 2]\n",
"bottom_nodes = [\"A\", \"B\", \"C\", \"D\"]\n",
"B.add_nodes_from(top_nodes, bipartite=0)\n",
"B.add_nodes_from(bottom_nodes, bipartite=1)\n",
"\n",
"# Add edges only between nodes of opposite node sets\n",
"B.add_edges_from([(1, \"A\"), (1, \"B\"), (1, \"C\"), (1, \"D\"), (2, \"A\"), (2, \"D\")])\n",
"\n",
"#Obtain the maximum cardinality matching\n",
"my_matching = bipartite.matching.hopcroft_karp_matching(B, top_nodes)\n",
"print(my_matching)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Minimum Weight Matching"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{1: 'A', 2: 'B', 'A': 1, 'B': 2}\n"
]
}
],
"source": [
"# Initialise the graph\n",
"B = nx.Graph()\n",
"\n",
"# Add nodes with the node attribute \"bipartite\"\n",
"top_nodes = [1, 2]\n",
"bottom_nodes = [\"A\", \"B\", \"C\", \"D\"]\n",
"B.add_nodes_from(top_nodes, bipartite=0)\n",
"B.add_nodes_from(bottom_nodes, bipartite=1)\n",
"\n",
"# Add edges with weights\n",
"B.add_edge(1, \"A\", weight = 1)\n",
"B.add_edge(1, \"B\", weight = 4)\n",
"B.add_edge(1, \"C\", weight = 2)\n",
"B.add_edge(1, \"D\", weight = 1)\n",
"B.add_edge(2, \"A\", weight = 3)\n",
"B.add_edge(2, \"B\", weight = 1)\n",
"B.add_edge(2, \"C\", weight = 2)\n",
"B.add_edge(2, \"D\", weight = 2)\n",
"\n",
"#Obtain the minimum weight full matching\n",
"my_matching = bipartite.matching.minimum_weight_full_matching(B, top_nodes, \"weight\")\n",
"print(my_matching)"
]
}
],
"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.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment