99import java .util .Set ;
1010
1111public class PrimMST {
12- static Map <Integer , HashMap <Integer , Double >> adjacencyList = new HashMap <Integer , HashMap <Integer , Double >>();
13- static Set <Integer > mst = new HashSet <Integer >();
14-
15- static void addEdge (int from , int to , double weight ) {
16- if (adjacencyList .containsKey (from )) {
17- adjacencyList .get (from ).put (to , weight );
18- } else {
19- HashMap <Integer , Double > edges = new HashMap <Integer , Double >();
20- edges .put (to , weight );
21- adjacencyList .put (from , edges );
22- }
23- }
24-
25-
26- static Set <Entry <Integer , Double >> neighborsAndWeight (int node ) {
27- Set <Entry <Integer , Double >> neighborsAndWeight = new HashSet <Entry <Integer , Double >>();
28- if (adjacencyList .containsKey (node )) {
29- neighborsAndWeight = adjacencyList .get (node ).entrySet ();
30- }
31- return neighborsAndWeight ;
32- }
33-
34- static double prim (int start ) {
35- HashSet <Integer > visited = new HashSet <Integer >();
36- Integer lastInserted = start ;
37- double sum = 0 ;
38- visited .add (start );
39-
40- PriorityQueue <WeightedEdge > possibleEdges = new PriorityQueue <Main .WeightedEdge >();
41-
42- while (adjacencyList .size () != visited .size ()) {
43- for (Entry <Integer , Double > nw : neighborsAndWeight (lastInserted )) {
44- possibleEdges .add (new WeightedEdge (lastInserted , nw .getKey (), nw .getValue ()));
45- }
12+ static Map <Integer , HashMap <Integer , Double >> adjacencyList = new HashMap <Integer , HashMap <Integer , Double >>();
13+ static Set <Integer > mst = new HashSet <Integer >();
14+
15+ static void addEdge (int from , int to , double weight ) {
16+ if (adjacencyList .containsKey (from )) {
17+ adjacencyList .get (from ).put (to , weight );
18+ } else {
19+ HashMap <Integer , Double > edges = new HashMap <Integer , Double >();
20+ edges .put (to , weight );
21+ adjacencyList .put (from , edges );
22+ }
23+ }
24+
25+
26+ static Set <Entry <Integer , Double >> neighborsAndWeight (int node ) {
27+ Set <Entry <Integer , Double >> neighborsAndWeight = new HashSet <Entry <Integer , Double >>();
28+ if (adjacencyList .containsKey (node )) {
29+ neighborsAndWeight = adjacencyList .get (node ).entrySet ();
30+ }
31+ return neighborsAndWeight ;
32+ }
33+
34+ static double prim (int start ) {
35+ HashSet <Integer > visited = new HashSet <Integer >();
36+ Integer lastInserted = start ;
37+ double sum = 0 ;
38+ visited .add (start );
39+
40+ PriorityQueue <WeightedEdge > possibleEdges = new PriorityQueue <Main .WeightedEdge >();
41+
42+ while (adjacencyList .size () != visited .size ()) {
43+ for (Entry <Integer , Double > nw : neighborsAndWeight (lastInserted )) {
44+ possibleEdges .add (new WeightedEdge (lastInserted , nw .getKey (), nw .getValue ()));
45+ }
4646
4747 WeightedEdge edge = possibleEdges .poll ();
4848
@@ -56,36 +56,36 @@ static double prim(int start) {
5656 sum += edge .weight ;
5757 }
5858
59- return sum ;
60- }
61-
62- static class WeightedEdge implements Comparable <WeightedEdge > {
63- public int from ;
64- public int to ;
65- public double weight ;
66-
67- public WeightedEdge (int from , int to , double weight ) {
68- super ();
69- this .from = from ;
70- this .to = to ;
71- this .weight = weight ;
72- }
73-
74- @ Override
75- public int compareTo (WeightedEdge anotherElement ) {
76- int ret ;
77-
78- if (this .weight < anotherElement .weight ) {
79- ret = -1 ;
80- } else if (this .weight == anotherElement .weight ) {
81- ret = 0 ;
82- } else {
83- ret = 1 ;
84- }
85-
86- return ret ;
87- }
88-
89- }
59+ return sum ;
60+ }
61+
62+ static class WeightedEdge implements Comparable <WeightedEdge > {
63+ public int from ;
64+ public int to ;
65+ public double weight ;
66+
67+ public WeightedEdge (int from , int to , double weight ) {
68+ super ();
69+ this .from = from ;
70+ this .to = to ;
71+ this .weight = weight ;
72+ }
73+
74+ @ Override
75+ public int compareTo (WeightedEdge anotherElement ) {
76+ int ret ;
77+
78+ if (this .weight < anotherElement .weight ) {
79+ ret = -1 ;
80+ } else if (this .weight == anotherElement .weight ) {
81+ ret = 0 ;
82+ } else {
83+ ret = 1 ;
84+ }
85+
86+ return ret ;
87+ }
88+
89+ }
9090
9191}
0 commit comments