0% found this document useful (0 votes)
10 views105 pages

DS Graphs

Uploaded by

Sailesh Sailesh
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)
10 views105 pages

DS Graphs

Uploaded by

Sailesh Sailesh
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/ 105

6.

Graphs

Compiled By
Ujjwal Rijal
[email protected]

[email protected] 1
Introduction to
Graph
 Graph is a non-linear data structure which contains a set of
points known as nodes (or vertices) and a set of links known as
edges (or arcs), which connects the vertices.
 In general, a graph G = (V, E) is a mathematical model that
consists of two non-empty sets i.e. V = {v1 , v2 , v3 , ………. , vn}
called set of vertices and E = {e1 , e2 , e3 , ………. , en} called set
of edges.
 Graph is a discrete structure consisting of vertices and edges that
connect these vertices. Problems in almost every conceivable discipline
can be solved using graph model.
 Graph was introduced in eighteenth century by the great Swiss
mathematician Leanhard Euler.

[email protected] 2
Applications of
Graph
 Graphs are used to solve many problems in many fields as applications.
For example,

• Graphs can be used to determine whether a circuit can be implemented on a


planar circuit board.
• Graphs can be used to distinguish two chemical compounds with the same
molecular formula but different structures.
• Graphs can be used to study the structure of the WWW (World Wide Web).
• Graph model of computer network can be used to determine whether two
computers are connected by a communication link.
• Graphs with weights assigned to their edges can be used to solve problems
such as finding the shortest path between two cities in a transportation network.
• Graphs can also be used to schedule exams and assign channels to television
stations, etc. [email protected] 3
Undirected and Directed Graphs
• If the edge pair is ordered, then the graph is called directed graph.

• Directed graphs are sometimes called digraphs.

• A directed graph (V, E) consists of a set “V” of vertices, a set “E” of edges that are ordered
pairs of elements of “V”.

• In directed graph, loop is allowed but no two vertices can have multiple or parallel edges in
the same direction.

• If the edge pair is unordered, then the graph is called undirected graph.

[email protected] 4
Types of Graph
 Simple Graph
• We define a simple graph as 2 – tuple consists of a non empty set of vertices V
and a set of unordered pairs of distinct elements of vertices called edges.

• A simple graph G = (V, E) consists of V, a nonempty set of vertices, and


E, a set of unordered pairs of distinct elements of V called edges,
having undirected edges, no loops and no multiple (or parallel) edges.

• The figure below is an example of simple graph:

