Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 42

DIRECTED

GRAPH
CHAPTER 4
AZLINAJUMADI MARCHJUNE2018
INTRODUCTION
• Graphs are a generalization of trees
– Nodes or vertices
– Edges or arcs
• Two kinds of graphs
– Directed
– Undirected Gemereh
Tebing Tinggi

Segamat Jementah

UiTM
CONCEPT OF A BASIC GRAPH
• Undirected Graph 
a set of objects (called vertices or nodes) that are connected together,
where all the edges are bidirectional.
(sometimes called an undirected network)
E

• Directed Graph (digraph) D

a graph where the edges point in a direction C


(arrows will indicate the directed arcs in a
B
directed graph)
A
CONCEPT OF A BASIC GRAPH
• Parallel Edge
Two edges, both associated with the same pair of vertices

• Loop
An edge incident on a single vertex

• Isolated Vertex
A vertex that is not incident on any edge

• Simple Graph
A graph with neither loops nor parallel edges
RELATIONS AND DIRECTED GRAPH
Given A and a relation R on the set A. We can construct a directed graph
G consisting of the vertices in set A and the edges in set E: A x A.

Example 1:
Draw a directed graph for the following relation, R.
a) R = {(1, 2), (1, 5), (2, 2), (5, 2), (5, 5)}
b) R = {(a, b), (a, c), (b, a), (b, d), (c, c), (c, d)}
RELATIONS AND DIRECTED GRAPH
PRORERTIES OF RELATION

From a graph, we can determine the properties of a relation:


i) Reflexive – existence of loop for each node
ii) Symmetry – A directed arc from v to w and a directed arc from w to
v
iii) Anti-symmetry – for any two nodes, there exist only one arc
iv) Transitive – if there is an arc from x to y and from y to z, there must
exist an arc from x to z.
RELATIONS AND DIRECTED GRAPH
Example 2:
Given A = {1, 2, 3, 4} and R = {(1, 2), (2, 3), (3, 4), (4, 1), (1, 1), (2, 1), (2, 2),
(2, 4) }. Draw the directed graph and determine if the relation is reflexive,
symmetry, anti-symmetry and transitive.

Solution: Not Reflexive


– no loop for node 3 and 4
Not Symmetry – there exist an arc from 2 to 3
but not exist an arc from 3 to 2
Not Anti-symmetry
– there exist two arc between node 1 and 2
Not Transitive
– there is an arc from 1 to 2 and from 2 to 3, but
not exist an arc from 1 to 3
RELATIONS AND DIRECTED GRAPH
Example 3:
Given R on A = {1, 2, 3, 4, 5} with (x, y)  R if x + y = 6. Draw the directed
graph and determine if the relation is reflexive, symmetry, anti-symmetry and
transitive. Give reason if your answer NO.

Solution: Not Reflexive


– no loop for node 1, 2, 5 and 4
Symmetry
Not Anti-symmetry
– there exist two arc between node 1 and 5 and
node 2 and 4
Not Transitive
– there is an arc from 1 to 5 and from 5 to 1, but
not exist a loop at node 1
EXERCISES
1) Given A = {a, b, c, d} and R = {(a, a), (b, c), (c, b), (d, d)}. Draw the
directed graph and determine if the relation is reflexive, symmetry, anti-
symmetry and transitive.
2) Given R is a relation on A = {1, 2, 4, 5} with (x, y)  R if x ≤ y for all x, y 
A. List all members of R and draw a graph representing R. Are the relations
reflexive, symmetry, anti-symmetry and transitive? Give reasons.
3) Given P is a set of partitions. P ={{1, 3, 5}, {2, 6}, {4}} for
X = {1, 2, 3, 4, 5, 6}. R is a relation on X. List all members of R and draw a
graph representing R. Are the relations reflexive, symmetry, anti-symmetry
and transitive?
1) Not reflexive, symmetry, not anti-symmetry, not transitive
2) R = {(1, 1), (1, 2), (1, 4), (1, 5), (2, 2), (2, 4), (2, 5), (4, 4), (4, 5), (5, 5)}. Reflexive, not symmetry, anti-symmetry, transitive
3) R = {(1, 1), (1, 3), (1, 5), (3, 1), (3, 3), (3, 5), (5, 1), (5, 3), (5, 5), (2, 2), (2, 6), (6, 2), (6, 6), (4, 4)}. Reflexive, symmetry, not anti-symmetry, transi
FUNCTIONS AND DIRECTED GRAPHS
• Function is a special relation
• Function
– each node in domain is connected to the other nodes in codomain.
– There are only one arc going out from each node
• One-to-one function
– each object has exactly one image.
– No two different objects can have the same image
– Only one arc going into a node
• Onto function
– each element in codomain is an image of any object in domain
– There are arc(s) going into each element in codomain
FUNCTIONS AND DIRECTED GRAPHS
Example 4:
Determine if the following graph is a function or not. If it is a function,
determine whether the function is one-to-one.
a) b)

