0% found this document useful (0 votes)
156 views31 pages

Unit 4 (Graph)

A graph is defined as an ordered pair consisting of vertices (nodes) and edges, which can represent complex relationships and are utilized in various applications like social networks and resource allocation. The document outlines various graph terminologies, types of graphs, their representations, applications, and traversal algorithms such as Breadth-First Search (BFS) and Depth-First Search (DFS). Additionally, it explains the algorithms' steps and complexities, providing examples for better understanding.

Uploaded by

akankshag7810
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)
156 views31 pages

Unit 4 (Graph)

A graph is defined as an ordered pair consisting of vertices (nodes) and edges, which can represent complex relationships and are utilized in various applications like social networks and resource allocation. The document outlines various graph terminologies, types of graphs, their representations, applications, and traversal algorithms such as Breadth-First Search (BFS) and Depth-First Search (DFS). Additionally, it explains the algorithms' steps and complexities, providing examples for better understanding.

Uploaded by

akankshag7810
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/ 31

UNIT -4(GRAPH)

What is a Graph?
A graph is an ordered pair G = (V, E) comprising a set V of vertices or nodes and a collection of
pairs of vertices from V, known as edges of a graph. For example, for the graph below.
V = { 1, 2, 3, 4, 5, 6 }
E = { (1, 4), (1, 6), (2, 6), (4, 5), (5, 6) }

Graphs are non-linear data structures that are made up of a set of nodes (or vertices),
connected by edges (or arcs). Nodes are entities where the data is stored and their
relationships are expressed using edges. Edges may be directed or undirected. Graphs
demonstrate complicated relationships with ease and are used to solve many real-life
problems.

For example, Facebook uses a graph data structure that consists of a group of entities and
their relationships. On Facebook, every user, photo, post, page, place, etc. that has data is
represented with a node. Every edge from one node to another represents their relationships,
friendships, ownerships, tags, etc. Whenever a user posts a photo, comments on a post, etc., a
new edge is created for that relationship. Both nodes and edges have meta-data associated
with them.

Graph Terminologies in Data Structure


The following are some of the commonly used terms in graph data structure:

Term Description
Every individual data element is called a vertex or a node. In the above
Vertex
image, A, B, C, D & E are the vertices.

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


It is a connecting link between two nodes or vertices. Each edge has two ends
Edge (Arc)
and is represented as (startingVertex, endingVertex).
Undirected
It is a bidirectional edge.
Edge
Directed Edge It is a unidirectional edge.
Weighted
An edge with value (cost) on it.
Edge
Degree The total number of edges connected to a vertex in a graph.
Indegree The total number of incoming edges connected to a vertex.
Outdegree The total number of outgoing edges connected to a vertex.
Self-loop An edge is called a self-loop if its two endpoints coincide with each other.

Types of Graphs in Data Structures


There are different types of graphs in data structures, each of which is detailed below.

1. Finite Graph

The graph G=(V, E) is called a finite graph if the number of vertices and edges in the graph is
limited in number

2. Infinite Graph

The graph G=(V, E) is called a finite graph if the number of vertices and edges in the graph is
interminable.

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


3. Trivial Graph

A graph G= (V, E) is trivial if it contains only a single vertex and no edges.

4. Simple Graph

If each pair of nodes or vertices in a graph G=(V, E) has only one edge, it is a simple graph.
As a result, there is just one edge linking two vertices, depicting one-to-one interactions
between two elements.

5. Multi Graph

If there are numerous edges between a pair of vertices in a graph G= (V, E), the graph is
referred to as a multigraph. There are no self-loops in a Multigraph.

6. Null Graph

It's a reworked version of a trivial graph. If several vertices but no edges connect them, a
graph G= (V, E) is a null graph.

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


7. Complete Graph

If a graph G= (V, E) is also a simple graph, it is complete. Using the edges, with n number of
vertices must be connected. It's also known as a full graph because each vertex's degree must
be n-1.

8. Pseudo Graph

If a graph G= (V, E) contains a self-loop besides other edges, it is a pseudograph.

9. Regular Graph

If a graph G= (V, E) is a simple graph with the same degree at each vertex, it is a regular
graph. As a result, every whole graph is a regular graph.

10. Weighted Graph

A graph G= (V, E) is called a labeled or weighted graph because each edge has a value or
weight representing the cost of traversing that edge.

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


