Skip to content

Commit cfbca11

Browse files
authored
Merge branch 'master' into dev3
2 parents b1dc96e + fcf93ed commit cfbca11

File tree

6 files changed

+719
-223
lines changed

6 files changed

+719
-223
lines changed

.pylintrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[MASTER]
2+
py-version=3.5

cyaron/__init__.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@
55
"""
66

77
from __future__ import absolute_import
8+
9+
from random import choice, randint, random, randrange, uniform
10+
11+
#from .visual import visualize
12+
from . import log
13+
from .compare import Compare
14+
from .consts import *
15+
from .graph import Edge, Graph
816
from .io import IO
9-
from .graph import Graph, Edge
10-
from .string import String
17+
from .math import *
18+
from .merger import Merger
19+
from .polygon import Polygon
1120
from .sequence import Sequence
21+
from .string import String
1222
from .utils import *
13-
from .consts import *
1423
from .vector import Vector
15-
from .polygon import Polygon
16-
from .compare import Compare
17-
from .math import *
18-
from .merger import Merger
19-
#from .visual import visualize
20-
from . import log
21-
from random import randint, randrange, uniform, choice, random

cyaron/graph.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ def __init__(self, point_count, directed=False):
3939
"""
4040
self.directed = directed
4141
self.edges = [[] for i in range(point_count + 1)]
42+
43+
def edge_count(self):
44+
"""edge_count(self) -> int
45+
Return the count of the edges in the graph.
46+
"""
47+
cnt = sum(len(node) for node in self.edges)
48+
if not self.directed:
49+
cnt //= 2
50+
return cnt
4251

4352
def to_str(self, **kwargs):
4453
"""to_str(self, **kwargs) -> str
@@ -260,6 +269,11 @@ def graph(point_count, edge_count, **kwargs):
260269
directed = kwargs.get("directed", False)
261270
self_loop = kwargs.get("self_loop", True)
262271
repeated_edges = kwargs.get("repeated_edges", True)
272+
if not repeated_edges:
273+
max_edge = Graph._calc_max_edge(point_count, directed, self_loop)
274+
if edge_count > max_edge:
275+
raise Exception("the number of edges of this kind of graph which has %d vertexes must be less than or equal to %d." % (point_count, max_edge))
276+
263277
weight_limit = kwargs.get("weight_limit", (1, 1))
264278
if not list_like(weight_limit):
265279
weight_limit = (1, weight_limit)
@@ -309,6 +323,11 @@ def DAG(point_count, edge_count, **kwargs):
309323
self_loop = kwargs.get("self_loop", False) # DAG default has no loop
310324
repeated_edges = kwargs.get("repeated_edges", True)
311325
loop = kwargs.get("loop", False)
326+
if not repeated_edges:
327+
max_edge = Graph._calc_max_edge(point_count, not loop, self_loop)
328+
if edge_count > max_edge:
329+
raise Exception("the number of edges of this kind of graph which has %d vertexes must be less than or equal to %d." % (point_count, max_edge))
330+
312331
weight_limit = kwargs.get("weight_limit", (1, 1))
313332
if not list_like(weight_limit):
314333
weight_limit = (1, weight_limit)
@@ -369,6 +388,11 @@ def UDAG(point_count, edge_count, **kwargs):
369388

370389
self_loop = kwargs.get("self_loop", True)
371390
repeated_edges = kwargs.get("repeated_edges", True)
391+
if not repeated_edges:
392+
max_edge = Graph._calc_max_edge(point_count, False, self_loop)
393+
if edge_count > max_edge:
394+
raise Exception("the number of edges of this kind of graph which has %d vertexes must be less than or equal to %d." % (point_count, max_edge))
395+
372396
weight_limit = kwargs.get("weight_limit", (1, 1))
373397
if not list_like(weight_limit):
374398
weight_limit = (1, weight_limit)
@@ -450,3 +474,12 @@ def hack_spfa(point_count, **kwargs):
450474
graph.add_edge(u, v, weight=weight_gen())
451475

452476
return graph
477+
478+
@staticmethod
479+
def _calc_max_edge(point_count, directed, self_loop):
480+
max_edge = point_count * (point_count - 1)
481+
if not directed:
482+
max_edge //= 2
483+
if self_loop:
484+
max_edge += point_count
485+
return max_edge

0 commit comments

Comments
 (0)