Download presentation
Presentation is loading. Please wait.
1
Breadth First Search & Depth-First Search
Kecerdasan Buatan Breadth First Search & Depth-First Search
2
Konsep yang perlu difahami sebelumnya:
Algoritma Searching Stack Queue Graph Object Oriented Programming
3
Graph
4
Contoh Implementasi Teori Graph
5
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.
6
Breadth First Search
7
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.
8
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
9
Breadth First Search - A B C D E F G H I front FIFO Queue
10
Breadth First Search A - enqueue source node FIFO Queue front A B C D
G H I enqueue source node A front FIFO Queue
11
Breadth First Search A - dequeue next vertex FIFO Queue front A B C D
G H I dequeue next vertex A front FIFO Queue
12
Breadth First Search - visit neighbors of A FIFO Queue front A B C D E
13
Breadth First Search - visit neighbors of A FIFO Queue front A B C D E
14
Breadth First Search B - A B discovered FIFO Queue front A B C D E F G
15
Breadth First Search B - A visit neighbors of A FIFO Queue front A B C
16
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
17
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
18
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
19
Breadth First Search I - A A visit neighbors of B FIFO Queue front A B
20
Breadth First Search I - A A visit neighbors of B FIFO Queue front A B
21
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
22
Breadth First Search I F - A B A visit neighbors of B FIFO Queue front
23
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
24
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
25
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
26
Breadth First Search F - A B A visit neighbors of I FIFO Queue front A
27
Breadth First Search F - A B A visit neighbors of I FIFO Queue front A
28
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
29
Breadth First Search F - A B A visit neighbors of I FIFO Queue front A
30
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
31
Breadth First Search F E - A I B A visit neighbors of I FIFO Queue
front FIFO Queue
32
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
33
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
34
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
35
Breadth First Search E - A I B A visit neighbors of F FIFO Queue front
36
Breadth First Search E G - A I B F A G discovered FIFO Queue front A B
37
Breadth First Search E G - A I B F A F finished FIFO Queue front A B C
38
Breadth First Search E G - A I B F A dequeue next vertex FIFO Queue
front FIFO Queue
39
Breadth First Search G - A I B F A visit neighbors of E FIFO Queue
front FIFO Queue
40
Breadth First Search G - A I B F A E finished FIFO Queue front A B C D
41
Breadth First Search G - A I B F A dequeue next vertex FIFO Queue
front FIFO Queue
42
Breadth First Search - A I B F A visit neighbors of G FIFO Queue front
43
Breadth First Search C - A G I B F A C discovered FIFO Queue front A B
44
Breadth First Search C - A G I B F A visit neighbors of G FIFO Queue
front FIFO Queue
45
Breadth First Search C H - A G I B F G A H discovered FIFO Queue front
46
Breadth First Search C H - A G I B F G A G finished FIFO Queue front A
47
Breadth First Search C H - A G I B F G A dequeue next vertex
front FIFO Queue
48
Breadth First Search H - A G I B F G A visit neighbors of C FIFO Queue
front FIFO Queue
49
Breadth First Search H D - A G C I B F G A D discovered FIFO Queue
front FIFO Queue
50
Breadth First Search H D - A G C I B F G A C finished FIFO Queue front
51
Breadth First Search H D - A G C I B F G A get next vertex FIFO Queue
front FIFO Queue
52
Breadth First Search D - A G C I B F G A visit neighbors of H
front FIFO Queue
53
Breadth First Search D - A G C I B F G A finished H FIFO Queue front A
54
Breadth First Search D - A G C I B F G A dequeue next vertex
front FIFO Queue
55
Breadth First Search - A G C I B F G A visit neighbors of D FIFO Queue
front FIFO Queue
56
Breadth First Search - A G C I B F G A D finished FIFO Queue front A B
57
Breadth First Search - A G C I B F G A dequeue next vertex FIFO Queue
front FIFO Queue
58
Breadth First Search - A G C I B F G A STOP FIFO Queue front A B C D E
59
Implementasi BFS dengan menggunakan Java
60
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
61
Depth First Search
62
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
63
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.
64
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
65
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
66
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
67
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)
68
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
69
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
70
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
71
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
72
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
73
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
74
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.
75
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
76
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.
77
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
78
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.
79
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
80
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).
81
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).
82
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).
83
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).
84
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).
85
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.
86
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
87
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.
88
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.
89
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.
90
Implementasi DFS dengan menggunakan Java
91
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)
92
Which Better? BFS DFS L M N O P G Q H J I K F E D B C A
93
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
94
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?
95
Latihan Lakukan proses traverse utk struktur graf berikut dengan menggunakan: BFS DFS Dimulai dari vertex 0
96
Kuliah AI Pekan 1 Pekan 2
97
Akhirnya selesai….. Ada pertanyaan?
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.