0% found this document useful (0 votes)
52 views70 pages

Unit 3 (Graph) Dsa

The document defines various graph terminology and properties such as types of graphs (directed, undirected, weighted), graph components (vertices, edges, degrees), and special graph structures (complete graphs, cycles). It also discusses ways to represent graphs through adjacency lists and matrices and describes algorithms for traversing graphs using depth-first search and breadth-first search.

Uploaded by

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

Unit 3 (Graph) Dsa

The document defines various graph terminology and properties such as types of graphs (directed, undirected, weighted), graph components (vertices, edges, degrees), and special graph structures (complete graphs, cycles). It also discusses ways to represent graphs through adjacency lists and matrices and describes algorithms for traversing graphs using depth-first search and breadth-first search.

Uploaded by

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

Unit III

GRAPHS

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Prepared by Prof. Ravindra Aher, MET's IOE, Nashik
Introduction
• DEFINITION: A graph G = (V ,E) consists of V , a nonempty set of
vertices (or nodes) and E, a set of edges.
• Each edge has either one or two vertices associated with it, called its
endpoints. An edge is said to connect its endpoints.
• Example : Consider a network is made up of data centers and communication
links between computers.

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Introduction
• Finite Graph & Infinite Graph: A graph G = (V, E) in which both V(G) and E(G)
are finite sets is called a Finite Graph.
• Weighted Graph: A graph G is in which weight are assigned to every edge is
called weighted graph.
• Null Graph: A Graph G= (V, E) in which set of edges E is empty is called Null
Graph.
• Degree of Vertex: The degree of a vertex in an undirected graph is the number of
edges incident with it, except that a loop at a vertex contributes twice to the degree
of that vertex. The degree of the vertex v is denoted by deg(v).
• In Degree: In directed graph G, No. of edges ending at vertex v . degG – (v).
• Out Degree: In directed graph G, No. of edges beginning at vertex v . degG + (v).
• Order and Size of graph: For Finite Graph No. of vertices is called Order of
Graph and No. of edges is called size of Graph.
Prepared by Prof. Ravindra Aher, MET's IOE, Nashik
Introduction
• A directed graph (or digraph) (V ,E) consists of a nonempty set of vertices V
and a set of directed edges (or arcs) E.
• Each directed edge is associated with an ordered pair of vertices.
• The directed edge associated with the ordered pair (u, v) is said to start at u
and end at v.
• When a directed graph has no loops and has no multiple directed edges, it is
called a simple directed.

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Graph Terminology and Special Types of Graphs
• Basic Terminology:
• Definition 1 : Two vertices u and v in an undirected graph G are called
adjacent (or neighbours) in G if u and v are endpoints of an edge e of G.
• Such an edge e is called incident with the vertices u and v and e is said to
connect u and v.

• Definition 2 : The set of all neighbours of a vertex v of G = (V ,E), denoted


by N(v), is called the neighbourhood of v.
• If A is a subset of V , we denote by N(A) the set of all vertices in G that are
adjacent to at least one vertex in A. So, N(A) = v∈A N(v).

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Some Special Simple Graphs
• Complete Graphs: A complete graph on n vertices, denoted by Kn, is a
simple graph that contains exactly one edge between each pair of distinct
vertices.
• The graphs Kn, for n = 1, 2, 3, 4, 5, 6, are displayed.
• A simple graph for which there is at least one pair of distinct vertex not
connected by an edge is called non-complete.

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Some Special Simple Graphs
• Cycles: A cycle Cn, n ≥ 3, consists of n vertices v1, v2, . . . , vn and edges
{v1,v2}, {v2, v3}, . . . , {vn-1, vn}, and {vn, v1}. The cycles C3, C4, C5, and C6
are displayed.

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Some Special Simple Graphs
• Sub Graph:

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Connectivity
• Connected Graphs: An undirected graph is called connected if there is a path
between every pair of distinct vertices of the graph.
• Disconnected Graphs : An undirected graph that is not connected is called
Disconnected.

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Connectivity
• Connected Components: A connected component of a graph G is a
connected subgraph of G that is not a proper subgraph of another connected
subgraph of G.

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Representing Graphs
• One way to represent a graph without multiple edges is to list all the edges of
this graph.
• Adjacency lists: Represent a graph with no multiple edges.

Vertex Adjacent Vertices


a b, c, e
b a
c a, d, e
d c, e
e a, c, d

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Representing Graphs
• One way to represent a graph without multiple edges is to list all the edges of
this graph.
• Adjacency lists: Represent a graph with no multiple edges.