11. Directed Graph

A directed graph also referred to as a digraph, is a set of nodes connected by edges, each with
a direction.

12. Undirected Graph

An undirected graph comprises a set of nodes and links connecting them. The order of the
two connected vertices is irrelevant and has no direction. You can form an undirected graph
with a finite number of vertices and edges.

13. Connected Graph

If there is a path between one vertex of a graph data structure and any other vertex, the graph
is connected.

14. Disconnected Graph

When there is no edge linking the vertices, you refer to the null graph as a disconnected
graph.

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


15. Cyclic Graph

If a graph contains at least one graph cycle, it is considered to be cyclic.

16. Acyclic Graph

When there are no cycles in a graph, it is called an acyclic graph.

17. Directed Acyclic Graph

It's also known as a directed acyclic graph (DAG), and it's a graph with directed edges but no
cycle. It represents the edges using an ordered pair of vertices since it directs the vertices and
stores some data.

18. Subgraph

The vertices and edges of a graph that are subsets of another graph are known as a subgraph.

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


Representation of Graphs in Data Structures
Graphs in data structures are used to represent the relationships between objects. Every graph
consists of a set of points known as vertices or nodes connected by lines known as edges. The
vertices in a network represent entities.

The most frequent graph representations are the two that follow:

 Adjacency matrix
 Adjacency list

You’ll look at these two representations of graphs in data structures in more detail:

Adjacency Matrix

 A sequential representation is an adjacency matrix.


 It's used to show which nodes are next to one another. I.e., is there any connection between
nodes in a graph?
 You create an MXM matrix G for this representation. If an edge exists between vertex a and
vertex b, the corresponding element of G, gi,j = 1, otherwise gi,j = 0.
 If there is a weighted graph, you can record the edge's weight instead of 1s and 0s.

Undirected Graph Representation

Directed Graph Representation

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


Weighted Undirected Graph Representation

Weight or cost is indicated at the graph's edge, a weighted graph representing these values in
the matrix.

Adjacency List

 A linked representation is an adjacency list.


 You keep a list of neighbors for each vertex in the graph in this representation. It means that
each vertex in the graph has a list of its neighboring vertices.
 You have an arra of vertices indexed by the vertex number, and the corresponding array
member for each vertex x points to a singly linked list of x's neighbors.

Weighted Undirected Graph Representation Using Linked-List

Weighted Undirected Graph Representation Using an Array

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


Application of Graphs in Data Structures
Following are some applications of graphs in data structures:

 Graphs are used in computer science to depict the flow of computation.


 Users on Facebook are referred to as vertices, and if they are friends, there is an edge
connecting them. The Friend Suggestion system on Facebook is based on graph
theory.
 You come across the Resource Allocation Graph in the Operating System, where each
process and resource are regarded vertically. Edges are drawn from resources to
assigned functions or from the requesting process to the desired resource. A stalemate
will develop if this results in the establishment of a cycle.
 Web pages are referred to as vertices on the World Wide Web. Suppose there is a link
from page A to page B that can represent an edge. This application is an illustration of
a directed graph.
 Graph transformation systems manipulate graphs in memory using rules. Graph
databases store and query graph-structured data in a transaction-safe, permanent
manner.

Graph Traversal Algorithm


The process of visiting or updating each vertex in a graph is known as graph traversal. The
sequence in which they visit the vertices is used to classify such traversals. Graph traversal is
a subset of tree traversal.

There are two techniques to implement a graph traversal algorithm:

 Breadth-first search
 Depth-first search

BFS is a search technique for finding a node in a graph data structure that meets a set of
criteria.

 It begins at the root of the graph and investigates all nodes at the current depth level
before moving on to nodes at the next depth level.
 To maintain track of the child nodes that have been encountered but not yet inspected,
more memory, generally you require a queue.

Applications of BFS algorithm

The applications of breadth-first-algorithm are given as follows -

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


 BFS can be used to find the neighboring locations from a given source location.
 In a peer-to-peer network, BFS algorithm can be used as a traversal method to find all the
neighboring nodes. Most torrent clients, such as BitTorrent, uTorrent, etc. employ this process
to find "seeds" and "peers" in the network.
 BFS can be used in web crawlers to create web page indexes. It is one of the main algorithms
