Kruskal's Algorithm Assignment
Code
this version of Kruskal's algorithm
in C++ or C. Write the program to read the following kind of input from
stdin:
7
1 2 1
2 3 2
4 5 3
6 7 3
1 4 4
2 5 4
4 7 4
3 5 5
2 4 6
3 6 6
5 7 7
5 6 8
Above the input gives first the number of nodes in the graph, then a list of
the edges and edge weights, in order by non-decreasing weight, in the format:
(one end of edge, other end of edge, weight of edge)
As output, have the program echo the input, then output the edges of a minimum
weight spanning tree, and lastly output the total weight of the tree. The
format of the edges output should be just like the input. Thus the output
corresponding to the input above would be something like:
=========================
The graph is:
7
1 2 1
2 3 2
4 5 3
6 7 3
1 4 4
2 5 4
4 7 4
3 5 5
2 4 6
3 6 6
5 7 7
5 6 8
A minimum weight spanning tree is:
1 2 1
2 3 2
4 5 3
6 7 3
1 4 4
4 7 4
Total weight: 17
=========================
If you like, you can assume that the number of nodes cannot exceed 10, and
that the number of edges cannot exceed 45.
Since the input is sorted by edge cost, your program is not required to
include the step
here
that sorts the edges.
Make some test input. Create at least one interesting graph of your own, and
feed the corresponding input to your program to test it. (Don't forget to
arrange the edges in the input in sorted order!) Also do a test run using the
sample input above.
You must implement the most efficient version of the union-find data
structure. (You will need to read all of section 4.6 to understand what that
means.) Remind me to talk to you about ways of doing the implementation of
the union-find data structure.
E-mail me a copy of the source code for your program before midnight on the
due date. At the beginning of class on the due date, turn in sketches of both
graphs and printouts of the test runs.
(This problems is designed to help you master the details of Kruskal's
algorithm, as well as the union-find problem. Efficient union-find procedures
are key pieces of many advanced algorithms.