%%%%%%%%%%%%%%%%%%%%
% fundamentals.m
% Matt Feenstra
%%%%%%%%%%%%%%%%%%%%
% This recursive function returns a cell array of routes that are the
% fundamental routes for a given transition matrix.
% Parameters: starting vertex (usually 1) or route of vertices, and
% the transition matrix
function all_routes = fundamentals(route, Tmatrix)
if(~exist('route') || ~exist('Tmatrix'))
error 'Incorrect syntax, fundamentals(starting node, transition matrix)';
end
all_routes = {};
[rows,cols] = size(Tmatrix);
vertex = route(end);
all_adjacencies = find(Tmatrix(:, vertex));
for adjacency = all_adjacencies'
% is this adjacency the first vertex in the route? if so then
% we're done with this route. Add route to cell array and exit.
if(route(1) == adjacency)
all_routes(end+1) = {route};
continue;
end
% is this adjacency some other part of the route already? if so
% then skip it
if(is_repeat(adjacency, route) || adjacency < route(1) )
continue;
else
% then its not repeated and adjacency is not in the route? get
% the routes from here and add those to the end of the cell array
all_routes_temp = fundamentals([route, adjacency], Tmatrix);
all_routes = [all_routes; all_routes_temp];
end
end
% has this vertex already been listed in this route?
function decision = is_repeat(vertex, my_route)
[rows,cols] = size(my_route);
decision = 0;
for i = 1:1:cols
if(vertex <= 0)
error('zero or negative vertex')
end
% are we repeating?
if(my_route(i) == vertex)
decision = 1;
break;
end
end
%%%%%% end of program
%%%%%%%%%%%%%%%%%%%%
% get_partitions.m
% Matt Feenstra
%%%%%%%%%%%%%%%%%%%%
function all_partitions = get_partitions(all_funds, graph)
all_partitions = {};
num_funds = length(all_funds);
for n = 1:num_funds
fun_cycle = all_funds{n};
% is fun_cycle in this graph?
% if not, continue to the next fun_cycle
if(fun_exist(fun_cycle, graph))
% if cycle exists in graph
recurs_graph = subtract_cycle_from_graph(fun_cycle, graph);
recurs_all_funds = all_funds(n:end);
% if not empty
if any(any((recurs_graph)))
% recurse_graph(:) to collapse
%if any(recurs_graph)
recurs_all_partitions = get_partitions(recurs_all_funds, recurs_graph);
all_partitions_temp = prefix_partitions_with_cycle(fun_cycle, recurs_all_partitions);
else
all_partitions_temp = {{fun_cycle}};
end
all_partitions = [all_partitions; all_partitions_temp];
end
end
% remove fundamental cycles prior to n from list
function sublist = subtract_previous_cycles(n, all_funds)
sublist = {};
len_all_funds = length(all_funds);
i = 0;
for j = n:1:len_all_funds
sublist(i+1) = all_funds(j);
i = i + 1;
end
sublist = sublist';
% does this fundamental cycle exist in the graph?
function proceed = fun_exist(fun_cycle, graph_matrix)
proceed = 1;
fun_cycle(end+1) = fun_cycle(1);
for ind = 1:length(fun_cycle) - 1
proceed = proceed*graph_matrix(fun_cycle(ind+1),fun_cycle(ind));
end
% remove the fundamental cycle from the graph
function graph = subtract_cycle_from_graph(fun_cycle, graph)
% we know its in here because fun_exist said so
cycle_len = length(fun_cycle);
for ind = 1:cycle_len
% then we are at the end of this cycle
if(ind == cycle_len)
graph(fun_cycle(1), fun_cycle(cycle_len)) = graph(fun_cycle(1), fun_cycle(cycle_len)) - 1;
else
graph(fun_cycle(ind+1), fun_cycle(ind)) = graph(fun_cycle(ind+1), fun_cycle(ind)) - 1;
end
end
% this function inserts this fundamental cycle at the beginning
% of each partition in the list
function all_partitions = prefix_partitions_with_cycle(cycle, all_partitions)
for ind = 1:length(all_partitions)
all_partitions{ind} = [cycle, all_partitions{ind}];
end
No comments:
Post a Comment