that can be used to index web pages. It starts traversing from the source page and follows the
links associated with the page. Here, every web page is considered as a node in the graph.
 BFS is used to determine the shortest path and minimum spanning tree.
 BFS is also used in Cheney's technique to duplicate the garbage collection.
 It can be used in ford-Fulkerson method to compute the maximum flow in a flow network.

Algorithm

The steps involved in the BFS algorithm to explore a graph are given as follows -

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 of BFS algorithm

Now, let's understand the working of BFS algorithm by using an example. In the example
given below, there is a directed graph having 7 vertices.

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


In the above graph, minimum path 'P' can be found by using the BFS that will start from
Node A and end at Node E. The algorithm uses two queues, namely QUEUE1 and QUEUE2.
QUEUE1 holds all the nodes that are to be processed, while QUEUE2 holds all the nodes that
are processed and deleted from QUEUE1.

Now, let's start examining the graph starting from Node A.

Step 1 - First, add A to queue1 and NULL to queue2.

1. QUEUE1 = {A}
2. QUEUE2 = {NULL}

Step 2 - Now, delete node A from queue1 and add it into queue2. Insert all neighbors of node
A to queue1.

1. QUEUE1 = {B, D}
2. QUEUE2 = {A}

Step 3 - Now, delete node B from queue1 and add it into queue2. Insert all neighbors of node
B to queue1.

1. QUEUE1 = {D, C, F}
2. QUEUE2 = {A, B}

Step 4 - Now, delete node D from queue1 and add it into queue2. Insert all neighbors of node
D to queue1. The only neighbor of Node D is F since it is already inserted, so it will not be
inserted again.

1. QUEUE1 = {C, F}
2. QUEUE2 = {A, B, D}

Step 5 - Delete node C from queue1 and add it into queue2. Insert all neighbors of node C to
queue1.

1. QUEUE1 = {F, E, G}
2. QUEUE2 = {A, B, D, C}

Step 5 - Delete node F from queue1 and add it into queue2. Insert all neighbors of node F to
queue1. Since all the neighbors of node F are already present, we will not insert them again.

AD

1. QUEUE1 = {E, G}
2. QUEUE2 = {A, B, D, C, F}

Step 6 - Delete node E from queue1. Since all of its neighbors have already been added, so
we will not insert them again. Now, all the nodes are visited, and the target node E is
encountered into queue2.

1. QUEUE1 = {G}
2. QUEUE2 = {A, B, D, C, F, E}

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


Complexity of BFS algorithm

Time complexity of BFS depends upon the data structure used to represent the graph. The
time complexity of BFS algorithm is O(V+E), since in the worst case, BFS algorithm
explores every node and edge. In a graph, the number of vertices is O(V), whereas the
number of edges is O(E).

The space complexity of BFS can be expressed as O(V), where V is the number of vertices.

Depth-First Search or DFS

DFS is a search technique for finding a node in a graph data structure that meets a set of
criteria.

 The depth-first search (DFS) algorithm traverses or explores data structures such as trees and
graphs. The DFS algorithm begins at the root node and examines each branch as far as
feasible before backtracking.
 To maintain track of the child nodes that have been encountered but not yet inspected, more
memory, generally a stack, is required.

Applications of DFS algorithm

The applications of using the DFS algorithm are given as follows -

 DFS algorithm can be used to implement the topological sorting.


 It can be used to find the paths between two vertices.
 It can also be used to detect cycles in the graph.
 DFS algorithm is also used for one solution puzzles.
 DFS is used to determine if a graph is bipartite or not.

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)

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


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 neighbors 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 of DFS algorithm

Now, let's understand the working of the DFS algorithm by using an example. In the example
given below, there is a directed graph having 7 vertices.

Now, let's start examining the graph starting from Node H.

Step 1 - First, push H onto the stack.

1. STACK: H

Step 2 - POP the top element from the stack, i.e., H, and print it. Now, PUSH all the
neighbors of H onto the stack that are in ready state.

1. Print: H]STACK: A

Step 3 - POP the top element from the stack, i.e., A, and print it. Now, PUSH all the
neighbors of A onto the stack that are in ready state.

1. Print: A
2. STACK: B, D

Step 4 - POP the top element from the stack, i.e., D, and print it. Now, PUSH all the
neighbors of D onto the stack that are in ready state.

