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});
ここでは連想配列が入れ子になっています。この連想配列gには5つの連想配列(1、2、3、4、5)が含まれています。外側の配列gでは、キー(1~5)と値(マップを定義する配列)の両方が重要です。内側の連想配列では、値は関係なく、 キーだけが意味を持っています。
図7.2 有向グラフの例
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 ) );
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 ) );
);
)
)
);