Skip to content

Commit 4f1b951

Browse files
committed
Add function to create subgraph
1 parent d0dd59e commit 4f1b951

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Code/Graph/Graph+SubGraph.swift

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import OrderedCollections
2+
3+
public extension Graph
4+
{
5+
func subGraph(nodes: OrderedSet<Node>) -> Graph where NodeValue: Identifiable, NodeValue.ID == NodeID
6+
{
7+
let nodeIDs = Set(nodes.map { $0.id })
8+
9+
var subGraph = Graph(values: nodes.map { $0.value })
10+
11+
for edge in edges
12+
{
13+
if nodeIDs.contains(edge.originID) && nodeIDs.contains(edge.destinationID)
14+
{
15+
subGraph.addEdge(from: edge.originID,
16+
to: edge.destinationID,
17+
count: edge.count)
18+
}
19+
}
20+
21+
return subGraph
22+
}
23+
}

0 commit comments

Comments
 (0)