Skip to content

Commit 9b2efc1

Browse files
committed
Edit README
1 parent bbcc927 commit 9b2efc1

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,17 @@ let node1 = graph.insert(1)
6060
let node2 = graph.insert(2)
6161

6262
// 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
6565

6666
// same result: edge.origin === node1, edge.destination === node2
6767
```
6868

69+
An `edge` is directed and goes from its `edge.origin` node to its `edge.destination` node.
70+
6971
### Specify Edge Counts
7072

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

7375
```swift
7476
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
7779

7880
### Remove Edges
7981

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

8284
```swift
8385
let edgeID_A = Edge.ID(node1, node2)
@@ -124,16 +126,18 @@ graph.sort { $0.id < $1.id } // graph.values == [3, 5]
124126

125127
Many algorithms produce a variant of a given graph. Rather than modifying the original graph, SwiftNodes suggests to copy it.
126128

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

129131
```swift
130132
let subsetCopy = graph.copy(includedNodes: [node2, node3],
131133
includedEdges: [edge23])
132134
```
133135

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.
135139

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.
137141

138142
[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.
139143

0 commit comments

Comments
 (0)