0

I am implementing an optimal solution algorithm and need to find a cycle that contains more than 3 vertices, because diagonal connections are not allowed(only up/down, left/right). I'd greatly appreciate it, if any one has any suggestions or can point me in a direction for resources.

I've already implemented a linkedlist method using an adjacency matrix. I based it on this but it only stops at the first cycle which only contains 3 vertices.

My current code uses custom object collection so it is a bit different to this.

// A Java Program to detect cycle in a graph 
class Graph { 
    private final int V; 
    private final List<List<Integer>> adj; 

    public Graph(int V)  
    { 
        this.V = V; 
        adj = new ArrayList<>(V); 

        for (int i = 0; i < V; i++) 
            adj.add(new LinkedList<>()); 

    }     

    // This function is a variation of DFSUytil() in  

    // https://www.geeksforgeeks.org/archives/18212 

    private boolean isCyclicUtil(int i, boolean[] visited, 

                                      boolean[] recStack)  

    { 

        // Mark the current node as visited and 
        // part of recursion stack 
        if (recStack[i]) 

            return true; 

        if (visited[i]) 

            return false; 

        visited[i] = true;   

        recStack[i] = true; 

        List<Integer> children = adj.get(i);          

        for (Integer c: children) 
            if (isCyclicUtil(c, visited, recStack)) 
                return true;                

        recStack[i] = false; 

        return false; 

    } 

    private void addEdge(int source, int dest) { 

        adj.get(source).add(dest); 
    } 

    // Returns true if the graph contains a  
    // cycle, else false. 
    // This function is a variation of DFS() in  
    // https://www.geeksforgeeks.org/archives/18212 
    private boolean isCyclic()  
    { 
        // Mark all the vertices as not visited and 
        // not part of recursion stack 

        boolean[] visited = new boolean[V]; 

        boolean[] recStack = new boolean[V]; 

        // Call the recursive helper function to 

        // detect cycle in different DFS trees 

        for (int i = 0; i < V; i++) 

            if (isCyclicUtil(i, visited, recStack)) 

                return true; 
        return false; 

    } 
    public static void main(String[] args) 

    { 
        Graph graph = new Graph(5); 
        graph.addEdge(0, 1); 
        graph.addEdge(0, 2); 
        graph.addEdge(1, 2); 
        graph.addEdge(2, 0); 
        graph.addEdge(2, 3); 
        graph.addEdge(3, 3); 

        graph.isCyclic();

    } 
} 

I'm unable to post my exact code as I could get flagged for plagiarism

An example of a cycle I would like to find is:
00000
01010
01010
Where the 4 edged cycle is represent by 1s
At the moment I get a result where a cycle
00000
00000
01110
and this is not what I need.
Kim
  • 23
  • 6
  • Could you explain your problem a bit more precisely ? What does "diagonal connections are not allowed(only up/down, left/right)" mean ? More than 3 vertices, do you mean as many as possible, or 4 is good already ? Could you paste your current implementation as well ? – m.raynal Apr 17 '19 at 14:30
  • 4 vertices would be good already. – Kim Apr 17 '19 at 21:57
  • Sorry this is the first time I've asked a question on Stack Overflow. I've edited my question so that it shows code. I think my main question is, is there a way to continue the dfs in isCyclicUtil() even after the first cycle is found? and how to specify the minimum vertices (4) of a cycle – Kim Apr 17 '19 at 22:09

0 Answers0