Upcoming SlideShare
×

Trees ayaz

655 views

Published on

Prim algorithm, kruskal algorithm, trees

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
655
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
12
0
Likes
0
Embeds 0
No embeds

No notes for slide

Trees ayaz

1. 1. TreesBirla Institute of Technology
2. 2. Trees A very important type of graph in CS is called a tree: Real Tree transformationL23 2
3. 3. Trees – Their Definition Let A be a set and let T be a relation on A. We say that T is a tree if there is a vertex v0 with the property that there exists a unique path in T from v0 to every other vertex in A, but no path from v0 to v0.
4. 4. Trees – Our Definition We need a way to describe a tree, specifically a “rooted” tree.  First, a rooted tree has a single root, v0, which is a vertex with absolutely no edges coming into it. (in-degree of v0 = 0)  Every other vertex, v, in the tree has exactly one path to it from v0. (in-degree of v = 1)  There may be any number of paths coming out from any vertex.  Denoted (T,v0) is a rooted tree
5. 5. Definitions Levels – all of the vertices located n- edges from v0 are said to be at level n. Level 0 Level 1 Level 2 Level 3
6. 6. More Definitions A vertex, v, is considered the parent of all of the vertices connected to it by edges leaving v. A vertex, v, is considered the offspring of the vertex connected to the single edge entering v. A vertex, v, is considered the sibling of all vertices at the same level with the same parent.
7. 7. More Definitions A vertex v2 is considered a descendant of a vertex v1 if there is a path from v1 to v2. The height of a tree is the number of the largest level. The vertices of a tree that have no offspring are considered leaves. If the vertices of a level of a tree can be ordered from left to right, then the tree is an ordered tree.
8. 8. More Definitions If every vertex of a tree has at most n offspring, then the tree is considered an n-tree. If every vertex of a tree with offspring has exactly n offspring, then the tree is considered a complete n-tree. When n=2, this is called a binary tree.
9. 9. Giving Meaning toVertices and Edges Trees implied that a vertex is simply an entity with parents and offspring much like a family tree. What if the position of a vertex relative to its siblings or the vertex itself represented an operation. Examples:  Edges from a vertex represent cases from a switch statement in software  Vertex represented a mathematical
10. 10. Mathematical Order ofPrecedence Represented withTrees Consider the equation: (3 – (2  x)) + ((x – 2) – (3 + x)) Each element is combined with another using an operator, i.e., this expression can be broken down into a hierarchy of (a  b) where “” represents an operation used to combine two elements. We can use a binary tree to represent this equation with the elements as the leaves.
11. 11. Precedence Example Tree + – – 3  – + 2 x x 2 3 x
12. 12. Positional Tree A positional tree is an n-tree that relates the direction/angle an edge comes out of a vertex to a characteristic of that vertex. For example: Yes Maybe Left Right X=0 X=2 No X=1 When n=2, then we have a positional binary tree.
13. 13. Tree to Convert Base-2 to Base- 10 Starting with the first digit, take the left or right edge to follow the path to the base-10 value. First digit  0 1Second digit  0 1 0 13rddigit  0 1 0 1 0 1 0 14th 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
14. 14. For-Loop Represented with Treefor i = 1 to 3 for j = 1 to 5 array[i,j] = 10*i + j next jnext i
15. 15. For Loop Positional Tree i=1 i=3 i=2 j=1 j=2 j=3 j=4 j=5 11 12 13 14 15 21 22 23 24 25 31 32 33 34 35
16. 16. Storing Binary Trees inComputer “linked lists”. Each item in the list was comprised of two components:  Data  Pointer to next item in list Positional binary trees require two links, one following the right edge and one following the left edge. This is referred to as a “doubly linked list.” Left Pointer Data Right
17. 17. Represent in computer using Linked List + (2) – (3) – (8) 3 (4)  (5) – (9) + (12) 2 (6) x (7) x (10) 2 (11) 3 (13) x (14)The numbers in parenthesis represent the index from whichthey Can be shown in the linked list
18. 18. Doubly Linked List Index Left Data Right 1 2 ------- 0 2 3 + 8 3 4 root – 5 4 0 3 0 5 6  7 6 0 2 0 7 0 x 0 8 9 – 12 9 10 – 11 10 0 x 0 11 0 2 0 12 13 + 14 13 0 3 0 14 0 x 0
19. 19. Huffman Code Depending on the frequency of the letters occurring in a string, the Huffman Code assigns patterns of varying lengths of 1’s and 0’s to different letters. These patterns are based on the paths taken in a binary tree. A Huffman Code Generator can be found at: http://www.inf.puc- rio.br/~sardinha/Huffman/Huffman.html
20. 20. Searching Trees
21. 21. Terminology “Visiting” a vertex – the act of performing a task at a vertex, e.g., perform a computation or make a decision. “Searching” the tree – the process of visiting each vertex in a specific order or path. The term “searching” can be misleading. Just think of it as “traversing” the tree.
22. 22. Tree Search The application of some trees involves traversing the tree in a methodical pattern so as to address every vertex. Our book uses the term “search”, but sometimes search can imply we’re looking for a particular vertex. This is not the case. Example: Assume we want to compute the average age, maximum age, and minimum age of all of the children from five families. (Tree is on next slide.)
23. 23. Search Example Neighborhood families A. Jones Halls Smith Taylor B. JonesKaty Tommy Taylor Lori Karen Mikeage 3 age 5 age 1 age 4 age 14 age 6 Phil Lexi Bart Ben age 8 age 2 age 12 age 2
24. 24. Search Example (continued) To calculate the average, max, and min ages for all of the children, we need to have a method for going through the tree so that we don’t miss a child. By defining a rigorous process, not only can a human be sure not to miss a vertex, but also an algorithm can be defined for a computer
25. 25. A Suggested Process forSearching a Tree1.Starting at the root, repeatedly take the leftmost “untraveled” edge until you arrive at a leaf which should be a child.2.Include this child in the average, max, and min calculations.3.One at a time, go back up the edges until you reach a vertex that hasn’t had all of its outgoing edges traveled.4.If you get back to the root and cannot find an untraveled edge, you are done. Otherwise, return to step 1.
26. 26. Vertices Numbered in Order of Visits 1 Neighborhood families 2 5 7 14 11 A. Jones Halls Smith Taylor B. Jones 3 4 8 10 13 16Katy Tommy Taylor Lori Karen Mikeage 3 age 5 age 1 age 4 age 14 age 6 6 9 12 15 Phil Lexi Bart Ben age 8 age 2 age 12 age 2
27. 27. Preorder Search This methodical pattern of traversing a tree is called a preorder search. Assume v is the root of a binary positional tree T.  Each vertex of this tree has at most a left vertex, vL, and a right vertex, vR.  If either vL or vR have offspring, then they are subtrees of T, and a search can be performed of them too.  By viewing a tree this way, then the search method we described in earlier slides can be performed using a recursive algorithm applied to each vertex.  The recursive algorithm is repeatedly applied until every leaf has been reached.
28. 28. Preorder Search Algorithm A preorder search of a tree has the following three steps: 1. Visit the root 2. Search the left subtree if it exists 3. Search the right subtree if it exists The term “search” in steps 2 and 3 implies that we apply all three steps to the subtree beginning with step 1.
29. 29. Vertices Visited in AlphabeticalOrder Using Preorder Search A B H C E I KD F G J L
30. 30. Prefix or Polish Form Binary tree representing: (a – b) × (c + (d ÷ e)) x – + a b c ÷ d e Preorder search produces: × – a b + c ÷ d e
31. 31. Polish Form (continued) Allows us to write complex arithmetic expressions without using parenthesis Expression is evaluated by performing following steps:  Move left to right until you find a string of the form Fxy, where F is the symbol for a binary operation and x and y are numbers.  Evaluate x F y and substitute answer for string Fxy.  Repeat starting at beginning of string again.
32. 32. Polish Form Example1. ×– 6 4 + 5 ÷ 2 2 1st pattern: – 6 42. ×2 +5÷22 2nd pattern: ÷ 2 23. ×2 +51 3rd pattern: + 5 14. ×2 6 4th pattern: × 2 65. 12 (6 – 4) × (5 + (2 ÷ 2)) = 12
33. 33. Inorder and PostorderSearches Preorder search gets its name from the fact that the operator that joins two items is evaluated first, e.g., the binary operation 6 – 4 is visited in the order – 6 4. Inorder search evaluates the expression as it is written, e.g., the binary operation 6 – 4 is visited in the order 6 – 4. Postorder search evaluates the operator after the elements are read, e.g., the binary operation 6 – 4 is visited in the order 6 4 –.
34. 34. Inorder Search Algorithm An inorder search of a tree has the following three steps: 1. Search the left subtree if it exists 2. Visit the root 3. Search the right subtree if it exists
35. 35. Postorder Search Algorithm A postorder search of a tree has the following three steps: 1. Search the left subtree if it exists 2. Search the right subtree if it exists 3. Visit the root
36. 36. Evaluation of Tree UsingInorder Search A B H C E I KD F G J L Resulting string: DCBFEGAIJHKL
37. 37. Evaluation of Tree UsingPostorder Search A B H C E I KD F G J L Resulting string: DCFGEBJILKHA
38. 38. Infix Formrepresenting: (a – b) × (c + (d ÷ e)) Binary tree x – + a b c ÷ d e Inorder search produces: a – b × c + d ÷ e Unfortunately, without parenthesis, we can’t do anything with this expression.
39. 39. Postfix or Reverse Polish Form Binary tree representing: (a – b) × (c + (d ÷ e)) x – + a b c ÷ d e Inorder search produces: a b – c d e ÷ + ×
40. 40. Reverse Polish Form (continued) Allows us to write complex arithmetic expressions without using parenthesis Expression is evaluated by performing following steps:  Move left to right until you find a string of the form xyF, where F is the symbol for a binary operation and x and y are numbers.  Evaluate x F y and substitute answer for string xyF.  Repeat starting at beginning of string again.
41. 41. Reverse Polish Form ExampleFrom left-to-right evaluate xyF first.1. 2 1 – 3 4 2 ÷ + × 1st pattern: 2 1 –2. 1 3 4 2 ÷ + × 2nd pattern: 4 2 ÷3. 1 3 2 + × 3rd pattern: 3 2 +4. 1 5 × 4th pattern: 1 5 ×5. 5 (2 – 1) × (3 + (4 ÷ 2)) = 5
42. 42. Converting an Orderedn-tree to a Positional Binary Tree An ordered n-tree where some vertices have more than two offspring can be converted to a positional binary tree. This allows easier computer representation with methods such as linked lists. A process exists for this conversion that works on any finite tree.
43. 43. Process to Convert Ordered n-tree to Positional Binary Tree A Starting at the root, the first or leftmost offspring of a vertex remains the leftmost vertex in B C D E the binary tree The first sibling to the right of the leftmost vertex A becomes the right offspring of the leftmost vertex B C Subsequent siblings become the right offspring D in succession until last sibling is converted. E
44. 44. Conversion Example A B C D F G H I E J K L
45. 45. Conversion Example A A B B C D E C F G D F G H IE H J K I J K L L Preorder search: Left tree – ABEFCGJKLHID Right tree – ABEFCGJKLHID
46. 46. Lets see the City 1 City 2 Mileage problem. Cleveland Philadelp 400 hi Cleveland Detroit 200 A small startup airline wants to provide service to the 5 Cleveland Chicago 350 cities in the table to the right. Cleveland Pittsburg 150 Allowing for multiple connecting flights, determine Philadelphi Detroit 600 all of the direct flights that Philadelphi Chicago 700 would be needed in order to service all five cities. Philadelphi Pittsburg 300 Detroit Chicago 300Source: http://www.usembassy Detroit Pittsburg 300malaysia.org.my/distance.html Chicago Pittsburg 450
47. 47. Minimal Spanning Trees
48. 48. Undirected Tree An undirected tree is simply the symmetric closure of a tree. It is relation that results from a tree where all edge are made bidirectional, i.e., there is no defined direction.L23 48
49. 49. Connected Relation A relation is connected if for every a and b in R, there is a path from a to b. It is easier to see a connected relation using a digraph than it is to describe in using words. A A C C B B E E D DConnected Not Connected
50. 50. Spanning Tree of connectedrelations Textbook definition: “If R is a symmetric, connected relation on a set A, we say that a tree T on A is a spanning tree for R if T is a tree with exactly the same vertices as R and which can be obtained from R by deleting some edges of R.” Basically, a undirected spanning tree is one that connects all n elements of A with n-1 edges. To make a cycle connecting n elements, more than n-1 edges will be needed. Therefore, there are no cycles.
51. 51. Weighted Graph In the past, we have represented a undirected graph with unlabeled edges. It can also be represented with a symmetric binary matrix. A CMT = 0 1 1 0 1 0 1 0 B 1 1 0 1 D 0 0 1 0
52. 52. Weighted Graph (continued) By giving the edges a numeric value indicating some parameter in the relation between two vertices, we can create a weighted tree. A 3 C 5 4 7 B D
53. 53. Weighted Graph (continued) We can still use matrix notation to represent a weighted graph. Replace the 1’s used to represent an edge with the edge’s weight. A 0 indicates no edge. 0 5 3 0 MT = 5 0 4 0 3 4 0 7 0 0 7 0
54. 54. Exercise(Not drawn to scale) Detroit 300 600 200 300 Cleveland 400 350 Philadelphia 700 150 300 Chicago 450 Pittsburg Note R relation has 5 vertices .
55. 55. Minimal Spanning Tree Assume T represents a spanning tree for an undirected graph. The total weight of the spanning tree T is the sum of all of the weights of all of the edges of T. The one(s) with the minimum total weight are called the minimal spanning tree(s). As suggested by the “(s)” in the above definition, there may be a number of minimal spanning trees for a particular undirected graph with the same total weight.
56. 56. Nearest neighbour of a vertex A vertex u is nearest neighbour of a vertex v if u and v are adjacent and no other vertex is joined to v by an edge of lesser weight.L23 56
57. 57. Algorithms for Determining theMinimal Spanning Tree There are two algorithms presented in our textbook for determining the minimal spanning tree of an undirected graph that is connected and weighted.  Prim’s Algorithm: process of stepping from vertex to vertex  Kruskal’s Algoritm: searching through edges for minimum weights
58. 58. Prim’s Algorithm Let R be a symmetric, connected relation with n vertices. 1. Choose a vertex v1 of R. Let V = {v1} and E = { }. 2. Choose a nearest neighbor vi of V that is adjacent to vj, vj  V, and for which the edge (vi, vj) does not form a cycle with members of E. Add vi to V and add (vi, vj) to E. 3. Repeat Step 2 until |E| = n – 1. Then V contains all n vertices of R, and E contains the edges of a minimal spanning tree for R.
59. 59. Prim’s Algorithm in English The goal is to one at a time include a new vertex by adding a new edge without creating a cycle Pick any vertex to start. From it, pick the edge with the lowest weight. As you add vertices, you will add possible edges to follow to new vertices. Pick the edge with the lowest weight to go to a new vertex without creating a cycle.
60. 60. Kruskal’s Algorithm Let R be a symmetric, connected relation with n vertices and let S = {e1, e2, e3, …ek} be the set of all weighted edges of R. 1. Choose an edge e1 in S of least weight. Let E = {e1}. Replace S with S – {e1}. 2. Select an edge ei in S of least weight that will not make a cycle with members of E. Replace E with E  {ei} and S with S – {ei}. 3. Repeat Step 2 until |E| = n – 1.
61. 61. Kruskal’s Algorithm in English The goal is to one at a time include a new edge without creating a cycle. Start by picking the edge with the lowest weight. Continue to pick new edges without creating a cycle. Edges do not necessarily have to be connected. Stop when you have n-1 edges as R have n vertices.
62. 62. Answer: Kruskal to Find MST (Not drawn to scale) B D 2 H 3 2 5 E A C 2 3 4 F GSequence of edge selections (D,E), (D,H)(A,C)(A,B)(E,G)(E,F)(C,E)=2+2+2+3+3+4+5=21,