Performance problems with digraph structure
2 views (last 30 days)
Show older comments
Tobias Manroth
on 31 Oct 2016
Commented: Tobias Manroth
on 6 Dec 2016
Hello, I need to add and remove nodes to a directional graph structure in real-time. Unfortunately i've encountered massive performance problems using the digraph structure from Matlab - especially when adding or removing nodes/edges. Are there any alternatives or more adequate ways to archive my goles. I'm thankful for any help!
nClosest = 16;
numFrames = 30;
idx = knnsearch(featureTree, features, 'K', nClosest).';
data = frameData(idx,:);
%%420 frames per second
nodes = table(data, 'VariableNames', {'Data'});
%%350 frames per second
% Node 1 is START, node 2 is END
if counter < numFrames
lastIdx = counter*nClosest+2;
graph = addnode(graph, nodes);
else
lastIdx = numFrames*nClosest+2;
graph = addnode(graph, nodes);
graph = rmnode(graph, 3:nClosest+2); % never remove START or END
end
myNodes = graph.Nodes;
%%180 frames per second
if lastIdx == numFrames*nClosest+2
firstRowIdx = lastIdx-nClosest+1 : lastIdx;
secondRowIdx = lastIdx-nClosest*2+1 : lastIdx-nClosest;
firstRow = myNodes(firstRowIdx,:).Data;
secondRow = myNodes(secondRowIdx,:).Data;
[t,knnW] = knnsearch(firstRow, secondRow);
edge = secondRowIdx(t);
startNodeIdx = 1;
endNodeIdx = 2;
w = zeros(1,nClosest*3);
w(nClosest*2+1:end) = knnW;
from = 3:nClosest*3+2; %last row
from(nClosest+1:nClosest*2) = ones(1,nClosest)*startNodeIdx; % start
from(nClosest*2+1:end) = firstRowIdx; % first row
to = ones(1,nClosest*3)*endNodeIdx; %end
to(nClosest+1:nClosest*2) = firstRowIdx; % first row
to(nClosest*2+1:end) = edge; % second row
graph = addedge(graph, from, to, w);
end
%%108 frames per second
0 Comments
Accepted Answer
Christine Tobler
on 31 Oct 2016
Generally speaking, it's much cheaper to construct a graph once, given all the nodes and edges, than incrementally using addnode/addedge. From your description, I assume that you need to update the graph as shown above, then call some methods on the graph, then update it again. Otherwise, computing all nodes and edges before constructing the graph would be the way to go.
Could you run the profiler to find out where the bottleneck is - is it in addedge itself, or in addnode, or in constructing the inputs to these functions?
If the bottleneck is addedge, one thing to consider would be to use the adjacency graph constructor instead of updating the graph using addedge. You would be carrying around a sparse matrix M, where M(i,j) has the value of the weight of the edge (i, j), and constructing the graph from M whenever you update it. I'm not sure this would be faster, but it's something to try.
3 Comments
Christine Tobler
on 2 Nov 2016
What methods of graph/digraph are you calling in between the updates? I don't see an obvious way of speeding up the graph constructor, but for some methods there might be a workaround to updating the graph so often.
More Answers (0)
See Also
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!