<?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: Kousik Raj</title>
    <description>The latest articles on DEV Community by Kousik Raj (@rajkousik).</description>
    <link>https://dev.to/rajkousik</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%2F811276%2F66be7eab-a668-4677-9b4d-08f82b3e6fdc.jpg</url>
      <title>DEV Community: Kousik Raj</title>
      <link>https://dev.to/rajkousik</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rajkousik"/>
    <language>en</language>
    <item>
      <title>The Top 10 Algorithms Every Programmer Should Know In Graph Data Structure!</title>
      <dc:creator>Kousik Raj</dc:creator>
      <pubDate>Wed, 09 Feb 2022 09:36:20 +0000</pubDate>
      <link>https://dev.to/rajkousik/the-top-10-algorithms-every-programmer-should-know-in-graph-data-structure-40a5</link>
      <guid>https://dev.to/rajkousik/the-top-10-algorithms-every-programmer-should-know-in-graph-data-structure-40a5</guid>
      <description>&lt;h2&gt;
  
  
  Why to learn these graph algorithms?
&lt;/h2&gt;

&lt;p&gt;Graph algorithms are a set of instructions that traverse (visits nodes of a) graph. Some algorithms are used to find a specific node or the path between two given nodes.&lt;/p&gt;

&lt;p&gt;Graphs are very useful data structures which can be to model various problems. These algorithms have direct applications on Social Networking sites, State Machine modeling and many more. I have attached my source code for all the algorithms discussed below. You can use it for your referral. &lt;/p&gt;

&lt;h3&gt;
  
  
  1. Breadth First Searching
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Breadth First Search&lt;/strong&gt; is a recursive algorithm for searching all the vertices of a graph or tree data structure.&lt;br&gt;
Breadth-first search is a graph traversal algorithm that starts traversing the graph from the root node and explores all the neighboring nodes. Then, it selects the nearest node and explores all the unexplored nodes. While using BFS for traversal, any node in the graph can be considered as the root node.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;An simple algorithm to be remembered for BFS:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it in a queue.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If no adjacent vertex is found, remove the first vertex from the queue.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Repeat Rule 1 and Rule 2 until the queue is empty.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;The Diagrammatic representation of BFS Traversal:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644347005226%2FePwZ0LVJF.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644347005226%2FePwZ0LVJF.jpeg" alt="bfs.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reference code :&lt;/strong&gt; &lt;a href="https://github.com/RajKousik/GraphDataStructure/blob/main/BFSTraversal.cpp" rel="noopener noreferrer"&gt;Breadth First Searching&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Depth First Searching
&lt;/h3&gt;

&lt;p&gt;Depth first Search or Depth first traversal is a recursive algorithm for searching all the vertices of a graph or tree data structure. &lt;br&gt;
Depth First Search (DFS) algorithm traverses a graph in a depthward motion and uses a stack to remember to get the next vertex to start a search, when a dead end occurs in any iteration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;An simple algorithm to be remembered for DFS:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push it in a stack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If no adjacent vertex is found, pop up a vertex from the stack. (It will pop up all the vertices from the stack, which do not have adjacent vertices.)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Repeat Rule 1 and Rule 2 until the stack is empty.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;The Diagrammatic representation of DFS Traversal:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644347262649%2FQRSUdEvSD.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644347262649%2FQRSUdEvSD.jpeg" alt="dfs.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reference code :&lt;/strong&gt; &lt;a href="https://github.com/RajKousik/GraphDataStructure/blob/main/DFSTraversal.cpp" rel="noopener noreferrer"&gt;Depth First Searching&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Topological Sorting
&lt;/h3&gt;

&lt;p&gt;Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge u v, vertex u comes before v in the ordering. Topological Sorting for a graph is not possible if the graph is not a DAG.&lt;br&gt;
There can be more than one topological sorting for a graph.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;An simple algorithm to be remembered for Topological sort:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;mark u as visited&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;for all vertices v which is adjacent with u, do&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;2.1 if v is not visited, then&lt;/p&gt;

&lt;p&gt;2.2 TopoSort(c, visited, stack)&lt;/p&gt;

