Kelas Vertex :
public class Vertex {
public String label;
public boolean wasVisited;
public Vertex(String lab) {
label = lab;
wasVisited = false;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}// akhir kelas
Kelas Stack :
class StackX {
private final int SIZE = 20;
private int[] st;
private int top;
public StackX()
{
st = new int[SIZE]; // buat array
top = -1;
}
public void push(int j)
{
st[++top] = j;
}
public int pop()
{
return st[top--];
}
public int peek()
{
return st[top];
}
public boolean isEmpty()
{
return (top == -1);
}
}// akhir stack
Kelas Graph :
public class Graph {
private final int MAX_VERTEX = 10;
private int adjMat[][];
private Vertex vertexList[];
private int nVerts;
private StackX stack;
public Graph() {
vertexList = new Vertex[MAX_VERTEX];
adjMat = new int[MAX_VERTEX][MAX_VERTEX];
nVerts = 0;
for (int i = 0; i < MAX_VERTEX; i++) {
for (int k = 0; k < MAX_VERTEX; k++) {
adjMat[i][k] = 0;
}
}
stack = new StackX();
}// akhir konstruktor
public void addVertex(String lab) {
vertexList[nVerts++] = new Vertex(lab);
}
// Metode Menambahkan Lintasan
public void addEdge(String v1, String v2) {
adjMat[getVertexList(v1)][getVertexList(v2)] = 1;
adjMat[getVertexList(v2)][getVertexList(v1)] = 1;
}
public void display(int v) {
System.out.print(vertexList[v].label);
System.out.print(" ");
}
public int getVertexList(String data) {
int dataI = 0;
for (int i = 0; i < MAX_VERTEX; i++) {
if (vertexList[i].getLabel().equals(data)) {
return i;
} else if (vertexList[i].getLabel().equals(data)) {
return i;
} else if (vertexList[i].getLabel().equals(data)) {
return i;
} else if (vertexList[i].getLabel().equals(data)) {
return i;
} else if (vertexList[i].getLabel().equals(data)) {
return i;
} else if (vertexList[i].getLabel().equals(data)) {
return i;
} else if (vertexList[i].getLabel().equals(data)) {
return i;
} else if (vertexList[i].getLabel().equals(data)) {
return i;
} else if (vertexList[i].getLabel().equals(data)) {
return i;
} else if (vertexList[i].getLabel().equals(data)) {
return i;
} else if (vertexList[i].getLabel().equals(data)) {
return i;
} else if (vertexList[i].getLabel().equals(data)) {
return i;
}
}
return dataI;
}
public int[][] getAdjMat() {
return adjMat;
}
public void setAdjMat(int[][] adjMat) {
this.adjMat = adjMat;
}
public int getnVerts() {
return nVerts;
}
public void setnVerts(int nVerts) {
this.nVerts = nVerts;
}
public void dfs() {
vertexList[0].wasVisited = true;
display(0);
stack.push(0);
while (!stack.isEmpty()) {
int v = getAdjUnvisited(stack.peek());
if (v == -1) {
stack.pop();
} else {
vertexList[v].wasVisited = true;
display(v);
stack.push(v);
}
}// akhir while
for (int i = 0; i < nVerts; i++) {
vertexList[i].wasVisited = false;
}
}
public int getAdjUnvisited(int v) {
for (int i = 0; i < nVerts; i++) {
if (adjMat[v][i] == 1 && vertexList[i].wasVisited == false) {
return i;
}
}
return -1;
}
}
Kelas Main :
public class Main {
public static void main(String[] args) {
// TODO code application logic here
Graph graph = new Graph();
graph.addVertex("A");
graph.addVertex("B");
graph.addVertex("C");
graph.addVertex("D");
graph.addVertex("E");
graph.addVertex("F");
graph.addVertex("G");
graph.addVertex("H");
graph.addVertex("I");
graph.addEdge("A", "I");
graph.addEdge("A", "B");
graph.addEdge("A", "F");
graph.addEdge("B", "C");
graph.addEdge("B", "E");
graph.addEdge("F", "G");
graph.addEdge("E", "C");
graph.addEdge("E", "G");
graph.addEdge("C", "D");
graph.addEdge("D", "G");
graph.addEdge("D", "H");
int isi[][] = graph.getAdjMat();
for (int i = 0; i < graph.getnVerts(); i++) {
for (int j = 0; j < graph.getnVerts(); j++) {
System.out.print(isi[i][j] + " ");
}
System.out.print("\n");
}// akhiran for
System.out.println();
graph.dfs();
System.out.println();
}
}
Hasil Running Program :
Tidak ada komentar:
Posting Komentar