# Show degree of vertex in directed graph

Here given code implementation process.

``````//C Program
//Show degree of vertex in directed graph
#include <stdio.h>
#include <stdlib.h>

struct AjlistNode
{
int id;//Vertices id
struct AjlistNode*next;
};

struct Graph
{
int data; //node key value
struct AjlistNode*next;
};
int size; //number of nodes

//set node key value
void set_node(struct Graph*node)
{
if(node!=NULL && size>0)
{
int index=0;

for(index;index<size;index++)
{
//set vertic node data
node[index].data=index;//set node key
//Initial no AjlistNode
//set NULL Value
node[index].next=NULL;
}
}else
{
printf("Vertic Node is Empty");
}
}

void bind_edge(struct Graph*node, int V ,int E)
{

struct AjlistNode *newEdge=(struct AjlistNode*)malloc(
sizeof(struct AjlistNode)
);
if(newEdge!=NULL)
{

newEdge->next=NULL;
newEdge->id=E;

struct AjlistNode *temp=node[V].next;

if(temp==NULL)
{
node[V].next=newEdge;

}else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newEdge;
}
}else
{
printf("\n Memory overflow");
exit(0);
}
}
//Add Edge from Two given Nodes
void add_edge(struct Graph*node, int V ,int E)
{

if(V<size && E <size)
{

bind_edge(node,V,E);

}else
{
//not valid Vertices
printf("Invalid Node Vertices %d  %d", V,E);
}
}

void print_graph(struct Graph*node)
{
if(node!=NULL)
{
struct AjlistNode *temp=NULL;

for(int index=0;index<size;index++)
{
printf("\n Adjacency list of vertex %d  :",index);
temp=node[index].next;
while(temp!=NULL)
{

//temp->id is graph node vertices
//in this case temp->id is same as
//node[temp->id].data

printf("  %d",node[temp->id].data);
temp=temp->next;
}
}
}else
{
printf("Empty Graph");
}
}

//Find indegree and outdegree of each nodes of a given graph
void find_degree(int indegree[],int outdegree[],struct Graph*node)
{

if(node==NULL) return;

struct AjlistNode *temp=NULL;
int length = 0;
for (int i = 0; i < size; ++i)
{
temp=node[i].next;
length = 0;
while(temp!=NULL)
{
//update indegree
indegree[temp->id]++;
temp=temp->next;
length++;
}
outdegree[i] = length;
}
}

void show_degree(struct Graph*node)
{
if(node!=NULL)
{
int indegree[size],outdegree[size];

for(int i=0;i<size;i++)
{
indegree[i]=0;
outdegree[i]=0;
}

find_degree(indegree,outdegree,node);

for (int i = 0; i < size; ++i)
{
printf("\n Node [%d] indegree [%d] outdegree [%d]",i,indegree[i],outdegree[i]);
}

}else
{
printf("Empty Graph");
}
}

int main(){

size=6;

struct Graph*node=(struct Graph*)malloc(
sizeof(struct Graph)*size
);

if(node==NULL)
{
printf("\n Memory overflow");
}else
{
//First set node keys
set_node(node);

//Connected two node with Edges

print_graph(node);
show_degree(node);
}

return 0;
}```
```

#### 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]``````
``````// C++ program
// Show degree of vertex in directed graph
#include<iostream>

