Class: RGL::DirectedAdjacencyGraph

### Home Classes Methods

### In Files

- lib/rgl/adjacency.rb

### Parent

Object

### Methods

- ::[]
- ::new
- #add_edge
- #add_vertex
- #basic_add_edge
- #directed?
- #each_vertex
- #edgelist_class=
- #has_edge?
- #has_vertex?
- #initialize_copy
- #remove_edge
- #remove_vertex

### Included Modules

- RGL::MutableGraph

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

### Public Class Methods

[](*a) click to toggle source

Shortcut for creating a DirectedAdjacencyGraph:

    RGL::DirectedAdjacencyGraph[1,2, 2,3, 2,4, 4,5].edges.to_a.to_s =>
      "(1-2)(2-3)(2-4)(4-5)"

    # File lib/rgl/adjacency.rb, line 29
    def self.[](*a)
      result = new
      0.step(a.size - 1, 2) { |i| result.add_edge(a[i], a[i + 1]) }
      result
    end

new(edgelist_class = Set, *other_graphs) click to toggle source

Returns a new empty DirectedAdjacencyGraph which has as its edgelist class the given class. The default edgelist class is Set, to ensure set semantics for edges and vertices.

If other graphs are passed as parameters their vertices and edges are added to the new graph.

    # File lib/rgl/adjacency.rb, line 42
    def initialize(edgelist_class = Set, *other_graphs)
      @edgelist_class = edgelist_class
      @vertices_dict = Hash.new
      other_graphs.each do |g|
        g.each_vertex { |v| add_vertex v }
        g.each_edge { |v, w| add_edge v, w }
      end
    end

### Public Instance Methods

add_edge(u, v) click to toggle source

See MutableGraph#add_edge.

    # File lib/rgl/adjacency.rb, line 105
    def add_edge(u, v)
      add_vertex(u) # ensure key
      add_vertex(v) # ensure key
      basic_add_edge(u, v)
    end

add_vertex(v) click to toggle source

See MutableGraph#add_vertex.

If the vertex is already in the graph (using eql?), the method does nothing.

    # File lib/rgl/adjacency.rb, line 99
    def add_vertex(v)
      @vertices_dict[v] ||= @edgelist_class.new
    end

directed?() click to toggle source

Returns true.

    # File lib/rgl/adjacency.rb, line 73
    def directed?
      true
    end

each_vertex(&b) click to toggle source

Iterator for the keys of the vertices list hash.

    # File lib/rgl/adjacency.rb, line 62
    def each_vertex(&b)
      @vertices_dict.each_key(&b)
    end

edgelist_class=(klass) click to toggle source

Converts the adjacency list of each vertex to be of type klass. The class is expected to have a new constructor which accepts an enumerable as parameter.

    # File lib/rgl/adjacency.rb, line 130
    def edgelist_class=(klass)
      @vertices_dict.keys.each do |v|
        @vertices_dict[v] = klass.new @vertices_dict[v].to_a
      end
    end

has_edge?(u, v) click to toggle source

Complexity is O(1), if a Set is used as adjacency list. Otherwise, complexity is O(out_degree(v)).

MutableGraph interface.

    # File lib/rgl/adjacency.rb, line 90
    def has_edge? (u, v)
      has_vertex?(u) && @vertices_dict[u].include?(v)
    end

has_vertex?(v) click to toggle source

Complexity is O(1), because the vertices are kept in a Hash containing as values the lists of adjacent vertices of v.

    # File lib/rgl/adjacency.rb, line 80
    def has_vertex? (v)
      @vertices_dict.has_key?(v)
    end

initialize_copy(orig) click to toggle source

Copy internal vertices_dict

    # File lib/rgl/adjacency.rb, line 53
    def initialize_copy(orig)
      @vertices_dict = orig.instance_eval { @vertices_dict }.dup
      @vertices_dict.keys.each do |v|
        @vertices_dict[v] = @vertices_dict[v].dup
      end
    end

remove_edge(u, v) click to toggle source

See MutableGraph::remove_edge.

    # File lib/rgl/adjacency.rb, line 122
    def remove_edge(u, v)
      @vertices_dict[u].delete(v) unless @vertices_dict[u].nil?
    end

remove_vertex(v) click to toggle source

See MutableGraph#remove_vertex.

    # File lib/rgl/adjacency.rb, line 113
    def remove_vertex(v)
      @vertices_dict.delete(v)

      # remove v from all adjacency lists
      @vertices_dict.each_value { |adjList| adjList.delete(v) }
    end

### Protected Instance Methods

basic_add_edge(u, v) click to toggle source

    # File lib/rgl/adjacency.rb, line 138
    def basic_add_edge(u, v)
      @vertices_dict[u].add(v)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.