1+ def visualize_graph (graph ):
2+ """
3+ Visualize SCC using Graphviz with a strictly enforced circular layout
4+ """
5+ import pygraphviz as pgv
6+
7+ # Create a new pygraphviz graph directly (instead of converting)
8+ A = pgv .AGraph (strict = False , directed = True )
9+
10+ # Set graph attributes for strict circular layout
11+ A .graph_attr .update ({
12+ 'layout' : 'circo' ,
13+ 'root' : 'circle' ,
14+ 'splines' : 'curved' ,
15+ 'overlap' : 'false' ,
16+ 'sep' : '+25,25' ,
17+ 'pad' : '0.5' ,
18+ 'ranksep' : '2.0' ,
19+ 'nodesep' : '0.8' ,
20+ 'mindist' : '2.0' ,
21+ 'start' : 'regular' ,
22+ 'ordering' : 'out' ,
23+ 'concentrate' : 'false' ,
24+ 'ratio' : '1.0' ,
25+ })
26+
27+ # Set node attributes for consistent sizing
28+ A .node_attr .update ({
29+ 'shape' : 'circle' ,
30+ 'fixedsize' : 'true' ,
31+ 'width' : '1.5' ,
32+ 'height' : '1.5' ,
33+ 'style' : 'filled' ,
34+ 'fillcolor' : 'lightblue' ,
35+ 'fontsize' : '11' ,
36+ 'fontname' : 'Arial'
37+ })
38+
39+ # Set default edge attributes
40+ A .edge_attr .update ({
41+ 'penwidth' : '1.5' ,
42+ 'arrowsize' : '0.8' ,
43+ 'len' : '2.0' ,
44+ 'weight' : '1' ,
45+ 'dir' : 'forward'
46+ })
47+
48+ # Add nodes first
49+ for node in graph .nodes ():
50+ short_name = node .split ('/' )[- 1 ]
51+ A .add_node (node , label = short_name )
52+
53+ # Add edges with their attributes
54+ for u , v , key , data in graph .edges (data = True , keys = True ):
55+ # Create a unique key for this edge
56+ edge_key = f"{ u } _{ v } _{ key } "
57+
58+ # Set edge attributes based on the data
59+ edge_attrs = {
60+ 'key' : edge_key , # Ensure unique edge
61+ 'color' : 'red' if data .get ('color' ) == 'red' else '#666666' ,
62+ 'style' : 'dashed' if data .get ('color' ) == 'red' else 'solid' ,
63+ 'label' : 'dynamic' if data .get ('color' ) == 'red' else '' ,
64+ 'fontcolor' : 'red' if data .get ('color' ) == 'red' else '#666666' ,
65+ 'fontsize' : '10'
66+ }
67+
68+ A .add_edge (u , v , ** edge_attrs )
69+
70+ # Force circo layout with specific settings
71+ A .layout (prog = 'circo' )
72+
73+ # Save with a larger size
74+ A .draw ('import_cycle.png' , format = 'png' , prog = 'circo' , args = '-Gsize=12,12!' )
75+
76+ from IPython .display import Image
77+ return Image ('import_cycle.png' )
0 commit comments