PSD1A. DESIGN AND ANALYSIS OF ALGORITHMS Unit : I-V

Similar documents
Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

IV/IV B.Tech (Regular) DEGREE EXAMINATION. Design and Analysis of Algorithms (CS/IT 414) Scheme of Evaluation

D.K.M.COLLEGE FOR WOMEN (AUTONOMOUS), VELLORE-1.

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

DESIGN AND ANALYSIS OF ALGORITHMS

Contents. 1 Introduction. 2 Searching and Traversal Techniques. Preface... (vii) Acknowledgements... (ix)

L.J. Institute of Engineering & Technology Semester: VIII (2016)

INSTITUTE OF AERONAUTICAL ENGINEERING

CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION

5105 BHARATHIDASAN ENGINEERING COLLEGE

CS6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK UNIT I

Lecture 3. Brute Force

Coping with the Limitations of Algorithm Power Exact Solution Strategies Backtracking Backtracking : A Scenario

Chapter 9 Graph Algorithms

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Algorithm Design Techniques. Hwansoo Han

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Chapter 9 Graph Algorithms

2. (a) Explain when the Quick sort is preferred to merge sort and vice-versa.

CS-6402 DESIGN AND ANALYSIS OF ALGORITHMS

Module 6 NP-Complete Problems and Heuristics

Chapter 9 Graph Algorithms

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

CS6402 Design and Analysis of Algorithm. 2 Marks Question & Answer UNIT I INTRODUCTION

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

Notation Index. Probability notation. (there exists) (such that) Fn-4 B n (Bell numbers) CL-27 s t (equivalence relation) GT-5.

DESIGN AND ANALYSIS OF ALGORITHMS

Further Mathematics 2016 Module 2: NETWORKS AND DECISION MATHEMATICS Chapter 9 Undirected Graphs and Networks

Backtracking and Branch-and-Bound

Graph definitions. There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs. An undirected graph

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Question Bank Subject Name: CS6402- Design & Analysis of Algorithm Year/Sem : II/IV UNIT-I INTRODUCTION

CS6402-DESIGN AND ANALYSIS OF ALGORITHM. TWO MARK QUESTION WITH ANSWERS Unit-I. Introduction

CS 440 Theory of Algorithms /

CSE 417 Branch & Bound (pt 4) Branch & Bound

Unit-5 Dynamic Programming 2016

Module 6 P, NP, NP-Complete Problems and Approximation Algorithms

Module 2: NETWORKS AND DECISION MATHEMATICS

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

Chapter-6 Backtracking

CS521 \ Notes for the Final Exam

Notation Index 9 (there exists) Fn-4 8 (for all) Fn-4 3 (such that) Fn-4 B n (Bell numbers) CL-25 s ο t (equivalence relation) GT-4 n k (binomial coef

Seach algorithms The travelling salesman problem The Towers of Hanoi Playing games. Comp24412: Symbolic AI. Lecture 4: Search. Ian Pratt-Hartmann

Graphs and trees come up everywhere. We can view the internet as a graph (in many ways) Web search views web pages as a graph

Module 5 Graph Algorithms

Computer Science and Software Engineering University of Wisconsin - Platteville. 3. Search (Part 1) CS 3030 Lecture Notes Yan Shi UW-Platteville

Reference Sheet for CO142.2 Discrete Mathematics II

Algorithm Design Techniques (III)

15CS43: DESIGN AND ANALYSIS OF ALGORITHMS

Assignment No 2 (Group B)

CSC 8301 Design and Analysis of Algorithms: Exhaustive Search

R13 SET - 1. ''' '' ''' ' blog: anilkumarprathipati.wordpress.com. Code No: RT32054

CSCE 321/3201 Analysis and Design of Algorithms. Prof. Amr Goneid. Fall 2016

Brute Force: Selection Sort

Lecture 3, Review of Algorithms. What is Algorithm?

Elements of Graph Theory

6. Algorithm Design Techniques

UNIT 3. Greedy Method. Design and Analysis of Algorithms GENERAL METHOD

PATH FINDING AND GRAPH TRAVERSAL

Introduction to Graph Theory

8 NP-complete problem Hard problems: demo

Search means finding a path or traversal between a start node and one of a set of goal nodes. Search is a study of states and their transitions.

Module 6 NP-Complete Problems and Heuristics

TOTAL CREDIT UNITS L T P/ S SW/F W. Course Title: Analysis & Design of Algorithm. Course Level: UG Course Code: CSE303 Credit Units: 5

Outline. Graphs. Divide and Conquer.

Graph Theory: Starting Out

Theorem 2.9: nearest addition algorithm

Module 6 NP-Complete Problems and Heuristics

INTRODUCTION TO ALGORITHMS

CSE 100: B+ TREE, 2-3 TREE, NP- COMPLETNESS

Questions? You are given the complete graph of Facebook. What questions would you ask? (What questions could we hope to answer?)

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

Unit 8: Coping with NP-Completeness. Complexity classes Reducibility and NP-completeness proofs Coping with NP-complete problems. Y.-W.

Introduction to Computer Science

Approximation Algorithms

END-TERM EXAMINATION

INDEX. Cambridge University Press How to Think About Algorithms Jeff Edmonds Index More information

Chapter 11: Graphs and Trees. March 23, 2008

Graph Algorithms (part 3 of CSC 282),

GRAPHS, GRAPH MODELS, GRAPH TERMINOLOGY, AND SPECIAL TYPES OF GRAPHS

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK UNIT-III. SUB NAME: DESIGN AND ANALYSIS OF ALGORITHMS SEM/YEAR: III/ II PART A (2 Marks)

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

Exercise 1. D-ary Tree

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

CS 231: Algorithmic Problem Solving


The complement of PATH is in NL

4. Give the diagram representation of Notion of algorithm. [M 13]

CSE373: Data Structure & Algorithms Lecture 23: More Sorting and Other Classes of Algorithms. Catie Baker Spring 2015

Overview of Data Structures, Algorithm Analysis

Practice Problems for the Final

Konigsberg Bridge Problem

Model Answer. Section A Q.1 - (20 1=10) B.Tech. (Fifth Semester) Examination Analysis and Design of Algorithm (IT3105N) (Information Technology)

Design and Analysis of Algorithms

The Algorithm Design Manual

6.2. Paths and Cycles

CS270 Combinatorial Algorithms & Data Structures Spring Lecture 19:

UNIT 1 BASICS OF AN ALGORITHM

Final. Name: TA: Section Time: Course Login: Person on Left: Person on Right: U.C. Berkeley CS170 : Algorithms, Fall 2013

Transcription:

PSD1A DESIGN AND ANALYSIS OF ALGORITHMS Unit : I-V

UNIT I -- Introduction -- Definition of Algorithm -- Pseudocode conventions -- Recursive algorithms -- Time and space complexity -- Big- o notation -- Practical complexities -- Randomized algorithms -- Repeated element & rimality testing -- Divide and Conquer: General Method -- Finding maximum and minimum -- Merge sort. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 2

Algorithm Algorithm is a finite set of instructions that, if followed, accomplishes a particular task. (OR) An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time. Criteria for Algorithm Input Output Definiteness Finiteness Effectiveness PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 3

PSEUDOCODE TM Algorithm can be described in many ways like English for specifying algorithm in step by step Flowchart for graphical representation. But these two ways work well only if algorithm is small or simple. For large or big algorithm we have to use pseudo code. Pseudo code is a kind of structured English for describing algorithms. It allows the designer to focus on the logic of the algorithm without being distracted by details of language syntax. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 4

Recursive Algorithms A function is called itself then it called recursive function. Similarly an algorithm is said to be recursive if the same algorithm is invoked in the body. Two types of Recursive algorithms. 1) Direct recursive algorithm An algorithm that calls itself is direct recursive. 2) Indirect recursive algorithm An algorithm, if it calls another algorithm is indirect recursive. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 5

