0% found this document useful (0 votes)
8 views148 pages

Chapter 5 Graphs

Graphs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views148 pages

Chapter 5 Graphs

Graphs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 148

Discrete Mathematics

Subject Code: MA211


Dr.Divya Jain

Chapter5
Graph Theory
Applications of Graphs
 Potentially anything (graphs can represent
relations, relations can describe the extension
of any predicate)
 Applications in networking, scheduling, flow
optimization, circuit design, path planning.
 Geneology analysis, computer game playing,
program compilation, object-oriented design,
….
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

A generalization of the simple concept of


a set of dots, links, edges or arcs.
Representation: Graph G =(V, E) consists set of
vertices denoted by V, or by V(G) and set of edges E, or
E(G)
Example

 What is a graph G?
 It is a pair G = (V, E),
where
 V = V(G) = set of vertices
 E = E(G) = set of edges
 Example:
 V = {s, u, v, w, x, y, z}

E = {(x,s), (x,v)1, (x,v)2, (x,u),
(v,w), (s,v), (s,u), (s,w), (s,y),
(w,y), (u,y), (u,z),(y,z)}
Edges
 An edge may be labeled by a pair of
vertices, for instance e = (v,w).
 e is said to be incident on v and w.
 Isolated vertex = a vertex without
incident edges.
Definitions – Edge Type
Directed: Ordered pair of vertices. Represented as (u,
v) directed from vertex u to v.

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}

 Multiple Edges: Two or more edges joining


the same pair of vertices.
Definitions – Graph Type
Simple (Undirected) Graph: consists of V, a
nonempty set of vertices, and E, a set of unordered
pairs of distinct elements of V called edges (undirected)

Representation Example: G(V, E), V = {u, v, w}, E =


{{u, v}, {v, w}, {u, w}}

u v

w
Definitions – Graph Type
Multigraph: G(V,E), consists of set of vertices V, set of
Edges E and a function f from E to {{u, v}| u, v V, u ≠
v}. The edges e1 and e2 are called multiple or parallel
edges if f (e1) = f (e2).
Representation Example: V = {u, v, w}, E = {e1, e2, e3}

u
e1 e2 w

e3
v

• E.g., nodes are cities, edges are segments of major


highways.
Definitions – Graph Type
Pseudograph: Like a multigraph, but edges connecting a
node to itself are allowed.
Representation Example: V = {u, v, w}, E = {e 1, e2, e3, e4}

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

E.g. : V = people , E={( ={(x, y) | ) | x likes y}


Definitions – Graph Type
Directed Multigraph: Like directed graphs, but there
may be more than one arc from a node to another.

Representation Example: V = {u, v, w}, E = {e1, e2, e3, e4}

u
w e4
e1 e2

v e3
Definitions – Graph Type

Type Edges Multiple Loops Allowed


Edges ?
Allowed ?
Simple Graph undirected No No
Multigraph undirected Yes No
Pseudograph undirected Yes Yes
Directed directed No Yes
Graph
Directed directed Yes Yes
Multigraph
Terminology – Undirected
graphs
 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
vV

(why?)
Every edge connects 2 vertices
Theorems: Undirected
Graphs
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)
vV u  V1 v  V2

 deg (v) is even for v  V1,


 The first term in the right hand side of the last inequality is even.
 The sum of the last two terms on the right hand side of
the last inequality is even since sum is 2e.
Hence second term is also even
 second term  deg(v) even
v  V2
Theorems: directed Graphs

 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.

Representation Example: K1, K2, K3, K4

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

How many edges are there in Cn ?


Simple graphs – special
cases

Wheels: Wn, obtained by adding additional vertex to Cn
and connecting all vertices to this new vertex by new
edges.
Representation Example: W3, W4

W3 W4

How many edges are there in Wn?


Simple graphs – special
cases
 N-cubes: Qn, vertices represented by 2^n bit strings of
length n. Two vertices are adjacent if and only if the bit
strings that they represent differ by exactly one bit
positions
Representation Example: 10Q1, Q2 11

0 1

00 01

Q1 Q2

