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

Graph Tree Theory 2023

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

Graph Tree Theory 2023

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

Graphs

(Python Networkx )
Introduction to Graphs- Simple Graph
•Definition:
•A simple graph G = (V, E) consists of V, a nonempty set of
vertices, and E, a set of unordered pairs of distinct
elements of V called edges.
•A simple graph is just like a directed graph, but with no
specified direction of its edges.

•Sometimes we want to model multiple connections


between vertices, which is known as multigraphs.
Graph Models-Ex
•Example I: How can we represent a network of (bi-
directional) railways connecting a set of cities?
•We should use a simple graph with an edge {a, b}
indicating a direct train connection between cities a and b.

Toronto Boston

Lübeck

New York

Hamburg

Washington
Introduction to Graphs-Multigraph
•Definition: A multigraph G = (V, E) consists of a set V of
vertices, a set E of edges, and a function
f from E to {{u, v} | u, v ∈ V, u ≠ v}.
•The edges e1 and e2 are called multiple or parallel edges if
f(e1) = f(e2).
•Note:
• Edges in multigraphs are not necessarily defined
as pairs, but can be of any type.
• No loops are allowed in multigraphs (u ≠ v).
Introduction to Graphs-Ex
•Example: A multigraph G with vertices V = {a, b, c, d}
• and edges {1, 2, 3, 4, 5} and function f with
f(1) = {a, b}, f(2) = {a, b},
f(3) = {b, c}, f(4) = {c, d} and f(5) = {c, d}:

1 4

a b 3 c d

2 5
Introduction to Graphs-Pseudograph
•If we want to define loops, we need the following
type of graph:
•Definition:
A pseudograph G = (V, E) consists of a set V of vertices, a
set E of edges, and a function
f from E to {{u, v} | u, v ∈ V}.
•An edge e is a loop if f(e) = {u, u} for some u∈V.
Introduction to Graphs-Directed graph
•Here is a type of graph that we already know:
•Definition:
•A directed graph G = (V, E) consists of a set V of vertices
and a set E of edges that are ordered pairs of elements in V.
•… leading to a new type of graph:
•Definition: A directed multigraph G = (V, E) consists of a
set V of vertices, a set E of edges, and a function f from E to
{(u, v) | u, v ∈ V}.
•The edges e1 and e2 are called multiple edges if f(e1) =
f(e2).
Introduction to Graphs
•Example: A directed multigraph G with vertices V = {a, b,
c, d}, edges {1, 2, 3, 4, 5} and function f with f(1) = (a, b),
f(2) = (b, a), f(3) = (c, b),
f(4) = (c, d) and f(5) = (d, c):

1 4

a b 3 c d

2 5
Introduction to Graphs
• Types of Graphs and Their Properties
Type Edges Multiple Edges? Loops?

simple graph undirected no no

multigraph undirected yes no

pseudograph undirected yes yes

directed graph directed no yes

dir. multigraph directed yes yes


Graph Models-Ex
•Example II: In a round-robin tournament, each team plays
against each other team exactly once. How can we
represent the results of the tournament (which team beats
which other team)?
•We should use a directed graph with an edge (a, b)
indicating that team a beats team b.

Maple Leafs Bruins

Penguins Lübeck Giants


Applications
• Graphs are used to define the flow of computation.
• Graphs are used to represent networks of communication.
• Graphs are used to represent data organization.
• Graph transformation systems work on rule-based in-memory
manipulation of graphs. Graph databases ensure transaction-safe,
persistent storing and querying of graph structured data.
• Graph theory is used to find shortest path in road or a network.
• In Google Maps, various locations are represented as vertices or
nodes and the roads are represented as edges and graph theory is
used to find the shortest path between two nodes.
• Graph theory is also used in network security.
• In linguistics, graphs are mostly used for parsing of a language
tree and grammar of a language tree.
Graph Terminology-Adjacent
•Definition: Two vertices u and v in an undirected graph G are called
adjacent (or neighbors) in G if {u, v} is an edge in G.
•If e = {u, v}, the edge e is called incident with the vertices u and v.
The edge e is also said to connect u and v.
•The vertices u and v are called endpoints of the edge {u, v}.

