Skip to main content

Find all mother vertex of a directed graph in c#

Csharp program for Find all mother vertex of a directed graph. Here problem description and explanation.

// Include namespace system
using System;
/*
    Csharp program for 
    Find all Mother Vertex in a 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;
				}
			}
		}
	}
	// Check Path of given vertex
	public void dfs(int point, bool[] path)
	{
		if (path[point])
		{
			// When alredy visit
			return;
		}
		else
		{
			// Collect the current node into the path
			path[point] = true;
			var temp = this.node[point].next;
			while (temp != null)
			{
				// Visit to edge node
				this.dfs(temp.id, path);
				// Visit to next edge
				temp = temp.next;
			}
		}
	}
	public void printMotherVertices()
	{
		if (this.size <= 0)
		{
			// No nodes
			return;
		}
		Console.Write("\nMother Vertex :");
		bool[] path = new bool[this.size];
		var n = 0;
		var status = true;
		var count = 0;
		while (n < this.size)
		{
			// Check path of node n
			this.dfs(n, path);
			// When current node are visit other nodes
			// Then that is a Mother Vertex
			for (var index = 0; index < this.size; index++)
			{
				if (path[index] == false)
				{
					// When path not contain current node
					status = false;
				}
				// Reset element value
				path[index] = false;
			}
			if (status)
			{
				// Display result node
				Console.Write("  " + n);
				count++;
			}
			// Reset status
			status = true;
			// Visit next node
			n++;
		}
		if (count == 0)
		{
			Console.WriteLine("Node");
		}
	}
	public static void Main(String[] args)
	{
		// 7 implies the number of nodes in graph
		var g = new Graph(7);
		// Connect node with an edge
		g.addEdge(1, 0);
		g.addEdge(1, 4);
		g.addEdge(2, 5);
		g.addEdge(3, 1);
		g.addEdge(3, 2);
		g.addEdge(3, 6);
		g.addEdge(4, 3);
		g.addEdge(5, 0);
		// Print graph element
		g.printGraph();
		// Test
		g.printMotherVertices();
	}
}

Output

Adjacency list of vertex 0 :
Adjacency list of vertex 1 :  0  4
Adjacency list of vertex 2 :  5
Adjacency list of vertex 3 :  1  2  6
Adjacency list of vertex 4 :  3
Adjacency list of vertex 5 :  0
Adjacency list of vertex 6 :
Mother Vertex :  1  3  4




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