-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeightedGraph.java
More file actions
192 lines (164 loc) · 5.26 KB
/
WeightedGraph.java
File metadata and controls
192 lines (164 loc) · 5.26 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import java.util.*;
class Vertex {
public String val;
public boolean isVisited;
public Vertex(String val){
this.val = val;
this.isVisited = false;
}
public void visit(){
this.isVisited = true;
System.out.println("Visiting "+val);
}
public void visit(boolean noprint){
this.isVisited = true;
}
}
class Edge implements Comparable {
public int start;
public int end;
public int weight;
public Edge(int s, int e, int w) {
start = s;
end = e;
weight = w;
}
/*
* Comparison is necessary since Edges are loaded into Priority Queue
*/
public int compareTo(Object o){
Edge e = (Edge) o;
if(this.weight < e.weight) {
return -1;
}else if(this.weight > e.weight) {
return 1;
}else {
return 0;
}
}
}
/**
* Weighted Graph based that uses a Dependency Matrix to
* hold edges.
*
*
*/
public class WeightedGraph{
public Vertex[] vertexList;
public int[][] adjMatrix;
int size;
int i;
public WeightedGraph(int n) {
this.size = n;
this.adjMatrix = new int[n][n];
this.vertexList = new Vertex[n];
}
public void addVertex(String s) {
vertexList[i] = new Vertex(s);
i++;
}
public void addEdge(int s, int d, int w){
this.adjMatrix[s][d] = w;
this.adjMatrix[d][s] = w;
}
public void printEdges(){
for(int i=0;i<size;i++) {
for(int j=0;j<size;j++) {
System.out.print("\t"+adjMatrix[i][j]);
}
System.out.print("\n");
}
}
/**
* Calculates Minimum spanning tree of Undirected Graph.
* Overall TC O(N) ; Space Complexity O(N)
*/
private void mst(){
/* Priority Queue to hold the edges */
Queue<Edge> edgePriorityQueue= new PriorityQueue<>();
HashSet<Integer> mst = new HashSet<>();
int currentVertex = 0;
// Start with vertex A ie. 0
mst.add(currentVertex);
// Just a counter to see how many loops it takes
int j = 1;
// O(N)
while(true){
System.out.println("Round "+j++);
// O(N)
fillPriorityQueue(currentVertex, mst, edgePriorityQueue);
// O(1)
Edge pathEdge = edgePriorityQueue.poll();
// Additional pruning to priority queue to delete all the edges that
// lead to a vertex already part of the tree.
// Pruning could take additional O(N)
while(pathEdge !=null && mst.contains(pathEdge.end)){
System.out.println("Pruning queue. Removed "+ getEdgeString(pathEdge));
pathEdge = edgePriorityQueue.poll();
}
if(pathEdge == null) {
// Exit condition
break;
}
// Add in MST
mst.add(pathEdge.end);
// Found the edge with the lowest weight, add the end of the edge
// to MST set
System.out.println(vertexList[pathEdge.start].val+
vertexList[pathEdge.end].val+pathEdge.weight);
// Start from the end vertex of that newly added Edge.
currentVertex = pathEdge.end;
System.out.println("---------------------------------------------------------");
}
}
/**
* Returns all the adjacent edges to Vertex s in O(N)
*
* Before an edge is added, it is filtered based on vertices covered so far.
* @param s Source Vertex
* @param mst HashSet containing the vertices covered so far in MST
* @param edgePriorityQueue A priority queue containing all the edges that have
* are visible/covered.
*/
private void fillPriorityQueue(int s, HashSet<Integer> mst, Queue<Edge> edgePriorityQueue) {
for(int i=0;i<size;i++) {
// There is a weighted edge between s and i
if(adjMatrix[s][i] > 0
// It is not a self edge
&& s != i
// Minimum Spanning Tree does not contain the destination vertex
&& !mst.contains(i)) {
// Add EDGE to priority queue.
edgePriorityQueue.add(new Edge(s,i,adjMatrix[s][i]));
}
}
}
/**
* To print.
*
*/
private String getEdgeString(Edge pathEdge) {
return vertexList[pathEdge.start].val+vertexList[pathEdge.end].val+pathEdge.weight;
}
public static void main(String []args){
WeightedGraph wg = new WeightedGraph(6);
wg.addVertex("A");
wg.addVertex("B");
wg.addVertex("C");
wg.addVertex("D");
wg.addVertex("E");
wg.addVertex("F");
wg.addEdge(0,1,6);
wg.addEdge(0,3,4);
wg.addEdge(1,3,7);
wg.addEdge(2,3,8);
wg.addEdge(1,2,10);
wg.addEdge(1,4,7);
wg.addEdge(2,4,5);
wg.addEdge(3,4,12);
wg.addEdge(2,5,6);
wg.addEdge(4,5,7);
//wg.printEdges();
wg.mst();
}
}