Print all cycle in graph by given length

Here given code implementation process.

//C Program 
//Print all cycle in graph by given length

#include<stdio.h>

#include<stdlib.h>

struct AjlistNode {
  int vId; //Vertices id
  struct AjlistNode *next;
};

struct Graph {
  int data; //node key value
  struct AjlistNode *next;
};

struct Stack {
  int data;
  struct Stack *next;
};

int size; //number of nodes

//set node key value
void set_data(struct Graph *node) {
  if (node != NULL && size > 0) {
    int index = 0;
    for (index; index < size; index++) {
      //set vertic node data
      node[index].data = index; //set node key
      //Initial no AjlistNode
      //set NULL Value
      node[index].next = NULL;
    }
  } else {
    printf("Vertic Node is Empty");
  }
}
void connect_edge(struct Graph *node, int V, int E) {

  // create Adjacency node
  struct AjlistNode *newEdge = (struct AjlistNode *) malloc(
    sizeof(struct AjlistNode)
  );
  if (newEdge != NULL) {

    newEdge->next = NULL;
    newEdge->vId = E;

    struct AjlistNode *temp = node[V].next;

    if (temp == NULL) {
      node[V].next = newEdge;
    } else {
      //Add node at last
      while (temp->next != NULL) {
        temp = temp->next;
      }
      temp->next = newEdge;
    }
  } else {
    printf("\n Memory overflow");

  }
}
//Add Edge from Two given Nodes
void add_edge(struct Graph *node, int V, int E) {
  //add edge form V to E
  //V and E is Node location
  if (V < size && E < size) {

    connect_edge(node, V, E);
    if (V == E) {
      //self loop
      return;
    }
    connect_edge(node, E, V);

  } else {
    //not valid Vertices
    printf("Invalid Node Vertices %d  %d", V, E);
  }
}
//Display Adjacency list of vertex
void print_graph(struct Graph *node) 
{
  if (node != NULL) 
  {
    struct AjlistNode *temp = NULL;
    for (int index = 0; index < size; index++) 
    {
      printf("\n Adjacency list of vertex %d  :", index);
      temp = node[index].next;
      while (temp != NULL) 
      {
        //temp->vId is graph node vertices
        //in this case temp->vId is same as 
        //node[temp->vId].data

        printf("  %d", node[temp->vId].data);
        temp = temp->next;
      }
    }
  } else {
    printf("Empty Graph");
  }
}
void push(struct Stack **root, int data) {
  struct Stack *element = (struct Stack *) malloc(
    sizeof(struct Stack)
  );

  if (element != NULL) {
    element->data = data;
    element->next = *root;
    *root = element;
  }
}
void pop(struct Stack **root) {
  if ( *root == NULL) {
    return; //already empty
  }
  struct Stack *temp = *root;
  *root = temp->next;
  temp->next = NULL;
  free(temp);
  temp = NULL;
}

//Display Path from source to destination
void print_path(struct Stack *temp) {
  if (temp == NULL) {
    return;
  }
  print_path(temp->next);
  printf(" %d ", temp->data);
}



void show_cycle(int start,
  int end,
  int *visit,
  struct Graph *node,
  struct Stack **output,
  int n,
  int length) {

  if (start > size ||
    end > size ||
    start < 0 ||
    end < 0 ||
    node == NULL
  ) {

    return;
  }

  if (start == end && n == length) {
    //add element into stack
    push(output, start);
    printf("\n (");
    print_path( *output);
    printf(")");
    //remove top element
    pop(output);
  }
  if (visit[start] == 1) {
    //base case
    return;

  } else {
    //add element into stack
    push(output, start);
  }

  visit[start] = 1;

  struct AjlistNode *temp = node[start].next;

  while (temp != NULL) {
    show_cycle(temp->vId, end, visit, node, output, n, length + 1);

    temp = temp->next;
  }
  //remove top element
  pop(output);
  //reset the  value of visited node
  visit[start] = 0;

}

void find_cycle(struct Graph *node, int n) {

  if (node == NULL) {
    printf("Graph is Empty\n");
  } else {
    int *visit = (int *) calloc(size, sizeof(int));
    struct Stack *root = NULL;

    for (int i = 0; i < size; ++i) {
      show_cycle(i, i, visit, node, & root, n, 0);

    }
    printf("\n");

  }
}


int main() {

  size = 6;
  struct Graph *node = NULL;

  node = (struct Graph *) malloc(sizeof(struct Graph) *size);

  if (node == NULL) 
  {
    printf("\n Memory overflow");
  } 
  else 
  {
    //First set node keys
    set_data(node);


    //Connected two node with Edges
    add_edge(node, 0, 1);
    add_edge(node, 0, 2);
    add_edge(node, 0, 5);
    add_edge(node, 1, 2);
    add_edge(node, 1, 3);
    add_edge(node, 2, 3);
    add_edge(node, 2, 4);
    add_edge(node, 2, 5);
    add_edge(node, 3, 4);
    add_edge(node, 4, 5);
    print_graph(node);

    int n = 3;
    find_cycle(node, n);


  }
  return 0;
}

Output

 Adjacency list of vertex 0  :  1  2  5
 Adjacency list of vertex 1  :  0  2  3
 Adjacency list of vertex 2  :  0  1  3  4  5
 Adjacency list of vertex 3  :  1  2  4
 Adjacency list of vertex 4  :  2  3  5
 Adjacency list of vertex 5  :  0  2  4
 ( 0  1  2  0 )
 ( 0  2  1  0 )
 ( 0  2  5  0 )
 ( 0  5  2  0 )
 ( 1  0  2  1 )
 ( 1  2  0  1 )
 ( 1  2  3  1 )
 ( 1  3  2  1 )
 ( 2  0  1  2 )
 ( 2  0  5  2 )
 ( 2  1  0  2 )
 ( 2  1  3  2 )
 ( 2  3  1  2 )
 ( 2  3  4  2 )
 ( 2  4  3  2 )
 ( 2  4  5  2 )
 ( 2  5  0  2 )
 ( 2  5  4  2 )
 ( 3  1  2  3 )
 ( 3  2  1  3 )
 ( 3  2  4  3 )
 ( 3  4  2  3 )
 ( 4  2  3  4 )
 ( 4  2  5  4 )
 ( 4  3  2  4 )
 ( 4  5  2  4 )
 ( 5  0  2  5 )
 ( 5  2  0  5 )
 ( 5  2  4  5 )
 ( 5  4  2  5 )
// C++ Program
// Print all cycle in graph by given length
#include<iostream>

using namespace std;
class AjlistNode {
	public:

	//Vertices node key
	int id;
	AjlistNode *next;
	AjlistNode(int value) {
		//Set value of node key
		this->id = value;
		this->next = NULL;
	}
};
class Vertices {
	public:
	int data;
	AjlistNode *next;
    Vertices()
    {
      this->data = 0;
      this->next = NULL;
    }
	Vertices(int value) {
		this->data = value;
		this->next = NULL;
	}
};
class MyStack {
	public:
	int element;
	MyStack *next;
	MyStack(int element, MyStack *top) {
		this->element = element;
		this->next = top;
	}
};
class MyGraph {
	public:

