Вы находитесь на странице: 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.

d e

b 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 Y C P 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 e1 v2 A e2 e3 e5 v3 v3 v1 e4 e6 B e3 v2 v5 e2 v4 e7

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 V is a set of edges (or arcs) a e d c V = {a,b,c,d,e} E = { (b,a), (c,b), (d,c), (e,d), (e,b), (b,e), (e,a) } b in-degree = 2 out-degree = 2

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 d c b

1. Edge list: {{a,b}, {b,c}, {c,d}, {d,e}, {e,a}, {e,b}} Vertex a b c d e


Adjacent vertex

2. Adjacency list

b, e a, c, e b, d c, 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

K2

K3

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 1 3 2 4 1 3 5 2 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 B (d) Which are simple graphs? A C B A B C D 1 D 2 A D 4 E B C E C D E 3

Theorem: Let G=(V,E) be an undirected graph. Then

degree(v)
vV

! 2E

Why? every 1 edge connects 2 vertices 5 1 3 2 4 vertex 1 2 3 4 5 degree 4 4 3 3 2

sum = 4+4+3+3+2 = 16 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)
vV

! 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
vVe vVo

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

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
b

5 4 6 c 3

2 1 d

v5 e1 v4 e3 v3 e6 v2

e2

e4 e5

v1

5 1 2 3 4

6 a4

a1

a2

a3

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.

e1

v1 e5 v3 e4

e3

v2 v5 e2 e7

v1 e4 v3 e6 v4

e6 B

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 diam (G) B b E e D A C F H a d c f h

Cutpoints & Bridges


G be a connected graph v - cutpoint if G-v is disconnected B e - bridge if G-e is disconnected b E D A c H a d C F 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 B A C B C X

X 1

Y A

2 B

Y C

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
1 2 3 4 5

(if a graph has an Eulerian path, 8 then you can draw it without lifting your pencil)
7 6 5 1 3 2 4 1 3 5 2 4

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

A graph is connected if there is a path between every pair of distinct vertices. Euler graph is always connected. 5 1 3 2 4 1 3 5 2 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 A traversable. B B A A

D C

D 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 p 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 2 3 4 7 5 6 1 3 5 2 4 1 3 5 2 4 8

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

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 non- Eulerian

Eulerian and 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) = eG ' w(e) To optimise a connected graph find the graph with the minimum weight (shortest path)

A1 3 3 P 4 A4 2 2

A2 6

A3 4

7 3

2 1 Q 2

4 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 y2 x2

y1 x1
Which Redrawn

x2

x3

x4

y3 x3 y4 x4

as:

y1

y2

y3

y4

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. C B P X A Z D Y Q

Star
A tree is a star if only 1 node has degree >1
B Y X A Z C P Q

Chain
A chain is a tree with no nodes of degree >2
B Y C P X A Z D Q

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 6 3 4 2

4 2

T1

1 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

Prims Minimal Spanning Tree Algorithm

Kruskals Minimal Spanning Tree Algorithm

e.g. Find the minimum spanning tree T for the weighted graph G A 2 D 3 G 2 3 3 2 2 B 1 E 1 H 1 3 1 1 1 C 2 F 3 I

A 2 D 3 G

B 1 E 1 H

F 3 I

e.g. Find the minimum spanning tree T for the weighted graph G A 8 7 C 6 7 4 E F 4 7 5 3 D B

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

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

d non-planar drawing of a planar graph

c planar drawing 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 A B r2 C F r1 r3 D r4

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 6 a R8 b R2 R4 R5 R3 c R7 R1 g h d f deg(R5)=4,deg(R6)=3 deg(R1)=5,deg(R2)=3 deg(R7)=5,deg(R8)=6 deg(R3)=3,deg(R4)=7 abghgfda
i !1

deg( Ri ) ! 18 ! deg( Ri ) ! 2 v 9 ! 2 | E |
i! 5