What is Q3?
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
V(G) = V(V1)  V(V2)
v2
|V(V1)| = m, |V(V2)| = n
v6
v3
V(V1) V(V2) = 

V V
2
1
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

V1

|V(V1)| = m
|V(V2)| = n
V2
K2,3
K3,3
Discrete Mathematics

Graph Theory
cont.
Subgraphs
 A subgraph of a graph G = (V, E) is a graph H =(V’, E’)
where V’ is a subset of V and E’ is a subset of E
Application example: solving sub-problems within a graph
Representation example: V = {u, v, w}, E = ({u, v}, {v,
w}, {w, u}}, H1 , H2

u u u

v w v w v

G H1 H2
Subgraphs
 G = G1 U G2 wherein E = E1 U E2 and V = V1 U V2, G, G1
and G2 are simple graphs of G

Representation example:
V1 = {u, w}, E1 = {{u, w}}, V2 = {w, v}, E1 = {{w, v}},
V = {u, v ,w}, E = { { {u, w}, {w, v} } }
u
u

w v
w w v

G1 G2 G
Representation
 Incidence (Matrix): Most useful when
information about edges is more desirable than
information about vertices.

 Adjacency (Matrix/List): Most useful when


information about the vertices is more desirable
than information about the edges. These two
representations are also most popular since
information about the vertices is often more
desirable than edges in most applications
Representation- Incidence
matrix
 Incidence matrix

Label rows with vertices

Label columns with edges e f g h j

1 if an edge is incident to a
vertex, 0 otherwise v 1 1 0 0 0

w 1 0 1 0 1

x 0 0 0 1 1

y 0 1 1 1 0
Representation- Adjacency
Matrix
 There is an N x N matrix, where |V| = N , the Adjacenct
Matrix (NxN) A = [aij]
 For undirected graph
1 if {vi, vj} is an edge of G
a ij 
0 otherwise
 For directed graph

1 if (vi, vj) is an edge of G


a ij 
0 otherwise

 This makes it easier to find subgraphs, and to reverse


graphs if needed.
Representation- Adjacency
Matrix
 Adjacency is chosen on the ordering of
vertices. Hence, there as are as many as
n! such matrices.
 The adjacency matrix of simple graphs
are symmetric (aij = aji) (why?)
 When there are relatively few edges in
the graph the adjacency matrix is a
sparse matrix
 Directed Multigraphs can be represented
by using aij = number of edges from vi to
vj
Representation- Adjacency
Matrix
 Example: Undirected Graph G (V, E)

v u w
u
v 0 1 1

u 1 0 1
v w

w 1 1 0
Representation- Adjacency
Matrix
 Example: directed Graph G (V, E)

v u w
u
v 0 1 0

u 0 0 1
v w
w 1 0 0
Linked Representation-
Adjacency List
Each node (vertex) has a list of which nodes (vertex) it is adjacent

Example: undirectd graph G (V, E)

u
node Adjacency List

u v,w

v w, u
v w
w u,v

G= [u: v, w; v: w, u; w: u, v]
Graph - Isomorphism

G1 and G2 are isomorphic


 if there exist one-to-one onto functions f: V(G1) →
V(G2) and g: E(G1) → E(G2) such that
 an edge e is adjacent to vertices v, w in G1 if and
only if g(e) is adjacent to f(v) and f(w) in G2
Graph - Isomorphism
Representation example: G1 = (V1, E1) , G2 = (V2, E2)
f(u1) = v1, f(u2) = v4, f(u3) = v3, f(u4) = v2,

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
A Path is a sequence of edges that begins at a
vertex of a graph and travels along edges of the
graph, always connecting pairs of adjacent
vertices.

Representation example: G = (V, E), Path P represented, from


u to v is
{{u, 1}, {1, 4}, {4, 5}, {5, v}}

2
1 v
3
u
4 5
Connectivity – Path
Definition for Directed Graphs
A Path of length n (> 0) from u to v in G is a sequence of n
edges e1, e2 , e3, …, en of G such that f (e1) = (xo, x1), f (e2)
= (x1, x2), …, f (en) = (xn-1, xn), where x0 = u and xn = v. A
path is said to pass through x0, x1, …, xn or traverse e1, e2 ,
e3, …, en