Initial Vertex Terminal Vertices


a b, c, d, e
b b, d
c a, c, e
d ---
e b, c, d

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Representing Graphs
• Adjacency lists, can be cumbersome if there are many edges in the graph.
• Matrices can be used to represent graph to simplify computations.
• Two types of matrices used to represent graphs.
• Adjacency Matrix: A = [aij], then
1 𝑖𝑓 𝑣𝑖 , 𝑣𝑗 𝑖𝑠 𝑎𝑛 𝑒𝑑𝑔𝑒 𝑜𝑓 𝐺
𝑎𝑖𝑗
0 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Representing Graphs
• Incidence Matrices:
• Loops are represented using a column with exactly one entry equal to 1,
corresponding to the vertex that is incident with this loop.

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Representing Graphs
• Another way to represent graphs is
• Incidence Matrices: let G = (V, E) be an undirected graph with v1, v2,…vn
vertices and e1, e2,…em edges then incidence matrix w.r.t. ordering of V and E
is the n x m matrix , M = [mij], where
1 when edge ej 𝑖𝑠 𝑖𝑛𝑐𝑖𝑑𝑒𝑛𝑡 𝑤𝑖𝑡ℎ vi
𝑚𝑖𝑗
0 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Tree traversal
• Depth First Search traversal (Using Stack):

• Rule 1 − Visit the adjacent unvisited vertex. Mark


it as visited. Display it. Push it in a stack.
• Rule 2 − If no adjacent vertex is found, pop up a
vertex from the stack. (It will pop up all the
vertices from the stack, which do not have
adjacent vertices.)
• Rule 3 − Repeat Rule 1 and Rule 2 until the stack is
empty

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Tree traversal
• Depth First Search traversal:

• Rule 1 − Visit the adjacent unvisited vertex.


Mark it as visited. Display it. Push it in a stack.
• Rule 2 − If no adjacent vertex is found, pop up a
vertex from the stack. (It will pop up all the
vertices from the stack, which do not have
adjacent vertices.)
• Rule 3 − Repeat Rule 1 and Rule 2 until the
stack is empty

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Tree traversal
• Depth First Search traversal:

• Rule 1 − Visit the adjacent unvisited vertex.


Mark it as visited. Display it. Push it in a stack.
• Rule 2 − If no adjacent vertex is found, pop up a
vertex from the stack. (It will pop up all the
vertices from the stack, which do not have
adjacent vertices.)
• Rule 3 − Repeat Rule 1 and Rule 2 until the
stack is empty

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Tree traversal
• Depth First Search traversal:

• Rule 1 − Visit the adjacent unvisited vertex.


Mark it as visited. Display it. Push it in a stack.
• Rule 2 − If no adjacent vertex is found, pop up a
vertex from the stack. (It will pop up all the
vertices from the stack, which do not have
adjacent vertices.)
• Rule 3 − Repeat Rule 1 and Rule 2 until the
stack is empty

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Tree traversal
• Depth First Search traversal:

• Rule 1 − Visit the adjacent unvisited vertex.


Mark it as visited. Display it. Push it in a stack.
• Rule 2 − If no adjacent vertex is found, pop up a
vertex from the stack. (It will pop up all the
vertices from the stack, which do not have
adjacent vertices.)
• Rule 3 − Repeat Rule 1 and Rule 2 until the
stack is empty

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Tree traversal
• Depth First Search traversal:

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Tree traversal
• Breadth First Search traversal (Using Queue):

• Rule 1 − Visit the adjacent unvisited vertex. Mark


it as visited. Display it. Insert it in a queue.

• Rule 2 − If no adjacent vertex is found, remove the


first vertex from the queue.

• Rule 3 − Repeat Rule 1 and Rule 2 until the queue


is empty

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Tree traversal
• Breadth First Search traversal (Using Queue):

• Rule 1 − Visit the adjacent unvisited vertex. Mark


it as visited. Display it. Insert it in a queue.

• Rule 2 − If no adjacent vertex is found, remove the


first vertex from the queue.

• Rule 3 − Repeat Rule 1 and Rule 2 until the queue


is empty

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Tree traversal
• Breadth First Search traversal (Using Queue):

• Rule 1 − Visit the adjacent unvisited vertex. Mark


it as visited. Display it. Insert it in a queue.

• Rule 2 − If no adjacent vertex is found, remove the


first vertex from the queue.

• Rule 3 − Repeat Rule 1 and Rule 2 until the queue


is empty

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Tree traversal
• Breadth First Search traversal (Using Queue):

• Rule 1 − Visit the adjacent unvisited vertex. Mark


it as visited. Display it. Insert it in a queue.

• Rule 2 − If no adjacent vertex is found, remove the


first vertex from the queue.

• Rule 3 − Repeat Rule 1 and Rule 2 until the queue


is empty

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Tree traversal
• Breadth First Search traversal (Using Queue):

• Rule 1 − Visit the adjacent unvisited vertex. Mark


it as visited. Display it. Insert it in a queue.

• Rule 2 − If no adjacent vertex is found, remove the


first vertex from the queue.

• Rule 3 − Repeat Rule 1 and Rule 2 until the queue


is empty

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Spanning Tree
• A spanning tree with smallest weight in a weighted graph is called a shortest
spanning tree or shortest-distance spanning tree or minimum spanning tree.
• Example : Suppose we want to connect n cities through network of roads.
• The problem is to find least expensive network that connects all n cities
together.
• This is the problem of finding minimum spanning tree in connected weighted
graph of n vertices.
• A minimum spanning tree in a connected weighted graph is a spanning tree
that has the smallest possible sum of weights of its edges.

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Greedy Algorithm
• Algorithm for optimization problems typically go through a sequence of
steps, with a set of choices at each step.
• For many optimization problems, greedy algorithm can be used. (not always)
• Greedy algorithm always makes the choice that looks best at the moment.
• It makes a locally optimal choice in the hope that this choice will lead to a
globally optimal solution.
• Example:
• Activity Selection Problem
• Dijkstra’s Shortest Path Problem
• Minimum Spanning Tree Problem

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Kruskal’s Algorithm
• A=∅
• Create a set for each vertex v ∈ V.
• Sort edges by increasing weight.
• For each e ∈ E
• add the edge e with lowest weight to A if it does not form circuit.
• Return A.

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Kruskal’s Algorithm
•A=∅
• Create a set F for each vertex v ∈ V.
• Sort edges by increasing weight.
• For each e ∈ E of G
• add the edge e with lowest weight to A if it does not form circuit.
• Return A. Edges Weight Edges Weight
(h, g) 1 (h, i) 7
• A=∅ (c, i) 2 (a, h) 8
(g, f) 2 (b, c) 8
• F = {a}, {b}, {c}, {d}, {e}, {f}, {g}, {h}, {i}
(a, b) 4 (d, e) 9
• E = {(h, g), (c, i), (g, f), (a, b), (c, f), (i, g), (c, d), (c, f) 4 (e, f) 10
(h, i), (a, h), (b, c), (d, e), (f, e), (b, h), (d, f)} (i, g) 6 (b, h) 11
(c, d) 7 (d, f) 14
Prepared by Prof. Ravindra Aher, MET's IOE, Nashik
Kruskal’s Algorithm
•A=∅
• Create a set F for each vertex v ∈ V.
• Sort edges by increasing weight.
• For each e ∈ E of G
• add the edge e with lowest weight to A if it does not form circuit.
• Return A. Edges Weight Edges Weight
(h, g) 1 (h, i) 7

• A = { ℎ, 𝑔 , (𝑐, 𝑖)} (c, i) 2 (a, h) 8


(g, f) 2 (b, c) 8
• F = {a}, {b}, {c, i}, {d}, {e}, {f}, {g, h} (a, b) 4 (d, e) 9
• E = {(h, g), (c, i), (g, f), (a, b), (c, f), (i, g), (c, d), (c, f) 4 (f, e) 10
(h, i), (a, h), (b, c), (d, e), (f, e), (b, h), (d, f)} (i, g) 6 (b, h) 11
(c, d) 7 (d, f) 14

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Kruskal’s Algorithm
•A=∅
• Create a set F for each vertex v ∈ V.
• Sort edges by increasing weight.
• For each e ∈ E of G
• add the edge e with lowest weight to A if it does not form circuit.
• Return A. Edges Weight Edges Weight
(h, g) 1 (h, i) 7

• A = { ℎ, 𝑔 , 𝑐, 𝑖 , (𝑔, 𝑓)} (c, i) 2 (a, h) 8


