MATLAB: Preserving node names in a digraph

digraphgraphgraph theoryMATLABnodes

I am constructing a large digraph with between 10k-100k nodes, in which I want to add, delete, and merge nodes. The nodes represent objects with other externally-stored data, which are indexed numerically, so the nodeIDs must be preserved to reference properly to the related data.
Is there a way of preserving node ids in a graph, other than giving the nodes string names?
In the following code
from_node=[1 1 2 3 4 4 5 6 7 3];
to_node= [3 2 5 7 6 5 7 7 2 4];
weights=rand(size(from_node));
g=digraph(to_node, from_node, weights);
h=rmnode(g,2);
when you remove node 2, it will reorder the nodes and call some other node 2 unless you specify node names, which must be strings, as such:
from_node=[1 1 2 3 4 4 5 6 7 3];
to_node= [3 2 5 7 6 5 7 7 2 4];
weights=rand(size(from_node));
names = cellstr(string(1:7));
g=digraph(to_node, from_node, weights,names);
h=rmnode(g,findnode(g,num2str(2)));
This is fine for small graphs, but for very large graphs that must be modified, this is extremely memory-inefficient, since you are forced to store a giant table of strings, which is redundant to your node id names.
Moreover, in this case you will need to do a findnode search each time that involves converting the number to a string, which could also be costly if done many many times.
Therefore, I am wondering if there is a more efficient way of preserving node ids upon insertion/deletion than using the names?
Thanks!!

Best Answer

Convert the node numbers to base 2^16. char() the result. Use those as the strings. For node names that are no larger than 100k then this takes two characters (4 bytes) each (plus any overhead from cell arrays.)