<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Iran Neto</title>
    <description>The latest articles on DEV Community by Iran Neto (@iranneto).</description>
    <link>https://dev.to/iranneto</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F248050%2Fa5f4ac90-5f0a-4a46-b665-18c245e925b4.jpeg</url>
      <title>DEV Community: Iran Neto</title>
      <link>https://dev.to/iranneto</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/iranneto"/>
    <language>en</language>
    <item>
      <title>Implementing a Graph bipartition checker in Python</title>
      <dc:creator>Iran Neto</dc:creator>
      <pubDate>Wed, 18 Dec 2019 00:40:07 +0000</pubDate>
      <link>https://dev.to/iranneto/implementing-graph-bipartition-checker-in-python-25e6</link>
      <guid>https://dev.to/iranneto/implementing-graph-bipartition-checker-in-python-25e6</guid>
      <description>&lt;p&gt;As a software developer I try make a routine that includes the practicing of my skills to solve mathematic/algorithm problems. Some days ago I found out a Google annual code competition called Code Jam and the very first problem I saw puzzled me (I will talk about it). After some research I was able to solve it through a graph bipartition checker algorithm and decided to write the path I took to come up with the solution.&lt;br&gt;
Let’s do it.&lt;/p&gt;
&lt;h1&gt;
  
  
  Graph
&lt;/h1&gt;

&lt;p&gt;A Graph is a non-linear abstract data structure well known in mathematics. They are used to model pairwise relations between objects through structures/concepts as nodes (or simply “points”) and edges (also called “links”, “arcs” or “lines” that connects two nodes) [1][2]. The study of graphs and its properties is called Graph Theory and was developed by Dénes König in 1936, a brilliant mathematician which wrote the very first book about graph theory [3]. Graphs are used to model lots of situations that happens in real world like most efficient airplane path from x to y or shortest distance between two points of a city (GPS algorithms), and so on.&lt;/p&gt;
&lt;h1&gt;
  
  
  The problem
&lt;/h1&gt;

&lt;p&gt;2014 Grad Test — Practice round.&lt;/p&gt;
&lt;h2&gt;
  
  
  Bad Horse Problem
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;As the leader of the Evil League of Evil, Bad Horse has a lot of problems to deal with. Most recently, there have been far too many arguments and far too much backstabbing in the League, so much so that Bad Horse has decided to split the league into two departments in order to separate troublesome members. Being the Thoroughbred of Sin, Bad Horse isn’t about to spend his valuable time figuring out how to split the League members by himself. That what he’s got you — his loyal henchman — for.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://code.google.com/codejam/contest/2933486/dashboard"&gt;https://code.google.com/codejam/contest/2933486/dashboard&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Solution
&lt;/h1&gt;

&lt;p&gt;After reading the description some ideas came to my mind before initiate to code a possible solution.&lt;/p&gt;

&lt;p&gt;First, I will have to &lt;strong&gt;store pair’s names&lt;/strong&gt; and somehow &lt;strong&gt;represent the relation&lt;/strong&gt; between then. Also, I have to be able to check the possibility to separate the pairs into 2 groups — No pair should be in the same group. With a bit more searches I’ve found out that this is classic problem of &lt;strong&gt;graph bipartite&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I’ve realized it’s a important step before start coding in super matrix hacker style. Analyse it, get into problem’s head.&lt;/p&gt;
&lt;h2&gt;
  
  
  Bipartite Graph
&lt;/h2&gt;

&lt;p&gt;A bipartite graph is a graph which all its nodes can be separated in two groups so that each element of one group is only related to elements of the other group. It’s also knows as bicolored graph (each group would represent a color, so every element in the graph is only related to another color element). They are very used in Petri Networks (Concurrency Systems resources simulation) and others real world situations [4].&lt;/p&gt;

&lt;p&gt;
    &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dpLIqz9j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://study.com/cimages/multimages/16/bipartite4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dpLIqz9j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://study.com/cimages/multimages/16/bipartite4.jpg"&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  Algorithm
&lt;/h1&gt;

