function [bdNode,bdEdge] = findboundary(elem)
% FINDBOUNDARY finds the boundary of a triangulation
%
%   [bdNode,bdEdge] = findboundary(elem) finds boundary nodes and edges for
%   a 2-dimensional mesh. Note only the topological structure of the mesh is
%   needed.
%
%   See also findboundary3

% Copyright (C) Long Chen. See COPYRIGHT.txt for details.

totalEdge = sort([elem(:,[2,3]); elem(:,[3,1]); elem(:,[1,2])],2);
[i,j,s] = find(sparse(totalEdge(:,1),totalEdge(:,2),1));
bdEdge = [i(s==1),j(s==1)];
isBdNode = false(max(bdEdge(:)),1); 
isBdNode(bdEdge) = true;
bdNode = find(isBdNode);