Discrete Mathematics
5. Graphs & Trees
What are Graphs?
Not
General meaning in everyday math:
A plot or chart of numerical data using a coordinate
system.
Technical meaning in discrete mathematics:
A particular class of discrete structures (to be
defined) that is useful for representing relations and
has a convenient webby-looking graphical
representation.
Discrete Mathematics, Spring 2009
Simple Graphs
Visual Representation
of a Simple Graph
The graph in which each edge connects two
different vertices and where no two edges
connect the same pair of vertices
Correspond to symmetric binary relations R.
Discrete Mathematics, Spring 2009
Simple Graphs
Visual Representation
of a Simple Graph
Definition:
A simple graph G=(V,E)
consists of:
a set V of vertices or nodes (V corresponds to the
universe of the relation R), and
a set E of edges / arcs / links: unordered pairs of
[distinct?] elements u,v V, such that uRv.
Discrete Mathematics, Spring 2009
Example of a Simple Graph
Let V be the set of states in the far-southeastern U.S.:
V={FL, GA, AL, MS, LA, SC, TN, NC}
Let E={{u,v}|u adjoins v}
={{FL,GA},{FL,AL},{FL,MS},
{FL,LA},{GA,AL},{AL,MS},
{MS,LA},{GA,SC},{GA,TN},
{SC,NC},{NC,TN},{MS,TN},
{MS,AL}}
TN
MS
AL
NC
SC
GA
LA
Discrete Mathematics, Spring 2009
FL
Multigraphs
Like simple graphs, but there may be more than one
edge connecting two given nodes.
Definition:
A multigraph G=(V, E, f ) consists of a set V of vertices, a
set E of edges (as primitive objects), and a function
f:E{{u,v}|u,vV uv}.
Parallel
edges
Example:
nodes are cities and
edges are segments of major highways.
Discrete Mathematics, Spring 2009
Pseudographs
Like a multigraph, but edges connecting a
node to itself are allowed.
Definition:
A pseudograph G=(V, E, f ) where
f:E{{u,v}|u,vV}. Edge eE is a loop if loop
f(e)={u,u}={u}.
Example:
nodes are campsites
in a state park and edges are
hiking trails through the woods.
Discrete Mathematics, Spring 2009
Directed Graphs
Correspond to arbitrary binary relations R,
which need not be symmetric.
Definition:
A directed graph (V, E) consists of a set of vertices
V and a binary relation E on V.
Example:
V = people, E={(x,y) | x loves y}
Discrete Mathematics, Spring 2009
Walk, loop, sling, and path
Definition:
A walk is a sequence x0, x1, , xn of the vertices of
a digraph such that xixi+1, 0in-1, is an edge.
The length of a walk is the number of edges in the
walk.
If a walk holds xixj (ij) i,j=0, , n, except x0, xn(i.e
x0=xn), the walk is called a cycle.
A loop is a cycle of length one.
A sling is a cycle of length two.
A walk is a path if no edge is repeated more than
once.
Discrete Mathematics, Spring 2009
Directed Multigraphs
Like directed graphs, but there may be more than one
arc from a node to another.
Definition:
A directed multigraph G=(V, E, f ) consists of a set V of
vertices, a set E of edges, and a function f:EVV.
Example:
The WWW is a directed multigraph.
V=web pages, E=hyperlinks.
Discrete Mathematics, Spring 2009
Types of Graphs: Summary
Keep in mind this terminology is not fully standardized...
T er m
S im p le g rap h
M u ltig rap h
P seu d o g rap h
D irected g rap h
D irected m u ltig rap h
E dge
ty p e
U n d ir.
U n d ir.
U n d ir.
D irected
D irected
M u ltip le
ed g es o k?
No
Y es
Y es
No
Y es
Discrete Mathematics, Spring 2009
S elflo o p s o k?
No
No
Y es
Y es
Y es
Graph Terminology
Adjacent, connects, endpoints, degree, initial,
terminal, in-degree, out-degree, complete, cycles,
wheels, n-cubes, bipartite, subgraph, and union.
Discrete Mathematics, Spring 2009
Adjacency
Let G be an undirected graph with edge set E. Let
eE be (or map to) the pair {u,v}. Then we say:
u, v are adjacent / neighbors / connected.
Edge e is incident with vertices u and v.
Edge e connects u and v.
Vertices u and v are endpoints of edge e.
Discrete Mathematics, Spring 2009
Degree of a Vertex
Let G be an undirected graph, vV a vertex.
The degree of v, deg(v), is its number of incident
edges. (Except that any self-loops are counted
twice.)
A vertex with degree 0 is isolated.
A vertex of degree 1 is pendant.
Discrete Mathematics, Spring 2009
Handshaking Theorem
Theorem:
Let G be an undirected (simple, multi-, or
pseudo-) graph with vertex set V and edge set E.
Then
deg(v) = 2 E
vV
Corollary:
Any undirected graph has an even number of
vertices of odd degree.
Discrete Mathematics, Spring 2009
Directed Adjacency
Let G be a directed (possibly multi-) graph, and
let e be an edge of G that is (or maps to) (u,v).
Then we say:
u is adjacent to v, v is adjacent from u
e comes from u, e goes to v.
e connects u to v, e goes from u to v
the initial vertex of e is u
the terminal vertex of e is v
Discrete Mathematics, Spring 2009
Directed Degree
Definition:
Let G be a directed graph, v a vertex of G.
The in-degree of v, deg(v), is the number of
edges going to v.
The out-degree of v, deg+(v), is the number of
edges coming from v.
The degree of v, deg(v)deg(v)+deg+(v), is the
sum of vs in-degree and out-degree.
Discrete Mathematics, Spring 2009
Directed Handshaking Theorem
Theorem:
Let G be a directed (possibly multi-) graph with
vertex set V and edge set E. Then:
1
deg (v) = deg (v) = deg(v) = E
2 vV
vV
vV
Note that the degree of a node is unchanged by
whether we consider its edges to be directed or
undirected.
Discrete Mathematics, Spring 2009
Special Graph Structures
Special cases of undirected graph structures:
Complete Graphs Kn
Cycles Cn
Wheels Wn
n-Cubes Qn
Bipartite Graphs
Complete Bipartite Graphs Km,n
Discrete Mathematics, Spring 2009
Complete Graphs
Definition:
K1
For any nN, a complete graph on n vertices, Kn,
is a simple graph with n nodes in which every
node is adjacent to every other node: u,vV:
uv{u,v}E.
K2
K3
K4
n 1
K5
Note that Kn has i = n(n2 1) edges.
i =1
Discrete Mathematics, Spring 2009
K6
Cycles
Definition:
C3
For any n3, a cycle on n vertices, Cn, is a simple
graph where V={v1,v2, ,vn} and
E={{v1,v2},{v2,v3},,{vn1,vn},{vn,v1}}.
C4
C5
C6
C7
How many edges are there in Cn?
Discrete Mathematics, Spring 2009
C8
Wheels
Definition:
For any n3, a wheel Wn, is a simple graph obtained by
taking the cycle Cn and adding one extra vertex vhub
and n extra edges {{vhub,v1}, {vhub,v2},,{vhub,vn}}.
W3
W4
W5
W6
W7
How many edges are there in Wn?
Discrete Mathematics, Spring 2009
W8
n-Cubes (hypercubes)
Definition:
A graph that has vertices representing the 2n bit
strings of length n
For any nN, the hypercube Qn is a simple
graph consisting of two copies of Qn-1 connected
together at corresponding nodes. Q0 has 1 node.
Q0
Q1
Q2
Q3
Q4
Number of vertices: 2n. Number of edges:Exercise to try!
Discrete Mathematics, Spring 2009
n-Cubes (hypercubes)
Definition:
For any nN, the hypercube Qn can be defined
recursively as follows:
Q0=({v0},) (one node and no edges)
For any nN, if Qn=(V,E), where V={v1,,va} and
E={e1,,eb}, then Qn+1=(V{v1,,va},
E{e1,,eb}{{v1,v1},{v2,v2},,
{va,va}}) where v1,,va are new vertices, and
where if ei={vj,vk} then ei={vj,vk}.
Discrete Mathematics, Spring 2009
Bipartite Graphs
Definition:
A simple graph G is called bipartite if its vertex
set V can be partitioned into two disjoint sets V1
and V2 such that every edge in the graph
connects a vertex in V1 and a vertex in V2 (so
that no edge in G connects either two vertices in
V1 or two vertices in V2)
V1
V2
a bipartite
Discrete Mathematics, Spring 2009
Complete Bipartite Graphs
Definition:
Let m, n be positive integers. The complete
bipartite graph Km,n is the graph whose vertices
can be partitioned V = V1 V2 such that
1.
2.
3.
4.
|V1| = m
|V2| = n
For all x V1 and for all y V2, there is an
edge between x and y
No edge has both its endpoints in V1 or both its
endpoints in V2
Discrete Mathematics, Spring 2009
Complete Bipartite Graphs (cont.)
V1
V2
K2,3
V1
V2
K3,4
Discrete Mathematics, Spring 2009
Subgraphs
Definition:
A subgraph of a graph G=(V,E) is a graph
H=(W,F) where WV and FE.
H
Discrete Mathematics, Spring 2009
Graph Unions
Definition:
The union G1G2 of two simple graphs G1=(V1,
E1) and G2=(V2,E2) is the simple graph (V1V2,
E1E2).
Discrete Mathematics, Spring 2009
Graph Representations & Isomorphism
Graph representations:
Adjacency lists.
Adjacency matrices.
Incidence matrices.
Graph isomorphism:
Two graphs are isomorphic iff they are identical
except for their node names.
Discrete Mathematics, Spring 2009
Adjacency Lists
A table with 1 row per vertex, listing its adjacent
vertices.
A way to represent a graph w/ no multiple edges
Vertex Adjacent Vertices
a
c
e
f
a
b
c
d
e
f
Discrete Mathematics, Spring 2009
b, c
a, c, e, f
a, b, f
b
c, b
Directed Adjacency Lists
1 row per node, listing the terminal nodes of
each edge incident from that node.
Discrete Mathematics, Spring 2009
Adjacency Matrices
Matrix A=[aij], where aij is 1 if {vi, vj} is an edge
of G, 0 otherwise.
Simple graph representation
Discrete Mathematics, Spring 2009
Adjacency Matrices
Matrix M=[mij], where
mij = 1 when edge ej is incident with vi,
mij = 0 otherwise.
A way of represent graphs
can be used to represent multiple edges and
loops
Discrete Mathematics, Spring 2009
Graph Isomorphism
Definition:
Simple graphs G1=(V1, E1) and G2=(V2, E2) are isomorphic
iff a bijection f:V1V2 such that a,bV1, a and b are
adjacent in G1 iff f(a) and f(b) are adjacent in G2.
f is the renaming function that makes the two
graphs identical.
Definition can easily be extended to other types of
graphs.
Discrete Mathematics, Spring 2009
Graph Invariants under Isomorphism
Graph Invariant
a property preserved by isomorphism of graphs
Necessary but not sufficient conditions for G1=(V1,
E1) to be isomorphic to G2=(V2, E2):
|V1|=|V2|, |E1|=|E2|.
The number of vertices with degree n is the same
in both graphs.
For every proper subgraph g of one graph, there
is a proper subgraph of the other graph that is
isomorphic to g.
Discrete Mathematics, Spring 2009
Isomorphism Example
If isomorphic, label the 2nd graph to show the
isomorphism, else identify difference.
d
b
a
e
e
c
f
Discrete Mathematics, Spring 2009
Are These Isomorphic?
If isomorphic, label the 2nd graph to show the
isomorphism, else identify difference.
* Same # of vertices
* Same # of edges
* Different # of verts of
degree 2! (1 vs 3)
d
c
Discrete Mathematics, Spring 2009
Connectedness
Definition:
A path of length n from u to v in G is
a sequence of n edges e1, e2, , en such that e1 is
associated with {x0, x1}, e2 is associated with {x1, x2}, and
so on, with en associated with {xn-1,xn}, where x0=u and
xn=v;
The path is circuit if it begins and ends at the same
vertex, that is u=v, and has length greater than zero.
A path or circuit is simple if it does not contain the
same edge more than once.
Discrete Mathematics, Spring 2009
Connectedness
Definition:
An undirected graph is connected iff there is a path
between every pair of distinct vertices in the graph.
Theorem:
There is a simple path between any pair of vertices in a
connected undirected graph.
Discrete Mathematics, Spring 2009
Directed Connectedness
Definition:
A directed graph is strongly connected iff there is
a directed path from a to b for any two vertices a
and b.
It is weakly connected iff the underlying undirected
graph (i.e., with edge directions removed) is
connected.
Note strongly implies weakly but not vice-versa.
Discrete Mathematics, Spring 2009
Paths & Isomorphism
Note that connectedness, and the existence of a
circuit or simple circuit of length k are graph
invariants with respect to isomorphism.
Discrete Mathematics, Spring 2009
Euler & Hamilton Paths
Definition:
An Euler circuit in a graph G is a simple circuit
containing every edge of G.
An Euler path in G is a simple path containing every
edge of G.
Examples:
e
d
a, e, c, d, e, b, a
a, c, d, e, b, d, a, b
Euler circuit
Euler path
Discrete Mathematics, Spring 2009
Euler & Hamilton Paths (cont.)
Definition:
A Hamilton circuit is a simple circuit that traverses
each vertex in G exactly once.
A Hamilton path is a simple path that traverses each
vertex in G exactly once.
Examples:
a
c
d
a, b, c, d, e, a
a, b, c, d
Hamilton circuit
Hamilton path
Discrete Mathematics, Spring 2009
Trees
Definition:
A tree is a connected undirected graph with no simple
circuits.
Since a tree cannot have a circuit, a tree cannot contain
multiple edges or loops.
Therefore, any tree must be a simple graph.
Theorem:
An undirected graph is a tree if and only if there is a
unique simple path between any two of its vertices.
In general, we use trees to represent hierarchical structures.
Discrete Mathematics, Spring 2009
Trees
Example: Are the following graphs trees?
Yes.
No.
Yes.
Discrete Mathematics, Spring 2009
No.
Root & Rooted tree
We often designate a particular vertex of a tree
as the root. Since there is a unique path from
the root to each vertex of the graph, we direct
each edge away from the root.
Definition:
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
Discrete Mathematics, Spring 2009
Tree Terminology
Definition:
If v is a vertex in a rooted tree 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.
Vertices with the same parent 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.
Discrete Mathematics, Spring 2009
Tree Terminology (cont.)
Definition:
The descendants of a vertex v are those vertices that
have v as an ancestor.
A vertex of a tree is called a leaf if it has no children.
Vertices that have children are called internal
vertices.
If a is a vertex in a tree, then the subtree with a as its
root is the subgraph of the tree consisting of a and its
descendants and all edges incident to these
descendants.
Discrete Mathematics, Spring 2009
Tree Terminology
Definition:
The level of a vertex v in a rooted tree is the
length of the unique path from the root to this
vertex.
The level of the root is defined to be zero.
The height of a rooted tree is the maximum of
the levels of vertices.
Discrete Mathematics, Spring 2009
Trees
Example 1: Family tree
James
Christine
Frank
Joyce
Discrete Mathematics, Spring 2009
Bob
Petra
Trees (cont.)
Example 2: File system
usr
bin
bin
spool
ls
Discrete Mathematics, Spring 2009
temp
Trees (cont.)
Example 3: Arithmetic expressions
y
This
tree represents the expression (y + z)(x - y).
Discrete Mathematics, Spring 2009
m-ary tree
Definition:
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.
Theorem:
A tree with n vertices has (n 1) edges.
A full m-ary tree with i internal vertices contains n =
mi + 1 vertices.
Discrete Mathematics, Spring 2009
Ordered Rooted Tree
Definition:
An ordered rooted tree is a rooted tree where
the children of each internal vertex are ordered.
For example, in an ordered binary tree (just called
binary tree), if an internal vertex has two children,
the first child is called the left child and the
second is called the right child.
the tree rooted at the left child is called the left
subtree, and at the right child, the right subtree
Ordered rooted trees can be defined recursively.
Discrete Mathematics, Spring 2009
Ordered Rooted Tree (binary tree)
Left child of the
Vertex a
Left subtree of
the Vertex b
Right child of
the Vertex a
b
f
d
i
h
k
l
Discrete Mathematics, Spring 2009
Right subtree of
the Vertex b
Tree Traversal
Procedures for systematically visiting every
vertex of an ordered rooted tree are called
traversal algorithms.
Discrete Mathematics, Spring 2009
Preorder traversal
Definition:
Let T be an ordered rooted
tree with root r.
If T consists only of r, then r is
the preorder traversal of T.
Otherwise, suppose that T1,
T2, , Tn are the subtrees at r
from left to right in T. The
T1
preorder traversal begins by
visiting r. It continues by
traversing T1 in preorder,
Step 2
then T2 in preorder, and so on, Visit T1 in
preorder
until Tn is traversed in
preorder.
Discrete Mathematics, Spring 2009
T2
step 1
Visit r
...
Tn
step 3
Step n+1
Visit T2 in
preorder
Visit Tn in
preorder
Inorder traversal
Definition:
Let T be an ordered rooted
tree with root r.
If T consists only of r, then r is
the inorder traversal of T.
Otherwise, suppose that T1,
T2, , Tn are the subtrees at r
from left to right. The inorder
T1
traversal begins by traversing
visiting T1 in inorder, then
visiting r. It continues by
Step 1
traversing T2 in inorder, then Visit T1 in
inorder
T3 in inorder, , and finally
Tn in inorder
Discrete Mathematics, Spring 2009
T2
step 2
Visit r
...
Tn
step 3
Step n+1
Visit T2 in
inorder
Visit Tn in
inorder
Postorder traversal
Definition:
Let T be an ordered rooted
tree with root r.
If T consists only of r, then r is
the postorder traversal of T.
Otherwise, suppose that T1, T2,
, Tn are the subtrees at r
from left to right. The postorder
T1
traversal begins by traversing
T1 in postorder, then T2 in
postorder, , then Tn in
Step 1
postorder, and ends by
Visit T1 in
postorder
visiting r.
Discrete Mathematics, Spring 2009
T2
step n+1
Visit r
...
Tn
step 2
Step n
Visit T2 in
postorder
Visit Tn in
postorder
Traversal Example
a
e
f
Preorder : a, b, e, j, k, n, o,
p, f, c, d, g, l, m, h, I
Inorder : j, e, n, k, o, p, b, f,
a, c, l, g, m, d, h, i
Postorder : j, n, o, p, k, e, f,
b, c, l, m, g, h, i, d, a
Discrete Mathematics, Spring 2009