0% found this document useful (0 votes)
43 views69 pages

Lecture 9 - Graph and Trees - Updated PDF

The document discusses graphs and trees. It defines types of graphs like undirected graphs, directed graphs, simple graphs, multigraphs and pseudographs. It also defines graph terminology like adjacent vertices, degree of a vertex, in-degree and out-degree of vertices in directed graphs. The document provides examples of different types of graphs and explains concepts like precedence graphs.

Uploaded by

Migi Alucrad
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)
43 views69 pages

Lecture 9 - Graph and Trees - Updated PDF

The document discusses graphs and trees. It defines types of graphs like undirected graphs, directed graphs, simple graphs, multigraphs and pseudographs. It also defines graph terminology like adjacent vertices, degree of a vertex, in-degree and out-degree of vertices in directed graphs. The document provides examples of different types of graphs and explains concepts like precedence graphs.

Uploaded by

Migi Alucrad
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/ 69

CS100: DISCRETE

STRUCTURES
Computer Sciences
Department
Lecture 9: Graphs and Trees: 9.1- 9.4 + 10.1
Lecture Contents
2

 Types of Graphs
 Graph Terminology

 Special Types of Graphs

20-Nov-21 Computer Sciences Department


Types of Graphs
3

Simple Graphs

undirected
Graphs Multigraphs

Pseudographs
Types of Graphs

Simple Directed
Graphs
directed Graphs
Directed
Multigraphs
20-Nov-21 Computer Sciences Department
Undirected Graph
4

 A graph G = ( V , E ) consists of
 V, a nonempty set of vertices (nodes)
 E, a set of edges (unordered pairs of
distinct elements of V)

20-Nov-21 Computer Sciences Department


Types of undirected Graphs
5

Simple
Multigraphs Pseudographs
Graphs

Each edge Graphs that may


connects two have multiple Graphs that may
different vertices edges connecting include loops
the same vertices

No two edges Possibly multiple


connect the same edges connecting
pair of vertices the same pair of
vertices

20-Nov-21 Computer Sciences Department


Simple Graph Example
6

Detroit
New York
San Francisco

Chicago
Denver Washington
Los Angeles

o This simple graph represents a network that


contains computers and telephone links between
computers

20-Nov-21 Computer Sciences Department


Multigraph Example
7

o There can be multiple telephone lines between


two computers in the network.
Detroit
New York
San Francisco
Chicago
Denver Washington
Los Angeles

20-Nov-21 Computer Sciences Department


Pseudograph Example
8

Detroit

San Francisco Chicago New York


Denver

Washington

Los Angeles

o There can be telephone lines in the network from


20-Nov-21 Computer Sciences Department
a computer to itself.
Directed Graph
9

 A directed graph (digraph) ( V , E ) consists of


 V, a nonempty set of vertices
 E, a set of directed edges (arcs)
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.

20-Nov-21 Computer Sciences Department


Types of directed Graphs
10

Simple Directed
Directed Graph Multigraph

Directed graphs
that may have
No loops multiple directed
edges from a
vertex to a second
(possibly the same)

No multiple
directed edges

20-Nov-21 Computer Sciences Department


Simple Directed Graph Example
11

 The edges are ordered pairs of (not necessarily


distinct) vertices.
Detroit
Chicago New York
San Francisco

Denver Washington

Los Angeles

Some telephone lines in the network may operate in only


one direction. Those that operate in two directions are
represented
20-Nov-21 by pairs of edges in opposite directions.
Computer Sciences Department
Directed Multigraph Example
12

Detroit
New York
Chicago
San Francisco

Denver Washington

Los Angeles

There may be several one-way lines in the same direction


from one computer to another in the network.
20-Nov-21 Computer Sciences Department
Graph Model Structure
13

 Three key questions can help us understand the


structure of a graph:
1. Are the edges of the graph undirected or directed (or
both)?
2. If the graph is undirected, are the multiple edges
present that connect the same pair of vertices?
If the graph is directed, are multiple directed edges
present?
3. Are the loops present?

20-Nov-21 Computer Sciences Department


Exercise
14

 Determine whether the graphs shown below have


 directed or undirected edges,
 multiple edges,

 one or more loops

A B
20-Nov-21 Computer Sciences Department
Solution
15

directed or
Graph multiple edges Loops Type
undirected

A undirected multi-graph no loops Multigraph

directed directed multi- Directed


B two loops
graph Multigraph

20-Nov-21 Computer Sciences Department


