0% found this document useful (0 votes)
385 views

Graphs

A graph is a mathematical structure consisting of vertices connected by edges. It can be represented as G = (V, E) where V is the set of vertices and E is the set of edges. There are different types of graphs like directed, undirected, complete, regular graphs. Graph theory concepts include paths, cycles, connectedness, isomorphism and more. Graphs are widely used in computer science, operations research and social sciences.

Uploaded by

Vishal Gaur
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
385 views

Graphs

A graph is a mathematical structure consisting of vertices connected by edges. It can be represented as G = (V, E) where V is the set of vertices and E is the set of edges. There are different types of graphs like directed, undirected, complete, regular graphs. Graph theory concepts include paths, cycles, connectedness, isomorphism and more. Graphs are widely used in computer science, operations research and social sciences.

Uploaded by

Vishal Gaur
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 156

GRAPH THEORY

What is a graph?

A graph is a set of points in a plane (or


in 3-space) and a set of line segments
(possibly curved), each of which either
joins two points or joins a point to itself.
Graphs
A graph is a structure (V,E), where V is a
finite set called the vertex set and E the set of
edges. E consists of 2-tuples of elements in
V.
a
d b
e c
Example:V={a,b,c,d,e},

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.

Different algorithms are suitable for processing


different types of graphs.
Formal Definition:
A graph G = (V, E) is a mathematical
structure consisting of two sets V and E. The
elements of V are called vertices (or nodes),
and the elements of E are called edges.
Undirected Graph : The edge set contains
the unordered pair of vertices.
Directed Graph (Digraph) : The edge set
contains the ordered pair of vertices.
Endpoints : a set of one or two vertices
associated to each edge.
Loop: an edge where both endpoints are
the same. Also called a self-loop.
Parallel edges: a collection of two or
more edges having identical endpoints.
A graph is simple if it has no loops or
parallel edges.
Example

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

The degree of a node: the number of


edges in the graph that have the node
as an endpoint (plus twice the number
of self-loops i.e. for a self-loop it is
counted twice).
Indegree
Outdegree
Directed graph: (V,E) such that
V is a set of vertices (or nodes)
E ⊆ V × V is a set of edges (or arcs)
a
e

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}}

Vertex Adjacent vertex


a b, e
b a, c, e
c b, d
2. Adjacency d c, e
list e a, b, d
Undirected Complete Graph
An undirected graph G=(V,E) of n vertices is a
graph in which each vertex is connected to
every other vertex i.e. and edge exists between
every pair of distinct vertices.
Notation :- kn
A complete graph with n vertices will have
n(n-1)/2 edges.
complete graph on n vertices (Kn):
undirected graph containing an edge
between each pair of distinct vertices

K1 K3
K2

K6
REGULAR GRAPHS

• regular or k-regular if every vertex has degree k


Connected and Disconnected Graph
A graph is connected if there is a path between
every pair of distinct vertices.
A graph is called disconnected if there is no path
between any two of its vertices.

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

Why? every 1 edge connects 2 vertices


5 vertex degree
1 4
1 2 2 4
3 3
3 4 4 3
sum = 4+4+3+3+2 = 16 5 2
no of edges = 8
Theorem- The number of vertices of odd degree in a graph
is always even.

Proof. If we consider the vertices with odd and even


degrees separately, the quantity in the left side of

∑ degree(v)
v∈V
= 2E

can be expressed as the sum of the two sums each taken


over vertices of even and odd degrees respectively, as
follows
∑ d (v ) + ∑ d (v ) = 2 E
v∈Ve v∈Vo

Since the RHS of above is even and the first expression on


the LHS is even (being a sum of even numbers), the second
expression must also be even.

Since each vertices are of odd degree , the total no of terms


in the sum must be even to make the sum an even number
Theorem- A graph G is disconnected iff its vertex set V
can be partitioned into two nonempty disjoint subsets
V1 and V2 such that there exists no edge in G whose one
end vertex is in subset V1 and the other in subset V2.

Theorem - If a graph (connected or disconnected) has