deg(r1) = 3, deg(r2) = 3, deg(r3) = 5, deg(r4) = 4, deg(r5) = 3 r5 A B r2 C F r1 r3 D r4

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: ve+r=2 number of regions

number of vertices

number of edges

Planar Graphs
Example of Eulers theorem
A planar graph divides the plane into several regions (faces), one R4 of them is the infinite region. v=4,e=6,r=4, v-e+r=2

K4

R1 R2 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 r 2 2 So 6 or e 3r. (since each region has a degree of at least 3) (2/3) e e+r

From Euler s theorem, 2 = v v v e + 2e/3 e/3 3v 3v e 6

Corollary 2: Let G = (V, E) be a connected simple planar graph then G has atleast one vertex

with degree e 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 u 6V, E u 3V > 3V - 6 p 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 Eulers 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 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 (G ) - minimum no of color needed to paint G. G (k n ) = n

G (G ) ! 3
2 a f 1 g 3 c 1 2d 2 b h 3

1e

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 c1 r1 c1 c3

r3

r4 c2

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 c d f b e

Vertex a b c d e f

Vertices b, c a, c, e, f a, b, f b c, b

Directed Adjacency Lists


1 row per node, listing the terminal nodes of each edge incident from that node.
0 1

node 0 1 2
2 3

Terminal nodes 3 0, 2, 4 1

3
4

0,2

Adjacency Matrix
y There is an N x N matrix, where |V| = N , the Adjacency Matrix (NxN) A = [aij] For undirected graph
a ij 1 if {vi, vj} is an edge of G ! 0 otherwise

y 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

d c

0 b 1 c 1 d 1
a

1 1 1 0 1 0 1 0 0 0 0 0

Adjacency Matrix
Example: directed Graph G (V, E)
v u 1 0 0 w 0 1 0

u
v u 0 0 1

w
w

Incidence Matrix
y 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 e3 e1 e5 v3 v2 e2 v4 e4

e1 e2 e3 e4 e5 v1 v2 v3 v4

1 0 1 0

1 1 0 0 0 1 1 0 0 0 0 1 1 0 1 1

write the adjacency list of the following graph

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 V is a set of edges (or arcs) a e d c V = {a,b,c,d,e} E = { (b,a), (c,b), (d,c), (e,d), (e,b), (b,e), (e,a) } b in-degree = 2 out-degree = 2

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 e2 e3 B e7 e1 e4 D e6 C

e5

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 e4 e2 e3 e6 B e7 e5 C

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 D x y b a e4 e2 e3 e6 d c w z e7 B e5 C

e.g. Find two simple paths from v1 to v6. Find all cycles in G which include v3. Is G unilaterally connected ? Strongly connected? v2 v1

v3

v4

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:

2 1 3 4

8 7 5 12 6

9 10

11

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 2 1 3 4 Unrooted tree Tree rooted with root 1 6 8 7 5 12 11 9 10 5 4 6 11 3 2 8 7 9 10 12

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 7 3 2 A node that has no children is called a leaf 8 9 5 The descendents of a node 10 4 6 are: itself, its children, 11 12 their children, all the way down The ancestors of a node are: itself, its parent, its grandparent, all the way to the root

level 0 level 1

root b

parent of c parent of b

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 2 a rooted tree is the depth 3 of the lowest leaf 5 8 Depth of node 10: 3 4 6 Depth of this tree: 4
Terminal vertex - zero outdegree Internal vertex - positive outdegree
11

7 9 10 12

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

g subtree with b as its 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 1.1 1.2 1.2.2 1.2.2.1 3 2 2.1 3.1.1 3.2.1.1 1.2.1 3.1 3.2 3.2.1

3.2.2

Lexicographic order 0 1 1.1 1.2 1.2.1 1.2.2 1.2.2.1 2 2.1 3 3.1 3.1.1 3.2 3.2.1 3.2.1.1 3.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.

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)

Defn

Left subtree

Right subtree

