Download - Implementasi Program
![Page 1: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/1.jpg)
Implementasi Program
Teknik Informatika - Universitas Muhammadiyah Malang (UMM)Tahun Akademik 2010-2011
Oleh : Nur Hayatin, S.ST
![Page 2: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/2.jpg)
Class Vertex
![Page 3: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/3.jpg)
Class Vertex
class Vertex{public char label; // label (e.g. 'A')public boolean wasVisited;// -------------------------------------------------------------public Vertex(char lab) // constructor{label = lab;wasVisited = false;}}
![Page 4: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/4.jpg)
Add Vertex
public void addVertex(char lab){vertexList[nVerts++] = new Vertex(lab);}
![Page 5: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/5.jpg)
Add Edge
public void addEdge(int start, int end){adjMat[start][end] = 1;adjMat[end][start] = 1;}
![Page 6: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/6.jpg)
Display Vertex
public void displayVertex(int v){System.out.print(vertexList[v].label);}
![Page 7: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/7.jpg)
Get Adjacency Unvisited Vertex
public int getAdjUnvisitedVertex(int v){for(int j=0; j<nVerts; j++)if(adjMat[v][j]==1 && vertexList[j].wasVisited==false)return j;return -1;}
![Page 8: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/8.jpg)
Class BFS
![Page 9: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/9.jpg)
Class BFS
public class BFS{private final int MAX_VERTS = 20;private Vertex vertexList[]; // list of verticesprivate int adjMat[][]; // adjacency matrixprivate int nVerts; // current number of verticesprivate Queue theQueue;
![Page 10: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/10.jpg)
Constructor
public BFS() // constructor{vertexList = new Vertex[MAX_VERTS];// adjacency matrixadjMat = new int[MAX_VERTS][MAX_VERTS];nVerts = 0;for(int j=0; j<MAX_VERTS; j++) // set adjacencyfor(int k=0; k<MAX_VERTS; k++) // matrix to 0adjMat[j][k] = 0;theQueue = new Queue();}
![Page 11: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/11.jpg)
public void bfs() // breadth-first search{ // begin at vertex 0vertexList[0].wasVisited = true; // mark itdisplayVertex(0); // display ittheQueue.insert(0); // insert at tailint v2;while( !theQueue.isEmpty() ) // until queue empty,{int v1 = theQueue.remove(); // remove vertex at head// until it has no unvisited neighborswhile( (v2=getAdjUnvisitedVertex(v1)) != -1 ){ // get one,vertexList[v2].wasVisited = true; // mark itdisplayVertex(v2); // display ittheQueue.insert(v2); // insert it} // end while} // end while(queue not empty)// queue is empty, so we're donefor(int j=0; j<nVerts; j++) // reset flagsvertexList[j].wasVisited = false;}
![Page 12: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/12.jpg)
Main Method
public static void main(String[] args) { BFS theGraph = new BFS(); theGraph.addVertex('a'); // 0 (start for dfs) theGraph.addVertex('b'); // 1 theGraph.addVertex('c'); // 2 theGraph.addVertex('d'); // 3 theGraph.addEdge(0,1); // AB theGraph.addEdge(0,2); // AC theGraph.addEdge(0,3); // AD theGraph.addEdge(1,3); // BD System.out.print("Visits: "); theGraph.bfs(); // breadth-first search System.out.println(); } // end main()
![Page 13: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/13.jpg)
Class Queue
![Page 14: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/14.jpg)
Class Queue & Constructor
class Queue{private final int SIZE = 20;private int[] queArray;private int front;private int rear;public Queue() // constructor{queArray = new int[SIZE];front = 0;rear = -1;}
![Page 15: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/15.jpg)
Insert Queue
public void insert(int j) // put item at rear of queue{if(rear == SIZE-1)rear = -1;queArray[++rear] = j;}
![Page 16: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/16.jpg)
Remove Queue
public int remove() // take item from front of queue{int temp = queArray[front++];if(front == SIZE)front = 0;return temp;
}
![Page 17: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/17.jpg)
isEmpty
public boolean isEmpty() // true if queue is empty{return ( rear+1==front || (front+SIZE-1==rear) );}
![Page 18: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/18.jpg)
Class DFS
![Page 19: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/19.jpg)
Class DFS
public class DFS{private final int MAX_VERTS = 20;private Vertex vertexList[]; // list of verticesprivate int adjMat[][]; // adjacency matrixprivate int nVerts; // current number of verticesprivate StackX theStack;
![Page 20: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/20.jpg)
Constructor
public DFS() // constructor{vertexList = new Vertex[MAX_VERTS];// adjacency matrixadjMat = new int[MAX_VERTS][MAX_VERTS];nVerts = 0;for(int j=0; j<MAX_VERTS; j++) // set adjacencyfor(int k=0; k<MAX_VERTS; k++) // matrix to 0adjMat[j][k] = 0;theStack = new StackX();}
![Page 21: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/21.jpg)
public void dfs() // depth-first search{ // begin at vertex 0vertexList[0].wasVisited = true; // mark itdisplayVertex(0); // display ittheStack.push(0); // push itwhile( !theStack.isEmpty() ) // until stack empty,{// get an unvisited vertex adjacent to stack topint v = getAdjUnvisitedVertex( theStack.peek() );if(v == -1) // if no such vertex,theStack.pop();else // if it exists,{vertexList[v].wasVisited = true; // mark itdisplayVertex(v); // display ittheStack.push(v); // push it}} // end while// stack is empty, so we're donefor(int j=0; j<nVerts; j++) // reset flagsvertexList[j].wasVisited = false;}
![Page 22: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/22.jpg)
Main Method
public static void main(String[] args) { DFS theGraph = new DFS(); theGraph.addVertex('a'); // 0 (start for dfs) theGraph.addVertex('b'); // 1 theGraph.addVertex('c'); // 2 theGraph.addVertex('d'); // 3 theGraph.addEdge(0,1); // AB theGraph.addEdge(0,2); // AC theGraph.addEdge(0,3); // AD theGraph.addEdge(1,3); // BD System.out.print("Visits: "); theGraph.dfs(); // depth-first search System.out.println(); } // end main()
![Page 23: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/23.jpg)
Class Stack
![Page 24: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/24.jpg)
Class Queue & Constructor
class StackX{private final int SIZE = 20;private int[] st;private int top;// -----------------------------------------------------------public StackX() // constructor{st = new int[SIZE]; // make arraytop = -1;}
![Page 25: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/25.jpg)
Push Stack
public void push(int j) // put item on stack{ st[++top] = j; }
![Page 26: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/26.jpg)
Pop Stack
public int pop() // take item off stack{ return st[top--]; }
![Page 27: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/27.jpg)
Peek Stack
public int peek() // peek at top of stack{ return st[top]; }
![Page 28: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/28.jpg)
isEmpty
public boolean isEmpty() // true if nothing on stack-{ return (top == -1); }
![Page 29: Implementasi Program](https://reader035.vdocument.in/reader035/viewer/2022062217/56812b85550346895d8fa2e0/html5/thumbnails/29.jpg)
Pustaka
• Sartaj Sahni , “Data Structures & Algorithms”, Presentation L20-24.
• Mitchell Waite, “Data Structures & Algorithms in Java”, SAMS, 2001