Professional Documents
Culture Documents
By
Chandan Giri
chandangiri@gmail.com
Graph Theory - History
Flow of material
liquid flowing through pipes
current through electrical networks
information through communication networks
parts through an assembly line
In Operating systems to model resource handling
(deadlock problems)
In compilers for parsing and optimizing the code.
Basics
Graph Definitions and Notation
1 1 2 3
2 3
4 5 6 4 5 6
Definitions
Vertex
Basic Element
Drawn as a node or a dot.
Vertex set of G is usually denoted by V(G), or V
Edge
A set of two elements
Drawn as a line connecting two vertices, called
end vertices, or endpoints.
The edge set of G is usually denoted by E(G), or E.
Graph Definitions and Notation
Let G=(V,E, g) be a graph and e be an edge of this
graph.
Then there are vertices v and w such that
g(e)={v,w}; vertices v and w are end vertices of e
When a vertex v is an end point of some edge e, we
say that e is an incident with the vertex v and v is
incident with the edge e.
Two vertices v and w of G are said to be adjacent if
there exists an edge e E such that g(e)={v,w}.
Graph Definitions and Notation
Two edges are said to be adjacent if they have
a common end vertex.
If e is an edge such that g(e) = {v,w}, where
v=w, then e is an edge from the vertex v to
itself. Such an edge is called a loop on the
vertex v or at the vertex v.
If there is a loop on v, then v is adjacent to
itself.
Graph Definitions and Notation
e1
v3
v1 v2
v7
e2
e5 e3 e4 e6 e7
v4 v5
v6
Graph Definitions and Notation
Let G=(V, E, g) be a graph.
The incidence function g need not be one one.
Therefore, there may exist edges e1, e2, ., en-1, en,
n 2 such that g(e1)= g(e2) = = g(en) = {v, w}.
Such edges are called parallel edges.
Example:
g(e1)=g(e2)={v1, v2}. So edges e1 and e2 are parallel. Similarly
edges e4, e6 and e7 are parallel.
e1
v1 v2
v7
e2
e5 e3 e4 e6 e7
v4 v5
v6
Graph Definitions and Notation
DEFINITION :: Isolated vertex
Let G be a graph and v be a vertex in G. We say that v is an
isolated vertex if it is not incident with any edge.
v6
v10 v7
v2 v3
v9 v8
v4 v5
Graph Definitions and Notation
DEFINITION :: Even (odd ) degree vertex
Example::
v1 e1 deg(v1)=2 Even
v2
deg (v2)= 1 Odd
e2 deg (v3)=3 Odd
e3
v4 v3 deg(v4)=2 Even
Graph Definitions and Notation
DEFINITION :: Degree Sequence
v4 v3 v4 v3
Graph Definitions and Notation
Theorem: The sum of the degrees of all vertices of a graph G is twice the number
of edges.
i.e= = . , where e is the number of edges of the graph and n is the
number of vertices of the graph G.
Proof:
Every edge contributes two to the sum of the degrees, one for each of its end points.
Corollary: The sum of degrees of all of the vertices of a graph G is an even integer.
--- Follows directly from above theorem
Graph Definitions and Notation
DEFINITION :: Simple Graph
Theorem:: Let G be a simple graph with at least two vertices. Then G has at least
two vertices of same degree
Example: e
b
K5
d
c
Graph Definitions and Notation
Theorem: The number of edges in a complete graph with n
vertices is n(n-1)/2
Proof:
Every node in the complete graph with n vertices is connected to
all of the other n1 nodes.
Hence, Since each edge has two endpoints, this implies that the
graph has n(n1)/2 edges.
A
X
B
Y
C
Z
D
Bipartite Graphs
Example I: Is C3 bipartite?
v1 v6
v5 v2
v3 v4
Graph Definitions and Notation
v3 v3
e3 v2 v2 e3
v2 v3
v2 e1
e2 e1
e1 e4
e1 e4 v1 v4 e2
e2 v1 v4
v1 v4 e2
v1 v4 e6 e5
e6
e5
e7 v5
v5 v6
v5
v6
G G1 G2 G3
DEFINITION :: Sub-graph
Let G= (V, E, g) be a graph. The graph G1=(V1, E1, g1) is a sub-graph of G if
a) V1 V and
b) Every edge of G1 is also an edge of G.
Example:
v1 v2 v5
b
f
Walks:
a c e g v1, b, v2, e, v3, h, v3, d, v4
v6 v4, c, v2, f, v5 , g, v6
v4 d v1, b, v2, c, v4, a, v1
v3
h
Walks, Paths, and Cycles
DEFINITION :: Directed walk
Let u and v be two vertices in a directed graph G.
A directed walk from u to v in G is an alternating sequence
of n+1 vertices and n arcs of G
(u= v1, e1, v2, e2, v3, e3, .., vn-1, en-1, vn, en, vn+1=v)
beginning with vertex u and ending with vertex v in which
each edge ei, for i=1, 2, , n, is an arc from vi to vi+1
Walks, Paths, and Cycles
DEFINITION :: Length of a walk (directed walk)
Open walk
Closed
walk
Walks, Paths, and Cycles
DEFINITION :: Trail and Path
v1 v2 v5
b
f
Walks:
e g v1, b, v2, e, v3, h, v3, d, v4 ---- Not a path, trail
a c
v6 v4, c, v2, f, v5 , g, v6 ------ Path, trail
d v1, b, v2, c, v4, a, v1 ----- Path (also closed path,
v4 v3
cycle), trail (circuit)
h
DEFINITION :: Cycle
DEFINITION :: Circuit
Walks, Paths, and Cycles
DEFINITION :: Trivial walk, path or trail
DEFINITION :: k-cycle
Some properties of Walks, Paths, and
Cycles
Vertices Edges Comments
i.e. deletion of a vertex v means the deletion of the vertex v and deletion of
all the edges incident on v.
Note: A graph consisting of only one vertex is always connected, because it does
not contain any pair of distinct vertices.
Connected graph
Example: Are the following graphs connected?
a b a
b
e e
d
d c
c
Yes. No.
a b a
b
e d e
c
d f
c No.
Yes.
44
Some Theorem
Theorem:
A graph is disconnected if and only if its vertex set V can be partitioned into two
nonempty, disjoint subsets V1 and V2 such that there exists no edge in G whose one end
vertex is in subset V1 and the other in subset V2.
Proof:
Suppose that such a partition exists.
Consider two arbitrary vertices a and b of G, such that a V1 and b V2. No path can
exist between vertices a and b; otherwise there would be at least one edge whose one end
vertex would be in V1 and the other in V2. hence, if a partition exists, G is not connected.
Proof:
If ni (>= 1) is the number of vertices for the i-th component of the
graph,
Then we have n1+n2+.+nk = n for the k component of the graph.
Now maximum number of edges of a component I = ni(ni-1)/2.
Again maximum number of edges n of G = ( ni2 ni) =
ni2 ni
Now =1( 1) =
2
Taking square at both sides we have ( =1( 1)) = ( )2
Equating both sides we can obtain (n-k)(n-k+1)/2 edges.
Walks, Paths, and Cycles
DEFINITION :: Distance
Euler graph
Euler line and Euler graph
If some closed walk in a graph contains all the
edges of the graph, then the walk is called an Euler
line and the graph an Euler graph.
Walks, Paths, and Cycles
Theorem: A graph is bipartite if and only if it does not contain any cycle of odd
length.
Proof:
() If G is bipartite with bipartition X,Y of the vertices, then
any cycle C has vertices that must alternatively be in X and Y.
Thus, since a cycle is closed, C must have an even number of
vertices and hence is an even cycle.
Because aij is the number of edges from vi to vj, the adjacency matrix AG is a square
matrix over the set of non-negative integers.
0 1 0 1
Example: v2 1 0 1 0
e6 1 = 0 1 1 1
v2
e1 1 0 1 0
e2
e1
e2
v1 v3
v1 v3 e7 e5
0 1 0 2
e5 e4 e3 1 0 2 0
e4 e3 2 = 0 2 1 1
v4 2 0 1 0
v4 G1 G2
Properties of Adjacency matrix
The Adjacency matrix AG of a graph has the
following property
1. AG is a symmetric matrix
2. If G does not contain any loops and parallel
edges then each element of AG is either 0 or 1
3. If G does not contain any loops, then all of the
diagonal elements of AG are 0.
Matrix Representation of a Graph
Incidence matrix
The all-vertex incidence matrix of a non-empty and loopless graph
G=(V, E) is an nxm matrix [aij], where n is the number of vertices
in G and m is the number of edges of G and
=
Properties of Adjacency matrix
The incidence matrix AG of a graph has the
following property
Since an edge is incident on two vertices, each column of A
will have exactly two 1s and rest are 0s.
If any row has all entries as 0, then no edge is incident on
that vertex, hence it is an isolated vertex.
The number of 1s is a row indicates the degree od the
corresponding vertex.
Cut-edge, Cut Vertex
A cut-edge or cut-vertex of a graph is an edge
or vertex whose deletion increases the number
of components
Cut-edge
Cut-vertex
Cut-edge, Cut Vertex
G-e or G-M : The subgraph obtained by
deleting an edge e or set of edges M
G-v or G-S : The subgraph obtained by deleting
a vertex v or set of vertices S
G G-e
e
Cut-edge, Cut Vertex
Theorem: An edge e is a cut-edge if and only if e
belongs to no cycles.
Proof :
Let e= (x, y) be an edge in a graph G and H be the
component containing e.
Since deletion of e effects no other component, it
suffices to prove that H-e is connected if and only if e
belongs to a cycle.
First suppose that H-e is connected.
This implies that H-e contains an x, y-path,
This path completes a cycle with e.
Cut-edge, Cut Vertex
Proof :2/2
Now suppose that e lies in a cycle C.
Choose u, vV(H)
Since H is connected, H has a u, v-path P
If P does not contain e
Then P exists in H-e
Otherwise (P contains e)
Suppose by symmetry that x is between u and y on P
Since H-e contains a u, x-path along P, the
transitivity of the connection relation implies that H-e has a
u, v-path
We did this for all u, v V(H), so H-e is connected.
Cut-edge, Cut Vertex
An Example:
x e y
u v
P
Eulerian Circuits
A graph is Eulerian if it has a closed trail
containing all edges.
We call a closed trail a circuit when we do not
specify the first vertex but keep the list in
cyclic order.
An Eulerian circuit or Eulerian trail in a
graph is a circuit or trail containing all the
edges.
Even Graph, Even Vertex
Proof: (Necessity)
Suppose that G has an Eulerian circuit C
Each passage of C through a vertex uses two incident
edges
And the first edge is paired with the last at the first vertex
Hence every vertex has even degree
a) A B E D C F A
b) A B C D E F C
EBFA
c) B C D E F B
B C D
A
F E
Example: Identifying Hamilton Circuits
Solution
a) It is a Hamilton circuit for the graph.
b) It is not a Hamilton circuit since it visits B
more than once.
c) It is not a Hamilton circuit since it does not
visit all the vertices.
Hamiltonian Path
If we remove any one edge from a
Hamiltonian circuit, we are left with a path.
This path is called a Hamiltonian path. .
Clearly, a Hamiltonian path in a graph G
traverses every vertex of G.
Since a Hamiltonian path is a sub-graph of a
Hamiltonian circuit, every graph that has a
Hamiltonian circuit also has a Hamiltonian
path.
Hamiltonian Circuits
There are, however, many graphs with
Hamiltonian paths that have no Hamiltonian
circuits.
The length of a Hamiltonian path (if there
exists) in a connected graph of n vertices is (n-
1).
75
Planar Graphs
Properties of a graph are not necessarily
immediate from one representation, but may be
apparent from another.
a
a
e
c b
b
d
d e c
77
Applications of planar graphs
Planar graphs are extensively used in Electrical, Mechanical
and Civil Engineering.
78
What are planar graph- drawings?
Definition :
Given a graph G= (V,E), a drawing maps
each vertex v V to a distinct point (v) in
plane, and each edge e E, e= (u,v) to a simple
open Jordan curve (u,v) with end points (u),
(v).
79
Problem definition: Planar Embedding
80
Problem (Decision problem)
Given a graph G= (V,E), is G planar?
- i.e. can G be drawn in the plane
without edge crossing?
- planarity of K4
yes, K4 is planar 1
1
2 3
2 4
4 3
Graph K4 81
K5 and K3,3
Question: Are the following graphs planar?
No, they are not. There always will be at least one
crossing.
- can be proved by Eulers celebraed
theorem
1 2
1 3
5
2
1 2 3
3
4
82
K5 and K3,3
Question: Are the following graphs
planar?
1 2
1 3
5
2
1 2 3
3
4
83
Is the following graph planar?
- Yes it is
- But how to arrive at the answer?? 85
Basic assumptions
We assume that our graphs are connected and
there are no self loops and no-multi edges.
86
More definitions
Definition: Subdivision of a graph
An edge e = (u,v) G is subdivided, when it is replaced by a
path u x v of length two by introducing a new vertex x.
87
Geometric properties
Let F be an open set of this plane R R, that is, every
point x F has a disk centered at x and contained in F.
Region:
F is a region, if any two points x, y F can be
joined by a continuous curve the points of which are
all in F.
Boundary:
The boundary of a region is consists of
those points for which every neighborhood contains
points from F and its complement.
88
Faces
Let G be a planar graph, and P(G) one of its
plane embeddings.
Regard now each edge e = (u,v) G as a line
from u to v.
The set (R R)\EG is open and it is divided into
a finite no. of disjoint regions, called faces of
P(G).
89
Interior faces:
A face of P(G) is an interior face, if it is
bounded.
Exterior faces:
The (unique) face that is unbounded is called the
exterior face of P(G).
The edges that surrounded a face F, constitute this
boundary of F.
Exterior boundary: Boundary of exterior face
- vertices on exterior boundary are called
exterior vertices of exterior edge.
- vertices that not on the exterior boundary are
interior vertices.
90
Properties
Embedding P(G) satisfy following properties:
Two different faces F1 and F2 are disjoint and their
boundaries can intersect only on edges.
P(G) has a unique exterior faces.
Each edge e belongs to this boundary of at most two
faces.
Each cycle of G surrounds at least one internal face of
P(G).
A bridge of G belongs to this boundary of only one face.
An edge that is not a bridge belongs to the boundary of
exactly two faces.
91
Eulers Formula
Lemma 1: A plane embedding P(G) of a planar graph
G has no interior faces if and only if G is acyclic,
that is, if and only if the connected components of G
are trees.
- follows from the geometric properties
Lemma 2: (Eulers formula) proved by Legendre
(1974)
Let G be a connected planar graph and let be any of
its plane embeddings. Then,
v e + f = 2, where f is the no. of faces of P(G)
92
Proof
It can be proved by the method of induction on no.
of faces f of a plane embedding P(G).
Note that f 1, since each P(G) has an exterior
face.
If f = 1, then here are no cycles in G, and since G is
connected, it is a face.
Hence, e = v 1.
94
Maximum planar graphs
Lemma:
If G is a planar graph of order vG 3, then
eG 3vG 6
Moreover, if G has no triangles C3, then eG 2vG 4.
Proof:
Let v be the no. of regions of G.
Now if the
For graph G with f faces, it follows from the fact
that, 2eG 3fG ........ (A)
95
Maximum planar graphs
Each face for embedding P(G) contains at least three edges on its
boundary .
Hence, 3fG 2eG, since each edge lies on at most two faces.
So from (A),
3fG 2eG
=> fG 2/3 eG
Now combining this with Eulers formula,
vG eG + fG = 2
=> fG = 2 + eG vG
=> eG vG + 2 = fG 2/3 eG
=> 3eG 3vG + 6 2eG
=> eG 3vG 6
Example: k5, vG = 5, eG = 10
Now 10 3x5 6 is contradicting. So it is non- planar.
96
Corollary: Let G be a connected planar graph with
vG vertices and eG edges, and no triangles. Then
eG 2vG 4.
Proof: For graph G with fG faces, it follows that a
face with triangle requires at least 4 edges.
Hence, 4fG 2eG
=> fG eG
combining this with Eulers formula
we have, vG eG + fG = 2
=> eG vG + 2 = fG eG
=> eG 2vG - 4
97
Example: K3,3
6 vertices and 9 edges and no triangles.
Now 9 12 4 8 contradicting.
So it is non- planar.
98
Graph Traversal
Graph Searching Algorithms
Searching a graph
Symmetrically following the edges of the graph so
as to visit all the vertices.
Breadth-first search
Depth-first search
Topological sort (of a directed acyclic
graph):
It is a linear ordering of all the vertices such that if
(u,v) is a directed edge, then u appears before v in
the ordering.
Breadth-first search : Idea
Graph G=(V, E), directed or undirected with adjacency list
representation.
GOAL: Systematically explores edges of G to
Discover every vertex reachable from the source vertex s
Compute the shortest path distance of every vertex from the
source vertex s
Produce a breadth-first tree (BFT) GH with root s
BFT contains all vertices reachable from s
The unique path from any vertex v to s in GH constitutes a shortest
path from s to v in G
IDEA: Expanding frontier across the breadth greedy
Propagate a wave 1 edge-distance at a time
Using a FIFO queue : O(1) time to update pointers to both ends.
Breadth-first search: Key
components
Maintains the following fields for each u V
Color[u]: color of u
WHITE: not discovered yet
GRAY: discovered and not to be or being processed
BLACK: discovered and processed
H[u]: parent of u (NIL if u=s or u is not
discovered yet)
d[u]: distance of u from s
b e
i
f
g h
Breadth-first search : Example
FIFO Just after
Queue Q Processing vertex
s
{a} --
a 0 d {a, b, c} a
1
c
1
b e
i
f
g h
Breadth-first search : Example
FIFO Just after
Queue Q Processing vertex
s
{a} --
a 0 d {a, b, c} a
{a, b, c, f} b
1
c
1
b e
2
i
f
g h
Breadth-first search : Example
FIFO Just after
Queue Q Processing vertex
s
{a} --
0 d {a, b, c} a
a
1 {a, b, c, f} b
{a, b, c, f, e} c
c
1 2
b e
2
i
f
g h
Breadth-first search : Example
FIFO Just after
Queue Q Processing vertex
s
{a} --
0 d {a, b, c} a
a {a, b, c, f} b
1
{a, b, c, f, e} c
c {a, b, c, f, e, g, h} f
1 2
b e
2
3 i
f 3
g h
Breadth-first search : Example
FIFO Just after
Queue Q Processing vertex
s
{a} --
3 d {a, b, c} a
a 0
1 {a, b, c, f} b
{a, b, c, f, e} c
c {a, b, c, f, e, g, h} f
1 2 {a, b, c, f, e, g, h, d, i} e
b e
2 All distances
i are filled in
3 f 3 after
3 processing e
g h
Breadth-first search : Example
FIFO Just after
Queue Q Processing vertex
s
{a} --
3 d {a, b, c} a
a 0
1 {a, b, c, f} b
{a, b, c, f, e} c
c {a, b, c, f, e, g, h} f
2 {a, b, c, f, e, g, h, d, i} g
1
b e
2
3 i
3 f
3
g h
Breadth-first search : Example
FIFO Just after
Queue Q Processing vertex
s
{a} --
0 3 d {a, b, c} a
a {a, b, c, f} b
1
{a, b, c, f, e} c
c {a, b, c, f, e, g, h} f
1 {a, b, c, f, e, g, h, d, i} h
2
b e
2
3 i
3 f
3
g h
Breadth-first search : Example
FIFO Just after
Queue Q Processing vertex
s
3 {a} --
a 0 d {a, b, c} a
{a, b, c, f} b
1
{a, b, c, f, e} c
c {a, b, c, f, e, g, h} f
{a, b, c, f, e, g, h, d, i} d
1 2
b e
2
f 3 i
3 3
g h
Breadth-first search : Example
FIFO Just after
Queue Q Processing vertex
s
{a} --
0 3 d {a, b, c} a
a {a, b, c, f} b
1
{a, b, c, f, e} c
c {a, b, c, f, e, g, h} f
1 2 {a, b, c, f, e, g, h, d, i} i
b e
2 Algorithm terminates :
All vertices are processed.
3 i
3 f
3
g h
Breadth-first Search: Analysis
Running time: O(V+E) = considered linear
time in graphs
Initialization: O(V)
Queue operations: O(V)
Each vertex enqueued and dequeued at most once
Both enqueue and dequeue operations take O(1) time
Processing gray vertices: O(E)
Each vertex is processed at most once and
= ()
Breadth-first search: The paths to the
root
BFT(G,s), where VH={vV:H(v) NIL} {s}
and EH={(H[v], v) E: v VH {s}}
is a breadth first tree such that
VH consists of all vertices in V that are reachable
from s
For all v VH, unique path p(v,s) in GH constitutes
a sp(s,v) in G
Breadth-first Tree: The BFS tree
s
a 0 3d
s 1
3 d c
0 1
a 2
1 b e
c 2
i
1 2 f 3
3 3
b e g h
2
3 i
3 f
3
g h
Depth-first search: Idea
Graph G=(V,E) directed or undirected
Adjacency list representation
Goal: Systematically explore every vertex and
every edge
Idea: Search deeper whenever possible
Using a LIFO queue (Stack; FIFO queue used in
BFS)
Depth-first search: Key components
Maintains several field for each v V
Like BFS, colors the vertices to indicate their
states. Each vertex is
Initially white
Grayed when discovered
Blacked when finished
Like BFS, records discovery of a white v
during scanning Adj[u] by H[v] u
Depth-first search: Key components
Unlike BFS, predecessor graph GH produced
by DFS forms spanning forest
GH= (V,EH) where
EH={(H[v],v): v V and H[v] != NIL}
GH = depth-first forest(DFF) is composed of
disjoint depth-first trees (DFTs)
Depth-first search: Key components
DFS also timestamps each vertex with two
timestamps
d[v] : records when v is first discovered and
grayed
f[v]: records when v is finished and blackened
Since there is only one discovery event and
finishing event for each vertex we have
1<=d[v] <f[v]<=2|V|
Depth-first search: Algorithm
DFS-VISIT(G, u)
DFS(G) color[u] gray
for each u V do
d[u] time time+1
color[u] white for each v Adj[u] do
H[u] NIL if color[v]= white then
time 0 H[v] u
for each u V do DFS-VISIT(G,v)
if color[u]= white
then color[u] black
DFS-VISIT(G,u) f[u] time time +1
Depth-first search: Analysis
Running time: O(V+E)
Initialization loop in DFS: O(V)
Main loop in DFS: O(V) exclusive of time to execute
calls to DFS-VISIT
DFS-VISIT is called exactly once for each v V since
DFS-VISIT is invoked only on white vertices and
DFS-VISIT(G,u) immediately colors u as gray
For loop of DFS-VISIT(G, u) is executed |Adj[u]|
time
Since sum |Adj[u]| = E, total cost of executing loop of
DFS-VISIT is O(E).
Depth-first search: Example
x y z
s t
w v u
Depth-first search: Example
x y z
1
s t
w v u
Depth-first search: Example
x y z
1
s t
w v u
Depth-first search: Example
x y z
1
s t
w v u
Depth-first search: Example
x y z
1
s t
w v u
Depth-first search: Example
x y z
1
s t
3 4
w v u
Depth-first search: Example
x y z
1
s t
3 4 5
w v u
Depth-first search: Example
x y z
1
s t
3 4 5 6
w v u
Depth-first search: Example
x y z
1
s t
2 7
3 4 5 6
w v u
Depth-first search: Example
x y z
1 8
s t
2 7
3 4 5 6
w v u
Depth-first search: Example
x y z
1 8
s t
2 7
3 4 5 6
w v u
Depth-first search: Example
x y z
1 8
s t
2 7 9
3 4 5 6
w v u
Depth-first search: Example
x y z
1 8
s t
2 7 9
3 4 5 6
w v u
Depth-first search: Example
x y z
1 8
s t
2 7 9 10
3 4 5 6
w v u
Depth-first search: Example
x y z
1 8 11
s t
2 7 9 10
3 4 5 6
w v u
Depth-first search: Example
x y z
1 12 8 11
s t
2 7 9 10
3 4 5 6
w v u
Depth-first search: Example
x y z
1 12 8 11 13
s t
2 7 9 10
3 4 5 6
w v u
Depth-first search: Example
x y z
1 12 8 11 13
s t
2 7 9 10
3 4 5 6
w v u
Depth-first search: Example
x y z
1 12 8 11 13
s t
2 7 9 10
3 4 5 6
w v u
Depth-first search: Example
x y z
1 12 8 11 13
s t
2 7 9 10
3 4 5 6 14
w v u
Depth-first search: Example
x y z
1 12 8 11 13
s t
2 7 9 10
3 4 5 6 14
w v u
Depth-first search: Example
x y z
1 12 8 11 13
s t
2 7 9 10
3 4 5 6 14 15
w v u
Depth-first search: Example
x y z
1 12 8 11 13 16
s t
2 7 9 10
3 4 5 6 14 15
w v u
Depth-first search: DFT and DFF
x y z
1 12 8 11 13 16
s t
2 7 9 10
3 4 5 6 14 15
w v u
y z
1 12 8 11 13 16
x t
s
2 7 9 10
3 4 5 6 14 15
w v u
Graph Coloring
Basics
Assignment of "colors" to certain objects in
a graph subject to certain constraints
Vertex coloring (the default)
Edge coloring
Face coloring (planar)
Not Graph Labeling
Graph coloring
Just markers to keep track of adjacency or
incidence
Graph labeling
Calculable problems that satisfy a numerical
condition
Vertex coloring
In its simplest form, it is a way of coloring
the vertices of a graph such that no two
adjacent vertices share the same color
Edge and Face coloring can be transformed
into Vertex version
Vertex Color example
Anything less results in adjacent vertices
with the same color
Known as proper
3-color example
Vertex coloring
A k-coloring of a graph G is a labeling f:
V(G) S, where |S| = k (often we use S =
[k]). The labels are colors; the vertices of
one color form a color class.
A k-coloring is proper if adjacent vertices
have different labels.
A graph is k-colorable if it has a proper k-
coloring. The chromatic number (G) is
the least k such that G is k-colorable.
k-chromatic
A graph G is k-chromatic if (G) =k.
A proper k-coloring of a k-chromatic graph
is an optimal coloring.
If (H) < (G) = k for every proper
subgraph H of G, then G is color-critical or
k-critical.
Chromatic number of a complete graph:
(G) = n
Spanning Trees
Definition:
A tree T is said to be a spanning tree of a
connected graph G if T is a subgraph of G and T
contains all the vertices of G.
6 4
5 9
14 2
10
15
3 8
Spanning Tree
Since the vertices of G are barely hanging
together is a panning tree, it is a sort of skeleton
of the original graph G.
This is why a spanning tree is sometimes refereed
to as a skeleton or scaffolding of G.
Since spanning trees are the largest (with
maximum number of edges) trees among all trees
in G. it is also quite appropriate to call a spanning
tree a maximal tree subgraph or maximal tree of
G.
Spanning Tree
Theorem:
Every connected graph has at least one spanning
tree.
Theorem:
With respect to any of its spanning trees, a connected
graph of n vertices and e edges has (n-1) tree
branches and (e-n+1) chords.
Finding a spanning tree
To find a spanning tree of a graph,
pick an initial node and call it part of the spanning tree
do a search from the initial node:
each time you find a node that is not in the spanning tree, add to the spanning tree both
the new node and the edge you followed to get to it
158
Minimum-cost spanning trees
Suppose you have a connected undirected graph with a weight (or
cost) associated with each edge
The cost of a spanning tree would be the sum of the costs of its
edges
A minimum-cost spanning tree is a spanning tree that has the
lowest cost
16 16
A B A B
21 11 6 11 6
19 5 5
F C F C
33 14
10
E 18 D E 18 D
A connected, undirected graph A minimum-cost spanning tree
159
Finding spanning trees
There are two basic algorithms for finding minimum-cost
spanning trees, and both are greedy algorithms
Prims algorithm: Start with any one node in the spanning tree,
and repeatedly add the cheapest edge, and the node it leads to,
for which the node is not already in the spanning tree.
Here, we consider the spanning tree to consist of both nodes and edges
160
Kruskals Algorithm
Kruskal()
{
T = ;
for each v V
MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
T = ; 14 17
8 25
for each v V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
T = ; 14 17
8 25
for each v V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
T = ; 14 17
8 25
for each v V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
T = ; 14 17
8 25
for each v V 5
MakeSet(v); 21 13 1?
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
T = ; 14 17
8 25
for each v V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2? 19
{ 9
T = ; 14 17
8 25
for each v V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
T = ; 14 17
8 25
for each v V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
T = ; 14 17
8 25
for each v V 5?
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
T = ; 14 17
8 25
for each v V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
T = ; 14 17
8? 25
for each v V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
T = ; 14 17
8 25
for each v V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9?
T = ; 14 17
8 25
for each v V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
T = ; 14 17
8 25
for each v V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
T = ; 14 17
8 25
for each v V 5
MakeSet(v); 21 13? 1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
T = ; 14 17
8 25
for each v V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
T = ; 14? 17
8 25
for each v V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
T = ; 14 17
8 25
for each v V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
T = ; 14 17?
8 25
for each v V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2 19?
{ 9
T = ; 14 17
8 25
for each v V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
T = ; 14 17
8 25
for each v V 5
MakeSet(v); 21? 13 1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
T = ; 14 17
8 25?
for each v V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
T = ; 14 17
8 25
for each v V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskals Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
T = ; 14 17
8 25
for each v V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T {{u,v}};
Union(FindSet(u), FindSet(v));
}
Prims algorithm
T = a spanning tree containing a single node s;
E = set of edges adjacent to s;
while T does not contain all the nodes {
remove an edge (v, w) of lowest cost from E
if w is already in T then discard edge (v, w)
else {
add edge (v, w) and node w to T
add to E the edges adjacent to w
}
}
An edge of lowest cost can be found with a priority queue
Testing for a cycle is automatic
Hence, Prims algorithm is far simpler to implement than Kruskals
algorithm
185
Walk-Through
2
Initialize array
3
10
F C K dv pv
A 7
4
3 A F
8 B F
18
4
9
B D C F
10
H 25 D F
2
3 E F
G 7
E F F
G F
H F
2
Start with any node, say D
3
10
F C K dv pv
A 7
4
3 A
8
18 B
4
9
B D C
10
H 25 D T 0
2
3 E
G 7
E F
G
H
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv
A 7
4
3 A
8
18 B
4
9
B D C 3 D
10
H 25 D T 0
2
3 E 25 D
G 7
E F 18 D
G 2 D
H
2 Select node with minimum
distance
3
10
F C K dv pv
A 7
4
3 A
8
18 B
4
9
B D C 3 D
10
H 25 D T 0
2
3 E 25 D
G 7
E F 18 D
G T 2 D
H
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv
A 7
4
3 A
8
18 B
4
9
B D C 3 D
10
H 25 D T 0
2
3 E 7 G
G 7
E F 18 D
G T 2 D
H 3 G
2 Select node with minimum
distance
3
10
F C K dv pv
A 7
4
3 A
8
18 B
4
9
B D C T 3 D
10
H 25 D T 0
2
3 E 7 G
G 7
E F 18 D
G T 2 D
H 3 G
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv
A 7
4
3 A
8
18 B 4 C
4
9
B D C T 3 D
10
H 25 D T 0
2
3 E 7 G
G 7
E F 3 C
G T 2 D
H 3 G
2 Select node with minimum
distance
3
10
F C K dv pv
A 7
4
3 A
8
18 B 4 C
4
9
B D C T 3 D
10
H 25 D T 0
2
3 E 7 G
G 7
E F T 3 C
G T 2 D
H 3 G
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv
A 7
4
3 A 10 F
8
18 B 4 C
4
9
B D C T 3 D
10
H 25 D T 0
2
3 E 2 F
G 7
E F T 3 C
G T 2 D
H 3 G
2 Select node with minimum
distance
3
10
F C K dv pv
A 7
4
3 A 10 F
8
18 B 4 C
4
9
B D C T 3 D
10
H 25 D T 0
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H 3 G
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv
A 7
4
3 A 10 F
8
18 B 4 C
4
9
B D C T 3 D
10
H 25 D T 0
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H 3 G
Table entries unchanged
2 Select node with minimum
distance
3
10
F C K dv pv
A 7
4
3 A 10 F
8
18 B 4 C
4
9
B D C T 3 D
10
H 25 D T 0
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv
A 7
4
3 A 4 H
8
18 B 4 C
4
9
B D C T 3 D
10
H 25 D T 0
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
2 Select node with minimum
distance
3
10
F C K dv pv
A 7
4
3 A T 4 H
8
18 B 4 C
4
9
B D C T 3 D
10
H 25 D T 0
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv
A 7
4
3 A T 4 H
8
18 B 4 C
4
9
B D C T 3 D
10
H 25 D T 0
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
Table entries unchanged
2 Select node with minimum
distance
3
10
F C K dv pv
A 7
4
3 A T 4 H
8
18 B T 4 C
4
9
B D C T 3 D
10
H 25 D T 0
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
2 Cost of Minimum Spanning
Tree = dv = 21
3
F C K dv pv
A 4
3 A T 4 H
B T 4 C
4
B D C T 3 D
H D T 0
2
3 E T 2 F
G E F T 3 C
G T 2 D
H T 3 G
Done