设置节点连接 - SetNodeConnection
函数简介
设置两个节点间的连接关系。可新增/更新边(并设置权重),或删除对应的边。
当 canConnect 为 true 且 weight 为 -1 时,自动使用欧几里得距离作为边权重。
接口名称
SetNodeConnection
DLL调用
int32_t SetNodeConnection(int64_t instance, int64_t graphPtr, char* from,
char* to, bool canConnect, double weight);
参数说明
参数名 | 类型 | 说明 |
---|---|---|
instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
graphPtr | 长整数型 | 图的指针。 |
from | 字符串 | 起始节点名称。 |
to | 字符串 | 目标节点名称。 |
canConnect | 布尔型 | 是否可以连接(true 创建/更新边;false 删除边)。 |
weight | 双精度型 | 连接权重(canConnect 为 true 时使用;-1 表示使用欧氏距离)。 |
示例
// 创建OLA实例
int64_t instance = CreateCOLAPlugInterFace();
int64_t g = CreateGraphFromCoordinates(instance,
"[{\"name\":\"A\",\"x\":0,\"y\":0},{\"name\":\"B\",\"x\":3,\"y\":4}]",
false, 0.0, true);
// 建立 A->B 的连接,使用欧氏距离作为权重
int32_t ok = SetNodeConnection(instance, g, "A", "B", true, -1.0);
printf("SetNodeConnection: %d\n", ok);
// 断开 A->B 的连接
ok = SetNodeConnection(instance, g, "A", "B", false, 0.0);
DeleteGraph(instance, g);
DestroyCOLAPlugInterFace(instance);
返回值
成功返回1,失败返回0。
注意事项
- 确保 graphPtr 是有效的图指针。
- 节点必须已存在于图中。
- 设置连接关系会影响路径计算。
- 如果 canConnect 为 false,会删除对应的边。