<?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: Ibrahim Banat</title>
    <description>The latest articles on DEV Community by Ibrahim Banat (@ibrahimbanat).</description>
    <link>https://dev.to/ibrahimbanat</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%2F659295%2F07550dbd-58d0-4b9c-8699-1c7670780e11.jpeg</url>
      <title>DEV Community: Ibrahim Banat</title>
      <link>https://dev.to/ibrahimbanat</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ibrahimbanat"/>
    <language>en</language>
    <item>
      <title>Introduction To Graphs</title>
      <dc:creator>Ibrahim Banat</dc:creator>
      <pubDate>Wed, 30 Jun 2021 23:13:28 +0000</pubDate>
      <link>https://dev.to/ibrahimbanat/introduction-to-graphs-1p57</link>
      <guid>https://dev.to/ibrahimbanat/introduction-to-graphs-1p57</guid>
      <description>&lt;p&gt;Graph Data Structure Introduction including: adjacency list, adjacency matrix and the incidence matrix.&lt;/p&gt;

&lt;h2&gt;
  
  
  what is a graph ?
&lt;/h2&gt;

&lt;p&gt;The graph data structure is not the same as a graph you may have learned about a math class, graphs are collections of things and the relationships or connections between them. &lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9qeem5niqqchu04lfpg5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9qeem5niqqchu04lfpg5.png" alt="undirectedgraph" width="283" height="163"&gt;&lt;/a&gt; &lt;br&gt;
The data in a graph are called nodes or vertices the connections between the nodes are called edges.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs7qxgqphqernyokkp47y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs7qxgqphqernyokkp47y.png" alt="undirectedgraph (1)" width="491" height="212"&gt;&lt;/a&gt;&lt;br&gt;
one example of graphs is a social network, where the nodes are you and other people and the edges are whether two people are friend with each other.&lt;/p&gt;
&lt;h2&gt;
  
  
  Types of Graphs
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Directed:&lt;br&gt;
directed graphs are graphs with a direction and its edges.&lt;br&gt;
example of a directed graph could be the internet and web page links the nodes are web pages and the directed edges are links to other pages.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fivstljgov23ye6rjtok4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fivstljgov23ye6rjtok4.png" alt="graphhh" width="523" height="317"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Undirected Graph: &lt;br&gt;
undirected graphs are graphs without any direction on the edges between nodes. example of an undirected graph could be a social network the nodes are people and the edges are friendships.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F703eh7l23jroqv0flxtg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F703eh7l23jroqv0flxtg.png" alt="cycle-BFS" width="800" height="379"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Graph Representation
&lt;/h2&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Graph representation is a technique to store graph into the memory of computer. To represent a graph, we just need the set of vertices, and for each vertex the neighbors of the vertex (vertices which is directly connected to it by an edge).&lt;/p&gt;

&lt;p&gt;the main representations of a graph is &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; Adjacency List: 
This representation for a graph associates each vertex in the graph with the collection of its neighboring vertices or edges.
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fplo98595wvpo3pn5uc7m.PNG" alt="pic1" width="412" height="178"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;in this image A is connected to B, B is connected to A and C, and C is connected to B.&lt;br&gt;
you could show the previous relationship with text as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Node1: Node2, Node3
Node2: Node1
Node3: Node3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the JSON list representation for the previous relationship would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var underictedG = {
  Node1: ["Node2", "Node3"],
  Node2: ["Node1"],
  Node3: ["Node3"]
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and it's considered a un-directed graph because it's not showing the direction of the edges.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adjacency Matrix:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;adjacency matrix is a two-dimensional array where each nested array has the same number of elements as the outer array so it's basically a matrix of numbers where the numbers represent the edges zeroes means there is no edge or relationship and one means there is a relationship.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;A&lt;/th&gt;
&lt;th&gt;B&lt;/th&gt;
&lt;th&gt;C&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;A&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;B&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;C&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;this table shows an adjacency matrix, you can see that the labels for the nodes are on the top and left.&lt;/p&gt;

&lt;p&gt;then, the JavaScript representation of the same thing is like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let adjMatrix = [
  [0, 1, 1],
  [0, 0, 0],
  [1, 0, 0],
] 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;unlike an adjacency list each row of the matrix has to have the same number of elements as nodes in the graph here we have a 3 by 3 matrix which means we have 3 nodes our graph&lt;/p&gt;

&lt;p&gt;Adjacency matrix can be used to represent a directed graph here's &lt;br&gt;
a graph where the second node has an edge pointing toward the first node and then the third node has an edge pointing to the first node.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxzxlp3qq3elfqlxt6vmc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxzxlp3qq3elfqlxt6vmc.png" alt="image" width="371" height="171"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;the numbers in the array will change, there are only ones where a node is pointing toward another node and since there are only two points there are only two nodes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let adjMatrix = [
  [0, 0, 0],
  [1, 0, 0],
  [0, 1, 0],
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Incidence Matrix&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Like the adjacency matrix, incidence matrix is a two-dimensional array however the rows and columns means something else here.&lt;/p&gt;

&lt;p&gt;the adjacency matrix use both rows and columns to represent nodes and incidence matrix uses rows and columns to represent nodes and the columns to represent edges. this means that we can have an uneven number of rows and columns.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjn2otcozmwfapifphxy0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjn2otcozmwfapifphxy0.png" alt="image" width="396" height="180"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;each column will represent a unique edge also each edge connects two nodes to show that there is edge between two nodes. you will put a 1 in the two rows of a particular column as you can see in the diagram.&lt;/p&gt;

&lt;p&gt;edge 1 is connected to nodes A and B now look at the column for edge 1 in the incidence matrix table you will see a 1 in both the A row and the B row. this shows the edge 1 connects the nodes A&lt;br&gt;
and B.&lt;/p&gt;

&lt;p&gt;and that's what you need to know about graphs in JavaScript, see in next blog.&lt;/p&gt;

&lt;p&gt;sources: &lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=DBRW8nwZV-g&amp;amp;ab_channel=freeCodeCamp.org" rel="noopener noreferrer"&gt;source1&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.geeksforgeeks.org/graph-data-structure-and-algorithms/" rel="noopener noreferrer"&gt;source2&lt;/a&gt; &lt;/p&gt;

</description>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
