主题
获取有向路径到所有节点 - GetDirectedPathToAllNodes
函数简介
在指定的有向图中计算从起点到所有其他节点的有向路径,使用有向图最短路径算法实现。
接口名称
GetDirectedPathToAllNodesDLL调用
c
long GetDirectedPathToAllNodes(long instance, long graphPtr, char* startNode);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
| graphPtr | 长整数型 | 图的指针,由 CreateGraph 接口返回。 |
| startNode | 字符串 | 起点节点名称。 |
示例
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) {
std::string paths = ola.GetDirectedPathToAllNodes(graphPtr, "上海");
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)
{
string paths = ola.GetDirectedPathToAllNodes(graphPtr, "上海");
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:
paths = ola.GetDirectedPathToAllNodes(graph_ptr, "上海")
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.GetDirectedPathToAllNodes(graphPtr, "上海");
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.GetDirectedPathToAllNodes(graphPtr, "上海");
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.GetDirectedPathToAllNodes(graphPtr, "上海");
ola.DeleteGraph(graphPtr)
End Iftext
.局部变量 ola, OLAPlug
ola.创建 ()
graphPtr = ola.CreateGraph (“[{\“directed\“:false,\“from\“:\“上海\“,\“to\“:\“北京\“,\“weight\“:3.0},{\“directed\“:false,\“from\“:\“北京\“,\“to\“:\“深圳\“,\“weight\“:5.0}]“)
.如果真 (graphPtr ≠ 0)
ola.GetDirectedPathToAllNodes(graphPtr, "上海");
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.GetDirectedPathToAllNodes(graphPtr, "上海");
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.GetDirectedPathToAllNodes(graphPtr, "上海");
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.GetDirectedPathToAllNodes(graphPtr, "上海");
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) {
std::string paths = ola.GetDirectedPathToAllNodes(graphPtr, "上海");
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) {
string paths = ola.GetDirectedPathToAllNodes(graphPtr, "上海");
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:
paths = ola.GetDirectedPathToAllNodes(graph_ptr, "上海")
ola.DeleteGraph(instance, graph_ptr)返回值
| 返回值 | 说明 |
|---|---|
| (返回值) | 返回包含所有有向路径信息的字符串指针,格式为JSON:。 |
json
{
"A": {"distance": 0, "path": "A"},
"B": {"distance": 4, "path": "A|B"},
"C": {"distance": 2, "path": "A|C"},
"D": {"distance": 9, "path": "A|B|D"},
"E": {"distance": 11, "path": "A|B|D|E"}
}| 字段名 | 类型 | 说明 |
|---|---|---|
| A | 对象 | A 对象信息,详见下方字段说明。 |
| B | 对象 | B 对象信息,详见下方字段说明。 |
| C | 对象 | C 对象信息,详见下方字段说明。 |
| D | 对象 | D 对象信息,详见下方字段说明。 |
| E | 对象 | E 对象信息,详见下方字段说明。 |
如果不存在路径返回0。
返回的字符串指针需要调用 FreeStringPtr 释放内存。
A 字段说明:
| 字段名 | 类型 | 说明 |
|---|---|---|
| distance | 整数 | distance 字段。 |
| path | 字符串 | path 字段。 |
B 字段说明:
| 字段名 | 类型 | 说明 |
|---|---|---|
| distance | 整数 | distance 字段。 |
| path | 字符串 | path 字段。 |
C 字段说明:
| 字段名 | 类型 | 说明 |
|---|---|---|
| distance | 整数 | distance 字段。 |
| path | 字符串 | path 字段。 |
D 字段说明:
| 字段名 | 类型 | 说明 |
|---|---|---|
| distance | 整数 | distance 字段。 |
| path | 字符串 | path 字段。 |
E 字段说明:
| 字段名 | 类型 | 说明 |
|---|---|---|
| distance | 整数 | distance 字段。 |
| path | 字符串 | path 字段。 |
注意事项
| 项目 | 说明 |
|---|---|
| startNode 节点在图中存在 | 确保 startNode 节点在图中存在。 |
| 路径 | 有向路径考虑了边的方向性,与无向图的最短路径不同。 |
| 如果起点无法到达某些节点 | 如果起点无法到达某些节点,这些节点将不会出现在结果中。 |
| 对于大型有向图 | 对于大型有向图,计算时间可能较长。 |
