ch-5 - Graph Theory Full
ch-5 - Graph Theory Full
• 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
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.
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
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
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.
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
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
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.