11 Graph Data Structure 23052023 090026am
11 Graph Data Structure 23052023 090026am
AND ALGORITHMS
DR SAMABIA TEHSIN
BS (AI)
Graph Data Structure
A graph data structure is a collection of nodes that have data and are
connected to other nodes.
Let's try to understand this through an example. On facebook, everything is a
node. That includes User, Photo, Album, Event, Group, Page, Comment, Story,
Video, Link, Note...anything that has data is a node.
Every relationship is an edge from one node to another. Whether you post a
photo, join a group, like a page, etc., a new edge is created for that
relationship.
Graph Data Structure
A graph is a data structure (V, E) that consists of
➢A collection of vertices V
➢A collection of edges E, represented as ordered pairs of vertices
(u,v)
V = {0, 1, 2, 3}
E = {(0,1), (0,2), (0,3), (1,2)}
G = {V, E}
Graph Terminology
•Adjacency: A vertex is said to be adjacent to another vertex if there is an edge
connecting them. Vertices 2 and 3 are not adjacent because there is no edge
between them.
•Path: A sequence of edges that allows you to go from vertex A to vertex B is
called a path. 0-1, 1-2 and 0-2 are paths from vertex 0 to vertex 2.
•Directed Graph: A graph in which an edge (u,v) doesn't necessarily mean that
there is an edge (v, u) as well. The edges in such a graph are represented by
arrows to show the direction of the edge.
Undirected Graphs
An undirected graph is a graph in which the edges do not point in
any direction (ie. the edges are bidirectional).
Connected Graphs
A connected graph is a graph in which there is always a path
from a vertex to any other vertex.
Graph Representation
Adjacency Matrix
An adjacency matrix is a 2D array of V x V vertices.
Each row and column represent a vertex.
If the value of any element a[i][ j] is 1, it represents that there is an edge
connecting vertex i and vertex j.
Graph Representation : Adjacency Matrix
Each cell in the above table/matrix is represented as Aij, where i and j are vertices.
The value of Aij is either 1 or 0 depending on whether there is an edge from
vertex i to vertex j.
If there is a path from i to j, then the value of A is 1 otherwise its 0. For instance,
ij
there is a path from vertex 1 to vertex 2, so A is 1 and there is no path from vertex
12
1 to 3, so A is 0.
13
In case of undirected graphs, the matrix is symmetric about the diagonal because
of every edge (i,j), there is also an edge (j,i).
Graph Representation : Adjacency Matrix
Pros of Adjacency Matrix
• The basic operations like adding an edge, removing an edge, and checking
whether there is an edge from vertex i to vertex j are extremely time efficient,
constant time operations.
• If the graph is dense and the number of edges is large, an adjacency matrix
should be the first choice.
• The biggest advantage, however, comes from the use of matrices. The recent
advances in hardware enable us to perform even expensive matrix operations
on the GPU.
• By performing operations on the adjacent matrix, we can get important
insights into the nature of the graph and the relationship between its vertices.
Graph Representation : Adjacency Matrix
Cons of Adjacency Matrix
• The VxV space requirement of the adjacency matrix makes it a
memory hog. Graphs out in the wild usually don't have too
many connections and this is the major reason why adjacency
lists are the better choice for most tasks.
• While basic operations are easy, operations like inEdges and
outEdges are expensive when using the adjacency matrix
representation.
// Adjacency Matrix representation in
C++ public:
#include <iostream>
// Initialize the matrix to zero
Graph(int numVertices) {
using namespace std; this->numVertices = numVertices;
adjMatrix = new bool*[numVertices];
class Graph { for (int i = 0; i < numVertices; i++) {
adjMatrix[i] = new bool[numVertices];
private: for (int j = 0; j < numVertices; j++)
bool** adjMatrix; adjMatrix[i][j] = false;
int numVertices; }
}
// Add edges
void addEdge(int i, int j) { // Print the martix
void toString() {
adjMatrix[i][j] = true;
for (int i = 0; i < numVertices; i++) {
adjMatrix[j][i] = true; cout << i << " : ";
} for (int j = 0; j < numVertices; j++)
// Remove edges cout <<“j”<< adjMatrix[i][j] << " ";
cout << "\n";
void removeEdge(int i, int j) {
}
adjMatrix[i][j] = false; }
adjMatrix[j][i] = false;
}
~Graph() { int main() {
Graph g(4);
for (int i = 0; i < numVertices; i++)
g.addEdge(0, 1);
delete[] adjMatrix[i];
g.addEdge(0, 2);
delete[] adjMatrix; g.addEdge(1, 2);
g.addEdge(2, 0);
} g.addEdge(2, 3);
};
g.toString();
}
Graph Representation: Adjacency List
Adjacency List
An adjacency list represents a graph as an array of linked lists.
The index of the array represents a vertex and each element in its linked list
represents the other vertices that form an edge with the vertex.
Graph Representation: Adjacency List
Pros of Adjacency List
•An adjacency list is efficient in terms of storage because we only
need to store the values for the edges. For a sparse graph with
millions of vertices and edges, this can mean a lot of saved space.
•It also helps to find all the vertices adjacent to a vertex easily.
Graph Representation: Adjacency List
Cons of Adjacency List
•Finding the adjacent list is not quicker than the adjacency matrix because all the
connected nodes must be first explored to find them.
Graph Operations
The most common graph operations are:
•Graph Traversal
https://www.programiz.com/