Summary
16

Multiple
Type Edges Loops
Edges
Simple Graph Undirected NO NO

Multigraph Undirected NO YES

Pseudograph Undirected YES YES


Simple Directed
Directed NO NO
Graph
Directed Multigraph
20-Nov-21
Directed YESComputer Sciences
YESDepartment
Precedence Graphs
17

In concurrent
processing,
some statements
must be
executed before
other statements.
A precedence
graph represents
these
relationships.
20-Nov-21 Computer Sciences Department
18 Graph Terminology

20-Nov-21 Computer Sciences Department


Adjacent Vertices in Undirected
19
Graphs
 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-Nov-21 Computer Sciences Department


Degree of a Vertex
20

 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 a vertex v is denoted by
deg(v)

20-Nov-21 Computer Sciences Department


Example
21

c d
b

a g f e
 Find the degrees of all the vertices.
Solution:
deg(a)= , deg(b)= , deg(c)= , deg(d)= ,
deg(e)= , deg(f)= , deg(g)= .
20-Nov-21 Computer Sciences Department
Adjacent Vertices in Directed Graphs
22

 When (u,v) is an edge of a directed graph G,


 u is said to be adjacent to (Neighbor) v .
 v is said to be adjacent from u .

(u ,v)

initial vertex terminal vertex

20-Nov-21 Computer Sciences Department


Degree of a Vertex
23

 In-degree of a vertex v
 Thenumber of vertices adjacent to v (the number of
edges with v as their terminal vertex)
 Denoted by deg(v)

 Out-degree of a vertex v
 The number of vertices adjacent from v (the number
of edges with v as their initial vertex)
 Denoted by deg+(v)

 A loop at a vertex contributes 1 to both the in-


degree and out-degree.
20-Nov-21 Computer Sciences Department
Degree of a Vertex : Example
24

• Find the in-degrees and out-degrees of this digraph.


20-Nov-21 Computer Sciences Department
Degree of a Vertex : Example
25

In-degrees: deg-(a) = 2, deg-(b) = 2, deg-(c) = 3,


deg-(d) = 2, deg-(e) = 3, deg-(f) = 0
Out-degrees: deg+(a) = 4, deg+(b) = 1, deg+(c) = 2,
deg+(d) = 2, deg+(e) = 3, deg+(f) = 0
20-Nov-21 Computer Sciences Department
Theorem
26

 Let G = (V, E) be a graph with directed


edges. Then:

 deg v    deg v   E
vV

vV

The sum of the in-degrees of all vertices in a digraph


= the sum of the out-degrees = the number of edges.
(an edge is in for a vertex and out for the other)
20-Nov-21 Computer Sciences Department
Exercise
27

 For following graph, determine:


 The number of vertices and
edges.
 The in-degree and out-degree of
each vertex for the given directed
multi-graph.
 The sum of the in-degrees of the
vertices and the sum of the out-
degrees of the vertices directly.
Show that they are both equal to
the number of edges in the graph.
20-Nov-21 Computer Sciences Department
Solution
28

 |V|= 4, |E|= 8.
 The degrees are : deg−(a) =2, deg+(a) = 2,
deg−(b) = 3, deg+(b) = 4, deg−(c) = 2,
deg+(c) = 1, deg−(d) = 1,and deg+(d) = 1.
 The sum of the in-degrees of the vertices =8, The
sum of the out-degrees of the vertices =8, |E|=
The sum of the in/out-degrees of the vertices = 8

20-Nov-21 Computer Sciences Department


COMPLETE GRAPH
29

 The complete graph on n vertices (Kn) is the


simple graph that contains exactly one edge
between each pair of distinct vertices.
 The figures above represent the complete
graphs, Kn , for n = 1, 2, 3, 4, 5, and 6.
20-Nov-21 Computer Sciences Department
CYCLE
30

 The cycle Cn (n  3), consists of n vertices


v1, v2, …, vn and edges {v1,v2}, {v2,v3}, …,
{vn-1,vn}, and {vn,v1}.

Cycles: C3 C4 C5 C6

20-Nov-21 Computer Sciences Department


WHEEL
31

 When a new vertex is added to a cycle Cn and


this new vertex is connected to each of the n
vertices in Cn, we obtain a wheel Wn.

Wheels: W3 W4 W5 W6
20-Nov-21 Computer Sciences Department
Bipartite Graph
32

 A simple graph is called bipartite if its vertex set


