Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy

Similar presentations


Presentation on theme: "Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy"— Presentation transcript:

1 Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy http://academy.telerik.com

2 1. Tree-like Data Structures 2. Trees and Related Terminology 3. Traversing Trees 4. Graphs  Graph Definitions  Representing Graphs  Graph Traversal Algorithms  Connected Components 2

3 Trees, Balanced Trees, Graphs, Networks

4  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 5 Project Manager Team Leader De- signer QA Team Leader Developer 1 Developer 2 Tester 1 Developer 3 Tester 2 Tree Graph 2 3 6 1 4 5 7 19 21 14 1 12 31 4 11 5 (20) 5 (10) 15 (15) 15 (30) 5 (5) 20(20) 10 (40) Network

6 Node, Edge, Root, Children, Parent, Leaf, Binary Search Tree, Balanced Tree

7  Tree data structure – terminology  Node, edge, root, child, children, siblings, parent, ancestor, descendant, predecessor, successor, internal node, leaf, depth, height, subtree Height = 3 Depth 0 Depth 1 Depth 2 17 15 14 9 6 5 8 7

8  Binary trees: most widespread form  Each node has at most 2 children 10 17 159 6 5 8 Root node Left subtree Right child Left child 8

9 Recursive Tree Data Structure

10  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# 10 public class TreeNode public class TreeNode { private T value; private T value; private List > children; private List > children; …} The value contained in the node List of child nodes, which are of the same type

11 11 7children 19children21children14children TreeNode<int> 1children 12children 31children23children6children int value List > children

12 12 public TreeNode(T value) { this.value = value; this.value = value; this.children = new List >(); this.children = new List >();} public T Value { get { return this.value; } get { return this.value; } set { this.value = value; } set { this.value = value; }} public void AddChild(TreeNode child) { child.hasParent = true; child.hasParent = true; this.children.Add(child); this.children.Add(child);} public TreeNode GetChild(int index) { return this.children[index]; return this.children[index];}

13  The class Tree keeps tree's root node 13 public class Tree public class Tree { private TreeNode root; private TreeNode root; public Tree(T value, params Tree [] children): this(value) public Tree(T value, params Tree [] children): this(value) { foreach (Tree child in children) foreach (Tree child in children) { this.root.AddChild(child.root); this.root.AddChild(child.root); } } public TreeNode Root public TreeNode Root { get { return this.root; } get { return this.root; } }} Flexible constructor for building trees

14 14 Tree tree = new Tree (7, new Tree (7, new Tree (19, new Tree (19, new Tree (1), new Tree (1), new Tree (12), new Tree (12), new Tree (31)), new Tree (31)), new Tree (21), new Tree (21), new Tree (14, new Tree (14, new Tree (23), new Tree (23), new Tree (6)) new Tree (6))); 7 14 19 23 6 21 31 1 12  Constructing tree by nested constructors:

15 DFS and BFS Traversals

16  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 node's successors first  Usually implemented by recursion  Breadth-First Search (BFS)  Nearest nodes visited first  Implemented by a queue 16

17  Depth-First Search first visits all descendants of given node recursively, finally visits the node itself  DFS algorithm pseudo code DFS(node){ for each child c of node for each child c of node DFS(c); DFS(c); print the current node; print the current node;} 7 14 19 23 6 21 31 1 12 1 2 3 4 5 8 6 7 9 17

18  Stack: 7  Output: (empty) 18 7 14 19 23 6 21 31 1 12

19  Stack: 7, 19  Output: (empty) 19 7 14 19 23 6 21 31 1 12

20  Stack: 7, 19, 1  Output: (empty) 20 7 14 19 23 6 21 31 1 12

21  Stack: 7, 19  Output: 1 21 7 14 19 23 6 21 31 1 12

22  Stack: 7, 19, 12  Output: 1 22 7 14 19 23 6 21 31 1 12

23  Stack: 7, 19  Output: 1, 12 23 7 14 19 23 6 21 31 1 12

24  Stack: 7, 19, 31  Output: 1, 12 24 7 14 19 23 6 21 31 1 12

25  Stack: 7, 19  Output: 1, 12, 31 25 7 14 19 23 6 21 31 1 12

26  Stack: 7  Output: 1, 12, 31, 19 26 7 14 19 23 6 21 31 1 12

27  Stack: 7, 21  Output: 1, 12, 31, 19 27 7 14 19 23 6 21 31 1 12

28  Stack: 7  Output: 1, 12, 31, 19, 21 28 7 14 19 23 6 21 31 1 12

29  Stack: 7, 14  Output: 1, 12, 31, 19, 21 29 7 14 19 23 6 21 31 1 12

30  Stack: 7, 14, 23  Output: 1, 12, 31, 19, 21 30 7 14 19 23 6 21 31 1 12

31  Stack: 7, 14  Output: 1, 12, 31, 19, 21, 23 31 7 14 19 23 6 21 31 1 12

32  Stack: 7, 14, 6  Output: 1, 12, 31, 19, 21, 23 32 7 14 19 23 6 21 31 1 12

33  Stack: 7, 14  Output: 1, 12, 31, 19, 21, 23, 6 33 7 14 19 23 6 21 31 1 12

34  Stack: 7  Output: 1, 12, 31, 19, 21, 23, 6, 14 34 7 14 19 23 6 21 31 1 12

35  Stack: (empty)  Output: 1, 12, 31, 19, 21, 23, 6, 14, 7 35 7 14 19 23 6 21 31 1 12 Traversal finished

36  Breadth-First Search first visits the neighbor nodes, later their neighbors, etc.  BFS algorithm pseudo code BFS(node){ queue  node queue  node while queue not empty while queue not empty v  queue v  queue print v print v for each child c of v for each child c of v queue  c queue  c} 7 14 19 23 6 21 31 1 12 5 6 7 2 3 4 8 9 1 36

37  Queue: 7  Output: 7 37 7 14 19 23 6 21 31 1 12

38  Queue: 7, 19  Output: 7 38 7 14 19 23 6 21 31 1 12

39  Queue: 7, 19, 21  Output: 7 39 7 14 19 23 6 21 31 1 12

40  Queue: 7, 19, 21, 14  Output: 7 40 7 14 19 23 6 21 31 1 12

41  Queue: 7, 19, 21, 14  Output: 7, 19 41 7 14 19 23 6 21 31 1 12

42  Queue: 7, 19, 21, 14, 1  Output: 7, 19 42 7 14 19 23 6 21 31 1 12

43  Queue: 7, 19, 21, 14, 1, 12  Output: 7, 19 43 7 14 19 23 6 21 31 1 12

44  Queue: 7, 19, 21, 14, 1, 12, 31  Output: 7, 19 44 7 14 19 23 6 21 31 1 12

45  Queue: 7, 19, 21, 14, 1, 12, 31  Output: 7, 19, 21 45 7 14 19 23 6 21 31 1 12

46  Queue: 7, 19, 21, 14, 1, 12, 31  Output: 7, 19, 21, 14 46 7 14 19 23 6 21 31 1 12

47  Queue: 7, 19, 21, 14, 1, 12, 31, 23  Output: 7, 19, 21, 14 47 7 14 19 23 6 21 31 1 12

48  Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14 48 7 14 19 23 6 21 31 1 12

49  Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14, 1 49 7 14 19 23 6 21 31 1 12

50  Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14, 1, 12 50 7 14 19 23 6 21 31 1 12

51  Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14, 1, 12, 31 51 7 14 19 23 6 21 31 1 12

52  Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14, 1, 12, 31, 23 52 7 14 19 23 6 21 31 1 12

53  Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14, 1, 12, 31, 23, 6 53 7 14 19 23 6 21 31 1 12

54  Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14, 1, 12, 31, 23, 6 54 7 14 19 23 6 21 31 1 12 The queue is empty  stop

55  DFS traversal of binary trees can be done in pre- order, in-order and post-order  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 17 199 6 12 25 55

56  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 56 BFS(node){ queue  node queue  node while queue not empty while queue not empty v  queue v  queue print v print v for each child c of v for each child c of v queue  c queue  c}DFS(node){ stack  node stack  node while stack not empty while stack not empty v  stack v  stack print v print v for each child c of v for each child c of v stack  c stack  c}

57 Definitions, Representation, Traversal Algorithms

58  Set of nodes with many-to-many relationship between them is called graph  Each node has multiple predecessors  Each node has multiple successors 7 19 21 14 1 12 31 4 11 Node with multiple predecessors Node with multiple successors 58

59  Node (vertex)  Element of graph  Can have name or value  Keeps a list of adjacent nodes  Edge  Connection between two nodes  Can be directed / undirected  Can be weighted / unweighted  Can have name / value A Node A Edge B 59

60  Directed graph  Edges have direction  Undirected graph  Undirected edges 7 19 21 1 12 4 3 22 2 3 G J F D A E C H 60

61  Weighted graph  Weight (cost) is associated with each edge G J F D A E C H Q K N 10 4 14 6 16 9 8 7 5 22 3 61

62  Path (in undirected graph)  Sequence of nodes n 1, n 2, … n k  Edge exists between each pair of nodes n i, n i+1  Examples:  A, B, C is a path  H, K, C is not a path G C B A HN K 62

63  Path (in directed graph)  Sequence of nodes n 1, n 2, … n k  Directed edge exists between each pair of nodes n i, n i+1  Examples:  A, B, C is a path  A, G, K is not a path G C B A HN K 63

64  Cycle  Path that ends back at the starting node  Example:  A, B, C, G, A  Simple path  No cycles in path  Acyclic graph  Graph with no cycles  Acyclic undirected graphs are trees G C B A HN K 64

65 Unconnected graph with two connected components  Two nodes are reachable if  Path exists between them  Connected graph  Every node is reachable from any other node G J F D A Connected graph G J F D A E C H 65

66  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 66

67  Adjacency list  Each node holds a list of its neighbors  Adjacency matrix  Each cell keeps whether and how two nodes are connected  Set of edges 01010010 1000 0100 1 2 3 4 1 234 {1,2} {1,4} {2,3} {3,1} {4,2} 1  {2, 4} 2  {3} 3  {1} 4  {2} 2 41 3 67

68 public class Graph { int[][] childNodes; int[][] childNodes; public Graph(int[][] nodes) public Graph(int[][] nodes) { this.childNodes = nodes; this.childNodes = nodes; }} Graph g = new Graph(new int[][] { new int[] {3, 6}, // successors of vertice 0 new int[] {3, 6}, // successors of vertice 0 new int[] {2, 3, 4, 5, 6}, // successors of vertice 1 new int[] {2, 3, 4, 5, 6}, // successors of vertice 1 new int[] {1, 4, 5}, // successors of vertice 2 new int[] {1, 4, 5}, // successors of vertice 2 new int[] {0, 1, 5}, // successors of vertice 3 new int[] {0, 1, 5}, // successors of vertice 3 new int[] {1, 2, 6}, // successors of vertice 4 new int[] {1, 2, 6}, // successors of vertice 4 new int[] {1, 2, 3}, // successors of vertice 5 new int[] {1, 2, 3}, // successors of vertice 5 new int[] {0, 1, 4} // successors of vertice 6 new int[] {0, 1, 4} // successors of vertice 6}); 0 6 4 1 5 2 3 68

69  Depth-First Search (DFS) and Breadth-First Search (BFS) can traverse graphs  Each vertex should be is visited at most once 69 BFS(node) { queue  node queue  node visited[node] = true visited[node] = true while queue not empty while queue not empty v  queue v  queue 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 queue  c visited[c] = true visited[c] = true} DFS(node) { stack  node stack  node visited[node] = true visited[node] = true while stack not empty while stack not empty v  stack 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] stack  c stack  c visited[c] = true visited[c] = true}

70 70 void TraverseDFSRecursive(node) { if (not visited[node]) if (not visited[node]) { visited[node] = true visited[node] = true print node print node foreach child node c of node foreach child node c of node { TraverseDFSRecursive(c); TraverseDFSRecursive(c); } }} vois Main() { TraverseDFS(firstNode); TraverseDFS(firstNode);}

71 Live Demo

72 Connecting the chain

73  Connected component of undirected graph  A sub-graph in which any two nodes are connected to each other by paths 73

74  A simple way to find number of connected components  A loop through all nodes and start a DFS or BFS traversing from any unvisited node  Each time you start a new traversing  You find a new connected component! 74

75 75 foreach node from graph G { if node is unvisited if node is unvisited { DFS(node); DFS(node); countOfComponents++; countOfComponents++; }}  Algorithm: *Note: Do not forget to mark each node in the DFS as visited! F D A G C H

76  Connected graph  A graph with only one connected component  In every connected graph a path exists between any two nodes  Checking whether a graph is connected  If DFS / BFS passes through all vertices  graph is connected! 76

77  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) 77

78 Questions? http://algoacademy.telerik.com


Download ppt "Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy"

Similar presentations


Ads by Google