0% found this document useful (0 votes)
7 views

Slide 9 - Graph

Uploaded by

crid
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Slide 9 - Graph

Uploaded by

crid
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 78

Graphs

Nakib Hayat Chowdhury


Assistant Professor, Dept. of CSE, BAUST

The truth prevails in the end. I will always stand for the truth
License

Your are free –


• to Share – to copy, distribute and transmit the work
• to Remix – to adapt the work

For non commercial and educational purpose.


References
Discrete Mathematics and its Applications by Kenneth H. Rosen
• Chapter 10 : Graphs

Special Thanks
• Sylvia Sorkin, Community College of Baltimore County - Essex Campus
• Zeph Grunschlag
Use of Graphs

• Car navigation system


• Efficient database
• Build a bot to retrieve info off WWW
• Representing computational models
• Many other applications.

This course we focus more on the properties of abstract graphs


rather on algorithms.
Graphs

• Graphs is special data structure, which we can represent through two sets-
• Set of Vertices / Nodes - V
• Set of Edges - E
Dhaka1
Rajshahi 2
Dilly
DHK

• Let,
• V = {1, 2, 3, 4}
• E = {(1,2), (2,3), (1,4)} 3
Bangkok
Khulna 4CTG
NY
Graphs –Intuitive Notion
• A graph is a bunch of vertices (or nodes) represented by circles which
are connected by edges, represented by line segments
Graphs

Definition
A graph G = (V , E) consists of V, a nonempty set of vertices (or nodes)
and E, a set of edges (possibly empty). Each edge has either one or two
vertices associated with it, called its end points. An edge is said to
connect its endpoints.
Graphs Example
{1,2}
1 2

{1,3} {2,3} {2,4}


{3,4}
3 4

{1,4}

SET OF VERTICES

V = { 1, 2, 3, 4 }
SET OF EDGES
E = { (1, 2), (1, 3), (2, 3), (2, 4), (3, 4), (1, 4) }
L23 8
Simple Graphs Example
Dhaka Dilly SET OF VERTICES

SET OF EDGES

Bangkok NY
Infinite and Finite Graphs

Infinite Graphs
A graph with an infinite vertex set or an infinite number of edges is
called an infinite graph.

Finite Graphs
A graph with a finite vertex set and a finite edge set is called a finite
graph.
Simple Graphs

Definition
A graph in which each edge connects two different vertices and where
no two edges connect the same pair of vertices is called a simple graph.

Dhaka Dilly

Bangkok NY
Multigraphs

Definition
Graphs that may have multiple edges connecting the same vertices are
called multigraphs.

Dhaka Dilly

Bangkok NY
Pseudographs
Definition
Edges that connect a vertex to itself are called loops. A graph with loop
(self-loop) is called pseudograph.

Dhaka
Dilly

Bangkok NY
Undirected Graphs

Dhaka
Dilly

Bangkok NY
Directed Graphs

• Dhaka to Rajshahi
• Dhaka to Chapai
• Rajshahi to Dhaka
• No Chapai to Dhaka, but Chapai to Rajshahi

Dhaka
Rajshahi

Chapai
A Directed Graph

Definition
A directed graph (or digraph) (V, E) consists of a nonempty set of vertices V
and a set of directed edges (or arcs) E. Each directed edge is associated with
an ordered pair of vertices. The directed edge associated with the ordered
pair (u, v) is said to start at u and end at v.

The edge (a,b) is also denoted by a b


• a is called the source of the edge while
• b is called the target of the edge.
Directed Graphs

• Simple Directed Graphs

• Multiple Directed Graphs

• Mixed Graphs
Types of Graphs
Undirected Graphs: Adjacent
Definition
Vertices are adjacent if they are the endpoints of the same edge or they
are connected by the same edge.

1 2
Adjacent of 1 : 2 and 4

Adjacent of 2 : 1 and 3

Adjacent of 3 : 2 3 4
Undirected Graphs Terminology
Adjacent Vertices (Neighbors)

Definition 1. Two vertices, u and v in an undirected graph G are


called adjacent (or neighbors) in G, if {u, v} is an edge of G.

An edge e connecting u and v is called incident with vertices u and


v, or is said to connect u and v. The vertices u and v are called
endpoints of edge {u, v}.

20
Undirected Graphs
Terminology
e1
1 e2 2

e3 e4 e5
3 e6 4

A: 1 is adjacent to 2 and 3
2 is adjacent to 1 and 3
3 is adjacent to 1 and 2
4 is not adjacent to any vertex

L23 21
Undirected Graphs
Terminology

A vertex is incident with an edge (and the edge is


incident with the vertex) if it is the endpoint of the edge.

e1
1 e2 2

e3 e4 e5
3 e6 4

Q: Which edges are incident to 1? How about incident to


2, 3, and 4?
L23 22
Undirected Graphs
Terminology
e1
1 e2 2

e3 e4 e5
3 e6 4

A: e1, e2, e3, e6 are incident with 1


