Skip to content

Commit a1aa86e

Browse files
authored
Merge pull request #16 from AceSrc/master
visual & merger
2 parents f0b5d7c + 661542d commit a1aa86e

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

cyaron/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515
from .polygon import Polygon
1616
from .compare import Compare
1717
from .math import *
18+
from .merger import Merger
19+
from .visual import visualize
1820
from random import randint, randrange, uniform, choice, random

cyaron/merger.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from .graph import *
2+
3+
class Merger:
4+
def __init__(self, *graphs, **kwargs):
5+
"""__init__(self, *graphs, **kwargs) -> None
6+
put several graphs into one
7+
list graphs -> the graphs that will be merged
8+
list kwargs:
9+
None
10+
"""
11+
self.graphs = graphs
12+
self.G = Graph(sum([len(i.edges) - 1 for i in graphs]), graphs[0].directed)
13+
14+
counter = 0
15+
for graph in self.graphs:
16+
graph.offset = counter
17+
for edge in graph.iterate_edges():
18+
self.G.add_edge(edge.start + counter,
19+
edge.end + counter,
20+
weight=edge.weight)
21+
counter += len(graph.edges) - 1
22+
23+
def __add_edge(self, u, v, **kwargs):
24+
"""__add_edge(self, u, v, **kwargs)
25+
tuple u -> (graph_index, vertex) indicating the start point
26+
tuple v -> (graph_index, vertex) indicating the end point
27+
**kwargs:
28+
int weight -> edge weight
29+
"""
30+
self.G.add_edge(self.graphs[ u[0] ].offset + u[1],
31+
self.graphs[ v[0] ].offset + v[1],
32+
weight=kwargs.get("weight", 1))
33+
34+
def add_edge(self, u, v, **kwargs):
35+
"""add_edge(self, u, v, **kwargs) -> None
36+
"""
37+
self.__add_edge(u, v, **kwargs)
38+
39+
def to_str(self, **kwargs):
40+
return self.G.to_str(**kwargs)
41+
42+
def __str__(self):
43+
return self.to_str()
44+
45+
@staticmethod
46+
def component(point_count, edge_count, **kwargs):
47+
"""component(point_count, edge_count, **kwargs)
48+
generate a graph with certain components
49+
int point_count -> the number of vertices of each component
50+
int edge_count -> the number of edges of each component
51+
**kwargs:
52+
int component_count -> indicating how many components there are
53+
"""
54+
component_count = kwargs.get("component_count", (2, 2))
55+
if not list_like(component_count):
56+
component_count = (component_count, component_count)
57+
real_count = random.randint(*component_count)
58+
graphs = [None] * real_count
59+
for i in xrange(real_count):
60+
graphs[i] = Graph.graph(point_count, edge_count, **kwargs)
61+
G = Merger(*graphs)
62+
return G.G

cyaron/visual.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from .graph import *
2+
from .merger import Merger
3+
import pygraphviz as pgv
4+
5+
def visualize(graph, output_path="a.png"):
6+
"""visualize(graph, **kwargs) -> None
7+
Graph/Merger graph -> the graph/Merger that will be visualized
8+
string output_path-> the output path of the image
9+
"""
10+
11+
if isinstance(graph, Merger): graph = Merger.G
12+
G = pgv.AGraph(directed=graph.directed)
13+
14+
G.add_nodes_from([i for i in xrange(1, len(graph.edges))])
15+
for edge in graph.iterate_edges():
16+
G.add_edge(edge.start, edge.end, label=edge.weight)
17+
18+
G.node_attr['shape'] = 'egg'
19+
G.node_attr['width'] = '0.25'
20+
G.node_attr['height'] = '0.25'
21+
G.edge_attr['arrowhead'] = 'open'
22+
23+
G.layout(prog='dot')
24+
G.draw(output_path)
25+
26+

0 commit comments

Comments
 (0)