Select Page
Latest 0.3.5 https://github.com/VihlayewAlex/SWGraphs MIT ios 8.0 VihlayewAlex

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

``pod "SWGraphs"``

## Usage

### 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

``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:

``````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]

### 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": "https://github.com/VihlayewAlex/SWGraphs",
"type": "MIT",
},
"authors": {
"VihlayewAlex": "[email protected]/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */"
},
"source": {
"git": "https://github.com/VihlayewAlex/SWGraphs.git",
"tag": "0.3.5"
},
"social_media_url": "https://vk.com/vihlayew",
"platforms": {
"ios": "8.0"
},
"source_files": "SWGraphs/Classes/**/*",
"pushed_with_swift_version": "3.0"
}```