•Definition: When (u, v) is an edge of the graph G with directed


edges, u is said to be adjacent to v, and v is said to be adjacent from
u.
•The vertex u is called the initial vertex of (u, v), and v is called the
terminal vertex of (u, v).
•The initial vertex and terminal vertex of a loop are the same.
Graph Terminology-Degree
•Definition: The degree of a vertex in an undirected graph
is the number of edges incident with it, except that a loop
at a vertex contributes twice to the degree of that vertex.
•In other words, you can determine the degree of a vertex
in a displayed graph by counting the lines that touch it.
•The degree of the vertex v is denoted by deg(v).
•deg(a) = 2,
•deg(b) = 3,
•deg(c) = 1, as there is 1 edge formed at vertex 'c'
•So 'c' is a pendent vertex.
•deg(d) = 2,
•deg(e) = 0, as there are 0 edges formed at vertex
'e'.
•So 'e' is an isolated vertex.
Graph Terminology
•A vertex of degree 0 is called isolated, since it is not
adjacent to any vertex.
•Note: A vertex with a loop at it has at least degree 2 and,
by definition, is not isolated, even if it is not adjacent to
any other vertex.
•A vertex of degree 1 is called pendant. It is adjacent to
exactly one other vertex.
deg(a) = 2, deg(b) = 2, deg(c) = 2, deg(d) = 2,
and deg(e) = 0.
The vertex 'e' is an isolated vertex. The graph
does not have any pendent vertex.
Graph Terminology-Ex
•Example: Which vertices in the following graph are
isolated, which are pendant, and what is the maximum
degree? What type of graph is it?

Solution: Vertex f ,e is isolated, and vertices a, d and j are pendant. The maximum
degree is deg(g) = 5.
This graph is a pseudograph (undirected, loops).
In unidirectional graph a vertex with a loop "sees" itself as an adjacent
vertex from both ends of the edge thus adding two, not one, to the degree.
Graph Terminology
•Let us look at the same graph again and determine the
number of its edges and the sum of the degrees of all its
vertices:

Result: There are 8 edges, and the sum of all degrees is 18.
This is easy to explain: Each new edge increases the sum of
degrees by exactly two.
deg(g+b+c+e+i+h+a+d+j+f) =
5+2+2+0+2+2+1+1+1+0 = 16
Graph Theorems
•The Handshaking Theorem: Let G = (V, E) be an undirected
graph with e edges. Then
•2e = ∑v∈V deg(v)
•Note: This theorem holds even if multiple edges and/or
loops are present.

•Example: How many edges are there in a graph with 10


vertices, each of degree 6?
•Solution: The sum of the degrees of the vertices is 6⋅10 =
60. According to the Handshaking Theorem, it follows that
2e = 60, so there are 30 edges.
Graph Theorems
•Theorem: An undirected graph has an even number of
vertices of odd degree.
•Idea: There are three possibilities for adding an edge to
connect two vertices in the graph:
•Before: After:
Both vertices have Both vertices have
even degree odd degree

Both vertices have Both vertices have


odd degree even degree

One vertex has odd degree, the other One vertex has even degree, the other
even odd
Graph Theorems
•There are two possibilities for adding a loop to a vertex in
the graph:
•Before: After:
The vertex has The vertex has
even degree even degree

The vertex has The vertex has


odd degree odd degree

•So if there is an even number of vertices of odd degree in


the graph, it will still be even after adding an edge.
•Therefore, since an undirected graph with no edges has an
even number of vertices with odd degree (zero), the same
must be true for any undirected graph.
Graph Terminology
•Definition: In a graph with directed edges, the in-degree
of a vertex v, denoted by deg-(v), is the number of edges
with v as their terminal vertex.
•The out-degree of v, denoted by deg+(v), is the number of
edges with v as their initial vertex.

•Question: How does adding a loop to a vertex change the


in-degree and out-degree of that vertex?
•Answer: It increases both the in-degree and the out-
degree by one.
Degree of Vertex in a Directed Graph

