Graph Data Structure
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.
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.
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.
Start at the root (or any arbitrary node) and mark it as visited.
Explore each adjacent node recursively that has not been visited yet.
Steps:
Initialize a stack and push the starting node onto the stack.
Example:
Start at A.
Pop E, visit E.
Pop D, visit D.
Traversal Order: A, C, E, B, D
Start at the root (or any arbitrary node) and mark it as visited.
Steps:
Example:
Start at A.
Dequeue D, visit D.
Dequeue E, visit E.
Traversal Order: A, B, C, D, E
1. Dijkstra's Algorithm
Initialize distances: Set the distance to the source node to 0 and all other nodes
to infinity.
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.
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].
1. Kruskal's Algorithm
Sort all edges in the graph in non-decreasing order of their weights.
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.
Extract the edge with the minimum weight from the priority queue.
Add all edges from the new vertex to the priority queue.
Root Node Has a single root node Does not necessarily have a root
node
Cycles Trees do not contain cycles Graphs can contain cycles (cyclic)
(acyclic). or be acyclic.
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.
Degree In a binary tree, each node has a Nodes in a graph can have any
maximum degree of 2. number of edges.
Example Family Tree, Binary Search Tree Road Network, Social Network