In this graph G = (V, E), the set of vertices, V (G) or V = {a, b, c


and the set of edges E (G) or E is given by,
{{a, b}, {a, f}, {b, c}, {b, e}, {c, d}, {c, g}, {d, e}, {e, f},{e,

[email protected]
5
Types of Graph
 Multi-Graph
• A graph G = (V, E) consists of a set of vertices “V”, a set of edges “E” such that
some of the edges are the parallel or multiple edges is called multi-graph, i.e. this
type of graph can have multiple edges between the same two vertices.

• Two or more edges between same pair of vertices are called parallel or multiple
edges.

• This kind of graph has undirected edges, and no loops.

• Since simple graph has


b single edge, every simple graph is a multigraph.
a
d e
• The figure below is an example of a multigraph:
g
c
f [email protected]
6
Types of Graph
 Pseudo-Graph
• A graph G = (V, E) consists of a set of vertices “V”, a set of edges “E” is said to
be a pseudo-graph if “G” has both loops and parallel edges or loops only.

• The figures below are the examples of pseudo-graph:

[email protected]
7
Types of Graph
 Directed Multigraph
• A graph G = (V, E) consists of a set of vertices “V”, a set of edges “E” is said to
be a directed multigraph if “G” has both loops and parallel (or multiple) edges.

• The figure below shows the example of directed multigraph:

[email protected]
8
Graph Models / Graph Terminologies
 Order and Size of Graph

• If G = (V, E) be a finite graph then the number of vertices in graph “G” is called order
of graph “G” and the number of edges in graph “G” is called size of graph “G”.

 Degree (or Valency) of a Vertex

• Let G = (V, E) be a graph and “v” be a vertex of G. The degree (or valency) of v,
denoted by d(v), is the number of edges incident on v.

• A vertex v in graph G = (V, E) is said to be even vertex if its degree is even and a
vertex v in graph G = (V, E) is said to be odd vertex if its degree is odd.

 Isolated Vertex and Pendant Vertex

• In a graph G = (V, E), a vertex having degree zero(0) is called isolated vertex and
vertex having degree one (1) [email protected]
called pendant vertex.
9
Graph Models / Graph Terminologies
 Degree Sequence of a Graph

• If v1 , v2 , v3 , ….. , vn are the “n” vertices of a graph “G”, then the sequence (d 1 , d2 , d3 , ……. ,
dn) where di = deg(vi), is the degree sequence of the graph “G”.

• In general, we order the vertices so that the degree sequence is monotonically increasing.

 Adjacent Vertices and Adjacent Edges

• Two vertices u and v in an undirected graph are called adjacent vertices if there is an edge between u
and v.

• Two edges e1 and e2 in an undirected graph are called adjacent edges if they share a common vertex.

 In-degree and Out-degree of a Vertex

• The number of incoming edges to a vertex of a graph G = (V, E) signifies the in-degree of that vertex,
and the number of outgoing edges from a vertex of a graph G = (V, E) signifies the out-degree of
that vertex.
[email protected]
10
Graph Models / Graph Terminologies

In the above graph G = (V, E), Order of graph


= no. of vertices = 6,
Size of graph = no. of edges = 10.
deg(v1) = 5, deg(v2) = 4, deg(v3) = 4, deg(v4)
= 6, deg(v5) = 1, deg(v6) = 0.
Vertex v5 has degree 1, so it is a pendant
vertex and vertex v6 has degree 0, so it is an
isolated vertex.
Degree sequence of graph G = (0, 1, 4, 4, 5,
6).
Adjacent vertices of vertex v1 are v2 , v3 and v4
but vertices v5 and v6 are non-adjacent to v1.
Adjacent edges of edge {v1 , v2} are {v1 11
[email protected] , v1},
Special Types of Graphs
 Trivial Graph
• A graph with one vertex and no edges is called a trivial graph.
v1

 Complete Graph

• A graph G is said to be complete if there exists exactly one edge between


any pair of distinct vertices in the graph G.

• The complete graph with “n” vertices is denoted by Kn.

• Example: What are K1, K3, and K5?

[email protected] 12
K1 K3 K5
Special Types of Graphs
 Round-Robin Tournament
• A game tournament where each team plays with each other exactly once is called
Round-Robin tournament.
• Such tournaments can be modelled by using complete graph where there exists
exactly one edge between each possible parts of vertices except itself.
• The following figure shows the tournament between 5 teams in Round-Robin
fashion. Here, in the graph, vertices represent the teams and the edges between
vertices represent the matches between the five teams.

[email protected]
13
Special Types of Graphs
 Call Graph
• Graph can be used to model telephone calls in a telecommunication network.
Such type of graph is known as the call graph.
• In such graph, each telephone number is represented by a vertex and each
telephone call is represented by edge.
• The source vertex represents a start call (sender) and the vertex where edge
ends represents the end of the call (receiver).

[email protected]
14
Special Types of Graphs
 Regular Graph
• A graph in which all the vertices are of same (equal) degree is called a regular
graph.
• If the degree of each vertex is “r”, then the graph is a r-regular graph.

• In the above figures, K1 , K3 and K5 are 0-regular, 2-regular and 4-regular


graphs respectively.
[email protected]
15
Special Types of Graphs
 Bipartite Graph
• A graph G = (V, E) is said to be a bipartite graph if V is the union of two non-empty
disjoint subsets V1 and V2 of V such that edge in E is incident to one vertex in V 1 and one
vertex in V2.
• In other words, a graph G = (V, E) is said to be a bipartite graph if its vertex set V can be
partitioned into two subsets V1 and V2 such that each edge of G connect the vertex of V1
to the vertex of V2 so that no edge in G connect two vertices in V1 or two vertices in V2.

• In fig (i), bipartite sets are {X1 , X2 , X3 , X4} and {Y1 , Y2 , Y3}.
• In fig (ii), bipartite sets are {1, 3, 7, 9} and {2, 4, 5, 6, 8}.
[email protected]
16
Special Types of Graphs
 Complete Bipartite Graph
• A bipartite graph G is said to be complete if each vertex of first bipartite set is
connected to every vertices of another bipartite set.
• A complete bipartite graph with “m” number of vertices in first bipartite set and
“n” number of vertices in second bipartite set is denoted by Km,n.

K3, 3 K2,3

K2, 4 [email protected]
17
Special Types of Graphs
 Cycle Graph
• A graph G = (V, E) with vertex n ≥ 3 in which every vertex is connected with two
other vertices one on either side of it and last vertex is connected with first one
to form a closed path is called a cycle graph.

[email protected]
18
The Handshaking Theorem
• The handshaking theorem states that," The sum of the degrees of the vertices of a
graph is equal to the twice the number of edges in that graph.“
Proof:
Consider a graph G = (V, E) with “n” vertices v 1 , v2 , ……. , vn and |E| be the number of
edges in G. Since each edge contributes a degree of 2, the sum of the degrees of all the
vertices is twice the number of edges in G.
i.e. ) = 2 |E|

 Note: The number of odd vertices in a graph is always even.


Proof:
By the handshaking theorem, we have:
) = 2 |E|
Suppose that graph G = (V, E) contains “k” number of odd vertices which are v 1 , v2 ,
……. , vk and “n” number of even vertices which are u1 , u2 , ……. , un. Clearly, d(u1) + d(u2)
+ ……. + d(un) is even. [email protected]
19
Then, by handshaking theorem, we get:
The Handshaking Theorem
[d(v1) + d(v2) + ……. + d(vk)] + [d(u1) + d(u2) + ……. + d(un)] = 2 |E|
Where |E| is the number of edges.
Then, [d(v1) + d(v2) + ……. + d(vk)] = 2 |E| – [d(u1) + d(u2) + ……. + d(un)]
= 2|E| – even
= even
Hence, proved.

[email protected]
20
The Handshaking Theorem
Example: Find the sum of degrees of the vertices of the
graph G = (V, E) where V(G) = {v1 , v2 , …. , v5} and E(G) =
{(v1, v2) , (v1, v4) , (v1, v5) , (v2, v3) , (v2, v5) , (v3, v4)}.
 Solution:
Number of edges in G = |E| = 6.
Now, by handshaking theorem, Sum of degrees
of vertices in G is given by:
) = 2 |E|
=2x6
= 12.

