class Graph {
private int[][] adjMat;
private Vertex[] vertices;
private int numVertices;
public Graph(int max) {
adjMat = new int[max][max];
vertices = new Vertex[max];
numVertices = 0;
for (int i = 0; i < max; i++) {
for (int j = 0; j < max; j++)
adjMat[i][j] = 0;
}
}
public void addVertex(Vertex v) {
v.setId(numVertices);
vertices[numVertices] = v;
numVertices += 1;
}
public void addEdge(Vertex src, Vertex dst) {
adjMat[src.getId()][dst.getId()] = 1;
adjMat[dst.getId()][src.getId()] = 1;
}
public void startDFS(Vertex start) {
Stack<Vertex> stack = new Stack();
stack.push(start);
Vertex current;
while (!stack.isEmpty()) {
current = stack.pop();
current.setVisited();
current.showLabel();
for (int i = 0; i < numVertices; i++) {
if (adjMat[current.getId()][i] == 1 && !vertices[i].isVisited())
stack.push(vertices[i]);
}
}
}
}
class Vertex {
private String label;
private int id;
private boolean isVisit;
public Vertex(String label) {
this.label = label;
this.isVisit = false;
this.id = -1;
}
public void showLabel() {
Log.i("kjw", "[" + label + "]");
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setVisited() {
this.isVisit = true;
}
public boolean isVisited() {
return isVisit;
}
}
'프로그래밍 > 알고리즘' 카테고리의 다른 글
Sort(countingSort, radixSort) (0) | 2017.02.24 |
---|---|
다익스트라(Dijkstra Algorithm) (0) | 2017.02.24 |
이진트리(BinaryTree) (0) | 2017.02.24 |
quickSort (0) | 2017.02.24 |
Queue (0) | 2017.02.24 |