You are on page 1of 6

PRAKTIKUM ALGORITMA DAN STRUKTUR DATA MODUL KE-6 GRAPH

LABORATORIUM PEMROGRAMAN PROGRAM STUDI TEKNIK INFORMATIKA FAKULTAS TEKNIK UNIVERSITAS MUHAMMADIYAH MALANG 2011

I. TUJUAN
Mahasiswa mampu : 1. Memahami dan mampu menggunakan ADT Graph

II. ALAT YANG DIGUNAKAN


Peralatan yang digunakan : 1. Perangkat PC yang terinstall Java 2. Editor Java

III. DASAR TEORI


(Disesuaikan dengan yang telah diberikan di kelas) Representasi graph Penelusuran graph (graph traversal)

IV. PROSEDUR PELAKSANAAN


Prosedur pelaksanaan praktikum adalah sebagai berikut : 1. Mahasiswa mencoba latihan yang ada pada modul praktikum 2. Mahasiswa menganalisa hasil dari program pada latihan yang telah dijalankan 3. Mahasiswa mengerjakan tugas yang diberikan 4. Mahasiswa mendemonstrasikan program yang telah dikerjakan pada dosen/assisten 5. Mahasiswa membuat laporan dari tugas yang telah dikerjakan 6. Upload laporan melalui e-labit.umm.ac.id

V. LATIHAN
1. Contoh program Graph.java menggunakan adjacency matrix.
public class AdjacencyMatriksGraph { private final int MAX_VERTS = 20; private Vertex vertexList[]; // list of vertices private int adjMat[][]; // adjacency matrix private int nVerts; // current number of vertices private StackX theStack; private Queue theQueue; public AdjacencyMatriksGraph() // constructor { vertexList = new Vertex[MAX_VERTS]; // adjacency matrix adjMat = new int[MAX_VERTS][MAX_VERTS]; nVerts = 0; for(int j=0; j<MAX_VERTS; j++) // set adjacency for(int k=0; k<MAX_VERTS; k++) // matrix to 0 adjMat[j][k] = 0; theStack = new StackX(); theQueue = new Queue(); }

public void dfs() // depth-first search { // begin at vertex 0 vertexList[0].wasVisited = true; // mark it displayVertex(0); // display it theStack.push(0); // push it while( !theStack.isEmpty() ) // until stack empty, { // get an unvisited vertex adjacent to stack top int v = getAdjUnvisitedVertex( theStack.peek() ); if(v == -1) // if no such vertex, theStack.pop(); else // if it exists, { vertexList[v].wasVisited = true; // mark it displayVertex(v); // display it theStack.push(v); // push it } } // end while // stack is empty, so we're done for(int j=0; j<nVerts; j++) // reset flags vertexList[j].wasVisited = false; } public void bfs() // breadth-first search { // begin at vertex 0 vertexList[0].wasVisited = true; // mark it displayVertex(0); // display it theQueue.insert(0); // insert at tail int v2; while( !theQueue.isEmpty() ) // until queue empty, { int v1 = theQueue.remove(); // remove vertex at head // until it has no unvisited neighbors while( (v2=getAdjUnvisitedVertex(v1)) != -1 ) { // get one, vertexList[v2].wasVisited = true; // mark it displayVertex(v2); // display it theQueue.insert(v2); // insert it } // end while } // end while(queue not empty) // queue is empty, so we're done for(int j=0; j<nVerts; j++) // reset flags vertexList[j].wasVisited = false; } public void addVertex(char lab) { vertexList[nVerts++] = new Vertex(lab); } public void addEdge(int start, int end) { adjMat[start][end] = 1; adjMat[end][start] = 1; } public void displayVertex(int v)

{ System.out.print(vertexList[v].label); } 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; } public static void main(String[] args) { AdjacencyMatriksGraph theGraph = new AdjacencyMatriksGraph(); theGraph.addVertex('4');//0 theGraph.addVertex('3');//1 theGraph.addVertex('1');//2 theGraph.addVertex('2');//3 theGraph.addEdge(0,1); theGraph.addEdge(1,2); theGraph.addEdge(1,3); System.out.print("Visits bfs: "); theGraph.bfs(); // depth-first search System.out.println(); System.out.print("Visits dfs: "); theGraph.dfs(); // depth-first search System.out.println(); } // end main() } 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; } public void insert(int j) // put item at rear of queue { if(rear == SIZE-1) rear = -1; queArray[++rear] = j; } public int remove() // take item from front of queue { int temp = queArray[front++]; if(front == SIZE) front = 0;

return temp; } public boolean isEmpty() // true if queue is empty { return ( rear+1==front || (front+SIZE-1==rear) ); } } class StackX { private final int SIZE = 20; private int[] st; private int top; // ----------------------------------------------------------public StackX() // constructor { st = new int[SIZE]; // make array top = -1; } public void push(int j) // put item on stack { st[++top] = j; } public int pop() // take item off stack { return st[top--]; } public int peek() // peek at top of stack { return st[top]; } public boolean isEmpty() // true if nothing on stack{ return (top == -1); } } class Vertex { public char label; // label (e.g. 'A') public boolean wasVisited; // ------------------------------------------------------------public Vertex(char lab) // constructor { label = lab; wasVisited = false; } }

VI. TUGAS PRAKTIKUM


1. [2 orang] Buatlah flowchart dan program untuk pencarian path/jalur dari titik awal ke titik tujuan. Misalkan dari graph dibawah ini akan dicari jalur/path dari node 1 ke node 8. Jika diasumsikan node 2 dikunjungi terlebih dahulu daripada node 4, maka akan didapatkan hasil : 1-2-5-9-8. Dalam contoh ini node 1 adalah titik awal yang akan menjadi root. Sedangkan node 8 adalah titik tujuan-nya. Proses visitasi atau kunjungan

node akan berhenti jika telah didapatkan node yang sesuai dengan node tujuan. Untuk memudahkan pengerjaan gunakan konsep penelusuran DFS. 2. [3 orang] Buatlah flowchart dan program pengembangan dari soal nomor 1 untuk mendapatkan shortest path yaitu digunakan untuk mencari path/jalur yang memiliki bobot paling kecil. Dari graph dibawah ini misalkan dicari jalur dari titik 1 ke titik 8. Maka jalur pilihan ada 2 yaitu, jalur 1 : 1-2-5-9-8 (bobot = 20) dan jalur 2 : 1-4-6-7-5-98 (bobot = 28). Sehingga yang akan ditampilkan menjadi jalur alternatif adalah jalur ke1 : 1-2-5-9-8. Jalur yang tidak menuju ke titik tujuan tidak perlu disimpan. Bobot jalur 1 didapat dari : 4+8+4+4 Bobot jalur 2 didapat dari : 2+5+7+6+4+4

VII. HASIL PRAKTIKUM


[Tuliskan hasil tugas praktikum di sini]

VIII.

KESIMPULAN

You might also like