Repository navigation
Expand file tree
/
Copy pathGridGraph.java
More file actions
75 lines (65 loc) · 1.73 KB
/
Copy pathGridGraph.java
File metadata and controls
75 lines (65 loc) · 1.73 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
import java.util.ArrayList;
import java.util.HashSet;
public class GridGraph {
HashSet<GridNode> allNodes = new HashSet<>();
public void addGridNode(final int x, final int y, final int nodeVal)
{
GridNode node = new GridNode(x, y, nodeVal);
allNodes.add(node);
}
public void addUndirectedEdge(final GridNode first, final GridNode second, final int edgeWeight)
{
first.neighbors.add(new GridEdge(second, 1));
second.neighbors.add(new GridEdge(first, 1));
}
public void removeWeightedEdge(final GridNode first, final GridNode second)
{
first.neighbors.remove(getEdge(second));
first.neighbors.remove(getEdge(first));
}
public HashSet<GridNode> getAllNodes()
{
return allNodes;
}
public GridNode getEdge(GridNode first)
{
for (GridEdge edge : first.neighbors) {
if (edge.destination == first) {
return first;
}
}
return null;
}
public GridNode getByCoordinates(int x, int y)
{
for (GridNode node : allNodes) {
if (node.x == x && node.y == y)
return node;
}
return null;
}
}
class GridNode
{
boolean visited;
ArrayList<GridEdge> neighbors;
int x, y, value;
public GridNode (int x, int y, int value)
{
this.x = x;
this.y = y;
neighbors = new ArrayList<>();
this.value = value;
visited = false;
}
}
class GridEdge
{
int weight;
GridNode destination;
public GridEdge (GridNode second, int weight)
{
this.weight = weight;
destination = second;
}
}