-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVertex.java
More file actions
138 lines (126 loc) · 4.67 KB
/
Vertex.java
File metadata and controls
138 lines (126 loc) · 4.67 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
/*
* Vertex class
* Specific to Sudoku use case
*ChatGPT provided immense assistance with functions: removeEdge, removeEdges, countEdgetypes, hasEdgeWith, toString.
*/
import java.util.*;
public class Vertex {
private HashSet<Integer> candidateData;
private int row;
private int col;
private int[] box = new int[2];
private ArrayList<Edge> edges;
private ArrayList<Vertex> neighbors;
public Vertex(HashSet<Integer> candidateData){
this.candidateData = candidateData;
}
public Vertex(int row,int col, int[] box, HashSet<Integer> candidateData){
this.row = row;
this.col = col;
this.box[0] = box[0];
this.box[1] = box[1];
this.candidateData = candidateData;
this.neighbors = new ArrayList<Vertex>();
this.edges = new ArrayList<Edge>();
}
public Vertex(){
this.candidateData = null;
this.neighbors = new ArrayList<Vertex>();
this.edges = new ArrayList<Edge>();
this.row = -1;
this.col = -1;
}
public Map<String, Integer> countEdgeTypes() {
Map<String, Integer> edgeCount = new HashMap<>();
// Initialize counts for each edge type
edgeCount.put("box", 0);
edgeCount.put("row", 0);
edgeCount.put("col", 0);
// Iterate through the edges and count each type
for (Edge edge : this.edges) {
int edgeType = edge.getEdgeType();
if (edgeType == 0) { // row edge type
edgeCount.put("row", edgeCount.get("row") + 1);
} else if (edgeType == 1) { // col edge type
edgeCount.put("col", edgeCount.get("col") + 1);
} else if (edgeType == 2) { // box edge type
edgeCount.put("box", edgeCount.get("box") + 1);
}
}
return edgeCount;
}
public void removeEdges(Vertex target) { //chat help
// Loop through edges list and remove edges that involve the target vertex
for (int i = 0; i < this.edges.size(); i++) {
Edge edge = this.edges.get(i);
// Assuming each edge connects this vertex with a neighbor
if (edge.getv1().equals(target) || edge.getV2().equals(target)) {
this.edges.remove(i); // Remove the edge at index i
i--; // Adjust the index to account for the removed element
}
}
}
public void removeEdge(Vertex neighbor) {
this.neighbors.remove(neighbor);
neighbor.neighbors.remove(this);
// Remove the corresponding edge
this.edges.removeIf(edge -> edge.getV2().equals(neighbor));
neighbor.edges.removeIf(edge -> edge.getV2().equals(this));
}
public void clearEdges() {
this.edges.clear();
}
public ArrayList<Vertex> getNeighbors(){
if (this.neighbors!= null)
{
return this.neighbors;
}else{
return new ArrayList<Vertex>();
}
}
public HashSet<Integer> getCandidates(){
if (this.candidateData!= null)
{
return this.candidateData;
}else{
return new HashSet<Integer>();
}
}
public int getRow(){
return this.row;
}
public int getCol(){
return this.col;
}
public int[] getBoxID(){
return this.box;
}
public ArrayList<Edge> getEdges(){
return this.edges;
}
public void addEdge(Vertex vertex, int edgeType){
if (!hasEdgeWith(vertex, edgeType)){
this.edges.add(new Edge(this, vertex, edgeType));
this.neighbors.add(vertex);
}
}
public boolean hasEdgeWith(Vertex v, int edgeType) {
for (Edge e : this.getEdges()) {
if (e.getV2().equals(v) && e.getEdgeType() == edgeType) {
return true;
}
}
return false;
}
public String toString(){
StringBuilder neighborData = new StringBuilder();//chat help
for (Vertex neighbor : neighbors) {
neighborData.append("[" +neighbor.getRow() + "," +neighbor.getCol()+ "]" + neighbor.getCandidates()).append(", "); // Replace getData() with an appropriate property
}
if (neighborData.length() > 0) {
neighborData.setLength(neighborData.length() - 2); // Remove the trailing ", "
}
return ("[" + this.row + ","+this.col + "]" +"here is my data: " + this.candidateData + "and ym neighbors: " + this.neighbors.size() + "\n " + countEdgeTypes());
// return("here is my data: " + this.candidateData + " \n here is my neigh: " + this.getNeighbors() + "\nrow,col,box: ");
}
}