(g, f) 2 (b, c) 8
• F = {a}, {b}, {c, i}, {g, h, f}, {d}, {e} (a, b) 4 (d, e) 9
• E = {(h, g), (c, i), (g, f), (a, b), (c, f), (i, g), (c, d), (c, f) 4 (f, e) 10
(h, i), (a, h), (b, c), (d, e), (f, e), (b, h), (d, f)} (i, g) 6 (b, h) 11
(c, d) 7 (d, f) 14

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Kruskal’s Algorithm
•A=∅
• Create a set F for each vertex v ∈ V.
• Sort edges by increasing weight.
• For each e ∈ E of G
• add the edge e with lowest weight to A if it does not form circuit.
• Return A. Edges Weight Edges Weight
(h, g) 1 (h, i) 7
(c, i) 2 (a, h) 8
• A = { ℎ, 𝑔 , 𝑐, 𝑖 , 𝑔, 𝑓 , (𝑎, 𝑏)}
(g, f) 2 (b, c) 8
• F = {a, b}, {c, i, g, h, f}, {d}, {e} (a, b) 4 (d, e) 9
• E = {(h, g), (c, i), (g, f), (a, b), (c, f), (i, g), (c, d), (c, f) 4 (f, e) 10
(h, i), (a, h), (b, c), (d, e), (f, e), (b, h), (d, f)} (i, g) 6 (b, h) 11
(c, d) 7 (d, f) 14

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Kruskal’s Algorithm
•A=∅
• Create a set F for each vertex v ∈ V.
• Sort edges by increasing weight.
• For each e ∈ E of G
• add the edge e with lowest weight to A if it does not form circuit.
• Return A. Edges Weight Edges Weight
(h, g) 1 (h, i) 7
• A = { ℎ, 𝑔 , 𝑐, 𝑖 , 𝑔, 𝑓 , 𝑎, 𝑏 , (𝑐, 𝑓)} (c, i) 2 (a, h) 8
(g, f) 2 (b, c) 8
• F = {a, b}, {c, i, g, h, f}, {d}, {e} (a, b) 4 (d, e) 9
• E = {(h, g), (c, i), (g, f), (a, b), (c, f), (i, g), (c, d), (c, f) 4 (f, e) 10
(h, i), (a, h), (b, c), (d, e), (f, e), (b, h), (d, f)} (i, g) 6 (b, h) 11
(c, d) 7 (d, f) 14

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Kruskal’s Algorithm
•A=∅
• Create a set F for each vertex v ∈ V.
• Sort edges by increasing weight.
• For each e ∈ E of G
• add the edge e with lowest weight to A if it does not form circuit.
• Return A. Edges Weight Edges Weight
(h, g) 1 (h, i) 7
• A = { ℎ, 𝑔 , 𝑐, 𝑖 , 𝑔, 𝑓 , 𝑎, 𝑏 , 𝑐, 𝑓 , (𝑐, 𝑑)} (c, i) 2 (a, h) 8
(g, f) 2 (b, c) 8
• F = {a, b}, {c, i, g, h, f, d}, {e} (a, b) 4 (d, e) 9
• E = {(h, g), (c, i), (g, f), (a, b), (c, f), (i, g), (c, d), (c, f) 4 (f, e) 10
(h, i), (a, h), (b, c), (d, e), (f, e), (b, h), (d, f)} (i, g) 6 (b, h) 11
(c, d) 7 (d, f) 14

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Kruskal’s Algorithm
•A=∅
• Create a set F for each vertex v ∈ V.
• Sort edges by increasing weight.
• For each e ∈ E of G
• add the edge e with lowest weight to A if it does not form circuit.
• Return A. Edges Weight Edges Weight
(h, g) 1 (h, i) 7
• A = { ℎ, 𝑔 , 𝑐, 𝑖 , 𝑔, 𝑓 , 𝑎, 𝑏 , 𝑐, 𝑓 , 𝑐, 𝑑 , (𝑎, ℎ)} (c, i) 2 (a, h) 8
(g, f) 2 (b, c) 8
• F = {a, b, c, i, g, h, f, d}, {e} (a, b) 4 (d, e) 9
• E = {(h, g), (c, i), (g, f), (a, b), (c, f), (i, g), (c, d), (c, f) 4 (f, e) 10
(h, i), (a, h), (b, c), (d, e), (f, e), (b, h), (d, f)} (i, g) 6 (b, h) 11
(c, d) 7 (d, f) 14

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Kruskal’s Algorithm
•A=∅
• Create a set F for each vertex v ∈ V.
• Sort edges by increasing weight.
• For each e ∈ E of G
• add the edge e with lowest weight to A if it does not form circuit.
• Return A. Edges Weight Edges Weight
(h, g) 1 (h, i) 7
• A = { ℎ, 𝑔 , 𝑐, 𝑖 , 𝑔, 𝑓 , 𝑎, 𝑏 , 𝑐, 𝑓 , 𝑐, 𝑑 , 𝑎, ℎ , (𝑑, 𝑒)} (c, i) 2 (a, h) 8
(g, f) 2 (b, c) 8
• F = {a, b, c, i, g, h, f, d, e} (a, b) 4 (d, e) 9
• E = {(h, g), (c, i), (g, f), (a, b), (c, f), (i, g), (c, d), (c, f) 4 (f, e) 10
(h, i), (a, h), (b, c), (d, e), (f, e), (b, h), (d, f)} (i, g) 6 (b, h) 11
(c, d) 7 (d, f) 14

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Kruskal’s Algorithm
•A=∅
• Create a set F for each vertex v ∈ V.
• Sort edges by increasing weight.
• For each e ∈ E of G
• add the edge e with lowest weight to A if it does not form circuit.
• Return A. Edges Weight Edges Weight
(h, g) 1 (h, i) 7
• A = { ℎ, 𝑔 , 𝑐, 𝑖 , 𝑔, 𝑓 , 𝑎, 𝑏 , 𝑐, 𝑓 , 𝑐, 𝑑 , 𝑎, ℎ , (𝑑, 𝑒)} (c, i) 2 (a, h) 8
• F = {a, b, c, i, g, h, f, d, e} (g, f) 2 (b, c) 8

