SWGraphs 0.3.5

SWGraphs 0.3.5

测试已测试
语言语言 SwiftSwift
许可协议 MIT
发布上次发布2017年3月
SwiftSwift 版本3.0
SPM支持 SPM

Vihlayew Alex维护。



SWGraphs 0.3.5

logo

SWGraphs 是一个用 Swift 编写的图数据结构、操作和算法库。

示例

要运行示例项目,首先克隆存储库,然后在 Example 目录中运行 pod install

要求

SWGraph 需要 Swift 3 支持。(Xcode 8+)

安装

SWGraphs 通过 CocoaPods 提供。要安装它,只需将以下行添加到 Podfile:

pod "SWGraphs"

使用方法

导入

不要忘记添加 import

import SWGraphs

使用关联矩阵初始化

有关关联矩阵的更多信息,请参阅 维基百科

let incidenceMatrix = [ [1,0,-1,1],
                        [-1,1,0,0],
                        [0,-1,1,0],
                        [0,0,0,-1] ]
let graph = SWGGraph(with: incidenceMatrix)

图类型

每个图都有一个类型,定义为 SWGGraphType

public enum SWGGraphType {
    case Oriented
    case Unoriented
}

请注意,类型是只读属性,因此不能手动更改。

图属性

半径

graph.graphRadius // Double

直径

graph.graphDiameter // Double

中心

graph.centers // [Int]

边内部初始化,不能手动初始化。

边的操作

从图中获取边

let edges = graph.edges // [SWGEdge]

与图类型类似,边的属性是只读的,只能使用以下列出的函数进行修改

向图中添加边

graph.addEdge(start: 2, end: 3, value: nil) // Adds edge from vertex 2 to 3
graph.addEdge(start: 16, end: 7, value: 13) // Adds edge from vertex 16 to 7 with value of 13

从图中移除边

graph.removeEdge(at: 3) // Removes edge with number 3
graph.removeLastEdge() // Removes last edge

获取边的顶点和连接

获取起始和结束顶点的编号

let startIndex = edge.startVertexNumber // Int
let endIndex = edge.endVertexNumber // Int

获取起始和结束的连接

let startConnections = edge.startVertexConnections // [SWGEdge]
let endConnections = edge.endVertexConnections // [SWGEdge]

顶点

获取顶点

从图中获取顶点

let vertices = graph.vertexes // [SWGVertex]

顶点由 SWGVertex 表示

public struct SWGVertex: CustomStringConvertible {

    public var description: String {
        return "SWGVertex(Number: \(self.number), Connections: [ \(self.connectedVertexes) ])"
    }

    public var number: Int
    public var connectedVertexes: [SWGVertexConnection]
}

顶点的 connectedVertexesSWGVertexConnection 的数组表示

public struct SWGVertexConnection: CustomStringConvertible {

    public var description: String {
        return "(\(self.direction) connection to \(self.connectedToVertex) with value \(self.connectionValue))"
    }

    public var direction: SWGVertexConnectionDirection
    public var connectedToVertex: Int
    public var connectionValue: Int?
}

连接的方向类型为 SWGVertexConnectionDirection

public enum SWGVertexConnectionDirection {
    case In
    case Out
}

顶点信息和函数

顶点类型

vertex.isLeaf // Bool
vertex.isSink // Bool
vertex.isSource // Bool
vertex.isIsolated // Bool

获取两个顶点之间的距离

graph.lengthInGraph(from: 3, to: 4) // Double

获取顶点离心率

graph.getEccentricity(forVertex: 3) // Double

作者

VihlayewAlex, [email protected]

许可证

SWGraphs 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。