Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Commit 07f497a

Browse files
committed
dataclass removed
1 parent 18077bd commit 07f497a

File tree

1 file changed

+29
-38
lines changed

1 file changed

+29
-38
lines changed

Algorithms/Greedy Algorithms/Prims_Algorithms.py

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,66 +13,57 @@
1313
Time Complexity : O(ElogV)
1414
"""
1515

16-
from dataclasses import dataclass
1716

18-
@dataclass(eq=0)
19-
class Node:
20-
_num : int
17+
# @dataclass(eq=0)
2118

22-
@dataclass
23-
class Graph:
24-
source : int
25-
_list : dict
19+
20+
class Node(object):
21+
def __init__(self, source=None):
22+
self._num = source
23+
24+
25+
class Prims(object):
26+
def __init__(self, source, graph):
27+
self.source = source
28+
self._list = graph
2629

2730
def prims_MCST(self):
2831
MCST_Cost = 0
29-
queue = {Node(self.source) : 0}
32+
queue = {Node(self.source): 0}
3033
added = [0]*len(self._list)
3134

3235
while queue:
33-
node = min(queue,key=queue.get)#choosing the adjancent Node having the minimum cost
36+
# choosing the adjancent Node having the minimum cost
37+
node = min(queue, key=queue.get)
3438
cost_Node = queue[node]
35-
#Delete that Node from the Dict
39+
# Delete that Node from the Dict
3640
del queue[node]
3741

38-
if added[node._num] == 0: #If node is not Visited
39-
MCST_Cost+=cost_Node
40-
41-
added[node._num] = True #Mark Visited
42+
if added[node._num] == 0: # If node is not Visited
43+
MCST_Cost += cost_Node
4244

43-
print(f"{node._num} --> ",end=" ")
45+
added[node._num] = True # Mark Visited
4446

47+
print(f"{node._num} --> ", end=" ")
4548

4649
for n in self._list[node._num]:
4750
adjancent_node = n[0]
4851
adjancent_cost = n[1]
4952
if added[adjancent_node] == 0:
5053
queue[Node(adjancent_node)] = adjancent_cost
51-
print("Cost : ",MCST_Cost)
54+
print("Cost : ", MCST_Cost)
5255

5356

5457
if __name__ == '__main__':
5558
list = {}
56-
list[1]=[(6,10),(2,28)]
57-
list[2]=[(1,28),(3,16),(0,14)]
58-
list[3]=[(2,16),(4,12)]
59-
list[4]=[(3,12),(5,22),(0,18)]
60-
list[5]=[(4,22),(6,25),(0,24)]
61-
list[6]=[(1,10),(5,25)]
62-
list[0]=[(2,14),(4,18),(5,24)]
63-
64-
A = Graph(3,list) #Insert Any Node
59+
list[1] = [(6, 10), (2, 28)]
60+
list[2] = [(1, 28), (3, 16), (0, 14)]
61+
list[3] = [(2, 16), (4, 12)]
62+
list[4] = [(3, 12), (5, 22), (0, 18)]
63+
list[5] = [(4, 22), (6, 25), (0, 24)]
64+
list[6] = [(1, 10), (5, 25)]
65+
list[0] = [(2, 14), (4, 18), (5, 24)]
66+
67+
A = Prims(3, list) # Insert Any Node
6568
print("MCST")
6669
A.prims_MCST()
67-
68-
69-
70-
71-
72-
73-
74-
75-
76-
77-
78-

0 commit comments

Comments
 (0)