• E = {(h, g), (c, i), (g, f), (a, b), (c, f), (i, g), (c, d), (a, b) 4 (d, e) 9
(c, f) 4 (f, e) 10
(h, i), (a, h), (b, c), (d, e), (f, e), (b, h), (d, f)} (i, g) 6 (b, h) 11
Weight of MST : 1+2+2+4+4+7+8+9=37 (c, d) 7 (d, f) 14

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Kruskal’s Algorithm V1 5 V2
4
•A=∅ V3 6 V4
2

• Create a set F for each vertex v ∈ V. 2


2
2

• Sort edges by increasing weight. 3


1

• For each e ∈ E of G V5 4 V6

• add the edge e with lowest weight to A if it does not form circuit.
• Return A. Edges Weight Edges Weight

• A={ }
• F = {V1, V2, V3, V4, V5, V6}
•E={ }
Weight of MST :

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Prim’s Algorithm
• Prim’s algorithm operates much like Dijkstra’s algorithm for finding shortest
paths in a graph.
• Prim’s algorithm has the property that the edges in the set A always form a
single tree.
• The tree starts from an arbitrary root vertex r and grows until the tree spans
all the vertices in V.
• Each step adds to the tree A a light edge that connects A to an isolated
vertex—one on which no edge of A is incident.
• When the algorithm terminates, the edges in A form a minimum spanning
tree.

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Prim’s Algorithm
•A=∅
• Find lightest edge such that one endpoint.
is in A and other in V – A. Add this edge
to E and other vertex to A.
• If V – A = ∅. Then stop and return A.

• A ={𝑎}
• V – A = {b, c, d, e, f, g, h, i}
• E = {}.

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Prim’s Algorithm
•A=∅
• Find lightest edge such that one endpoint.
is in A and other in V – A. Add this edge
to E and other vertex to A.
• If V – A = ∅. Then stop and return A.

• A ={𝑎, 𝑏}
• V – A = { c, d, e, f, g, h, i}
• E = {(a, b)}.

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Prim’s Algorithm
•A=∅
• Find lightest edge such that one endpoint.
is in A and other in V – A. Add this edge
to E and other vertex to A.
• If V – A = ∅. Then stop and return A.

• A ={𝑎, 𝑏, 𝑐}
• V – A = {d, e, f, g, h, i}
• E = {(a, b), (b, c)}.

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Prim’s Algorithm
•A=∅
• Find lightest edge such that one endpoint.
is in A and other in V – A. Add this edge
to E and other vertex to A.
• If V – A = ∅. Then stop and return A.

• A ={𝑎, 𝑏, 𝑐, 𝑖}
• V – A = {d, e, f, g, h}
• E = {(a, b), (b, c), (c, i)}.

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Prim’s Algorithm
•A=∅
• Find lightest edge such that one endpoint.
is in A and other in V – A. Add this edge
to E and other vertex to A.
• If V – A = ∅. Then stop and return A.

• A ={𝑎, 𝑏, 𝑐, 𝑖, 𝑓}
• V – A = {d, e, g, h}
• E = {(a, b), (b, c), (c, i), (c, f)}.

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Prim’s Algorithm
•A=∅
• Find lightest edge such that one endpoint.
is in A and other in V – A. Add this edge
to E and other vertex to A.
• If V – A = ∅. Then stop and return A.

• A ={𝑎, 𝑏, 𝑐, 𝑖, 𝑓, 𝑔}
• V – A = {d, e, h}
• E = {(a, b), (b, c), (c, i), (c, f), (f, g)}.

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Prim’s Algorithm
•A=∅
• Find lightest edge such that one endpoint.
is in A and other in V – A. Add this edge
to E and other vertex to A.
• If V – A = ∅. Then stop and return A.

• A ={𝑎, 𝑏, 𝑐, 𝑖, 𝑓, 𝑔, ℎ}
• V – A = {d, e}
• E = {(a, b), (b, c), (c, i), (c, f), (f, g) , (g, h)}.

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Prim’s Algorithm
•A=∅
• Find lightest edge such that one endpoint.
is in A and other in V – A. Add this edge
to E and other vertex to A.
• If V – A = ∅. Then stop and return A.

• A ={𝑎, 𝑏, 𝑐, 𝑖, 𝑓, 𝑔, ℎ}
• V – A = {e}
• E = {(a, b), (b, c), (c, i), (c, f), (f, g), (g, h), (c, d)}.

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Prim’s Algorithm
•A=∅
• Find lightest edge such that one endpoint.
is in A and other in V – A. Add this edge
to E and other vertex to A.
• If V – A = ∅. Then stop and return A.

• A ={𝑎, 𝑏, 𝑐, 𝑖, 𝑓, 𝑔, ℎ, 𝑒}
• V – A = {} = ∅ stop.
• E = {(a, b), (b, c), (c, i), (c, f), (f, g), (g, h), (c, d), (e, d)}.
• MST = 4+8+2+4+2+1+7+9=37

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Prim’s Algorithm
•A=∅
• Find lightest edge such that one endpoint.
is in A and other in V – A. Add this edge
to E and other vertex to A.
• If V – A = ∅. Then stop and return A.

• A ={} = ∅
• V – A = {a, b,c,d,e,f,g,h,i,j,k,l} .
• E = {}.
• MST = ?

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Prim’s Algorithm
•A=∅
• Find lightest edge such that one endpoint.
is in A and other in V – A. Add this edge
to E and other vertex to A.
• If V – A = ∅. Then stop and return A.

• A ={a, b, c, d, e, f, g, h, i, j, k, l }
• V – A = {} = ∅.
• E = {(a, b), (b, f), (a, e), (f, j), (i, j), (f, g)
, (c, g), (c, d), (g, h), (h, l), (l, k)}.
• MST = 24

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Prim’s Algorithm
•A=∅
• Find lightest edge such that one endpoint.
is in A and other in V – A. Add this edge
to E and other vertex to A.
• If V – A = ∅. Then stop and return A.

• A ={} = ∅
• V – A = {𝑎, 𝑏, 𝑐, 𝑑, 𝑒, 𝑓, 𝑔, ℎ, 𝑖} .
• E = {}.
• MST = ?

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Prim’s Algorithm
•A=∅
• Find lightest edge such that one endpoint.
is in A and other in V – A. Add this edge
to E and other vertex to A.
• If V – A = ∅. Then stop and return A.

• A ={𝑎, 𝑑, 𝑏, 𝑐, 𝑓, 𝑒, ℎ, 𝑖, 𝑔}
• V – A = {} = ∅.
• E = {(a, d), (d, b), (b, c), (c, f), (e, f), (e, h),
(h, i), (h, g),}.
• MST = 2+3+4+3+1+3+2+4

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Connectivity
• PATH: is a sequence of edges that begins at a vertex of a graph and travels
from vertex to vertex along edges of the graph.
• Many problems can be modelled with paths formed by traveling along the
edges of graphs.
• The problem of determining whether a message can be sent between two
computers using intermediate links can be studied with a graph model.
• Problems of efficiently planning routes for mail delivery, diagnostics in
computer networks, and so on can be solved using models that involve paths
in graphs.

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Connectivity
• Simple path : A path in graph G is said to be a simple path if edges do not
repeat in the path. Vertices may be repeated.
• v1 – e1 – v2 – e2 – v3 – e4 – v5 – e5 – v2 .
• Elementary path : A path in graph G is said to be a elementary path if
vertices do not repeat in the path.
• v1 – e1 – v2 – e2 – v3 – e4 – v5.
• Circuit : A closed path is called circuit.

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Shortest Path Algorithm
• Dijkstra’s Algorithm: To find shortest path from the vertex a to vertex z of
graph G.
• L(x) is label of vertex = length of shortest path from vertex x
• Wij = weight of an edge eij = (vi, vj)
• Consider steps:
• Step 1 : P = set of those vertices having permanent label, T = set of all vertices of G.
set L(a)=0, L(x) = ∞; ∀x ∈ 𝑇 and x ≠ 𝑎, P = ∅ and T = V (all vertices).
• Step 2 : Select v from T having smallest label. Set P = P ∪ 𝑣 𝑎𝑛𝑑 𝑇 = 𝑇 − {𝑣}
• If v = z then L(z) is length of shortest path from a to z and stop.
• Step 3 : if v ≠ z then revise label of vertices in T.
L(x) = min { old L(x), L(v) + w(v, x) } , if no edge between v & x then weight is ∞.
• Step 4 : repeat step 2 and 4 until z get permanent label.

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Dijkstra’s Algorithm b 3 e
• Find shortest path between a to z. 2 1
• P = ∅, T = {a, b, c, d, e, f, z} a 2 5 z
1
• L{a} = 0, L{x}=∞, ∀𝑥 ∈ 𝑇, 𝑥 ≠ 𝑎 c
• v = a as L{a} = 0. 7
4 2 3
P = P ∪ {𝑎} = {a}, T = {b, c, d, e, f, z} d f
4
L{b} = min{ old L(b), L(a) + w(a, b)} = min{∞, 0 + 2 } = L(b) = 2.
L{c} = min{ old L(c), L(a) + w(a, c)} = min{∞, 0 + 1 } = L(c) = 1.
L{d} = min{ old L(d), L(a) + w(a, d)} = min{∞, 0 + 4 } = L(d) = 4.
L{e} = L{f} = L{z} = ∞. ∴ L{c} = 1 min. label.
• v = c as L{c} = 1.
P = P ∪ {𝑐} = {a, c}, T = {b, d, e, f, z}
L{b} = min{ old L(b), L(c) + w(c, b)} = min{2, 1 + 2 } = L(b) = 2.
L{d} = min{ old L(d), L(c) + w(c, d)} = min{4, 1 + 2 } = L(d) = 3.
Prepared by Prof. Ravindra Aher, MET's IOE, Nashik
Dijkstra’s Algorithm b 3 e
• Find shortest path between a to z. 2 1
a 2 5 z
• v = c as L{c} = 1. 1
P = P ∪ {𝑐} = {a, c}, T = {b, d, e, f, z} c
7
L{b} = 2. L{d} = 3. 4 2 3
d f
4
L{e} = min{ old L(e), L(c) + w(c, e)} = min{∞, 1 + 5 } = L(e) = 6.
L{f} = min{ old L(f), L(c) + w(c, f)} = min{∞, 1 + 7 } = L(f) = 8.
L{z} = min{ old L(z), L(c) + w(c, z)} = min{∞, 1 + ∞} = L(z) = ∞.
L{b} = 2 is min. label.
• v = b as L{b} = 2. , P = P ∪ {𝑏} = {a, c, b}, T = {d, e, f, z}
L{d} = min{ old L(d), L(b) + w(b, d)} = min{3, 2 + ∞ } = L(d) = 3.
L{e} = min{ old L(e), L(b) + w(b, e)} = min{6, 2 + 3 } = L(e) = 5.
L{f} = min{ old L(f), L(b) + w(b, f)} = min{8, 2 + ∞ } = L(f) = 8.
L{z} = min{ old L(z), L(b) + w(b, z)} = min{∞, 2 + ∞} = L(z) = ∞.
L{d} = 3 is min. label Prepared by Prof. Ravindra Aher, MET's IOE, Nashik
Dijkstra’s Algorithm b 3 e
• Find shortest path between a to z. 2
2
1
a 5 z
• v = d as L{d} = 3. 1
P = P ∪ {𝑑} = {a, c, b, d}, T = {e, f, z} c
2 7
4 3
d f
4

L{e} = min{ old L(e), L(d) + w(d, e)} = min{5, 3 + ∞ } = L(e) = 5.