&lt;p&gt;2.3 done&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;push u into a stack&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;The Diagrammatic representation of Topological sort&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644347962433%2FisQhR1CxR.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644347962433%2FisQhR1CxR.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One of the Topological sort for this graph is:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;5 -&amp;gt; 4 -&amp;gt; 2 -&amp;gt; 3 -&amp;gt; 1 -&amp;gt; 0&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reference code :&lt;/strong&gt; &lt;a href="https://github.com/RajKousik/GraphDataStructure/blob/main/TopologicalSort.cpp" rel="noopener noreferrer"&gt;Topological Sorting&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Detecting a cycle using Kahn's Algorithm
&lt;/h3&gt;

&lt;p&gt;Kahn’s topological sort algorithm is cycle detection algorithm, which also provides an efficient way to print the topological order.&lt;br&gt;
Kahn’s topological sort algorithm works by finding vertices with no incoming edges and removing all outgoing edges from these vertices.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reference code :&lt;/strong&gt; &lt;a href="https://github.com/RajKousik/GraphDataStructure/blob/main/CycleDetection.cpp" rel="noopener noreferrer"&gt;Detecting a cycle using Kahn's Algorithm&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Dijkstra's Algorithm
&lt;/h3&gt;

&lt;p&gt;Dijkstra's algorithm allows us to find the shortest path between any two vertices of a graph.&lt;br&gt;
It differs from the minimum spanning tree because the shortest distance between two vertices might not include all the vertices of the graph.&lt;/p&gt;

&lt;p&gt;It is a single-source shortest path algorithm. Here, single-source means that only one source is given, and we have to find the shortest path from the source to all the nodes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644348986664%2F0G1xuDPrU.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644348986664%2F0G1xuDPrU.jpeg" alt="dik1.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After applying Dijkstra's Algorithm:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644348996710%2F6DA6Oq-El.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644348996710%2F6DA6Oq-El.jpeg" alt="dik2.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reference code :&lt;/strong&gt; &lt;a href="https://github.com/RajKousik/GraphDataStructure/blob/main/Dijkstra'sAlgorithm.cpp" rel="noopener noreferrer"&gt;Dijkstra's Algorithm&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Bellman Ford's Algorithm
&lt;/h3&gt;

&lt;p&gt;Bellman Ford algorithm helps us find the shortest path from a vertex to all other vertices of a weighted graph.&lt;br&gt;
It is similar to Dijkstra's algorithm but it can detect graphs in which edges can have negative weight cycles.&lt;/p&gt;

&lt;p&gt;Dijkstra's Algorithm (can't able to detect negative weight cycle) can give an incorrect result because they can go through a negative weight cycle and reduce the path length.&lt;/p&gt;

&lt;p&gt;Dijkstra doesn’t work for Graphs with negative weights, Bellman-Ford works for such graphs. Bellman-Ford is also simpler than Dijkstra and suites well for distributed systems.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644349191573%2FUC8Jutlwf.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644349191573%2FUC8Jutlwf.jpeg" alt="bell1.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Output Of Bellman Ford's Algorithm:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644349208711%2F7sinOsJGt.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644349208711%2F7sinOsJGt.jpeg" alt="bell2.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reference code :&lt;/strong&gt; &lt;a href="https://github.com/RajKousik/GraphDataStructure/blob/main/BellmanFord.cpp" rel="noopener noreferrer"&gt;Bellman Ford's Algorithm&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Floyd-Warshall Algorithm
&lt;/h3&gt;

&lt;p&gt;Floyd-Warshall Algorithm is an algorithm for finding the shortest path between all the pairs of vertices in a weighted graph. This algorithm works for both the directed and undirected weighted graphs. But, it does not work for the graphs with negative cycles (where the sum of the edges in a cycle is negative). Here, a dynamic programming concept will be used.&lt;/p&gt;

&lt;p&gt;Algorithm Behind Floyd-Warshall Algorithm:&lt;/p&gt;

&lt;p&gt;Dij(k) ← min ( Dij(k-1), Dik(k-1)+Dkj(k-1) )&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reference code :&lt;/strong&gt; &lt;a href="https://github.com/RajKousik/GraphDataStructure/blob/main/FLoydWarshall.cpp" rel="noopener noreferrer"&gt;Floyd-Warshall Algorithm&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  8. Prim's Algorithm
&lt;/h3&gt;

&lt;p&gt;Prim's Algorithm is a greedy algorithm that is used to find the minimum spanning tree from a graph. Prim's algorithm finds the subset of edges that includes every vertex of the graph such that the sum of the weights of the edges can be minimized.&lt;/p&gt;

