lib/networkx/multidigraph.rb
# Returns subgraph consisting of given array of nodes
#
# @example
#   graph.subgraph(%w[Mumbai Nagpur])
#
# @param nodes [Array<Object>] the nodes to be included in the subgraph
Returns subgraph consisting of given array of nodes
Returns subgraph consisting of given array of nodes
def subgraph(nodes)
      case nodes
      when Array, Set
        sub_graph = NetworkX::MultiDiGraph.new(@graph)
        nodes.each do |u, _|
          raise KeyError, "#{u} does not exist in the current graph!" unless @nodes.key?(u)
          sub_graph.add_node(u, @nodes[u])
          @adj[u].each do |v, edge_val|
            edge_val.each { |_, keyval| sub_graph.add_edge(u, v, keyval) if @adj[u].key?(v) && nodes.include?(v) }
          end
          return sub_graph
        end
      else
        raise ArgumentError, 'Expected Argument to be Array or Set of nodes, '\
                             "received #{nodes.class.name} instead."
      end
    end
def subgraph ( nodes ) case nodes when Array , Set sub_graph = NetworkX :: MultiDiGraph . new ( @graph ) nodes . each do | u , _ | raise KeyError , "#{u} does not exist in the current graph!" unless @nodes . key? ( u ) sub_graph . add_node ( u , @nodes [ u ] ) @adj [ u ] . each do | v , edge_val | edge_val . each { | _ , keyval | sub_graph . add_edge ( u , v , keyval ) if @adj [ u ] . key? ( v ) && nodes . include? ( v ) } end return sub_graph end else raise ArgumentError , 'Expected Argument to be Array or Set of nodes, ' "received #{nodes.class.name} instead." end end
Returns subgraph consisting of given array of nodes
Returns subgraph consisting of given array of nodes
nodes
the nodes to be included in the subgraph
the nodes to be included in the subgraph
example
None