EE378B Homework 5 Solution
Thanks to
Yanjun Han
MDS-MAP Algorithm
For a sphere of radius R and two points (θ1 , φ1 ), (θ2 , φ2 ) on it (where θi denotes the latitude and φi
denotes the longitude), the geodesic distance between them is given by
d = R arccos (sin θ1 sin θ2 + cos θ1 sin φ1 · cos θ2 sin φ2 + cos θ1 cos φ1 · cos θ2 cos φ2 )
= R arccos (sin θ1 sin θ2 + cos θ1 cos θ2 cos(φ1 − φ2 )) .
As a result, the Matlab codes for Part (a) and (b) are as follows:
1
2
3
4
5
6
7
%% load data
fin = fopen('cities.txt');
data = textscan(fin,'%s %f %f %f %f %[ˆ\n]','CommentStyle','#','delimiter','\t');
name = data{1};
n = length(name);
latitude = (data{2} + data{3}/60) * (pi/180);
longitude = (data{4} + data{5}/60) * (pi/180);
8
9
10
11
12
13
14
15
16
17
18
19
%% compute connectivity/distance graph
% distance graph
R = 6378137;
dist = zeros(n,n);
for i = 1 : n
for j = i+1 : n
dist(i,j) = R * acos(sin(latitude(i)) * sin(latitude(j)) + ...
cos(latitude(i)) * cos(latitude(j)) * cos(longitude(i) − longitude(j)));
end
end
dist = dist + dist';
20
21
22
23
24
25
26
27
28
% connectivity graph
connt = zeros(n,n);
for i = 1 : n
dist sort = sort(dist(i,:), 'ascend');
connt(i,:) = double(dist(i,:) <= dist sort(7)); % closest 6 neighbors
end
connt = connt − diag(diag(connt));
connt = double(connt + connt' > 0);
As for Part (c) and (d), the MDS-MAP algorithm is implemented as follows:
1
2
3
4
5
6
function location = MDS MAP(dist)
% the MDS−MAP algorithm
% input:
%%% dist: the n*n distance/connectivity matrix
% output:
%%% location: the n*2 matrix of 2−D locations
7
8
9
10
11
12
13
14
[n, ˜] = size(dist);
D = graphallshortestpaths(sparse(dist));
D = D.ˆ2; % original matrix D
P = eye(n) − ones(n,n)/n;
Q = −P*D*P; % centered matrix
[U, S, ˜] = svds(Q,2); % eigendecomposition
location = U * sqrt(S); % return locations
15
1
2
16
end
Based on this algorithm, we can recover the city locations using the connectivity graph or the actual
distance graph. Since the resulting locations are only accurate up to translation, rotation and reflection,
we have manually rotated and reflected our results to fit the real US map. Moreover, for clarity, we have
chosen some cities and displayed them in the plot. Note that although in (d) we have used the fully
connected graph as the input of the MDS-MAP algorithm, in the display we only show the edges from the
corresponding closest neighbors. The code is as follows:
1
2
3
4
%% Plot locations
location connt = MDS MAP(connt);
location dist = MDS MAP(dist);
chosen idx = [4,5,6,13,21,27,45,58,63,69,80,105,106,110,121,126];
5
6
7
8
% manually rotate/reflect data
location connt = location connt * [−1 0;0 −1];
location dist = location dist * [1 0;0 −1];
9
10
11
12
13
14
figure(1);
gplot(connt, location connt, ':');
for idx = chosen idx
text(location connt(idx,1),location connt(idx,2),name(idx));
end
15
16
17
18
19
20
figure(2);
gplot(connt, location dist, ':');
for idx = chosen idx
text(location dist(idx,1),location dist(idx,2),name(idx));
end
The plots are shown in the following Figure 1. It can be seen that the relative positions are roughly
correct in Figure 1a, where without knowing the actual distances some isolated positions (e.g., Hawaii and
Alaska) suffer from huge errors. In contrast, with the actual distances, Figure 1b displays a quite accurate
map of US cities.
3
6
Anchorage, Alaska
4
Boston, Mass.
Seattle, Wash.
Toronto, Ont., Can.
2
Minneapolis, Minn.
New York, N.Y.
Washington, D.C.
Chicago, Ill.
Honolulu, Hawaii
San Francisco, Calif.
San Jose, Calif.
0
Las
Vegas, Nev.
Los Angeles,
Calif.
Denver, Colo.
-2
Atlanta, Ga.
-4
Austin, Tex.
-6
-10
-8
-6
-4
-2
0
2
4
6
8
10
(a) MDS-MAP using connectivity graph
5
×10 6
4
Anchorage, Alaska
3
2
Boston, Mass.
Toronto, Ont., Can.
New York, N.Y.
1
Seattle, Wash.
Minneapolis, Minn.
Chicago, Ill.
Washington, D.C.
0
Denver, Colo.
Atlanta, Ga.
San
SanFrancisco,
Jose, Calif.Calif.
Las Vegas, Nev.
-1
Los Angeles, Calif.
Austin, Tex.
-2
-3
-10
Honolulu, Hawaii
-5
0
5
×10 6
(b) MDS-MAP using fully-connected distance graph
Figure 1. The plots of the maps returned by MDS-MAP using the connectivity graph
and the actual distance graph. Names of some cities are explicitly shown in the plots.
© Copyright 2026 Paperzz