图中的Ruby连接组件

时间:2018-01-08 07:35:22

标签: ruby algorithm graph-theory

我已经编写了以下代码来查找ruby中图形的连通组件,但由于我没有对不相交的集合使用任何合理的数据结构,所以它很慢,因此find_set可能是为O(n ^ 2)

有人可以帮我改进吗?我对红宝石比较新。此代码旨在解决Here:

带来的问题

我的解决方案,用简单的英语,是找到给定图表中连接组件的数量,然后计算在每个节点中构建库的成本或在每个连接组件中构建库的成本加上为每个组件的最小生成树构建道路。

代码:

#!/bin/ruby

require 'set'

def roadsAndLibraries(n, c_lib, c_road, cities)
  vertices = make_set((1..n).to_a)

  #find connected components    
  cities.each do |a, b|
    first_set = find_set(vertices, a)
    second_set = find_set(vertices, b)
    if first_set != second_set
      vertices.add(first_set | second_set)
      vertices.delete(first_set)
      vertices.delete(second_set)
    end
  end

  #calculate the cost of building the edges required for a MST of each component
  roads_cost = c_road * vertices.to_a.map{|x| x.size - 1}.inject(:+)

  if c_road > c_lib
    return n * c_lib
  else
    return vertices.size * c_lib + roads_cost
  end

end


def make_set(arr_n)
  vertices = Set.new
  for i in 1..arr_n.size
    vertices.add([i])
  end
  vertices.map!{|x| x.to_set}
end

def find_set(set, node)
  set.find{|x| x.include?(node)}
end





# q is number of queries, n is the number of vertices in the graph 
q = gets.strip.to_i
for a0 in (0..q-1)
  n, m, c_lib, c_road = gets.strip.split(' ')
  n = n.to_i
  m = m.to_i
  c_lib = c_lib.to_i
  c_road = c_road.to_i
  cities = Array.new(m)
  for cities_i in (0..m-1)
    cities_t = gets.strip
    cities[cities_i] = cities_t.split(' ').map(&:to_i)
  end
  result = roadsAndLibraries(n, c_lib, c_road, cities)
  puts result
end

0 个答案:

没有答案