[email protected]
21
Planar Graph
• A graph G = (V, E) is said to be a planar graph if it can be drawn in a plane such
that no intersection of edges exist at a point other than their common end-point
i.e. a graph is planar if it is drawn without crossing the edge.
• If a plannar graph is drawn in the plane, then the plane is divided into contiguous
regions, called faces.
• A face is characterized by a cycle that forms its boundary.

[email protected]
22
Planar Graph

[email protected]
23
Planar Graph

[email protected]
24
Planar Graph

[email protected]
25
Sub-Graph
• Let G=(V,E) be a graph with vertex set V(G) and edge set E(G) and
H=(V,E) be a graph with vertex set V(H) and edge set E(H), then H is
said to be subgraph of G if:
(i). All the vertices of H are in G.
(ii). All the edges of H are in G.
(iii). Each edge of H has the same end points in H as in G.

• Note: In above figures , G2 is a sub-graph of G1 but G3 is not a


subgraph of G1. [email protected] 26
Union and Intersection of Graphs

d [email protected] 27
 Representation of Undirected Graphs
One of the representation method of Graph is Matrix representation,
based on adjacency of vertices ,called adjacency matrix.
Another representation is based on incidence of vertices and edges called
incidence matrix.
And, the another way of representing an undirected graph without
multiple edges and directed graphs is by listing its edges, called
adjacency list.
1. Adjacency Matrix:
Let G=(V,E) be a graph with ‘n’ vertices V1,V2,V3,…….Vn. The
adjacency matrix of G with respect to given ordered list of vertices is a
n*n matrix denoted by A(G), such that
0 if there is no edge between the vertices vi and vj.
aij =
1 if there is an edge between the vertices vi and vj.
k if there are k>=2 edges between the vertices v28i
[email protected]
[email protected] 29
2. Incidence Matrix