using namespace std;
class AjlistNode {
public:

//Vertices node key
int id;
AjlistNode *next;
AjlistNode(int id) {
//Set value of node key
this->id = id;
this->next = NULL;
}
};
class Vertices {
public:
int data;
AjlistNode *next;
Vertices()
{
this->data = 0;
this->next = NULL;
}
Vertices(int data) {
this->data = data;
this->next = NULL;
}
};
class MyGraph {
public:

//number of Vertices
int size;
Vertices *node;
MyGraph(int size) {
this->size = size;
this->node = new Vertices[size];
//set initial values of graph node
this->set_data();
}
//Set initial node value
void set_data() {
if (this->node == NULL) {
cout << "\nEmpty Graph";
} else {
int index = 0;
while (index < this->size) {
this->node[index] = index;
index++;
}
}
}
//Connect two node
void add_edge(int start, int last) {
if (start >= 0 &&
start < this->size &&
last >= 0 &&
last < this->size) {
AjlistNode *newEdge = new AjlistNode(last);
if (this->node[start].next == NULL) {
//Include first adjacency list node of location start
this->node[start].next = newEdge;
} else {
AjlistNode *temp = this->node[start].next;
//Add new node at the last of edge
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newEdge;
}
} else {
cout << "\nInvalid node location (" << start << "," << last;
}
}
//Display graph elements
void print_graph() {
if (this->size > 0 &&
this->node != NULL) {
int index = 0;
while (index < this->size) {
cout << "\nAdjacency list of vertex " << index << " : ";
AjlistNode *temp = this->node[index].next;
while (temp != NULL) {
cout << this->node[temp->id].data << " ";
temp = temp->next;
}
index++;
}
}
}

//Find indegree and outdegree of each nodes of a given graph
void find_degree(int indegree[], int outdegree[]) {
AjlistNode *temp = NULL;
int length = 0;
for (int 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++;
}
outdegree[i] = length;
}
}
void show_degree() {
if (this->node != NULL) {
int indegree[size];
int outdegree[size];
for (int i = 0; i < this->size; i++) {
indegree[i] = 0;
outdegree[i] = 0;
}
this->find_degree(indegree, outdegree);
for (int i = 0; i < this->size; ++i) {
cout << "\n Node [" << i << "] indegree [" << indegree[i] << "] outdegree [" << outdegree[i] << "]";
}
} else {
cout << "Empty Graph";
}
}
};
int main() {
MyGraph g =  MyGraph(6);
//Connected two node with Edges
g.print_graph();
g.show_degree();
return 0;
}```
```

#### 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]``````
``````// Java program
// Show degree of vertex in directed graph

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;
}
}
class Vertices {

public int data;
public AjlistNode next;

public Vertices(int data) {
this.data = data;
this.next = null;
}
}

public class MyGraph {

//number of Vertices
private int size;

private Vertices[] node;

public MyGraph(int size) {

this.size = size;
this.node = new Vertices[size];
//set initial values of graph node
this.set_data();

}

//Set initial node value
public void set_data() {
if (node == null) {
System.out.println("\nEmpty Graph");
} else {

int index = 0;

while (index < size) {

node[index] = new Vertices(index);

index++;
}
}
}

//Connect two node
public void add_edge(int start, int last) {
if(start>=0 && start < size && last >= 0 && last<size )
{
AjlistNode newEdge = new AjlistNode(last);

if (node[start].next == null)
{
//Include first adjacency list node of location start
node[start].next = newEdge;
}
else
{
AjlistNode temp = node[start].next;
//Add new node at the last of edge
while (temp.next != null)
{
temp = temp.next;
}
temp.next = newEdge;
}
}
else
{
System.out.print("\nInvalid node location ("+start +","+last);
}

}
//Display graph elements
public void print_graph() {

if (size > 0 && node != null) {
int index = 0;
while (index < size) {
System.out.print("\nAdjacency list of vertex " + index + " : ");
AjlistNode temp = node[index].next;
while (temp != null) {
System.out.print(node[temp.id].data + "  ");
temp = temp.next;
}
index++;
}
}
}
//Find indegree and outdegree of each nodes of a given graph
public void find_degree(int []indegree,int []outdegree)
{

AjlistNode temp=null;
int length = 0;
for (int i = 0; i < this.size; ++i)
{
temp=node[i].next;
length = 0;
while(temp!=null)
{
//update indegree
indegree[temp.id]++;
temp=temp.next;
length++;
}
outdegree[i] = length;
}
}

public void show_degree()
{
if(this.node!=null)
{
int []indegree=new int[size];
int []outdegree=new int[size];

for(int i=0;i<size;i++)
{
indegree[i]=0;
outdegree[i]=0;
}

find_degree(indegree,outdegree);

for (int i = 0; i < this.size; ++i)
{
System.out.print("\n Node ["+i+"] indegree ["+indegree[i]+"] outdegree ["+outdegree[i]+"]");
}

}else
{
System.out.print("Empty Graph");
}
}

public static void main(String[] args) {

MyGraph g = new MyGraph(6);
//Connected two node with Edges

g.print_graph();

g.show_degree();
}
}
```
```

#### 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]``````
``````// C# program
// Show degree of vertex in directed graph
using System;
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;
}
}
class Vertices {
public int data;
public AjlistNode next;
public Vertices(int data) {
this.data = data;
this.next = null;
}
}
public class MyGraph {
//number of Vertices
private int size;
private Vertices[] node;
public MyGraph(int size) {
this.size = size;
this.node = new Vertices[size];
this.set_data();
}
//Set initial node value
public void set_data() {
if (node == null) {
Console.WriteLine("\nEmpty Graph");
} else {
int index = 0;
while (index < size) {
node[index] = new Vertices(index);
index++;
}
}
}
//Connect two node
public void add_edge(int start, int last) {
if (start >= 0 &&
start < size &&
last >= 0 &&
last < size) {
AjlistNode newEdge = new AjlistNode(last);
if (node[start].next == null) {
//Include first adjacency list node of location start
node[start].next = newEdge;
} else {
AjlistNode temp = node[start].next;
//Add new node at the last of edge
while (temp.next != null) {
temp = temp.next;
}
temp.next = newEdge;
}
} else {
Console.Write("\nInvalid node location (" + start + "," + last);
}
}
//Display graph elements
public void print_graph() {
if (size > 0 &&
node != null) {
int index = 0;
while (index < size) {
Console.Write("\nAdjacency list of vertex " + index + " : ");
AjlistNode temp = node[index].next;
while (temp != null) {
Console.Write(node[temp.id].data + " ");
temp = temp.next;
}
index++;
}
}
}
//Find indegree and outdegree of each nodes of a given graph
public void find_degree(int[] indegree, int[] outdegree) {
AjlistNode temp = null;
int length = 0;
for (int i = 0; i < this.size; ++i) {
temp = node[i].next;
length = 0;
while (temp != null) {
//update indegree
indegree[temp.id]++;
temp = temp.next;
length++;
}
outdegree[i] =length;
}
}
public void show_degree() {
if (this.node != null) {
int[] indegree = new int[size];
int[] outdegree = new int[size];
for (int i = 0; i < size; i++) {
indegree[i] = 0;
outdegree[i] = 0;
}
find_degree(indegree, outdegree);
for (int i = 0; i < this.size; ++i) {
Console.Write("\n Node [" + i + "] indegree [" + indegree[i] + "] outdegree [" + outdegree[i] + "]");
}
} else {
Console.Write("Empty Graph");
}
}
public static void Main(String[] args) {
MyGraph g = new MyGraph(6);
g.print_graph();
g.show_degree();
}
}```
```

#### 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]``````
``````<?php
// Php program
// Show degree of vertex in directed graph
class AjlistNode {
//Vertices node key

public \$id;
public \$next;

function __construct(\$id) {
//Set value of node key
\$this->id = \$id;
\$this->next = null;
}
}
class Vertices {
public \$data;
public \$next;

function __construct(\$data) {
\$this->data = \$data;
\$this->next = null;
}
}
class MyGraph {
//number of Vertices

private \$size;
private \$node;

function __construct(\$size) {
\$this->size = \$size;
\$this->node = array_fill(0, \$size, 0);
//set initial values of graph node
\$this->set_data();
}
//Set initial node value

public 	function set_data() {
if (\$this->node == null) {
echo("\nEmpty Graph");
} else {
\$index = 0;
while (\$index < \$this->size) {
\$this->node[\$index] = new Vertices(\$index);
\$index++;
}
}
}
//Connect two node

if (\$start >= 0 &&
\$start < \$this->size &&
\$last >= 0 &&
\$last < \$this->size) {
\$newEdge = new AjlistNode(\$last);
if (\$this->node[\$start]->next == null) {
//Include first adjacency list node of location start
\$this->node[\$start]->next = \$newEdge;
} else {
\$temp = \$this->node[\$start]->next;
//Add new node at the last of edge
while (\$temp->next != null) {
\$temp = \$temp->next;
}
\$temp->next = \$newEdge;
}
} else {
echo("\nInvalid node location (". \$start .",". \$last);
}
}
//Display graph elements

public 	function print_graph() {
if (\$this->size > 0 &&
\$this->node != null) {
\$index = 0;
while (\$index < \$this->size) {
echo("\nAdjacency list of vertex ". \$index ." : ");
\$temp = \$this->node[\$index]->next;
while (\$temp != null) {
echo(\$this->node[\$temp->id]->data ." ");
\$temp = \$temp->next;
}
\$index++;
}
}
}
//Find indegree and outdegree of each nodes of a given graph