Function,
Not 1-to- 1 Not Function
FUNCTIONS AND DIRECTED GRAPHS
Example 5:
Given f: A→B where f(a) = 2a and A = {2, 3, 4}, B = {4, 6, 8, 10}. Is f one-to-
one? Is f onto? Why?

1-to- 1
- only one arc going into
node 4, 8, and 6
Not onto
- No arc going into node 10
FUNCTIONS AND DIRECTED GRAPHS
CONNECTED GRAPH
A graph is connected if there is an edge that connects two nodes v and w, and
v ≠ w. In other words, a connected graph is a graph in which we can get from
one node to any other node on a path. There will be no isolated node.

Example 6:
Determine if the following graph is a connected graph or not.
a) b)
YES NO
FUNCTIONS AND DIRECTED GRAPHS
SUBGRAPH
• A subgraph S of a graph G is a graph whose set of vertices and set of edges
are all subsets of G.
• Since every set is a subset of itself, every graph is a subgraph of itself.
• All the edges and vertices of G might not be present in S
• If a vertex is present in S, it has a corresponding vertex in G 
• Any edge that connects two vertices in S will also connect the corresponding
vertices in G.
FUNCTIONS AND DIRECTED GRAPHS
## SUBGRAPH

Example 7:
Let G be the graph given below.

One example of its subgraph:


FUNCTIONS AND DIRECTED GRAPHS
## SUBGRAPH

Example 8:
Let G be the graph given below. Draw a subgraph G1=G \ {e} and G2=G \ {v}.
PATH IN DIRECTED GRAPH
• Degree
The number of edges incident on a node
• Size of graph
Number of vertices that are in the graph
• In degree
The number of arcs going into a node
• Out degree
The number of arcs going out from a node
• Distance of a path
Number of arcs that are visited. (distance = nodes – 1)
PATH IN DIRECTED GRAPH
• Path
Allows repetitions of vertices or edges or both
• Cycle/circuit
– No repeated edges (except for the beginning and ending vertices that are
both equal)
– Distance of cycle must at least 3
PATH IN DIRECTED GRAPH
(SIMPLE PATH VS ELEMENTARY PATH)
• Simple Path
A path with no repeated edges
• Elementary Path
A path that that has no repeated nodes

 Each elementary path is a simple path


 A graph of one node is an elementary path
 cycle – start and ends at the same node
PATH IN DIRECTED GRAPH
(SIMPLE PATH VS ELEMENTARY PATH)
Example 9:
Given a graph G as below. Determine whether the path given is a simple path
or elementary path or both.

a) P1: KL → JB → KB → KL → GT SIMPLE PATH

b) P2: AS → KB → KL → JB SIMPLE PATH


ELEMENTARY PATH
c) P3: KB → KL → AS → KB → KL → JB
NOT SIMPLE PATH
NOT ELEMENTARY PATH
PATH IN DIRECTED GRAPH
(SIMPLE PATH VS ELEMENTARY PATH)
Example 10:
For the graph given below, obtain an elementary path of length 6 that begins at
vertex a.

a–f–d–c–g–b–e
or
a–f–d–c–g–e–b
PATH IN DIRECTED GRAPH
• Hamiltonian Path
– A path that visits each node in the graph exactly one time
– may not pass through all the edges in the graph
• Hamiltonian Cycle
– A path that starts and ends at the same node, visits each node in the
graph exactly one time
– except for the initial node which is visited twice; at the beginning and
end of the cycle
PATH IN DIRECTED GRAPH
Example 11:
Given a graph G as below. Determine whether a Hamiltonian path and a
Hamiltonian circuit exist. If it exists, give an example of the path/circuit. If it
doesn’t, state the reason(s).
a) b) c)

Hamiltonian path exist. A – B – C – D – A