&lt;p&gt;*&lt;strong&gt;&lt;em&gt;An simple algorithm to be remembered for Prim's Algorithm:&lt;/em&gt;&lt;/strong&gt;*&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Initialize the minimum spanning tree with a vertex chosen at random.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Find all the edges that connect the tree to new vertices, find the minimum and add it to the tree.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Keep repeating step 2 until we get a minimum spanning tree&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644383396035%2FDVZKgQTNq.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644383396035%2FDVZKgQTNq.jpeg" alt="prim1.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644383428636%2FBP41C4Ke7.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644383428636%2FBP41C4Ke7.jpeg" alt="prim2.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reference code :&lt;/strong&gt; &lt;a href="https://github.com/RajKousik/GraphDataStructure/blob/main/PrimsAlgo.cpp" rel="noopener noreferrer"&gt;Prim's Algorithm&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  9. Kruskal's Algorithm
&lt;/h3&gt;

&lt;p&gt;Kruskal's Algorithm is used to find the minimum spanning tree for a connected weighted graph. The main target of the algorithm is to find the subset of edges by using which we can traverse every vertex of the graph. It follows the greedy approach that finds an optimum solution at every stage instead of focusing on a global optimum.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;An simple algorithm to be remembered for Prim's Algorithm:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First, sort all the edges from low weight to high.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now, take the edge with the lowest weight and add it to the spanning tree. If the edge to be added creates a cycle, then reject the edge.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Continue to add the edges until we reach all vertices, and a minimum spanning tree is created.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Reference code :&lt;/strong&gt; &lt;a href="https://github.com/RajKousik/GraphDataStructure/blob/main/KruskalsAlgo.cpp" rel="noopener noreferrer"&gt;Kruskal's Algorithm&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  10. Kosaraju's Algorithm
&lt;/h3&gt;

&lt;p&gt;If we can reach every vertex of a component from every other vertex in that component then it is called a Strongly Connected Component (SCC). Single node is always a SCC. The Brute force method will be Floyd Warshall Algorithm. But for better time complexity, we can use Kosaraju's Algorithm.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;An simple algorithm to be remembered for Prim's Algorithm:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Perform DFS traversal of the graph. Push node to stack before returning.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Find the transpose graph by reversing the edges.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pop nodes one by one from the stack and again to DFS on the modified graph.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644384005676%2FQvPPRY0ii.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644384005676%2FQvPPRY0ii.jpeg" alt="kosa1.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644384013067%2FPuN--mHzO.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644384013067%2FPuN--mHzO.jpeg" alt="kosa2.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reference code :&lt;/strong&gt; &lt;a href="https://github.com/RajKousik/GraphDataStructure/blob/main/Kosaraju'sAlgo.cpp" rel="noopener noreferrer"&gt;Kosaraju's Algorithm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These are the most important algorithms one should know as a programmer in graph data structure.&lt;br&gt;
I will be uploading detailed explanation of each algorithm in upcoming posts. Stay Tuned!&lt;/p&gt;

&lt;p&gt;Did you find the Algorithms listed in this article helpful?&lt;br&gt;
If you enjoyed this article, share it with your friends and colleagues! &lt;br&gt;
Leave your comments below!&lt;/p&gt;

&lt;p&gt;Here's my another article: &lt;a href="https://kousikraj.hashnode.dev/learn-data-structures-and-algorithms" rel="noopener noreferrer"&gt;Learn Data Structures and Algorithms&lt;/a&gt;&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>cpp</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Learn Data Structures and Algorithms</title>
      <dc:creator>Kousik Raj</dc:creator>
      <pubDate>Tue, 08 Feb 2022 07:40:20 +0000</pubDate>
      <link>https://dev.to/rajkousik/learn-data-structures-and-algorithms-455m</link>
      <guid>https://dev.to/rajkousik/learn-data-structures-and-algorithms-455m</guid>
      <description>&lt;h2&gt;
  
  
  Why to Learn Data Structures and Algorithms?
&lt;/h2&gt;

&lt;p&gt;Data structures and algorithms (DSA) goes through solutions to standard problems in detail and gives you an insight into how efficient it is to use each one of them. It also teaches you the science of evaluating the efficiency of an algorithm. This enables you to choose the best of various choices.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Algorithms?
&lt;/h2&gt;