[email protected] 30
[email protected] 31
3. Adjacency List
One of the ways of representing a graph without multiple edges is by
listing its edges.
This type of representation is suitable for the undirected graphs without
multiple edges, and directed graphs.
This representation looks like as shown in the below table:

Edge list for Simple Graphs


Vertex Adjacent
Vertices
A B, D
B A, C
C B, D
D A, C

[email protected] 32
 Representation of Directed Graphs

[email protected] 33
[email protected] 34
[email protected] 35
[email protected] 36
 Matching Theory
A matching graph is a sub-graph of a graph where there are no edges
adjacent to each other.
Simply, there should not be any common vertex between any two edges.
In a matching, no two edges are adjacent i.e. matching theory discusses
about the independent edges.
In a matching,
if deg(V) = 1, then V is said to be matched.
if deg(V) = 0, then V is not matched.
It is because if any two edges are adjacent, then the degree of vertex
which is joining those two edges will have the degree of two vertices,
which violates the matching rule or theory.

[email protected] 37
[email protected] 38
[email protected] 39
Graph Connectivity
1. Walk
o A walk can be defined as a sequence of edges and vertices of a graph. When we have a graph and traverse it,
then that traverse will be known as a walk. In a walk, there can be repeated edges and vertices. The number of
edges which is covered in a walk will be known as the Length of the walk.
So for a walk, the following two points are important, which are described as follows:
• Edges can be repeated
• Vertex can be repeated

In the above graph, there can be many walks, but some of them are described as follows:
1. A, B, C, E, D (Number of length = 4)
2. D, B, A, C, E, D, B, C (Number of length = 7)
3. E, C, B, A, C, E, D (Number of length = 6)

[email protected] 40
 Types of Walks
• There are two types of the walk, which are described as
follows:
1.Open walk
2.Closed walk
• Open Walk:
A walk will be known as an open walk in the graph theory if the vertices at
which the walk starts and ends are different. That means for an open walk,
the starting vertex and ending vertex must be different.
• Closed Walk:
A walk will be known as a closed walk in the graph theory if the vertices at
which the walk starts and ends are identical. That means for a closed walk,
the starting vertex and ending vertex must be the same.
[email protected] 41
• In this graph, there is also a closed walk and an open walk, which are described
as follows:

• Closed walk = A, B, C, D, E, C, A
• Open walk = A, B, C, D, E, C

[email protected] 42
2. Trails
• A trail can be described as a walk where no edge is allowed to repeat. In the
trails, the vertex can be repeated.

• In the above graph, there is a tail and closed tail, which is described as follows:
• Open Trail = A, C, H, F, C, B
• Closed Trail = A, C, H, F, C, B, A

[email protected] 43
3. Circuit
• A closed trail in the graph theory is also known as a circuit. So for a circuit, the following two
points are important, which are described as follows:
• Edges cannot be repeated
• Vertex can be repeated

Circuit: A, B, D, C, F, H, C, A

[email protected] 44
4. Cycle
• In cycle, neither edges, nor vertices are allowed to repeat. There is a possibility that only the starting vertex
and ending vertex are the same in a cycle. So for a cycle, the following two points are important, which are
described as follows:
• Edges cannot be repeated
• Vertex cannot be repeated

Cycle: A, B, D, C, A
[email protected] 45
5. Path
• A path is a type of open walk where neither edges nor vertices are allowed to
repeat. There is a possibility that only the starting vertex and ending vertex are
the same in a path. So for a path, the following two points are important,
which are described as follows:
• Edges cannot be repeated
• Vertex cannot be repeated