exactly two vertices of odd degree, there must be a path
joining these two vertices.
Proof: Suppose that such a partitioning exists.
Consider two arbitrary vertices a and b of G, such that
∈ b V2. No∈path can exist between vertices a
a V1 and
and b; otherwise, there would be at least one edge
whose one end vertex would be in V1 and the other in
V2. Hence, if a partition exists, G is not connected.
Conversely, let G be a disconnected graph. Consider a
vertex a in G. Let V1 be the set of all vertices that are
joined by paths to a. Since G is disconnected, V1 does
not include all vertices of G. The remaining vertices
will form a set V2. No vertex in V1 is joined to any in V2
by an edge. Hence the partition.
Proof: Let G be a graph with all even
vertices except vertices V1 and V2 , which
are odd. Since the number of vertices of
odd degree in a graph is always even,
therefore for every component of a
disconnected graph, no graph can have
an odd number of odd vertices.
Therefore, in graph G, V1 and V2 must
belong to the same component, and
hence must have a path between them.
Isomorphism
Two graphs G1 and G2 are called isomorphic graphs
if there is a one-to-one correspondence between
their vertices and edges i.e., the graphs have
identical representation except that the vertices may
have different labels.
Observations:
If the vertex sets differ in size, then no isomorphism
exists.
If the edge sets differ in size, then no isomorphism
exists.
a
e
A 5 4 2

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.

(a) (b) (c)


Walk: A walk is defined as a finite
alternating sequence of vertices and edges,
beginning and ending with vertices, such
that each edge is incident with the vertices
preceding and following it.
e1
v1 e3 v2 v1 e4 v3 e6 v4
v5
e5
e4 e2
e7
v3 e6 v4

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

• e - bridge if G-e is disconnected


B E b e

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

Not an Euler graph,


since it is not possible
to walk over each of
the seven bridges
exactly ones
Beginning anywhere and ending anywhere, can a person
walk through two islands crossing all bridges but not
crossing any bridge twice? Euler proved that a solution for
this problem does not exist.
EULER GRAPHS
• traversable if it can be drawn without any breaks in
the curve and without repeating any edges .
• such path must be trail
• called as traversable trail.
• Traversable multigraph – Consider a multigraph
G=(V,E). If the multigraph G consists of a
path which includes all vertices and whose edge
list contains each edge of the graph exactly once.
Then, the multigraph G is called a traversable
multigraph.
Determine whether or not each of the graph is
traversable. A
B
B
A A B

D
C D
C
C
D
Theorem
A graph has an Eulerian cycle iff it is connected
and every vertex has even degree.

A graph has an Eulerian path iff it is connected


and exactly two vertices have odd degree.

A brief sketch of the proof

Eulerian cycle → all even degrees:

For a cycle, when enter vertex along one edge,


we must immediately leave along another edge:
•Thus, for every vertex, no. of entrances = no. of
exits.
degree(vertex) = no of entrances + no of exits =
2*no of exits is even.

•For path, start and end vertices do not to have


even degree.
Note: Consequently a multigraph with more than two
odd vertices cannot be traversable.
Multigraph corresponding to the Konigsberg bridge
problem has four odd vertices.
A path is Hamiltonian if it touches every vertex in
the graph exactly once.

A Hamiltonian circuit comes back to original vertex.


1 Hamiltonian:
8
4 <1,2,3,4,5,6,7,8>
2
7
5 5
3 5 6
1 2 1 2

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

Hamiltonian and Eulerian and


non- Eulerian non-Hamiltonian
LABELED GRAPHS

G is called labeled graph if its edges and or


vertices are assigned data of one kind or
another.
Weighted Graph

A graph G is weighted if there is a value


associated with each edge (e.g. speed, cost,
distance, etc.)
 Weight of the edge e = w(e )
i i

We often denote this graph by (G, w). If G’ is


any subgraph of G, then w(G’) = ∑ e∈G ' w(e)
To optimise a connected graph find the graph
with the minimum weight (shortest path)
A1 A2
3 6 A3

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

Tree: a connected, simple graph without


cycles.
Star: a tree in which only 1 node has
degree greater than 1.
Chain: a tree in which no node has
degree greater than 2.
Any tree with n nodes has n-1 edges.
Tree
A tree is a connected simple graph with no cycles e.g.

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

Let G be a connected weighted graph.


A spanning subgraph includes all the
nodes of G.
A tree T is a spanning tree of G if T is a
spanning subgraph of G.
MST: A spanning tree of G whose total
edge-weight is a minimum.
G T1
7 4

6
3 1 2
4
2 1

T2 T3
Finding the MST

Two algorithms Kruskal and Prim


Kruskal's Algorithm:
Take a graph with 'n' vertices
keep adding the shortest (least cost) edge,
while avoiding the creation of cycles, until
(n - 1) edges have been added.
NOTE: Sometimes two or more edges may
have the same cost. The order in which the
edges are chosen, in this case, does not
matter.
Different MSTs may result, but they will all
have the same total cost, which will always be
the minimum cost
Prim's Algorithm:
start at any vertex in a graph (vertex A, for example), and
finds the least cost vertex (vertex B, for example)
connected to the start vertex.
From either 'A' or 'B', it will find the next least costly vertex
connection, without creating a cycle (vertex C, for
example).
From either 'A', 'B', or 'C', it will find the next least costly
vertex connection, without creating a cycle, and so on it
goes.
Eventually, all the vertices will be connected, without any
cycles, and an MST will be the result.
NOTE: Two or more edges may have the same cost, so
when there is a choice by two or more vertices that is
exactly the same, then one will be chosen, and an MST
will still result
Prim’s Minimal Spanning Tree Algorithm
Kruskal’s
Minimal Spanning Tree Algorithm
e.g. Find the minimum spanning tree T for the weighted
graph G
A 2 B 1 C

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:

1. For each vertex x,y there is a path from x to y in T

2. The graph obtained by removing an edge from


a tree has two components, both of which are trees.

. | E| = | V| - 1
Definition

A graph or multigraph is planar if it can be drawn


on a flat piece of paper so that no two edges
cross each other.

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

Theorem- The sum of the degrees of the region of a map is


equal to twice the number of edges.
Note: Each edge either borders two regions or is contained
in a region and will occur twice in any walk along the
border of the region.
Region Degree

R Degree of R = 3

Degree of R = ?

R
Planar Graphs
Theorem : Euler's planar graph theorem

For a connected planar graph:


v–e+r=2

number
number number of regions
of vertices of edges
Planar Graphs
Example of Euler’s theorem

A planar graph divides the plane


R1 into several regions (faces), one
K4 R4
R2 of them is the infinite region.

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

Theorem (Kuratowski)- A graph is nonplanar iff it


contains a sub graph homeomorphic to k 3,3 or k 5
k 3, 3
This graph has 6 vertices and 9 edges
Suppose the graph is planar, then by Euler’s formula a
planar representation has r=5 regions.
No three vertices are connected to each other, hence the
degree of each region must be 4 or more and so the sum
of the degrees of the regions must be 20 or more. Hence,
by corollary 1, the graph must have 10 or more edges.
This contradicts the fact that the graph has 9 edges.
Hence the graph is non planar.
k5

This is complete graph and has 5 vertices and 10 edges


If the graph is planar then

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.

• chromatic number χ (G ) - minimum no of color


needed to paint G.
• χ (k n ) = n
χ (G ) = 3
a
2
f 1 2
b h 3
g
3
1e
c 1
2d
ALGORITHM (Welch-Powell)
1. Order the vertices of G according to decreasing
degrees.
2. Assign the first color C1 to the first vertex and
then, in sequential order, assign C1 to each vertex
which is not adjacent to a previous vertices which
was assigned C1.
3. Repeat step 2 with a second color C2 and the
subsequence of non colored vertices.
4. Repeat step 3 with a second color C3, then a
fourth color C4, and so on until all vertices are
colored
5. Exit.
Theorem- The following are equivalent for a graph
G:
(i) G is 2-colorable
(ii) G is bipartite
(iii) Every cycle of G has even length.
The chromatic number for Cn = 3 (n is odd) or 2 (n
is even), Kn = n, Km,n = 2
Cn: cycle with n vertices;
Kn: fully connected graph with n vertices; Km,n:
complete bipartite graph

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*.

Coloring of regions of M is same as coloring of


vertices of M*.
M is n-colorable iff the planar graph of the dual map
M* is vertex n-colorable.
Theorem: Let G be a plane graph. Then
(i) G* is connected
(ii) G is connected =>G**≅ G;
(iii) G is not connected =>G** is obtained from
G by “gluing together” vertices of G to make
new cut vertices (of G**).
Graph Representations

Graph representations:
 Adjacency lists.
 Adjacency matrices.

 Incidence matrices.
Representation
Incidence (Matrix): Most useful when information about edges
is more desirable than information about vertices.

Adjacency (Matrix/List): Most useful when information about


the vertices is more desirable than information about the edges.
These two representations are also most popular since
information about the vertices is often more desirable than
edges in most applications.
Adjacency Lists
A table with 1 row per vertex, listing its
adjacent vertices. Adjacent
a b Vertex Vertices
a b, c
c d b a, c, e, f
e
f c a, b, f
d
e b
f c, b
Directed Adjacency Lists
1 row per node, listing the terminal nodes of
each edge incident from that node.
0 1
node Terminal nodes
0 3
1 0, 2, 4
2 1
3
3
2 4
4 0,2
Adjacency Matrix
• There is an N x N matrix, where |V| = N , the Adjacency
Matrix (NxN) A = [aij]

For undirected graph


1 if {vi, vj} is an edge of G
a ij = 
0 otherwise
• For directed graph
1 if (vi, vj) is an edge of G
a ij = 
0 otherwise
Adjacency Matrices
Matrix A=[aij], where aij is 1 if {vi, vj} is an
edge of G, 0 otherwise.
a b c d
a
b a 0 1 1 1
b
1 0 1 
0
d 
c 1 1 0 0
c  
d 1 0 0 0
Adjacency Matrix
Example: directed Graph G (V, E)

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

1 when edge e j is incident on vertex v i


m ij = 
0 otherwise
Can also be used to represent :
Multiple edges: by using columns with identical entries,
since these edges are incident with the same pair of
vertices
Loops: by using a column with exactly one entry equal
to 1, corresponding to the vertex that is incident with the
loop
Incidence matrices
Matrix M=[mij], where mij is 1 when edge ej
is incident with vi, 0 otherwise
v1 e2 e1 e2 e3 e4 e5
e3 v4 v1 1 1 1 0 0
e1 e4 0 0 1 1 
0
v2
e5 v2 
v3 1 0 0 0 1
v3
 
v4 0 1 0 1 1
• write the adjacency list of the following graph

a b c

e
d
DIRECTED GRAPH
Suppose e = (u,v) is a directed edge in a digraph G.

• e begins at u and ends at v.


• u is the origin or initial point of e, and v is the
destination or terminal point of e.
• v is a successor of u.
• u is adjacent to v and v is adjacent from u
• If u=v, then e is called a loop .
• Succesors of u is

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.

• labeled: edges and/or vertices of a directed graph


G is labeled with some type of data.

• finite: G(V,E) is finite if its set V of vertices and


its set E of edges are finite.

• subgraph: Let G(V,E) be a directed graph, and


let V’ be a subset of set V of vertices of G. Suppose
E’ is a subset of E such that the end points of the
edges in E’ belongs to V’. Then H(V’,E’) is a
directed graph, and is called a subgraph of G.
Directed graph: (V,E) such that
V is a set of vertices (or nodes)
E ⊆ V × V is a set of edges (or arcs)
a
e

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.

• Simple Path – is a path with distinct vertices.


• Trail - is a path with distinct edges.
• closed path: Same first and last vertices.
• Semi path: same as path except the edge ei may begin
at v i-1 or vi and end at the other vertex.
• Spanning path: contains all the vertices of graph G
• Reachable: v is reachable from u if there is a path
from u to v.
DIRECTED GRAPH
• e.g. (a) P1 = (D,C,B,A) is semipath but not a path since
(C,B) is not an edge.
(b) P2 = (D,B,A) is a path from D to A Since (D,B) and
(B,A) are edges. Thus A is reachable from D.
A e1 D
e2 e4
e3 e6

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.

Find two simple paths from v1 to v6.


