Efficiently implementing adjacency list in directed graph

Here given code implementation process.

Adjacency list representation of directed graph
// Include header file
#include <iostream>
#include <vector>

using namespace std;
/*
    C++ Program
    Efficiently implementing adjacency list in directed graph
*/
class Graph
{
	public: int vertices;
	vector < vector < int > > adgeList;
	Graph(int vertices)
	{
		this->vertices = vertices;
		for (int i = 0; i < this->vertices; ++i)
		{
			this->adgeList.push_back( vector < int > ());
		}
	}
	void addEdge(int u, int v)
	{
		if (u < 0 || u >= this->vertices || v < 0 || v >= this->vertices)
		{
			return;
		}
		// add node edge
		this->adgeList.at(u).push_back(v);
	}
	// Display graph nodes and edges
	void printGraph()
	{
		cout << "\n Graph Adjacency List ";
		for (int i = 0; i < this->vertices; ++i)
		{
			cout << " \n [" << i << "] :";
			// iterate edges of i node
			for (int j = 0; j < this->adgeList.at(i).size(); j++)
			{
				cout << "  " << this->adgeList.at(i).at(j);
			}
		}
	}
};
int main()
{
	Graph *g = new Graph(10);
	g->addEdge(0, 1);
	g->addEdge(0, 2);
	g->addEdge(0, 7);
	g->addEdge(1, 5);
	g->addEdge(1, 7);
	g->addEdge(2, 3);
	g->addEdge(2, 7);
	g->addEdge(3, 4);
	g->addEdge(3, 8);
	g->addEdge(4, 9);
	g->addEdge(5, 6);
	g->addEdge(5, 8);
	g->addEdge(6, 8);
	g->addEdge(6, 9);
	g->addEdge(7, 8);
	// Display graph element
	g->printGraph();
	return 0;
}

input

 Graph Adjacency List
 [0] :  1  2  7
 [1] :  5  7
 [2] :  3  7
 [3] :  4  8
 [4] :  9
 [5] :  6  8
 [6] :  8  9
 [7] :  8
 [8] :
 [9] :
import java.util.ArrayList;
/*
    Java Program
    Efficiently implementing adjacency list in directed graph
*/
public class Graph
{
	public int vertices;
	public ArrayList < ArrayList < Integer >> adgeList;
	public Graph(int vertices)
	{
		this.vertices = vertices;
		this.adgeList = new ArrayList < ArrayList < Integer >> (vertices);
		for (int i = 0; i < this.vertices; ++i)
		{
			this.adgeList.add(new ArrayList < Integer > ());
		}
	}
	public void addEdge(int u, int v)
	{
		if (u < 0 || u >= this.vertices || v < 0 || v >= this.vertices)
		{
			return;
		}
		// add node edge
		adgeList.get(u).add(v);
	}
	// Display graph nodes and edges
	public void printGraph()
	{
		System.out.print("\n Graph Adjacency List ");
		for (int i = 0; i < this.vertices; ++i)
		{
			System.out.print(" \n [" + i + "] :");
			// iterate edges of i node
			for (int j = 0; j < this.adgeList.get(i).size(); j++)
			{
				System.out.print("  " + this.adgeList.get(i).get(j));
			}
		}
	}
	public static void main(String[] args)
	{
		Graph g = new Graph(10);
		g.addEdge(0, 1);
		g.addEdge(0, 2);
		g.addEdge(0, 7);
		g.addEdge(1, 5);
		g.addEdge(1, 7);
		g.addEdge(2, 3);
		g.addEdge(2, 7);
		g.addEdge(3, 4);
		g.addEdge(3, 8);
		g.addEdge(4, 9);
		g.addEdge(5, 6);
		g.addEdge(5, 8);
		g.addEdge(6, 8);
		g.addEdge(6, 9);
		g.addEdge(7, 8);
		// Display graph element
		g.printGraph();
	}
}

input

 Graph Adjacency List
 [0] :  1  2  7
 [1] :  5  7
 [2] :  3  7
 [3] :  4  8
 [4] :  9
 [5] :  6  8
 [6] :  8  9
 [7] :  8
 [8] :
 [9] :
// Include namespace system
using System;
using System.Collections.Generic;
/*
    Csharp Program
    Efficiently implementing adjacency list in directed graph
*/
public class Graph
{
	public int vertices;
	public List < List < int >> adgeList;
	public Graph(int vertices)
	{
		this.vertices = vertices;
		this.adgeList = new List < List < int >> (vertices);
		for (int i = 0; i < this.vertices; ++i)
		{
			this.adgeList.Add(new List < int > ());
		}
	}
	public void addEdge(int u, int v)
	{
		if (u < 0 || u >= this.vertices || v < 0 || v >= this.vertices)
		{
			return;
		}
		// add node edge
		this.adgeList[u].Add(v);
	}
	// Display graph nodes and edges
	public void printGraph()
	{
		Console.Write("\n Graph Adjacency List ");
		for (int i = 0; i < this.vertices; ++i)
		{
			Console.Write(" \n [" + i + "] :");
			// iterate edges of i node
			for (int j = 0; j < this.adgeList[i].Count; j++)
			{
				Console.Write("  " + this.adgeList[i][j]);
			}
		}
	}
	public static void Main(String[] args)
	{
		Graph g = new Graph(10);
		g.addEdge(0, 1);
		g.addEdge(0, 2);
		g.addEdge(0, 7);
		g.addEdge(1, 5);
		g.addEdge(1, 7);
		g.addEdge(2, 3);
		g.addEdge(2, 7);
		g.addEdge(3, 4);
		g.addEdge(3, 8);
		g.addEdge(4, 9);
		g.addEdge(5, 6);
		g.addEdge(5, 8);
		g.addEdge(6, 8);
		g.addEdge(6, 9);
		g.addEdge(7, 8);
		// Display graph element
		g.printGraph();
	}
}

input

 Graph Adjacency List
 [0] :  1  2  7
 [1] :  5  7
 [2] :  3  7
 [3] :  4  8
 [4] :  9
 [5] :  6  8
 [6] :  8  9
 [7] :  8
 [8] :
 [9] :
package main
import "fmt"
/*
    Go Program
    Efficiently implementing adjacency list in directed graph
*/
type Graph struct {
	vertices int
	adgeList [][] int
}
func getGraph(vertices int) * Graph {
	var me *Graph = &Graph {}
	me.vertices = vertices
	me.adgeList = make([][]int,vertices)
	for i := 0 ; i < me.vertices ; i++ {
		me.adgeList = append(me.adgeList, )
	}
	return me
}
func(this Graph) addEdge(u, v int) {
	if u < 0 || u >= this.vertices || v < 0 || v >= this.vertices {
		return
	}
	// add node edge
	this.adgeList[u] = append(this.adgeList[u], v)
}
// Display graph nodes and edges
func(this Graph) printGraph() {
	fmt.Print("\n Graph Adjacency List ")
	for i := 0 ; i < this.vertices ; i++ {
		fmt.Print(" \n [", i, "] :")
		// iterate edges of i node
		for j := 0 ; j < len(this.adgeList[i]) ; j++ {
			fmt.Print("  ", this.adgeList[i][j])
		}
	}
}
func main() {
	var g * Graph = getGraph(10)
	g.addEdge(0, 1)
	g.addEdge(0, 2)
	g.addEdge(0, 7)
	g.addEdge(1, 5)
	g.addEdge(1, 7)
	g.addEdge(2, 3)
	g.addEdge(2, 7)
	g.addEdge(3, 4)
	g.addEdge(3, 8)
	g.addEdge(4, 9)
	g.addEdge(5, 6)
	g.addEdge(5, 8)
	g.addEdge(6, 8)
	g.addEdge(6, 9)
	g.addEdge(7, 8)
	// Display graph element
	g.printGraph()
}

input

 Graph Adjacency List  
 [0] :  1  2  7 
 [1] :  5  7 
 [2] :  3  7 
 [3] :  4  8 
 [4] :  9 
 [5] :  6  8 
 [6] :  8  9 
 [7] :  8 
 [8] : 
 [9] :
<?php
/*
    Php Program
    Efficiently implementing adjacency list in directed graph
*/
class Graph
{
	public $vertices;
	public $adgeList;
	public	function __construct($vertices)
	{
		$this->vertices = $vertices;
		$this->adgeList = array();
		for ($i = 0; $i < $this->vertices; ++$i)
		{
			$this->adgeList[] = array();
		}
	}
	public	function addEdge($u, $v)
	{
		if ($u < 0 || $u >= $this->vertices || $v < 0 || $v >= $this->vertices)
		{
			return;
		}
		// add node edge
		$this->adgeList[$u][] = $v;
	}
	// Display graph nodes and edges
	public	function printGraph()
	{
		echo("\n Graph Adjacency List ");
		for ($i = 0; $i < $this->vertices; ++$i)
		{
			echo(" \n [".$i.
				"] :");
			// iterate edges of i node
			for ($j = 0; $j < count($this->adgeList[$i]); $j++)
			{
				echo("  ".$this->adgeList[$i][$j]);
			}
		}
	}
}

