Graphs V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

Similar documents
March 20/2003 Jayakanth Srinivasan,

Binary Trees, Binary Search Trees


Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Introduction to Computers and Programming. Concept Question

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Chapter 20: Binary Trees

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

Graph Theory CS/Math231 Discrete Mathematics Spring2015

Chapter 10: Trees. A tree is a connected simple undirected graph with no simple circuits.

Data Structure - Binary Tree 1 -

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

Chapter 11: Graphs and Trees. March 23, 2008

Trees. (Trees) Data Structures and Programming Spring / 28

Definition of Graphs and Trees. Representation of Trees.

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

CSI33 Data Structures

Graph and Digraph Glossary

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Binary Trees

Graph Algorithms Using Depth First Search

CPCS Discrete Structures 1

Adjacent: Two distinct vertices u, v are adjacent if there is an edge with ends u, v. In this case we let uv denote such an edge.

CMSC th Lecture: Graph Theory: Trees.

CS 441 Discrete Mathematics for CS Lecture 26. Graphs. CS 441 Discrete mathematics for CS. Final exam

Binary Trees. Height 1

Chapter 2 Graphs. 2.1 Definition of Graphs

CE 221 Data Structures and Algorithms

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

Trees Algorhyme by Radia Perlman

DEFINITION OF GRAPH GRAPH THEORY GRAPHS ACCORDING TO THEIR VERTICES AND EDGES EXAMPLE GRAPHS ACCORDING TO THEIR VERTICES AND EDGES

Graphs. Introduction To Graphs: Exercises. Definitions:

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Discrete Structures CISC 2315 FALL Graphs & Trees

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

CSC Intro to Intelligent Robotics, Spring Graphs

Data Structure. IBPS SO (IT- Officer) Exam 2017

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

v V Question: How many edges are there in a graph with 10 vertices each of degree 6?

Tree Data Structures CSC 221

CHAPTER 2. Graphs. 1. Introduction to Graphs and Graph Isomorphism

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Trees. Truong Tuan Anh CSE-HCMUT

CSI33 Data Structures

Elements of Graph Theory

Data Structures and Algorithms (DSA) Course 9 Lists / Graphs / Trees. Iulian Năstac

Programming II (CS300)

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

Data Structures Brett Bernstein

Objectives. In this session, you will learn to:

Trees, Binary Trees, and Binary Search Trees

Foundations of Discrete Mathematics

Trees. CSE 373 Data Structures

MAT 7003 : Mathematical Foundations. (for Software Engineering) J Paul Gibson, A207.

Lecture 5: Graphs & their Representation

Undirected Graphs. Hwansoo Han

3.1 Basic Definitions and Applications

BBM 201 Data structures

Data and File Structures Laboratory

TREES. Trees - Introduction

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

Binary Trees. Examples:

Trees: examples (Family trees)

MATH 363 Final Wednesday, April 28. Final exam. You may use lemmas and theorems that were proven in class and on assignments unless stated otherwise.

Data Structures and Algorithms

ROOT: A node which doesn't have a parent. In the above tree. The Root is A.

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Discrete mathematics II. - Graphs

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Graphs Introduction and Depth first algorithm

Introduction to Binary Trees

DATA STRUCTURES USING C

Copyright 2000, Kevin Wayne 1

Each element of a binary tree is called a node of the tree. The following figure shows a binary tree with 9 nodes where A is the root.

Trees. Trees. CSE 2011 Winter 2007

Principles of Computer Science

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

Crossing bridges. Crossing bridges Great Ideas in Theoretical Computer Science. Lecture 12: Graphs I: The Basics. Königsberg (Prussia)

Graphs. Pseudograph: multiple edges and loops allowed

Why Do We Need Trees?

Tree Structures. A hierarchical data structure whose point of entry is the root node

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.

An undirected graph is a tree if and only of there is a unique simple path between any 2 of its vertices.

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

Graph Theory. ICT Theory Excerpt from various sources by Robert Pergl

Section 10.1: Graphs and Graph Models. Introduction to Graphs Definition of a Graph Types of Graphs Examples of Graphs

CS 234. Module 5. October 18, CS 234 Module 5 ADTS with items related by structure 1 / 25

Data Structures Question Bank Multiple Choice

Successor/Predecessor Rules in Binary Trees

Hamilton paths & circuits. Gray codes. Hamilton Circuits. Planar Graphs. Hamilton circuits. 10 Nov 2015

CS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department

ITEC2620 Introduction to Data Structures

CMPT 225. Binary Search Trees

CS204 Discrete Mathematics. 11 Trees. 11 Trees

