class NetworkX::DiGraph

Describes the class for making Directed Graphs

@attr_reader adj [Hash{ Object => Hash{ Object => Hash{ Object => Object } } }]

Stores the edges and their attributes in an adjencency list form

@attr_reader pred [Hash{ Object => Hash{ Object => Hash{ Object => Object } } }]

Stores the reverse edges and their attributes in an adjencency list form

@attr_reader nodes [Hash{ Object => Hash{ Object => Object } }] Stores the nodes and their attributes @attr_reader graph [Hash{ Object => Object }] Stores the attributes of the graph

Attributes

adj[R]
graph[R]
nodes[R]
pred[R]

Public Class Methods

new(**graph_attrs) click to toggle source

Constructor for initializing graph

@example Initialize a graph with attributes 'type' and 'name'

graph = NetworkX::Graph.new(name: "Social Network", type: "undirected")

@param graph_attrs [Hash{ Object => Object }] the graph attributes in a hash format

Calls superclass method
# File lib/networkx/digraph.rb, line 19
def initialize(**graph_attrs)
  super(graph_attrs)

  @pred = {}
end

Public Instance Methods

add_edge(node_1, node_2, **edge_attrs) click to toggle source

Adds the respective edge

@example Add an edge with attribute name

graph.add_edge(node1, node2, name: "Edge1")

@example Add an edge with no attribute

graph.add_edge("Bangalore", "Chennai")

@param node_1 [Object] the first node of the edge @param node_2 [Object] the second node of the edge @param edge_attrs [Hash{ Object => Object }] the hash of the edge attributes

# File lib/networkx/digraph.rb, line 36
def add_edge(node_1, node_2, **edge_attrs)
  add_node(node_1)
  add_node(node_2)

  edge_attrs = (@adj[node_1][node_2] || {}).merge(edge_attrs)
  @adj[node_1][node_2] = edge_attrs
  @pred[node_2][node_1] = edge_attrs
end
add_node(node, **node_attrs) click to toggle source

Adds a node and its attributes to the graph

@example Add a node with attribute 'type'

graph.add_node("Noida", type: "city")

@param node [Object] the node object @param node_attrs [Hash{ Object => Object }] the hash of the attributes of the node

Calls superclass method
# File lib/networkx/digraph.rb, line 52
def add_node(node, **node_attrs)
  super(node, node_attrs)

  @pred[node] = {} unless @pred.key?(node)
end
clear() click to toggle source

Clears the graph

@example

graph.clear
Calls superclass method
# File lib/networkx/digraph.rb, line 98
def clear
  super

  @pred.clear
end
edge_subgraph(edges) click to toggle source

Returns subgraph consisting of given edges

@example

graph.edge_subgraph([%w[Nagpur Wardha], %w[Nagpur Mumbai]])

@param edges [Array<Object, Object>] the edges to be included in the subraph

# File lib/networkx/digraph.rb, line 210
def edge_subgraph(edges)
  case edges
  when Array, Set
    sub_graph = NetworkX::DiGraph.new(@graph)
    edges.each do |u, v|
      raise KeyError, "Edge between #{u} and #{v} does not exist in the graph!" unless @nodes.key?(u)\
                                                                                && @adj[u].key?(v)
      sub_graph.add_node(u, @nodes[u])
      sub_graph.add_node(v, @nodes[v])
      sub_graph.add_edge(u, v, @adj[u][v])
    end
    return sub_graph
  else
    raise ArgumentError, 'Expected Argument to be Array or Set of edges, '\
    "received #{edges.class.name} instead."
  end
end
in_degree(node) click to toggle source

Returns in-degree of a given node

@example

graph.in_degree(node)

@param node [Object] the node whose in degree is to be calculated

# File lib/networkx/digraph.rb, line 136
def in_degree(node)
  @pred[node].length
end
number_of_edges() click to toggle source

Returns number of edges

@example

graph.number_of_edges
# File lib/networkx/digraph.rb, line 108
def number_of_edges
  @adj.values.map(&:length).inject(:+)
end
out_degree(node) click to toggle source

Returns out-degree of a given node

@example

graph.out_degree(node)

@param node [Object] the node whose out degree is to be calculated

# File lib/networkx/digraph.rb, line 146
def out_degree(node)
  @adj[node].length
end
remove_edge(node_1, node_2) click to toggle source

Removes edge from the graph

@example

graph.remove_edge('Noida', 'Bangalore')

@param node_1 [Object] the first node of the edge @param node_2 [Object] the second node of the edge

# File lib/networkx/digraph.rb, line 85
def remove_edge(node_1, node_2)
  raise KeyError, "#{node_1} is not a valid node." unless @nodes.key?(node_1)
  raise KeyError, "#{node_2} is not a valid node" unless @nodes.key?(node_2)
  raise KeyError, 'The given edge is not a valid one.' unless @adj[node_1].key?(node_2)

  @adj[node_1].delete(node_2)
  @pred[node_2].delete(node_1)
end
remove_node(node) click to toggle source

Removes node from the graph

@example

graph.remove_node("Noida")

@param node [Object] the node to be removed

# File lib/networkx/digraph.rb, line 64
def remove_node(node)
  raise KeyError, "Error in deleting node #{node} from Graph." unless @nodes.key?(node)

  neighbours = @adj[node]
  neighbours.each_key { |k| @pred[k].delete(node) }
  @pred[node].each_key do |k|
    @adj[k].delete(node)
  end

  @pred.delete(node)
  @adj.delete(node)
  @nodes.delete(node)
end
reverse() click to toggle source

Returns the reversed version of the graph

@example

graph.reverse
# File lib/networkx/digraph.rb, line 154
def reverse
  new_graph = NetworkX::DiGraph.new(@graph)
  @nodes.each { |u, attrs| new_graph.add_node(u, attrs) }
  @adj.each do |u, edges|
    edges.each { |v, attrs| new_graph.add_edge(v, u, attrs) }
  end
  new_graph
end
size(is_weighted=false) click to toggle source

Returns the size of graph

@example

graph.size(true)

@param is_weighted [Bool] if true, method returns sum of weights of all edges

else returns number of edges
# File lib/networkx/digraph.rb, line 119
def size(is_weighted=false)
  if is_weighted
    graph_size = 0
    @adj.each do |_, hash_val|
      hash_val.each { |_, v| graph_size += v[:weight] if v.key?(:weight) }
    end
    return graph_size
  end
  number_of_edges
end
subgraph(nodes) click to toggle source

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

# File lib/networkx/digraph.rb, line 184
def subgraph(nodes)
  case nodes
  when Array, Set
    sub_graph = NetworkX::DiGraph.new(@graph)
    nodes.each do |u|
      raise KeyError, "#{u} does not exist in the current graph!" unless node?(u)
      sub_graph.add_node(u, @nodes[u])
      @adj[u].each do |v, uv_attrs|
        sub_graph.add_edge(u, v, uv_attrs) 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
to_undirected() click to toggle source

Returns the undirected version of the graph

@example

graph.to_undirected
# File lib/networkx/digraph.rb, line 167
def to_undirected
  new_graph = NetworkX::Graph.new(@graph)
  @nodes.each { |u, attrs| new_graph.add_node(u, attrs) }
  @adj.each do |u, edges|
    edges.each { |v, attrs| new_graph.add_edge(u, v, attrs) }
  end
  new_graph
end