• In a directed graph, each vertex has an indegree and an outdegree.


• Indegree of a Graph
• Indegree of vertex V is the number of edges which are coming into the
vertex V.
• Notation − deg−(V).
• Outdegree of a Graph
• Outdegree of vertex V is the number of edges which are going out from
the vertex V.
• Notation − deg+(V).
Graph Terminology
•Example: What are the in-degrees and out-degrees
of the vertices a, b, c, d in this graph:

deg-(a) = 1 deg-(b) = 4
deg+(a) = 2 deg+(b) = 2

a
b

deg-(d) = 2 deg-(c) = 0
deg+(d) = 1 deg+(c) = 2

d c
Graph Terminology

•Theorem: Let G = (V, E) be a graph with directed edges.


Then:
•∑v∈V deg-(v) = ∑v∈V deg+(v) = |E|

•This is easy to see, because every new edge increases


both the sum of in-degrees and the sum of out-degrees by
one.
Articulation Point
• In a graph, a vertex is called an articulation point if removing it
and all the edges associated with it results in the increase of the
number of connected components in the graph. For example
consider the graph given in following figure.
Articulation Point
• If in the above graph, vertex 1 and all the edges associated
with it, i.e. the edges 1-0, 1-2 and 1-3 are removed, there will
be no path to reach any of the vertices 2, 3 or 4 from the
vertices 0 and 5, that means the graph will split into two
separate components. One consisting of the vertices 0 and 5
and another one consisting of the vertices 2, 3 and 4 as
shown in the figure.

• Likewise removing the vertex 0 will disconnect the vertex 5


from all other vertices. Hence the given graph has two
articulation points: 0 and 1.
• Articulation Points represents vulnerabilities in a network.
• (the brute force approach is used)
Bridges

• An edge in a graph between vertices say u and v is called a


Bridge,
• if after removing it, there will be no path left
between u and v. It's definition is very similar to that of
Articulation Points.
Biconnected Components
• A graph is said to be Biconnected if:
1.It is connected, i.e. it is possible to reach every vertex from
every other vertex, by a simple path.
2.Even after removing any vertex the graph remains
connected.

Removing any of the vertices does


not increase the number of
connected components. So the
given graph is Biconnected.

If it has no articulation point then it is


Biconnected otherwise not.

https://www.hackerearth.com/practice/algorithms/graphs/biconnected-components/tutorial/
Special Graphs- Simple Graph
• A graph with no loops and no parallel edges is called a
simple graph.
• The maximum number of edges possible in a single graph
with 'n' vertices is
• nC2 where nC2 = n(n – 1)/2.

• The maximum number of edges with n=3 vertices


Special Graphs- Simple Graph
• The number of simple graphs possible with 'n' vertices =
Special Graphs- Null Graph
Null Graph: A null graph is defined as a graph which consists only the
isolated vertices.
Connected and Disconnected
Graph:
• Connected Graph: A
graph is called connected
if there is a path from
any vertex u to v or vice-
versa.
• Disconnected Graph: A
graph is called
disconnected if there is
no path between any two
of its vertices.

in fig is a Disconnected Graph, and its


connected components are
{V1,V2,V4,V3},{V5,V6,V7,V8} and {V9,V10}.
Complementary Graph:
• The complement of a graph G is defined to be a graph
which has the same number of vertices as in graph G
and has two vertices connected if and only they are not
related in the graph.
Special Graphs- Regular Graph
• A graph G is said to be regular, if all its vertices have the
same degree. In a graph, if the degree of each vertex is 'k',
then the graph is called a 'k-regular graph’.

In both the graphs, all the vertices have degree 2. They are called 2-
Regular Graphs.
Special Graphs- Regular Graph
• Example: Draw a 2-regular graph of five vertices.
• Solution: The 2-regular graph of five vertices is shown
in fig:
Special Graphs-Complete graph
•Definition: The complete graph on n vertices, denoted by
Kn, is the simple graph that contains exactly one edge
between each pair of distinct vertices.
•Number of edges n(n-1)/2
Special Graphs-Complete graph
Special Graphs-Cycle
•Definition: The cycle Cn, n ≥ 3, consists of n vertices v1, v2,
…, vn and edges {v1, v2}, {v2, v3}, …, {vn-1, vn}, {vn, v1}.
• If the degree of each vertex in the graph is two, then it is called a Cycle
Graph.
• Notation − Cn