	//number of Vertices
	int size;
	Vertices *node;
	MyStack *top;
	MyGraph(int size) {
		//set value
		this->size = size;
		this->top = NULL;
		node = new Vertices[size];
		this->set_data();
	}
	//Set initial node value
	void set_data() {
		if (node == NULL) {
			cout << "\nEmpty Graph";
		} else {
			for (int index = 0; index < this->size; index++) {
				node[index] = index;
			}
		}
	}
	//Connect two nodes
	void connect(int start, int last) {
		AjlistNode *new_edge = new AjlistNode(last);
		if (node[start].next == NULL) {
			node[start].next = new_edge;
		} else {
			AjlistNode *temp = node[start].next;
			while (temp->next != NULL) {
				temp = temp->next;
			}
			temp->next = new_edge;
		}
	}
	//Add edge of two nodes
	void add_edge(int start, int last) {
		if (start >= 0 &&
			start < this->size &&
			last >= 0 &&
			last < this->size &&
			node != NULL) {
			this->connect(start, last);
			if (start != last) {
				this->connect(last, start);
			}
		} else {
			cout << "\nHere Something Wrong";
		}
	}
	void push(int element) {
		MyStack *node = new MyStack(element, this->top);
		this->top = node;
	}
	void pop() {
		if (this->top != NULL) {
			this->top = this->top->next;
		}
	}
	//Display Path from source to destination
	void print_path(MyStack *temp) {
		if (temp == NULL) {
			return;
		}
		this->print_path(temp->next);
		cout << " " << temp->element << " ";
	}
	void show_cycle(int start, int last, bool visit[], int n, int length) {
		if (start > this->size ||
			last > this->size ||
			start < 0 ||
			last < 0 ||
			node == NULL) {
			return;
		}
		if (start == last &&
			n == length) {
			//add element into stack
			this->push(start);
			cout << "\n (";
			this->print_path(this->top);
			cout << ")";
			//remove top element
			this->pop();
		}
		if (visit[start] == true)
		//base case
		{
			return;
		} else {
			//Add element into stack
			this->push(start);
		}
		visit[start] = true;
		AjlistNode *temp = node[start].next;
		while (temp != NULL) {
			this->show_cycle(temp->id, last, visit, n, length + 1);
			temp = temp->next;
		}
		//remove top element
		this->pop();
		//reset the  value of visited node
		visit[start] = false;
	}
	void print_graph() {
		if (this->size > 0 &&
			node != NULL) {
			for (int index = 0; index < this->size; index++) {
				cout << "\n Adjacency list of vertex " << index << " : ";
				AjlistNode *temp = node[index].next;
				while (temp != NULL) {
					cout << " " << node[temp->id].data;
					temp = temp->next;
				}
			}
		}
	}
	void find_cycle(int n) {
		if (node == NULL) {
			cout << "Graph is Empty\n";
		} else {
			bool *visit = new bool[size];
			for (int i = 0; i < this->size; i++) {
				visit[i] = false;
			}
			for (int i = 0; i < this->size; ++i) {
				this->show_cycle(i, i, visit, n, 0);
			}
			cout << "\n";
		}
	}
};
int main() {
	int totalNode = 6;
	MyGraph g =  MyGraph(totalNode);
	//Connected two node with Edges
	g.add_edge(0, 1);
	g.add_edge(0, 2);
	g.add_edge(0, 5);
	g.add_edge(1, 2);
	g.add_edge(1, 3);
	g.add_edge(2, 3);
	g.add_edge(2, 4);
	g.add_edge(2, 5);
	g.add_edge(3, 4);
	g.add_edge(4, 5);
	g.print_graph();
	int cycle_length = 3;
	cout << "\n Cycles of length : " << cycle_length << " \n";
	g.find_cycle(cycle_length);
	return 0;
}

Output

 Adjacency list of vertex 0 :  1 2 5
 Adjacency list of vertex 1 :  0 2 3
 Adjacency list of vertex 2 :  0 1 3 4 5
 Adjacency list of vertex 3 :  1 2 4
 Adjacency list of vertex 4 :  2 3 5
 Adjacency list of vertex 5 :  0 2 4
 Cycles of length : 3

 ( 0  1  2  0 )
 ( 0  2  1  0 )
 ( 0  2  5  0 )
 ( 0  5  2  0 )
 ( 1  0  2  1 )
 ( 1  2  0  1 )
 ( 1  2  3  1 )
 ( 1  3  2  1 )
 ( 2  0  1  2 )
 ( 2  0  5  2 )
 ( 2  1  0  2 )
 ( 2  1  3  2 )
 ( 2  3  1  2 )
 ( 2  3  4  2 )
 ( 2  4  3  2 )
 ( 2  4  5  2 )
 ( 2  5  0  2 )
 ( 2  5  4  2 )
 ( 3  1  2  3 )
 ( 3  2  1  3 )
 ( 3  2  4  3 )
 ( 3  4  2  3 )
 ( 4  2  3  4 )
 ( 4  2  5  4 )
 ( 4  3  2  4 )
 ( 4  5  2  4 )
 ( 5  0  2  5 )
 ( 5  2  0  5 )
 ( 5  2  4  5 )
 ( 5  4  2  5 )
// Java Program
// Print all cycle in graph by given length

class AjlistNode {
  //Vertices node key
  public int id; 
  public AjlistNode next;

  public AjlistNode(int value) {
    //Set value of node key
    id = value;
    next = null;
  }
}
class Vertices {

  public int data;
  public AjlistNode next;

  public Vertices(int value) {
    data = value;
    next = null;
  }
}
class MyStack {
  public int element;
  public MyStack next;
  public MyStack(int element,MyStack top) 
  {
    this.element = element;
    this.next = top;
  }
}

public class MyGraph
{

  //number of Vertices
  public int size;
  
  public Vertices node[];
  
  public MyStack top;
  public MyGraph(int size)
  {
    //set value
    this.size = size;
    this.top = null;
    node=new Vertices[size];
    this.set_data();
    
  }

  //Set initial node value
  public void set_data()
  {
    if(node == null)
    {
      System.out.println("\nEmpty Graph");
    }else
    {
      for(int index=0;index<size;index++)
      {

        node[index]=new Vertices(index); 
      }
    }
  }

  //Connect two nodes
  public void connect(int start,int last )
  {
    AjlistNode new_edge=new AjlistNode(last);

    if(node[start].next==null)
    {
      node[start].next=new_edge;
    }else
    {
      AjlistNode temp=node[start].next;

      while(temp.next!=null)
      {
        temp=temp.next;
      }
      temp.next=new_edge;
    }
  }
  //Add edge of two nodes
  public void add_edge(int start,int last)
  {
    if(start>=0 && start < size && last >= 0 &&  last < size && node != null)
    {
      connect(start,last);
      if(start!=last)
      {
        connect(last,start);
      }
   
    }else
    {
      System.out.println("\nHere Something Wrong");
    }
  }

