0% found this document useful (0 votes)
30 views62 pages

Graphs and Relations Ann

Uploaded by

Joy Song
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)
30 views62 pages

Graphs and Relations Ann

Uploaded by

Joy Song
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/ 62

CS2800

Graphs and Relations

c Renee Mirka & Anke van Zuylen


Spring 2023

1
Graphs

2
Graphs

A graph consists of vertices (aka nodes) and edges (aka links, arcs)
between vertices.

A directed graph (digraph) is one where the edges have a direction,


usually denoted with an arrow; and undirected graph is one where the
edges do not have a direction.

1 2 1 2

4 3 4 3

3
Graph notation

A graph G is a pair (V , E ), where V is the set of vertices and E is the


set of edges.

• We write G = (V , E ) so we can use G to refer to the entire graph,


and V and E to refer to just its vertices or just its edges.
• If G is introduced without introducing (V , E ), we can also write
V (G ) and E (G ) to indicate the vertices and edges in G .

4
Edge notation

To denote an edge between vertices u and v (where u and v are two, not
necessarily distinct, vertices), we use di↵erent notation depending on the
type of graph:

• if G is a digraph, then we denote the edge by an ordered pair (u, v ).


• If G is an undirected graph, then we denote the edge by an
unordered pair {u, v }.
• If we don’t want to specify whether an edge is directed or
undirected, we’ll use notation uv .

Although it is possible to have a graph that has both directed and


undirected edges, this is not common.
In addition, it is common to explicitly mention it if a graph is allowed to
have self-loops (edges of the form vv ) or duplicate edges.

5
Graph notation examples

Describe these two graphs using the notation from the previous two
slides.

1 2 1 2

4 3 4 3

6
Some more graph terminology

In an undirected graph:

• Two vertices in a graph are said to be adjacent i↵ they are the


endpoints of the same edge, and an edge is said to be incident to
each of its endpoints.
• The number of edges incident to a vertex v is called the degree of
the vertex, and denoted deg(v ).
• We say u is a neighbor of v if u and v are adjacent.

In a directed graph:

• For a directed edge (u, v ), we call u the tail of the edge, and we call
v the head.
• In a digraph, the in-degree (deg (v )) of a vertex v is the number of
edges of which v is the head, and the out-degree (deg+ (v )) of v is
the number of edges of which v is the tail.

7
Degree examples

Write down the degree of all vertices.

1 2 1 2

4 3 4 3

8
Edges and degrees

(Handshake Lemma) Let G = (V , E ) be an undirected graph. Then


X
2|E | = deg (v ).
v 2V

(Note that this applies even if duplicate edges and loops are present.)

9
Edges and degrees

How about for directed graphs? Describe the relation between the
following quantities:

X X
|E | deg (v ) deg+ (v )
v 2V v 2V

10
Graph applications

Graphs come up everywhere.

• We can view the internet as a graph (in many ways);


• niche graphs in ecology view the species as vertices which are
connected by an edge if they compete (use the same food resources,
etc.);
• contact tracing sars-cov-2 infections can be viewed as finding
“neighborhoods” in a graph;
• finding kidney exchanges has been defined as finding cycles in a
directed graph;
• ...

11
Connectivity

12
More graph terminology: paths and circuits/cycles

From now on, we’ll assume graphs are simple, that is, they do not have
duplicate edges.

A path in a graph G is a sequence (ordered list) of vertices v0 , v1 , . . . , vk


with the property that vi vi+1 2 E for i = 0, . . . , k 1.

• We say the path v0 , v1 , . . . , vk goes from v0 to vk .


• The number of edges on the path v0 , v1 , . . . , vk is k and this is
called the length of the path.
• Note that the number of vertices in a path is 1 more than the
number of edges.
• A path v0 , v1 , . . . , vk is called simple if all vertices except possibly v0
and vk are distinct.
• A path v0 , v1 , . . . , vk is called a circuit or cycle if v0 = vk .
• A simple circuit is also called a cycle.
13
Paths and circuits example

• In the graph on the left, is there a path from vertex 1 to vertex 3?


What is its length?
• In the graph on the right, is there a path of length three
• from vertex 1 to vertex 4?
• from vertex 4 to vertex 1?
• from vertex 1 to vertex 2?
• Give a cyle of length 2 in the graph on the right.
• Question: Is it possible to have a path of length 0?

1 2 1 2

4 3 4 3
14
Warning

Di↵erent authors use di↵erent terminology and notation. For example,


you might see what we call a path be referred to as a walk, and you
might see authors writing down the sequence of edges instead of vertices,
or an alternating sequence of vertices and edges to denote a path.

15
Connectivity in social networks

16
Some special graphs

17
Special graphs

There are many special graphs that have many applications in computer
science, such as:

