<?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: Diego Pessoa</title>
    <description>The latest articles on DEV Community by Diego Pessoa (@diegoep).</description>
    <link>https://dev.to/diegoep</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%2F221305%2F92c18670-77f4-4bdd-a609-89fc08399513.jpg</url>
      <title>DEV Community: Diego Pessoa</title>
      <link>https://dev.to/diegoep</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/diegoep"/>
    <language>en</language>
    <item>
      <title>How to create and plot graphs in Python</title>
      <dc:creator>Diego Pessoa</dc:creator>
      <pubDate>Thu, 29 Jun 2023 23:17:44 +0000</pubDate>
      <link>https://dev.to/diegoep/how-to-create-and-plot-graphs-in-python-gn5</link>
      <guid>https://dev.to/diegoep/how-to-create-and-plot-graphs-in-python-gn5</guid>
      <description>&lt;h2&gt;
  
  
  Step 1: Install the python-igraph package
&lt;/h2&gt;

&lt;p&gt;To get started, you'll need to install the python-igraph package. You can do this by running the following command in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install python-igraph
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Import the necessary modules
&lt;/h2&gt;

&lt;p&gt;Once you have installed the package, you need to import the required modules in your Python script. You will typically need the igraph module and the plot module for visualization. Include the following lines at the beginning of your script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import igraph as ig
from igraph import plot
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Create a graph object
&lt;/h2&gt;

&lt;p&gt;To create a graph, you can start by initializing an empty Graph object. You can then add vertices and edges to this object to define the structure of your graph. Here's an example that creates a simple graph with three vertices and two edges:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Create an empty graph
g = ig.Graph()

# Add vertices
g.add_vertices(3)

# Add edges
g.add_edges([(0, 1), (1, 2)])

# Print the graph
print(g)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Visualize the graph
&lt;/h2&gt;

&lt;p&gt;Now that you have created the graph, you can visualize it using the plot module. The plot function takes the graph object as input and generates a visualization of the graph. You can save the plot to a file or display it directly. Here's an example that saves the plot as a PNG image:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;plot(g, "graph.png")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 5: Customize the graph appearance
&lt;/h2&gt;

&lt;p&gt;You can further customize the appearance of the graph by modifying various attributes. For example, you can change the color, shape, and size of the vertices, as well as the color and width of the edges. Here's an example that demonstrates some customization options:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Customize vertex attributes
g.vs["color"] = "blue"
g.vs["size"] = 20
g.vs["shape"] = "square"

# Customize edge attributes
g.es["color"] = "red"
g.es["width"] = 2

# Create the plot
plot(g, "graph.png")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are just a few examples of what you can do with python-igraph. The package offers many more functionalities for working with graphs, including various graph algorithms and community detection methods.&lt;/p&gt;

</description>
      <category>datastructures</category>
      <category>graphs</category>
    </item>
    <item>
      <title>How to easily create Graphs in Python</title>
      <dc:creator>Diego Pessoa</dc:creator>
      <pubDate>Thu, 29 Jun 2023 14:36:48 +0000</pubDate>
      <link>https://dev.to/diegoep/how-to-easily-create-graphs-in-python-24hk</link>
      <guid>https://dev.to/diegoep/how-to-easily-create-graphs-in-python-24hk</guid>
      <description>&lt;p&gt;Graphs are fundamental data structures used to represent relationships between objects. They consist of a collection of vertices (also known as nodes) and edges (also known as arcs) that connect these vertices. Graphs are widely used in various fields, including computer science, mathematics, and social network analysis, to model and analyze complex relationships and networks.&lt;/p&gt;

&lt;p&gt;A graph can be defined as a tuple G = (V, E), where V represents the set of vertices and E represents the set of edges. The set of vertices, V, can be finite or infinite. Each vertex in V is uniquely identified and can be associated with additional information known as vertex properties. &lt;/p&gt;

&lt;p&gt;The set of edges, E, represents the connections between vertices. Each edge in E is an unordered pair of vertices (u, v), where u and v belong to V. Edges can be directed (having a specific direction from one vertex to another) or undirected (without any specific direction). In the case of directed edges, the pair (u, v) represents an edge originating from vertex u and ending at vertex v.&lt;/p&gt;

&lt;p&gt;For example, let's consider a graph G = (V, E) with the following sets:&lt;/p&gt;

&lt;p&gt;V = {1, 2, 3, 4, 5}&lt;/p&gt;

&lt;p&gt;E = {(1, 2), (1, 3), (2, 3), (2, 4), (3, 4), (4, 5)}&lt;/p&gt;

&lt;p&gt;In this example, V represents the set of vertices, and E represents the set of edges.&lt;/p&gt;

&lt;p&gt;The set V contains five vertices: {1, 2, 3, 4, 5}.&lt;/p&gt;

&lt;p&gt;The set E contains six edges, where each edge is represented by an unordered pair of vertices. The edges are:&lt;br&gt;
{(1, 2), (1, 3), (2, 3), (2, 4), (3, 4), (4, 5)}.&lt;/p&gt;

&lt;p&gt;This graph can be visually represented as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      1
     / \
    2---3
   / \ / \
  4---5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is an implementation of a basic graph in Python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from collections import defaultdict

class Graph:
    """Basic implementation of a graph."""

    def __init__(self, edges, directed=False):
        """Initialize the basic structures of the graph."""
        self.adj = defaultdict(set) # defaultdict: key not found won't raise an error, it will create a new one
        self.directed = directed
        self.add_edges(edges)

    def get_vertices(self):
        """Return the list of vertices in the graph."""
        return list(self.adj.keys())

    def get_edges(self):
        """Return the list of edges in the graph."""
        return [(k, v) for k in self.adj.keys() for v in self.adj[k]]

    def add_edges(self, edges):
        """Add edges to the graph."""
        for u, v in edges:
            self.add_arc(u, v)

    def add_arc(self, u, v):
        """Add a connection (arc) between nodes 'u' and 'v'."""
        self.adj[u].add(v)
        # If the graph is undirected, we need to add arcs in both directions.
        if not self.directed:
            self.adj[v].add(u)

    def has_edge(self, u, v):
        """Does an edge exist between vertices 'u' and 'v'?"""
        return u in self.adj and v in self.adj[u]

    def __len__(self):
        return len(self.adj)

    def __str__(self):
        return '{}({})'.format(self.__class__.__name__, dict(self.adj))

    def __getitem__(self, v):
        return self.adj[v]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's an example to create the graph described earlier:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;edges = [ (1, 2), (1, 3), (2, 3), (2, 4), (3, 4), (4, 5) ]

g = Graph(edges, directed=False)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This implementation provides basic functionalities for a graph, including initializing the graph with a set of edges, adding edges and arcs, checking if an edge exists between two vertices, and retrieving the vertices and edges of the graph. It can handle both directed and undirected graphs.&lt;/p&gt;

&lt;p&gt;Feel free to use this implementation as a starting point for working with graphs in your projects or applications. Remember to create an instance of the Graph class and pass the appropriate edges to initialize the graph.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>datastructures</category>
    </item>
  </channel>
</rss>
