Lecture 21

  • April 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Lecture 21 as PDF for free.

More details

  • Words: 680
  • Pages: 4
‫‪Data Structures‬‬ ‫‪Notes for Lecture 21‬‬ ‫‪Introduction of Searching‬‬ ‫‪By‬‬ ‫‪Samaher Hussein Ali‬‬ ‫‪2007-2008‬‬

‫‪Searching‬‬ ‫اﻟﺒﺤﺚ هﻮ ﻋﻤﻠﻴﺔ اﻟﺘﻔﻴﺶ ﻋﻦ ﻋﻨﺼﺮ ﻣﻌﻴﻦ ﻓﻲ ﻣﺠﻤﻮﻋﺔ ﻣﻦ اﻟﺒﻴﺎﻧﺎت أوهﻮ ﻋﻤﻠﻴﺔ اﻳﺠﺎد ﻋﻨﺼﺮ ﻣﻌﻴﻦ ﻓﻲ ﻣﺠﻤﻮﻋﺔ ﻣﻦ اﻟﺒﻴﺎﻧﺎت ﻓﺎذا آﺎن‬ ‫ذﻟﻚ اﻟﻌﻨﺼﺮ ﻣﻮﺟﻮد ﺗﻜﻮن ﻋﻤﻠﻴﺔ اﻟﺒﺤﺚ )اﻳﺠﺎﺑﻴﺔ( ﻓﻌﺎﻟﺔ وهﺬا ﻳﺤﺪث اذا آﺎﻧﺖ ﻗﺎﺋﻤﺔ اﻟﺒﺤﺚ ﻣﺮﺗﺒﺔ ﺑﺎﻻﻋﺘﻤﺎد ﻋﻠﻰ ﺣﻘﻞ ﻣﻌﻴﻦ ﺑﻴﻨﻤﺎ ﺗﻜﻮن‬ ‫ﻋﻤﻠﻴﺔ اﻟﺒﺤﺚ ﻏﻴﺮ ﻓﻌﺎﻟﺔ ﻋﻨﺪ ﻋﺪم وﺟﻮد اﻟﻌﻨﺼﺮ ﻓﻲ ﻗﺎﺋﻤﺔ اﻟﺒﺤﺚ ‪.‬‬

‫*اﻧﻮاع ﺧﻮارزﻣﻴﺎت اﻟﺒﺤﺚ‬ ‫وﻳﻮﺟﺪ ﻋﺪة ﺗﺼﻨﻴﻔﺎت ﻟﺨﻮارزﻣﻴﺎت اﻟﺒﺤﺚ ﻣﻨﻬﺎ‪-:‬‬ ‫‪.1‬ﺧﻮارزﻣﻴﺎت اﻟﺒﺤﺚ اﻟﺘﺴﻠﺴﻠﻲ‬

‫‪Sequential Search Algorithm‬‬

‫‪ .2‬ﺧﻮارزﻣﻴﺎت اﻟﺒﺤﺚ اﻟﺜﻨﺎﺋﻲ‬

‫‪Binary Search Algorithm‬‬

‫‪ .3‬ﺧﻮارزﻣﻴﺎت اﻟﺒﺤﺚ اﻟﻜﺘﻠﻲ‬

‫‪Block Search Algorithm‬‬

‫‪ .4‬ﺧﻮارزﻣﻴﺎت اﻟﺒﺤﺚ اﻟﻤﺤﺮم‬ ‫‪ .5‬ﺑﺤﺚ اﻟﺸﺠﺮة اﻟﺜﻨﺎﺋﻴﺔ‬

‫‪Tabu Search Algorithm‬‬

‫*اﺳﺲ ﻗﻴﺎس آﻔﺎءة ﺧﻮارزﻣﻴﺎت اﻟﺒﺤﺚ‬ ‫‪ .1‬ﻣﻘﺪار اﻟﺨﺰن اﻟﻼزم‬ ‫‪ .2‬ﻣﻘﺪار اﻟﺘﻌﻘﻴﺪات ﻓﻲ اﻟﺨﻮارزﻣﻴﺔ‬ ‫‪ .3‬ﻋﺪد اﻟﻨﻘﻼت او اﻟﺘﺤﺮﻳﻜﺎت او اﻟﻤﻘﺎرﻧﺎت ﻓﻲ اﻟﺨﻮارزﻣﻴﺔ‬ ‫‪ .4‬ﻣﻌﺪل ﻃﻮل اﻟﺒﺤﺚ‬

‫‪Average Search Length‬‬

‫وﻳﻤﻜﻦ ﺣﺴﺎﺑﻪ ﺣﺴﺐ اﻟﻘﺎﻧﻮن اﻟﺘﺎﻟﻲ‪-:‬‬ ‫‪n‬‬

‫‪Pi‬‬

‫‪i‬‬

‫‪C‬‬

‫∑‬

‫= ‪L‬‬

‫‪i=1‬‬

‫ﺣﻴﺚ ‪-:‬‬ ‫‪ L‬ﻣﻌﺪل ﻃﻮل اﻟﺒﺤﺚ‬ ‫‪ Ci‬ﻋﺪد اﻟﻤﻘﺎرﻧﺎت اﻟﻤﻄﻠﻮﺑﺔ ﻟﻠﻮﺻﻮل اﻟﻰ اﻟﻌﻨﺼﺮ‬ ‫‪ P‬اﺣﺘﻤﺎﻟﻴﺔ اﻟﻮﺻﻮل اﻟﻰ اﻟﻌﻨﺼﺮ‬

‫‪Sequential Search Algorithm‬‬ ‫ﺧﻮارزﻣﻴﺎت اﻟﺒﺤﺚ اﻟﺘﺴﻠﺴﻠﻲ‬

‫ﺧﻮارزﻣﻴﺎت اﻟﺒﺤﺚ اﻟﺘﺴﻠﺴﻠﻲ وﺗﺘﻀﻤﻦ ﻋﻤﻠﻴﺔ اﻟﺒﺤﺚ ﻋﻦ ﻋﻨﺼﺮ ﻣﻌﻴﻦ ﻓﻲ ﻗﺎﺋﻤﺔ ﻣﻦ اﻟﻌﻨﺎﺻﺮ ﻣﻦ ﺧ ﻼل ﻣ ﺴﺢ ﺟﻤﻴ ﻊ ﻋﻨﺎﺻ ﺮ اﻟﻘﺎﺋﻤ ﺔ ﻣ ﻦ‬ ‫ﺑﺪاﻳﺘﻬﺎ وﺑﺎﻟﺘﺴﻠﺴﻞ ﻟﺤﻴﻦ اﻟﻮﺻﻮل اﻟﻰ اﻟﻌﻨﺼﺮ اﻟﻤﻄﻠﻮب ﻓﻲ ﺣﺎﻟﺔ وﺟﻮدﻩ او اﻟﻮﺻﻮل اﻟﻰ ﻧﻬﺎﻳﺔ اﻟﻘﺎﺋﻤﺔ ﻋﻨﺪﻣﺎ ﻳﻜﻮن اﻟﻌﻨﺼﺮ ﻏﻴﺮ ﻣﻮﺟﻮد‪-:‬‬ ‫‪1‬‬

‫ﻟﺬا ﻓﺎن ﻣﻌﺪل ﻋﺪد اﻟﻤﻘﺎرﻧﺎت هﻮ )‬

