You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+11-7Lines changed: 11 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -60,15 +60,17 @@ let node1 = graph.insert(1)
60
60
let node2 = graph.insert(2)
61
61
62
62
// two ways to add an edge:
63
-
let edge = graph.addEdge(from: node1, to: node2)
64
-
let edge = graph.addEdge(from: node1.id, to: node2.id)
63
+
let edge = graph.addEdge(from: node1, to: node2)// by nodes
64
+
_= graph.addEdge(from: node1.id, to: node2.id)// by node IDs
65
65
66
66
// same result: edge.origin === node1, edge.destination === node2
67
67
```
68
68
69
+
An `edge` is directed and goes from its `edge.origin` node to its `edge.destination` node.
70
+
69
71
### Specify Edge Counts
70
72
71
-
Every `edge` has an integer count accessible via `edge.count`. It is more specifically a "count" rather than a "weight", as it inceases when the same edge is added again. By default, a new edge has count 1 and adding it again increases the count by 1. But you can specify different counts when adding an edge:
73
+
Every `edge` has an integer count accessible via `edge.count`. It is more specifically a "count" rather than a "weight", as it increases when the same edge is added again. By default, a new edge has `count` 1 and adding it again increases the `count` by 1. But you can specify different counts when adding an edge:
72
74
73
75
```swift
74
76
graph.addEdge(from: node1, to: node2, count: 40) // edge count is 40
@@ -77,7 +79,7 @@ graph.addEdge(from: node1, to: node2, count: 2) // edge count is 42
77
79
78
80
### Remove Edges
79
81
80
-
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:
82
+
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:
Many algorithms produce a variant of a given graph. Rather than modifying the original graph, SwiftNodes suggests to copy it.
126
128
127
-
A `graph.copy()` is identical to the original `graph` in IDs, values and structure but contains its own new node- and edge objects. You may also copy just a subset of a graph and limit the included edges and/or nodes:
129
+
A `graph.copy()` is identical to the original `graph` in IDs, values and structure but contains its own new node- and edge objects. You may also copy just a subset of a `graph` and limit the included edges and/or nodes:
128
130
129
131
```swift
130
132
let subsetCopy = graph.copy(includedNodes: [node2, node3],
131
133
includedEdges: [edge23])
132
134
```
133
135
134
-
## How to Write Graph Algorithms
136
+
## How Algorithms Mark Nodes
137
+
138
+
Many graph algorithms do associate little intermediate results with individual nodes. The literature often refers to this as "marking" a node. The most prominent example is marking a node as visited while traversing a potentially cyclic graph. Some algorithms write multiple different markings to nodes.
135
139
136
-
To support algorithms, every `node` has an optional property `node.marking` which can store a `GraphNode.Marking` object. A marking can be used to generally mark a node, but it also has two integers and two boolean flags that algorithms can use in whatever way they need.
140
+
To be able to achieve optimal performance (time- and space efficiency) in practice, algorithms must be able to mark nodes directly instead of implementing node markings via hash maps. So in SwiftNodes, every `node` has an optional property `node.marking` which can store a `GraphNode.Marking` object. The marking itself can be used to generally mark a node, but it also contains four general-purpose properties (two integer numbers and two boolean flags) that algorithms can use in whatever way they need.
137
141
138
142
[Graph+Node.Marking.swift](https://github.com/codeface-io/SwiftNodes/blob/master/Code/Graph%2BAlgorithms/Graph%2BNode.Marking.swift) contains some conveniences for marking and unmarking nodes. Also have a look at how the [included algorithms](https://github.com/codeface-io/SwiftNodes/tree/master/Code/Graph%2BAlgorithms) make use of node markings.
0 commit comments