Skip to content

Commit 899afcc

Browse files
committed
Rename edge nodes to better differentiate edge origin from graph source
1 parent 8826cd5 commit 899afcc

File tree

7 files changed

+80
-73
lines changed

7 files changed

+80
-73
lines changed

Code/Graph+Algorithms/Graph+CondensationGraph.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@ public extension Graph
3131
// add condensation edges
3232
for edge in edgesByID.values
3333
{
34-
guard let sourceSCC = sccHash[edge.source], let targetSCC = sccHash[edge.target] else
34+
guard let originSCC = sccHash[edge.origin],
35+
let destinationSCC = sccHash[edge.destination] else
3536
{
3637
fatalError("mising scc in hash map")
3738
}
3839

39-
if sourceSCC !== targetSCC
40+
if originSCC !== destinationSCC
4041
{
41-
condensationGraph.addEdge(from: sourceSCC.id, to: targetSCC.id)
42+
condensationGraph.addEdge(from: originSCC.id,
43+
to: destinationSCC.id)
4244
}
4345
}
4446

Code/Graph+Algorithms/Graph+StronglyConnectedComponents.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,26 @@ extension Graph
4343
stack.append(node)
4444

4545
// Consider descendants of node
46-
for target in node.descendants
46+
for descendant in node.descendants
4747
{
48-
if let targetMarkings = target.marking
48+
if let descendantMarking = descendant.marking
4949
{
50-
// If target is not on stack, then edge (node, target) is pointing to an SCC already found and must be ignored
51-
if targetMarkings.isOnStack
50+
// If descendant is not on stack, then edge (node, descendant) is pointing to an SCC already found and must be ignored
51+
if descendantMarking.isOnStack
5252
{
53-
// Successor "target" is in stack and hence in the current SCC
54-
nodeMarking.lowLink = min(nodeMarking.lowLink, targetMarkings.index)
53+
// Successor "descendant" is in stack and hence in the current SCC
54+
nodeMarking.lowLink = min(nodeMarking.lowLink, descendantMarking.index)
5555
}
5656
}
57-
else // if target index is undefined then
57+
else // if descendant index is undefined then
5858
{
59-
// Successor "target" has not yet been visited; recurse on it
60-
let targetMarking = findSCCsRecursively(node: target,
61-
index: &index,
62-
stack: &stack,
63-
handleNewSCC: handleNewSCC)
59+
// Successor "descendant" has not yet been visited; recurse on it
60+
let descendantMarking = findSCCsRecursively(node: descendant,
61+
index: &index,
62+
stack: &stack,
63+
handleNewSCC: handleNewSCC)
6464

65-
nodeMarking.lowLink = min(nodeMarking.lowLink, targetMarking.lowLink)
65+
nodeMarking.lowLink = min(nodeMarking.lowLink, descendantMarking.lowLink)
6666
}
6767
}
6868

Code/Graph+Copying.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ extension Graph
3232

