38
|
1 def create_nodes(node_num)
|
|
2 (0..node_num - 1).map { |i|
|
|
3 i = "node" + i.to_s
|
|
4 }
|
|
5 end
|
|
6
|
|
7 def print_dot(connections)
|
|
8 puts "digraph test {"
|
|
9 connections.each { |connection|
|
|
10 print "\t"
|
|
11 print connection[0]
|
|
12 print " -> "
|
|
13 print connection[1]
|
|
14 print ' [label="' + connection[2] + '"]'
|
|
15 puts
|
|
16 }
|
|
17 puts "}"
|
|
18 end
|
|
19
|
|
20 node_num = ARGV[0].to_i
|
|
21 child_num = ARGV[1].to_i
|
|
22 nodes = create_nodes(node_num)
|
|
23 connections = Array.new
|
|
24 nodes.each_with_index { |node, i|
|
|
25 child1 = child_num * i + 1
|
|
26 if child1 >= node_num then
|
|
27 nodes[i] = "cli" + i.to_s
|
|
28 end
|
|
29 }
|
|
30 nodes.each_with_index { |node, i|
|
|
31 parent = (i - 1) / child_num;
|
|
32 child = Array.new
|
|
33 for n in 1..child_num
|
|
34 child[n] = child_num * i + n
|
|
35 end
|
|
36
|
|
37 if parent >= 0 then
|
|
38 connections << [nodes[i], nodes[parent], "parent"]
|
|
39 end
|
|
40 for n in 1..child_num
|
|
41 if child[n] < node_num then
|
|
42 connections << [nodes[i], nodes[child[n]], "child"+(n-1).to_s]
|
|
43 end
|
|
44 end
|
|
45 }
|
|
46 print_dot(connections)
|
|
47
|