Towers of Hanoi Problem Tower A, B, and C are diamond towers. Tower A has 64 golden disks. The disks were of decreasing size and where stacked on the towers in decreasing order of size bottom to top. Brahma priests have been attempting to move the disk from tower A to Tower B by using Tower C for intermediate storage. A very elegant solution for this towers of Hanoi by using Recursion. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 6

Space Complexity The space complexity of an algorithm is the amount of memory it needs to run to completion. The space needed by algorithm is the sum of following components A fixed part that is independent of the characteristics of input and output. Example Number & size of Input & Output. This includes instruction space [space for code] + Space for simple variables + Fixed-size component variables + Space for constant and so on. A variable part that consists of the space needed by component variables whose size is dependent on the particular problem instance being solved + the space needed by referenced variables + Recursion stack space. The space requirement S(P) of any algorithm P can be written as S(P)=C+ S P Where C Constant, S P Instance characteristics. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 7

Time complexity T(P) Time complexity of an algorithm is the amount of computer time it needs to run to completion. Here RUN means Compile + Execution. Time Complexity : T(P)=t c +t p But we are neglecting t c Because the compile time does not depends on the instance characteristics. The compiled program will be run several times without recompilation. So T(P)= t p Here t p instance characteristics. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 8

Big - Oh Notation (O) Consider the following graph drawn for the values of f(n) and C g(n) for input (n) value on X-Axis and time required is on Y-Axis In above graph after a particular input value n 0, always C g(n) is greater than f(n) which indicates the algorithm's upper bound. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 9

Big - Theta Notation (Θ) Consider the following graph drawn for the values of f(n) and C g(n) for input (n) value on X-Axis and time required is on Y-Axis In above graph after a particular input value n 0, always C 1 g(n) is less than f(n) and C 2 g(n) is greater than f(n) which indicates the algorithm's average bound. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 10

Big - Omega Notation (Ω) Consider the following graph drawn for the values of f(n) and C g(n) for input (n) value on X-Axis and time required is on Y-Axis In above graph after a particular input value n 0, always C x g(n) is less than f(n) which indicates the algorithm's lower bound. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 11

Randomized Algorithms A randomized algorithm is one that makes use of randomizer. Ex. Random number generator. Decisions made by the algorithm depends on the output of the randomizer. The execution time of a randomized algorithm could also vary from run to run for the same input. Randomized algorithms are of two classes. Las Vegas Algorithms Monte Carlo Algorithms Las Vegas Algorithms Algorithms that produce the same output for the same input. Monte Carlo Algorithms Algorithms whose outputs might differ from run to run for the same input. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 12

Diagrammatic Representation of Divide & Conquer Problem of size N Subprogram of size N/2 Subprogram of size N/2 Solution to subprogram 1 Solution to subprogram 2 Solution to the original problem of size n PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 13

Finding Max & Min using Divide and Conquer PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 14

Finding Max & Min using Divide and Conquer Blocks received after Recursive Algorithm Finding max and min PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 15

Video Link- https://www.youtube.com/watch?v=e5ik2ugjhbk PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 16

UNIT II -- Divide and conquer -- Quick Sort -- Selection Sort -- Strassen's matrix multiplication -- Greedy Method -- knapsack Problem -- Tree vertex splitting -- Job sequencing with dead lines -- Optimal storage on tapes. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 17

Quick Sort Video Link: https://www.youtube.com/watch?v=mzaf_9izcrc PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 18

Average-Case Analysis PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 19

Selection Sort PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 20

Divide and Conquer Matrix Multiply A B = R A 0 A 1 A 2 A 3 B 0 B 1 = B 2 B 3 A 0 B 0 +A 1 B 2 A 0 B 1 +A 1 B 3 A 2 B 0 +A 3 B 2 A 2 B 1 +A 3 B 3 Divide matrices into sub-matrices: A 0, A 1, A 2 etc Use blocked matrix multiply equations Recursively multiply sub-matrices PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 21

PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 22

A Greedy Method Problem: Pick k numbers out of n numbers such that the sum of these k numbers is the largest. Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each decision is locally optimal. These locally optimal solutions will finally add up to a globally optimal solution. Algorithm: FOR i = 1 to k pick out the largest number and delete this number from the input. ENDFOR Video Link : https://www.youtube.com/watch?v=hzek7g8cd0y PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 23

The knapsack problem The greedy algorithm: Step 1: Sort p i /w i into nonincreasing order. Step 2: Put the objects into the knapsack according to the sorted sequence as possible as we can. Example n = 3, M = 20, (p 1, p 2, p 3 ) = (25, 24, 15) (w 1, w 2, w 3 ) = (18, 15, 10) Sol: p 1 /w 1 = 25/18 = 1.39 p 2 /w 2 = 24/15 = 1.6 p 3 /w 3 = 15/10 = 1.5 Optimal solution: x 1 =0,x 2 =1,x 3 = 1/2 PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 24

Job Sequencing with Deadlines Problem: n jobs, S={1, 2,, n}, each job i has a deadline d i 0 and a profit p i 0. We need one unit of time to process each job and we can do at most one job each time. We can earn the profit p i if job i is completed by its deadline. i 1 2 3 4 5 p i 20 15 10 5 1 d i 2 2 1 3 3 The optimal solution = {1, 2, 4}. The total profit = 20 + 15 + 5 = 40. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 25

Job Sequencing with Deadlines PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 26

Optimal Storage on Tapes The problem: Given n programs to be stored on tape, the lengths of these n programs are l 1, l 2,..., l n respectively. Suppose the programs are stored in the order of i 1, i 2,..., i n Let t j be the time to retrieve program i j. Assume that the tape is initially positioned at the beginning. t j is proportional to the sum of all lengths of programs stored in front of the program i j. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 27

The goal is to minimize MRT (Mean Retrieval Time), Ex: 1 n j 1 i.e. want to minimize n 3, ( l1, l2, l3) (5,10,3) n t j n j l ik j 1 k 1 There are n! = 6 possible orderings for storing them. order total retrieval time MRT 1 2 3 4 5 6 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 5+(5+10)+(5+10+3)=38 5+(5+3)+(5+3+10)=31 10+(10+5)+(10+5+3)=43 10+(10+3)+(10+3+5)=41 3+(3+5)+(3+5+10)=29 3+(3+10)+(3+10+5)=34 38/3 31/3 43/3 41/3 29/3 34/3 Smallest PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 28

UNIT III -- Dynamic Programming -- Multistage graphs -- All pairs shortest paths -- Single source shortest paths -- String Editing -- 0/1 knapsack -- Search techniques for graphs DFS & BFS -- Connected components -- Biconnected components. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 29

Dynamic Programming Dynamic Programming is an algorithm design technique for optimization problems: often minimizing or maximizing. Like divide and conquer, DP solves problems by combining solutions to sub problems. Unlike divide and conquer, sub problems are not independent. Sub problems may share sub-sub problems, However, solution to one sub problem may not affect the solutions to other sub problems of the same problem. DP reduces computation by Solving sub problems in a bottom-up fashion. Storing solution to a sub problem the first time it is solved. Looking up the solution when sub problem is encountered again. Comp 122, Spring 2004 PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 30

Steps in Dynamic Programming 1. Characterize structure of an optimal solution. 2. Define value of optimal solution recursively. 3. Compute optimal solution values either top-down with caching or bottomup in a table. 4. Construct an optimal solution from computed values. Overlapping Sub problems The space of sub problems must be small. The total number of distinct sub problems is a polynomial in the input size. A recursive algorithm is exponential because it solves the same problems repeatedly. If divide-and-conquer is applicable, then each problem solved will be brand new. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 31

