Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Graph Theory Prudence Wong.

Slides:



Advertisements
Similar presentations
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1.
Advertisements

Introduction to Graphs
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
Data Structures Using C++
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Graphs COL 106 Slide Courtesy : Douglas W. Harder, U Waterloo.
Graphs.
Advanced Data Structures
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph & BFS.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
GRAPH Learning Outcomes Students should be able to:
Intro to Graphs CSIT 402 Data Structures II. CSIT 402 Graph Introduction2 Graphs Graphs are composed of ›Nodes (vertices) Can be labeled ›Edges (arcs)
Chapter 2 Graph Algorithms.
Representing and Using Graphs
Foundations of Discrete Mathematics
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
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.
1 CS104 : Discrete Structures Chapter V Graph Theory.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Lecture 13 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Graphs.  Definition A simple graph G= (V, E) consists of vertices, V, a nonempty set of vertices, and E, a set of unordered pairs of distinct elements.
Week 11 - Monday.  What did we talk about last time?  Binomial theorem and Pascal's triangle  Conditional probability  Bayes’ theorem.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Unit – V Graph theory. Representation of Graphs Graph G (V, E,  ) V Set of vertices ESet of edges  Function that assigns vertices {v, w} to each edge.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
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 Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
Discrete Structures CISC 2315 FALL 2010 Graphs & Trees.
Graphs Upon completion you will be able to:
Union By Rank Ackermann’s Function Graph Algorithms Rajee S Ramanikanthan Kavya Reddy Musani.
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.
Chapter 9: Graphs.
Graphs and Paths : Chapter 15 Saurav Karmakar
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.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
1 GRAPH Learning Outcomes Students should be able to: Explain basic terminology of a graph Identify Euler and Hamiltonian cycle Represent graphs using.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
1 Lecture 5 (part 2) Graphs II (a) Circuits; (b) Representation Reading: Epp Chp 11.2, 11.3
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
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.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Graph Theory Prudence Wong
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
COMP108 Algorithmic Foundations Graph Theory
Chapter 12 Abstract Data Type.
Data Structures Graphs - Terminology
Depth-First Search.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
COMP108 Algorithmic Foundations Graph Theory
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Graph Theory Prudence Wong

Algorithmic Foundations COMP108 How to Measure 4L? You can pour water from one container to another a 3L container & a 5L container (without mark) infinite supply of water How to measure 4L of water? 3L 5L

Algorithmic Foundations COMP108 3 (Graph) Learning outcomes  Able to tell what is an undirected graph and what is a directed graph  Know how to represent a graph using matrix and list  Understand what Euler circuit and able to determine whether such circuit exists in an undirected graph  Able to apply BFS and DFS to traverse a graph  Able to tell what a tree is

Algorithmic Foundations COMP108 Graph …

Algorithmic Foundations COMP108 5 (Graph) Graphs Graph theory – an old subject with many modern applications. introduced in the 18th century An undirected graph G=(V,E) consists of a set of vertices V and a set of edges E. Each edge is an unordered pair of vertices. (E.g., {b,c} & {c,b} refer to the same edge.) A directed graph G=(V,E) consists of … Each edge is an ordered pair of vertices. (E.g., (b,c) refer to an edge from b to c.) ab cd e f ab cd e f

Algorithmic Foundations COMP108 6 (Graph) Applications of graphs In computer science, graphs are often used to model  computer networks,  precedence among processes,  state space of playing chess (AI applications)  resource conflicts, … In other disciplines, graphs are also used to model the structure of objects. E.g.,  biology - evolutionary relationship  chemistry - structure of molecules

Algorithmic Foundations COMP108 7 (Graph) Undirected graphs Undirected graphs:  simple graph: at most one edge between two vertices, no self loop (i.e., an edge from a vertex to itself).  multigraph: allows more than one edge between two vertices. ab cd e f Reminder: An undirected graph G=(V,E) consists of a set of vertices V and a set of edges E. Each edge is an unordered pair of vertices.

Algorithmic Foundations COMP108 8 (Graph) Undirected graphs In an undirected graph G, suppose that e = {u, v} is an edge of G  u and v are said to be adjacent and called neighbors of each other.  u and v are called endpoints of e.  e is said to be incident with u and v.  e is said to connect u and v.  The degree of a vertex v, denoted by deg(v), is the number of edges incident with it (a loop contributes twice to the degree) u v e deg(u) = 1 deg(v) = 2 w

Algorithmic Foundations COMP108 9 (Graph) Representation (of undirected graphs) An undirected graph can be represented by adjacency matrix, adjacency list, incidence matrix or incidence list. Adjacency matrix and adjacency list record the relationship between vertex adjacency, i.e., a vertex is adjacent to which other vertices Incidence matrix and incidence list record the relationship between edge incidence, i.e., an edge is incident with which two vertices

Algorithmic Foundations COMP108 Data Structure - Matrix Rectangular / 2-dimensional array  m-by-n matrix m rows n columns  a i,j row i, column j 10 (Graph) m-by-n matrix a i,j n columns a 1,1 a 1,2 a 1,3 … a 1,n a 2,1 a 2,2 a 2,3 … a 2,n m rows a 3,1 a 3,2 a 3,3 … a 3,n a m,1 a m,2 a m,3 … a m,n

Algorithmic Foundations COMP108 Data Structure - Linked List List of elements (nodes) connected together like a chain Each node contains two fields:  "data" field: stores whatever type of elements  "next" field: pointer to link this node to the next node in the list Head / Tail  pointer to the beginning & end of list 11 (Graph) datanext head tail

Algorithmic Foundations COMP108 Data Structure - Linked List Queue (FIFO: first-in-first-out) Insert element (enqueue) to tail Remove element (dequeue) from head 12 (Graph) headtail Insert headtail 40 create newnode of 40; tail.next = newnode; tail = tail.next Remove headtail 40 return whatever head points to; head = head.next

Algorithmic Foundations COMP (Graph) Adjacency matrix / list Adjacency matrix M for a simple undirected graph with n vertices is an nxn matrix  M(i, j) = 1 if vertex i and vertex j are adjacent  M(i, j) = 0 otherwise Adjacency list: each vertex has a list of vertices to which it is adjacent ab cd e abcdeabcde a00110a00110 b00110b00110 c11011c11011 d11101d11101 e00110e00110 a b c d e c a c c d d b d d e a c b e

Algorithmic Foundations COMP (Graph) Representation (of undirected graphs) An undirected graph can be represented by adjacency matrix, adjacency list, incidence matrix or incidence list. Adjacency matrix and adjacency list record the relationship between vertex adjacency, i.e., a vertex is adjacent to which other vertices Incidence matrix and incidence list record the relationship between edge incidence, i.e., an edge is incident with which two vertices

Algorithmic Foundations COMP (Graph) Incidence matrix / list Incidence matrix M for a simple undirected graph with n vertices and m edges is an mxn matrix  M(i, j) = 1 if edge i and vertex j are incidence  M(i, j) = 0 otherwise Incidence list: each edge has a list of vertices to which it is incident with ab cd e abcdeabcde a b a c c c d d b d 6 7 c e d e labels of edge are edge number

Algorithmic Foundations COMP (Graph) Exercise Give the adjacency matrix and incidence matrix of the following graph ab def c labels of edge are edge number abcdefabcdef a000110a b001100b c010000c d110000d e100001e f000010f abcdefabcdef

Algorithmic Foundations COMP108 Directed graph …

Algorithmic Foundations COMP (Graph) Directed graph Given a directed graph G, a vertex a is said to be connected to a vertex b if there is a path from a to b. E.g., G represents the routes provided by a certain airline. That means, a vertex represents a city and an edge represents a flight from a city to another city. Then we may ask question like: Can we fly from one city to another? Reminder: A directed graph G=(V,E) consists of a set of vertices V and a set of edges E. Each edge is an ordered pair of vertices. ab cd e E = { (a,b), (b,d), (b,e), (c,b), (c,e), (d,e) } N.B. (a,b) is in E, but (b,a) is NOT

Algorithmic Foundations COMP (Graph) In/Out degree (in directed graphs) The in-degree of a vertex v is the number of edges leading to the vertex v. The out-degree of a vertex v is the number of edges leading away from the vertex v. vin-deg(v)out-deg(v) a01a01 b22b22 c02c02 d11d11 e30e30 sum:66Always equal? ab cd e

Algorithmic Foundations COMP (Graph) Representation (of directed graphs) Similar to undirected graph, a directed graph can be represented by adjacency matrix, adjacency list, incidence matrix or incidence list.

Algorithmic Foundations COMP (Graph) Adjacency matrix / list Adjacency matrix M for a directed graph with n vertices is an nxn matrix  M(i, j) = 1 if (i,j) is an edge  M(i, j) = 0 otherwise Adjacency list:  each vertex u has a list of vertices pointed to by an edge leading away from u abcdeabcde a01000a01000 b00011b00011 c01001c01001 d00001d00001 e00000e00000 a b c d e d b b e e e ab cd e

Algorithmic Foundations COMP (Graph) Incidence matrix / list Incidence matrix M for a directed graph with n vertices and m edges is an mxn matrix  M(i, j) = 1 if edge i is leading away from vertex j  M(i, j) = -1 if edge i is leading to vertex j Incidence list: each edge has a list of two vertices (leading away is 1st and leading to is 2nd) abcdeabcde c b a d b d e b b e 6 c e ab cd e

Algorithmic Foundations COMP (Graph) 4 Exercise Give the adjacency matrix and incidence matrix of the following graph ab def c labels of edge are edge number abcdefabcdef a000110a b001000b c000000c d010000d e000001e f001000f abcdefabcdef

Algorithmic Foundations COMP108 Euler circuit …

Algorithmic Foundations COMP (Graph) Paths, circuits (in undirected graphs)  In an undirected graph, a path from a vertex u to a vertex v is a sequence of edges e 1 = {u, x 1 }, e 2 = {x 1, x 2 }, …e n = {x n-1, v}, where n≥1.  The length of this path is n.  Note that a path from u to v implies a path from v to u.  If u = v, this path is called a circuit (cycle). u v e1e1 e2e2 enen

Algorithmic Foundations COMP (Graph) Euler circuit A simple circuit visits an edge at most once. An Euler circuit in a graph G is a circuit visiting every edge of G exactly once. (NB. A vertex can be repeated.) Does every graph has an Euler circuit ? ab cd e a c b d e c d a ab cd e no Euler circuit

Algorithmic Foundations COMP (Graph) no Euler circuit History: In Konigsberg, Germany, a river ran through the city and seven bridges were built. The people wondered whether or not one could go around the city in a way that would involve crossing each bridge exactly once a b c d bridges a b c d river banks

Algorithmic Foundations COMP (Graph) no Euler circuit History: In Konigsberg, Germany, a river ran through the city and seven bridges were built. The people wondered whether or not one could go around the city in a way that would involve crossing each bridge exactly once a b c d bridges a b c d river banks How to determine whether there is an Euler circuit in a graph?

Algorithmic Foundations COMP (Graph) Necessary and sufficient condition Let G be a connected graph. Lemma: G contains an Euler circuit if and only if degree of every vertex is even.

Algorithmic Foundations COMP (Graph) Hamiltonian circuit Let G be an undirected graph. A Hamiltonian circuit is a circuit containing every vertex of G exactly once. Note that a Hamiltonian circuit may NOT visit all edges. Unlike the case of Euler circuits, determining whether a graph contains a Hamiltonian circuit is a very difficult problem. (NP-hard)

Algorithmic Foundations COMP108 Breadth First Search BFS …

Algorithmic Foundations COMP108 Walking in a maze 32 (Graph)

Algorithmic Foundations COMP (Graph) Breadth First Search (BFS) All vertices at distance k from s are explored before any vertices at distance k+1. ab def c The source is a. Order of exploration a, ghk

Algorithmic Foundations COMP (Graph) Breadth First Search (BFS) All vertices at distance k from s are explored before any vertices at distance k+1. ab def c Order of exploration a, b, e, d ghk Distance 1 from a. The source is a.

Algorithmic Foundations COMP (Graph) Breadth First Search (BFS) All vertices at distance k from s are explored before any vertices at distance k+1. ab def c The source is a. Order of exploration a, b, e, d, c, f, h, g ghk Distance 2 from a.

Algorithmic Foundations COMP (Graph) Breadth First Search (BFS) All vertices at distance k from s are explored before any vertices at distance k+1. ab def c The source is a. Order of exploration a, b, e, d, c, f, h, g, k ghk Distance 3 from a.

Algorithmic Foundations COMP (Graph) In general (BFS) … Explore dist 0 frontier s distance 0

Algorithmic Foundations COMP (Graph) … In general (BFS) … Explore dist 1 frontier s distance 0 distance 1

Algorithmic Foundations COMP (Graph) … In general (BFS) … distance 0 Explore dist 2 frontier s distance 1 … distance 2

Algorithmic Foundations COMP (Graph) Breadth First Search (BFS) A simple algorithm for searching a graph. Given G=(V, E), and a distinguished source vertex s, BFS systematically explores the edges of G such that  all vertices at distance k from s are explored before any vertices at distance k+1.

Algorithmic Foundations COMP108 Apply BFS to the following graph starting from vertex a and list the order of exploration 41 (Graph) Exercise ab def c a, d, e, f, b, c

Algorithmic Foundations COMP108 Apply BFS to the following graph starting from vertex a and list the order of exploration 42 (Graph) Exercise (2) a d bce f a, b, c, e, d, f, g g a, c, e, b, g, d, f

Algorithmic Foundations COMP (Graph) BFS – Pseudo code unmark all vertices choose some starting vertex s mark s and insert s into tail of list L while L is nonempty do begin remove a vertex v from front of L visit v for each unmarked neighbor w of v do mark w and insert w into tail of list L end

Algorithmic Foundations COMP108 BFS using linked list 44 (Graph) ab def c ghk a headtail b headtail ed e headtail dc a, b, e, d, c, f, h, g, k f headtail dcf c headtail fhg headtail fhg headtail hgk & so on …

Algorithmic Foundations COMP108 Depth First Search DFS …

Algorithmic Foundations COMP (Graph) Depth First Search (DFS) Edges are explored from the most recently discovered vertex, backtracks when finished Order of exploration a, DFS searches "deeper" in the graph whenever possible ab def c The source is a. ghk

Algorithmic Foundations COMP (Graph) Depth First Search (DFS) Edges are explored from the most recently discovered vertex, backtracks when finished Order of exploration a, DFS searches "deeper" in the graph whenever possible ab def c The source is a. ghk search space

Algorithmic Foundations COMP (Graph) Depth First Search (DFS) Edges are explored from the most recently discovered vertex, backtracks when finished Order of exploration a, b DFS searches "deeper" in the graph whenever possible ab def c The source is a. ghk search space

Algorithmic Foundations COMP (Graph) Depth First Search (DFS) Edges are explored from the most recently discovered vertex, backtracks when finished Order of exploration a, b, c DFS searches "deeper" in the graph whenever possible ab def c The source is a. ghk search space is empty

Algorithmic Foundations COMP (Graph) Depth First Search (DFS) Edges are explored from the most recently discovered vertex, backtracks when finished Order of exploration a, b, c DFS searches "deeper" in the graph whenever possible ab def c The source is a. ghk search space nowhere to go, backtrack

Algorithmic Foundations COMP (Graph) Depth First Search (DFS) Edges are explored from the most recently discovered vertex, backtracks when finished Order of exploration a, b, c, f DFS searches "deeper" in the graph whenever possible ab def c The source is a. ghk search space

Algorithmic Foundations COMP (Graph) search space is empty Depth First Search (DFS) Edges are explored from the most recently discovered vertex, backtracks when finished Order of exploration a, b, c, f, k DFS searches "deeper" in the graph whenever possible ab def c The source is a. ghk

Algorithmic Foundations COMP (Graph) Depth First Search (DFS) Edges are explored from the most recently discovered vertex, backtracks when finished Order of exploration a, b, c, f, k DFS searches "deeper" in the graph whenever possible ab def c The source is a. ghk backtrack search space

Algorithmic Foundations COMP (Graph) empty search space Depth First Search (DFS) Edges are explored from the most recently discovered vertex, backtracks when finished Order of exploration a, b, c, f, k, e DFS searches "deeper" in the graph whenever possible ab def c The source is a. ghk

Algorithmic Foundations COMP (Graph) Depth First Search (DFS) Edges are explored from the most recently discovered vertex, backtracks when finished Order of exploration a, b, c, f, k, e DFS searches "deeper" in the graph whenever possible ab def c The source is a. ghk backtrack search space

Algorithmic Foundations COMP (Graph) Depth First Search (DFS) Edges are explored from the most recently discovered vertex, backtracks when finished Order of exploration a, b, c, f, k, e, d DFS searches "deeper" in the graph whenever possible ab def c The source is a. ghk search space

Algorithmic Foundations COMP (Graph) search space is empty Depth First Search (DFS) Edges are explored from the most recently discovered vertex, backtracks when finished Order of exploration a, b, c, f, k, e, d, h DFS searches "deeper" in the graph whenever possible ab def c The source is a. ghk

Algorithmic Foundations COMP (Graph) Depth First Search (DFS) Edges are explored from the most recently discovered vertex, backtracks when finished Order of exploration a, b, c, f, k, e, d, h DFS searches "deeper" in the graph whenever possible ab def c The source is a. ghk search space backtrack

Algorithmic Foundations COMP (Graph) search space is empty Depth First Search (DFS) Edges are explored from the most recently discovered vertex, backtracks when finished Order of exploration a, b, c, f, k, e, d, h, g DFS searches "deeper" in the graph whenever possible ab def c The source is a. ghk

Algorithmic Foundations COMP (Graph) Depth First Search (DFS) Edges are explored from the most recently discovered vertex, backtracks when finished Order of exploration a, b, c, f, k, e, d, h, g DFS searches "deeper" in the graph whenever possible ab def c The source is a. ghk backtrack DONE!

Algorithmic Foundations COMP (Graph) Depth First Search (DFS) Depth-first search is another strategy for exploring a graph; it search "deeper" in the graph whenever possible.  Edges are explored from the most recently discovered vertex v that still has unexplored edges leaving it.  When all edges of v have been explored, the search "backtracks" to explore edges leaving the vertex from which v was discovered.

Algorithmic Foundations COMP108 Apply DFS to the following graph starting from vertex a and list the order of exploration 62 (Graph) Exercise ab def c a, d, b, c, e, f a, e, f, d, b, c a, f, e, d, b, c a, f, d, b, c, e??

Algorithmic Foundations COMP108 Apply DFS to the following graph starting from vertex a and list the order of exploration 63 (Graph) Exercise (2) a d bc e f a, b, d, f, e, c, g g a, b, f, e, d, c, g a, c, g, b, d, f, e a, c, g, b, f, e, d a, c, g, e, f, b, d a, e, f, b, d, c, g a, e, b, …?a, b, f, d, c, …?

Algorithmic Foundations COMP (Graph) DFS – Pseudo code (recursive) Algorithm DFS(vertex v) visit v for each unvisited neighbor w of v do begin DFS(w) end

Algorithmic Foundations COMP108 Tree …

Algorithmic Foundations COMP (Graph) Outline  What is a tree?  What are subtrees  How to traverse a binary tree?  Pre-order, In-order, Postorder  Application of tree traversal

Algorithmic Foundations COMP (Graph) Trees An undirected graph G=(V,E) is a tree if G is connected and acyclic (i.e., contains no cycles) Other equivalent statements: 1. There is exactly one path between any two vertices in G 2. G is connected and removal of one edge disconnects G 3. G is acyclic and adding one edge creates a cycle 4. G is connected and m=n-1 (where |V|=n, |E|=m)

Algorithmic Foundations COMP (Graph) Rooted trees Tree with hierarchical structure, e.g., directory structure of file system C:\ Program Files My Documents Microsoft Office Internet Explorer My Pictures My Music

Algorithmic Foundations COMP (Graph) Terminologies r abc defhg sqpk  Topmost vertex is called the root.  A vertex u may have some children directly below it, u is called the parent of its children.  Degree of a vertex is the no. of children it has. (N.B. it is different from the degree in an unrooted tree.)  Degree of a tree is the max. degree of all vertices.  A vertex with no child (degree-0) is called a leaf. All others are called internal vertices. root r is parent of a, b & c; a, b, & c are children of r deg-0: d, k, p, g, q, s (leaves) deg-1: b, e, f deg-2: a, c, h deg-3: r What is the degree of this tree?

Algorithmic Foundations COMP (Graph) More terminologies r abc defhg sqpk  We can define a tree recursively  A single vertex is a tree.  If T 1, T 2, …, T k are disjoint trees with roots r 1, r 2, …, r k, the graph obtained by attaching a new vertex r to each of r 1, r 2, …, r k with a single edge forms a tree T with root r.  T 1, T 2, …, T k are called subtrees of T. T1T1 T2T2 T3T3 T1T1 T2T2 T3T3 r three subtrees which are the roots of the subtrees?

Algorithmic Foundations COMP (Graph) Binary tree  a tree of degree at most TWO  the two subtrees are called left subtree and right subtree (may be empty) r ab cdfe khg left subtreeright subtree There are three common ways to traverse a binary tree:  preorder traversal - vertex, left subtree, right subtree  inorder traversal - left subtree, vertex, right subtree  postorder traversal - left subtree, right subtree, vertex

Algorithmic Foundations COMP (Graph) Traversing a binary tree r ab cdfe khg preorder traversal - vertex, left subtree, right subtree r -> a -> c -> d -> g -> b -> e -> f -> h -> k r ab cdfe khg

Algorithmic Foundations COMP (Graph) Traversing a binary tree r ab cdfe khg preorder traversal - vertex, left subtree, right subtree r -> a -> c -> d -> g -> b -> e -> f -> h -> k inorder traversal - left subtree, vertex, right subtree c -> a -> g -> d -> r -> e -> b -> h -> f -> k r ab cdfe khg

Algorithmic Foundations COMP (Graph) Traversing a binary tree r ab cdfe khg preorder traversal - vertex, left subtree, right subtree r -> a -> c -> d -> g -> b -> e -> f -> h -> k inorder traversal - left subtree, vertex, right subtree c -> a -> g -> d -> r -> e -> b -> h -> f -> k r ab cdfe khg postorder traversal - left subtree, right subtree, vertex c -> g -> d -> a -> e -> h -> k -> f -> b -> r

Algorithmic Foundations COMP (Graph) hg Example Give the order of traversal of preorder, inorder, and postorder traversal of the tree r ab cdfe nm k preorder:r, a, c, g, h, d, b, e, k, f, m, n inorder:g, c, h, a, d, r, k, e, b, m, f, n postorder:g, h, c, d, a, k, e, m, n, f, b, r

Algorithmic Foundations COMP108 Binary Search Tree 76 (Graph) for a vertex with value X, left child has value  X & right child has value > X which traversal gives numbers in ascending order? inorder traversal

Algorithmic Foundations COMP108 Expression Tree 77 (Graph) (2+5*4)*3 * + * postorder traversal gives * + 3 * 1.push numbers onto stack 2.when operator is encountered, pop 2 numbers, operate on them & push results back to stack 3.repeat until the expression is exhausted

Algorithmic Foundations COMP108 Data Structure - Stack Data organised in a vertical manner LIFO: last-in-first-out Top: top of stack Operations: push & pop  push: adds a new element on top of stack  pop: remove the element from top of stack 78 (Graph) top

Algorithmic Foundations COMP108 Data Structure - Stack 79 (Graph) top++; stack[top] = newvalue top top 40 Push 40 return stack[top]; top top Pop top return stack[top]; top-- Pop