-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVertex.java
80 lines (45 loc) · 1.67 KB
/
Vertex.java
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
package DS;
import java.util.ArrayList;
public class Vertex {
final String name;
private final ArrayList<Edge> edges;
public Vertex(String name1) {
name = name1;
edges = new ArrayList<>();
}
public boolean isAdjacent(Vertex secondVertex) {
// if (edges.stream().anyMatch((e) -> ((e.first == secondVertex) || (e.second == secondVertex)))) {
// return true;
// }
// return false;
// Functional Operation can be used
return edges.stream().anyMatch((e)-> ((e.first == secondVertex) || (e.second == secondVertex)));
}
public void addEdge(Edge e) { edges.add(e); }
public ArrayList<Vertex> getAdjacent() {
ArrayList<Vertex> output = new ArrayList<>();
edges.stream().forEach((e) -> {
if (this == e.first) output.add(e.second);
else output.add(e.first);
});
return output;
}
public void removeEdgeWith(Vertex toBeRemoved) {
for (int i = 0; i < edges.size(); i++) {
Edge e = edges.get(i);
if ((e.first == toBeRemoved) || (e.second == toBeRemoved)) {
edges.remove(i);
return;
}
}
}
public int getDegree() { return edges.size(); }
public void print() {
System.out.print(name + ":");
ArrayList<Vertex> adjacent = getAdjacent();
adjacent.stream().forEach((v) -> {
System.out.print(v.name + ",");
});
System.out.println();
}
}