Crtanje grafova u SAGE-u

In [1]:
import sage.misc.banner
banner()
┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 9.2, Release Date: 2020-10-24                     │
│ Using Python 3.8.6. Type "help()" for help.                        │
└────────────────────────────────────────────────────────────────────┘
In [2]:
G1=Graph({1:[2,5],2:[2,3],3:[4,4],4:[5]})
print(G1)
Looped multi-graph on 5 vertices

Dobivanje PNG slike. Desnim klikom miša na sliku, ona se može spremiti na disk.

In [3]:
G1.plot().show(figsize=(4,4))
In [4]:
G1.plot(graph_border=True).show(figsize=(4,4))
In [5]:
G1.show()
In [6]:
grafSlika=G1.plot(graph_border=True)
grafSlika.show(figsize=(4,4))

ako želimo eksplicitno sami odrediti položaj pojedinog vrha

postoje i algoritmi koji automatski određuju položaj vrhova, a oni se koriste preko opcije layout

In [7]:
pozicije={1:[1,3],2:[-1,2],3:[0,0],4:[2,0],5:[3,2]}
In [8]:
G1.plot(pos=pozicije,loop_size=0.15).show(figsize=(4,4))
In [9]:
G1.plot(pos=pozicije,loop_size=0.15).show(axes=True,figsize=(5,5))

vrhovi grafa raspoređeni po kružnici

In [10]:
G1.plot(layout="circular").show(figsize=(4,4))

automatsko određivanje koordinata vrhova

In [11]:
G1.layout()
Out[11]:
{1: [1.0308517690048336, -0.6364821628207848],
 2: [1.0, 0.5463354548456119],
 3: [2.0863403316652636, 0.9509655349181225],
 4: [2.7543921872505357, 0.054152460612290496],
 5: [2.1391316630849317, -0.9149712875552396]}
In [12]:
G1.layout()
Out[12]:
{1: [1.798805434298371, -0.9011273982323282],
 2: [2.851677569978533, -0.496131745458085],
 3: [2.7295555922773507, 0.615599391792881],
 4: [1.5985675557421923, 0.874067712661951],
 5: [0.9999999999999999, -0.0924079607644178]}
In [13]:
G1.layout_circular()
Out[13]:
{1: (0.0, 1.0),
 2: (-0.9510565163, 0.3090169944),
 3: (-0.5877852523, -0.8090169944),
 4: (0.5877852523, -0.8090169944),
 5: (0.9510565163, 0.3090169944)}
In [14]:
G1.layout_default()
Out[14]:
{1: [2.840932915318917, 0.19341574683940402],
 2: [2.0177811729387805, 0.9479473588901806],
 3: [1.0, 0.38511090205766096],
 4: [1.1716767697007966, -0.7225407211492403],
 5: [2.2846348084127075, -0.8039332866380052]}
In [15]:
G1.layout_default()
Out[15]:
{1: [1.0, 0.541531348188629],
 2: [2.1011503593932694, 0.9443265079156166],
 3: [2.798932787613709, 0.049988690054090935],
 4: [2.1956328715867297, -0.9326712033552483],
 5: [1.078810165870321, -0.603175342803088]}

još neke opcije

možete uočiti problem da se petlja ne vidi, ali kod grafova bez petlji to neće predstavljati problem

In [16]:
G1.show(graph_border=True,loop_size=0.15,figsize=(2,2))
In [17]:
G1.show(loop_size=0.15,figsize=(2,2))
In [18]:
G1.show(loop_size=0.2,figsize=(2,2),axes=True,xmax=3,xmin=0,ymin=-1.5,ymax=1.5)
In [19]:
G1.show(figsize=(2,2),xmax=2.8,xmin=0.6,ymin=-1.1,ymax=1.1,loop_size=0.15)

razlika između plot i show

In [20]:
type(G1.plot())
Out[20]:
<class 'sage.plot.graphics.Graphics'>
In [21]:
type(G1.show())
Out[21]:
<class 'NoneType'>

spremanje slika u eps ili pdf format

slika se spremi u tekućem direktoriju ipynb datoteke

In [22]:
G1.plot(pos=pozicije).save('graf.eps')
In [23]:
G1.plot(graph_border=True).save('graf.pdf')

neke opcije s vrhovima

In [24]:
G1.plot(vertex_labels=False).show(figsize=(4,4))
In [25]:
G1.plot(vertex_size=800,graph_border=True,loop_size=0.15).show(figsize=(4,4))
In [26]:
G1.plot(vertex_labels=False,vertex_size=10).show(figsize=(4,4))
In [27]:
G1.plot(vertex_color="yellow").show(figsize=(4,4))
In [28]:
G1.plot(vertex_color=(0,0.9,0.8)).show(figsize=(4,4))
In [29]:
G1.plot(vertex_color={"yellow":[1,3,5],"#a3e4d7":[2,4]}).show(figsize=(4,4))

neke opcije s bridovima