&lt;p&gt;In computer programming terms, an algorithm is a set of well-defined instructions to solve a particular problem. Informally, an algorithm is nothing but a mention of steps to solve a problem. It takes a set of input and produces a desired output. For Example,&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;An algorithm to find largest of two numbers:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Take two numbers as an input&lt;/li&gt;
&lt;li&gt;Compare number1 with number2 with '&amp;gt;'. If it is greater, then store number1 in another variable named max&lt;/li&gt;
&lt;li&gt;Else it is obvious that number2 is greater. So, store number2 in the variable max.&lt;/li&gt;
&lt;li&gt;Print the variable max.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is how a algorithm looks like. It is independent of Programming languages. It is just a step-by-step solution to given a problem which could be understood by everyone. &lt;/p&gt;

&lt;h2&gt;
  
  
  Data Structures
&lt;/h2&gt;

&lt;p&gt;Data Structures are the programmatic way of storing data so that data can be used efficiently. Almost every enterprise application uses various types of data structures in one or the other way. &lt;br&gt;
Data structure is a storage that is used to store and organize data. It is a way of arranging data on a computer so that it can be accessed and updated efficiently.&lt;br&gt;
Depending upon the requirement, we can select a particular data structure.&lt;/p&gt;

&lt;p&gt;For example, if you want to store data sequentially in the memory, then you can go for Array data structure&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--b5478iHB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6rmmsiztkqbpcuk0jx10.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--b5478iHB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6rmmsiztkqbpcuk0jx10.jpg" alt="Image description" width="700" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Remember these two simple equations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Related data + Permissible operations on the data = Data Structures&lt;/li&gt;
&lt;li&gt;Data structures + Algorithms = Programs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;NOTE :&lt;/strong&gt; Data structure and data types are slightly different. Data structure is the collection of data types arranged in a specific order.&lt;/p&gt;

&lt;h2&gt;
  
  
  Classifications of Data Structure
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RQ6yroRH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5jlau7ctjfd46qycwpr0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RQ6yroRH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5jlau7ctjfd46qycwpr0.png" alt="Image description" width="665" height="303"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Primitive Data Structure&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Integer&lt;/li&gt;
&lt;li&gt;Float&lt;/li&gt;
&lt;li&gt;Boolean&lt;/li&gt;
&lt;li&gt;Character&lt;/li&gt;
&lt;li&gt;Pointer, etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Non-Primitive Data Structure&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;i) Linear Data Structure&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Array&lt;/li&gt;
&lt;li&gt;Linked Lists&lt;/li&gt;
&lt;li&gt;Stack&lt;/li&gt;
&lt;li&gt;Queue&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ii) Non-linear Data Structure&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Trees&lt;/li&gt;
&lt;li&gt;Graphs&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Linear Data Structure
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;linear data structure&lt;/strong&gt; has data elements connected to each other so that elements are arranged in a sequential manner and each element is connected to the element in front of it and behind it. This way, the structure can be traversed in a single run.&lt;/p&gt;

&lt;p&gt;Linear data structures can be implemented easily as computer memory is also arranged in a linear manner.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Arrays
&lt;/h3&gt;

&lt;p&gt;One of the simplest data structures, an array is a collection of items that are stored sequentially. An array contains values or variables—known as “elements”—of the same data type and is of a fixed size, so you cannot change the size of an array. Each item in an array is indexed starting with 0.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gWa3pZm---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7u2lfp6nyoy7rz16ff2c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gWa3pZm---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7u2lfp6nyoy7rz16ff2c.png" alt="Image description" width="880" height="307"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Usage:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Arrays are commonly used as structures for building other, more complicated data structures. They are also used for sorting algorithms.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Linked Lists
&lt;/h3&gt;

&lt;p&gt;A linked list stores a collection of items in a linear order. Each element, or node, in a linked list contains a data item, as well as a reference, or link, to the next item in the list. In Other words, Data elements are connected through a series of nodes and, each node contains the data items and address to the next node.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hZSsG5Qq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zbpamsoookkv3u1plicp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hZSsG5Qq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zbpamsoookkv3u1plicp.png" alt="Image description" width="437" height="115"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Usage:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Linked lists are used for symbol table management in switching between programs using Alt + Tab (On a PC).&lt;br&gt;
Multiplayer games use a circular list to swap between players in a loop.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Stacks
&lt;/h3&gt;

