breadth-first-search

Writing nested dictionary (forest) of a huge depth to a text file in BFS style

纵然是瞬间 提交于 2019-12-11 04:07:34
问题 Continued to my older question: Writing nested dictionary (forest) of a huge depth to a text file Now I want to write the forest traversal in the BFS style: I have a huge depth dictionary that represents forest (many non-binary trees) which I want to process the forest and create a text file with the sequences of (father, son) relations from the forest, i.e. given the dictionary: {'a': {'b': {'c': {'x': {}}, 'd': {'p': {}}}, 'g': {}, 'f': {}}, 't': {'r': {'o': {}}, 'y': {}}} the generated

BFS traversal of directed graph from a given node

限于喜欢 提交于 2019-12-11 03:55:38
问题 My understanding of basic breadth-first search traversal for a graph is: BFS Start from any node. Add it to queue. Add it to visited array. While queue is not empty: Remove head from queue; Print node. add all unvisited direct subchilds to que; mark them as visited However, if we have to traverse a directed graph from a given node and not all nodes are accessible from the given node (directly or indirectly), how do we use BFS for traversing the graph of this situation? Can you please explain

Traversing a graph breadth first, marking visited nodes in Haskell

不想你离开。 提交于 2019-12-11 00:46:04
问题 So the question is simple. Given a graph (I hope that the structure of the graph doesn't matter much in this problem), how do I go about doing a BFS on it? I've recently asked a question about generating a list where each element appends many elements to the end of it. The answer should hopefully let me make a queue I need to do the BFS. But there's another key component that a search needs and it's marking the nodes as visited so we don't go over them again. This also needs to have no

Finding the smallest distance from any vertex to the border of the graph

↘锁芯ラ 提交于 2019-12-10 21:15:58
问题 So I have triangular mesh approximating a surface. It's like a graph with the following properties: The vertices on the graph border are trivially identifiable. (Number of neighbor vertices > number of containing triangles) You can trivially calculate the distance between any two vertices. (Euclidean distance) For any vertex v , any vertex that is not a neighbor of v must have a greater distance to v than at least one of v 's neighbors. In other words, no non-neighbor vertices may appear

Implementing graph and BFS in C++ using STL

China☆狼群 提交于 2019-12-10 20:55:47
问题 Following is the code I have written. #include <iostream> #include <vector> #include <list> #include <queue> using namespace std; const int V=5; vector<list<int> > a(V); int BFS(int s) { int visited[V]={0}; queue<int> Q; visited[s]=1; Q.push(s); while(!Q.empty()) { int x=Q.front(); vector<list<int> >::iterator it1=a.begin()+x; list<int> it2=*it1; list<int>::iterator iter=it2.begin(); while(iter!=it2.end()) { if(visited[*iter]==0) { visited[*iter]=1; Q.push(*iter); } visited[x]=2; Q.pop();

Find first null in binary tree with limited memory

蹲街弑〆低调 提交于 2019-12-10 17:29:45
问题 I have a binary tree where each node can have a value. I want to find the node in the tree that has value null and is closest to the root. If there are two nodes with the same distance from the root, either will do. I need to minimize the number of read accesses to the binary tree. Assume that working memory is limited to just k nodes. DFS to depth k is exhaustive but will not find the closest node unless I run through the whole tree first. BFS will find the closest, but it might fail because

How to detect if an undirected graph has a cycle and output it using BFS or DFS

心不动则不痛 提交于 2019-12-10 13:47:10
问题 The other question on this only answered how to detect a cycle, not also output it. So, I'd like to write an algorithm run BFS or DFS in O(V + E) time (V=vertices, E=edges), on an undirected graph, and output the cycle if it has one. What I know so far is how BFS/DFS works and that you can detect a cycle using BFS if you visit a node that already has been marked as visited. 回答1: To detect and output a cycle with DFS, just mark each vertex as you get to it; if any child of the current vertex

Counting levels in Breadth First Search (Distance between start node and goal node)

自闭症网瘾萝莉.ら 提交于 2019-12-10 13:26:31
问题 Can anyone help me how to count the visited levels of a graph using Breadth First Search in Java? Here is my method, I have start node (str) and end node (goal), when the loop reach the goal node it should be stopped. What I want now is counting the levels from start node to end node. public void bfs(String str,String goal) { int strInx = findIndex(str); vertexList[strInx].wasVisited = true; theQueue.insert(strInx); int v2; boolean bre = false; while (!theQueue.isEmpty()) { System.out.println

Breadth First Traversal With Binary Search Tree C++

六眼飞鱼酱① 提交于 2019-12-10 10:47:37
问题 Maybe fast/simple Question. I have a a Binary Tree Implemented already, Then I was hoping to convert binary search tree into an array or at least print it out as if in an array. Where I am having trouble with is how to get the NULL/flags in there '\0'. for example lets say I have a tree like: 10 / \ 6 12 / \ \ 1 8 15 \ 4 And I want it to print how its supposed to print. Like: [10,6,12,1,8,\0,15,\0,4,\0,\0,\0,\0,\0,\0] ^Something Like this^ I don't know if I counted the NULL correctly. Or

Find the shortest path to reach a given destination cell in 2D matrix with obstacles

你离开我真会死。 提交于 2019-12-10 05:31:33
问题 I am working on below interview question and I am confuse on how to use BFS search here. I am confuse on how to deal with the blockades here? Given a MxN matrix find the shortest path to reach a given destination cell. The robot can move up, down, left or right. The matrix also comes with set of blockades in few of the cells where the robot can't pass through. Output the minimum number of moves the robot needs to make to reach the destination. Output -1 if the destination is not reachable.