MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

Similar documents
Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

CSC Design and Analysis of Algorithms. Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms. Brute-Force Approach

CS Data Structures and Algorithm Analysis

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

Graph. Vertex. edge. Directed Graph. Undirected Graph

CS 310 Advanced Data Structures and Algorithms

CSCE 350: Chin-Tser Huang. University of South Carolina

Brute Force: Selection Sort

Module 5 Graph Algorithms

Copyright 2000, Kevin Wayne 1

Computer Science 431 Algorithms The College of Saint Rose Spring Topic Notes: Decrease and Conquer

Topic 12: Elementary Graph Algorithms

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

e-pg PATHSHALA- Computer Science Design and Analysis of Algorithms Module 14 Component-I (A) - Personal Details

Outline. Graphs. Divide and Conquer.

Graphs & Digraphs Tuesday, November 06, 2007

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

CS 350 Algorithms and Complexity

Problem Score Maximum MC 34 (25/17) = 50 Total 100

Algorithm Design and Analysis

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

Graph Algorithms. Definition

Lecture 3. Brute Force

Graph: representation and traversal

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

CSI 604 Elementary Graph Algorithms

Fundamental Algorithms

CS/COE

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Brute-Force Algorithms

Chapter 3. Graphs. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

3.1 Basic Definitions and Applications

CSE 100: GRAPH ALGORITHMS

CS 206 Introduction to Computer Science II

Plan. CMPSCI 311: Introduction to Algorithms. Recall. Adjacency List Representation. DFS Descriptions. BFS Description

Lecture 3: Graphs and flows

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Algorithm Design and Analysis

Lecture 9 Graph Traversal

Lecture 26: Graphs: Traversal (Part 1)

CSE 373: Data Structures and Algorithms

CS61BL. Lecture 5: Graphs Sorting

Trees. Arash Rafiey. 20 October, 2015

Data Structures and Algorithms for Engineers

Announcements Problem Set 4 is out!

Introduction to Graphs. CS2110, Spring 2011 Cornell University

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

CS302 - Data Structures using C++

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

COMP 251 Winter 2017 Online quizzes with answers

CS 491 CAP Introduction to Graphs and Search

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

3.1 Basic Definitions and Applications

Graphs and Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Homework 2. Sample Solution. Due Date: Thursday, May 31, 11:59 pm

Directed Graphs (II) Hwansoo Han

Figure 1: A directed graph.

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up. Nicki Dell Spring 2014

Algorithms: Lecture 10. Chalmers University of Technology

22.3 Depth First Search

Announcements. HW3 is graded. Average is 81%

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

CS-6402 DESIGN AND ANALYSIS OF ALGORITHMS

CSC263 Week 8. Larry Zhang.

Minimum Spanning Trees Ch 23 Traversing graphs

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

DESIGN AND ANALYSIS OF ALGORITHMS

COT 6405 Introduction to Theory of Algorithms

Module 11: Additional Topics Graph Theory and Applications

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

Info 2950, Lecture 16

Practical Session No. 12 Graphs, BFS, DFS Topological Sort

Algorithm Design and Analysis

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Copyright 2000, Kevin Wayne 1

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

Graph Representations and Traversal

Solutions to relevant spring 2000 exam problems

Graph Algorithms: Chapters Part 1: Introductory graph concepts

CS 206 Introduction to Computer Science II

CMSC 132: Object-Oriented Programming II

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

3.1 Basic Definitions and Applications. Chapter 3. Graphs. Undirected Graphs. Some Graph Applications

CS781 Lecture 2 January 13, Graph Traversals, Search, and Ordering

Goals! CSE 417: Algorithms and Computational Complexity!

Graph Representation

Review: Graph Theory and Representation

TIE Graph algorithms

Transcription:

MA/CSSE 473 Day 12 Interpolation Search Insertion Sort quick review DFS, BFS Topological Sort MA/CSSE 473 Day 12 Questions? Interpolation Search Insertion sort analysis Depth first Search Breadth first Search Topological Sort (Introduce permutation and subset generation) 1

Decrease and Conquer Algorithms Three variations. Decrease by constant amount constant factor variable amount Variable Decrease Examples Euclid's algorithm b and a % b are smaller than a and b, but not by a constant amount or constant factor Interpolation search Each of he two sides of the partitioning element are smaller than n, but can be anything from 0 to n 1. 2

Interpolation Search Searches a sorted array similar to binary search but estimates location of the search key in A[l..r] by using its value v. Specifically, the values of the array s elements are assumed to increase linearly from A[l] to A[r] Location of v is estimated as the x coordinate of the point on the straight line through (l, A[l]) and (r, A[r]) whose y coordinate is v: x = l + (v A[l])(r l)/(a[r] A[l] ) See Weiss, section 5.6.3 Levitin Section 4.5 [5.6] Interpolation Search Running time Average case: (log (log n)) Worst: (n) What can lead to worst case behavior? Social Security numbers of US residents Phone book (Wilkes Barre) CSSE department employees*, 1984 2017 *Red and blue are current employees 3

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 4

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

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 6

Example: DFS traversal of undirected graph a b c d e f g h 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) 7

Pseudocode for BFS Note that this code is like DFS, with the stack replaced by a queue Example of BFS traversal of undirected graph a b c d e f g h BFS traversal queue: BFS tree: 8

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 DFS and BFS 9

Directed graphs In an undirected graph, each edge is a "twoway 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 a b a b a dag not a dag c d c d 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. 10

Topological Sort 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 Reversing order solves topological sorting problem Back edges encountered? NOT a dag! Example: a b c d e f g h Efficiency: 11

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: a b c d e f g h Efficiency: same as efficiency of the DFS based algorithm Application: Spreadsheet program What is an allowable order of computation of the cells' values? 12

Cycles cause a problem! (We may not get to this today) Permutations Subsets COMBINATORIAL OBJECT GENERATION 13

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

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 15