3333
for originalEdge in includedEdges ?? Set(edgesByID.values)
3434
{
35-
guard graphCopy.contains(originalEdge.source.id),
36-
graphCopy.contains(originalEdge.target.id) else { continue }
35+
guard graphCopy.contains(originalEdge.origin.id),
36+
graphCopy.contains(originalEdge.destination.id) else { continue }
3737

38-
graphCopy.addEdge(from: originalEdge.source.id,
39-
to: originalEdge.target.id,
38+
graphCopy.addEdge(from: originalEdge.origin.id,
39+
to: originalEdge.destination.id,
4040
count: originalEdge.count)
4141
}
4242

Code/Graph/Graph.swift

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ public class Graph<NodeID: Hashable, NodeValue>
4343
/**
4444
Removes the corresponding ``GraphEdge``, see ``Graph/remove(_:)``
4545
*/
46-
public func removeEdge(from sourceID: NodeID, to targetID: NodeID)
46+
public func removeEdge(from originID: NodeID, to destinationID: NodeID)
4747
{
48-
removeEdge(with: .init(sourceID, targetID))
48+
removeEdge(with: .init(originID, destinationID))
4949
}
5050

5151
/**
5252
Removes the corresponding ``GraphEdge``, see ``Graph/remove(_:)``
5353
*/
54-
public func removeEdge(from source: Node, to target: Node)
54+
public func removeEdge(from origin: Node, to destination: Node)
5555
{
56-
removeEdge(with: .init(source, target))
56+
removeEdge(with: .init(origin, destination))
5757
}
5858

5959
/**
@@ -66,14 +66,14 @@ public class Graph<NodeID: Hashable, NodeValue>
6666
}
6767

6868
/**
69-
Removes the ``GraphEdge``, also removing it from the caches of its ``GraphEdge/source`` and ``GraphEdge/target``
69+
Removes the ``GraphEdge``, also removing it from the caches of its ``GraphEdge/origin`` and ``GraphEdge/destination``
7070

7171
*/
7272
public func remove(_ edge: Edge)
7373
{
7474
// remove from node caches
75-
edge.source.descendants -= edge.target
76-
edge.target.ancestors -= edge.source
75+
edge.origin.descendants -= edge.destination
76+
edge.destination.ancestors -= edge.origin
7777
edge.count = 0
7878

7979
// remove edge itself
@@ -83,35 +83,36 @@ public class Graph<NodeID: Hashable, NodeValue>
8383
/**
8484
Adds a ``GraphEdge`` from one ``GraphNode`` to another, see ``Graph/addEdge(from:to:count:)-mz60``
8585

86-
- Returns: `nil` if no ``GraphNode`` exists for `sourceID` or `targetID`
86+
- Returns: `nil` if no ``GraphNode`` exists for `originID` or `destinationID`
8787
*/
8888
@discardableResult
89-
public func addEdge(from sourceID: NodeID,
90-
to targetID: NodeID,
89+
public func addEdge(from originID: NodeID,
90+
to destinationID: NodeID,
9191
count: Int = 1) -> Edge?
9292
{
93-
guard let source = node(for: sourceID), let target = node(for: targetID) else
93+
guard let origin = node(for: originID),
94+
let destination = node(for: destinationID) else
9495
{
95-
log(warning: "Tried to add edge between non-existing node IDs:\nsource ID = \(sourceID)\ntarget ID = \(targetID)")
96+
log(warning: "Tried to add edge between non-existing node IDs:\norigin ID = \(originID)\ndestination ID = \(destinationID)")
9697
return nil
9798
}
9899

99-
return addEdge(from: source, to: target, count: count)
100+
return addEdge(from: origin, to: destination, count: count)
100101
}
101102

102103
/**
103104
Adds a ``GraphEdge`` from one ``GraphNode`` to another
104105

105-
This also adds `source` and `target` to each other's neighbour caches, see ``GraphNode``
106+
This also adds `origin` and `destination` to each other's neighbour caches, see ``GraphNode``
106107

107-
- Returns: The new ``GraphEdge`` if none existed from `source` to `target`, otherwise the existing ``GraphEdge`` with its ``GraphEdge/count`` increased by the given `count`
108+
- Returns: The new ``GraphEdge`` if none existed from `origin` to `destination`, otherwise the existing ``GraphEdge`` with its ``GraphEdge/count`` increased by the given `count`
108109
*/
109110
@discardableResult
110-
public func addEdge(from source: Node,
111-
to target: Node,
111+
public func addEdge(from origin: Node,
112+
to destination: Node,
112113
count: Int = 1) -> Edge
113114
{
114-
let edgeID = Edge.ID(source, target)
115+
let edgeID = Edge.ID(origin, destination)
115116

116117
if let edge = edgesByID[edgeID]
117118
{
@@ -123,32 +124,32 @@ public class Graph<NodeID: Hashable, NodeValue>
123124
}
124125
else
125126
{
126-
let edge = Edge(from: source, to: target, count: count)
127+
let edge = Edge(from: origin, to: destination, count: count)
127128
edgesByID[edgeID] = edge
128129

129130
// add to node caches
130-
source.descendants += target
131-
target.ancestors += source
131+
origin.descendants += destination
132+
destination.ancestors += origin
132133

133134
return edge
134135
}
135136
}
136137

137138
/**
138-
The ``GraphEdge`` from `source` to `target` if it exists, otherwise `nil`
139+
The ``GraphEdge`` from `origin` to `destination` if it exists, otherwise `nil`
139140
*/
140-
public func edge(from source: Node, to target: Node) -> Edge?
141+
public func edge(from origin: Node, to destination: Node) -> Edge?
141142
{
142-
guard contains(source), contains(target) else { return nil }
143-
return edge(from: source.id, to: target.id)
143+
guard contains(origin), contains(destination) else { return nil }
144+
return edge(from: origin.id, to: destination.id)
144145
}
145146

146147
/**
147148
The ``GraphEdge`` between the corresponding nodes if it exists, otherwise `nil`
148149
*/
149-
public func edge(from sourceID: NodeID, to targetID: NodeID) -> Edge?
150+
public func edge(from originID: NodeID, to destinationID: NodeID) -> Edge?
150151
{
151-
edgesByID[.init(sourceID, targetID)]
152+
edgesByID[.init(originID, destinationID)]
152153
}
153154

154155
/**

Code/Graph/GraphEdge.swift

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import SwiftyToolz
33
/**
44
Directed connection of two ``GraphNode``s in a ``Graph``
55

6-
A `GraphEdge` has a direction and goes from its ``GraphEdge/source`` to its ``GraphEdge/target``
6+
A `GraphEdge` has a direction and goes from its ``GraphEdge/origin`` to its ``GraphEdge/destination``
77

8-
A `GraphEdge` is `Identifiable` by its ``GraphEdge/id-swift.property``, which is a combination of the ``GraphNode/id``s of ``GraphEdge/source`` and ``GraphEdge/target``.
8+
A `GraphEdge` is `Identifiable` by its ``GraphEdge/id-swift.property``, which is a combination of the ``GraphNode/id``s of ``GraphEdge/origin`` and ``GraphEdge/destination``.
99

1010
Edges are owned and managed by a ``Graph``. You create, query and destroy them via a given ``Graph``:
1111

@@ -35,36 +35,36 @@ public class GraphEdge<NodeID: Hashable, NodeValue>: Identifiable, Hashable
3535
// MARK: - Identity
3636

3737
/**
38-
The edge's `ID` combines the ``GraphNode/id``s of ``GraphEdge/source`` and ``GraphEdge/target``
38+
The edge's `ID` combines the ``GraphNode/id``s of ``GraphEdge/origin`` and ``GraphEdge/destination``
3939
*/
40-
public var id: ID { ID(source, target) }
40+
public var id: ID { ID(origin, destination) }
4141

4242
/**
43-
An edge's `ID` combines the ``GraphNode/id``s of its ``GraphEdge/source`` and ``GraphEdge/target``
43+
An edge's `ID` combines the ``GraphNode/id``s of its ``GraphEdge/origin`` and ``GraphEdge/destination``
4444
*/
4545
public struct ID: Hashable
4646
{
47-
internal init(_ source: Node, _ target: Node)
47+
internal init(_ origin: Node, _ destination: Node)
4848
{
49-
self.init(source.id, target.id)
49+
self.init(origin.id, destination.id)
5050
}
5151

52-
internal init(_ sourceID: NodeID, _ targetID: NodeID)
52+
internal init(_ originID: NodeID, _ destinationID: NodeID)
5353
{
54-
self.sourceID = sourceID
55-
self.targetID = targetID
54+
self.originID = originID
55+
self.destinationID = destinationID
5656
}
5757

58-
public let sourceID: NodeID
59-
public let targetID: NodeID
58+
public let originID: NodeID
59+
public let destinationID: NodeID
6060
}
6161

6262
// MARK: - Basics
6363

64-
internal init(from source: Node, to target: Node, count: Int = 1)
64+
internal init(from origin: Node, to destination: Node, count: Int = 1)
6565
{
66-
self.source = source
67-
self.target = target
66+
self.origin = origin
67+
self.destination = destination
6868

6969
self.count = count
7070
}
@@ -77,17 +77,17 @@ public class GraphEdge<NodeID: Hashable, NodeValue>: Identifiable, Hashable
7777
public internal(set) var count: Int
7878

7979
/**
80-
The origin ``GraphNode`` at which the edge starts / goes out
80+
The origin ``GraphNode`` at which the edge starts / from which it goes out
8181
*/
82-
public let source: Node
82+
public let origin: Node
8383

8484
/**
85-
The destination ``GraphNode`` at which the edge ends / goes in
85+
The destination ``GraphNode`` at which the edge ends / to which it goes in
8686
*/
87-
public let target: Node
87+
public let destination: Node
8888

8989
/**
90-
A shorthand for the `source`- and `target` type `GraphNode<NodeID, NodeValue>`
90+
A shorthand for the `origin`- and `destination` type `GraphNode<NodeID, NodeValue>`
9191
*/
9292
public typealias Node = GraphNode<NodeID, NodeValue>
9393
}

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ let node = graph.insert(IdentifiableValue()) // node.id == node.value.id
5656
let graph = Graph<String, Int> { "id\($0)" }
5757
let node1 = graph.insert(1)
5858
let node2 = graph.insert(2)
59-
let edge = graph.addEdge(from: node1,
60-
to: node2) // edge.source === node1, edge.target === node2
59+
60+
// two ways to add an edge:
61+
let edge = graph.addEdge(from: node1, to: node2)
62+
let edge = graph.addEdge(from: node1.id, to: node2.id)
63+
64+
// same result: edge.origin === node1, edge.destination === node2
6165
```
6266

6367
### Specify Edge Counts
@@ -71,7 +75,7 @@ graph.addEdge(from: node1, to: node2, count: 2) // edge count is 42
7175

7276
### Remove Edges
7377

74-
A `GraphEdge<NodeID: Hashable, NodeValue>` has its own `ID` type which combines the `NodeID`s of the edge's source- and target nodes. In the context of a `Graph` or `GraphEdge`, you can create edge IDs easily in two ways:
78+
A `GraphEdge<NodeID: Hashable, NodeValue>` has its own `ID` type which combines the `NodeID`s of the edge's origin- and destination nodes. In the context of a `Graph` or `GraphEdge`, you can create edge IDs easily in two ways:
7579

7680
```swift
7781
let edgeID_A = Edge.ID(node1, node2)
@@ -171,4 +175,4 @@ The above image was created with [Codeface](https://codeface.io).
171175

172176
From version/tag 0.1.0 on, SwiftNodes adheres to [semantic versioning](https://semver.org). So until it has reached 1.0.0, its API may still break frequently, but this will be expressed in version bumps.
173177

174-
SwiftNodes is already being used in production, but [Codeface](https://codeface.io) is still its primary client. SwiftNodes will move to version 1.0.0 as soon as its basic practicality and conceptual soundness have been validated by serving multiple real-world clients.
178+
SwiftNodes is already being used in production, but [Codeface](https://codeface.io) is still its primary client. SwiftNodes will move to version 1.0.0 as soon as its basic practicality and conceptual soundness have been validated by serving multiple real-world clients.

Tests/SwiftNodesTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ class SwiftNodesTests: XCTestCase {
126126
XCTAssertEqual(edge12.count, 1)
127127
XCTAssertIdentical(edge12, graph.addEdge(from: "id1", to: "id2"))
128128
XCTAssertEqual(edge12.count, 2)
129-
XCTAssertIdentical(edge12.source, node1)
130-
XCTAssertIdentical(edge12.target, node2)
129+
XCTAssertIdentical(edge12.origin, node1)
130+
XCTAssertIdentical(edge12.destination, node2)
131131

132132
XCTAssertFalse(node1.isSink)
133133
XCTAssert(node1.descendants.contains(node2))

0 commit comments

Comments
 (0)