The objective of this assignment is to exercise your ability to sort,
hold data read from a file, use arrays to establish a loop invariant,
implement an algorithm for solving a basic real-world optimization problem.
Given a file containing a collection of costs of laying a cable
between two cities. Cities are identified by a number. All integers
from 1 to the number of cities are used for city identities. The
first line of the file contains two numbers stating the number of
cables and the number of cities, in that order. Each remaining line
specifies the cost of laying a particular cable. The format of such a
line is
<city-identity-1> <city-identity-2> <cost>
where all three tokens are numbers. The meaning of each line is: the
cost of a cable laid between <city-identity-1> and <city-identity-2> is <cost>. An example of a small input file
is this:
10 5 1 5 100 2 4 141 3 5 121 2 3 138 4 5 102 1 2 139 1 3 143 1 4 139 2 5 101 3 4 159
The problem is to read the data of a file with this format into a matrix called cables with the number of rows equal to the number of cables and the number of columns equal to three. Each row of cables will contain two cities (of type number) and the cost of a cable between them (also of type number). See the Help section in case of trouble.
Sort cables so that all elements of any row remain together in a
row but all rows are in increasing order of cost (most likely the
third column). See the Help section in case of trouble.
Create a one-dimensional array group that has a number of
elements equal to the number of distinct cities with respect to the
file read in above and is such that all its elements are distinct
integers. See the Help section in case of trouble.
Let
be a list of cities. Let
be a list of cables, each of which
connects two cities in
. For all
, define
to be the
cost of cable
. A sublist
of cables which
allows every city in
to connect with every other city in
is
called a spanning network of
. The cost of a spanning
network
is the sum of the costs of the cables in
: that is
. The minimum
cost spanning network of
is a spanning network of
which has cost no greater than any other spanning network of
.
The problem is, given
,
, and
, find the minimum cost
spanning network of
.
Your program should output the total minimum cost and the number of cables involved in the solution. Your program should maintain an array solution which holds all cables in the solution so that typing solution at the MATLAB prompt will show the cables themselves.
An algorithm that works is this:
Create a solution list S, initially empty.
Read in all the data to the cables array.
Sort the cables array by increasing order of cost.
For each cable d in the cables array, in order, do the following:
Let c1 and c2 be the cities d connects
If c1 and c2 are NOT yet connected by cables in S add d to S.
end
The first line is trivial to program. The next two lines are taken
care of by the solutions to the first and second problems above. The
fourth line is a for loop where a particular row or column of cables (you choose, but choose carefully) is considered on each
iteration. This is taken care of by indexing into the array. The
sixth line, and the only one left to consider, is tricky. How can we
determine easily whether two cities are connected by cables in list
?.
What we can try to do is associate a number with each city in such a
way that when the numbers of two cities are consulted, we can tell
right away whether they are connected. What would be a good
association? Well, suppose the number associated with two cities is
the same if the two cities are connected and different if they are not
connected? Then the test is simple: compare the two numbers for
equality. We try to build on this idea. How do we associate a number
with a city? We can create an array with a number of elements equal
to the number of cities and use the
element of the array to
hold the association with the
city. Call this array group. The test is something like
if group(cable(i,1)) ~= group(cable(i,2))
where cable(i,...) are elements of a single cable from the
sorted array. But how does the group array get its values?
Well, recall that we are trying to determine connectedness among
cables in
The only thing we haven't considered is how to add a cable to
.
This is covered in the help section.
To open a file use something similar to this:
fid = fopen(filename,'r'); % open the data file, read onlyTo read the next number in a file use something like this:
n = fscanf(fid,'%d',1); % get the number of data pointsTo read a row at a time use something like this:
cable = fscanf(fid,'%d',3); % read a cable (3 numbers) from fileTo add a row to an array use something like this:
[cables] = [cables cable]; % append a cable to an array cablesTo sort a matrix by rows use sortrows (you can do a help on this function at the MATLAB prompt). Be careful, the matrix to sort may have to be transposed! To define an empty solution array use:
solution = [];To define a group array with different integers, the easiest thing to do is use 1:m where m is the number of elements in the array. To add a cable to the solution array use:
[solution] = [solution c];where c contains the data of the cable to add but the data is organized differently.
Submit an m file which solves the problem of Section 5 on or before
May 25 using blackboard. See the course webpage at