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