1. Print: D

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


2. STACK: B, F

Step 5 - POP the top element from the stack, i.e., F, and print it. Now, PUSH all the
neighbors of F onto the stack that are in ready state.

1. Print: F
2. STACK: B

Step 6 - POP the top element from the stack, i.e., B, and print it. Now, PUSH all the
neighbors of B onto the stack that are in ready state.

1. Print: B
2. STACK: C

Step 7 - POP the top element from the stack, i.e., C, and print it. Now, PUSH all the
neighbors of C onto the stack that are in ready state.

1. Print: C
2. STACK: E, G

Step 8 - POP the top element from the stack, i.e., G and PUSH all the neighbors of G onto the
stack that are in ready state.

1. Print: G
2. STACK: E

Step 9 - POP the top element from the stack, i.e., E and PUSH all the neighbors of E onto the
stack that are in ready state.

AD

1. Print: E
2. STACK:

Now, all the graph nodes have been traversed, and the stack is empty.

Complexity of Depth-first search algorithm

The time complexity of the DFS algorithm is O(V+E), where V is the number of vertices and
E is the number of edges in the graph.

The space complexity of the DFS algorithm is O(V).

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


What is a spanning tree?

A spanning tree can be defined as the subgraph of an undirected connected graph. It includes
all the vertices along with the least possible number of edges. If any vertex is missed, it is not
a spanning tree. A spanning tree is a subset of the graph that does not have cycles, and it also
cannot be disconnected.

A spanning tree consists of (n-1) edges, where 'n' is the number of vertices (or nodes). Edges
of the spanning tree may or may not have weights assigned to them. All the possible spanning
trees created from the given graph G would have the same number of vertices, but the
number of edges in the spanning tree would be equal to the number of vertices in the given
graph minus 1.

A complete undirected graph can have nn-2 number of spanning trees where n is the number
of vertices in the graph. Suppose, if n = 5, the number of maximum possible spanning trees
would be 55-2 = 125.

Applications of the spanning tree

Basically, a spanning tree is used to find a minimum path to connect all nodes of the graph.
Some of the common applications of the spanning tree are listed as follows -

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


 Cluster Analysis
 Civil network planning
 Computer network routing protocol

AD

Now, let's understand the spanning tree with the help of an example.

Example of Spanning tree

Suppose the graph be -

As discussed above, a spanning tree contains the same number of vertices as the graph, the
number of vertices in the above graph is 5; therefore, the spanning tree will contain 5
vertices. The edges in the spanning tree will be equal to the number of vertices in the graph
minus 1. So, there will be 4 edges in the spanning tree.

Some of the possible spanning trees that will be created from the above graph are given as
follows -

Properties of spanning-tree
Some of the properties of the spanning tree are given as follows -

 There can be more than one spanning tree of a connected graph G.


 A spanning tree does not have any cycles or loop.
 A spanning tree is minimally connected, so removing one edge from the tree will make the
graph disconnected.
 A spanning tree is maximally acyclic, so adding one edge to the tree will create a loop.
 There can be a maximum nn-2 number of spanning trees that can be created from a complete
graph.
 A spanning tree has n-1 edges, where 'n' is the number of nodes.

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


 If the graph is a complete graph, then the spanning tree can be constructed by removing
maximum (e-n+1) edges, where 'e' is the number of edges and 'n' is the number of vertices.

So, a spanning tree is a subset of connected graph G, and there is no spanning tree of a
disconnected graph.

Minimum Spanning tree


A minimum spanning tree can be defined as the spanning tree in which the sum of the
weights of the edge is minimum. The weight of the spanning tree is the sum of the weights
given to the edges of the spanning tree. In the real world, this weight can be considered as the
distance, traffic load, congestion, or any random value.

Example of minimum spanning tree

Let's understand the minimum spanning tree with the help of an example.

The sum of the edges of the above graph is 16. Now, some of the possible spanning trees
created from the above graph are -

So, the minimum spanning tree that is selected from the above spanning trees for the given
weighted graph is –

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


Applications of minimum spanning tree

The applications of the minimum spanning tree are given as follows -

 Minimum spanning tree can be used to design water-supply networks, telecommunication


networks, and electrical grids.
 It can be used to find paths in the map.

