Class: NetworkX::DiGraph

Inherits:
Graph
  • Object
show all
Defined in:
lib/networkx/digraph.rb

Overview

Describes the class for making Directed Graphs

Direct Known Subclasses

MultiDiGraph

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Graph

#add_edges, #add_nodes, #add_weighted_edge, #add_weighted_edges, #directed?, #edge?, #get_edge_data, #get_node_data, #multigraph?, #neighbours, #node?, #number_of_nodes, #remove_edges, #remove_nodes

Constructor Details

#initialize(**graph_attrs) ⇒ DiGraph

Constructor for initializing graph

Examples:

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

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

Parameters:

  • graph_attrs (Hash{ Object => Object })

    the graph attributes in a hash format



19
20
21
22
23
# File 'lib/networkx/digraph.rb', line 19

def initialize(**graph_attrs)
  super(graph_attrs)

  @pred = {}
end

Instance Attribute Details

#adjHash{ Object => Hash{ Object => Hash{ Object => Object } } } (readonly)

Stores the edges and their attributes in an adjencency list form

Returns:

  • (Hash{ Object => Hash{ Object => Hash{ Object => Object } } })

    the current value of adj



10
11
12
# File 'lib/networkx/digraph.rb', line 10

def adj
  @adj
end

#graphHash{ Object => Object } (readonly)

Stores the attributes of the graph

Returns:

  • (Hash{ Object => Object })

    the current value of graph



10
11
12
# File 'lib/networkx/digraph.rb', line 10

def graph
  @graph
end

#nodesHash{ Object => Hash{ Object => Object } } (readonly)

Stores the nodes and their attributes

Returns:

  • (Hash{ Object => Hash{ Object => Object } })

    the current value of nodes



10
11
12
# File 'lib/networkx/digraph.rb', line 10

def nodes
  @nodes
end

#predHash{ Object => Hash{ Object => Hash{ Object => Object } } } (readonly)

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

Returns:

  • (Hash{ Object => Hash{ Object => Hash{ Object => Object } } })

    the current value of pred



10
11
12
# File 'lib/networkx/digraph.rb', line 10

def pred
  @pred
end

Instance Method Details

#add_edge(node_1, node_2, **edge_attrs) ⇒ Object

Adds the respective edge

Examples:

Add an edge with attribute name

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

Add an edge with no attribute

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

Parameters:

  • node_1 (Object)

    the first node of the edge

  • node_2 (Object)

    the second node of the edge

  • edge_attrs (Hash{ Object => Object })

    the hash of the edge attributes



36
37
38
39
40
41
42
43
# 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) ⇒ Object

Adds a node and its attributes to the graph

Examples:

Add a node with attribute 'type'

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

Parameters:

  • node (Object)

    the node object

  • node_attrs (Hash{ Object => Object })

    the hash of the attributes of the node



52
53
54
55
56
# File 'lib/networkx/digraph.rb', line 52

def add_node(node, **node_attrs)
  super(node, node_attrs)

  @pred[node] = {} unless @pred.key?(node)
end

#clearObject

Clears the graph

Examples:

graph.clear


98
99
100
101
102
# File 'lib/networkx/digraph.rb', line 98

def clear
  super

  @pred.clear
end

#edge_subgraph(edges) ⇒ Object

Returns subgraph consisting of given edges

Examples:

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

Parameters:

  • edges (Array<Object, Object>)

    the edges to be included in the subraph



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# 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) ⇒ Object

Returns in-degree of a given node

Examples:

graph.in_degree(node)

Parameters:

  • node (Object)

    the node whose in degree is to be calculated



136
137
138
# File 'lib/networkx/digraph.rb', line 136

def in_degree(node)
  @pred[node].length
end

#number_of_edgesObject

Returns number of edges

Examples:

graph.number_of_edges


108
109
110
# File 'lib/networkx/digraph.rb', line 108

def number_of_edges
  @adj.values.map(&:length).inject(:+)
end

#out_degree(node) ⇒ Object

Returns out-degree of a given node

Examples:

graph.out_degree(node)

Parameters:

  • node (Object)

    the node whose out degree is to be calculated



146
147
148
# File 'lib/networkx/digraph.rb', line 146

def out_degree(node)
  @adj[node].length
end

#remove_edge(node_1, node_2) ⇒ Object

Removes edge from the graph

Examples:

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

Parameters:

  • node_1 (Object)

    the first node of the edge

  • node_2 (Object)

    the second node of the edge

Raises:

  • (KeyError)


85
86
87
88
89
90
91
92
# 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) ⇒ Object

Removes node from the graph

Examples:

graph.remove_node("Noida")

Parameters:

  • node (Object)

    the node to be removed

Raises:

  • (KeyError)


64
65
66
67
68
69
70
71
72
73
74
75
76
# 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

#reverseObject

Returns the reversed version of the graph

Examples:

graph.reverse


154
155
156
157
158
159
160
161
# 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) ⇒ Object

Returns the size of graph

Examples:

graph.size(true)

Parameters:

  • is_weighted (Bool) (defaults to: false)

    if true, method returns sum of weights of all edges else returns number of edges



119
120
121
122
123
124
125
126
127
128
# 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) ⇒ Object

Returns subgraph consisting of given array of nodes

Examples:

graph.subgraph(%w[Mumbai Nagpur])

Parameters:

  • nodes (Array<Object>)

    the nodes to be included in the subgraph



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# 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_undirectedObject

Returns the undirected version of the graph

Examples:

graph.to_undirected


167
168
169
170
171
172
173
174
# 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