V can be partitioned into two disjoint nonempty
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).

a b a
b
c d d
c
e
20-Nov-21 e Computer Sciences Department
Bipartite Graph : Example 1
33
1 2

Is C6 Bipartite?
6 3

Yes. Why? 5 4
Because:
 its vertex set can be partitioned into the two sets
V1 = {v1, v3, v5} and V2 = {v2, v4, v6}
 every edge of C6 connects a vertex in V1 with a
vertex in V2
20-Nov-21 Computer Sciences Department
Bipartite Graph : Example 2
34 1

Is K3 Bipartite?
No. Why not?
2 3
Because:
 Each vertex in K3 is connected to every other
vertex by an edge
 If we divide the vertex set of K3 into two disjoint
sets, one set must contain two vertices
 These two vertices are connected by an edge
 20-Nov-21
But this can’t be the case if the graph isSciences
Computer bipartite
Department
Exercise
35

 Determine whether the graphs shown below are


bipartite. Justify your answer.

A B

20-Nov-21 Computer Sciences Department


Solution
36

A. The graph is bipartite, its vertex set can be


partitioned into two sets V1= { a, c } and V2 = {
b, d, e }.
B. The graph is bipartite, its vertex set can be
partitioned into two sets V1= { c, f } and V2 = { a,
b, d, e }.

20-Nov-21 Computer Sciences Department


Subgraph
37

 A subgraph of a graph G = (V,E) is a graph


H = (W,F) where W  V and F  E.

K5 C5
Is C5 a subgraph of K5? …...
20-Nov-21 Computer Sciences Department
Union
38

 The union of 2 simple graphs G1 = (V1, E1)


and G2 = (V2, E2) is the simple graph with
vertex set V = V1V2 and edge set E =
E1E2. The union is denoted by G1G2.
c c c

b f d
b d b d
f
a e
a e a e
C5 W5
S5
20-Nov-21 S5  C5 = W5 Computer Sciences Department
39 Representing Graphs

20-Nov-21 Computer Sciences Department


Adjacency Matrix
40

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


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

1 if { vi ,v j } is an edge in G
aij  
0 otherwise
20-Nov-21 Computer Sciences Department
Adjacency Matrix Example
41

c
To
a b c d e f
b d From
f a 0 1 0 0 1 1
b 1 0 1 0 0 1
a e c 0 1 0 1 0 1
d 0 0 1 0 1 1
W5 e 1 0 0 1 0 1
f 1 1 1 1 1 0
{v1,v2}
row column
20-Nov-21 Computer Sciences Department
Incidence Matrix
42

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


Suppose v1,v2,v3,…,vn are the vertices and
e1,e2,e3,…,em are the edges of G.
 The incidence matrix with respect to this

ordering of V and E is the nm matrix M =


[mij], where
1 if edge e j is incident with vi
mij  
0
20-Nov-21 otherwise Computer Sciences Department
Incidence Matrix Example
43

 Represent the graph shown with an incidence


matrix.
e1 e2 e3 e4 e5 e6 edges
v1 v2 v3
e6 v1 1 1 0 0 0 0
e3 v2 0 0 1 1 0 1
e1 e4 v3
e5 0 0 0 0 1 1
e2 v4 1 0 1 0 0 0
v4 v5 v5 0 1 0 1 1 0

vertices
20-Nov-21 Computer Sciences Department
44 Connectivity

20-Nov-21 Computer Sciences Department


Paths in Undirected Graphs
45

• There is a path from vertex v0 to vertex vn if


there is a sequence of edges from v0 to vn
 This path is labeled as v0,v1,v2,…,vn and has
a length of m.
• The path is a circuit if the path begins and ends
with the same vertex.
• A path is simple if it does not contain the same
edge more than once.
20-Nov-21 Computer Sciences Department
Paths in Undirected Graphs Cont.
46

 A path or circuit is said to pass through the


vertices v0, v1, v2, …, vn or traverse the edges
e1, e2, …, en.

20-Nov-21 Computer Sciences Department


Example 1
47

 u1, u4, u2, u3


u1 u2
– Is it simple?

– What is the length?


u5 u4 u3

– Does it have any circuits?


20-Nov-21 – Computer Sciences Department
Example 2
48

 u1, u5, u4, u1, u2, u3

– Is it simple?
u1 u2

u5 u3 – What is the length?


u4

– Does it have any circuits?



20-Nov-21 Computer Sciences Department
Example 3
49

 u1, u2, u5, u4, u3

