SA : I ask if anyone can help me in this algorithm , it is suppose that this algorithm find all the cycles in given graph . but there exist a bug it return only one cycle not all :

public class Main {
    public boolean funcs(boolean Graph[][]){
        ArrayList<ArrayList<Integer>>l=new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer>c=new ArrayList<Integer>();


        for (int i = 0; i < Graph.length; i++) {       
            for (int j = 0; j < Graph.length; j++) {
               
                if (Graph[i][j]){
                boolean vis []=new boolean [Graph.length];
                  boolean[][] Gcopy=Graph.clone();
                ArrayList<Integer>cycle=new ArrayList<Integer>();
                    if (DFS(Gcopy, vis, i, j, cycle)){
                        cycle.add(i);
                        Graph[i][j]=false;

                         l.add(cycle);                         
                    }      
                }
            }
        }
        System.out.println(l);
        
        if (l.isEmpty())
            return true;// e.g no cycle in the graph

       return false;
    }
    
    public boolean DFS(boolean graph[][], boolean [] vis, int strtNode, int node , ArrayList<Integer> cycle){
        if (node==strtNode)
            return true;
        if(vis[node])
            return false;
        vis[node]=true;
        for (int i = 0; i < graph.length; i++) {
            if(graph[node][i] )
            {
                if(DFS(graph,vis,strtNode,i,cycle))
                {
                    cycle.add(node);
                    graph[node][i]=false;
                    return true;
                }
            }
        }
        return false;
    }
    public void parse (String transaction ){
        String pros[]=transaction.split(",");
        for (int i = 0; i < pros.length; i++)
             pros[i]=pros[i].toUpperCase();





    }

    public static void main(String[] args) {
        boolean [][]a={{false,true,false,false},{false,false,true,false},{true,false,false,true},{true,false,false,false}};
       new Main().funcs(a);
       

    }

}

the example in the main contain two cycles also it return only one .

please help urgent .

Recommended Answers

All 7 Replies

Can you explain the algorithm you are using as comments in your code?
Can you show the current output from the program
and can you show what the output should be?

You left off the import statements. The code will not compile without errors

Um trying find all cycles in the graph as sets i.e. no duplication in cycles .

the current output is [[2,1,0]]
the expected output is [[2,1,0] and [0,1,2,3] ]

You left off the import statements. The code will not compile without errors

Also your code does NOT document the algorithm you are using.
I can read code, but I have no idea what you want the code to do.
For example how do you detect a duplicate cycle?

import java.util.ArrayList; the missing import .

0->1->2->3->0
\=>0
cycles here {0,1,2} and {0,1,2,3} with any order it doesn't matter i concern with the edges .

Sorry, I don't understand you explanation.
What I'm looking for is documentation in your code on what each section is supposed to do. For example what is the purpose of the following code:

if (node==strtNode)
            return true;
        if(vis[node])
            return false;

        vis[node] = true;

This is just one example of many. Don't bother explaining here in the thread. It will be a waste of time. I'm not going to copy your comments from here to the code.

here i check if the start node equals the current node i.e. there exist cycle .
return true .

if this node is visited before and not equals the start node i.e. i can't find cycle .

else mark this node as visited .

Don't bother explaining here in the thread. It will be a waste of time. I'm not going to copy your comments from here to the code.

The only people that can help you are those that know the algorithm.

Good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.