簡體   English   中英

大查詢 Dijkstra

[英]Big Query Dijkstra

我正在嘗試使用 2 個表在 bigquery 上運行 Dijkstra 算法。 第一個表有節點信息(ID,緯度,經度)第二個表有頂點信息(Start_node_ID,End_node_ID,節點之間的距離)。 我不太確定如何開始這個項目,我對 bigquery 沒有那么多經驗,我看到有人在 SQL 上做了類似的東西所以我知道這是可能的,但我很難復制它在大查詢上。

歡迎所有幫助,感謝您的寶貴時間。

PS 這里是數據的樣子,一些頂點只在一個方向上。

NODE

 1. |ID|LAT|LONG|
 2. |1 |1.2| 1.3|
 3. |2 |1.2| 1.4|
 4. |3 |3.4|-2.5|
VERTEX

 1. |STR|END|DST|
 2. | 1 | 2 | 3 |
 3. | 2 | 1 | 3 |
 4. | 1 | 3 | 4 |

我嘗試了以下代碼,但我不確定如何將其轉換為 bigquery SQL

https://kainwen.com/2019/10/31/dijkstra-via-sql-a-glance-at-recursive-cte/

BigQuery 支持遞歸 CTE,但存在一些可能對您的用例很關鍵的限制。

最終SQL代碼在這里解釋一下: https://kainwen.com/2019/10/31/dijkstra-via-sql-a-glance-at-recursive-cte/

並將其轉換為 BigQuery:

create OR REPLACE table `qwiklabs-gcp-01-6fe73054adde.data.roadmap`(a int, b int, d int);
insert into `qwiklabs-gcp-01-6fe73054adde.data.roadmap` values
(1, 2, 7),
(1, 3, 9),
(1, 6, 14),
(2, 3, 10),
(2, 4, 15),
(3, 4, 11),
(3, 6, 2),
(4, 5, 6),
(5, 6, 9);

create OR REPLACE table `qwiklabs-gcp-01-6fe73054adde.data.roadmap_sym`
AS SELECT * FROM `qwiklabs-gcp-01-6fe73054adde.data.roadmap`;

insert into `qwiklabs-gcp-01-6fe73054adde.data.roadmap_sym` select b,a,d from `qwiklabs-gcp-01-6fe73054adde.data.roadmap`;
insert into `qwiklabs-gcp-01-6fe73054adde.data.roadmap_sym` values (null, null, null);
 
create OR REPLACE table `qwiklabs-gcp-01-6fe73054adde.data.known`
AS SELECT * FROM `qwiklabs-gcp-01-6fe73054adde.data.roadmap`
WHERE 1<>1;


insert into `qwiklabs-gcp-01-6fe73054adde.data.known` values (6, 3, 2);
 
with recursive sp as
(
select * from `qwiklabs-gcp-01-6fe73054adde.data.known`
union all
select a, b, d
from
(
  select
    a, b, d, s, rank() over (order by (d+s)) as rk
  from
  (
    select a, b, d, s
    from
    (
      select
      a, b, min(d) as d, sum(t) as s
      from
      (
        select
          sp.a as a,
          case
        when rm.a is null then sp.b
        else rm.b
      end as b,
          case
        when rm.a is null then sp.d
            when sp.a = rm.a then rm.d
            else sp.d + rm.d
          end as d,
      case
        when rm.a is null then 1000
        else 0
      end as t
        from
          sp, `qwiklabs-gcp-01-6fe73054adde.data.roadmap_sym` as rm
        where
      rm.a is null
      or
      (
            (sp.a = rm.a or
             sp.b = rm.a) and
            sp.a <> rm.b
      )
      ) x
      group by a, b
    )y
  )z
)q where rk = 1 or s = 1000
)
select * from sp where b = 4 limit 1;

由於此錯誤,BigQuery 在執行 CTE 時將失敗:

A subquery containing a recursive reference may not use DISTINCT, GROUP BY, or any aggregate function

鑒於此,是的,您可以利用遞歸 CTE,但現有限制可能成為某些查詢的阻礙問題。

請查看此處記錄的所有規范和限制: https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#recursive_keyword

此類函數在 SQL 中執行起來相當棘手。通常改為編寫 JavaScript UDF 是有意義的。

Carto 編寫了這樣的路由算法代碼,您可能會看到是否可以根據您的數據調整它: https://carto.com/blog/how-to-do-route-optimization-at-scale-with-carto-bigquery/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM