Data Structures

What is Graph Data Structure? | Data Structure Tutorial

What is Graph Data Structure? | Data Structure Tutorial

What is Graph Data Structure? | Data Structure Tutorial

In this article, you will get to know about the Graph Data Structure.

A graph data structure is a set of nodes that have data and are also connected to other nodes of the tree. To understand the graph, let’s take Instagram. Everything is a node, it includes a User, event, group, page, story, video, link, and note and anything that has data is a node. Every relationship is an edge. It connects one node to another node. Whether you post a photo, join a group, like a page, and more, a new edge is created to set up a relationship. All Instagram is nothing but a collection of nodes and edges. It is because it uses the graph data structure to store the data. 

A notation of graph data structure (V, E) that consists of 

  • A collection of vertices V
  • A hub of edges E, represented as ordered pairs of vertices (u,v).

Terminology of Graph

  • Adjacency: If the edges of the two vertices are connected is called an adjacency. Vertices 7 and 3 are not adjacent because there is no edge between them. 
  • Path: A sequence of edges that permits you to move from vertex A to vertex B is called a path. For example, 0-1, 1-2, and 0-2 are path
  • Directed Graph: It is a type of graph that does not necessarily mean to have an edge (v,u) as well. The edges in a graph are represented by arrows to guide the direction of the edge.

Representation of Graph

It is mainly represented in two ways:

1. Adjacency Matrix: Suppose the adjacency matrix is a 2D array of V*V vertices. Each row and column of the matrix individually represent the matrix. If the value of any element a[i][j] is equal to 1, it means that there is a common edge between vertex i and vertex j. Edge lookup is a condition where we need to check if an edge exists between vertex A and vertex B. The edge lookup is so fast in adjacency matrix representation but we have to reserve space for every feasible link between all the vertices (V*V), so obviously it needs more space.

2. Adjacency list: It represents a graph as an array of the linked lists. The index of the array displays a vertex and every element in its linked list represents the other vertices that form an edge with the vertex. It is sufficient in terms of storage because we only need to store the values for the edges. 

Graph Operations:

  • Find whether the element is present in the graph.
  • Traversal of Graph elements
  • Insert elements into graph
  • Finding the path from one vertex to another vertex
Yamini  Sharma

I am working as a professional engineer in "HIghRadius Technologies Pvt. Ltd." Passionate about learning and exploring new technologies. Enthusiast in writing and sharing what I have in my knowledge bucket.