0% found this document useful (0 votes)
40 views98 pages

ch-5 - Graph Theory Full

graph teory from discreete mathematics

Uploaded by

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

ch-5 - Graph Theory Full

graph teory from discreete mathematics

Uploaded by

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

Representing Graphs

• Adjacency lists
• Adjacency matrix
• Incidence matrices
Linked Representation of a Graph G
Linked Representation of a Graph G
Adjacency Matrix
Suppose G is a graph with m vertices and
suppose the vertices have been ordered, say, v1,
v2, . . . , vm.
Then the adjacency matrix A = [aij] of the graph
G is the m × m matrix defined by
Adjacency Matrix
Example:
Incidence Matrix :Undirected graph
Incidence Matrix :Undirected graph
Incidence Matrix :Directed graph
Isomorphic graphs

The same graph exists in multiple forms.

Therefore, they are Isomorphic graphs.


Graph Isomorphism Conditions-
For any two graphs to be isomorphic, the following 4 Necessary conditions
must be satisfied-
The number of vertices in both graphs must be the same.
• The number of edges in both graphs must be the same.
• The degree sequence of both graphs must be the same.
• If a cycle of length k is formed by the vertices { v1, v2, ….., vk } in one graph, then a
cycle of the same length k must be formed by the vertices { f(v1) , f(v2) , ….. , f(vk)
} in the other graph as well.
• Vertex correspondence and edge correspondence.
Note: Degree Sequence
The degree sequence of a graph is defined as a sequence of the degree of all the vertices in ascending order.
Note: Cycles
Two isomorphic graphs have the same number of cycles of the same
lenth
Graph Isomorphism Conditions-
• Important Points-
• The given 4 conditions are just necessary for any two graphs to be
isomorphic.
• They are not at all sufficient to prove that the two graphs are
isomorphic.
• If all the 4 conditions are satisfied, even then, it can’t be said that the
graphs
are surely isomorphic.
• However, if any condition is violated, then it can be said that the graphs
are surely not isomorphic.
Sufficient Conditions
The following conditions are the sufficient conditions to prove any two
graphs isomorphic.
If any one of these conditions satisfy, then it can be said that the graphs are
surely isomorphic.
• Two graphs are isomorphic if and only if their complement graphs are
isomorphic.
• Two graphs are isomorphic if their adjacency matrices are same.
• Two graphs are isomorphic if their corresponding sub-graphs
obtained by deleting some vertices of one graph and their
corresponding images in the other graph are isomorphic.
Problem-01:
• Are the following graphs isomorphic?
Checking Necessary Conditions- Condition-
02:
Condition-01: Number of edges in graph G1 = 5
Number of edges in graph G2 = 5
Number of vertices in graph G1 = 4 Number of edges in graph G3 = 4
Number of vertices in graph G2 = 4
Number of vertices in graph G3 = 4 Here,
The graphs G1 and G2 have same number of edges.
Here, So, Condition-02 satisfies for the graphs G1 and G2.
All the graphs G1, G2 and G3 have same number However, the graphs (G1, G2) and G3 have different number
of vertices. of edges.
So, Condition-01 satisfies. So, Condition-02 violates for the graphs (G1, G2) and G3.

Since Condition-02 violates for the graphs (G1, G2) and G3,
so they can not be isomorphic.
∴ G3 is neither isomorphic to G1 nor G2.

Since Condition-02 satisfies for the graphs G1 and G2, so they


may be isomorphic.
∴ G1 may be isomorphic to G2.
Now, let us continue to check for the Condition-04:
graphs G1 and G2.
Both the graphs contain two cycles each
Condition-03: of length 3 formed by the vertices
having degrees { 2 , 3 , 3 }
Degree Sequence of graph It means both the graphs G1 and G2
G1 = { 2 , 2 , 3 , 3 } have same cycles in them.
Degree Sequence of graph So, Condition-04 satisfies.
G2 = { 2 , 2 ,
3,3} Thus,
All the 4 necessary conditions are
Here, satisfied.
Both the graphs G1 and G2 So, graphs G1 and G2 may be
have same isomorphic.
degree sequence.
So, Condition-03 satisfies. Now, let us check the sufficient
condition.
• We know that two graphs are
surely isomorphic if and only if
their complement graphs are
isomorphic.

• So, let us draw the


complement graphs of G1 and
G2.
• The complement graphs of G1
and G2 are-
• Clearly, Complement graphs of
G1 and G2 are isomorphic.
• ∴ Graphs G1 and G2 are
isomorphic graphs.
Problem-02:
Problem-03:
Problem-04:
C o n n ectiv ity
Paths
In f o rm a l Definition: 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. As the path travels along its edges,
it visits the vertices along this path, that is, the endpoints of
these edges.
Applications: Numerous problems can be modeled with
paths formed by traveling along edges of graphs such as:
● determining whether a message can be sent between two
computers.
● efficiently planning routes for mail delivery.
Paths
Definition: Let n be a nonnegative integer and G an undirected graph. A path of
length n from u to v in G is a sequence of n edges e1, … , e n of G for which there
exists a sequence x 0 = u, x1, … , xn-1, x n = v of vertices such that e i has,
for i = 1, …, n, the endpoints xi-1 and xi.
● When the graph is simple, we denote this path by its vertex sequence
x0, x1, … , x n (since listing the vertices uniquely determines the path).
● The path is a circuit if it begins and ends at the same vertex (u = v) and has
length greater than zero.
● The path or circuit is said to pass through the vertices x1, x2, … , x n-1 and
traverse the edges e1, … , e n .
● A path or circuit is simple if it does not contain the same edge more than
once.
Paths (continued)
E x a m pl e : In the simple graph here:
● a, d, c, f, e is a simple path of length 4.
● d, e, c, a is not a path because e is not connected to c.
● b, c, f, e, b is a circuit of length 4.
● a, b, e, d, a, b is a path of length 5, but it is not a simple path.
Connectedness in Undirected Graphs

Definition:
● An undirected graph is called connected if there is a path between
every pair of vertices.
● An undirected graph that is not connected is called disconnected.
● We say that we disconnect a graph when we remove vertices or
edges, or both, to produce a disconnected subgraph.
E x a mp l e : G 1 is connected. G 2 is not connected.
Connectedness in Undirected Graphs

Definition:
● An undirected graph is called connected if there is a path between
every pair of vertices.
● An undirected graph that is not connected is called disconnected.
● We say that we disconnect a graph when we remove vertices or
edges, or both, to produce a disconnected subgraph.
E x a mp l e : G 1 is connected. G 2 is not connected.

H1 H2
Connectedness in Undirected Graphs

One special type of connected (sub)graph is a tree,


which is a connected undirected graph with no simple circuits.
For example,
The graph on the left is a tree;
The graph in the center is not a tree because it contains a circuit;
The graph on the right is not a tree because it is not connected.
E x a mp l e : .
Connected Components
Definition: A connected component or simply component of an
undirected graph is a subgraph in which each pair of nodes is
connected with each other via a path.

Subgraph
A subgraph H of a graph G is another graph formed from a subset of
the vertices and edges of G if all endpoints of the edges of H are in the
vertex set of H. In other words, it is a subset of the larger original
graph.
Connected Components
Definition: A connected component or simply component of
an undirected graph is a subgraph in which each pair of nodes
is connected with each other via a path.
E x a m pl e :
Connected Components
Connected Components
Connected Components
Connected Components
Properties
Connectedness in Directed Graphs

Definition: A directed graph is strongly connected if there is


a path from a to b and a path from b to a whenever a and b
are vertices in the graph.
Definition: A directed graph is weakly connected if there is
a path between every two vertices in the underlying
undirected graph, which is the undirected graph obtained by
ignoring the directions of the edges of the directed graph.
Connectedness in Directed Graphs (continued)

Ex a m ple:
Connectedness in Directed Graphs (continued)

Ex a m ple:
● G is strongly connected because there is a path between any two
vertices in the directed graph. Hence, G is also weakly connected.
● The graph H is not strongly connected, since there is no directed
path from a to b, but it is weakly connected.
Connectedness in Directed Graphs (continued)

Ex a m ple:


Connectedness in Directed Graphs (continued)
Ex a m ple: Determine whether each of these graphs is strongly connected and if not,
whether it is weakly connected.


Connectedness in Directed Graphs (continued)
Ex a m ple: Determine whether each of these graphs is strongly connected and if not,
whether it is weakly connected.

This graph a) is not strongly connected This graph b) is not strongly connected
because there are no paths that start with a. because there are no paths that start with
This graph is weakly connected because the c. This graph is weakly connected because
underlying undirected graph is connected. the underlying undirected graph is
connected.
Counting Paths between Vertices
● We can use the adjacency matrix of a graph to find the number of paths
between two vertices in the graph.

Theorem: Let G be a graph with adjacency matrix A with respect to the


