forked from seanlinmt/DelimitationToolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.py
69 lines (61 loc) · 2.32 KB
/
graph.py
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
58
59
60
61
62
63
64
65
66
67
68
69
"""
/***************************************************************************
DelimitationToolbox
Electorate Rebalancing and Redistricting
-------------------
begin : 2014-07-06
git sha : $Format:%H$
copyright : (C) 2014 by Sean Lin
email : seanlinmt at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
from builtins import str
from builtins import object
class Graph(object):
def __init__(self, is_sorted=True):
self.sorted = is_sorted
self.nodeEdge = {}
pass
def add_edge(self, i, j):
ij = [i, j]
if self.sorted:
ij.sort()
(i, j) = ij
if i in self.nodeEdge:
self.nodeEdge[i].add(j)
else:
self.nodeEdge[i] = {j}
def dump(self):
out = []
for k in list(self.nodeEdge.keys()):
out.append(str(k)+":")
for v in self.nodeEdge[k]:
out.append(" --> "+str(v))
return "\n".join(out)
def write_dot(self, name, filepath):
dot = self.make_dot(name)
fp = open(filepath, "w")
fp.write(dot)
fp.close()
def make_dot(self, name):
s = ['graph "%s" {' % name]
for k in list(self.nodeEdge.keys()):
for v in self.nodeEdge[k]:
s.append('"%s" -- "%s" ;' % (str(k), str(v)))
s.append("}")
return "\n".join(s)
def makefull(self):
g = Graph(is_sorted=False)
for k in list(self.nodeEdge.keys()):
for v in self.nodeEdge[k]:
g.add_edge(v, k)
g.add_edge(k, v)
return g