Class Nanoc::DirectedGraph
In: lib/nanoc/base/directed_graph.rb
Parent: Object

Represents a directed graph. It is used by the dependency tracker for storing and querying dependencies between items.

@example Creating and using a directed graph

  # Create a graph with three vertices
  graph = Nanoc::DirectedGraph.new(%w( a b c d ))

  # Add edges
  graph.add_edge('a', 'b')
  graph.add_edge('b', 'c')
  graph.add_edge('c', 'd')

  # Get (direct) predecessors
  graph.direct_predecessors_of('d').sort
    # => %w( c )
  graph.predecessors_of('d').sort
    # => %w( a b c )

  # Modify edges
  graph.delete_edge('a', 'b')

  # Get (direct) predecessors again
  graph.direct_predecessors_of('d').sort
    # => %w( c )
  graph.predecessors_of('d').sort
    # => %w( b c )

Methods

Public Class methods

Creates a new directed graph with the given vertices.

Public Instance methods

Adds an edge from the first vertex to the second vertex.

@param from Vertex where the edge should start

@param to Vertex where the edge should end

@return [void]

Adds the given vertex to the graph.

@param v The vertex to add to the graph

@return [void]

@since 3.2.0

Removes the edge from the first vertex to the second vertex. If the edge does not exist, nothing is done.

@param from Start vertex of the edge

@param to End vertex of the edge

@return [void]

@since 3.2.0

Deletes all edges coming from the given vertex.

@param from Vertex from which all edges should be removed

@return [void]

@since 3.2.0

Deletes all edges going to the given vertex.

@param to Vertex to which all edges should be removed

@return [void]

Removes the given vertex from the graph.

@param v Vertex to remove from the graph

@return [void]

@since 3.2.0

Returns the direct predecessors of the given vertex, i.e. the vertices x where there is an edge from x to the given vertex y.

@param to The vertex of which the predecessors should be calculated

@return [Array] Direct predecessors of the given vertex

Returns the direct successors of the given vertex, i.e. the vertices y where there is an edge from the given vertex x to y.

@param from The vertex of which the successors should be calculated

@return [Array] Direct successors of the given vertex

Returns an array of tuples representing the edges. The result of this method may take a while to compute and should be cached if possible.

@return [Array] The list of all edges in this graph.

Returns the predecessors of the given vertex, i.e. the vertices x for which there is a path from x to the given vertex y.

@param to The vertex of which the predecessors should be calculated

@return [Array] Predecessors of the given vertex

@deprecated Use {delete_edge} instead

Returns all root vertices, i.e. vertices where no edge points to.

@return [Set] The set of all root vertices in this graph.

@since 3.2.0

Returns the successors of the given vertex, i.e. the vertices y for which there is a path from the given vertex x to y.

@param from The vertex of which the successors should be calculated

@return [Array] Successors of the given vertex

@return [Array] The list of all vertices in this graph.

[Validate]