https://www.tutorialspoint.com/types-of-graph
Special Graphs-Wheel
•Definition: We obtain the wheel Wn when we add an
additional vertex to the cycle Cn, for n ≥ 3, and connect this
new vertex to each of the n vertices in Cn by adding new
edges.
Special Graphs: n-cube
•Definition: The n-cube, denoted by Qn, is the graph that
has vertices representing the 2n bit strings of length n. Two
vertices are adjacent if and only if the bit strings that they
represent differ in exactly one bit position.

011 111
01 11
010 110
0 1 001 101
00 10
000 100
Q1 Q2 Q3
Special Graphs- Bipartite
•Definition: A simple graph is called bipartite if its vertex
set V can be partitioned into two disjoint nonempty sets V1
and V2 such that every edge in the graph connects a vertex
in V1 with a vertex in V2 (so that no edge in G connects
either two vertices in V1 or two vertices in V2).
Special Graphs Bipartite
•Example I: Is C3 bipartite?
v1 No, because there is no way to partition the
vertices into two sets so that there are no edges
v2 with both endpoints in the same set.
v3

Example II: Is C6 bipartite?


v1 v1 v6
v6
Yes, because we
v5 v2
v2 v5 can display C6 like
this: v4
v3
v3 v4
Special Graphs Bipartite
Which of the graphs below are bipartite? Justify your answers.

Three of the graphs are bipartite.


The C7 (second from the right).
To see that the three graphs are bipartite, just give the bipartition into two sets A and B
Planar Graphs
A graph (or multigraph) G is called planar if G can be drawn in the
plane with its edges intersecting only at vertices of G, such a
drawing of G is called an embedding of G in the plane.

Application Example: VLSI design (overlapping edges requires


extra layers), Circuit design (cannot overlap wires on board)

Representation examples: K1,K2,K3,K4 are planar, Kn for n>4


are non-planar

K5 non planar
Planar Graphs
Theorem : Euler's planar graph theorem

Example

A planar graph divides the plane


into several regions (faces), one
of them is the infinite region.

v=4,e=6,r=4, v-e+r=2
Planar Graphs
• Example: Determine the number of regions, finite regions
and an infinite region.
• Solution: There are five regions in the above graph, i.e.
r1,r2,r3,r4,r5.
• There are four finite regions in the graph, i.e., r2,r3,r4,r5.
• There is only one finite region, i.e., r1
Properties of Planar Graphs:

1.If a connected planar graph G has e edges and r regions, then


r ≤ 2/3 e.
2.If a connected planar graph G has e edges, v vertices, and r
regions, then v-e+r=2.
3.If a connected planar graph G has e edges and v vertices, then
3v-e≥6.
4.A complete graph Kn is a planar if and only if n<5.
5.A complete bipartite graph Kmn is planar if and only if m<3 or
n>3.
Example
The graph G has 6 vertices with degrees 2,2,3,4,4,5. How many edges
does G have? Could G be planar?

It is not planar.
Isomorphic Graphs
• Two graphs are isomorphic if there is a one-to-one matching between
vertices of the two graphs with the property that whenever there is an
edge between two vertices of either one of the graphs, there is an edge
between the corresponding vertices of the other graph.

Representation example: G1 = (V1, E1) , G2 = (V2, E2)


f(u1) = v1, f(u2) = v4, f(u3) = v3, f(u4) = v2,
Isomorphic Graphs 1
• G1 = (V1, E1) and G2 = (V2, E2) are isomorphic if:
• There is a one-to-one and onto function f from V1 to V2 with
the property that
• a and b are adjacent in G1 if and only if f (a) and f (b) are adjacent in
G2, for all a and b in V1.
• Function f is called isomorphism

