0% found this document useful (0 votes)
8 views53 pages

Graph and Trees

The lecture covers fundamental concepts in graph theory and tree structures, including definitions of paths, circuits, and connected graphs. It explains tree properties, traversal methods, and applications in computer science, as well as the differences between infix, prefix, and postfix notations for expressions. Additionally, it includes examples and exercises to reinforce understanding of these concepts.

Uploaded by

hamzatahir9090
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views53 pages

Graph and Trees

The lecture covers fundamental concepts in graph theory and tree structures, including definitions of paths, circuits, and connected graphs. It explains tree properties, traversal methods, and applications in computer science, as well as the differences between infix, prefix, and postfix notations for expressions. Additionally, it includes examples and exercises to reinforce understanding of these concepts.

Uploaded by

hamzatahir9090
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 53

Lecture -----

Lecture Outline

• Graphs
– Path and Distance Matrix
• Trees
– Rooted Tree
– Tree Terminologies
– Ordered Rooted Tree
– Tree Traversal
– Infix, Prefix and Postfix Notation

06/27/2025 CSC102 - Discrete Structures 2


Walk – Path- circuit

Repeated Repeated Vertex Starts and Ends at Same Point


Edge
walk allowed Allowed allowed

closed walk allowed Allowed yes(means, where it starts also ends


at that point)
circuit no Allowed yes

simple circuit no first and last only yes

path no Allowed allowed

simple path no no No
Path
• A path is a sequence of edges that begins at a
vertex of a graph and travels from vertex to
vertex along edges of the graph. (path does
not contain a repeated edge)
• Find a path between a and e.

a---e length=1
a---b---e length=2
a---b---f---e length=3
a---b---c---f---e length=4
Path
Find a path between a and e.
• a, d, c, f , e is a simple path of length 4, because {a, d}, {d,
c}, {c, f }, and {f, e} are all edges.
• a, e is a simple path of length 1, because {a, e} is the edge
connecting both vertices.
• a, b, f , e is a simple path of length 3, because {a, b}, {b, f},
and {f, e} are all edges.

a---e length=1
a---b---e length=2
a---b---f---e length=3
a---b---c---f---e length=4
Circuit

• The path is a circuit if it begins and ends at the


same vertex.

• Path a, b, c, d, a is a circuit.
Circuit

• The path is a circuit if it begins and ends at the


same vertex.

• Path a, b, c, d, a is a circuit.
Circuit
Connected Graph

• An undirected graph is called connected if


there is a path between every pair of distinct
vertices of the graph.
Connected Graph
• The graph G1 is connected.
• The graph G2 is not connected. (For instance,
there is no path in G2 between vertices a and
d.)
Graph Distance

• The graph distance between two vertices in


a graph is the number of edges in a shortest
path connecting them.
• Also known as the geodesic distance.
• There may be more than one shortest path
between two vertices.
• If we compute the distance between every
pair of vertices of graph G, we can construct a
distance matrix D.
Eccentricity

• The eccentricity of a vertex in a connected graph is max


for all . In other words, a vertex’s eccentricity is equal to
the distance from itself to the vertex farthest away.

called the radius of the graph,


• The minimum eccentricity of all vertices in a graph is

diameter of the graph.


• The maximum eccentricity of all vertices in a graph is the
Example

Draw a undirected graph with the adjacency


matrix

with respect to the ordering of vertices a, b, c, d.


Find distance matrix of graph, eccentricity of
each vertex, diameter and radius of graph.
Example (Solution)

[ ]
0 1 1 0
1 0 1 1 a b
1 1 0 1
0 1 1 0
Given Matrix
d c

We have four vertices; so


eccentricity of each vertex is

[ ]
E(a)=2 0 1 1 2
E(b)=1 1 0 1 1
E(c)=1 1 1 0 1
E(d)=2 2 1 1 0

Distance Matrix
Radius =1
Diameter=2
Tree
Tree
• Trees are particularly useful in computer
science, where they are employed in a wide
range of algorithms.

Tree

• A tree is an undirected graph T such that


– T is connected
– T has no cycles (circuits)
• A tree cannot contain multiple edges or loops.
Therefore any tree must be a simple graph.
Applications of Tree
Trees are used to solve problems in a wide variety of
disciplines. In computer science trees are employed
to
1) construct efficient algorithms for locating items in a list.
2) construct networks with the least expensive set of
telephone lines linking distributed computers.
3) construct efficient codes for storing and transmitting
data.
4) model procedures that are carried out using a sequence
of decisions, which are valuable in the study of sorting
algorithms.
Example

Which of the graphs are trees?


Example

• G1 and G2 are trees, because both are


connected graphs with no simple circuits.
• G3 is not a tree because e, b, a, d, e is a simple
circuit in this graph.
• G4 is not a tree because it is not connected.
Forest

A forest is an undirected graph such that


• It has no simple circuits
• It is not necessarily connected
The connected components of a forest are trees
Rooted Tree
• A rooted tree is a tree in which one vertex has
been designated as the root and every edge is
directed away from the root.
• Root, parent, child, siblings relations between
vertices
Terminologies

• Suppose that T is a rooted


tree.
• If v is a vertex in T other than
the root, the parent of v is
the unique vertex u such that
there is a directed edge from
u to v. When u is the parent
of v, v is called a child of u.
• Vertices with the same
parent are called siblings.
Terminologies

• The ancestors of a vertex


