The DivideandConquer Strategy 1 The DivideandConquer Strategy n
- Slides: 71
The Divide-and-Conquer Strategy (個各擊破)
演算法方式總覽 1. The Divide-and-Conquer Strategy (個各擊 破) n binary Searching、Quick Sort…. 2. The Greedy Method(貪婪演算法) n Prim MST、Kruskal MST、Djikstra's algorithm 3. Dynamic Programming(動態演算法) n 二項是係數、矩陣連乘、最佳二元搜尋樹… 4. Trace Back(回溯) n 圖形著色、漢米爾迴路問題…. 5. Tree Searching Strategy(樹的追蹤)
階乘 求n!,一般可能用的規則: n! = n * (n-1)!; 利用迴圈: ans = 1; for (i = 2; i <= n; i++) ans = ans * i; 利用遞迴: int fact(int n) { int x, y; if (n == 0) // boundary condition return(1); x = n-1; y = fact(x); return(n*y); } /* end fact */
遞迴呼叫流程圖 int fact(int n) { int x, y; if (n == 0) // boundary condition return(1); x = n-1; y = fact(x); return(n*y); } /* end fact */ n=4 fact(4) n=3 … y=fact(3 ) Fact(4)=24 fact(3) n=2 … Fact(3)=6 y=fact(2 ) fact(2) n=1 … Fact(2)=2 y=fact(1) n=0 … … Fact(1)=1 y=fact(0 ) fact(0) Fact(0)=1 Return(1)
遞迴呼叫流程圖 n 練習:fact(5)的呼叫過程,遞迴呼叫流程圖為 何。 n=5 int fact(int n) { int x, y; if (n == 0) // boundary condition return(1); x = n-1; y = fact(x); return(n*y); } /* end fact */
遞迴推疊圖 (The recursive stack) 呼叫過程: f 4 f 3 f 2 f 1 f 0 10
(i)printf(“%d”, fact(3)) n x y The stack at various times during execution. (An asterisk indicates an uninitialized value. ) 11
費氏數列 n 費氏數列 (Fibonacci number): f(0) = 0; * f(1) = 1; f(2) = f(1) + f(0); f(3) = f(2) + f(1); : f(n) = f(n-1) + f(n-2); n 利用遞迴範例: int fib(int n) { int x, y; if (n <= 1) return(n); x = fib(n-1); y = fib(n-2); return(x+y); } /* end fib */
遞迴呼叫流程圖 fib(4) … x=fib(3) n=3 fib(3)=2 … x=fib(2) n=1 fact(2) fib(3) n=2 … fib(1)=1 n=0 fib(2)=1 n=1 fact(2) n=2 y=fib(2) … fib(0) Return(0) fib(1)=1 n=1 fib(1)=1 n=0 y=fact(0) fib(4)=3 fib(0)=0 Return(1) fib(1) Return(1) x=fib(1) fib(2)=1 Return(1) x=fib(1) y=fact(0) y=fib(1) fib(0)=0 fib(0) Return(0) n=4 int fib(int n) { int x, y; if (n <= 1) return(n); x = fib(n-1); y = fib(n-2); return(x+y); } /* end fib */
遞迴推疊圖 (The recursive stack) 呼叫過程: f 4 f 3 f 2 f 1 15 f 2 f 1 f 0
遞迴推疊圖 f 5 呼叫過程: f 4 f 3 f 2 f 1 17 f 2 f 1 f 0 f 1 f 2 f 0 f 1 f 0
A general divide-and-conquer algorithm Step 1: If the problem size is small, solve this problem directly; otherwise, split the original problem into 2 sub-problems with equal sizes. Step 2: Recursively solve these 2 sub-problems by applying this algorithm. Step 3: Merge the solutions of the 2 subproblems into a solution of the original problem.
29
35
37
40
樞紐(pivot)
練習 1. finding the maximum of a set S of n numbers 2. 給下列陣列S,請用各個擊破演算法設計找 出最大的數 S 29, 14, 15, 1, 6, 10, 32, 12
Time complexity n Time complexity: n Calculation of T(n): Assume n = 2 k, T(n) = 2 T(n/2)+1 = 2(2 T(n/4)+1)+1 = 4 T(n/4)+2+1 : =2 k-1 T(2)+2 k-2+…+4+2+1 =2 k-1 = n-1
Strassen矩陣相乘設計 58
Example
Example
Strassen 演算法
68
- Tia chieu sa te
- Alleluia hat len nguoi oi
- Một số thể thơ truyền thống
- Sơ đồ cơ thể người
- Tư thế ngồi viết
- Cong thức tính động năng
- Số nguyên là gì
- đặc điểm cơ thể của người tối cổ
- Tỉ lệ cơ thể trẻ em
- Các châu lục và đại dương trên thế giới
- độ dài liên kết
- ưu thế lai là gì
- Môn thể thao bắt đầu bằng chữ f
- Thẻ vin
- Hát kết hợp bộ gõ cơ thể
- Bàn tay mà dây bẩn
- Từ ngữ thể hiện lòng nhân hậu
- Trời xanh đây là của chúng ta thể thơ
- Tư thế ngồi viết
- Thế nào là giọng cùng tên?
- Voi kéo gỗ như thế nào
- Thơ thất ngôn tứ tuyệt đường luật
- Khi nào hổ mẹ dạy hổ con săn mồi
- Thế nào là hệ số cao nhất
- Diễn thế sinh thái là
- đại từ thay thế
- Ng-html
- Vẽ hình chiếu vuông góc của vật thể sau
- Thế nào là mạng điện lắp đặt kiểu nổi
- Cách giải mật thư tọa độ
- Lời thề hippocrates
- Vẽ hình chiếu đứng bằng cạnh của vật thể
- Tư thế worm breton là gì
- Quá trình desamine hóa có thể tạo ra
- Sự nuôi và dạy con của hổ
- điện thế nghỉ
- Các châu lục và đại dương trên thế giới
- Các loại đột biến cấu trúc nhiễm sắc thể
- Biện pháp chống mỏi cơ
- Bổ thể
- Thiếu nhi thế giới liên hoan
- Làm thế nào để 102-1=99
- Strategy formation
- Firms that emphasize global integration make and sell
- Aligning hr strategy with business strategy
- Listening strategies
- Level strategy aggregate planning example
- Transnational strategy vs global strategy
- Concentric growth strategy
- The strategy making strategy executing process
- International or multinational
- International strategy example
- Corporate strategy and business strategy
- Copy strategy exemple
- Financial strategy objectives
- What is global strategy example
- Cyber security strategy austin
- Strategic management chapter 6
- Chapter 7 customer driven marketing strategy
- Blue water strategy
- Information systems, organizations, and strategy
- Asos social media strategy
- Bryson strategy change cycle
- Differentiation strategy
- Heuristic test strategy model
- Creative strategy planning and development
- Global competitive strategy
- Define corporate level strategy
- Netflix market development strategy
- Framing strategy examples
- Strategy as a perspective
- Csr communication strategy