All-Pairs Shortest Paths 3 6 0 1 4 11 2 2 A -1 0 1 2 0 0 4 11 1 6 0 2 2 3 0 A 0 0 1 2 0 0 4 11 1 6 0 2 2 3 7 0 (b) A -1 (c) A 0 A 1 0 1 2 A 2 0 1 2 0 0 4 6 0 0 4 6 1 6 0 2 1 5 0 2 2 3 7 0 2 3 7 0 (d) A 1 (e) A 2 PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 32

Shortest paths on a special graph Problem: Find a shortest path from v 0 to v 3. The greedy method can solve this problem. The shortest path: 1 + 2 + 4 = 7. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 33

Shortest paths on a Multi-stage Graph Problem: Find a shortest path from v 0 to v 3 in the multi-stage graph. Greedy method: v 0 v 1,2 v 2,1 v 3 = 23 Optimal: v 0 v 1,1 v 2,2 v 3 = 7 The greedy method does not work. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 34

Multi-stage Graph Problem Solution d min (i,j): minimum distance between i and j. d min (v 0,v 3 )=min 3+d min (v 1,1,v 3 ) 1+d min (v 1,2,v 3 ) 5+d min (v 1,3,v 3 ) 7+d min (v 1,4,v 3 ) This problem can be solved by the dynamic programming method. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 35

Minimum spanning trees (MST) It may be defined on Euclidean space points or on a graph. G = (V, E): weighted connected undirected graph Spanning tree : S = (V, T), T E, undirected tree Minimum spanning tree(mst) : a spanning tree with the smallest total weight. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 36

An example of MST A graph and one of its minimum costs spanning tree PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 37

Single-Source Shortest Path Problem shortest paths from v 0 to all destinations PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 38

Dijkstra s algorithm 1 2 3 4 5 6 7 8 1 0 2 300 0 3 1000 800 0 4 1200 0 5 1500 0 250 6 1000 0 900 1400 7 0 1000 8 1700 0 In the cost adjacency matrix, all entries not shown are +. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 39

Vertex Iteration S Selected (1) (2) (3) (4) (5) (6) (7) (8) Initial ---- 1 5 6 + + + 1500 0 250 + + 2 5,6 7 + + + 1250 0 250 1150 1650 3 5,6,7 4 + + + 1250 0 250 1150 1650 4 5,6,7,4 8 + + 2450 1250 0 250 1150 1650 5 5,6,7,4,8 3 3350 + 2450 1250 0 250 1150 1650 6 5,6,7,4,8,3 2 3350 3250 2450 1250 0 250 1150 1650 5,6,7,4,8,3,2 3350 3250 2450 1250 0 250 1150 1650 Time complexity : O(n 2 ), n = V. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 40

Adjacency-Matrix Representation I D A B C D E F G A B C E F G A B C D E F G -- One simple way of representing a graph is the adjacency matrix -- A 2-D array has a mark at [i][j] if there is an edge from node i to node j -- The adjacency matrix is symmetric about the main diagonal -- This representation is only suitable for small graphs PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 41

Adjacency Matrix Representation II D A B C D E F G A G E B F C A B C D E F G An adjacency matrix can equally well be used for digraphs (directed graphs) A 2-D array has a mark at [i][j] if there is an edge from node i to node j. 42 PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 42

Edge-Set Representation s D t w A G p u E B r v F q C NodeSet = {A, B, C, D, E, F, G} EdgeSet = { p: (A, E), q: (B, C), r: (E, B), s: (D, D), t: (D, A), u: (E, G), v: (F, E), w: (G, D) } Here we have a set of nodes, and each node contains only its element. Each edge contains references to its source and its destination. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 43

Adjacency Set Representation s D t w A G p u E B r v F q C A { p } B { q } C { } D { s, t } p: (A, E) q: (B, C) r: (E, B) s: (D, D) E { r, u } t: (D, A) F { v } u: (E, G) G { w } v: (F, E) w: (G, D) Here we have a set of nodes, and each node refers to a set of edges. Each edge contains references to its source and its destination. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 44

