annotate scripts/ring.rb @ 8:8a33595a0d8c

add tree.rb
author suruga
date Wed, 07 Feb 2018 19:31:33 +0900
parents 5d1ecfb72d1d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
1 def create_nodes(node_num)
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
2 (0..node_num - 1).map { |i|
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
3 i = "node" + i.to_s
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
4 }
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
5 end
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
6
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
7 def print_dot(connections)
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
8 puts "digraph test {"
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
9 connections.each { |connection|
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
10 print "\t"
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
11 print connection[0]
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
12 print " -> "
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
13 print connection[1]
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
14 print ' [label="' + connection[2] + '"]'
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
15 puts
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
16 }
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
17 puts "}"
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
18 end
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
19
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
20 node_num = ARGV[0].to_i
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
21 nodes = create_nodes(node_num)
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
22 connections = Array.new
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
23 nodes.each_with_index { |node, i|
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
24 connections << [nodes[i], nodes[(i + 1) % node_num], "right"]
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
25 connections << [nodes[i], nodes[i - 1], "left"]
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
26 }
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
27 print_dot(connections)
5d1ecfb72d1d add ring test for Alice
kono
parents:
diff changeset
28