Skip to content

pathbetweennodes starting from the far right #6

Open
@eragonngo

Description

@eragonngo

Dear Miss Kearney,

I noticed that you use Depth-first search and print out the node in visited from the furthest left side of the branch. I attempted to rewrite the program to make sure it visit the furthest right side of the branch but fail. I wonder if you can take a look at my code for this? I replaced next with next_2 to make sure it is reversed to the far right, but visited is still wrong after 3,4 if loop. My code is as:

clearvars;
tic
load('sparsegraph_test');
%verbose = false;
adj=SparseGraph;

src=1;
snk=length(MatrixGraph);
n = size(adj,1);
stack = src;

stop = false;

pth = cell(0);
cycles = cell(0);

next = cell(n,1);
next_2 = next;

for in = 1:n
    next{in} = find(adj(in,:));
end

for in = 1:n
    next_2{in} = flip(find(adj(in,:)),2);
end

for i = 1:n
    list(i,1)=length(next{i});
end

next_cache = next_2;

for i =1:n
    if list(i,1) == 2 && list (i+1,1) ~= 2
        for j=i+1:n
            if list(j,1) == 2 
                lala = j;
                break
            end
        end
        momo = lala-i+1;
        momo_2 = floor(momo/2);
        mod_momo = rem(momo,2);
        if momo == 0
            for in = 1:momo_2
                next_2(i+in-1) = next_cache(lala-in+1);
                next_2(lala-in+1) = next_cache(i+in-1);   
            end
        else
            for in = 1:momo_2
                next_2(i+in-1) = next_cache(lala-in+1);
                next_2(lala-in+1) = next_cache(i+in-1); 
            end
        end
    end
end
 %}                                                      
visited = cell(0);

pred = src;
for i=1:3 %Replace while 1 so I can see what happened with visited 
    visited = [visited; sprintf('%d,', stack)];
    [stack, pred] = addnode(stack, next_2, visited,pred);
    %if verbose
    %    fprintf('%2d ', stack);
    %    fprintf('\n');
    %end
    
    if isempty(stack)
        break;
    end
    
    if stack(end) == snk
        pth = [pth; {stack}];
        visited = [visited; sprintf('%d,', stack)];
        stack = popnode(stack);
    elseif length(unique(stack)) < length(stack)
        cycles = [cycles; {stack}];
        visited = [visited; sprintf('%d,', stack)];
        stack = popnode(stack);  
    end

end

edgval = cell(size(pth));

for ip = 1:length(pth)
    eidx = sub2ind(size(MatrixGraph), pth{ip}(1:end-1), pth{ip}(2:end));
    edgval{ip} = full(MatrixGraph(eidx));
    % Note: dense sparse matrices are a waste of memory, hence we convert back to full here
end

path_index=cell2mat(pth);
    
wtime = toc;
fprintf ( 1, 'Program took %f seconds to run.\n', wtime );    
    
function [stack, pred] = addnode(stack, next_2, visited, pred)

newnode = setdiff(next_2{stack(end)}, pred);
possible = arrayfun(@(x) sprintf('%d,',[stack x]), newnode, 'uni', 0);

isnew = ~ismember(possible, visited);

if any(isnew)
    idx = find(isnew, 1);
    stack = str2num(possible{idx});
    pred = stack(end-1);
else
    [stack, pred] = popnode(stack);
end
end

function [stack, pred] = popnode(stack)

stack = stack(1:end-1);
if length(stack) > 1
    pred = stack(end-1);
else
    pred = [];
end
end
%}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions