Why my code is very slow?

5 views (last 30 days)
huda nawaf on 2 Aug 2022
Commented: huda nawaf on 2 Aug 2022
hello,
Can anybody optimize my code to be faster.
It is very slowly, in addition to that the network I used is very large.
I badly need to optimize it. My code include whil and several for that is why it is slow, and I does not know how upgrade it.
function diff =IC_realdata1(node)
s2=s1(1:1000,:);
uu=unique(s2(:,2));
% vec(:,1)=(1:length(uu));
% vec(:,2)=uu;
g=g(1:1000,:);
a0=node;%randi(length(uu),1);
T=false;
a1=a0;
b=a0;neb2=[];
while ~ T
neb1=[];
for i=1:length(b)
y=g(b(i),:);
nb=y(y~=0);
neb=[];
kk=1;
for j=1:length(nb)
idx=find(uu==nb(j));%% to be sure if distination within source
if ~isempty(idx)
neb(kk)=idx; %vec(idx,1);
kk=kk+1;
end
end
if ~isempty(neb)
neb1=[neb1 neb];
for k=1:length(neb)
r=ismember (neb(k),a1);
if r==1
neb1(neb1==neb(k))=[];
end
end
neb2= [neb2 neb1];
else
neb2=[];
end
end
neb=neb2;
neb=unique(neb);
for j=1:length(a0)
neb(neb==a0(j))=[];
end
b=neb1;
a1=union(a1,b);
if isempty(b)
T= true;
end
end
diff=a1;
huda nawaf on 2 Aug 2022
Thanks for notes
The purpose of the code is to find the cascade of each node using the independent cascade model. It means when inputting the id of a node that tweeted the function must return all infected (interact) nodes with that tweet
function diff =IC_realdata1(node) %%% the input of the function is the id of node(seed)
uu=unique(s2(:,2)); %%% extract only the source node ( who tweeted )
g=dlmread('d:\sample\rel1.txt'); %%% read the relations file ( each node with their relations, I created it from s1)
a0=node;%% initial node (active or seed node), Sometimes a0 contains more than one node (for simplicity we make it one )
T=false; %% condition
a1=a0; %%% a1 is the vector save all infected nodes including the initial one
b=a0;neb2=[];
while ~ T %%% the code continue in execution as long as b is not empty( b is a vector including the current nodes that not have been processed yet)
neb1=[];
for i=1:length(b)
y=g(b(i),:); %% extract the neighbors (relations in g)of current node
nb=y(y~=0); %%
neb=[];
kk=1;
for j=1:length(nb) %% for each neighbor
idx=find(uu==nb(j));%% extract the index of each neighbor in uu
if ~isempty(idx) %% to be sure if the neighbor within source nodes or not
neb(kk)=idx; % %% if it is, it should save it in neb, then track each one and their relations
kk=kk+1;
end
end
if ~isempty(neb)
neb1=[neb1 neb]; %%% we need a buffer to save the nodes in neb because neb will subject to change
for k=1:length(neb)
r=ismember (neb(k),a1); %%to check if any node in neb within a1
if r==1
neb1(neb1==neb(k))=[]; %%if it is, it should remove it from neb1 ( we need neb1 include the new infected nodes) infected nodes)
end
end
neb2= [neb2 neb1]; %%we need another buffer neb2
else %% if neb is empty
neb2=[]; %% it should make neb2 is empty
end
end (end if (neb));
%%%% after finish for (b), it means complete all nodes in it
neb=neb2; %% make ned= neb2 whether neb2 is empty or has nodes
neb=unique(neb);
for j=1:length(a0)
neb(neb==a0(j))=[]; %% remove from neb the initial node(s) that exists at the same time in a0
end
b=neb1; %% to make b include the current nodes that have not been processed yet
a1=union(a1,b); %% we unify a1 with b, because a1 has to include all infected nodes
if isempty(b) %% if b is empty that means there are no any longer infected nodes, then while will stop
T= true;
end
end (end of while)
diff=a1;

Categories

Find more on Problem-Based Optimization Setup in Help Center and File Exchange

R2013a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!