other than the root are the
vertices in the path from the
root to this vertex, excluding
the vertex itself and
including the root (that is, its
parent, its parent’s parent,
and so on, until the root is
reached).
• The descendants of a vertex
v are those vertices that
have v as an ancestor.
Terminologies
• A vertex of a rooted tree is called a
leaf if it has no children.
• Vertices that have children are
called internal vertices. The root is
an internal vertex unless it is the
only vertex in the graph, in which
case it is a leaf.
• If v is a vertex in a tree, the
subtree with v as its root is the
subgraph of the tree consisting of
v and its descendants and all edges
incident to these descendants.
Example
• In the rooted tree T (with root a), find the
parent of c, the children of g, the siblings of h,
all ancestors of e, all descendants of b, all
internal vertices, and all leaves.

A Rooted Tree T
Example
• In the rooted tree T (with root a), What is the
subtree rooted at g?

A Rooted Tree T Subtree Rooted at g


Rooted Tree

• A rooted tree is called an m-ary tree if every


internal vertex has no more than m children.
The tree is called a full m-ary tree if every
internal vertex has exactly m children. An m-ary
tree with m = 2 is called a binary tree.
Definitions
• The level of a vertex v in a rooted tree is the
length of the unique path from the root to this
vertex.

• The level of the root is defined to be zero.

• The height of a rooted tree is the maximum of


the levels of vertices. In other words, the
height of a rooted tree is the length of the
longest path from the root to any vertex.
Example
• Find the level of each vertex in the rooted tree
T. What is the height of this tree?
Balanced Trees

• A rooted m-ary tree of height h is balanced if


all leaves are at levels h or h − 1.
Ordered rooted Tree

• An ordered rooted tree is a rooted tree where


the children of each internal vertex are
ordered.

• In a drawing of an ordered rooted tree, with


the root shown at the top, the children of a
vertex are shown from left to right.
Ordered rooted Tree

• An ordered rooted tree is a rooted tree where


the children of each internal vertex are
ordered.
Tree Traversal and Traversal Algorithms

• Ordered rooted trees are often used to store


information. Tree traversal is the procedure of
visiting different vertices of the tree to read
information stored in that vertex. There are three
different orders of the tree traversal.
• Most commonly used algorithms:
– Preorder traversal
– In-order traversal
– Post-order traversal
• All recursively defined.
Preorder Traversal
• Let T be an ordered rooted tree with root r and are the subtrees
at r from left to right. The preorder traversal begins by visiting
r . It continues by traversing in preorder, then in preorder, then
in preorder, . . . , and finally in preorder.
Preorder Traversal

Step
1
Step
1
Step
Step 1
3

Step
3
Step Step
Step Step 2 3
2 2
𝑐 , 𝑎, 𝑏, 𝑓 , 𝑔 , 𝑑, 𝑒
Preorder Traversal

• Root, left, right


• Example:
F

B H

A D K

• F, B, A, D, H, K
Preorder Traversal
Postorder Traversal
• Let T be an ordered rooted tree with root r and are
the subtrees at r from left to right. The postorder
traversal begins by traversing in postorder, then in
postorder, then in postorder, . . . , and finally in
postorder and ends by visiting r.
Post-order Traversal

Step
3
Step
3
Step
Step 3
2

Step
2
Step Step
Step Step 1 2
1 1
𝑓 , 𝑔, 𝑏, 𝑑 , 𝑎, 𝑒, 𝑐
Postorder Traversal

• Left, right, root


• Example:
F

B H

A D K

• A, D, B, K, H, F
Postorder Traversal
In order Traversal
• Let be an ordered rooted tree with root r and are
the subtrees at r from left to right. The in order
traversal begins by traversing in inorder, then visiting
r. It continues by traversing in inorder, then in
inorder, . .. , and finally in inorder.
In-order Traversal

Step
2
Step
2
Step
Step 2
3

Step
3
Step Step
Step Step 1 3
1 1
𝑓 , 𝑏, 𝑔, 𝑎, 𝑑, 𝑐 , 𝑒
In-order Tree Walk

• Left, root, right


• Example:
F

B H

A D K

• A, B, D, F, H, K
Example
Binary Search Trees
• A Binary search tree is a binary tree in
symmetric order.
• Symmetric order means that:
– every node has a key
– every node’s key is
• larger than all keys in its left subtree
• smaller than all keys in its right subtree
Infix, Prefix and Postfix Notation

• We can represent complicated expressions using ordered


rooted tree.
• The representation of an arithmetic expression involving
operators + (addition), − (subtraction), ∗ (multiplication),
/ (division), and ↑ (exponentiation).
• An ordered rooted tree can be used to represent such
expressions, where the internal vertices represent operations,
and the leaves represent the variables or numbers.
• Use parentheses to indicate the order of the operations.
• Each operation operates on its left and right sub trees (in that
order).
Example
• What is the ordered rooted tree that represents the expression .
Example

preorder :    xy 2 /  x 43
postorder : xy  2  x 4  3 / 
inorder : x  y  2  x  4 / 3
Infix, Prefix and Postfix form
• Infix Form
– It is the common arithmetic and logical formula notation,
in which operators are written between the operands

• Prefix Form
– Also known as Polish notation.
– A form of notation for logic, arithmetic, and algebra.
– It places operators to the left of their operands.

• Postfix Form
– Also known as Reverse Polish Notation.
– We obtain the postfix form of an expression by traversing
its tree in postorder.
Example

• What is the value of prefix expression   2 3 5/  2 3 4.


Example
• What is the value of postfix expression 7 2 3  4  9 3 / .
Chapter Exercise

• Chapter # 11
• Topic # 11.1
• Question # 1, 2, 3, 4, 5, 6, 7, 8, 9,10
• Topic # 11.3
• Question # 7 – 18, 23 ,24

You might also like