function PathExist = traverseStack(Vertex,binary,r,c)
% This function "traverses" the array to see if a connected path exists
% across the graph, utilizing the Stack data type from java
%
% INPUTS
% Vertex = The input vertex on the bottom side of the graph which we start
%          to look for a path from
% binary = r x c size lattice which details the pairs of adjacent indices 
%          that are connected
%      r = Number of horizontal rows in the rectangular lattice
%      c = Number of vertical columns in the rectangular lattice
%
% OUTPUTS
% PathExist = Index of the vertex if a path does exist, 0 if a path does
%             not exist
%
%TRUE/FALSE VARIABLES
%         TF = Whether or not the stack is empty
%  PondEmpty = Whether or not any neighbors were found to be Melt Ponds (1)
%SeaIceEmpty = Whether or not any neighbors were found to be Sea Ice (0)
%
% ===========================================================

PathExist = 0; %default value for PathExist, i.e. no path exists

%Include and define stacks from Java
stack = java.util.Stack;
node = Vertex;
stack.push(node); %Push the current vertex onto the stack

global Visited %call the global Visited variable

%Check if the stack is empty (1) or not (0)
TF = stack.empty();

%While there still exist elements in the stack, loop through the following
while TF == 0;
    
    %Take the element off the top of the stack
    Popped = stack.pop();
    
    %Check if the vertex has been visited already, and record it if it
    %hasn't been
    VisitTF = any(Visited) == Popped;
    if VisitTF == 1
        %Vertex has already been visited, do nothing
    else
        %Add the vertex to the list of visited vertices
        Visited = cat(2,Visited,Popped);
    end
    
    %If we have reached the last row, end, as a path has been found
    if Popped > c*(r-1)
        PathExist = Popped;
        break
    else
        %Have not reached the last row, do nothing
    end
    
    %Find the adjacent neighbors of the current vertex that are connected
    Neighbors = getLatNeigh(Popped,Visited,binary,r,c);
    tf = isempty(Neighbors);

    if tf == 1;
        %No neighbors detected, do nothing
    else
        %Push these new neighbors onto the stack and continue
        for n = 1:length(size(Neighbors))
            node = Neighbors(1,n);
            stack.push(node); %something is wrong with this (note from circa Nov 2015)
        end
    end

    %Check again if the stack is empty to continue while loop or not
    TF = stack.empty();
end

end
   
