Lecture 14
This presentation is the property of its rightful owner.
Sponsored Links
1 / 29

Main Index PowerPoint PPT Presentation


  • 120 Views
  • Uploaded on
  • Presentation posted in: General

Lecture 14 – Search Algorithms. 1. Main Index. Contents. Search Algorithms Illustrating the Binary Search - Successful - Unsuccessful Binary Search Alg. Hash Search. 1.Sequential Search Algorithms.

Download Presentation

Main Index

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Main index

Lecture 14

– Search Algorithms

1

Main Index

Contents

  • Search Algorithms

  • Illustrating the Binary Search

  • -Successful

  • -Unsuccessful

  • Binary Search Alg.

  • Hash Search


1 sequential search algorithms

1.Sequential Search Algorithms

  • Search algorithms start with a target value and employ some strategy to visit the elements looking for a match.

    • If target is found, the index of the matching element becomes the return value.


Search algorithms

3

Main Index

Contents

Search Algorithms


Search algorithms sequential search algorithm

4

Main Index

Contents

Search Algorithms- Sequential Search Algorithm

int seqSearch(const int arr[], int first, int last, int target)

{

int i = first;

while(i != last && arr[i] != target)

i++;

 return i;

}//O(n)


2 binary search algorithms

2. Binary Search Algorithms

Case 1.

A match occurs. The search is complete and mid is the index that locates the target.

if (midValue == target)// found match

return mid;


Binary search algorithms

Binary Search Algorithms

Case 2.

The value of target is less than midvalue and the search must continue in the lower sublist. Reposition the index last to the end of the sublist (last = mid).

// search the lower sublist

if (target < midvalue)<reposition last to mid><search sublist arr[first]…arr[mid-1]


Binary search algorithms1

Binary Search Algorithms

Case 3.

The value of target is greater than midvalue and the search must continue in the upper sublist . Reposition the index first to the front of the sublist (first = mid+1).

// search upper sublist

if (target > midvalue)

<reposition first to mid+1>

<search sublist arr[mid+1]…arr[last-1]>


Illustrating the binary search successful search

8

Main Index

Contents

Illustrating the Binary Search- Successful Search

1.Search for target = 23

Step 1: Indices first = 0, last = 9, mid = (0+9)/2 = 4.

Since target = 23 > midvalue = 12, step 2 searches the upper sublist with first = 5 and last = 9.


Illustrating the binary search successful search1

9

Main Index

Contents

Illustrating the Binary Search- Successful Search

Step 2:

Indices first = 5, last = 9, mid = (5+9)/2 = 7.

Since target = 23 < midvalue = 33, step 3 searches the lower sublist with first = 5 and last = 7.


Illustrating the binary search successful search2

10

Main Index

Contents

Illustrating the Binary Search- Successful Search

Step 3:Indices first = 5, last = 7, mid = (5+7)/2 = 6.

Since target = midvalue = 23, a match is found at index mid = 6.


Illustrating the binary search unsuccessful search

Illustrating the Binary Search- Unsuccessful Search

Search for target = 4.

Step 1: Indices first = 0, last = 9, mid = (0+9)/2 = 4.

Since target = 4 < midvalue = 12, step 2 searches the lower sublist with first = 0 and last = 4.


Illustrating the binary search unsuccessful search1

Illustrating the Binary Search- Unsuccessful Search

Step 2: Indices first = 0, last = 4, mid = (0+4)/2 = 2.

Since target = 4 < midvalue = 5, step 3 searches the lower sublist with first = 0 and last 2.


Illustrating the binary search unsuccessful search2

Illustrating the Binary Search- Unsuccessful Search

Step 3: Indices first = 0, last = 2, mid = (0+2)/2 = 1.

Since target = 4 > midvalue = 3, step 4 should search the upper sublist with first = 2 and last =2. However, since first >= last, the target is not in the list and we return index last = 9.


Binary search algorithm

14

Main Index

Contents

Binary Search Algorithm

int binSearch(const int arr[], int first, int last, int target)

{int mid;

int midvalue;

int origLast = last;

while (first < last){

mid = (first+last)/2;

midvalue = arr[mid];

if (target == midvalue)return mid;

else if (target < midvalue)last = mid;

elsefirst = mid+1;

}

return origLast;

}//O(log2n)


Main index

15

Main Index

Contents

二分查找(或折半查找)判定树(二叉搜索树)

12

5

23

3

55

16

-7

33


3 hash

16

Main Index

Contents

3. 散列表(Hash)

散列查找:支持快速数据存储和提取而设计的。

散列方法:对数据键值进行散列,即用散列函数对键值进行处理,得的计算结果为该数据在散列表中的位置。(可以理解为将该数据映射到一个散列表中)

散列函数:决定数据项i在散列表中位置的函数h,称为

     散列函数。h(x)=i

常用的散列函数是质数取余法,即mod函数。

h(x)=x mod k=i

其中,k一般为大于或等于数据集的质数。


Main index

散列函数/冲突解决

h(k)=k mod p ,p 最好是一个大素数。

原因:为了尽力避免冲突。

一般地说,如果 p 的约数越多,冲突的几率就越大。

简单的证明:假设 p 是一个有较多约数的数,同时在数据中存在 q 满足 gcd(p,q)=d >1 ,即有 p=a*d , q=b*d, 则有 q mod p=q-p*[b div a]

