trees dr. yasir ali. a graph is called a tree if, and only if, it is circuit-free and connected. a...

37
Trees Dr. Yasir Ali

Upload: tracy-kelley

Post on 08-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

Internal and Terminal vertices Let T be a tree. If T has only one or two vertices, then each is called a terminal vertex. If T has at least three vertices, then a vertex of degree 1 in T is called a terminal vertex (or a leaf ), and a vertex of degree greater than 1 in T is called an internal vertex (or a branch vertex).

TRANSCRIPT

Page 1: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

TreesDr. Yasir Ali

Page 2: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

A graph is called a tree if, and only if, it is circuit-free and connected.

A graph is called a forest if, and only if, it is circuit-free and not connected.

Page 3: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

Internal and Terminal verticesLet T be a tree. If T has only one or two vertices, then each is called a terminal vertex. If T has at least three vertices, then a vertex of degree 1 in T is called a terminal vertex (or a leaf ), and a vertex of degree greater than 1 in T is called an internal vertex (or a branch vertex).

Page 4: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

Rooted Tree• A rooted tree is a tree in which there is one

vertex that is distinguished from the others and is called the root.

• The level of a vertex is the number of edges along the unique path between it and the root.

• The height of a rooted tree is the maximum level of any vertex of the tree. Given the root or any internal vertex v of a rooted tree,

• The children of v are all those vertices that are adjacent to v and are one level farther away from the root than v.

• If w is a child of v, then v is called the parent of w, and

• Two distinct vertices that are both children of the same parent are called siblings.

• Given two distinct vertices v and w, if v lies on the unique path between w and the root, then v is an ancestor of w and w is a descendant of v.

u

v w

root

Level 0

Level 1

Level 2

Level 3

Level 4

v is a child of u.u is the parent of v.v and w are siblings.

Vertices in the enclosed regionare descendants of u, u is an ancestor of each.

Page 5: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

Consider the tree with root shown below.a. What is the level of? b. What is the level of ?c. What is the height of this rooted tree? d. What are the children of ?e. What is the parent of ?f. What are the siblings of?g. What are the descendants of ?

Page 6: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

Binary Tree• A binary tree is a rooted tree in which

every parent has at most two children.• Each child in a binary tree is

designated either a left child or a right child (but not both),

• Every parent has at most one left child and one right child.

• A full binary tree is a binary tree in which each parent has exactly two children.

• Given any parent v in a binary tree T , if v has a left child, then the left sub tree of v is the binary tree whose root is the left child of v, whose vertices consist of the left child of v and all its descendants, and whose edges consist of all those edges of T that connect the vertices of the left sub tree.

• The right sub tree of v is defined analogously.

u w

v x

Root

v is the leftchild of u.

x is the rightchild of w.

Left sub tree of w

Right sub tree of w.

Page 7: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

Spanning TreesA spanning tree for a graph G is a subgraph of G that contains every vertex of G and is a tree.

• Every connected graph has a spanning tree.• Any two spanning trees for a graph have the same number of

edges. Find all spanning trees for the graph G pictured below.

Page 8: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free
Page 9: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

Minimum Spanning TreesA minimum spanning tree for a connected weighted graph is a spanning tree that has the least possible total weight compared to all other spanning trees for the graph.

Page 10: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

Minimum Spanning Trees - Applications

• Network design. – telephone, electrical, hydraulic, TV cable, computer, road.

• You want to lease phone lines to connect different offices • Phone company charges different amounts of money to

connect different pairs of cities. • You want a set of lines that connects all your offices with a

minimum total cost.

Page 11: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

Algorithms for finding minimum spanning Trees

In 1956 and 1957 Joseph B. Kruskal and Robert C. Prim each described much more efficient algorithms to construct minimum spanning trees. Even for large graphs, both algorithms can be implemented so as to take relatively short computing times.

Page 12: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

Kruskal’s Algorithm-Main Idea

In Kruskal’s algorithm, the edges of a connected weighted graph are examined one by one in order of increasing weight. At each stage the edge being examined is added to what will become the minimum spanning tree, provided that this addition does not create a circuit.

Page 13: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

Kruskal’s Algorithm-Steps

The steps are: The forest is constructed - with each node in a separate tree.The edges are placed in a priority queue. Until we've added n-1 edges,

1. Extract the cheapest edge from the queue, 2. If it forms a cycle, reject it, 3. Else add it to the forest. Adding it to the forest will join two

trees together. Every step will have joined two trees in the forest together, so that at the end, there will only be one tree in T.

Page 14: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

Kruskal’s Algorithm-Working

Page 15: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free
Page 16: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free
Page 17: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free
Page 18: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free
Page 19: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free
Page 20: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free
Page 21: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free
Page 22: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free
Page 23: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free
Page 24: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free
Page 25: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free
Page 26: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free
Page 27: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free
Page 28: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

Prim’s Algorithm- Working

-

-

-

-

-

Page 29: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

Prim’s Algorithm- Working

- 1 4 - 2

1 -

4 -

- -

2 -

We can start from any vertex, here we start from . so, write the weights of the edges incident on .

Page 30: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

Prim’s Algorithm- Working

- 1 4 - 2

1 -

4 -

- -

2 -

1 is minimum, so we select the edge with weight 1

d

a b

c

e𝑎𝑏

Page 31: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

Prim’s Algorithm- Working

- 1 4 - 2

1 - - 3 3

4 - -

- 3 -

2 3 -

d

a b

c

e𝑎𝑏

Now we write the weights of edges from , and select the minimum weight from the table

Page 32: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

Prim’s Algorithm- Working

- 1 4 - 2

1 - - 3 3

4 - -

- 3 -

2 3 -

d

a b

c

e𝑎𝑏+𝑎𝑒

2 is the minimum weight of the edge , and it will not create any circuit.

Page 33: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

Prim’s Algorithm- Working

- 1 4 - 2

1 - - 3 3

4 - - 3

- 3 - 2

2 3 3 2 -

d

a b

c

e𝑎𝑏+𝑎𝑒

Now write the weights of edges from

Page 34: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

Prim’s Algorithm- Working

- 1 4 - 2

1 - - 3 3

4 - - 3

- 3 - 2

2 3 3 2 -

d

a b

c

e𝑎𝑏+𝑎𝑒+de

2 is the minimum weight of the edge , and no circuit will form if we add this edge.

Page 35: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

Prim’s Algorithm- Working

- 1 4 - 2

1 - - 3 3

4 - - 3 3

- 3 3 - 2

2 3 3 2 -

d

a b

c

e𝑎𝑏+𝑎𝑒+de

We now write the weights from

Page 36: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

Prim’s Algorithm- Working

- 1 4 - 2

1 - - 3 3

4 - - 1 3

- 3 1 - 2

2 3 3 2 -

d

a b

c

e

𝑎𝑏+𝑎𝑒+de+cd

1 is the minimum weight of

Note: if at any step more than one edges have same weights then we may select any of the edge so that it will not create circuit in the graph.

Page 37: Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free

Prim’s Algorithm- Working

- 1 4 - 2

1 - - 3 3

4 - - 1 3

- 3 1 - 2

2 3 3 2 -

d

a b

c

e

𝑎𝑏+𝑎𝑒+de+cd

This is the minimum spanning Tree with weight 7. The algorithm terminates as all the vertices are marked in the table and we have four edges which is one less than the number of vertices.