-
Notifications
You must be signed in to change notification settings - Fork 1
/
random_graph_generator.rb
88 lines (75 loc) · 3.12 KB
/
random_graph_generator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class Graph
def initialize(properties, types, relationships)
@graph = {}
@node_lists = {}
@types = types
@relationships = relationships
@node_count = properties[:node_count]
@max_edge_count = properties[:max_edge_count]
@source_vertex = 0
@total_edge_count = 0
generate_node_types && generate_edges
end
def print_edge_counts
total_count = 0
@node_count.times do |current_node|
puts "#{total_count} #{@graph[current_node][:connections].size} #{@graph[current_node][:type]}"
total_count += @graph[current_node][:connections].size
end
end
def print_edges
@node_count.times do |current_node|
@graph[current_node][:connections].each do |destination|
puts "#{destination} #{rand(10)}" #{@graph[current_node][:type]}=>#{@graph[destination][:type]}"
end
end
end
def print_node_count
p(@node_count)
end
def print_source_vertex
p(@source_vertex)
end
def print_total_edge_count
p(@total_edge_count)
end
private
def generate_node_types
@node_count.times do |current_node|
@graph[current_node] = {}
type = @types.sample
@graph[current_node][:type] = type
@node_lists[type] ||= []
@node_lists[type] << current_node
end
end
def generate_edges
@node_count.times do |current_node|
edge_count_for_current_node = 1 + rand(@max_edge_count - 1)
@graph[current_node][:connections] = []
generate_edges_for(current_node, edge_count_for_current_node) unless @relationships[@graph[current_node][:type]].nil?
end
end
def generate_edges_for(current_node, count)
p @node_lists
count.times do
relationship = @relationships[@graph[current_node][:type]].sample
destination_node = @node_lists[relationship].sample
p relationship
p destination_node
while(@graph[current_node][:connections].include?(destination_node) || destination_node == current_node) do
destination_node = @node_lists[relationship].sample
end
@graph[current_node][:connections] << destination_node
@total_edge_count += count
end
end
end
g = Graph.new({:node_count => 10000000, :max_edge_count => 100}, ["person", "restaurant"], {"person" => ["restaurant", "person"]} )
g.print_node_count
g.print_edge_counts
puts
g.print_source_vertex
puts
g.print_total_edge_count
g.print_edges