Depth-First Search 0,1,3,7,4,5,2,6 1 0 2 HeadNodes 3 4 5 6 [0] [1] [2] [3] [4] [5] [6] [7] 1 0 0 1 1 2 2 3 2 3 5 7 7 7 7 4 0 0 0 0 0 4 6 0 0 7 5 6 0 PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 45

PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 46

Breadth-First Search 0,1,2,3,4,5,6,7 1 0 2 HeadNodes 3 4 5 6 [0] [1] [2] [3] [4] [5] [6] [7] 1 0 0 1 1 2 2 3 2 3 5 7 7 7 7 4 0 0 0 0 0 4 6 0 0 7 5 6 0 PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 47

Breadth-First Search PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 48

Graphs: Connected Components H 1 0 H 2 4 1 2 3 G 4 5 6 7 0 0 1 1 2 2 PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 49

Biconnected Components 0 8 9 0 8 9 1 2 3 4 5 7 6 (a) A connected graph 1,3,5,7 is articulation point 1 7 7 1 7 2 3 3 5 5 4 6 (b) Its biconnected components PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 50

UNIT IV -- Back Tracking -- 8-queens -- Sum of subsets -- Graph Coloring -- Hamiltonian cycles -- Branch and Bound -- Traveling Salesperson problem PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 51

Backtracking Suppose you have to make a series of decisions, among various choices, where You don t have enough information to know what to choose Each decision leads to a new set of choices Some sequence of choices (possibly more than one) may be a solution to your problem Backtracking is a methodical way of trying out various sequences of decisions, until you find one that works 52 PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 52

Coloring a Map You wish to color a map with not more than four colors red, yellow, green, blue Adjacent countries must be in different colors You don t have enough information to choose colors Each choice leads to another set of choices One or more sequences of choices may (or may not) lead to a solution Many coloring problems can be solved with backtracking PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 53

Solving a puzzle In this puzzle, all holes but one are filled with white pegs You can jump over one peg with another Jumped pegs are removed The object is to remove all but the last peg You don t have enough information to jump correctly Each choice leads to another set of choices One or more sequences of choices may (or may not) lead to a solution Many kinds of puzzle can be solved with backtracking PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 54

Backtracking (Animation) dead end? dead end dead end start??? dead end dead end? success! PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 55

Terminology I A tree is composed of nodes There are three kinds of nodes: The (one) root node Internal nodes Leaf nodes Backtracking can be thought of as searching a tree for a particular goal leaf node PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 56

Terminology II Each non-leaf node in a tree is a parent of one or more other nodes (its children) Each node in the tree, other than the root, has exactly one parent parent Usually, however, we draw our trees downward, with the root at the top parent children children PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 57

Backtracking Eight Queens Problem Find an arrangement of 8 queens on a single chess board such that no two queens are attacking one another. In chess, queens can move all the way down any row, column or diagonal (so long as no pieces are in the way). Due to the first two restrictions, it's clear that each row and column of the board will have exactly one queen. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 58

