Skip to content
Open
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
83 changes: 66 additions & 17 deletions snapx/snapx/classes/digraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from copy import deepcopy

import snapx as sx
import snap

from snapx.classes.graph import Graph
from snapx.classes.coreviews import AdjacencyView
from snapx.classes.reportviews import (
Expand Down Expand Up @@ -323,11 +325,12 @@ def __init__(self, incoming_graph_data=None, **attr):
{'day': 'Friday'}

"""
self._graph = TNEANet.New()
self._graph = snap.TNEANet.New()

# Because we are emulating an directed graph using multigraph,
# we need a counter to keep track of edges
self._num_edges = 0

# Supplementary dicts to keep data not supported by snap graph
self._node_extra_attr = {}
self._edge_extra_attr = {}
Expand Down Expand Up @@ -435,7 +438,7 @@ def add_node(self, node_for_adding, **attr):
NetworkX Graphs, though one should be careful that the hash
doesn't change on mutables.
"""
super.add_node(node_for_adding, **attr)
super().add_node(node_for_adding, **attr)

def add_nodes_from(self, nodes_for_adding, **attr):
"""Add multiple nodes.
Expand Down Expand Up @@ -481,7 +484,7 @@ def add_nodes_from(self, nodes_for_adding, **attr):
11

"""
super.add_nodes_from(nodes_for_adding, **attr)
super().add_nodes_from(nodes_for_adding, **attr)

def remove_node(self, n):
"""Remove node n.
Expand Down Expand Up @@ -513,7 +516,7 @@ def remove_node(self, n):
[]

"""
super.remove_node(n)
super().remove_node(n)

def remove_nodes_from(self, nodes):
"""Remove multiple nodes.
Expand All @@ -539,7 +542,7 @@ def remove_nodes_from(self, nodes):
[]

"""
super.remove_nodes_from(nodes)
super().remove_nodes_from(nodes)

def add_edge(self, u_of_edge, v_of_edge, **attr):
"""Add an edge between u and v.
Expand Down Expand Up @@ -651,7 +654,7 @@ def add_edges_from(self, ebunch_to_add, **attr):
>>> G.add_edges_from([(1, 2), (2, 3)], weight=3)
>>> G.add_edges_from([(3, 4), (1, 4)], label='WN2898')
"""
super.add_edges_from(ebunch_to_add, **attr)
super().add_edges_from(ebunch_to_add, **attr)

def remove_edge(self, u, v):
"""Remove the edge between u and v.
Expand Down Expand Up @@ -680,11 +683,14 @@ def remove_edge(self, u, v):
>>> e = (2, 3, {'weight':7}) # an edge with attribute data
>>> G.remove_edge(*e[:2]) # select first part of edge tuple
"""
if not self.has_edge(u, v):
return

try:
del self._succ[u][v]
del self._pred[v][u]
except KeyError as e:
raise NetworkXError("The edge {u}-{v} not in graph.".format(u, v)) from e
self._graph.DelEdge(u, v)
self._num_edges -= 1
except TypeError:
raise SnapXTypeError("SNAP only supports int as edge keys.")

def remove_edges_from(self, ebunch):
"""Remove all edges specified in ebunch.
Expand Down Expand Up @@ -712,7 +718,46 @@ def remove_edges_from(self, ebunch):
>>> ebunch = [(1, 2), (2, 3)]
>>> G.remove_edges_from(ebunch)
"""
raise NotImplementedError("TODO")
super().remove_edges_from(ebunch)

def has_edge(self, u, v):
"""PORTED FROM NETWORKX
Returns True if the edge (u, v) is in the graph.
This is the same as `v in G[u]` without KeyError exceptions.
Parameters
----------
u, v : nodes
Nodes can be, for example, strings or numbers.
Nodes must be hashable (and not None) Python objects.
Returns
-------
edge_ind : bool
True if edge is in the graph, False otherwise.
Examples
--------
>>> G = nx.path_graph(4) # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.has_edge(0, 1) # using two nodes
True
>>> e = (0, 1)
>>> G.has_edge(*e) # e is a 2-tuple (u, v)
True
>>> e = (0, 1, {'weight':7})
>>> G.has_edge(*e[:2]) # e is a 3-tuple (u, v, data_dictionary)
True
The following syntax are equivalent:
>>> G.has_edge(0, 1)
True
>>> 1 in G[0] # though this gives KeyError if 0 not in G
True
"""
# Avoid snap runtime error by first checking
# if the nodes exist
if u not in self or v not in self:
return False
try:
return self._graph.IsEdge(u, v)
except TypeError:
return False

def has_successor(self, u, v):
"""Returns True if node u has successor v.
Expand Down Expand Up @@ -758,7 +803,10 @@ def successors(self, n):
# return iter(self._succ[n])
# except KeyError as e:
# raise NetworkXError(f"The node {n} is not in the digraph.") from e
raise NotImplementedError("TODO")
try:
return iter(self.adj[n])
except KeyError:
raise SnapXError("The node {} is not in the graph.".format(n))

# digraph definitions
neighbors = successors
Expand Down Expand Up @@ -1042,7 +1090,7 @@ def clear(self):
[]

"""
super.clear()
super().clear()

def clear_edges(self):
"""Remove all edges from the graph without altering nodes.
Expand All @@ -1057,10 +1105,11 @@ def clear_edges(self):
[]

"""
for predecessor_dict in self._pred.values():
predecessor_dict.clear()
for successor_dict in self._succ.values():
successor_dict.clear()
# for predecessor_dict in self._pred.values():
# predecessor_dict.clear()
# for successor_dict in self._succ.values():
# successor_dict.clear()
raise NotImplementedError("TODO")

def is_multigraph(self):
"""Returns True if graph is a multigraph, False otherwise."""
Expand Down
9 changes: 4 additions & 5 deletions snapx/snapx/classes/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"""

import snapx as sx

from snap import TNEANet, TNEANetNodeI, Nodes, Edges, TSIn
import snap

from snapx.classes.reportviews import NodeView, EdgeView
from snapx.classes.coreviews import AdjacencyView
Expand Down Expand Up @@ -88,7 +87,7 @@ def __init__(self, incoming_graph_data=None, **attr):
>>> G.graph
{'day': 'Friday'}
"""
self._graph = TNEANet.New()
self._graph = snap.TNEANet.New()

# Because we are emulating an undirected graph using multigraph,
# we need a counter to keep track of edges
Expand All @@ -97,7 +96,7 @@ def __init__(self, incoming_graph_data=None, **attr):
self._node_extra_attr = {}
self._edge_extra_attr = {}
if incoming_graph_data is not None:
if type(incoming_graph_data) == type(TNEANet.New()):
if type(incoming_graph_data) == type(snap.TNEANet.New()):
self._graph = incoming_graph_data
self._num_edges = self._graph.GetEdges()
else:
Expand Down Expand Up @@ -731,7 +730,7 @@ def remove_edges_from(self, ebunch):
>>> ebunch = [(1, 2), (2, 3)]
>>> G.remove_edges_from(ebunch)
"""
for e in ebunch_to_add:
for e in ebunch:
ne = len(e)
if ne == 3:
u, v, _ = e
Expand Down
Loading