Graph Data Structure: Learning Outcomes: 1. Understand What Is A Graph 2. Types of Graphs 3. Representing Graphs
Graph Data Structure: Learning Outcomes: 1. Understand What Is A Graph 2. Types of Graphs 3. Representing Graphs
Learning Outcomes:
1. Understand what is a Graph
2. Types of graphs
3. Representing graphs
Graphs:
A Graph is a finite collection of objects and relations existing between objects. If we represent
objects as vertices(or nodes) and relations as edges then we can get following two types of
graph:-
Directed Graphs: In directed graph, an edge is represented by an ordered pair of vertices (i,j) in
which edge originates from vertex i and terminates on vertex j. Given below is an example of an
directed graph.
Fig: D.1
Undirected Graphs: In Undireced graph, edges are represented by unordered pair of
vertices.Given below is an example of an undirected graph.
Fig: UD.1
Representation
The two most common ways of representing graphs are:
1. Adjacency matrix
2. Adjacency List
Adjacency Matrix
Let us consider a graph in which there are N vertices numbered from 0 to N-1 and E number of
edges in the form (i,j). Where (i,j) represent an edge originating from ith vertex and terminating
on jth vertex. Now, A Adjacency Matrix is a N*N binary matrix in which value of [i,j]th cell
is 1 if there exists an edge originating from ith vertex and terminating to jth vertex, otherwise the
value is 0. Given below are Adjacency matrices for both Directed and Undirected graph shown
above:
Adjacency List
Lets consider a graph in which there are N vertices numbered from 0 to N-1 and E number of
edges in the form (i,j). Where (i,j) represent an edge from ith vertex to jth vertex.
Now, Adjacency List is an array of seperate lists. Each element of array is a list of
corresponding neighbour(or directly connected) vertices.In other words ith list of Adjacency
List is a list of all those vertices which is directly connected to ith vertex. Given below are
Adjacency lists for both Directed and Undirected graph shown above: