I need a graph of at least two distinct layers of nodes (vertices), connected by edges (arcs). The graph may not contain any loops. Therefore I select a number of bottom layer nodes -- the rest is going to be non-bottom layer nodes. For the ease of addressing both kinds of nodes, let's say, the bottom layer nodes have IDs 1..x, the non-bottom layer nodes have ID x+1..n.
To avoid loops, each bottom layer node shall be connected to a non-bottom layer node, each non-bottom layer node shall be connected to a bottom layer node.
Though desirable, this does not imply the result of graph generation will be a single graph: The generation may result in several independent graphs. -- If you are aware of a better approach to get the graph generated, please let me know.
The connectivity of the graph might get improved by randomly adding some further edges. To avoid to cause loops here, the additional edges may be drawn only from non-bottom layer nodes to nodes with a smaller ID, including bottom layer nodes. -- Effectively, this adding of edges may cause the generated graph become multi-leveled.
An example for such a mini MOM net generator can be seen below. It's implemented in Pascal (using the GNU Pascal Compiler for compilation) and omits the connectivity increasing step. Though it provides a dump of the generated graph:
program MOMnetGeneration;
const
all_nodes = 50;
bottom_nodes = 2 * all_nodes DIV 5;
type
appropriate_int = shortint;
tEdges = array [1..all_nodes] of appropriate_int;
function rnd(min, max : appropriate_int) : appropriate_int;
begin
rnd := random(max - min) + min;
END;
var
edge : tEdges;
i : appropriate_int;
begin
{ connect bottom to non-bottom nodes: }
for i:=1 to bottom_nodes do
edge [i] := rnd(bottom_nodes+1, all_nodes);
{ connect non-bottom to bottom nodes: }
for i:=bottom_nodes+1 to all_nodes do
edge [i] := rnd(1, bottom_nodes);
{ dump the generated network: }
for i:=1 to all_nodes do
writeln(i, ' -> ', edge [i]);
end.
I compared the speed of Ruby and Pascal for creating 220 31-bit numbers, which took the Pascal compilate about 3 seconds and Ruby between one and two minutes. Therefore, I am back to copiled languages which work close to the hardware. Might try C/C++ as well.
Updates: none so far
No comments:
Post a Comment