Application Example:
In chemistry, to find if two compounds have the same
structure

https://www.javatpoint.com/graph-isomorphism-in-discrete-mathematics
Example: Isomorphic Graphs
Example: Isomorphic Graphs
Example: Isomorphic Graphs
Is it possible for two different (non-isomorphic) graphs to have the
same number of vertices and the same number of edges? What if
the degrees of the vertices in the two graphs are the?

For example, both graphs below contain 6 vertices, 7 edges, and


have degrees (2,2,2,2,3,3).

No It is Isomorphic
Example: Isomorphic Graphs
Is it possible for two different (non-isomorphic) graphs to have the
same number of vertices and the same number of edges? What if
the degrees of the vertices in the two graphs are the?

For example, both graphs below contain 6 vertices, 7 edges, and


have degrees {2, 2, 2, 2, 3, 3, 3, 3}

No It is Isomorphic as graph G1 and G2 has different cycles of


different length
Walk, Path, Circuit
A walk in a graph is a sequence of vertices, each linked to the
next vertex by a specified edge of the graph.
A path in a graph is a walk that uses no edge more than once.
A circuit in a graph is a path that begins and ends at the same
vertex.
1. Walk
• A walk is a sequence of vertices and edges of a graph i.e. if
we traverse a graph then we get a walk.
Vertex can be repeated Edges can be repeated

Walk can repeat anything (edges or vertices).


Here 1->2->3->4->2->1->3 is a walk

Open walk
Closed walk

In the diagram:
1->2->3->4->5->3-> is an open walk.
1->2->3->4->5->3->1-> is a closed walk.

https://www.geeksforgeeks.org/mathematics-walks-trails-paths-cycles-and-circuits-in-graph/
2. Trail
Trail is an open walk in which no edge is repeated.
Vertex can be repeated

Here 1->3->8->6->3->2 is trail


3. Circuit
Traversing a graph such that not an edge is repeated but vertex
can be repeated and it is closed also i.e. it is a closed trail.
Vertex can be repeated
Edge not repeated

Here 1->2->4->3->6->8->3->1 is a circuit


4. Path
• It is a trail in which neither vertices nor edges are repeated
i.e. if we traverse a graph such that we do not repeat a vertex
and nor we repeat an edge.
• As path is also a trail, thus it is also an open walk.
• Vertex not repeated
• Edge not repeated

Here 6->8->3->1->2->4 is a Path


Example: Classifying Walks
Subgraph
A graph G = (V1, E1) is called subgraph of a graph G(V, E) if
V1(G) is a subset of V(G) and E1(G) is a subset of E(G) such that
each edge of G1 has same end vertices as in G.
A graph consisting of some of the vertices of the original graph and
some the original edges between those vertices is called a subgraph.
Types of Subgraph:
•Vertex disjoint subgraph: Any two graph G1 = (V1, E1) and G2 =
(V2, E2) are said to be vertex disjoint of a graph G = (V, E) if V1(G1)
intersection V2(G2) = null.

•Edge disjoint subgraph: A subgraph is said to be edge disjoint if


E1(G1) intersection E2(G2) = null. In figure there is no common edge
between G1 and G2.
Subgraph Example
Vertex disjoint subgraph and Edge disjoint subgraph
Euler - definitions
An Eulerian path (Eulerian trail, Euler walk) in a graph is a
path that uses each edge precisely once. If such a path exists,
the graph is called traversable.

An Eulerian cycle (Eulerian circuit, Euler tour) in a graph is


a cycle that uses each edge precisely once. If such a cycle
exists, the graph is called Eulerian (also unicursal).

Representation example: G1 has Euler path a, c, d, e, b, d, a, b


Euler PATH Euler Circuit

Euler’s Path = d-c-a-b-d-e. Euler’s Circuit = a-b-c-d-a-g-f-e-c-a.


Euler -Example
• Example: The graph shown in fig is a Euler graph. Determine Euler
Circuit for this graph.

The Euler Circuit for this graph is


