Skip to main content

Find indegree and outdegree of a directed graph in scala

Scala program for Find indegree and outdegree of a directed graph. Here problem description and other solutions.

/*
    Scala Program for 
    Show degree of vertex in directed graph
*/
class AjlistNode(
	// Vertices node key
	var id: Int,
		var next: AjlistNode)
{
	def this(id: Int)
	{
		// Set value of node key
		this(id, null);
	}
}
class Vertices(var data: Int,
	var next: AjlistNode,
		var last: AjlistNode)
{
	def this(data: Int)
	{
		this(data, null, null);
	}
}
class Graph(
	// Number of Vertices
	var size: Int,
		var node: Array[Vertices])
{
	def this(size: Int)
	{
		this(size, Array.fill[Vertices](size)(null));
      	// Set value
		this.setData();
	}
	// Set initial node value
	def setData(): Unit = {
		if (size <= 0)
		{
			println("\nEmpty Graph");
		}
		else
		{
			var index: Int = 0;
			while (index < size)
			{
				// Set initial node value
				node(index) = new Vertices(index);
				index += 1;
			}
		}
	}
	//  Handling the request of adding new edge
	def addEdge(start: Int, last: Int): Unit = {
		if (start >= 0 && start < size && 
             last >= 0 && last < size)
		{
			// Safe connection
			var edge: AjlistNode = new AjlistNode(last);
			if (node(start).next == null)
			{
				node(start).next = edge;
			}
			else
			{
				// Add edge at the end
				node(start).last.next = edge;
			}
			// Get last edge 
			node(start).last = edge;
		}
		else
		{
			// When invalid nodes
			println("\nHere Something Wrong");
		}
	}
	def printGraph(): Unit = {
		if (size > 0)
		{
			var index: Int = 0;
			// Print graph ajlist Node value
			while (index < size)
			{
				print("\nAdjacency list of vertex " + index + " : ");
				var temp: AjlistNode = node(index).next;
				while (temp != null)
				{
					// Display graph node 
					print(" " + node(temp.id).data);
					// Visit to next edge
					temp = temp.next;
				}
				index += 1;
			}
		}
	}
	// Find indegree and outdegree of 
	// each nodes of a given graph
	def findDegree(indegree: Array[Int], 
      outdegree: Array[Int]): Unit = {
		var temp: AjlistNode = null;
		var length: Int = 0;
		var i: Int = 0;
		while (i < this.size)
		{
			temp = this.node(i).next;
			length = 0;
			while (temp != null)
			{
				// Update indegree
				indegree(temp.id) += 1;
				temp = temp.next;
				length += 1;
			}
			// Set outdegree value
			outdegree(i) = length;
			i += 1;
		}
	}
	def showDegree(): Unit = {
		if (this.node != null)
		{
			var indegree: Array[Int] = Array.fill[Int](this.size)(0);
			var outdegree: Array[Int] = Array.fill[Int](this.size)(0);
			// Find degree
			this.findDegree(indegree, outdegree);
			println("\n");
			var i: Int = 0;
			// Display result
			while (i < this.size)
			{
				println("Node [" + i + 
                        "] indegree [" + 
                        indegree(i) + "] outdegree [" + 
                        outdegree(i) + "]");
				i += 1;
			}
		}
		else
		{
			print("Empty Graph");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		// 6 implies the number of nodes in graph
		var g: Graph = 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