For Simple Graphs, sequence is x0, x1, …, xn

In directed multigraphs when it is not necessary to


distinguish between their edges, we can use sequence of
vertices to represent the path

Circuit/Cycle: u = v, length of path > 0

Simple Path: does not contain an edge more than once


Paths and cycles
 A path of length n is
a sequence of n + 1
vertices and n
consecutive edges
 A cycle is a path
that begins and
ends at the same
vertex
Connectivity –
Connectedness
Undirected Graph
An undirected graph is connected if there
exists is a simple path between every pair of
vertices

Representation Example: G (V, E) is connected


since for V = {v1, v2, v3, v4, v5}, there exists a
path between {vi, vj}, 1 ≤ i, j≤ 5
v4
v1 v3

v2 v5
Connectivity –
Connectedness
Undirected Graph

 Articulation Point (Cut vertex): removal of a vertex


produces a subgraph with more connected components than
in the original graph. The removal of a cut vertex from a
connected graph produces a graph that is not connected
 Cut Edge: An edge whose removal produces a subgraph
with more connected components than in the original graph.
Representation example: G (V, E), v3 is the articulation point
or edge {v2, v3}, the number of connected components is 2
(> 1)

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 b
 A directed graph is weakly connected if there is a (undirected) path
between every two vertices in the underlying undirected path
c d

A strongly connected Graph can be weakly connected but the vice-versa is


not true (why?)
a b

c d
Unilaterally connected:
 A directed graph is unilaterally connected or
unilateral if for any pair of vertices a and b,
there is a path from a to b to b to a, i.e. one of
them is reachable from the other.
Strongly connected=> Unilaterally=>Weakly
connected

Digraph G Digraph H
rify that G is strongly connected. H is not strongly connected
H unilateral ?
Connectivity –
Connectedness
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
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.

Proof: By Mathematical Induction.


Counting Paths
 Theorem: Let G be a graph with …. of (adjacency matrix) A r.
Proof: By Mathematical Induction.

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.

By induction hypothesis, bik is the number of paths of


length r from vi to vk.

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 b ik* 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

Number of paths of length 4 from a to d is (1,4) th entry of A 4


= 8.
Path Matrix:
 Let G be a simple directed graph with m
vertices v1, v2,…,vm. The path matrix or
reachability matrix of G is the square matrix
P = [pij]m×m defined as
1 if there is a path from v to v
p 
i j

0 otherwise
ij

Theorem: Let A be the adjacency matrix of a


graph G with m vertices and let Bm= A + A2 + A3
+ …+Am. Then the path matrix P and Bm have
the same nonzero entries.
Remarks:
 In a strongly connected directed graph, for any
pair of vertices u and v in G, there is a path from u
to v and from v to u => G is Strongly connected if
and only if the path matrix P of G has no zero
entries.
 Consider the following digraph:
Contd:

0 0 0 1 1 0 1 0 1 0 0 2
     
1 0 1 1 2 0 1 2
3 0 2 3
A  A 
2

A 
3

1 0 0 1 1 0 1 1 2 0 1 2
     
1 0 1 0 1 0 0 2
2 0 2 1
2 0 2 1
  Thus B4=A+A2+A3+A4
5 0 3 5
A 
4
 4 0 3 4
3 0 2 3  
 11 0 7 11 
  
3 0 1 4 7 0 4 7
 7 
 0 4 7 
Continued:
 1 0 1 1
 By the above  
theorem, the path 1 0 1 1
P 
matrix P will be
1 0 1 1
given by  
1 0 1 1
The whole zero column represents that v2 is not
Reachable from any other vertices. Since the matrix
P has zero entries therefore the digraph is not
trongly connected.
Discrete Mathematics
Graph Theory R.K.Bajaj

Cont:
(Euler and
Hamilton)
The Seven Bridges of Königsberg,
Germany
 The residents of Königsberg wondered if it was
possible to take a walking tour of the town that
crossed each of the seven bridges over the
Presel river exactly once. Is it possible to start at
some node and take a walk that uses each edge
exactly once, and ends at the starting node?
The Seven Bridges of Königsberg

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

Redrawn: 2

