Module 4
Introduction to Graphs
raph is a non linear data structure.Agraphcontainsasetofpointsknownasnodes(or
G
vertices) and set of links known as edges (or Arcs) which connects the vertices.
graphisdefinedasGraphisacollectionofverticesandarcswhichconnectsverticesin
A
thegraph.AgraphGisrepresentedasG=(V,E),whereVissetofverticesandEis
set of edges.
Example: graph G can be defined as G = ( V , E ) Where V = {A,B,C,D,E} and
= {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}. This is a graph with 5 vertices and
E
6 edges.
Graph Terminology
.
1 Vertex: An individual data element of a graph iscalled as Vertex. Vertex is
also known as node. In above example graph, A, B, C, D & E are known as
vertices.
.
2 Edge: An edge is a connecting link between two vertices.Edge is also known
as Arc. An edge is represented as (starting Vertex, ending Vertex).
3. Adjacent vertices:Two vertices are adjacent, if theyare connected by an edge.
4. Self loop :Any edge that have only one vertex arecalled self loop.
.
5 Parallel edges:In some cases, two edges are presentassociated with
two vertex.These edges are called parallel edges.
6. Degree of vertex :Total number of edges connectedto a vertex is said to be degree
of that vertex.
Indegree
Total number of incoming edges connected to a vertex is said to be indegree of that vertex.
Outdegree
Total number of outgoing edges connected to a vertex is said to be outdegree of that vertex.
7. Isolated vertex:A vertex with degree zero.
8. Pendant vertex:A vertex with degree one is calledPendant vertex.
9.Sub Graph :
graph S is said to be a sub graph of a graph G if all the vertices and all the edges of S are
A
in G, and each edge of S has the same end vertices in S as in G. A subgraph of G is a
graph
G’ such that V(G’)⊆V(G) and E(G’)⊆E(G)
In data structures, a subgraph refers to a subset of a larger graph. It consists of a subset
of vertices and edges taken from the original graph, maintaining the relationships
between the selected vertices. Subgraphs are commonly used to analyze specific parts
of a larger graph or to perform operations on a smaller portion of the data.
10 Path:In a graph ,a path is a sequence of nodes inwhich each node is connected by
an edge to the next.
11 Cycles :A cycle in a graph is a non empty trail inwhich only the first and last
vertices are equal.
R A Cycle is a path that starts and ends at the same vertex, traversing a series of
O
edges without revisiting any vertex (except the starting vertex).
12Outgoing Edge
A directed edge is said to be outgoing edge on its orign vertex.
13 Incoming Edge
A directed edge is said to be incoming edge on its destination vertex.
TYPES OF GRAPH
1.Undirected Graph
A graph with only undirected edges is said to be undirected graph.
2.Directed Graph
A graph with only directed edges is said to be directed graph.
3.Complete Graph
graphinwhichanyVnodeisadjacenttoallothernodespresentinthegraphisknown
A
as a complete graph. An undirected graph contains the edges that are equal to edges =
n(n-1)/2 where n is the number of vertices present in the graph. The following figure
shows a complete graph. Or
A complete graph is a graph in which there is a unique edge connecting every pair of
distinct vertices. Inacompletegraphwithnvertices,there aren.(n-1)/2 edges,
nd it is denoted by ��Kn , where n represents the number of vertices.
a
Complete graphs are characterized by their high level of connectivity, and they
are often used in graph theory as a reference point for certain properties or
algorithms.
4.Regular Graph
regular graph is a graph in which each vertex has the same number of
A
neighbors, i.e., the same degree. If all vertices in the graph have the same
egree, the graph is
d
said to be k-regular, where k is the degree of each vertex
5.Cyclic Graph
graph having cycle is called cyclic graph. In this case thefirstandlastnodesarethe
A
same. A closed simple path is a cycle.
6.Acyclic Graph
A graph without cycle is called acyclic graphs.
7 Simple Graph
A graph is said to be simple if there are no parallel and self-loop edges.
8 Connected Graph
graph G is said to be connected if there is at least one path between every pair of
A
vertices in G. Otherwise,G is disconnected.
connected graph G
A A
disconnected graph G
his graph is disconnected because the vertex v1 is not connected with the other vertices of
T
the graph.
9 Disconnected Graph
graphisdisconnectedifatleasttwoverticesofthegrapharenotconnected
A
by a path. If a graph G is disconnected, then every maximal connected
subgraph of G is called a connected component of the graph G.
Vertex 1 Vertex 2 PATH
a b a b
a c Not Available
a d Not Available
b c Not Available
c d c d
10. Bipartite Graph: Abipartite graph is a graph in whichthe vertices can
e divided into two disjoint sets, such that no two vertices within the same set
b
are adjacent. In other words, it is a graph in which every edge connects a
vertex of one set to a vertex of the other set.
An alternate definition:Formally, a graph G = (V,E) is bipartite if and only if its
vertex set V can be partitioned into two non-empty subsets X and Y, such that
every edge in E has one endpoint in X and the other endpoint in Y. This
partition of vertices is also known as bi-partition.
Bipartite Graph
Note: In the above image nodes of the same colourbelong to the same set.
1. Complete bipartite graph : Completebipartitegraphisaspecialtypeof
1
bipartite graph where every vertex of one set is connected to every vertex of other
set.
GRAPH REPRESENTATION
1. Adjacent Matrix
he adjacency matrix in a graph is a matrix with rows and columns, represented by
T
graph vertices with 1 or 0 in the position𝑉𝑖𝑗 according to whether𝑉𝑖 or𝑉𝑗 has a link or
ot.Insuchamatrixwhen𝑖𝑡ℎ rowand𝑗𝑡ℎ columnelementis1,thentherewillbeanedge
n
between 𝑖 and 𝑗vertex.Whenthereisnoedge,thevaluewillbezero.Theabovegraph
can be represented is as follows:
2. Adjacent List
headjacentlistofagraphislinkedwithonelistpervertex.Itwillhaveaheadlist
T
which contains all the vertices in a sequential order and each element in its linked list
represents the other vertices that form an edge with the vertex. The adjacency list
representation of above graph is as follows:
GRAPH TRAVERSAL METHODS
1. Depth First Search (DFS)
his algorithm traverses a graph in a depth ward direction and uses a stack to
T
remember the next vertex to be visited when a dead end occurs i.e. for back tracking.
tep 1:Define a stack of size with total number ofvertices in the graph.
S
Step 2:Select any vertex as starting point for traversalvisit that vertex and push it on to
the stack.
Step 3:Visit any one of the adjacent vertex of thevertex which at the top of stack and is
not visited and push it on to the stack.
Step 4:Repeat step 3 until there are no vertex tovisit from vertices on the stack.
tep 5:When there is no vertex to visit then useback tracking and pop one vertex from the
S
stack.
Step 6:Repeat steps 3, 4 and 5 until stack becomesempty.
Step 7:When stack become empty it will give the sequenceof vertices visited
2.Breadth First Search (BFS)
This algorithm traverses a graph in breadth wise direction and is utilising a queue for processing
vertices.
tep 1:Define a sequence of size with total numberof vertices in a graph.
S
Step 2:Select any vertex as starting point for thetraversal and visit that vertex. Insert the newly
selected vertex into the queue.
Step 3:Visit all adjacent vertex that vertex whichis at the front of the queue and is not visited, insert that
vertex on to queue.
Step 4:Repeat step 3 until there is no new vertexto be visited from queue.
Step 5:When queue is empty an algorithm produces BFS sequence