Posted on by Kalkicode
Code Graph

DFS traversal of directed graph in kotlin

Kotlin program for DFS traversal of directed graph. Here problem description and explanation.

/*
    Kotlin program for 
    DFS traversal of directed graph using recursion
*/
class AjlistNode
{
	// Vertices node key
	var id: Int;
	var next: AjlistNode ? ;
	constructor(id: Int)
	{
		// Set value of node key
		this.id = id;
		this.next = null;
	}
}
class Vertices
{
	var data: Int;
	var next: AjlistNode ? ;
	var last: AjlistNode ? ;
	constructor(data: Int)
	{
		this.data = data;
		this.next = null;
		this.last = null;
	}
}
class Graph
{
	// Number of Vertices
	var size: Int;
	var node: Array < Vertices?> ;
	constructor(size: Int)
	{
		// Set value
		this.size = size;
		this.node = Array(size)
		{
			null
		};
		this.setData();
	}
	// Set initial node value
	fun setData(): Unit
	{
		if (this.size <= 0)
		{
			println("\nEmpty Graph");
		}
		else
		{
			var index: Int = 0;
			while (index < this.size)
			{
				// Set initial node value
				this.node[index] = Vertices(index);
				index += 1;
			}
		}
	}
	// Connect two nodes
	fun connect(start: Int, last: Int): Unit
	{
		val edge: AjlistNode = 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
	fun addEdge(start: Int, last: Int): Unit
	{
		if (start >= 0 && start < this.size && 
            last >= 0 && last < this.size)
		{
			// Safe connection
			this.connect(start, last);
		}
		else
		{
			// When invalid nodes
			println("\nHere Something Wrong");
		}
	}
	fun printGraph(): Unit
	{
		if (this.size > 0)
		{
			var index: Int = 0;
			// Print graph ajlist Node value
			while (index < this.size)
			{
				print("\nAdjacency list of vertex " + index + " :");
				var temp: AjlistNode ? = this.node[index]?.next;
				while (temp != null)
				{
					// Display graph node 
					print("  " + this.node[temp.id]?.data);
					// visit to next edge
					temp = temp.next;
				}
				index += 1;
			}
		}
	}
	// Dfs traversal of given node point
	fun dfs(visit: Array < Boolean > , point: Int): Unit
	{
		if (visit[point])
		{
			// We already visited this node
			return;
		}
		// Set active node visit
		visit[point] = true;
		// Display visiting node point
		print("  " + point);
		// Get node edge  
		var temp: AjlistNode ? = this.node[point]?.next;
		// Iterate all edges in given node point
		while (temp != null)
		{
			this.dfs(visit, temp.id);
			// next edge
			temp = temp.next;
		}
	}
	// Handles the request of printing dfs sequence
	fun printDFS(point: Int): Unit
	{
		if (this.size <= 0 || point < 0 || point >= this.size)
		{
			return;
		}
		// This is node visitor
		val visit: Array < Boolean > = Array(this.size)
		{
			false
		};
		var i: Int = 0;
		// There is no initial node visiting
		while (i < this.size)
		{
			visit[i] = false;
			i += 1;
		}
		// Start traversal
		this.dfs(visit, point);
	}
}
fun main(args: Array < String > ): Unit
{
	// 6 implies the number of nodes in graph
	val g: Graph = Graph(6);
	g.addEdge(0, 1);
	g.addEdge(0, 5);
	g.addEdge(1, 1);
	g.addEdge(2, 1);
	g.addEdge(3, 0);
	g.addEdge(3, 3);
	g.addEdge(4, 2);
	g.addEdge(4, 3);
	g.addEdge(5, 1);
	g.printGraph();
	print("\nDFS :");
	g.printDFS(4);
}

Output

Adjacency list of vertex 0 :  1  5
Adjacency list of vertex 1 :  1
Adjacency list of vertex 2 :  1
Adjacency list of vertex 3 :  0  3
Adjacency list of vertex 4 :  2  3
Adjacency list of vertex 5 :  1
DFS :  4  2  1  3  0  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