Presentation is loading. Please wait.

Presentation is loading. Please wait.

Analysis and design of algorithm

Similar presentations


Presentation on theme: "Analysis and design of algorithm"— Presentation transcript:

1 Analysis and design of algorithm
Sub code:10CS43 Analysis and design of algorithm Unit-5 Engineered for Tomorrow Presented by Shruthi N Asst.Professor CSE, MVJCE 12/8/13

2 Decrease and conquer :Introduction
Engineered for Tomorrow Decrease and conquer :Introduction Decrease & conquer is a general algorithm design strategy based on exploiting the relationship between a solution to a given instance of a problem and a solution to a smaller instance of the same problem. The exploitation can be either top-down (recursive) or bottom- up (non-recursive). The major variations of decrease and conquer are 1. Decrease by a constant :(usually by 1): a. insertion sort b. graph traversal algorithms (DFS and BFS)

3 Contd.. c. topological sorting
Engineered for Tomorrow Contd.. c. topological sorting d. algorithms for generating permutations, subsets 2. Decrease by a constant factor (usually by half) a. binary search and bisection method 3. Variable size decrease a. Euclid‘s algorithm 12/8/13

4 Decrease by a constant :(usually by 1)
Engineered for Tomorrow Decrease by a constant :(usually by 1)

5 Decrease by a constant factor
Engineered for Tomorrow Decrease by a constant factor

6 Engineered for Tomorrow
Insertion sort Insertion sort is an application of decrease & conquer technique. It is a comparison based sort in which the sorted array is built on one entry at a time

7 Algorithm: ALGORITHM Insertionsort(A [0 … n-1] )
Engineered for Tomorrow Algorithm: ALGORITHM Insertionsort(A [0 … n-1] ) //sorts a given array by insertion sort //i/p: Array A[0…n-1] //o/p: sorted array A[0…n-1] in ascending order for i → 1 to n-1 V→ A[i] j→ i-1 while j ≥ 0 AND A[j] > V do A[j+1] A[j] j→ j – 1 A[j + 1]→ V

8 Example: Sort the following list of elements using insertion sort:
Engineered for Tomorrow Example: Sort the following list of elements using insertion sort: 89, 45, 68, 90, 29, 34, 17

9 Advantages of insertion sort:
Engineered for Tomorrow Advantages of insertion sort: Simple implementation. There are three variations o Left to right scan o Right to left scan Binary insertion sort • Efficient on small list of elements, on almost sorted list • Running time is linear in best case • Is a stable algorithm • Is a in-place algorithm

10 Depth-first search (DFS) and Breadth-first search (BFS)
Engineered for Tomorrow Depth-first search (DFS) and Breadth-first search (BFS) DFS and BFS are two graph traversing algorithms and follow decrease and conquer approach – decrease by one variation to traverse the graph. Some useful definition: • Tree edges: edges used by DFS traversal to reach previously unvisited vertices • Back edges: edges connecting vertices to previously visited vertices other than their immediate predecessor in the traversals • Cross edges: edge that connects an unvisited vertex to vertex other than its immediate predecessor. (connects siblings)

11 Depth-first search (DFS)
Engineered for Tomorrow Depth-first search (DFS) Description: • DFS starts visiting vertices of a graph at an arbitrary vertex by marking it as visited. • It visits graph‘s vertices by always moving away from last visited vertex to an unvisited one, backtracks if no adjacent unvisited vertex is available. • It is a recursive algorithm, 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)

12 Algorithm: ALGORITHM DFS (G)
Engineered for Tomorrow Algorithm: ALGORITHM DFS (G) //implements DFS traversal of a given graph //i/p: Graph G = { V, E} //o/p: DFS tree Mark each vertex in V with 0 as a mark of being ―unvisited‖ count→ 0 for each vertex v in V do if v is marked with 0 dfs(v)

13 Contd.. dfs(v) count→ count + 1 mark v with count
Engineered for Tomorrow Contd.. dfs(v) count→ count + 1 mark v with count for each vertex w in V adjacent to v do if w is marked with 0 dfs(w) 12/8/13

14 Engineered for Tomorrow
Applications of DFS: The two orderings are advantageous for various applications like topological sorting, etc • To check connectivity of a graph (number of times stack becomes empty tells the number of components in the graph) • To check if a graph is acyclic. (no back edges indicates no cycle) • To find articulation point in a graph

15 Efficiency: • Depends on the graph representation:
Engineered for Tomorrow Efficiency: • Depends on the graph representation: o Adjacency matrix : Θ(n2) o Adjacency list: Θ(n + e) 12/8/13