function main()
{
	$g = new Graph(10);
	$g->addEdge(0, 1);
	$g->addEdge(0, 2);
	$g->addEdge(0, 7);
	$g->addEdge(1, 5);
	$g->addEdge(1, 7);
	$g->addEdge(2, 3);
	$g->addEdge(2, 7);
	$g->addEdge(3, 4);
	$g->addEdge(3, 8);
	$g->addEdge(4, 9);
	$g->addEdge(5, 6);
	$g->addEdge(5, 8);
	$g->addEdge(6, 8);
	$g->addEdge(6, 9);
	$g->addEdge(7, 8);
	// Display graph element
	$g->printGraph();
}
main();

input

 Graph Adjacency List
 [0] :  1  2  7
 [1] :  5  7
 [2] :  3  7
 [3] :  4  8
 [4] :  9
 [5] :  6  8
 [6] :  8  9
 [7] :  8
 [8] :
 [9] :
/*
    Node JS Program
    Efficiently implementing adjacency list in directed graph
*/
class Graph
{
	constructor(vertices)
	{
		this.vertices = vertices;
		this.adgeList = [];
		for (var i = 0; i < this.vertices; ++i)
		{
			this.adgeList.push([]);
		}
	}
	addEdge(u, v)
	{
		if (u < 0 || u >= this.vertices || v < 0 || v >= this.vertices)
		{
			return;
		}
		// add node edge
		this.adgeList[u].push(v);
	}
	// Display graph nodes and edges
	printGraph()
	{
		process.stdout.write("\n Graph Adjacency List ");
		for (var i = 0; i < this.vertices; ++i)
		{
			process.stdout.write(" \n [" + i + "] :");
			// iterate edges of i node
			for (var j = 0; j < this.adgeList[i].length; j++)
			{
				process.stdout.write("  " + this.adgeList[i][j]);
			}
		}
	}
}

function main()
{
	var g = new Graph(10);
	g.addEdge(0, 1);
	g.addEdge(0, 2);
	g.addEdge(0, 7);
	g.addEdge(1, 5);
	g.addEdge(1, 7);
	g.addEdge(2, 3);
	g.addEdge(2, 7);
	g.addEdge(3, 4);
	g.addEdge(3, 8);
	g.addEdge(4, 9);
	g.addEdge(5, 6);
	g.addEdge(5, 8);
	g.addEdge(6, 8);
	g.addEdge(6, 9);
	g.addEdge(7, 8);
	// Display graph element
	g.printGraph();
}
main();

input

 Graph Adjacency List
 [0] :  1  2  7
 [1] :  5  7
 [2] :  3  7
 [3] :  4  8
 [4] :  9
 [5] :  6  8
 [6] :  8  9
 [7] :  8
 [8] :
 [9] :
#    Python 3 Program
#    Efficiently implementing adjacency list in directed graph
class Graph :
	def __init__(self, vertices) :
		self.vertices = vertices
		self.adgeList = []
		i = 0
		while (i < self.vertices) :
			self.adgeList.append([])
			i += 1
		
	
	def addEdge(self, u, v) :
		if (u < 0 or u >= self.vertices or v < 0 or v >= self.vertices) :
			return
		
		#  add node edge
		self.adgeList[u].append(v)
	
	#  Display graph nodes and edges
	def printGraph(self) :
		print("\n Graph Adjacency List ", end = "")
		i = 0
		while (i < self.vertices) :
			print(" \n [", i ,"] :", end = "")
			j = 0
			#  iterate edges of i node
			while (j < len(self.adgeList[i])) :
				print("  ", self.adgeList[i][j], end = "")
				j += 1
			
			i += 1
		
	

def main() :
	g = Graph(10)
	g.addEdge(0, 1)
	g.addEdge(0, 2)
	g.addEdge(0, 7)
	g.addEdge(1, 5)
	g.addEdge(1, 7)
	g.addEdge(2, 3)
	g.addEdge(2, 7)
	g.addEdge(3, 4)
	g.addEdge(3, 8)
	g.addEdge(4, 9)
	g.addEdge(5, 6)
	g.addEdge(5, 8)
	g.addEdge(6, 8)
	g.addEdge(6, 9)
	g.addEdge(7, 8)
	#  Display graph element
	g.printGraph()

