Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
23 views35 pages

Module 5 Graphs

The document provides an overview of data structures using C, focusing on hashing and graph representation. It explains the components of hashing, including keys, hash functions, and hash tables, as well as collision resolution techniques like separate chaining and open addressing. Additionally, it covers graph representation methods such as adjacency matrices and lists, along with elementary graph operations like Breadth-First Search (BFS).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views35 pages

Module 5 Graphs

The document provides an overview of data structures using C, focusing on hashing and graph representation. It explains the components of hashing, including keys, hash functions, and hash tables, as well as collision resolution techniques like separate chaining and open addressing. Additionally, it covers graph representation methods such as adjacency matrices and lists, along with elementary graph operations like Breadth-First Search (BFS).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Hashing – Introduction, Static Hashing, Dynamic Hashing Text Book 3 -8.1 – 8.3 Graphs - Graph representation, Elementary graph operations,
Minimum cost spanning Trees – Kruskal’s Algorithm, Prim’s algorithm Text Book 3 - 6.1,6.2,6.3.1,6.3.2

Hashing – Introduction,

Hashing is the process of transforming any given key or a string of characters into another value. This is usually represented by a shorter, fixed-length value or key that represents and makes
it easier to find or employ the original string.

What is Hashing?

Hashing in Data Structures refers to the process of transforming a given key to another value. It involves mapping data to a specific index in a hash table using a hash function that enables

fast retrieval of information based on its key. The transformation of a key to the corresponding value is done using a Hash Function and the value obtained from the hash function is

called Hash Code .

Components of Hashing
There are majorly three components of hashing:
1. Key: A Key can be anything string or integer which is fed as input in the hash function the technique that determines an index or location for storage of
an item in a data structure.

2. Hash Function: The hash function receives the input key and returns the index of an element in an array called a hash table. The index
is known as the hash index .

3. Hash Table: Hash table is a data structure that maps keys to values using a special function called a hash function. Hash stores the data in an asso -
ciative manner in an array where each data value has its own unique index.

Components of Hashing
There are majorly three components of hashing:

1. Key: A Key can be anything string or integer which is fed as input in the hash function the technique that determines an index or location for storage of an item in a data struc -

ture.

2. Hash Function: The hash function receives the input key and returns the index of an element in an array called a hash table. The index is known as the hash index.

3. Hash Table: Hash table is a data structure that maps keys to values using a special function called a hash function. Hash stores the data in an associative manner in an array

where each data value has its own unique index.

Dr. Nataraju A B Page - 1


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

How does Hashing work?

Suppose we have a set of strings {“ab”, “cd”, “efg”} and we would like to store it in a table.

Our main objective here is to search or update the values stored in the table quickly in O(1) time and we are not concerned about the ordering of strings in the table. So the given set of strings

can act as a key and the string itself will act as the value of the string but how to store the value corresponding to the key?

 Step 1: We know that hash functions (which is some mathematical formula) are used to calculate the hash value which acts as the index of the data structure where the value will

be stored.

 Step 2: So, let’s assign

o “a” = 1,

o “b”=2, .. etc, to all alphabetical characters.

 Step 3: Therefore, the numerical value by summation of all characters of the string:

 “ab” = 1 + 2 = 3,

 “cd” = 3 + 4 = 7 ,

 “efg” = 5 + 6 + 7 = 18

 Step 4: Now, assume that we have a table of size 7 to store these strings. The hash function that is used here is the sum of the characters in key mod Table size . We can com-

pute the location of the string in the array by taking the sum(string) mod 7 .

 Step 5: So we will then store

o “ab” in 3 mod 7 = 3,

o “cd” in 7 mod 7 = 0, and

Dr. Nataraju A B Page - 2


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

o “efg” in 18 mod 7 = 4.

The above technique enables us to calculate the location of a given string by using a simple hash function and rapidly find the value that is stored in that location. Therefore the idea of hash -

ing seems like a great way to store (key, value) pairs of the data in a table.

What is a Hash function?

The hash function creates a mapping between key and value, this is done through the use of mathematical formulas known as hash functions. The result of the

hash function is referred to as a hash value or hash. The hash value is a representation of the original string of characters but usually smaller than the original.

For example: Consider an array as a Map where the key is the index and the value is the value at that index. So for an array A if we have index i which will be

treated as the key then we can find the value by simply looking at the value at A[i].

Representation of Graph Data Structure:

There are two ways to store a graph:

 Adjacency Matrix

 Adjacency List

Adjacency Matrix Representation of Graph Data Structure:

Dr. Nataraju A B Page - 3


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

In this method, the graph is stored in the form of the 2D matrix where rows and columns denote vertices. Each entry in the matrix represents the weight of the

edge between those vertices.

Adjacency List Representation of Graph:


This graph is represented as a collection of linked lists. There is an array of pointer which points to the edges connected to that vertex.

Dr. Nataraju A B Page - 4


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Static Hashing,

Dynamic Hashing

In dynamic hashing mechanism the collisions will resolved in two different ways namely open addressing and closed addressing.

 Open Addressing
 chaining
 Closed addressing
 Linear probing
 Quadratic probing
 Double hashing

Open addressing with separate chaining for collision handling in hashing operations

Separate Chaining:

The idea behind separate chaining is to implement the array as a linked list called a chain.

The linked list data structure is used to implement this technique. So what happens is, when multiple elements are hashed into the same slot index, then these elements are inserted into a

singly-linked list which is known as a chain.

Dr. Nataraju A B Page - 5


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Here, all those elements that hash into the same slot index are inserted into a linked list. Now, we can use a key K to search in the linked list by just linearly traversing. If the intrinsic key for

any entry is equal to K then it means that we have found our entry. If we have reached the end of the linked list and yet we haven’t found our entry then it means that the entry does not exist.

Hence, the conclusion is that in separate chaining, if two different elements have the same hash value then we store both the elements in the same linked list one after the other.

Example: Let us consider a simple hash function as “key mod 5” and a sequence of keys as 12, 22, 15, 25

Open Addressing Collision Handling technique in Hashing

Dr. Nataraju A B Page - 6


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Open Addressing is a method for handling collisions. In Open Addressing, all elements are stored in the hash table itself. So at any point, the size of the table must
be greater than or equal to the total number of keys (Note that we can increase table size by copying old data if needed). This approach is also known as closed
hashing. This entire procedure is based upon probing.

We will understand the types of probing ahead:

 Insert(k): Keep probing until an empty slot is found. Once an empty slot is found, insert k.
 Search(k): Keep probing until the slot’s key doesn’t become equal to k or an empty slot is reached.
 Delete(k): Delete operation is interesting. If we simply delete a key, then the search may fail. So slots of deleted keys are marked specially as “deleted”.
The insert can insert an item in a deleted slot, but the search doesn’t stop at a deleted slot.

Different ways of Open Addressing:

1. Linear Probing:

In linear probing, the hash table is searched sequentially that starts from the original location of the hash. If in case the location that we get is already occupied, then
we check for the next location.

The function used for rehashing is as follows: rehash(key) = (n+1)%table-size.

For example, The typical gap between two probes is 1 as seen in the example below:

Let hash(x) be the slot index computed using a hash function and S be the table size

If slot hash(x) % S is full, then we try (hash(x) + 1) % S


If (hash(x) + 1) % S is also full, then we try (hash(x) + 2) % S
If (hash(x) + 2) % S is also full, then we try (hash(x) + 3) % S

Example: Let us consider a simple hash function as “key mod 5” and a sequence of keys that are to be inserted are 50, 70, 76, 85, 93.

Dr. Nataraju A B Page - 7


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

2. Quadratic Probing

If you observe carefully, then you will understand that the interval between probes will increase proportionally to the hash value. Quadratic probing is a method
with the help of which we can solve the problem of clustering that was discussed above. This method is also known as the mid-square method. In this method, we
look for the i2‘th slot in the ith iteration. We always start from the original hash location. If only the location is occupied then we check the other slots.

let hash(x) be the slot index computed using hash function.

If slot hash(x) % S is full, then we try (hash(x) + 1*1) % S


If (hash(x) + 1*1) % S is also full, then we try (hash(x) + 2*2) % S
If (hash(x) + 2*2) % S is also full, then we try (hash(x) + 3*3) % S

Dr. Nataraju A B Page - 8


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

2
Example: Let us consider table Size = 7, hash function as Hash(x) = x % 7 and collision resolution strategy to be f(i) = i . Insert = 22, 30, and 50.

Dr. Nataraju A B Page - 9


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Dr. Nataraju A B Page - 10


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Graphs - Graph representation,

Here are the two most common ways to represent a graph :

1. Adjacency Matrix

2. Adjacency List

Adjacency Matrix

An adjacency matrix is a way of representing a graph as a matrix of boolean (0’s and 1’s).

Let’s assume there are n vertices in the graph So, create a 2D matrix adjMat[n][n] having dimension n x n.


If there is an edge from vertex i to j, mark adjMat[i][j] as 1.


If there is no edge from vertex i to j, mark adjMat[i][j] as 0.

Representation of Undirected Graph to Adjacency Matrix:

The below figure shows an undirected graph. Initially, the entire Matrix is initialized to 0. If there is an edge from source to destination, we insert 1 to both cases
(adjMat[destination] and adjMat[destination]) because we can go either way.

Representation of Directed Graph to Adjacency Matrix:

Dr. Nataraju A B Page - 11


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

The below figure shows a directed graph. Initially, the entire Matrix is initialized to 0. If there is an edge from source to destination, we insert 1 for that
particular adjMat[destination].

Adjacency List

An array of Lists is used to store edges between two vertices. The size of array is equal to the number of vertices (i.e, n). Each index in this array represents a
specific vertex in the graph. The entry at the index i of the array contains a linked list containing the vertices that are adjacent to vertex i.

Let’s assume there are n vertices in the graph So, create an array of list of size n as adjList[n].


adjList[0] will have all the nodes which are connected (neighbour) to vertex 0.


adjList[1] will have all the nodes which are connected (neighbour) to vertex 1 and so on.

Representation of Undirected Graph to Adjacency list:

The below undirected graph has 3 vertices. So, an array of list will be created of size 3, where each indices represent the vertices. Now, vertex 0 has two neighbours
(i.e, 1 and 2). So, insert vertex 1 and 2 at indices 0 of array. Similarly, For vertex 1, it has two neighbour (i.e, 2 and 0) So, insert vertices 2 and 0 at indices 1 of
array. Similarly, for vertex 2, insert its neighbours in array of list.

Dr. Nataraju A B Page - 12


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Representation of Directed Graph to Adjacency list:

The below directed graph has 3 vertices. So, an array of list will be created of size 3, where each indices represent the vertices. Now, vertex 0 has no neighbours.
For vertex 1, it has two neighbour (i.e, 0 and 2) So, insert vertices 0 and 2 at indices 1 of array. Similarly, for vertex 2, insert its neighbours in array of list.

Elementary graph operations,

BFS of Graphs

Dr. Nataraju A B Page - 13


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Breadth-First Search (BFS) is an algorithm for traversing or searching tree or graph data structures. It starts at a given node (often called the "source" or "root"
node) and explores all of the neighbor nodes at the present depth level before moving on to nodes at the next depth level. Here's a step-by-step explanation of how
BFS works:

1. Initialization:
o
Start with a queue and enqueue the starting node.
o
Mark the starting node as visited.
o

2. Process:
o
While the queue is not empty:

Dequeue a node from the queue.

Process the dequeued node (e.g., print it, check if it is the target node, etc.).

Enqueue all adjacent (neighboring) nodes of the dequeued node that have not been visited yet and mark them as visited.

3. Termination:
o
The algorithm continues until the queue is empty, meaning all reachable nodes have been visited.

Example

Consider a graph represented as an adjacency list:

Graph = {
'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'F'],
'D': ['B'],
'E': ['B', 'F'],
'F': ['C', 'E']
}

Let's perform BFS starting from node 'A':


1. Initialize the queue: Q = ['A']
2. Mark 'A' as visited: visited = {'A'}
Iteration 1:
 Dequeue 'A': Q = []
 Process 'A': Visit 'A'
 Enqueue neighbors 'B' and 'C': Q = ['B', 'C']
 Mark 'B' and 'C' as visited: visited = {'A', 'B', 'C'}
Iteration 2:
 Dequeue 'B': Q = ['C']
 Process 'B': Visit 'B'
 Enqueue neighbors 'D' and 'E': Q = ['C', 'D', 'E']
 Mark 'D' and 'E' as visited: visited = {'A', 'B', 'C', 'D', 'E'}
Iteration 3:
 Dequeue 'C': Q = ['D', 'E']

Dr. Nataraju A B Page - 14


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

 Process 'C': Visit 'C'


 Enqueue neighbor 'F': Q = ['D', 'E', 'F']
 Mark 'F' as visited: visited = {'A', 'B', 'C', 'D', 'E', 'F'}
Iteration 4:
 Dequeue 'D': Q = ['E', 'F']
 Process 'D': Visit 'D'
Iteration 5:
 Dequeue 'E': Q = ['F']
 Process 'E': Visit 'E'
Iteration 6:
 Dequeue 'F': Q = []
 Process 'F': Visit 'F'
The BFS traversal order is: A -> B -> C -> D -> E -> F.

BFS of Graphs

Breadth First Search (BFS) for a Graph Algorithm:

Let’s discuss the algorithm for the BFS:

1. Initialization: Enqueue the starting node into a queue and mark it as visited.

2. Exploration: While the queue is not empty:

 Dequeue a node from the queue and visit it (e.g., print its value).

 For each unvisited neighbor of the dequeued node:

o Enqueue the neighbor into the queue.

o Mark the neighbor as visited.

3. Termination: Repeat step 2 until the queue is empty.

This algorithm ensures that all nodes in the graph are visited in a breadth-first manner, starting from the starting node.

How Does the BFS Algorithm Work?

Starting from the root, all the nodes at a particular level are visited first and then the nodes of the next level are traversed till all the nodes are visited.

To do this a queue is used. All the adjacent unvisited nodes of the current level are pushed into the queue and the nodes of the current level are marked visited and

popped from the queue.

Dr. Nataraju A B Page - 15


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Illustration:

Let us understand the working of the algorithm with the help of the following example.

Step1: Initially queue and visited arrays are empty.

Queue and visited arrays are empty initially.

Step2: Push node 0 into queue and mark it visited.

Push node 0 into queue and mark it visited.

Step 3: Remove node 0 from the front of queue and visit the unvisited neighbours and push them into queue.

Dr. Nataraju A B Page - 16


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Remove node 0 from the front of queue and visited the unvisited neighbours and push into queue.

Step 4: Remove node 1 from the front of queue and visit the unvisited neighbours and push them into queue.

Remove node 1 from the front of queue and visited the unvisited neighbours and push

Step 5: Remove node 2 from the front of queue and visit the unvisited neighbours and push them into queue.

Dr. Nataraju A B Page - 17


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Remove node 2 from the front of queue and visit the unvisited neighbours and push them into queue.

Step 6: Remove node 3 from the front of queue and visit the unvisited neighbours and push them into queue.

As we can see that every neighbours of node 3 is visited, so move to the next node that are in the front of the queue.

Remove node 3 from the front of queue and visit the unvisited neighbours and push them into queue.

Steps 7: Remove node 4 from the front of queue and visit the unvisited neighbours and push them into queue.

As we can see that every neighbours of node 4 are visited, so move to the next node that is in the front of the queue.

Remove node 4 from the front of queue and visit the unvisited neighbours and push them into queue.

Now, Queue becomes empty, So, terminate these process of iteration.

BFS traversal O/P  1, 2, 2, 3, 4

Minimum cost spanning Trees

Dr. Nataraju A B Page - 18


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

A Minimum Cost Spanning Tree (MCST) is a subset of the edges in a connected, weighted graph that connects all the vertices together without any cycles and with the minimum possible

total edge weight. The problem of finding an MCST is fundamental in graph theory and has practical applications in network design, such as designing least-cost communication networks,

electrical grids, and transportation networks.

There are several algorithms to find an MCST, with the two most popular being:

1. Kruskal's Algorithm:
o This algorithm sorts all the edges in the graph by their weights.
o It adds edges one by one to the spanning tree, starting with the smallest weight, ensuring that no cycles are formed.
o It uses a union-find data structure to efficiently check for cycles.
2. Prim's Algorithm:
o This algorithm starts with a single vertex and grows the spanning tree one edge at a time.
o It always adds the smallest edge that connects a vertex in the tree to a vertex outside the tree.
o It can be efficiently implemented using a priority queue (or min-heap).