Find all cycles in G which include v3.
Is G unilaterally connected ? Strongly connected?
v1 v2

v4
v3

v5 v6
CONNECTIVITY
strongly connected ⇒ unilaterally connected ⇒ weakly connected

• strictly unilateral: if it is unilateral but not strong.


• strictly weak: if it is weak but not unilateral.
Theorem - Let G be a finite directed graph.
Then:
(i) G is strong iff G has a closed spanning path.
(ii) G is unilateral iff G has a spanning path.
(iii) G is weak iff G has a spanning semi path.
Trees
A tree is a connected acyclic (no cycle)
undirected graph. The following are
three trees:

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:

The underlying graph is a tree.


There is a unique vertex R called the root
with indegree 0 and all other vertices
have indegree 1.
Example of a Rooted
Tree 1
8 9
2 3 2 7
10
1 7
5 12 5 8 9
3 11 4 6 10
4 6
11 12
Unrooted tree
Tree rooted with root 1
Rooted Trees
Properties
The number of vertices is one
more than the number of directed
edges.
There are no directed cycles.
There is a unique directed simple
path from the root to every other
vertex.
Properties of Trees
A tree with n vertices has n-1 edges.
Properties of Trees
The level of a vertex v in a rooted tree is
the length of the unique path from the root
to this vertex.
level 1
level 2

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

All edges directed away from root.


Depth (or level) is distance from root.
Leaves are nodes with no children.
Every node (except root) has 1 parent.
Tree-Related Concepts
(Contd.)
The depth of a node is the number of
edges from the root to that node.
The depth (or height) of 1
a rooted tree is the depth 3 2 7
of the lowest leaf 5 8 9
Depth of node 10: 3 10
4 6
Depth of this tree: 4
11 12
Terminal vertex - zero outdegree
Internal vertex - positive outdegree
Rooted Trees
A (Root)

E (Internal Vertex) B (Parent)

F (Terminal Vertex) G (Ancestor of D)


C (Child)

D (Descendent of B)
a

b c

d e f g

subtree with b as its


h i root
subtree with c as its
root
ORDERED ROOTED TREES
Universal address system
• systematically label(or add) the vertices of such trees

1. Assign 0 to the root r


2. Assign a,b,c,---- to the vertices immediately
following r
3. If a is the label of a vertex v, then a.1,a.2,--- are
assigned to the vertices immediately following v

• important way of linearly describing (or storing)


an ordered rooted tree.
Ordered Rooted Tree

An ordered rooted tree is a rooted tree


where the children of each internal vertex
are ordered. Ordered trees are drawn so
that the children of each internal vertex
are shown in order from left to right.
0

1 3
2 3.1 3.2

1.1 2.1 3.2.1


3.2.2
1.2 3.1.1
3.2.1.1
1.2.2
1.2.1

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

This Lexicographic order is identical to the order obtained


by moving down the leftmost branch of the tree, then the
second branch to the right, and so on.
Def’n
If the outdegree of every node is less than or equal to 2 in a
directed tree then the tree is called a binary tree.

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.

• internal nodes - circles


• external nodes - squares

Converting a binary tree T into a 2-tree


a

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

Visit the root.


Go to the left subtree, 2 10
if one exists, and do a 3 9 11 12
preorder traversal. 4 13
Go to the right 5 8 16
subtree, if one exists, 14 15
6 7
and do a preorder
traversal.
Rooted Trees
Inorder Traversal Algorithm:

Go to the left subtree, if


one exists, and do an 7 11
inorder traversal. 8 10
1 15
Visit the root. 5 13
Go to the right subtree, 3 6 16
if one exists, and do an 12 14
inorder traversal. 2 4
Rooted Trees
Postorder Traversal
Algorithm: 16

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

We will use the notation:


2 0 2 1 ak(i,j)=the ij entry in the matrix Ak
5 0 3 5
A =
4
3 0 2 3
Note that a1(i,j)=aij gives the number
of directed paths of length 1 from
3 0 1 4 vertex vi to vj.
a2(i,j) gives the number of directed
paths of length 2 from vi to vj and so
Remark :
Suppose A is the adjacency matrix of a graph G, and
suppose we now define the matrix Br as follows :
Br = A + A + − − − − − − − + A
2 r