if __name__ == "__main__": main()

input

 Graph Adjacency List
 [ 0 ] :   1   2   7
 [ 1 ] :   5   7
 [ 2 ] :   3   7
 [ 3 ] :   4   8
 [ 4 ] :   9
 [ 5 ] :   6   8
 [ 6 ] :   8   9
 [ 7 ] :   8
 [ 8 ] :
 [ 9 ] :
#    Ruby Program
#    Efficiently implementing adjacency list in directed graph
class Graph 
	# Define the accessor and reader of class Graph
	attr_reader :vertices, :adgeList
	attr_accessor :vertices, :adgeList
	def initialize(vertices) 
		self.vertices = vertices
		self.adgeList = []
		i = 0
		while (i < self.vertices) 
			self.adgeList.push([])
			i += 1
		end

	end

	def addEdge(u, v) 
		if (u < 0 || u >= self.vertices || v < 0 || v >= self.vertices) 
			return
		end

		#  add node edge
		self.adgeList[u].push(v)
	end

	#  Display graph nodes and edges
	def printGraph() 
		print("\n Graph Adjacency List ")
		i = 0
		while (i < self.vertices) 
			print(" \n [", i ,"] :")
			j = 0
			#  iterate edges of i node
			while (j < self.adgeList[i].length) 
				print("  ", self.adgeList[i][j])
				j += 1
			end

			i += 1
		end

	end

end

def main() 
	g = Graph.new(10)
	g.addEdge(0, 1)
	g.addEdge(0, 2)
	g.addEdge(0, 7)
	g.addEdge(1, 5)
	g.addEdge(1, 7)
	g.addEdge(2, 3)
	g.addEdge(2, 7)
	g.addEdge(3, 4)
	g.addEdge(3, 8)
	g.addEdge(4, 9)
	g.addEdge(5, 6)
	g.addEdge(5, 8)
	g.addEdge(6, 8)
	g.addEdge(6, 9)
	g.addEdge(7, 8)
	#  Display graph element
	g.printGraph()
end

main()

input

 Graph Adjacency List  
 [0] :  1  2  7 
 [1] :  5  7 
 [2] :  3  7 
 [3] :  4  8 
 [4] :  9 
 [5] :  6  8 
 [6] :  8  9 
 [7] :  8 
 [8] : 
 [9] :
