Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Similar documents
Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Binary Trees. Recursive definition. Is this a binary tree?

Binary search trees :

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Elementary Graph Algorithms

Graph Representation

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

Graph: representation and traversal

Binary Search Trees. 1. Inorder tree walk visit the left subtree, the root, and right subtree.

Binary search trees. Support many dynamic-set operations, e.g. Search. Minimum. Maximum. Insert. Delete ...

Outlines: Graphs Part-2

Graph Search. Adnan Aziz

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

Design and Analysis of Algorithms

Representations of Graphs

Lecture 6: Analysis of Algorithms (CS )

Basic Graph Algorithms

Chapter 22. Elementary Graph Algorithms

Graph Algorithms. Definition

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Graph representation

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

Basic Graph Algorithms (CLRS B.4-B.5, )

CSI 604 Elementary Graph Algorithms

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

DFS & STRONGLY CONNECTED COMPONENTS

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 17. Depth-First Search. DFS (Initialize and Go) Last Time Depth-First Search

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Introduction to Algorithms. Lecture 11

COT 6405 Introduction to Theory of Algorithms

Lecture: Analysis of Algorithms (CS )

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

Minimum Spanning Trees Ch 23 Traversing graphs

Binary Search Trees. Antonio Carzaniga. Faculty of Informatics University of Lugano. October 22, Antonio Carzaniga 1

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 16. Graph Search Algorithms. Recall BFS

Practical Session No. 12 Graphs, BFS, DFS Topological Sort

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Elementary Graph Algorithms

Data Structures. Dynamic Sets

Course goals. exposure to another language. knowledge of specific data structures. impact of DS design & implementation on program performance

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

Review: Graph Theory and Representation

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

csci 210: Data Structures Graph Traversals

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department

Graphs. Graphs. Representation of Graphs. CSE 680 Prof. Roger Crawfis. Two standard ways. Graph G = (V, E)» V = set of vertices

Final Exam. EECS 2011 Prof. J. Elder - 1 -

CMSC 441: Algorithms. Hillol Kargupta, Professor.

Design and Analysis of Algorithms

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

Graph implementations :

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Announcements Problem Set 4 is out!

Suggested Study Strategy

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

Announcements. HW3 is graded. Average is 81%

W4231: Analysis of Algorithms

12 Binary Search Tree

22.3 Depth First Search

CS521 \ Notes for the Final Exam

CSC263 Week 8. Larry Zhang.

Graph Algorithms Using Depth First Search

CS24 Week 8 Lecture 1

Binary Trees, Binary Search Trees

Graph: representation and traversal

Analysis of Algorithms

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

Proof: if not f[u] < d[v], then u still grey while v is being visited. DFS visit(v) will then terminate before DFS visit(u).

Binary Search Trees. Motivation. Binary search tree. Tirgul 7

Searching: Introduction

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

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

Binary Search Trees. July 13th, Computer Information Systems. c 2009 Vaidė Narváez

Topic 12: Elementary Graph Algorithms

TREES. Trees - Introduction

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search


Course Review for Finals. Cpt S 223 Fall 2008

Binary Trees

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

Binary Search Trees, etc.

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

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Unit 2: Algorithmic Graph Theory

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Trees. Truong Tuan Anh CSE-HCMUT

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

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

March 20/2003 Jayakanth Srinivasan,

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Transcription:

Trees and Graphs

Basic Definitions Tree: Any connected, acyclic graph G = (V,E) E = V -1 n-ary Tree: Tree s/t all vertices of degree n+1 A root has degree n Binary Search Tree: A binary tree such that If node y is the left child of node x, key[y] key[x] If node z is the right child of node x, key[x] key[z] Inorder Traversal (for Binary Trees): Recursively process left child, process root, recursively process right child Takes Θ( V ) time Used with a BST to print values in sorted order Other traversal methods include preorder and postorder

Searching a BST Tree-Search(x, k) if x == NIL or key[x] == k then return x if k < key[x] return Tree-Search(left[x], k) return Tree-Search(right[x], k) Eliminating tail recursion: Tree-Search (x, k) do if x == NIL or key[x] == k then return x if k < key[x] then x = left[x] else x = right[x] loop What is the runtime?

Other operations on BSTs Some other easy operations to perform include: Minimum Left most node Maximum Symmetric to minimum Successor Minimum of the key s right subtree if it exists, otherwise first ancestor such that the key lies in it s left subtree Predecessor Symmetric to successor What are the runtimes?

Inserting / Deleting BST Nodes Insert(z): Search down the tree from the root from a node x, move left if key[z] < key[x], else move right. Insert z in the first empty location encountered. Delete(z): If z has less than 2 children, splice it out. If z has 2 children, find node y = Successor(z) and splice y out, then replace z with y. Note that y cannot have 2 children, so it can always be spliced (this is because y will be the minimum element in z s right subtree by definition of Successor, thus y has no left child).

Expected Case for a BST Assume n nodes are inserted in random order. How many comparisons are performed? Consider the first arrival (the root). How many subsequent nodes go to the left/right of it? Root is the k-th smallest with probability 1/n The root acts like the pivot in QuickSort, all elements are compared against with it, then sent left or right How many total comparisons? T(n) = n-1 + (1/n) i=1n [T(i-1)+T(n-i)] We have solved this before: T(n) = O(n log n)

2-3 Trees A balanced search tree, with special properties: All internal nodes have 2 or 3 children All leaves are at the same depth Plus standard search tree property (ordered subtrees) Guides can be used to improve efficiency Each node remembers the maximum key in its subtree O(log n) Search/Insert/Delete operations See additional notes in handout for details I will demonstrate in class Useful as a Dictionary data structure

General Directed Graphs May contain cycles, which often need to be handled as a special case Adjacency Matrix vs Adjacency List representation Matrix requires Θ( V 2 ) space Constant time to search for existence of an edge List requires Θ( V + E ) space Needs O( E ) time in the worst case to search an edge Traversal techniques: Breadth First Search (BFS) Depth First Search (DFS)

BFS Given graph G = (V,E) and a source vertex s V: BFS(V,E, s) forall x V color[x]=white, d[x] =, π[x]=nil Q = // Initialize an empty FIFO queue Q d[s] = 0, Enqueue(Q, s) while Q u = Dequeue(Q) forall v Adj[u] if color[v] == WHITE color[v] = GRAY d[v] = d[u] + 1, π[v] = u, Enqueue(Q, v) color[u] = BLACK Runtime? O( V + E ) Useful shortest path property

DFS Given graph G = (V,E) and a source vertex s V: DFS-VISIT(s) // Not the full DFS procedure (see CLRS p. 541) // Assume nodes x were originally initialized with // color[x] = WHITE, π[x] = NIL, and that global variable time = 0 color[u] = GRAY time = time + 1 forall v Adj[u] if color[v] == WHITE π[v] = u DFS-VISIT(v) color[u] = BLACK, time = time+1, f [u] = time Runtime? O( V + E )

Observations about DFS DFS can also be implemented by replacing the FIFO queue in BFS with a LIFO stack Parenthesis structure (see CLRS p. 543) Classification of edges. Edge (u,v) in a tree T is a Tree edge if (u,v) T In a DFS tree, this means v is first visited coming from u Forward edge if u is an ancestor of v but (u,v) T In a DFS tree, means v was first visited by a descendent of u Back edge if v is an ancestor of u in T Exists if and only if there is a cycle in G Cross edge if it is none of the above

Topological Sort A topological sort of a directed acyclic graph G=(V,E) is an ordering of V such that if (u,v) E then u appears before v in the sort DFS the graph G, and sort V in order of decreasing finishing time (i.e. last to be blackened is first) Runtime is Θ( V + E ) for DFS + O( V ) time to maintain a stack containing blackened vertices

Strongly Connected Components A strongly connected component is a maximal set of vertices C V such that for any pair of distinct vertices u and v in C, there is a path from u to v and a path from v to u in G Find SCCs by doing a complete DFS of G to get a topological sort of V, and DFSing the transpose graph of G in the topologically sorted order Be sure to read details in CLRS, I will only sketch the procedure and proof in class