Assignment 03 MdMH
Assignment 03 MdMH
Course Title: Data Structure & Algorithm Lab II Course Code: CSE 2218
Trimester & Year: Fall 2023 Section: D Credit Hours: 1.0 (MdMH)
For example:
The MST (Minimum Spanning Tree) for the above graph is:
Input Format:
The first line Contains an integer ‘T’ representing the number of the test case. Then the test cases are as
follows.
The first line of each test case argument contains a given integer ‘N’ representing the number of nodes
in the graph.
2 of 3
UNITED INTERNATIONAL UNIVERSITY
Department of Computer Science and Engineering (CSE)
The second line of each test case contains a given integer ‘M’ representing the number of edges in the
graph.
The next ‘M’ lines in each test case contain a matrix ‘E’ of size M x 2 which represents the ‘M’ edges such
that there is an edge directed from node E[i][0] to node E[i][1].
Output Format:
For each test case, print the minimum spanning tree in the form of edges and their weights which are
included in the MST.
Sample Input 1:
1
5 14
122
146
212
233
248
255
323
357
416
428
459
525
537
549
Sample Output 1:
122
146
233
255
Explanation of Input 1 :
The Minimum spanning tree for the given graph will contain the edges: (1,2) with weight 2, (1,4) with
weight 6, (2,3) with weight 3 and (2,5) with weight 5.
Sample Input 2 :
1
5 15
1 2 21
1 4 16
3 of 3
UNITED INTERNATIONAL UNIVERSITY
Department of Computer Science and Engineering (CSE)
2 1 12
2 3 13
2 4 18
2 5 15
3 2 13
3 5 17
4 1 16
4 2 18
4 5 19
5 1 18
5 2 15
5 3 17
5 4 19
Sample Output 2 :
1 2 12
1 4 16
2 3 13
2 5 15
Explanation of Input 2 :
The Minimum spanning tree for the given graph will contain the edges: (1,2) with weight 12, (1,4) with
weight 16, (2,3) with weight 13 and (2,5) with weight 15.
Tip:
Create a function like the following that will find the MST and return it:
}
4 of 3
UNITED INTERNATIONAL UNIVERSITY
Department of Computer Science and Engineering (CSE)
You want to determine if there is a valid path that exists from vertex source to vertex
destination.
Given edges and the integers n, source, and destination, return true if there is a valid path from
source to destination, or false otherwise.
Example 1:
Input: n = 3, edges = [[0,1],[1,2],[2,0]], source = 0,
destination = 2
Output: true
Explanation: There are two paths from vertex 0 to
vertex 2:
-0→1→2
-0→2
Example 2:
Input: n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], source
= 0, destination = 5
Output: false
Explanation: There is no path from vertex 0 to vertex 5.
Constraints:
edges[i].length == 2
ui != vi