Why my code is very slow?

2 ビュー (過去 30 日間)
huda nawaf
huda nawaf 2022 年 8 月 2 日
コメント済み: huda nawaf 2022 年 8 月 2 日
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.
Thanks in advance
function diff =IC_realdata1(node)
s1=xlsread('d:\retweet.csv');
s2=s1(1:1000,:);
uu=unique(s2(:,2));
% vec(:,1)=(1:length(uu));
% vec(:,2)=uu;
g=dlmread('d:\sample\rel1.txt');
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;
  2 件のコメント
Walter Roberson
Walter Roberson 2022 年 8 月 2 日
Do not expect people to analyze your code in detail to figure out what it is intended to do and then invent better ways of doing it. You should document the purpose of the code, and have comments about how the code achieves the purpose.
huda nawaf
huda nawaf 2022 年 8 月 2 日
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)
s1=xlsread('d:\retweet.csv'); %% network of twitter (include two colomns; tweet and retweet)
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;

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeSystems of Nonlinear Equations についてさらに検索

製品


リリース

R2013a

Community Treasure Hunt

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

Start Hunting!

Translated by