Algorithms for Minimum spanning tree

A minimum spanning tree can be found from a weighted graph by using the algorithms given
below -

 Prim's Algorithm
 Kruskal's Algorithm

Prim's Algorithm is a greedy algorithm that is used to find the minimum spanning tree from
a graph. 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.

Prim's algorithm starts with the single node and explores all the adjacent nodes with all the
connecting edges at every step. The edges with the minimal weights causing no cycles in the
graph got selected.

How does the prim's algorithm work?


Prim's algorithm is a greedy algorithm that starts from one vertex and continue to add the
edges with the smallest weight until the goal is reached. The steps to implement the prim's
algorithm are given as follows -

 First, we have to initialize an MST with the randomly chosen vertex.


 Now, we have to find all the edges that connect the tree in the above step with the
new vertices. From the edges found, select the minimum edge and add it to the tree.
 Repeat step 2 until the minimum spanning tree is formed.

The applications of prim's algorithm are -

 Prim's algorithm can be used in network designing.


 It can be used to make network cycles.
 It can also be used to lay down electrical wiring cables.

AD

Example 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.

Suppose, a weighted graph is -

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


Step 1 - First, we have to choose a vertex from the above graph. Let's choose B.

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.

Step 3 - Now, again, choose the edge with the minimum weight among all the other edges. In
this case, the edges DE and CD are such edges. Add them to MST and explore the adjacent of
C, i.e., E and A. So, select the edge DE and add it to the MST.

Step 4 - Now, select the edge CD, and add it to the MST.

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


Step 5 - Now, choose the edge CA. 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. The cost of
the MST is given below -

Cost of MST = 4 + 2 + 1 + 3 = 10 units.

Algorithm
1. Step 1: Select a starting vertex
2. Step 2: Repeat Steps 3 and 4 until there are fringe vertices
3. Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that has minimu
m weight
4. Step 4: Add the selected edge and the vertex to the minimum spanning tree T
[END OF LOOP]

5. Step 5: EXIT

AD

Complexity of Prim's algorithm


Now, let's see the time complexity of Prim's algorithm. The running time of the prim's
algorithm depends upon using the data structure for the graph and the ordering of edges.
Below table shows some choices -

 Time Complexity

Data structure used for the minimum edge weight Time Complexity
Adjacency matrix, linear searching O(|V|2)
Adjacency list and binary heap O(|E| log |V|)
Adjacency list and Fibonacci heap O(|E|+ |V| log |V|)

Kruskal's Algorithm is used to find the minimum spanning tree for a connected weighted
graph. The main target of the algorithm is to find the subset of edges by using which we can
traverse every vertex of the graph. It follows the greedy approach that finds an optimum
solution at every stage instead of focusing on a global optimum.

How does Kruskal's algorithm work?

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


In Kruskal's algorithm, we start from edges with the lowest weight and keep adding the edges
until the goal is reached. The steps to implement Kruskal's algorithm are listed as follows -

 First, sort all the edges from low weight to high.


 Now, take the edge with the lowest weight and add it to the spanning tree. If the edge
to be added creates a cycle, then reject the edge.
 Continue to add the edges until we reach all vertices, and a minimum spanning tree is
created.

The applications of Kruskal's algorithm are -

 Kruskal's algorithm can be used to layout electrical wiring among cities.


 It can be used to lay down LAN connections.

AD

Example of Kruskal's algorithm


Now, let's see the working of Kruskal's algorithm using an example. It will be easier to
understand Kruskal's algorithm using an example.

Suppose a weighted graph is -

The weight of the edges of the above graph is given in the below table -

Edge AB AC AD AE BC CD DE
Weight 1 7 10 5 3 4 2

Now, sort the edges given above in the ascending order of their weights.

Edge AB DE BC CD AE AC AD
Weight 1 2 3 4 5 7 10

Now, let's start constructing the minimum spanning tree.

Step 1 - First, add the edge AB with weight 1 to the MST.

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


Step 2 - Add the edge DE with weight 2 to the MST as it is not creating the cycle.

Step 3 - Add the edge BC with weight 3 to the MST, as it is not creating any cycle or loop.

Step 4 - Now, pick the edge CD with weight 4 to the MST, as it is not forming the cycle.

Step 5 - After that, pick the edge AE with weight 5. Including this edge will create the cycle,
so discard it.

