Programming Assignment 5: Minimum Spanning Trees: Algorithms On Graphs Class
Programming Assignment 5: Minimum Spanning Trees: Algorithms On Graphs Class
Introduction
Welcome to the third (and the last one) programming assignment of the Algorithms on Graphs class! In this
programming assignment you will be practicing implementing algorithms computing minimum spanning
trees.
Learning Outcomes
Upon completing this programming assignment you will be able to:
1. connect the given cities by roads of minimum total length such that there is a path between any two
cities;
Contents
1 Building Roads to Connect Cities 4
2 Clustering 6
3 Appendix 9
3.1 Compiler Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.2 Frequently Asked Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1
Graph Representation in Programming Assignments
In programming assignments, graphs are given as follows. The first line contains non-negative integers 𝑛 and
𝑚 — the number of vertices and the number of edges respectively. The vertices are always numbered from 1
to 𝑛. Each of the following 𝑚 lines defines an edge in the format u v where 1 ≤ 𝑢, 𝑣 ≤ 𝑛 are endpoints of
the edge. If the problem deals with an undirected graph this defines an undirected edge between 𝑢 and 𝑣. In
case of a directed graph this defines a directed edge from 𝑢 to 𝑣. If the problem deals with a weighted graph
then each edge is given as u v w where 𝑢 and 𝑣 are vertices and 𝑤 is a weight.
It is guaranteed that a given graph is simple. That is, it does not contain self-loops (edges going from a
vertex to itself) and parallel edges.
Examples:
∙ An undirected graph with four vertices and five edges:
4 5
2 1
4 3
1 4
2 4
3 2
4 3
1 2
2 5 4
1 3
2 5 4
1 3
Note that the vertices 1, 2, and 5 are isolated (have no adjacent edges), but they are still present in
the graph.
2
∙ A weighted directed graph with three vertices and three edges.
3 3
2 39
1 35
1 2 -2
3
5 9
1 2
−2
3
1 Building Roads to Connect Cities
Problem Introduction
In this problem, the goal is to build roads between some pairs of the
given cities such that there is a path between any two cities and the
total length of the roads is minimized.
Problem Description
Task. Given 𝑛 points on a plane, connect them with segments of minimum total length such that there is a
path between
√︀ any two points. Recall that the length of a segment with endpoints (𝑥1 , 𝑦1 ) and (𝑥2 , 𝑦2 )
is equal to (𝑥1 − 𝑥2 )2 + (𝑦1 − 𝑦2 )2 .
Input Format. The first line contains the number 𝑛 of points. Each of the following 𝑛 lines defines a point
(𝑥𝑖 , 𝑦𝑖 ).
Constraints. 1 ≤ 𝑛 ≤ 200; −103 ≤ 𝑥𝑖 , 𝑦𝑖 ≤ 103 are integers. All points are pairwise different, no three
points lie on the same line.
Output Format. Output the minimum total length of segments. The absolute value of the difference
between the answer of your program and the optimal value should be at most 10−6 . To ensure this,
output your answer with at least seven digits after the decimal point (otherwise your answer, while
being computed correctly, can turn out to be wrong because of rounding issues).
Time Limits.
language C C++ Java Python C# Haskell JavaScript Ruby Scala
time (sec) 2 2 3 10 3 4 10 10 6
Sample 1.
Input:
4
00
01
10
11
Output:
3.000000000
An optimal way to connect these four points is shown below. Note that there exists other ways of
connecting these points by segments of total weight 3.
4
Sample 2.
Input:
5
00
02
11
30
32
Output:
7.064495102
𝑥
√ √
The total length here is equal to 2 2 + 5 + 2.
5
2 Clustering
Problem Introduction
Problem Description
Task. Given 𝑛 points on a plane and an integer 𝑘, compute the largest possible value of 𝑑 such that the
given points can be partitioned into 𝑘 non-empty subsets in such a way that the distance between any
two points from different subsets is at least 𝑑.
Input Format. The first line contains the number 𝑛 of points. Each of the following 𝑛 lines defines a point
(𝑥𝑖 , 𝑦𝑖 ). The last line contains the number 𝑘 of clusters.
Constraints. 2 ≤ 𝑘 ≤ 𝑛 ≤ 200; −103 ≤ 𝑥𝑖 , 𝑦𝑖 ≤ 103 are integers. All points are pairwise different.
Output Format. Output the largest value of 𝑑. The absolute value of the difference between the answer of
your program and the optimal value should be at most 10−6 . To ensure this, output your answer with
at least seven digits after the decimal point (otherwise your answer, while being computed correctly,
can turn out to be wrong because of rounding issues).
Time Limits.
language C C++ Java Python C# Haskell JavaScript Ruby Scala
time (sec) 2 2 3 10 3 4 10 10 6
6
Sample 1.
Input:
12
76
43
51
17
27
57
33
78
28
44
67
26
3
Output:
2.828427124746
√
The answer is 8. The corresponding partition of the set of points into three clusters is shown below.
Sample 2.
Input:
8
31
12
46
98
99
89
3 11
4 12
4
Output:
5.000000000
The answer is 5. The corresponding partition of the set of points into four clusters is shown below.
7
𝑦
What To Do
Think about ways of adopting the Kruskal’s algorithm for solving this problem.
8
3 Appendix
3.1 Compiler Flags
C (gcc 7.4.0). File extensions: .c. Flags:
gcc - pipe - O2 - std = c11 < filename > - lm
If your C/C++ compiler does not recognize -std=c++14 flag, try replacing it with -std=c++0x flag
or compiling without this flag at all (all starter solutions can be compiled without it). On Linux
and MacOS, you most probably have the required compiler. On Windows, you may use your favorite
compiler or install, e.g., cygwin.
C# (mono 4.6.2). File extensions: .cs. Flags:
mcs
9
3.2 Frequently Asked Questions
Why My Submission Is Not Graded?
You need to create a submission and upload the source file (rather than the executable file) of your solution.
Make sure that after uploading the file with your solution you press the blue “Submit” button at the bottom.
After that, the grading starts, and the submission being graded is enclosed in an orange rectangle. After the
testing is finished, the rectangle disappears, and the results of the testing of all problems are shown.
∙ Good job! Hurrah! Your solution passed, and you get a point!
∙ Wrong answer. Your solution outputs incorrect answer for some test case. Check that you consider
all the cases correctly, avoid integer overflow, output the required white spaces, output the floating
point numbers with the required precision, don’t output anything in addition to what you are asked
to output in the output specification of the problem statement.
∙ Time limit exceeded. Your solution worked longer than the allowed time limit for some test case.
Check again the running time of your implementation. Test your program locally on the test of max-
imum size specified in the problem statement and check how long it works. Check that your program
doesn’t wait for some input from the user which makes it to wait forever.
∙ Memory limit exceeded. Your solution used more than the allowed memory limit for some test case.
Estimate the amount of memory that your program is going to use in the worst case and check that it
does not exceed the memory limit. Check that your data structures fit into the memory limit. Check
that you don’t create large arrays or lists or vectors consisting of empty arrays or empty strings, since
those in some cases still eat up memory. Test your program locally on the tests of maximum size
specified in the problem statement and look at its memory consumption in the system.
∙ Cannot check answer. Perhaps the output format is wrong. This happens when you output
something different than expected. For example, when you are required to output either “Yes” or
“No”, but instead output 1 or 0. Or your program has empty output. Or your program outputs not
only the correct answer, but also some additional information (please follow the exact output format
specified in the problem statement). Maybe your program doesn’t output anything, because it crashes.
∙ Unknown signal 6 (or 7, or 8, or 11, or some other). This happens when your program
crashes. It can be because of a division by zero, accessing memory outside of the array bounds, using
uninitialized variables, overly deep recursion that triggers a stack overflow, sorting with a contradictory
comparator, removing elements from an empty data structure, trying to allocate too much memory,
and many other reasons. Look at your code and think about all those possibilities. Make sure that you
use the same compiler and the same compiler flags as we do.
∙ Internal error: exception... Most probably, you submitted a compiled program instead of
a source code.
10
∙ Grading failed. Something wrong happened with the system. Report this through Coursera or edX
Help Center.
11