2 is incident with e1, e2, e4, e5, e6
3 is incident with e3, e4, e5
4 is not incident with any edge
L23 23
Undirected Graphs: Degree
Definition
The degree of a vertex in an undirected graph is the number of edges incident with
it, except that a loop at a vertex contributes twice to the degree of that vertex. The
degree of the vertex v is denoted by deg(v).

1 2
deg (1) : 2

deg (3) : 1

deg (4) : 3 3 4
Directed Graphs : Adjacent
Definition

1 2
Adjacent of 1 : 2 and 4

Adjacent of 2 : 3

Adjacent of 3 : 3 4
Directed Graphs : Degree
Definition
Directed Graphs : Degree

1 2

In degree :

Out degree:
3 4
Oriented Degree
when Edges Directed
The in-degree of a vertex (deg-) counts the number of edges
that stick in to the vertex.
The out-degree (deg+) counts the number sticking out.

1 3

Q: What are in-degrees and out-degrees of all the vertices?


L23 28
Oriented Degree
when Edges Directed
A: deg-(1) = 0
deg-(2) = 3
deg-(3) = 4 2
deg+(1) = 2
1 3
deg+(2) = 3
deg+(3) = 2

L23 29
Feedback
At last Class: In this Class:
1. Definition Of Graphs 1. Special Simple Graphs
• Simple Graphs • Complete Graphs
• Infinite and Finite Graphs • Cycles
• Multigraphs • Wheels
• Pseudographs • Cubes
• Directed Graphs 2. Bipartite
2. Adjacent 3. Handshaking Theorem
3. Degree 4. Adjacency Matrix
• In degree
• Out degree
Some Special Simple Graphs :
Complete Graphs - Kn
Complete Graphs - Kn
A complete graph on n vertices, denoted by Kn, is a simple graph that
contains exactly one edge between each pair of distinct vertices.

No. of Edges = n(n-1) / 2


Graph Patterns
Complete Graphs - Kn
Complete Graphs – Kn , when n = 1, 2, 3, 4, 5

K1 K2 K3 K4 K5

L23 32
Some Special Simple Graphs : Cycles -
Cn
Cycles - Cn
A cycle Cn, n ≥ 3, consists of n vertices v1, v2, ….., vn and edges {v1, v2},
{v2, v3}, ..., {vn−1, vn}, and {vn, v1}.

That is, vertex i is connected to i +1 mod n and to i -1 mod n vertex.

No. of Edges = n
Graph Patterns
Cycles - Cn
Q: What type of graph are C1 and C2 ?

C1 C2 C3 C4 C5
A: Pseudographs
L23 34
Some Special Simple Graphs : Wheel
Graph
Wheel Graph
We obtain a wheel Wn when we add an additional vertex to a cycle Cn,
for n ≥ 3, and connect this new vertex to each of then vertices in Cn, by
new edges.
Graph Patterns
Wheels - Wn
Wn ; n = 3, 4, 5

W3 W4 W5

L23 36
Some Special Simple Graphs : Cubes -
Qn
Cubes - Qn
The n-cube Qn is defined recursively. Q0 is just a vertex. Qn+1 is gotten by
taking 2 copies of Qn and joining each vertex v of Qn with its copy v’ :

L23 Q0 Q1 Q2 Q3 Q4 (hypercube)
37
Some Special Simple Graphs
Cubes - Qn
The n-cube Qn is defined recursively. Q0 is just a vertex. Qn+1 is gotten by
taking 2 copies of Qn and joining each vertex v of Qn with its copy v’ :

Q0 Q1 Q2 Q3 Q4 (hypercube)
L23 38
Some Special Simple Graphs
Cubes - Qn
The n-cube Qn is defined recursively. Q0 is just a vertex. Qn+1 is gotten by
taking 2 copies of Qn and joining each vertex v of Qn with its copy v’ :

L23 Q0 Q1 Q2 Q3 Q4 (hypercube)
39
Bipartite Graphs

Definition
Is Bipartite Graphs?
Bipartite Graphs

Another definition

L23 42
EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

L23 43
EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

L23 44
EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

L23 45
EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

L23 46
EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

L23 47
Bipartite Graphs
EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

L23 48
EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

L23 49
EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

L23 50
Bipartite Graphs
EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

L23 51
EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

L23 52
EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

L23 53
EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

Q: For which n is Cn bipartite?


L23 54
Is Bipartite Graphs?
Bipartite Graphs

A: Cn is bipartite when n is even. For even n color


all odd numbers red and all even numbers green
so that vertices are only adjacent to opposite color.
If n is odd, Cn is not bipartite. If it were, color 0 red.
So 1 must be green, and 2 must be red. This way,
all even numbers must be red, including vertex n-
1. But n-1 connects to 0 .

L23 56
Graph Patterns
Complete Bipartite - Km,n

When all possible edges exist in a simple bipartite


graph with m red vertices and n green vertices, the
graph is called complete bipartite and the notation
Km,n is used. EG:

K2,3 K4,5

L23 57
Degree of a vertex

Definition 1. The degree of a vertex in an undirected graph is the


number of edges incident with it, except that a loop at a vertex
contributes twice to the degree of that vertex.

c d
b
deg( d ) = 1

a g f e

