Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit db30f69

Browse filesBrowse files
committed
changes in Weighted Map Graph
1 parent 0adad87 commit db30f69
Copy full SHA for db30f69

11 files changed

+23
-25
lines changed

‎.idea/workspace.xml

Copy file name to clipboardExpand all lines: .idea/workspace.xml
+12-10Lines changed: 12 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

‎src/com/company/Lecture22/AdjMapGraph.java

Copy file name to clipboardExpand all lines: src/com/company/Lecture22/AdjMapGraph.java
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import java.util.*;
44

5-
public class AdjMapGraph<E> {
5+
public class
6+
AdjMapGraph<E> {
67

78
private Map<E, Vertex> vertices = new HashMap<E, Vertex>();
89

‎src/com/company/Lecture22/AdjMapWeightGraph.java

Copy file name to clipboardExpand all lines: src/com/company/Lecture22/AdjMapWeightGraph.java
+9-14Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@ public class AdjMapWeightGraph<E> {
88

99
private class Vertex{
1010
E value;
11-
Map<E, Vertex> neighbours = new HashMap<E, Vertex>();
12-
Map<E, Integer> weights = new HashMap<E, Integer>();
11+
Map<Vertex, Integer> neighbours = new HashMap<Vertex, Integer>();
1312

1413
public Vertex(E value) {
1514
this.value = value;
1615
}
1716

1817
public void addNeighbour(Vertex vertex, Integer weight){
19-
neighbours.put(vertex.value,vertex);
20-
weights.put(vertex.value,weight);
18+
neighbours.put(vertex, weight);
2119
}
2220
}
2321

@@ -36,8 +34,8 @@ public Edge(Vertex start, Vertex end, Integer weight) {
3634
public Integer kruskal(){
3735
ArrayList<Edge> list = new ArrayList<Edge>();
3836
for(Vertex start : vertices.values()){
39-
for(Vertex end : start.neighbours.values()){
40-
Integer weight = start.weights.get(end.value);
37+
for(Vertex end : start.neighbours.keySet()){
38+
Integer weight = start.neighbours.get(end.value);
4139
list.add(new Edge(start, end, weight));
4240
}
4341
}
@@ -63,7 +61,6 @@ public int compare(Edge o1, Edge o2) {
6361
}
6462

6563
public int prims(){
66-
6764
Vertex start = vertices.values().iterator().next();
6865
Set<Vertex> visited = new HashSet<Vertex>();
6966

@@ -75,28 +72,26 @@ public int compare(Edge o1, Edge o2) {
7572
});
7673

7774
visited.add(start);
78-
for (Vertex end : start.neighbours.values()){
79-
int weight = start.weights.get(end.value);
75+
for (Vertex end : start.neighbours.keySet()){
76+
int weight = start.neighbours.get(end.value);
8077
queue.add(new Edge(start, end, weight));
8178
}
82-
8379
int total = 0;
8480
while (!queue.isEmpty()){
8581
Edge edge = queue.remove();
8682
if (!visited.contains(edge.end)){
8783
total += edge.weight;
8884

8985
Vertex temp_s = edge.end;
90-
for (Vertex temp_e : temp_s.neighbours.values()){
86+
for (Vertex temp_e : temp_s.neighbours.keySet()){
9187
if (!visited.contains(temp_e)){
92-
int weight = temp_s.weights.get(temp_e.value);
88+
int weight = temp_s.neighbours.get(temp_e.value);
9389
queue.add(new Edge(temp_s, temp_e, weight));
9490

9591
}
9692
}
9793
}
9894
}
99-
10095
return total;
10196
}
10297

@@ -170,7 +165,7 @@ public void DFT(E value){
170165
Vertex top = stack.pop();
171166
System.out.print(top.value + " ");
172167

173-
for (Vertex vertex : top.neighbours.values()){
168+
for (Vertex vertex : top.neighbours.keySet()){
174169
if(!set.contains(vertex)){
175170
stack.push(vertex);
176171
set.add(vertex);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.