function DescendantList = DescendantFinder(ChildrenList, vertex)
%DescendantList = DescendantFinder(ChildrenList, vertex)
%Input: ChildrenList: m + 1 column matrix (of integers) for an m-ary tree
%Output: ParentList: a two column matrix giving the parent of each vertex
[n s] = size(ChildrenList); %n = number of vertices
%Find all children of inputted vertex
vertexInd = find(ChildrenList(:,1) == vertex); %need to work with the index of vertex
%in ChildrenList matrix
Children = ChildrenList(vertexInd, find(ChildrenList(vertexInd,2:s)>0)+1);
DescendantList =Children;
while length(Children) > 0
%now form next generation of children
NewGeneration = []; %initialize
for i = 1:length(Children)
childInd = find(ChildrenList(:,1) == Children(i));
NewChildren = ChildrenList(childInd, find(ChildrenList(childInd,2:s)>0)+1);
NewGeneration = [NewGeneration NewChildren];
end
DescendantList =[DescendantList NewGeneration]; %append new children to descendant list
clear Children %done with past generation
Children = NewGeneration;
end