Slide phân tiach và thiết kế giải thuật chap3 decrease and conquer

47 2 0
Slide phân tiach và thiết kế giải thuật chap3 decrease and conquer

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

.c om ng th an co ng Chapter cu u du o Decrease-and-conquer CuuDuongThanCong.com https://fb.com/tailieudientucntt Outline u du o ng th an co ng c om Decrease- and-conquer strategy Insertion sort Graph traversal algorithms Topological sorting Generating all permutations from a set cu CuuDuongThanCong.com https://fb.com/tailieudientucntt Decrease-and-conquer strategy is 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 There are three major variations of decreaseand-conquer: ‰ „ du o u ‰ decrease by a constant decrease by a factor variable size decrease cu ‰ ng th „ an co ng „ c om Decrease-and-conquer strategy Insertion sort is a typical example of decreaseand-conquer strategy CuuDuongThanCong.com https://fb.com/tailieudientucntt Decrease-and-conquer strategy (cont.) Euclid’s algorithm for computing the greatest common divisor of two integers This algorithm is based on the formula gcd(m,n) = gcd(n, m mod n) Euclid’s algorithm is an example of decrease-and-conquer strategy (variablesize-decrease) co ng c om „ th an Example: m = 60 and n = 24 ng 1) m = 60 and n = 24 cu u du o Algorithm Euclid(m,n) /* m,n : two nonnegative integers m and n */ while n0 r := m mod n; m:= n; n:= r End while return m; 2) m = 24 and n = 12 3) m = 12 and n = So 12 is the greatest common divisor CuuDuongThanCong.com https://fb.com/tailieudientucntt Decrease-and-conquer strategy (cont.) cu u du o ng th an co ng c om Graph traversal algorithms (DFS, BFS) „At each step of depth-first-search (DFS) or breadth-first-search (BFS), the algorithm marks the visited vertices and moves to its neighbor vertices „These two algorithms apply decrease-andconquer strategy (decrease-by-one) CuuDuongThanCong.com https://fb.com/tailieudientucntt Insertion sort cu u du o ng th an co ng c om Idea : • Consider an application of the decrease-by-one technique to sort an array a[0 n-1] Following the technique’s idea, we assume that the smaller problem of sorting the array: a[0 n2] has already solved All we need is to find an appropriate position to insert a[n-1] into the sorted subarray a[0 n-2] • There are two alternatives to this - First, we can scan the sorted subarray from left to right until the first element greater than or equal to a[n-1] is encountered and then insert a[n-1] before that element - Second, we can scan the sorted subarray from right to left until the first element smaller or equal to a[n-1] is encountered and then insert a[n-1] right after that element CuuDuongThanCong.com https://fb.com/tailieudientucntt Insertion sort (cont.) c om The second way is often chosen: ng th → 182 205 390 45 235 du o u → 205 390 182 45 235 cu 390 205 182 45 235 an co ng a[0] ≤ … ≤ a[j] < a[j+1] ≤ … ≤ a[i-1] | a[i] … a[n-1] → 45 182 205 390 235 → 45 182 205 235 390 CuuDuongThanCong.com https://fb.com/tailieudientucntt A Pseudo code of Insertion Sort cu u du o ng th an co ng c om procedure insertion; var i; j; v:integer; begin for i:=2 to N begin v:=a[i]; j:= i; while a[j-1]> v begin a[j] := a[j-1]; // pull down j:= j-1 end; a[j]:=v; end; end; CuuDuongThanCong.com https://fb.com/tailieudientucntt Some notes on insertion sort algorithm c om We can use a sentinel element at a[0], which is smaller than the smallest element in the array du o ng th an co ng The outer loop of the algorithm repeats N-1 times The worstcase happens when the array is in descending order In that case, the inner loop executes with the total number of times as follows (N-1) + (N-2) + + =N(N-1)/2 = O(N2) Number of shifts ≈ N2/2 Number of comparisons ≈ N2/2 cu u In the average, there are about i/2 comparisons executed in the inner loop for the given i value in the outer loop Therefore, in the average, the total number of comparisons is: (N-1)/2 + (N-2)/2 + + 1/2 =N(N-1)/4 ≈ N2/4 CuuDuongThanCong.com https://fb.com/tailieudientucntt .c om Complexity of Insertion Sort co ng Propertty 3.1: Insertion sort uses about N2/2 comparison and N2/2 exchanges in the worst-case ng th an Property 3.2: Insertion sort uses about N2/4 comparions and N2/4 exchanges in the average-case cu u du o Property 3.3: Insertion sort is linear for “almost sorted” arrays 10 CuuDuongThanCong.com https://fb.com/tailieudientucntt Some notes on directed graphs c om If we represent a directed graph by adjacency lists, each edge in the graph corresponds to one node in the adjacency lists (the edge from x to y is represented by a node with label y added in the adjacency list of x) th The total number of nodes in adjacency lists of a directed graph is |E| u If we represent a directed graph by adjacency matrix, each edge in the graph corresponds to one bit in the matrix (the edge from x to y is represented by one true at a[x, y]) cu „ du o ng ‰ an co ng „ 33 CuuDuongThanCong.com https://fb.com/tailieudientucntt Topological Sorting Directed Acyclic Graph c om A directed graph with no directed cycles are called directed acyclic graphs (dags) co ng A partial ordering of a set and topological sorting du o ng th an Given a directed acyclic graph G Consider a partial ordering < (called by “precedes”) which is defined in the set of vertices as follows: u < v if there is a path from vertex u to vertex v in G cu u This relation satisfies the following three properties: (1) For all u, v in V[G], not (u < u) (irreflexivity) (2) if u < v, then not( v < u) (asymmetry) (3) if u < v and v < w, then u < w (Transitivity) 34 CuuDuongThanCong.com https://fb.com/tailieudientucntt .c om Topological Sorting th an co ng Given a directed acyclic graph G A topological sort T of G is a linear ordering that respects the partial ordering embedded in the vertex set of the graph V[G] cu u du o ng That means: if u < v in V (i.e if there exists a path from u to v in G), then u appears before v in the linear ordering T 35 CuuDuongThanCong.com https://fb.com/tailieudientucntt .c om A H C co E J K an D G ng B I L M du o ng th F cu u The vertices in the graph of the above graph can be topologically sorted as the following ordering: J K L M A G H I F E D B C 36 CuuDuongThanCong.com https://fb.com/tailieudientucntt Method co ng c om The first method is simple application of depth-first-search This method starts by pushing all source vertices to the stack and adds a vertex into the list whenever it is popped out of the stack Whenever we find at the top of the stack a vertex with no successor (dead-end), we pop it from the stack We repeat this process until the stack is empty We reverse the list to get the topological ordering cu u du o ng th an Algorithm: Start with nodes with no predecessor, put them in the stack while the stack is not empty if the node at top of the stack has some successors then push all its successors (not yet processed) onto the stack else pop it from the stack, remove it from the graph and add it to the list Reverse the list to get the topological sorting 37 CuuDuongThanCong.com https://fb.com/tailieudientucntt c om 10 ng an co 10 2 1 7 u cu 7 du o ng th 10 7 Figure 3.5 Topological sorting 10 10 2 1 1 7 7 7 38 CuuDuongThanCong.com https://fb.com/tailieudientucntt Complexity of Method for topological sorting Property 3.6: The complexity of Method for topological sorting is O(|E|+|V|) if the dag is represented as adjacency lists „ Proof: This property is obvious since the body of the while loops will be executed at most once for each edge And the stack initialization is proportional to the number of vertices (O(V)) „ Notes: Based on the in-degree of each vertex, we can identify the source vertices in the dag cu u du o ng th an co ng c om „ 39 CuuDuongThanCong.com https://fb.com/tailieudientucntt Method c om Idea: repeatedly, identify in a remaining digraph a source vertex (which is a vertex with no incoming edges) and delete it along with all the edges outgoing from it The process will stop when there is no such vertex found The order in which the vertices be deleted yields a solution to the topological sorting problem This algorithm exhibits the spirit of decrease-by-one technique du o u cu „ ng th an co ng „ 40 CuuDuongThanCong.com https://fb.com/tailieudientucntt a d d delete b c c c ng b e e e th an co b c om delete a d ng d delete d cu u du o delete c e delete e e The solution obtained is a, b, c, d, e 41 CuuDuongThanCong.com https://fb.com/tailieudientucntt Algorithm for Method cu u du o ng th an co ng c om Algorithm: Start with nodes with no predecessor, put them in the queue while the queue is not empty remove the front node N of the queue for each neighbor node M of node N delete the edge from N to M if the node M has no predecessor then add M to the rear of the queue endfor endwhile What is the complexity of this method? 42 CuuDuongThanCong.com https://fb.com/tailieudientucntt Generating all permutations from a set cu u du o ng th an „ co ng „ Given a set of n elements A= {a1,a2,…,an} We want to generate all n! permutations from that set Can the decrease-and-conquer strategy can help in this permutation generation problem? A smaller instance of this problem is generating (n-1)! permutations for a subset with n -1 elements from the set A Assume that this smaller problem has been already solved, we can solve the original problem by inserting the last remaining element into every possible positions in each generated permutation of the subset with n-1 elements All these such permutations will be different from each other .c om „ „ 43 CuuDuongThanCong.com https://fb.com/tailieudientucntt Initially insert 12 c om Example 21 co 123 132 312 an insert ng right to left right to left ng th right to left 213 231 321 cu u du o For the convenience, assume that the set A is a set of integers from to n This set can be viewed as the set of all indices of the set of n elements {a1,a2,…,an} 44 CuuDuongThanCong.com https://fb.com/tailieudientucntt Algorithm PERM cu u du o ng th an co ng c om Set j:= and write down the permutation set j:= j+1 for each permutation on j-1 elements create and list P:=< a1 a2…aj-1j> for i:= j-1 downto set P:= P with the values assigned to positions i and i+1 switched and list P // end for loop at step if j < n, then go to step else stop 45 CuuDuongThanCong.com https://fb.com/tailieudientucntt Complexity of PERM cu u du o ng th an co ng c om Property 3.7: Complexity of PERM algorithm for generating all permutations from a set of n elements is proportional to n! Proof: „ The basic operation: insertion of the remaining element into a generated permutation „ For each permutation from the set of n-1 elements (consisting of (n-1)! such permutations), we have to insert the remaining element into n possible positions Therefore we need in total n.(n-1)! insertions of the remaining element into a generated permutation „ So: C(n) = O(n!) „ Remark: Since n! grows very fast even when n is just a bit large (greater than10), PERM gives the result very slowly 46 CuuDuongThanCong.com https://fb.com/tailieudientucntt Stirling’s formula c om n! can be approximated by the function an co ng ∏ n (n/e)n du o cu u (e = 2.71828) ng th where e the base of the natural logarithm 47 CuuDuongThanCong.com https://fb.com/tailieudientucntt ... three major variations of decreaseand -conquer: ‰ „ du o u ‰ decrease by a constant decrease by a factor variable size decrease cu ‰ ng th „ an co ng „ c om Decrease- and- conquer strategy Insertion... strategy Insertion sort is a typical example of decreaseand -conquer strategy CuuDuongThanCong.com https://fb.com/tailieudientucntt Decrease- and- conquer strategy (cont.) Euclid’s algorithm for... n) Euclid’s algorithm is an example of decrease- and- conquer strategy (variablesize -decrease) co ng c om „ th an Example: m = 60 and n = 24 ng 1) m = 60 and n = 24 cu u du o Algorithm Euclid(m,n)

Ngày đăng: 06/12/2021, 14:34

Mục lục

  • A Pseudo code of Insertion Sort

  • Some notes on insertion sort algorithm

  • Complexity of Insertion Sort

  • How to represent graphs

  • Representing a graph by adjacency matrix

  • Representing a graph by adjacency-lists

  • a b c d e f g h i j k l m

  • Comparing two graph representations

  • Depth-First Search and Breadth-first Search

  • Depth First Search – represented by adjacency lists (recursive algorithm)

  • DFS – represented by adjacency matrix

  • Some notes on directed graphs

  • Complexity of Method 1 for topological sorting

  • 5. Generating all permutations from a set

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan