clc;
clear all;

max_queue_size = 200000;
graph_size = 16384;
%fileName = 'C:\\MissingNodes\\DataSets\\Facebook\\uni-socialgraph-anonymized.txt';
max_facebook_node = 957359;
fileName = 'd:\\MissingNodes\\DataSets\\Facebook\\mhrw-socialgraph-anonymized.txt';

for iter = 1 : 10
    
    bfs_start_node = floor(rand(1) * max_facebook_node); %some node from the mhrw dataset
    %bfs_start_node =21632297; %most connected node in the uni dataset
    
    %bfs_start_node =72261595;
    
    curr_node = bfs_start_node;
    queue = [];
    developed_nodes = [];
    graph = [];
    curr_graph_size = 0;
    while curr_graph_size < graph_size
        
        if curr_graph_size == 1024 || curr_graph_size==2048 || curr_graph_size==4096 || curr_graph_size==8192
            data = BuildFacebookData(graph);
            
            save(sprintf('facebook_sparse_%d_%d', curr_graph_size, iter))
            
            clear data;
        end
        
        %fprintf('Current Node: %d\n', curr_node);
        developed_nodes = [developed_nodes, curr_node];
        curr_neighbors = FindFacebookNeighbors_binary(fileName, curr_node);
        if curr_neighbors ~= -1
            curr_neighbors = curr_neighbors(curr_neighbors <= max_facebook_node);
            for j = 1 : size(curr_neighbors,1)
                %queue = union(curr_neighbors(j,:), queue); %add the new nodes to the queue but make it exclusive (no repetitions)
                queue = [queue, setdiff(curr_neighbors(j,:),queue)];
                %queue = [queue, curr_neighbors(j,:)]; %BFS
                if size(queue,2) > max_queue_size
                    queue = queue(1:max_queue_size);
                end
                curr_graph_size = curr_graph_size+1;
                graph{curr_graph_size} = curr_neighbors;
                
            end
        end
        %queue = setdiff(queue, developed_nodes);  %remove nodes that were already developed from the queue - also sorts the queue
        
        
        
        fprintf('graph size: %d\n' , size(graph,2));
        fprintf('queue size: %d\n', size(queue,2));
        if size(queue, 1) == 0
            fprintf('queue is empty');
            break;
        end
        while ismember(curr_node, developed_nodes)
            curr_node = queue(1);
            queue(1) = [];
        end
        %curr_node = queue(ceil(rand(1) * size(queue,2))); %select a random node from the queue
        
    end
    
    clear queue;
    
    pack
    
    data = BuildFacebookData(graph);
    
    save(sprintf('facebook_sparse_%d_%d', graph_size, iter))
    
    clear data;
    
    pack
    
end


