breadth-first-search

Java BFS Algorithm - Trying to paint nodes on JPanel

烂漫一生 提交于 2020-01-03 05:10:28
问题 I've just implemented a BFS and DFS algorithm. My ultimate goal is to animate the algorithm onto a JPanel... But firstly I'd like to paint the nodes to the screen in their respective parent-child relationship: So far I've been able to achieve: My paint component is as follows: public void paintComponent(Graphics g) { ArrayList<Nodes> nodePrintList = new ArrayList<Nodes>(); g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); int x = 50, y = 50; //if currentNode has no more children, move

Lazy, breadth-first traversal of a Rose Tree?

▼魔方 西西 提交于 2020-01-02 03:59:06
问题 I'm trying to refactor a component that currently produces a Seq[X] using a fairly expensive recursive algorithm so that it produces a Stream[X] instead, so X 's can be loaded/calculated on-demand, and the producer doesn't have to try to guess beforehand how much digging it'll have to do to satisfy the consumer. From what I've read, this is an ideal use for an "unfold", so that's the route I've been trying to take. Here's my unfold function, derived from David Pollak's example, which has been

Implementing BFS in Java

我的未来我决定 提交于 2020-01-01 09:14:08
问题 I am a beginner in Java, and I need some help. I am trying to implement Breadth First Search algorithm to solve a puzzle game (Unblock Me a game on Android). I am done with the GUI, but I am stuck with the algorithm. So far I can count the available moves of each block, which supposed to be the children nodes of the root node. Each node (linkedlist) has the position of each block, and all the nodes are being stored in a Set. What I need now is to mark each node as visited, so I don't get into

Recursive breadth-first travel function in Java or C++?

若如初见. 提交于 2020-01-01 02:16:06
问题 Here is a java code for breadth-first travel: void breadthFirstNonRecursive(){ Queue<Node> queue = new java.util.LinkedList<Node>(); queue.offer(root); while(!queue.isEmpty()){ Node node = queue.poll(); visit(node); if (node.left != null) queue.offer(node.left); if (node.right != null) queue.offer(node.right); } } Is it possible to write a recursive function to do the same? At first, I thought this would be easy, so I came out with this: void breadthFirstRecursive(){ Queue<Node> q = new

Javascript-ONLY DOM Tree Traversal - DFS and BFS?

一笑奈何 提交于 2019-12-31 22:26:11
问题 Can anyone provide either code, pseudocode, or even provide good links to implementing DFS and BFS in plain JavaScript (No JQuery, or any helper libraries)? I've been trying to understand how to implement either traversal, but I can't seem to really distinguish the difference of a BFS and DFS implementation. If we want a concrete problem as an example: I want to traverse down the DOM at a given node, and get all the class names. (The only way I can think to traverse is to go through each

Returning only the vertices in the actual shortest path

旧巷老猫 提交于 2019-12-30 06:59:13
问题 I know the title is a bit messy, but I don't know how to explain it better. What I'm trying to do: Using a graph found in a text file, find and print the shortest path (minimum amount of vertices) from vertex A to vertex B. Note: using breadth-first search, not Dijkstra's. What I've got: A working algorithm that applies BFS on the graph, but no good way of actually printing out the shortest path. I'm having a hard time distinguishing a vertex in the shortest path from one that is simply run

Printing BFS (Binary Tree) in Level Order with _specific formatting_

元气小坏坏 提交于 2019-12-28 04:50:28
问题 To begin with, this question is not a dup of this one, but builds on it. Taking the tree in that question as an example, 1 / \ 2 3 / / \ 4 5 6 How would you modify your program to print it so, 1 2 3 4 5 6 rather than the general 1 2 3 4 5 6 I'm basically looking for intuitions on the most efficient way to do it - I've got a method involving appending the result to a list, and then looping through it. A more efficient way might be to store the last element in each level as it is popped, and

How to indicate preorder of a spanning tree using the algorithm BFS

懵懂的女人 提交于 2019-12-25 09:01:12
问题 I'm doing an implementation of the BFS algorithm in c++ to find a spanning tree, the output for a spanning tree should be shown in preorder, but I have a doubt in the implementation, how I can build a tree if not exactly know how many children have each node?. Considering a tree structure recursive The data structure of the tree can be written as: typedef struct node { int val; struct node *left, *right; }*tree; //tree has been typedefed as a node pointer. But do not think it works this

Shortest paths by BFS, porting a code from CUDA to openCL

折月煮酒 提交于 2019-12-25 06:27:45
问题 I am currently porting a CUDA code that finds shortest paths from each node to other nodes in a (undirected) graph. So basically, the CUDA code constructs a graph read from a text file. Then it proceeds to build adjancent arrays h_v and h_e. For example A B A C B C gives h_v[0] = 0, h_e[0]=1 h_v[1] = 0, h_e[1]=2 h_v[2] = 1, h_e[2]=2 Then it calls the kernel to compute shortest paths from each node using BFS. The cuda host code is as follow: int cc_bfs(int n_count, int e_count, int *h_v, int

Javascript - getElementID from scratch using BFS?

僤鯓⒐⒋嵵緔 提交于 2019-12-25 01:59:13
问题 I'm trying to learn javascript, and spent tonight writing a getElementByID() function using Breadth-First Search. In short: I'm lost. Fiddle: http://jsfiddle.net/timdown/a2Fm6/ Code: var nodes = []; function getElementById(node, id) { alert(nodes.length); if (node.childNodes[i].id == id) { return node.childNodes[i]; } else if (node.childNodes[i].length > 0) { for (var i = 0, len = node.childNodes.length; i < len; ++i) { nodes.push(node.childNodes[i]); } } if (nodes.length > 0) {