L{f} = min{ old L(f), L(d) + w(d, f)} = min{8, 3 + 4 } = L(f) = 7.
L{z} = min{ old L(z), L(d) + w(d, z)} = min{∞, 3 + ∞} = L(z) = ∞.
L{e} = 5 is min. label.
• v = e as L{e} = 5. , P = P ∪ {𝑒} = {a, c, b, d, e}, T = {f, z}
L{f} = min{ old L(f), L(e) + w(e, f)} = min{7, 5 + ∞ } = L(f) = 7.
L{z} = min{ old L(z), L(e) + w(e, z)} = min{∞, 5 + 1} = L(z) = 6.
L{z} = 6 is min. label
Prepared by Prof. Ravindra Aher, MET's IOE, Nashik
Dijkstra’s Algorithm
• Find shortest path between a to z.

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Dijkstra’s Algorithm
2
• Find shortest path between a to z.
b 2 e

22 2 4
a 20 6
16 10 z
c
10 3 7
8 9
d f
6

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


Dijkstra’s Algorithm
2
• Find shortest path between a to z.
b 2 e

22 2 4
a 20 6
16 10 z
c
10 3 7
8 9
d f
6

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


All Pair Shortest Path – Floyd Warshall Algorithm
• Dijkstra’s Algorithm solves the Single-Source Shortest Path problem.
• Floyd Warshall Algorithm solves All pair shortest path problem.
• Finding shortest paths between all pairs of vertices of directed graph G.
• Works on positive or negative weights and graph contains no negative-weight
cycles

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


All Pair Shortest Path – Floyd Warshall Algorithm
• Example:

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


All Pair Shortest Path – Floyd Warshall Algorithm

1 2 3 4 5 1 2 3 4 5
1 0 3 8 ∞ -4 1 0 3 8 4 -4

2 ∞ 0 ∞ 1 7 2 ∞ 0 ∞ 1 7

𝑑1 = 3 ∞ 4 0 ∞ ∞ 𝑑2 = 3 ∞ 4 0 5 11

4 2 5 -5 0 -2 4 2 5 -5 0 -2

5 ∞ ∞ ∞ 6 0 5 ∞ ∞ ∞ 6 0

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


All Pair Shortest Path – Floyd Warshall Algorithm

1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
1 0 3 8 4 -4 1 0 3 8 4 -4 1 0 3 -1 4 -4

2 ∞ 0 ∞ 1 7 2 ∞ 0 ∞ 1 7 2 3 0 -4 1 -1
𝑑2 = 3 ∞ 4 0 5 11
𝑑3 = 3 ∞ 4 0 5 11
𝑑4 = 3 7 4 0 5 3

4 2 5 -5 0 -2 4 2 -1 -5 0 -2 4 2 -1 -5 0 -2

5 ∞ ∞ ∞ 6 0 5 ∞ ∞ ∞ 6 0 5 8 5 1 6 0

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


All Pair Shortest Path – Floyd Warshall Algorithm

1 2 3 4 5 1 2 3 4 5
1 0 3 -1 4 -4 1 0 1 -3 2 -4

2 3 0 -4 1 -1 2 3 0 -4 1 -1
𝑑4 = 3 7 4 0 5 3
𝑑5 = 3 7 4 0 5 3

4 2 -1 -5 0 -2 4 2 -1 -5 0 -2

5 8 5 1 6 0 5 8 5 1 6 0

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


All Pair Shortest Path – Floyd Warshall Algorithm
Example :

k = 0 , direct path between 2 vertices


k = 1 , path via vertex ‘0’ , k = 2 , path via vertex ‘1’
k = 3 , path via vertex ‘2’ , k = 4 , path via vertex ‘3’

0 1 2 3 0 1 2 3 0 1 2 3
0 0 0

1 1 1
𝑑0 = 2
𝑑1 = 2
𝑑2 = 2

3 3 3

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik


All Pair Shortest Path – Floyd Warshall Algorithm
Example :

k = 0 , direct path between 2 vertices


k = 1 , path via vertex ‘0’ , k = 2 , path via vertex ‘1’
k = 3 , path via vertex ‘2’ , k = 4 , path via vertex ‘3’

0 1 2 3 0 1 2 3 0 1 2 3
0 0 0

1 1 1
𝑑2 = 2
𝑑3 = 2
𝑑4 = 2

3 3 3

Prepared by Prof. Ravindra Aher, MET's IOE, Nashik

You might also like