Step 6 - Pick the edge AC with weight 7. Including this edge will create the cycle, so discard
it.

Step 7 - Pick the edge AD with weight 10. Including this edge will also create the cycle, so
discard it.

So, the final minimum spanning tree obtained from the given weighted graph by using
Kruskal's algorithm is -

The cost of the MST is = AB + DE + BC + CD = 1 + 2 + 3 + 4 = 10.

AD

Now, the number of edges in the above tree equals the number of vertices minus 1. So, the
algorithm stops here.

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


Algorithm
1. Step 1: Create a forest F in such a way that every vertex of the graph is a separate tree.

2. Step 2: Create a set E that contains all the edges of the graph.
3. Step 3: Repeat Steps 4 and 5 while E is NOT EMPTY and F is not spanning
4. Step 4: Remove an edge from E with minimum weight
5. Step 5: IF the edge obtained in Step 4 connects two different trees, then add it to the f
orest F
(for combining two trees into one tree).
ELSE
Discard the edge

6. Step 6: END

AD

Complexity of Kruskal's algorithm


Now, let's see the time complexity of Kruskal's algorithm.

 Time Complexity
The time complexity of Kruskal's algorithm is O(E logE) or O(V logV), where E is
the no. of edges, and V is the no. of vertices.

Transitive closure of a Graph


Transitive Closure it the reachability matrix to reach from vertex u to vertex v of a graph.
One graph is given, we have to find a vertex v which is reachable from another vertex u, for
all vertex pairs (u, v).

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


The final matrix is the Boolean type. When there is a value 1 for vertex u to vertex v, it
means that there is at least one path from u to v.

Input and Output


Input:
1101
0110
0011
0001

Output:
The matrix of transitive closure
1111
0111
0011
0001

SHORTEST PATH ALGORITHMS:

Dijkstra’s Algorithm:

Dijkstra’s algorithm is the iterative algorithmic process to provide us with the shortest path
from one specific starting node to all other nodes of a graph. It is different from the minimum
spanning tree as the shortest distance among two vertices might not involve all the vertices
of the graph.

It is important to note that Dijkstra’s algorithm is only applicable when all weights are
positive because, during the execution, the weights of the edges are added to find the shortest
pat

Dijkstra’s Algorithm lets take a graph and find the shortest path from source to all nodes.
Consider below graph and src = 0

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


Step 1:

 The set sptSet is initially empty and distances assigned to vertices are {0, INF, INF,
INF, INF, INF, INF, INF} where INF indicates infinite.
 Now pick the vertex with a minimum distance value. The vertex 0 is picked, include it
in sptSet. So sptSetbecomes {0}. After including 0 to sptSet, update distance values of
its adjacent vertices.
 Adjacent vertices of 0 are 1 and 7. The distance values of 1 and 7 are updated as 4 and
8.

The following subgraph shows vertices and their distance values, only the vertices with finite
distance values are shown. The vertices included in SPT are shown in green colour.

Step 2:

 Pick the vertex with minimum distance value and not already included in SPT (not in
sptSET). The vertex 1 is picked and added to sptSet.
 So sptSet now becomes {0, 1}. Update the distance values of adjacent vertices of 1.
 The distance value of vertex 2 becomes 12.

Step 3:

 Pick the vertex with minimum distance value and not already included in SPT (not in
sptSET). Vertex 7 is picked. So sptSet now becomes {0, 1, 7}.
 Update the distance values of adjacent vertices of 7. The distance value of vertex 6
and 8 becomes finite (15 and 9 respectively).

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


Step 4:

 Pick the vertex with minimum distance value and not already included in SPT (not in
sptSET). Vertex 6 is picked. So sptSet now becomes {0, 1, 7, 6}.
 Update the distance values of adjacent vertices of 6. The distance value of vertex 5
and 8 are updated.

We repeat the above steps until sptSetincludes all vertices of the given graph. Finally, we get
the following Shortest Path Tree (SPT).

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


DATA STRUCTURE AIML1 NOTES PALAK SHANDIL
Floyd-Warshall :
Floyd-Warshall Algorithm is an algorithm for finding the shortest path between all the pairs
of vertices in a weighted graph. This algorithm works for both the directed and undirected
weighted graphs. But, it does not work for the graphs with negative cycles (where the sum of
the edges in a cycle is negative).

