Professional Documents
Culture Documents
Graphs and Trees
Graphs and Trees
AQ010-3-1 (Version E)
Example: Now suppose that a network is made up of data centers and communication
links between computers. We can represent the location of each data center by a point
and each communications link by a line segment, as shown in Figure 1.
(a loop: )
a b Sol : deg(a)=4
c
deg(b)=6
deg(c)=1
deg(d)=5
e d f
deg(e)=6
H deg(f)=0
Example 2: Find the in-degree and out-degree of each vertex in the graph
with directed edges shown below.
In –degree Out–degree
Let G = (V, E) be an undirected graph with e edges (i.e., |E| = e). Then
deg(v) 2e
vV
Example 3:
a b c
The graph H has 11 edges, and
deg(v) 22
vV
e d f
H
AQ010-3-1-MCFC Graphs and Trees SLIDE 19
The Handshaking Theorem (Cont.…)
Example 4: How many edges are there in a graph with 10 vertices each of
degree six?
Sol :10 6 = 2e e=30
Example 5: How many edges are there in a graph with 5 vertices each of
degree 3?
Sol :5 3 = 2e e=7.5 Graph doesn’t exist.
Example 7: Use adjacency lists to describe the simple graph given below.
b
Initial Terminal vertices
c vertex
a
a b,c,d,e
b b,d
c a,c,e
d -
e d e b,c,d
a a b c d
b a 0
1 1 1
1 0 1 1
b
A1
c 1 1 0 0
c d
d 1 1 0 0
Note:
1.There are n! different adjacency matrices for a graph with n vertices.
2.The adjacency matrix of an undirected graph is symmetric.
1 , if
vi vj
aij =
0 , otherwise
AQ010-3-1-MCFC Graphs and Trees SLIDE 25
Graph Representation
Example 11: Use adjacency list and adjacency matrix to represent the graph:
a b c
e d
• 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.
• The path is a circuit if it begins and ends at the same vertex.
a d
b
AQ010-3-1-MCFC Graphs and Trees SLIDE 29
Euler Paths and Circuits
a b a b a b
e e
d c d c c d e
Find the
a) Degree
b) Adjacency List
c) Adjacency Matrix
d) Euler Circuit
e) Euler Path
Yes; this is a circuit that passes through each vertex exactly once.
If a graph G has a Hamilton circuit, then G has a subgraph H with the following
properties:
1. H contains every vertex of G.
2. H is connected.
3. H has the same number of edges as vertices.
4. Every vertex of H has degree 2.
Answer:
The shortest path is : a-d-e-z and the length of the shortest path from a to z is 6
(2+3+1).
• To solve this problem we can assume the salesman starts in Detroit (because this must
be part of the circuit) and examine all possible ways for him to visit the other four
cities and then return to Detroit (starting elsewhere will produce the same circuits).
• There are a total of 24 such circuits, but because we travel the same distance when we
travel a circuit in reverse order, we need only consider 12 different circuits to find the
minimum total distance he must travel.
• We list these 12 different circuits and the total distance traveled for each circuit.
• As can be seen from the list, the minimum total distance of 458 miles is traveled using
the circuit Detroit-Toledo-Kalamazoo-Grand Rapids-Saginaw-Detroit (or its reverse).
Example 18: What are the chromatic numbers of the graph G shown in
below Figure? Solution
Example 19: What are the chromatic numbers of the graph H shown in below
Figure?
Solution
Theorem: A undirected graph is a tree if and only if there is a unique simple path
between any of its vertices.
Example 1. Which of the graphs are trees?
Sol: G1, G2
AQ010-3-1-MCFC Graphs and Trees SLIDE 57
Tree Terminology
• If v is a vertex of tree T other than the root, the parent of v is the unique vertex u
such that there is a directed edge from u to v.
• When u is the parent of v, v is called the child of u.
• If two vertices share the same parent, then they are called siblings.
• The ancestors of a vertex other than the root are the vertices in the path from the
root to this vertex, excluding the vertex itself and including the root.
• The descendants of a vertex v are those vertices that have v as an ancestor.
• A vertex with no children is called a leaf.
• Vertices with children are called internal vertices.
Root a
b c d
e f g h
i j k l m
Siblings b c d
e f g h
i j k l m
Leaves a
b c d
e f g h
i j k l m
a
Internal vertices
b c d
e f g h
i j k l m
Subtree at b Subtree at d
b c d
e f g h
i j k l m
AQ010-3-1-MCFC Graphs and Trees SLIDE 63
Tree Traversal
• Ordered trees are often used to restore data/info.
• Tree traversal is a procedure for systematically visiting each vertex of an
ordered rooted tree to access data.
• Tree traversal algorithm
– Preorder, inorder and postorder traversal
r STEP 1
Visit r TIPS
Preorder Traversal:
Visit root, visit
subtrees left to right
T1 T2 … Tn
r STEP 2
Visit r TIPS
Inorder Traversal:
Visit leftmost
subtree, Visit root,
T1 T2 … Tn Visit other subtrees
left to right.
STEP 1 STEP 3 STEP n+1
Visit T1 in inorder Visit T2 in inorder Visit Tn in inorder
AQ010-3-1-MCFC Graphs and Trees SLIDE 67
EXAMPLE: Inorder Traversal
a e b f a c g d h i
T b d
c
• • • • • • •
e f k m
g h i j l
j k m
l n ITERATION 2
o p
n
o p j e k b f a c l g m d h i
• • • • • •• • • • • •
b a c d
• • n ITERATION 3
o p
e f
g h i
k j e n k opb f ac l g m d h i
j m
l •• • •••• ••••• • • • •
ITERATION 4
n
o p
ITERATION 1
The inorder traversal of T
AQ010-3-1-MCFC Graphs and Trees SLIDE 68
Postorder Traversal
• Let T be an ordered rooted tree with root r.
– If T consists only of r, then r is the postorder traversal of T.
– If T1, T2, …, Tn are subtrees at r from left to right in T, then the preorder
traversal begins by traversing T1 in postorder, then T2 in postorder, and so
on until Tn is traversed in postorder and ends by visiting r.
r STEP n+1
Visit r TIPS
Postorder Traversal:
Visit subtrees left to
right, Visit root.
T1 T2 … Tn
e f
g h i
k j n o pk e f bc l m g hi d a
j m
l •• • ••• ••• •• • •• • •
ITERATION 4
n
o p
ITERATION 1
The postorder traversal of T
AQ010-3-1-MCFC Graphs and Trees SLIDE 70
Summary
• Introduction
• Definition
• Degree of a Vertex
• Graphs & Representations
• Paths & Circuits
• Shortest path
• Graph coloring
• Planar Graph
• Tree terminology
• Tree traversals
• Spanning tree