0% found this document useful (0 votes)
6 views

Ch8_Traversing_Optimization

The document provides an overview of graph theory, focusing on rooted trees, their properties, and traversal methods such as inorder, preorder, and postorder. It also discusses breadth-first search (BFS) and depth-first search (DFS) algorithms, including their applications and examples. Additionally, it covers minimum spanning tree algorithms, specifically Kruskal's and Prim's algorithms, with exercises for practical understanding.

Uploaded by

2491178958
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Ch8_Traversing_Optimization

The document provides an overview of graph theory, focusing on rooted trees, their properties, and traversal methods such as inorder, preorder, and postorder. It also discusses breadth-first search (BFS) and depth-first search (DFS) algorithms, including their applications and examples. Additionally, it covers minimum spanning tree algorithms, specifically Kruskal's and Prim's algorithms, with exercises for practical understanding.

Uploaded by

2491178958
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

GRAPH THEORY

Traversing and Optimization


Rooted Trees
• If G is a directed graph, then G is called a directed tree if the
undirected graph associated with G is a tree. When G is a directed
tree, G is called a rooted tree if there is a unique vertex r, called the
root, in G with the indegree of r = id(r) = 0, and for all other vertices v,
the indegree of v = id(v) = 1.
• Example: Which one is a rooted tree?
• In a rooted tree, a vertex with outdegree 0 is
called a leaf (or terminal vertex.) All other
vertices are called branch nodes (or internal
vertices).
Rooted Trees’ Terms: 1
• The level of a vertex v in the rooted T is the length of the path from
the root r to the vertex v (rTv).
• Example: Determine the level of r, s, x, and y in the following rooted
tree:

• Each vertex on the path rTv, including the vertex v


itself, is called an ancestor of v, and each vertex of
which v is an ancestor is a descendant of v.
• Example: Find the ancestors and descendants of s:
Rooted Trees’ Terms: 2
• We call y a child of x, and we call x the parent of y if x  y is an edge in
the rooted tree T.
• Two vertices with a common parent are referred to as siblings.
• If v is any vertex of the tree, the subtree
at v is the subgraph induced by the root
v andvall of its descendants (there may
be none).
• Example: a three-chapter book
Binary rooted tree
• A rooted tree is a binary rooted tree if for each vertex v, od(v) = 0, 1,
or 2 — that is, if v has at most two children. If od(v) = 0 or 2 for all v in
V, then the rooted tree is called a complete binary tree.
• A complete binary tree can represent a binary operation as shown in
the following figures:

( (7 − 𝑎)/ 5 ) ∗ ((a+ b) ↑3)


where denotes multiplication
and denotes exponentiation.
Tree traversal: Infix Notation
(Inorder Traversal)
• Tree traversal means how to systematically order the vertices in a tree.
• Infix Notation (Inorder Traversal): In this notation, a binary operation symbol is placed
between the expressions representing its two operands.
• Formal definition (Inorder Traversal): Let be a binary rooted tree with vertex the root.
 If , then the vertex constitutes the inorder traversal of .
 When , let and denote the left and right subtrees of . The inorder traversal of first traverses the
vertices in inorder, then it visits the root , and then it traverses, in inorder, the vertices of .
• Example: in normal infix notation:
*
• Awejio
• WEKHFK + −
• FKAJWEFNK
• AW.KEJNSB a b c d
Tree traversal: Infix Notation
(Inorder Traversal) Exercise
• Exercise 1: Apply the inorder traversal to the binary rooted tree shown in
the following graph.

• Note that inoredr traversal is only defined for


binary rooted trees.
Tree traversal: Prefix/Polish
Notation (Preorder Traversal)
• Prefix/Polish Notation (Preorder Traversal): In this notation, the symbol which represents the
operation precedes (comes before) the symbols representing the operands (written from left to
right, in the same way as text).
• Formal definition (Preorder Traversal): Let be a rooted tree with vertex the root. If , let denote
the subtrees of as we go from left to right (as in the figure).
 The preorder traversal of first visits and then traverses the vertices of in preorder, then the vertices of in
preorder, and so on until the vertices of are traversed in preorder.
• Example: In normal prefix notation we have:
• Awejio *
• WEKHFK
• FKAJWEFNK + −
• The advantage is that the expression
can be rewritten without parentheses
a b c d
(application in compiler). AW.KEJNS
Tree traversal: Prefix/Polish
Notation (Preorder Traversal)
Exercise
• Exercise 2: Apply the preorder traversal to the rooted tree shown in
the following graph.
Tree traversal: Postfix Notation
(Postorder Traversal)
• Postfix Notation (Postorder Traversal): In this notation, the symbol which
represents the operation succeeds (comes after) the symbols representing the
operands.
• Formal definition (Postorder Traversal): Let be a rooted tree with vertex the
root. If , let denote the subtrees of as we go from left to right (as in the figure).
 The postorder traversal of traverses in postorder the vertices of and then visits the root.
