Skip to content

Commit

Permalink
AdjacencyGraph matches Graph source parameter definition
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfischer2781 committed Jul 27, 2017
1 parent b88fddb commit 9d096f4
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion graphi/types/adjacency_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,22 @@ class AdjacencyGraph(abc.Graph):
:param source: adjacency information
:param undirected: whether the graph enforces symmetry
"""
def __init__(self, source=None, undirected=False, max_distance=None):
def __init__(self, *source, undirected=False, max_distance=None):
self.undirected = undirected
self._adjacency = {} # {node: {neighbour: distance, neighbour: distance, ...}, ...}
super(AdjacencyGraph, self).__init__(*source, max_distance=max_distance)
# TODO: handle different possible sources
if isinstance(source, abc_collection.Mapping):
self._adjacency.update(self._adjacency_from_mapping(source, max_distance))
if undirected:
self._ensure_symmetry()

@staticmethod
def _adjacency_from_graph(graph, max_distance):
adjacency = {}
for node in graph:
adjacency[node] = {other: graph[node:other] for other in graph.get_neighbours(node, max_distance)}
return adjacency

@staticmethod
def _adjacency_from_mapping(adjacency_dict, max_distance):
Expand All @@ -37,6 +47,37 @@ def _adjacency_from_mapping(adjacency_dict, max_distance):
}
return adjacency

def __init_empty__(self, max_distance=None):
return

# initialize a new graph by copying nodes, edges and values from another graph
def __init_graph__(self, graph, max_distance=None):
self._adjacency.update(self._adjacency_from_graph(graph, max_distance=max_distance))

# initialize a new graph by copying nodes from an iterable
def __init_iterable__(self, iterable, **kwargs):
for node in iterable:
self._adjacency.setdefault(node, {})

# initialize a new graph by copying nodes, edges and values from a nested mapping
def __init_mapping__(self, mapping, max_distance=None):
self._adjacency.update(self._adjacency_from_mapping(mapping, max_distance=max_distance))

def _ensure_symmetry(self):
"""
Ensure that adjacency list is symmetric
Adds any missing inverted edges. Raises :py:exc:`ValueError` if inverted edges do not have the same value.
"""
adjacency = self._adjacency
for node_a in adjacency:
for node_b in adjacency[node_a]:
try:
if adjacency[node_a][node_b] != adjacency[node_b][node_a]:
raise ValueError("symmetric graph initialized with asymmetric edges")
except KeyError:
adjacency.setdefault(node_b, {})[node_a] = adjacency[node_a][node_b]

def __getitem__(self, item):
if isinstance(item, slice):
assert item.step is None, "%s does not support stride argument for edges" % \
Expand Down

0 comments on commit 9d096f4

Please sign in to comment.