Slide 9 - Graph
Slide 9 - Graph
The truth prevails in the end. I will always stand for the truth
License
Special Thanks
• Sylvia Sorkin, Community College of Baltimore County - Essex Campus
• Zeph Grunschlag
Use of 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,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.
• 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)
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
e1
1 e2 2
e3 e4 e5
3 e6 4
e3 e4 e5
3 e6 4
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
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.
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}.
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:
L23 43
EG: C4 is a bichromatic:
L23 44
EG: C4 is a bichromatic:
L23 45
EG: C4 is a bichromatic:
L23 46
EG: C4 is a bichromatic:
L23 47
Bipartite Graphs
EG: C4 is a bichromatic:
L23 48
EG: C4 is a bichromatic:
L23 49
EG: C4 is a bichromatic:
L23 50
Bipartite Graphs
EG: C4 is a bichromatic:
L23 51
EG: C4 is a bichromatic:
L23 52
EG: C4 is a bichromatic:
L23 53
EG: C4 is a bichromatic:
L23 56
Graph Patterns
Complete Bipartite - Km,n
K2,3 K4,5
L23 57
Degree of a vertex
c d
b
deg( d ) = 1
a g f e
58
Degree
Degree of a vertex
of a vertex
c d
b
deg( e ) = 0
a g f e
59
Degree
Degree of a vertex
of a vertex
c d
deg( b ) = 6 b
a g f e
60
Degree
Degree of a vertex
of a vertex
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
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
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.
TOTAL of degrees = 2 + 4 + 3 + 4 + 6 + 1 + 0 = 20
deg( e ) = 0
a g f e
64
Handshaking Theorem
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
1 if { vi, vj } is an edge in G,
aij =
0 otherwise.
66
Finding the adjacency matrix
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