trees dr. yasir ali. a graph is called a tree if, and only if, it is circuit-free and connected. a...
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
TreesDr. 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 and not connected.
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).
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.
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 ?
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.
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.
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.
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.
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.
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.
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.
Kruskal’s Algorithm-Working
Prim’s Algorithm- Working
-
-
-
-
-
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 .
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𝑎𝑏
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
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.
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
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.
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
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.
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.