58
Degree
Degree of a vertex
of a vertex

Definition 1. The degree of a vertex in an undirected graph is the number of


edges incident with it, except that a loop at a vertex contributes twice to
the degree of that vertex.

c d
b

deg( e ) = 0
a g f e

59
Degree
Degree of a vertex
of a vertex

Definition 1. The degree of a vertex in an undirected graph is the number


of edges incident with it, except that a loop at a vertex contributes twice
to the degree of that vertex.

c d
deg( b ) = 6 b

a g f e

60
Degree
Degree of a vertex
of a vertex

Find the degree of all the other vertices.

deg( a ) deg( c ) deg( f )deg( g )

c d
deg( b ) = 6 b
deg( d ) = 1

deg( e ) = 0
a g f e

61
Degree
Degree of a vertex
of a vertex

Find the degree of all the other vertices.

deg( a ) = 2 deg( c ) = 4 deg( f ) = 3 deg( g ) = 4

c d
deg( b ) = 6 b
deg( d ) = 1

deg( e ) = 0
a g f e

62
Degree
Degree of a vertex
of a vertex

Find the degree of all the other vertices.

deg( a ) = 2 deg( c ) = 4 deg( f ) = 3 deg( g ) = 4

TOTAL of degrees = 2 + 4 + 3 + 4 + 6 + 1 + 0 = 20

c d
deg( b ) = 6 b
deg( d ) = 1

deg( e ) = 0
a g f e

63
Degree
Degree of a vertex
of a vertex
Find the degree of all the other vertices.

deg( a ) = 2 deg( c ) = 4 deg( f ) = 3 deg( g ) = 4

TOTAL of degrees = 2 + 4 + 3 + 4 + 6 + 1 + 0 = 20

TOTAL NUMBER OF EDGES = 10


c d
deg( b ) = 6 b
deg( d ) = 1

deg( e ) = 0
a g f e

64
Handshaking Theorem

Theorem 1. Let G = (V, E) be an undirected graph


G with e edges. Then

 deg( v ) = 2 e
v V

“The sum of the degrees over all the vertices equals twice
the number of edges.”

NOTE: This applies even if multiple edges and loops are present.
65
Adjacency Matrix

A simple graph G = (V, E) with n vertices


can be represented by its adjacency matrix,
A, where entry aij in row i and column j is

1 if { vi, vj } is an edge in G,
aij =
0 otherwise.

66
Finding the adjacency matrix

c This graph has 6 vertices


a, b, c, d, e, f. We can
b d arrange them in that order.
f

a e

W5

67
Finding
Finding theadjacency
the adjacency matrix
matrix
TO
c
FROM
a b c d e f

b d
a 0 1 0 0 1 1
f b

a e
c
d
W5 e
f
There are edges from a to b, from a to e, and from a to f
68
Finding
Finding theadjacency
the adjacency matrix
matrix
TO
c
FROM
a b c d e f

a 0 1 0 0 1 1
b d
f b 1 0 1 0 0 1
c
a e
d
W5 e
f
There are edges from b to a, from b to c, and from b to f
69
Finding
Finding theadjacency
the adjacency matrix
matrix
TO
c
FROM
a b c d e f

a 0 1 0 0 1 1
b d
f b 1 0 1 0 0 1
c 0 1 0 1 0 1
a e
d
W5 e
f
There are edges from c to b, from c to d, and from c to f
70
Finding
Finding theadjacency
the adjacency matrix
matrix
TO
c
FROM
a b c d e f

a 0 1 0 0 1 1
b d
f b 1 0 1 0 0 1
c 0 1 0 1 0 1
a e
d
W5 e
f
COMPLETE THE ADJACENCY MATRIX . . .
71
Finding
Finding theadjacency
the adjacency matrix
matrix
TO
c
FROM
a b c d e f

a 0 1 0 0 1 1
b d
f b 1 0 1 0 0 1
c 0 1 0 1 0 1
a e
d 0 0 1 0 1 1
W5 e 1 0 0 1 0 1
f 1 1 1 1 1 0
Notice that this matrix is symmetric. That is aij = aji Why?
72
Isomorphism of Graphs

Definition

• When two simple graphs are isomorphic, there is a one-to-one correspondence


between vertices of the two graphs that preserves the adjacency relationship.
• Isomorphism of simple graphs is an equivalence relation.
Isomorphic?
Isomorphic?
Isomorphic?
How to show that two graphs are not
isomorphic?
• Two graphs are not isomorphic if we can find a property only one of the two graphs
has, but that is preserved by isomorphism.
• A property preserved by isomorphism of graphs is called a graph invariant.
• Isomorphic simple graphs must have the same number of vertices, because there
is a one-to-one correspondence between the sets of vertices of the graphs.
• Isomorphic simple graphs also must have the same number of edges, because
the one-to-one correspondence between vertices establishes a one-to-one
correspondence between edges.
Feedback
In this Class:
1. Special Simple Graphs
• Complete Graphs
• Cycles
• Wheels
• Cubes
2. Bipartite
3. Handshaking Theorem
4. Adjacency Matrix

You might also like