Lec (Graph1)
Lec (Graph1)
5/11/2024 1
Agenda
• Graph basics and definitions
5/11/2024 2
Agenda
• Types of Graphs
Trees
Undirected graphs
• Simple graphs, Multigraphs,
Pseudographs
Digraphs, Directed multigraph
Bipartite
Complete graphs, cycles, wheels, cubes,
complete bipartite
5/11/2024 3
Uses of Graph Theory in CS
• Efficient database
5/11/2024 4
Graphs –Intuitive Notion
A graph is a bunch of vertices (or nodes) represented by
circles which are connected by edges, represented by
line segments.
5/11/2024 5
Trees
Real Tree
5/11/2024 6
Trees
Real
Tree
transformation
5/11/2024 7
Trees
Real
Tree
transformation
5/11/2024 8
Trees
A very important type of graph in CS is called a tree:
Real Abstract
Tree Tree
transformation
5/11/2024 9
Simple Graphs
5/11/2024 10
Simple Graphs
{1,2}
1 2
{1,3} {2,3} {2,4}
{3,4}
3 4
{1,4}
5/11/2024 11
Simple Graphs
5/11/2024 12
Simple Graphs
5/11/2024 13
Simple Graphs
5/11/2024 14
Multigraphs
• If computers are connected via internet instead of directly,
there may be several routes to choose from for each
connection. Depending on traffic, one route could be
better than another. Makes sense to allow multiple edges,
but still no self-loops:
5/11/2024 15
Multigraphs
e1
1 e2 2
e3 e 4 e5
3 e6 4
5/11/2024 16
Multigraphs
DEF: A multigraph G = (V,E,f ) consists of a non-empty set
V of vertices (or nodes), a set E (possibly empty) of
edges and a function f with domain E and codomain the
set of pairs in V.
5/11/2024 17
Pseudographs
If self-loops are allowed we get a pseudograph:
e1 e6
1 e2 2
e3 e4 e5 e7
3 4
5/11/2024 18
Graph Terminology
5/11/2024 19
8.2 Undirected Graphs Terminology
Vertices are adjacent if they are the endpoints of the same
edge.
e1
1 e2 2
e3 e4 e5
3 e6 4
5/11/2024 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
5/11/2024 21
Undirected Graphs Terminology
e1
1 e2 2
e3 e4 e5
3 e6 4
5/11/2024 22
Undirected Graphs Terminology
e1
1 e2 2
e3 e4 e5
3 e6 4
5/11/2024 23
Digraphs
1 3
(2,2)
2
(1,2) (2,3)
5/11/2024 25
Digraphs
5/11/2024 26
Digraphs
A: The same as the number of relations on V, which is the
number of subsets of V V so 2n·n.
5/11/2024 27
Directed Multigraphs
If also want to allow multiple edges in a digraph, get a
directed multigraph (or multi-digraph).
1 3
5/11/2024 29
Degree
e1 e6
1 e2 2 e5
e3 e4
3
5/11/2024 30
Degree
e1 e6
1 e2 2 e5
e3 e4
3
magnify
5/11/2024 31
Degree
The degree of a vertex counts the number of edges that
seem to be sticking out if you looked under a magnifying
glass:
e1 e6
1 e2 2 e5 magnify
e3 e4
3
5/11/2024 32
Degree
e1 e6
1 e2 2 e5 magnify
e3 e4
3
5/11/2024 33
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
5/11/2024 34
Oriented Degree
when Edges Directed
A: deg-(1) = 0
deg-(2) = 3
deg-(3) = 4
deg+(1) = 2 2
deg+(2) = 3
deg+(3) = 2 1 3
5/11/2024 35
Handshaking Theorem
e1
1 e2 2 e6
e3 e4 e5 e7
3 4
( deg(1)+deg(2)+deg(3)+deg(4) )/2
5/11/2024 36
= (3+7+2+2)/2 = 14/2 = 7
Handshaking Theorem
1
| E | deg( e)
2 eE
In a directed graph
|E| deg
eE
(e) deg
eE
(e)
Q: In a party of 5 people can each person be friends with
exactly three others?
5/11/2024 37
Handshaking Theorem
5/11/2024 38
Handshaking Theorem
Lemma: The number of vertices of odd degree must be even
in an undirected graph.
5/11/2024 39
Exercises:
1. How many edges are there in a graph with ten
vertices each of degree six?
5/11/2024 41
Complete Graphs - Kn
• The complete graph on n vertices , is the simple graph
that contains exactly one edge between each pair of
distinct vertices. The notation Kn denotes the complete
graph on n vertices.
K1 K2 K3 K4 K5
5/11/2024 42
Cycles graph - Cn
C1 C2 C3 C4 C5
5/11/2024 43
Wheels Graph- Wn
W1 W2 W3 W4 W5
5/11/2024 44
Cubes graphs - Qn
Q0 Q1 Q2 Q3 Q4 (hypercube)
5/11/2024 45
Bipartite Graphs
5/11/2024 46
Bipartite Graphs
EG: C4 is a bichromatic:
5/11/2024 47
Bipartite Graphs
EG: C4 is a bichromatic:
5/11/2024 48
Bipartite Graphs
EG: C4 is a bichromatic:
5/11/2024 49
Bipartite Graphs
EG: C4 is a bichromatic:
5/11/2024 50
Bipartite Graphs
EG: C4 is a bichromatic:
5/11/2024 51
Bipartite Graphs
EG: C4 is a bichromatic:
5/11/2024 52
Bipartite Graphs
EG: C4 is a bichromatic:
5/11/2024 53
Bipartite Graphs
EG: C4 is a bichromatic:
5/11/2024 54
Bipartite Graphs
EG: C4 is a bichromatic:
5/11/2024 55
Bipartite Graphs
EG: C4 is a bichromatic:
5/11/2024 56
Bipartite Graphs
EG: C4 is a bichromatic:
5/11/2024 57
Bipartite Graphs
EG: C4 is a bichromatic:
5/11/2024 58
Q: Determine whether the graph is bipartite
• (a) (b)
5/11/2024 59
Bipartite Graphs
5/11/2024 60
Complete Bipartite - Km,n
Example:
K2,3 K4,5
5/11/2024 61
Some applications of special types of
graphs
• We will show how special types of graphs are used in
models for data communications and parallel
processing.
5/11/2024 62
Representing Graphs and Graphs Isomorphism
5/11/2024 63
Adjacency Matrix
We already saw a way of representing relations on a set with
a Boolean matrix:
R digraph(R) MR
1 1
2 2
2
3 3 1 1 1 1
1 3 0 1 1 1
4 4
0 0 1 1
4
0 1
5/11/2024
0 0
64
Adjacency Matrix
– 0 otherwise
5/11/2024 65
Adjacency Matrix
(Directed Multigraphs)
Can easily generalize to directed multigraphs by putting in the
number of edges between vertices, instead of only allowing 0
and 1:
5/11/2024 66
Adjacency Matrix
(Directed Multigraphs)
Q: What is the adjacency matrix?
1 4 3
5/11/2024 67
Adjacency Matrix
(Directed Multigraphs)
A:
1 4 3
0 3 0 1
0 1 2 0
0 1 2 0
0 0
0 0
5/11/2024 68
Adjacency Matrix
1 2 1 2
3 4 3 4
5/11/2024 69
Adjacency Matrix
1 2
3 4
5/11/2024 70
Adjacency Matrix
1 2
A: 3 4
0 2 1 0
2 2 1 0
Notice that answer is symmetric. 1 1 0 0
0 1
0 0
5/11/2024 71
Adjacency Matrix
5/11/2024 72
Incidence Matrices
• Another common way to represent graphs is to use
incidence matrices. Let G = (V,E) be an undirected graph.
Suppose that v1, v2, …., vn are the vertices and e1, e2, ….,
en are the edges of G. Then the incidence matrix with
respect to this ordering of V and E is the
n X m matrix M = [mij], where
5/11/2024 73
Represent the graph using an incidence matrix
v2 e6
v1 v3
e3
e4 e5
e1
e2
v5
v4
5/11/2024 74
The incidence matrix is
e1 e2 e3 e4 e5 e6
1 1 0 0 0 0
v1
0 0 1 1 0
1
v2
0 0 0 0 1 1
v3
1 0 1 0 0 0
v4
0 1 0 1 1 0
v5
5/11/2024 75
• Find an adjacency matrix for each of these
graphs.
a) K4
b) C4
c) W3
d) K2,3
e) Q2
5/11/2024 76
Graph Isomorphism
Various mathematical notions come with their own concept of
equivalence, as opposed to equality:
5/11/2024 77
Graph Isomorphism
to obtain:
5/11/2024 78
Graph Isomorphism
Undirected Graphs
DEF: Suppose G1 = (V1, E1 ) and G2 = (V2, E2 ) are pseudographs.
Let f :V1V2 be a function s.t.:
1) f is bijective
2) for all vertices u,v in V1, the number of edges between u
and v in G1 is the same as the number of edges between
f (u) and f (v ) in G2.
5/11/2024 79
Graph Isomorphism
Digraphs
DEF: Suppose G1 = (V1, E1 ) and G2 = (V2, E2 ) are directed
multigraphs. Let f :V1V2 be a function s.t.:
1) f is bijective
2) for all vertices u,v in V1, the number of edges from u
to v in G1 is the same as the number of edges
between f (u) and f (v ) in G2.
is isomorphic to .
2 2
1 3 1 3
5 4
5 4
5/11/2024 81
Graph Isomorphism
-Example
Next, set f (1) = 1 and try to walk around clockwise on
the star.
2 2
1 3 1 3
5 4
5 4
5/11/2024 82
Graph Isomorphism
-Example
Next, set f (1) = 1 and try to walk around clockwise on
the star. The next vertex seen is 3, not 2 so set f
(2) = 3.
2 2
1 3 1 3
5 4
5 4
5/11/2024 83
Graph Isomorphism
-Example
• Next, set f (1) = 1 and try to walk around clockwise on the
star. The next vertex seen is 3, not 2 so set f (2) = 3. Next
vertex is 5 so set f (3) = 5.
2 2
1 1
3 3
5 4 5 4
5/11/2024 84
Graph Isomorphism
-Example
• Next, set f (1) = 1 and try to walk around clockwise on
the star. The next vertex seen is 3, not 2 so set f (2) =
3. Next vertex is 5 so set f (3) = 5. In this fashion we
get f (4) = 2
2 2
1 3 1 3
5 4
5 4
5/11/2024 85
Graph Isomorphism
-Example
Next, set f (1) = 1 and try to walk around clockwise on
the star. The next vertex seen is 3, not 2 so set f
(2) = 3. Next vertex is 5 so set f (3) = 5. In this
fashion we get f (4) = 2, f (5) = 4.
2 2
1 3 1 3
5 4 5 4
5/11/2024 86
Graph Isomorphism
-Example
Next, set f (1) = 1 and try to walk around clockwise on
the star. The next vertex seen is 3, not 2 so set f (2) =
3. Next vertex is 5 so set f (3) = 5. In this fashion we
get f (4) = 2, f (5) = 4. If we would continue, we
would get back to f (1) =1 so this process is well
defined and f is a morphism.
2 2
1 3 1 3
5 4
5 4
5/11/2024 87