In [30]:
G1.plot(edge_style='dashed',edge_thickness=3,edge_color="blue").show(figsize=(4,4))
In [31]:
G1.edges()
Out[31]:
[(1, 2, None), (1, 5, None), (2, 2, None), (2, 3, None), (3, 4, None), (3, 4, None), (4, 5, None)]
In [32]:
G1.plot(edge_style="dotted",
        edge_colors={"blue":[(1,2),(1,5)],"red":[(4,5),(2,2),(2,3),(3,4)]}).show(figsize=(4,4))
In [33]:
G1.plot(edge_style="dashdot",edge_thickness=1.5).show(figsize=(4,4))

ako želimo bridove s oznakama

višestruki bridovi su problematični

In [34]:
G2=Graph({1:{2:'jedan',5:'dva'},2:{2:'tri',3:'cetiri'},3:{4:'pet',4:'sest'},4:{5:'sedam'}})
G2dva=Graph({1:{2:'jedan',5:'dva'},2:{2:'tri',3:'cetiri'},3:{4:['pet','sest']},4:{5:'sedam'}});
In [35]:
G2.plot(edge_labels=True).show(figsize=(4,4))
In [36]:
G2dva.plot(edge_labels=True).show(figsize=(4,4))
In [37]:
G2.plot(edge_labels=True,color_by_label=True).show(figsize=(4,4))

ako želimo usmjereni graf

In [38]:
G3=DiGraph({1:[2],2:[2,3],3:[4],4:[3,5],5:[1]});G3
Out[38]:
In [39]:
G3.plot(vertex_colors={"yellow":[1,3,5],"#a3e4d7":[2,4]},edge_colors={"red":G3.edges()}).show(figsize=(4,4))
In [40]:
G4=DiGraph({1:{2:'jedan'},2:{2:'dva',3:'tri'},3:{4:'cetiri'},4:{3:'pet',5:'sest'},5:{1:'sedam'}})
G4.plot(edge_labels=True,vertex_colors={"yellow":[1,3,5],"#a3e4d7":[2,4]},edge_colors={"red":G4.edges()}).show(figsize=(4,4))

Interakcija s grafovima

jednostavni primjer interaktivnog crtanja potpunih bipartitnih grafova $K_{m,n}$

In [41]:
@interact 
def Kmn(m=(3,(1..6)), n=(5,(1..6))): 
    graf = graphs.CompleteBipartiteGraph(m,n) 
    graf.show(graph_border=True)

3D crtanje grafova

Tetraedar

In [42]:
tetraedar=graphs.TetrahedralGraph()
In [43]:
tetraedar.show()
In [44]:
tetraedar.show(layout="planar")

možete rotirati sliku (s pritisnutom lijevom tipkom miša), zumiranje s "kotačićem" i mnogo drugih opcija koje dobijete desnim klikom na sliku. Ako napišete help(Graph.plot3d), dobit ćete help o dodatnim opcijama da ih sve ovdje ne spominjemo.

In [45]:
tetraedar.plot3d(vertex_size=0.08,viewer='threejs',online=True)
Out[45]:

Kocka

In [46]:
kocka=graphs.CubeGraph(3)
In [47]:
kocka.show(layout="spring",vertex_size=500)
In [48]:
kocka.plot(layout="planar",vertex_size=500)
Out[48]:
In [49]:
kocka.plot3d(vertex_size=0.08,viewer='threejs',online=True)
Out[49]:

Oktaedar

In [50]:
oktaedar=graphs.OctahedralGraph()
In [51]:
oktaedar.show()
In [52]:
oktaedar.plot(layout="planar")
Out[52]:
In [53]:
oktaedar.plot3d(vertex_size=0.08,viewer='threejs',online=True,aspect_ratio=[1,1,1.5])
Out[53]:

Dodekaedar

In [54]:
dodekaedar=graphs.DodecahedralGraph()
In [55]:
dodekaedar.show()
In [56]:
dodekaedar.plot(layout="planar",vertex_size=30,vertex_labels=False)
Out[56]:
In [57]:
dodekaedar.plot3d(vertex_size=0.08,viewer='threejs',online=True)
Out[57]:

Ikozaedar

In [58]:
ikozaedar=graphs.IcosahedralGraph()
In [59]:
ikozaedar.show()
In [60]:
ikozaedar.plot(layout="planar")
Out[60]:
In [61]:
ikozaedar.plot3d(vertex_size=0.08,viewer='threejs',online=True)
Out[61]:

3D prikazi potpunog grafa

In [62]:
graphs.CompleteGraph(3).plot3d(vertex_size=0.08,viewer='threejs',online=True)
Out[62]:

možemo postaviti vrhove na željene pozicije u prostoru

In [63]:
graphs.CompleteGraph(3).plot3d(pos3d={0:[0,2,0],1:[1,0,0],2:[0,1,0]},vertex_size=0.08,viewer='threejs',online=True)
Out[63]:
In [64]:
graphs.CompleteGraph(7).plot3d(vertex_size=0.08,viewer='threejs',online=True)
Out[64]:
In [ ]: