Module 22 Graphs
Module 22 Graphs
University of Florida
Dept. of Computer & Information Science & Engineering
COT 3100
Applications of Discrete Structures
Dr. Michael P. Frank
Module #22:
Graph Theory
Applications of Graphs
• Potentially anything (graphs can represent
relations, relations can describe the
extension of any predicate).
• Apps in networking, scheduling, flow
optimization, circuit design, path planning.
• More apps: Geneology analysis, computer
game-playing, program compilation, object-
oriented design, …
12/01/22 (c)2001-2003, Michael P. Frank 4
Module #22 - Graphs
Simple Graphs
• Correspond to symmetric,
irreflexive binary relations R.
• A simple graph G=(V,E)
Visual Representation
consists of: of a Simple Graph
– a set V of vertices or nodes (V corresponds to
the universe of the relation R),
– a set E of edges / arcs / links: unordered pairs
of [distinct] elements u,v V, such that uRv.
Multigraphs
• Like simple graphs, but there may be more
than one edge connecting two given nodes.
• A multigraph G=(V, E, f ) consists of a set
V of vertices, a set E of edges (as primitive
objects), and a function Parallel
f:E{{u,v}|u,vV uv}. edges
Pseudographs
• Like a multigraph, but edges connecting a node to
itself are allowed. (R may be reflexive.)
• A pseudograph G=(V, E, f ) where
f:E{{u,v}|u,vV}. Edge eE is a loop if
f(e)={u,u}={u}.
• E.g., nodes are campsites
in a state park, edges are
hiking trails through the
woods.
Directed Graphs
• Correspond to arbitrary binary relations R,
which need not be symmetric.
• A directed graph (V,E) consists of a set of
vertices V and a binary relation E on V.
• E.g.: V = set of People,
E={(x,y) | x loves y}
Directed Multigraphs
• Like directed graphs, but there may be more
than one arc from a node to another.
• A directed multigraph G=(V, E, f ) consists
of a set V of vertices, a set E of edges, and a
function f:EVV.
• E.g., V=web pages,
E=hyperlinks. The WWW is
a directed multigraph...
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.
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 called isolated.
• A vertex of degree 1 is called pendant.
Handshaking 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.
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
Directed Degree
• 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 v’s in-degree and out-degree.
vV
2 vV
• Note that the degree of a node is unchanged
by whether we consider its edges to be
directed or undirected.
Complete Graphs
• 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.
K1 K2 K3 K4
K5 K6
n 1
n(n 1)
Note that Kn has i 2 edges.
i 1
Cycles
• 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}}.
C3 C4 C5 C6 C8
C7
How many edges are there in Cn?
Wheels
• 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 W8
W7
How many edges are there in Wn?
12/01/22 (c)2001-2003, Michael P. Frank 22
Module #22 - Graphs
n-cubes (hypercubes)
• 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 Q4
Q3
Number of vertices: 2n. Number of edges:Exercise to try!
n-cubes (hypercubes)
• 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´}.
Bipartite Graphs
• Def’n.: A graph G=(V,E) is bipartite (two-
part) iff V = V1∩V2 where V1V2= and
eE: v1V1,v2V2: e={v1,v2}.
• In English: The graph can
be divided into two parts
in such a way that all edges
go between the two parts. V1 V2
This definition can easily be adapted for the
case of multigraphs and directed graphs as well. Can represent with
zero-one matrices.
12/01/22 (c)2001-2003, Michael P. Frank 25
Module #22 - Graphs
Subgraphs
• A subgraph of a graph G=(V,E) is a graph
H=(W,F) where WV and FE.
G H
Graph Unions
• The union G1G2 of two simple graphs
G1=(V1, E1) and G2=(V2,E2) is the simple
graph (V1V2, E1E2).
a
a b c b c
d e d f
Adjacency Lists
• A table with 1 row per vertex, listing its
adjacent vertices. Adjacent
a b Vertex Vertices
a b, c
c d b a, c, e, f
e
f c a, b, f
d
e b
f c, b
Adjacency Matrices
• A way to represent simple graphs
– possibly with self-loops.
• Matrix A=[aij], where aij is 1 if {vi, vj} is an
edge of G, and is 0 otherwise.
• Can extend to pseudographs by letting each
matrix elements be the number of links
(possibly >1) between the nodes.
Graph Isomorphism
• Formal 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 between the two node
sets that makes the two graphs identical.
– This definition can easily be extended to other
types of graphs.
12/01/22 (c)2001-2003, Michael P. Frank 33
Module #22 - Graphs
Isomorphism Example
• If isomorphic, label the 2nd graph to show
the isomorphism, else identify difference.
b d
a b a
d c
e
e c f
f
§8.4: Connectivity
• In an undirected graph, a path of length n
from u to v is a sequence of adjacent edges
going from vertex u to vertex v.
• A path is a circuit if u=v.
• A path traverses the vertices along it.
• A path is simple if it contains no edge more
than once.
Connectedness
• 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.
• Connected component: connected subgraph
• A cut vertex or cut edge separates 1
12/01/22 connected component into
(c)2001-2003, Michael 2 if removed.
P. Frank 39
Module #22 - Graphs
Directed Connectedness
• A directed graph is strongly connected iff
there is a directed path from a to b for any
two verts 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.
12/01/22 (c)2001-2003, Michael P. Frank 40
Module #22 - Graphs
B D
Round-the-World Puzzle
• Can we traverse all the vertices of a
dodecahedron, visiting each once?`
HAM-CIRCUIT is NP-complete
• Let HAM-CIRCUIT be the problem:
– Given a simple graph G, does G contain a
Hamiltonian circuit?
• This problem has been proven to be
NP-complete!
– This means, if an algorithm for solving it in
polynomial time were found, it could be used to
solve all NP problems in polynomial time.
Rooted Trees
• A rooted tree is a tree in which one node
has been designated the root.
– Every edge is (implicitly or explicitly) directed
away from the root.
• You should know the following terms about
rooted trees:
– Parent, child, siblings, ancestors, descendents,
leaf, internal node, subtree.
n-ary trees
• A rooted tree is called n-ary if every vertex
has no more than n children.
– It is called full if every internal (non-leaf)
vertex has exactly n children.
• A 2-ary tree is called a binary tree.
– These are handy for describing sequences of
yes-no decisions.
• Example: Comparisons in binary search algorithm.
Trees as Models
• Can use trees to model the following:
– Saturated hydrocarbons
– Organizational structures
– Computer file systems
• In each case, would you use a rooted or a
non-rooted tree?
Coin-Weighing Problem
• Imagine you have 8 coins, one
of which is a lighter counterfeit,
and a free-beam balance.
– No scale of weight markings
is required for this problem!
• How many weighings are
?
needed to guarantee that the
counterfeit coin will be found?
12/01/22 (c)2001-2003, Michael P. Frank 69
Module #22 - Graphs
As a Decision-Tree Problem
• In each situation, we pick two disjoint and equal-
size subsets of coins to put on the scale.
The balance then A given sequence of
“decides” whether weighings thus yields
to tip left, tip right, a decision tree with
or stay balanced. branching factor 3.
General Solution
Strategy
• The problem is an example of searching for 1 unique particular
item, from among a list of n otherwise identical items.
– Somewhat analogous to the adage of “searching for a needle in haystack.”
• Armed with our balance, we can attack the problem using a divide-
and-conquer strategy, like what’s done in binary search.
– We want to narrow down the set of possible locations where the desired
item (coin) could be found down from n to just 1, in a logarithmic fashion.
• Each weighing has 3 possible outcomes.
– Thus, we should use it to partition the search space into 3 pieces that are as
close to equal-sized as possible.
• This strategy will lead to the minimum possible worst-case number
of weighings required.
12/01/22 (c)2001-2003, Michael P. Frank 72
Module #22 - Graphs
Game Trees
• pp. 651-656