Commit 3a339480 authored by Argyris Kalogeratos's avatar Argyris Kalogeratos
Browse files

minor code improvements

parent cae19c5c
Loading
Loading
Loading
Loading
+15 −9
Original line number Diff line number Diff line
@@ -40,17 +40,23 @@ rngInfo = rng;
    
    % a. Generate graph
        graphPars.N         = 100;           % the size of graph to generate
        graphPars.graphType = 'erdosRenyi';    % available: 'erdosRenyi', 'preferentialAttachment', 'smallWorld', 'adjacencyMatrix'
        graphPars.graphType = 'preferentialAttachment';  % available: 'erdosRenyi', 'preferentialAttachment', 'smallWorld', 'adjacencyMatrix'
        
        if (strcmp(graphPars.graphType, 'erdosRenyi') == 1)
            graphPars.prob_arete_graph = 0.1;  % only for erdosRenyiGraph() generation
        switch graphPars.graphType,  % specific parameters needed for some random graph models
        case 'erdosRenyi'
            graphPars.edge_prob = 0.1;
        case 'preferentialAttachment'
            graphPars.edge_num = 5;
            graphPars.delta = 0.01;
        case 'smallWorld'
            graphPars.edge_num  = 5;  
            graphPars.edge_prob = 0.05;                
        end 
        
    % b. Use an input graph
    %  - enter 'adjacencyMatrix' in graphPars.graphType
    %  - include the following:
    %  - load or create your graph G 
        %    G = erdosRenyiGraph(graphPars.N, graphPars.prob_arete_graph);   % this is just an example
    %  - load your graph G or create it include the following:
        %    G = erdosRenyiGraph(graphPars.N, graphPars.edge_prob);   % this is just an example
        %    graphPars.adjacencyMatrix = G;
        %    graphPars.N = size(G,1);

+2 −2
Original line number Diff line number Diff line
@@ -18,8 +18,8 @@
function showMeasures (fRresults, varargin)

methodNames = fieldnames(fRresults);
numMeasures = length(varargin);
numMethods  = length(methodNames);
numMeasures = numel(varargin);
numMethods  = numel(methodNames);

measures = cell(numMeasures, 1);
for j=1:numMeasures
+6 −6
Original line number Diff line number Diff line
%erdosRenyiGraph - A generator for Erdos Renyi graphs.
%
% function G = erdosRenyiGraph (N, p)
% function G = erdosRenyiGraph (N, edge_num)
%
% Input:
%   - N: the size of the graph to generate.
%   - p: the probability of creating each edge (i,j).
%   - edge_prob: the probability of creating each edge (i,j).
%
% Output:
%   - G: the generated graph.
@@ -17,14 +17,14 @@
% Copyright (c) by Argyris Kalogeratos and Kevin Scaman, 2015.
%---

function G = erdosRenyiGraph (N, p)
function G = erdosRenyiGraph (N, edge_num)

G = rand(N);
G = G + G';
if (p <= 0.5)
    G = G < sqrt(2 * p);
if (edge_num <= 0.5)
    G = G < sqrt(2 * edge_num);
else
    G = G >= sqrt(2 * (1 - p));
    G = G >= sqrt(2 * (1 - edge_num));
end
G = G - diag(diag(G));
G = sparse(G);
+5 −5
Original line number Diff line number Diff line
%preferentialAttachmentGraph - A generator for Preferential Attachment graphs.
%
% function G = preferentialAttachmentGraph (N, M, delta)
% function G = preferentialAttachmentGraph (N, num_edges, delta)
%
% Input:
%   - N: the size of the graph to generate.
%   - M: is the number of esges to generate.
%   - num_edges: is the number of edges to generate.
%   - delta: is an extra additive weight that increases the probability of 
%     creating an edge, for each node.
%
@@ -19,19 +19,19 @@
% Copyright (c) by Argyris Kalogeratos and Kevin Scaman, 2015.
%---

function G = preferentialAttachmentGraph (N, M, delta)
function G = preferentialAttachmentGraph (N, num_edges, delta)

if (nargin < 3)
    delta = 0;
end

G = spalloc(N, N, N*M);
G = spalloc(N, N, N*num_edges);
G(1, 2) = 1;
G(2, 1) = 1;

for i = 3:N
    numNeighbors = full(sum(G,1));
    newNeighbors = randp(max(0, numNeighbors + delta), [M 1]);
    newNeighbors = randp(max(0, numNeighbors + delta), [num_edges 1]);
    G(i, newNeighbors) = 1;
    G(newNeighbors, i) = 1;
end
+9 −9
Original line number Diff line number Diff line
%smallWorldGraph - A generator for Small World graphs.
%
% function G = smallWorldGraph (N, k, p)
% function G = smallWorldGraph (N, edge_num, edge_prob)
%
% Input:
%   - N: the size of the graph to generate.
%   - k: the size of the neighbors to connect each node (it assumes that 
%   - edge_num: the size of the neighbors to connect each node (it assumes that 
%     each node is located on the periphery of a ring, then k is the number 
%     of nearest neighbors to connect with each node)
%   - p: the probability of creating each edge (i,j) of the Erdos Renyi model.
%   - edge_prob: the probability of creating each edge (i,j) of the Erdos Renyi model.
%     the generator makes an OR between the graph generated with the ring
%     layout and an Erdos Renyi graph.
%
@@ -22,18 +22,18 @@
% Copyright (c) by Argyris Kalogeratos and Kevin Scaman, 2015.
%---

function G = smallWorldGraph (N, k, p)
function G = smallWorldGraph (N, edge_num, edge_prob)

G = zeros(N,N);
for i = 1:k
for i = 1:edge_num
    G = G + diag(ones(N - i, 1), i);
end

G = G + G';
G = max(G, erdosRenyiGraph(N, p));
p = randperm(N);
G = G(p,:);
G = G(:,p);
G = max(G, erdosRenyiGraph(N, edge_prob));
edge_prob = randperm(N);
G = G(edge_prob,:);
G = G(:,edge_prob);
G = sparse(G);

end
Loading