Skip to content

Commit aae5d77

Browse files
committed
visual and merger
1 parent f0b5d7c commit aae5d77

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 result(self):
35+
return self.G
36+
37+
@staticmethod
38+
def component(point_count, edge_count, **kwargs):
39+
"""component(point_count, edge_count, **kwargs)
40+
generate a graph with certain components
41+
int point_count -> the number of vertices of each component
42+
int edge_count -> the number of edges of each component
43+
**kwargs:
44+
int component_count -> indicating how many components there are
45+
"""
46+
component_count = kwargs.get("component_count", (2, 2))
47+
if not list_like(component_count):
48+
component_count = (component_count, component_count)
49+
real_count = random.randint(*component_count)
50+
graphs = [None] * real_count
51+
for i in xrange(real_count):
52+
graphs[i] = Graph.graph(point_count, edge_count, **kwargs)
53+
G = Merger(*graphs)
54+
return G.result()

cyaron/visual.py

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

0 commit comments

Comments
 (0)