# Find indegree and outdegree of a directed graph in swift

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

``````import Foundation
/*
Swift 4 Program for
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? ;
var last: AjlistNode? ;
init(_ data: Int)
{
self.data = data;
self.next = nil;
self.last = nil;
}
}
class Graph
{
// Number of Vertices
var size: Int;
var node: [Vertices? ];
init(_ size: Int)
{
// Set value
self.size = size;
self.node = Array(repeating: nil, count: size);
self.setData();
}
// Set initial node value
func setData()
{
if (self.size <= 0)
{
print("\nEmpty Graph");
}
else
{
var index: Int = 0;
while (index < self.size)
{
// Set initial node value
self.node[index] = Vertices(index);
index += 1;
}
}
}
//  Handling the request of adding new edge
func addEdge(_ start: Int, _ last: Int)
{
if (start >= 0 && start < self.size && last >= 0 && last < self.size)
{
// Safe connection
let edge: AjlistNode? = AjlistNode(last);
if (self.node[start]!.next == nil)
{
self.node[start]!.next = edge;
}
else
{
// Add edge at the end
self.node[start]!.last!.next = edge;
}
// Get last edge
self.node[start]!.last = edge;
}
else
{
// When invalid nodes
print("\nHere Something Wrong");
}
}
func printGraph()
{
if (self.size > 0)
{
var index: Int = 0;
// Print graph ajlist Node value
while (index < self.size)
{
print("\nAdjacency list of vertex ", index, terminator: " : ");
var temp: AjlistNode? = self.node[index]!.next;
while (temp  != nil)
{
// Display graph node
print(self.node[temp!.id]!.data,
terminator: " ");
// Visit to next edge
temp = temp!.next;
}
index += 1;
}
}
}
// Find indegree and outdegree of
// each nodes of a given graph
func findDegree(_ 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;
}
// Set outdegree value
outdegree[i] = length;
i += 1;
}
}
func showDegree()
{
if (self.size  > 0)
{
var indegree: [Int] = Array(repeating: 0, count: self.size);
var outdegree: [Int] = Array(repeating: 0, count: self.size);

// Find degree
self.findDegree(&indegree, &outdegree);
print("\n");
var i: Int = 0;
// Display result
while (i < self.size)
{
print("Node [" + String(i) +
"] indegree [" + String(indegree[i]) +
"] outdegree [" + String(outdegree[i]) + "]");
i += 1;
}
}
else
{
print("Empty Graph", terminator: "");
}
}
static func main(_ args: [String])
{
// 6 implies the number of nodes in graph
let g: Graph? = Graph(6);
// Connected two node with Edges
g!.printGraph();
// Test
g!.showDegree();
}
}
Graph.main([String]());``````

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.