Unit 4 - Graph Contents
Unit 4 - Graph Contents
INTRODUCTION
TERMINOLOGIES
Graph
A graph G consists of a nonempty set V called the set of nodes (points,
vertices) of the graph, a set E which is the set of edges of the graph and a
mapping from the set E to a set V. or A graph is a collection of edges and
vertices.
1
Diagraph
A graph in which every edge is directed that is called directed or diagraph.
Undirected Graph
A graph in which every edge is undirected that is called undirected graph.
Mixed Graph
In graph ‘G’ if some edges are directed and some edges are undirected than
graph ‘G’ is known as mixed graph.
Weighted Graph
If all the edges in the graph are labeled with some weight than this graph is
known as weighted graph.
Incident
2
An edge connects two vertices, these two vertices are said to be incident to
that edge.
Adjacent Vertices
A vertex Vi is adjacent to Vj if there is an edge from Vi to Vj.
Cycle
A path which starting and ending at the same node is called cycle.
Self loop/sling
If there is an edge whose starting and ending vertices are same, it is called
self loop.
Parallel Edges
If there are more than one edges between the same pair of vertices then
they are known as parallel edges.
Simple Graph
A directed graph which does not have any self loop or parallel edges, it is
called simple graph.
3
Complete Graph
Graph ‘G’ is said to be complete if each vertex Vi is adjacent to every
other vertex Vj in G is called complete graph. (In short every node is
connected to every node).
Acyclic Graph
If a directed graph do not have any cycle than it is called acyclic graph.
Isolated Graph
A vertex is isolated if there is no edge connected from any other vertex to
that vertex.
Path
A route that does not pass any edge more than one’s is called path of the
graph.
4
Length of the path
Total edges between starting and ending nodes of the path is called length
of the path. (Distance).
Degree of Vertex
The number of edges connected with vertex Vi is called the degree of
vertex Vi. (How many nodes are connected to one node…).
Indegree
Number of edges comes into Vi, is called indegree of Vi.
Out Degree
Number of edges going from Vi, is called outdegree of Vi.
5
Pendent Degree
A vertex Vi is called pendent if, its indegree is one and outdegree is zero.
REPRESENTATION OF GRAPH
1. Set representation
2. Linked representation
3. Sequential (matrix) representation.
6
Read Graph_Representation.pdf
Set Representation
This is one of the straight forward methods of representing a graph. With
this method, two sets are maintained: (i) V, the set of vertices, (ii) E, the
set of edges.
Graph G1
V(G1) = { A, B, C, D}
e(G1) = {e1, e2, e3, e4}
E(G1) ={(A-B),(A-C),(B-D),(C-D)}
Linked Representation
Linked representation is another space-saving way of graph representation.
In it, the number of lists depends on the number of vertices in the graph.
Matrix Representation
Matrix representation is the most useful way of representing any graph.
This representation uses a square matrix of order n x n, n being the number
of vertices in the graph. The matrix is alternatively termed as bit matrix or
Boolean matrix as the entries are either 0 or 1.
7
A B C D
A 0 1 1 0
B 1 0 0 1
C 1 0 0 1
D 0 1 1 0
Set
G1(V)={v1, v2, v3, v4, v5, v6, v7}
G1(e)={e1, e2, e3, e4, e5, e6, e7, e8, e9}
G1(E)={(v1-v2),(v1-v3),(v2-v4),(v2-v1),(v2-v5),(v3-v1),(v3-v7),(v3-
v4),(v4-v2),(v4-v3),(v4-v6),(v5-v2),(v5-v6),(v6-v4),(v6-v5),(v6-v7),(v7-
v6),(v7-v3)}
List
8
Matrix
V1 V2 V3 V4 V5 V6 V7
V1 0 1 1 0 0 0 0
V2 1 0 0 1 0 0 0
V3 1 0 0 1 0 0 1
V4 0 1 1 0 0 1 0
V5 0 1 0 0 0 1 0
V6 0 0 0 1 1 0 1
V7 0 0 1 0 0 1 0
Set
G1(V)={v1, v2, v3, v4, v5, v6, v7}
G1(e)={e1, e2, e3, e4, e5, e6, e7, e8, e9}
G1(E)={(v1-v2),(v3-v1),(v2-v4),(v3-v4),(v5-v2),(v4-v6),(v3-v7),(v5-
v6),(v6-v7)}
List
9
Matrix
V1 V2 V3 V4 V5 V6 V7
V1 0 1 0 0 0 0 0
V2 0 0 0 1 0 0 0
V3 1 0 0 1 0 0 1
V4 0 0 0 0 0 1 0
V5 0 1 0 0 0 1 0
V6 0 0 0 0 0 0 1
V7 0 0 0 0 0 0 0
GRAPH TRAVERSALS
10
There are two methods of traversing a graph.
First method is traversing the graph breadth wise, while second method is
traversing a graph in depth.
This method is used to traverse a graph for finding a shortest path from the
starting node to the ending node.
Using BFS algorithm, shortest distance is the minimum number of edges
from the start node to the other remaining nodes. BFS traversal method
will traverse node according to the level that is first level, second level etc.
In BFS we can start from any node of the graph which will be the starting
node of traversal. First we, visit all the adjacent nodes of the start node.
Then we take the next start node, whichever is the nearest of the main start
node. Again we follow the same process that is visit all adjacent nodes.
Here we maintain the flag which will monitor that one node should not
visited more than once.
11
Let’s take an example to understand above process.
Ans:
V1 – v2 – v8 – v3 – v5 – v4 – v7 – v6
12
Example 4: Convert the given graph into BFS.
Ans:
V1 - v3 – v4 – v2 – v6 – v7 – v5
Ans:
V1 – v2 – v3 – v5 – v4 – v7 – v6 – v8
13
Depth First Search
14
Ans:
V1 – V2 – V4 – V3
Ans:
v1 – v2 – v5 – v6 – v4 – v8 – v3 – v7
Back track
Ans:
v1 – v2 – v5 – v6 – v4 – v8 – v7 – v3
15
Ans:
V1 – V2 – V5 – V6 – V4 – V8 – V3 – V7
Application of Graph
16