Permalink
Browse files

Added documentation to Dijkstra

  • Loading branch information...
1 parent fb50365 commit 462be0c8e5e6d47e75d2d196c24aa702dce9aa39 David committed May 16, 2009
Showing with 50 additions and 4 deletions.
  1. +3 −3 Contents.m
  2. +33 −1 dijkstra.m
  3. +14 −0 test/test_examples.m
View
@@ -41,9 +41,9 @@
% TODO for release
% Write demo code
% Fix mlintrpt errors
-% Update copyright info
-% Examples for mst_prim, dijkstra, dirclustercoeffs, dfs
-% Documentation for Dijkstra, dirclustercoeffs
+% Update copyright info everywhere
+% Examples for dirclustercoeffs
+% Documentation for dirclustercoeffs
% Implement bipartite matching
% Future todos
View
@@ -1,10 +1,42 @@
function [d pred]=dijkstra(A,u)
+% DIJKSTRA Compute shortest paths using Dijkstra's algorithm
+%
+% d=dijkstra(A,u) computes the shortest path from vertex u to all nodes
+% reachable from vertex u using Dijkstra's algorithm for the problem.
+% The graph is given by the weighted sparse matrix A, where A(i,j) is
+% the distance between vertex i and j. In the output vector d,
+% the entry d(v) is the minimum distance between vertex u and vertex v.
+% A vertex w unreachable from u has d(w)=Inf.
+%
+% [d pred]=dijkstra(A,u) also returns the predecessor tree to generate
+% the actual shorest paths. In the predecessor tree pred(v) is the
+% vertex preceeding v in the shortest path and pred(u)=0. Any
+% unreachable vertex has pred(w)=0 as well.
+%
+% If your network is unweighted, then use bfs instead.
+%
+% See also BFS
+%
+% Example:
+% % Find the minimum travel time between Los Angeles (LAX) and
+% % Rochester Minnesota (RST).
+% load_gaimc_graph('airports')
+% A = -A; % fix funny encoding of airport data
+% lax=247; rst=355;
+% [d pred] = dijkstra(A,lax);
+% fprintf('Minimum time: %g\n',d(rst));
+% % Print the path
+% fprintf('Path:\n');
+% path =[]; u = rst; while (u ~= lax) path=[u path]; u=pred(u); end
+% fprintf('%s',labels{lax});
+% for i=path; fprintf(' --> %s', labels{i}); end, fprintf('\n');
% David Gleich
-% Copyright, Stanford University, 2008
+% Copyright, Stanford University, 2008-2009
% History
% 2008-04-09: Initial coding
+% 2009-05-15: Documentation
if isstruct(A), rp=A.rp; ci=A.ci; ai=A.ai; check=0;
else [rp ci ai]=sparse_to_csr(A); check=1;
View
@@ -6,6 +6,20 @@
load_gaimc_graph('dfs_example.mat') % use the dfs example from Boost
d = dfs(A,1)
+%% dijkstra
+% Find the minimum travel time between Los Angeles (LAX) and
+% Rochester Minnesota (RST).
+load_gaimc_graph('airports')
+A = -A; % fix funny encoding of airport data
+lax=247; rst=355;
+[d pred] = dijkstra(A,lax);
+fprintf('Minimum time: %g\n',d(rst));
+% Print the path
+fprintf('Path:\n');
+path =[]; u = rst; while (u ~= lax) path=[u path]; u=pred(u); end
+fprintf('%s',labels{lax});
+for i=path; fprintf(' --> %s', labels{i}); end, fprintf('\n');
+
%% mst_prim
load_gaimc_graph('airports'); % A(i,j) = negative travel time
A = -A; % convert to travel time.

0 comments on commit 462be0c

Please sign in to comment.