Breadth First Search & Depth-First Search

Slides:



Advertisements
Similar presentations
Breadth First Search AB F I EH DC G FIFO Queue - front.
Advertisements

LIST ITEM. List Style … Pop List Ditampilkan dalam bentuk field dengan tombol di sebelah kanan. Ketika tombol di klik maka sekumpulan data akan muncul.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
1 Breadth First Search AB F I EH DC G FIFO Queue - front.
Breadth First Search (BFS) Part 2 COMP171. Graph / Slide 2 Shortest Path Recording * BFS we saw only tells us whether a path exists from source s, to.
Tree Searching Breadth First Search Dept First Search.
1 Subgraphs A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Chapter 05 Introduction to Graph And Search Algorithms.
Backtracking Algorithm Depth-First Search Text Read Weiss, § 9.6 Depth-First Search and § 10.5 Backtracking Algorithms.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Introduction to Graph Theory HKOI Training (Intermediate) Kelly Choi 29 Mar 2008.
Introduction to Algorithms
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs A New Data Structure
Graphs CSE 2320 – Algorithms and Data Structures Alexandra Stefan
Graphs Breadth First Search & Depth First Search
Topological Sorting.
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Data Structures Graphs - Terminology
Graphs-Part II Depth First Search (DFS)
CSC317 Graph algorithms Why bother?
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
Graph Searching.
CC 215 Data Structures Graph Searching
Graphs Breadth First Search & Depth First Search
Depth First Search—Backtracking
Graph: representation and traversal CISC4080, Computer Algorithms
CS120 Graphs.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graph.
Binhai Zhu Computer Science Department, Montana State University
Elementary Graph Algorithms
Graph Representation Adjacency list representation of G = (V, E)
Graph & BFS.
Finding Shortest Paths
BFS,DFS Topological Sort
BFS,DFS Topological Sort
Breadth First Search 11/21/ s
Search Related Algorithms
Graph Representation (23.1/22.1)
Chapter 22: Elementary Graph Algorithms
Graph Traversals Depth-First Traversals. Algorithms. Example.
Tree Searching.
CSE 373 Data Structures Lecture 16
Spanning Trees Longin Jan Latecki Temple University based on slides by
Decrease and Conquer Decrease and conquer technique Insertion sort
Graphs CSE 2320 – Algorithms and Data Structures Alexandra Stefan
COMP171 Depth-First Search.
Breadth First Search - A B C D E F G H I front FIFO Queue.
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Depth-First Search CSE 2011 Winter April 2019.
Algorithms Searching in a Graph.
Algorithms: Design and Analysis
Lecture 6 Graph Traversal
Depth-First Search CSE 2011 Winter April 2019.
Tree Searching.
Tree Searching.
Tree Searching.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Elementary Graph Algorithms
Analysis and design of algorithm
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

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 uV 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?