• complete graphs (simple, undirected graph, where all possible edges


are present);
• hypercube (see book);
• bipartite graphs (discussed next);
• trees (also discussed next);
• and many more...

18
Trees

A simple undirected graph is called a tree if

• it is connected (this means that there must be a path between every


pair of vertices), and
• it has no cycles.

19
Not a tree

20
Bipartite graphs

A simple graph G is called bipartite if its vertex set 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 in V2 (so that no edge in G
connects either two vertices in V1 or two vertices in V2 ).

When this condition holds, we call the pair (V1 , V2 ) a bipartition of the
vertex set V of G .

21
Bipartite graph example

Are the following graphs bipartite?

1 2

4 3

22
Some useful facts

Theorem 1: A tree on n vertices has n 1 edges.

23
Some useful facts

Theorem 2: A graph is bipartite if and only if it doesn’t have odd cycles.

24
Binary relations

25
Binary relations

Recall the cartesian product of sets S and T :

S ⇥ T = {(s, t) : s 2 S, t 2 T }.

A (binary) relation on S and T is a subset R of S ⇥ T .

• Notation: (s, t) 2 R or s R t
• Example: Let S = {a, b, c}, T = {1, 2} and
R = {(a, 1), (a, 2), (b, 1), (c, 2)}.
• Representation as a bipartite graph and as a matrix:

26
Binary relations and functions

Recall that a function from S to T was formally defined as a subset f of


S ⇥ T such that for each s 2 S, there is a unique t 2 T such that
(s, t) 2 f .
In other words, if f is a function from S to T then f is is a binary
relation on S and T (but the converse is not true)!

27
Binary relations on a single set

A binary relation on a single set A is a subset of A ⇥ A.


From now on, we will restrict attention to such binary relations.

Examples:

• < relation on R
• “is a family member of” relation on people
•  relation on the cardinalities of infinite sets
• “bought the same product as” relation on Amazon customers
• ✓ relation on sets
• ...

28
Properties of relations

A relation R on A is

• reflexive if 8x (xRx)
• symmetric if 8x8y (xRy ) yRx).
• transitive if 8x8y 8z (xRy ^ yRz ) xRz).

Examples:
Set A reals Amazon customers
Relation R <  “bought same product” “lives on same address’
reflexive
symmetric
transitive

29
Properties of the ⇢ relation

Question: The “strict-subset” relation ⇢ is defined as follows:


X ⇢ Y is true i↵ X ✓ Y and X 6= Y . Select which of the following is
true (choose one):

(a) ⇢ is reflexive.
(b) ⇢ is symmetric.
(c) ⇢ is transitive.

30
One more property

A relation R on A is anti-symmetric if 8x8y (xRy ^ yRx ) x = y ).

Examples:
Set A integers sets
Relation R <  “has same parity” ✓
antisymmetric

31
Properties of binary relations

Question: Choose the statement that is true:

(a) If R is anti-symmetric, then R must be reflexive.


(b) If R is not symmetric, then R is anti-symmetric.
(c) If R is transitive and not anti-symmetric, then R must be reflexive.
(d) None of the above.

32
Inverting relations

For functions, we defined the inverse f 1 (which exists only if f is


bijective).
A binary relation always has an inverse.
If R is a relation on A, then R 1 is the relation on A satisfying:
1
8x, y xRy , yR x .

Reminder: We can also write this as


1
8x, y (x, y ) 2 R , (y , x) 2 R .

33
Example: inverting a relation

Example: Let A = {1, 2, 3, 4} and R = {(1, 1), (1, 2), (2, 1), (3, 4)}.
1
• Find R .
• Is R anti-symmetric?

34
Exercise 1: revisiting anti-symmetry

The identity (or diagonal) relation on a set A is the set


{(a, a) : a 2 A} (in other words, a b holds if and only if b = a)
Express the definition for R being anti-symmetric using only the following
symbols:
1
• R and R