Then the ij entry of the matrix Br gives the no. of


directed paths of length r or less from vertex
vi to v j .
Path Matrix
Let G = G(V, E) be a simple directed graph with
m vertices v1 , v 2 ,............., v m . The path matrix
or reachability matrix of G is the m - square matrix P = (p ij )
1 if there is a path from vi to v j
defined as follows pij = 
0 otherwise
Proposition - Let A be the adjacency matrix G with m
vertices , and let Bm = A + A + − − − − + A .
2 m

Then the path matrix P and Bm have the same nonzero


entries.
Given A find B4 and P

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 .

1 if there is a simple path from vi to v j



 which does not use any other vertices except
Pk [i, j ] = 
 possibly v1 , v2 ,......., vk
0 otherwise

WARSHALL’S ALGORITHM
i.e.
P0 [i, j ] = 1 if there is an edge from vi to v j
P1[i, j ] = 1 if there is a simple path from vi to v j which
does not use any other vert ex except possibly v1
P2 [i, j ] = 1 if there is a simple path from vi to v j which
does not use any other vert ex except possibly v1 and
v2
And so on.
Observe that the first matrix P0 = A , the adjacency
matrix of G.
Since G has only m vertices, the last matrix Pm = P,
the path matrix of G.
X Y

V={X,Y,Z,W}

Z W

e.g. 1. Find the adjacency matrix A of the graph G


2. Find the path matrix P of G using powers of
adjacency matrix A
3. Is G strongly connected?
Find the path matrix P of G using Warshall’s Algorithm
rather than the powers of A.
Compute the matrices P0 , P1 , P2 , P3 , and P4
where initially P0 = A and Pk [i, j] = Pk −1[i, j] ∨ (Pk −1[i, k ] ∧ Pk −1[k , j])
Pk [i, j ] = 1 if Pk −1 [i, j ] = 1 or Pk −1 [i, k ] = 1 and Pk −1 [k , j ] = 1

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

The last matrix is the required path matrix P of the


graph G.
Shortest Path for Weighted Graph
w(e) if there is an edge e from vi to v j
Weight matrix W = 
0 if there is no edge from vi to v j
Q = (qij ) where qij = length of shortest path from vi to v j

Qk [i, j ] = MIN (Qk −1 [i, j ], Qk −1 [i, k ] + Qk −1 [k , j ])

The initial matrix Q 0 is the same as the weight matrix W


except that 0 is replaced by ∞.
The final matrix Q m will be the desired matrix Q.
7
4 7 5 0 0
U 7 0 0 
2
R
W =
0 3 0 0
 
2 4 0 1 0
5
7 1
 7 5 ∞ ∞
7 ∞ ∞ 2
S T
Q0 =  
∞ 3 ∞ ∞ 
3  
 4 ∞ 1 ∞
V={R,S,T.U}
Qk [i, j ] = MIN (Qk −1 [i, j ], Qk −1 [i, k ] + Qk −1 [k , j ])
Q1 [4,2] = MIN (Q0 [4,2], Q0 [4,1] + Q0 [1,2]) = MIN (∞,4 + 5) = 9
Q2 [1,3] = MIN (Q1 [1,3], Q1 [1,2] + Q1 [2,3]) = MIN (∞,5 + ∞) = ∞
 7 5 ∞ ∞ 7 5 ∞ 7
 7 12 ∞ 2   7 12 ∞ 2 
Q1 =   Q2 =  
∞ 3 ∞ ∞  10 3 ∞ 5 
   
 4 9 1 ∞   4 9 1 11
 7 5 ∞ 7 7 5 8 7 
 7 12 ∞ 2 7 11 3 2
Q3 =   Q4 =  
10 3 ∞ 5 9 3 6 5 
   
 4 4 1 6 4 4 1 6
• e.g. Given a directed graph
V = 4; E = 6;(1,2,5),(2,4,2),(3,2,3),(1,1,7),(4,1,4),(4,3,1)
Find W of G
Find Q of shortest path using Warshall’s Algorithm

You might also like