trees

44
1 NOR AIDAWATI BINTI ABDILLAH TREES Discrete Mathematics

Upload: noraidawati

Post on 05-Dec-2014

744 views

Category:

Education


6 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Trees

1

NOR AIDAWATI BINTI ABDILLAH

TREES

Discrete Mathematics

Page 2: Trees

2

Definition 1. A tree is a connected

undirected graph with no simple circuits.

Theorem 1. An undirected graph is a tree if

and only if there is a unique simple path

between any two of its vertices.

Tree

Page 3: Trees

3

Which graphs are trees?

a) b)

c)

Page 4: Trees

4

yes!

NO!

All the nodes are not connected

NO!

There is a cycle and an extra

edge (5 nodes and 5 edges)

yes! (it’s actually the same graph as the blue one) – but

usually we draw tree by its “levels”

Is it a tree?

yes! (but not a binary tree)

Page 5: Trees

5

Rooted Trees

Rooted tree is a tree in which one vertex is distinguished and called a root

Level of a vertex is the number of edges between the vertex and the root

The height of a rooted tree is the maximum level of any vertex

Children, siblings and parent vertices in a rooted tree

Ancestor, descendant relationship between vertices

Page 6: Trees

6

The parent of a non-root vertex is the unique vertex u with a directed

edge from u to v.

A vertex is called a leaf if it has no children.

The ancestors of a non-root vertex are all the vertices in the path from

root to this vertex.

The descendants of vertex v are all the vertices that have v as an

ancestor.

The level of vertex v in a rooted tree is the length of the unique path

from the root to v.

The height of a rooted tree is the maximum of the levels of its vertices.

Page 7: Trees

7

a

b c

d e f g

h i

root node

parent of g

siblingsleaf

internal vertex

A Tree Has a Root

Page 8: Trees

a

b c

d e f g

h iancestors of h and i

Page 9: 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 3

Page 10: Trees

a

b c

d e f g

h isubtree with b as its root

subtree with c as its root

Page 11: Trees

Tree Properties

There is one and only one path between every pair of

vertices in a tree, T. 

A tree with n vertices has n-1 edges. 

Any connected graph with n vertices and n-1 edges is a

tree. 

A graph is a tree if and only if it is minimally connected. 

11

Page 12: Trees

12

Theorem . There are at most 2 H leaves in a

binary tree of height H.

Corallary. If a binary tree with L leaves is

full and balanced, then its height is

H = log2 L .

Tree Properties

Page 13: Trees

Properties of Trees

There are at most mh leaves in an m-ary tree of height h.

A rooted m-ary tree of height h is called balanced if all leaves are at levels h or h-1.

Page 14: Trees

Properties of Trees

A full m-ary tree with

(i) n vertices has i = (n-1)/m internal vertices and l = [(m-1)n+1]/m leaves.

(ii) i internal vertices has n = mi + 1 vertices and l = (m-1)i + 1 leaves.

(iii) l leaves has n = (ml - 1)/(m-1) vertices and i = (l-1)/(m-1) internal vertices.

Page 15: Trees

Properties of Trees

A full m-ary tree with i internal vertices contains n = mi+1 vertices.

Page 16: Trees

Proof

We know n = mi+1 (previous theorem) and n = l+i,

n – no. vertices i – no. internal vertices l – no. leaves For example, i = (n-1)/m

Page 17: Trees

17

A rooted tree in which each vertex has either no children, one child or two children.

The tree is called a full binary tree if every internal vertex has exactly 2 children.

Binary Tree

A

B C

GD E

left subtree of A

H I J

F

right subtree of C

right child of A

Page 18: Trees

18

Definition 2’’. An ordered rooted tree is a rooted tree where the children of each internal vertex are ordered.

In an ordered binary tree, the two possible children of a vertex are called the left child and the right child, if they exist.

Ordered Binary Tree

Page 19: Trees

19

An Ordered Binary Tree

J

IM

HL

A

B

C

D

E

F G K

Page 20: Trees

20

Is this binary tree balanced?

Hal

Lou

Ken

Joe Ted

Sue Ed

Max

A rooted binary tree of height

H is called balanced if all its

leaves are at levels H or H-1.

Page 21: Trees

21

So the goal in computer programs is to find any stored item efficiently when all stored items are ordered.

A Binary Search Tree can be used to store items in its vertices. It enables efficient searches.

Searching takes time . . .

Page 22: Trees

A special kind of binary tree in which:

1. Each vertex contains a distinct key value,

2. The key values in the tree can be compared using “greater than” and “less than”, and

3. The key value of each vertex in the tree is

less than every key value in its right subtree, and greater than every key value in its left subtree.

A Binary Search Tree (BST) is . . .

Page 23: Trees

23

A special kind of binary tree in which:

1) Each leaf node contains a single operand,

2) Each nonleaf node contains a single binary operator, and

3) The left and right subtrees of an operator node represent subexpressions that must be evaluated before applying the operator at the root of the subtree.

A Binary Expression Tree is . . .

Page 24: Trees

Expression Tree

24

Each node contains an operator or an operand

Operands are stored in leaf nodes

Parentheses are not stored in the tree because the tree structure dictates the order of operand evaluation

Operators in nodes at higher levels are evaluated after operators in nodes at lower levels

(x + y)*((a + b)/c)

Page 25: Trees

25

A Binary Expression Tree

‘*’

‘+’

‘4’

