Download presentation
Presentation is loading. Please wait.
Published byTracy Kelley Modified over 9 years ago
1
Trees Dr. Yasir Ali
2
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.
3
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).
4
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 vw 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 region are descendants of u, u is an ancestor of each.
6
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. uw vx Root v is the left child of u. x is the right child of w. Left sub tree of w Right sub tree of w.
7
Spanning Trees A 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.
9
Minimum Spanning Trees A 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.
10
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.
11
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.
12
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.
13
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.
14
Kruskal’s Algorithm-Working
28
Prim’s Algorithm- Working - - - - -
29
-14-2 1- 4- -- 2-
30
-14-2 1- 4- -- 2- d ab c e
31
-14-2 1--33 4-- -3- 23- d ab c e
32
-14-2 1--33 4-- -3- 23- d ab c e
33
-14-2 1--33 4--3 -3-2 2332- d ab c e
34
-14-2 1--33 4--3 -3-2 2332- d ab c e
35
-14-2 1--33 4--33 -33-2 2332- d ab c e
36
-14-2 1--33 4--13 -31-2 2332- d ab c e
37
-14-2 1--33 4--13 -31-2 2332- d ab c e 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.