SageMath简明教程-安装

  • 下载: 官网 https://www.sagemath.org/

  • Windows用户安装参考 https://wiki.sagemath.org/SageWindows

  • 语言: Python 2 或 Python 3

  • 打开

    • SageMath

    • SageMath Notebook

      • Jupyter Notebook

      • 电脑系统语言设置成英文

      • 窗口可以最小化但是不要关,否则服务器会中断

  • 基础教程

    • 中文 https://www.lainme.com/doku.php/topic/sage/start

    • 英语 https://doc.sagemath.org/html/en/tutorial/index.html

几种常见定义图的方法

1. Sage内置

  • 参考链接 https://doc.sagemath.org/html/en/reference/graphs/sage/graphs/graph_generators.html
  • 示例 1
C= graphs.CompleteGraph(8)
C.show()
K8
  • 示例 2
PG=graphs.PetersenGraph()
PG.show()
Petersen Graph

2. 根据点的邻接性输入

  • 也可以根据邻接矩阵
  • 示例 3
H= Graph({0:[1,4,5],1:[2,6],2:[3,7],3:[4,8],4:[9],5:[7,8],6:[8,9],7:[9]})
H.show()

可用sage检验上述两个图是否同构

H.is_isomorphic(PG)

[Out] True

3. 添加点和边

  • 参考链接 https://doc.sagemath.org/html/en/reference/graphs/sage/graphs/generic_graph.html
  • 添加一个点
G.add_vertex(10) 
  • 添加多个点
G.add_vertices([10,11,12])
  • 添加一条边
G.add_edge((10, 11))
  • 添加多条边
G.add_edges([(10,1),(11,12),(11,0)])
  • 示例 4
g = PG
g.add_vertices([10,11,12])
g.add_edges([(10,1),(11,12),(11,0)])
g.show()

实例

  • 定义circular complete graph
def Circular_Clique(k,d):
    if k < 2*d:
        print("Input illegal")
    else:
        dic = {}
        G = Graph(dic)
        G.add_vertices(range(0,k))
        for u in range(0,k):
            for v in range(0,k):
                if abs(u-v)>= d and abs(u-v) <= k-d:
                    G.add_edge((u, v))
        return G

G1 = Circular_Clique(7,3)
G1.show(vertex_color = "red",layout = 'circular')


![](https://upload-images.jianshu.io/upload_images/24723959-1fdb80c902554d93.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

###  常用函数调用
**函数的调用一定要记得参考官网上的用法,有些不能直接用,需要导入(import) 一些包, 用法中会提及, 要多留意.**
- 参考链接
https://doc.sagemath.org/html/en/reference/graphs/sage/graphs/graph.html

- 染色数

```swift
PG.chormatic_number()

[Out] 3

  • 最大独立集
from sage.graphs.independent_sets import IndependentSets
PG.independent_set()

[Out] [0, 3, 6, 7]

  • 围长
PG.girth()

[Out] 5


实例

定义circular coloring

circular coloring在sage中并没有直接的定义, 我们可以通过homomorphism来转化


所以 G has a k/d-coloring 等价于 G has a homomorphism to K_{k/d}

  • 定义circular complete graph
def Circular_Clique(k,d):
    if k < 2*d:
        print("Input illegal")
    else:
        dic = {}
        G = Graph(dic)
        G.add_vertices(range(0,k))
        for u in range(0,k):
            for v in range(0,k):
                if abs(u-v)>= d and abs(u-v) <= k-d:
                    G.add_edge((u, v))
        return G
  • 定义函数判断图G是否是k/d-colorable
def Circular_Colorable(G,k,d):
    return  G.has_homomorphism_to(Circular_Complete_Graph(k,d)) is not False
  • 已知长度为7的圈是circular 5/2-colorable的, 我们用刚刚定义的函数验证是否正确.
G2 = graphs.CycleGraph(7)
Circular_Colorable(G2,5,2)

[Out] True

定义circular coloring

circular coloring在sage中并没有直接的定义, 我们可以通过homomorphism来转化


所以 G has a k/d-coloring 等价于 G has a homomorphism to K_{k/d}

你可能感兴趣的:(SageMath简明教程-安装)