{"id":148,"date":"2021-02-28T06:44:33","date_gmt":"2021-02-28T06:44:33","guid":{"rendered":"http:\/\/aslak.nu\/?page_id=148"},"modified":"2021-03-14T17:06:49","modified_gmt":"2021-03-14T17:06:49","slug":"networkx","status":"publish","type":"page","link":"http:\/\/aslak.nu\/index.php\/portfolio\/networkx\/","title":{"rendered":"Graphs &#8211; NetworkX &#038; Matplotlib"},"content":{"rendered":"\n<h2 class=\"alignwide has-text-align-center wp-block-heading\"><span class=\"has-inline-color has-accent-color\"><\/span><\/h2>\n\n\n\n<div class=\"wp-block-columns alignwide is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<h5 class=\"wp-block-heading\"><span class=\"has-inline-color has-accent-color\">Graphs<\/span><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">A Graph is a collection of Vertices <em>&amp;<\/em> Edges. It can be Sparse, Fully connected, Planar, (un)directed &#8211; there are many types of graphs. In this case, a graph is simply a tool to visualize interconnected-ness. A vertex represents any object defined within the domain of interest. An edge represents a type of relationship between two objects. A typical example of a graph could be a network of friends. Each person of the social network represents a unique Vertex, while each edge represents  &#8216;friends&#8217; relation. In this case we assume that the connected persons are indeed both friends, so the graph would be un-directed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can represent a social graph with the python libraries NetworkX &#8211; for graphs and Matplotlib &#8211; for visualization. Lets represent a fake family with on such graph<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># import the NetworkX library\nimport networkx as nx\n\n# Initialize a graph\nfamily_graph = nx.Graph()\n\n\n# A fake family \nfamily_nodes = &#91;'Dad', 'Mom', 'Brother', 'Sister', 'Best Friend']\n\n# Some fake relationships - each family member directly knows eachother. The brother and the mother are the only ones who know the best friend \n# An edge is either a 2-tuple '(from, to)' or a 3-tuple '(from, to, &lt;property-dictionary&gt;)'\n# The property-dictionary can contain arbitrary information bout the relationship. It could contain a weight, used for coloring or guiding the\n# layout of the final graph.\nfamily_relations = &#91;\n    ('Dad', 'Mom'),\n    ('Dad', 'Brother'),\n    ('Dad', 'Sister'),\n    ('Mom', 'Brother'),\n    ('Mom', 'Sister'),\n    ('Brother', 'Sister'),\n    ('Brother', 'Best friend'),\n    ('Mom', 'Best friend')\n]\n\n# Nodes can be added from any python iterable (such as the list above)\nfamily_graph.add_nodes_from(family_nodes)\n\n# likewise edges as well\nfamily_graph.add_edges_from(family_relations)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">At this point we have a graph consisting of 5 Vertices and 8 edges. It can be examined i various ways. One can find the degree (number of edges to\/from a vertex), or the actual adjacent vertices.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># find the number of connected edges to the 'Brother' Vertex in the graph\nfamily_graph.degree&#91;'Best friend']\n\n&gt; 2\n\n\n# find the adjacent vertices to the brother\nlist(family_graph.adj&#91;'Best friend'])\n\n&gt; &#91;'Brother', 'Mother']<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">There is a multitude of other graph related topics about joining graphs and and converting them. For now lets draw the graph<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># import matplotlib for drawing\nimport matplotlib as plt\n\n# draw the current graph with labels given a random layout\n\n# labels are represented as a dict where the node is the key. In this case, the key is the label\nnode_labels = {}\nfor elem in family_nodes:\n    node_labels&#91;elem] = elem\n\npos = nx.random_layout(family_graph)\nnx.draw_networkx(family_graph, pos, family_nodes, with_labels=True, labels=node_labels)<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"503\" src=\"http:\/\/aslak.nu\/wp-content\/uploads\/2021\/03\/fake_family_random-1024x503.png\" alt=\"\" class=\"wp-image-165\" srcset=\"http:\/\/aslak.nu\/wp-content\/uploads\/2021\/03\/fake_family_random-1024x503.png 1024w, http:\/\/aslak.nu\/wp-content\/uploads\/2021\/03\/fake_family_random-300x148.png 300w, http:\/\/aslak.nu\/wp-content\/uploads\/2021\/03\/fake_family_random-768x378.png 768w, http:\/\/aslak.nu\/wp-content\/uploads\/2021\/03\/fake_family_random-1536x755.png 1536w, http:\/\/aslak.nu\/wp-content\/uploads\/2021\/03\/fake_family_random-1200x590.png 1200w, http:\/\/aslak.nu\/wp-content\/uploads\/2021\/03\/fake_family_random.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Lets add another layout &#8211; a circular one<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># choose a specific force directed layout algorithm\npos = nx.kamada_kawai_layout(family_graph)\nnx.draw_networkx(family_graph, pos, family_nodes, with_labels=True, labels=node_labels)\n<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"503\" src=\"http:\/\/aslak.nu\/wp-content\/uploads\/2021\/03\/fake_family_kamada_kawai-1024x503.png\" alt=\"\" class=\"wp-image-166\" srcset=\"http:\/\/aslak.nu\/wp-content\/uploads\/2021\/03\/fake_family_kamada_kawai-1024x503.png 1024w, http:\/\/aslak.nu\/wp-content\/uploads\/2021\/03\/fake_family_kamada_kawai-300x148.png 300w, http:\/\/aslak.nu\/wp-content\/uploads\/2021\/03\/fake_family_kamada_kawai-768x378.png 768w, http:\/\/aslak.nu\/wp-content\/uploads\/2021\/03\/fake_family_kamada_kawai-1536x755.png 1536w, http:\/\/aslak.nu\/wp-content\/uploads\/2021\/03\/fake_family_kamada_kawai-1200x590.png 1200w, http:\/\/aslak.nu\/wp-content\/uploads\/2021\/03\/fake_family_kamada_kawai.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h5 class=\"wp-block-heading\"><span class=\"has-inline-color has-accent-color\">A Short note on layout Algorithms<\/span><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Quite a few layout algorithms exist, but the most common and tunable algorithm is the Kamada Kawai algorithm. It is a force directed iterative layout, which uses the nodes degree among other metrics, in order to calculate where each node can be placed. It&#8217;s tunable as each edge in the graph can be weighted. A higher weighted edge has a stronger force impact on the layout. However the algorithm is quite expensive as the number of nodes increase. Often a combination of this and a random layout may be a trade-off of interconnected-ness and randomness in the graphs visual representation.<br>Other layouts do exist for specific domains, such as spiraling of circular layout, which have their capabilities as well. They are somewhere in between a random layout and a Kamada Kawai layout when comparing computation time. <\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Graphs A Graph is a collection of Vertices &amp; Edges. It can be Sparse, Fully connected, Planar, (un)directed &#8211; there are many types of graphs. In this case, a graph is simply a tool to visualize interconnected-ness. A vertex represents any object defined within the domain of interest. An edge represents a type of relationship [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":104,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-148","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/aslak.nu\/index.php\/wp-json\/wp\/v2\/pages\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/aslak.nu\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/aslak.nu\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/aslak.nu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/aslak.nu\/index.php\/wp-json\/wp\/v2\/comments?post=148"}],"version-history":[{"count":5,"href":"http:\/\/aslak.nu\/index.php\/wp-json\/wp\/v2\/pages\/148\/revisions"}],"predecessor-version":[{"id":192,"href":"http:\/\/aslak.nu\/index.php\/wp-json\/wp\/v2\/pages\/148\/revisions\/192"}],"up":[{"embeddable":true,"href":"http:\/\/aslak.nu\/index.php\/wp-json\/wp\/v2\/pages\/104"}],"wp:attachment":[{"href":"http:\/\/aslak.nu\/index.php\/wp-json\/wp\/v2\/media?parent=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}