public 	function find_degree( & \$indegree, & \$outdegree) {
\$temp = null;
\$length = 0;
for (\$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++;
}
\$outdegree[\$i] = \$length;
}
}
public 	function show_degree() {
if (\$this->node != null) {
\$indegree = array_fill(0, \$this->size, 0);
\$outdegree = array_fill(0, \$this->size, 0);
for (\$i = 0; \$i < \$this->size; \$i++) {
\$indegree[\$i] = 0;
\$outdegree[\$i] = 0;
}
\$this->find_degree(\$indegree, \$outdegree);
for (\$i = 0; \$i < \$this->size; ++\$i) {
echo("\n Node [". \$i ."] indegree [". \$indegree[\$i] ."] outdegree [". \$outdegree[\$i] ."]");
}
} else {
echo("Empty Graph");
}
}
}

function main() {
\$g = new MyGraph(6);
//Connected two node with Edges
\$g->print_graph();
\$g->show_degree();

}
main();```
```

#### 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]``````
``````// Node Js program
// Show degree of vertex in directed graph
class AjlistNode {

constructor(id) {
//Set value of node key
this.id = id;
this.next = null;
}
}
class Vertices {
constructor(data) {
this.data = data;
this.next = null;
}
}
class MyGraph {
constructor(size) {
this.size = size;
this.node = Array(size).fill(null);
//set initial values of graph node
this.set_data();
}

//Set initial node value
set_data() {
if (this.node == null) {
process.stdout.write("\nEmpty Graph");
} else {
var index = 0;
while (index < this.size) {
this.node[index] = new Vertices(index);
index++;
}
}
}

//Connect two node
if (start >= 0 &&
start < this.size &&
last >= 0 &&
last < this.size) {
var newEdge = new AjlistNode(last);
if (this.node[start].next == null) {
//Include first adjacency list node of location start
this.node[start].next = newEdge;
} else {
var temp = this.node[start].next;
//Add new node at the last of edge
while (temp.next != null) {
temp = temp.next;
}

temp.next = newEdge;
}
} else {
process.stdout.write("\nInvalid node location (" + start + "," + last);
}
}

//Display graph elements
print_graph() {
if (this.size > 0 &&
this.node != null) {
var index = 0;
while (index < this.size) {
process.stdout.write("\nAdjacency list of vertex " + index + " : ");
var temp = this.node[index].next;
while (temp != null) {
process.stdout.write(this.node[temp.id].data + " ");
temp = temp.next;
}
index++;
}
}
}

//Find indegree and outdegree of each nodes of a given graph
find_degree(indegree, outdegree) {
var 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++;
}
outdegree[i] = length;
}
}
show_degree() {
if (this.node != null) {
var indegree = Array(this.size).fill(0);
var outdegree = Array(this.size).fill(0);
for (var i = 0; i < this.size; i++) {
indegree[i] = 0;
outdegree[i] = 0;
}
this.find_degree(indegree, outdegree);
for (var i = 0; i < this.size; ++i) {
process.stdout.write("\n Node [" + i + "] indegree [" + indegree[i] + "] outdegree [" + outdegree[i] + "]");
}
} else {
process.stdout.write("Empty Graph");
}
}
}

function main(args) {
var g = new MyGraph(6);
//Connected two node with Edges
g.print_graph();
g.show_degree();
}

main();```
```

#### 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]``````
``````#  Python 3 program
#  Show degree of vertex in directed graph
class AjlistNode :

def __init__(self, id) :
# Set value of node key
self.id = id
self.next = None

class Vertices :

def __init__(self, data) :
self.data = data
self.next = None

class MyGraph :

def __init__(self, size) :
self.size = size
self.node = [0] * size
self.set_data()

# Set initial node value
def set_data(self) :
if (self.node == None) :
print("\nEmpty Graph", end = "")
else :
index = 0
while (index < self.size) :
self.node[index] = Vertices(index)
index += 1

# Connect two node
if (start >= 0 and start < self.size and last >= 0 and last < self.size) :
newEdge = AjlistNode(last)
if (self.node[start].next == None) :
# Include first adjacency list node of location start
self.node[start].next = newEdge
else :
temp = self.node[start].next
# Add new node at the last of edge
while (temp.next != None) :
temp = temp.next

temp.next = newEdge

else :
print("\nInvalid node location (", start ,",", last, end = "")

# Display graph elements
def print_graph(self) :
if (self.size > 0 and self.node != None) :
index = 0
while (index < self.size) :
print("\nAdjacency list of vertex ", index ," : ", end = "")
temp = self.node[index].next
while (temp != None) :
print(self.node[temp.id].data ," ", end = "")
temp = temp.next

