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.
GraphsA 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.
Example:V={a,b,c,d,e},
E= {(a,b),(b,c),(d,e)}.
be c
da
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
A,B,C etc. are vertices(nodes) (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
XY
Z
PQ
A
B
D
C
v1
v2 v3
v4
e1
e2
e3
e4
A
e7
v1 v2
v3 v4
v5
e1
e2
e3
e4e5
e6
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 thatV is a set of vertices (or nodes)E V V is a set of edges (or arcs)
a
b
c
d
e
in-degree = 2out-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.
b
c
d
e
a
1. Edge list: {{a,b}, {b,c}, {c,d}, {d,e}, {e,a}, {e,b}}
2. Adjacency list
a, b, de
c, ed
b, dc
a, c, eb
b, ea
Adjacent vertexVertex
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 edgebetween each pair of distinct vertices
.
K1 K2K3
K6
REGULAR GRAPHS
• regular or k-regular if every vertex has degree k
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.
1 2
3 4
5
1 2
3 4
5
Connected and Disconnected Graph
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?(d) Which are simple graphs?
A B
CD
1 23
4
A
A
A
B
B
BC
C
C
D
D
D
E
E
E
Why? every 1 edge connects 2 vertices
1 2
3 4
5 vertex degree 1 4 2 4 3 3 4 3 5 2sum = 4+4+3+3+2 = 16
no of edges = 8
Theorem: Let G=(V,E) be an undirected graph. Then
EvVv
2)(degree
Theorem- The number of vertices of odd degree in a graphis always even.
Proof. If we consider the vertices with odd and even degrees separately, the quantity in the left side of
EvVv
2)(degree
can be expressed as the sum of the two sums each taken over vertices of even and odd degrees respectively, as follows
EvdvdVovVv e
2)()(
Since the RHS of above is even and the first expression on the LHS is even (being a sum of even numbers), the secondexpression 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 a V1 and b V2. No path can exist between vertices a 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.
IsomorphismTwo 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.
1
2
3
45
6
a
b
cd
e
v1 v2
v3v4
v5
e1
e2
e3
e4
e5
e6
A
B
1 2 3 45
6
a1 a2 a3
a4
a5 a6
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.
e7
v1 v2
v3 v4
v5
e1
e2
e3
e4e5
e6
B
v1 e4 v3 e6 v4
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
A
B
C
D
E
F
H
a
b
c
d f
e
h
• diam (G)
Cutpoints & Bridges
• G be a connected graph
• v - cutpoint if G-v is disconnected
• e - bridge if G-e is disconnectedB
A
D
E
H
FC
a
b
c
d f
h
e
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
AC
1
B
X Y Z
3
A BC
X Z
2
B
C
X Y
A path is Eulerian if it covers every edge in thegraph exactly once. A graph that consists of an Eulerian path is called Euler graph.
(if a graph has an Eulerian path, then you candraw it without lifting your pencil)2
1
4
3 5 6
7
8
Eulerian path:<1,2,3,4,5,6,7,8>
1 2
3 4
5
1 2
3 4
5
can replacepath with cycle
A graph is connected if there is a pathbetween every pair of distinct
vertices. Euler graph is always connected.
1 2
3 4
5
1 2
3 4
5
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.
• 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.
EULER GRAPHS
Determine whether or not each of the graph is traversable.
AB
CD
AB
C D
A B
CD
A graph has an Eulerian cycle iff it is connectedand every vertex has even degree.
A graph has an Eulerian path iff it is connectedand 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:
Theorem
•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 inthe graph exactly once.
A Hamiltonian circuit comes back to original vertex.
2
1
4
3 5 6
7
8Hamiltonian: <1,2,3,4,5,6,7,8>
1 2
3 4
5
1 2
3 4
5
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 ei = w(ei)
We often denote this graph by (G, w). If G’ is any subgraph of G, then w(G’) = '
)(Ge
ew
To optimise a connected graph find the graph with the minimum weight (shortest path)
P Q
A1 A2A3
A4 A5 A6
3
4
3
22
6
3
6
4
2
17 2
4
...
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.
V2V1 V2 V1
y4
y1
y3
y2
x1
x2
x4
x3 y1 y2 y3 y4
x1 x2 x3 x4
Bipartite Graph
Which
Redrawn
as:
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.
XY
Z
P Q
A
B
D
C
Star
A tree is a star if only 1 node has degree >1
X
Y
Z
PQ
A
B
D
C
Chain
A chain is a tree with no nodes of degree >2
XY
Z
P Q
A
B
D
C
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.
7 4
3
2 1
216
4
G T1
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 B C
DE F
G H I
2 1
22 21
1
1
113 3
3 3
3
2
A B C
DE F
G H I
2 1
21
1
13 3
e.g. Find the minimum spanning tree T for the weighted graph G
A B
C D
E F
7 7
76
83
5
4
4
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.
3. E = V - 1
Def
init
ion 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.
non-planar drawingof a planar graph
a b
cd
planar drawingof a planar graph
a b
dc
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
r1r2
r3
r4A B C
DEF
Planar Graphs
degree of a region (deg(R)): the number of edges traversed in a shortest closed walk about the boundary of R.
R1
R2
R3
R4 R5
R6
R7
R8
two different embeddings
deg(R1)=5,deg(R2)=3deg(R3)=3,deg(R4)=7
deg(R5)=4,deg(R6)=3deg(R7)=5,deg(R8)=6
deg( ) deg( ) | |R R Eii
ii
1
4
5
818 2 9 2
abghgfda
a b
c
d fg h
r5
r1r2
r3
r4A B C
DEF
deg(r1) = 3, deg(r2) = 3, deg(r3) = 5, deg(r4) = 4, deg(r5) = 3
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
R
Degree of R = 3
Degree of R = ?
Theorem : Euler's planar graph theorem
For a connected planar graph: v – e + r = 2
numberof vertices
numberof edges
numberof regions
Planar Graphs
Planar Graphs
Example of Euler’s theorem
K4
R1R2
R3
A planar graph divides the planeinto several regions (faces), one of them is the infinite region.
v=4,e=6,r=4, v-e+r=2
R4
Planar GraphsCorollary 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
or e ≤ 3v – 6
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) = 2E2E 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 53,3 or kk
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.
Hence the graph is non planar.
3,3k
No three vertices are connected to each other, hence thedegree 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.
This is complete graph and has 5 vertices and 10 edgesIf the graph is planar then
96156310 veThis is impossible. Thus it is non planar.
5k
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 - minimum no of color needed to paint G.
)(G
• = n )( nk
a
b
c
d
e
f
g
h1
1
1
2
2
2
3
3
3)( G
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
C5 K4K2, 3C4
Regions of map M are called adjacent if they have an edge in common.Coloring of M means assignment of a color to eachregion of M such that adjacent regions have different colors.
r1
r2r3
r4
r5r6
MAP COLORING
c1
c1
c2
c2
c3c1
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 curvethrough 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.
a b
dc
fe
Vertex
Adjacent Vertices
a b
b, c a, c, 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
23
4
node Terminal nodes
0 3
1 0, 2, 4
2 1
3
4 0,2
Adjacency Matrix
There is an N x N matrix, where |V| = N , the Adjacency Matrix (NxN) A = [aij]
For undirected graph
For directed graph
otherwise 0
G of edgean is ) v,(v if 1 a
jiij
otherwise 0
G of edgean is } v,{v if 1 a
jiij
Adjacency Matrices
Matrix A=[aij], where aij is 1 if {vi, vj} is an edge of G, 0 otherwise.
ab
c
d
0001
0011
0101
1110a
b
c
d
a b c d
Adjacency Matrix
Example: directed Graph G (V, E)
v u w
v 0 1 0
u 0 0 1
w 1 0 0
u
v w
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
Can also be used to represent :Multiple edges: by using columns with identical entries, since these edges are incident with the same pair of verticesLoops: by using a column with exactly one entry equal to 1, corresponding to the vertex that is incident with the loop
otherwise 0
von vertex incident is e edge when 1 m
ijij
Incidence matrices
Matrix M=[mij], where mij is 1 when edge ej
is incident with vi, 0 otherwise
v1
v2
v3
v4
e1 e2 e3 e4v1v4
v3
v2
e5
11010
000
000
001
11
11
11e1
e2
e5
e4e3
a b c
de
• write the adjacency list of the following 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
Directed graph: (V,E) such thatV is a set of vertices (or nodes)E V V is a set of edges (or arcs)
a
b
c
d
e
in-degree = 2out-degree = 2
V = {a,b,c,d,e}E = { (b,a), (c,b),(d,c), (e,d), (e,b),(b,e), (e,a) }
• source: A vertex v with zero indegree is called a source. • sink: A vertex with zero outdegree is called a sink
(vertex C).
A
B C
De1
e2 e3e4
e5
e6
e7
DIRECTED GRAPH
• 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.
A
B C
De1
e2 e3e4
e5
e6
e7
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.
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
B C
De1
e2 e3 e4
e5
e6
e7
a b
cd
yx
zw
v1 v2
v3 v4
v5 v6
Find two simple paths from v1 to v6.Find all cycles in G which include v3.
Is G unilaterally connected ? Strongly connected?
• e.g.
strongly connected unilaterally connected weakly connected
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.
CONNECTIVITY
• strictly unilateral: if it is unilateral but not strong.• strictly weak: if it is weak but not unilateral.
TreesA tree is a connected acyclic (no cycle) undirected graph. The following are three trees:
1
2
53 11
12
10
98
7
64
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 TreesDefinition 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
2
53 11
12
10
98
7
64
1
23
10
11
98
4 6
5
7
12Unrooted tree
Tree rooted with root 1
Rooted Trees
PropertiesThe 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 2
level 1
level 3
Properties of Trees
The height of a rooted tree is the maximum of the levels of vertices.
Tree-Related ConceptsThe nodes adjacent to x and below x are called the children of x,and x is called their parentsA node that has no children is called a leafThe descendents of a node are: itself, its children, their children, all the way downThe ancestors of a node are: itself, its parent, its grandparent, all the way to the root
1
23
10
11
98
4 6
5
7
12
rootlevel 0
level 1
level 2
level 3
a
b c
parent of cparent of b
child of asibling of c
child of asibling of b
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.
Two nodes having the same 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
a rooted tree is the depth
of the lowest leaf
Depth of node 10: 3
Depth of this tree: 4
1
23
10
11
98
4 6
5
7
12Terminal vertex - zero outdegreeInternal vertex - positive outdegree
Rooted Trees
D (Descendent of B)
C (Child)G (Ancestor of D)F (Terminal Vertex)
B (Parent)E (Internal Vertex)
A (Root)
a
b c
d e f g
h isubtree with b as its root
subtree with c as its root
ORDERED ROOTED TREES
• 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
Universal address system
• 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
12
3
1.1
1.2
1.2.11.2.2
1.2.2.1
2.1
3.1
3.1.1
3.2
3.2.13.2.2
3.2.1.1
Lexicographic order
011.11.2
1.2.11.2.21.2.2.12
2.133.13.1.1
3.23.2.13.2.1.13.2.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 adirected tree then the tree is called a binary tree.
root
Leftsubtree
Rightsubtree
internal node(i.e. non-leaf)
Binary Trees
Example:
1
23
10
11
98
4 6
5
7
12
1
3
10
11
98
4 6
5
7
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 childA node can have a left anda right child, a left childonly, a right child only,or no childrenThe tree made up of a leftchild (of a node x) and all itsdescendents is called the left subtree of xRight subtrees are defined similarly
9
1
2
10
87
4 5
3
6
11
In a full binary tree (complete), every internal nodehas 2 children.
• internal nodes - circles • external nodes - squares
Converting a binary tree T into a 2-tree
BINARY TREE
a
b e
c d f
g
Preorder:• visit the root of the tree.• recursively traverse left subtree.• recursively traverse right subtree.
a b c e d f g In order:•recursively traverse left subtree.•visit the root.•recursively traverse right subtree.
b c a d e g f
Postorder:•recursively traverse left subtree.•recursively traverse right subtree.•visit the root.
c b d g f e a
Preorder Traversal Algorithm:
Visit the root.Go to the left subtree, if one exists, and do a preorder traversal.Go to the right subtree, if one exists, and do a preorder traversal.
Rooted Trees
161514
13
1211
10
9
76
5 8
4
3
2
1
Inorder Traversal Algorithm:
Go to the left subtree, if one exists, and do an inorder traversal.Visit the root.Go to the right subtree, if one exists, and do an inorder traversal.
Rooted Trees
161412
13
1510
11
8
42
3 6
5
1
7
9
Postorder Traversal Algorithm:
Go to the left subtree, if one exists, and do a postorder traversal.Go to the right subtree, if one exists, and do a postorder traversal.Visit the root
Rooted Trees
131110
12
149
15
7
21
3 4
5
6
8
16
+
- *
2 4 +
1 3
Preorder: + - 2 * 4 + 1 3
In order: -2 + 4 * 1 + 3Postorder: 2 - 4 1 3 + * +
Expression TreesProperties
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.
Rooted Trees
E = )2()3( 4 bayx
• e.g.
b2
y3
*x
4+
_
*
-
*a
kk
jiij
jiij
mmij
Ajia
vva
vva
aA
matrix in theelement ij the),(Notation
digraph) multipleor edges parallel of case (In the
at ending and at begining edges ofnumber
otherwise 0
),( edgean is thereif 1 as defined
][
th
ji
ji
vvjia
vjia
to from 2length of paths directed of no. ),(
to vfrom 1length of paths directed of no. ),(
: Note
2
1
Digraphs and Relations, Adjacency Matrix
Y
Z
X
W
Write the adjacency matrix of the digraph V={X,Y,Z,W}
A = 0 0 0 11 0 1 11 0 0 11 0 1 0
Digraphs and Relations, Adjacency Matrix
1 0 1 02 0 1 21 0 1 11 0 0 2
2A =
1 0 0 23 0 2 32 0 1 22 0 2 1
3A
2 0 2 15 0 3 53 0 2 33 0 1 4
4A
We will use the notation:ak(i,j)=the ij entry in the matrix Ak
Note that a1(i,j)=aij gives the numberof directed paths of length 1 from vertex vi to vj.
a2(i,j) gives the number of directed paths of length 2 from vi to vj and so on.
. to
vertex from lessor r length of paths directed
of no. thegives matrix theofentry ij Then the
: follows as matrix thedefine now wesuppose
and G,graph a ofmatrix adjacency theisA Suppose
:Remark
2
ji
r
rr
r
vv
B
AAAB
B
otherwise 0
to frompath a is thereif 1 follows as defined
)(p Pmatrix square-m theisG ofmatrix ty reachabilior
matrixpath The .....,,........., verticesm
graph with directed simple a be E)G(V, G Let
MatrixPath
ij
21
jiij
m
vvp
vvv
entries.
nonzero same thehave and Pmatrix path Then the
.let and , vertices
mG with matrix adjacency thebeA Let -n Propositio2
m
mm
B
AAAB
A = 0 0 0 11 0 1 11 0 0 11 0 1 0
Given A find and P 4B
Ans:
B4 = 4 0 3 411 0 7 11 7 0 4 77 0 4 7
P = 1 0 1 11 0 1 11 0 1 11 0 1 1
SHORTEST PATHWARSHALL’S ALGORITHM
.matrix theofentry ij thedenote ],[Let
. .,,........., matricesBoolean square-m Defineth
10
kk
m
PjiP
PPP
otherwise 0
,.......,,possibly
except icesother vertany usenot does which
to frompath simple a is thereif 1
],[21 k
ji
kvvv
vv
jiP
on. so And
possibly except ex other vertany usenot does
which to frompath simple a is thereif 1],[
possibly except ex other vertany usenot does
which to frompath simple a is thereif 1],[
to from edgean is thereif 1],[
i.e.
2
1
2
1
1
0
v
andv
vvjiP
v
vvjiP
vvjiP
ji
ji
ji
WARSHALL’S ALGORITHM
G. ofmatrix path the
,matrix last the vertices,monly hasG Since
G. ofmatrix
adjacency the,A matrix first that theObserve 0
PP
P
m
X Y
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?
V={X,Y,Z,W}
Find the path matrix P of G using Warshall’s Algorithmrather than the powers of A.
])j,k[P]k,i[P(]j,i[P]j,i[P and APinitially where
P and ,P,P,P,P matrices theCompute
1k1k1kk0
43210
1],[ and 1],[or 1],[ if 1],[ 111 jkPkiPjiPjiP kkkk
0100
1010
1000
1110
0P
0100
1010
1000
1110
2P
0100
1010
1000
1110
1P
1110
1010
1000
1110
3P
1110
1110
1110
1110
4P
The last matrix is the required path matrix P of the graph G.
Shortest Path for Weighted Graph
to from edge no is thereif 0
to from e edgean is thereif )(matrix Weight
ji
ji
vv
vvewW
jiijij vvqqQ to frompath shortest oflength where)(
]),[],[],,[(],[ 111 jkQkiQjiQMINjiQ kkkk
.Qmatrix desired thebe willQmatrix final The
.by replaced is 0t except tha
Wmatrix weight theas same theis Qmatrix initial The
m
0
0104
0030
2007
0057
W
14
3
27
57
0Q
R
S T
U
75
4
7
3
21
V={R,S,T.U}
9)54,(])2,1[]1,4[],2,4[(]2,4[ 0001 MINQQQMINQ
)5,(])3,2[]2,1[],3,1[(]3,1[ 1112 MINQQQMINQ
194
3
2127
57
1Q
11194
5310
2127
757
2Q
6144
5310
2127
757
3Q
6144
5639
23117
7857
4Q
]),[],[],,[(],[ 111 jkQkiQjiQMINjiQ kkkk
• 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 GFind Q of shortest path using Warshall’s Algorithm