{"id":23745,"date":"2021-11-18T17:45:01","date_gmt":"2021-11-18T17:45:01","guid":{"rendered":"https:\/\/www.askpython.com\/?p=23745"},"modified":"2021-11-18T17:45:02","modified_gmt":"2021-11-18T17:45:02","slug":"bipartite-graph-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/bipartite-graph-in-python","title":{"rendered":"Bipartite Graph in Python &#8211; Complete Guide"},"content":{"rendered":"\n<p>Hey folks! Today, in this tutorial, we are gonna understand what Bipartite graphs are and how to implement them in the python programing language using the networkx library. <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Introduction to Bipartite Graph<\/strong><\/h2>\n\n\n\n<p>A<strong><em>\u00a0<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"http:\/\/en.wikipedia.org\/wiki\/Bipartite_graph\">Bipartite Graph<\/a>\u00a0<\/em><\/strong>is a graph whose vertices can be divided into two independent sets &#8211; A and B. Every (a, b) means a connection between a node from set A and a node from set B. Here &#8220;a&#8221; belongs to A and &#8220;b&#8221; belongs to B.<\/p>\n\n\n\n<p>The nodes in one set cannot be connected to one another; they can only be connected to nodes in the other set.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"334\" height=\"345\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/bipartite_graph_example.png\" alt=\"Bipartite Graph Example\" class=\"wp-image-23767\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/bipartite_graph_example.png 334w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/bipartite_graph_example-290x300.png 290w\" sizes=\"auto, (max-width: 334px) 100vw, 334px\" \/><figcaption>Bipartite Graph Example<\/figcaption><\/figure><\/div>\n\n\n\n<p>A bipartite graph can be useful in the modeling of a customer&#8217;s purchases, for example. The nodes are divided into two groups in this case: the customers&#8217; partition and the products partition. <\/p>\n\n\n\n<p>Edges indicate that a consumer has purchased a certain product. In this scenario, it seems to reason that items cannot be linked to one another; after all, a product cannot purchase another product.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Implementing Bitpartite Graph in Python<\/strong><\/h2>\n\n\n\n<p>The first step in a program is importing modules\/libraries into our code. We would require importing basic networkx along with bipartite from <a href=\"https:\/\/www.askpython.com\/python-modules\/networkx-package\" data-type=\"post\" data-id=\"18292\">networkx<\/a>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimport networkx as nx\nfrom networkx.algorithms import bipartite\n<\/pre><\/div>\n\n\n<p>Next, we will be creating an empty Graph in order to add nodes and edges to it in the later sections.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nG = nx.Graph()\n<\/pre><\/div>\n\n\n<p>The next step is to add nodes with the node attribute &#8220;bipartite&#8221;. Here, the value of the bipartite attribute determines the class of the node. If its value is 0 then it belongs to first-class and in case its value is 1 it belongs to the second class.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nG.add_nodes_from(&#x5B;&#039;A1&#039;,&#039;A2&#039;,&#039;A3&#039;,&#039;A4&#039;], bipartite=0)\nG.add_nodes_from(&#x5B;&#039;B1&#039;,&#039;B2&#039;,&#039;B3&#039;],bipartite=1)\n<\/pre><\/div>\n\n\n<p>Next, we will add edges only between nodes of opposite classes. You can add as many edges as you want, for now, we have added a few of them.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nG.add_edges_from(&#x5B;(&#039;A1&#039;, &quot;B3&quot;),(&#039;A4&#039;, &quot;B1&quot;),(&#039;A2&#039;, &quot;B2&quot;),(&#039;A2&#039;, &quot;B3&quot;),(&#039;A3&#039;, &quot;B1&quot;)])\n<\/pre><\/div>\n\n\n<p>We can also confirm if the graph is bipartite or not using the simple code line mentioned below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nbipartite.is_bipartite(G)\n<\/pre><\/div>\n\n\n<p>Now, visualize the graph very easily through the code snippet mentioned below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnx.draw_networkx(G, pos = nx.drawing.layout.bipartite_layout(G, &#x5B;&#039;A1&#039;,&#039;A2&#039;,&#039;A3&#039;,&#039;A4&#039;]), width = 2)\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"349\" height=\"231\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/bipartite_graph_visualization.png\" alt=\"Bipartite Graph Visualization\" class=\"wp-image-23763\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/bipartite_graph_visualization.png 349w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/bipartite_graph_visualization-300x199.png 300w\" sizes=\"auto, (max-width: 349px) 100vw, 349px\" \/><figcaption>Bipartite Graph Visualization<\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p id=\"block-d144faf1-1abb-488c-a4bd-8a80520fbf7b\">Congratulations! You just learned how to build a bipartite graph using Networkx. Hope you enjoyed it! &#x1f607;<\/p>\n\n\n\n<p id=\"block-4e73ecff-f7fa-46a5-be6e-35bbb0867ba8\">Liked the tutorial? In any case, I would recommend you to have a look at the tutorials mentioned below:<\/p>\n\n\n\n<ol class=\"wp-block-list\" id=\"block-39ec4c2c-ab54-45a6-804e-58eb24573b45\"><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/stock-price-prediction-python\"><\/a><a href=\"https:\/\/www.askpython.com\/python-modules\/networkx-package\">NetworkX Package \u2013 Python Graph Library<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/distance-between-nodes-unweighted-graph\">Calculating the Distance Between Nodes in an Unweighted Graph<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/graph-operations\">Graph Operations in Python [With Easy Examples]<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/graph-in-python\">Implementing a Graph in Python<\/a><\/li><\/ol>\n\n\n\n<p id=\"block-177e4b87-db13-427b-90fa-d70aac750e1d\">Thank you for taking your time out! Hope you learned something new!! &#x1f604;<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n","protected":false},"excerpt":{"rendered":"<p>Hey folks! Today, in this tutorial, we are gonna understand what Bipartite graphs are and how to implement them in the python programing language using the networkx library. Introduction to Bipartite Graph A\u00a0Bipartite Graph\u00a0is a graph whose vertices can be divided into two independent sets &#8211; A and B. Every (a, b) means a connection [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":23776,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-23745","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/23745","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=23745"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/23745\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/23776"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=23745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=23745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=23745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}