Graph Theory TreesAlgorithms. Graphs: Basic Definitions 4 n Let n be the number of nodes (stations) and e be the number of edges (links). n A graph is.

Slides:



Advertisements
Similar presentations
CS 336 March 19, 2012 Tandy Warnow.
Advertisements

Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
CMPS 2433 Discrete Structures Chapter 5 - Trees R. HALVERSON – MIDWESTERN STATE UNIVERSITY.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Copyright M.R.K. Krishna Rao 2003 Ch 9 - Trees Definition: A tree is a connected undirected graph with no simple circuits. Since a tree cannot have a.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Discrete Mathematics Transparency No. 8-1 Chapter 8 Trees.
Discussion #36 Spanning Trees
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank and Modified By Mingwu Chen Trees.
2/10/03Tucker, Sec Tucker, Applied Combinatorics, Sec. 3.2, Important Definitions Enumeration: Finding all of the possible paths in a rooted tree.
KNURE, Software department, Ph , N.V. Bilous Faculty of computer sciences Software department, KNURE The trees.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
Data Structures Using C++ 2E
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
ساختمانهای گسسته دانشگاه صنعتی شاهرود – اردیبهشت 1392.
BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS
LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6 th ed., by Kenneth.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Foundations of Discrete Mathematics
CSCI 115 Chapter 7 Trees. CSCI 115 §7.1 Trees §7.1 – Trees TREE –Let T be a relation on a set A. T is a tree if there exists a vertex v 0 in A s.t. there.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
 Rooted tree and binary tree  Theorem 5.19: A full binary tree with t leaves contains i=t-1 internal vertices.
5.5.2 M inimum spanning trees  Definition 24: A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible.
Trees Dr. B. Prabhakaran Application Examples Useful for locating items in a list Used in Huffman coding algorithm Study games like.
5.5.3 Rooted tree and binary tree  Definition 25: A directed graph is a directed tree if the graph is a tree in the underlying undirected graph.  Definition.
Discrete Structures Trees (Ch. 11)
Introduction to Graph Theory
Chap 8 Trees Def 1: A tree is a connected,undirected, graph with no simple circuits. Ex1. Theorem1: An undirected graph is a tree if and only if there.
Agenda Review: –Planar Graphs Lecture Content:  Concepts of Trees  Spanning Trees  Binary Trees Exercise.
Chapter 7. Trees Weiqi Luo ( 骆伟祺 ) School of Software Sun Yat-Sen University : Office : A309
5.5.2 M inimum spanning trees  Definition 24: A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
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.
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
Trees Thm 2.1. (Cayley 1889) There are nn-2 different labeled trees
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
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.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Discrete Mathematics Chapter 10 Trees. Outline 10.1 Introduction to Trees 10.2 Applications of Trees 10.3 Tree Traversal 10.4 Spanning Trees 10.5 Minimal.
Discrete Mathematics Chapter 10 Trees.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
5.6 Prefix codes and optimal tree Definition 31: Codes with this property which the bit string for a letter never occurs as the first part of the bit string.
Chapter 11. Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum Spanning.
Discrete Structures Li Tak Sing( 李德成 ) Lectures
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Discrete Mathematics Trees.
Non Linear Data Structure
CSCE 210 Data Structures and Algorithms
Chapter 5 : Trees.
Binary Search Tree (BST)
12. Graphs and Trees 2 Summary
Section 8.1 Trees.
Binary Trees, Binary Search Trees
Graph Algorithms Using Depth First Search
Binary Trees, Binary Search Trees
5.4 T-joins and Postman Problems
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

Graph Theory TreesAlgorithms

Graphs: Basic Definitions 4 n Let n be the number of nodes (stations) and e be the number of edges (links). n A graph is connected if there is a sequence of nodes and edges between every pair of nodes. n A cycle is a sequence of nodes and edges visiting none more than once, except for the first/last one n Degree: # of edges incident on node

Trees 6 n A tree is a simple graph having the property that there is a unique path between every pair of nodes. n A rooted tree is one in which a particular node is designated as the root n A node in a rooted tree is a leaf if it is at root i (i>=0) and not adjacent to any nodes at level i+1 n A tree which is a subgraph of a graph G and contains every node of G is called a spanning tree of G n A node which is not a leaf is an internal node n Chartrand p. 87/ #1,2

Trees (cont.)

Spanning and Rooted trees n Petersen Graph (find spanning trees—Maple counttrees) n Rooted tree n Rooted tree 2

Theorem: Let T be a simple graph with n nodes. TFAE: n (a) T is a tree n (b) T is connected and acyclic n (c)T is connected and has n-1 edges n (d) T has n-1 edges and is acyclic

Proof (partial) n (a)  (b) n There is a path between every pair of nodes (definition of tree) n Hence, T is connected n If T had a cycle, then there would exist at least two paths from node a to node b n But the path from a to b is unique (by definition) n Hence, T is acyclic. nnnn n (b)  (c),(c)  (d)

Proof (partial--continued) n To show (d)  (a) n Assume acyclic and n-1 edges n RTS:tree(unique path betw. every node pair) n WTS:T connected n Assume not; then T1,T2,…Tk are components (disjoint connected subgraphs) n Let Ti have ni nodes; no cycles, so each must have ni-1 edges n n-1=(n1-1)+(n2-1)+(n3-1)+…+(nk-1) < (n1+n2+n3+…+nk)-1=n-1 n  Contradiction; T connected, so T is a tree

