Input and Output Format

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Input and Output format

The input for all the programs must be read from .txt files. The detailed format for each problem is
given in the below.

1. (2 marks) Given a graph G (0-1 adjacency matrix) and a coloring/labelling to the


vertices of G, verify that the coloring is valid or not.
Input: The input file (.txt format) contains n+2 rows. The first row contains a single value
(positive integer) and it denotes the number of vertices n in the graph G . The next n rows
and n columns represent the n × n0-1 adjacency matrix. The elements in each row are
separated by a space. The last row contains n positive integers, need not be distinct. Each
distinct integer denotes a color. The integers in the last row are separated by a space and i -
th color, from left to right, is corresponding to i -th vertex.

Output: print 1, if the coloring is valid, otherwise print 0.

2. (2 marks) Given a graph G (0-1 adjacency matrix), verify G is bipartite graph or not.

Input: The input file (.txt format) contains n+1 rows. The first row contains a single value
(positive integer) and it denotes the number of vertices n in the graph G . The next n rows
and n columns represent the n × n0-1 adjacency matrix. The elements in each row are
separated by a space.

Output: print 1, if the graph is bipartite, otherwise print 0.

3. (2 marks) Given a bipartite graph G=(V 1 ∪V 2 , E) (0-1 adjacency matrix), verify is


there any complete matching from V 1 to V 2 (Hint: use Hall’s marriage theorem).

Input: The input file (.txt format) contains n+1 rows. The first row contains two values
(positive integers) and they are separated by a space. The first number denotes |V 1|and the
second value denotes |V 2| . Further, the sum of the two values denotes n . The next n rows
and n columns represent the n × n0-1 adjacency matrix. The elements in each row are
separated by a space. Further, the first |V 1| rows and columns are corresponding to the
vertices in V 1 and the latter |V 2| rows and columns are corresponding to the vertices in V 2.

Output: print 1, if there is a complete matching from V 1 to V 2, otherwise print 0.


4. (3 marks) Given a graph G (0-1 adjacency matrix), find the number of connected
components in G using DFS method and display the vertices in each component as
well.

Input: The input file (.txt format) contains n+1 rows. The first row contains a single value
(positive integer) and it denote the number of vertices n in the graph G . . The next n rows
and n columns represent the n × n0-1 adjacency matrix. The elements in each row are
separated by a space.

Output: print your output in the format k $ $ strin g1 $ $ strin g2 $ $ … $ $ strin gk where k
denotes the number of connected components in the graph G and strin g i contains the
vertices in i -th connected components and if v1 , v 2 , … , v j are vertices in i -th connected
component, then strin g i is of the form v1 $ v 2 $ … $ v j.

5. (3 marks) Given a graph G (0-1 adjacency matrix), and a designated root r (a vertex
of G), find the levels of all the nodes in the spanning tree rooted at r obtained by
applying BFS method.

Input: The input file (.txt format) contains n+2 rows. The first row contains a single value
(positive integer) and it denote the number of vertices n in the graph G . The next n rows
and n columns represent the n × n0-1 adjacency matrix. The elements in each row are
separated by a space. The last row contains a value which denote the root r . Assume that
the vertex naming is 1 ,2 , … , n.

Output: print the level of each vertex, in the order 1 ,2 , … , n from left to right, separated by
a $.

6. (3 marks) Given a weighted graph G, find the cost of the minimum spanning tree
using Kruskal’s algorithm.

Input: The input file (.txt format) contains n+1 rows. The first row contains a single value
(positive integer) and it denote the number of vertices n in the graph G . The next n rows
and n columns represent the n × nweight matrix W =[w ij ] where w ij is a non-negative
integer and it denotes the cost of the edge {i , j } and if { i , j } is not an edge in the graph,
then w ij =0.

Output: print a single value which is the cost of MST returned by Kruskal’s algorithm.

You might also like