0% found this document useful (0 votes)
15 views87 pages

Lec (Graph1)

Uploaded by

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

Lec (Graph1)

Uploaded by

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

Graphs

5/11/2024 1
Agenda
• Graph basics and definitions

 Vertices/nodes, edges, adjacency,


incidence
 Degree, in-degree, out-degree
 Degree, in-degree, out-degree
 Subgraphs, unions, isomorphism
 Adjacency matrices

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

• Car navigation system

• Efficient database

• Build a bot to retrieve info off WWW

• Representing computational models

• Many other applications.

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.

 Mathematically, graphs are binary-relations on their vertex


set (except for multigraphs).

 In Data Structures one often starts with trees and


generalizes to graphs. In this course, opposite approach:
We start with graphs and restrict to get trees.

5/11/2024 5
Trees

A very important type of graph in CS is called a tree:

Real Tree

5/11/2024 6
Trees

A very important type of graph in CS is called a tree:

Real
Tree

transformation
5/11/2024 7
Trees

A very important type of graph in CS is called a tree:

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

Different purposes require different types of graphs.

EG: Suppose a local computer network


 Is bidirectional (undirected)
 Has no loops (no “self-communication”)
 Has unique connections between computers

Sensible to represent as follows:

5/11/2024 10
Simple Graphs

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

• Vertices are labeled to associate with particular computers

• Each edge can be viewed as the set of its two endpoints

5/11/2024 11
Simple Graphs

Q: For a set V with n elements, how many possible edges


there?

5/11/2024 12
Simple Graphs

A: The number of pairs in V


= C (n, 2) = n · (n -1) / 2

Q: How many possible graphs are there for the same


set of vertices V ?

5/11/2024 13
Simple Graphs

A: The number of subsets in the set of possible edges.


There are n · (n -1) / 2 possible edges, therefore the
number of graphs on V is 2n(n -1)/2

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

Edge-labels distinguish between edges sharing same endpoints.


Labeling can be thought of as function:

e1  {1,2}, e2  {1,2}, e3  {1,3}, e4  {2,3},


e5  {2,3}, e6  {1,2}

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

Now edges may be associated with a single vertex, when the


edge is a loop
e1  {1,2}, e2  {1,2}, e3  {1,3},
e4  {2,3}, e5  {2}, e6  {2}, e7  {4}

5/11/2024 18
Graph Terminology

TABLE 1: Graph Terminology


Type Edges Multiple Edges Loops
Allowed? Allowed
?
Simple Graph Undirected No No

Multigraph Undirected Yes No

Pseudograph Undirected Yes Yes

Directed graph Directed No Yes

Directed Multigraph Directed Yes Yes

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

Q: Which vertices are adjacent to 1? How about adjacent to


2, 3, and 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

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?

5/11/2024 22
Undirected Graphs Terminology

e1
1 e2 2
e3 e4 e5
3 e6 4

Ans: 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

5/11/2024 23
Digraphs

Last time introduced digraphs as a way of representing


relations:
2

1 3

Q: What type of pair should each edge be (multiple edges not


allowed)?
5/11/2024 24
Digraphs
A: Each edge is directed so an ordered pair (or tuple) rather
than unordered pair.

(2,2)
2
(1,2) (2,3)

(1,1) 1 (1,3) 3 (3,3)


(2,4) (3,4)
(1, 4)
4
(4,4)

Thus the set of edges E is just the represented relation on V.

5/11/2024 25
Digraphs

DEF: A directed graph (or digraph) G = (V,E ) consists of a


non-empty set V of vertices (or nodes) and a set E of
edges with E  V  V.

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


source of the edge while b is called the target of the
edge.

Q: For a set V with n elements, how many possible digraphs


are there?

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

Q: How to use sets and functions to deal with multiple


directed edges, loops?
5/11/2024 28
Directed Multigraphs

A: Have function with domain the edge set and co-


domain
VV. e3
2
e1 e4 e6
e2 e5
1 3
e7

e1(1,2), e2(1,2), e3(2,2), e4  (2,3),


e5  (2,3), e6  (3,3), e7  (3,3)

5/11/2024 29
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
e3 e4
3

5/11/2024 30
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
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

Thus deg(2) = 7 even though 2 only incident with 5 edges.


Q: How to define this formally?

5/11/2024 32
Degree

A: Add 1 for every regular edge incident with vertex and 2


for every loop. Thus deg(2) = 1 + 1 + 1 + 2 + 2 = 7

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

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

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

There are two ways to count the number of edges in the


above graph:
1. Just count the set of edges: 7

2. Count seeming edges vertex by vertex and divide by 2


because double-counted edges:

( deg(1)+deg(2)+deg(3)+deg(4) )/2
5/11/2024 36
= (3+7+2+2)/2 = 14/2 = 7
Handshaking Theorem

THM: In an undirected graph

1
| E |   deg( e)
2 eE
In a directed graph

|E|   deg
eE

(e)   deg
eE

(e)
Q: In a party of 5 people can each person be friends with
exactly three others?

5/11/2024 37
Handshaking Theorem

A: Imagine a simple graph with 5 people as vertices and edges


being undirected edges between friends (simple graph
assuming friendship is symmetric and irreflexive).
Number of friends each person has is the degree of the
person.

Handshaking would imply that


|E | = (sum of degrees)/2 or
2|E | = (sum of degrees) = (5·3) = 15.

Impossible as 15 is not even. In general:

5/11/2024 38
Handshaking Theorem
Lemma: The number of vertices of odd degree must be even
in an undirected graph.

Proof : Otherwise would have


2|E | = Sum of even no.’s
+ an odd number of odd no.’s
 even = even + odd
–this is impossible.

5/11/2024 39
Exercises:
1. How many edges are there in a graph with ten
vertices each of degree six?

