Skip to main content

Print all cycles of length n in a graph using c#

Csharp program for Print all cycles of length n in a graph. Here more information.

// Include namespace system
using System;
/*
    Csharp program for 
    Print all cycle in graph by given length
*/
public class AjlistNode
{
	// Vertices node key
	public int id;
	public AjlistNode next;
	public AjlistNode(int id)
	{
		// Set value of node key
		this.id = id;
		this.next = null;
	}
}
public class Vertices
{
	public int data;
	public AjlistNode next;
	public AjlistNode last;
	public Vertices(int data)
	{
		this.data = data;
		this.next = null;
		this.last = null;
	}
}
public class Graph
{
	// Number of Vertices
	public int size;
	public Vertices[] node;
	public Graph(int size)
	{
		// Set value
		this.size = size;
		this.node = new Vertices[size];
		this.setData();
	}
	// Set initial node value
	public void setData()
	{
		if (this.size <= 0)
		{
			Console.WriteLine("\nEmpty Graph");
		}
		else
		{
			for (var index = 0; index < this.size; index++)
			{
				// Set initial node value
				this.node[index] = new Vertices(index);
			}
		}
	}
	public void connection(int start, int last)
	{
		// Safe connection
		var edge = new AjlistNode(last);
		if (this.node[start].next == null)
		{
			this.node[start].next = edge;
		}
		else
		{
			// Add edge at the end
			this.node[start].last.next = edge;
		}
		// Get last edge 
		this.node[start].last = edge;
	}
	//  Handling the request of adding new edge
	public void addEdge(int start, int last)
	{
		if (start >= 0 && start < this.size && 
            last >= 0 && last < this.size)
		{
			this.connection(start, last);
			this.connection(last, start);
		}
		else
		{
			// When invalid nodes
			Console.WriteLine("\nHere Something Wrong");
		}
	}
	public void printGraph()
	{
		if (this.size > 0)
		{
			// Print graph ajlist Node value
			for (var index = 0; index < this.size; ++index)
			{
				Console.Write("\nAdjacency list of vertex " + 
                              index + " :");
				var edge = this.node[index].next;
				while (edge != null)
				{
					// Display graph node 
					Console.Write("  " + this.node[edge.id].data);
					// Visit to next edge
					edge = edge.next;
				}
			}
		}
	}
	public void printCycleByLength(bool[] visit, 
                                   int start, int source, 
                                   int length, int n, String result)
	{
		if (start >= this.size || source >= this.size || 
            start < 0 || source < 0 || this.size <= 0)
		{
			return;
		}
		if (visit[start] == true)
		{
			// Here length are indicate number of edge
			if (start == source && length == n)
			{
				// Display cycle of length n
				Console.WriteLine("(" + result + ")");
			}
			return;
		}
		// Here modified the value of visited node
		visit[start] = true;
		// This is used to iterate nodes edges
		var edge = this.node[start].next;
		while (edge != null)
		{
			this.printCycleByLength(visit, edge.id, 
                                    source, length + 1, n, 
                                  result + "  " + edge.id);
			// Visit to next edge
			edge = edge.next;
		}
		// Reset the value of visited node status
		visit[start] = false;
	}
	public void cycleByLength(int n)
	{
		if (n < 2 || n >= this.size)
		{
			return;
		}
		if (this.size <= 0)
		{
			// Empty graph
			return;
		}
		// Auxiliary space which is used to store 
		// information about visited node
		bool[] visit = new bool[this.size];
		Console.WriteLine("\nResult : ");
		for (var i = 0; i < this.size; ++i)
		{
			// Check cycle of node i to i with length l
			this.printCycleByLength(visit, i, i, 0, n, "" + i.ToString());
		}
	}
	public static void Main(String[] args)
	{
		// 6 implies the number of nodes in graph
		var g = new Graph(6);
		// Connect node with an edge
		// First and second parameter indicate node
		g.addEdge(0, 1);
		g.addEdge(0, 2);
		g.addEdge(0, 5);
		g.addEdge(1, 2);
		g.addEdge(1, 3);
		g.addEdge(2, 3);
		g.addEdge(2, 4);
		g.addEdge(2, 5);
		g.addEdge(3, 4);
		g.addEdge(4, 5);
		// Print graph element
		g.printGraph();
		// Test
		g.cycleByLength(3);
	}
}

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
Result :
(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)




Comment

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