Lesson 22: Basic Graph Concepts

Transcription:

Graphs and Trees 1 Graphs (simple) graph G = (V, ) consists of V, a nonempty set of vertices and, a set of unordered pairs of distinct vertices called edges. xamples V={,,,,} ={ (,),(,),(,), (,),(,),(,)}

Graphs and Trees 2 irected Graphs directed graph (or digraph) G = (V, ) consists of V, a nonempty set of vertices and, a set of ordered pairs of distinct vertices called edges. xamples

Graphs and Trees 3 Multigraphs multigraph (directed multigraph) G = (V, ) consists of V, a set of vertices,, a set of edges, and a function f from to {{u,v} : u v V } Two edges e 1 and e 2 with f(e 1 ) = f(e 2 ) are called multiple edges. Put simply, a multigraph G = (V, ) is a graph in which multiple edges are allowed. xamples

Graphs and Trees 4 Weighted Graphs weighted graph is a graph (or digraph) with the additional property that each edge e has associated with it a real number w(e) called its weight. weighted digraph is often called a network. xamples 3-6 12 7 0 3 4 4 1 3-2 4 11 0 3 3 2 7 6 4 8 1 5

Graphs and Trees 5 Psuedographs psuedograph G = (V, ) consists of V, a set of vertices, a set of edges, and a function f from to {{u, v} : u, v V }. Psuedo-multigraphs are defined similarly. Put another way, a psuedograph is a graph in which we allow loops, that is, edges from a vertex to itself. xamples

Graphs and Trees 6 Graph efinitions Summary There are several ways to categorize graphs: irected or undirected edges. Weighted or unweighted edges. llow multiple edges or not. llow loops or not. Unless specified, you can usually assume a graph does not allow multiple edges and loops. These aren t that common. For clarity, if a graph is not specified as weighted or directed, assume it isn t. The most common graphs we ll use are graphs, digraphs, weighted graphs, and networks. When writing graph algorithms, it is important to know what characteristics the graphs have. For instance, if a graph might have loops, the algorithm should be able to handle it.

Graphs and Trees 7 Graph Terminology Let u and v be vertices, and let e = {u, v} be an edge in an undirected graph G. The vertices u and v are said to be adjacent The edge e issaidtobeincident to u and v. The edge e is said to connect u and v. The vertices u and v are called the endpoints of the edge e. The degree of a vertex, denoted deg(v), in an undirected graph is the number of edges incident to it (where loops are counted twice). y w u z G 1 v x v u x y w z u y G 2 G 3 x w v

Graphs and Trees 8 xamples y w z u G 1 v x v u x y w z u y G 2 G 3 x w v G 1 G 2 G 3 deg(u)=3 deg(u)=2 deg(u)=2 deg(v)=5 deg(v)=3 deg(v)=4 deg(w)=3 deg(w)=2 deg(w)=3 deg(x)=2 deg(x)=4 deg(x)=2 deg(y)=2 deg(y)=3 deg(y)=3 deg(z)=3 deg(z)=2

Graphs and Trees 9 More Graph Terminology subgraph of a graph G = (V, ) is a graph G = (V, ) such that V V and. H 1 H 2 H 3 path is a sequence of vertices v 1, v 2,...,v k such that consecutive vertices v i and v i+1 are adjacent a b a b c c d abecde e d bedc e

Graphs and Trees 10 More Graph Terminology simple path is a path with no repeated vertices. a b c bec d e cycle is a simple path whose last vertex is the same as the first vertex. a b c cdec d e

Graphs and Trees 11 More Graph Terminology graph is called connected if there is a path between every pair of distinct vertices. a b a b c c d e d e connected not connected connected component of a graph is a maximal connected subgraph. e.g. the graph below has 3 connected components.

Graphs and Trees 12 Trees tree (or unrooted tree, or free tree) is a connected acyclic graph. That is, a graph with no cycles. forest is a collection of trees. tree tree tree tree These trees are not to be confused with rooted trees

Graphs and Trees 13 Spanning Tree spanning tree of G is a subgraph which is a tree and contains all of the vertices of G. G spanning tree of G

Graphs and Trees 14 Some Special Graphs K n : The complete graph on n vertices. K 2 K3 K 4 K 5 n : The cycle of length n. 3 4 5

Graphs and Trees 15 Q n : The n-cube. Q 1 Q 2 Q 3 Q 4

Graphs and Trees 16 ipartite Graphs: simple graph G is called bipartite if the vertex set V can be partitioned into two disjoint nonempty sets V 1 and V 2 such that every edge connects a vertex in V 1 to a vertex in V 2. Put another way, no edges in V 1 are connected to each other, and no edges in V 2 are connected to each other.