V1,V2,V3,V5,V2,V4,V7,V10,V6,V3,V9,V6,V4,V10,V8,V5,V9,V8,V1
Euler - Theorems

1. A connected graph G is Eulerian if and only if G is


connected and has no vertices of odd degree

2. A connected graph G is has an Euler trail from node a to


some other node b if and only if G is connected and a ≠ b
are the only two nodes of odd degree OR
A connected multigraph (and simple graph) has an Euler
path but not an Euler circuit if and only if it has exactly two
vertices of odd degree.
Euler - Example
Euler - Example

Euler’s Path − b-e-a-b-d-c-a is not


an Euler’s circuit, but it is an
Euler’s path. Clearly it has exactly
2 odd degree vertices.

Note − In a connected graph G, if the number of vertices with


odd degree = 0, then Euler’s circuit exists.
Euler - Example

Euler’s circuit = ABCDFBEDA

Euler’s circuit = ADFABCDEA


Representation- Incidence Matrix
Hamiltonian Graph

• Hamiltonian Path
• A connected graph is said to be Hamiltonian if it contains each
vertex of G exactly once. Such a path is called a Hamiltonian
path.
• In a Hamiltonian cycle, some edges of the graph can be
skipped.
Hamiltonian Graph
• if there is a vertex of degree one in a graph then it is
impossible for it to have a Hamiltonian circuit.

•Solution- No the graph does


not have a Hamiltonian circuit
as there are two vertices with
degree one in the graph.
A graph of the vertices of a dodecahedron.

Is it Hamiltonian?
Hamiltonian Graph

Hamiltonian Path − e-d-b-a-c.

For the graph shown −


•Euler path exists – false
•Euler circuit exists – false
•Hamiltonian cycle exists – true
•Hamiltonian path exists – true
Hamiltonian Graph

Hamiltonian Circuit −
ABCDEFGA

Hamiltonian Circuit −
ABCDEFGHIA
Hamiltonian Graph
DIRAC’S Theorem: if G is a simple graph with n vertices with
n ≥ 3 such that the degree of every vertex in G is at least
n/2 then G has a Hamilton circuit.

ORE’S Theorem: if G is a simple graph with n vertices with n


≥ 3 such that deg (u) + deg (v) ≥ n for every pair of
nonadjacent vertices u and v in G, then G has a Hamilton
circuit.
Coloring and Chromatic Number

• A coloring for a graph is a coloring of the vertices in such a


way that the vertices joined by an edge have different colors.
• The chromatic number of a graph is the least number of
colors needed to make a coloring.
• Application: Sudoku Puzzle (9* 9)
Coloring a Graph
Step 1: Choose a vertex with highest degree, and
color it. Use the same color to color as
many vertices as you can without coloring
vertices joined by an edge of the same
color.
Step 2: Choose a new color, and repeat what you
did in Step 1 for vertices not already
colored.
Step 3: Repeat Step 1 until all vertices are colored.
Example: Coloring a Graph
Vertex Coloring Problem
• The problem of finding a minimum coloring of a graph is NP-
Hard
• The corresponding decision problem (Is there a coloring
which uses at most k colors?) is NP-complete
• The chromatic number for Cn = 3 (n is odd) or 2 (n is even),
Kn = n, Km,n = 2

• Cn: cycle with n vertices; Kn: fully connected graph with n


vertices; Km,n: complete bipartite graph
Vertex Covering Problem

The Four color theorem: the chromatic number of a planar graph is


no greater than 4
Example: G1 chromatic number = 3, G2 chromatic number = 4
(Most proofs rely on case by case analysis).
Applications of Graph Coloring

• Some applications of graph coloring include:


• Register Allocation
• Map Coloring
• Bipartite Graph Checking
• Mobile Radio Frequency Assignment
• Making a time table, etc.
• Clustering
• Data mining
• Image capturing
• Image segmentation
• Networking
• Resource allocation
• Processes scheduling

Note: The chromatic number of Kn is n


