Last Updated : 15 Jul, 2025
Graph Data Structure is a non-linear data structure consisting of vertices and edges. It is useful in fields such as social network analysis, recommendation systems, and computer networks. In the field of sports data science, graph data structure can be used to analyze and understand the dynamics of team performance and player interactions on the field.
What is Graph Data Structure?Graph is a non-linear data structure consisting of vertices and edges. The vertices are sometimes also referred to as nodes and the edges are lines or arcs that connect any two nodes in the graph. More formally a Graph is composed of a set of vertices( V ) and a set of edges( E ). The graph is denoted by G(V, E).
Imagine a game of football as a web of connections, where players are the nodes and their interactions on the field are the edges. This web of connections is exactly what a graph data structure represents, and it's the key to unlocking insights into team performance and player dynamics in sports.
Components of Graph Data StructureA graph is known as a null graph if there are no edges in the graph.
2. Trivial GraphGraph having only a single vertex, it is also the smallest graph possible.
3. Undirected GraphA graph in which edges do not have any direction. That is the nodes are unordered pairs in the definition of every edge.
4. Directed GraphA graph in which edge has direction. That is the nodes are ordered pairs in the definition of every edge.
5. Connected GraphThe graph in which from one node we can visit any other node in the graph is known as a connected graph.
6. Disconnected GraphThe graph in which at least one node is not reachable from a node is known as a disconnected graph.
7. Regular GraphThe graph in which the degree of every vertex is equal to K is called K regular graph.
8. Complete GraphThe graph in which from each node there is an edge to each other node.
9. Cycle GraphThe graph in which the graph is a cycle in itself, the minimum value of degree of each vertex is 2.
10. Cyclic GraphA graph containing at least one cycle is known as a Cyclic graph.
11. Directed Acyclic GraphA Directed Graph that does not contain any cycle.
12. Bipartite GraphA graph in which vertex can be divided into two sets such that vertex in each set does not contain any edge between them.
13. Weighted GraphThere are multiple ways to store a graph: The following are the most common representations.
In this method, the graph is stored in the form of the 2D matrix where rows and columns denote vertices. Each entry in the matrix represents the weight of the edge between those vertices.
Below is the implementation of Graph Data Structure represented using Adjacency Matrix:
C++
// C++ program to demonstrate Adjacency Matrix
// representation of undirected and unweighted graph
#include <bits/stdc++.h>
using namespace std;
void addEdge(vector<vector<int>> &mat, int i, int j)
{
mat[i][j] = 1;
mat[j][i] = 1; // Since the graph is undirected
}
void displayMatrix(vector<vector<int>> &mat)
{
int V = mat.size();
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
cout << mat[i][j] << " ";
cout << endl;
}
}
int main()
{
// Create a graph with 4 vertices and no edges
// Note that all values are initialized as 0
int V = 4;
vector<vector<int>> mat(V, vector<int>(V, 0));
// Now add edges one by one
addEdge(mat, 0, 1);
addEdge(mat, 0, 2);
addEdge(mat, 1, 2);
addEdge(mat, 2, 3);
/* Alternatively we can also create using below
code if we know all edges in advacem
vector<vector<int>> mat = {{ 0, 1, 0, 0 },
{ 1, 0, 1, 0 },
{ 0, 1, 0, 1 },
{ 0, 0, 1, 0 } }; */
cout << "Adjacency Matrix Representation" << endl;
displayMatrix(mat);
return 0;
}
C
#include<stdio.h>
#define V 4
void addEdge(int mat[V][V], int i, int j) {
mat[i][j] = 1;
mat[j][i] = 1; // Since the graph is undirected
}
void displayMatrix(int mat[V][V]) {
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++)
printf("%d ", mat[i][j]);
printf("\n");
}
}
int main() {
// Create a graph with 4 vertices and no edges
// Note that all values are initialized as 0
int mat[V][V] = {0};
// Now add edges one by one
addEdge(mat, 0, 1);
addEdge(mat, 0, 2);
addEdge(mat, 1, 2);
addEdge(mat, 2, 3);
/* Alternatively, we can also create using the below
code if we know all edges in advance
int mat[V][V] = {
{0, 1, 0, 0},
{1, 0, 1, 0},
{0, 1, 0, 1},
{0, 0, 1, 0}
}; */
printf("Adjacency Matrix Representation\n");
displayMatrix(mat);
return 0;
}
Java
import java.util.Arrays;
public class GfG {
public static void addEdge(int[][] mat, int i, int j) {
mat[i][j] = 1;
mat[j][i] = 1; // Since the graph is undirected
}
public static void displayMatrix(int[][] mat) {
for (int[] row : mat) {
for (int val : row) {
System.out.print(val + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
// Create a graph with 4 vertices and no edges
// Note that all values are initialized as 0
int V = 4;
int[][] mat = new int[V][V];
// Now add edges one by one
addEdge(mat, 0, 1);
addEdge(mat, 0, 2);
addEdge(mat, 1, 2);
addEdge(mat, 2, 3);
/* Alternatively we can also create using below
code if we know all edges in advance
int[][] mat = {{ 0, 1, 0, 0 },
{ 1, 0, 1, 0 },
{ 0, 1, 0, 1 },
{ 0, 0, 1, 0 } }; */
System.out.println("Adjacency Matrix Representation");
displayMatrix(mat);
}
}
Python
def add_edge(mat, i, j):
# Add an edge between two vertices
mat[i][j] = 1 # Graph is
mat[j][i] = 1 # Undirected
def display_matrix(mat):
# Display the adjacency matrix
for row in mat:
print(" ".join(map(str, row)))
# Main function to run the program
if __name__ == "__main__":
V = 4 # Number of vertices
mat = [[0] * V for _ in range(V)]
# Add edges to the graph
add_edge(mat, 0, 1)
add_edge(mat, 0, 2)
add_edge(mat, 1, 2)
add_edge(mat, 2, 3)
# Optionally, initialize matrix directly
"""
mat = [
[0, 1, 0, 0],
[1, 0, 1, 0],
[0, 1, 0, 1],
[0, 0, 1, 0]
]
"""
# Display adjacency matrix
print("Adjacency Matrix:")
display_matrix(mat)
C#
using System;
public class GfG
{
// Add an edge between two vertices
public static void AddEdge(int[,] mat, int i, int j)
{
mat[i, j] = 1; // Since the graph is
mat[j, i] = 1; // undirected
}
// Display the adjacency matrix
public static void DisplayMatrix(int[,] mat)
{
int V = mat.GetLength(0);
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
Console.Write(mat[i, j] + " ");
}
Console.WriteLine();
}
}
// Main method to run the program
public static void Main(string[] args)
{
int V = 4; // Number of vertices
int[,] mat = new int[V, V]; // Initialize matrix
// Add edges to the graph
AddEdge(mat, 0, 1);
AddEdge(mat, 0, 2);
AddEdge(mat, 1, 2);
AddEdge(mat, 2, 3);
// Optionally, initialize matrix directly
/*
int[,] mat = new int[,]
{
{ 0, 1, 0, 0 },
{ 1, 0, 1, 0 },
{ 0, 1, 0, 1 },
{ 0, 0, 1, 0 }
};
*/
// Display adjacency matrix
Console.WriteLine("Adjacency Matrix:");
DisplayMatrix(mat);
}
}
JavaScript
function addEdge(mat, i, j) {
mat[i][j] = 1; // Graph is
mat[j][i] = 1; // undirected
}
function displayMatrix(mat) {
// Display the adjacency matrix
for (const row of mat) {
console.log(row.join(" "));
}
}
// Main function to run the program
const V = 4; // Number of vertices
// Initialize matrix
let mat = Array.from({ length: V }, () => Array(V).fill(0));
// Add edges to the graph
addEdge(mat, 0, 1);
addEdge(mat, 0, 2);
addEdge(mat, 1, 2);
addEdge(mat, 2, 3);
/* Optionally, initialize matrix directly
let mat = [
[0, 1, 0, 0],
[1, 0, 1, 0],
[0, 1, 0, 1],
[0, 0, 1, 0]
];
*/
// Display adjacency matrix
console.log("Adjacency Matrix:");
displayMatrix(mat);
Adjacency Matrix Representation 0 1 1 0 1 0 1 0 1 1 0 1 0 0 1 0Adjacency List Representation of Graph:
This graph is represented as a collection of linked lists. There is an array of pointer which points to the edges connected to that vertex.
Below is the implementation of Graph Data Structure represented using Adjacency List:
C++
#include <iostream>
#include <vector>
using namespace std;
// Function to add an edge between two vertices
void addEdge(vector<vector<int>>& adj, int i, int j) {
adj[i].push_back(j);
adj[j].push_back(i); // Undirected
}
// Function to display the adjacency list
void displayAdjList(const vector<vector<int>>& adj) {
for (int i = 0; i < adj.size(); i++) {
cout << i << ": "; // Print the vertex
for (int j : adj[i]) {
cout << j << " "; // Print its adjacent
}
cout << endl;
}
}
// Main function
int main() {
// Create a graph with 4 vertices and no edges
int V = 4;
vector<vector<int>> adj(V);
// Now add edges one by one
addEdge(adj, 0, 1);
addEdge(adj, 0, 2);
addEdge(adj, 1, 2);
addEdge(adj, 2, 3);
cout << "Adjacency List Representation:" << endl;
displayAdjList(adj);
return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
// Structure for a node in the adjacency list
struct Node {
int data;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to add an edge between two vertices
void addEdge(struct Node* adj[], int i, int j) {
struct Node* newNode = createNode(j);
newNode->next = adj[i];
adj[i] = newNode;
newNode = createNode(i); // For undirected graph
newNode->next = adj[j];
adj[j] = newNode;
}
// Function to display the adjacency list
void displayAdjList(struct Node* adj[], int V) {
for (int i = 0; i < V; i++) {
printf("%d: ", i); // Print the vertex
struct Node* temp = adj[i];
while (temp != NULL) {
printf("%d ", temp->data); // Print its adjacent
temp = temp->next;
}
printf("\n");
}
}
// Main function
int main() {
// Create a graph with 4 vertices and no edges
int V = 4;
struct Node* adj[V];
for (int i = 0; i < V; i++) {
adj[i] = NULL; // Initialize adjacency list
}
// Now add edges one by one
addEdge(adj, 0, 1);
addEdge(adj, 0, 2);
addEdge(adj, 1, 2);
addEdge(adj, 2, 3);
printf("Adjacency List Representation:\n");
displayAdjList(adj, V);
return 0;
}
Java
import java.util.ArrayList;
import java.util.List;
public class GfG {
// Method to add an edge between two vertices
public static void addEdge(List<List<Integer>> adj, int i, int j) {
adj.get(i).add(j);
adj.get(j).add(i); // Undirected
}
// Method to display the adjacency list
public static void displayAdjList(List<List<Integer>> adj) {
for (int i = 0; i < adj.size(); i++) {
System.out.print(i + ": "); // Print the vertex
for (int j : adj.get(i)) {
System.out.print(j + " "); // Print its adjacent
}
System.out.println();
}
}
// Main method
public static void main(String[] args) {
// Create a graph with 4 vertices and no edges
int V = 4;
List<List<Integer>> adj = new ArrayList<>(V);
for (int i = 0; i < V; i++) {
adj.add(new ArrayList<>());
}
// Now add edges one by one
addEdge(adj, 0, 1);
addEdge(adj, 0, 2);
addEdge(adj, 1, 2);
addEdge(adj, 2, 3);
System.out.println("Adjacency List Representation:");
displayAdjList(adj);
}
}
Python
def add_edge(adj, i, j):
adj[i].append(j)
adj[j].append(i) # Undirected
def display_adj_list(adj):
for i in range(len(adj)):
print(f"{i}: ", end="")
for j in adj[i]:
print(j, end=" ")
print()
# Create a graph with 4 vertices and no edges
V = 4
adj = [[] for _ in range(V)]
# Now add edges one by one
add_edge(adj, 0, 1)
add_edge(adj, 0, 2)
add_edge(adj, 1, 2)
add_edge(adj, 2, 3)
print("Adjacency List Representation:")
display_adj_list(adj)
C#
using System;
using System.Collections.Generic;
public class GfG
{
// Method to add an edge between two vertices
public static void AddEdge(List<List<int>> adj, int i, int j)
{
adj[i].Add(j);
adj[j].Add(i); // Undirected
}
// Method to display the adjacency list
public static void DisplayAdjList(List<List<int>> adj)
{
for (int i = 0; i < adj.Count; i++)
{
Console.Write($"{i}: "); // Print the vertex
foreach (int j in adj[i])
{
Console.Write($"{j} "); // Print its adjacent
}
Console.WriteLine();
}
}
// Main method
public static void Main(string[] args)
{
// Create a graph with 4 vertices and no edges
int V = 4;
List<List<int>> adj = new List<List<int>>(V);
for (int i = 0; i < V; i++)
adj.Add(new List<int>());
// Now add edges one by one
AddEdge(adj, 0, 1);
AddEdge(adj, 0, 2);
AddEdge(adj, 1, 2);
AddEdge(adj, 2, 3);
Console.WriteLine("Adjacency List Representation:");
DisplayAdjList(adj);
}
}
JavaScript
function addEdge(adj, i, j) {
adj[i].push(j);
adj[j].push(i); // Undirected
}
function displayAdjList(adj) {
for (let i = 0; i < adj.length; i++) {
console.log(`${i}: `);
for (const j of adj[i]) {
console.log(`${j} `);
}
console.log();
}
}
// Create a graph with 4 vertices and no edges
const V = 4;
const adj = Array.from({ length: V }, () => []);
// Now add edges one by one
addEdge(adj, 0, 1);
addEdge(adj, 0, 2);
addEdge(adj, 1, 2);
addEdge(adj, 2, 3);
console.log("Adjacency List Representation:");
displayAdjList(adj);
Adjacency List Representation: 0: 1 2 1: 0 2 2: 0 1 3 3: 2Comparison between Adjacency Matrix and Adjacency List
When the graph contains a large number of edges then it is good to store it as a matrix because only some entries in the matrix will be empty. An algorithm such as Prim's and Dijkstra adjacency matrix is used to have less complexity.
Action Adjacency Matrix Adjacency List Adding Edge O(1) O(1) Removing an edge O(1) O(N) Initializing O(N*N) O(N) Basic Operations on Graph Data Structure:Below are the basic operations on the graph:
Tree is a restricted type of Graph Data Structure, just with some more rules. Every tree will always be a graph but not all graphs will be trees. Linked List, Trees, and Heaps all are special cases of graphs.
Real-Life Applications of Graph Data Structure:Graph Data Structure has numerous real-life applications across various fields. Some of them are listed below:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4