Graphs and Trees 17 Some Theorems Theorem 1: Let G = (V, ) be an undirected graph with edges. Then 2 = v V deg(v). Proof: Let X = {(e, v) : e, v V, e and v are incident}. We will compute X in two ways. ach edge e, is incident with exactly 2 vertices. Thus, X = 2e. lso, each vertex v V is incident with deg(v) edges. Thus, we have that X = v V deg(v). Setting these equal, we have the result orollary 2: n undirected graph has an even number of vertices of odd degree.

Graphs and Trees 18 xamples y w z u G 1 v x v u x y w z u y G 2 G 3 x w v Graph G 1 G 2 G 3 9 7 8 deg(v) 18 14 16 v V

Graphs and Trees 19 irected Graph Terminology Let u and v be vertices in a directed graph G, and let e = (u, v) be an edge in G. u is said to be adjacent to v. v is said to be adjacent from u. u is called the initial vertex of (u, v). v is called the terminal or end vertex of (u, v). The in-degree of u, denoted by deg (u), is the number of edges in G which have u as their terminal vertex. The out-degree of u, denoted by deg + (u), is the number of edges in G which have u as their initial vertex. Theorem 3: Let G = (V, ) be a directed graph. Then deg (v) = deg + (v) =. v V v V

xamples w x w y x v v v z u u u y G G 4 5 G 4 G 5 G 6 deg (u)=2 deg + (u)=4 deg (u)=1 deg + (u)=0 deg (u)=1 deg + (u)=1 deg (v)=2 deg + (v)=2 deg (v)=1 deg + (v)=2 deg (v)=2 deg + (v)=2 deg (w)=1 deg + (w)=1 deg (w)=1 deg + (w)=1 deg (w)=2 deg + (w)=2 deg (x)=2 deg + (x)=3 deg (x)=1 deg + (x)=1 deg (x)=1 deg + (x)=1 deg (y)=3 deg + (y)=0 deg (y)=2 deg + (y)=2 deg (y)=2 deg + (y)=2 deg (z)=1 deg + (z)=1 G 6 y w x Graphs and Trees 20

Graphs and Trees 21 Graph Representation There are two common ways of representing G. Let G = (V, ) be a graph with n vertices and m edges. djacency Lists For each vertex v in G, we store a list of vertices adjacent to v. This is often implemented using linked lists. a b a b c d d c e b c d e a e a d c a c e b c d For weighted graphs, an additional field can be stored in each node. The space required for storage is Θ(n + 2m) = Θ(n + m) for graphs, and Θ(n + m) = Θ(n + m) for digraphs.

