Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort."— Presentation transcript:

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

2 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)

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

4 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

5 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)

6 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).

7 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)

8 Pseudocode for DFS

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

10 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)

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

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

13 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

14 DFS and BFS

15 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.

16 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

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

18 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

19 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

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

21 Cycles cause a problem!

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

23 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

24 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.

25 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.

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


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

Similar presentations


Ads by Google