-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprims.txt
executable file
·115 lines (96 loc) · 2.96 KB
/
prims.txt
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package Prims;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Prims {
static int V,E;
static List<Vertex> graph;
public static void main(String[] args) {
Prims primsDriver = new Prims();
Scanner in = new Scanner(System.in);
V=in.nextInt();
E=in.nextInt();
Vertex[] vertex = new Vertex[V];
for(int i=0;i<V;i++){
vertex[i] = new Vertex(i);
}
int a;
int b;
int wt;
for(int i=0;i<E;i++){
a=in.nextInt();
b=in.nextInt();
wt=in.nextInt();
vertex[a].incidentEdges.add(new Edge(vertex[b],wt));
}
graph = new ArrayList<Vertex>();
for(int i=0;i<V;i++){
graph.add(vertex[i]);
}
// List<Vertex> graph = primsDriver.prepareGraphAdjacencyList();
primsDriver.primsMST(graph);
for (Vertex v : graph){
if(v.previous != null)
System.out.println(v.name + " "+v.previous.name);
}
}
/**
* COde for breadth first traversal given an adjacency list.
*
* @param graph
* @return
*/
private void primsMST(List<Vertex> graph) {
PriorityQueue<Vertex> priorityQueue = new PriorityQueue<Vertex>();
for (Vertex v : graph) {
v.key = Integer.MAX_VALUE;
priorityQueue.add(v);
}
Vertex peek = priorityQueue.peek();
peek.key = 0;
while (!priorityQueue.isEmpty()) {
Vertex minVertex = priorityQueue.poll();
if (minVertex.incidentEdges != null && minVertex.incidentEdges.size() > 0) {
for (Edge edge : minVertex.incidentEdges) {
if (priorityQueue.contains(edge.end) && edge.weight < edge.end.key) {
priorityQueue.remove(edge.end);
edge.end.key = edge.weight;
edge.end.previous = minVertex;
priorityQueue.add(edge.end);
}
}
}
}
}
}
class Vertex implements Comparable<Vertex> {
int name;
int key;
public List<Edge> incidentEdges=new ArrayList<Edge>();; //adjacency list of everry vertex
public Vertex previous;
public Vertex(int name) {
this.name = name;
// List<Edge> incidentEdges= new ArrayList<Edge>();
}
@Override
public String toString() {
return "Vertex [name=" + name + ", key=" + key + ", previous=" + previous + "]";
}
@Override
public int compareTo(Vertex o) {
return Integer.compare(key, o.key);
}
}
class Edge {
int weight;
Vertex end;
public Edge(Vertex end, int weight) {
this.weight = weight;
this.end = end;
}
@Override
public String toString() {
return "Edge [weight=" + weight + ", end=" + end + "]";
}
}