Skip to content

添加边 - AddEdge

函数简介

向指定的图中添加一条边,支持有向边和无向边,可以设置边的权重。

接口名称

AddEdge

DLL调用

c
int32_t AddEdge(int64_t instance, int64_t graphPtr, char* from, char* to, double weight, bool isDirected);

参数说明

参数名类型说明
instance长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
graphPtr长整数型图的指针,由 CreateGraph 接口返回。
from字符串边的起点节点名称。
to字符串边的终点节点名称。
weight双精度浮点数边的权重,用于最短路径计算,当权重为0时删除对应边。
isDirected整数型是否为有向边,true 表示有向边,false 表示无向边。

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
long graphPtr = ola.CreateGraph("[{\"directed\":false,\"from\":\"上海\",\"to\":\"北京\",\"weight\":3.0},{\"directed\":false,\"from\":\"北京\",\"to\":\"深圳\",\"weight\":5.0}]");
if (graphPtr != 0) {
    int ret = ola.AddEdge(graphPtr, "深圳", "广州", 2.0, false);
    ola.DeleteGraph(graphPtr);
}
csharp
using OLAPlug;

var ola = new OLAPlugServer();
long graphPtr = ola.CreateGraph(@"[{"directed":false,"from":"上海","to":"北京","weight":3.0},{"directed":false,"from":"北京","to":"深圳","weight":5.0}]");
if (graphPtr != 0)
{
    int ret = ola.AddEdge(graphPtr, "深圳", "广州", 2.0, false);
    ola.DeleteGraph(graphPtr);
}
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
graph_ptr = ola.CreateGraph(r'[{"directed":false,"from":"上海","to":"北京","weight":3.0},{"directed":false,"from":"北京","to":"深圳","weight":5.0}]')
if graph_ptr != 0:
    ret = ola.AddEdge(graph_ptr, "深圳", "广州", 2.0, False)
    ola.DeleteGraph(graph_ptr)
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
long graphPtr = ola.CreateGraph("[{\"directed\":false,\"from\":\"上海\",\"to\":\"北京\",\"weight\":3.0},{\"directed\":false,\"from\":\"北京\",\"to\":\"深圳\",\"weight\":5.0}]");
if (graphPtr != 0) {
    ola.AddEdge(graphPtr, "深圳", "广州", 2.0, false);
    ola.DeleteGraph(graphPtr);
}
cpp
var ola = com("OlaPlug.OlaSoft")
var graphPtr = ola.CreateGraph("[{\"directed\":false,\"from\":\"上海\",\"to\":\"北京\",\"weight\":3.0},{\"directed\":false,\"from\":\"北京\",\"to\":\"深圳\",\"weight\":5.0}]")
if(graphPtr) {
    ola.AddEdge(graphPtr, "深圳", "广州", 2.0, false);
    ola.DeleteGraph(graphPtr)
}
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
graphPtr = ola.CreateGraph("[{\"directed\":false,\"from\":\"上海\",\"to\":\"北京\",\"weight\":3.0},{\"directed\":false,\"from\":\"北京\",\"to\":\"深圳\",\"weight\":5.0}]")
If graphPtr <> 0 Then
    ola.AddEdge(graphPtr, "深圳", "广州", 2.0, false);
    ola.DeleteGraph(graphPtr)
End If
text
.局部变量 ola, OLAPlug
ola.创建 ()
graphPtr = ola.CreateGraph (“[{\“directed\“:false,\“from\“:\“上海\“,\“to\“:\“北京\“,\“weight\“:3.0},{\“directed\“:false,\“from\“:\“北京\“,\“to\“:\“深圳\“,\“weight\“:5.0}]“)
.如果真 (graphPtr ≠ 0)
    ola.AddEdge(graphPtr, "深圳", "广州", 2.0, false);
    ola.DeleteGraph (graphPtr)
.如果真结束
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var graphPtr = ola.CreateGraph("[{\"directed\":false,\"from\":\"上海\",\"to\":\"北京\",\"weight\":3.0},{\"directed\":false,\"from\":\"北京\",\"to\":\"深圳\",\"weight\":5.0}]");
if(graphPtr){
    ola.AddEdge(graphPtr, "深圳", "广州", 2.0, false);
    ola.DeleteGraph(graphPtr);
}
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
长整数 graphPtr = ola.CreateGraph("[{\"directed\":false,\"from\":\"上海\",\"to\":\"北京\",\"weight\":3.0},{\"directed\":false,\"from\":\"北京\",\"to\":\"深圳\",\"weight\":5.0}]")
如果真 (graphPtr ≠ 0)
{
    ola.AddEdge(graphPtr, "深圳", "广州", 2.0, false);
    ola.DeleteGraph(graphPtr)
}
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
long graphPtr = ola.CreateGraph("[{\"directed\":false,\"from\":\"上海\",\"to\":\"北京\",\"weight\":3.0},{\"directed\":false,\"from\":\"北京\",\"to\":\"深圳\",\"weight\":5.0}]");
if (graphPtr != 0) {
    ola.AddEdge(graphPtr, "深圳", "广州", 2.0, false);
    ola.DeleteGraph(graphPtr);
}

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long graphPtr = CreateGraph(instance, "[{\"directed\":false,\"from\":\"上海\",\"to\":\"北京\",\"weight\":3.0},{\"directed\":false,\"from\":\"北京\",\"to\":\"深圳\",\"weight\":5.0}]");
if (graphPtr != 0) {
    int ret = ola.AddEdge(graphPtr, "深圳", "广州", 2.0, false);
    DeleteGraph(instance, graphPtr);
}
csharp
using System.Runtime.InteropServices;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();

long instance = CreateCOLAPlugInterFace();
long graphPtr = CreateGraph(instance, @"[{"directed":false,"from":"上海","to":"北京","weight":3.0},{"directed":false,"from":"北京","to":"深圳","weight":5.0}]");
if (graphPtr != 0) {
    int ret = ola.AddEdge(graphPtr, "深圳", "广州", 2.0, false);
    DeleteGraph(instance, graphPtr);
}
python
from ctypes import CDLL, c_int, c_int64

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
graph_ptr = ola.CreateGraph(instance, b'[{"directed":false,"from":"上海","to":"北京","weight":3.0},{"directed":false,"from":"北京","to":"深圳","weight":5.0}]')
if graph_ptr:
    ret = ola.AddEdge(graph_ptr, "深圳", "广州", 2.0, False)
    ola.DeleteGraph(instance, graph_ptr)

返回值

返回值说明
1成功。
0失败。

注意事项

项目说明
from 和 to 节点在图中存在确保 from 和 to 节点在图中存在。
路径权重值应为正数,用于最短路径计算。
有向边只允许从 from 到 to 的方向有向边只允许从 from 到 to 的方向,无向边允许双向通行。
重复添加相同的边会覆盖之前的权重设置重复添加相同的边会覆盖之前的权重设置。