Գագաթի որոնում (Peak Finder) Ալգորիթմներ Գագաթի որոնում (Peak Finder)
Գագաթի որոնում: 1-չափանի դեպք։ a-i թվեր են 2-րդ էլեմենտը կանվանենք գագաթ այն և միայն այն դեպքում երբ, a ≤ b և b ≥ c: 1-ին էլեմենտը կանվանենք գագաթ է եթե a ≥ b 9-րդ տեղը կանվանենք գագաթ է եթե i ≥ h
Օրինակ
Խնդրի դրվածք։ Գտնել որևէ 1D-գագաթ, եթե այն գոյություն ունի Ապացուցել, որ սկզբում տրված սահմանումից հետևում է գագաթի գոյությունը
Հերթով դիտարկել բոլոր էլեմենտները։ Լուծման ալգորիթմ 1։ Հերթով դիտարկել բոլոր էլեմենտները։
Բարդություն։ T(n) = Θ(n) Վատագույն դեպք T(n) = Θ(n)
Լուծման ալգորիթմ 2։ Բաժանիր, որ տիրես (divide and conquer) Եթե a[n/2] < a[n/2-1] ապա գագաթ ման գալ 1-ից n/2-1 հատվածում Հակառակ դեպքում Եթե a[n/2] < a[n/2+1] ապա գագաթ ման գալ 1-ից n/2+1 հատվածում a[n/2] գագաթ է։
Բարդություն։ T(n) = Θ(log 2 𝑛 ) T(n) = T(n/2) + Θ(1) T(n/2) = T(n/4) + Θ(1) ⋯ T(2) = T(1) + Θ(1) T(1) = Θ(1) Θ(log 2 𝑛 ) T(n) = Θ(1) + ⋯ +Θ(1)
Θ(n) vs Θ(log 2 𝑛 )
Implementation of algorithms in java Lab Implementation of algorithms in java
Գագաթի որոնում: 2-չափանի դեպք։ a էլեմենտը կանվանենք գագաթ այն և միայն այն դեպքում, երբ a ≥ e, a ≥ b, a ≥ c, a ≥ d
Խնդրի դրվածք։ Գտնել որևէ 2D-գագաթ, եթե այն գոյություն ունի Ապացուցել, որ սկզբում տրված սահմանումից հետևում է գագաթի գոյությունը
Լուծման ալգորիթմ 1։ Վերցնել կամայական էլեմենտ, շարժվել ձախ-ներքև-աջ եթե համապատասխան էլեմենտը ավելի մեծ է։ Օրինակ՝ n=m=3
Բարդություն։ T(n,m) = Θ(n*m)
Լուծման ալգորիթմ 2։ Ֆիքսենք կամայական սյուն (օրինակ ՝ i-րդ) և գտնենք 1D-գագաթ։ Ենթադրենք այդ գագաթը գտնվում է j-րդ տողում։ Գտնենք այդ j-րդ տողի 1D-գագաթը որը և կհանդիսանա 2D- գագաթ։
Հակաօրինակ
Լուծման ալգորիթմ 3։ Վերցնենք մեջտեղի սյունակը` j = m/2 և գնտենք այդ սյունակի գլոբալ մաքսիմումը։ Ենթադրենք դա i-րդ տողում է։ Եթե (i,j-1) > (i,j) ապա դիտարկել ձախ սյունակներև ՝ (i,1) –ից (i,j-1) Հակառակ դեպքում, եթե (i,j+1) > (i,j) ապա դիտարկել աջ սյունակները ՝ (i,յ+1) –ից (i,m) Հակառակ դեպքում (արդեն կունենանք որ (i,j) ≥ (i,j-1) և (i,j) ≥ (i,j+1)) (i,j) կհանդիսանա 2D-գագաթ
Բարդություն։ T(n,m) = Θ(n∗log 2 𝑚 ) Կիսվեց մաքսիմում T(n,m) = T(n,m/2) + Θ(n) T(n,m/2) = T(n,m/4) + Θ(n) ⋯ T(n,2) = T(n,1) + Θ(n) T(n,1) = Θ(n) Ունենք T(n,1) = Θ(n) T(n,m) = Θ(n)+ ⋯ + Θ(n) Θ(log 2 𝑚 )
Θ(n*m) vs Θ(n∗log 2 𝑚 ) Մասնավորապես, երբ m=n
Շնորհակալություն https://www.youtube.com/watch?v=HtSuA80QTyo&list=PLUl4u3cNGP61Oq3tWYp6V_F- 5jb5L2iHb&index=1 https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction- to-algorithms-fall-2011/ https://ipc.am