我正在嘗試計算存儲在 CouchDB 中的圖形中的最短路徑。我必須在 db 中執(zhí)行此操作,因為我的任務(wù)是比較 3 種不同 DBMS 在各種情況下的查詢速度。因此,在 python(或其他任何東西)中加載數(shù)據(jù)和運行 dijkstra 不是一種選擇。我對基于文檔的數(shù)據(jù)庫很陌生,所以我可能錯了,但在我看來,我唯一的選擇是視圖。我的數(shù)據(jù)庫結(jié)構(gòu)如下:一份文件代表一張圖。在帶有“邊緣”鍵的文檔中,有一組具有 3 個屬性的對象:開始、結(jié)束、距離。start和end是節(jié)點 ID,但沒有關(guān)于節(jié)點的其他有趣信息,因此它們不會存儲在其他任何地方。距離是一個浮動我的想法是創(chuàng)建一個返回最短路徑的視圖。我有計算它的代碼。它基于這篇文章。我只需要稍微修改一下,否則我會遇到像let,foreach這樣的語法錯誤:function (doc) { function Graph() { this.nodes = []; this.adjacencyList = {}; this.addNode = function(node) { if(this.nodes.indexOf(node) != -1) return; this.nodes.push(node); this.adjacencyList[node] = []; } this.addEdge = function(node1, node2, weight) { this.adjacencyList[node1].push({node:node2, weight: weight}); //this.adjacencyList[node2].push({node:node1, weight: weight}); } this.shortestPath = function(startNode, endNode){ var times = {}; var backtrace = {}; var pq = new PriorityQueue(); times[startNode] = 0; for(var i = 0; i<this.nodes.length; i++){ if(this.nodes[i] != startNode){ times[node] = Infinity; } } pq.enqueue([startNode, 0]); while (!pq.isEmpty()) { var shortestStep = pq.dequeue(); var currentNode = shortestStep[0]; for(var i=0;i< this.adjacencyList[currentNode].length; i++){ var neighbor = this.adjacencyList[currentNode][i]; var time = times[currentNode] + neighbor.weight; if (time < times[neighbor.node]) { times[neighbor.node] = time; backtrace[neighbor.node] = currentNode; pq.enqueue([neighbor.node, time]); } } } var path = [endNode]; var lastStep = endNode; while(lastStep !== startNode) { path.unshift(backtrace[lastStep]); lastStep = backtrace[lastStep]; } return 'Path is ${path} and time is ${times[endNode]}'; } };但是,在查詢視圖時,我得到 0 行。
CouchDB 圖形中的最短路徑
縹緲止盈
2021-06-16 13:01:56