Chapter 5.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 5 Decrease-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 5: Decrease and Conquer
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.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials.
Design and Analysis of Algorithms - Chapter 5
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort.
MA/CSSE 473 Day 15 BFS Topological Sort Combinatorial Object Generation Intro.
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.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 7.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
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 14 Strassen's Algorithm: Matrix Multiplication Decrease and Conquer DFS.
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.
Breadth-First Search (BFS)
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.
Graphs Chapter 20.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Decrease-and-Conquer
Data Structures Graphs - Terminology
Graphs Representation, BFS, DFS
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
Chapter 5 Decrease-and-Conquer
MA/CSSE 473 Day 13 Finish Topological Sort Permutation Generation
Csc 2720 Instructor: Zhuojun Duan
CSC 421: Algorithm Design & Analysis
Depth-First Search.
Chapter 5 Decrease-and-Conquer
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples – based directly.
CS120 Graphs.
Graph Algorithms Using Depth First Search
Graph Search Applications
Graphs Graph transversals.
Lecture 10 Algorithm Analysis
Decrease-and-Conquer
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Search Related Algorithms
Decrease-and-Conquer
Lectures on Graph Algorithms: searching, testing and sorting
Graphs.
Decrease-and-Conquer
Connected Components, Directed Graphs, Topological Sort
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.
Chapter 5 Decrease-and-Conquer
Decrease and Conquer Decrease and conquer technique Insertion sort
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSC 421: Algorithm Design & Analysis
Graph Implementation.
CSC 380: Design and Analysis of Algorithms
Chapter 16 1 – Graphs Graph Categories Strong Components
Introduction to Algorithms
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Elementary Graph Algorithms
Analysis and design of algorithm
CSC 421: Algorithm Design & Analysis
Richard Anderson Winter 2019 Lecture 5
GRAPH TRAVERSAL.
Presentation transcript:

Chapter 5

Decrease-and-Conquer Reduce problem instance to smaller instance of the same problem Solve smaller instance Extend solution of smaller instance to obtain solution to original instance Can be implemented either top-down or bottom-up Also referred to as inductive or incremental approach

3 Types of Decrease and Conquer Decrease by a constant (usually by 1): insertion sort graph traversal algorithms (DFS and BFS) topological sorting algorithms for generating permutations, subsets Decrease by a constant factor (usually by half) binary search and bisection method exponentiation by squaring multiplication à la russe Variable-size decrease Euclid’s algorithm selection by partition Nim-like games This usually results in a recursive algorithm.

Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among the sorted A[0..n-2] Usually implemented bottom up (non recursively) Example: Sort 6, 4, 1, 8, 5 6 | 4 1 8 5 4 6 | 1 8 5 1 4 6 | 8 5 1 4 6 8 | 5 1 4 5 6 8

Pseudocode of Insertion Sort

Analysis of Insertion Sort Time efficiency Cworst(n) = n(n-1)/2  Θ(n2) Cavg(n) ≈ n2/4  Θ(n2) Cbest(n) = n - 1  Θ(n) (also fast on almost sorted arrays) Space efficiency: in-place Stability: yes Best elementary sorting algorithm overall Binary insertion sort

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

Depth-First Search (DFS) Visits graph’s vertices by always moving away from last visited vertex to an unvisited one, backtracks if no adjacent unvisited vertex is available. Recursive or it 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 is no adjacent unvisited vertex “Redraws” graph in tree-like fashion (with tree edges and back edges for undirected graph)

Pseudocode of DFS

Example: DFS traversal of undirected graph b e f c d g h abgcdh abgcd abfe abgc abf abg ab … a DFS traversal stack: DFS tree: 1 2 6 7 Traversal using alphabetical order of vertices. Work through this example in detail, showing the traversal by highlighting edges on the graph and showing how the stack evolves: 8 h 3 7 d 4 stack shown growing upwards 4 e 1 6 c 5 number on left is order that vertex was pushed onto stack 3 f 2 5 g 6 number on right is order that vertex was popped from stack 2 b 7 overlap is because after e, f are popped off stack, g and c 1 a 8 are pushed onto stack in their former locations. order pushed onto stack: a b f e g c d h order popped from stack: e f h d c g b a * show dfs tree as it gets constructed, back edges a b c d Red edges are tree edges and white edges are back edges. e f g h 4 3 5 8

Notes on DFS DFS can be implemented with graphs represented as: adjacency matrices: Θ(|V|2). adjacency lists: Θ(|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 (if no back edges) finding articulation points and biconnected components searching the state-space of problems for solutions (in AI)

Breadth-first search (BFS) Visits graph vertices by moving across to all the neighbors of the last visited vertex Instead of a stack, BFS uses a queue Similar to level-by-level tree traversal “Redraws” graph in tree-like fashion (with tree edges and cross edges for undirected graph)

Pseudocode of BFS

Example of BFS traversal of undirected graph bef efg fg ch hd a g d BFS traversal queue: BFS tree: 1 2 6 8 a b c d Red edges are tree edges and white edges are cross edges. e f g h 3 4 5 7

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 single ordering of vertices (order added/deleted from queue is the same) Applications: same as DFS, but can also find paths from a vertex to all other vertices with the smallest number of edges

Connected Component Input Description: A directed or undirected graph G. A start vertex s. Problem: Traverse each edge and vertex of the connected component containing s. The connected components of a graph represent, in grossest terms, the pieces of the graph. Two vertices are in the same component of G if and only if there is some path between them. Finding connected components is at the heart of many graph applications. For example, consider the problem of identifying clusters in a set of items. We can represent each item by a vertex and add an edge between each pair of items that are deemed ``similar.'' The connected components of this graph correspond to different classes of items.

Testing whether a graph is connected is an essential preprocessing step for every graph algorithm. Such tests can be performed so quickly and easily that you should always verify that your input graph is connected, even when you know it has to be. Subtle, difficult-to-detect bugs often result when your algorithm is run only on one component of a disconnected graph. Connected Graph can be determined by DFS or BFS( Input Graph and then traversal graph by DFS or BFS for getting connected graph)

DAGs and Topological Sorting A dag: a directed acyclic graph, i.e. a directed graph with no (directed) cycles Arise in modeling many problems that involve prerequisite constraints (construction projects, document version control) Vertices of a dag can be linearly ordered so that for every edge its starting vertex is listed before its ending vertex (topological sorting). Being a dag is also a necessary condition for topological sorting to be possible. a b a b a dag not a dag c d c d

Topological Sorting Example Order the following items in a food chain tiger human fish sheep shrimp plankton wheat

DFS-based Algorithm DFS-based algorithm for topological sorting Perform DFS traversal, noting the order vertices are popped off the traversal stack Reverse order solves topological sorting problem If Back edges encountered then not a dag! Example: Efficiency: The same as that of DFS. b a c d e f g h

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 or there is no source among the remaining vertices (not a dag) Example: Efficiency: same as efficiency of the DFS-based algorithm, but how would you identify a source? How do you remove a source from the dag? a b e f c d g h “Invert” the adjacency lists for each vertex to count the number of incoming edges by going thru each adjacency list and counting the number of times that each vertex appears in these lists. To remove a source, decrement the count of each of its neighbors by one.