‫‪n‬‬ ‫‪2‬‬

‫( ‪ .‬ووﻗﺖ ﺗﻨﻔﻴﺬ اﻟﺨﻮارزﻣﻴﺔ هﻮ ) )‪.( Ο(n‬‬

‫ﻳﺴﺘﺨﺪم اﻟﺒﺤﺚ اﻟﺘﺴﻠﺴﻠﻲ ﻣﻊ اﻟﺒﻴﺎﻧﺎت اﻟﺘﻲ ﺗﺤﺘﺎج اﻟﻰ ﻣﻌﺎﻟﺠﺔ دورﻳﺔ ﻣﺜﻞ ﻧﻈﺎم اﻟﺮواﺗﺐ‪.‬‬ ‫‪Algorithm of sequential search‬‬

‫‪(Given an unordered vector k consisting of n+1 elements, (n>=1) this algorithm search the vector for a‬‬ ‫)‪particular elements having the value x‬‬ ‫]‪1. [ initialize search‬‬ ‫‪I=1‬‬ ‫‪K(n+1)=x‬‬ ‫] ‪2. [search the vector‬‬ ‫‪Repeat‬‬ ‫‪While k(i)<>x‬‬ ‫‪I=i+1‬‬ ‫]‪3. [successful search‬‬ ‫‪If i=n+1 then‬‬ ‫)"‪Msgbox("unsuccessful search‬‬ ‫‪Else‬‬ ‫)"‪Msgbox("successful search‬‬ ‫ﻣﻼﺣﻈﺔ‪-:‬‬ ‫ﻋﺪد اﻟﻤﻘﺎرﻧﺎت = ﻋﺪد اﻟﻌﻨﺎﺻﺮ اﻟﻤﻘﺮءة او اﻟﻤﺤﺴﻮﺑﺔ أي‬

‫‪= i‬‬ ‫‪i * Pi‬‬

‫‪n‬‬

‫∑‬

‫‪i‬‬

‫‪C‬‬

‫= ‪L‬‬

‫‪i=1‬‬

‫ﻣﺜﺎل ‪ -:‬اﻓﺮض ان اﺣﺘﻤﺎﻟﻴﺔ اﻟﻮﺻﻮل اﻟﻰ آﻞ ﻋﻨﺼﺮ ﻏﻴﺮ ﻣﺘﺴﺎوﻳﺔ ﻓﻲ ﻗﺎﺋﻤﺔ ﺗﺤﺘﻮي ﻋﻠﻰ ﺧﻤﺴﺔ ﻗﻴﻮد اوﺟﺪ ﻣﻌﺪل ﻃﻮل اﻟﺒﺤﺚ ﺑﺎﺳﺘﺨﺪام‬ ‫اﻟﺒﺤﺚ اﻟﺘﺴﻠﺴﻠﻲ اذا آﺎﻧﺖ اﺣﺘﻤﺎﻟﻴﺔ اﻟﻮﺻﻮل ﻟﻜﻞ ﻋﻨﺼﺮ هﻲ‪-:‬‬ ‫‪0.4‬‬

‫‪P1‬‬

‫‪0.3‬‬

‫‪P2‬‬

‫‪0.2‬‬

‫‪P3‬‬

‫‪0.07‬‬

‫‪P4‬‬

‫‪0.03‬‬

‫‪P5‬‬

‫‪i * Pi‬‬

‫‪n‬‬

‫∑‬

‫= ‪L‬‬

‫‪i=1‬‬

‫‪L=1*0.4+2*0.3+3*0.2+4*0.07+5*0.03 = 2.03‬‬ ‫‪2‬‬

‫‪Binary Search Algorithm‬‬ ‫ﺧﻮارزﻣﻴﺔ اﻟﺒﺤﺚ اﻟﺜﻨﺎﺋﻲ‬ ‫ﻓﻜﺮﺗﻬﺎ‪-:‬‬

