-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
140 lines (132 loc) · 3.74 KB
/
Graph.java
File metadata and controls
140 lines (132 loc) · 3.74 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
139
140
import java.util.*;
/**
* Graph representation of the network using adjacency list
*/
public class Graph {
private List<EdgeList> adjList = new ArrayList<>();
private ArrayList<Node> nodes;
/**
* Constructor
* @param nodes
*/
public Graph(ArrayList<Node> nodes){
this.nodes = nodes;
for (Node n: nodes){
this.adjList.add(new EdgeList(n, setNodeEdges(n)));
}
}
/**
* Filter to get just the edges for each node
* @param node
* @return
*/
private ArrayList<Node> setNodeEdges(Node node) {
ArrayList<Node> edges = new ArrayList<>();
for (Node n: this.nodes) {
if (n.getId() != node.getId())
edges.add(n);
}
return edges;
}
/**
* Find node by id
* @param id
* @return
*/
public Node findNode(int id){
for (Node n: nodes){
if (n.getId() == id)
return n;
}
return null; //Node not found
}
/**
*
* @param node
*/
public void addNode(Node node){
this.adjList.add(new EdgeList(node, setNodeEdges(node)));
this.nodes.add(node);
for (EdgeList edgeList: this.adjList) {
if (!edgeList.getNode().equals(node)){
edgeList.setEdge(node);
}
}
}
/**
*
* @param node
*/
public void removeNode(Node node){
//Remove node from nodes
for(Node n: this.nodes){
if (node.equals(n)){
this.nodes.remove(n);
break;
}
}
//Remove edgelist from adjacency list containing the node
for(EdgeList edgeList: this.adjList){
if (edgeList.getNode().equals(node)){
this.adjList.remove(edgeList);
break;
}
}
//For the remaining edgelist entries, remove the node from the other edgelist edges
for(EdgeList edgeList: this.adjList){
//If edges contains the node to be removed, then iterate over that list of edges to find such node
if (edgeList.getEdges().contains(node)){
for (Node edge: edgeList.getEdges()){
//Once node found in the list, remove it
if (edge.equals(node)){
edgeList.getEdges().remove(edge);
break;
}
}
}
}
}
/**
* Return the ids of all nodes in the graph
* @return
*/
public ArrayList<Integer> getAllNodeIds(){
ArrayList<Integer> nodeIds = new ArrayList<>();
for (Node n: this.nodes) {
nodeIds.add(n.getId());
}
return nodeIds;
}
/**
* Return the edges as integer ids connected to the specified node
* @param node
* @param size
* @return
*/
public ArrayList<Integer> findEdges(Node node, int size){
ArrayList<Integer> edgeIds = new ArrayList<>();
for (EdgeList edgeList: this.adjList){
if (edgeList.getNode().equals(node)){
for (Node n: edgeList.getEdges()){
edgeIds.add(n.getId());
}
}
}
return edgeIds;
}
/**
* Print network status
* @return result
*/
@Override
public String toString(){
String result = "NETWORK STATUS:\n";
for(EdgeList el: this.adjList){
result += "Processor " + el.getNode().getId() + ": " + "Stored Keys: " + el.getNode().getStoredKeys() + " => Connected Nodes: ";
for(Node n: el.getEdges())
result += n.getId() + ", ";
result += "\n";
}
return result;
}
}