Mata kuliah :K0362/ Matematika Diskrit Tahun :2008 Tree Graph Pertemuan 10 :
Learning Outcomes Mahasiswa dapat menunjukkan tree yg meliputi konsep dasar tree, komponen tree, jenis-jenisnya dan contoh tentang penyelesain masalah dgn menggunakan tree atau ordered rooted tree. Mahasiswa dapat mengkombinasikan materi Ordered Rooted Tree dgn disiplin ilmu yg lain serta menerapkannya untuk berbagai kasus..
Outline Materi: Pengertian Tree Spanning Tree Cabang, akar & daun Titik berat, Berat pohon & Pusat berat Ordered Rooted Tree Pengertian Ordered rooted tree Leksikografik order Prefix form/ Pre order Posfix form/Post order Infix form/ In order Contoh pemakaian
Pengertian Pohon Suatu graph yang banyak vertexnya sama dengan n (n>1) disebut pohon, jika : Graph tersebut tidak mempunyai lingkar (cycle free) dan banyaknya rusuk (n-1). Graph tersebut terhubung .
Pengertian Pohon (2) Diagram pohon, diagram pohon merupakan digraph, yang : mempunyai source tak ada vertex yang indegreenya lebih dari satu. hutan (forest) = himpunan pohon diagram pohon dapat digunakan sebagai alat untuk memecahkan masalah dengan menggambarkan semua alternatif pemecahan. Hutan : banyaknya titik = n banyaknya pohon = k banyaknya rusuk = n-k
Applications of Graphs: Topological Sorting Topological order A list of vertices in a directed graph without cycles such that vertex x precedes vertex y if there is a directed edge from x to y in the graph There may be several topological orders in a given graph Topological sorting Arranging the vertices into a topological order
Topological Sorting Figure 13.14 Figure 13.15 A directed graph without cycles Figure 13.15 The graph in Figure 13-14 arranged according to the topological orders a) a, g, d, b, e, c, f and b) a, b, g, d, e, f, c
Topological Sorting Simple algorithms for finding a topological order topSort1 (Example: Figure 13-16) Find a vertex that has no successor Remove from the graph that vertex and all edges that lead to it, and add the vertex to the beginning of a list of vertices Add each subsequent vertex that has no successor to the beginning of the list When the graph is empty, the list of vertices will be in topological order
Topological Sorting Simple algorithms for finding a topological order (Continued) topSort2 (Example: Figure 13-17) A modification of the iterative DFS algorithm Strategy Push all vertices that have no predecessor onto a stack Each time you pop a vertex from the stack, add it to the beginning of a list of vertices When the traversal ends, the list of vertices will be in topological order
Spanning Trees A tree An undirected connected graph without cycles A spanning tree of a connected undirected graph G A subgraph of G that contains all of G’s vertices and enough of its edges to form a tree Example: Figure 13-18 To obtain a spanning tree from a connected undirected graph with cycles Remove edges until there are no cycles
Spanning Trees You can determine whether a connected graph contains a cycle by counting its vertices and edges A connected undirected graph that has n vertices must have at least n – 1 edges A connected undirected graph that has n vertices and exactly n – 1 edges cannot contain a cycle A connected undirected graph that has n vertices and more than n – 1 edges must contain at least one cycle
Spanning Trees Figure 13.19 Connected graphs that each have four vertices and three edges
The DFS Spanning Tree To create a depth-first search (DFS) spanning tree Traverse the graph using a depth-first search and mark the edges that you follow After the traversal is complete, the graph’s vertices and marked edges form the spanning tree Example: Figure 13-20
The BFS Spanning Tree To create a breath-first search (BFS) spanning tree Traverse the graph using a breadth-first search and mark the edges that you follow When the traversal is complete, the graph’s vertices and marked edges form the spanning tree Example: Figure 13-21
Minimum Spanning Tree Minimum spanning tree A spanning tree for which the sum of its edge weights is minimal Prim’s algorithm Finds a minimal spanning tree that begins at any vertex Strategy (Example: Figures 13-22 and 13-23) Find the least-cost edge (v, u) from a visited vertex v to some unvisited vertex u Mark u as visited Add the vertex u and the edge (v, u) to the minimum spanning tree Repeat the above steps until there are no more unvisited vertices
Tree Traversals Definition: A traversal is the process for “visiting” all of the vertices in a tree Often defined recursively Each kind corresponds to an iterator type Iterators are implemented non-recursively
Visit vertex, then visit child vertices (recursive definition) Preorder Traversal Visit vertex, then visit child vertices (recursive definition) Depth-first search Begin at root Visit vertex on arrival Implementation may be recursive, stack-based, or nested loop
Preorder Traversal 1 2 3 4 5 6 8 7 root 1 2 3 4 5 6 8 7 root 1 2 3 4 5
Preorder Traversal 1 2 3 4 5 6 8 7 root 1 2 3 4 5 6 8 7 root 1 2 3 4 5
Postorder Traversal Visit child vertices, then visit vertex (recursive definition) Depth-first search Begin at root Visit vertex on departure Implementation may be recursive, stack-based, or nested loop
Postorder Traversal 1 2 3 4 5 6 8 7 root 1 2 3 4 5 6 8 7 root 1 2 3 4
Postorder Traversal 1 2 3 4 5 6 8 7 root 1 2 3 4 5 6 8 7 root 1 2 3 4
Postorder Traversal 1 2 3 4 5 6 8 7 root 1 2 3 4 5 6 8 7 root 1 2 3 4
Postorder Traversal 1 2 3 4 5 6 8 7 root 1 2 3 4 5 6 8 7 root 1 2 3 4
Levelorder Traversal Visit all vertices in level, starting with level 0 and increasing Breadth-first search Begin at root Visit vertex on departure Only practical implementation is queue-based
Levelorder Traversal 1 2 3 4 5 6 8 7 root 1 2 3 4 5 6 8 7 root 1 2 3 4
Levelorder Traversal 1 2 3 4 5 6 8 7 root 1 2 3 4 5 6 8 7 root 1 2 3 4
Tree Traversals Preoder: depth-first search (possibly stack-based), visit on arrival Postorder: depth-first search (possibly stack-based), visit on departure Levelorder: breadth-first search (queue-based), visit on departure
Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children Definition: A binary tree is complete iff every layer but the bottom is fully populated with vertices. root 1 2 3 4 5 6 7
Binary Tree Traversals Inorder traversal Definition: left child, visit, right child (recursive) Algorithm: depth-first search (visit between children)
Inorder Traversal 1 2 3 4 5 7 6 root 1 2 3 4 5 7 6 root 1 2 3 4 5 7 6
Inorder Traversal 1 2 3 4 5 7 6 root 1 2 3 4 5 7 6 root 1 2 3 4 5 7 6
Inorder Traversal 1 2 3 4 5 7 6 root 1 2 3 4 5 7 6 root 1 2 3 4 5 7 6
Inorder Traversal 1 2 3 4 5 7 6 root 1 2 3 4 5 7 6 root
Terima kasih Semoga sukses