1 / 25

Decrease and Conquer Algorithms: Insertion Sort, Graph Traversal, Connected Components

Learn about decrease and conquer algorithms, including insertion sort for sorting arrays, graph traversal algorithms like DFS and BFS, and finding connected components in a graph.

ameghan
Download Presentation

Decrease and Conquer Algorithms: Insertion Sort, Graph Traversal, Connected Components

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5

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

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

  5. Pseudocode of Insertion Sort

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

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

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

  9. Pseudocode of DFS

  10. a b c d e f g h Red edges are tree edges and white edges are back edges. Example: DFS traversal of undirected graph a ab abf abfe abf ab abg abgc abgcd abgcdh abgcd … DFS traversal stack: DFS tree: 1 2 6 7 a b c d e f g h 4 3 5 8

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

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

  13. Pseudocode of BFS

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

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

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

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

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

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

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

  21. a b c d e f g h Source Removal Algorithm 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? “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.

More Related