2. How many edges does a graph have if it has vertices


of degree 4, 3, 3, 2, 2? Draw such a graph.

3. Determine whether each of these sequences is


graphic. For those that are, draw a graph having the
given degree sequence.
a) 5, 4, 3, 2, 1, 0 b) 6, 5, 4, 3, 2, 1
c) 2, 2, 2, 2, 2, 2 d) 3, 3, 3, 2, 2, 2
e) 3, 3, 2, 2, 2, 2 f) 1, 1, 1, 1, 1, 1
g) 5, 3, 3, 3, 3, 3 h) 5, 5, 4, 3, 2, 1
5/11/2024 40
Some Special Simple Graph

• We will now introduce several classes of


simple graphs. These graphs are often used as
examples and arise in many applications.

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.

• The graphs Kn, for n = 1,2,3,4,5,6 are displayed in the


following figure.

K1 K2 K3 K4 K5

5/11/2024 42
Cycles graph - Cn

The cycle graph Cn , is a circular graph consists of n vertices v1,


v2, …., vn and edges {v1, v2, }, {v2, v3, }, …., {vn-1, vn}, and
{vn, v1, }. They look like polygons:

C1 C2 C3 C4 C5

Q: What type of graph are C1 and C2 ?


A: Pseudographs

5/11/2024 43
Wheels Graph- Wn

The wheel graph Wn is just a cycle graph with an extra


vertex in the middle:

W1 W2 W3 W4 W5

Usually consider wheels with 3 or more spokes only.

5/11/2024 44
Cubes graphs - 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)
5/11/2024 45
Bipartite Graphs

• A simple graph is bipartite if V can be partitioned into V =


V1  V2 so that any two adjacent vertices are in different
parts of the partition. Another way of expressing the same
idea is bichromatic : vertices can be colored using two
colors so that no two vertices of the same color are
adjacent.

5/11/2024 46
Bipartite Graphs

EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

5/11/2024 47
Bipartite Graphs

EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

5/11/2024 48
Bipartite Graphs

EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

5/11/2024 49
Bipartite Graphs

EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

5/11/2024 50
Bipartite Graphs

EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

5/11/2024 51
Bipartite Graphs

EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

5/11/2024 52
Bipartite Graphs

EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

5/11/2024 53
Bipartite Graphs

EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

5/11/2024 54
Bipartite Graphs

EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

5/11/2024 55
Bipartite Graphs

EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

5/11/2024 56
Bipartite Graphs

EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

5/11/2024 57
Bipartite Graphs
EG: C4 is a bichromatic:

And so is bipartite, if we redraw it:

Q: For which n is Cn bipartite?

5/11/2024 58
Q: Determine whether the graph is bipartite

• (a) (b)

5/11/2024 59
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 .

5/11/2024 60
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.

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.

Local Area Network


Interconnection Networks for Parallel
Computation
Parallel Processing

5/11/2024 62
Representing Graphs and Graphs Isomorphism

• There are many useful ways to represent graphs. In this


section we will show how to represent graphs in several
different ways.

• One way to represent a graph without multiple edges is to


list all edges of this graph.

• Another way to represent a graph with no multiple edges


is to use adjacent lists, which specify the vertices that are
adjacent to each vertices of the graph.

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

Since digraphs are relations on their vertex sets, can adopt


the concept to represent digraphs. In the context of
graphs, we call the representation an adjacency matrix
:

For a digraph G = (V,E ) define matrix AG by:

 Rows, Columns –one for each vertex in V

 Value at i th row and j th column is

– 1 if i th vertex connects to j th vertex (i  j )

– 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:

For a directed multigraph G = (V,E ) define the matrix AG by:

• Rows, Columns –one for each vertex in V

• Value at i th row and j th column is

• The number of edges with source the i th vertex and


target the j th vertex

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

Undirected graphs can be viewed as directed graphs by turning


each undirected edge into two oppositely oriented directed
edges, except when the edge is a self-loop in which case only
1 directed edge is introduced.
EG:

1 2 1 2

3 4 3 4

5/11/2024 69
Adjacency Matrix

Q: What’s the 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

For an undirected graph G = (V,E ) define the matrix AG by:

• Rows, Columns –one for each element of V

• Value at i th row and j th column is the number of


edges incident with vertices i and j.

This is equivalent to converting first to a directed graph as


above. Or by allowing undirected edges to take us from i
to j can simply use definition for directed graphs.

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

1 when edges is incident w ith vi


mij  
0 otherwise

Example : see the book

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:

• Equivalence for sets is bijectivity:

 EG { , ,, }  {12, 23, 43}

• Equivalence for graphs is isomorphism:


 EG

5/11/2024 77
Graph Isomorphism

Intuitively, two graphs are isomorphic if can bend, stretch


and reposition vertices of the first graph, until the second
graph is formed. Etymologically, isomorphic means “same
shape”.

EG: Can twist or relabel:

to obtain:

5/11/2024 78
Graph Isomorphism
Undirected Graphs
DEF: Suppose G1 = (V1, E1 ) and G2 = (V2, E2 ) are pseudographs.
Let f :V1V2 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.

Then f is called an isomorphism and G1 is said to be


isomorphic to G2.

5/11/2024 79
Graph Isomorphism
Digraphs
DEF: Suppose G1 = (V1, E1 ) and G2 = (V2, E2 ) are directed
multigraphs. Let f :V1V2 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.

Then f is called an isomorphism and G1 is said to be


isomorphic to G2.

Note: Only difference between two definitions is the italicized


“from” in no. 2 (was “between”).
5/11/2024 80
Graph Isomorphism
-Example
EG: Prove that

is isomorphic to .

First label the vertices:

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

You might also like