Upcoming SlideShare
×

# 17. Trees and Graphs

202,863 views

Published on

Trees. Defining, Creating and Traversing Trees. Traversing the File System
Binary Search Trees. Balanced Trees
Graphs and Graphs Traversal Algorithms
Exercises: Working with Trees and Graphs

51 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• @Abdul Haleem Solangi , yes agree with you on the mistake of DFS

Are you sure you want to  Yes  No
• Once registered with the #Binary Signal Provider program, you'll be able to cooperate with #Binary Option Trading under the best terms. HERE&gt;&gt;&gt;http://www.binaryoptiontrading24.com/millionaresblueprint.htm

Are you sure you want to  Yes  No
• There is a mistake in DFS of Binary Tree, It will be as follows; Pre order: Root-Left-Right, Inorder: Left-Root-Right, Post Order: Left-Right-Root

Are you sure you want to  Yes  No
• nice

Are you sure you want to  Yes  No
• thanks...

Are you sure you want to  Yes  No
Views
Total views
202,863
On SlideShare
0
From Embeds
0
Number of Embeds
162,708
Actions
Shares
0
2,190
6
Likes
51
Embeds 0
No embeds

No notes for slide
• ### 17. Trees and Graphs

1. 1. Trees and Graphs Trees, Binary Search Trees, Balanced Trees, Graphs <ul><li>Svetlin Nakov </li></ul><ul><li>Telerik Corporation </li></ul><ul><li>www.telerik.com </li></ul>
2. 2. Table of Contents <ul><li>Tree-like Data Structures </li></ul><ul><li>Trees and Related Terminology </li></ul><ul><li>Implementing Trees </li></ul><ul><li>Traversing Trees </li></ul><ul><li>Balanced Trees </li></ul><ul><li>Graphs </li></ul>
3. 3. Tree-like Data Structures Trees, Balanced Trees, Graphs, Networks
4. 4. Tree-like Data Structures <ul><li>Tree-like data structures are </li></ul><ul><ul><li>Branched recursive data structures </li></ul></ul><ul><ul><ul><li>Consisting of nodes </li></ul></ul></ul><ul><ul><ul><li>Each node can be connected to other nodes </li></ul></ul></ul><ul><li>Examples of tree-like structures </li></ul><ul><ul><li>Trees: binary / other degree, balanced, etc. </li></ul></ul><ul><ul><li>Graphs: directed / undirected, weighted, etc. </li></ul></ul><ul><ul><li>Networks </li></ul></ul>
5. 5. Tree-like Data Structures Tree Graph 2 3 6 1 4 5 5 (20) 5 (10) 15 (15) 15 (30) 5 (5) 20(20) 10 (40) Network Project Manager Team Leader De-signer QA Team Leader Developer 1 Developer 2 Tester 1 Developer 3 Tester 2 7 19 21 14 1 12 31 4 11
6. 6. Trees and Related Terminology Node, Edge, Root, Children, Parent, Leaf , Binary Search Tree, Balanced Tree
7. 7. Trees <ul><li>Tree data structure – terminology </li></ul><ul><ul><li>Node, edge, root, child, children, siblings, parent, ancestor, descendant, predecessor, successor, internal node, leaf, depth, height, subtree </li></ul></ul>Height = 2 Depth 0 Depth 1 Depth 2 17 15 14 9 6 5 8
8. 8. Binary Trees <ul><li>Binary trees : most widespread form </li></ul><ul><ul><li>Each node has at most 2 children </li></ul></ul>10 17 15 9 6 5 8 Root node Left subtree Right child Right child Left child
9. 9. Binary Search Trees <ul><li>Binary search trees are ordered </li></ul><ul><ul><li>For each node x in the tree </li></ul></ul><ul><ul><ul><li>All the elements of the left subtree of x are ≤ x </li></ul></ul></ul><ul><ul><ul><li>All the elements of the right subtree of x are > x </li></ul></ul></ul><ul><li>Binary search trees can be balanced </li></ul><ul><ul><li>Balanced trees have height of ~ log 2 ( x ) </li></ul></ul><ul><ul><li>Balanced trees have for each node nearly equal number of nodes in its subtrees </li></ul></ul>
10. 10. Binary Search Trees (2) <ul><li>Example of balanced binary search tree </li></ul><ul><li>If the tree is balanced, add / search / delete operations take approximately log( n ) steps </li></ul>17 19 9 6 12 25
11. 11. Implementing Trees Recursive Tree Data Structure
12. 12. Recursive Tree Definition <ul><li>The recursive definition for tree data structure: </li></ul><ul><ul><li>A single node is tree </li></ul></ul><ul><ul><li>Tree nodes can have zero or multiple children that are also trees </li></ul></ul><ul><li>Tree node definition in C# </li></ul>public class TreeNode<T> { private T value; private List<TreeNode<T>> children; … } The value contained in the node List of child nodes, which are of the same type
13. 13. TreeNode<int> Structure TreeNode<int> int value List<TreeNode<int>> children 7 children 19 children 21 children 14 children 1 children 12 children 31 children 23 children 6 children
14. 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]; }
15. 15. <ul><li>The class Tree<T> keeps tree's root node </li></ul>Implementing Tree<T> public class Tree<T> { private TreeNode<T> root; public Tree(T value, params Tree<T>[] children): this(value) { foreach (Tree<T> child in children) { this.root.AddChild(child.root); } } public TreeNode<T> Root { get { return this.root; } } } Flexible constructor for building trees
16. 16. Building a Tree <ul><li>Constructing tree by nested constructors: </li></ul>Tree<int> tree = new Tree<int>(7, new Tree<int>(19, new Tree<int>(1), new Tree<int>(12), new Tree<int>(31)), new Tree<int>(21), new Tree<int>(14, new Tree<int>(23), new Tree<int>(6)) ); 7 14 19 23 6 21 31 1 12
17. 17. Tree Traversals DFS and BFS Traversals
18. 18. Tree Traversal Algorithms <ul><li>Traversing a tree means to visit each of its nodes exactly one in particular order </li></ul><ul><ul><li>Many traversal algorithms are known </li></ul></ul><ul><ul><li>Depth-First Search (DFS) </li></ul></ul><ul><ul><ul><li>Visit node's successors first </li></ul></ul></ul><ul><ul><ul><li>Usually implemented by recursion </li></ul></ul></ul><ul><ul><li>Breadth-First Search (BFS) </li></ul></ul><ul><ul><ul><li>Nearest nodes visited first </li></ul></ul></ul><ul><ul><ul><li>Implemented by a queue </li></ul></ul></ul>
19. 19. <ul><li>Depth-First Search first visits all descendants of given node recursively, finally visits the node itself </li></ul><ul><li>DFS algorithm pseudo code </li></ul>Depth-First Search (DFS) DFS(node) { for each child c of node DFS( c ); print the current node; } 1 2 3 4 5 8 6 7 9 7 14 19 23 6 21 31 1 12
20. 20. DFS in Action (Step 1 ) <ul><li>Stack: 7 </li></ul><ul><li>Output: (empty) </li></ul>7 14 19 23 6 21 31 1 12
21. 21. DFS in Action (Step 2 ) <ul><li>Stack: 7 , 19 </li></ul><ul><li>Output: (empty) </li></ul>7 14 19 23 6 21 31 1 12
22. 22. DFS in Action (Step 3 ) <ul><li>Stack: 7 , 19 , 1 </li></ul><ul><li>Output: (empty) </li></ul>7 14 19 23 6 21 31 1 12
23. 23. DFS in Action (Step 4 ) <ul><li>Stack: 7 , 19 </li></ul><ul><li>Output: 1 </li></ul>7 14 19 23 6 21 31 1 12
24. 24. DFS in Action (Step 5 ) <ul><li>Stack: 7 , 19 , 12 </li></ul><ul><li>Output: 1 </li></ul>7 14 19 23 6 21 31 1 12
25. 25. DFS in Action (Step 6 ) <ul><li>Stack: 7 , 19 </li></ul><ul><li>Output: 1 , 12 </li></ul>7 14 19 23 6 21 31 1 12
26. 26. DFS in Action (Step 7 ) <ul><li>Stack: 7 , 19 , 31 </li></ul><ul><li>Output: 1 , 12 </li></ul>7 14 19 23 6 21 31 1 12
27. 27. DFS in Action (Step 8 ) <ul><li>Stack: 7 , 19 </li></ul><ul><li>Output: 1 , 12 , 31 </li></ul>7 14 19 23 6 21 31 1 12
28. 28. DFS in Action (Step 9 ) <ul><li>Stack: 7 </li></ul><ul><li>Output: 1 , 12 , 31 , 19 </li></ul>7 14 19 23 6 21 31 1 12
29. 29. DFS in Action (Step 10 ) <ul><li>Stack: 7 , 21 </li></ul><ul><li>Output: 1 , 12 , 31 , 19 </li></ul>7 14 19 23 6 21 31 1 12
30. 30. DFS in Action (Step 11 ) <ul><li>Stack: 7 </li></ul><ul><li>Output: 1 , 12 , 31 , 19 , 21 </li></ul>7 14 19 23 6 21 31 1 12
31. 31. DFS in Action (Step 12 ) <ul><li>Stack: 7 , 14 </li></ul><ul><li>Output: 1 , 12 , 31 , 19 , 21 </li></ul>7 14 19 23 6 21 31 1 12
32. 32. DFS in Action (Step 13 ) <ul><li>Stack: 7 , 14 , 23 </li></ul><ul><li>Output: 1 , 12 , 31 , 19 , 21 </li></ul>7 14 19 23 6 21 31 1 12
33. 33. DFS in Action (Step 14 ) <ul><li>Stack: 7 , 14 </li></ul><ul><li>Output: 1 , 12 , 31 , 19 , 21 , 23 </li></ul>7 14 19 23 6 21 31 1 12
34. 34. DFS in Action (Step 15 ) <ul><li>Stack: 7 , 14 , 6 </li></ul><ul><li>Output: 1 , 12 , 31 , 19 , 21 , 23 </li></ul>7 14 19 23 6 21 31 1 12
35. 35. DFS in Action (Step 16 ) <ul><li>Stack: 7 , 14 </li></ul><ul><li>Output: 1 , 12 , 31 , 19 , 21 , 23 , 6 </li></ul>7 14 19 23 6 21 31 1 12
36. 36. DFS in Action (Step 17 ) <ul><li>Stack: 7 </li></ul><ul><li>Output: 1 , 12 , 31 , 19 , 21 , 23 , 6 , 14 </li></ul>7 14 19 23 6 21 31 1 12
37. 37. DFS in Action (Step 18 ) <ul><li>Stack: (empty) </li></ul><ul><li>Output: 1 , 12 , 31 , 19 , 21 , 23 , 6 , 14 , 7 </li></ul>Traversal finished 7 14 19 23 6 21 31 1 12
38. 38. <ul><li>Breadth-First Search first visits the neighbor nodes, later their neighbors, etc. </li></ul><ul><li>BFS algorithm pseudo code </li></ul>Breadth-First Search (BFS) BFS(node) { queue  node while queue not empty v  queue print v for each child c of v queue  c } 5 6 7 2 3 4 8 9 1 7 14 19 23 6 21 31 1 12
39. 39. BFS in Action (Step 1 ) <ul><li>Queue: 7 </li></ul><ul><li>Output: 7 </li></ul>7 14 19 23 6 21 31 1 12
40. 40. BFS in Action (Step 2 ) <ul><li>Queue: 7 , 19 </li></ul><ul><li>Output: 7 </li></ul>7 14 19 23 6 21 31 1 12
41. 41. BFS in Action (Step 3 ) <ul><li>Queue: 7 , 19 , 21 </li></ul><ul><li>Output: 7 </li></ul>7 14 19 23 6 21 31 1 12
42. 42. BFS in Action (Step 4 ) <ul><li>Queue: 7 , 19 , 21 , 14 </li></ul><ul><li>Output: 7 </li></ul>7 14 19 23 6 21 31 1 12
43. 43. BFS in Action (Step 5 ) <ul><li>Queue: 7 , 19 , 21 , 14 </li></ul><ul><li>Output: 7 , 19 </li></ul>7 14 19 23 6 21 31 1 12
44. 44. BFS in Action (Step 6 ) <ul><li>Queue: 7 , 19 , 21 , 14 , 1 </li></ul><ul><li>Output: 7 , 19 </li></ul>7 14 19 23 6 21 31 1 12
45. 45. BFS in Action (Step 7 ) <ul><li>Queue: 7 , 19 , 21 , 14 , 1 , 12 </li></ul><ul><li>Output: 7 , 19 </li></ul>7 14 19 23 6 21 31 1 12
46. 46. BFS in Action (Step 8 ) <ul><li>Queue: 7 , 19 , 21 , 14 , 1 , 12 , 31 </li></ul><ul><li>Output: 7 , 19 </li></ul>7 14 19 23 6 21 31 1 12
47. 47. BFS in Action (Step 9 ) <ul><li>Queue: 7 , 19 , 21 , 14 , 1 , 12 , 31 </li></ul><ul><li>Output: 7 , 19 , 21 </li></ul>7 14 19 23 6 21 31 1 12
48. 48. BFS in Action (Step 10 ) <ul><li>Queue: 7 , 19 , 21 , 14 , 1 , 12 , 31 </li></ul><ul><li>Output: 7 , 19 , 21 , 14 </li></ul>7 14 19 23 6 21 31 1 12
49. 49. BFS in Action (Step 11 ) <ul><li>Queue: 7 , 19 , 21 , 14 , 1 , 12 , 31 , 23 </li></ul><ul><li>Output: 7 , 19 , 21 , 14 </li></ul>7 14 19 23 6 21 31 1 12
50. 50. BFS in Action (Step 12 ) <ul><li>Queue: 7 , 19 , 21 , 14 , 1 , 12 , 31 , 23 , 6 </li></ul><ul><li>Output: 7 , 19 , 21 , 14 </li></ul>7 14 19 23 6 21 31 1 12
51. 51. BFS in Action (Step 13 ) <ul><li>Queue: 7 , 19 , 21 , 14 , 1 , 12 , 31 , 23 , 6 </li></ul><ul><li>Output: 7 , 19 , 21 , 14 , 1 </li></ul>7 14 19 23 6 21 31 1 12
52. 52. BFS in Action (Step 14 ) <ul><li>Queue: 7 , 19 , 21 , 14 , 1 , 12 , 31 , 23 , 6 </li></ul><ul><li>Output: 7 , 19 , 21 , 14 , 1 , 1 2 </li></ul>7 14 19 23 6 21 31 1 12
53. 53. BFS in Action (Step 15 ) <ul><li>Queue: 7 , 19 , 21 , 14 , 1 , 12 , 31 , 23 , 6 </li></ul><ul><li>Output: 7 , 19 , 21 , 14 , 1 , 1 2 , 31 </li></ul>7 14 19 23 6 21 31 1 12
54. 54. BFS in Action (Step 16 ) <ul><li>Queue: 7 , 19 , 21 , 14 , 1 , 12 , 31 , 23 , 6 </li></ul><ul><li>Output: 7 , 19 , 21 , 14 , 1 , 1 2 , 31 , 23 </li></ul>7 14 19 23 6 21 31 1 12
55. 55. BFS in Action (Step 16 ) <ul><li>Queue: 7 , 19 , 21 , 14 , 1 , 12 , 31 , 23 , 6 </li></ul><ul><li>Output: 7 , 19 , 21 , 14 , 1 , 1 2 , 31 , 23 , 6 </li></ul>7 14 19 23 6 21 31 1 12
56. 56. BFS in Action (Step 17 ) <ul><li>Queue: 7 , 19 , 21 , 14 , 1 , 12 , 31 , 23 , 6 </li></ul><ul><li>Output: 7 , 19 , 21 , 14 , 1 , 1 2 , 31 , 23 , 6 </li></ul>The queue is empty  stop 7 14 19 23 6 21 31 1 12
57. 57. Binary Trees DFS Traversals <ul><li>DFS traversal of binary trees can be done in pre-order, in-order and post-order </li></ul><ul><ul><li>Pre-order: left, root, right  6 , 9 , 12 , 17 , 19 , 25 </li></ul></ul><ul><ul><li>In-order: root, left, right  17 , 9 , 6 , 12 , 19 , 25 </li></ul></ul><ul><ul><li>Post-order: left, right, root  6 , 12 , 9 , 25 , 19 , 17 </li></ul></ul>17 19 9 6 12 25
58. 58. Iterative DFS and BFS <ul><li>What will happen if in the Breadth-First Search (BFS) algorithm a stack is used instead of queue? </li></ul><ul><ul><li>An iterative Depth-First Search (DFS) – in-order </li></ul></ul>BFS(node) { queue  node while queue not empty v  queue print v for each child c of v queue  c } DFS(node) { stack  node while stack not empty v  stack print v for each child c of v stack  c }
59. 59. Trees and Traversals Live Demo
60. 60. Balanced Search Trees AVL Trees, B-Trees, Red-Black Trees, AA-Trees
61. 61. Balanced Binary Search Trees <ul><li>Ordered Binary Trees ( Binary Search Trees ) </li></ul><ul><ul><li>For each node x the left subtree has values ≤ x and the right subtree has values > x </li></ul></ul><ul><li>Balanced Trees </li></ul><ul><ul><li>For each node its subtrees contain nearly equal number of nodes  nearly the same height </li></ul></ul><ul><li>Balanced Binary Search Trees </li></ul><ul><ul><li>Ordered binary search trees that have height of log 2 (n) where n is the number of their nodes </li></ul></ul><ul><ul><li>Searching costs about log 2 (n) comparisons </li></ul></ul>
62. 62. Balanced Binary Search Tree – Example 33 18 15 24 3 17 20 29 54 42 60 37 43 59 85
63. 63. Balanced Binary Search Trees <ul><li>Balanced binary search trees are hard to implement </li></ul><ul><ul><li>Rebalancing the tree after insert / delete is complex </li></ul></ul><ul><li>Well known implementations of balanced binary search trees </li></ul><ul><ul><li>AVL trees – ideally balanced, very complex </li></ul></ul><ul><ul><li>Red-black trees – roughly balanced, more simple </li></ul></ul><ul><ul><li>AA-Trees – relatively simple to implement </li></ul></ul><ul><li>Find / insert / delete operations need log 2 (n) steps </li></ul>
64. 64. B-Trees <ul><li>B-trees are generalization of the concept of ordered binary search trees </li></ul><ul><ul><li>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 </li></ul></ul><ul><ul><li>The keys in each node are ordered increasingly </li></ul></ul><ul><ul><li>All keys in a child node have values between their left and right parent keys </li></ul></ul><ul><li>If the b-tree is balanced, its search / insert / add operations take about log(n) steps </li></ul><ul><li>B-trees can be efficiently stored on the disk </li></ul>
65. 65. <ul><li>B-Tree of order 2 , also known as 2 - 3 - 4 -tree: </li></ul>B-Tree – Example 17 21 7 11 18 20 26 31 2 4 5 6 8 9 12 16 22 23 25 27 29 30 32 35
66. 66. Balanced Trees in .NET <ul><li>.NET Framework has several built-in implementations of balanced search trees: </li></ul><ul><ul><li>SortedDictionary<K,V> </li></ul></ul><ul><ul><ul><li>Red-black tree based map of key-value pairs </li></ul></ul></ul><ul><ul><li>OrderedSet<T> </li></ul></ul><ul><ul><ul><li>Red-black tree based set of elements </li></ul></ul></ul><ul><li>External libraries like &quot;Wintellect Power Collections for .NET&quot; are more flexible </li></ul><ul><ul><li>http://powercollections.codeplex.com </li></ul></ul>
67. 67. Graphs Definitions, Representation, Traversal Algorithms
68. 68. Graph Data Structure <ul><li>Set of nodes with many-to-many relationship between them is called graph </li></ul><ul><ul><li>Each node has multiple predecessors </li></ul></ul><ul><ul><li>Each node has multiple successors </li></ul></ul>Node with multiple predecessors Node with multiple successors 7 19 21 14 1 12 31 4 11
69. 69. Graph Definitions <ul><li>Node ( vertex ) </li></ul><ul><ul><li>Element of graph </li></ul></ul><ul><ul><li>Can have name or value </li></ul></ul><ul><ul><li>Keeps a list of adjacent nodes </li></ul></ul><ul><li>Edge </li></ul><ul><ul><li>Connection between two nodes </li></ul></ul><ul><ul><li>Can be directed / undirected </li></ul></ul><ul><ul><li>Can be weighted / unweighted </li></ul></ul><ul><ul><li>Can have name / value </li></ul></ul>A Node A Edge B
70. 70. Graph Definitions (2) <ul><li>Directed graph </li></ul><ul><ul><li>Edges have direction </li></ul></ul><ul><li>Undirected graph </li></ul><ul><ul><li>Undirected edges </li></ul></ul>7 19 21 1 12 4 3 22 2 3 G J F D A E C H
71. 71. Graph Definitions (3) <ul><li>Weighted graph </li></ul><ul><ul><li>Weight (cost) is associated with each edge </li></ul></ul>3 G J F D A E C H Q K N 10 4 14 6 16 9 8 7 5 22
72. 72. Graph Definitions (4) <ul><li>Path (in undirected graph) </li></ul><ul><ul><li>Sequence of nodes n 1 , n 2 , … n k </li></ul></ul><ul><ul><li>Edge exists between each pair of nodes n i , n i+1 </li></ul></ul><ul><ul><li>Examples: </li></ul></ul><ul><ul><ul><li>A, B, C is a path </li></ul></ul></ul><ul><ul><ul><li>H, K, C is not a path </li></ul></ul></ul>G C B A H N K
73. 73. Graph Definitions (5) <ul><li>Path (in directed graph) </li></ul><ul><ul><li>Sequence of nodes n 1 , n 2 , … n k </li></ul></ul><ul><ul><li>Directed edge exists between each pair of nodes n i , n i+1 </li></ul></ul><ul><ul><li>Examples: </li></ul></ul><ul><ul><ul><li>A, B, C is a path </li></ul></ul></ul><ul><ul><ul><li>A, G, K is not a path </li></ul></ul></ul>G C B A H N K
74. 74. Graph Definitions (6) <ul><li>Cycle </li></ul><ul><ul><li>Path that ends back at the starting node </li></ul></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><ul><li>A, B, C, G, A </li></ul></ul></ul><ul><li>Simple path </li></ul><ul><ul><li>No cycles in path </li></ul></ul><ul><li>Acyclic graph </li></ul><ul><ul><li>Graph with no cycles </li></ul></ul><ul><ul><li>Acyclic undirected graphs are trees </li></ul></ul>G C B A H N K
75. 75. Graph Definitions (8) <ul><li>Two nodes are reachable if </li></ul><ul><ul><li>Path exists between them </li></ul></ul><ul><li>Connected graph </li></ul><ul><ul><li>Every node is reachable from any other node </li></ul></ul>Unconnected graph with two connected components G J F D A Connected graph G J F D A E C H
76. 76. Graphs and Their Applications <ul><li>Graphs have many real-world applications </li></ul><ul><ul><li>Modeling a computer network like Internet </li></ul></ul><ul><ul><ul><li>Routes are simple paths in the network </li></ul></ul></ul><ul><ul><li>Modeling a city map </li></ul></ul><ul><ul><ul><li>Streets are edges, crossings are vertices </li></ul></ul></ul><ul><ul><li>Social networks </li></ul></ul><ul><ul><ul><li>People are nodes and their connections are edges </li></ul></ul></ul><ul><ul><li>State machines </li></ul></ul><ul><ul><ul><li>States are nodes, transitions are edges </li></ul></ul></ul>
77. 77. Representing Graphs <ul><li>Adjacency list </li></ul><ul><ul><li>Each node holds a list of its neighbors </li></ul></ul><ul><li>Adjacency matrix </li></ul><ul><ul><li>Each cell keeps whether and how two nodes are connected </li></ul></ul><ul><li>Set of edges </li></ul>1 2 3 4 1 2 3 4 {1,2} {1,4} {2,3} {3,1} {4,2} 1  {2, 4} 2  {3} 3  {1} 4  {2} 0 1 0 1 0 0 1 0 1 0 0 0 0 1 0 0 2 4 1 3
78. 78. Representing Graphs in C# public class Graph { int[][] childNodes; public Graph(int[][] nodes) { this.childNodes = nodes; } } Graph 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 }); 0 6 4 1 5 2 3
79. 79. Graph Traversal Algorithms <ul><li>Depth-First Search (DFS) and Breadth-First Search (BFS) can traverse graphs </li></ul><ul><ul><li>Each vertex should be is visited at most once </li></ul></ul>BFS( node ) { queue  node visited[ node ] = true while queue not empty v  queue print v for each child c of v if not visited[ c ] queue  c visited[ c ] = true } DFS( node ) { stack  node visited[ node ] = true while stack not empty v  stack print v for each child c of v if not visited[ c ] stack  c visited[ c ] = true }
80. 80. Recursive DFS Graph Traversal void TraverseDFSRecursive(node) { if (not visited[node]) { visited[node] = true print node foreach child node c of node { TraverseDFSRecursive( c ); } } } vois Main() { TraverseDFS(firstNode); }
81. 81. Graphs and Traversals Live Demo
82. 82. Summary <ul><li>Trees are recursive data structure – node with set of children which are also nodes </li></ul><ul><li>Binary Search Trees are ordered binary trees </li></ul><ul><li>Balanced trees have weight of log(n) </li></ul><ul><li>Graphs are sets of nodes with many-to-many relationship between them </li></ul><ul><ul><li>Can be directed/undirected, weighted / unweighted, connected / not connected, etc. </li></ul></ul><ul><li>Tree / graph traversals can be done by Depth-First Search (DFS) and Breadth-First Search (BFS) </li></ul>
83. 83. Trees and Graphs <ul><li>Questions? </li></ul>http://academy.telerik.com
84. 84. Exercises <ul><li>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 . </li></ul><ul><li>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. </li></ul>
85. 85. Exercises (2) <ul><li>Implement the recursive Depth-First-Search (DFS) traversal algorithm. Test it with the sample graph from the demonstrations. </li></ul><ul><li>Implement the queue-based Breath-First-Search (BFS) traversal algorithm. Test it with the sample graph from the demonstrations. </li></ul><ul><li>Write a program for finding all cycles in given undirected graph using recursive DFS. </li></ul><ul><li>Write a program for finding all connected components of given undirected graph. Use a sequence of DFS traversals. </li></ul>
86. 86. Exercises (3) <ul><li>Write a program for finding the shortest path between two vertices in a weighted directed graph. Hint: Use the Dijkstra's algorithm . </li></ul><ul><li>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 { t i , t j } where t j depends on the result of t i 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. </li></ul><ul><li>Example: { 1 , 2 }, { 2 , 5 }, { 2 , 4 }, { 3 , 1 }  3 , 1 , 2 , 5 , 4 </li></ul>