index += 1

# Find indegree and outdegree of each nodes of a given graph
def find_degree(self, indegree, outdegree) :
temp = None
length = 0
i = 0
while (i < self.size) :
temp = self.node[i].next
length = 0
while (temp != None) :
# update indegree
indegree[temp.id] += 1
temp = temp.next
length += 1

outdegree[i] = length
i += 1

def show_degree(self) :
if (self.node != None) :
indegree = [0] * self.size
outdegree = [0] * self.size
i = 0
while (i < self.size) :
indegree[i] = 0
outdegree[i] = 0
i += 1

self.find_degree(indegree, outdegree)
i = 0
while (i < self.size) :
print("\n Node [", i ,"] indegree [", indegree[i] ,"] outdegree [", outdegree[i] ,"]", end = "")
i += 1

else :
print("Empty Graph", end = "")

def main() :
g = MyGraph(6)
# Connected two node with Edges
g.print_graph()
g.show_degree()

if __name__ == "__main__":
main()```
```

#### 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 ]``````
``````#  Ruby program
#  Show degree of vertex in directed graph
class AjlistNode
# Define the accessor and reader of class AjlistNode
attr_accessor :id, :next

def initialize(id)
# Set value of node key
self.id = id
self.next = nil
end
end
class Vertices
# Define the accessor and reader of class Vertices
attr_accessor :data, :next

def initialize(data)
self.data = data
self.next = nil
end
end
class MyGraph
# Define the accessor and reader of class MyGraph
attr_accessor :size, :node

def initialize(size)
self.size = size
self.node = Array.new(size) {nil}
self.set_data()
end
# Set initial node value
def set_data()
if (@node == nil)
print("\nEmpty Graph")
else
index = 0
while (index < @size)
@node[index] = Vertices.new(index)
index += 1
end
end
end
# Connect two node
if (start >= 0 &&
start < @size &&
last >= 0 &&
last < @size)
newEdge = AjlistNode.new(last)
if (@node[start].next == nil)
# Include first adjacency list node of location start
@node[start].next = newEdge
else
temp = @node[start].next
# Add new node at the last of edge
while (temp.next != nil)
temp = temp.next
end
temp.next = newEdge
end
else
print("\nInvalid node location (", start ,",", last)
end
end
# Display graph elements
def print_graph()
if (@size > 0 &&
@node != nil)
index = 0
while (index < @size)
print("\nAdjacency list of vertex ", index ,"  : ")
temp = @node[index].next
while (temp != nil)
print(@node[temp.id].data ," ")
temp = temp.next
end
index += 1
end
end
end
# Find indegree and outdegree of each nodes of a given graph
def find_degree(indegree, outdegree)
temp = nil
length = 0
i = 0
while (i < self.size)
temp = @node[i].next
length = 0
while (temp != nil)
# update indegree
indegree[temp.id] += 1
temp = temp.next
length += 1
end
outdegree[i] = length
i += 1
end
end
def show_degree()
if (self.node != nil)
indegree = Array.new(@size) {0}
outdegree = Array.new(@size) {0}
i = 0
while (i < @size)
indegree[i] = 0
outdegree[i] = 0
i += 1
end
self.find_degree(indegree, outdegree)
i = 0
while (i < self.size)
print("\n Node [", i ,"] indegree [", indegree[i] ,"] outdegree [", outdegree[i] ,"]")
i += 1
end
else
print("Empty Graph")
end
end
end
def main()
g = MyGraph.new(6)
# Connected two node with Edges
g.print_graph()
g.show_degree()
end
main()```
```

