-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphUsingDisjointSetDataStructures.java
More file actions
51 lines (40 loc) · 1.31 KB
/
Copy pathGraphUsingDisjointSetDataStructures.java
File metadata and controls
51 lines (40 loc) · 1.31 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
package com.geeksforgeeks.graph;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GraphUsingDisjointSetDataStructures<T> {
private List<Edge<T>> allEdges;
private Map<Long, Vertex<T>> allVertex;
boolean isDirected = false;
public GraphUsingDisjointSetDataStructures(boolean isDirected) {
allEdges = new ArrayList<>();
allVertex = new HashMap<>();
this.isDirected = isDirected;
}
public void addEdge(long l1, long l2) {
addEdge(l1, l2, 0);
}
public void addEdge(long l1, long l2, int weight) {
allVertex.putIfAbsent(l1, new Vertex<>(l1));
allVertex.putIfAbsent(l2, new Vertex<>(l2));
Vertex vertex1 = allVertex.get(l1);
Vertex vertex2 = allVertex.get(l2);
Edge edge = new Edge(vertex1, vertex2, isDirected, weight);
allEdges.add(edge);
vertex1.addAdjacentVertex(edge, vertex2);
if (!isDirected) {
vertex2.addAdjacentVertex(edge, vertex1);
}
}
public Vertex<T> getVertex(long id) {
return allVertex.get(id);
}
public List<Edge<T>> getAllEdges() {
return allEdges;
}
public Collection<Vertex<T>> getAllVertex() {
return allVertex.values();
}
}