Assignment5-NIT CALICUT DSA
Assignment5-NIT CALICUT DSA
Assignment-5
Standard of Conduct
Violations of academic integrity will be severely penalized. Each student is expected to adhere to
high standards of ethical conduct, especially those related to cheating and plagiarism. Any
submitted work MUST BE an individual effort. Any academic dishonesty will result in zero
marks in the corresponding exam or evaluation and will be reported to the department council for
record keeping and for permission to assign an F grade in the course. The department policy on
academic integrity can be found at:
http://minerva.nitc.ac.in/cse/sites/default/files/attachments/news/Academic-Integrity_new.pdf
1. Write programs that compute the minimum spanning tree of a connected undirected graph
using the following algorithms:
a. Kruskal’s algorithm
b. Prim’s algorithm
Input format:
●The first line of the input contains a positive integer n, the number of vertices in the graph,
in the range 1 to 1000.
● The subsequent n l ines contain the labels of the nodes adjacent to the respective nodes,
sorted in ascending order from left to right.
● The subsequent n lines contain the weights of the edges corresponding to the adjacency list.
The edge weights are real numbers in the range [-10000, 10000]. Further, no two edges have
the same weight.
Output format:
Single line containing the sum of the edge weights of the minimum spanning tree.
Note -
In a graph with n vertices, the vertices are labeled from 0 to n-1. Use adjacency lists to store the
graphs, with the vertices sorted in ascending order. The adjacency list of each node is a singly
linked list that contains its adjacent nodes sorted in ascending order from left to right. The nodes
in this list contain two fields, namely, the label of the adjacent node and the weight of the edge,
if provided. Unless specified otherwise, the adjacency lists must be processed iteratively from
left to right.
Input
7
15
026
13
246
356
04
134
28 10
28 16 14
16 12
12 22 18
22 25 24
10 25
14 18 24
Output
99
2. Given a directed graph G= (V, E) , edge weights w ≥0, source s ∈ V, find the weight of
shortest path from s to all other vertices.
Input Format
● First line is the number of nodes (V) in a graph. Vertices are labelled from 0 to V-1.
● Second line is the number of edges (E) in a graph.
● Next E lines each line consist of three space separated integers x y z, where x and y
denote the two nodes between which the directed edge exist and z is the weight of the
edge.
● Next line consists of one integer s, where s denotes the source node.
Output Format
● Print the destination node and weight associated with it. If there is no path print “INF”
Input
4
4
011
034
212
325
0
Output
00
11
29
34
3. Write a C program to check if there is a negative cycle in a directed graph. A negative cycle is
one in which the overall sum of the weights in the cycle is negative.
Input Format
● First line contains two integers n, m denoting number of vertices and number of edges
present in a directed graph
● Next m lines contains 3 integers x, y, w denoting there is an directed edge from x to y
having a weight w
Output Format
Input
58
0 1 -1
024
123
132
142
325
311
4 3 -3
Output
-1