ordering v1, … , v n of vertices (with directed or undirected edges, multiple
edges and loops allowed). The number of different paths of length r from vi to
vj, where r > 0 is a positive integer, equals the (i,j)th entry of Ar.

Proof by mathematical induction:


Basis Step: By definition of the adjacency matrix, the number of paths from v i to vj of
length 1 is the (i,j)th entry of A.
Inductive Step: For the inductive hypothesis, we assume that that the (i,j)th entry of
A r is the number of different paths of length r from vi to vj.
● Because A r + 1 = A r A, the (i,j)th entry of A r + 1 equals bi1a1j + bi2a2j + ⋯ + binanj, where bik is
the (i,k)th entry of A r . By the inductive hypothesis, bik is the number of paths of length
r from v i to v k .
● A path of length r + 1 from vi to vj is made up of a path of length r from vi to some v k , and
an edge from v k to vj. By the product rule for counting, the number of such paths is
the product of the number of paths of length r from vi to v k (i.e., bik ) and the number
of edges from from v k to vj (i.e, akj). The sum over all possible intermediate
vertices v k is bi1a1j + bi2a2j + ⋯ + binanj .
Counting Paths between Vertices
(continued)

E xam p l e : How many paths of length four are there from a to d


in the graph G.
adjacency
G A=
matrix of G

Solution : The adjacency matrix of G (ordering the vertices as


a, b, c, d) is given above. Hence the number of paths of length
four from a to d is the (1, 4)th entry of A 4 . The eight paths are:

a, b, a, b, d a, b, a, c, d
a, b, d, b, d a, b, d, c, d A4 =
a, c, a, b, d a, c, a, c, d
a, c, d, b, d a, c, d, c, d
Counting Paths between Vertices
(continued)
Path Matrix
Let G = G(V,E) be a simple directed graph with
m vertices v1, v2, . . ., vm. The path matrix or
reachability matrix of G is the m-square matrix
P = [pij] defined as follows:
Weighted Graph
Weighted Graph
Counting Paths between Vertices
(continued)
Counting Paths between Vertices
(continued)
Counting Paths between Vertices
(continued)
Warshall Algorithm to produce Path
matrix
• Warshall's algorithm determines the transitive closure of a
directed graph or all paths in a directed graph by using the
adjacency matrix.
• It generates a sequence of n matrices, where n describes the
number of vertices.
Warshall Algorithm to produce Path
matrix
Warshall Algorithm to produce Path
matrix
Warshall Algorithm to produce Path
matrix
Warshall Algorithm to produce Path
matrix
Warshall Algorithm to produce Path
matrix
Warshall Algorithm to produce Path
matrix
Warshall Algorithm to produce Path
matrix
Warshall Algorithm to produce Path
matrix
Warshall Algorithm to produce Path
matrix
Warshall Algorithm to produce Path
matrix

Weight Distance
Matrix Matrix
Warshall Algorithm:All pairs shortest path
Tree
• A tree is a connected undirected graph with no simple
circuits.
• Because a tree cannot have a simple circuit, a tree
cannot contain multiple edges or loops.
• Therefore any tree must be a simple graph.
• Moreover, this path must be unique,
• for if there were a second such path, the path formed by
combining the first path from x to y followed by the path
from y to x obtained by reversing the order of the second
path from x to y would form a circuit
Tree
• Application:
• Representing Organizations
Structure
• Computer File Systems
Tree
• Which of the graphs shown in below Figure are trees?
Tree
• THEOREM 1:
• An undirected graph is a tree if and only if there is a
unique simple path between any two of its vertices.
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.
• Vertices that have children are called internal
vertices.
Rooted Tree • The root is an internal vertex unless it is the only
vertex in the graph, in which case it is a leaf.
Root

Parent

Child

Siblings
The Subtree Rooted at g

Leaf
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.
Rooted Tree

• T1=Full Binary ,T2= Full 3-ary tree,T3= Full 5-ary