&lt;p&gt;The algorithm to check a graph bipartition can be very similar to DFS in trees data structures (Depth First Search). DFS algorithm it’s used to find the depth of a tree and works by choosing the root element and dive into their neighbors until there aren’t no element’s child or leaf.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Curious fact: Every tree is a graph bipartite&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;
    &lt;a href="https://camo.githubusercontent.com/6838d1b149fa8d2e3092222f5a687c3a3d5e8766/68747470733a2f2f692e696d6775722e636f6d2f525647746e32322e676966" class="article-body-image-wrapper"&gt;&lt;img src="https://camo.githubusercontent.com/6838d1b149fa8d2e3092222f5a687c3a3d5e8766/68747470733a2f2f692e696d6775722e636f6d2f525647746e32322e676966"&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;To add the function of bipartition check we need to initiate another structure to store the group (or color) information of each element. Besides that, it’s necessary also to check the groups belonging to each graph node given its neighbors group&lt;/p&gt;

&lt;p&gt;The pseudocode analysis will be divided in two parts to a better understanding.&lt;/p&gt;

&lt;h1&gt;
  
  
  Pseudocode - Controller
&lt;/h1&gt;

&lt;p&gt;Let’s check the pseudo algorithm.&lt;/p&gt;

&lt;p&gt;The first part will function as a initialize or “controller” assuring that the routine will be executed for all nodes in graph.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;OBS: To explain the pseudocode I’m going to assume the following constants&lt;br&gt;
V = graph node&lt;br&gt;
C1 = group 01 value&lt;br&gt;
graph = the data structure represented in code&lt;br&gt;
C2 = group 02 value&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FOR each graph node v
    Initialize v group/color c with a neutral value, representing no color/group
end FOR

FOR each graph node V
    Initialize V group value C = C1
end FOR
FOR every graph node V
    IF group C is C1:
        IF NOT SET_GROUP_VALUE(graph, V, C1)
            return FALSE
        end IF
    end IF
end FOR
return TRUE

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;At the first loop we’re going to initiate all nodes with group 01 value. At the second loop for every node that has the group 01 value we’re going to check its neighbors and, if possible, set it a different group value.&lt;/p&gt;
&lt;h1&gt;
  
  
  Pseudocode — Recursive Search
&lt;/h1&gt;

&lt;p&gt;The second part is the recursive algorithm that holds the logic to check if a node can be part of a different group than his neighbors.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- Start a routine SET_GROUP_VALUE(graph, V, C) -- Ignore this line

SET the group of V as C
FOR every neighbors W of V
    IF group value C of W is neutral
        IF NOT SET_GROUP_VALUE(graph, W, C2)
            return FALSE
        end IF
    ELSE
        IF group value of W is C
            return FALSE
        end IF
    end ELSE
end FOR
return TRUE

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The first loop start to dive into the subset of V nodes (subtree with root in V). If V’s neighbor W have group 01 value then the algorithm will check if it’s possible to give group 02 value.&lt;/p&gt;
&lt;h1&gt;
  
  
  Implementation
&lt;/h1&gt;

