MATLAB: Generating a random directed graph (network) with some properties, efficiently

directed graphgenerate graphgraphgraphsNetworknetworks

Hi,
Im currently building a code to simulate the push/relabel max-flow algorithm.
Therefor, in order to make the simulation more entertaining and also to check myself, I need a graph generator.
The graph need to have certain properties:
-directed
-no self loops (ie no (v,v) edges)
-relatively sparse
-connected
the sparse + connected part is the problematic part for me. If anybody has any idea how to do this efficiently please share with me.
Thanks, Dan

Best Answer

temp = randi(NumberOfNodes, NumberOfVertices, 2);
temp(diff(temp,[],2)==0, :) = []; %remove self-loops
s = temp(:,1); t = temp(:,2);

which you can then digraph()

Note: this might not use all of the nodes. randperm() can help use all of the nodes.