Backtracking Eight Queens Problem The backtracking strategy is as follows: 1) Place a queen on the first available square in row 1. 2) Move onto the next row, placing a queen on the first available square there (that doesn't conflict with the previously placed queens). 3) Continue in this fashion until either: a) you have solved the problem, or b) you get stuck. When you get stuck, remove the queens that got you there, until you get to a row where there is another valid square to try. Q Q Q Q Q Continue Q Video Link http://www.hbmeyer.de/backtrack/ac htdamen/eight.htm#up PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 59

Sum of Subsets State Space Tree for Three items PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 60

Hamiltonian Cycle Icosian Game: Invented by Sir. William Rowan Hamilton (1805 1865) in 1857 and sold to a London game dealer in 1859 for 25 pounds. Game Description: The corners of a regular dodecahedron are labeled with the names of cities; the task is to find a circular tour along the edges of the dodecahedron visiting each city exactly once. Solution Model: Hamiltonian cycle; i.e. look for a cycle in the corresponding dodecahedral graph which contains each vertex exactly once. The Platonic Solid used in Icosian game; the corresponding Hamiltonian cycle is designated by darkened edges. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 61

Hamiltonian Cycle Hamiltonian Cycle: If G = (V, E) is a graph or multi-graph with V >=3, we say that G has a Hamiltonian cycle if there is a cycle in G that contains every vertex in V. Hamiltonian Path A Hamiltonian path is a path (and not a cycle) in G that contains each vertex. It is possible, however, for a graph to have a Hamiltonian path without having a Hamiltonian cycle. There is no helpful connection between the idea of Eulerian circuits (or trails) and Hamiltonian cycles (or paths). Hamiltonian path Hamiltonian cycle There do not exist conditions on a graph G that guarantee the existence of a Hamiltonian cycle (path). PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 62

A Connected Graph with No Cycle of Odd length Does G contain a Hamiltonian path? Starting from vertex a, alternatively label each vertex and its adjacent with x s and y s. If G is to have a Hamiltonian path, there must be an alternating sequence of five x's and five y's. A graph G = (V, E) is bipartite iff it contains no cycle of odd length. G (no cycle of odd length) G is, in fact, a bipartite graph PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 63

Branch and Bound PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 64

Traveling salesman problem Input: Graph (V,E) Problem: Shortest tour visiting all nodes. Closed loop: B C 2 2 2 4 4 3 5 4 A D E F 3 3 2 3 2 G H Optimal = A-B-C-F-H-E-D-G-A Length = 22 Open loop: B C 2 2 2 4 4 3 5 4 A D E F 3 3 2 3 2 G H Optimal = D-G-A-B-C-F-H-E Length = 17 PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 65

Brute force TSP Brute force: search full tree O(N!) 2 2 2 2 1 1 277 153 157 283 2 2 2 2 PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 66

UNIT V: -- Lower Bound Theory: -- Comparison trees -- Oracles and advisory arguments -- Lower bounds through reduction -- Basic Concepts of NP-Hard and NP-Complete problems. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 67

Polynomial Reductions Problem P is reducible to Q -- P <= p Q -- Transforming inputs of P -- To inputs of Q Reducibility relation is transitive PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 68

Decision Trees A convenient model of algorithms involving comparisons in which: internal nodes represent comparisons leaves represent outcomes PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 69

Lower Bound Through Reduction Lower bound: an estimate on a minimum amount of work needed to solve a given problem Lower bound can be an exact count an efficiency class ( ) Tight lower bound: there exists an algorithm with the same efficiency as the lower bound PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 70

Oracle & Adversary Arguments Adversary argument: a method of proving a lower bound by playing role of adversary that makes algorithm work the hardest by adjusting input Example: Guessing a number between 1 and n with yes/no questions Adversary: Puts the number in a larger of the two subsets generated by last question PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 71

P, NP, NP-complete, and NP-hard Problems Decision and Optimization problems Decidable, semi-decidable and undecidable problems Class P, NP, NP-complete and NP-hard problems Optimization problem: Find a solution that maximizes or minimizes some objective function Decision problem: A question that has two possible answers yes or no. The question is about some input. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 72

P, NP, NP-complete, and NPhard Problems PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 73

NP Complete Problem PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 74

Decidability of Decision Problems A problem is decidable if there is an algorithm that says yes if the answer is yes, and no otherwise A problem is semi-decidable if there is an algorithm that says yes if the answer is yes, however it may loop infinitely if the answer is no. A problem is undecidable if we can prove that there is no algorithm that will deliver an answer. PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 75

Class P & Class NP P: the class of decision problems that are solvable in O(p(n)) time, where p(n) is a polynomial of problem s input size n. Problems in this class are called tractable Examples: searching graph connectivity NP (nondeterministic polynomial): Class of decision problems whose proposed solutions can be verified in polynomial time = solvable by a nondeterministic polynomial algorithm. Problems in this class are called intractable PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 76

Non Deterministic Polynomial Algorithm PSD1A DESIGN AND ANALYSIS OF ALGORITHMS 77