其中 [b div a ] 的取值范围是[0,b] ,即[b div a] 的值只有 b+1 种可能,而 p 是一个预先确定的数。因此q mod p 的值就只有 b+1 种可能了;虽然mod 运算之后的余数仍然在 [0,p-1] 内,但是它的取值仅限于[0,b],分布就变得不均匀了。

p 的约数越多,发生这种分布不均匀的情况就越频繁,冲突几率越高。质数的约数最少,因此应选用大质数。

例如:p=12=3*4, q=21=3*7

q mode p=21mode12=21-12*[21/12]=21-12*[7/4]


Main index

散列函数/冲突解决

在一般情况下,散列表的空间必须比数据的集合大。

若散列表的空间大小为M,填入表中的结点数为N,则称 N/M 为散列表的负载因子(load factor,或“装填因子”)。

若散列函数对不相等的关键码计算出相同的散列地址时,称该现象为冲突(collision),发生冲突的两个关键码称为该散列函数的同义词。

采用散列技术时需要考虑的两个首要问题是:

(1)如何构造使数据分布均匀的散列函数;

(2)一旦发生冲突,用什么方法来解决;


Hash example

19

Main Index

Contents

Hash Example


Main index

20

Main Index

Contents

1、除余法(除留余数法)

除余法是最简单的散列方法,散列函数为: h(x) = x mod M。

2、平方取中法

当不知道全部关键字时,可通过求关键码的平方值作为散列地址。

  因为一个数平方后中间几位数和该数的每一位都相关,由此得到的

  散列地址比较随机。

3、数字分析法

  关键字中某些数字可能在某些位上分布均匀,在某些位上分布不均

  匀。可选取其中各种符号分布均匀的若干位作为散列地址,或进行

  叠加后作散列地址,如电话号码。

4、折叠法

有时关键码所含的位数很多,则可将关键码分割成位数相同的几部

分,然后取这几部分的叠加和作为散列地址,这方法称为折叠法。

如处理国家标准图书编号,ISBN 9 787302 023685。常用的有移位

叠加、间界叠加(来回折叠):

例如:0-442-20586-4,分割后为04 4220 5864

移位叠加:5864 间界叠加: 5864

+ 4220 + 0224

+ 04         + 04

10088 6092

5、随机数法  H(key)=random(key)


Main index

散列冲突

21

Main Index

Contents


Main index

散列冲突

22

Main Index

Contents

冲突解决技术可以分为两类:

开散列方法( open hashing,也称为链地址法,

separate chaining )

闭散列方法( closed hashing,也称为开地址方法,

open addressing )。

这两种方法的不同之处:

开散列法把发生冲突的关键码存储在散列表主表之外,如存储于相应的链表,或桶;

闭散列法(开地址法)把发生冲突的关键码存储在表中另一个槽内。


Hash table using open probe addressing

23

Main Index

Contents

Hash Table Using Open Probe Addressing (使用闭散列方法,即开地址探测法)


Chaining with separate lists

Chaining with Separate Lists (使用开散列法,即链地址法)


Main index

25

Main Index

Contents

主要探测方法

1、线性探查法

依次探查地址单元:d+1,d+2,......,M-1,0,1,......,d-1直到找到一个空闲地址或查找到关键码为key的结点为止。

2、二次探查法

二次探查法的基本思想是:生成的后继散列地址不是连续的,而是跳跃式的:d+1,d-1,d+4,d-4,d+9,d-9,….

3、随机探查法

探查序列是散列表位置的一个随机排列。

4、双散列探查法

线性探测和二次探测都能消除基本聚集,但如果两个关键码散列到同一个基地址,那么采用这两种方法还是得到同样的探查序列,仍然会产生聚集。这是因为线性探查和二次探查产生的探查序列只是基地址的函数,而不是原来关键码值的函数。这个问题称为二级聚集( secondary clustering )。

为了避免二级聚集,应使探查序列是原来关键码值的函数,而不是基位置的函数。双散列探查法利用第二个散列函数作为常数,每次跳过常数项,做线性探查。


Main index

26

Main Index

Contents

双散列探测方法

第一次探测:h1(x)=i

第二次探测:h2(x)=k

于是,项x产生了探测序列i,i+k,i+2k,…

例如:

h1(x)=x%31

h2(x)=17-(x%17)

对于64,467,777,使用第一散列函数得到散列值均为2,有冲突;但使用第二散列函数后:

h2(64)=4

其散列探测序列为:2,2+4,2+8,2+12,2+16,…

h2(467)=17-(467%17)=17-8=9

其散列探测序列为:2, 2+9, 2+18, 2+27, 2+36,…

h2(777)=17-(777%17)=17-12=5

其散列探测序列为:2, 2+10, 2+15, 2+20, 2+25,…


Main index

散列检索效率分析

散列表的效率实质上是平均检索长度,包括:

成功检索/ 不成功检索

例:依次散列54,77,94,89,14,45,35,76

AVL=(1*5+2*1+3*1+7*1)=17/8

不成功:最好情况比较1次,最坏情况比较整个散列表


Main index

散列检索效率分析

例:依次散列54,77,94,89,14,45,35,76

AVL=(1*6+2*2)=10/8

不成功:最好情况比较1次,最坏情况比较次数为最长链的长度


Main index

散列检索效率分析

实验表明,开散列方法(链地址法)的效率最好,实际系统中使用的散列大多都是开散列。开散列方法非常简单、易于实现,它不会产生聚集现象。

但过精心设计的闭散列的效率比开散列稳定。


  • Login