‫ان هﺬﻩ اﻟﺨﻮارزﻣﻴﺔ ﺗﻔﺘﺮض اﻟﺒﺤﺚ ﻋﻦ ﻋﻨﺼﺮ ﻓﻲ ﻗﺎﺋﻤﺔ ﻣﺮﺗﺒﺔ ﺣﺴﺐ ﺗﺮﺗﻴﺐ ﻣﻌﻴﻦ وﺗﺘﻀﻤﻦ اﻟﺨﻄﻮات اﻟﺘﺎﻟﻴﺔ ‪-:‬‬ ‫‪ .1‬ﻣﻘﺎرﻧﺔ اﻟﻌﻨﺼﺮ اﻟﻤﺮاد اﻟﺒﺤﺚ ﻋﻨﻪ وﻟﻴﻜﻦ )‪ (X‬ﻣﻊ اﻟﻌﻨﺼﺮ اﻟﺬي ﻳﻘﻊ ﻣﻨﺘﺼﻒ اﻟﻘﺎﺋﻤﺔ‬ ‫‪ .2‬اذا آﺎن اﻟﻌﻨ ﺼﺮ اﻟﻤﻄﻠ ﻮب )‪ (X‬اﻗ ﻞ ﻣ ﻦ ﻗﻴﻤ ﺔ اﻟﻌﻨ ﺼﺮ اﻟ ﺬي ﻳﻘ ﻊ ﻣﻨﺘ ﺼﻒ اﻟﻘﺎﺋﻤ ﺔ اذن ﺳﻴﻨﺤ ﺼﺮ اﻟﺒﺤ ﺚ ﻓ ﻲ اﻟﺠ ﺰء اﻟ ﺬي ﻳ ﻀﻢ‬ ‫اﻟﻌﻨﺎﺻﺮ اﻻﺻﻐﺮ ﻣﻦ ﻋﻨﺼﺮ اﻟﻤﻨﺘﺼﻒ )ﺟﻬﺔ اﻟﻴﺴﺎر(‬ ‫‪ .3‬اذا آﺎن اﻟﻌﻨﺼﺮ اﻟﻤﻄﻠﻮب )‪ (X‬اآﺒ ﺮ ﻣ ﻦ ﻗﻴﻤ ﺔ اﻟﻌﻨ ﺼﺮ اﻟ ﺬي ﻳﻘ ﻊ ﻣﻨﺘ ﺼﻒ اﻟﻘﺎﺋﻤ ﺔ اذن ﺳﻴﻨﺤ ﺼﺮ اﻟﺒﺤ ﺚ ﻓ ﻲ اﻟﺠ ﺰء اﻟ ﺬي ﻳ ﻀﻢ‬ ‫اﻟﻌﻨﺎﺻﺮ اﻻآﺒﺮ ﻣﻦ ﻋﻨﺼﺮ اﻟﻤﻨﺘﺼﻒ )ﺟﻬﺔ اﻟﻴﻤﻦ(‪ .‬وﺗﺴﺘﻤﺮ اﻟﻌﻤﻠﻴﺔ ﺣﺘﻰ ﻳﺘﻢ اﻟﻌﺜﻮر ﻋﻠﻰ اﻟﻌﻨﺼﺮوذﻟﻚ ﺑﺘﻜﺮار اﻟﺨﻄﻮﺗﻴﻦ ‪2‬و‪3‬‬ ‫‪ .4‬اذا آﺎن اﻟﻌﻨﺼﺮ اﻟﻤﻄﻠﻮب )‪ (X‬ﻣﺴﺎوﻳًﺎ ﺑﺎﻟﻘﻴﻤﺔ اﻟﻰ اﻟﻌﻨﺼﺮ ﻓﻲ ﻣﻨﺘﺼﻒ اﻟﻘﺎﺋﻤﺔ اذن ﺳﺘﻨﺘﻬﻲ ﻋﻤﻠﻴﺔ اﻟﺒﺤﺚ ﻣﺒﺎﺷﺮﺗًﺎ‪.‬‬ ‫‪Algorithm of binary search‬‬

‫‪(Given a vector k, consisting of n elements in ascending order this algorithm searches the structure for a‬‬ ‫‪given element whose value is given by x. the variables low, middle, and high denote the lower, middle‬‬ ‫)‪and upper limits of the search interval respectively‬‬ ‫]‪1. [ initialize search‬‬ ‫‪Low =1‬‬ ‫‪High =n‬‬ ‫]‪2. [perform search‬‬ ‫‪Repeat thru step4‬‬ ‫‪While low<=high‬‬ ‫]‪3. [obtain index of mid point of interval‬‬ ‫‪Middle=(low + high)/2‬‬ ‫]‪4. [compare‬‬ ‫‪If xk(middle) then‬‬ ‫‪Low=middle+1‬‬ ‫‪Else‬‬ ‫)"‪Msgbox("successful‬‬ ‫]‪5. [unsuccessful‬‬ ‫)"‪Msgbox("unsuccessful‬‬ ‫‪3‬‬

‫ ﻓﻲ اﻟﻘﺎﺋﻤﺔ اﻟﺘﺎﻟﻴﺔ‬45 ‫ اﺑﺤﺚ ﻋﻦ اﻟﻌﻨﺼﺮ‬-: ‫ﻣﺜﺎل‬ 1

17

45

52

63

81

92

100

-:‫اﻟﺤﻞ‬ Low=1 High=8 Middle=(1+8)/2=4 K(middle)=k(4)=52 X=52 52>45 The element in smallest element list

1

17

45

Low=1 High=3 Middle=(1+3)/2=2 K(middle)=k(2)=17 X=17 17<45 Go to right 45=45 "the element is found and the search is successful"

4

Related Documents

Lecture 21
May 2020 9
Lecture 21
June 2020 17
Lecture 21
May 2020 4
Lecture 21
December 2019 14
Lecture 21
April 2020 5
Lecture 21
November 2019 19