%% Limit Number of Paths Returned
% Use the 'MaxNumPaths', 'MaxPathLength', and 'MinPathLength' options to limit the number of paths returned by allpaths.
%
% Create an adjacency matrix for a complete graph with 20 nodes. Create an undirected graph from the adjacency matrix, omitting self-loops.

A = ones(20);
G = graph(A,'omitselfloops');

%
% Since all of the nodes in the graph are connected to all other nodes, there are a large number of paths in the graph between any two nodes (more than 1.7e16). Therefore, it is not feasible to calculate all of the paths between two nodes since the results will not fit in memory. Instead, calculate the first 10 paths from node 2 to node 5.

paths1 = allpaths(G,2,5,'MaxNumPaths',10)

%
% Now calculate the first 10 paths between node 2 and node 5 that have a path length less than or equal to 2.

paths2 = allpaths(G,2,5,'MaxNumPaths',10,'MaxPathLength',2)

%
% Finally, calculate the first 10 paths between node 2 and node 5 that have a path length greater than or equal to 3.

paths3 = allpaths(G,2,5,'MaxNumPaths',10,'MinPathLength',3)

%
% Copyright 2020 The MathWorks, Inc.
%