创建图 - CreateGraph
函数简介
创建一个新的图数据结构,可以通过JSON格式初始化图的节点和边。 JSON数据类型解析:
[{
"directed": false,//是否单向,为false表示能从上海到北京,也可以从北京到上海
"from": "上海",//起点
"to": "北京",//终点
"weight": 3.0//距离权重
},
{
"directed": true,//是否单向,为true表示只能从上海到深圳,无法从深圳到上海
"from": "深圳",//起点
"to": "上海",//终点
"weight": 3.0//距离权重
}
]
接口名称
CreateGraph
DLL调用
int64_t CreateGraph(int64_t instance, char* json);
参数说明
参数名 | 类型 | 说明 |
---|---|---|
instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
json | 字符串 | 图的JSON表示,包含节点和边的信息,传空创建一个空的图对象 |
示例
// 创建OLA实例
int64_t instance = CreateCOLAPlugInterFace();
// 定义图的JSON表示
char* json = "[{\"directed\":true,\"from\":\"北京\",\"to\":\"上海\",\"weight\":5.0},{\"directed\":false,\"from\":\"上海\",\"to\":\"广州\",\"weight\":3.0},{\"directed\":true,\"from\":\"广州\",\"to\":\"深圳\",\"weight\":3.0}]";
// 创建图
int64_t graphPtr = CreateGraph(instance, json);
// 释放资源
DestroyCOLAPlugInterFace(instance);
返回值
返回图的指针,用于后续的图操作。如果创建失败返回0。
注意事项
- 返回的图指针需要调用DeleteGraph释放内存
- 确保JSON格式正确,否则可能导致创建失败