Hamiltonian circuit exist. A – B – C – D
Hamiltonian path exist. a – b – c – d – e Hamiltonian path/circuit not exist
Hamiltonian circuit not exist because the because the path may pass node B
path may pass node b twice twice
PATH IN DIRECTED GRAPH
• Eulerian Path
– A path that passes through all the edges in the graph exactly one time
– may visits each node more than once
• Eulerian Cycle
– A path that starts and ends at the same node, passes through all the edges
in the graph exactly one time
– except for the initial node which is visited twice; at the beginning and
end of the cycle
PATH IN DIRECTED GRAPH
• Tips for Eulerian Path / Circuit
– Eulerian Cycle exist if each node has an even number of degrees. The
path can begin at any nodes and ends at the same node as it begins.
– Eulerian Path exist if all nodes except two have an even number of
degrees (exactly two nodes have odd number of degrees). The path must
begin at the odd degree node and end at another odd degree node.
PATH IN DIRECTED GRAPH
Example 12:
Given a graph G as below. Determine whether an Eulerian path and an Eulerian
circuit exist. If it exists, give an example of the path/circuit. If it doesn’t, state the
reason(s).
a) b)

Eulerian path exist. 3 – 2 – 1 – 3 – 4 – 1 – 5 – 4 Eulerian path not exist because the path not have exactly two
Eulerian circuit not exist because the path have odd odd degree vertices
degree vertices Eulerian circuit exist.
a–b–c–f–i–h–g–d–b–e–f–h–e–d–a
EXERCISES
1) Given a graph G as below.

a) Determine whether the graph is connected or disconnected. State the reason(s)


for your answer.
b) Find the in-degree and out-degree of each vertices in your graph.
EXERCISES
2) Refer to the graph below. Give

a) a connected subgraph with 5 edges.


b) an elementary cycle of length 6.
c) A simple but not elementary path that contains 6 different vertices.
EXERCISES
3) Given a graph G as below.

a) From the graph above, determine whether a Hamiltonian path, a Hamiltonian circuit, an
Eulerian path and an Eulerian circuit exist. If it exists, give an example of the
path/circuit. If it doesn’t, state the reason(s).
b) Draw a subgraph G1=G\{b, e}.
EXERCISES
4) For the graph given below,

a) determine a Hamiltonian cycle. Give a reason if it doesn’t exist.


b) State the degree of each vertex. Hence, obtain an Eulerian path of the graph. Give a
reason if it does not exist.
TREES
INTRODUCTION
• A tree is a special graph and relation
• Examples of trees: electric network, transportation network, how computer
sorts and search
• A tree is a connected graph but has no circuit. (non-circuit graph)

Example 13:
Determine if the following graph is a connected graph or not.
a) YES b) c) NO
NO
TREES
SPANNING TREE
• A spanning tree of a graph is a subgraph of G that contains all vertices of G
• A graph will have several spanning tree. Spanning trees are not unique.
• A spanning tree will have the least number of edges that connects all nodes
in the original graph.
• Example:

An example of spanning tree


TREES
Example 14:
For the graph given below, give an example of a spanning tree rooted at d.
TREES
Example 15:
Figure below represents the graph G = (V, E). Give an example of a spanning tree
rooted at G with the height of 3.
TREES
ROOTED TREE
• A rooted tree is a tree in which a particular node is designated the root.
• The number of arcs going to root is 0.
• Rooted tree are typically drawn with their roots at the top
• The level of the root is level 0.
• The nodes under the root are said to be on level 1 and so on
• The node of a root is the parent and the nodes at level 1 downwards are called descendent.
• Node at level 1 are children for root.
• The nodes related to same parents are siblings.
• The height of a rooted tree is the maximum level number that occurs.
• Leaf is a node that has no children.
• Subtree - any node can be considered to be the root of a subtree, which consists of its
children and its children’s children and so on.
TREES
Example 16:
For the graph given below, give an example of a subtree rooted at c.
TREES
BINARY TREE
• A binary tree is a tree in which each vertex either has no children, one child
or two children.
TREES
•Example
  17:
Draw a binary tree for the following expression. State the leaves and height of the
tree.
1)
2)
3)
TREES
HUFFMAN CODE
• Example of binary tree

Example 15:
Based on Huffman Code given,
a) Decode 110000001000, 110011001101, 1000100111101
b) Encode FLEET, BELL, FLOUT
TREES
HUFFMAN CODE
• Example of binary tree

Example 18:
Based on Huffman Code given,
a) Decode 110000001000, 110011001101, 1000100111101
b) Encode FLEET, BELL, FLOUT
1) BOOT, BLUE, TUBE
2) 010011011011000, 11101001001, 0100100010011000
EXERCISES
•   Given the following algebraic expression.
1)

a) Draw a rooted tree to represent the given algebraic expression.


b) Determine the root of the tree.
c) List all the nodes al level 2.
d) List all the leaves.
e) List all the descendants of node .
f) State the height of the rooted tree.
EXERCISES
2) Based on Huffman Code given, decode the following

a) 011000010
b) 01110100110
c) 01111001001110
d) 1110011101001111
PEN, LAP, DEAL, SALAD

You might also like