Class: RGL::EdmondsKarpAlgorithm

### Home Classes Methods

### In Files

- lib/rgl/edmonds_karp.rb

### Parent

Object

### Namespace

- CLASS RGL::EdmondsKarpAlgorithm::EdmondsKarpBFSIterator

### Methods

- ::new
- #maximum_flow

### 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::EdmondsKarpAlgorithm

### Public Class Methods

new(graph, edge_capacities_map) click to toggle source

Initializes Edmonds-Karp algorithm for a graph with provided edges capacities map.

    # File lib/rgl/edmonds_karp.rb, line 10
    def initialize(graph, edge_capacities_map)
      raise NotDirectedError.new('Edmonds-Karp algorithm can only be applied to a directed graph') unless graph.directed?

      @graph = graph
      validate_edge_capacities(edge_capacities_map)
      @edge_capacities_map = NonNegativeEdgePropertiesMap.new(edge_capacities_map, true)
    end

### Public Instance Methods

maximum_flow(source, sink) click to toggle source

Finds the maximum flow from the source to the sink in the graph.

Returns flows map as a hash that maps each edge of the graph to a flow through that edge that is required to reach the maximum total flow.

    # File lib/rgl/edmonds_karp.rb, line 23
    def maximum_flow(source, sink)
      raise ArgumentError.new("source and sink can't be equal") if source == sink

      @flow_map = Hash.new(0)
      @residual_capacity_map = lambda { |u, v| @edge_capacities_map.edge_property(u, v) - @flow_map[[u, v]] }

      loop do
        bfs = EdmondsKarpBFSIterator.new(@graph, source, sink, @residual_capacity_map)

        bfs.move_forward_until { bfs.color_map[sink] == :GRAY }

        if bfs.color_map[sink] == :WHITE
          break # no more augmenting paths
        else
          min_residual_capacity = INFINITY

          augmenting_path = [sink]

          while augmenting_path.first != source
            v = augmenting_path.first
            u = bfs.parents_map[v]

            augmenting_path.unshift(u)
            min_residual_capacity = [min_residual_capacity, @residual_capacity_map[u, v]].min
          end

          augmenting_path.each_cons(2) do |(u, v)|
            @flow_map[[u, v]] += min_residual_capacity
            @flow_map[[v, u]] -= min_residual_capacity
          end
        end
      end

      @flow_map
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.