演算法基礎 第5版 第四章 最小生成樹Prim方法的Ruby實現

reoh發表於2021-01-23

演算法基礎 第5版

第四章 最小生成樹Prim方法

GitHub

def prim(array_w)
  array_f  = []
  nearest  = []
  distance = []
  n = array_w.length

  (0..n - 1).each do |i|
    nearest[i]  = 1
    distance[i] = array_w[0][i]
  end

  (n - 1).times do
    min   = Float::INFINITY
    vnear = nil
    (1..n - 1).each do |i|
      if distance[i] > 0 && distance[i] < min
        min   = distance[i]
        vnear = i
      end
    end

    e = "v#{nearest[vnear]} -> v#{vnear + 1}"
    array_f << e
    distance[vnear] = -1

    (1..n - 1).each do |i|
      if distance[i] > array_w[i][vnear]
        distance[i] = array_w[i][vnear]
        nearest[i]  = vnear + 1
      end
    end
  end

  array_f
end

infinite = Float::INFINITY
adjacency_matrix = [[0, 1, 3, infinite, infinite], [1, 0, 3, 6, infinite], [3, 3, 0, 4, 2], [infinite, 6, 4, 0, 5], [infinite, infinite, 2, 5, 0]] # 連通加權無向圖的鄰接矩陣
p prim(adjacency_matrix)

同時我也在使用Ruby完成書中的演算法樣例,期待大家多多關注,多挑毛病。

相關文章