MA/CSSE 473 Day 15 BFS Topological Sort Combinatorial Object Generation Intro.

Slides:



Advertisements
Similar presentations
What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Advertisements

CSC 421: Algorithm Design & Analysis
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 2 Some of the sides are exported from different sources.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 5 Decrease and Conquer. Homework 7 hw7 (due 3/17) hw7 (due 3/17) –page 127 question 5 –page 132 questions 5 and 6 –page 137 questions 5 and 6.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
CS202 - Fundamental Structures of Computer Science II
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Chapter 4 Decrease-and-Conquer. Decrease-and-Conquer 1. Reduce problem instance to smaller instance of the same problem 2. Solve smaller instance 3. Extend.
Chap 5: Decrease & conquer. Objectives To introduce the decrease-and-conquer mind set To show a variety of decrease-and-conquer solutions: Depth-First.
Design and Analysis of Algorithms - Chapter 51 Decrease and Conquer 1. Reduce problem instance to smaller instance of the same problem 2. Solve smaller.
Graph & BFS.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lectures 3 Tuesday, 9/25/01 Graph Algorithms: Part 1 Shortest.
Connected Components, Directed Graphs, Topological Sort COMP171.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
Design and Analysis of Algorithms - Chapter 5
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
CS2420: Lecture 36 Vladimir Kulyukin Computer Science Department Utah State University.
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.
Important Problem Types and Fundamental Data Structures
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
MA/CSSE 473 Day 13 Permutation Generation. MA/CSSE 473 Day 13 HW 6 due Monday, HW 7 next Thursday, Student Questions Tuesday’s exam Permutation generation.
MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort.
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
RAIK 283: Data Structures & Algorithms
UNIT-II DECREASE-AND-CONQUER ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 5:
Chapter 5 Decrease-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Lecture 13 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
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.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 7.
CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Now, Chapter 5: Decrease and Conquer Reduce problem instance to smaller instance of the same problem and extend solution Solve smaller instance Extend.
MA/CSSE 473 Day 16 Combinatorial Object Generation Permutations.
GRAPH ALGORITHM. Graph A pair G = (V,E) – V = set of vertices (node) – E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ( (1,2),(2,3),(3,5),(1,4),(4,5),(6,7)
MA/CSSE 473 Day 14 Strassen's Algorithm: Matrix Multiplication Decrease and Conquer DFS.
Graphs David Kauchak cs302 Spring Admin HW 12 and 13 (and likely 14) You can submit revised solutions to any problem you missed Also submit your.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
MA/CSSE 473 Day 11 Knuth interview Amortization (growable Array) Brute Force Examples.
MA/CSSE 473 Day 12 Interpolation Search Insertion Sort quick review
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
MA/CSSE 473 Day 13 Finish Topological Sort Permutation Generation
CSC 421: Algorithm Design & Analysis
Chapter 5.
CS202 - Fundamental Structures of Computer Science II
Graphs Chapter 13.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
CSC 421: Algorithm Design & Analysis
Analysis and design of algorithm
CSC 421: Algorithm Design & Analysis
Presentation transcript:

MA/CSSE 473 Day 15 BFS Topological Sort Combinatorial Object Generation Intro

MA/CSSE 473 Day 15 HW 6 due today, HW 7 next Monday, Exam Friday Student Questions DFS and BFS Topological Sort Combinatorial Object Generation - Intro Q1-2

Breadth-first search (BFS) Visits graph vertices in increasing order of length of path from initial vertex. Vertices closer to the start are visited early Instead of a stack, BFS uses a queue Level-order traversal of a rooted tree is a special case of BFS “Redraws” graph in tree-like fashion (with tree edges and cross edges for undirected graph)

Pseudocode for BFS Note that this code is like DFS, with the stack replaced by a queue

Example of BFS traversal of undirected graph BFS traversal queue: ab ef cd gh BFS tree: Q3

Notes on BFS BFS has same efficiency as DFS and can be implemented with graphs represented as: –adjacency matrices: Θ (V 2 ) –adjacency lists: Θ (|V|+|E|) Yields a single ordering of vertices (order added/deleted from the queue is the same) Applications: same as DFS, but can also find shortest paths (smallest number of edges) from a vertex to all other vertices Q4

DFS and BFS

Directed graphs In an undirected graph, each edge is a "two- way street". –The adjacency matrix is symmetric In an directed graph (digraph), each edge goes only one way. –(a,b) and (b,a) are separate edges. –One such edge can be in the graph without the other being there.

Dags and Topological Sorting dag : a directed acyclic graph, i.e. a directed graph with no (directed) cycles Dags arise in modeling many problems that involve prerequisite constraints (construction projects, document version control, compilers) The vertices of a dag can be linearly ordered so that every edge's starting vertex is listed before its ending vertex (topological sort). A graph must be a dag in order for a topological sort of its vertices to be possible. ab cd ab cd a dag not a dag Q5-6

Topological Sort Example Order the following items in a food chain fish human shrimp sheep wheatplankton tiger

DFS-based Algorithm DFS-based algorithm for topological sorting –Perform DFS traversal, noting the order vertices are popped off the traversal stack –Reversing order solves topological sorting problem –Back edges encountered?→ NOT a dag! Example: Efficiency: ab ef cd gh Q7

Source Removal Algorithm Repeatedly identify and remove a source (a vertex with no incoming edges) and all the edges incident to it until either no vertex is left (problem is solved) or there is no source among remaining vertices (not a dag) Example: Efficiency: same as efficiency of the DFS-based algorithm ab ef cd gh Q8

Application: Spreadsheet program What is an allowable order of computation of the cells' values? Q9-11

Cycles cause a problem!

COMBINATORIAL OBJECT GENERATION (We may not get to this today) Permutations Subsets

Combinatorial Object Generation Generation of permutations, combinations, subsets. This is a big topic in CS We will just scratch the surface of this subject. –Permutations of a list of elements (no duplicates) –Subsets of a set

Permutations We generate all permutations of the numbers 1..n. –Permutations of any other collection of n distinct objects can be obtained from these by a simple mapping. How would a "decrease by 1" approach work? –Find all permutations of 1.. n-1 –Insert n into each position of each such permutation –We'd like to do it in a way that minimizes the change from one permutation to the next. –It turns out we can do it so that we always get the next permutation by swapping two adjacent elements.

First approach we might think of for each permutation of 1..n-1 –for i=0..n-1 insert n in position i That is, we do the insertion of n into each smaller permutation from left to right each time However, to get "minimal change", we alternate: –Insert n L-to-R in one permutation of 1..n-1 –Insert n R-to-L in the next permutation of 1..n-1 –Etc.

Example Bottom-up generation of permutations of 123 Example: Do the first few permutations for n=4