Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init commit #11

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/networkx.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
require 'networkx/version'
require 'networkx/graph'
83 changes: 83 additions & 0 deletions lib/networkx/graph.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
module NetworkX
class Graph

attr_reader :node, :adj

def initialize(incoming_graph_data=nil, **graph_attr)
@graph = Hash.new
@node = Hash.new
@adj = Hash.new

if incoming_graph_data != nil
# Not yet implemented!
end

@graph.merge!(graph_attr)
end

def name
@graph["name"]
end

def name(name_val)
@graph["name"] = name_val
end

def to_s
@graph["name"]
end

def add_node(node_for_adding, **node_attr)
if [email protected]?(node_for_adding)
@node[node_for_adding] = node_attr.clone
@adj[node_for_adding] = Hash.new
else
@node[node_for_adding].merge!(node_attr)
end
end

def add_nodes_from(*nodes, **nodes_attrs)
nodes.each do |n|
if [email protected]?(n)
@node[n] = nodes_attr.clone
@adj[n] = Hash.new
else
@node[n].merge!(nodes_attrs)
end
end
end

def add_edge(u, v, **edge_attrs)
if [email protected]?(u)
@node[u] = Hash.new
@adj[u] = Hash.new
end
if [email protected]?(v)
@node[v] = Hash.new
@adj[v] = Hash.new
end

edge_attr_hash = Hash.new

if @adj[u].key?(v)
edge_attr_hash = @adj[u][v]
end

edge_attr_hash.merge!(edge_attrs)

@adj[u][v] = edge_attr_hash
@adj[v][u] = edge_attr_hash
end

def remove_edge(u, v)
begin
@adj[u].delete(v)
if u != v
@adj[v].delete(u)
end
rescue NoMethodError, KeyError
raise NetworkXError, "There exists no edge between #{u} and #{v}"
end
end
end
end