Data Structures Tutorial

Data Structures Tutorial Asymptotic Notation Structure and Union Array Data Structure Linked list Data Structure Type of Linked list Advantages and Disadvantages of linked list Queue Data Structure Implementation of Queue Stack Data Structure Implementation of Stack Sorting Insertion sort Quick sort Selection sort Heap sort Merge sort Bucket sort Count sort Radix sort Shell sort Tree Traversal of the binary tree Binary search tree Graph Spanning tree Linear Search Binary Search Hashing Collision Resolution Techniques

Misc Topic:

Priority Queue in Data Structure Deque in Data Structure Difference Between Linear And Non Linear Data Structures Queue Operations In Data Structure About Data Structures Data Structures Algorithms Types of Data Structures Big O Notations Introduction to Arrays Introduction to 1D-Arrays Operations on 1D-Arrays Introduction to 2D-Arrays Operations on 2D-Arrays Strings in Data Structures String Operations Application of 2D array Bubble Sort Insertion Sort Sorting Algorithms What is DFS Algorithm What Is Graph Data Structure What is the difference between Tree and Graph What is the difference between DFS and BFS Bucket Sort Dijkstra’s vs Bellman-Ford Algorithm Linear Queue Data Structure in C Stack Using Array Stack Using Linked List Recursion in Fibonacci Stack vs Array What is Skewed Binary Tree Primitive Data Structure in C Dynamic memory allocation of structure in C Application of Stack in Data Structures Binary Tree in Data Structures Heap Data Structure Recursion - Factorial and Fibonacci What is B tree what is B+ tree Huffman tree in Data Structures Insertion Sort vs Bubble Sort Adding one to the number represented an array of digits Bitwise Operators and their Important Tricks Blowfish algorithm Bubble Sort vs Selection Sort Hashing and its Applications Heap Sort vs Merge Sort Insertion Sort vs Selection Sort Merge Conflicts and ways to handle them Difference between Stack and Queue AVL tree in data structure c++ Bubble sort algorithm using Javascript Buffer overflow attack with examples Find out the area between two concentric circles Lowest common ancestor in a binary search tree Number of visible boxes putting one inside another Program to calculate the area of the circumcircle of an equilateral triangle Red-black Tree in Data Structures Strictly binary tree in Data Structures 2-3 Trees and Basic Operations on them Asynchronous advantage actor-critic (A3C) Algorithm Bubble Sort vs Heap Sort Digital Search Tree in Data Structures Minimum Spanning Tree Permutation Sort or Bogo Sort Quick Sort vs Merge Sort

Graph Data Structure

A graph is a non-primitive and non-linear data structure. It is a group of (V, E) where V is a set of vertexes, and E is a set of edge. In the graph, a vertex is connected with another vertex, and the connection between two vertexes is called edge. Edge acts as a communication link between two vertexes. A graph is shown in the figure below.

Where, V = {A, B, C, D, E, F}

             E = {(AB), (AC), (AE), (BE), (BD), (CD), (CF), (DF)}

Types of Graph

Directed graph: A directed graph is a graph in which the edges have any direction, and such type of edge is called a directed edge. Directed edges are also called arcs. In a graph, edges are represented by a line, and the directed graph at each line has an arrow mark. The figure of the directed graph is shown below.

Undirected graph: An undirected graph is a graph in which the edges have not any direction. That is, there is no arrow mark in it. If an edge exists between the vertex A and B in the undirected graph, then the vertices can be traversed from both directions. The figure of the undirected graph is shown below.        

Terminology of graph

Weighted graph: A weighted graph is a graph in which edges are assigned some weights (value). These weights are real numbers. In other words, weight is the distance between one node to another.

Unweighted graph: An unweighted graph is a graph that does not have the weight (value) of the edges.

DAG: DAG stands for the directed acyclic graph. DAG is a directed graph that contains no cycles.

Multigraph: Multigraph is an undirected graph in which multiple edges are allowed in a single vertex.

Complete graph: A complete graph is a graph in which all nodes are linked with each other. A complete graph contains n(n-1)/2 edges. Where n is the number of the vertex.

Graph traversal

Graph traversal means visiting each vertex of the graph. There are several ways to visit the vertices of a graph. There are two types of graph traversal.

  1. BFS (Breadth first search)
  2. DFS (Depth first search) 

BFS is an algorithm for traversing and searching the graph data structures. It is used to find the shortest path in the graph and solve puzzle games. In the data structure, the queue is used to implement BFS.

In BFS, any one node is first visited, and then the nodes adjacent to it are visited. After this, all adjacent nodes of these adjacent nodes are also visited. And this process continues until all nodes have been visited.

Algorithm of BFS

Step 1. SET STATUS = 1 (ready state) for each node in G. 
Step 2. Enqueue the starting node A, and set its STATUS = 2 (waiting state). 
Step 3. Repeat Steps 4 and 5 until QUEUE is empty. 
Step 4. Dequeue a node N. Process it and set its STATUS = 3 (processed state). 
Step 5. Enqueue all the neighbors of N that are in the ready state (whose STATUS = 1) and set their STATUS = 2 (waiting state).
            [END OF LOOP]. 
Step 6. EXIT.  

DFS is also an algorithm for traversing and searching graph data structures like BFS. In the data structure, the stack is used to implement DFS. DFS is a recursive algorithm that works on the principle of backtracking. DFS is used to analyze networks, map routes, and solve other computer science problems.

Algorithm of DFS

Step 1. SET STATUS = 1 (ready state) for each node in G. 
Step 2. Push the starting node A on the stack and set its STATUS = 2 (waiting state). 
Step 3. Repeat Steps 4 and 5 until STACK is empty. 
Step 4. Pop the top node N. Process it and set its STATUS = 3 (processed state). 
Step 5. Push on the stack all the neighbours of N that are in the ready state (whose STATUS = 1)                                 and set their.
            STATUS = 2 (waiting state)
            [END OF LOOP] 
Step 6. EXIT.  



ADVERTISEMENT
ADVERTISEMENT