Mata kuliah Tahun :K0144/ Matematika Diskrit :2008 Tree Graph Pertemuan 21 : 1 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.. 2 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 3 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 . pohon bukan pohon 4 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 5 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 6 Topological Sorting Figure 13.14 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 7 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 8 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 9 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 10 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 11 Spanning Trees Figure 13.19 Connected graphs that each have four vertices and three edges 12 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 13 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 14 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 15 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 16 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 17 Preorder Traversal 5 5 root 1 2 3 4 6 7 8 root 1 2 3 4 6 7 8 5 5 root 1 2 3 4 6 7 8 root 1 2 3 4 6 7 8 18 Preorder Traversal 5 5 root 1 2 3 4 6 7 8 root 1 2 3 4 6 7 8 5 5 root 1 2 3 4 6 7 8 root 1 2 3 4 6 7 8 19 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 20 Postorder Traversal 5 5 root 1 2 3 4 6 7 8 root 1 2 3 4 6 7 8 5 5 root 1 2 3 4 6 7 8 root 1 2 3 4 6 7 8 21 Postorder Traversal 5 5 root 1 2 3 4 6 7 8 root 1 2 3 4 6 7 8 5 5 root 1 2 3 4 6 7 8 root 1 2 3 4 6 7 8 22 Postorder Traversal 5 5 root 1 2 3 4 6 7 8 root 1 2 3 4 6 7 8 5 5 root 1 2 3 4 6 7 8 root 1 2 3 4 6 7 8 23 Postorder Traversal 5 5 root 1 2 3 4 6 7 8 root 1 2 3 4 6 7 8 5 5 root 1 2 3 4 6 7 8 root 1 2 3 4 6 7 8 24 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 25 Levelorder Traversal 5 5 root 1 2 3 4 6 7 8 root 1 2 3 4 6 7 8 5 5 root 1 2 3 4 6 7 8 root 1 2 3 4 6 7 8 26 Levelorder Traversal 5 5 root 1 2 3 4 6 7 8 root 1 2 3 4 6 7 8 5 5 root 1 2 3 4 6 7 8 root 1 2 3 4 6 7 8 27 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 28 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 4 1 2 3 5 6 7 29 Binary Tree Traversals • Inorder traversal – Definition: left child, visit, right child (recursive) – Algorithm: depth-first search (visit between children) 30 Inorder Traversal root 4 root 2 3 5 6 root 4 1 7 4 1 2 3 5 6 root 2 3 5 6 7 4 1 7 1 2 3 5 6 7 31 Inorder Traversal root 4 root 2 3 5 6 root 4 1 7 4 1 2 3 5 6 root 2 3 5 6 7 4 1 7 1 2 3 5 6 7 32 Inorder Traversal root 4 root 2 3 5 6 root 4 1 7 4 1 2 3 5 6 root 2 3 5 6 7 4 1 7 1 2 3 5 6 7 33 Inorder Traversal root 4 1 root 2 3 5 6 7 4 1 2 3 5 6 7 34 35
© Copyright 2026 Paperzz