2 Graph Theory
2 Graph Theory
Graph Theory
Varying Applications (examples)
• Computer networks
• Distinguish between two chemical compounds with
the same molecular formula but different structures
• Solve shortest path problems between cities
• Scheduling exams and assign channels to television
stations
Topics Covered
• Definitions
• Types
• Terminology
• Representation
• Sub-graphs
• Connectivity
• Hamilton and Euler definitions
• Shortest Path
• Planar Graphs
• Graph Coloring
Definitions - Graph
u v
Undirected: Unordered pair of vertices. Represented as {u, v}. Disregards
any sense of direction and treats both end vertices interchangeably.
u v
Definitions – Edge Type
• Loop: A loop is an edge whose endpoints are equal i.e., an
edge joining a vertex to it self is called a loop. Represented as
{u, u} = {u}
u v
w
Definitions – Graph Type
u
e1 e2 w
v e3
Definitions – Graph Type
u
e1 w e4
e2
v e3
Definitions – Graph Type
Directed Graph: G(V, E), set of vertices V, and set of Edges E, that are
ordered pair of elements of V (directed edges)
Representation Example: G(V, E), V = {u, v, w}, E = {(u, v), (v, w), (w, u)}
u v
w
Definitions – Graph Type
u
u e4
e1 e2
u e3
Definitions – Graph Type
• u and v are adjacent if {u, v} is an edge, e is called incident with u and v. u and v are called
endpoints of {u, v}
• Degree of Vertex (deg (v)): the number of edges incident on a vertex. A loop contributes twice to
the degree (why?).
• Pendant Vertex: deg (v) =1
• Isolated Vertex: deg (v) = 0
Representation Example: For V = {u, v, w} , E = { {u, w}, {u, w}, (u, v) }, deg (u) = 2, deg (v) = 1, deg (w) =
1, deg (k) = 0, w and v are pendant , k is isolated
u v
k
w
Terminology – Directed graphs
• For the edge (u, v), u is adjacent to v OR v is adjacent from u, u – Initial vertex, v – Terminal vertex
• In-degree (deg- (u)): number of edges for which u is terminal vertex
• Out-degree (deg+ (u)): number of edges for which u is initial vertex
Note: A loop contributes 1 to both in-degree and out-degree (why?)
Representation Example: For V = {u, v, w} , E = { (u, w), ( v, w), (u, v) }, deg- (u) = 0, deg+ (u) = 2, deg- (v) =
1,
deg (v) = 1, and deg- (w) = 2, deg+ (u) = 0
+
u v
w
Theorems: Undirected Graphs
Theorem 1
The Handshaking theorem:
2e v
vV
Theorem 2:
An undirected graph has even number of vertices with odd degree
Pr oof V 1 is the set of even degree vertices and V2 refers to odd degree vertices
2e deg(v) deg(u) deg(v)
vV u V1 v V2
• Theorem 3:
deg + (u) = deg - (u) = |E|
Simple graphs – special cases
• Complete graph: Kn, is the simple graph that contains exactly one
edge between each pair of distinct vertices.
K1 K2 K3
K4
Simple graphs – special cases
• Cycle: Cn, n ≥ 3 consists of n vertices v1, v2, v3 … vn and edges {v1, v2},
{v2, v3}, {v3, v4} … {vn-1, vn}, {vn, v1}
Representation Example: C3, C4
C3 C4
Simple graphs – special cases
W3 W4
Simple graphs – special cases
10 11
0 1
00 01
Q1 Q2
Bipartite graphs
• In a simple graph G, if 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 V2 (so that no
edge in G connects either two vertices in V1 or two vertices in V2)
Application example: Representing Relations
Representation example: V1 = {v1, v2, v3} and V2 = {v4, v5, v6},
v4
v1
v5
v2
v6
v3
V1 V2
Complete Bipartite graphs
• Km,n is the graph that has its vertex set portioned into two subsets of
m and n vertices, respectively There is an edge between two vertices
if and only if one vertex is in the first subset and the other vertex is in
the second subset.
Representation example: K2,3, K3,3
K2,3 K3,3
Subgraphs
u u u
v w v w v
G H1 H2
Subgraphs
u
u
w v
w w v
G1 G2 G
Representation
e1 e2 e3
u
v 1 0 1
e1 e2
u 1 1 0
v w w 0 1 1
e3
Representation- Adjacency Matrix
v u w
u
v 0 1 1
u 1 0 1
v w
w 1 1 0
Representation- Adjacency Matrix
v u w
u
v 0 1 0
u 0 0 1
v w
w 1 0 0
Representation- Adjacency List
u
node Adjacency List
u v,w
v w, u
v w
w u,v
Graph - Isomorphism
Application Example:
In chemistry, to find if two compounds have the same structure
Graph - Isomorphism
u1 u2 v1 v2
u3 u4 v4
v3
Connectivity
• Basic Idea: In a Graph Reachability among vertices by
traversing the edges
Application Example:
- In a city to city road-network, if one city can be reached from
another city.
- Problems if determining whether a message can be sent
between two
computer using intermediate links
- Efficiently planning routes for data delivery in the Internet
Connectivity – Path
2
1 v
3
u
4 5
Connectivity – Path
Undirected Graph
An undirected graph is connected if there exists is a
simple path between every pair of vertices
v2 v5
Connectivity – Connectedness
Undirected Graph
v3
v5
v1
v2
v4
Connectivity – Connectedness
Directed Graph
• A directed graph is strongly connected if there is a path from a to b
and from b to a whenever a and b are vertices in the graph
• A directed graph is weakly connected if there is a (undirected) path
between every two vertices in the underlying undirected path
Directed Graph
Representation example: G1 (Strong component), G2 (Weak Component), G3
is undirected graph representation of G2 or G1
G1 G2 G3
Connectivity – Connectedness
• Directed Graph
Strongly connected Components: subgraphs of a Graph G
that are strongly connected
Representation example: G1 is the strongly connected
component in G
G G1
Isomorphism - revisited
G1 G2
Counting Paths
• Theorem: Let G be a graph with adjacency matrix A with respect to the ordering
v1, v2, …, Vn (with directed on undirected edges, with multiple edges and loops
allowed). The number of different paths of length r from Vi to Vj, where r is a
positive integer, equals the (i, j)th entry of (adjacency matrix) Ar.
Base Case: For the case N = 1, aij =1 implies that there is a path of length 1. This is true
since this corresponds to an edge between two vertices.
We assume that theorem is true for N = r and prove the same for N = r +1. Assume that
the (i, j)th entry of Ar is the number of different paths of length r from vi to vj. By induction
hypothesis, bik is the number of paths of length r from vi to vk.
Counting Paths
Case r +1: In Ar+1 = Ar. A,
The (i, j)th entry in Ar+1 , bi1a1j + bi2 a2j + …+ bin anj
where bik is the (i, j)th entry of Ar.
The (i, j)th entry in Ar+1 corresponds to the length between i and j and the length is
r+1. This path is made up of length r from vi to vk and of length from vk to vj. By
product rule for counting, the number of such paths is bik* akj The result is bi1a1j +
bi2 a2j + …+ bin anj ,the desired result.
Counting Paths
a ------- b
| |
| |
c -------d
A=0110 A4 = 8 0 0 8
1001 0880
1001 0880
0110 8008
You can redraw the original picture as long as for every edge between nodes i
and j in the original you put an edge between nodes i and j in the redrawn
version (and you put no other edges in the redrawn version).
Original:
2 3
4
Redrawn: 2
4 1
3
The Seven Bridges of Königsberg, Germany
Euler:
a b
c d e
The problem in our language:
For every node besides a and b, T uses an edge to exit for each edge it
uses to enter. Thus, the degree of the node is even.
a b
f c d
c d
e
Delete the simple path:
{a,b}, {b,c}, {c,f}, {f,a}
c d
a b
f c d
“Splice” the circuits in the 2 graphs:
{a,b}, {b,c}, {c,f}, {f,a}
“+”
e {c,d}, {d,e}, {e,c}
“=“
{a,b}, {b,c}, {c,d}, {d,e}, {e,c}, {c,f}
{f,a}
Euler Circuit
e1 e2 e3 e4 e5 e6 e7
e1
a b a 1 0 0 0 0 0 1
e2 b 1 1 0 0 0 0 0
e7
e3 c 0 1 1 0 1 1 0
f c d
d 0 0 1 1 0 0 0
e6
e5 e 0 0 0 1 1 0 0
e e4
f 0 0 0 0 0 1 1
Homework 1
• Hamiltonian path (also called traceable path) is a path that visits each vertex
exactly once.
Is it Hamiltonian?
Yes
.
Hamiltonian Graph
• Similar notions may be defined for directed graphs, where edges (arcs) of a
path or a cycle are required to point in the same direction, i.e., connected
tail-to-head.
• There is a simple relation between the two problems. The Hamiltonian path
problem for graph G is equivalent to the Hamiltonian cycle problem in a graph
H obtained from G by adding a new vertex and connecting it to all vertices of
G.
k 1
w( p) w(vi , vi 1 )
• Shortest path = a path of thei minimum
1 weight
• Applications
– static/dynamic network routing
– robot motion planning
– map/route generation in traffic
Shortest-Path Problems
• Shortest-Path problems
– Single-source (single-destination). Find a shortest path
from a given source (vertex s) to each of the vertices.
The topic of this lecture.
– Single-pair. Given two vertices, find a shortest path
between them. Solution to single-source problem
solves this problem efficiently, too.
– All-pairs. Find shortest-paths for every pair of vertices.
Dynamic programming algorithm.
– Unweighted shortest-paths – BFS.
Optimal Substructure
• Theorem: subpaths of shortest paths are
shortest paths
• Proof (”cut and paste”)
– if some subpath were not the shortest path, one
could substitute the shorter subpath and create a
shorter total path
Negative Weights and Cycles?
• Negative edges are OK, as long as there are no negative
weight cycles (otherwise paths with arbitrary small “lengths”
would be possible)
• Shortest-paths can have no cycles (otherwise we could
improve them by removing cycles)
– Any shortest-path in graph G can be no longer than n – 1
edges, where n is the number of vertices
Shortest Path Tree
• The result of the algorithms – a shortest path tree. For each vertex v, it
– records a shortest path from the start vertex s to v. v.parent() gives a
predecessor of v in this shortest path
– gives a shortest path length from s to v, which is recorded in v.d().
• The same pseudo-code assumptions are used.
• Vertex ADT with operations:
– adjacent():VertexSet
– d():int and setd(k:int)
– parent():Vertex and setparent(p:Vertex)
Relaxation
• For each vertex v in the graph, we maintain v.d(), the estimate of the
shortest path from s, initialized to at the start
• Relaxing an edge (u,v) means testing whether we can improve the
shortest path to v found so far by going through u
u v u v
2 2 Relax (u,v,G)
5 9 5 6
if v.d() > u.d()+G.w(u,v) then
Relax(u,v) Relax(u,v) v.setd(u.d()+G.w(u,v))
v.setparent(u)
5 2 7 5 2 6
u v u v
Dijkstra's Algorithm
• Non-negative edge weights
• Greedy, similar to Prim's algorithm for MST
• Like breadth-first search (if all weights = 1, one can simply use BFS)
• Use Q, a priority queue ADT keyed by v.d() (BFS used FIFO queue, here we
use a PQ, which is re-organized whenever some d decreases)
• Basic idea
– maintain a set S of solved vertices
– at each step select "closest" vertex u, add it to S, and relax all edges
from u
Dijkstra’s ALgorithm
Solution to Single-source (single-destination).
Dijkstra(G,s)
01 for each vertex u G.V()
02 u.setd()
03 u.setparent(NIL)
04 s.setd(0)
05 S // Set S is used to explain the
algorithm
06 Q.init(G.V()) // Q is a priority queue ADT
07 while not Q.isEmpty()
08 u Q.extractMin()
09 S S {u} relaxing
10 for each v u.adjacent() do
edges
11 Relax(u, v, G)
12 Q.modifyKey(v)
Dijkstra’s Example
u v
1
Dijkstra(G,s) 10
01 for each vertex u G.V() 9
2 3
02 u.setd()
s 0 4 6
7
03 u.setparent(NIL) 5
04 s.setd(0) 2
05 S x y
06 Q.init(G.V())
07 while not Q.isEmpty() u v
08 u Q.extractMin() 1
09 S S {u} 10 10
10 for each v u.adjacent() do 9
2 3
11 Relax(u, v, G) s 0 4 6
7
12 Q.modifyKey(v) 5
5 2
x y
Dijkstra’s Example
u v
1
10 8 14
Dijkstra(G,s) 9
for each vertex u G.V() 2 3
01 s 0 4 6
02 u.setd() 7
03 u.setparent(NIL) 5
04 s.setd(0) 5 2 7
05 S x y
06 Q.init(G.V())
07 while not Q.isEmpty() u v
08 u Q.extractMin() 1
10 8 13
09 S S {u}
10 for each v u.adjacent() do 9
2 3
11 Relax(u, v, G)
s 0 4 6
7
12 Q.modifyKey(v) 5
5 2 7
x y
Dijkstra’s Example
u v
1
10 8 9
Dijkstra(G,s) 9
2 3
01 for each vertex u G.V() 0 4 6
02 u.setd() 7
5
03 u.setparent(NIL) 5 2 7
04 s.setd(0)
05 S
x y
06 Q.init(G.V())
07 while not Q.isEmpty() u v
1
08 u Q.extractMin() 10 8 9
09 S S {u} 9
for each v u.adjacent() do 2 3
10 0 4 6
11 Relax(u, v, G) 7
12 Q.modifyKey(v) 5
5 2 7
x y
Dijkstra’s Algorithm
• O(n2) operations
– (n-1) iterations: 1 for each vertex added to the
distinguished set S.
– (n-1) iterations: for each adjacent vertex of the
one added to the distinguished set.
• Note: it is single source – single destination
algorithm
Traveling Salesman Problem
• Given a number of cities and the costs of traveling from one to the other,
what is the cheapest roundtrip route that visits each city once and then
returns to the starting city?
• It can be shown that the requirement of returning to the starting city does
not change the computational complexity of the problem.
Application Example: VLSI design (overlapping edges requires extra layers), Circuit
design (cannot overlap wires on board)
K4
Planar Graphs
• Representation examples: Q3
Planar Graphs
v1 v2 v3 v1 v5 v1 v5
R21
R2 R1 R1
R22
v3
v4 v5 v6 v4 v2 v4 v2
Planar Graphs
Theorem : Euler's planar graph theorem
number number
number of regions
of vertices of edges
Planar Graphs
Example of Euler’s theorem
v=4,e=6,r=4, v-e+r=2
R3
Planar Graphs
v
u
R1
n+1 Case: Assume, rn = en – vn + 2 is true. Let {an+1, bn+1} be the edge that
is added to Gn to obtain Gn+1 and we prove that rn = en – vn + 2 is true. Can
be proved using two cases.
Planar Graphs
• Case 1:
an+1
bn+1
Planar Graphs
• Case 2:
an+1
R
bn+1
Planar Graphs
Corollary 1: Let G = (V, E) be a connected simple planar graph with
|V| = v, |E| = e > 2, and r regions. Then 3r ≤ 2e and e ≤ 3v – 6
Proof: Since G is loop-free and is not a multigraph, the boundary of
each region (including the infinite region) contains at least three
edges. Hence, each region has degree ≥ 3.
Degree of region: No. of edges on its boundary; 1 edge may occur
twice on boundary -> contributes 2 to the region degree.
Each edge occurs exactly twice: either in the same region or in 2
different regions
an+1
R
bn+1
Region Degree
R Degree of R = 3
Degree of R = ?
R
Planar Graphs
Each edge occurs exactly twice: either in the same region or in 2
different regions
2e = sum of degree of r regions determined by 2e
2e ≥ 3r. (since each region has a degree of at least 3)
r ≤ (2/3) e
From Euler’s theorem, 2 = v – e + r
2 ≤ v – e + 2e/3
2 ≤ v – e/3
So 6 ≤ 3v – e
or e ≤ 3v – 6
Planar Graphs
c
j
d
c
c
h e
i
k e
d
g f d
e H K5
g
f
G
Graph Coloring Problem
• Graph coloring is an assignment of "colors", almost always taken to be
consecutive integers starting from 1 without loss of generality, to certain
objects in a graph. Such objects can be vertices, edges, faces, or a mixture
of the above.
• Assignment of colors to the vertices of the graph such that proper coloring
takes place (no two adjacent vertices are assigned the same color)
• Chromatic number: least number of colors needed to color the graph
• A graph that can be assigned a (proper) k-coloring is k-colorable, and it is k-
chromatic if its chromatic number is exactly k.
Vertex Coloring Problem
C4 C5
K4 K2, 3
Vertex Covering Problem
• The Four color theorem: the chromatic number of a planar graph is no
greater than 4
• Example: G1 chromatic number = 3, G2 chromatic number = 4
• (Most proofs rely on case by case analysis).
G1 G2