4 1
3
The Seven Bridges of Königsberg

Euler:

 Has no tour that uses each edge exactly once.


 (Even if we allow the walk to start and finish in
different places.)
 Can you see why?
Euler - definitions
 An Eulerian path (Eulerian trail, Euler walk) in a graph is
a path that uses each edge precisely once. If such a path
exists, the graph is called traversable.

 An Eulerian cycle (Eulerian circuit, Euler tour) in a graph


is a cycle that uses each edge precisely once. If such a cycle
exists, the graph is called Eulerian (also unicursal).

 Representation example: G1 has Euler path a, c, d, e, b, d, a,


b

a b

c d e
The problem in our language:

Show that is not Eulerian.

In fact, it contains no Euler trail.


Euler - theorems

1. A connected graph G is Eulerian if and only if


G is connected and has no vertices of odd
degree

2. A connected graph G is has an Euler trail from


node a to some other node b if and only if G is
connected and a  b are the only two nodes of
odd degree
Euler – theorems (=>)
Assume G has an Euler trail T from node a to node b (a
and b not necessarily distinct).

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.

1. If a = b, then a also has even degree.  Euler circuit

2. If a  b, then a and b both have odd degree.  Euler


path
Theorems
 Theorem: If G is a graph with m edges and n vertices v 1, v2,…, vn, then
n

 (vi) = 2m
i=1

 Theorem: In any graph, there are an even number of vertices of


odd degree.
 Divide the vertices into two groups: even degree x 1,x2,
…,xm and odd degree y1,y2,…,yk

S=  (xp) (even)
T=  (yj) thus T is even, therefore k is even
S+T =  (Vi)= 2m (even)
Euler - theorems
1. A connected graph G is Eulerian if and only if G is
connected and has no vertices of odd degree

a b

f c d

Building a simple path:


{a,b}, {b,c}, {c,f}, {f,a}
e
Euler circuit constructed if all edges
are used. True here?
Euler - theorems
1. A connected graph G is Eulerian if and only if G is
connected and has no vertices of odd degree

c d
Delete the simple path:
{a,b}, {b,c}, {c,f}, {f,a}

e C is the common vertex for this


sub-graph with its “parent”.
Euler - theorems
1. A connected graph G is Eulerian if and only if G is
connected and has no vertices of odd degree

c d

Constructed subgraph may not be connected.

e C is the common vertex for this sub-graph


with its “parent”.

C has even degree.

Start at c and take a walk:


{c,d}, {d,e}, {e,c}
Euler - theorems
1. A connected graph G is Eulerian if and only if G is
connected and has no vertices of odd degree

a b

f c d

Building a simple path:


{a,b}, {b,c}, {c,f}, {f,a}
e
Euler circuit constructed if all edges
are used. True here?
Euler - theorems
1. A connected graph G is Eulerian if and only if G is
connected and has no vertices of odd degree

c d
Delete the simple path:
{a,b}, {b,c}, {c,f}, {f,a}

e C is the common vertex for this


sub-graph with its “parent”.
Euler - theorems
1. A connected graph G is Eulerian if and only if G is
connected and has no vertices of odd degree

c d

Constructed subgraph may not be connected.

e C is the common vertex for this sub-graph


with its “parent”.

C has even degree.

Start at c and take a walk:


{c,d}, {d,e}, {e,c}
Euler - theorems
1. A connected graph G is Eulerian if and only if G is
connected and has no vertices of odd degree

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}
Representation- Incidence
Matrix

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
Theorem?

 If a graph G contains a cycle from v to v,


G contains a simple cycle from v to v?

 A cycle (or circuit) is a path of nonzero


length from v to v with no repeated
edges.
 A simple cycle is a cycle from v to v,
there are no repeated vertices.
Homework 4- Part one
 Write a program to obtain Euler Circuits.

Include a simple user interface to “input” the
graph.
Recommended: Input graph from file
(XMLGraph format).

Input graphs can be Eulerian, no need for
checking “non” Euler graphs

Minimum of 15 edges (no more than 20 edges
needed)

Simple documentation

Include a sample graph, if needed, to test

Any programming language

 Due on final exam