import scala.collection.mutable._;
/*
    Scala Program
    Efficiently implementing adjacency list in directed graph
*/
class Graph(var vertices: Int,
	var adgeList: ArrayBuffer[ArrayBuffer[Int]])
{
	def this(vertices: Int)
	{
		this(vertices,new ArrayBuffer[ArrayBuffer[Int]](vertices));
		var i: Int = 0;
		while (i < this.vertices)
		{
			this.adgeList += new ArrayBuffer[Int]();
			i += 1;
		}
	}
	def addEdge(u: Int, v: Int): Unit = {
		if (u < 0 || u >= this.vertices || v < 0 || v >= this.vertices)
		{
			return;
		}
		// add node edge
		adgeList(u) += v;
	}
	// Display graph nodes and edges
	def printGraph(): Unit = {
		print("\n Graph Adjacency List ");
		var i: Int = 0;
		while (i < this.vertices)
		{
			print(" \n [" + i + "] :");
			var j: Int = 0;
			// iterate edges of i node
			while (j < this.adgeList(i).size)
			{
				print("  " + this.adgeList(i)(j));
				j += 1;
			}
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var g: Graph = new Graph(10);
		g.addEdge(0, 1);
		g.addEdge(0, 2);
		g.addEdge(0, 7);
		g.addEdge(1, 5);
		g.addEdge(1, 7);
		g.addEdge(2, 3);
		g.addEdge(2, 7);
		g.addEdge(3, 4);
		g.addEdge(3, 8);
		g.addEdge(4, 9);
		g.addEdge(5, 6);
		g.addEdge(5, 8);
		g.addEdge(6, 8);
		g.addEdge(6, 9);
		g.addEdge(7, 8);
		// Display graph element
		g.printGraph();
	}
}

input

 Graph Adjacency List
 [0] :  1  2  7
 [1] :  5  7
 [2] :  3  7
 [3] :  4  8
 [4] :  9
 [5] :  6  8
 [6] :  8  9
 [7] :  8
 [8] :
 [9] :
import Foundation;
/*
    Swift 4 Program
    Efficiently implementing adjacency list in directed graph
*/
class Graph
{
	var vertices: Int;
	var adgeList: [[Int]];
	init(_ vertices: Int)
	{
		self.vertices = vertices;
		self.adgeList = [[Int]]();
		var i = 0;
		while (i < self.vertices)
		{
			self.adgeList.append([Int]());
			i += 1;
		}
	}
	func addEdge(_ u: Int, _ v: Int)
	{
		if (u < 0 || u >= self.vertices || v < 0 || v >= self.vertices)
		{
			return;
		}
		// add node edge
		self.adgeList[u].append(v);
	}
	// Display graph nodes and edges
	func printGraph()
	{
		print("\n Graph Adjacency List ", terminator: "");
		var i = 0;
		while (i < self.vertices)
		{
			print(" \n [", i ,"] :", terminator: "");
			var j = 0;
			// iterate edges of i node
			while (j < self.adgeList[i].count)
			{
				print("  ", self.adgeList[i][j], terminator: "");
				j += 1;
			}
			i += 1;
		}
	}
}
func main()
{
	let g = Graph(10);
	g.addEdge(0, 1);
	g.addEdge(0, 2);
	g.addEdge(0, 7);
	g.addEdge(1, 5);
	g.addEdge(1, 7);
	g.addEdge(2, 3);
	g.addEdge(2, 7);
	g.addEdge(3, 4);
	g.addEdge(3, 8);
	g.addEdge(4, 9);
	g.addEdge(5, 6);
	g.addEdge(5, 8);
	g.addEdge(6, 8);
	g.addEdge(6, 9);
	g.addEdge(7, 8);
	// Display graph element
	g.printGraph();
}
main();

input

 Graph Adjacency List
 [ 0 ] :   1   2   7
 [ 1 ] :   5   7
 [ 2 ] :   3   7
 [ 3 ] :   4   8
 [ 4 ] :   9
 [ 5 ] :   6   8
 [ 6 ] :   8   9
 [ 7 ] :   8
 [ 8 ] :
 [ 9 ] :
/*
    Kotlin Program
    Efficiently implementing adjacency list in directed graph
*/
class Graph
{
	var vertices: Int;
	var adgeList: MutableList < MutableList < Int >>  ;
	constructor(vertices: Int)
	{
		this.vertices = vertices;
		this.adgeList = mutableListOf<MutableList<Int>>();
		var i: Int = 0;
		while (i < this.vertices)
		{
			this.adgeList.add(mutableListOf<Int>());
			i += 1;
		}
	}
	fun addEdge(u: Int, v: Int): Unit
	{
		if (u < 0 || u >= this.vertices || v < 0 || v >= this.vertices)
		{
			return;
		}
		// add node edge
		this.adgeList[u].add(v);
	}
	// Display graph nodes and edges
	fun printGraph(): Unit
	{
		print("\n Graph Adjacency List ");
		var i: Int = 0;
		while (i < this.vertices)
		{
			print(" \n [" + i + "] :");
			var j: Int = 0;
			// iterate edges of i node
			while (j < this.adgeList[i].size)
			{
				print("  " + this.adgeList[i][j]);
				j += 1;
			}
			i += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val g: Graph = Graph(10);
	g.addEdge(0, 1);
	g.addEdge(0, 2);
	g.addEdge(0, 7);
	g.addEdge(1, 5);
	g.addEdge(1, 7);
	g.addEdge(2, 3);
	g.addEdge(2, 7);
	g.addEdge(3, 4);
	g.addEdge(3, 8);
	g.addEdge(4, 9);
	g.addEdge(5, 6);
	g.addEdge(5, 8);
	g.addEdge(6, 8);
	g.addEdge(6, 9);
	g.addEdge(7, 8);
	// Display graph element
	g.printGraph();
}

input

 Graph Adjacency List
 [0] :  1  2  7
 [1] :  5  7
 [2] :  3  7
 [3] :  4  8
 [4] :  9
 [5] :  6  8
 [6] :  8  9
 [7] :  8
 [8] :
 [9] :


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







© 2021, kalkicode.com, All rights reserved