-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunDirectedGraph.java
54 lines (46 loc) · 1.33 KB
/
unDirectedGraph.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
import java.util.HashSet;
public class unDirectedGraph implements Graph {
HashSet<GraphNode> allNodes;
public unDirectedGraph() {
allNodes = new HashSet<>();
}
public HashSet<GraphNode> getAllNodes()
{
return allNodes;
}
public GraphNode get(int element)
{
for (GraphNode node : allNodes) {
if (node.value == (element))
return node;
}
return null;
}
public void addNode(final int nodeVal)
{
GraphNode node = new GraphNode(nodeVal);
allNodes.add(node);
}
public void addUndirectedEdge(final GraphNode first, final GraphNode second)
{
Edge edge = new Edge(second, 1);
first.neighbors.add(edge);
Edge edge2 = new Edge(first, 1);
second.neighbors.add(edge2);
}
public void removeUndirectedEdge(final GraphNode first, final GraphNode second)
{
for (Edge edge : first.neighbors) {
if (edge.destination == second) {
first.neighbors.remove(edge);
break;
}
}
for (Edge edge : second.neighbors) {
if (edge.destination == first) {
second.neighbors.remove(edge);
break;
}
}
}
}