Uses of trees 9 n Ex/ Huffman codes n ASCII-each character encoded in a 7-bit string n A: n B: n C: n 1: n 2: n !: n *:

Uses of trees (cont) n See board (BNF 15.3,15.4)

Depth –first search n Suppose we wish to search the nodes of a graph, beginning at a specified node. Two types of strategy could be used: –forge ahead, moving on to a new node whenever one is available –Spread out—checking all nodes at a each level before moving on n First strategy: depth-first search

DFS algorithm n This algorithm will assign labels to nodes and select edges of a graph G.  is the set of nodes with labels,  is the set of edges selected, and the predecessor of a node Y is a node in  that is used in labeling Y.

DFS algorithm n STEP 1: (Start) Pick a node u and assign it label 1. Let k=1,  ={u}, and  =  and X=u. n STEP 2:(check for completion) If  contains each node of G, then Stop—G connected n STEP 3:(Find next node and edge) Find node Y not in  that is adjacent to X; if no such node, go to Step 4. Otherwise, put{X,Y} in , increment k to k+1, assign the label k to Y, and put Y in . Replace X by Y, and go to Step 2 n (Back up) If X=u, then Stop. G is not connected. Else, replace X by the predecessor of X and go to Step 3

BFS algorithm n This algorithm will find a spanning tree, if it exists, for a graph on n nodes. In the algorithm,  is the set of nodes with labels and  is the set of edges connecting nodes in .

BFS algorithm n STEP 1: (Start) Pick a node u and assign it label 0. Let  ={u}, and  =  and X=u. n STEP 2:(check for completion) If |  |<n, go to Step 3. Otherwise, if |  |=n, stop; the edges in  and the nodes in  form a spanning tree for G. n STEP 3: (label the nodes) Find the nodes not in  that are adjacent to nodes in  having the largest label number; call it k. If there are no such nodes, then the graph has no spanning tree. Otherwise, assign the newly found nodes the label k+1, and put them in . For each new node with label k+1, place in  one edge joining this node to a node with label k. If there is more than one such edge, choose one arbitrarily. Go to Step 2.

Minimum Weight Spanning Tree algorithm (Kruskal) n This algorithm will find a minimal spanning tree, if one exists, for a weighted graph G with n nodes, e edges.

MWST algorithm--Kruskal n STEP 1: (Start) If no edges, G is not connected; else, pick an edge with the smallest weight (ties can be broken arbitrarily). Place edge in E and node in N. n STEP 2:(check for completion) If E contains n-1 edges, Stop; have MWST; else, go to Step 3. n STEP 3: (pick next edge) Find the edges of smallest weight which do not form a cycle with any of the edges of E. If no such edges, G is not connected and there is no spanning tree. Else, choose one such edge and place it in E and the nodes in N. Go to Step 2.

Binary trees and traversals 6 n Binary Tree: a rooted tree in which each node has at most two children and each child is designated left child or right child. n Thus in a binary tree, each node may have 0,1, or 2 children n Left child—left and below parent n Right child—right and below parent n The left subtree of a node N in a binary tree is the graph formed by the left child L, the descendants of L, and the edges connecting these nodes n Right subtree—defined similarly

Binary trees and traversals 6 n A is the root, A has two children, left child B and right child C n Node B has one child, left child D n Node C has right child E but no left child.

Rooted Tree D B A C

Expression trees n 4.6. n Polish notation—Polish mathematician Lukasiewicz—no parens needed n RT4

Traversal 4 n Traversal: visit each node of a graph exactly once n BFS/DFS—traversal of a connected graph— nodes are “visited”, i.e., labeled, exactly once n A preorder traversal of a binary tree is characterized by visiting the parent before the children and the left child before the right child n Listing the nodes in the order they are visited is called a n Listing the nodes in the order they are visited is called a preorder listing

Preorder Traversal (i.e., DFS w/ choosing left before right) 4 n STEP 1: (Visit) Visit the root n STEP 2:(go left) Go to the left subtree, if one exists, and do a preorder traversal n STEP 3: Go to the right subtree, if one exists, and do a preorder traversal. n 4.6.4,4.6.5

Postorder Traversal 6 n RPN—Reverse Polish Notation n Operation sign is followed by the operands (HP calculator) n (2-3*4)+(4+8/2)=-2 n Pre: +-2*34+4/82 Preorder—look for operation sign followed by two numbers n Post: 234*-482/++ –Postorder—look for two condecutive numbers followed by an operation n By using a traversal called postorder, can obtain the RPN for an expression

Postorder Traversal (child before parent, left before right) 4 n STEP 1: (Start) Go to the root n STEP 2:(go left) Go to the left subtree, if one exists, and do a postorder traversal n STEP 3: (go right) Go to the right subtree, if one exists, and do a postorder traversal. n Step 4: (Visit) Visit the root n 4.6.7, 4.6.8

Binary Search Tree n (to determine if an element a is in a binary search tree) n Step 1(Start) Let V be the root of the binary search tree. n Step 2 (compare) If a = V, then A is in the tree; STOP. Else, go to Step 3 n Step 3 (if smaller, go left) If V<=a, go to Step 4. Otherwise, a<=V –If no left child of V, a is not in tree. STOP –Else, V has left child L; let V=L and go to Step 2 n Step 4 (if larger, go right)