%% 
load four_clusters

%%
gplot(A,xy,'.-');

%%
[x,r,hist,visited] = ppr_push_heap(A,0.85,1,1e-3);
gplot(A,xy,'.-');
hold on; scatter(xy(:,1),xy(:,2),100,x,'filled'); hold off;
%% Total number of edges looked at
hist(end,1)

%% Let's watch it
[~,~,histtrue,~] = ppr_push_heap(A,0.85,1,1e-3,100);
for steps=0:10 
    [x,r,hist,visited] = ppr_push_heap(A,0.85,1,1e-3,steps);
    ind=histtrue(steps+1,5); 
    gplot(A,xy,'.-');
    hold on; 
    scatter(xy(:,1),xy(:,2),100,x,'filled') ; 
    scatter(xy(:,1),xy(:,2)+10,100,r,'filled');
    plot(xy(ind,1),xy(ind,2)-10,'rx');
    colormap(1-bone);
    hold off;
    pause;
end

%% Now what happens when we use a queue?
[~,~,histtrue,~] = ppr_push_queue(normout(A),0.85,1,1e-3,100);
for steps=0:10
    [x,r,hist,visited] = ppr_push_queue(normout(A),0.85,1,1e-3,steps);
    ind=histtrue(steps+1,4); 
    gplot(A,xy,'.-');
    hold on; 
    scatter(xy(:,1),xy(:,2),100,x,'filled'); 
    scatter(xy(:,1),xy(:,2)+10,100,r,'filled');
    plot(xy(ind,1),xy(ind,2)-10,'rx');
    colormap(1-bone);
    hold off;
    pause;
end

%% Compare the algorithms
alpha=0.85; e1 = zeros(size(A,1),1); e1(1) = 1;
tols = [1e-2,1e-3,1e-4,1e-5,1e-6,1e-7];
xtrue = (speye(size(A,1))-alpha*normout(A)')\((1-alpha)*e1);
work = zeros(numel(tols),2);
visited = zeros(numel(tols),2);
for ti=1:numel(tols)
    [xh,rh,histh,visitedh] = ppr_push_heap(A,alpha,1,tols(ti),1e6);
    assert(all(rh<tols(ti)))
    [xq,rq,histq,visitedq] = ppr_push_queue(normout(A),alpha,1,tols(ti),1e6);
    assert(all(rq<tols(ti)))
    fprintf('Errors heap: %8.3e    queue: %8.3e\n', ...
        norm(xh-xtrue,1), norm(xq-xtrue,1));
    work(ti,1) = histh(end,1); visited(ti,1) = visitedh;
    work(ti,2) = histq(end,1); visited(ti,2) = visitedq;
end
%% Plot
loglog(tols,work(:,1),'r.-',tols,work(:,2),'k.-'); legend('heap','queue');
%% Plot visisted    
loglog(tols,visited(:,1),'r.-',tols,visited(:,2),'k.-'); legend('heap','queue');
