Class: RGL::PrimAlgorithm

### Home Classes Methods

### In Files

- lib/rgl/prim.rb

### Parent

Object

### Methods

- ::new
- #minimum_spanning_tree

### Class/Module Index

Quicksearch

- RGL
- RGL::AdjacencyGraph
- RGL::BFSIterator
- RGL::BellmanFordAlgorithm
- RGL::BellmanFordVisitor
- RGL::BidirectionalGraph
- RGL::BipartiteBFSIterator
- RGL::DFSIterator
- RGL::DFSVisitor
- RGL::DOT
- RGL::DOT::Digraph
- RGL::DOT::DirectedEdge
- RGL::DOT::Edge
- RGL::DOT::Element
- RGL::DOT::Graph
- RGL::DOT::Node
- RGL::DOT::Port
- RGL::DOT::Subgraph
- RGL::DijkstraAlgorithm
- RGL::DijkstraVisitor
- RGL::DirectedAdjacencyGraph
- RGL::Edge
- RGL::Edge::DirectedEdge
- RGL::Edge::UnDirectedEdge
- RGL::EdgePropertiesMap
- RGL::EdmondsKarpAlgorithm
- RGL::EdmondsKarpAlgorithm::EdmondsKarpBFSIterator
- RGL::Graph
- RGL::Graph::TarjanSccVisitor
- RGL::GraphIterator
- RGL::GraphVisitor
- RGL::GraphVisitor::ClassMethods
- RGL::GraphVisitor::DistanceMapSupport
- RGL::ImplicitGraph
- RGL::MutableGraph
- RGL::MutableGraph::MutableGraphParser
- RGL::NoEdgeError
- RGL::NoVertexError
- RGL::NonNegativeEdgePropertiesMap
- RGL::NotDirectedError
- RGL::NotUndirectedError
- RGL::PrimAlgorithm
- RGL::TopsortIterator
- Enumerable
- Object

No matching classes.

# RGL::PrimAlgorithm

### Constants

DISTANCE_COMBINATOR

  Replacement for default distance combinator that is used in Dijkstra's algorithm. While building a minimum spanning tree (MST) we're interested not in the distance from the source (the vertex that is added first to the MST) to a vertex, but rather in the distance between already completed part of the MST (that includes all examined vertices) and the vertex. Therefore, when we examine an edge (u, v), where u is already in the MST and v is not, the distance from the MST to the vertex v is the weight of the edge (u, v).

### Public Class Methods

new(graph, edge_weights_map, visitor) click to toggle source

Initializes Prim's algorithm for a graph with provided edges weights map.

    # File lib/rgl/prim.rb, line 17
    def initialize(graph, edge_weights_map, visitor)
      @graph            = graph
      @edge_weights_map = EdgePropertiesMap.new(edge_weights_map, @graph.directed?)
      @visitor          = visitor
      @dijkstra         = DijkstraAlgorithm.new(@graph, @edge_weights_map, @visitor, DISTANCE_COMBINATOR)
    end

### Public Instance Methods

minimum_spanning_tree(start_vertex = nil) click to toggle source

Returns minimum spanning tree for the graph. If the graph is disconnected, Prim's algorithm will find the minimum spanning tree only for one of the connectivity components. If start_vertex is given, Dijkstra's search will be started in this vertex and the algorithm will return the minimum spanning tree for the component it belongs to.

    # File lib/rgl/prim.rb, line 28
    def minimum_spanning_tree(start_vertex = nil)
      @dijkstra.find_shortest_paths(start_vertex || @graph.vertices.first)
      AdjacencyGraph[*@visitor.parents_map.to_a.flatten]
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.