×

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.  

Related Topics

Convert Binary Tree into a Threaded Binary Tree

Implementation /*Writing a C++ program that will help us change the binary tree into a threaded binary tree and help us transform. */ #include <bits/stdc++.h> using namespace std; /*Creating the structure of a node...

11 minutes read.

Rearrange a linked list into alternate fashion first and the last element

Rearrange a linked list into alternate fashion first and the last element This article will explain how to rearrange the linked list into alternate fashion first and the last element. Here,...

3 minutes read.

Dijkstra’s vs Bellman-Ford Algorithm

The Dijkstra Algorithm One of the SSSP (Single Source Shortest Path) algorithms is Dijkstra's. As a result, it finds the shortest path between a source node and all other nodes in...

7 minutes read.

Find Number of Minimum Insertion to Make a String Palindrome

You have been given a string. You have to find out the number of minimum insertions to make this string palindrome. The string will contain only lower case alphabets. Note:What is...

4 minutes read.

Optimal binary search tree in DSA

Implementation // A simple way of the recursive implementation of the optimal search that we will perform on the binary tree.   #include <bits/stdc++.h> using namespace std; // we have to create a basic utility...

8 minutes read.

Bin Packing Problem (How to minimize the number of used Bins)

You have been given an array. The values of the array represent the size of n different items. You have been also given some bins. You have to store the...

3 minutes read.

All About Minimum Cost Spanning Trees in Data Structure

Data management is called database management. This allows the computer to sort or organize the data for efficient retrieval. A data model is a system that stores, manages, and optimizes...

7 minutes read.

Detect and Remove Loop in a Linked List

Create a function called detectAndRemovetheLoop() that verifies whether a given Linked List has a loop, eliminates the loop if it does, and returns true if it does. It returns false...

6 minutes read.

Does Overloading Work with Inheritance

This is a question that occasionally comes to many programmers. Who are curious to know more now has a complete explanation and a solution through this tutorial! Inheritance: The functions of...

3 minutes read.

Dynamic memory allocation of structure in C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

5 minutes read.

Identical Linked Lists

Identical Linked Lists In this problem, we have given two linked lists, and we need to check whether the given linked lists are identical or not. Identical means they have the...

4 minutes read.

What Should We Learn First? Trees or Graphs in Data Structures

A data structure is a database used to store and manage data and optimize and manage computing resources. A data structure is a form used intelligently and quickly to store,...

6 minutes read.

Sort the linked list of 0s, 1s and 2s

Sort the linked list of 0s, 1s and 2s In this, we are given a linked list of 0s, 1s, and 2s, and we need to sort it. Examples: Input: 1  ->  1 ...

2 minutes read.

Given a Perfect Binary Tree, Reverse Alternate Levels

Implementation //writing a program in C++ language to see how to approach it. #include <bits/stdc++.h> using namespace std; // creating a tree node. struct Nod { char ky; struct Nod *Lft, *Rt; }; // creating a new utility function...

9 minutes read.

Bubble Sort vs Quick Sort

In this article, we are going to compare two sorting techniques, Bubble sort and Quick Sort. In starting, we will first discuss the idea of sorting an array using bubble...

7 minutes read.

Binary Tree to Doubly Linked List

Binary Tree to Doubly Linked List This article will explain how to convert the given binary tree into a Doubly Linked List. The left and right pointers in tree nodes are...

2 minutes read.

Delete N nodes after M nodes of a linked list

Delete N nodes after M nodes of a linked list In this problem, we have given a linked list and two integers M and N. We need to traverse the linked...

3 minutes read.

Deletion in B+ Tree

Make a search for the leaf node that containing the key value by taking the value in a key value. If the required key value is found, then it will remove...

6 minutes read.

Asymptotic Notation

Asymptotic notation is expressions that are used to represent the complexity of algorithms. The complexity of the algorithm is analyzed from two perspectives:  Time complexitySpace complexity Time complexity The time complexity of an algorithm is the...

3 minutes read.

Number of visible boxes putting one inside another

You have given one array, which consists of values which represent the sizes of different boxes. We can put one box inside another if the size of the outside box...

3 minutes read.