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

Slides:



Advertisements
Similar presentations
CS203 Lecture 15.
Advertisements

Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 27 Graph Applications.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Graphs CS-240/341. Uses for Graphs computer networks and routing airline flights geographic maps course prerequisite structures tasks for completing a.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank and Modified By Mingwu Chen Trees.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
Trees, Tre-Like Structures, Binary Search Trees, Balanced Trees, Tree Traversals, DFS and BFS Svetlin Nakov Telerik Software Academy academy.telerik.com.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Tree A connected graph that contains no simple circuits is called a tree. Because a tree cannot have a simple circuit, a tree cannot contain multiple.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Discrete Structures Trees (Ch. 11)
Graphs and Graph Algorithms Fundamentals, Terminology, Traversal, Algorithms SoftUni Team Technical Trainers Software University
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Hello Everyone!!! 1. Tree And Graphs 2 Features of Trees  Tree Nodes Each node have 0 or more children A node have must one parent  Binary tree Tree.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Chapter 05 Introduction to Graph And Search Algorithms.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
SoftUni Team Technical Trainers Software University Trees and Tree-Like Structures Trees, Tree-Like Structures, Binary Search Trees,
Trees A non-linear implementation for collection classes.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
Graphs and Graph Algorithms
Chapter 12 Abstract Data Type.
Non Linear Data Structure
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Basic Tree Data Structures
Fundamentals of Programming II Introduction to Trees
Fundamentals, Terminology, Traversal, Algorithms
Csc 2720 Instructor: Zhuojun Duan
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Trees.
Trees.
Trees.
Presentation transcript:

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

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

Trees, Balanced Trees, Graphs, Networks

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

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

 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

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

Recursive Tree Data Structure

 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 7children 19children21children14children TreeNode<int> 1children 12children 31children23children6children int value List > children

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];}

 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 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)));  Constructing tree by nested constructors:

DFS and BFS Traversals

 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

 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;}

 Stack: 7  Output: (empty)

 Stack: 7, 19  Output: (empty)

 Stack: 7, 19, 1  Output: (empty)

 Stack: 7, 19  Output:

 Stack: 7, 19, 12  Output:

 Stack: 7, 19  Output: 1,

 Stack: 7, 19, 31  Output: 1,

 Stack: 7, 19  Output: 1, 12,

 Stack: 7  Output: 1, 12, 31,

 Stack: 7, 21  Output: 1, 12, 31,

 Stack: 7  Output: 1, 12, 31, 19,

 Stack: 7, 14  Output: 1, 12, 31, 19,

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

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

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

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

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

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

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

 Queue: 7  Output:

 Queue: 7, 19  Output:

 Queue: 7, 19, 21  Output:

 Queue: 7, 19, 21, 14  Output:

 Queue: 7, 19, 21, 14  Output: 7,

 Queue: 7, 19, 21, 14, 1  Output: 7,

 Queue: 7, 19, 21, 14, 1, 12  Output: 7,

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

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

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

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

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

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

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

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

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

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

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

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

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

Definitions, Representation, Traversal Algorithms

 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 multiple predecessors Node with multiple successors 58

 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

 Directed graph  Edges have direction  Undirected graph  Undirected edges G J F D A E C H 60

 Weighted graph  Weight (cost) is associated with each edge G J F D A E C H Q K N

 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

 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

 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

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

 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

 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 {1,2} {1,4} {2,3} {3,1} {4,2} 1  {2, 4} 2  {3} 3  {1} 4  {2}

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});

 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 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);}

Live Demo

Connecting the chain

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

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

 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

 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

Questions?