Graph Coloring on Peterson and India Map
Trees
Definition: Tree
• Tree is a discrete structure that represents hierarchical relationships
between individual elements or nodes.
• A Tree is a connected undirected graph with no simple circuits.
• A Tree is an acyclic graph or graph having no cycles
• A Tree or general trees is defined as a non-empty finite set of
elements called vertices or nodes having the property that each
node can have minimum degree 1 and maximum degree n-1
Examples

Not Tree

d
Tournament Trees

A common form of tree used in everyday life is


the tournament tree, used to describe the
outcome of a series of games, such as a tennis
tournament.
Alice
Antonia Alice
Anita Alice
Abigail
Abigail Alic
Amy Agnes e
Agnes Angela
Angela Angela
Audrey
Properties of Trees:
1.There is only one path between each pair of vertices of a
tree.
2.If a graph G there is one and only one path between each
pair of vertices G is a tree.
3.A tree T with n vertices has n-1 edges.
4.A graph is a tree if and only if it a minimal connected.
Forest
Graphs containing no simple circuits that are not connected, but each
connected component is a tree.
Rooted Trees
Once a vertex of a tree has been designated as the root of
the tree, it is possible to assign direction to each of the
edges.
Properties of Trees
The level of a vertex v in
• The height of a rooted tree
a rooted tree is the length
is the maximum of the
of the unique path from
levels of vertices.
the root to this vertex.

level 2

level 3
Subtree
m-ary trees
• 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.

• A full m-ary tree with i internal


vertices contains n = mi+1 vertices.
Ordered Rooted Tree

An ordered rooted tree is a rooted tree where the


children of each internal vertex are ordered. Ordered
trees are drawn so that the children of each internal
vertex are shown in order from left to right.
Properties of Trees
• A rooted m-ary tree of height h is called balanced if all leaves
are at levels h or h-1.

• There are at most mh leaves in an m-ary tree of height h.


• If an m-ary tree of height h has l leaves, then
THEOREM
• Let (T, v0) be a rooted tree on a set A. Then
• (a) T is irreflexive.
• (b) T is asymmetric.
• (c) If (a, b) ∈ T and (b, c) ∈ T , then (a, c) /∈ T , for
all a, b, and c in A.
Binary Trees:
• Traversing Binary Trees

1.Preorder Traversal
2.Postorder Traversal
3.Inorder Traversal

• Binary Search Trees


• Minimum Spanning Tree:
• Kruskal's Algorithm
• Prim’s Algorithm
• Dijkstra's Algorithm:( (Shortest
Path)
• Travelling Salesman Problem
(Hint: Hamiltonian Circuit)
Applications of Trees
Complexity of Binary search tree
• Binary Search Trees (DSA
Sem III)
• Decision Trees
• Prefix Codes : Huffman
Coding
Huffman coding

• Huffman coding is a lossless data compression algorithm.


• variable-length codes to input characters
• based on the frequencies of corresponding characters.
• The most frequent character gets the smallest code and the
least frequent character gets the largest code.

Example 3 Use the Huffman code tree in Figure to


decode the string 0101100

We begin at the root and move to the left (using the first 0).
This leaf has label E.
We return to the root and use the string 101100. We go to the
right, then to the left
Code and record A. Repeating the process with 1100 produces S
E0 and finally another E.
A 10 The string 0101100 represents EASE.
S 110
Huffman coding Example
Use Huffman coding to encode the following symbols with the
frequencies listed: A: 0.08, B: 0.10, C: 0.12, D: 0.15, E: 0.20, F: 0.35.
What is the average number of bits used to encode a character?
Solution: Figure displays the steps used to encode these symbols.
The encoding produced encodes
A by 111, B by 110, C by 011, D by 010, E by 10, and F by 00.
The average number of bits used to encode a symbol using this
encoding is
3 ·0.08 + 3 ·0.10 + 3 ·0.12 + 3 ·0.15 + 2 . 0.20 + 2 . 0.35 = 2.45.
Huffman coding Example
Use Huffman coding to encode the following symbols with the
frequencies listed: What is the average number of bits used to encode
a character?

The total number of bits used to encode a symbol using this encoding is 224

You might also like