Skip to content

Commit b663735

Browse files
committed
update
1 parent 788e2a6 commit b663735

File tree

3 files changed

+64
-28
lines changed

3 files changed

+64
-28
lines changed

visualize/graph.py

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import svgwrite
2-
31
class Graph:
42
def __init__(self, directed=False):
53
self.nodes = set()
@@ -21,37 +19,15 @@ def add_edge(self, u, v):
2119
self.edges[v] = [u]
2220

2321

24-
2522
def main():
2623
# Create a list of edges for a tree with 7 nodes
27-
edges = {
28-
1: [2, 3],
29-
2: [3, 4, 5],
30-
3: [6, 7]
31-
}
24+
edges = {1: [2, 3], 2: [3, 4, 5], 3: [6, 7]}
3225

33-
if __name__ == '__main__':
3426
# Create a list of edges for a tree with 7 nodes
35-
edges = {
36-
1: [2, 3],
37-
2: [3, 4, 5],
38-
3: [6, 7]
39-
}
27+
edges = {1: [2, 3], 2: [3, 4, 5], 3: [6, 7]}
4028

41-
# Create an instance of the Graph class with 7 nodes and the list of edges
4229
graph = Graph(7, edges)
4330

44-
# Generate an SVG image of the tree and save it to a file
45-
graph.renderSvg('graph.svg')
46-
47-
class Visualizer:
48-
def __init__(self):
49-
pass
5031

51-
def visualize(self, graph):
52-
# calculate the position of each nodes
53-
positions = []
54-
for i, u in enumerate(graph.nodes):
55-
x = i * 30
56-
y = 50
57-
self.position.push((x, y))
32+
if __name__ == "__main__":
33+
main()

visualize/tests/test_visualizer.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import unittest
2+
from typing import List
3+
4+
from graphics import Circle, Rectangle, Line
5+
from graph import Graph
6+
from graphics import GraphicObject
7+
from visualizer import Visualizer
8+
9+
class TestVisualizer(unittest.TestCase):
10+
def test_visualize(self):
11+
# Create a graph with two nodes and one edge
12+
graph = Graph()
13+
graph.add_edge(1, 2)
14+
15+
# Create an instance of Visualizer
16+
visualizer = Visualizer()
17+
18+
# Call the visualize method and store the result in a variable
19+
graphic_objects = visualizer.visualize(graph)
20+
21+
self.assertEqual(len(graphic_objects), 4)
22+
23+
# Check that the first element is a Circle
24+
self.assertIsInstance(graphic_objects[0], Circle)
25+
26+
# Check that the second element is a Line
27+
self.assertIsInstance(graphic_objects[3], Line)
28+
29+
30+
if __name__ == '__main__':
31+
unittest.main()

visualize/visualizer.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import List
2+
from graph import Graph
3+
from graphics import GraphicObject, Circle, Line
4+
5+
6+
class Visualizer:
7+
def __init__(self):
8+
pass
9+
10+
def visualize(self, graph: Graph) -> List[GraphicObject]:
11+
"""Generates a list of GraphicObject instances based on the given graph.
12+
13+
Args:
14+
graph: A Graph object.
15+
16+
Returns:
17+
A list of GraphicObject instances.
18+
"""
19+
graphic_objects = []
20+
positions = {}
21+
for i, node in enumerate(graph.nodes):
22+
x = i * 30
23+
y = 50
24+
positions[node] = (x, y)
25+
graphic_objects.append(Circle((x, y), 10))
26+
for u, vs in graph.edges.items():
27+
for v in vs:
28+
graphic_objects.append(Line(positions[u], positions[v]))
29+
return graphic_objects

0 commit comments

Comments
 (0)