graph-shortest-path 運算符 (預覽)
適用於:✅Microsoft網狀架構✅Azure 數據✅總管 Azure 監視器✅Microsoft Sentinel
graph-shortest-paths
運算子會尋找一組來源節點與圖表中一組目標節點之間的最短路徑,並傳回具有結果的數據表。
注意
這個運算子會與 make-graph運算子搭配使用。
語法
G graph-shortest-paths
|
[output
=
OutputOption] Pattern where
Predicate project
[ColumnName =
] Expression [,
...]
參數
姓名 | 類型 | 必要 | 描述 |
---|---|---|---|
G | 字串 | ✔️ | 圖表來源,通常是作業的 make-graph 輸出。 |
模式 | 字串 | ✔️ | 描述要尋找之路徑的路徑模式。 模式必須包含至少一個可變長度邊緣,且不能包含多個序列。 |
謂語 | expression | 布爾運算式,包含模式和常數中具名變數的屬性。 | |
運算式 | expression | ✔️ | 純量表達式,定義每個找到路徑的輸出數據列,並使用模式中具名變數屬性的常數和參考。 |
OutputOption | 字串 | 將搜尋輸出指定為 any (預設值) 或 all 。 針對每個來源/目標組的單一最短路徑,以及all 長度相等的所有最短路徑,輸出會指定為 any 。 |
路徑模式表示法
下表顯示支持的路徑模式表示法。
元素 | 具名變數 | Anonymous 元素 |
---|---|---|
節點 | ( n) |
() |
從左到右方向的邊緣 | -[ e]-> |
--> |
從右至左方向的邊緣 | <-[ e]- |
<-- |
任何方向邊緣 | -[ e]- |
-- |
可變長度邊緣 | -[ e*3..5]- |
-[*3..5]- |
可變長度邊緣
可變長度邊緣可讓特定模式在定義的限制內重複多次。 星號 (*
) 表示這種類型的邊緣,後面接著最小值和最大出現次數值的格式最小值..
和最大值。 這些值必須是整數純量。 此範圍內的任何邊緣序列都可以符合模式的變數邊緣,前提是序列中的所有邊緣都符合 where
子句條件約束。
傳回
運算符 graph-shortest-paths
會傳回表格式結果,其中每個記錄都會對應至圖表中找到的路徑。 傳回的數據行會使用模式中定義的節點和邊緣的屬性,在運算子的 project
子句中定義。 可變長度邊緣屬性的屬性和函式會以動態數位的形式傳回。 陣列中的每個值都會對應至可變長度邊緣的出現次數。
範例
本節提供實用範例,示範如何在不同的案例中使用 graph-shortest-paths
運算符。
尋找 any
兩個火車站之間的最短路徑
下列範例示範如何使用 graph-shortest-paths
操作員來尋找運輸網路中兩個月臺之間的最短路徑。 此查詢會從 中的數據 connections
建構圖形,並尋找從 "South-West"
到 "North"
月臺的最短路徑,並考慮最多五個連接長的路徑。 由於預設輸出為 any
,因此它會尋找任何最短的路徑。
let connections = datatable(from_station:string, to_station:string, line:string)
[
"Central", "North", "red",
"North", "Central", "red",
"Central", "South", "red",
"South", "Central", "red",
"South", "South-West", "red",
"South-West", "South", "red",
"South-West", "West", "red",
"West", "South-West", "red",
"Central", "East", "blue",
"East", "Central", "blue",
"Central", "West", "blue",
"West", "Central", "blue",
];
connections
| make-graph from_station --> to_station with_node_id=station
| graph-shortest-paths (start)-[connections*1..5]->(destination)
where start.station == "South-West" and destination.station == "North"
project from = start.station, path = connections.to_station, line = connections.line, to = destination.station
輸出
寄件者 | path | line | 打給 |
---|---|---|---|
South-West | [ “南”, “Central”, “北” ] |
[ “red”, “red”, “red” ] |
北 |
尋找兩個火車站之間的所有最短路徑
下列範例與上一個範例一樣,會在運輸網路中尋找最短的路徑。 不過,它會使用 output=all
,因此會傳回所有最短的路徑。
let connections = datatable(from_station:string, to_station:string, line:string)
[
"Central", "North", "red",
"North", "Central", "red",
"Central", "South", "red",
"South", "Central", "red",
"South", "South-West", "red",
"South-West", "South", "red",
"South-West", "West", "red",
"West", "South-West", "red",
"Central", "East", "blue",
"East", "Central", "blue",
"Central", "West", "blue",
"West", "Central", "blue",
];
connections
| make-graph from_station --> to_station with_node_id=station
| graph-shortest-paths output=all (start)-[connections*1..5]->(destination)
where start.station == "South-West" and destination.station == "North"
project from = start.station, path = connections.to_station, line = connections.line, to = destination.station
輸出
寄件者 | path | line | 打給 |
---|---|---|---|
South-West | [ “南”, “Central”, “北” ] |
[ “red”, “red”, “red” ] |
北 |
South-West | [ “West”, “Central”, “北” ] |
[ “red”, “blue”, “red” ] |
北 |