SWGraphs 0.3.5

SWGraphs 0.3.5

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Mar 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Vihlayew Alex.



SWGraphs 0.3.5

logo

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

Example

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

Requirements

SWGraph requires Swift 3 support. (Xcode 8+)

Installation

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

pod "SWGraphs"

Usage

Import

Don’t forget to add import:

import SWGraphs

Graphs

Initializing with incidence matrix

More about incidence matrixes on Wikipedia.

let incidenceMatrix = [ [1,0,-1,1],
                        [-1,1,0,0],
                        [0,-1,1,0],
                        [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

Radius:

graph.graphRadius // Double

Diameter:

graph.graphDiameter // Double

Centers:

graph.centers // [Int]

Edges

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]

Vertices

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

Author

VihlayewAlex, [email protected]

License

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