-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraph.py
More file actions
57 lines (48 loc) · 1.8 KB
/
Copy pathgraph.py
File metadata and controls
57 lines (48 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Frank Zhao 2014
# Code generation from algorithmic graphs
import networkx as nx
from collections import OrderedDict
# Graph classes
class Graph:
def __init__(self, nodes=[], relations=[]):
self.nodes = nodes
self.relations = relations
class Node:
def __init__(self, value=None, name=None):
self.value = value
self.name = name
def __str__(self):
if self.value is None:
return str(self.name)
elif self.name is None:
return str(self.value)
else:
return str(self.name)
class Relation:
def __init__(self, method=None, in_nodes=[], out_nodes=[]):
self.method = method
self.in_nodes = in_nodes
self.out_nodes = out_nodes
def __str__(self):
return str(self.method)
# Create an OrderedDiGraph to preserve operation order
# Inherits from nx.Digraph
class OrderedDiGraph(nx.DiGraph):
def __init__(self, data=None, **attr):
self.node_dict_factory = OrderedDict
self.adjlist_dict_factory = OrderedDict
self.edge_attr_dict_factory = OrderedDict
self.graph = {} # dictionary for graph attributes
self.node = OrderedDict() # dictionary for node attributes
# We store two adjacency lists:
# the predecessors of node n are stored in the dict self.pred
# the successors of node n are stored in the dict self.succ=self.adj
self.adj = OrderedDict() # empty adjacency dictionary
self.pred = OrderedDict() # predecessor
self.succ = self.adj # successor
# attempt to load graph with data
if data is not None:
convert.to_networkx_graph(data,create_using=self)
# load graph attributes (must be after convert)
self.graph.update(attr)
self.edge=self.adj