Click here to Skip to main content
15,907,910 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello. Can Someone help me in order to add new posibilities to a program that work fine but I need add new possibility.

The original version was copied from GeekForGeeks, to find how many Cycles with n Nodes have an nondirected graph, that solution working fine but I need add to the solution what are the count of nodes in the solution, and when whatever nodes are repetead count as one.

I was try to but can't find the solution. Below are a copy of initial solution.

Thanks so much for your time.

Best regards.

LEG.

What I have tried:

// GeeksforGeeks.com
// C# program to calculate   cycles of 
// length n in a given graph
 
using System; 
  
class GFG  
{ 
      
    // Number of vertices 
    public static int V = 5; 
    static int count = 0; 
      
    static void DFS(int [,]graph, bool []marked, 
                    int n, int vert, int start)  
    { 
          
        // mark the vertex vert as visited 
        marked[vert] = true; 
          
        // if the path of length (n-1) is found 
        if (n == 0)  
        { 
              
            // mark vert as un-visited to  
            // make it usable again 
            marked[vert] = false; 
              
            // Check if vertex vert end  
            // with vertex start 
            if (graph[vert, start] == 1) 
            { 
                count++; 
                return; 
            }  
            else
                return; 
        } 
          
        // For searching every possible  
        // path of length (n-1) 
        for (int i = 0; i < V; i++) 
            if (!marked[i] && graph[vert, i] == 1) 
              
                // DFS for searching path by 
                // decreasing length by 1 
                DFS(graph, marked, n - 1, i, start); 
          
        // marking vert as unvisited to make it 
        // usable again 
        marked[vert] = false; 
    } 
      
    // Count cycles of length N in an  
    // undirected and connected graph. 
    static int countCycles(int [,]graph, int n)  
    { 
          
        // all vertex are marked un-visited 
        // initially. 
        bool []marked = new bool[V]; 
          
        // Searching for cycle by using  
        // v-n+1 vertices 
        for (int i = 0; i < V - (n - 1); i++)  
        { 
            DFS(graph, marked, n - 1, i, i); 
              
            // ith vertex is marked as visited 
            // and will not be visited again 
            marked[i] = true; 
        } 
          
        return count / 2;  
    } 
      
    // Driver code 
    public static void Main() 
    { 
        int [,]graph = {{0, 1, 0, 1, 0}, 
                        {1, 0, 1, 0, 1}, 
                        {0, 1, 0, 1, 0}, 
                        {1, 0, 1, 0, 1}, 
                        {0, 1, 0, 1, 0}}; 
          
        int n = 4; 
          
        Console.WriteLine("Total cycles of length "+ 
                        n + " are "+  
                        countCycles(graph, n)); 
    } 
} 
Posted
Updated 17-Sep-19 19:53pm

Why don't you create an account on GeeksForGeeks, login, and post your question as a comment on the article [^] ?

The article was written recently, it appears.
 
Share this answer
 
Comments
Maciej Los 18-Sep-19 2:09am    
5ed!
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900