A weighted graph is a graph in which each edge has a numerical value associated with it.

Floyd-Warhshall algorithm is also called as Floyd's algorithm, Roy-Floyd algorithm, Roy-


Warshall algorithm, or WFI algorithm.

This algorithm follows the dynamic programming approach to find the shortest paths.

How Floyd-Warshall Algorithm Works?


Let the given graph be:

Initial graph

Follow the steps below to find the shortest path between all the pairs of vertices.

1. Create a matrix A0 of dimension n*n where n is the number of vertices. The row and
the column are indexed as i and j respectively. i and j are the vertices of the graph.

Each cell A[i][j] is filled with the distance from the ith vertex to the jth vertex. If there

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


is no path from ith vertex to jth vertex, the cell is left as infinity.

2. Fill each cell with the distance between ith and jth vertex
3. Now, create a matrix A1 using matrix A0. The elements in the first column and the first
row are left as they are. The remaining cells are filled in the following way.

Let k be the intermediate vertex in the shortest path from source to destination. In this
step, k is the first vertex. A[i][j] is filled with (A[i][k] + A[k][j]) if (A[i][j] > A[i][k] + A[k][j]).

That is, if the direct distance from the source to the destination is greater than the path
through the vertex k, then the cell is filled with A[i][k] + A[k][j].

In this step, k is vertex 1. We calculate the distance from source vertex to destination
vertex through this vertex k.

Calculate the distance from the source vertex to destination vertex through this vertex
k
For example: For A1[2, 4], the direct distance from vertex 2 to 4 is 4 and the sum of the
distance from vertex 2 to 4 through vertex (ie. from vertex 2 to 1 and from vertex 1 to
4) is 7. Since 4 < 7, A0[2, 4] is filled with 4.
4. Similarly, A2 is created using A1. The elements in the second column and the second
row are left as they are.

In this step, k is the second vertex (i.e. vertex 2). The remaining steps are the same as
in step 2.

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


Calculate the distance from the source vertex to destination vertex through this vertex
2
5. Similarly, A3 and A4 is also created.

Calculate the distance from the source vertex to destination vertex through this vertex

3
Calculate the distance from the source vertex to destination vertex through this vertex
4
6. A4 gives the shortest path between each pair of vertices.

Floyd Warshall Algorithm:


 Initialize the solution matrix same as the input graph matrix as a first step.
 Then update the solution matrix by considering all vertices as an intermediate
vertex.
 The idea is to one by one pick all vertices and updates all shortest paths which
include the picked vertex as an intermediate vertex in the shortest path.
 When we pick vertex number k as an intermediate vertex, we already have
considered vertices {0, 1, 2, .. k-1} as intermediate vertices.

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL


 For every pair (i, j) of the source and destination vertices respectively, there
are two possible cases.
o k is not an intermediate vertex in shortest path from i to j. We keep the
value of dist[i][j] as it is.
o k is an intermediate vertex in shortest path from i to j. We update the
value of dist[i][j] as dist[i][k] + dist[k][j] if dist[i][j] >dist[i][k] +
dist[k][j]

DIFFERENCES:

Main Purposes:

 Dijkstra’s Algorithm is one example of a single-source shortest or SSSP algorithm,


i.e., given a source vertex it finds shortest path from source to all other vertices.
 Floyd Warshall Algorithm is an example of all-pairs shortest path algorithm, meaning
it computes the shortest path between all pair of nodes.

Time Complexities :

 Time Complexity of Dijkstra’s Algorithm: O(E log V)


 Time Complexity of Floyd Warshall: O(V3)

Other Points:

 We can use Dijskstra’s shortest path algorithm for finding all pair shortest paths by
running it for every vertex. But time complexity of this would be O(VE Log V) which
can go (V3 Log V) in worst case.
 Another important differentiating factor between the algorithms is their working
towards distributed systems. Unlike Dijkstra’s algorithm, Floyd Warshall can be
implemented in a distributed system, making it suitable for data structures such as
Graph of Graphs (Used in Maps).
 Lastly Floyd Warshall works for negative edge but no negative cycle, whereas
Dijkstra’s algorithm don’t work for negative edges.

DATA STRUCTURE AIML1 NOTES PALAK SHANDIL

You might also like