Professional Documents
Culture Documents
1
Learning Outcomes
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
4
Pengertian Pohon (2)
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
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
13
The BFS Spanning Tree
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
16
Preorder Traversal
17
Preorder Traversal
root 1 root 1
2 3 4 2 3 4
5 6 7 8 5 6 7 8
root 1 root 1
2 3 4 2 3 4
5 6 7 8 5 6 7 8
18
Preorder Traversal
root 1 root 1
2 3 4 2 3 4
5 6 7 8 5 6 7 8
root 1 root 1
2 3 4 2 3 4
5 6 7 8 5 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
root 1 root 1
2 3 4 2 3 4
5 6 7 8 5 6 7 8
root 1 root 1
2 3 4 2 3 4
5 6 7 8 5 6 7 8
21
Postorder Traversal
root 1 root 1
2 3 4 2 3 4
5 6 7 8 5 6 7 8
root 1 root 1
2 3 4 2 3 4
5 6 7 8 5 6 7 8
22
Postorder Traversal
root 1 root 1
2 3 4 2 3 4
5 6 7 8 5 6 7 8
root 1 root 1
2 3 4 2 3 4
5 6 7 8 5 6 7 8
23
Postorder Traversal
root 1 root 1
2 3 4 2 3 4
5 6 7 8 5 6 7 8
root 1 root 1
2 3 4 2 3 4
5 6 7 8 5 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
root 1 root 1
2 3 4 2 3 4
5 6 7 8 5 6 7 8
root 1 root 1
2 3 4 2 3 4
5 6 7 8 5 6 7 8
26
Levelorder Traversal
root 1 root 1
2 3 4 2 3 4
5 6 7 8 5 6 7 8
root 1 root 1
2 3 4 2 3 4
5 6 7 8 5 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 1
2 3
4 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 1 root 1
2 3 2 3
4 5 6 7 4 5 6 7
root 1 root 1
2 3 2 3
4 5 6 7 4 5 6 7
31
Inorder Traversal
root 1 root 1
2 3 2 3
4 5 6 7 4 5 6 7
root 1 root 1
2 3 2 3
4 5 6 7 4 5 6 7
32
Inorder Traversal
root 1 root 1
2 3 2 3
4 5 6 7 4 5 6 7
root 1 root 1
2 3 2 3
4 5 6 7 4 5 6 7
33
Inorder Traversal
root 1 root 1
2 3 2 3
4 5 6 7 4 5 6 7
34