What is going wrong with my hasCycle() method using DFS?

find cycle in undirected graph using dfs
detect cycle in directed graph using dfs
detect cycle in undirected graph
topological sort
dfs algorithm
check if a given graph is tree or not
print cycle in undirected graph
detect cycle in directed graph c++

I am trying to make a simple hasCycle() method that detects cycle in a graph, but I am facing some problems in it.

The code I am using is:

public static boolean hasCycle(Graph g, Vertex prev, Vertex u, Set<Vertex> known) {
known.add(u);

for(Vertex temp : g.getNeighbours(u)){
  if(!known.contains(temp)){
    if(hasCycle(g,u,temp,known))
      return true;

    else if(temp != prev)  
      return true;
  }
}

return false;
}

public static boolean hasCycle(Graph g) {
Set<Vertex> known = new TreeSet<>();

for(Vertex u : g.getAllVertices()){
  known.add(u);
  return hasCycle(g,u,u,known); // is this correct, how do I overload this method
}

return false;
}

When I am testing it for an input like this:

public static void main(String[] args){
    Graph g = new Graph();
    Vertex v = new Vertex(0);
    Vertex w = new Vertex(1);

    g.addVertex(v);
    g.addVertex(w);
    g.addEdge(v, w);
    System.out.println(hasCycle(g)); // this is printing true
}

And

public static void main(String[] args){
    Graph g = new Graph();
    Vertex v = new Vertex(0);

    g.addVertex(v);
    g.addEdge(v, v);
    System.out.println(hasCycle(g)); // this is printing false
}

I am not able to understand what's going wrong. I would appreciate any help.

Maybe move the else statement to the outer if... Also the function taking just the graph should not return on the first iteration...

Detect Cycle in a Directed Graph, Approach: Depth First Traversal can be used to detect a cycle in a Create the graph using the given number of edges and vertices. This function is a variation of DFS() in https://www.geeksforgeeks.org/archives/18212. \\<Domain Name>\<DFS Namespace> is not accessible. You might not have permission to use this network resource. Contact the administrator of this server to find out if you have access permissions. Configuration information could not be read from the domain controller, either because the machine is unavailable, or access has been denied.

Check if a given graph is tree or not, Since the graph is undirected, we can start BFS or DFS from any vertex and check if all (not marked by isCyclicUtil(), then we return false. Here is an example of what the incorrect order looks like in dfsutil /pktinfo gathered from the client computer. The client should have gone to ContosoFS1 when connecting to \\Contoso\DFSRoot\TargetFolder but the client was instead going to ContosoFS4 across the WAN. c:\>dfsutil /pktinfo; Microsoft(R) Windows(TM) Dfs Utility Version 4.2

This code will work. I haven't tested.

import java.util.*;

public class DetectCycle {
  public boolean dfs(Graph g, Vertex u, Set<Vertex> known) {
    known.add(u);
    for (Vertex v: g.getNeighbours(u)) {
      if (known.contains(v)) {
        return true;
      }
      return this.dfs(g, v, known);
    }
    return false;
  }

  public boolean hasCycle(Graph g) {
    Set<Vertex> known = new Set<Vertex>();
    for (Vertex v: g.getAllVertices()) {
      if (known.contains(v)) {
        continue;
      }
      known.add(v);
      if (this.dfs(g, v, new Set<Vertex>())) {
        return true;
      }
    }
    return false;
  }

  public static void main(String[] args){
    Graph g = new Graph();
    Vertex v = new Vertex(0);
    Vertex w = new Vertex(1);

    g.addVertex(v);
    g.addVertex(w);
    g.addEdge(v, w);
    System.out.println(this.hasCycle(g));
  }
}

Please note that I am passing a newly initialized known set for each DFS call. Each DFS will have its own copy of visited vertices. If we encounter the vertex twice in an individual DFS call, the graph is said to have a cycle.

[PDF] Graph ADT, implementing DFS and cycle detection, topological sort, The DFS tree is represented as a Map from vertex -> discovery edge. Assume when Here is the complete hasCycles() method. Why doesn't it work graph where the method would return the wrong answer. you may either We are going to need a modified version of BFS to solve this problem. We'll call� 0 What is going wrong with my hasCycle() method using DFS? Apr 17 '19 0 I want to have an 'else' statement on code using a 'for' and 'if' statement to iterate trough a list to find an item that is a key to a dictionary Apr 20 '19

Undirected Graphs, We use the names 0 through V-1 for the vertices in a V-vertex graph. The key method adj() allows client code to iterate through the vertices DFS marks all the vertices connected to a given source in time proportional to the� Depth-first search (DFS) is an algorithm (or technique) for traversing a graph. Following are the problems that use DFS as a building block. 1) For a weighted graph, DFS traversal of the graph produces the minimum spanning tree and all pair shortest path tree. 2) Detecting cycle in a graph A graph has cycle if and only if we see a back edge

Assignment 5 - Mark Dolan Programming, Run the DFS-based topological ordering algorithm on the following graph. Whenever you (c) What topological ordering is found by the algorithm? It takes linear time to go through the adjacency list and assign each node a degree value. I would like to use DFS as I have read how simply and secure it is but I do not see anything on how this is published to the End Users nor does any guide/tutorial go into deep folder creation. Goals: create folders for each department (Around 6 folders), Create Public and Self-Help Folders.

[PDF] Graphs, BFS, DFS, Topological sort, Starting from the source node s, BFS computes the minimal distance DFS can be used to classify the edges of the input graph G=(V,E). Then according to the way G' is built: Solve the next larger problems, and so on, up to the problem, we originally wanted to For each vertex u, when going over the neighbors v of u:. My GPO is redirecting users' folders (profile, my documents, downloads etc.) to a DFS namespace, whose target is a folder in my file server. Now I want to change the DFS target to another drive on the same server (which is physically in a different storage). I've been having a hard time deciding on the best way to do this exactly.

Comments
  • You should really write than with pen and paper before starting to implement, also add a lot of comments if you trying to understand what is going wrong.