What is Graph Data Structure?
A graph in data structure is a way to show how things are connected. Think of it like a
map of roads connecting different cities. In a graph, the cities are called "nodes" or
"vertices," and the roads are called "edges." Graphs help us understand how things are
linked together and how they interact.
A graph is made up of nodes and edges. Nodes represent objects, and edges represent
the connections between these objects.
For example, in a social network, people are nodes, and their friendships are edges.
Types of Graph Data Structure with Examples
Graphs can be categorized based on their characteristics and properties:
1. Directed Graph
In a directed graph, edges have a direction, meaning they go from one node to another
in a specific way.
Example:
Think of a Twitter network where one person follows another. If Alice follows Bob, there
is an edge from Alice to Bob but not necessarily from Bob to Alice.
2. Undirected Graph
In an undirected graph, edges do not have a direction. They simply connect two nodes
without any particular order.
Example:
Think of a Facebook friendship where if Alice is friends with Bob, then Bob is also friends
with Alice. The edge goes both w
Example:
Think of a Facebook friendship where if Alice is friends with Bob, then Bob is also friends
with Alice. The edge goes both ways.
3. Weighted Graph
In a weighted graph, edges have weights or costs associated with them. These weights
can represent distances, costs, or any other metric.
Example:
A road map where the weights on the edges represent the distance between cities.
4. Unweighted Graph
In an unweighted graph, all edges have the same weight, typically considered as 1.
Example:
A simple social network where each friendship has the same importance.
Representations of Graph Data Structure
Graphs can be represented in several ways. The three most common methods are
adjacency matrix, adjacency list, and edge list.
1. Adjacency Matrix
An adjacency matrix is a 2D array used to represent a graph. Each cell in the matrix
indicates whether an edge exists between a pair of vertices. If an edge exists, the cell
contains a value (typically 1 for unweighted graphs or the weight of the edge for
weighted graphs); otherwise, it contains 0.
Example:
Consider a graph with 4 vertices (A, B, C, and D) and the following edges: A-B, A-C, B-
D, and C-D.
Adjacency Matrix:
ABCD
A0110
B1001
C1001
D0110
Graph:
2. Adjacency List
An adjacency list is an array of lists. The array's indices represent the vertices, and each
element in the array is a list of vertices adjacent to the vertex at that index.
Example:
Consider the same graph with 4 vertices (A, B, C, and D) and the following edges: A-B,
A-C, B-D, and C-D.
Adjacency List:
A: B, C
B: A, D
C: A, D
D: B, C
Graph:
Graph Traversal Algorithms
Graph traversal algorithms are used to visit all the vertices and edges in a graph. The
two most common traversal methods are Depth-First Search (DFS) and Breadth-First
Search (BFS). Each method has its own algorithm and applications.
1. Depth-First Search (DFS)
Algorithm:
Start at the root (or any arbitrary node) and mark it as visited.
Explore each adjacent node recursively that has not been visited yet.
Repeat the process until all nodes are visited.
Steps:
Initialize a stack and push the starting node onto the stack.
While the stack is not empty:
Pop a node from the stack.
If the node has not been visited:
Mark the node as visited.
Push all adjacent unvisited nodes onto the stack.
Example:
Consider the following graph:
DFS Traversal from A:
Start at A.
Visit A, push B and C to the stack.
Pop C, visit C, push E to the stack.
Pop E, visit E.
Pop B, visit B, push D to the stack.
Pop D, visit D.
Traversal Order: A, C, E, B, D
2. Breadth-First Search (BFS)
Algorithm:
Start at the root (or any arbitrary node) and mark it as visited.
Explore each adjacent node level by level.
Repeat the process until all nodes are visited.
Steps:
Initialize a queue and enqueue the starting node.
While the queue is not empty:
Dequeue a node from the queue.
If the node has not been visited:
Mark the node as visited.
Enqueue all adjacent unvisited nodes.
Example:
Consider the same graph:
BFS Traversal from A:
Start at A.
Visit A, enqueue B and C.
Dequeue B, visit B, enqueue D.
Dequeue C, visit C, enqueue E.
Dequeue D, visit D.
Dequeue E, visit E.
Traversal Order: A, B, C, D, E
Shortest Path Algorithms
Shortest path algorithms are used to find the shortest path from a source node to other
nodes in a graph. Here are three common shortest path algorithms:
1. Dijkstra's Algorithm
Initialize distances: Set the distance to the source node to 0 and all other nodes
to infinity.
Set the source node as current.
For the current node, consider all its unvisited neighbors and calculate their
tentative distances through the current node.
Compare the newly calculated tentative distance to the current assigned value
and assign the smaller one.
Once all neighbors of the current node are considered, mark the current node as
visited.
If the destination node has been visited, the algorithm has finished. If not, select
the unvisited node with the smallest tentative distance as the new "current node"
and repeat the process.
2. Bellman-Ford Algorithm
Initialize distances: Set the distance to the source node to 0 and all other nodes
to infinity.
For each edge, update the distance to the destination node if the distance to the
source node plus the edge's weight is less than the current distance to the
destination node.
Repeat step 2 for all edges |V| - 1 times, where |V| is the number of vertices.
Check for negative-weight cycles: If a distance can still be updated, then a
negative-weight cycle exists.
3. Floyd-Warshall Algorithm
Create a 2D array dist and initialize it with the distances between every pair of
vertices. Set the distance from each vertex to itself to 0.
For each pair of vertices (i, j), check if a vertex k exists such that the path from i
to j through k is shorter than the direct path from i to j. If it is, update dist[i][j].
Repeat for all vertices.
Minimum Spanning Tree Algorithms
Minimum Spanning Tree (MST) algorithms are used to find a subset of edges in a
weighted graph that connects all vertices with the minimum possible total edge weight,
and without any cycles. The two most common MST algorithms are Kruskal's Algorithm
and Prim's Algorithm.
1. Kruskal's Algorithm
Sort all edges in the graph in non-decreasing order of their weights.
Initialize an empty set for the MST.
For each edge in the sorted list:
If the edge does not form a cycle with the edges already in the MST, add it to the
MST.
Use a union-find data structure to keep track of which vertices are in which
components.
Repeat until the MST contains V−1 edges, where V is the number of vertices.
2. Prim's Algorithm
Initialize a set for the MST and a priority queue to store the edges.
Start with an arbitrary node and add all its edges to the priority queue.
While the MST does not include all vertices:
Extract the edge with the minimum weight from the priority queue.
If the edge connects a new vertex, add it to the MST.
Add all edges from the new vertex to the priority queue.
Repeat until all vertices are included in the MST.
Uses of Graph Data Structure (Applications)
Following are the main uses and applications of graph data structure:
Application Explanation Example
Representing user interactions and Facebook, LinkedIn,
Social Networks
connections. Twitter
Computer Optimizing data transfer and Internet routers and
Networks network structure. switches
Ranking web pages based on
Web Page Ranking Google PageRank
importance.
Finding shortest paths and
Transportation GPS navigation systems
optimizing travel routes.
Planning and optimizing task
Scheduling Gantt charts, PERT charts
execution order.
Modeling protein interactions and Protein-protein interaction
Biology
genetic networks. networks
Game Designing game levels, AI, and Pathfinding algorithms in
Development player interactions. game AI
Modeling and analyzing electrical Circuit design software like
Electrical Circuits
circuits. SPICE
Dependency Managing software project Package managers like
Resolution dependencies. npm, pip, Maven
Difference Between Tree and Graph Data Structure
The following is a comparison highlighting the key differences between tree and graph
data structures:
Feature Tree Graph
Definition A tree is a hierarchical data A graph is a collection of nodes
structure with a root node and (vertices) connected by edges,
child nodes forming a parent- representing relationships between
child relationship. the nodes.
Structure Hierarchical (parent-child Network-like (arbitrary connections
relationship) between nodes)
Root Node Has a single root node Does not necessarily have a root
node
Path There is exactly one path There can be multiple paths
between any two nodes. between any two nodes.
Cycles Trees do not contain cycles Graphs can contain cycles (cyclic)
(acyclic). or be acyclic.
Connectivity Trees are connected (all nodes Graphs can be connected or
are reachable from the root). disconnected.
Parent-Child Each node (except the root) has Nodes can have multiple parents.
Relationship exactly one parent.
Leaf Nodes Nodes with no children are called Graphs do not have the concept of
leaf nodes. leaf nodes.
Types Binary Tree, AVL Tree, B-Tree, Directed Graph, Undirected Graph,
etc. Weighted Graph, etc.
Usage Used to represent hierarchical Used to represent network
data, such as file systems, structures, such as social networks,
organization structures, and XML computer networks, and
documents. transportation systems.
Degree In a binary tree, each node has a Nodes in a graph can have any
maximum degree of 2. number of edges.
Traversal In-order, Pre-order, Post-order, Depth-First Search (DFS), Breadth-
Methods Level-order First Search (BFS)
Example Family Tree, Binary Search Tree Road Network, Social Network
Applications Efficient searching, sorting, and Network analysis, shortest path
hierarchical data storage algorithms, and connectivity checks