Skip to main content

Find indegree and outdegree of a directed graph in c#

Csharp program for Find indegree and outdegree of a directed graph. Here problem description and explanation.

// Include namespace system
using System;
/*
    Csharp Program for 
    Show degree of vertex in directed graph
*/
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);
			}
		}
	}
	//  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)
		{
			// 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;
		}
		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 temp = this.node[index].next;
				while (temp != null)
				{
					// Display graph node 
					Console.Write(" " + this.node[temp.id].data);
					// Visit to next edge
					temp = temp.next;
				}
			}
		}
	}
	// Find indegree and outdegree of 
	// each nodes of a given graph
	public void findDegree(int[] indegree, int[] outdegree)
	{
		AjlistNode temp = null;
		var length = 0;
		for (var i = 0; i < this.size; ++i)
		{
			temp = this.node[i].next;
			length = 0;
			while (temp != null)
			{
				// Update indegree
				indegree[temp.id]++;
				temp = temp.next;
				length++;
			}
			// Set outdegree value
			outdegree[i] = length;
		}
	}
	public void showDegree()
	{
		if (this.node != null)
		{
			int[] indegree = new int[this.size];
			int[] outdegree = new int[this.size];
			// Set zero degree
			for (var i = 0; i < this.size; i++)
			{
				indegree[i] = 0;
				outdegree[i] = 0;
			}
			// Find degree
			this.findDegree(indegree, outdegree);
			Console.WriteLine("\n");
			// Display result
			for (var i = 0; i < this.size; ++i)
			{
				Console.WriteLine("Node [" + i + "] indegree [" +
                                  indegree[i] + 
                                  "] outdegree [" +
                                  outdegree[i] + "]");
			}
		}
		else
		{
			Console.Write("Empty Graph");
		}
	}
	public static void Main(String[] args)
	{
		// 6 implies the number of nodes in graph
		var g = new Graph(6);
		// Connected two node with Edges
		g.addEdge(0, 1);
		g.addEdge(0, 2);
		g.addEdge(0, 5);
		g.addEdge(2, 3);
		g.addEdge(3, 1);
		g.addEdge(4, 0);
		g.addEdge(4, 1);
		g.addEdge(5, 0);
		g.addEdge(5, 2);
		g.printGraph();
		// Test
		g.showDegree();
	}
}

Output

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

Node [0] indegree [2] outdegree [3]
Node [1] indegree [3] outdegree [0]
Node [2] indegree [2] outdegree [1]
Node [3] indegree [1] outdegree [1]
Node [4] indegree [0] outdegree [2]
Node [5] indegree [1] outdegree [2]




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