Binary Trees
Example:
1 3 5 4 6 11 Non binary tree 2 8 7 9 10 12 4 5 6 11 Binary tree 3 8 1 7 9 10 12

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 2 6 a right child, a left child only, a right child only, 3 7 8 or no children 9 5 The tree made up of a left 4 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

Preorder: b e visit the root of the tree. recursively traverse left subtree. c d recursively traverse right subtree. abcedfg g In order: recursively traverse left subtree. bcadegf visit the root. recursively traverse right subtree. Postorder: recursively traverse left subtree. recursively traverse right subtree. visit the root. cbdgfea

Rooted Trees
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
Inorder Traversal Algorithm:
9

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.

7 1 5 3 2 4 6 12 8 10

11 15 13 16 14

Rooted Trees
Postorder Traversal Algorithm:
16

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

8 6 5 3 1 2 4 10 7 9

15 14 12 13 11

Preorder: + - 2 * 4 + 1 3 In order: -2 + 4 * 1 + 3 *

2 4

Postorder: 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)

* o + x 3 * y 4 a 2 _ * b

Digraphs and Relations, Adjacency Matrix


A ! [aij ] mvm 1 if there is an edge (vi , v j ) defined as a ij ! 0 otherwise a ij ! 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 1 1 1 0 0 0 0 0 1 0 1 1 1 1 0

A=

1 2 1 1

0 0 0 0

1 1 1 0

0 2 1 2

A !

1 3 2 2

0 0 0 0

0 2 1 2

2 3 2 1

A !

2 5 3 3

0 0 0 0

2 3 2 1

1 5 3 4

We will use the notation: ak(i,j)=the ij entry in the matrix Ak Note that a1(i,j)=aij gives the number of 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.

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 2       A m . Then the path matrix P and Bm have the same nonzero entries.

Given A find B4 and P A= Ans: B4 = 0 1 1 1 0 0 0 0 3 7 4 4 0 1 0 1 4 11 7 7 1 1 1 0 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1

4 0 11 0 7 0 7 0

P=

SHORTEST PATH
WARSHALLS ALGORITHM
Define m - square Boolean matrices P0 , P ,.........., Pm . 1 Let Pk [i, j ] denote the ijth entry of the matrix Pk .

1 Pk [i, j ] ! 0

if there is a simple path from vi to v j which does not use any other vertices except possibly v1 , v2 ,......., vk otherwise

WARSHALLS ALGORITHM
i.e. P0 [i, j ] ! 1 if there is an edge from vi to v j P [i, j ] ! 1 if there is a simple path from vi to v j which 1 does not use any other vertex except possibly v1 P2 [i, j ] ! 1 if there is a simple path from vi to v j which does not use any other vertex 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.

Y V={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?

Find the path matrix P of G using Warshalls 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 0 P0 ! 0 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0 P1 ! 0 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0 P2 ! 0 0 1 1 1 0 0 1 1 0 1 0 1 0

0 0 P3 ! 0 0

1 1 1 0 0 1 1 0 1 1 1 1

0 0 P4 ! 0 0

1 1 1 1 1 1 1 1 1 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 g. The final matrix Q m will be the desired matrix Q.

7 4 R U

2 7 1

7 7 W ! 0 4

5 0 3 0

0 0 0 1

0 2 0 0

S 3 V={R,S,T.U}

7 5 g g 7 g g 2 Q0 ! g 3 g g 4 g 1 g

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 (g,4  5) ! 9
Q2 [1,3] ! MIN (Q1[1,3], Q1 [1,2]  Q1 [2,3]) ! MIN (g,5  g) ! g 7 5 g g 7 5 g 7 7 12 g 2 7 12 g 2 Q1 ! Q2 ! g 3 g g 10 3 g 5 4 9 1 g 4 9 1 11

7 5 g 7 7 12 g 2 Q3 ! 10 3 g 5 4 4 1 6

7 5 8 7 7 11 3 2 Q4 ! 9 3 6 5 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 Warshalls Algorithm

Вам также может понравиться