Kruskal's Algorithm Steps:

1. Sort all edges in non-decreasing order of their weight.


2. Initialize an empty forest (a set of trees), where each vertex is a separate tree.
3. For each edge in the sorted list:
o If adding the edge connects two different trees, add it to the spanning tree and merge the two trees.
o If adding the edge would form a cycle, skip it.
4. Repeat until there are V−1V - 1V−1 edges in the spanning tree (where VVV is the number of vertices).

Prim's Algorithm Steps:

1. Start with an arbitrary vertex and add it to the spanning tree.


2. Initialize a priority queue with all edges from this vertex.
3. While the priority queue is not empty and the spanning tree does not contain all vertices:
o Extract the edge with the minimum weight from the priority queue.
o If the edge connects a new vertex to the spanning tree, add it to the tree and add all edges from this new vertex to the priority queue.
4. Repeat until all vertices are included in the spanning tree

Kruskal’s Algorithm,

Kruskal’s Minimum Spanning Tree (MST) Algorithm

A minimum spanning tree (MST) or minimum weight spanning tree for a weighted, connected, undirected graph is a spanning tree with a weight less than or equal to the weight of every

other spanning tree.

Dr. Nataraju A B Page - 19


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

In Kruskal’s algorithm, sort all edges of the given graph in increasing order. Then it keeps on adding new edges and nodes in the MST if the newly added edge does not form a cycle. It picks
the minimum weighted edge at first and the maximum weighted edge at last. Thus we can say that it makes a locally optimal choice in each step in order to find the optimal solution. Hence
this is a Greedy Algorithm.

How to find MST using Kruskal’s algorithm?

Below are the steps for finding MST using Kruskal’s algorithm:

Sort all the edges in non-decreasing order of their weight.

Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If the cycle is not formed, include this edge. Else, discard it.

Repeat step#2 until there are (V-1) edges in the spanning tree.

Illustration:

Below is the illustration of the above approach:

Input Graph:

The graph contains 9 vertices and 14 edges. So, the minimum spanning tree formed will be having (9 – 1) = 8 edges.

After sorting:

Weight Source Destination

1 7 6

2 8 2

Dr. Nataraju A B Page - 20


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Weight Source Destination

2 6 5

4 0 1

4 2 5

6 8 6

7 2 3

7 7 8

8 0 7

8 1 2

9 3 4

10 5 4

11 1 7

14 3 5

Now pick all edges one by one from the sorted list of edges

Step 1: Pick edge 7-6. No cycle is formed, include it.

Dr. Nataraju A B Page - 21


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Add edge 7-6 in the MST

Step 2: Pick edge 8-2. No cycle is formed, include it.

Add edge 8-2 in the MST

Step 3: Pick edge 6-5. No cycle is formed, include it.

Dr. Nataraju A B Page - 22


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Add edge 6-5 in the MST

Step 4: Pick edge 0-1. No cycle is formed, include it.

Add edge 0-1 in the MST

Step 5: Pick edge 2-5. No cycle is formed, include it.

Dr. Nataraju A B Page - 23


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Add edge 2-5 in the MST

Step 6: Pick edge 8-6. Since including this edge results in the cycle, discard it. Pick edge 2-3: No cycle is formed, include it.

Add edge 2-3 in the MST

Dr. Nataraju A B Page - 24


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Step 7: Pick edge 7-8. Since including this edge results in the cycle, discard it. Pick edge 0-7. No cycle is formed, include it.

Add edge 0-7 in MST

Step 8: Pick edge 1-2. Since including this edge results in the cycle, discard it. Pick edge 3-4. No cycle is formed, include it.

Dr. Nataraju A B Page - 25


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Add edge 3-4 in the MST

Note: Since the number of edges included in the MST equals to (V – 1), so the algorithm stops here

Prim’s algorithm

The algorithm starts with an empty spanning tree. The idea is to maintain two sets of vertices. The first set contains the vertices already included in the MST, and the other set contains the

vertices not yet included. At every step, it considers all the edges that connect the two sets and picks the minimum weight edge from these edges. After picking the edge, it moves the other

endpoint of the edge to the set containing MST.

A group of edges that connects two sets of vertices in a graph is called cut in graph theory. So, at every step of Prim’s algorithm, find a cut, pick the
minimum weight edge from the cut, and include this vertex in MST Set (the set that contains already included vertices).

How does Prim’s Algorithm Work?

The working of Prim’s algorithm can be described by using the following steps:

Step 1: Determine an arbitrary vertex as the starting vertex of the MST.


Step 2: Follow steps 3 to 5 till there are vertices that are not included in the MST (known as fringe vertex).
Step 3: Find edges connecting any tree vertex with the fringe vertices.
Step 4: Find the minimum among these edges.
Step 5: Add the chosen edge to the MST if it does not form any cycle.
Step 6: Return the MST and exit

Note: For determining a cycle, we can divide the vertices into two sets [one set contains the vertices included in MST and the other contains the fringe vertices.]

Illustration of Prim’s Algorithm:

Consider the following graph as an example for which we need to find the Minimum Spanning Tree (MST).

Dr. Nataraju A B Page - 26


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Example of a graph

Step 1: Firstly, we select an arbitrary vertex that acts as the starting vertex of the Minimum Spanning Tree. Here we have selected vertex 0 as the starting vertex.

0 is selected as starting vertex

Step 2: All the edges connecting the incomplete MST and other vertices are the edges {0, 1} and {0, 7}. Between these two the edge with minimum weight is {0, 1}.
So include the edge and vertex 1 in the MST.

Dr. Nataraju A B Page - 27


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

1 is added to the MST

Step 3: The edges connecting the incomplete MST to other vertices are {0, 7}, {1, 7} and {1, 2}. Among these edges the minimum weight is 8 which is of the edges
{0, 7} and {1, 2}. Let us here include the edge {0, 7} and the vertex 7 in the MST. [We could have also included edge {1, 2} and vertex 2 in the MST].

7 is added in the MST

Dr. Nataraju A B Page - 28


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Step 4: The edges that connect the incomplete MST with the fringe vertices are {1, 2}, {7, 6} and {7, 8}. Add the edge {7, 6} and the vertex 6 in the MST as it has
the least weight (i.e., 1).

6 is added in the MST

Step 5: The connecting edges now are {7, 8}, {1, 2}, {6, 8} and {6, 5}. Include edge {6, 5} and vertex 5 in the MST as the edge has the minimum weight (i.e., 2)
among them.

Dr. Nataraju A B Page - 29


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Include vertex 5 in the MST

Step 6: Among the current connecting edges, the edge {5, 2} has the minimum weight. So include that edge and the vertex 2 in the MST.

Dr. Nataraju A B Page - 30


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Include vertex 2 in the MST

Step 7: The connecting edges between the incomplete MST and the other edges are {2, 8}, {2, 3}, {5, 3} and {5, 4}. The edge with minimum weight is edge {2, 8}
which has weight 2. So include this edge and the vertex 8 in the MST.

Dr. Nataraju A B Page - 31


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Add vertex 8 in the MST

Step 8: See here that the edges {7, 8} and {2, 3} both have same weight which are minimum. But 7 is already part of MST. So we will consider the edge {2, 3} and
include that edge and vertex 3 in the MST.

Dr. Nataraju A B Page - 32


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Include vertex 3 in MST

Step 9: Only the vertex 4 remains to be included. The minimum weighted edge from the incomplete MST to 4 is {3, 4}.

Dr. Nataraju A B Page - 33


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Include vertex 4 in the MST

The final structure of the MST is as follows and the weight of the edges of the MST is (4 + 8 + 1 + 2 + 4 + 2 + 7 + 9) = 37.

The structure of the MST formed using the above method

Note: If we had selected the edge {1, 2} in the third step then the MST would look like the following.

Dr. Nataraju A B Page - 34


Data Structures using C [BEC405D] Dept of ECE, ACIT, Bangalore

Structure of the alternate MST if we had selected edge {1, 2} in the MST

Dr. Nataraju A B Page - 35

You might also like