• Path: F, H, C, A, B, D (Open Path)


: A, C, D, B, A (Closed Path  Cycle)

[email protected] 46
 Isomorphism of a Graph
The isomorphism graph can be described as a graph in which a single graph can
have more than one form. That means two different graphs can have the same
number of edges, vertices, and same edges connectivity. These types of graphs are
known as isomorphism graphs. The example of an isomorphism graph is
described as follows:

[email protected] 47
 Conditions for Graph isomorphism
• Any two graphs will be known as isomorphism if they
satisfy the following five conditions:
1. There will be an equal number of vertices in the given graphs.
2. There will be an equal number of edges in the given graphs.
3. There will be an equal amount of degree sequence in the given graphs.
4. Identical Mapping.
5. A(G1) = A(G2) with respect to orders of vertices v1 , v2 , ……….. , vn .
Here, A(Gi) stands for the adjacency matrix of the given two graphs.

[email protected] 48
Example-1:

Condition1: No.of vertices: fig(a)= 4= fig(b).


Condition 2: No. of edges: fig(a)=5=fig(b).
Condition 3: Degree sequence: In fig(a): a=3, b=2, c=2, d=3.(2,2,3,3)
: In fig(b): x=3,z=2, w=2, y=3.(2, 2, 3,3)
Condition 4: Mapping : f(a) = x
:f(b) = w
:f(c) = z
:f(d) = y
Since all the properties of graph isomorphism are satisfied, so the given two graphs are isomorphic
graphs. [email protected] 49
Example-2:

Condition1: No.of vertices: G1 = 4= G2.


Condition 2: No. of edges: G1 = 3 = G2.
Condition 3: Degree sequence: In G1 : 1 = 1, 2 = 2, 3 = 2, 4 = 1.(1, 1, 2, 2)
: In G2 : a = 1, b = 2, c = 1, d = 2.(1, 1, 2, 2)
Condition 4: Mapping : f(1) = a
:f(2) = b
:f(3) = d
:f(4) = c
Since all the properties of graph isomorphism are satisfied, so the given two graphs G1 and G2 are isomorphic graphs.
[email protected] 50
Hence, Proved.
Example-3:

[email protected] 51
Example-4: Verify whether the following given graphs G and H are isomorphic or not.

[email protected] 52
 Travelling Salesman Problem (TSP)
• This problem was first formulated in 1930 and it is the most intensively studied
problem in graph theory and optimization.
• The general Travelling Salesman Problem (TSP) problem can be
formulated as:
Given a list of cities (represented by vertex of graph) and distance between
each pair of cities (edges), the problem here is to find out the shortest possible
route or path that the salesman travels each city exactly once and returns to the
origin or starting point (city).
• Example-1: Solve the Travelling Salesman Problem in the given graph where the
weights or costs of the paths are given in the following matrix.

[email protected]
53
[email protected]

54
• Example-2: Solve the Travelling Salesman Problem in the given graph.
5

10
2 10
10

3
Solution:
• First of all, we draw it’s corresponding matrix as follows:

[email protected]
55
[email protected]

56
 Euler’s Path
o A connected graph “G” has an Eulerian path or trail if and only if it has
exactly two odd vertices.
o In general, a path in a graph “G” is called Euler’s path if it includes every
edges exactly once.
o Here, vertices can be repeated but edges cannot be repeated.

 Euler’s Circuit (Cycle)


o A connected graph “G” is an Eulerian circuit if and only if each vertex has
even degree.
o In general, an Euler’s circuit is an Euler’s path with a circuit.

[email protected] 57
Examples:

[email protected] 58
[email protected] 59
[email protected] 60
 Hamiltonian Path / Hamilton Path
o Hamiltonian path or Hamilton path is a path containing each vertex of a
graph “G” exactly once.
o Here, it is not necessary to cover all the edges. The concern is that
we need to cover every vertex exactly once i.e. without repeating.
o In short, in this case, neither vertices nor edges can be repeated.

 Hamiltonian Circuit / Hamilton Circuit


