Lecture 9 - Graph and Trees - Updated PDF
Lecture 9 - Graph and Trees - Updated PDF
STRUCTURES
Computer Sciences
Department
Lecture 9: Graphs and Trees: 9.1- 9.4 + 10.1
Lecture Contents
2
Types of Graphs
Graph Terminology
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)
Simple
Multigraphs Pseudographs
Graphs
Detroit
New York
San Francisco
Chicago
Denver Washington
Los Angeles
Detroit
Washington
Los Angeles
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
Denver Washington
Los Angeles
Detroit
New York
Chicago
San Francisco
Denver Washington
Los Angeles
A B
20-Nov-21 Computer Sciences Department
Solution
15
directed or
Graph multiple edges Loops Type
undirected
Multiple
Type Edges Loops
Edges
Simple Graph Undirected NO NO
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
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
(u ,v)
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)
deg v deg v E
vV
vV
|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
Cycles: C3 C4 C5 C6
Wheels: W3 W4 W5 W6
20-Nov-21 Computer Sciences Department
Bipartite Graph
32
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
A B
K5 C5
Is C5 a subgraph of K5? …...
20-Nov-21 Computer Sciences Department
Union
38
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
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
vertices
20-Nov-21 Computer Sciences Department
44 Connectivity
– Is it simple?
u1 u2
–
– Is it simple?
u1 u2 –
any circuits
a,e,b,c,b.
e,b,a,d,b,e.
c,b,d,a,e,c.
c e
f d c
d f
g e
…... …...
20-Nov-21 Computer Sciences Department
Exercise
53
…………
b d e f
a c h g
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
• Properties:
– There is a unique simple path between any 2 of its
vertices
– No loops
– No multiple edges
• 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