&lt;p&gt;A stack stores a collection of items in the linear order that operations are applied. This order could be last in, first out (LIFO) or first in, Last out (FILO). That is, the last element stored in a stack will be removed first. It works just like a pile of plates where the last plate kept on the pile will be removed first.&lt;/p&gt;

&lt;p&gt;You can “push” a new element onto the top of the stack, or you can “pop,” deleting the element inserted last which is at the top of the stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hTzPy3mc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ur8z53o17i0icmxz7om3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hTzPy3mc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ur8z53o17i0icmxz7om3.png" alt="Image description" width="475" height="250"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Usage:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Stacks can be used for Memory Management.&lt;br&gt;
Stack data structures are used in backtracking problems.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Queues
&lt;/h3&gt;

&lt;p&gt;A queue functions similarly to a stack, but instead of being a LIFO structure, it is a FIFO (First In First Out) structure. The easiest way to think about a queue is to think of a line of people waiting to enter a building. The person at the beginning of the line will enter the building first, while the person at the end will enter last.&lt;/p&gt;

&lt;p&gt;You can enqueue an element in this structure, which means inserting the element to the end of the queue. You can also dequeue an element, which means deleting an element from the beginning of the queue. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QJsRuxVy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mkkqu202cvtijsf61r5z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QJsRuxVy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mkkqu202cvtijsf61r5z.png" alt="Image description" width="561" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Usage:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;In real life scenario, Call Center phone systems uses Queues to hold people calling them in an order, until a service representative is free.&lt;/p&gt;

&lt;h2&gt;
  
  
  Non-Linear Data Structure
&lt;/h2&gt;

&lt;p&gt;Unlike linear data structures, elements in non-linear data structures are not in any sequence. Instead they are arranged in a hierarchical manner where one element will be connected to one or more elements.&lt;/p&gt;

&lt;p&gt;As the arrangement is non-sequential, so the data elements cannot be traversed or accessed in a single run. In the case of linear data structure, element is connected to two elements (previous and the next element), whereas, in the non-linear data structure, an element can be connected to more than two elements.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Graphs
&lt;/h3&gt;

&lt;p&gt;It is a non-linear data structure that consists of various linked nodes. It has a hierarchical tree structure that forms a parent-child relationship. In graph data structure, each node is called vertex and each vertex is connected to other vertices through edges. It is a collection of nodes that have data and are connected to other nodes.&lt;/p&gt;

&lt;p&gt;The diagrammatic representation of a graph data structure looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2lqwzDrU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xg0z1zdy9cbjbunu9aeq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2lqwzDrU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xg0z1zdy9cbjbunu9aeq.png" alt="Image description" width="450" height="112"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Usage:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Google maps uses graphs for building transportation systems, thus their navigation system is based on the algorithm to calculate the shortest path between two vertices.&lt;br&gt;
Facebook’s Friend suggestion algorithm uses graph theory. Facebook is an example of undirected graph.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Trees
&lt;/h3&gt;

&lt;p&gt;Similar to a graph, a tree is also a collection of vertices and edges. However, in tree data structure, there can only be one edge between two vertices. Each node is associated with a key value, with parent nodes linked to child nodes or sub nodes. There is one root node that is the ancestor of all the nodes in the tree.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6SBYBD7J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rusixm3v3qesr5nfi91g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6SBYBD7J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rusixm3v3qesr5nfi91g.png" alt="Image description" width="318" height="159"&gt;&lt;/a&gt;``&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Usage:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Binary search trees are used in many different types of search applications. Other types of trees are used in wireless networking and to create expression solvers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Data Structures:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Hash Table&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A hash table also known as a hash map stores a collection of items in an associative array that plots keys to values. A hash table uses a hash function to convert an index into an array of buckets that contain the desired data item.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Heaps&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A heap is a tree-based structure in which each parent node's associated key value is greater than or equal to the key values of any of its children's key values.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Trie&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A Trie is an advanced data structure that is sometimes also known as prefix tree or digital tree. It is a tree that stores the data in an ordered and efficient way. We generally use trie's to store strings.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion:
&lt;/h3&gt;

&lt;p&gt;Knowledge about data structures help you understand the working of each data structure. This helps you write memory and time efficient code. &lt;/p&gt;

&lt;p&gt;This is my first post. Please let me know your valuable feedbacks.&lt;/p&gt;

&lt;p&gt;Thank You! :)&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>computerscience</category>
      <category>datastructure</category>
    </item>
  </channel>
</rss>
