-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphMstPrimOperator.java
More file actions
76 lines (47 loc) · 1.17 KB
/
Copy pathGraphMstPrimOperator.java
File metadata and controls
76 lines (47 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.ArrayList;
import java.util.Collection;
public class GraphMstPrimOperator implements GraphMSTOperators {
// Implement the PRIM algorithm, return a graph that represents
// the MST of graph g
private Graph primMSTAlgorithm(Graph g) {
int V1dx =0;
int maxSize = 5;
Graph tree = (Graph)new GraphAL();
boolean[]s = new boolean[g.getNumberOfVertices()];
Heap p = new Heap(g.getNumberOfVertices());
s[V1dx]=false;
AdjacentNode n = null;
for(int i=0;i<g.getNumberOfVertices();i++)
{
tree.addVertex(g.getVertex(i).name);
}
for(int i=0;i<g.getNumberOfVertices();i++)
{
Vertex d = g.getVertex(i);
s[d.VertexNum]=true;
ArrayList<Vertex> c = g.getAdjacentVertices(d.name);
for(Vertex w : c)
{
p.add(w);
}
Vertex e = p.remove();
while (s[d.VertexNum]==true)
{
if(p.isEmpty()==true)
{
break;
}
else
{
p.remove();
}
}
//s[d.VertexNum]=true;
tree.addBidirectionalEdge(e.name, d.name, g.getWeight(d.name, e.name));
}
return tree;
}
public Graph getMSTree(Graph g) {
return primMSTAlgorithm(g) ;
}
}