Class: NetworkX::MultiDiGraph
- Defined in:
- lib/networkx/multidigraph.rb
Overview
Describes the class for making MultiDiGraphs
Instance Attribute Summary collapse
-
#adj ⇒ Hash{ Object => Hash{ Object => Hash{ Integer => Hash{ Object => Object } } } }
readonly
Stores the edges and their attributes in an adjencency list form.
-
#graph ⇒ Hash{ Object => Object }
readonly
Stores the attributes of the graph.
-
#nodes ⇒ Hash{ Object => Hash{ Object => Object } }
readonly
Stores the nodes and their attributes.
-
#pred ⇒ Hash{ Object => Hash{ Object => Hash{ Integer => Hash{ Object => Object } } } }
readonly
Stores the reverse edges and their attributes in an adjencency list form.
Instance Method Summary collapse
-
#add_edge(node_1, node_2, **edge_attrs) ⇒ Object
Adds the respective edge.
-
#edge?(node_1, node_2, key = nil) ⇒ Boolean
Checks if the the edge consisting of two nodes is present in the graph.
-
#edge_subgraph(edges) ⇒ Object
Returns subgraph conisting of given edges.
-
#in_degree(node) ⇒ Object
Returns in-degree of a given node.
-
#new_edge_key(node_1, node_2) ⇒ Object
Returns a new key.
-
#number_of_edges ⇒ Object
Returns number of edges.
-
#out_degree(node) ⇒ Object
Returns out-degree of a given node.
-
#remove_edge(node_1, node_2, key = nil) ⇒ Object
Removes edge from the graph.
-
#reverse ⇒ Object
Returns the reversed version of the graph.
-
#size(is_weighted = false) ⇒ Object
Returns the size of the graph.
-
#subgraph(nodes) ⇒ Object
Returns subgraph consisting of given array of nodes.
-
#to_directed ⇒ Object
Returns the directed version of the graph.
-
#to_multigraph ⇒ Object
Returns the multigraph version of the graph.
-
#to_undirected ⇒ Object
Returns the undirected version of the graph.
Methods inherited from DiGraph
#add_node, #clear, #initialize, #remove_node
Methods inherited from Graph
#add_edges, #add_node, #add_nodes, #add_weighted_edge, #add_weighted_edges, #clear, #directed?, #get_edge_data, #get_node_data, #initialize, #multigraph?, #neighbours, #node?, #number_of_nodes, #remove_edges, #remove_node, #remove_nodes
Constructor Details
This class inherits a constructor from NetworkX::DiGraph
Instance Attribute Details
#adj ⇒ Hash{ Object => Hash{ Object => Hash{ Integer => Hash{ Object => Object } } } } (readonly)
Stores the edges and their attributes in an adjencency list form
10 11 12 |
# File 'lib/networkx/multidigraph.rb', line 10 def adj @adj end |
#graph ⇒ Hash{ Object => Object } (readonly)
Stores the attributes of the graph
10 11 12 |
# File 'lib/networkx/multidigraph.rb', line 10 def graph @graph end |
#nodes ⇒ Hash{ Object => Hash{ Object => Object } } (readonly)
Stores the nodes and their attributes
10 11 12 |
# File 'lib/networkx/multidigraph.rb', line 10 def nodes @nodes end |
#pred ⇒ Hash{ Object => Hash{ Object => Hash{ Integer => Hash{ Object => Object } } } } (readonly)
Stores the reverse edges and their attributes in an adjencency list form
10 11 12 |
# File 'lib/networkx/multidigraph.rb', line 10 def pred @pred end |
Instance Method Details
#add_edge(node_1, node_2, **edge_attrs) ⇒ Object
Adds the respective edge
33 34 35 36 37 38 39 40 41 |
# File 'lib/networkx/multidigraph.rb', line 33 def add_edge(node_1, node_2, **edge_attrs) add_node(node_1) add_node(node_2) key = new_edge_key(node_1, node_2) all_edge_attrs = @adj[node_1][node_2] || {} all_edge_attrs[key] = edge_attrs @adj[node_1][node_2] = all_edge_attrs @pred[node_2][node_1] = all_edge_attrs end |
#edge?(node_1, node_2, key = nil) ⇒ Boolean
Checks if the the edge consisting of two nodes is present in the graph
73 74 75 76 |
# File 'lib/networkx/multidigraph.rb', line 73 def edge?(node_1, node_2, key=nil) super(node_1, node_2) if key.nil? node?(node_1) && @adj[node_1].key?(node_2) && @adj[node_1][node_2].key?(key) end |
#edge_subgraph(edges) ⇒ Object
Returns subgraph conisting of given edges
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/networkx/multidigraph.rb', line 222 def edge_subgraph(edges) case edges when Array, Set sub_graph = NetworkX::MultiDiGraph.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]) @adj[u][v].each { |_, keyval| sub_graph.add_edge(u, v, keyval) } 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
148 149 150 |
# File 'lib/networkx/multidigraph.rb', line 148 def in_degree(node) @pred[node].values.map(:length).inject(:+) end |
#new_edge_key(node_1, node_2) ⇒ Object
Returns a new key
15 16 17 18 19 20 |
# File 'lib/networkx/multidigraph.rb', line 15 def new_edge_key(node_1, node_2) return 0 if @adj[node_1][node_2].nil? key = @adj[node_1][node_2].length key += 1 while @adj[node_1][node_2].key?(key) key end |
#number_of_edges ⇒ Object
Returns number of edges
166 167 168 |
# File 'lib/networkx/multidigraph.rb', line 166 def number_of_edges @adj.values.flat_map(&:values).map(&:length).inject(:+) end |
#out_degree(node) ⇒ Object
Returns out-degree of a given node
158 159 160 |
# File 'lib/networkx/multidigraph.rb', line 158 def out_degree(node) @adj[node].values.map(:length).inject(:+) end |
#remove_edge(node_1, node_2, key = nil) ⇒ Object
Removes edge from the graph
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/networkx/multidigraph.rb', line 52 def remove_edge(node_1, node_2, key=nil) if key.nil? super(node_1, node_2) return end 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) raise KeyError, 'The given edge is not a valid one.' unless @adj[node_1][node_2].key?(key) @adj[node_1][node_2].delete(key) @pred[node_2][node_1].delete(key) end |
#reverse ⇒ Object
Returns the reversed version of the graph
133 134 135 136 137 138 139 140 |
# File 'lib/networkx/multidigraph.rb', line 133 def reverse new_graph = NetworkX::MultiDiGraph.new(@graph) @nodes.each { |node, attrs| new_graph.add_node(node, attrs) } @adj.each do |u, u_edges| u_edges.each { |v, uv_attrs| uv_attrs.each { |_k, edge_attrs| new_graph.add_edge(v, u, edge_attrs) } } end new_graph end |
#size(is_weighted = false) ⇒ Object
Returns the size of the graph
177 178 179 180 181 182 183 184 185 186 |
# File 'lib/networkx/multidigraph.rb', line 177 def size(is_weighted=false) if is_weighted graph_size = 0 @adj.each do |_, hash_val| hash_val.each { |_, v| v.each { |_, attrs| graph_size += attrs[:weight] if attrs.key?(:weight) } } end return graph_size end number_of_edges end |
#subgraph(nodes) ⇒ Object
Returns subgraph consisting of given array of nodes
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/networkx/multidigraph.rb', line 196 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 |
#to_directed ⇒ Object
Returns the directed version of the graph
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/networkx/multidigraph.rb', line 99 def to_directed graph = NetworkX::DiGraph.new(@graph) @nodes.each { |node, node_attr| graph.add_node(node, node_attr) } @adj.each do |node_1, node_1_edges| node_1_edges.each do |node_2, node_1_node_2| edge_attrs = {} node_1_node_2.each { |_key, attrs| edge_attrs.merge!(attrs) } graph.add_edge(node_1, node_2, edge_attrs) end end graph end |
#to_multigraph ⇒ Object
Returns the multigraph version of the graph
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/networkx/multidigraph.rb', line 116 def to_multigraph graph = NetworkX::MultiGraph.new(@graph) @nodes.each { |node, node_attr| graph.add_node(node, node_attr) } @adj.each do |node_1, node_1_edges| node_1_edges.each_key do |node_2, node_1_node_2| edge_attrs = {} node_1_node_2.each { |_key, attrs| graph.add_edge(node_1, node_2, attrs) } graph.add_edge(node_1, node_2, edge_attrs) end end graph end |
#to_undirected ⇒ Object
Returns the undirected version of the graph
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/networkx/multidigraph.rb', line 82 def to_undirected graph = NetworkX::Graph.new(@graph) @nodes.each { |node, node_attr| graph.add_node(node, node_attr) } @adj.each do |node_1, node_1_edges| node_1_edges.each do |node_2, node_1_node_2| edge_attrs = {} node_1_node_2.each { |_key, attrs| edge_attrs.merge!(attrs) } graph.add_edge(node_1, node_2, edge_attrs) end end graph end |