×

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

Reverse a Linked List in groups of given size

Reverse a Linked List in groups of given size This article will explain how to reverse a linked list in groups of given size. Here we have given a linked list...

2 minutes read.

Heap Data Structure

In this article, we will learn in detail about Heap (Min heap and Max heap). Before going to the main topics, let’s have a look at what is complete binary...

19 minutes read.

Extended Binary Tree

A form of binary tree known as an extended binary tree replaces all of the original tree's null subtrees with special nodes known as external nodes, while the remaining nodes...

4 minutes read.

Lowest Common Ancestor in a Binary Tree

The lowest node in the tree that contains both n1 and n2 as descendants is the lowest common ancestor (LCA), and n1 and n2 are the nodes for which we...

11 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.

Recaman’s Sequence

Recamán's succession repeat connection in arithmetic and software engineering. Since its components are obviously connected with the past components, they are as often as possible characterized utilizing recursion. It takes its...

4 minutes read.

How to get Better in Data Structures and Algorithms?

Introduction Data structures and algorithms are fundamental computer science concepts that store, organize, and process data efficiently. By understanding different data structures and algorithms and using them effectively, you can become...

19 minutes read.

Merge Sort

Merge Sort is one of the most widely used sorting algorithms, and it is based on the Divide and Conquer principle. A problem is subdivided into multiple sub-problems in this method....

8 minutes read.

Difference between Stack and Queue

In this article, we will learn about the major differences between Stack and Queue data structures. What is a stack? Stack – A stack is an abstract data structure defined as the...

3 minutes read.

Stack vs Queue: Data Structure

 Difference Between Stack and Queue What is Stack? The LIFO principle applies on insertion and deletion operations of the stack which means last inserted element to the stack will remove first....

3 minutes read.

AVL Tree

AVL Tree AVL Tree is referred to as self-balanced or height-balanced binary search tree where the difference between heights of its left subtree and right subtree (Balance Factor) can't more than...

25 minutes read.

Find the fractional (n/kth) node in the linked list

Find the fractional (n/kth) node in the linked list In this problem, we have given a singly linked list and a number k. Here we need to find the (n/k)th element...

2 minutes read.

Diameter of a Binary Tree

Implementation We will now witness the implementation of the diameter of a binary tree. // Creating a recursive and challenging C program that will help us determine the diameter of a binary...

4 minutes read.

Difference between complete and full binary tree

As we all know that the  binary tree is a tree it contains one or two children at each other node. It contains two children's nodes in the Binary tree. The...

6 minutes read.

Big O Notations

What is Big O Notation, and why is it important? "Big O notation is a mathematical notation that depicts a function's limiting behaviour when the input tends towards a certain value...

10 minutes read.

Types of Data Structures

Almost every programme or software system that has been built makes use of data structures. Furthermore, data structures are basics of computer science and software engineering. When it comes to...

7 minutes read.

Print kth least significant bit number

You have given a number and you have to find out the kth least significant bit of this number. K will be given to you.  The bit will be from...

3 minutes read.

Given a Generate all Structurally Unique Binary Search Trees

Implementation // Creating a C++ program that will help us build all the binary search trees for the keys from 1 to n.  #include <bits/stdc++.h> using namespace std; // creating a structure that will...

8 minutes read.

Invert binary tree

Invert binary tree is a mirror image of a tree. It is pretty much the same compared to the only difference: its left and right children are swapped with the...

4 minutes read.

Comb Sort

Brush sort is a fairly direct orchestrating computation at first arranged by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered (and given the name "Combsort") by Stephen Lacey and...

5 minutes read.