Graph Tree Theory 2023
Graph Tree Theory 2023
(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.
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?
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.
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
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
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.
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
K5 non planar
Planar Graphs
Theorem : Euler's planar graph theorem
Example
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:
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.
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?
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?
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
• 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.
Is it Hamiltonian?
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.
Not Tree
d
Tournament Trees
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.
1.Preorder Traversal
2.Postorder Traversal
3.Inorder Traversal
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