Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

L18 Graph

Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

Part VI: Graphs

▪ Reading: Rosen 10.1, 10.2, 10.3, 10.4, 10.5

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

▪ Q: (Reformulated as a graph problem) Can you


find a path in the graph that includes every edge
exactly once?

2
The Traveling Salesman Problem
▪ Q: How to visit all places with the shortest total
distance, and come back to origin?

▪ Q: (Reformulated as a graph problem) Given a graph


where edges have weights (lengths), how to find a
cycle with minimum total weight that includes all
vertices?
3
World Wide Web
▪ Web graph.
▪ Node: web page.
▪ Edge: hyperlink from one page to another
(directed).
cnn.com

netscape.com novell.com cnnsi.com timewarner.com

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

This is a graph with four vertices and five edges.

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(𝑣).

𝐺: deg(𝑎) = 2, deg(𝑏) = deg(𝑐) = deg(𝑓) = 4, deg(𝑑) = 1,


deg(𝑒) = 3, deg(𝑔) = 0.
𝐻: deg(𝑎) = 4, deg(𝑏) = deg(𝑒) = 6, deg(𝑐) = 1, deg(𝑑) = 5.
8
Property of Degrees
▪ Theorem
If 𝐺 = (𝑉, 𝐸) is an undirected graph with 𝑚 edges, then
2𝑚 = ෍ deg 𝑣
𝑣∈𝑉
▪ Proof
Each edge contributes two to the total degree of all
vertices.
▪ Example
How many edges are there in a graph with 10 vertices,
all of which have degree 6?
▪ Example
Show that at a party, the number of people who shake
hands with an odd number of people must be even.
9
Directed Graphs
▪ Definition
The in-degree of a vertex 𝑣, denoted deg − (𝑣), is the
number of edges which terminate at 𝑣. The out-degree
of 𝑣, denoted deg + (𝑣), is the number of edges with 𝑣
as their initial vertex. Note that a loop at a vertex
contributes 1 to both the in-degree and the out-degree
of the vertex.

deg−(a) = 2, deg−(b) = 2, deg−(c) = 3,


deg−(d) = 2, deg−(e) = 3, deg−(f) = 0.

deg+(a) = 4, deg+(b) = 1, deg+(c) = 2,


deg+(d) = 2, deg+ (e) = 3, deg+(f) = 0.

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.

▪ A cycle 𝐶𝑛 for 𝑛 ≥ 3 consists of 𝑛 vertices 𝑣1, 𝑣2 , ⋯ , 𝑣𝑛 ,


and edges {𝑣1, 𝑣2}, {𝑣2, 𝑣3} , ⋯ , {𝑣𝑛−1 , 𝑣𝑛 }, {𝑣𝑛 , 𝑣1}.

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

▪ Definition: Suppose that 𝐺 = (𝑉, 𝐸) is a simple graph


where |𝑉| = 𝑛. Arbitrarily list the vertices of 𝐺 as
𝑣1 , 𝑣2 , … , 𝑣𝑛 . The adjacency matrix 𝑨𝐺 of 𝐺, with
respect to the listing of vertices, is the 𝑛 × 𝑛 zero-one
matrix with 1 as its (𝑖, 𝑗)th entry when 𝑣𝑖 and 𝑣𝑗 are
adjacent, and 0 as its (𝑖, 𝑗)th entry when they are not
adjacent.
▪ In other words, if the graphs adjacency matrix is 𝑨𝐺 =
[𝑎𝑖𝑗 ], then

18
Adjacency Matrices
▪ Example:
The ordering of
vertices is a, b, c, d.

The ordering of
vertices is a, b, c, d.

▪ The adjacency matrix of a simple graph


▪ is symmetric, i.e., 𝑎𝑖𝑗 = 𝑎𝑗𝑖
▪ each diagonal entry 𝑎𝑖𝑖 = 0
▪ Note:
▪ Adjacency list is better for a sparse graph
▪ Adjacency matrix is better for a dense graph
19
Adjacency Matrices
▪ Non-simple graphs
▪ A self-loop at the vertex 𝑣𝑖 is represented by a 1 at the (𝑖, 𝑖)-th
position of the matrix
▪ When multiple edges connect the same pair of vertices 𝑣𝑖 and
𝑣𝑗 , (or if multiple self-loops are present at the same vertex),
the (𝑖, 𝑗)th entry equals the number of edges connecting the
pair of vertices.
▪ Example (using the ordering of vertices a, b, c, d. )

▪ Can also represent directed graphs (the matrix may


not be symmetric)
20
Incidence Matrices
▪ Definition: Let 𝐺 = (𝑉, 𝐸) be an undirected graph with
vertices where 𝑣1 , 𝑣2 , … 𝑣𝑛 and edges 𝑒1 , 𝑒2 , … 𝑒𝑚 . The
incidence matrix with respect to the ordering of 𝑉 and
𝐸 is the 𝑛 × 𝑚 matrix 𝑴 = [𝑚𝑖𝑗 ], where

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

▪ The best algorithms known for determining whether


two graphs are isomorphic have exponential worst-
case time complexity (in the number of vertices of the
graphs).
▪ However, there are algorithms with linear average-case
time complexity.

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 {𝑢, 𝑣}
𝑢←𝑣

▪ Observation: This algorithm always finds a cycle,


because all degrees are even
▪ Problem: This algorithm may not traverse all edges.

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)

▪ The “only if” direction


▪ Suppose there is an Euler path from 𝑢 to 𝑣 and 𝑢 ≠
𝑣. Add an edge between 𝑢 and 𝑣 in the graph as
well as in the path. This turns the Euler path into a
cycle. By previous theorem, all degrees in the new
graph are even. Then removing the added edge
from the graph introduces exactly two vertices with
odd degrees.

38
Chinese Postman Problem

▪ Given a graph where the edges represent streets and


vertices represent intersections, find the shortest tour
so that the postman can traverse every edge at least
once and return to the starting point. For each edge,
its length is given.
▪ Solution
▪ Easy case: If all vertices have even degree, then just
use the Euler circuit.
▪ What if there are odd-degree vertices
▪ Note that due to the property of degrees, there
must be an even number of odd-degree vertices

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)

▪ Hard case: There are more than 2 odd-degree vertices.


▪ For every pair of such vertices, find their shortest path
▪ Find the best pairing of these vertices, such that the
total length of the shortest paths between the pairs
vertices is minimized
▪ Add the shortest paths between the paired vertices to
the graph and find the Euler circuit.
▪ Further questions
▪ How to find the shortest path?
▪ How to find the best pairing (matching)?
▪ Will be covered in COMP 3711
42
Hamilton Paths and Circuits
William
▪ Euler paths and circuits contains Rowan
every edge only once. Hamilton
(1805- 1865)
▪ Hamilton paths and circuits contains every vertex
exactly once

▪ Determining if a graph has a Hamilton path a circuit is


NP-hard
▪ On weighted graphs: The traveling salesman problem.
43

You might also like