PPT 07 Pendekatan BruteForce dan Exhaustive Search Bag
PPT 07 – Pendekatan Brute-Force dan Exhaustive Search (Bag 3)
Graph Traversal § Many problems require processing all graph vertices in a systematic fashion § Graph traversal algorithms: • Depth-first search • Breadth-first search
Some Terminologies § Vertices and Edges § Undirected and directed graphs § Parents, children, and ancestors. § Connected graphs • A graph is said to be connected if for every pair of its vertices u and v there is a path from u to v.
Graph Representation § Adjacency matrix • n x n boolean matrix if |V| is n. • The element on the ith row and jth column is 1 if there’s an edge from ith vertex to the jth vertex; otherwise 0. • The adjacency matrix of an undirected graph is symmetric. • It takes |V| (comparisons) to figure out all the adjacent nodes of a certain node i. § Adjacency linked lists • A collection of linked lists, one for each vertex, that contain all the vertices adjacent to the list’s vertex.
Graph Representation § An example
Depth-First Search § The idea • traverse “deeper” whenever possible. • On each iteration, the algorithm proceeds to an unvisited vertex that is adjacent to the one it is currently in. If there are more than more neighbors, break the tie by the alphabetic order of the vertices. • When reaching a dead end, the algorithm backs up one edge to the parent and tries to continue visiting unvisited vertices from there. • The algorithm halts after backing up to the starting vertex, with the latter being a dead end. § It’s convenient to use a stack to track the operation of depthfirst search • Push a vertex onto the stack when the vertex is reached for the first time. • Pop a vertex off the stack when it becomes a dead end.
Example – undirected graphs a b c d e f g h § Depth-first traversal: Give the order in which the vertices were reached. abfegcdh 7
Depth-first search (DFS) § Pseudocode for Depth-first-search of graph G=(V, E) DFS(G) // Use depth-first to visit a graph, which might // contain multiple connected components. count 0 //visiting sequence number mark each vertex with 0 // (unvisited) for each vertex v∈ V do if v is marked with 0 //w has not been visited yet. dfs(v) //Use depth-first to visit a connected component starting //from vertex v. count + 1 mark v with count //visit vertex v for each vertex w adjacent to v do if w is marked with 0 //w has not been visited yet. dfs(w) 8
Example – directed graph a b c d e f g h § Depth-first traversal: Give the order in which the vertices were reached. abfghecd 9
Depth-first search: Notes § DFS can be implemented with graphs represented as: • Adjacency matrices: Θ(|V |2) • Adjacency linked lists: Θ(|V |+|E| ) § Applications: • checking connectivity • finding connected components 10
Breadth-First Search § The idea • Traverse “wider” whenever possible. • Discover all vertices at distance k from s (on level k) before discovering any vertices at distance k +1 (at level k+1) § Similar to level-by-level tree traversals § Instead of a stack, breadth-first uses a queue.
Example – undirected graph a b c d e f g h § Breadth-first traversal: abefgchd 12
Breadth-first search algorithm BFS(G) count 0 mark each vertex with 0 for each vertex v∈ V do if v is marked with 0 bfs(v) count + 1 mark v with count //visit v initialize queue with v //enqueue while queue is not empty do a front of queue //dequeue for each vertex w adjacent to a do if w is marked with 0 //w hasn’t been visited. count + 1 mark w with count //visit w add w to the end of the queue //enqueue remove a from the front of the queue 13
Example – directed graph a b c d e f g h § Breadth-first traversal: abefghcd 14
Breadth-first search: Notes § BFS has same efficiency as DFS and can be implemented with graphs represented as: • Adjacency matrices: Θ(|V |2) • Adjacency linked lists: Θ(|V |+|E| ) § Applications: • checking connectivity • finding connected components • finding paths between two vertices with the smallest number of edges 15
Exercises § Exercises 3. 5 No. 1 § Exercises 3. 5 No. 4 § Exercises 3. 5 No. 7 § Exercises 3. 5 No. 8
Tugas Proyek 1: Brute Force 1. A graph is said to be bipartite if all its vertices can be partitioned into two disjoint subsets X and Y so that every edge connects a vertex in X with a vertex in Y. (One can also say that a graph is bipartite if its vertices can be colored in two colors so that every edge has its vertices colored in different colors; such graphs are also called 2 colorable. ) For example, graph (i) is bipartite while graph (ii) is not. • Design a DFS-based algorithm for checking whether a graph is bipartite. • Design a BFS-based algorithm for checking whether a graph is bipartite.
Tugas Proyek 1: Brute Force 2. Write a program implementing the brute-force algorithm for the convex-hull problem. Yang dikumpulkan: • Jawaban Nomor 1, nilai: 40 • Source Code Nomor 2 (cpp), nilai: 30 • Laporan (doc) yang berisi: ü Algoritma brute-force (nilai: 10) ü Penjelasan source code (variabel, fungsi), nilai: 10 ü Screen shot running program dan penjelasan input, output, running time, nilai: (10)
Sample Input 2 15 30 30 50 60 60 20 70 45 86 39 112 60 200 113 250 50 300 200 130 240 76 150 47 76 36 40 33 35 30 30 -1 12 50 60 60 20 70 45 100 70 125 90 200 113 250 140 180 170 105 140 79 140 60 85 50 60 Sample Output 2 8 60 20 250 50 300 200 130 240 76 150 47 76 30 30 60 20 -1 6 60 20 250 140 180 170 79 140 50 60 60 20
- Slides: 19