You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@age.apache.org by GitBox <gi...@apache.org> on 2022/10/19 14:15:17 UTC

[GitHub] [age-viewer] MJinH commented on issue #44: Sample JavaScript codes for DFS and BFS algorithms

MJinH commented on issue #44:
URL: https://github.com/apache/age-viewer/issues/44#issuecomment-1284089213

   Code for BFS
   ```
   const bfs = (graph, node) => {
     let visited = [];
     let nodes= [];
   
     nodes.push(node); 
   
     while (nodes.length !== 0) { 
       const newNode = nodes.shift(); 
       if (!visited.includes(newNode )) {
         visited.push(newNode ); 
         nodes= [...nodes, ...graph[newNode ]];
       }
     }
     return visited;
   };
   ````
   
   Code for DFS
   ```
   const dfs = (graph, node) => {
     let stack= [];
     let queue= [];
   
     stack.push(node);
   
     // 탐색을 해야 할 노드가 남아 있다면
     while (stack.length !== 0) {
       const newNode = stack.pop();
       if (!queue.includes(newNode )) {
         queue.push(newNode );
         stack= [...stack, ...graph[newNode]];
       }
     }
     return queue;
   };
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@age.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org