o Hamiltonian circuit is a circuit containing each vertex exactly once except for
the first vertex which is also the last vertex.
o It is clear that loops and multiple edges are of no use in finding the
Hamiltonian circuits, since loops cannot be used and only one edge can be
used between any two given vertices.
o In short, in this case, neither vertices nor edges can be repeated.
[email protected] 61
Examples:

2.
1.

In this graph, it is not possible to Hamiltonian Path = acebd


cover every vertex without But it is not a Hamiltonian
repeating. So, it is not a circuit, since it is not possible to
Hamiltonian path. It means that it return to the first vertex at last
is not a Hamiltonian circuit also. without repeating i.e. the first
Hence, it is not a Hamiltonian and the last vertices are not
Graph as well. identical.

[email protected] 62
Examples:

4.
3. 4.

Hamiltonian Path = ABCD


Hamiltonian Circuit = ABCDA Hamiltonian Path = v1 v2 v3 v4 v5 v6
Hamiltonian Circuit = v1 v2 v3 v4 v5
v6 v1

[email protected] 63
Examples:

5.

Euler Path = No path (since there are not exactly two odd
vertices).
Euler Circuit = No circuit (since each vertex does not have even
degree).
Hamiltonian Path = v1 v2 v3 v7 v6 v5 v8 v4
Hamiltonian Circuit = v1 v2 v3 v7 v6 v5 v8 v4 v1
[email protected] 64
 Graph Coloring
Painting all vertices of a graph with colors such that no two adjacent
vertices have the same color is called coloring of graph.
 Chromatic Number: It is defined as the least number of colors
required for coloring of a graph ‘G’.
 It is denoted by X(G) or “K”. It is also known as chromaticity of a
graph.
And the graph consisting the “K” chromatic number is known
as the
K-chromatic graph.

[email protected] 65
Examples:
1.What is the chromatic number of complete graph?
A graph G is said to be complete if there exists exactly one edge
between any pair of distinct vertices in the graph G.

The complete graph with “n” vertices is denoted by Kn.

 So, every Kn complete graph will have n-chromatic number.

K3 complete graph i.e. 3-color


graph. K5 complete graph i.e. 5-color
i.e. X(G) or K = 3. graph.
i.e. X(G) or K = 5.
[email protected] 66
2.What is the chromatic number of bipartite graph and complete bipartite
grah?

The chromatic number of Km,n complete bipartite graph is always

2.

Also, the chromatic number of bipartite graph is always 2.

Bipartite graph i.e. 2-color


graph. K2,4 complete bipartite graph i.e.2-
i.e. X(G) or K = 2. color graph.
i.e. X(G) or K = 2.
[email protected] 67
3. 4.

2-color graph, i.e. X(G) or K = 2. 3-color graph, i.e. X(G) or K =


3.

5. 6.

3-color graph, i.e. X(G) or K = 3.


2-color graph, i.e. X(G) or K = 2.
[email protected] 68
7. 8.

3-color graph, i.e. X(G) or K = 3. 3-color graph, i.e. X(G) or K =


3.

9.

4-color graph, i.e. X(G) or K = 4.


[email protected] 69
 Application of Graph Coloring
Exam Scheduling

[email protected]

70
Exam Scheduling
Example-2: Use graph coloring to schedule exams of 10 subjects by assuming that
the pairs
{(1, 2), (1, 5), (1, 8), (2, 4), (2, 7), (2, 9), (3, 6), (3, 7), (3, 10), (4, 3), (4, 8), (4, 10), (5, 6),
(5, 7)} have common students for the exam.

Solution:
Now, the graph of the above given condition is drawn as follows:

Thus, we get,
Time Courses
Period
I 1, 3, 9 (Green Color)
II 2, 5, 8, 10 (Red Color)
III 4, 6, 7 (Yellow Color)

Hence, three time periods or slots


are needed for the examination.
[email protected]
71
 Graph Traversal

o Graph Traversal means traversing a graph. Traversing a graph means visiting