#### 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]``````
``````// Scala program
// Show degree of vertex in directed graph
class AjlistNode(var id: Int,
var next: AjlistNode) {
def this(id: Int) {
//Set value of node
this(id,null);
}
}
class Vertices(var data: Int,
var next: AjlistNode) {

def this(data: Int) {
this(data,null);
}
}
class MyGraph(var size: Int,
var node: Array[Vertices]) {

def this(size: Int) {
//set value
this(size,Array.fill[Vertices](size)(null));
//set initial values of graph node
this.set_data();
}
//Set initial node value
def set_data(): Unit = {
if (this.node == null) {
print("\nEmpty Graph");
} else {
var index: Int = 0;
while (index < this.size) {
this.node(index) = new Vertices(index);
index += 1;
}
}
}
//Connect two node
def add_edge(start: Int, last: Int): Unit = {
if (start >= 0 &&
start < this.size &&
last >= 0 &&
last < this.size) {
var newEdge: AjlistNode = new AjlistNode(last);

if (this.node(start).next == null) {
//Include first adjacency list node of location start
this.node(start).next = newEdge;
} else {
var temp: AjlistNode = this.node(start).next;

//Add new node at the last of edge
while (temp.next != null) {
temp = temp.next;
}
temp.next = newEdge;
}
} else {
print("\nInvalid node location (" + start + "," + last);
}
}
//Display graph elements
def print_graph(): Unit = {
if (this.size > 0 &&
this.node != null) {
var index: Int = 0;
while (index < this.size) {
print("\nAdjacency list of vertex " + index + " : ");
var temp: AjlistNode = this.node(index).next;
while (temp != null) {
print(" "+this.node(temp.id).data );
temp = temp.next;
}
index += 1;
}
}
}
//Find indegree and outdegree of each nodes of a given graph
def find_degree(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;
}
outdegree(i) = length;
i += 1;
}
}
def show_degree(): 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);
var i: Int = 0;
this.find_degree(indegree, outdegree);
i = 0;
while (i < this.size) {
print("\n Node [" + i + "] indegree [" + indegree(i) + "] outdegree [" + outdegree(i) + "]");
i += 1;
}
} else {
print("Empty Graph");
}
}
}
object Main {
def main(args: Array[String]): Unit = {
var g: MyGraph = new MyGraph(6);

//Connected two node with Edges
g.print_graph();
g.show_degree();
}
}```
```

#### 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]``````
``````// Swift program
// Show degree of vertex in directed graph
class AjlistNode {
//Vertices node key
var id: Int;
var next: AjlistNode? ;
init(_ id: Int) {
//Set value of node key
self.id = id;
self.next = nil;
}
}
class Vertices {
var data: Int;
var next: AjlistNode? ;
init(_ data: Int) {
self.data = data;
self.next = nil;
}
}
class MyGraph {
//number of Vertices
var size: Int;
var node: [Vertices]? = [Vertices]() ;
init(_ size: Int) {
self.size = size;

var i = 0;
while (i<size) {
self.node!.append(Vertices(i));
i+=1;
}
}

//Connect two node
func add_edge(_ start: Int, _ last: Int) {
let newEdge: AjlistNode? = AjlistNode(last);
if (self.node![start].next == nil) {
//Include first adjacency list node of location start
self.node![start].next = newEdge;
} else {
var temp: AjlistNode? = self.node![start].next;
//Add new node at the last of edge
while (temp!.next != nil) {
temp = temp!.next;
}
temp!.next = newEdge;
}
}
//Display graph elements
func print_graph() {
if (self.size > 0 &&
self.node != nil) {
var index: Int = 0;
while (index < self.size) {
print("\nAdjacency list of vertex ", index ," : ", terminator: "");
var temp: AjlistNode? = self.node![index].next;
while (temp != nil) {
print(self.node![temp!.id].data ," ", terminator: "");
temp = temp!.next;
}
index += 1;
}
}
}
//Find indegree and outdegree of each nodes of a given graph
func find_degree(_ indegree: inout[Int], _ outdegree: inout[Int]) {
var temp: AjlistNode? = nil;
var length: Int = 0;
var i: Int = 0;
while (i < self.size) {
temp = self.node![i].next;
length = 0;
while (temp != nil) {
//update indegree
indegree[temp!.id] += 1;
temp = temp!.next;
length += 1;
}
outdegree[i] = length;
i += 1;
}
}
func show_degree() {
if (self.node != nil) {
var indegree: [Int] = Array(repeating: 0, count: self.size);
var outdegree: [Int] = Array(repeating: 0, count: self.size);
var i: Int = 0;
self.find_degree(&indegree, &outdegree);

while (i < self.size) {
print("\n Node [", i ,"] indegree [", indegree[i] ,"] outdegree [", outdegree[i] ,"]", terminator: "");
i += 1;
}
} else {
print("Empty Graph", terminator: "");
}
}
}
func main() {
let g: MyGraph? = MyGraph(6);
//Connected two node with Edges
g!.print_graph();
g!.show_degree();
}
main();```
```

#### 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 ]``````

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.