16 Breadth-first search (BFS)
Engineered for Tomorrow Breadth-first search (BFS) Description: BFS starts visiting vertices of a graph at an arbitrary vertex by marking it as visited. • It visits graph‘s vertices by across to all the neighbours 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)

17 Algorithm ALGORITHM BFS (G)
Engineered for Tomorrow Algorithm ALGORITHM BFS (G) //implements BFS traversal of a given graph //i/p: Graph G = { V, E} //o/p: BFS tree/forest Mark each vertex in V with 0 as a mark of being ―unvisited‖ count→ 0 for each vertex v in V do if v is marked with 0

18 bfs(v) //bfs(v) count→ count + 1
Engineered for Tomorrow bfs(v) //bfs(v) count→ count + 1 mark v with count and initialize a queue with v while the queue is NOT empty do for each vertex w in V adjacent to front‘s vertex v do if w is marked with 0 mark w with count add w to the queue remove vertex v from the front of the queue 12/8/13

19 • To check if a graph is acyclic. (no cross edges indicates no cycle)
Engineered for Tomorrow Applications of BFS: • To check connectivity of a graph (number of times queue becomes empty tells number of components in the graph) • To check if a graph is acyclic. (no cross edges indicates no cycle) • To find minimum edge path in a graph Efficiency: • Depends on the graph representation: o Array : Θ(n2) o List: Θ(n + e)

20 Difference between DFS & BFS:
Engineered for Tomorrow Difference between DFS & BFS:

21 Engineered for Tomorrow
Topological Sorting Description: Topological sorting is a sorting method to list the vertices of the graph in such an order that for every edge in the graph, the vertex where the edge starts is listed before the vertex where the edge ends. Topological sorting problem can be solved by using 1. DFS method 2. Source removal method

22 Engineered for Tomorrow
Motivation example How to sort array a[1], a[2], …., a[n], whose values are known to come from a set, e.g., {a, b, c, d, e, f, …. x, y, z}?

23 Engineered for Tomorrow
Space-time tradeoffs For many problems some extra space really pays off: Extra space in tables hashing non comparison-based sorting (e.g., sorting by counting the array a[1], a[2], …., a[n], whose values are known to come from a set, e.g., {a, b, c, d, e, f, …. x, y, z})

24 Contd.. Input enhancement
Engineered for Tomorrow Contd.. Input enhancement auxiliary tables (shift tables for pattern matching) indexing schemes (e.g., B-trees) Tables of solutions for overlapping subproblems dynamic programming 12/8/13

25 String matching pattern: a string of m characters to search for
Engineered for Tomorrow String matching pattern: a string of m characters to search for text: a (long) string of n characters to search in Brute force algorithm?

26 String matching pattern: a string of m characters to search for
Engineered for Tomorrow String matching pattern: a string of m characters to search for text: a (long) string of n characters to search in Brute force algorithm: Align pattern at beginning of text

27 Engineered for Tomorrow
Contd.. 2. moving from left to right, compare each character of pattern to the corresponding character in text until all characters are found to match (successful search); or a mismatch is detected 3. while pattern is not found and the text is not yet exhausted, realign pattern one position to the right and repeat step 12/8/13

28 Engineered for Tomorrow
Assignment questions 1.Discuss about major variants of decrease - and - conquer method. Give one example for each. June-July 2008 (10 Marks) 2. Show how DFS method can be used to conduct topological sorting. June-July 2008 (05 Marks) 3. Write depth first search algorithm. June-July 2009 (08 Marks) 4. Briefly explain how breadth first search can be used to check connectness of a graph and also to find the number of components in a graph. June-July 2009 (06 Marks), Dec 2012 12/8/13

29 BESS_ KNEW _ABOUT_BAOBABA. Dec.08/Jan.09 , Dec 2012 (10 Marks)
Engineered for Tomorrow 5. Explain the difference between DFS and BFS. Solve topological sorting problem using DFS algorithm, with an example. Dec.08/Jan.09, Dec.09/Jan.l0 (12 Marks) , Dec 2012 6. Apply insertion sort to sort the list E,X,A,M,P,L,E in alphabetical order. 7. Write Horspool's algorithm. Apply Horspool algorithm to search for the pattern BAOBAB in the text BESS_ KNEW _ABOUT_BAOBABA. Dec.08/Jan.09 , Dec (10 Marks) 8. Apply Horspool's algorithm to search for the pattern BAOBAB in the text BESS KNEW ABOUT BAOBABS . Also, find the total number of comparisons made. June-July 2009 (04 Marks) 12/8/13

30 Engineered for Tomorrow
Thank You..


Download ppt "Analysis and design of algorithm"

Similar presentations


Ads by Google