Latest 0.3.5
License MIT
Platforms ios 8.0


CI Status

SWGraphs is a graph data structure, operations and algorithms library, written with Swift.


To run the example project, clone the repo, and run pod install from the Example directory first.


SWGraph requires Swift 3 support. (Xcode 8+)


SWGraphs is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "SWGraphs"



Don’t forget to add import:

import SWGraphs


Initializing with incidence matrix

More about incidence matrixes on Wikipedia.

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

Graph types

Every graph have it’s type, defined as SWGGraphType:

public enum SWGGraphType {
    case Oriented
    case Unoriented

Notice that type is get-only property so it can not be changed manually.

Graph properties


graph.graphRadius // Double


graph.graphDiameter // Double


graph.centers // [Int]


Edges are initialized in graph internaly and must not be initialized manually.

Operations with edges

Getting edges from graph:

let edges = graph.edges // [SWGEdge]

Like a graph types, edges age get-only and can be modified only with functions listed below:

Adding edges to graph:

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

Removing edges from graph:

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

Getting edge’s vertices and connections

Getting start and end vertices numbers:

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

Getting start and end connections:

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


Getting vertices

Getting vertices from graph:

let vertices = graph.vertexes // [SWGVertex]

Vertex is represented by 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]

Vertexe’s connectedVertexes are represented by array of SWGVertexConnection:

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?

Connection’s direction is of type SWGVertexConnectionDirection:

public enum SWGVertexConnectionDirection {
    case In
    case Out

Vertices info and methods

Vertices types:

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

Getting distance between two vertices:

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

Getting vertex eccentricity:

graph.getEccentricity(forVertex: 3) // Double


VihlayewAlex, [email protected]


SWGraphs is available under the MIT license. See the LICENSE file for more info.

Latest podspec

    "name": "SWGraphs",
    "version": "0.3.5",
    "summary": "A Swift library for operations on graphs.",
    "description": "A Swift library for operations on graphs. This description will be extended.",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "authors": {
        "VihlayewAlex": "[email protected]"
    "source": {
        "git": "",
        "tag": "0.3.5"
    "social_media_url": "",
    "platforms": {
        "ios": "8.0"
    "source_files": "SWGraphs/Classes/**/*",
    "pushed_with_swift_version": "3.0"

Pin It on Pinterest

Share This