Graphs and Trees 22 djacency Matrix Number the vertices 1, 2,..., n in some arbitrary order. We use a n by n matrix M defined as { 1 if (i, j) is an edge M(i, j) = 0 if (i, j) is not an edge If G is weighted, we store the weights in the matrix. For non-adjacent vertices, we store, or MX INT. It is clear that this representation requires Θ(n 2 ) space. a d c b e a a 0 b 1 c 1 d 1 e 0 b 1 0 0 0 1 c 1 0 0 1 1 d 1 0 1 0 1 e 0 1 1 1 0

Graphs and Trees 23 Some asic Graph Problems Is there a path from to? YLS: oes the graph contain a cycle? ONNTIVITY (SPNNING TR): Is there a way connect the vertices? IONNTIVITY: Will the graph become disconnected if one vertex is removed? PLNRITY: Is there a way to draw the graph without edges crossing? SHORTST PTH: What is the shortest way from to (weighted or unweighted)? LONGST PTH: What is the longest way from to (weighted or unweighted)? MINIML SPNNING TR: What is the best way connect the vertices? TRVLING SLSMN: What is the shortest route to connect the vertices without visiting the same vertex twice?

Graphs and Trees 24 (Rooted) Trees rooted tree is a tree which has a specially designated vertex called the root. In rooted trees, vertices are called nodes. ach node contains some information and one or more links to other nodes further down the hierarchy. (Similar to nodes in a linked list.) For convenience, we can think of trees as acyclic digraphs in which every edge points away from the root. Level 0 1 F G H I J 2 K L M 3

Graphs and Trees 25 Rooted Trees Terminology Level 0 1 F G H I J 2 K L M 3 node that is adjacent from v is a child of v. node that is adjacent to v is a parent of v. Two nodes who have the same parent are siblings. leaf or external node is a node with degree zero. (i.e. a node with no children) n internal node is a nonleaf node. (i.e. a node with at least one child) n ancestor of a node is any node on the path from the root to the node. descendant of a node v is any node which has v as an ancestor.

Graphs and Trees 26 Rooted Trees Terminology Level 0 1 F G H I J 2 K L M 3 The degree of a node is the number of children the node has. The depth of a node is the length of the path from the root to the node. The height of a node is the maximum length of a path from the node to a leaf. The height or depth of a tree is the maximum height of any node in the tree. The subtree rooted at x is the subtree consisting of x and all of its descendents.

Graphs and Trees 27 inary Tree binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left subtree and the right subtree Put another way, a binary tree is a rooted tree such that each node has no children, a right child, a left child, or both a right child and a left child. F G H I

Graphs and Trees 28 inary Trees: efinitions full binary tree is one in which internal nodes completely fill every level, except possibly the last. complete binary tree is a full binary tree where the internal nodes on the bottom level all appear to the left of the external nodes on that level. xample: full binary tree 1 2 3 4 5 6 7 8 9 11 13 14 15

Graphs and Trees 29 inary Tree xamples xample: complete binary tree 1 2 3 4 5 6 7 8 9 10 11 12 xample: totally complete binary tree 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Graphs and Trees 30 Properties of inary Trees The maximum number of nodes on level i of a binary tree is 2 i, i 1. The maximum number of nodes in a binary tree of depth k is 2 k+1 1, k 1. There is exactly one path connecting any two nodes in a tree tree with n nodes has n 1 edges The height of a full binary tree with n internal nodes is about log 2 n. F G H I

Graphs and Trees 31 inary Tree Representation: rrays Let G be a tree of height log 2 n with m nodes, where m n. We can represent G with an array of size n. [1] is the root, and given a node with index i, we can find the index of parents and children as follows: { i/2 if i 1 parent(i) = undefined if i = 1 { 2i if 2i n left(i) = undefined if 2i > n { 2i + 1 if 2i + 1 n right(i) = undefined if 2i + 1 > n 1 2 3 4 5 8 16 1 2 3 4 5 6 7 8 9 F G H I H I F G 1 2 3 4 5

Graphs and Trees 32 inary Tree Representation: Linked Lists We define a tree node by struct treenode{ int data; treenode *left_child; treenode *right_child; }; We find children by following the pointers. Parents are harder to find, unless we use doubly linked lists. root root ^ ^ ^ ^ ^ ^ F ^ ^ G ^ ^ ^ H ^ ^ I ^ ^ ^

Graphs and Trees 33 inary Tree Traversals When we visit each node in the tree exactly once, we say we have Traversed the tree. full traversal produces a linear order of the information in a tree. There are several ways to traverse a tree. preorder: visit a node, then traverse its left subtree, and then traverse its right subtree. inorder: traverse the left subtree, visit the node and then traverse its right subtree postorder: first traverse the left subtree, traverse the right subtree, and then visit the node. There is a natural correspondence between these traversals and producing the prefix, infix and postfix form of an expression.

Graphs and Trees 34 Tree Traversal preorder: visit a node, then traverse its left subtree, and then traverse its right subtree. inorder: traverse the left subtree, visit the node and then traverse its right subtree. postorder: first traverse the left subtree, traverse the right subtree. Preorder: + * * / 1 + Inorder: / * * + nfix form of the expression 3 * 2 * 17 18 19 14 Postorder: / * * + 4 / 11 15 16 5 8 12 13 6 7 9 10

Graphs and Trees 35 Tree Traversal: Implementation ssume we have used a linked list to implement a tree. struct tree_node{ int data; tree_node *left_child; tree_node *right_child; }; ssume we have a pointer to the root node. From this, we can traverse the tree with any of the methods.

Graphs and Trees 36 Preorder Traversal Visit a node, then traverse its left subtree, and then traverse its right subtree. void PreOrderTraversal(treenode *ptr) { if (ptr!=0) { cout << ptr->data; PreOrderTraversal(ptr->left); PreOrderTraversal(ptr->right); } } F G H I

Graphs and Trees 37 Postorder Traversal First traverse the left subtree, traverse the right subtree, and finally visit the node. void PostOrderTraversal(treenode *ptr) { if (ptr!=0) { PostOrderTraversal(ptr->left); PostOrderTraversal(ptr->right); cout << ptr->data; } } F G H I

Graphs and Trees 38 Inorder Traversal Traverse the left subtree, visit the node and then traverse its right subtree. void InOrderTraversal(treenode *ptr) { if (ptr!=0) { InOrderTraversal(ptr->left); cout << ptr->data; InOrderTraversal(ptr->right); } } F G H I