0% found this document useful (0 votes)
24 views49 pages

Unit 6 Graph

The document provides an overview of graph theory, defining key concepts such as vertices, edges, paths, cycles, and various types of graphs including directed, undirected, weighted, and complete graphs. It also discusses graph representation methods like adjacency matrices and lists, and introduces graph traversal algorithms such as Breadth First Search (BFS). Additionally, it explains the characteristics of connected and disconnected graphs, as well as the concept of complementary graphs and labeled graphs.

Uploaded by

parthpraval05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views49 pages

Unit 6 Graph

The document provides an overview of graph theory, defining key concepts such as vertices, edges, paths, cycles, and various types of graphs including directed, undirected, weighted, and complete graphs. It also discusses graph representation methods like adjacency matrices and lists, and introduces graph traversal algorithms such as Breadth First Search (BFS). Additionally, it explains the characteristics of connected and disconnected graphs, as well as the concept of complementary graphs and labeled graphs.

Uploaded by

parthpraval05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

UNIT 6

GRAPH
INTRODUCTION

 A graph G can be defined as an ordered set G(V,


E) where V(G) represents the set of vertices and
E(G) represents the set of edges which are used
to connect these vertices.
 A Graph G(V, E) with 5 vertices (A, B, C, D, E) and

six edges ((A,B), (B,C), (C,E), (E,D), (D,B), (D,A)) is


shown in the following figure.
GRAPH TERMINOLOGY
 Path: A path can be defined as the sequence of
nodes that are followed in order to reach some
terminal node V from the initial node U.
 Closed Path : A path will be called as closed path if

the initial node is same as terminal node. A path will


be closed path if V0=VN.
 Simple Path : If all the nodes of the graph are
distinct with an exception V0=VN, then such path P is
called as closed simple path.
 Cycle : A cycle can be defined as the path which has

no repeated edges or vertices except the first and


last vertices.
 Connected Graph : A connected graph is the one in

which some path exists between every two vertices


CONT…
 Complete Graph : A complete graph is the one in which every
node is connected with all other nodes. A complete graph
contain n(n-1)/2 edges where n is the number of nodes in the
graph.
 Weighted Graph :In a weighted graph, each edge is assigned
with some data such as length or weight. The weight of an
edge e can be given as w(e) which must be a positive (+)
value indicating the cost of traversing the edge.
 Digraph : A digraph is a directed graph in which each edge of
the graph is associated with some direction and the traversing
can be done only in the specified direction.
 Loop : An edge that is associated with the similar end points
can be called as Loop.
 Adjacent Nodes : If two nodes u and v are connected via an
edge e, then the nodes u and v are called as neighbors or
adjacent nodes.
 Degree of the Node : A degree of a node is the number of
edges that are connected with that node. A node with degree
TYPES OF GRAPHS:
1. Null Graph
2. Undirected Graphs
3. Multigraphs
4. Directed Graphs
5. Undirected Complete Graph
6. Connected and Disconnected Graph
7. Connected Component
8. Directed Complete Graph
9. Complementary Graph
10. Labeled Graphs
11.Weighted Graphs
NULL GRAPH
 A null graph is defined as a graph which consists
only the isolated vertices.
 Example: The graph shown in fig is a null graph,

and the vertices are isolated vertices.


UNDIRECTED GRAPHS
 An Undirected graph G consists of a set of vertices, V and a set of edge
E.
 The edge set contains the unordered pair of vertices.
 If (u, v)∈E then we say u and v are connected by an edge where u and
v are vertices in the set V.
 Example: Let V = {1, 2, 3, 4} and E = {(1, 2), (1, 4), (3, 4), (2,
3)}.Draw the graph.
 Solution: The graph can be drawn in several ways. Two of which are as
follows:
MULTIGRAPH.
 If in a graph multiple edges between the same
set of vertices are allowed, it is known as
Multigraph.
 In other words, it is a graph having at least one

loop or multiple edges.


DIRECTED GRAPHS
 A directed graph or digraph G is defined as an unordered pair (V,
E), where V is the set of points called vertices and E is the set of
edges.
 Each edge in the graph G is assigned a direction and is identified
with an ordered pair (u, v), where u is the initial vertex, and v is
the end vertex.
 Example: Consider the graph G = (V, E) as shown in fig.
Determine the vertex set and edge set of graph G.
 Solution: The vertex and edge set of graph G =(V, E) is as
follow
G={{1,2,3},{(1,2),(2,1),(2,2),(2,3),(1,3)}}.
UNDIRECTED COMPLETE GRAPH:
 An undirected complete graph G=(V,E) of n vertices
is a graph in which each vertex is connected to
every other vertex i.e., and edge exist between
every pair of distinct vertices. It is denoted by K n.
 Example: Draw Undirected Complete Graphs k4and
k6.
 Solution: The undirected complete graph of k4 is
shown in fig1 and that of k6is shown in fig2.
CONNECTED AND DISCONNECTED
GRAPH:
 Connected Graph: A graph is called connected if there is a
path from any vertex u to v or vice-versa.
 Disconnected Graph: A graph is called disconnected if
there is no path between any two of its vertices.
 Example: Consider the graph shown in fig. Determine
whether the graphs are
(a)Disconnected Graph
(b)Connected Graph.
Also, write their connected components.
SOLUTION:
 (i) The graph is shown in fig is a Disconnected Graph, and
its connected components are
{V1,V2,V3,V4},{V5,V6,V7,V8} and {V9,V10}.
 (ii) The graph shown in fig is a Disconnected Graph and its
connected components are
{V1,V2}, {V3,V4}, {V5,V6}, {V7,V8}, {V9,V10} and {V11,V12}.

 (iii) The graph shown in fig is a connected graph.


CONNECTED COMPONENT:
 A subgraph of graph G is called the connected component
of G, if it is not contained in any bigger subgraph of G,
which is connected. It is defined by listing its vertices.
 Example: Consider the graph shown in fig. Determine its
connected components.
 Solution: The connected components of this graph is {a,
b, c}, {d, e, f}, {g, h ,i} and {j}.
DIRECTED COMPLETE GRAPH
 A directed complete graph G = (V, E) on n vertices is a
graph in which each vertex is connected to every other
vertex by an arrow. It is denoted by K n.
 Example: Draw directed complete graphs K 3 and K5.
 Solution: Place the number of vertices at the
appropriate place and then draw an arrow from each
vertex to every other vertex as shown in fig:
COMPLEMENTARY GRAPH
 The complement of a graph G is defined to be a
graph which has the same number of vertices as in
graph G and has two vertices connected if and only
they are not related in the graph.
 Example: Consider the graph G shown in fig. Find

the complement of this graph.


 Solution: The complement of the above graph is

shown in Fig:
LABELED GRAPHS:
 A graph G=(V, E) is called a labeled graph if its edges
are labeled with some name or data. So, we can write
these labels in place of an ordered pair in its edges set.
 Example: The graph shown in fig is labeled graphs.

G= {{a, b, c, d}, {e1,e2,e3,e4}}


WEIGHTED GRAPHS:

 A graph G=(V, E) is called a weighted graph if


each edge of graph G is assigned a positive
number w called the weight of the edge e.
 Example: The graph shown in fig is a Weighted

Graph.
REPRESENTATION OF GRAPHS
 By Graph representation, we simply mean the
technique which is to be used in order to store
some graph into the computer's memory.
 There are two ways to store Graph into the

computer's memory.
 Sequential, List structure, Adjacency list,
multilinked representation
1. SEQUENTIAL REPRESENTATION

 In sequential representation, we use adjacency


matrix to store the mapping represented by
vertices and edges. In adjacency matrix, the rows
and columns are represented by the graph
vertices. A graph having n vertices, will have a
dimension n x n.
 An entry M in the adjacency matrix representation
ij
of an undirected graph G will be 1 if there exists
an edge between Vi and Vj.
ADJACENCY MATRIX
REPRESENTATION OF
UNDIRECTED GRAPH
 If an Undirected Graph G consists of n vertices then the adjacency
matrix of a graph is an n x n matrix A = [aij] and defined by
 If there exists an edge between vertex vi and vj, where i is a row
and j is a column then the value of aij=1.
 If there is no edge between vertex vi and vj, then value of aij=0.
 Example: Find the adjacency matrix MA of graph G shown in Fig:
CONT..

 Solution: Since graph G consist of four vertices.


Therefore, the adjacency matrix wills a 4 x 4 matrix.
The adjacency matrix is as follows in fig:
ADJACENCY MATRIX
REPRESENTATION FOR DIRECTED
GRAPH
 If a directed graph G consists of n vertices then
the adjacency matrix of a graph is an n x n matrix
A = [aij] and defined by

o If there exists an edge between vertex Vi and Vj,


with Vi as initial vertex and Vj as a final vertex, then
the value of aij=1.
o If there is no edge between vertex Vi and Vj, then
the value of aij=0.
o The number of ones in the adjacency matrix of a
directed graph is equal to the number of edges.
EXAMPLE:
 Consider the directed graph shown in fig.
Determine its adjacency matrix MA.

Solution: Since the directed graph G consists of


five vertices. Therefore, the adjacency matrix will
be a 5 x 5 matrix. The adjacency matrix of the
directed graphs is as follows:
2. LINKED REPRESENTATION
 In the linked representation, an adjacency list is
used to store the Graph into the computer's
memory.
 Consider the undirected graph shown in the

following figure and check the adjacency list


representation.
CONT…
 An adjacency list is maintained for each node present in the
graph which stores the node value and a pointer to the next
adjacent node to the respective node.
 If all the adjacent nodes are traversed then store the NULL in
the pointer field of last node of the list.
 The sum of the lengths of adjacency lists is equal to the twice
of the number of edges present in an undirected graph.
 Consider the directed graph shown in the following figure and
check the adjacency list representation of the graph.
CONT..
 In a directed graph, the sum of lengths of all the adjacency
lists is equal to the number of edges present in the graph.
 In the case of weighted directed graph, each node
contains an extra field that is called the weight of the
node. The adjacency list representation of a directed graph
is shown in the following figure.
3. MULTI LINKED REPRESENTATON
 Adjacency Multi lists are lists in which nodes may be shared
among several lists.
 An edge in an undirected graph is represented by two nodes in
adjacency list representation.
 Adjacency Multi-lists are an edge, rather than vertex based,
graph representation.
 In the Multilist representation of graph structures; these are two
parts, a directory of Node information and a set of linked list of
edge information.
 There is one entry in the node directory for each node of the
graph.
 The directory entry for node i points to a linked adjacency list for
node i.
 each record of the linked list area appears on two adjacency lists:
one for the node at each end of the represented edge.
CONT..
An edge in an undirected graph is
represented by two nodes in adjacency
list representation.
Adjacency Multilists
– lists in which nodes may be shared among
several lists.
(an edge is shared by two different paths)

marked vertex1 vertex2 path1 path2


GRAPH TRAVERSAL ALGORITHM

 Traversing the graph means examining all the


nodes and vertices of the graph.
 There are two standard methods by using which,

we can traverse the graphs.


 Breadth First Search
 Depth First Search
BREADTH FIRST SEARCH (BFS) ALGORITHM

 Breadth first search is a graph traversal algorithm that


starts traversing the graph from root node and explores all
the neighboring nodes.
 Then, it selects the nearest node and explore all the
unexplored nodes.
 The algorithm follows the same process for each of the
nearest node until it finds the goal.
 The algorithm of breadth first search is given below. The
algorithm starts with examining the node A and all of its
neighbours.
 In the next step, the neighbours of the nearest node of A
are explored and process continues in the further steps.
 The algorithm explores all neighbours of all the nodes and
ensures that each node is visited exactly once and no node
is visited twice.
ALGORITHM

 Step 1: SET STATUS = 1 (ready state)


for each node in G
 Step 2: Enqueue the starting node A
and set its STATUS = 2
(waiting state)
 Step 3: Repeat Steps 4 and 5 until
QUEUE is empty
 Step 4: Dequeue a node N. Process it
and set its STATUS = 3
(processed state).
 Step 5: Enqueue all the neighbours of
N that are in the ready state
(whose STATUS = 1) and set
their STATUS = 2
(waiting state)
[END OF LOOP]
 Step 6: EXIT
EXAMPLE

 Consider the graph G shown in the following


image, calculate the minimum path p from
node A to node E. Given that each edge has
a length of 1.
SOLUTION:
 Minimum Path P can be found by applying breadth first
search algorithm that will begin at node A and will end at E.
 The algorithm uses two queues, namely QUEUE1 & QUEUE2.
 QUEUE1 holds all the nodes that are to be processed
while QUEUE2 holds all the nodes that are processed and
deleted from QUEUE1.
 Lets start examining the graph from Node A.
 1. Add A to QUEUE1 and NULL to QUEUE2.
QUEUE1 = {A}
QUEUE2 = {NULL}
 2. Delete the Node A from QUEUE1 and insert all its
neighbours. Insert Node A into QUEUE2
QUEUE1 = {B, D}
QUEUE2 = {A}
 3. Delete the node B from QUEUE1 and insert all its
neighbours . Insert node B into QUEUE2.
QUEUE1 = {D, C, F}
QUEUE2 = {A, B}
CONT..
 4. Delete the node D from QUEUE1 and insert all its neighbours.
Since F is the only neighbour of it which has been inserted, we will
not insert it again. Insert node D into QUEUE2.
QUEUE1 = {C, F}
QUEUE2 = { A, B, D}
 5. Delete the node C from QUEUE1 and insert all its neighbours.
Add node C to QUEUE2.
QUEUE1 = {F, E, G}
QUEUE2 = {A, B, D, C}
 6. Remove F from QUEUE1 and add all its neighbours. Since all of
its neighbours has already been added, we will not add them
again. Add node F to QUEUE2.
QUEUE1 = {E, G}
QUEUE2 = {A, B, D, C, F}
 7. Remove E from QUEUE1, all of E's neighbours has already been
added to QUEUE1 therefore we will not add them again. All the
nodes are visited and the target node i.e. E is encountered into
QUEUE2.
QUEUE1 = {G}
QUEUE2 = {A, B, D, C, F, E}
 Now, backtrack from E to A, using the nodes available in QUEUE2.
 The minimum path will be A → B → C → E.
APPLICATIONS OF BREADTH-
FIRST SEARCH ALGORITHM
 Breadth-first search can be used to solve
many problems such as:
 Finding all connected components in a graph G.
 Finding all nodes within an individual connected
component.
 Finding the shortest path between two nodes, u
and v, of an unweighted graph.
 Finding the shortest path between two nodes, u
and v, of a weighted graph.
DEPTH FIRST SEARCH (DFS)
ALGORITHM
 Depth first search (DFS) algorithm starts with the
initial node of the graph G, and then goes to deeper
and deeper until we find the goal node or the node
which has no children.
 The algorithm, then backtracks from the dead end

towards the most recent node that is yet to be


completely unexplored.
 The data structure which is being used in DFS is

stack.
 The process is similar to BFS algorithm.

 In DFS, the edges that leads to an unvisited node are

called discovery edges while the edges that leads to


an already visited node are called block edges.
ALGORITHM
 Step 1: SET STATUS = 1 (ready state) for each node in
G
 Step 2: Push the starting node A on the stack and set

its STATUS = 2 (waiting state)


 Step 3: Repeat Steps 4 and 5 until STACK is empty

 Step 4: Pop the top node N. Process it and set its

STATUS = 3 (processed state)


 Step 5: Push on the stack all the neighbours of N that

are in the ready state (whose STATUS = 1) and set their


STATUS= 2 (waiting state)
[END OF LOOP]
 Step 6: EXIT
EXAMPLE :
 Consider the graph G along with its adjacency list,
given in the figure below.
 Calculate the order to print all the nodes of the

graph starting from node H, by using depth first


search (DFS) algorithm.
SOLUTION :
 Push H onto the stack
STACK : H
 POP the top element of the stack i.e. H, print it and push all the
neighbours of H onto the stack that are is ready state.
Print H
STACK : A
 Pop the top element of the stack i.e. A, print it and push all the
neighbours of A onto the stack that are in ready state.
Print A
Stack : B, D
 Pop the top element of the stack i.e. D, print it and push all the
neighbours of D onto the stack that are in ready state.
Print D
Stack : B, F
 Pop the top element of the stack i.e. F, print it and push all the
neighbours of F onto the stack that are in ready state.
Print F
Stack : B
CONT..
 Pop the top of the stack i.e. B and push all the neighbours
Print B
Stack : C
 Pop the top of the stack i.e. C and push all the
neighbours.
Print C
Stack : E, G
 Pop the top of the stack i.e. G and push all its neighbours.
