Graphs
Graphs
What is a graph?
E= {(a,b),(b,c),(d,e)}.
Graphs can be complete, sparse, dense, or none of
these. Sparse matrices contain few edges and dense
ones contain many.
B C
P
Y Q
X
A
■ A,B,C etc. are vertices(nodes) Z D
■ (A,X), (X,Y) etc. are edges
■ P,Q,Z is a cycle
■ Degree of a node (vertex) is the number of edges incident
on a vertex.
– Degree Y =3, degree C=1
v1 e4 v4
e1
e3 v1
e1 e3 v2
v5
e5
v2 e2 v3 e4 e2
e7
A v3 e6 v4
B
Directed Graph
b
d in-degree = 2
c out-degree = 2
V = {a,b,c,d,e}
E = { (b,a), (c,b),
(d,c), (e,d), (e,b),
(b,e), (e,a) }
Adjacent vertices: Two vertices are called
adjacent if they are connected by an edge. If
there is an edge (u,v), then we can say vertex u
is adjacent to vertex v and vertex v is adjacent
to vertex u.
Incidence edge: The relationship between an
edge and its endpoints.
Adjacent edges: Two non parallel edges are
said to be adjacent if they are incident on a
common vertex.
a
e
b
d
c
1. Edge list: {{a,b}, {b,c}, {c,d}, {d,e}, {e,a}, {e,b}}
K1 K3
K2
K6
REGULAR GRAPHS
5 5
1 2 1 2
3 4 3 4
Sub graph: A graph G’=(V’,E’) is a
subgraph of G=(V,E) if V’⊆ V and
E’⊆ E.
Component:
A sub graph of graph G is called the
connected component of G, if it is not
contained in any bigger sub graph of G,
which is connected.
e.g.
Consider the multigraphs G
(a) Which of them are connected ? If a graph is not
connected, find its connected components.
(b) Which are cycle-free?
(c) Which are loop-free?
A
(d) Which are simple graphs? B
A C
A B
C
B E
D
D B
C D 2 E
1 C
A 3
D E
4
Theorem: Let G=(V,E) be an undirected graph. Then
∑ degree(v)
v∈V
= 2E
∑ degree(v)
v∈V
= 2E
6 c 3 1
b d
v5
e1
v4 v3
e3
B e2 e6
e4
v1 e5 v2
5
1 2 3 4
a4
a1 a2 a3 a6
a5
Homeomorphic Graphs
Two graph G and G’ are called homeomorphic graphs if G’
can be obtained from G by a sequence of subdivisions of the
edges of G. In other words, we can introduce vertices of
degree two in any edge of graph G.
B
Trail: a walk with no repeated edges. A
vertex can appear more than once.
Terminal vertices: vertices with which a
walk begins and ends are called terminal
vertices.
Open and closed walk: If the walk begin
and end at the same vertex then it is closed
otherwise open.
Simple Path: An open walk in which no
vertex appears more than once.
e.g. <5,2,4,3,1> but not <5,2,4,3,1,2,5>
Circuit(Cycle): A closed walk in which
no vertex appears more than ones
except the terminal vertices is called a
circuit.
Length of a path: The number of edges
in a path.
Loop: closed walk; a walk from a node
to itself.
Cyclic Graphs
A graph is cyclic iff it can be written as a
cycle. Cyclic graphs are usually written as
Cn, where n is the vertex set in the graph.
Observation
A cyclic graph is always regular, i.e, all the
vertices have the same valency or degree.
Distance & Diameter
• length of shortest path between u and v
• d(u,v)
• maximum distance between any two points in G
• diam (G)
B E b
e
D a
A H d f
C F c h
Cutpoints & Bridges
• G be a connected graph
• v - cutpoint if G-v is disconnected
D H a
A d f
C F c h
e.g. Let G be the graph given. Find
(a) all simple paths from B to C (b) all cycles
(c) G -X (d) all cutpoints (e) all bridges
B C B
A
C
X Z X Y
Y 2
1 A B
C
X 3 Z
A path is Eulerian if it covers every edge in the
graph exactly once. A graph that consists of an
Eulerian path is called Euler graph.
can replace
path with cycle
(if a graph has an Eulerian path,
1
8 then you can
2 4 draw it without lifting your pencil)
7
Eulerian path:
3 5 6 <1,2,3,4,5,6,7,8>
5 5
1 2 1 2
3 4 3 4
A graph is connected if there is a path
between every pair of distinct
vertices. Euler graph is always connected.
5 5
1 2 1 2
3 4 3 4
D
C D
C
C
D
Theorem
A graph has an Eulerian cycle iff it is connected
and every vertex has even degree.
3 4 3 4
Which of these graphs are traversable , i.e. Have Euler
paths ? Which are Eulerian, i.e. have an Euler circuit?
Which of the graphs have a Hamiltonian circuit ?
Draw a graph with six vertices which is Eulerian but
not Hamiltonian and vice versa
3 4
2 7 2
P 2 3 1 Q
4
2
4
A4 6 A5 A6
Bipartite graph: (V,E)
An undirected graph whose vertex set V can be
partitioned in two disjoint, nonempty sets V1 and
V2 such that every edge connects a vertex in V1 to
a vertex in V2. ...
V1 V2 V1 V2
Bipartite Graph
x1 y1
x1 x2 x3 x4
y2 x2
Which
Redrawn
y3 as:
x3 y1 y2 y3 y4
y4 x4
Trees
B C
P Q
Y
X
A
Z
D
Star
A tree is a star if only 1 node has degree >1
B C
P
Y Q
X
A Z
D
Chain
A chain is a tree with no nodes of degree >2
B C
P Q
Y
X
A
Z
D
Minimal Spanning Trees
6
3 1 2
4
2 1
T2 T3
Finding the MST
2 1 1 2
2
2 E 1
D F
3 3 1 3
1
G 3 H 3 I
A 2 B 1 C
1
2
E 1
D F
3 1 3
G H I
e.g. Find the minimum spanning tree T
for the weighted graph G
A B
8
3
7 7
C 5 D
4
6
7
4
E F
Observations about
the Properties of Trees
If T = (V,E) is a tree with atleast two vertices, then following holds:
. | E| = | V| - 1
Definition
b a b
a
d c
c d
planar drawing
non-planar drawing of a planar graph
of a planar graph
MAPS , REGIONS
• a particular planar representation of a finite
planar multigraph is called a map.
• connected map
• map divides the plane into various regions.
• Note: A planar graph has only one infinite region.
r5
r4
A B r2 C
r1 F E
r3
D
Planar Graphs
degree of a region (deg(R)): the number of edges traversed in
a shortest closed walk about the boundary of R.
two different embeddings R
a b 6
R8
R2 R4
c R3 R5
R7
R1 g h
d f
deg(R1)=5,deg(R2)=3 deg(R5)=4,deg(R6)=3
deg(R3)=3,deg(R4)=7 deg(R7)=5,deg(R8)=6
abghgfda
4 8
∑ deg( Ri ) = 18 = ∑ deg( Ri ) = 2 × 9 = 2 | E |
i =1 i= 5
deg(r1) = 3, deg(r2) = 3, deg(r3) = 5, deg(r4) = 4,
deg(r5) = 3
r5
r4
A B r2 C
r1 F E
r3
D
R Degree of R = 3
Degree of R = ?
R
Planar Graphs
Theorem : Euler's planar graph theorem
number
number number of regions
of vertices of edges
Planar Graphs
Example of Euler’s theorem
v=4,e=6,r=4, v-e+r=2
R3
Planar Graphs
Corollary 1: In any simple, connected planar graph
with r regions, v vertices, e edges(e>2), the following
inequality must hold: e ≤ 3v – 6
Proof:Since each region is bounded by atleast three
edges and each edge belongs to exactly two regions
⇒2e ≥ 3r. (since each region has a degree of at least
3)
⇒r ≤ (2/3) e
⇒From Euler’s theorem, 2 = v – e + r
⇒2 ≤ v – e + 2e/3
⇒2 ≤ v – e/3
⇒So 6 ≤ 3v – e
Corollary 2: Let G = (V, E) be a connected
simple planar graph then G has atleast one
vertex with degree ≤ 5.
Proof: If G has one or two vertices the result
is true.
If G has 3 or more vertices then by Corollary
1, e ≤ 3v – 6
⇒ 2e ≤ 6v – 12
If the degree of every vertex were at least 6:
by theorem: 2e = Sum (deg(v))
⇒ 2e ≥ 6v. But this contradicts the inequality
2e ≤ 6v – 12
⇒ There must be at least one vertex with
degree not greater than 5
Example
∑ degree(v) = 2E
2E ≥ 6V, E ≥ 3V > 3V - 6 → not planar!
vertices v
V=5 E=10
NON PLANAR GRAPHS
10 = e ≤ 3v − 6 = 15 − 6 = 9
This is impossible. Thus it is non planar.
GRAPH COLORING
• assignment of colors to the vertices of G such that
adjacent vertices have different colors.
• n - colorable if there exists a coloring of G which
uses n colors.
C4 C5 K4 K2,3
MAP COLORING
Regions of map M are called adjacent if they have an
edge in common.
Coloring of M means assignment of a color to each
region of M such that adjacent regions have different
colors. r6
c1 r5
c2
r2 r4
r3 c2
c1
r1 c3
c1
DUAL MAP M*
Consider a map M. In each region of M we choose a
point , and if two regions have an edge in common
then we connect the corresponding points with a curve
through the common edge.
These curves can be drawn so that they are non
crossing, called as Dual Map M*.
Graph representations:
Adjacency lists.
Adjacency matrices.
Incidence matrices.
Representation
Incidence (Matrix): Most useful when information about edges
is more desirable than information about vertices.
v u w
u
v 0 1 0
u 0 0 1
v w
w 1 0 0
Incidence Matrix
• G = (V, E) be an unditected graph. Suppose that v1, v2,
v3, …, vn are the vertices and e1, e2, …, em are the edges
of G. Then the incidence matrix with respect to this
ordering of V and E is the nx m matrix M = [m ij ], where
a b c
e
d
DIRECTED GRAPH
Suppose e = (u,v) is a directed edge in a digraph G.
succ(u) = {v ∈ V :∃ ’s (u,v) ∈ E}
• It is called successor list or adjacency list
DIRECTED GRAPH
• picture: representation of graph G in the plane.
b
d in-degree = 2
c out-degree = 2
V = {a,b,c,d,e}
E = { (b,a), (c,b),
(d,c), (e,d), (e,b),
(b,e), (e,a) }
DIRECTED GRAPH
• source: A vertex v with zero indegree is called a source.
• sink: A vertex with zero outdegree is called a sink
(vertex C).
A e1 D
e2 e4
e3 e6
B e5 C
e7
• Directed path - alternating sequence of
vertices and directed edges.
• Length of path: Its number of edges.
B e5 C
e7
CONNECTIVITY
• strongly connected or strong: If for any pair of vertices u
and v in G, there is a path from u to v and from v to u.
• unilaterally connected or unilateral:If for any pair of
vertices u and v in G, there is a path from u to v or from
v to u.
• weakly connected or weak: If there is a semipath between
any pair of vertices u and v in G.
A e1
b x y D
a
e2 e4
e3 e6
B e5 C
d c w z
e7
• e.g.
v4
v3
v5 v6
CONNECTIVITY
strongly connected ⇒ unilaterally connected ⇒ weakly connected
8 9
2
10
1 7
5 12
3 11
4 6
Rooted Trees
A rooted tree is a tree where one of
the nodes is designated as the root
node. (Only one root in a tree)
A rooted tree has a hierarchical
structure: the root on top, followed by
the nodes adjacent to it right below,
followed by the nodes adjacent to
those next, and so on.
Rooted Trees
Definition
A rooted tree is a directed graph T
satisfying:
level 3
Properties of Trees
The height of a rooted tree is the maximum
of the levels of vertices.
Tree-Related Concepts
The nodes adjacent to x and below x are called
the children of x,
1
and x is called their parents
3 2 7
A node that has no
children is called a leaf 5 8 9
The descendents of a node
are: itself, its children, 4 6 10
their children, all the way down
11 12
The ancestors of a node are: itself, its parent, its
grandparent, all the way to the root
parent of c
root parent of b
level 0 a
level 1
b c
child of a child of a
sibling of c sibling of b
level 2
Two nodes having
the same parent
level 3
D (Descendent of B)
a
b c
d e f g
1 3
2 3.1 3.2
1.2.2.1
Lexicographic order
1.2.1 2.1 3.2
0
1.2.2 3 3.2.1
1
1.2.2.1 3.1 3.2.1.1
1.1
2 3.1.1 3.2.2
1.2
root
internal node
(i.e. non-leaf)
Left Right
subtree subtree
Binary Trees
Example:
1 1
3 2 7 3 7
5 8 9 5 8 9
4 6 10 4 6 10
11 12 11 12
Non binary tree Binary tree
Binary-Tree Related
Definitions
The children of any node in a binary tree are
ordered into a left child and a right child
1
A node can have a left and
a right child, a left child 2 6
only, a right child only, 3 7 8
or no children
4 5 9
The tree made up of a left
child (of a node x) and all its 10 11
descendents is called the left subtree of x
Right subtrees are defined similarly
BINARY TREE
In a full binary tree (complete), every internal node
has 2 children.
Preorder:
• visit the root of the tree. b e
• recursively traverse left subtree.
• recursively traverse right subtree. c d f
abcedfg
In order: g
•recursively traverse left subtree.
•visit the root. bcadegf
•recursively traverse right subtree.
Postorder:
•recursively traverse left subtree. cbdgfea
•recursively traverse right subtree.
•visit the root.
Rooted Trees
Preorder Traversal
Algorithm:
1
8 15
Go to the left subtree,
if one exists, and do a 6 7 9 14
postorder traversal. 5 12
Go to the right subtree, 3 4 13
if one exists, and do a 10 11
postorder traversal. 1 2
Visit the root
+
Preorder: + - 2 * 4 + 1 3
In order: -2 + 4 * 1 +
- * 3
Postorder: 2 - 4 1 3 + * +
2 4 +
1 3
Rooted Trees
Expression Trees
Properties
A binary tree can be used to represent an
arithmetic expression with the internal
vertices representing operations and the
terminal vertices representing operands.
The root of the tree is the final operation to be
done in the entire operation; the root of each
subtree is the final operation for that subtree.
When you reach the terminal vertices , the
operands are selected in left, right order.
• e.g.
E= ( x + 3 y ) (a − 2b)
4
↑ _-
a *
+ 4
2 b
x *
3 y
Digraphs and Relations, Adjacency Matrix
A = [ aij ] m×m
1 if there is an edge (vi , v j )
defined as aij =
0 otherwise
aij = number of edges begining at vi and ending at v j
(In the case of parallel edges or multiple digraph)
Notation a k (i, j ) = the ij th element in the matrix A k
Note :
a1 (i, j ) = no. of directed paths of length 1 from v i to v j
a 2 (i, j ) = no. of directed paths of length 2 from vi to v j
Digraphs and Relations, Adjacency Matrix
Y X
Z
W
Write the adjacency matrix of the digraph V={X,Y,Z,W}
0 0 0 1
A= 1 0 1 1
1 0 0 1
1 0 1 0
1 0 1 0 1 0 0 2
2 2 0 1 2 3 0 2 3
A = 1 0 1 1 A3 = 2 0 1 2
1 0 0 2 2 0 2 1
0 0 0 1
A= 1 0 1 1
1 0 0 1
Ans: 1 0 1 0
4 0 3 4 1 0 1 1
B4 = 11 0 7 11 P= 1 0 1 1
7 0 4 7 1 0 1 1
7 0 4 7 1 0 1 1
SHORTEST PATH
WARSHALL’S ALGORITHM
Define m - square Boolean matrices P0 , P1 ,.........., Pm .
Let Pk [i, j ] denote the ijth entry of the matrix Pk .
V={X,Y,Z,W}
Z W
0 1 1 1 0 1 1 1 0 1 1 1
0 0 0 1 0 0 0 1 0
P0 = P1 = 0 0 1
0 P2 =
1 0 1 0 1 0 1 0 1 0 1
0 0 1 0 0 0 1 0 0 0 1 0
0 1 1 1 0 1 1 1
0 0 1 1 1
0 0 1 P4 =
P3 = 0 1 1 1
0 1 0 1
0 1 1 1
0 1 1 1