Hamiltonian Graph
 Hamiltonian path (also called traceable path) is a
path that visits each vertex exactly once.

 A Hamiltonian cycle (also called Hamiltonian


circuit, vertex tour or graph cycle) is a cycle that
visits each vertex exactly once (except for the
starting vertex, which is visited once at the start and
once again at the end).
 A graph that contains a Hamiltonian path is called a
traceable graph. A graph that contains a
Hamiltonian cycle is called a Hamiltonian graph.
Any Hamiltonian cycle can be converted to a
Hamiltonian path by removing one of its edges, but a
Hamiltonian path can be extended to Hamiltonian
cycle only if its endpoints are adjacent.
A graph of the vertices of a
dodecahedron.

Is it Hamiltonian?

Yes
.
Hamiltonian Graph

This one has a Hamiltonian path, but not a


Hamiltonian tour.
Hamiltonian Graph

This one has an Euler tour, but no Hamiltonian path.


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.

 The Hamiltonian cycle problem or Hamiltonian circuit problem in


graph theory is to find a Hamiltonian cycle in a given graph. The
Hamiltonian path problem is to find a Hamiltonian path in a given
graph.

 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.

 Both problems are NP-complete. However, certain classes of graphs


always contain Hamiltonian paths.
Hamiltonian Graph
 DIRAC’S Theorem: if G is a simple graph
with n vertices with n ≥ 3 such that the
degree of every vertex in G is at least n/2
then G has a Hamilton circuit.

 ORE’S Theorem: if G is a simple graph with


n vertices with n ≥ 3 such that deg (u) + deg
(v) ≥ n fro every pair of nonadjacent vertices
u and v in G, then G has a Hamilton circuit.
Traveling salesperson
problem
 The problem of finding a
Hamiltonian cycle in a graph

 Given a weighted graph G


 Vertices: cities, Edge weights: distance
 Find a minimum-length Hamiltonian
cycle.
 Find a shortest route in which the salesperson
can visit each city one time, starting and ending
at the same city.
The 3-cube

The Hamiltonian
cycle (000, 001,
011, 010, 110, 111,
101, 100, 000)
joins vertices that
differ by one bit.
Planar Graphs:
 A graph is called planar if it can be drawn in
the plane such that its edges do not cross.
Such a drawing is called a planar
representation of the graph.
 Is K4 planar?
Other examples:

Graph Q3 Planar representation


Of Q3
Question: Draw K3,3 .Is it planar?
If not, justify your answer. Look at p-605 (Rosen)
Euler’s Formula:
 A planar representation of a graph splits the plane
into regions, including an unbounded region. The
graph in planar representation given by
R4
R2
R3 R6
R1
R5

splits the plane into six regions. Let G be a


connected planar simple graph with e edges and
v vertices. Let r be the number of regions in a
planar representation of G. Then v – e + r = 2.
Example:
 Suppose that a connected planar simple graph
has 20 vertices, each of degree 3. Into how
many regions does a representation of this
planar graph split the plane? Ans: 12
 Corollary 1. If G is a connected planar graph
with e edges and v vertices where v >= 3, then
e<= 3v -6.

 Corollary 2. If G is a connected planar simple


graph, then G has a vertex of degree not
exceeding five.
Degree of a region:
 Degree of a region is defined to be the number
of edges on the boundary of this region. When
an edge occurs twice on the boundary, it
contributes two to the degree.
Degree of R1 = 3
Degree of R2 = 6 R3
Degree of R3 = 7 R1
R2

Using previous corollary you can prove that K5 is


not a planar graph.
Kuratowski’s Theorem
(Characterization of planarity of graph
with the help of Homeomorphism)

 Statement: A graph is nonplanar if and only if it


contains a subgraph homeomorphic to K3,3 or K5.

Hence G is nonplanar
Coloring of a graph:
 Definition 1: A coloring of a simple graph is
the assignment of a color to each vertex of
the graph so that no two adjacent vertices
are assigned the same color.
 Definition 2: The chromatic number of a
graph is the least number of colors needed
 (G )it is denoted
for a coloring of the graph and
by
Example1:

Note that  (G ) 3,  ( H ) 4