‘3’

‘2’

What value does it have?

( 4 + 2 ) * 3 = 18

Page 26: Trees

26

A Binary Expression Tree

‘*’

‘+’

‘4’

‘3’

‘2’

Infix: ( ( 4 + 2 ) * 3 )

Prefix: * + 4 2 3 evaluate from right

Postfix: 4 2 + 3 * evaluate from left

Page 27: Trees

27

Levels Indicate Precedence

When a binary expression tree is used to represent an expression, the levels of the nodes in the tree indicate their relative precedence of evaluation.

Operations at higher levels of the tree are evaluated later than those below them. The operation at the root is always the last operation performed.

Page 28: Trees

28

Evaluate this binary expression tree

‘*’

‘-’

‘8’ ‘5’

What expressions does it represent?

‘/’

‘+’

‘4’

‘3’

‘2’

Page 29: Trees

29

A binary expression tree

Infix: ( ( 8 - 5 ) * ( ( 4 + 2 ) / 3 ) )

Prefix: * - 8 5 / + 4 2 3 evaluate from right

Postfix: 8 5 - 4 2 + 3 / * evaluate from left

‘*’

‘-’

‘8’ ‘5’

‘/’

‘+’

‘4’

‘3’

‘2’

Page 30: Trees

Binary Tree for Expressions

30

Page 31: Trees

Complete Binary Tree

31

Also be defined as a full binary tree in which all leaves are at depth n or n-1 for some n.

In order for a tree to be the latter kind of complete binary tree, all the children on the last level must occupy the leftmost spots consecutively, with no spot left unoccupied in between any two

A

B C

GE

K

D

J

F

IH ONML

Full binary tree of depth 4

A

B C

GE

I

D

H

F

Complete binary tree

Page 32: Trees

Difference between binary and complete binary tree

32

BINARY TREE ISN'T NECESSARY THAT ALL OF LEAF NODE IN SAME LEVEL BUT

COMPLETE BINARY TREE MUST HAVE ALL LEAF NODE IN SAME LEVEL.

Binary Tree

Complete Binary Tree

Page 33: Trees

A spanning tree of a connected graph G is a sub graph that is a tree and that includes every vertex of G.

A minimum spanning tree of a weighted graph is a spanning tree of least weight (the sum of the weights of all its edges is least among all spanning tree).

Think: “smallest set of edges needed to connect everything together”

33

SPANNING TREES

Page 34: Trees

34

A graph G and three of its spanning tree

We can delete any edge without deleting any vertex (to remove the cycle), but leave the graph connected.

Page 35: Trees

PRIM’S ALGORITHM

Choose any edge with smallest weight, putting it into the spanning tree.

Successively add to the tree edges of minimum weight that are incident to a vertex already in the tree and not forming a simple circuit with those edges already in the tree.

Stop when n – 1 edges have been added.

35

Page 36: Trees

36

4 3

3 51 2

2 4

2 3 1a c db

e g hf

i k lj

4 3 3

3 13

Use Prim’s algorithm to find a minimum spanning tree in the weighted graph below:

EXAMPLE PRIM’S ALGORITHM

Page 37: Trees

SOLUTION

37

3

3 1 2

2

ji

a b

e f

c d

g h

k l

2 1

3 3

13

Choice Edge Weight1 {b, f} 12 {a, b} 23 {f, j} 24 {a, e} 35 {i, j} 36 {f, g} 37 {c, g} 28 {c, d} 19 {g, h} 3

10 {h, l} 311 {k, l} 1

Total: 24

Page 38: Trees

KRUSKAL’S ALGORITHM

Choose an edge in the graph with minimum weight.

Successively add edges with minimum weight that do not form a simple circuit with those edges already chosen.

Stop after n – 1 edges have been selected.

38

Page 39: Trees

39

Pick the cheapest link (edge) available and mark it

Pick the next cheapest link available and mark it again

Continue picking and marking link that does not create the circuit

***Kruskal’s algorithm is efficient and optimal

Kruskal’s Algorithm

Page 40: Trees

40

Use Kruskal’s algorithm to find a minimum spanning tree in the weighted graph below:

EXAMPLE KRUSKAL’S ALGORITHM

4 3

3 51 2

2 4

2 3 1a c db

e g hf

i k lj

4 3 3

3 13

Page 41: Trees

SOLUTION

41

3 1 2

2

2 1

3

3

13 ji

a b c d

g h

k l

e f

3

Choice Edge Weight1 {c, d} 12 {k, l} 13 {b, f} 14 {c, g} 25 {a, b} 26 {f, j} 27 {b, c} 38 {j, k} 39 {g, h} 3

10 {i, j} 311 {a, e} 3

Total: 24

Page 42: Trees

TRAVELLING SALESMAN PROBLEM (TSP)

42

The goal of the Traveling Salesman Problem (TSP) is to find the “cheapest” tour of a select number of “cities” with the following restrictions:

● You must visit each city once and only once● You must return to the original starting point

Page 43: Trees

TSP

43

TSP is similar to these variations of Hamiltonian Circuit problems:

● Find the shortest Hamiltonian cycle in a weighted graph.

● Find the Hamiltonian cycle in a weighted graph with the minimal length of the longest edge. (bottleneck TSP).

A route returning to the beginning is known as a Hamiltonian Circuit

A route not returning to the beginning is known as a Hamiltonian Path

Page 44: Trees

THANK YOU

44