# Find indegree and outdegree of a directed graph in python

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

``````#    Python 3 Program for
#    Show degree of vertex in directed graph
class AjlistNode :
#  Vertices node key
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
self.last = None

class Graph :
#  Number of Vertices
def __init__(self, size) :
#  Set value
self.size = size
self.node = [None] * (size)
self.setData()

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

#   Handling the request of adding new edge
if (start >= 0 and start < self.size and
last >= 0 and last < self.size) :
#  Safe connection
edge = AjlistNode(last)
if (self.node[start].next == None) :
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")

def printGraph(self) :
if (self.size > 0) :
index = 0
#  Print graph ajlist Node value
while (index < self.size) :
print("\nAdjacency list of vertex ", index , end = " : ")
temp = self.node[index].next
while (temp != None) :
#  Display graph node
print(self.node[temp.id].data, end = "  ")
#  Visit to next edge
temp = temp.next

index += 1

#  Find indegree and outdegree of
#  each nodes of a given graph
def findDegree(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

#  Set outdegree value
outdegree[i] = length
i += 1

def showDegree(self) :
if (self.node != None) :
indegree = [0] * (self.size)
outdegree = [0] * (self.size)
i = 0
#  Set zero degree
while (i < self.size) :
indegree[i] = 0
outdegree[i] = 0
i += 1

#  Find degree
self.findDegree(indegree, outdegree)
print("\n")
i = 0
#  Display result
while (i < self.size) :
print("Node [", i ,"] indegree [",
indegree[i] ,"] outdegree [",
outdegree[i] ,"]")
i += 1

else :
print("Empty Graph")

def main() :
#  6 implies the number of nodes in graph
g = Graph(6)
#  Connected two node with Edges
g.printGraph()
#  Test
g.showDegree()

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

## 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.