<!--
{
  "availability" : [
    "iOS: 9.0.0 -",
    "iPadOS: 9.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.11.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "GameplayKit",
  "identifier" : "/documentation/GameplayKit/GKGraph/connectToLowestCostNode(node:bidirectional:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "GameplayKit"
    ],
    "preciseIdentifier" : "c:objc(cs)GKGraph(im)connectNodeToLowestCostNode:bidirectional:"
  },
  "title" : "connectToLowestCostNode(node:bidirectional:)"
}
-->

# connectToLowestCostNode(node:bidirectional:)

Adds a node to the graph, connecting it to the node already in the graph for which the connection has the lowest cost.

```
func connectToLowestCostNode(node: GKGraphNode, bidirectional: Bool)
```

## Parameters

`node`

A graph node object.

`bidirectional`

<doc://com.apple.documentation/documentation/Swift/true> to create connections in both directions; <doc://com.apple.documentation/documentation/Swift/false> to create just a single connection from the nearest node to the newly added node.

## Discussion

This method uses the [`cost(to:)`](/documentation/GameplayKit/GKGraphNode/cost(to:)) method of the specified node to find the node already in the graph that is most easily reached from the new node. For nodes that contain geometry information ([`GKGraphNode2D`](/documentation/GameplayKit/GKGraphNode2D) or [`GKGridGraphNode`](/documentation/GameplayKit/GKGridGraphNode) objects), cost is by default based on distance, so this method connects the new node to the geometrically closest node already in the graph. If you create a custom [`GKGraphNode`](/documentation/GameplayKit/GKGraphNode) subclass, this method selects the “closest” node according to whatever algorithm you implement in your [`cost(to:)`](/documentation/GameplayKit/GKGraphNode/cost(to:)) method.

Using this method with an instance of the [`GKGraphNode`](/documentation/GameplayKit/GKGraphNode) base class is not recommended.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)