Example 2: What is chromatic Number of K5 and Kn.
Ans:5 and n.
Example 3: What is Chromatic number of Km,n? Ans:
Four Color Theorem:
 The chromatic number of a planar graph is not
greater than four.
 Non planar graphs can have arbitrarily large
chromatic number.
 Consider two maps in the next slide and their
corresponding graph which are called dual
graph of the map, i.e. the problem of coloring
the regions of a map is equivalent to the
problem of coloring the vertices of the dual
graph.
 Note that in the left map, 4 colors are sufficient
and in the right map 3 colors are sufficient.
Application of Graph Coloring:
(Welch Powell Algorithm):
 How can the final exams at a university be
scheduled so that no student has two exams at
the same time?
 Modeling: Model the problem into a graph,
with vertices representing courses and with an
edge between two vertices if there is a
common student in the courses they represent
 Each time slot for a final exam is represented
by a different color.
 Thus a scheduling of the exams corresponds to
Continued:
 Suppose that there are 7
finals to be scheduled.
Number of the courses
from 1 to 7. Suppose
following pairs of courses
have common students:
1 &2 , 1 & 3, 4 & 1, 1 &
7, 2 & 3, 2 & 4, 2 & 5, 2
& 7, and so on….
 The graph associated is
given by:
Final Scheduling:
Time Period
Courses
I 1,5
II 2,6
III 3
IV 4,7
Welch Powell Algorithm
 Order the vertices of G according to
decreasing degrees. For above graph:
 Vertices: 2 3 7 4 5 6 1
 Degrees: 5 5 5 5 4 4 4
 Colors: B G P P R B R
 Example 8.21 (Look at graph on p-
219 Schaum’s Series)
Discrete Mathematics

Trees
Application Examples
 Useful for locating items in a list
 Used in Huffman coding algorithm
 Study games like checkers and chess
to determine winning strategies
 Weighted trees used to model
various categories of problems
Trees
Definition: A connected
undirected graph with no simple
circuits

Characteristics
- No multiple edges
- No loops
Example

UoK

School of School of School of


School of
Engineering Science Social
Arts
Sciences

CS EE ME Math.

Hierarchical definition
Which of the following graphs are
trees?

e a e
a b a
b

b c c
d a a
c f

e d d b
e f d c

A B C D