• Example: In normal postfix notation we have:
*
• Awejio
• WEKHFK + −
• FKAJWEFNK
• AW.KEJNS a b c d
Tree traversal: Postfix Notation
(Postorder Traversal) Exercise
• Exercise 3: Apply the postorder traversal to the rooted tree shown in
the following graph.
Tree traversals: More Exercise
• Exercise 4: Apply the preorder, postorder, and inorder traversal to the
binary rooted trees shown in the following graphs.
Breadth-first search (BFS)
• Given a graph and a distinguished source vertex , breadth-first search
systematically explores the edges of to “discover” every vertex that is reachable
from .
• BFS computes the distance (smallest number of edges, shortest path) from to
each reachable vertex.
• The algorithm works on both directed and undirected graphs.
• To keep track of progress, breadth-first search colors each vertex white, gray, or
black. All vertices start out white and may later become gray and then black.
• BFS algorithm: It starts at the tree root and explores all nodes at the present
depth prior to moving on to the nodes at the next depth level. A queue (first-in-
first-out (FIFO) data structure) is used to keep track of the child nodes that were
encountered but not yet explored.
Breadth-first search (BFS): Example
• Example: The operation of BFS on an undirected graph. Tree edges
are shown shaded as they are produced by BFS.
Breadth-first search (BFS): Examples
and Applications
• Example: Animated example of a breadth-first search. Black:
explored, grey: queued to be explored later on.
• Note: Order in which the nodes are expanded in a
rooted tree.

• Applications: BFS on Maze-solving algorithm


• Finding the shortest path between two nodes
Breadth-first search (BFS): Exercise
• Exercise 5:
Depth-first search (DFS)
• The strategy followed by depth-first search is, as its name implies, to search
“deeper” in the graph whenever possible. Depth-first search explores edges out of
the most recently discovered vertex that still has unexplored edges leaving it. Once
all of ’s edges have been explored, the search “backtracks” to explore edges leaving
the vertex from which was discovered. This process continues until we have
discovered all the vertices that are reachable from the original source vertex. If any
undiscovered vertices remain, then depth-first search selects one of them as a new
source, and it repeats the search from that source. The algorithm repeats this entire
process until it has discovered every vertex.
• As in breadth-first search, depth-first search colors vertices during the search to
indicate their state. Each vertex is initially white, is grayed when it is discovered in
the search, and is blackened when it is finished, that is, when its adjacency list has
been examined completely.
Depth-first search (DFS): Example
• Example: The progress of the depth-first-search algorithm DFS on a directed graph. As edges are
explored by the algorithm, they are shown as either shaded (if they are tree edges) or dashed
(otherwise). Nontree edges are labeled B, C, or F according to whether they are back, cross, or
forward edges. Timestamps within vertices indicate discovery time/finishing times.

• SED
• SRDG
• SGD
• SGD
• SDG
• SDG
• SDG
Depth-first search (DFS): Examples
and Applications
• Example: Animated example of a depth-first search.

• Note: Order in which the nodes are expanded in a


rooted tree.

• Applications: Finding connected components,


• Solving puzzles with only one solution, such as mazes.
Depth-first search (DFS): Exercise
• Exercise 6:
Minimum-Spanning-Tree algorithms:
Kruskal’s algorithm
• Minimum-Spanning-Tree: A minimum spanning tree of a connected graph is a
subset of the edges that forms a tree that includes every vertex, where the sum of
the weights of all the edges in the tree is minimized. For a disconnected graph, a
minimum spanning forest is composed of a minimum spanning tree for each
connected component.
• Kruskal’s algorithm: It is a greedy algorithm in graph theory as in each step it adds
the next lowest-weight edge that will not form a cycle to the minimum spanning
forest.
• Example: The execution of Kruskal’s algorithm on the following graph:

• ad
Kruskal’s algorithm: Exercise
• Exercise 7: Apply Kruskal’s algorithm on the following graph:
Minimum-Spanning-Tree algorithms:
Prim's algorithm
• Prim's algorithm: The algorithm operates by building a tree one vertex at a
time, from an arbitrary starting vertex, at each step adding the cheapest
possible connection from the tree to another vertex.
• Example: The execution of Prim’s algorithm on the following graph: Prim's
algorithm starting at vertex A. In the third step, edges BD and AB both have
weight 2, so BD is chosen arbitrarily. After that step, AB is no longer a
candidate for addition to the tree because it links two nodes that are already
in the tree.
• Asef
• SFE
• SEF
Prim's algorithm: Exercise
• Exercise 8: Apply Prim's algorithm on the following graph:

You might also like