GRAPHS II

GRAPHS II
Schaum’s outline Chapter 8
Rosen Chapter 10
December 1, 2016
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
1 / 44
Contents
1
Some Special Simple Graphs
2
Euler Paths and Euler Circuits
3
Hamlitonian paths
4
Labelled and weighted graphs
The shortest path problem
Traveling Salesman Problem
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
2 / 44
What is a Graph?
Definition
A graph G is an ordered pair G = (V , E ), where
V = V (G ) is a set of vertices (or nodes or points);
E = E (G ) ⊆ V × V is a set of edges (or arcs or lines).
Remark
In this course, we restrict ourselves with the graphs where the sets V and E are finite.
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
3 / 44
Next section
1
Some Special Simple Graphs
2
Euler Paths and Euler Circuits
3
Hamlitonian paths
4
Labelled and weighted graphs
The shortest path problem
Traveling Salesman Problem
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
4 / 44
Complete and null graph
Definition
A complete graph is a simple undirected graph in which every pair of distinct vertices is
connected by a unique edge. The complete graph on n vertices is denoted by Kn .
Definition
A null graph (or empty graph or edgeless graph) is a graph with n vertices and zero edges,
denoted by On or Nn or K n .
Example: the graphs K5 and O5
Property. The complete graph Kn has n(n − 1)/2 edges.
Idea of the proof.
Use induction on the number of nodes n.
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
5 / 44
k-regular graphs
Definition
A graph G is regular of degree k (or k-regular) if every vertex has degree k. In other words, a
graph is regular if every vertex has the same degree.
Example 1: connected 0-, 1- and 2-regular graphs
The connected 2-regular graph Cn with n nodes is called a cycle graph.
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
6 / 44
k-regular graphs
Definition
A graph G is regular of degree k (or k-regular) if every vertex has degree k. In other words, a
graph is regular if every vertex has the same degree.
Example 2: connected 3-regular graphs (or cubic graphs) with 6 nodes
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
6 / 44
k-regular graphs
Definition
A graph G is regular of degree k (or k-regular) if every vertex has degree k. In other words, a
graph is regular if every vertex has the same degree.
Property
The complete graph with n vertices Kn is regular of degree n − 1.
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
6 / 44
Wheel graphs
Definition
A wheel graph Wn is a graph with n vertices (n > 4), formed by connecting a single vertex to all
vertices of an (n − 1)-cycle.
Examples of wheel graphs
Property
The wheel graph Wn with n + 1 vertices has 2n edges.
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
7 / 44
Bipartite graphs
Definition
A simple graph G is called bipartite if its vertex set can be partitioned into two disjoint sets U
and V such that every edge in the graph connects a vertex in U and a vertex in V (so that no
edge in G connects either two vertices in U or two vertices in V ).
Example: bipartite graphs (incomplete and complete)
By a complete bipartite graph, we mean that each vertex of U is connected to each vertex of V ;
this graph is denoted by Km,n where m is the number of vertices in U and n is the number of
vertices in V .
Property
ioc.pdf
The graph Km,n has mn edges.
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
8 / 44
Bipartite graph is 2-colourable
Theorem
A simple graph is bipartite if and only if it is possible to assign one of two different colours to
each vertex of the graph so that no two adjacent vertices are assigned the same colour.
Proof.
(⇒) Suppose that G is a bipartite simple graph. Its vertex set can be partitioned
into two disjoint sets U and V and no edge in G connects either two vertices
in U or two vertices in V . Assign one colour to each vertex in U and a second
colour to each vertex in V , then no two adjacent vertices are assigned the
same colour.
(⇐) Now suppose that it is possible to assign colours to the vertices of the graph
using just two colours so that no two adjacent vertices are assigned the same
colour. Let U be the set of vertices assigned one colour and V be the set of
vertices assigned the other colour. Then, U and V are disjoint and
V (G ) = U ∪ V . No two adjacent vertices are either both in U or both in V .
Consequently, G is bipartite.
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
9 / 44
Cycles in a bipartite graph
Theorem
A graph is bipartite if and only if it does not contain an odd cycle.
Necessity.
If G is bipartite with vertex sets U and V , every step alone a path takes either from U to V or
from V to U. To end up where started, therefore, we must take an even number of steps.
Sufficiency.
Let’s paint the vertices of G black and white:
take an arbitrary vertex v0 and colour it white.
Let u be a vertex that has uncoloured neighbours. Let v be such a vertex in the
neighbourhood that is not coloured yet and colour it by the colour different from the
c
colour of u. Denote by v →
− u the fact that selecting colour for v was based on the colour
of u.
Repeat the previous point until two adjacent vertices x and y get the same colour or all
vertices are coloured
see next slide ...
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
10 / 44
Cycles in a bipartite graph (2)
Continuation of the proof ....
If we reach to such vertices x and y , then we have a cycle with odd length
x −− · · · −− v 0 −− · · · −− y −− x.
y
c
c
c
c
x
c
c
v0
c
c
v0
c
This is contradiction with the premise and cannot happen. Thus, the set of vertices can be
partitioned into two disjoint sets according to the colours.
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
11 / 44
Next section
1
Some Special Simple Graphs
2
Euler Paths and Euler Circuits
3
Hamlitonian paths
4
Labelled and weighted graphs
The shortest path problem
Traveling Salesman Problem
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
12 / 44
Eulerian graph
An Eulerian path is a a graph which visits every
edge exactly once.
An Eulerian circuit is an Eulerian path which
starts and ends on the same vertex.
An Eulerian grph is a graph with an Eulerian
circuit (= a graph with every vertex of even
degree for connected graphs). A graph that has
an Eulerian path but not an Eulerian circuit is
called semi-Eulerian.
Leonhard Euler
(1707–1783)
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
13 / 44
An exmple of Eulerian circuit
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
14 / 44
Some well-known examples
Which of the following pictures can be drawn on paper without ever lifting
the pencil and without retracing over any segment?
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
15 / 44
The main property of Eulerian graph
Theorem
Let G = (V , E ) be a connected graph. The following conditions are equivalent:
(a) G is Eulerian graph;
(b) all vertices of G have even degree;
(c) E can be partitioned into cycles (≡ E = E1 ∪ · · · ∪ Ek , where Ei ∩ Ej = ∅ if i 6= j).
Tõestus.
(a)⇒(b). Let P be an Eulerian circuit in graph G . For every vertex v , each time we enter v via
an edge ei , we must leave v via a different edge ei+1 . So, in total, since we see all edges
incident to v exactly once, all such vertices v must have even degree.
(b)⇒(c). Induction on the edges.
Base. If |E | = 0, then E can be partitioned to zero parts, each of which.
Step.|E | > 0. Let H be a connected component of G that has edges. All vertices of H have
degree > 2, then there is a cycle C . Let’s construct the graph G 0 by removing edges of C from
G . All vertices still have even degree. According to the induction hypothesis G 0 can be
partitioned into cycles.
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
16 / 44
The main property of Eulerian graph
(continuation of the proof ...)
˙ 2 ∪˙ . . . ∪C
˙ n , whaere C1 , . . . , Cn are cycles without common edges, but
(c)⇒(a). Let E = C1 ∪C
have common vertices.
Proof by induction:
Base. E is a single cycle. Eulerian walk goes through it.
Step.
Let P be a closed path passing through edges in C1 ∪ · · · ∪ Ck−1 . (Exists because of the
induction hypothesis.)
P passes through some vertex on Ck .
At this vertex, interrupt P, pass around Ck , continue with P.
P is an Eulerian cycle in G .
Remark
Note that this also yields a reasonably efficient algorithm for computing an Eulerian circuit in a
graph where every vertex has even degree.
You need to:
1
partition the edges of the graph into cycles;.
2
pass through all of them.
See an example on the following slides
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
17 / 44
Example of constructing an Eulerian cycle
a
e
b
f
c
g
h
d
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
18 / 44
Example of constructing an Eulerian cycle (2)
a
e
b
f
c
g
h
d
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
19 / 44
EExample of constructing an Eulerian cycle (3)
a
e
b
f
c
g
h
d
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
20 / 44
Example of constructing an Eulerian cycle (4)
a
e
b
f
c
g
h
d
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
21 / 44
Example of constructing an Eulerian cycle (5)
a
e
b
f
c
g
h
d
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
22 / 44
Example of constructing an Eulerian cycle (6)
a
e
b
f
c
g
a
h
d
e
c
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
23 / 44
Example of constructing an Eulerian cycle (7)
a
e
b
f
c
g
a
h
d
e
c
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
24 / 44
Example of constructing an Eulerian cycle (8)
a
e
b
f
g
a
h
c
d
a
b
e
c
f
c
d
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
25 / 44
Example of constructing an Eulerian cycle (9)
a
e
b
f
g
a
h
c
d
a
b
e
c
f
c
d
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
26 / 44
Example of constructing an Eulerian cycle (10)
a
e
b
f
g
a
h
e
c
d
c
a
b
b
g
f
c
d
h
d
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
27 / 44
Example of constructing an Eulerian cycle (11)
a
e
b
f
g
a
h
e
c
d
c
a
b
b
g
f
c
d
h
d
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
28 / 44
Example of constructing an Eulerian cycle (12)
a
b
a
1
e
2
f
g
h
e
3
c
d
c
a
b
b
g
f
c
d
h
d
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
29 / 44
Example of constructing an Eulerian cycle (13)
a
1
b
2
a
6
e
8
g
f
7
3
h
e
5
c
4
a
d
c
b
b
g
f
c
d
h
d
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
30 / 44
Example of constructing an Eulerian cycle (14)
a
1
b
2
10
e
11
12
6
g
f
9
8
a
h
7
5
c
a
3
4
d
c
b
b
g
f
c
e
d
h
d
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
31 / 44
Semi-Eulerian graphs
Theorem
A connected graph is semi-Eulerian iff it has exactly two vertices with odd degree.
The proof is very similar to the case of Euler circuits: just start the initial walk at one of
the vertices of odd degree.
The proof is thus similarly constructive, and yields an efficient algorithm to construct an
Euler path, if one exists.
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
32 / 44
Conclusions on Eulerian graphs
Theorem
Let G be a connected graph.
(a) If G has more than two nodes with odd degree, then it has no Eulerian path;
(b) If G has exactly two nodes with odd degree, then it has an open Eulerian path that starts
at one of these nodes and ends at the other one (G is an semi-Eulerian graph);
(c) If G has no nodes with odd degree, then it has an Eulerian circuit (G is an Eulerian
graph).
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
33 / 44
Next section
1
Some Special Simple Graphs
2
Euler Paths and Euler Circuits
3
Hamlitonian paths
4
Labelled and weighted graphs
The shortest path problem
Traveling Salesman Problem
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
34 / 44
Hamiltonian Paths
A Hamiltonian path in a (undirected) graph G is a
simple path that visits every vertex exactly once.
A Hamiltonian circuit in a (undirected) graph G
is a simple circuit that passes through every
vertex exactly once.
Question: Is there a simple criterion for
determining whether a (simple
undirected) graph has a Hamiltonian
path, or Hamiltonian circuit?
Answer: No. Nobody knows any efficient
algorithm for determining whether a
given (arbitrary) graph G has a
Hamiltonian path/circuit. The
problem is “NP-complete”.
William R. Hamilton
(1805–1865)
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
35 / 44
Examples of Hamiltonian paths or cycles:
c
c
Source: wikipedia.org, Christoph
Sommer, GFDL / cc-by-sa; Claudio
Rocchini
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
36 / 44
Eulerian and Hamiltonian graphs
Example
(a)
(b)
(a) Hamiltonian and non-Eulerian graph
(b) Eulerian and non-Hamiltonian graph
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
37 / 44
Next section
1
Some Special Simple Graphs
2
Euler Paths and Euler Circuits
3
Hamlitonian paths
4
Labelled and weighted graphs
The shortest path problem
Traveling Salesman Problem
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
38 / 44
Next subsection
1
Some Special Simple Graphs
2
Euler Paths and Euler Circuits
3
Hamlitonian paths
4
Labelled and weighted graphs
The shortest path problem
Traveling Salesman Problem
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
39 / 44
Labelled and weighted graphs
A graph G is called a labelled graph if its edges and/or vertices are assigned data of one
kind or another.
G is called a weighted graph if each edge e of G is assigned a nonnegative number w (e)
called the weight or length of v .
The weight (or length) of a path P in such a weighted graph G is defined to be the sum
of the weights of the edges in the path.
Example: a shortest path between A and H
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
40 / 44
Labelled and weighted graphs
Alternatively:
A weighted graph is a structure G = (V , E , w ), where H = (V , E ) is a
(simple) graph and w : E → R is a total function that defines the weight
w (e) for every edge e ∈ E .
For a given path P in a weighted graph G = (V , E , w ), the weight (or
cost) of the path is the sum
w (P) =
∑ w (e).
e∈P
Example: a shortest path between A and H
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
40 / 44
Dijkstra’s algorithm for finding the shortest paths
Shortest paths in the graph:
See demo here
0 + 4 = 4;
0 + 5 = 5;
5 + 1 = 6;
5 + 1 + 0 = 6;
5 + 1 + 0 + 1 = 7;
5 + 1 + 0 + 6 = 12;
3
1
5
a
10
4
8
z
1
7
0
4
6
13
Jaan Penjam, email: [email protected]
I601: Lecture 7
ioc.pdf
December 1, 2016
41 / 44
Dijkstra’s algorithm for finding the shortest paths
Shortest paths in the graph:
See demo here
0 + 4 = 4;
0 + 5 = 5;
5 + 1 = 6;
5 + 1 + 0 = 6;
5 + 1 + 0 + 1 = 7;
5 + 1 + 0 + 6 = 12;
3
∞
1
5
a
8
0
∞
10
4
1
∞
∞
z
7
0
4
∞
Jaan Penjam, email: [email protected]
13
I601: Lecture 7
6
∞
ioc.pdf
December 1, 2016
41 / 44
Dijkstra’s algorithm for finding the shortest paths
Shortest paths in the graph:
See demo here
0 + 4 = 4;
0 + 5 = 5;
5 + 1 = 6;
5 + 1 + 0 = 6;
5 + 1 + 0 + 1 = 7;
5 + 1 + 0 + 6 = 12;
3
5
1
5
a
8
0
∞
10
4
1
8
∞
z
7
0
4
4
Jaan Penjam, email: [email protected]
13
I601: Lecture 7
6
∞
ioc.pdf
December 1, 2016
41 / 44
Dijkstra’s algorithm for finding the shortest paths
Shortest paths in the graph:
See demo here
0 + 4 = 4;
0 + 5 = 5;
5 + 1 = 6;
5 + 1 + 0 = 6;
5 + 1 + 0 + 1 = 7;
5 + 1 + 0 + 6 = 12;
3
5
1
5
a
8
0
∞
10
4
1
8
∞
z
7
0
4
4
Jaan Penjam, email: [email protected]
13
I601: Lecture 7
6
17
ioc.pdf
December 1, 2016
41 / 44
Dijkstra’s algorithm for finding the shortest paths
Shortest paths in the graph:
See demo here
0 + 4 = 4;
0 + 5 = 5;
5 + 1 = 6;
5 + 1 + 0 = 6;
5 + 1 + 0 + 1 = 7;
5 + 1 + 0 + 6 = 12;
3
5
1
5
a
8
0
8
10
4
1
6
∞
z
7
0
4
4
Jaan Penjam, email: [email protected]
13
I601: Lecture 7
6
17
ioc.pdf
December 1, 2016
41 / 44
Dijkstra’s algorithm for finding the shortest paths
Shortest paths in the graph:
See demo here
0 + 4 = 4;
0 + 5 = 5;
5 + 1 = 6;
5 + 1 + 0 = 6;
5 + 1 + 0 + 1 = 7;
5 + 1 + 0 + 6 = 12;
3
5
1
5
a
8
0
8
10
4
1
6
∞
z
7
0
4
4
Jaan Penjam, email: [email protected]
13
I601: Lecture 7
6
6
ioc.pdf
December 1, 2016
41 / 44
Dijkstra’s algorithm for finding the shortest paths
Shortest paths in the graph:
See demo here
0 + 4 = 4;
0 + 5 = 5;
5 + 1 = 6;
5 + 1 + 0 = 6;
5 + 1 + 0 + 1 = 7;
5 + 1 + 0 + 6 = 12;
3
5
1
5
a
8
0
7
10
4
1
6
12
z
7
0
4
4
Jaan Penjam, email: [email protected]
13
I601: Lecture 7
6
6
ioc.pdf
December 1, 2016
41 / 44
Dijkstra’s algorithm for finding the shortest paths
Shortest paths in the graph:
See demo here
0 + 4 = 4;
0 + 5 = 5;
5 + 1 = 6;
5 + 1 + 0 = 6;
5 + 1 + 0 + 1 = 7;
5 + 1 + 0 + 6 = 12;
3
5
1
5
a
8
0
7
10
4
1
6
12
z
7
0
4
4
Jaan Penjam, email: [email protected]
13
I601: Lecture 7
6
6
ioc.pdf
December 1, 2016
41 / 44
Dijkstra’s algorithm for finding the shortest paths
Shortest paths in the graph:
See demo here
0 + 4 = 4;
0 + 5 = 5;
5 + 1 = 6;
5 + 1 + 0 = 6;
5 + 1 + 0 + 1 = 7;
5 + 1 + 0 + 6 = 12;
3
5
1
5
a
8
0
7
10
4
1
6
12
z
7
0
4
4
Jaan Penjam, email: [email protected]
13
I601: Lecture 7
6
6
ioc.pdf
December 1, 2016
41 / 44
Dijkstra’s algorithm for finding the shortest paths
Shortest paths in the graph:
See demo here
0 + 4 = 4;
0 + 5 = 5;
5 + 1 = 6;
5 + 1 + 0 = 6;
5 + 1 + 0 + 1 = 7;
5 + 1 + 0 + 6 = 12;
3
5
1
5
a
8
0
7
10
4
1
6
12
z
7
0
4
4
Jaan Penjam, email: [email protected]
13
I601: Lecture 7
6
6
ioc.pdf
December 1, 2016
41 / 44
Next subsection
1
Some Special Simple Graphs
2
Euler Paths and Euler Circuits
3
Hamlitonian paths
4
Labelled and weighted graphs
The shortest path problem
Traveling Salesman Problem
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
42 / 44
Traveling Salesman Problem, TSP
The Traveling Salesman Problem ( TSP ) is one of the most famous problem of combinatorial
optimization:
Given a list of n cities and the distances between each pair of cities, what is the
shortest possible route that visits each city exactly once and returns to the
origin city?
TSP in graph theory:
Find a Hamiltonian cycle (tour) of G = (V , E , w ) with minimum cost.
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
43 / 44
Traveling Salesman Problem, TSP
The Traveling Salesman Problem ( TSP ) is one of the most famous problem of combinatorial
optimization:
Given a list of n cities and the distances between each pair of cities, what is the
shortest possible route that visits each city exactly once and returns to the
origin city?
TSP in graph theory:
Find a Hamiltonian cycle (tour) of G = (V , E , w ) with minimum cost.
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
43 / 44
Traveling Salesman Problem, (2)
Näide
The problem is NP-hard.
a number of heuristics are developed to solve TSP in practice.
ioc.pdf
Jaan Penjam, email: [email protected]
I601: Lecture 7
December 1, 2016
44 / 44