L18 Graph
L18 Graph
L18 Graph
1
The Seven Bridges of Königsberg
▪ Q: Can you find a path to cross all seven bridges,
each exactly once?
D
D
A B
A
B
C
C
2
The Traveling Salesman Problem
▪ Q: How to visit all places with the shortest total
distance, and come back to origin?
hbo.com
sorpranos.com
4
Outline
▪ Definition of graphs
▪ Representing Graphs
▪ Connectivity
▪ Euler Paths and Circuits
5
Graphs
▪ Definition
A graph 𝐺 = (𝑉, 𝐸) consists of a nonempty set 𝑉 of
vertices (or nodes) and a set 𝐸 of edges. An edge is
said to connect its two endpoints. The endpoints
connected by an edge are called adjacent (or
neighbors), and the edge is incident to its endpoints.
a b
d c
6
Types of Edges
▪ Loop: An edge connecting a vertex to itself
▪ Multiple edges: Edges connecting the same two
vertices.
▪ Simple graph: Graphs without loops and multiple
edges.
a b
▪ Edges can be directed or undirected
▪ Undirected edge 𝑒 = {𝑢, 𝑣}
▪ Directed edge 𝑒 = (𝑢, 𝑣)
c
a b b
a
c c
7
Degrees
▪ Definition
The degree of a vertex in a undirected graph is the
number of edges incident to it, except that a loop at a
vertex contributes two to the degree of that vertex.
The degree of the vertex 𝑣 is denoted by deg(𝑣).
10
Property of Degrees
▪ Theorem
If 𝐺 = (𝑉, 𝐸) is a directed graph with 𝑚 edges, then
𝑚 = deg − 𝑣 = deg + 𝑣
𝑣∈𝑉 𝑣∈𝑉
▪ Proof
Each edge contributes one to the out-degree of its
starting vertex and one to the in-degree of its terminal
vertex
11
Special Graphs
▪ Complete graphs: 𝐾𝑛
▪ A simple graph that contains exactly one edge
between each pair of distinct vertices.
12
Bipartite Graphs
▪ Definition
A simple graph 𝐺 = (𝑉, 𝐸) is bipartite if 𝑉 can be
partitioned into two disjoint subsets 𝑉1 and 𝑉2 such that
every edge connects a vertex in 𝑉1 and a vertex in 𝑉2.
▪ Equivalently, a bipartite graph is a graph where it is
possible to color the vertices red or blue so that no
two adjacent vertices are the same color.
𝐺 is 𝐻 is not
bipartite bipartite
▪ Example
Show that 𝐶3 is not bipartite.
13
Complete Bipartite Graphs
▪ Definition
A complete bipartite graph 𝐾𝑚,𝑛 is a graph that has its
vertex set partitioned into two subsets 𝑉1 of size 𝑚
and 𝑉2 of size 𝑛 such that there is an edge from every
vertex in 𝑉1 to every vertex in 𝑉2.
14
Subgraphs
▪ Definition
A subgraph of a graph 𝐺 = (𝑉, 𝐸) is a graph (𝑊, 𝐹),
where 𝑊 ⊆ 𝑉 and 𝐹 ⊆ 𝐸, such that for any 𝑒 ∈ 𝐹, both of
its endpoints must be in 𝑊.
15
Outline
▪ Definition of graphs
▪ Representing Graphs
▪ Connectivity
▪ Euler Paths and Circuits
16
Adjacency Lists
▪ Definition: An adjacency list can be used to represent
a graph with no multiple edges by specifying the
vertices that are adjacent to each vertex of the graph.
▪ Examples:
17
Adjacency Matrices
18
Adjacency Matrices
▪ Example:
The ordering of
vertices is a, b, c, d.
The ordering of
vertices is a, b, c, d.
21
Graph Isomorphism
▪ Definition: Two simple graphs 𝐺1 = (𝑉1 , 𝐸1 ) and 𝐺2 =
(𝑉2 , 𝐸2 ) are isomorphic if there is a bijection 𝑓: 𝑉1 → 𝑉2
with the property that 𝑎 and 𝑏 are adjacent in 𝐺1 if and
only if 𝑓(𝑎) and 𝑓(𝑏) are adjacent in 𝐺2 , for all 𝑎 and 𝑏
in 𝑉1 . Such an 𝑓 is called an isomorphism.
▪ Example: Show that the graphs G =(V, E) and
H = (W, F) are isomorphic.
▪ Solution: Using the bijection 𝑓 with
▪ 𝑓 𝑢1 = 𝑣1
▪ 𝑓 𝑢2 = 𝑣4
▪ 𝑓 𝑢3 = 𝑣3
▪ 𝑓(𝑢4) = 𝑣2
22
Algorithms for Graph Isomorphism
23
Outline
▪ Definition of graphs
▪ Representing Graphs
▪ Connectivity
▪ Euler Paths and Circuits
24
Paths
▪ Definition
Let 𝑛 be a nonnegative integer and 𝐺 an undirected
graph. A path of length 𝑛 from 𝑢 to 𝑣 in G is a
sequence of 𝑛 edges 𝑒1, … , 𝑒𝑛 of 𝐺 for which there
exists a sequence 𝑥0 = 𝑢, 𝑥1, … , 𝑥𝑛−1 , 𝑥𝑛 = 𝑣 of vertices
such that 𝑒𝑖 connects 𝑥𝑖−1 and 𝑥𝑖 , for 𝑖 = 1, … , 𝑛.
▪ For directed graphs, replace “𝑒𝑖 connects 𝑥𝑖−1 and 𝑥𝑖 ”
with “𝑒𝑖 = (𝑥𝑖−1 , 𝑥𝑖 )”.
▪ When the graph is simple, we denote this path by its
vertex sequence 𝑥0, 𝑥1, … , 𝑥𝑛
▪ The path is a circuit (or a cycle) if 𝑢 = 𝑣.
▪ A path or circuit is simple if it does not contain the
same edge more than once.
25
Connectivity in Undirected Graphs
▪ Definition
In an undirected graph, two vertices 𝑢, 𝑣 are connected
if there is a path from 𝑢 to 𝑣.
▪ Example
Give a recursive definition of connectedness of two
vertices without using the concept of a path
▪ Solution
▪ For any edge 𝑒 = {𝑢, 𝑣}, 𝑢 and 𝑣 are connected;
▪ For any 𝑢, 𝑣, 𝑤, if 𝑢 and 𝑣 are connected, and there is
an edge between 𝑣 and 𝑤, then 𝑢 and 𝑤 are
connected.
▪ An undirected graph is called connected if there is a
path between every pair of vertices.
26
Example
▪ Suppose in a wireless network of 𝑛 mobile devices,
each device is within communication range with at
least 𝑛/2 other devices (assuming 𝑛 is an even
number). Show that all devices are connected.
▪ Reformulated as a graph problem: Let 𝐺 be an
undirected graph where each node has degree ≥
𝑛/2. Show that 𝐺 is connected.
27
Proof
▪ Consider any two nodes 𝑢 and 𝑣 in 𝐺. There are two
cases:
▪ If there is an edge {𝑢, 𝑣}, then 𝑢 and 𝑣 are connected.
▪ If there is no direct edge between 𝑢 and 𝑣, then they
must have a common neighbor, say 𝑤, because
▪ There are 𝑛 − 2 nodes other than 𝑢 and 𝑣.
▪ 𝑢 and 𝑣 each have ≥ 𝑛/2 neighbors among these
nodes.
▪ Thus there is a path between 𝑢 and 𝑣.
▪ The above argument holds for any two nodes 𝑢, 𝑣, so
the graph 𝐺 is connected.
▪ Q: If the threshold 𝑛/2 is changed to 𝑛/2 − 1, does the
claim still hold?
28
Connected Components
▪ Definition
A connected component of a graph 𝐺 is a connected
subgraph of 𝐺 that is not a proper subgraph of
another connected subgraph of 𝐺.
29
Connectivity in Directed Graphs
▪ Definition
A directed graph 𝐺 is weakly connected if the
undirected graph obtained by ignoring the directions
of the edges of 𝐺 is connected.
▪ Definition
A directed graph 𝐺 = (𝑉, 𝐸) is strongly connected if for
any 𝑢, 𝑣 ∈ 𝑉, there is a path from 𝑢 to 𝑣.
Weakly
Strongly
connected by
connected
not strongly
connected
30
Strongly Connected Components
▪ Definition
The subgraphs of a directed graph 𝐺 that are strongly
connected but not contained in larger strongly
connected subgraphs are called the strongly
connected components of G.
31
Outline
▪ Definition of graphs
▪ Representing Graphs
▪ Connectivity
▪ Euler Paths and Circuits
32
Euler Paths and Circuits
Leonard Euler
▪ Definition (1707-1783)
An Euler circuit in a graph 𝐺 is a simple circuit
containing every edge of 𝐺. An Euler path in 𝐺 is a
simple path containing every edge of 𝐺.
▪ Example
Which of the graphs below have Euler circuits? Which
have Euler paths?
33
Euler Circuits
▪ Theorem
An undirected connected graph has an Euler circuit iff
all vertices have even degrees.
▪ Proof
▪ The “only if” direction:
▪ An Euler circuit begins with a vertex 𝑎 and
continues with an edge incident with 𝑎, say {𝑎, 𝑏}.
The edge {𝑎, 𝑏} contributes one to deg(𝑎).
▪ Each time the circuit passes through a vertex it
contributes two to the vertex’s degree.
▪ Finally, the circuit terminates at 𝑎, contributing one
to deg(𝑎). Therefore deg(𝑎) must be even, too.
34
Proof (cnt’d)
▪ The “if” direction: We will give an algorithm to find an
Euler cycle when all degrees are even.
▪ First, consider the following simple algorithm:
𝑢 ← any vertex
while 𝑢 has an edge not taken yet
take that edge {𝑢, 𝑣}
𝑢←𝑣
35
Proof (cnt’d)
▪ Idea: The subgraph consisting all edges not traversed
must still have even degrees at all vertices.
▪ Then just repeat the algorithm on those edges, and
“connect” the two cycles into one.
𝑐 ← empty cycle
while there are still edges not taken yet
𝑢 ← any vertex already seen
𝑐′ ← Find-Cycle(𝑢)
insert 𝑐′ into 𝑐 at 𝑢
Find-Cycle(𝑢):
while 𝑢 has an edge not taken yet
take that edge {𝑢, 𝑣}
𝑢←𝑣
36
Euler Paths
▪ Theorem
An undirected connected graph has an Euler path but
not an Euler circuit iff there are exactly two vertices of
odd degree.
▪ Proof
▪ The “if” direction
▪ Suppose 𝑢, 𝑣 have odd degrees. Add an edge
between 𝑢 and 𝑣. In the new graph, there are no
odd degrees, so there is an Euler circuit. Remove
the added edge from the Euler circuit turns it into a
path, starting at 𝑢 and terminating at 𝑣.
37
Proof (cnt’d)
38
Chinese Postman Problem
39
Solution
▪ Idea: Since there are odd-degree vertices, an Euler
circuit does not exist, so some edges will have to be
traversed more than once.
▪ We want to minimize the total length of such edges.
▪ Medium case: There are two odd-degree vertices
▪ Find the shortest path between them
▪ Add all edges on this path to the graph
▪ Now all vertices have even degrees and then can
find an Euler circuit.
40
Example
41
Solution (cnt’d)