Print G
Stack : E
 Pop the top of the stack i.e. E and push all its neighbours.
Print E
Stack :
 Hence, the stack now becomes empty and all the nodes
of the graph have been traversed.
 The printing sequence of the graph will be :
 H→A→D→F→B→C→G→E
APPLICATIONS OF DEPTH-FIRST
SEARCH ALGORITHM
 Depth-first search is useful for:
 Finding a path between two specified nodes,

u and v, of an unweighted graph.


 Finding a path between two specified nodes,

u and v, of a weighted graph.


 Finding whether a graph is connected or not.

 Computing the spanning tree of a connected

graph.
SHORTEST PATH ALGORITHM
 Let G = (V,E) be a simple diagraph with n vertices
 The problem is to find out shortest distance from a

vertex to all other vertices of a graph


 Dijkstra Algorithm – it is also called Single Source

Shortest Path Algorithm


DIJKSTRA ALGORITHM – SHORTEST PATH

2
1 B D
6 A B C DE F
4
0 A 1 7 F Distance 0 ∞ ∞ ∞∞ ∞
Visite
5 2 0 0 0 000
d
C 5
E

1st Iteration: Select Vertex A with minimum distance

1 ∞
B
2
D A B C DE F

1 6 Distanc 0∞1∞5 ∞∞ ∞
4 F e Visite
0 A 1 7
10 0000
d
5 2
C 5
E∞
5
DIJKSTRA ALGORITHM – SHORTEST PATH
2nd Iteration: Select Vertex B with minimum distance
ost of going to C via B = dist[B] + cost[B][C] = 1 + 1 = 2
ost of going to D via B = dist[B] + cost[B][D] = 1 + 2 = 3
Cost of going to E via B = dist[B] + cost[B][E] = 1 + 4 = 5
ost of going to F via B = dist[B] + cost[B][F] = 1 + ∞ = ∞ A B C DE F
Distance 0 1 5 ∞∞ ∞
1 3 1 0 0 0 0 0
Visited
2
B D
6
1
4
0 A 1 7 F∞ A B C DE F
5
2
Distanc 0 1 2 3 5 ∞
5 e Visite
C E
11 0000
2 5 d
DIJKSTRA ALGORITHM – SHORTEST PATH
3rd Iteration: Select Vertex C via B with minimum distance
ost of going to D via C = dist[C] + cost[C][D] = 2 + ∞ = ∞
ost of going to E via C = dist[C] + cost[C][E] = 2 + 5 = 7
Cost of going to F via C = dist[C] + cost[C][F] = 2 + ∞ = ∞ A B C D E F
Distance 0 1 2 3 5 ∞
1 3 1 1 0 0 0 0
Visited
2
B D
6
1
4
0 A 1 7 F A B C DE F
5 Distanc
2 ∞ 0 1 2 35∞
C
5
E e
Visited 11 1000
2 5
DIJKSTRA ALGORITHM – SHORTEST PATH
4th Iteration: Select Vertex D via path A - B with minimum distance
ost of going to E via D = dist[D] + cost[D][E] = 3 + 7 = 10
ost of going to F via D = dist[D] + cost[D][F] = 3 + 6 = 9
A B C DE F
Distance 0 1 2 3 5 ∞
1 3 Visited 1 1 1 0 0 0
2
B D
6
1
4
A 1 7 F
5 A B C DE F
9 Distanc
2
5 0 1 2 359
C E e Visite
11 1100
2 5 d
DIJKSTRA ALGORITHM – SHORTEST PATH
4th Iteration: Select Vertex E via path A – B – E with minimum distance
Cost of going to F via E = dist[E] + cost[E][F] = 5 + 2 = 7
A B C DE F
Distance 0 1 2 3 5 9
Visited 1 1 1 1 0 0

1 3
2
B D
6
1 A B C DE F
4
A 1 7 F Distanc
7
5 0 1 2 357
2 e
Visited 1 1 1 1 1 0
C E
2 5
Shortest Path from A to F is
ABEF=7
SHORTEST PATH
Find out shortest path from node 0 to all other nodes
using Dijkstra Algorithm
0
0 100 0 100
10 10 60
10
30 4 30 4
1 1

60 60
50 10 50 10

2 3 2 3
20 50 20 30

You might also like