From 9b7b382962d8a2969ae1eb94857701ad8b275d72 Mon Sep 17 00:00:00 2001 From: Cindy Chang Date: Mon, 19 Aug 2024 17:07:56 +0800 Subject: [PATCH] WIP: DFS --- src/stores/mapPathStore.ts | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/stores/mapPathStore.ts b/src/stores/mapPathStore.ts index f131d71..dafdbd4 100644 --- a/src/stores/mapPathStore.ts +++ b/src/stores/mapPathStore.ts @@ -8,27 +8,47 @@ export default defineStore('useMapPathStore', { clickedPath: [], allMapData: AllMapData(), cytoscape: null, + allPaths: [], + startNode: null, }), actions: { setCytoscape(cytoscape) { this.cytoscape = cytoscape; }, createAllPaths() { + this.startNode = this.cytoscape.nodes().filter(function(elem) { + return elem.data('label').toLocaleLowerCase() === 'start'; + }); + // Depth First Search from the starting node + this.depthFirstSearchCreatePath(this.startNode, [this.startNode.id()]); + this.allPaths.forEach(path => { + console.log('path',path ); + }); const { insights } = this.allMapData; for(let i = 0; i < INSIGHTS_FIELDS_AND_LABELS.length; i++) { const curButton = insights[INSIGHTS_FIELDS_AND_LABELS[i][0]]; for(let listIndex = 0; listIndex < curButton.length; listIndex++) { for(let nodeIndex = 0; nodeIndex < curButton[listIndex].length; nodeIndex++){ if(nodeIndex === 0) { - const startNodes = this.cytoscape.nodes().filter(function(elem) { - return elem.data('label').toLocaleLowerCase() === 'start'; - // highlight between start node and first node - }); - } + + } } } } }, + depthFirstSearchCreatePath(node, currentPath){ + const outgoingEdges = node.outgoers('edge'); + if (outgoingEdges.length === 0) { + this.allPaths.push([...currentPath]); + } else { + outgoingEdges.targets().forEach((targetNode) => { + if (!currentPath.includes(targetNode.id())) { + // 避免loop,只有當目標節點不在當前路徑中之時才繼續 + this.depthFirstSearchCreatePath(targetNode, [...currentPath, targetNode.id()]); + } + }); + } + }, }, }); \ No newline at end of file