&lt;p&gt;As described in the problem, our goal is to execute T test cases of &lt;strong&gt;M&lt;/strong&gt; element pairs described in a file. The first line of this file will contain the &lt;strong&gt;T&lt;/strong&gt; value followed by &lt;strong&gt;M&lt;/strong&gt; number of pairs, thus, the next &lt;strong&gt;M&lt;/strong&gt; lines will contain the &lt;strong&gt;M&lt;/strong&gt; pairs information. The output will be also a file that describes the test case number and if the set of &lt;strong&gt;M&lt;/strong&gt; pairs names can be divided into two groups, or, if the set of pairs can be represented as a bipartite graph.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- Input file example -- Ignore this line
2
1
Dead_Bowie Fake_Thomas_Jefferson
3
Dead_Bowie Fake_Thomas_Jefferson
Fake_Thomas_Jefferson Fury_Leika
Fury_Leika Dead_Bowie
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- Output file example -- Ignore this line
Case #1: Yes
Case #2: No
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;There are some ways to represent a graph in code [5], one of them is an adjacency matrix and that is how it’s gonna be represent in this article, but with some small modifications the solution can be able to work as well. Here, I will represent it as a python dictionary: The keys will be the nodes and the key’s value will be a list of all its neighbors. Here’s an example&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;g = { 
    “a” : [“d”],
    “b” : [“c”],
    “c” : [“b”, “c”, “d”, “e”],
    “d” : [“a”, “c”],
    “e” : [“c”],
    “f” : []
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h1&gt;
  
  
  Code
&lt;/h1&gt;

&lt;p&gt;Let’s check my implementation of pseudocode.&lt;br&gt;
&lt;em&gt;OBS: color = group value&lt;/em&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;From line 1 to 27 is implemented the algorithm we’ve seen before in the pseudocode. In line 31, I’m getting from input file (which was in the same directory of this code) the number of tests which will be done.&lt;/p&gt;

&lt;p&gt;From line 35 until the end of the file we’re going to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For each test case (&lt;strong&gt;iTestCase&lt;/strong&gt;), retrieve the number of pairs (&lt;strong&gt;nPairs&lt;/strong&gt;) that’s going to be analyzed.&lt;/li&gt;
&lt;li&gt;For each pair, we’re going to get the names described.&lt;/li&gt;
&lt;li&gt;From line 40 until 50, it’s going to construct the dictionary based on the pair connection described in file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The output tell us which test case is represents a bipartite graph or not.&lt;/p&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;p&gt;I’m a Junior software developer in Brazil and I just fascinated by technology and programming.&lt;/p&gt;

&lt;p&gt;So please, any doubts, hints or comments are more than welcome!&lt;/p&gt;

&lt;p&gt;Follow me in Github, Twitter or Linkedin.&lt;/p&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;h1&gt;
  
  
  References
&lt;/h1&gt;

&lt;p&gt;[1] &lt;a href="https://mathworld.wolfram.com/Graph.html"&gt;https://mathworld.wolfram.com/Graph.html&lt;/a&gt;&lt;br&gt;
[2] &lt;a href="https://en.wikipedia.org/wiki/Graph_theory"&gt;https://en.wikipedia.org/wiki/Graph_theory&lt;/a&gt;&lt;br&gt;
[3] &lt;a href="https://pt.wikipedia.org/wiki/D%C3%A9nes_K%C3%B6nig"&gt;https://pt.wikipedia.org/wiki/D%C3%A9nes_K%C3%B6nig&lt;/a&gt;&lt;br&gt;
[4] &lt;a href="https://pt.wikipedia.org/wiki/Grafo_bipartido"&gt;https://pt.wikipedia.org/wiki/Grafo_bipartido&lt;/a&gt;&lt;br&gt;
[5] &lt;a href="https://www.geeksforgeeks.org/graph-and-its-representations/"&gt;https://www.geeksforgeeks.org/graph-and-its-representations/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>algorithms</category>
      <category>googlecodejam</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Design Patterns to a beginner developer</title>
      <dc:creator>Iran Neto</dc:creator>
      <pubDate>Sat, 14 Dec 2019 19:01:56 +0000</pubDate>
      <link>https://dev.to/iranneto/design-patterns-to-a-beginner-developer-lb5</link>
      <guid>https://dev.to/iranneto/design-patterns-to-a-beginner-developer-lb5</guid>
      <description>&lt;p&gt;As a beginner developer and fresh graduated boy I’ve started to look into some base concepts of programming in order to keep growing up as a professional. I realized that there are dozens of concepts that students usually take for granted and are really important in “real life”. One of them is Design Patterns.&lt;/p&gt;

&lt;h1&gt;
  
  
  Design Patterns
&lt;/h1&gt;

&lt;p&gt;Design Patterns aren’t the silver bullet that solve all your problems. After years of programming languages been used in the world, software developers noticed that some logical solutions have been used repeatedly over and over again. In 1995, four developers published Design Patterns: Elements of Reusable Object-Oriented Software with 23 patterns solving various problems of object-oriented design and became a best-seller very quickly. Nowadays it’s known by the GoF book. GoF = Gang of Four.&lt;/p&gt;

&lt;p&gt;That’s what make Design Patterns really important, they’re manuals of how to deal with logical problems in programming. If you’re a developer you should know and study about it because during your carrer you will face this problems every day.&lt;/p&gt;

&lt;p&gt;Every design pattern can be described in terms of three aspects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Intent briefly describes both the problem and the solution.&lt;/li&gt;
&lt;li&gt;Motivation explains the problem and the solution the pattern makes possible.&lt;/li&gt;
&lt;li&gt;Structure shows each part of the pattern and how they are related.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Patterns also can be classified into groups of intents&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creational patterns provide object creation mechanisms that increase flexibility and reuse of existing code.&lt;/li&gt;
&lt;li&gt;Structural patterns explain how to assemble objects and classes into larger structures, while keeping the structures flexible and efficient.&lt;/li&gt;
&lt;li&gt;Behavioral patterns take care of effective communication and the assignment of responsibilities between objects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;References:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://refactoring.guru/"&gt;https://refactoring.guru/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Any hints or comments are welcome, as many information and discussion better :)&lt;/p&gt;

</description>
      <category>designpatterns</category>
      <category>softskills</category>
      <category>softwaredevelopment</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
