import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
/**
* Implementation of a directed unweighted Graph using Adjacency Matrix
*
* Complexity: addEdge, removeEdge, hasEdge O(1) / outEdges, inEdges O(n) /
* Space O(n^2)
*/
class AdjMatrixGraph {
int size;
private boolean[][] matrix;
public static final byte WHITE = 1;
public static final byte GREY = 2;
public AdjMatrixGraph(int size) {
this.size = size;
this.matrix = new boolean[size][size];
}
public void addEdge(int nodeA, int nodeB) {
matrix[nodeA][nodeB] = true;
}
public void removeEdge(int nodeA, int nodeB) {
matrix[nodeA][nodeB] = false;
}
public boolean hasEdge(int nodeA, int nodeB) {
return matrix[nodeA][nodeB];
}
public List<Integer> outEdges(int i) {
List<Integer> edges = new ArrayList<Integer>();
for (int j = 0; j < size; j++) {
if (matrix[i][j]) {
edges.add(j);
}
}
return edges;
}
public List<Integer> inEdges(int i) {
List<Integer> edges = new ArrayList<Integer>();
for (int j = 0; j < size; j++) {
if (matrix[j][i]) {
edges.add(j);
}
}
return edges;
}
public int nVertices() {
return size;
}
public void breadthFirstSearch(int root) {
boolean[] seen = new boolean[nVertices()];
Queue<Integer> q = new LinkedList<Integer>();
q.add(root);
seen[root] = true;
while (!q.isEmpty()) {
int i = q.remove();
if (!seen[j]) {
q.add(j);
seen[j] = true;
}
}
}
}
public void depthFirstSearch(int r) {
byte[] c = new byte[nVertices()];
Stack<Integer> s = new Stack<Integer>();
s.push(r);
while (!s.isEmpty()) {
int i = s.pop();
if (c[i] == WHITE) {
c[i] = GREY;
for (int j : outEdges(i))
s.push(j);
}
}
}
public static void main
(String[] args
) { AdjMatrixGraph graph = new AdjMatrixGraph(10);
graph.addEdge(1, 3);
graph.addEdge(2, 4);
graph.addEdge(6, 8);
graph.addEdge(3, 2);
System.
out.
println("Edge from 6 to 8 is : " + graph.
hasEdge(6,
8)); System.
out.
println("Edge from 2 to 4 is : " + graph.
hasEdge(2,
4)); System.
out.
println("Edge from 3 to 2 is : " + graph.
hasEdge(3,
2)); System.
out.
println("Edge from 1 to 3 is : " + graph.
hasEdge(1,
3));
graph.removeEdge(6, 8);
System.
out.
println("Edge from 6 to 8 is : " + graph.
hasEdge(6,
8));
}
}
aW1wb3J0IGphdmEudXRpbC5BcnJheUxpc3Q7CmltcG9ydCBqYXZhLnV0aWwuTGlua2VkTGlzdDsKaW1wb3J0IGphdmEudXRpbC5MaXN0OwppbXBvcnQgamF2YS51dGlsLlF1ZXVlOwppbXBvcnQgamF2YS51dGlsLlN0YWNrOwoKLyoqCiAqIEltcGxlbWVudGF0aW9uIG9mIGEgZGlyZWN0ZWQgdW53ZWlnaHRlZCBHcmFwaCB1c2luZyBBZGphY2VuY3kgTWF0cml4CiAqIAogKiBDb21wbGV4aXR5OiBhZGRFZGdlLCByZW1vdmVFZGdlLCBoYXNFZGdlIE8oMSkgLyBvdXRFZGdlcywgaW5FZGdlcyBPKG4pIC8KICogU3BhY2UgTyhuXjIpCiAqLwpjbGFzcyBBZGpNYXRyaXhHcmFwaCB7CglpbnQgc2l6ZTsKCXByaXZhdGUgYm9vbGVhbltdW10gbWF0cml4OwoJcHVibGljIHN0YXRpYyBmaW5hbCBieXRlIFdISVRFID0gMTsKCXB1YmxpYyBzdGF0aWMgZmluYWwgYnl0ZSBHUkVZID0gMjsKCglwdWJsaWMgQWRqTWF0cml4R3JhcGgoaW50IHNpemUpIHsKCQl0aGlzLnNpemUgPSBzaXplOwoJCXRoaXMubWF0cml4ID0gbmV3IGJvb2xlYW5bc2l6ZV1bc2l6ZV07Cgl9CgoJcHVibGljIHZvaWQgYWRkRWRnZShpbnQgbm9kZUEsIGludCBub2RlQikgewoJCW1hdHJpeFtub2RlQV1bbm9kZUJdID0gdHJ1ZTsKCX0KCglwdWJsaWMgdm9pZCByZW1vdmVFZGdlKGludCBub2RlQSwgaW50IG5vZGVCKSB7CgkJbWF0cml4W25vZGVBXVtub2RlQl0gPSBmYWxzZTsKCX0KCglwdWJsaWMgYm9vbGVhbiBoYXNFZGd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