# Find Sum of dependencies in a graph

Here given code implementation process.

``````//C Program
//Find Sum of dependencies in a graph
#include<stdio.h>

#include<stdlib.h>

struct AjlistNode {
int vId; //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_data(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 connect_edge(struct Graph *node, int V, int E) {

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

newEdge->next = NULL;
newEdge->vId = 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) {

connect_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->vId is graph node vertices
//in this case temp->vId is same as
//node[temp->vId].data

printf("  %d", node[temp->vId].data);
temp = temp->next;
}
}
} else {
printf("Empty Graph");
}
}
//return dependencies of graph all nodes
int dependencies(struct Graph *node) {
if (node != NULL) {
struct AjlistNode *temp = NULL;
int result = 0;
//count the number of edges
for (int index = 0; index < size; index++) {

temp = node[index].next;
while (temp != NULL) {
//count edges
result++;
temp = temp->next;
}
}
return result;
} else {
printf("Empty Graph");
return 0;
}
}

int main() {

size = 5;

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

if (node == NULL) {
printf("\n Memory overflow");
} else {
//First set node keys
set_data(node);
//Connected two node with Edges
print_graph(node);
printf("\n Sum of dependencies : %d\n", dependencies(node));
}

return 0;
}```
```

#### Output

`````` Adjacency list of vertex 0  :
Adjacency list of vertex 1  :  0  2
Adjacency list of vertex 2  :
Adjacency list of vertex 3  :  0  2  4
Adjacency list of vertex 4  :  2
Sum of dependencies : 6``````
``````// C++ program
// Find Sum of dependencies in a 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) {
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;
}
}
//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++;
}
}
}
//return dependencies of graph all nodes
int dependencies() {
int result = 0;
if (this->node != NULL) {
AjlistNode *temp = NULL;
//count the number of edges

for (int index = 0; index < this->size; index++) {
temp = this->node[index].next;
while (temp != NULL) {
//count edges
result++;
temp = temp->next;
}
}
} else {
cout << "Empty Graph";
}
return result;
}
};
int main() {
MyGraph g =  MyGraph(6);
//Connected two node with Edges
g.print_graph();
cout << "\n Sum of dependencies : " << g.dependencies()<<endl;
return 0;
}```
```

#### Output

``````Adjacency list of vertex 0 :
Adjacency list of vertex 1 : 0 2
Adjacency list of vertex 2 :
Adjacency list of vertex 3 : 0 2 4
Adjacency list of vertex 4 : 2
Adjacency list of vertex 5 :
Sum of dependencies : 6``````
``````// Java program
// Find Sum of dependencies in a 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) {
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;
}
}
//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++;
}
}
}

//return dependencies of graph all nodes
public int dependencies() {
int result = 0;
if (node != null) {
AjlistNode temp = null;

//count the number of edges
for (int index = 0; index < this.size; index++) {
temp = node[index].next;
while (temp != null) {
//count edges
result++;
temp = temp.next;
}
}

} else {
System.out.print("Empty Graph");
}
return result;
}
public static void main(String[] args) {

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

System.out.print("\n Sum of dependencies  : "+g.dependencies());

}
}
```
```

#### Output

``````Adjacency list of vertex 0 :
Adjacency list of vertex 1 : 0 2
Adjacency list of vertex 2 :
Adjacency list of vertex 3 : 0 2 4
Adjacency list of vertex 4 : 2
Adjacency list of vertex 5 :
Sum of dependencies : 6``````
``````// C# program
// Find Sum of dependencies in a graph
using System;
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 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) {
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;
}
}
//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++;
}
}
}
//return dependencies of graph all nodes
public int dependencies() {
int result = 0;
if (node != null) {
AjlistNode temp = null;
//count the number of edges

for (int index = 0; index < this.size; index++) {
temp = node[index].next;
while (temp != null) {
//count edges
result++;
temp = temp.next;
}
}
} else {
Console.Write("Empty Graph");
}
return result;
}
public static void Main(String[] args) {
MyGraph g = new MyGraph(6);
g.print_graph();
Console.Write("\n Sum of dependencies : " + g.dependencies());
}
}```
```

#### Output

``````Adjacency list of vertex 0 :
Adjacency list of vertex 1 : 0 2
Adjacency list of vertex 2 :
Adjacency list of vertex 3 : 0 2 4
Adjacency list of vertex 4 : 2
Adjacency list of vertex 5 :
Sum of dependencies : 6``````
``````<?php
// Php program
// Find Sum of dependencies in a 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
\$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;
}
}
//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++;
}
}
}
//return dependencies of graph all nodes
public 	function dependencies() {
\$result = 0;
if (\$this->node != null) {
\$temp = null;
//count the number of edges
for (\$index = 0; \$index < \$this->size; \$index++) {
\$temp = \$this->node[\$index]->next;
while (\$temp != null) {
//count edges
\$result++;
\$temp = \$temp->next;
}
}
} else {
echo("Empty Graph");
}
return \$result;
}
}

function main() {
\$g = new MyGraph(6);
//Connected two node with Edges
\$g->print_graph();
echo("\n Sum of dependencies : ". \$g->dependencies());

}
main();```
```

#### Output

``````Adjacency list of vertex 0 :
Adjacency list of vertex 1 : 0 2
Adjacency list of vertex 2 :
Adjacency list of vertex 3 : 0 2 4
Adjacency list of vertex 4 : 2
Adjacency list of vertex 5 :
Sum of dependencies : 6``````
``````// Node Js program
// Find Sum of dependencies in a 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(0);
//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
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;
}
}

//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++;
}
}
}

//return dependencies of graph all nodes
dependencies() {
var result = 0;
if (this.node != null) {
var temp = null;
//count the number of edges

for (var index = 0; index < this.size; index++) {
temp = this.node[index].next;
while (temp != null) {
//count edges
result++;
temp = temp.next;
}
}
} else {
process.stdout.write("Empty Graph");
}

return result;
}
}

function main(args) {
var g = new MyGraph(6);
//Connected two node with Edges
g.print_graph();
process.stdout.write("\n Sum of dependencies : " + g.dependencies());
}

main();```
```

#### Output

``````Adjacency list of vertex 0 :
Adjacency list of vertex 1 : 0 2
Adjacency list of vertex 2 :
Adjacency list of vertex 3 : 0 2 4
Adjacency list of vertex 4 : 2
Adjacency list of vertex 5 :
Sum of dependencies : 6``````
``````#  Python 3 program
#  Find Sum of dependencies in a 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
# set initial values of graph node
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
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

# 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

# return dependencies of graph all nodes
def dependencies(self) :
result = 0
if (self.node != None) :
temp = None
index = 0
# count the number of edges
while (index < self.size) :
temp = self.node[index].next
while (temp != None) :
# count edges
result += 1
temp = temp.next

index += 1

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

return result

def main() :
g = MyGraph(6)
# Connected two node with Edges
g.print_graph()
print("\n Sum of dependencies : ", g.dependencies())

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

#### Output

``````Adjacency list of vertex  0  :
Adjacency list of vertex  1  : 0  2
Adjacency list of vertex  2  :
Adjacency list of vertex  3  : 0  2  4
Adjacency list of vertex  4  : 2
Adjacency list of vertex  5  :
Sum of dependencies :  6``````
``````#  Ruby program
#  Find Sum of dependencies in a 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) {0}
# set initial values of graph node
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
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
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
# return dependencies of graph all nodes
def dependencies()
result = 0
if (@node != nil)
temp = nil
index = 0
# count the number of edges
while (index < self.size)
temp = @node[index].next
while (temp != nil)
# count edges
result += 1
temp = temp.next
end
index += 1
end
else
print("Empty Graph")
end
return result
end
end
def main()
g = MyGraph.new(6)
# Connected two node with Edges
g.print_graph()
print("\n Sum of dependencies  : ", g.dependencies())
end
main()```
```

#### Output

``````Adjacency list of vertex 0  :
Adjacency list of vertex 1  : 0 2
Adjacency list of vertex 2  :
Adjacency list of vertex 3  : 0 2 4
Adjacency list of vertex 4  : 2
Adjacency list of vertex 5  :
Sum of dependencies  : 6``````
``````// Scala program
// Find Sum of dependencies in a graph
class AjlistNode(var id: Int,
var next: AjlistNode) {

def this(id: Int) {
//Set value of node value
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) {
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 (node == null) {
print("\nEmpty Graph");
} else {
var index: Int = 0;
while (index < size) {
node(index) = new Vertices(index);
index += 1;
}
}
}
//Connect two node
def add_edge(start: Int, last: Int): Unit = {
var newEdge: AjlistNode = new AjlistNode(last);

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

//Add new node at the last of edge
while (temp.next != null) {
temp = temp.next;
}
temp.next = newEdge;
}
}
//Display graph elements
def print_graph(): Unit = {
if (size > 0 &&
node != null) {
var index: Int = 0;
while (index < size) {
print("\nAdjacency list of vertex " + index + " :");
var temp: AjlistNode = node(index).next;
while (temp != null) {
print(" "+node(temp.id).data );
temp = temp.next;
}
index += 1;
}
}
}
//return dependencies of graph all nodes
def dependencies(): Int = {
var result: Int = 0;

if (node != null) {
var temp: AjlistNode = null;
var index: Int = 0;

//count the number of edges
while (index < this.size) {
temp = node(index).next;
while (temp != null) {
//count edges
result += 1;
temp = temp.next;
}
index += 1;
}
} else {
print("Empty Graph");
}
return result;
}
}
object Main {
def main(args: Array[String]): Unit = {
var g: MyGraph = new MyGraph(6);

//Connected two node with Edges
g.print_graph();
print("\n Sum of dependencies : " + g.dependencies());
}
}```
```

#### Output

``````Adjacency list of vertex 0 :
Adjacency list of vertex 1 : 0 2
Adjacency list of vertex 2 :
Adjacency list of vertex 3 : 0 2 4
Adjacency list of vertex 4 : 2
Adjacency list of vertex 5 :
Sum of dependencies : 6``````
``````// Swift program
// Find Sum of dependencies in a 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;
}
}
//Set initial node value
func set_data() {
if (self.node == nil) {
print("\nEmpty Graph", terminator: "");
} else {
var index: Int = 0;
while (index < self.size) {
self.node![index] = Vertices(index);
index += 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 end 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;
}
}
}
//return dependencies of graph all nodes
func dependencies() -> Int {
var result: Int = 0;
if (self.node != nil) {
var temp: AjlistNode? = nil;
var index: Int = 0;
//count the number of edges
while (index < self.size) {
temp = self.node![index].next;
while (temp != nil) {
//count edges
result += 1;
temp = temp!.next;
}
index += 1;
}
} else {
print("Empty Graph", terminator: "");
}
return result;
}
}
func main() {
let g: MyGraph? = MyGraph(6);
//Connected two node with Edges
g!.print_graph();
print("\n Sum of dependencies : ", g!.dependencies(), terminator: "");
}
main();```
```

#### Output

``````Adjacency list of vertex  0  :
Adjacency list of vertex  1  : 0  2
Adjacency list of vertex  2  :
Adjacency list of vertex  3  : 0  2  4
Adjacency list of vertex  4  : 2
Adjacency list of vertex  5  :
Sum of dependencies :  6``````

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.