design and analysis of algorithms
play

Design and Analysis of Algorithms - PDF document

Design and Analysis of Algorithms


  1. คําเตือน Design and Analysis of Algorithms เนื้อหาอันรวมถึงขอความ ตัวเลข สัญลักษณ รูปภาพ และ คําบรรยาย อาจมีขอผิดพลาดแฝงอยู ผูจัดทําจะไมรับผิด ผศ . ดร . สมชาย ประสิทธิ์จูตระกูล ชอบตอความเสียหายทั้งทางดานผลการเรียน สุขภาพกาย ภาควิชาวิศวกรรมคอมพิวเตอร และสุขภาพจิตใดๆ อันเนื่องมาจากการใชสื่อการเรียนนี้ จุฬาลงกรณมหาวิทยาลัย 2542 ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj Outline Dynamic Programming • Definition • Optimal Substructure • Recurrence of solution Longest Common Subsequence • Example • Analysis ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj Subsequence Common Subsequence • X = < s, o, m, c, h, a, i > • X = < s, o, m, c, h, a, i >, Y = < c, h, u, a, n > • subsequences of X • common subsequences of X and Y – < s, o, m, c , h, a, i > → < c > – < s, o, m , c, h, a, i > → < s, o, m > < c , h, u, a, n > – < s, o, m, c, h, a, i > → <c, h, a, i> – < s, o, m, c, h, a , i > → <c, a> < c, h, u, a , n > – < s, o, m, c, h, a, i > → <s, o, h, a, i> – < s, o, m, c, h, a , i > → <c, h, a> – ... < c, h, u, a , n > – ... ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj

  2. Longest Common Subsequence Optimal Structure of an LCS x i • Input : two sequences, X = < x 1 , x 2 ,…, x m > X i a and Y = < y 1 , y 2 ,…, y n > x i = y j Y j a • Output : longest common subsequence of X and Y y j • X = <a, b, c, b, d, a, b>, Y = <b, d, c, a, b, a> LCS ( X i , Y j ) = LCS ( X i - 1 , Y j - 1 ) + x i • LCS( X , Y ) = <b, c, a, b>, <b, d, a, b> c ( i, j ) = c ( i - 1 , j - 1 ) + 1 ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj Optimal Structure of an LCS Optimal Structure of an LCS x i x i X i a X i a x i ≠ y j x i ≠ y j Y j b Y j b y j y j LCS ( X i , Y j ) = LCS ( X i - 1 , Y j ) LCS ( X i , Y j ) = LCS ( X i , Y j - 1 ) c ( i, j ) = c ( i - 1 , j ) c ( i, j ) = c ( i , j - 1 ) ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj Recurrence of Solution LCS : Example j 0 1 2 3 4 5 i y j B D C A B x i 0 0 0 0 0 0 x i 0 X i A 0 1 0 B 2 Y j C 0 3 y j 0 B 4 D 0 if i =0 or j =0 5 0 if i, j > 0 and x i = y j c ( i , j ) = 1 + c ( i - 1 , j - 1 ) 0 if i =0 or j =0 c ( i , j ) = 1 + c ( i - 1 , j - 1 ) if i, j > 0 and x i ≠ y j max( c ( i - 1 , j ) c ( i , j - 1 )) if i, j > 0 and x i = y j max( c ( i - 1 , j ) c ( i , j - 1 )) if i, j > 0 and x i ≠ y j ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj

  3. LCS : Example LCS : Example j 0 1 2 3 4 5 j 0 1 2 3 4 5 i i y j y j B D C A B B D C A B x i 0 0 0 0 0 0 x i 0 0 0 0 0 0 0 0 A 0 0 A 0 0 0 1 1 0 0 B B 2 2 C 0 C 0 3 3 0 0 B B 4 4 D 0 D 0 5 5 0 if i =0 or j =0 0 if i =0 or j =0 c ( i , j ) = 1 + c ( i - 1 , j - 1 ) c ( i , j ) = 1 + c ( i - 1 , j - 1 ) if i, j > 0 and x i = y j if i, j > 0 and x i = y j max( c ( i - 1 , j ) c ( i , j - 1 )) max( c ( i - 1 , j ) c ( i , j - 1 )) if i, j > 0 and x i ≠ y j if i, j > 0 and x i ≠ y j ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj LCS : Example LCS : Example j 0 1 2 3 4 5 j 0 1 2 3 4 5 i y j i y j B D C A B B D C A B x i 0 0 0 0 0 0 x i 0 0 0 0 0 0 0 0 A 0 0 0 0 A 0 0 0 0 1 1 1 0 0 B B 2 2 C 0 C 0 3 3 0 0 B B 4 4 D D 5 0 5 0 0 if i =0 or j =0 0 if i =0 or j =0 c ( i , j ) = 1 + c ( i - 1 , j - 1 ) c ( i , j ) = 1 + c ( i - 1 , j - 1 ) if i, j > 0 and x i = y j if i, j > 0 and x i = y j max( c ( i - 1 , j ) c ( i , j - 1 )) max( c ( i - 1 , j ) c ( i , j - 1 )) if i, j > 0 and x i ≠ y j if i, j > 0 and x i ≠ y j ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj LCS : Example LCS : Example j 0 1 2 3 4 5 j 0 1 2 3 4 5 i i y j y j B D C A B B D C A B 0 0 0 0 0 0 0 0 0 0 0 0 x i x i 0 0 A 0 0 0 0 1 1 A 0 0 0 0 1 1 1 1 0 0 1 B B 2 2 C 0 C 0 3 3 0 0 B B 4 4 D 0 D 0 5 5 0 if i =0 or j =0 0 if i =0 or j =0 c ( i , j ) = 1 + c ( i - 1 , j - 1 ) c ( i , j ) = 1 + c ( i - 1 , j - 1 ) if i, j > 0 and x i = y j if i, j > 0 and x i = y j max( c ( i - 1 , j ) c ( i , j - 1 )) max( c ( i - 1 , j ) c ( i , j - 1 )) if i, j > 0 and x i ≠ y j if i, j > 0 and x i ≠ y j ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj

  4. LCS : Example LCS : Example j 0 1 2 3 4 5 j 0 1 2 3 4 5 i i y j y j B D C A B B D C A B x i 0 0 0 0 0 0 x i 0 0 0 0 0 0 0 0 A 0 0 0 0 1 1 A 0 0 0 0 1 1 1 1 0 1 1 0 1 1 1 B B 2 2 C 0 C 0 3 3 0 0 B B 4 4 D 0 D 0 5 5 0 if i =0 or j =0 0 if i =0 or j =0 c ( i , j ) = 1 + c ( i - 1 , j - 1 ) c ( i , j ) = 1 + c ( i - 1 , j - 1 ) if i, j > 0 and x i = y j if i, j > 0 and x i = y j max( c ( i - 1 , j ) c ( i , j - 1 )) max( c ( i - 1 , j ) c ( i , j - 1 )) if i, j > 0 and x i ≠ y j if i, j > 0 and x i ≠ y j ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj LCS : Example LCS : Example j 0 1 2 3 4 5 j 0 1 2 3 4 5 i y j i y j B D C A B B D C A B x i 0 0 0 0 0 0 x i 0 0 0 0 0 0 0 0 A 0 0 0 0 1 1 A 0 0 0 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 2 B B 2 2 C 0 C 0 3 3 0 0 B B 4 4 D D 5 0 5 0 0 if i =0 or j =0 0 if i =0 or j =0 c ( i , j ) = 1 + c ( i - 1 , j - 1 ) c ( i , j ) = 1 + c ( i - 1 , j - 1 ) if i, j > 0 and x i = y j if i, j > 0 and x i = y j max( c ( i - 1 , j ) c ( i , j - 1 )) max( c ( i - 1 , j ) c ( i , j - 1 )) if i, j > 0 and x i ≠ y j if i, j > 0 and x i ≠ y j ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj LCS : Example LCS : Example j 0 1 2 3 4 5 j 0 1 2 3 4 5 i i y j y j B D C A B B D C A B 0 0 0 0 0 0 0 0 0 0 0 0 x i x i 0 0 A 0 0 0 0 1 1 A 0 0 0 0 1 1 1 1 0 1 1 1 1 2 0 1 1 1 1 2 B B 2 2 C 0 1 1 2 2 2 C 0 1 1 2 2 2 3 3 0 0 1 1 2 2 3 B B 4 4 D 0 D 0 5 5 0 if i =0 or j =0 0 if i =0 or j =0 c ( i , j ) = 1 + c ( i - 1 , j - 1 ) c ( i , j ) = 1 + c ( i - 1 , j - 1 ) if i, j > 0 and x i = y j if i, j > 0 and x i = y j max( c ( i - 1 , j ) c ( i , j - 1 )) max( c ( i - 1 , j ) c ( i , j - 1 )) if i, j > 0 and x i ≠ y j if i, j > 0 and x i ≠ y j ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj

  5. LCS : Example LCS : Example j 0 1 2 3 4 5 j 0 1 2 3 4 5 i i y j y j B D C A B B D C A B x i 0 0 0 0 0 0 x i 0 0 0 0 0 0 0 0 A 0 0 0 0 1 1 A 0 0 0 0 1 1 1 1 0 1 1 1 1 2 0 1 1 1 1 2 B B 2 2 C 0 1 1 2 2 2 C 0 1 1 2 2 2 3 3 0 1 1 2 2 3 0 1 1 2 2 3 B B 4 4 D 0 1 2 2 2 3 D 0 1 2 2 2 3 5 5 0 if i =0 or j =0 c ( i , j ) = 1 + c ( i - 1 , j - 1 ) if i, j > 0 and x i = y j max( c ( i - 1 , j ) c ( i , j - 1 )) if i, j > 0 and x i ≠ y j ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj LCS : Analysis j 0 1 2 ... m i 0 0 0 0 0 0 x x .... x 1 0 x x .... x 2 ... n 0 x x .... x Θ ( mn ) ht t p: / / www. cp. eng. chul a. ac. t h/ f acul t y/ spj

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend