Breadth First Search & Depth-First Search Kecerdasan Buatan Breadth First Search & Depth-First Search
Konsep yang perlu difahami sebelumnya: Algoritma Searching Stack Queue Graph Object Oriented Programming
Graph
Contoh Implementasi Teori Graph
Contoh Implementasi Teori Graph Facebook's example Graph Search queries, which is to find all of the Sushi restaurants in New York that my friends like.
Breadth First Search
Breadth First Search BFS (Breadth First Search) merupakan salah satu algoritma penelusuran struktur graf / pohon BFS melakukan pencarian secara melebar atau per level pohon. Simpul ditelusuri dari root kemudian menelusuri semua simpul pada setiap level di bawahnya ( misalnya prioritas penelusuran dari kiri ke kanan ), maka penelusuran dilakukan terus dari simpul paling kiri ke simpul anak – anak tetangganya yang selevel.
Algoritma Breadth First Search Masukkan simpul root ke dalam antrian (Queue) Periksa antrian terdepan apakah memiliki anak simpul Jika ya, masukan semua anak simpul ke dalam antrian Hapus antrian terdepan Jika antrian kosong berhenti, tapi jika tidak kembali ke langkah dua
Breadth First Search - A B C D E F G H I front FIFO Queue
Breadth First Search A - enqueue source node FIFO Queue front A B C D G H I enqueue source node A front FIFO Queue
Breadth First Search A - dequeue next vertex FIFO Queue front A B C D G H I dequeue next vertex A front FIFO Queue
Breadth First Search - visit neighbors of A FIFO Queue front A B C D E
Breadth First Search - visit neighbors of A FIFO Queue front A B C D E
Breadth First Search B - A B discovered FIFO Queue front A B C D E F G
Breadth First Search B - A visit neighbors of A FIFO Queue front A B C
Breadth First Search B I - A A I discovered FIFO Queue front A B C D E G H I A I discovered B I front FIFO Queue
Breadth First Search B I - A A finished with A FIFO Queue front A B C G H I A finished with A B I front FIFO Queue
Breadth First Search B I - A A dequeue next vertex FIFO Queue front A G H I A dequeue next vertex B I front FIFO Queue
Breadth First Search I - A A visit neighbors of B FIFO Queue front A B
Breadth First Search I - A A visit neighbors of B FIFO Queue front A B
Breadth First Search I F - A B A F discovered FIFO Queue front A B C D G H B I A F discovered I F front FIFO Queue
Breadth First Search I F - A B A visit neighbors of B FIFO Queue front
Breadth First Search I F - A B A A already discovered FIFO Queue front G H B I A A already discovered I F front FIFO Queue
Breadth First Search I F - A B A finished with B FIFO Queue front A B G H B I A finished with B I F front FIFO Queue
Breadth First Search I F - A B A dequeue next vertex FIFO Queue front G H B I A dequeue next vertex I F front FIFO Queue
Breadth First Search F - A B A visit neighbors of I FIFO Queue front A
Breadth First Search F - A B A visit neighbors of I FIFO Queue front A
Breadth First Search F - A B A A already discovered FIFO Queue front A G H B I A A already discovered F front FIFO Queue
Breadth First Search F - A B A visit neighbors of I FIFO Queue front A
Breadth First Search F E - A I B A E discovered FIFO Queue front A B C G H I B I A E discovered F E front FIFO Queue
Breadth First Search F E - A I B A visit neighbors of I FIFO Queue front FIFO Queue
Breadth First Search F E - A I B A F already discovered FIFO Queue G H I B I A F already discovered F E front FIFO Queue
Breadth First Search F E - A I B A I finished FIFO Queue front A B C D G H I B I A I finished F E front FIFO Queue
Breadth First Search F E - A I B A dequeue next vertex FIFO Queue G H I B I A dequeue next vertex F E front FIFO Queue
Breadth First Search E - A I B A visit neighbors of F FIFO Queue front
Breadth First Search E G - A I B F A G discovered FIFO Queue front A B
Breadth First Search E G - A I B F A F finished FIFO Queue front A B C
Breadth First Search E G - A I B F A dequeue next vertex FIFO Queue front FIFO Queue
Breadth First Search G - A I B F A visit neighbors of E FIFO Queue front FIFO Queue
Breadth First Search G - A I B F A E finished FIFO Queue front A B C D
Breadth First Search G - A I B F A dequeue next vertex FIFO Queue front FIFO Queue
Breadth First Search - A I B F A visit neighbors of G FIFO Queue front
Breadth First Search C - A G I B F A C discovered FIFO Queue front A B
Breadth First Search C - A G I B F A visit neighbors of G FIFO Queue front FIFO Queue
Breadth First Search C H - A G I B F G A H discovered FIFO Queue front
Breadth First Search C H - A G I B F G A G finished FIFO Queue front A
Breadth First Search C H - A G I B F G A dequeue next vertex front FIFO Queue
Breadth First Search H - A G I B F G A visit neighbors of C FIFO Queue front FIFO Queue
Breadth First Search H D - A G C I B F G A D discovered FIFO Queue front FIFO Queue
Breadth First Search H D - A G C I B F G A C finished FIFO Queue front
Breadth First Search H D - A G C I B F G A get next vertex FIFO Queue front FIFO Queue
Breadth First Search D - A G C I B F G A visit neighbors of H front FIFO Queue
Breadth First Search D - A G C I B F G A finished H FIFO Queue front A
Breadth First Search D - A G C I B F G A dequeue next vertex front FIFO Queue
Breadth First Search - A G C I B F G A visit neighbors of D FIFO Queue front FIFO Queue
Breadth First Search - A G C I B F G A D finished FIFO Queue front A B
Breadth First Search - A G C I B F G A dequeue next vertex FIFO Queue front FIFO Queue
Breadth First Search - A G C I B F G A STOP FIFO Queue front A B C D E
Implementasi BFS dengan menggunakan Java
Pemanfaatan Algoritma BFS dalam dunia nyata Shortest Path Peer to Peer Networks. (ex: BitTorrent, is used to find all neighbor nodes.) Crawlers in Search Engines Social Networking Websites GPS Navigation systems Broadcasting in Network Path Finding
Depth First Search
Penjelasan DFS DFS(G) DFS-VISIT(G, u) for each uV do color[u] gray color[u] white [u] NIL time 0 if color[u] white then DFS-VISIT(G, u) DFS-VISIT(G, u) color[u] gray d[u] time time 1 for each v Adj[u] do if color[v] white then [v] u DFS-VISIT(G, v) color[u] black f[u] time time 1
Penjelasan DFS DFS (Depth-First-Search) adalah salah satu algoritma penelusuran struktur graf / pohon berdasarkan kedalaman. Simpul ditelusuri dari root kemudian ke salah satu simpul anaknya ( misalnya prioritas penelusuran berdasarkan anak pertama [simpul sebelah kiri] ), maka penelusuran dilakukan terus melalui simpul anak pertama dari simpul anak pertama level sebelumnya hingga mencapai level terdalam. Setelah sampai di level terdalam, penelusuran akan kembali ke 1 level sebelumnya untuk menelusuri simpul anak kedua pada pohon biner [simpul sebelah kanan] lalu kembali ke langkah sebelumnya dengan menelusuri simpul anak pertama lagi sampai level terdalam dan seterusnya.
Algoritma DFS Masukkan simpul root ke dalam tumpukan dengan push Ambil dan simpan isi elemen (berupa simpul pohon) dari tumpukan teratas Hapus isi stack teratas dengan prosedur pop Periksa apakah simpul pohon yang disimpan tadi memiliki anak simpul Jika ya, push semua anak simpul yang dibangkitkan ke dalam stack Jika tumpukan kosong berhenti, tapi jika tidak kembali ke langkah dua
Task: Conduct a depth-first search of the graph starting with node D Walk-Through Visited Array F C A B C D E F G H A B D H G E Task: Conduct a depth-first search of the graph starting with node D
Walk-Through F C A B D H G E The order nodes are visited: D Visit D Visited Array F C A B C D √ E F G H D A B D H G E The order nodes are visited: D Visit D
Walk-Through F C A B D H G E The order nodes are visited: D Visited Array F C A B C D √ E F G H D A B D H G E The order nodes are visited: D Consider nodes adjacent to D, decide to visit C first (Rule: visit adjacent nodes in alphabetical order)
Walk-Through F C A B D H G E The order nodes are visited: D, C Visit C Visited Array F C A B C √ D E F G H C D A B D H G E The order nodes are visited: D, C Visit C
Walk-Through F C A B D H G E The order nodes are visited: D, C Visited Array F C A B C √ D E F G H C D A B D H G E The order nodes are visited: D, C No nodes adjacent to C; cannot continue backtrack, i.e., pop stack and restore previous state
Back to D – C has been visited, decide to visit E next Walk-Through Visited Array F C A B C √ D E F G H D A B D H G E The order nodes are visited: D, C Back to D – C has been visited, decide to visit E next
Back to D – C has been visited, decide to visit E next Walk-Through Visited Array F C A B C √ D E F G H E D A B D H G E The order nodes are visited: D, C, E Back to D – C has been visited, decide to visit E next
Walk-Through F C A B D H G E The order nodes are visited: D, C, E Visited Array F C A B C √ D E F G H E D A B D H G E The order nodes are visited: D, C, E Only G is adjacent to E
Walk-Through F C A B D H G E The order nodes are visited: D, C, E, G Visited Array F C A B C √ D E F G H G E D A B D H G E The order nodes are visited: D, C, E, G Visit G
Walk-Through F C A B D H G E The order nodes are visited: D, C, E, G Visited Array F C A B C √ D E F G H G E D A B D H G E The order nodes are visited: D, C, E, G Nodes D and H are adjacent to G. D has already been visited. Decide to visit H.
Walk-Through F C A B D H G E The order nodes are visited: Visited Array F C A B C √ D E F G H H G E D A B D H G E The order nodes are visited: D, C, E, G, H Visit H
Nodes A and B are adjacent to F. Decide to visit A next. Walk-Through Visited Array F C A B C √ D E F G H H G E D A B D H G E The order nodes are visited: D, C, E, G, H Nodes A and B are adjacent to F. Decide to visit A next.
Walk-Through F C A B D H G E The order nodes are visited: Visited Array F C A √ B C D E F G H A H G E D A B D H G E The order nodes are visited: D, C, E, G, H, A Visit A
Only Node B is adjacent to A. Decide to visit B next. Walk-Through Visited Array F C A √ B C D E F G H A H G E D A B D H G E The order nodes are visited: D, C, E, G, H, A Only Node B is adjacent to A. Decide to visit B next.
Walk-Through F C A B D H G E The order nodes are visited: Visited Array F C A √ B C D E F G H B A H G E D A B D H G E The order nodes are visited: D, C, E, G, H, A, B Visit B
No unvisited nodes adjacent to B. Backtrack (pop the stack). Walk-Through Visited Array F C A √ B C D E F G H A H G E D A B D H G E The order nodes are visited: D, C, E, G, H, A, B No unvisited nodes adjacent to B. Backtrack (pop the stack).
No unvisited nodes adjacent to A. Backtrack (pop the stack). Walk-Through Visited Array F C A √ B C D E F G H H G E D A B D H G E The order nodes are visited: D, C, E, G, H, A, B No unvisited nodes adjacent to A. Backtrack (pop the stack).
No unvisited nodes adjacent to H. Backtrack (pop the stack). Walk-Through Visited Array F C A √ B C D E F G H G E D A B D H G E The order nodes are visited: D, C, E, G, H, A, B No unvisited nodes adjacent to H. Backtrack (pop the stack).
No unvisited nodes adjacent to G. Backtrack (pop the stack). Walk-Through Visited Array F C A √ B C D E F G H E D A B D H G E The order nodes are visited: D, C, E, G, H, A, B No unvisited nodes adjacent to G. Backtrack (pop the stack).
No unvisited nodes adjacent to E. Backtrack (pop the stack). Walk-Through Visited Array F C A √ B C D E F G H D A B D H G E The order nodes are visited: D, C, E, G, H, A, B No unvisited nodes adjacent to E. Backtrack (pop the stack).
F is unvisited and is adjacent to D. Decide to visit F next. Walk-Through Visited Array F C A √ B C D E F G H D A B D H G E The order nodes are visited: D, C, E, G, H, A, B F is unvisited and is adjacent to D. Decide to visit F next.
Walk-Through F C A B D H G E The order nodes are visited: Visited Array F C A √ B C D E F G H F D A B D H G E The order nodes are visited: D, C, E, G, H, A, B, F Visit F
No unvisited nodes adjacent to F. Backtrack. Walk-Through Visited Array F C A √ B C D E F G H D A B D H G E The order nodes are visited: D, C, E, G, H, A, B, F No unvisited nodes adjacent to F. Backtrack.
No unvisited nodes adjacent to D. Backtrack. Walk-Through Visited Array F C A √ B C D E F G H A B D H G E The order nodes are visited: D, C, E, G, H, A, B, F No unvisited nodes adjacent to D. Backtrack.
Stack is empty. Depth-first traversal is done. Walk-Through Visited Array F C A √ B C D E F G H A B D H G E The order nodes are visited: D, C, E, G, H, A, B, F Stack is empty. Depth-first traversal is done.
Implementasi DFS dengan menggunakan Java
Pemanfaatan DFS pada dunia nyata Detecting cycle in a graph Path Finding Topological Sorting Finding Strongly Connected Components of a graph Solving puzzles with only one solution Game (Chess, Tictactoe, etc)
Which Better? BFS DFS L M N O P G Q H J I K F E D B C A
BFS DFS Queue (recursion no help) Can be used to find shortest paths from the start vertex Can be used to find short alternating paths for matching Stack or recursion Many applications
Depth- vs. breadth-first searching When a breadth-first search succeeds, it finds a minimum-depth (nearest the root) goal node A separate mechanism is needed to keep track of the path to the goal node When a depth-first search succeeds, the path to the goal node is on the stack The found goal node is not necessarily minimum depth For a large tree, breadth-first search memory requirements may be excessive For a large tree, a depth-first search may take an excessively long time to find even a very nearby goal node How can we combine the advantages (and avoid the disadvantages) of these two search techniques?
Latihan Lakukan proses traverse utk struktur graf berikut dengan menggunakan: BFS DFS Dimulai dari vertex 0
Kuliah AI Pekan 1 Pekan 2
Akhirnya selesai….. Ada pertanyaan?