all the vertices in a graph exactly once.
o In graph, all the nodes are treated equally. So, the starting node in a graph
can be chosen arbitrarily (or randomly).
o There are two common approaches for the graph traversal:
Breadth First Search (BFS)
Depth First Search (DFS)
o Now, they are discussed below:

[email protected] 72
 Graph Traversal

Breadth First Search (BFS)


o The traversal starts at a node “v”, after marking the node, the traversal visits
all the incident edges to node “v” after marking the nodes and then moving
to an adjacent node, and repeating the same process.
o The traversal continues until all the unmarked nodes in the graph have been
visited.
o A queue is maintained in this technique to maintain the list of incident edges
and marked nodes. It works on First-in-First-out (FIFO) basis.
o In short, BFS works as follows:
First move horizontally and visit all the nodes of the current layer.
Move to the next layer and repeat until the shallowest unexpanded node is
expanded or explored.
[email protected] 73
 Graph Traversal

Let us consider the following graph:

Now, BFS works as follows:


[email protected] 74
 Graph Traversal

i.e. It follows the following sequence during BFS Search:


Z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V
[email protected] 75
 Graph Traversal

Depth First Search (DFS)


o This technique starts at the root node and explores as far as possible along
each branch before the process of backtracking.
o A stack is maintained in this technique to maintain the list of incident edges
and marked nodes. It works on Last-in-First-out (LIFO) basis.
o This technique picks up a node and marks it. An unmarked adjacent node to
previous node is then selected and marked, becomes the new start node,
possibly leaving the previous unexplored edges for the present.
o In short, DFS works as follows:
Explore the deeper part into the graph vertically whenever possible i.e.
expand or explore the deepest unexpanded node. If not found, then perform
backtracking.
[email protected] 76
 Graph Traversal

Let us consider the following graph:

Now, DFS works as follows:


[email protected] 77
 Graph Traversal

i.e. It follows the following sequence during DFS Search:


A, B, E, F, C, D
[email protected] 78
 Spanning Tree

o A spanning tree is a subset of graph “G”, which has all the vertices covered
with minimum possible number of edges.
o Hence, a spanning tree does not have cycles and it cannot be disconnected.
o Simply, all the possible trees constructed from the given graph are called
spanning trees of such a graph.
o Example: Find the spanning tree of the following given graph:

[email protected] 79
 Spanning Tree

o The possible spanning trees of the given graph are:

» Removing one edge from the spanning tree will make the graph
disconnected i.e. the spanning tree is minimally connected.
» Adding one edge to the spanning tree will create a circuit or a cycle or a
loop, i.e. the spanning tree is maximally cycle.
[email protected] 80
 Spanning Forest
o Spanning Forest consists of many kinds of different spanning trees.

 Minimal Spanning Tree / Minimum Spanning


Tree (MST)
o A MST in a connected weighted graph is a spanning tree that has the
smallest possible total sum of weights of its edges out of all the possible
spanning trees.

o Note:
If the given graph has “N” vertices, then the resulting MST will have “N-1”
edges.

[email protected] 81
 MST finding using Kruskal’s Algorithm
o Kruskal’s algorithm is a MST finding algorithm that takes a graph as
input and finds the subset of the edges of that graphs which:
forms a tree that includes every vertices i.e. connectedness.
has the minimum sum of weights among all the trees that can
be formed from the given graph.
does not contain any cycles.
o It is the procedure for finding a MST of a given graph, which is weighted and
that successively adds edges of least weight that are not already in the tree
such that no edges produce a simple circuit when it is added.

[email protected] 82
 Algorithmic Steps of Kruskal’s Algorithm

Step-1: Start
Step-2: Remove all the loops and parallel edges from the given graph, if
any.
Step-3: Arrange or sort all the edges according to their weights in
ascending
(or increasing) order of their edge weights.
Step-4: Choose the edge having minimum edge weight.
Step-5: Reject that edge which forms or creates a cycle.
Step-6: Keep adding edges until we reach all the vertices i.e.
connectedness.
Step-7: Stop

[email protected] 83
Example-1: Find the Minimal Spanning Tree (MST) from
the following graph by using Kruskal’s algorithm.

