MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort.

Slides:



Advertisements
Similar presentations
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Advertisements

CSC 421: Algorithm Design & Analysis
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 2 Some of the sides are exported from different sources.
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
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.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
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.
 Last lesson  Graphs  Today  Graphs (Implementation, Traversal)
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
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.
Chapter 2 Graph Algorithms.
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,
MA/CSSE 473 Day 15 BFS Topological Sort Combinatorial Object Generation Intro.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
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.
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.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
MA/CSSE 473 Day 14 Strassen's Algorithm: Matrix Multiplication Decrease and Conquer DFS.
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 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.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
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.
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.
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
Important Problem Types and Fundamental Data Structures
Analysis and design of algorithm
Presentation transcript:

MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort

MA/CSSE 473 Day 12 Changes to HW 6 (details: schedule page Day 15) –Due date postponed –Problems 9-11 added back in Questions? Depth-first Search Breadth-first Search Topological Sort (Introduce permutation and subset generation)

Some "decrease by one" algorithms Insertion sort Selection Sort Depth-first search of a graph Breadth-first search of a graph

Review: Analysis of Insertion Sort Time efficiency C worst (n) = n(n-1)/2  Θ (n 2 ) C avg (n) ≈ n 2 /4  Θ (n 2 ) C best (n) = n - 1  Θ (n) (also fast on almost-sorted arrays) Space efficiency: in-place (constant extra storage) Stable: yes Binary insertion sort (HW 6) –use Binary search, then move elements to make room for inserted element

Graph Traversal Many problems require processing all graph vertices (and edges) in systematic fashion Most common Graph traversal algorithms: –Depth-first search (DFS) –Breadth-first search (BFS)

Depth-First Search (DFS) Visits a graph’s vertices by always moving away from last visited vertex to unvisited one, backtracks if no adjacent unvisited vertex is available Uses a stack –a vertex is pushed onto the stack when it’s reached for the first time –a vertex is popped off the stack when it becomes a dead end, i.e., when there are no adjacent unvisited vertices “Redraws” graph in tree-like fashion (with tree edges and back edges for undirected graph) –A back edge is an edge of the graph that goes from the current vertex to a previously visited vertex (other than the current vertex's parent).

Notes on DFS DFS can be implemented with graphs represented as: –adjacency matrix: Θ (V 2 ) –adjacency list: Θ (|V|+|E|) Yields two distinct ordering of vertices: –order in which vertices are first encountered (pushed onto stack) –order in which vertices become dead-ends (popped off stack) Applications: –checking connectivity, finding connected components –checking acyclicity –finding articulation points –searching state-space of problems for solution (AI)

Pseudocode for DFS

Example: DFS traversal of undirected graph ab ef cd gh DFS traversal stack: DFS tree:

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

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

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