
% find rooted spanning tree
% input 
% G = adjacency graph
% p = a vertex (integer index of a row of G)
% output
% tree1 = linear representation of tree
% tree2 = adjacency matrix of tree
% dist = graph distance of vertex k from v

function [tree1,dist]=find_spanning_tree(G,p)
    V=size(G,1);
    tree1=zeros(1,V);
    dist=-ones(1,V);
    dist(p)=0;
    for k=0:V-2
       leaves=find(dist==k);
       for j=leaves
	      adj=find((dist==-1)&G(j,:)>0);
	      dist(adj)=k+1;
	      tree1(adj)=j;
       end % for j
    end % for k
   return