tree,T4=not Full m-ary tree
Properties of Trees
1. A tree with n vertices has n-1 edges.
2. A full m-ary tree with i internal vertices contains n=mi+1
vertices.
3. A full m -ary tree with
• (i) n vertices has i = (n - 1 )/ m internal vertices
and l = [(m - l )n + 1 ]/ m leaves,
• (ii) i internal vertices has n = mi + 1 vertices
and l = (m - l )i + 1 leaves,
• (iii) l leaves has n = (m*l - 1 )/(m - l ) vertices
and i = (L - 1 )/(m - l ) internal vertices.
Properties of Trees
Example:
Suppose that someone starts a chain letter. Each person who receives the letter is asked to
send it on to four other people. Some people do this, but others do not send any letters.
How many people have seen the letter, including the first person, if no one receives more
than one letter and if the chain letter ends after there have been 100 people who read it
but did not send it out?
How many people sent out the letter?
Properties of Trees
Example:
Suppose that someone starts a chain letter. Each person who receives the letter is asked to
send it on to four other people. Some people do this, but others do not send any letters.
How many people have seen the letter, including the first person, if no one receives more
than one letter and if the chain letter ends after there have been 1 00 people who read it
but did not send it out?
How many people sent out the letter?
Solu:
The chain letter can be represented using a 4-ary tree. The internal vertices correspond to
people who sent out the letter, and the leaves correspond to people who did not send it
out.
Because 100 people did not send out the letter, the number of leaves in this rooted tree is
I = 100.
Hence, part (iii) of Theorem 4 shows that the number of people who have seen the letter
is
n = (4 · 1 00 - 1 )/(4 - 1) = 1 33 . Also, the number of internal vertices is 133 - 100 = 33, so
33 people sent out the letter.
Properties of Trees
4. 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.
5. A rooted m-ary tree of height h is balanced if all leaves are
at levels h or h-1.
6. There are at most mh leaves in an m -ary tree of height h.
Properties of Trees
• Example: What is the height of given tree?
Properties of Trees
• Example: Which of the rooted trees given below are
balanced?
Tree Traversal
Preorder Traversal:
•Visit the root of the tree.
•Traverse the left subtree in preorder.
•Traverse the right subtree in preorder.

Postorder Traversal:
•Traverse the left subtree in postorder.
•Traverse the right subtree in postorder.
•Visit the root of the tree.

Inorder Traversal:
•Traverse in inorder the left subtree.
•Visit the root of the tree.
•Traverse in inorder the right subtree.
Tree Traversal Example

Preor der 1 2 3 4 5 6 7 8 9 10 11

Posto rder 3 5 4 2 7 10 9 11 8 6 1

Inord er 3 2 5 4 1 7 6 9 10 8 11
Tree Traversal Example
Tree Traversal Example

Preord 1 2 5 6 7 3 4
er
Postor 5 6 7 2 3 4 1
der

Inorder 5 6 2 7 3 1 4
Tree Traversal Example
Tree Traversal Example

Preord 1 2 3 5 4 6
er
Postor 2 5 3 6 4 1
der

Inorder 2 5 3 1 4 6
Application of Tree Traversal Example
Binary Search Trees
Definition: Suppose T is a binary tree. Then T is called a binary search tree if
each node N of T has the following
property:
Application of Tree Traversal Example
Binary Search Trees
Searching and Inserting in a Binary Search Tree
That is, every node N in T exceeds every number in its left subtree and is less than every
number in its right subtree.
Application of Tree Traversal Example
Binary Search Trees
Searching and Inserting in a Binary Search Tree
Suppose ITEM = 20 is given, and we want to find or insert ITEM into T
Application of Tree Traversal Example
Binary Search Trees
Searching and Inserting in a Binary Search Tree
Application of Tree Traversal Example
Binary Search Trees
Searching and Inserting in a Binary Search Tree
Application of Tree Traversal Example
Binary Search Trees
Form a binary search tree for the words mathematics,
physics, geography,
zoology, meteorology, geology, psychology, and chemistry (using
alphabetical order).
Application of Tree Traversal Example
Binary Search Trees
Form a binary search tree for the words mathematics,
physics, geography,
zoology, meteorology, geology, psychology, and chemistry (using
alphabetical order).
Application of Tree Traversal Example
HUFFMAN CODING
• Huffman coding is a fundamental algorithm in data compression,
• the subject devoted to reducing the number of bits required to represent
information.
• Huffman coding is extensively used to compress bit strings representing text
and it also plays an important role in compressing audio and image files
Application of Tree Traversal Example
HUFFMAN CODING
Application of Tree Traversal Example
HUFFMAN CODING

Example: Let A, B, C, D, E, F, G, H be eight data items with the following assigned


weights:

Construct a 2-tree T with a minimum weighted path length P using the above data as
external nodes
Application of Tree Traversal Example
HUFFMAN CODING
Application of Tree Traversal Example
HUFFMAN CODING

Example: Use Huffman coding to encode the following symbols with the frequencies
listed:
A: 0.08, B:0.10, C: 0.1 2, D : 0.15, E: 0.20, F: 0.35.

What is the average number of bits used to encode a character?


Application of Tree Traversal Example
HUFFMAN CODING

You might also like