%% Nodes and Edges in Fundamental Cycles
% Compute the nodes and edges in the fundamental cycles of a graph, visualize the fundamental cycles, and then use the fundamental cycles to find other cycles in the graph.
%
% Create and plot an undirected graph.

s = [1 1 1 2 2 3 3 4 4 4 5 6];
t = [2 3 4 4 5 4 6 5 6 7 7 7];
G = graph(s,t);
plot(G);

%
% Compute the fundamental cycle basis of the graph.

[cycles,edgecycles] = cyclebasis(G)

%
% Highlight each of the fundamental cycles, using tiledlayout and nexttile to construct an array of subplots. For each subplot, first get the nodes of the corresponding cycle from cycles, and the edges from edgecycles. Then, plot the graph and highlight those nodes and edges.

tiledlayout flow
for k = 1:length(cycles)
    nexttile
    highlight(plot(G),cycles{k},'Edges',edgecycles{k},'EdgeColor','r','NodeColor','r')
    title("Cycle " + k)
end

%
% You can construct any other cycle in the graph by finding the symmetric difference between two or more fundamental cycles using the setxor function. For example, take the symmetric difference between the first two cycles and plot the resulting new cycle.

figure
new_cycle_edges = setxor(edgecycles{1},edgecycles{2});
highlight(plot(G),'Edges',new_cycle_edges,'EdgeColor','r','NodeColor','r')

% While every cycle can be constructed by combining cycles from the cycle basis, not every combination of basis cycles forms a valid cycle.
%
% Copyright 2020 The MathWorks, Inc.