0
Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# 21. Trees and Graphs - C# Fundamentals

15,297

Published on

This presentation discusses the Trees. …

This presentation discusses the Trees.
Trees like data structures. Tree and related terminology.
Traversing through the Tree? What is Balanced Tree? What is Graph data structure?
The website and all video materials are in Bulgarian

Tree-like Data Structures
Trees and Related Terminology
Implementing Trees
Traversing Trees
Balanced Trees
Graphs

C# Programming Fundamentals Course @ Telerik Academy

1 Comment
12 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Views
Total Views
15,297
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
348
1
Likes
12
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. Trees and Graphs Trees, Binary Search Trees, Balanced Trees, GraphsSvetlin NakovManager Technical Trainerhttp://www.nakov.com/Telerik Software Academyhttp://academy.telerik.com/
• 2. Table of Contents1. Tree-like Data Structures2. Trees and Related Terminology3. Implementing Trees4. Traversing Trees5. Balanced Trees6. Graphs 2
• 3. Tree-like Data Structures Trees, Balanced Trees, Graphs, Networks
• 4. Tree-like Data Structures Tree-like data structures are  Branched recursive data structures  Consisting of nodes  Each node can be connected to other nodes Examples of tree-like structures  Trees: binary / other degree, balanced, etc.  Graphs: directed / undirected, weighted, etc.  Networks 4
• 5. Tree-like Data Structures Network 2 Tree 1 Project Manager 6 3 Team De- QA Team Leader signer Leader 4 5Developer Developer Tester 1 3 Tester 1 2 Developer 7 1 2 14 4 21 Graph 11 12 19 31 5
• 6. Trees and Related TerminologyNode, Edge, Root, Children, Parent, Leaf , Binary Search Tree, Balanced Tree
• 7. Trees Tree data structure – terminology  Node, edge, root, child, children, siblings, parent, ancestor, descendant, predecessor, successor, internal node, leaf, depth, height, subtree 17 Depth 0 Height = 3 9 15 Depth 1 14 6 5 8 Depth 2 7
• 8. Binary Trees Binary trees: most widespread form  Each node has at most 2 children Root node Right child 17 Left subtree 9 15 Right child 6 5 8 10 Left child 8
• 9. Binary Search Trees Binary search trees are ordered  For each node x in the tree  All the elements of the left subtree of x are ≤ x  All the elements of the right subtree of x are > x Binary search trees can be balanced  Balanced trees have height of ~ log(x)  Balanced trees have for each node nearly equal number of nodes in its subtrees 9
• 10. Binary Search Trees (2) Example of balanced binary search tree 17 9 19 6 12 25 If the tree is balanced, add / search / delete operations take approximately log(n) steps 10
• 11. Implementing Trees Recursive Tree Data Structure
• 12. Recursive Tree Definition The recursive definition for tree data structure:  A single node is tree  Tree nodes can have zero or multiple children that are also trees Tree node definition in C# public class TreeNode<T> The value contained { in the node private T value; private List<TreeNode<T>> children; … List of child nodes, which } are of the same type 12
• 13. TreeNode<int> Structure List<TreeNode<int>> children int value TreeNode<int> 7 children 19 children 21 children 14 children1 children 31 children 23 children 6 children 12 children 13
• 14. Implementing TreeNode<T>public TreeNode(T value){ this.value = value; this.children = new List<TreeNode<T>>();}public T Value{ get { return this.value; } set { this.value = value; }}public void AddChild(TreeNode<T> child){ child.hasParent = true; this.children.Add(child);}public TreeNode<T> GetChild(int index){ return this.children[index];} 14
• 15. Implementing Tree<T> The class Tree<T> keeps trees root node public class Tree<T> { private TreeNode<T> root; public Tree(T value, params Tree<T>[] children): this(value) { foreach (Tree<T> child in children) Flexible constructor { for building trees this.root.AddChild(child.root); } } public TreeNode<T> Root { get { return this.root; } } } 15
• 16. Building a Tree Constructing tree by nested constructors: Tree<int> tree = new Tree<int>(7, 7 new Tree<int>(19, new Tree<int>(1), new Tree<int>(12), new Tree<int>(31)), 19 21 14 new Tree<int>(21), new Tree<int>(14, new Tree<int>(23), 1 12 31 23 6 new Tree<int>(6)) ); 16
• 17. Tree TraversalsDFS and BFS Traversals
• 18. Tree Traversal Algorithms Traversing a tree means to visit each of its nodes exactly one in particular order  Many traversal algorithms are known  Depth-First Search (DFS)  Visit nodes successors first  Usually implemented by recursion  Breadth-First Search (BFS)  Nearest nodes visited first  Implemented by a queue 18
• 19. Depth-First Search (DFS) Depth-First Search first visits all descendants of given node recursively, finally visits the node itself DFS algorithm pseudo code 9 7 DFS(node) { 4 5 8 for each child c of node 19 21 14 DFS(c); print the current node; } 1 2 3 6 7 1 12 31 23 6 19
• 20. DFS in Action (Step 1) Stack: 7 Output: (empty) 7 19 21 14 1 12 31 23 6 20
• 21. DFS in Action (Step 2) Stack: 7, 19 Output: (empty) 7 19 21 14 1 12 31 23 6 21
• 22. DFS in Action (Step 3) Stack: 7, 19, 1 Output: (empty) 7 19 21 14 1 12 31 23 6 22
• 23. DFS in Action (Step 4) Stack: 7, 19 Output: 1 7 19 21 14 1 12 31 23 6 23
• 24. DFS in Action (Step 5) Stack: 7, 19, 12 Output: 1 7 19 21 14 1 12 31 23 6 24
• 25. DFS in Action (Step 6) Stack: 7, 19 Output: 1, 12 7 19 21 14 1 12 31 23 6 25
• 26. DFS in Action (Step 7) Stack: 7, 19, 31 Output: 1, 12 7 19 21 14 1 12 31 23 6 26
• 27. DFS in Action (Step 8) Stack: 7, 19 Output: 1, 12, 31 7 19 21 14 1 12 31 23 6 27
• 28. DFS in Action (Step 9) Stack: 7 Output: 1, 12, 31, 19 7 19 21 14 1 12 31 23 6 28
• 29. DFS in Action (Step 10) Stack: 7, 21 Output: 1, 12, 31, 19 7 19 21 14 1 12 31 23 6 29
• 30. DFS in Action (Step 11) Stack: 7 Output: 1, 12, 31, 19, 21 7 19 21 14 1 12 31 23 6 30
• 31. DFS in Action (Step 12) Stack: 7, 14 Output: 1, 12, 31, 19, 21 7 19 21 14 1 12 31 23 6 31
• 32. DFS in Action (Step 13) Stack: 7, 14, 23 Output: 1, 12, 31, 19, 21 7 19 21 14 1 12 31 23 6 32
• 33. DFS in Action (Step 14) Stack: 7, 14 Output: 1, 12, 31, 19, 21, 23 7 19 21 14 1 12 31 23 6 33
• 34. DFS in Action (Step 15) Stack: 7, 14, 6 Output: 1, 12, 31, 19, 21, 23 7 19 21 14 1 12 31 23 6 34
• 35. DFS in Action (Step 16) Stack: 7, 14 Output: 1, 12, 31, 19, 21, 23, 6 7 19 21 14 1 12 31 23 6 35
• 36. DFS in Action (Step 17) Stack: 7 Output: 1, 12, 31, 19, 21, 23, 6, 14 7 19 21 14 1 12 31 23 6 36
• 37. DFS in Action (Step 18) Stack: (empty) Output: 1, 12, 31, 19, 21, 23, 6, 14, 7 Traversal finished 7 19 21 14 1 12 31 23 6 37
• 38. Breadth-First Search (BFS) Breadth-First Search first visits the neighbor nodes, later their neighbors, etc. BFS algorithm pseudo code BFS(node) 1 { 7 queue  node while queue not empty 2 3 4 v  queue 19 21 14 print v for each child c of v 6 7 8 5 9 queue  c 1 12 31 23 6 } 38
• 39. BFS in Action (Step 1) Queue: 7 Output: 7 7 19 21 14 1 12 31 23 6 39
• 40. BFS in Action (Step 2) Queue: 7, 19 Output: 7 7 19 21 14 1 12 31 23 6 40
• 41. BFS in Action (Step 3) Queue: 7, 19, 21 Output: 7 7 19 21 14 1 12 31 23 6 41
• 42. BFS in Action (Step 4) Queue: 7, 19, 21, 14 Output: 7 7 19 21 14 1 12 31 23 6 42
• 43. BFS in Action (Step 5) Queue: 7, 19, 21, 14 Output: 7, 19 7 19 21 14 1 12 31 23 6 43
• 44. BFS in Action (Step 6) Queue: 7, 19, 21, 14, 1 Output: 7, 19 7 19 21 14 1 12 31 23 6 44
• 45. BFS in Action (Step 7) Queue: 7, 19, 21, 14, 1, 12 Output: 7, 19 7 19 21 14 1 12 31 23 6 45
• 46. BFS in Action (Step 8) Queue: 7, 19, 21, 14, 1, 12, 31 Output: 7, 19 7 19 21 14 1 12 31 23 6 46
• 47. BFS in Action (Step 9) Queue: 7, 19, 21, 14, 1, 12, 31 Output: 7, 19, 21 7 19 21 14 1 12 31 23 6 47
• 48. BFS in Action (Step 10) Queue: 7, 19, 21, 14, 1, 12, 31 Output: 7, 19, 21, 14 7 19 21 14 1 12 31 23 6 48
• 49. BFS in Action (Step 11) Queue: 7, 19, 21, 14, 1, 12, 31, 23 Output: 7, 19, 21, 14 7 19 21 14 1 12 31 23 6 49
• 50. BFS in Action (Step 12) Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6 Output: 7, 19, 21, 14 7 19 21 14 1 12 31 23 6 50
• 51. BFS in Action (Step 13) Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6 Output: 7, 19, 21, 14, 1 7 19 21 14 1 12 31 23 6 51
• 52. BFS in Action (Step 14) Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6 Output: 7, 19, 21, 14, 1, 12 7 19 21 14 1 12 31 23 6 52
• 53. BFS in Action (Step 15) Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6 Output: 7, 19, 21, 14, 1, 12, 31 7 19 21 14 1 12 31 23 6 53
• 54. BFS in Action (Step 16) Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6 Output: 7, 19, 21, 14, 1, 12, 31, 23 7 19 21 14 1 12 31 23 6 54
• 55. BFS in Action (Step 16) Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6 Output: 7, 19, 21, 14, 1, 12, 31, 23, 6 7 19 21 14 1 12 31 23 6 55
• 56. BFS in Action (Step 17) Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6 Output: 7, 19, 21, 14, 1, 12, 31, 23, 6 The queue is empty  7 stop 19 21 14 1 12 31 23 6 56
• 57. Binary Trees DFS Traversals DFS traversalof binary trees can be done in pre- order, in-order and post-order 17 9 19 6 12 25  Pre-order: root, left, right  17, 9, 6, 12, 19, 25  In-order: left, root, right  6, 9, 12, 17, 19, 25  Post-order: left, right, root  6, 12, 9, 25, 19, 17 57
• 58. Iterative DFS and BFS What will happen if in the Breadth-First Search (BFS) algorithm a stack is used instead of queue?  An iterative Depth-First Search (DFS) – in-order BFS(node) DFS(node) { { queue  node stack  node while queue not empty while stack not empty v  queue v  stack print v print v for each child c of v for each child c of v queue  c stack  c } } 58
• 59. Trees and Traversals Live Demo
• 60. Balanced Search TreesAVL Trees, B-Trees, Red-Black Trees, AA-Trees
• 61. Balanced Binary Search Trees Ordered Binary Trees (Binary Search Trees)  For each node x the left subtree has values ≤ x and the right subtree has values > x Balanced Trees  For each node its subtrees contain nearly equal number of nodes  nearly the same height Balanced Binary Search Trees  Ordered binary search trees that have height of log2(n) where n is the number of their nodes  Searching costs about log2(n) comparisons 61
• 62. Balanced Binary Search Tree – Example 33 18 54 15 24 42 603 17 20 29 37 43 59 85 62
• 63. Balanced Binary Search Trees Balanced binary search trees are hard to implement  Rebalancing the tree after insert / delete is complex Well known implementations of balanced binary search trees  AVL trees – ideally balanced, very complex  Red-black trees – roughly balanced, more simple  AA-Trees – relatively simple to implement Find / insert / delete operations need log(n) steps 63
• 64. B-Trees B-trees are generalization of the concept of ordered binary search trees  B-tree of order d has between d and 2*d keys in a node and between d+1 and 2*d+1 child nodes  The keys in each node are ordered increasingly  All keys in a child node have values between their left and right parent keys If the b-tree is balanced, its search / insert / add operations take about log(n) steps B-trees can be efficiently stored on the disk 64
• 65. B-Tree – Example B-Tree of order 2 (also known as 2-3-4-tree): 17 21 7 11 18 20 26 312 4 5 6 8 9 12 16 22 23 25 27 29 30 32 35 65
• 66. Balanced Trees in .NET .NET Framework has severalbuilt-in implementations of balanced search trees:  SortedDictionary<K,V>  Red-black tree based map of key-value pairs  OrderedSet<T>  Red-black tree based set of elements External libraries like "Wintellect Power Collections for .NET" are more flexible  http://powercollections.codeplex.com 66
• 67. GraphsDefinitions, Representation, Traversal Algorithms
• 68. Graph Data Structure Set of nodes with many-to-many relationship between them is called graph  Each node has multiple predecessors  Each node has multiple successors Node with Node with multiple multiple predecessors successors 7 1 14 4 21 11 12 19 31 68
• 69. Graph Definitions Node (vertex)  Element of graph Node  Can have name or value A  Keeps a list of adjacent nodes Edge Edge  Connection between two nodes  Can be directed / undirected A B  Can be weighted / unweighted  Can have name / value 69
• 70. Graph Definitions (2) Directed graph  Undirected graph  Edges have direction  Undirected edges 22 2 3 A F 7 J 1 4 D G 21 12 E C H 19 3 70
• 71. Graph Definitions (3) Weighted graph  Weight (cost) is associated with each edge 10 A F 4 5 N 6 8 J 16 Q 7 22 D 9 G 3 14 K E C H 71
• 72. Graph Definitions (4) Path (in undirected graph)  Sequence of nodes n1, n2, … nk  Edge exists between each pair of nodes ni, ni+1  Examples:  A, B, C is a path B C  H, K, C is not a path A G K H N 72
• 73. Graph Definitions (5) Path (in directed graph)  Sequence of nodes n1, n2, … nk  Directed edge exists between each pair of nodes ni, ni+1  Examples:  A, B, C is a path B C  A, G, K is not a path A G K H N 73
• 74. Graph Definitions (6) Cycle  Path that ends back at the starting node  Example:  A, B, C, G, A B C Simple path  No cycles in path A G K Acyclic graph H N  Graph with no cycles  Acyclic undirected graphs are trees 74
• 75. Graph Definitions (7) Two nodes are reachable if Unconnected graph with two  Path exists between them connected components Connected graph  Every node is reachable from any other node Connected graph A F J A F J D G D G E H C 75
• 76. Graphs and Their Applications Graphs have many real-world applications  Modeling a computer network like Internet  Routes are simple paths in the network  Modeling a city map  Streets are edges, crossings are vertices  Social networks  People are nodes and their connections are edges  State machines  States are nodes, transitions are edges 76
• 77. Representing Graphs Adjacency list 1  {2, 4} 3 2 2  {3}  Each node holds a 3  {1} list of its neighbors 4  {2} 1 4 Adjacency matrix 1 2 3 4  Each cell keeps 1 0 1 0 1 whether and how two 2 0 0 1 0 3 1 0 0 0 nodes are connected 4 0 1 0 0 Set of edges {1,2} {1,4} {2,3} {3,1} {4,2} 77
• 78. Representing Graphs in C#public class Graph 0{ 6 int[][] childNodes; 3 public Graph(int[][] nodes) 1 { this.childNodes = nodes; 4 5 }} 2Graph g = new Graph(new int[][] { new int[] {3, 6}, // successors of vertice 0 new int[] {2, 3, 4, 5, 6}, // successors of vertice 1 new int[] {1, 4, 5}, // successors of vertice 2 new int[] {0, 1, 5}, // successors of vertice 3 new int[] {1, 2, 6}, // successors of vertice 4 new int[] {1, 2, 3}, // successors of vertice 5 new int[] {0, 1, 4} // successors of vertice 6}); 78
• 79. Graph Traversal Algorithms Depth-First Search (DFS) and Breadth-First Search (BFS) can traverse graphs  Each vertex should be is visited at most once BFS(node) DFS(node) { { queue  node stack  node visited[node] = true visited[node] = true while queue not empty while stack not empty v  queue v  stack print v print v for each child c of v for each child c of v if not visited[c] if not visited[c] queue  c stack  c visited[c] = true visited[c] = true } } 79
• 80. Recursive DFS Graph Traversalvoid TraverseDFSRecursive(node){ if (not visited[node]) { visited[node] = true print node foreach child node c of node { TraverseDFSRecursive(c); } }}vois Main(){ TraverseDFS(firstNode);} 80
• 81. Graphs and Traversals Live Demo
• 82. Summary Trees are recursive data structure – node with set of children which are also nodes Binary Search Trees are ordered binary trees Balanced trees have weight of log(n) Graphs are sets of nodes with many-to-many relationship between them  Can be directed/undirected, weighted / unweighted, connected / not connected, etc. Tree / graph traversals can be done by Depth-First Search (DFS) and Breadth-First Search (BFS) 82
• 83. Trees and Graphs курсове и уроци по програмиране, уеб дизайн – безплатно BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране – Телерик академия форум програмиране, форум уеб дизайн уроци по програмиране и уеб дизайн за ученици ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NET http://academy.telerik.com/ програмиране за деца – безплатни курсове и уроци ASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC безплатен SEO курс - оптимизация за търсачки алго академия – състезателно програмиране, състезаниякурсове и уроци по програмиране, книги – безплатно от Наков курс мобилни приложения с iPhone, Android, WP7, PhoneGap уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop Дончо Минков - сайт за програмиране free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране безплатен курс "Качествен програмен код" безплатен курс "Разработка на софтуер в cloud среда" C# курс, програмиране, безплатно
• 84. Exercises1. Write a program to traverse the directory C:WINDOWS and all its subdirectories recursively and to display all files matching the mask *.exe. Use the class System.IO.Directory.2. Define classes File { string name, int size } and Folder { string name, File[] files, Folder[] childFolders } and using them build a tree keeping all files and folders on the hard drive starting from C:WINDOWS. Implement a method that calculates the sum of the file sizes in given subtree of the tree and test it accordingly. Use recursive DFS traversal. 84
• 85. Exercises (2)3. Implement the recursive Depth-First-Search (DFS) traversal algorithm. Test it with the sample graph from the demonstrations.4. Implement the queue-based Breath-First-Search (BFS) traversal algorithm. Test it with the sample graph from the demonstrations.5. Write a program for finding all cycles in given undirected graph using recursive DFS.6. Write a program for finding all connected components of given undirected graph. Use a sequence of DFS traversals. 85
• 86. Exercises (3)7. Write a program for finding the shortest path between two vertices in a weighted directed graph. Hint: Use the Dijkstras algorithm.8. We are given a set of N tasks that should be executed in a sequence. Some of the tasks depend on other tasks. We are given a list of tasks { ti, tj} where tj depends on the result of ti and should be executed after it. Write a program that arranges the tasks in a sequence so that each task depending on another task is executed after it. If such arrangement is impossible indicate this fact. Example: {1, 2}, {2, 5}, {2, 4}, {3, 1}  3, 1, 2, 5, 4 86