% Delivered to the Government in Accordance with Contract FA8650-10-C-7080
% 
%  (c) 2011 United Technologies Corp.
% Developed with the sponsorship of the Defense Advanced Research Projects Agency (DARPA).
%  
% Permission is hereby granted, free of charge, to any person obtaining a copy of this data, including any software or models in source or binary form, as well as any drawings, specifications, and documentation (collectively "the Data"), to deal in the Data without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Data, and to permit persons to whom the Data is furnished to do so, subject to the following conditions:
%  
% The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Data.
%  
% THE DATA IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, SPONSORS, DEVELOPERS, CONTRIBUTORS, OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE DATA OR THE USE OR OTHER DEALINGS IN THE DATA.
% 

% This function implements the Depth First Search Algorithm for Graphs
% It finds disconnected subgraphs of mechanical connections and identifies
% them as spools

% If vertex has not been searched the second column is set to 1
% If vertex is being searched the second column is set to 2
% IF vertex has been searched the second column is set to 3
% Initialize Vertex State vector in SSD_Autogen code

function [vertex_state, tree_structure] = DFS(current_vertex, tree_structure) 

% Global variables
global connections
global n_connections
global vertex_state
global spool
global n

% Check that you have not been there before 

    
    % Visit current vertex
    vertex_state(current_vertex,2)=3;
    tree_structure(spool,n) = current_vertex;

    for count = 1:n_connections
        
        % Check that the source of the connection is the current node
        
        if  connections(count,1) == current_vertex 
            n = n+1;
            tree_structure(spool, n) = connections(count, 2);
            vertex_state(connections(count,2),2)=3;
             
            % Recursive aspect of algorithm goes as deep as necessary
            [vertex_state, tree_structure] = DFS(connections(count, 2),tree_structure);
           
        end
        
        
    end
    
end