Pertemuan 22 Graph Operation Matakuliah : T0026/Struktur Data Tahun : 2005 Versi : 1/1 Pertemuan 22 Graph Operation
Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Mahasiswa dapat menghasilkan program modular untuk mengimplementasikan graph operation dan spanning tree
Pengertian graph operation Graph traversal Depth First Search Outline Materi Pengertian graph operation Graph traversal Depth First Search Breadth First Search Contoh operasi DFS dan DFS Spanning Tree Contoh konstruksi Spanning Tree
Depth First Search (DFS) Bread First Search (BFS) Graph Traversal Depth First Search (DFS) Algoritma akan memanfaatkan stack Bread First Search (BFS) Algoritma akan memanfaatkan queue Pembahasan DFS dan BFS akan menggunakan representasi Adjacency List Pada saat mengunjungi sebuah node, nama node akan dicetak
Graph – DEPTH FIRST SEARCH Algoritma DFS : For (all v in G) visited[v] = FALSE; void dfs (int v) { node_pointer w; visited[v] = TRUE; printf (“%d”, v); for (w = graph[v]; w; w=w->link) if (!visited[w->vertex]) dfs(w->vertex); }
Graph – BREADTH FIRST SEARCH (BFS) Algoritma BFS: void BFS(G){ for (all v in G) { if (visited[v] = FALSE){ EnQueue(v,Queue); do { DeQueue(v, Queue); visited[v] = TRUE; Visit(v); for (all w adjacent to v) if (visited(w)= FALSE) EnQueue(w,Queue); } while (!Empty(Queue)); }
Graph – Contoh(1) Vo V1 V2 V3 V4 V5 V6 V7 1 2 3 4 5 6 7 Adjacency List : perhatikan urutan node pd edge list sesuai dengan urutan head node list Vo V1 V2 V3 V4 V5 V6 V7 1 2 3 4 5 6 7
v Stack Status visited Printout vo Empty v1 v2 v3 v2 v4 v7 v4 v2 v4 v5 Graph – Contoh(1) - DEPTH FIRST SEARCH (DFS) v Stack Status visited Printout vo Empty v1 v2 v3 v2 v4 v7 v4 v2 v4 v5 v5 v2 v4 v7 v6 - empty
Graph – Contoh(1) – Hasil DFS dan BFS Hasil BFS Vo V1 V2 V3 V4 V5 V6 V7 Vo V1 V2 V3 V4 V5 V6 V7
Graph – BREADTH FIRST SEARCH (BFS) v w Queue Printout Status visited vo v1 v2 v1 v2 v0 v3 v2 v3 v4 v2 v3 v4 v3 v4 v5 v3 v4 v5 v6 v3 v4 v5 v6 v4 v5 v6 v7 v4 v5 v6 v7 v5 v6 v7
Graph – BREADTH FIRST SEARCH (BFS) v w Queue Printout Status visited v4 v1 v5 v6 v7 v7 v5 v2 v6 v7 v6 v3 empty
Graph – Contoh 2 A B F C E D A B C E D F
Graph – Contoh 2 - DFS verteks Stack Status visited Printout A B C C E E D C E F F
Graph – Contoh(2) - BFS v w Queue Printout Status visited A B C B C D C D E F C D E F - empty
Graph – Contoh(2) – Hasil DFS dan BFS Hasil DFS Hasil BFS A B F C E D A B F C E D
Graph – Contoh 3 A B E C F D G H A B C E F D G H
Graph – Contoh(3) - DEPTH FIRST SEARCH (DFS) verteks Stack Status visited Printout A empty B C C E F D C E E G C E E H H E -
Graph – Contoh(3) - BREADTH FIRST SEARCH (BFS) v w Queue Printout Status visited A B C B C D B C D C D E C D E F C D E F D E F E F G H
Graph – Contoh(3) - DEPTH FIRST SEARCH (DFS) Hasil DFS Hasil BFS A B E C F D G H A B E C F D G H