– Is it simple?
u1 u2 –

u5 u3 – What is the length?


u4 –

– Does it have any circuits?


20-Nov-21
– Computer Sciences Department
Exercise
50

 Given the following graph and list of vertices, for


each list determine if :
 It forms a path, if yes give the length
 Is simple

 any circuits

list of vertices forms a path length simple circuits

a,e,b,c,b.

e,b,a,d,b,e.

c,b,d,a,e,c.

20-Nov-21 Computer Sciences Department


Connectedness
51

 An undirected graph is called connected if


there is a path between every pair of distinct
vertices of the graph.
 Theorem
There is a simple path between every pair of distinct
vertices of a connected undirected graph.

20-Nov-21 Computer Sciences Department


Example
52

Are the following graphs connected?


a b a b

c e

f d c

d f
g e

…... …...
20-Nov-21 Computer Sciences Department
Exercise
53

 Determine whether the given graphs are connected.

…………

20-Nov-21 Computer Sciences Department


Connectedness (Cont.)
54

 A graph that is not connected if the union


of two or more disjoint connected
subgraphs called the connected
components of the graph.

20-Nov-21 Computer Sciences Department


Example
55

 What are the connected components of


the following graph?

b d e f

a c h g

20-Nov-21 Computer Sciences Department


Example Cont.
56

 What are the connected components of


the following graph?

b d e f

a c h g

20-Nov-21
{a, b, c}, {d, e}, {f, g, h}
Computer Sciences Department
Connectedness in Directed Graphs
57

 A directed graph is strongly connected if


there is a directed path between every
pair of vertices.
 A directed graph is weakly connected if
there is a path between every pair of
vertices in the underlying undirected
graph.
20-Nov-21 Computer Sciences Department
Example 1
58

 Is the following graph strongly


connected? Is it weakly connected?
a b This graph is strongly
connected. Why? Because
there is a directed path
c
between every pair of
vertices.
e d If a directed graph is strongly
connected, then it must also
20-Nov-21 be weakly connected.
Computer Sciences Department
Example 2
59

 Is the following graph strongly


connected? Is it weakly connected?
a b
This graph is not strongly
connected. Why not?
c Because there is no directed path
between a and b, a and e, etc.
However, it is weakly connected.
e d
(Imagine this graph as an
undirected graph.)
20-Nov-21 Computer Sciences Department
Exercise
60

 Determine whether each of these graphs is strongly


connected and if not, whether it is weakly connected.

weakly connected weakly connected

20-Nov-21 Computer Sciences Department


61 Trees

20-Nov-21 Computer Sciences Department


What is a tree?
62

• A tree is a connected simple undirected graph


with no simple circuits

• Properties:
– There is a unique simple path between any 2 of its
vertices
– No loops
– No multiple edges

20-Nov-21 Computer Sciences Department


Example 1
63

This graph is a tree because it is a connected


graph with no simple circuits

20-Nov-21 Computer Sciences Department


Example 2
64

• This graph is not a tree because there is a


cycle a, b, e, d, a

20-Nov-21 Computer Sciences Department


Rooted (Directed) Trees
65

• A rooted tree is a tree in which one node has


been designated the root and every and edge
is directed away from the root.

• You should know the following terms about


rooted trees:
– Root, Parent, child, siblings, ancestors,
descendents, leaf, internal node, subtree

20-Nov-21 Computer Sciences Department


Terminology
• Root: a vertex with indegree
zero [node a is parent]
• Parent: Vertex u is a parent
such that there is a directed
edge from vertex u to vertex
v [b is a parent of f and g]
• Child: If vertex u is a parent
of vertex v then vertex v is
the child of vertex u [g and f
are children of b]

66 20-Nov-21 Computer Sciences Department


Terminology
67

• Siblings: Vertices with the


same parents are siblings [ f
and g ]
• Ancestors: Vertices in the
path from the root to vertex
v, excluding v itself and
including the root are the
ancestors of vertex v [a and b
are the ancestors of g]

20-Nov-21 Computer Sciences Department


68

20-Nov-21 Computer Sciences Department


Terminology
69

• Subtree: Subtree is a
subgraph consisting of v and
its descendents and their
incident edges [subtree
rooted at b]
• Level (of a vertex v) is the
length of unique path from
root to v [level of root = 0,
level of b = 1, level of g =2]
• Height is the maximum of
vertices levels. [Height = 3]
20-Nov-21 Computer Sciences Department

You might also like