g = Associative Array();
g[1] = Associative Array({1, 2, 4});
g[2] = Associative Array({1, 3});
g[3] = Associative Array({4, 5});
g[4] = Associative Array({4, 5});
g[5] = Associative Array({1, 2});
This is a two-level associative array. The associative array g contains five associative arrays (1, 2, 3, 4, and 5). In the containing array g, both the keys (1-5) and the values (the arrays that define the map) are important. In the inner associative arrays, the values do not matter. Only the keys are important.
Figure 6.2 Example of a Directed Graph
dfs = Function( {ref, node, visited},
{chnode, tmp},
Write( "\!NNode: ", node, ", ", ref[node] << Get Keys );
visited[node] = 1;
tmp = ref[node];
chnode = tmp << first;
While( !Is Missing( chnode ),
If( !visited[chnode],
visited = Recurse( ref, chnode, visited )
);
chnode = tmp << Next( chnode );
);
visited;
);
dfs( g, 2, J( N Items( g << Get Keys ), 1, 0 ) );
Node 2: {1, 3}
Node 1: {1, 2, 4}
Node 4: {4, 5}
Node 5: {1, 2}
Node 3: {4, 5}
[1, 1, 1, 1, 1]
New Window( "Directed Graph",
Graph Box(
Frame Size( 300, 300 ),
X Scale( -1.5, 1.5 ),
Y Scale( -1.5, 1.5 ),
Local( {n = N Items( g ), k = 2 * Pi() / n, r, i, pt, from, to,
edge, v, d},
Fill Color( "green" );
Pen Size( 3 );
r = 1 / (n + 2);
For( i = 1, i <= n, i++,
pt = Eval List( {Cos( k * i ), Sin( k * i )} );
edges = g[i];
For( edge = edges << First, !Is Empty( edge ),
edge = edges << Next( edge ),
to = Eval List( {Cos( k * edge ), Sin( k * edge )} );
If( i == edge,
Circle( Eval List( 1.2 * pt ), 0.9 * r ), // else
v = pt - to;
d = Sqrt( Sum( v * v ) );
{from, to} = Eval List(
{pt * (d - r) / d + to * r / d, pt * r / d + to *
(d - r) / d}
);
Arrow( from, to );
);
);
Circle( pt, r, "fill" );
Text( Center Justified, pt - {0, 0.05}, Char( i ) );
);
)
)
);