lib/math/discrete/graph/algorithms.rb
# A minimum spanning tree (MST) is a subset of the edges of a connected, edge-weighted undirected graph that
# connects all the vertices together, without any cycles and with the minimum possible total edge weight.
# That is, it is a spanning tree whose sum of edge weights is as small as possible.
# Read more:
# https://en.wikipedia.org/wiki/Minimum_spanning_tree
A minimum spanning tree (MST) is a subset of the edges of a connected, edge-weighted undirected graph that
connects all the vertices together, without any cycles and with the minimum possible total edge weight.
That is, it is a spanning tree whose sum of edge weights is as small as possible.
A minimum spanning tree ( MST ) is a subset of the edges of a connected edge - weighted undirected graph that connects all the vertices together without any cycles and with the minimum possible total edge weight . That is it is a spanning tree whose sum of edge weights is as small as possible .
def minimum_spanning_tree
    minimum_edge_set = Set[]
    union_set = UnionFind::UnionFind.new vertex_set
    edge_set.sort_by(&:weight).each do |edge|
      unless union_set.connected?(edge.from, edge.to)
        minimum_edge_set.add edge
        union_set.union(edge.from, edge.to)
      end
    end
    Graph[vertex_set, minimum_edge_set]
  end
def minimum_spanning_tree minimum_edge_set = Set [ ] union_set = UnionFind :: UnionFind . new vertex_set edge_set . sort_by ( & :weight ) . each do | edge | unless union_set . connected? ( edge . from , edge . to ) minimum_edge_set . add edge union_set . union ( edge . from , edge . to ) end end Graph [ vertex_set , minimum_edge_set ] end
A minimum spanning tree (MST) is a subset of the edges of a connected, edge-weighted undirected graph that
connects all the vertices together, without any cycles and with the minimum possible total edge weight.
A minimum spanning tree ( MST ) is a subset of the edges of a connected edge - weighted undirected graph that connects all the vertices together without any cycles and with the minimum possible total edge weight .