# Minimum weight cycle in an undirected weighted graph in python

Python program for Minimum weight cycle in an undirected weighted graph. Here problem description and explanation.

``````import sys
#  Python 3 program for
#  Minimum weight cycle in an undirected graph
class AjlistNode :
#  Vertices node key
def __init__(self, id, weight) :
#  Set value of node key
self.id = id
self.weight = weight
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.result = 0
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

def connection(self, start, last, weight) :
#  Safe connection
edge = AjlistNode(last, weight)
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

#   Handling the request of adding new edge
def addEdge(self, start, last, weight) :
if (start >= 0 and start < self.size and
last >= 0 and last < self.size) :
#  Connect edge with weight
self.connection(start, last, weight)
self.connection(last, start, weight)
else :
#  When invalid nodes
print("\nNode missing (", start ," ", last ,")", end = "")

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

index += 1

def minimumCycle(self, start, last, visit, sum, length) :
if (start >= self.size or last >= self.size or
start < 0 or last < 0 or self.size <= 0) :
return

if (visit[start] == True) :
#  Here length are indicate loop length
if (length > 2 and start == last and sum < self.result) :
#  Here length is indicate number of nodes
#  Because graph is undirected so we consider all cycle
#  Which contains more than 2 node
#  ---------------------
#  When find a new min weight cycle
self.result = sum

return

#  Here modified  the value of visited node
visit[start] = True
#  This is used to iterate nodes edges
edge = self.node[start].next
while (edge != None) :
#   Find solution using recursion
self.minimumCycle(edge.id, last, visit,
sum + (edge.weight), length + 1)
#  Visit to next edge
edge = edge.next

#  Reset the value of visited node status
visit[start] = False

def minWeightCycle(self) :
if (self.size <= 0) :
#  Empty graph
return

#  Auxiliary space which is used to store
#  Set initial visited node status
visit = [False] * (self.size)
self.result = sys.maxsize
i = 0
while (i < self.size) :
#  Check cycle of node i to i
#  Here initial cycle weight is zero
self.minimumCycle(i, i, visit, 0, 0)
i += 1

#  Display result
print("\nMin weight cycle : ", self.result)

def main() :
#  6 implies the number of nodes in graph
g = Graph(6)
#  Connect node with an edge
#  First and second parameter indicate node
#  Last parameter is indicate weight
#  Print graph element
g.printGraph()
#  Test
g.minWeightCycle()

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

Output

``````Adjacency list of vertex  0  :   1 [ 3 ]   3 [ -3 ]   4 [ 7 ]   5 [ 1 ]
Adjacency list of vertex  1  :   0 [ 3 ]   2 [ 11 ]   4 [ 8 ]   5 [ 0 ]
Adjacency list of vertex  2  :   1 [ 11 ]   3 [ 1 ]   5 [ 4 ]
Adjacency list of vertex  3  :   0 [ -3 ]   2 [ 1 ]   4 [ 2 ]
Adjacency list of vertex  4  :   0 [ 7 ]   1 [ 8 ]   3 [ 2 ]   5 [ 8 ]
Adjacency list of vertex  5  :   0 [ 1 ]   2 [ 4 ]   4 [ 8 ]   1 [ 0 ]
Min weight cycle :  3``````

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