A & B : Trees
C: Not a Tree (Cycle abdcfa)
D: Not a Tree (Not connected). However it is a forest i.e.
unconnected graphs
Sub-trees
A subtree of a tree T is a tree T' such that
 V(T')  V(T) and
L
 E(T')  E(T)
H M

E K S

J T
Tree Theorem -Theorem
1
An undirected Graph is a tree if and only if there
is a unique simple path between any two of its
vertices
Proof (=>) The graph is a Tree and it does not have
cycles. If there were cycles then there will be more than
one simple path between two vertices. Hence, there is a
unique simple path between every pair of vertices

Proof (<=) There exists a unique simple path between any


pair of vertices. Therefore the graph is connected and
there are no cycles in the graph. Hence, the graph is a
tree
Tree terminology
Rooted tree: one vertex
designated as root and every
edge directed away from the
root
Parent: u is the parent of v
iff (if and only if) there is an
edge directed from u to v

Child: v is called the child of u

Every non-root node has a unique


parent (?)
Tree terminology
Siblings: vertices with same parent

Ancestors: all vertices on path from the root to


this vertex, excluding the vertex

Descendants: Descendants of vertex v are all


vertices that have v as an ancestor

Internal Nodes: Nodes that have children

External or Leaf Nodes: Nodes that have no


children
Given : Tree rooted at A
Find: Descendants (B), Ancestor (A), Siblings
(G) ?
A Root Node = A

Internal Nodes = A, B, C,
B E and F
C

D F
E

G H I J
K

External Nodes or leaves =


D,G, H ,I, J and K
Definition - Level

 The level of vertex v in a rooted


tree is the length of the unique
path from the root to v
 The level of root is 1
What is the level of T?
L

H M

E K S

J T
Definition - Height

 The height of a rooted tree is the


maximum of the levels of its
vertices
What is the height?
L

H M

E K S

J T
Ordered Rooted Trees:
 An ordered rooted tree is a rooted tree
where the children of each internal vertex
are shown in order from left to right.
Contd:
 Observe that number of decimal points in any label
is one less than the level of the vertex. We refere
this system of labeling as Universal address system
for an ordered rooted tree.
 The addresses in the previous tree are linearly

ordered as follows:
0 , 1, 1.1, 1.2, 1.2.1, 1.2.2, 1.2.2.1, 2, 2.1, 3, 3.1,
3.1.1, 3.2, 3.2.1, 3.2.1.1, 3.2.2
This is also called Dictionary order or Lexicographical
order.
Definition: m-ary trees
 Rooted tree where every vertex has no more
than ‘m’ children
 Full m-ary if every internal vertex has exactly
‘m’ children (i.e., except leaf/external vertices).
 m=2 gives a binary tree
 A complete m - ary tree is a full m-ary tree
where every leaf is at the same level.
 Exercise: Construct a complete binary tree of
height 4 and a complete 3-ary tree of height 3.
Example: 3-ary tree
Definition: Binary Tree
 Every internal vertex has a maximum of
2 children

 An ordered rooted tree is a rooted tree


where the children of each internal
vertex are ordered.

 In an ordered binary tree, the two


possible children of a vertex are called
the left child and the right child, if they
exist.
An Ordered Binary Tree
L

H M

E K S

J T
Definition: Balanced

 A rooted binary tree of height h


is called balanced if all its leaves
are at levels h or h-1
Is this tree balanced?
L

H M

E K S

J T
Theorem ????
 Let T be a graph with n vertices.
 The following are equivalent
 T is a tree
 T is connected and acyclic
 T is a connected and has n-1 edges
 T is acyclic and has n-1 edges
Tree Traversal
 Information is stored in Ordered Rooted tree
 Methods are needed for visiting each vertex
of the tree in order to access data
 Three most commonly used traversal
algorithms are:
1. Preorder Traversal
2. Inorder Traversal
3. Postorder Traversal
Preorder Traversal
Let T be an ordered rooted tree with
root r only r, then r is the preorder traversal.
If T has
Otherwise, suppose T1, T2 are the left and right
subtrees at r. The preorder traversal begins by
visiting r. Then traverses T1 in preorder, then
traverses T2 in preorder.
Preorder Traversal Example: J E A H T M Y

‘J’

‘E’ ‘T’

‘A’ ‘H’ ‘M’ ‘Y’


Preorder Traversal

Procedure preorder (T: ordered rooted tree)


r := root of T
List r;
for each child c of r from left to right
T(c) := subtree with c as its root
preorder(T(c));
End.
Inorder Traversal

Let T be an ordered rooted tree with


root r.
If T has only r, then r is the inorder traversal. Otherwise,
suppose T1, T2 are the left and right subtrees at r. The
inorder traversal begins by traversing T1 in inorder. Then
visits r, then traverses T2 in inorder.
Inorder Traversal: A E H J M T Y

‘J’

‘E’ ‘T’

‘A’ ‘H’ ‘M’ ‘Y’


Inorder Traversal

Procedure inorder (T: ordered rooted tree)


r = root of T
if r is a leaf list r
Else
{
l = first child of r from left to right;
T(l) = subtree with l as its root
inorder (T(l));
list r;
for each child c of r except for l from left to right
{
T(c) = subtree with c as its root
inorder(T(c));
}
}
End.
Postorder Traversal

Let T be an ordered rooted tree with


root r.
If T has only r, then r is the postorder traversal. Otherwise,
suppose T1, T2 are the left and right subtrees at r. The
postorder traversal begins by traversing T1 in postorder.
Then traverses T2 in postorder, then ends by visiting r.
Postorder Traversal: A H E M Y T

‘J’

‘E’ ‘T’

‘A’ ‘H’ ‘M’ ‘Y’


Postorder Traversal

Procedure postorder (T: ordered rooted tree)


r := root of T
for each child c of r from left to right
T(c) := subtree with c as its root
postorder(T(c));
End.
List r.
Infix, Prefix and Postfix
Notation
 We can represent complicated expressions, such as
compound propositions, combinations of sets, and
arithmetic expressions using ordered rooted trees.
 A binary tree representing arithmetic expression

+ - +
/
x y x 4
- 3
/ + 2

2 - 3 x y x 4
+

y x 4
x
Thus in this way, for such a given
expression we can draw an ordered
rooted tree in which the operations are
the internal nodes and variables are
leaves.
# We obtain the infix form of an expression when
we traverse its rooted tree in inorder. In inorder
traversal it is necessary to include parentheses
whenever we encounter an operation because the
infix form of the expressions (x+y)/(x+3) ,
(x+(y/x))+3, and x+(y/(x+3)) will be x+y/x+3.

# The prefix form of an expression is obtained by


traversing its rooted tree in preorder. Expression
written in prefix form are said to be in
Polish Notation, which is named after the Polish
Logician Jan Lukasiewicz. No parentheses is needed
# Sequentially the postfix form are said to
be reverse Polish Notation.
(Parentheses not needed)

 What is the value of the prefix


expression + - * 2 3 5 / 2 3 4
 Ans : 3
Minimum Spanning Tree (Min SPT)

Given: Graph G=(V, E), |V|=n Cost function c: E  R .


Goal: Find a minimum-cost spanning tree for V i.e.,
find a subset of arcs E*  E which connects any two
nodes of V with minimum possible cost
Example:

G=(V,E) Min. span. tree: G*=(V,E*)


3 b 3 d
b d
2 2 8
8
a 3 a 3 5
5
4 7 4 7
c e c e
4 4
Red bold arcs are in E*
Algorithms for Min SPT

 Prim’s Algorithm

 Kruskal’s Algorithm
Prims Algorithm
Procedure Prims (G: weighted connected undirected graph
with n vertices)
T := a minimum-weight edge
For i := 1 to n-2
Begin
e:= an edge of minimum weight incident to a vertex in
T and not forming a simple circuit in T if added to T
T := T with e added
End {T is a minimum spanning tree of G}
Prim’s Example
 Initially
3
2 b d
8 T = {}
a 5
3
7
4 c e
4
Prim’s Example
 Initial step, chose min weight edge
3
2 b d
8 T = {{a, b}}
a 5
3
7
4 c e
4
Prim’s Example
 Iteration 1
3
2 b d
8 T = {{a, b}, {b, c}}
a 5
3
7
4 c e
4
Prim’s Example
 Iteration 2
3
2 b d
8 T = {{a, b}, {b, c}, {b,
a 5
3 d}}
7
4 c e
4
Prim’s Example
 Iteration 3
3
2 b d
8 T = {{a, b}, {b, c}, {b,
a 5 d} , {c, e}}
3
7
4 c e
4
Kruskal’s algorithm
Procedure Kruskal (G: weighted connected undirected
graph with n vertices)
T := empty graph
For i := 1 to n-1
Begin
e := any edge in G with smallest weight that does
not form simple circuit when added to T
T := T with e added
End { T is the minimum spanning tree of G}
Kruskal’s Example
 Initially
3
2 b d
8 T = {}
a 5
3
7
4 c e
4
Kruskal’s Example
 Iteration 1
3
2 b d
8 T = {{a, b}}
a 5
3
7
4 c e
4
Kruskal’s Example
 Iteration 2
3
2 b d
8 T = {{a, b}, {b,
a 5
3 c}}
7
4 c e
4
Kruskal’s Example
 Iteration 3
3
2 b d
8 T = {{a, b}, {b, c}, {b,
a 5
3 d}}
7
4 c e
4
Kruskal’s Example
 Iteration 4
3
2 b d
8 T = {{a, b}, {b, c}, {b,
a 5 d} , {c, e}}
3
7
4 c e
4
Prim’s and Kruskal’s
 Prim’s
 Chosen edges: minimum weight &
incident to a vertex already in the
tree.
 Kruskal’s
 Chosen edge: minimum weight & not
necessarily
Understand theincident
difference:to a vertex in the
tree.
http://students.ceid.upatras.gr/~papagel/project/prim.htm

http://students.ceid.upatras.gr/~papagel/project/kruskal.htm

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy