Permalink
Please sign in to comment.
Browse files
Merge branch 'master' of git@github.com:dgleich/gaimc
Conflicts: sparse_to_csr.m
- Loading branch information...
Showing
with
1,405 additions
and 22 deletions.
- +3 −0 .gitignore
- +19 −2 Contents.m
- +2 −0 bfs.m
- +5 −2 clustercoeffs.m
- +22 −0 convert_sparse.m
- +1 −1 corenums.m
- +124 −0 demo/airports.m
- +308 −0 demo/demo.m
- +2 −0 dfs.m
- +33 −1 dijkstra.m
- +26 −4 dirclustercoeffs.m
- +588 −0 graph_draw.m
- BIN graphs/airports.mat
- BIN graphs/bfs_example.mat
- BIN graphs/celegans.mat
- BIN graphs/dfs_example.mat
- +46 −0 largest_component.m
- +35 −0 load_gaimc_graph.m
- +24 −4 mst_prim.m
- +16 −5 scomponents.m
- +6 −3 sparse_to_csr.m
- +2 −0 test/test_bfs.m
- +75 −0 test/test_examples.m
- +33 −0 test/test_largest_component.m
- +31 −0 test/test_load_gaimc_graph.m
- +4 −0 test/test_main.m
| @@ -0,0 +1,3 @@ | ||
| +*~ | ||
| +*.asv | ||
| + |
21
Contents.m
| @@ -0,0 +1,22 @@ | ||
| +function As = convert_sparse(A) | ||
| +% CONVERT_SPARSE Convert a sparse matrix to the native gaimc representation | ||
| +% | ||
| +% As = convert_sparse(A) returns a struct with the three arrays defining | ||
| +% the compressed sparse row structure of A. | ||
| +% | ||
| +% Example: | ||
| +% load('graphs/all_shortest_paths_example') | ||
| +% As = convert_sparse(A) | ||
| +% | ||
| +% See also SPARSE_TO_CSR SPARSE | ||
| + | ||
| +% David Gleich | ||
| +% Copyright, Stanford University, 2008-2009 | ||
| + | ||
| +% History | ||
| +% 2009-04-29: Initial coding | ||
| + | ||
| +[rp ci ai] = sparse_to_csr(A); | ||
| +As.rp = rp; | ||
| +As.ci = ci; | ||
| +As.ai = ai; |
124
demo/airports.m
| @@ -0,0 +1,124 @@ | ||
| +%% The US airport network | ||
| +% THe North American airport network is an interesting graph to examine. | ||
| +% The source for this data was a file on Brendan Frey's affinity | ||
| +% propagation website. A(i,j) is the negative travel time between two | ||
| +% airports. Although the data didn't include the airport locations, I used | ||
| +% the Yahoo! Geocoding API to generate a latitude and longitude for each | ||
| +% airport. | ||
| + | ||
| +%% The data | ||
| +load_gaimc_graph('airports'); | ||
| +%% | ||
| +% Plot a histogram for all route time estimates | ||
| +[si, ti, rt] = find(A); | ||
| +hist(-rt,100); % times are stored as negative values | ||
| + | ||
| +%% | ||
| +% Find the lengthiest route | ||
| +[val,ind] = max(-rt) | ||
| +{labels{si(ind)} labels{ti(ind)}} | ||
| + | ||
| +%% | ||
| +% Some of the routes include stop overs, so it's probable that is what | ||
| +% we find in this case. | ||
| + | ||
| +%% Graph analysis: connected? | ||
| +% One of the first questions about any graph should be if it's connected or | ||
| +% not. | ||
| + | ||
| +max(scomponents(A)) | ||
| + | ||
| +%% | ||
| +% There is only one connected component, so the graph is connected. | ||
| + | ||
| +%% Distance instead of time | ||
| +% Let's see how the edges correlate distance with estimated travel time. | ||
| +[ai aj te] = find(A); | ||
| +de = distance(xy(ai,:), xy(aj,:)); | ||
| +plot(de,-te,'.'); | ||
| +xlabel('distance (arclength)'); ylabel('time (?)'); | ||
| + | ||
| +%% | ||
| +% Wow! It's all over the place, but there is a lower bound. Some of | ||
| +% these routes can include stop | ||
| + | ||
| +%% Minimum spanning tree | ||
| +% This section repeats and extends some analysis in the overall gaimc demo. | ||
| +% First, let's recompute the minimum spanning tree based on travel time. | ||
| +load_gaimc_graph('airports') | ||
| +A = -A; % we store the negative travel time | ||
| +A = max(A,A'); % travel time isn't symmetric | ||
| +T = mst_prim(A); | ||
| +clf; | ||
| +gplot(T,xy); | ||
| + | ||
| + | ||
| +% These next lines plot a map of the US with states colored. | ||
| +ax = worldmap('USA'); | ||
| +load coast | ||
| +geoshow(ax, lat, long,... | ||
| + 'DisplayType', 'polygon', 'FaceColor', [.45 .60 .30]) | ||
| +states = shaperead('usastatelo', 'UseGeoCoords', true); | ||
| +faceColors = makesymbolspec('Polygon',... | ||
| + {'INDEX', [1 numel(states)], 'FaceColor', polcmap(numel(states))}); | ||
| + geoshow(ax, states, 'DisplayType', 'polygon', 'SymbolSpec', faceColors) | ||
| +set(gcf,'Position', [ 52 234 929 702]); | ||
| +%% | ||
| +% That's the US, now we need to plot our data on top of it. | ||
| +[X,Y] = gplot(T,xy); % get the information to reproduce a gplot | ||
| +plotm(Y,X,'k.-','LineWidth',1.5); % plot the lines on the map | ||
| +%% | ||
| +% We need to clear the axes after the mapping toolbox | ||
| +clf; | ||
| +%% | ||
| +% Let's just look at the continential US too. | ||
| +figure; ax = usamap('conus'); | ||
| +states = shaperead('usastatelo', 'UseGeoCoords', true, 'Selector',... | ||
| + {@(name) ~any(strcmp(name,{'Alaska','Hawaii'})), 'Name'}); | ||
| +faceColors = makesymbolspec('Polygon',... | ||
| + {'INDEX', [1 numel(states)], 'FaceColor', polcmap(numel(states))}); | ||
| +geoshow(ax, states, 'DisplayType', 'polygon', 'SymbolSpec', faceColors) | ||
| +framem off; gridm off; mlabel off; plabel off | ||
| +set(gcf,'Position', [ 52 234 929 702]); | ||
| +plotm(Y,X,'k.-','LineWidth',1.5); % plot the lines on the map | ||
| +%% | ||
| +% One interesting aspect of this map is that major airline hubs (Chicago, | ||
| +% New York, etc. are not well represented. One possible explaination is | ||
| +% that they have larger delays than other regional airports. | ||
| +% | ||
| +% Clear the figure again before proceeding. | ||
| +clf; | ||
| + | ||
| +%% Honolulu to St. Johns? | ||
| +% Before, we saw that the lengthiest route was between St. John's and | ||
| +% Honolulu. But, does the network have a better path to follow between | ||
| +% these cities? Let's check using Dijkstra's shortest path algorithm! | ||
| + | ||
| +% Reload the network to restore it. | ||
| +load_gaimc_graph('airports') | ||
| +A = -A; | ||
| + | ||
| +% find the longest route again | ||
| +[si, ti, rt] = find(A); | ||
| +[val,ind] = max(rt); % we've already negated above, so no need to redo it | ||
| +start = si(ind); | ||
| +dest = ti(ind); | ||
| +[d pred] = dijkstra(A,start); % compute the distance to everywhere from St. Johns | ||
| +d(dest) | ||
| + | ||
| +%% | ||
| +% That value is considerably shorter than the direct time. How do we find | ||
| +% this awesome route? | ||
| +path =[]; u = dest; while (u ~= start) path=[u path]; u=pred(u); end | ||
| +fprintf('%s',labels{start}); | ||
| +for i=path; fprintf(' --> %s', labels{i}); end, fprintf('\n'); | ||
| + | ||
| +%% All pairs shortest paths | ||
| +% At this point, the right thing to do would be to recompute each edge in | ||
| +% the network using an all-pairs shortest path algorithm, and then look at | ||
| +% how distance correlates with time in that network. However, I haven't | ||
| +% had time to implement that algorithm yet. | ||
| + | ||
| +%% Conclusion | ||
| +% Hopefully, you will agree that network algorithms are a powerful way to | ||
| +% look at the relationships between airports! |
Oops, something went wrong.
0 comments on commit
9bccc10