• [, \, \, ⇢, ✓, =

and prove your new definition is equivalent to the original definition.

35
51
Composition and powers of relations

For functions, we defined composition f g . The same definition applies


for composing binary relations.
Using the definition of composition, we see that R R is the relation
satisfying:

8x8y ((x, y ) 2 R R, ).

36
Composition and powers of relations

The powers R n of R, for n = 1, 2, 3, . . ., are defined recursively as

• R 1 = R,
• R n+1 = R n R for n 1.

37
Example: composition and powers of relations

Example: Let A = {1, 2, 3, 4} and R = {(1, 1), (1, 2), (2, 3), (3, 4)}.

• Find R n for n = 1, 2, 3, . . .
• Is R transitive?

38
Exercise 2: revisiting transitivity

Express the definition for R being transitive using only the following
symbols:

• R, R R, R n (for n = 1, 2, 3, . . .)
• [, \, \, ⇢, ✓, =

and prove your new definition is equivalent to the original definition.

39
51
Reviewing binary relations using
their graph representation

40
Graph representation of a binary relation

A binary relation R on a set A can be represented as a digraph G with


vertex set A and directed edge set R.
For example, if A = {1, 2, 3, 4} and
R = {(1, 2), (2, 2), (2, 3), (3, 2), (1, 3), (4, 3)}, then the following graph
represents R:

Note: We can use a graph to represent a binary relation R on A only if


|A| is finite but binary relations also exist on infinite sets.

41
Binary relations and their graph representation

Question:
Let R be represented by the graph above. Which of the following
properties does R have?

(a) reflexive (8x xRx)


(b) symmetric (8x8y (xRy ) yRx))
(c) antisymmetric (8x8y (xRy ^ yRx ) x = y ))
(d) transitive (8x8y 8z (xRy ^ yRz ) xRz))

42
Composing binary relations

Question: Let R be represented by the following graph.

Which of the following is not in R R?

(a) (1,3)
(b) (1,2)
(c) (4,3)

43
Composition and powers of relations

Finish the following theorem. How would you prove it?

Theorem Let digraph G represent a binary relation R on A. Then


relation R n contains (a, b) if and only if G has the property that ...
.

44
Transitivity

Remember that in Exercise 2 we gave the following alternative


(equivalent) definition for a binary relation R to be transitive:

R R ✓ R.

Can you rephrase this requirement as a requirement on the graph


representing R?

45
The transitive closure

46
Transitive closure of a binary relation

Definition: The transitive closure of a relation R on A is the relation


R trans on A such that

(i) R ✓ R trans ,
(ii) R trans is transitive,
(iii) For any transitive relation R 0 on A that contains R, it holds that
R trans ✓ R 0 .

Note that that A ⇥ A always satisfies the two properties! But if (iii)
doesn’t hold, then there is a smaller relation that satisfies them.
In other words, we want the smallest relation that contains all of R, and
that is transitive.

47
Example of transitive closure

Question: Suppose A = {1, 2, 3, 4} and R = {(1, 2), (2, 3), (3, 4)}.
What is R trans ?

(a) R trans = {(1, 3), (2, 4)}


(b) R trans = {1, 2, 3, 4} ⇥ {1, 2, 3, 4}
(c) R trans = R [ {(1, 3), (2, 4), (1, 4)}
(d) R trans = R [ {(1, 3), (2, 4)}

48
The connectivity relation

Given graph representing a relation R, the connectivity relation R ⇤


consists of the pairs (a, b) such that there is a path of length at least one
from a to b.
Express R ⇤ in terms of the powers of R.

49
Transitive closure and the connectivity relation

Theorem: The transitive closure equals the connectivity relation of R.


Proof:

50
51
Equivalence Relations

52
Equivalence relations

A relation R is an equivalence relation if it is reflexive, symmetric, and


transitive.
Examples: Verify that the following relations are equivalence relations:

• = on N
• ⇠P on N, where x ⇠P y i↵ x y is even.
• =1 on N ⇥ N, where (x1 , y1 ) =1 (x2 , y2 ) i↵ x1 = x2 .

53
Equivalence class

If R is an equivalence relation, the equivalence class of a is denoted [a],


and is defined as [a] = {b : aRb}
Examples: What are the equivalence classes of the two equivalence
relations given on the previous slide?

54
Partition

Definition: Let A be a set. A partition of A is a collection A1 , . . . , Ak of


subsets of A such that every element of A is in exactly one of
A1 , . . . , Ak .
In other words, A1 , . . . , Ak ✓ A form a partition of A if

• the subsets A1 , . . . , Ak are disjoint,


Sk
• i=1 Ai = A.

Examples:

• The equivalence classes of the parity relation ⇠P give a partition of


the positive integers into {odd positive integers} and {even positive
integers}.
• The equivalence classes of the = relation give a partition of the
positive integers into .......

55
Equivalence classes

Theorem 1: The equivalence classes of an equivalence relation on A


form a partition of A.
In other words, for any two elements a, b of A, either their equivalence
class are the same (so [a] = [b]) or their equivalence classes are disjoint
(so [a] \ [b] = ;).
Proof:

56
57
Partial orders

58
Partial orders

A relation is (weak or non-strict) partial order if it is reflexive, transitive,


and antisymmetric.
Examples:

•  and are (weak) partial orders on numbers.


• ✓ is a (weak) partial order on sets.

59
The graph for a partial order

Question: What can we say about the graph representing a partial


order?

(a) Every edge is bidirected.


(b) There is an edge (in one or both possible directions) between every
pair of vertices.
(c) It has no self-loops.
(d) It has no cycles except for self-loops.

60

You might also like