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
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)
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.
Radius:
graph.graphRadius // Double
Diameter:
graph.graphDiameter // Double
Centers:
graph.centers // [Int]
Availability matrix:
graph.getAvailabilityMatrix() // [[Int]]
Lengthы matrix:
graph.getLengthsMatrix() // [[Int]]
DFS:
graph.getDFSvertexes(fromVertex: 2) // [SWGVertex]
BFS:
graph.getBFSvertexes(fromVertex: 2) // [SWGVertex]
Edges are initialized in graph internaly and must not be initialized manually.
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 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 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 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.