  public void push(int element)
  {
    MyStack node = new MyStack(element,top);

    top = node;

  }
  public void pop()
  {
    if(top != null)
    {
      top = top.next;
    }
  }
  //Display Path from source to destination
  public void print_path(MyStack temp) {
    if (temp == null) {
      return;
    }
    print_path(temp.next);
    System.out.print(" "+temp.element+" " );
  }
  public void show_cycle(int start, int last, boolean []visit, int n, int length) {
    if (start > size || last > size 
      || start < 0 || last < 0 || node == null) {
      return;
    }
    if (start == last && n == length) {
      //add element into stack
      push(start);
      System.out.print("\n (");
      print_path(top);
      System.out.print(")");
      //remove top element
      pop();
    }
    if (visit[start] == true) 
    {
      return ;
      //base case

    } 
    else 
    {
      //Add element into stack
      push(start);
    }
    visit[start] = true;
    AjlistNode temp = node[start].next;
    while (temp != null) {
      show_cycle(temp.id, last, visit, n, length + 1);
      temp = temp.next;
    }
    //remove top element
    pop();
    //reset the  value of visited node
    visit[start] = false;
  }

  public void print_graph()
  {

    if(size >0 && node!=null)
    {
      for(int index = 0; index < size; index++)
      {
        System.out.print("\n Adjacency list of vertex "+index+" : ");
        AjlistNode temp = node[index].next;
        while(temp != null)
        {
          System.out.print(" "+node[temp.id].data);
          temp=temp.next;
        }
      }
    }
  }
  public void find_cycle(int n) {
    if (node == null) 
    {
      System.out.print("Graph is Empty\n");
    } 
    else 
    {
      boolean []visit = new boolean[size];

      for (int i=0;i<size ;i++ ) 
      {
        visit[i]=false;
      }
 
      for (int i = 0; i < size; ++i) 
      {
        show_cycle(i, i, visit, n, 0);
      }
      System.out.print("\n");
    }
  }
  public static void main(String[] args) 
  {
    int totalNode=6;

    MyGraph g=new MyGraph(totalNode);

    //Connected two node with Edges
    g.add_edge( 0, 1);
    g.add_edge( 0, 2);
    g.add_edge( 0, 5);
    g.add_edge( 1, 2);
    g.add_edge( 1, 3);
    g.add_edge( 2, 3);
    g.add_edge( 2, 4);
    g.add_edge( 2, 5);
    g.add_edge( 3, 4);
    g.add_edge( 4, 5);

    g.print_graph();
    int cycle_length = 3;
    System.out.print("\n Cycles of length : "+cycle_length+" \n");
    g.find_cycle(cycle_length);

  }
}

Output

 Adjacency list of vertex 0 :  1 2 5
 Adjacency list of vertex 1 :  0 2 3
 Adjacency list of vertex 2 :  0 1 3 4 5
 Adjacency list of vertex 3 :  1 2 4
 Adjacency list of vertex 4 :  2 3 5
 Adjacency list of vertex 5 :  0 2 4
 Cycles of length : 3

 ( 0  1  2  0 )
 ( 0  2  1  0 )
 ( 0  2  5  0 )
 ( 0  5  2  0 )
 ( 1  0  2  1 )
 ( 1  2  0  1 )
 ( 1  2  3  1 )
 ( 1  3  2  1 )
 ( 2  0  1  2 )
 ( 2  0  5  2 )
 ( 2  1  0  2 )
 ( 2  1  3  2 )
 ( 2  3  1  2 )
 ( 2  3  4  2 )
 ( 2  4  3  2 )
 ( 2  4  5  2 )
 ( 2  5  0  2 )
 ( 2  5  4  2 )
 ( 3  1  2  3 )
 ( 3  2  1  3 )
 ( 3  2  4  3 )
 ( 3  4  2  3 )
 ( 4  2  3  4 )
 ( 4  2  5  4 )
 ( 4  3  2  4 )
 ( 4  5  2  4 )
 ( 5  0  2  5 )
 ( 5  2  0  5 )
 ( 5  2  4  5 )
 ( 5  4  2  5 )