[email protected] 84
[email protected] 85
[email protected]

86
[email protected] 87
[email protected] 88
Example-2: Find the MST from the following graph
using Kruskal’s Algorithm.

[email protected] 89
Example-3: Find the MST from the following graph
using Kruskal’s Algorithm.

[email protected] 90
 MST using Prim's Algorithm
o It is a greedy algorithm that is used to find the minimum spanning tree from
a graph.
o Prim's algorithm finds the subset of edges that includes every vertex of the
graph such that the sum of the weights of the edges can be minimized.
Example-1 of prim's algorithm
• Now, let's see the working of prim's algorithm using an example. It will be
easier to understand the prim's algorithm using an example.

[email protected] 91
• Step 1 - First, if there are any loops and parallel edges, then we remove those
loops and parallel edges, and then we have to choose a source vertex from the
given graph. Let's choose vertex B as the source vertex.

• Step 2 - Now, we have to choose and add the shortest edge from vertex B. There
are two edges from vertex B that are B to C with weight 10 and edge B to D with
weight 4. Among the edges, the edge BD has the minimum weight. So, add it to
the MST.
Note: Remaining edge: BC(10)

[email protected] 92
• Step 3 - Now, current node is ‘D’ .In this case, the possible the edges DE(1) and
DC(2) are such edges + old edge in account BC(10). Now select minimum
weighted edge and add to MST So, selected edge is DE(1) and add it to the MST.
• Note: Remaining edge: BC(10) , DC(2)

[email protected] 93
• Step 4 - Now, current node ‘E’ ,possible travel is EC(6)+ old edges BC(10) , DC(2) select the edge DC(2) which is minimum ,
and add it to the MST.
Note: Remaining edge: EC(6), BC(10)

• Step 5 - Now, current node is ‘C’ , possible travel is CA(3)+Remaining edge: EC(6), BC(10) choose the edge CA(3). Here, we
cannot select the edge CE as it would create a cycle to the graph. So, choose the edge CA and add it to the MST.

• So, the graph produced in step 5 is the minimum spanning tree of the given graph. Since edges EC and BC form cycle, they
are rejected.
Hence, the cost of the MST is: 4 + 2 + 1 + 3 = 10 units.
[email protected] 94
Example-2: Find the MST from the following graph
using Prim’s Algorithm.

[email protected] 95
Example-3: Find the MST from the following graph
using Prim’s Algorithm.

[email protected] 96
Example-4: Find the MST from the following graph
using Prim’s Algorithm.

[email protected] 97
 The Shortest Path Algorithm

o Consider a weighted graph “G”. The length of a path in a


weighted graph is the sum of the weights of the edges of
this path, and the shortest path between the two vertices is
the minimum length of the path.
o Several algorithms are used to find the shortest path
between two vertices in a weighted graph. Dijkstra’s
algorithm is one of them.

[email protected] 98
 Finding the Shortest Path using Dijkstra’s
Algorithm

o Dijkstra’s algorithm was discovered by E.W. Dijkstra.


o It is an approach of getting single source shortest path i.e. it
asks to find the shortest path form the given source to all
other vertices in a connected weighted graph.
o In this algorithmic approach, only one source vertex and lots
of the destination vertices are taken.
o In this algorithm, it is assumed that there is no negative edge
weight in the graph.
o Dijkstra’s algorithm works by using greedy algorithm
approach.
[email protected] 99
 Pseudo Code / Algorithm for Dijkstra’s Algorithm

[email protected] 100
Example-1: Find the shortest path from source vertex to othe
vertices in the following graph using Dijkstra’s algorithm and
consider vertex “A” as the source vertex.

[email protected] 101
Solution:

[email protected] 102
Example-2: Find the shortest path between a to z in the
following given weighted graph using Dijkstra’s
algorithm.

[email protected] 103
Solution:

[email protected] 104
Example-3: Find the shortest path from source vertex to othe
vertices in the following graph using Dijkstra’s algorithm and
consider vertex “t” as the source vertex.

THANK YOU !!!

[email protected] 105

You might also like