Please give me pseudocode for finding cycles using BFS. I know other questions of this type exist but NONE give the code.

Just in case, DFS is much more suitable for the task, even more so in directed graphs. If you already knew that, ignore this.

As for the pseudocode, well, in an undirected graph, it's a traditional BFS that aborts and reports a cycle found when it reaches a node previously marked as visited. You can find pseudocode for BFS here.

In a directed graph, it gets trickier, since you have to remember which way were you walking when you reached the node, and the spatial complexity disadvantage over DFS gets even worse.

Edit: oh, I was talking about testing a graph for cycles, not actually finding the cycle. Finding cycles with DFS is close to trivial, while finding cycles with BFS is much more complex both in terms of actual algorithmic complexity and code complexity. That's why you don't find pseudo-code.

You probably meant DFS, which is far more common in cycle detection, so I'll assume you made that mistake. Changing to BFS is fairly easy though as the core idea remains the same.

```
func detectCycle()
for node in graph:
visited = bool[N]
set all visited to false
detectCycle(n, n, visited)
func detectCycle(n, origin, visited)
for neighbour in graph[n]
if neighbour == origin
cycle detected
if not visited[neighbour]
visited[neighbour] = true
detectCycle(neighbour, visited)
visited[neighbour] = false
```

Similar Questions

I'm using OCaml. I have type: type 'a bt = Empty | Node of 'a * 'a bt * 'a bt;; Also I have example BST: let tree = Node(1,Node(2,Node(4,Empty,Empty),Empty),Node(3,Node(5,Empty,Node(6,Empty,Empty)),

So If I post a status update on Facebook with a photo (public), I want to use Graph Search api to find it. Here is the link I've been using: https://graph.facebook.com/search?q=%23tacomaevent&typ

I am working on finding cycles in directed graph using recursive backtracking. There is a suggested pseudocode for this here, which is here: dfs(adj,node,visited): if (visited[node]): if (node == star

I am doing a Breadth First Search program that will read in an adjacency list type text file, then perform BFS on it accordingly. I am having trouble reading in the text file, then adding it as an arr

First of all, thank you for looking at this question. For a school assignment we're supposed to create a BFS algorithm and use it to do various things. One of these things is that we're supposed to f

I'm using breadth first search to find a location in a graph, and I'm pretty sure my algorithm works correctly, but I'm having troubling finding the shortest path to the result when I'm done. Essentia

I would like to find the shortest route from station A to station B in prolog in bidirectional graph(if A is connected to B than B is connected to A),the graph has no weights on branches. The question

How can I find the shortest cycle in a simple (not directed ) bipartite graph using Breadth First Search ?

I'm making Tetris as a fun side project (not homework) and would like to implement AI so the computer can play itself. The way I've heard to do it is use BFS to search through for available places, th

I need to write a program which must be able to find all subgraphs of a graph using an algorithm like depth first. The thing is I cannot understand how could I represent a set of nodes with relations

The no. of nodes generated by breadth-first search is, according to my book: N(BFS) = b + b^2 + .... + b^d + ( b^(d+1) - b ) where b is the branching factor and d is the depth of the shallowest node.

I want a url like this for the Facebook graph API search to retrieve data. When I tried using this url, it gives { error: { message: (#200) Must have a valid access_token to access this endpoint

My question isn't really about the mechanism of either search type. I feel it's a lot more mundane than that - I don't understand the input and output of either. More specifically, in CLRS, BFS takes

Both can be used to find the shortest path from single source. BFS runs in O(E+V) while Dijkstra's runs in O((V+E)*log(V)) But I've seen Dijkstra used a lot like in routing protocols

here is my algorithm pseudocode: input: a directed graph G=(V,E) output: all source nodes of G function sources(G) for all u in V: in[u]=0 for all u in V: for all edges (u,w) in E in[w]=in[w]+1 L = em

I'm looking for an algorithm to find the first number less than X in an array of integers. Actually I'm using linear search,but I think that binary search may be better(as I already have seen sometime

I have an undirected graph and what I would like to do is detect cycles that have three or more nodes in them. Is there a library in R that would do this? If not is there a simple algorithm that I cou

I need to find the complexity of finding all the cycles in a undirected graph consisting of 50 nodes. Moreover, if the graph grows large, will the complexity be changed and what will be it if the netw

I'm using Ruby and the Ruby Graph Library http://rgl.rubyforge.org/ How do I find all the cliques (complete subgraphs) in a graph? Specifically, I'm looking for a 5 clique involving two particular ver

I wonder if you could help me with a simple implementation for detecting cycles in a directed graph in C#. I've read about the algorithms but I'd like to find something already implemented, very simpl

I want to create a graph using Jgraph, and later use that graph to find the minimum spanning Tree. How to create a graph using Jgraph?. this is what I have implemented. Can you please tell me how to

I'm looking for an algorithm that given a graph it returns all the minimal cycles in it. To make clear what I want, I need the algorithm to return exactly the following cycles from this graph: (1,3,

I am curious whether there is the basic difference between graph search and tree search versions regarding DFS, A* Searches in Artificial Intelligence?

Does anyone have any idea about logical query in Facebook Graph search API? Problem: I am trying to fetch all public post using following URL: https://graph.facebook.com/search?q=+testKeywords+&

Sometimes I know a file is not so deep away, but a very dense sub-directory does not allow me to find the files I want easily. Can find (or any other tool) look for files using breadth-first search?

Is it possible to use Breadth first search logic to do a topological sort of a DAG? The solution in Cormen makes use of Depth first search but wouldn't be easier to use BFS? Reason: BFS visits all the

In an undirected random graph of 8 vertices, the probability of an edge being present between a pair of vertices in 1/2. What is the expected number of unordered cycles of length 3? Here's how I thoug

I created a search bar that uses the graph API and it worked fine until about a month ago. You could type something into the search bar, and get info back from Facebook related to your search paramete

First of all, I should say I am not familiar with the Graph theory and also my mathematics knowledge is very poor. Anyhow I am using graph concepts for my analysis. Basically, I am decomposing an undi

I am trying to make a (spanning) tree that comes naturally from traversing a graph (undirected and connected) using Breadth First Search, but I am having difficulties modifying the algorithm such that

I am using facebook graph api to search for posts matching a particular query.But I cannot find any documentation about the time zone of the time stamp returned in the json format. My query is this a

I want to reconstruct the incidence structure of a graph in Haskell, which is given by the output of a breadth first traversal of it. Explicitly, the output consists of a root vertex and a list of nei

Although this question has been asked before but that was an year before and not exactly what I wanted to ask. So, here it goes: I am using https://graph.facebook.com/search?q=conference&type=even

The following code performs a breadth first search algorithm using an adjacency list structure. I was curious. If I were to implement this an adjacency matrix, what kinds of edits would I have to do t

I was trying out an iterative method to find the height/depth of a binary search tree. Basically, I tried using Breadth First Search to calculate the depth, by using a Queue to store the tree nodes an

I can't find a formal definition of cycle in an undirected graph. The CLRS only reports a definition of symple cycles that i can't manage to generalize for a generic cycle. This is the CLRS definition

I have been working on a Binary Search Tree and several of its function implementations for a school assignment. I just wrote my Breadth First Search functions and I am getting a segmentation fault in

Does anyone knows if there exists a breadth first(from multiple sources) implementation in either of the graph processing systems- Giraph, Pregel or Graphchi. Or please tell some easier implementation

I seem to be having a problem understanding how to retrive the Shortest Path that is discovered by the Breadth First Search algorithm I am implementing. Currently the Algorithm works as it can find th

Here is my code: import matplotlib.pyplot as plt plt.loglog(length,time,'--') where length and time are lists. How do I find the slope of this graph?

I am trying to find the pseudocode for the XOR encryption algorithm. However I've had no luck so far. Anybody know where I can find it? EDIT: XOR 32 if that helps EDIT 2: For Passwords

Given an undirected graph what is the best algorithm to detect if it contains a cycle or not? A breadth first or depth first search while keeping track of visited nodes is one method, but it's O(n^2)

I'm wondering, is there any way to find WCF service certificate, using several search criteria in .config file? E.g., if I want to find certificate by subject name, then my config will contain these l

I am using facebook graph api to search the keywords, like as follows https://graph.facebook.com/search?q='test'&limit=60&access_token=(myAccessToken) Which works fine and returns the data c

I've been experimenting with the Facebook Graph API, and been researching several questions on Stack Overflow and other internet resources, and I can't seem to figure out how to search users in Graph

I'm working on a programming assignment (in java) to solve a fifteen-puzzle sort of thing. The first part is to use depth first search to find a solution. I want it to be able to solve an arbitrarily

I have a very small object graph that I'm using: public struct Address { public string AddressLine1 { get; set; } public string AddressLine2 { get; set; } etc... } public class User { public Address H

Code jam problem is the following: You are given a complete undirected graph with N nodes and K forbidden edges. N <= 300, K <= 15. Find the number of Hamiltonian cycles in the graph that do n

I implemented a depth-first-search in c# based on information I found on the net and old java books and I used the Node and NodeList and Graph from the msdn site. How can one modify the DFS or BFS to

I was trying to solve one interview question, but for that I have to travel the binary tree level by level. I have designed BinaryNode with having below variable private object data; private BinaryNo