// C# Program
// Print all cycle in graph by given.Length
using System;
public class AjlistNode {
	//Vertices node key
	public int id;
	public AjlistNode next;
	public AjlistNode(int value) {
		//Set value of node key
		id = value;
		next = null;
	}
}
public class Vertices {
	public int data;
	public AjlistNode next;
	public Vertices(int value) {
		data = value;
		next = null;
	}
}
public class MyStack {
	public int element;
	public MyStack next;
	public MyStack(int element, MyStack top) {
		this.element = element;
		this.next = top;
	}
}
public class MyGraph {
	//number of Vertices
	public int size;
	public Vertices[] node;
	public MyStack top;
	public MyGraph(int size) {
		//set value
		this.size = size;
		this.top = null;
		node = new Vertices[size];
		this.set_data();
	}
	//Set initial node value
	public void set_data() {
		if (node == null) {
			Console.WriteLine("\nEmpty Graph");
		} else {
			for (int index = 0; index < size; index++) {
				node[index] = new Vertices(index);
			}
		}
	}
	//Connect two nodes
	public void connect(int start, int last) {
		AjlistNode new_edge = new AjlistNode(last);
		if (node[start].next == null) {
			node[start].next = new_edge;
		} else {
			AjlistNode temp = node[start].next;
			while (temp.next != null) {
				temp = temp.next;
			}
			temp.next = new_edge;
		}
	}
	//Add edge of two nodes
	public void add_edge(int start, int last) {
		if (start >= 0 &&
			start < size &&
			last >= 0 &&
			last < size &&
			node != null) {
			connect(start, last);
			if (start != last) {
				connect(last, start);
			}
		} else {
			Console.WriteLine("\nHere Something Wrong");
		}
	}
	public void push(int element) {
		MyStack node = new MyStack(element, top);
		top = node;
	}
	public void pop() {
		if (top != null) {
			top = top.next;
		}
	}
	//Display Path from source to destination
	public void print_path(MyStack temp) {
		if (temp == null) {
			return;
		}
		print_path(temp.next);
		Console.Write(" " + temp.element + " ");
	}
	public void show_cycle(int start, int last, Boolean[] visit, int n, int length) {
		if (start > size ||
			last > size ||
			start < 0 ||
			last < 0 ||
			node == null) {
			return;
		}
		if (start == last &&
			n ==length) {
			push(start);
			Console.Write("\n (");
			print_path(top);
			Console.Write(")");
			pop();
		}
		if (visit[start] == true)
		//base case
		{
			return;
		} else {
			push(start);
		}
		visit[start] = true;
		AjlistNode temp = node[start].next;
		while (temp != null) {
			show_cycle(temp.id, last, visit, n,length + 1);
			temp = temp.next;
		}
		pop();
		//reset the  value of visited node
		visit[start] = false;
	}
	public void print_graph() {
		if (size > 0 &&
			node != null) {
			for (int index = 0; index < size; index++) {
				Console.Write("\n Adjacency list of vertex " + index + " : ");
				AjlistNode temp = node[index].next;
				while (temp != null) {
					Console.Write(" " + node[temp.id].data);
					temp = temp.next;
				}
			}
		}
	}
	public void find_cycle(int n) {
		if (node == null) {
			Console.Write("Graph is Empty\n");
		} else {
			Boolean[] visit = new Boolean[size];
			for (int i = 0; i < size; i++) {
				visit[i] = false;
			}
			for (int i = 0; i < size; ++i) {
				show_cycle(i, i, visit, n, 0);
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args) {
		int totalNode = 6;
		MyGraph g = new MyGraph(totalNode);
		g.add_edge(0, 1);
		g.add_edge(0, 2);
		g.add_edge(0, 5);
		g.add_edge(1, 2);
		g.add_edge(1, 3);
		g.add_edge(2, 3);
		g.add_edge(2, 4);
		g.add_edge(2, 5);
		g.add_edge(3, 4);
		g.add_edge(4, 5);
		g.print_graph();
		int cycle_length = 3;
		Console.Write("\n Cycles of.Length : " + cycle_length + " \n");
		g.find_cycle(cycle_length);
	}
}

Output

 Adjacency list of vertex 0 :  1 2 5
 Adjacency list of vertex 1 :  0 2 3
 Adjacency list of vertex 2 :  0 1 3 4 5
 Adjacency list of vertex 3 :  1 2 4
 Adjacency list of vertex 4 :  2 3 5
 Adjacency list of vertex 5 :  0 2 4
 Cycles of.Length : 3

 ( 0  1  2  0 )
 ( 0  2  1  0 )
 ( 0  2  5  0 )
 ( 0  5  2  0 )
 ( 1  0  2  1 )
 ( 1  2  0  1 )
 ( 1  2  3  1 )
 ( 1  3  2  1 )
 ( 2  0  1  2 )
 ( 2  0  5  2 )
 ( 2  1  0  2 )
 ( 2  1  3  2 )
 ( 2  3  1  2 )
 ( 2  3  4  2 )
 ( 2  4  3  2 )
 ( 2  4  5  2 )
 ( 2  5  0  2 )
 ( 2  5  4  2 )
 ( 3  1  2  3 )
 ( 3  2  1  3 )
 ( 3  2  4  3 )
 ( 3  4  2  3 )
 ( 4  2  3  4 )
 ( 4  2  5  4 )
 ( 4  3  2  4 )
 ( 4  5  2  4 )
 ( 5  0  2  5 )
 ( 5  2  0  5 )
 ( 5  2  4  5 )
 ( 5  4  2  5 )
<?php
// Php Program
// Print all cycle in graph by given length
class AjlistNode {
	//Vertices node key
	public $id;
	public $next;

	function __construct($value) {
		//Set value of node key
		$this->id = $value;
		$this->next = null;
	}
}
class Vertices {
	public $data;
	public $next;

	function __construct($value) {
		$this->data = $value;
		$this->next = null;
	}
}
class MyStack {
	public $element;
	public $next;

	function __construct($element, $top) {
		$this->element = $element;
		$this->next = $top;
	}
}
class MyGraph {
	//number of Vertices
	public $size;
	public $node;
	public $top;

	function __construct($size) {
		//set value
		$this->size = $size;
		$this->top = null;
		$this->node = array_fill(0, $size, null);
		$this->set_data();
	}
	//Set initial node value
	public 	function set_data() {
		if ($this->node == null) {
			echo("\nEmpty Graph");
		} else {
			for ($index = 0; $index < $this->size; $index++) {
				$this->node[$index] = new Vertices($index);
			}
		}
	}
	//Connect two nodes
	public 	function connect($start, $last) {
		$new_edge = new AjlistNode($last);
		if ($this->node[$start]->next == null) {
			$this->node[$start]->next = $new_edge;
		} else {
			$temp = $this->node[$start]->next;
			while ($temp->next != null) {
				$temp = $temp->next;
			}
			$temp->next = $new_edge;
		}
	}
	//Add edge of two nodes
	public 	function add_edge($start, $last) {
		if ($start >= 0 &&
			$start < $this->size &&
			$last >= 0 &&
			$last < $this->size &&
			$this->node != null) {
			$this->connect($start, $last);
			if ($start != $last) {
				$this->connect($last, $start);
			}
		} else {
			echo("\nHere Something Wrong");
		}
	}
	public 	function push($element) {
		$node = new MyStack($element, $this->top);
		$this->top = $node;
	}
	public 	function pop() {
		if ($this->top != null) {
			$this->top = $this->top->next;
		}
	}
	//Display Path from source to destination
	public 	function print_path($temp) {
		if ($temp == null) {
			return;
		}
		$this->print_path($temp->next);
		echo(" ". $temp->element ." ");
	}
	public 	function show_cycle($start, $last, & $visit, $n, $length) {
		if ($start > $this->size ||
			$last > $this->size ||
			$start < 0 ||
			$last < 0 ||
			$this->node == null) {
			return;
		}
		if ($start == $last &&
			$n == $length) {
			//add element into stack
			$this->push($start);
			echo("\n (");
			$this->print_path($this->top);
			echo(")");
			//remove top element
			$this->pop();
		}
		if ($visit[$start] == true)
		//base case
		{
			return;
		} else {
			//Add element into stack
			$this->push($start);
		}
		$visit[$start] = true;
		$temp = $this->node[$start]->next;
		while ($temp != null) {
			$this->show_cycle($temp->id, $last, $visit, $n, $length + 1);
			$temp = $temp->next;
		}
		//remove top element
		$this->pop();
		//reset the  value of visited node
		$visit[$start] = false;
	}
	public 	function print_graph() {
		if ($this->size > 0 &&
			$this->node != null) {
			for ($index = 0; $index < $this->size; $index++) {
				echo("\n Adjacency list of vertex ". $index ." : ");
				$temp = $this->node[$index]->next;
				while ($temp != null) {
					echo(" ". $this->node[$temp->id]->data);
					$temp = $temp->next;
				}
			}
		}
	}
	public 	function find_cycle($n) {
		if ($this->node == null) {
			echo("Graph is Empty\n");
		} else {
			$visit = array_fill(0, $this->size, false);
			for ($i = 0; $i < $this->size; $i++) {
				$visit[$i] = false;
			}
			for ($i = 0; $i < $this->size; ++$i) {
				$this->show_cycle($i, $i, $visit, $n, 0);
			}
			echo("\n");
		}
	}
}

function main() {
	$totalNode = 6;
	$g = new MyGraph($totalNode);
	//Connected two node with Edges
	$g->add_edge(0, 1);
	$g->add_edge(0, 2);
	$g->add_edge(0, 5);
	$g->add_edge(1, 2);
	$g->add_edge(1, 3);
	$g->add_edge(2, 3);
	$g->add_edge(2, 4);
	$g->add_edge(2, 5);
	$g->add_edge(3, 4);
	$g->add_edge(4, 5);
	$g->print_graph();
	$cycle_length = 3;
	echo("\n Cycles of length : ". $cycle_length ." \n");
	$g->find_cycle($cycle_length);

}
main();

Output

 Adjacency list of vertex 0 :  1 2 5
 Adjacency list of vertex 1 :  0 2 3
 Adjacency list of vertex 2 :  0 1 3 4 5
 Adjacency list of vertex 3 :  1 2 4
 Adjacency list of vertex 4 :  2 3 5
 Adjacency list of vertex 5 :  0 2 4
 Cycles of length : 3

 ( 0  1  2  0 )
 ( 0  2  1  0 )
 ( 0  2  5  0 )
 ( 0  5  2  0 )
 ( 1  0  2  1 )
 ( 1  2  0  1 )
 ( 1  2  3  1 )
 ( 1  3  2  1 )
 ( 2  0  1  2 )
 ( 2  0  5  2 )
 ( 2  1  0  2 )
 ( 2  1  3  2 )
 ( 2  3  1  2 )
 ( 2  3  4  2 )
 ( 2  4  3  2 )
 ( 2  4  5  2 )
 ( 2  5  0  2 )
 ( 2  5  4  2 )
 ( 3  1  2  3 )
 ( 3  2  1  3 )
 ( 3  2  4  3 )
 ( 3  4  2  3 )
 ( 4  2  3  4 )
 ( 4  2  5  4 )
 ( 4  3  2  4 )
 ( 4  5  2  4 )
 ( 5  0  2  5 )
 ( 5  2  0  5 )
 ( 5  2  4  5 )
 ( 5  4  2  5 )
// Node Js Program
// Print all cycle in graph by given length
class AjlistNode {
	//Vertices node key
	constructor(value) {
		//Set value of node key
		this.id = value;
		this.next = null;
	}
}
class Vertices {
	constructor(value) {
		this.data = value;
		this.next = null;
	}
}
class MyStack {
	constructor(element, top) {
		this.element = element;
		this.next = top;
	}
}
class MyGraph {
	//number of Vertices
	constructor(size) {
		//set value
		this.size = size;
		this.top = null;
		this.node = Array(size).fill(null);
		this.set_data();
	}

	//Set initial node value
	set_data() {
		if (this.node == null) {
			process.stdout.write("\nEmpty Graph");
		} else {
			for (var index = 0; index < this.size; index++) {
				this.node[index] = new Vertices(index);
			}
		}
	}

	//Connect two nodes
	connect(start, last) {
		var new_edge = new AjlistNode(last);
		if (this.node[start].next == null) {
			this.node[start].next = new_edge;
		} else {
			var temp = this.node[start].next;
			while (temp.next != null) {
				temp = temp.next;
			}
			temp.next = new_edge;
		}
	}

	//Add edge of two nodes
	add_edge(start, last) {
		if (start >= 0 &&
			start < this.size &&
			last >= 0 &&
			last < this.size &&
			this.node != null) {
			this.connect(start, last);
			if (start != last) {
				this.connect(last, start);
			}
		} else {
			process.stdout.write("\nHere Something Wrong");
		}
	}
	push(element) {
		var node = new MyStack(element, this.top);
		this.top = node;
	}
	pop() {
		if (this.top != null) {
			this.top = this.top.next;
		}
	}

	//Display Path from source to destination
	print_path(temp) {
		if (temp == null) {
			return;
		}
		this.print_path(temp.next);
		process.stdout.write(" " + temp.element + " ");
	}
	show_cycle(start, last, visit, n, length) {
		if (start > this.size ||
			last > this.size ||
			start < 0 ||
			last < 0 ||
			this.node == null) {
			return;
		}

		if (start == last &&
			n == length) {
			//add element into stack
			this.push(start);
			process.stdout.write("\n (");
			this.print_path(this.top);
			process.stdout.write(")");
			//remove top element
			this.pop();
		}

		if (visit[start] == true)
		//base case
		{
			return;
		} else {
			//Add element into stack
			this.push(start);
		}
		visit[start] = true;
		var temp = this.node[start].next;
		while (temp != null) {
			this.show_cycle(temp.id, last, visit, n, length + 1);
			temp = temp.next;
		}

		//remove top element
		this.pop();
		//reset the  value of visited node
		visit[start] = false;
	}
	print_graph() {
		if (this.size > 0 &&
			this.node != null) {
			for (var index = 0; index < this.size; index++) {
				process.stdout.write("\n Adjacency list of vertex " + index + " : ");
				var temp = this.node[index].next;
				while (temp != null) {
					process.stdout.write(" " + this.node[temp.id].data);
					temp = temp.next;
				}
			}
		}
	}
	find_cycle(n) {
		if (this.node == null) {
			process.stdout.write("Graph is Empty\n");
		} else {
			var visit = Array(this.size).fill(false);
			for (var i = 0; i < this.size; i++) {
				visit[i] = false;
			}

			for (var i = 0; i < this.size; ++i) {
				this.show_cycle(i, i, visit, n, 0);
			}

			process.stdout.write("\n");
		}
	}
}

function main(args) {
	var totalNode = 6;
	var g = new MyGraph(totalNode);
	//Connected two node with Edges
	g.add_edge(0, 1);
	g.add_edge(0, 2);
	g.add_edge(0, 5);
	g.add_edge(1, 2);
	g.add_edge(1, 3);
	g.add_edge(2, 3);
	g.add_edge(2, 4);
	g.add_edge(2, 5);
	g.add_edge(3, 4);
	g.add_edge(4, 5);
	g.print_graph();
	var cycle_length = 3;
	process.stdout.write("\n Cycles of length : " + cycle_length + " \n");
	g.find_cycle(cycle_length);
}

main();

Output

 Adjacency list of vertex 0 :  1 2 5
 Adjacency list of vertex 1 :  0 2 3
 Adjacency list of vertex 2 :  0 1 3 4 5
 Adjacency list of vertex 3 :  1 2 4
 Adjacency list of vertex 4 :  2 3 5
 Adjacency list of vertex 5 :  0 2 4
 Cycles of length : 3

 ( 0  1  2  0 )
 ( 0  2  1  0 )
 ( 0  2  5  0 )
 ( 0  5  2  0 )
 ( 1  0  2  1 )
 ( 1  2  0  1 )
 ( 1  2  3  1 )
 ( 1  3  2  1 )
 ( 2  0  1  2 )
 ( 2  0  5  2 )
 ( 2  1  0  2 )
 ( 2  1  3  2 )
 ( 2  3  1  2 )
 ( 2  3  4  2 )
 ( 2  4  3  2 )
 ( 2  4  5  2 )
 ( 2  5  0  2 )
 ( 2  5  4  2 )
 ( 3  1  2  3 )
 ( 3  2  1  3 )
 ( 3  2  4  3 )
 ( 3  4  2  3 )
 ( 4  2  3  4 )
 ( 4  2  5  4 )
 ( 4  3  2  4 )
 ( 4  5  2  4 )
 ( 5  0  2  5 )
 ( 5  2  0  5 )
 ( 5  2  4  5 )
 ( 5  4  2  5 )
#  Python 3 Program
#  Print all cycle in graph by given length
class AjlistNode :
	# Vertices node key
	def __init__(self, value) :
		# Set value of node key
		self.id = value
		self.next = None
	

class Vertices :
	
	def __init__(self, value) :
		self.data = value
		self.next = None
	

class MyStack :
	
	def __init__(self, element, top) :
		self.element = element
		self.next = top
	

class MyGraph :
	# number of Vertices
	def __init__(self, size) :
		# set value
		self.size = size
		self.top = None
		self.node = [None] * size
		self.set_data()
	
	# Set initial node value
	def set_data(self) :
		if (self.node == None) :
			print("\nEmpty Graph", end = "")
		else :
			index = 0
			while (index < self.size) :
				self.node[index] = Vertices(index)
				index += 1
			
		
	
	# Connect two nodes
	def connect(self, start, last) :
		new_edge = AjlistNode(last)
		if (self.node[start].next == None) :
			self.node[start].next = new_edge
		else :
			temp = self.node[start].next
			while (temp.next != None) :
				temp = temp.next
			
			temp.next = new_edge
		
	
	# Add edge of two nodes
	def add_edge(self, start, last) :
		if (start >= 0 and start < self.size and 
            last >= 0 and last < self.size and self.node != None) :
			self.connect(start, last)
			if (start != last) :
				self.connect(last, start)
			
		else :
			print("\nHere Something Wrong", end = "")
		
	
	def push(self, element) :
		node = MyStack(element, self.top)
		self.top = node
	
	def pop(self) :
		if (self.top != None) :
			self.top = self.top.next
		
	
	# Display Path from source to destination
	def print_path(self, temp) :
		if (temp == None) :
			return
		
		self.print_path(temp.next)
		print(" ", temp.element ," ", end = "")
	
	def show_cycle(self, start, last, visit, n, length) :
		if (start > self.size or last > self.size or 
            start < 0 or last < 0 or self.node == None) :
			return
		
		if (start == last and n == length) :
			self.push(start)
			print("\n (", end = "")
			self.print_path(self.top)
			print(")", end = "")
			self.pop()
		
		if (visit[start] == True) :
        	# base case
			return
		else :
			self.push(start)
		
		visit[start] = True
		temp = self.node[start].next
		while (temp != None) :
			self.show_cycle(temp.id, last, visit, n, length + 1)
			temp = temp.next
		
		self.pop()
		# reset the  value of visited node
		visit[start] = False
	
	def print_graph(self) :
		if (self.size > 0 and self.node != None) :
			index = 0
			while (index < self.size) :
				print("\n Adjacency list of vertex ", index ," : ", end = "")
				temp = self.node[index].next
				while (temp != None) :
					print(" ", self.node[temp.id].data, end = "")
					temp = temp.next
				
				index += 1
			
		
	
	def find_cycle(self, n) :
		if (self.node == None) :
			print("Graph is Empty\n", end = "")
		else :
			visit = [False] * self.size
			i = 0
			while (i < self.size) :
				visit[i] = False
				i += 1
			
			i = 0
			while (i < self.size) :
				self.show_cycle(i, i, visit, n, 0)
				i += 1
			
			print("\n", end = "")
		
	

def main() :
	totalNode = 6
	g = MyGraph(totalNode)
	g.add_edge(0, 1)
	g.add_edge(0, 2)
	g.add_edge(0, 5)
	g.add_edge(1, 2)
	g.add_edge(1, 3)
	g.add_edge(2, 3)
	g.add_edge(2, 4)
	g.add_edge(2, 5)
	g.add_edge(3, 4)
	g.add_edge(4, 5)
	g.print_graph()
	cycle_length = 3
	print("\n Cycles of length : ", cycle_length ," \n", end = "")
	g.find_cycle(cycle_length)


if __name__ == "__main__":
	main()

Output

 Adjacency list of vertex  0  :   1  2  5
 Adjacency list of vertex  1  :   0  2  3
 Adjacency list of vertex  2  :   0  1  3  4  5
 Adjacency list of vertex  3  :   1  2  4
 Adjacency list of vertex  4  :   2  3  5
 Adjacency list of vertex  5  :   0  2  4
 Cycles of length :  3

 (  0    1    2    0  )
 (  0    2    1    0  )
 (  0    2    5    0  )
 (  0    5    2    0  )
 (  1    0    2    1  )
 (  1    2    0    1  )
 (  1    2    3    1  )
 (  1    3    2    1  )
 (  2    0    1    2  )
 (  2    0    5    2  )
 (  2    1    0    2  )
 (  2    1    3    2  )
 (  2    3    1    2  )
 (  2    3    4    2  )
 (  2    4    3    2  )
 (  2    4    5    2  )
 (  2    5    0    2  )
 (  2    5    4    2  )
 (  3    1    2    3  )
 (  3    2    1    3  )
 (  3    2    4    3  )
 (  3    4    2    3  )
 (  4    2    3    4  )
 (  4    2    5    4  )
 (  4    3    2    4  )
 (  4    5    2    4  )
 (  5    0    2    5  )
 (  5    2    0    5  )
 (  5    2    4    5  )
 (  5    4    2    5  )
#  Ruby Program
#  Print all cycle in graph by given length
class AjlistNode
    # Define the accessor and reader of class AjlistNode
    attr_reader :id, :next
    attr_accessor :id, :next 
	# Vertices node key
	def initialize(value) 
		# Set value of node key
		@id = value
		@next = nil
	end
end
class Vertices
    # Define the accessor and reader of class Vertices
    attr_reader :data, :next
    attr_accessor :data, :next 
	
	def initialize(value) 
		@data = value
		@next = nil
	end
end
class MyStack
    # Define the accessor and reader of class MyStack
    attr_reader :element, :next
    attr_accessor :element, :next 
	
	def initialize(element, top) 
		self.element = element
		self.next = top
	end
end
class MyGraph
    # Define the accessor and reader of class MyGraph
    attr_reader :size, :node, :top
    attr_accessor :size, :node, :top 
	# number of Vertices
	def initialize(size) 
		# set value
		self.size = size
		self.top = nil
		@node = Array.new(size) {nil}
		self.set_data()
	end
	# Set initial node value
	def set_data() 
		if (@node == nil) 
			print("\nEmpty Graph")
		else 
			index = 0
			while (index < @size) 
				@node[index] = Vertices.new(index)
				index += 1
			end
		end
	end
	# Connect two nodes
	def connect(start, last) 
		new_edge = AjlistNode.new(last)
		if (@node[start].next == nil) 
			@node[start].next = new_edge
		else 
			temp = @node[start].next
			while (temp.next != nil) 
				temp = temp.next
			end
			temp.next = new_edge
		end
	end
	# Add edge of two nodes
	def add_edge(start, last) 
		if (start >= 0 &&
			start < @size &&
			last >= 0 &&
			last < @size &&
			@node != nil) 
			self.connect(start, last)
			if (start != last) 
				self.connect(last, start)
			end
		else 
			print("\nHere Something Wrong")
		end
	end
	def push(element) 
		new_node = MyStack.new(element, @top)
		@top = new_node
	end
	def pop() 
		if (@top != nil) 
			@top = @top.next
		end
	end
	# Display Path from source to destination
	def print_path(temp) 
		if (temp == nil) 
			return
		end
		self.print_path(temp.next)
		print(" ", temp.element ," ")
	end
	def show_cycle(start, last, visit, n, length) 
		if (start > @size ||
			last > @size ||
			start < 0 ||
			last < 0 ||
			@node == nil) 
			return
		end
		if (start == last &&
			n == length) 
			self.push(start)
			print("\n (")
			self.print_path(@top)
			print(")")
			self.pop()
		end
		if (visit[start] == true)
			# base case
			return
		else 
			self.push(start)
		end
		visit[start] = true
		temp = @node[start].next
		while (temp != nil) 
			self.show_cycle(temp.id, last, visit, n, length + 1)
			temp = temp.next
		end
		self.pop()
		# reset the  value of visited node
		visit[start] = false
	end
	def print_graph() 
		if (@size > 0 &&
			@node != nil) 
			index = 0
			while (index < @size) 
				print("\n Adjacency list of vertex ", index ," : ")
				temp = @node[index].next
				while (temp != nil) 
					print(" ", @node[temp.id].data)
					temp = temp.next
				end
				index += 1
			end
		end
	end
	def find_cycle(n) 
		if (@node == nil) 
			print("Graph is Empty\n")
		else 
			visit = Array.new(@size) {false}
			i = 0
			while (i < @size) 
				visit[i] = false
				i += 1
			end
			i = 0
			while (i < @size) 
				self.show_cycle(i, i, visit, n, 0)
				i += 1
			end
			print("\n")
		end
	end
end
def main() 
	totalNode = 6
	g = MyGraph.new(totalNode)
	g.add_edge(0, 1)
	g.add_edge(0, 2)
	g.add_edge(0, 5)
	g.add_edge(1, 2)
	g.add_edge(1, 3)
	g.add_edge(2, 3)
	g.add_edge(2, 4)
	g.add_edge(2, 5)
	g.add_edge(3, 4)
	g.add_edge(4, 5)
	g.print_graph()
	cycle_length = 3
	print("\n Cycles of length : ", cycle_length ," \n")
	g.find_cycle(cycle_length)
end
main()

Output

 Adjacency list of vertex 0 :  1 2 5
 Adjacency list of vertex 1 :  0 2 3
 Adjacency list of vertex 2 :  0 1 3 4 5
 Adjacency list of vertex 3 :  1 2 4
 Adjacency list of vertex 4 :  2 3 5
 Adjacency list of vertex 5 :  0 2 4
 Cycles of length : 3 

 ( 0  1  2  0 )
 ( 0  2  1  0 )
 ( 0  2  5  0 )
 ( 0  5  2  0 )
 ( 1  0  2  1 )
 ( 1  2  0  1 )
 ( 1  2  3  1 )
 ( 1  3  2  1 )
 ( 2  0  1  2 )
 ( 2  0  5  2 )
 ( 2  1  0  2 )
 ( 2  1  3  2 )
 ( 2  3  1  2 )
 ( 2  3  4  2 )
 ( 2  4  3  2 )
 ( 2  4  5  2 )
 ( 2  5  0  2 )
 ( 2  5  4  2 )
 ( 3  1  2  3 )
 ( 3  2  1  3 )
 ( 3  2  4  3 )
 ( 3  4  2  3 )
 ( 4  2  3  4 )
 ( 4  2  5  4 )
 ( 4  3  2  4 )
 ( 4  5  2  4 )
 ( 5  0  2  5 )
 ( 5  2  0  5 )
 ( 5  2  4  5 )
 ( 5  4  2  5 )
// Scala Program
// Print all cycle in graph by given length
class AjlistNode(var id: Int,
	var next: AjlistNode) {

	def this(id: Int) {
		//Set value
      	this(id,null);
	}
}
class Vertices(var data: Int,
	var next: AjlistNode) {

	def this(data: Int) {
		this(data,null);
	}
}
class MyStack(var element: Int,
	var next: MyStack) {
}
class MyGraph(var size: Int,
  var node: Array[Vertices],
  var top : MyStack) {

  def this(size: Int) {
    this(size,Array.fill[Vertices](size)(null),null);

    //set initial values of graph node
    this.set_data();
  }
	//Set initial node value
	def set_data(): Unit = {
		if (node == null) {
			print("\nEmpty Graph");
		} else {
			var index: Int = 0;
			while (index < size) {
				node(index) = new Vertices(index);
				index += 1;
			}
		}
	}
	//Connect two nodes
	def connect(start: Int, last: Int): Unit = {
		var new_edge: AjlistNode = new AjlistNode(last);

		if (node(start).next == null) {
			node(start).next = new_edge;
		} else {
			var temp: AjlistNode = node(start).next;
			while (temp.next != null) {
				temp = temp.next;
			}
			temp.next = new_edge;
		}
	}
	//Add edge of two nodes
	def add_edge(start: Int, last: Int): Unit = {
		if (start >= 0 &&
			start < size &&
			last >= 0 &&
			last < size &&
			node != null) {
			connect(start, last);

			if (start != last) {
				connect(last, start);
			}
		} else {
			print("\nHere Something Wrong");
		}
	}
	def push(element: Int): Unit = {
		var node: MyStack = new MyStack(element, top);
		top = node;
	}
	def pop(): Unit = {
		if (top != null) {
			top = top.next;
		}
	}
	//Display Path from source to destination
	def print_path(temp: MyStack): Unit = {
		if (temp == null) {
			return;
		}
		print_path(temp.next);
		print(" " + temp.element + " ");
	}
	def show_cycle(start: Int, last: Int, visit: Array[Boolean], n: Int, length: Int): Unit = {
		if (start > size ||
			last > size ||
			start < 0 ||
			last < 0 ||
			node == null) {
			return;
		}
		if (start == last &&
			n == length) {
			push(start);
			print("\n (");
			print_path(top);
			print(")");
			pop();
		}
		if (visit(start) == true)
		//base case
		{
			return;
		} else {
			push(start);
		}
		visit(start) = true;
		var temp: AjlistNode = node(start).next;
		while (temp != null) {
			show_cycle(temp.id, last, visit, n, length + 1);
			temp = temp.next;
		}
		pop();

		//reset the  value of visited node
		visit(start) = false;
	}
	def print_graph(): Unit = {
		if (size > 0 &&
			node != null) {
			var index: Int = 0;
			while (index < size) {
				print("\n Adjacency list of vertex " + index + " : ");
				var temp: AjlistNode = node(index).next;
				while (temp != null) {
					print(" " + node(temp.id).data);
					temp = temp.next;
				}
				index += 1;
			}
		}
	}
	def find_cycle(n: Int): Unit = {
		if (node == null) {
			print("Graph is Empty\n");
		} else {
			var visit: Array[Boolean] = Array.fill[Boolean](this.size)(false);
			var i: Int = 0;
			while (i < size) {
				visit(i) = false;
				i += 1;
			}
			i = 0;
			while (i < size) {
				show_cycle(i, i, visit, n, 0);
				i += 1;
			}
			print("\n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var totalNode: Int = 6;
		var g: MyGraph = new MyGraph(totalNode);
		g.add_edge(0, 1);
		g.add_edge(0, 2);
		g.add_edge(0, 5);
		g.add_edge(1, 2);
		g.add_edge(1, 3);
		g.add_edge(2, 3);
		g.add_edge(2, 4);
		g.add_edge(2, 5);
		g.add_edge(3, 4);
		g.add_edge(4, 5);
		g.print_graph();
		var cycle_length: Int = 3;
		print("\n Cycles of length : " + cycle_length + " \n");
		g.find_cycle(cycle_length);
	}
}

Output

 Adjacency list of vertex 0 :  1 2 5
 Adjacency list of vertex 1 :  0 2 3
 Adjacency list of vertex 2 :  0 1 3 4 5
 Adjacency list of vertex 3 :  1 2 4
 Adjacency list of vertex 4 :  2 3 5
 Adjacency list of vertex 5 :  0 2 4
 Cycles of length : 3

 ( 0  1  2  0 )
 ( 0  2  1  0 )
 ( 0  2  5  0 )
 ( 0  5  2  0 )
 ( 1  0  2  1 )
 ( 1  2  0  1 )
 ( 1  2  3  1 )
 ( 1  3  2  1 )
 ( 2  0  1  2 )
 ( 2  0  5  2 )
 ( 2  1  0  2 )
 ( 2  1  3  2 )
 ( 2  3  1  2 )
 ( 2  3  4  2 )
 ( 2  4  3  2 )
 ( 2  4  5  2 )
 ( 2  5  0  2 )
 ( 2  5  4  2 )
 ( 3  1  2  3 )
 ( 3  2  1  3 )
 ( 3  2  4  3 )
 ( 3  4  2  3 )
 ( 4  2  3  4 )
 ( 4  2  5  4 )
 ( 4  3  2  4 )
 ( 4  5  2  4 )
 ( 5  0  2  5 )
 ( 5  2  0  5 )
 ( 5  2  4  5 )
 ( 5  4  2  5 )
// Swift Program
// Print all cycle in graph by given length
class AjlistNode {
	//Vertices node key
	var id: Int;
	var next: AjlistNode? ;
	init(_ value: Int) {
		//Set value of node key
		self.id = value;
		self.next = nil;
	}
}
class Vertices {
	var data: Int;
	var next: AjlistNode? ;
	init(_ value: Int) {
		self.data = value;
		self.next = nil;
	}
}
class MyStack {
	var element: Int;
	var next: MyStack? ;
	init(_ element: Int, _ top: MyStack? ) {
		self.element = element;
		self.next = top;
	}
}
class MyGraph {
	//number of Vertices
	var size: Int;
	var node: [Vertices]?=[Vertices]();
	var top: MyStack? ;
	init(_ size: Int) {
		//set value
		self.size = size;
		self.top = nil;
        var i = 0;
        while (i<size) {
          self.node!.append(Vertices(i));
          i+=1;
        }
	}
	
	//Connect two nodes
	func connect(_ start: Int, _ last: Int) {
		let new_edge: AjlistNode? = AjlistNode(last);
		if (self.node![start].next == nil) {
			self.node![start].next = new_edge;
		} else {
			var temp: AjlistNode? = self.node![start].next;
			while (temp!.next != nil) {
				temp = temp!.next;
			}
			temp!.next = new_edge;
		}
	}
	//Add edge of two nodes
	func add_edge(_ start: Int, _ last: Int) {
		if (start >= 0 &&
			start < self.size &&
			last >= 0 &&
			last < self.size &&
			self.node != nil) {
			self.connect(start, last);
			if (start != last) {
				self.connect(last, start);
			}
		} else {
			print("\nHere Something Wrong", terminator: "");
		}
	}
	func push(_ element: Int) {
		let node: MyStack? = MyStack(element, self.top);
		self.top = node;
	}
	func pop() {
		if (self.top != nil) {
			self.top = self.top!.next;
		}
	}
	//Display Path from source to destination
	func print_path(_ temp: MyStack? ) {
		if (temp == nil) {
			return;
		}
		self.print_path(temp!.next);
		print(" ", temp!.element ," ", terminator: "");
	}
	func show_cycle(_ start: Int, _ last: Int, _ visit: inout[Bool], _ n: Int, _ length: Int) {
		if (start > self.size ||
			last > self.size ||
			start < 0 ||
			last < 0 ||
			self.node == nil) {
			return;
		}
		if (start == last &&
			n == length) {
			self.push(start);
			print("\n (", terminator: "");
			self.print_path(self.top);
			print(")", terminator: "");
			self.pop();
		}
		if (visit[start] == true)
		//base case
		{
			return;
		} else {
			self.push(start);
		}
		visit[start] = true;
		var temp: AjlistNode? = self.node![start].next;
		while (temp != nil) {
			self.show_cycle(temp!.id, last, &visit, n, length + 1);
			temp = temp!.next;
		}
		self.pop();
		//reset the  value of visited node
		visit[start] = false;
	}
	func print_graph() {
		if (self.size > 0 &&
			self.node != nil) {
			var index: Int = 0;
			while (index < self.size) {
				print("\n Adjacency list of vertex ", index ," : ", terminator: "");
				var temp: AjlistNode? = self.node![index].next;
				while (temp != nil) {
					print(" ", self.node![temp!.id].data, terminator: "");
					temp = temp!.next;
				}
				index += 1;
			}
		}
	}
	func find_cycle(_ n: Int) {
		if (self.node == nil) {
			print("Graph is Empty\n", terminator: "");
		} else {
			var visit: [Bool] = Array(repeating: false, count: self.size);
			var i: Int = 0;
			while (i < self.size) {
				visit[i] = false;
				i += 1;
			}
			i = 0;
			while (i < self.size) {
				self.show_cycle(i, i, &visit, n, 0);
				i += 1;
			}
			print("\n", terminator: "");
		}
	}
}
func main() {
	let totalNode: Int = 6;
	let g: MyGraph? = MyGraph(totalNode);
	g!.add_edge(0, 1);
	g!.add_edge(0, 2);
	g!.add_edge(0, 5);
	g!.add_edge(1, 2);
	g!.add_edge(1, 3);
	g!.add_edge(2, 3);
	g!.add_edge(2, 4);
	g!.add_edge(2, 5);
	g!.add_edge(3, 4);
	g!.add_edge(4, 5);
	g!.print_graph();
	let cycle_length: Int = 3;
	print("\n Cycles of length : ", cycle_length ," \n", terminator: "");
	g!.find_cycle(cycle_length);
}
main();

Output

 Adjacency list of vertex  0  :   1  2  5
 Adjacency list of vertex  1  :   0  2  3
 Adjacency list of vertex  2  :   0  1  3  4  5
 Adjacency list of vertex  3  :   1  2  4
 Adjacency list of vertex  4  :   2  3  5
 Adjacency list of vertex  5  :   0  2  4
 Cycles of length :  3

 (  0    1    2    0  )
 (  0    2    1    0  )
 (  0    2    5    0  )
 (  0    5    2    0  )
 (  1    0    2    1  )
 (  1    2    0    1  )
 (  1    2    3    1  )
 (  1    3    2    1  )
 (  2    0    1    2  )
 (  2    0    5    2  )
 (  2    1    0    2  )
 (  2    1    3    2  )
 (  2    3    1    2  )
 (  2    3    4    2  )
 (  2    4    3    2  )
 (  2    4    5    2  )
 (  2    5    0    2  )
 (  2    5    4    2  )
 (  3    1    2    3  )
 (  3    2    1    3  )
 (  3    2    4    3  )
 (  3    4    2    3  )
 (  4    2    3    4  )
 (  4    2    5    4  )
 (  4    3    2    4  )
 (  4    5    2    4  )
 (  5    0    2    5  )
 (  5    2    0    5  )
 (  5    2    4    5  )
 (  5    4    2    5  )


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved