This presentation is the property of its rightful owner.
Sponsored Links
1 / 97

搜索初探 PowerPoint PPT Presentation


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

搜索初探. Cs 2.6 1040310615, simple, Swai. 算法的时间复杂性. 百鸡问题 公鸡每只五元,母鸡每只三元,小鸡三只一元用 n 元买 n 只鸡,求公鸡,母鸡,小鸡的只数 约束方程: a+b+c=n 5a+3b+c/3=n. 输入:所购买三种鸡的总数目 n 输出:满足问题的解的数目 k, 公鸡 , 母鸡 , 小鸡的只数 g[], m[], s[] 1.void chicken_question(int n, int &k, int g[], int m[], int s[]) 2.{ int a, b, c;

Download Presentation

搜索初探

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


4099142

Cs 2.6

1040310615, simple, Swai


4099142

  • nn

    a+b+c=n

    5a+3b+c/3=n


4099142

  • n

  • k,,,g[], m[], s[]

    1.void chicken_question(int n, int &k, int g[], int m[], int s[])

    2.{

  • int a, b, c;

  • k=0;

  • for(a=0;a<=n;a++){

  • for(b=0;b<=n;b++){

  • for(c=0;c<=n;c++){

    8. if((a+b+c==n)&&(5*a+3*b+c/3==n)&&(c%3==0)){

  • g[k]=a;

  • m[k]=b;

  • s[k]=c;

  • k++;

  • }

  • }

  • }

  • }

    17. }


4099142

  • T1(n)

    4151+2(n+1);6n+1+2(n+1)2;7(n+1)2+2(n+1)3;814(n+1)3;9,10,11,128,4(n+1)3,T1(n)<= 1+2(n+1)+n+1+2(n+1)2+(n+1)2+16(n+1)3+4(n+1)3=20n3+63n2+69n+27

    nT1(n) c1n3 c1>0

    n=100001113


4099142

  • n

  • k,,,g[], m[], s[]

    1.void chicken_question(int n, int &k, int g[], int m[], int s[])

    2.{

  • int i, j, a, b, c;

  • k=0;

  • i=n/5;

  • j=n/3;

  • for(a=0;a<=n;a++){

  • for(b=0;b<=n;b++){

  • c=n-a-b;

  • if((a+b+c==n)&&(5*a+3*b+c/3==n)&&(c%3==0)){

  • g[k]=a;

  • m[k]=b;

  • s[k]=c;

  • k++;

  • }

  • }

  • }

    16.}


4099142

  • 415,62;71+2(n/5+1);8n/5+1+2(n/5+1)(n/3+1);93(n/5+1)(n/3+1);1010(n/5+1)(n/3+1);11,12,13,14104(n/5+1)(n/3+1)/

    T2(n)<=1+2+2+1+2(n/5+1)+n/5+1+2(n/5+1)(n/3+1)+(3+10+4)(n/5+1)(n/3+1)=19n2/15+161n/15+28c2n2 , c2 >0 n

    n=100006.7s


4099142

  • NR+f:N R+g:N R+n0c,n>= n0,f(n)<=cg(n),f(n)(g(n)).

    lim f(n)/g(n),

    n

    lim f(n)/g(n) :f(n)= (g(n))

    nf(n)g(n)(g(n))f(n)


4099142

  • s

    NR+f:N R+g:N R+n0c,n>= n0,f(n)>=cg(n),f(n)s(g(n)).

    lim f(n)/g(n),

    n

    lim f(n)/g(n) 0:f(n)= s(g(n))

    nf(n)g(n)s(g(n))f(n)


4099142

  • NR+f:N R+g:N R+n0c1c2,n>= n0,c1g(n)<=f(n)<=c2g(n),f(n)(g(n)).

    lim f(n)/g(n),

    n

    lim f(n)/g(n) = c:f(n)= (g(n))

    n


4099142

e.g.f(n)=5*2n+n2

n0=0,n>=n0c1=5,g(n)=2n,

f(n)>=5*2n=c1g(n)

f(n)=s(g(n))=s(2n)

n0=4,n>=n0c2=6,g(n)=2n

f(n)<=5*2n+2n<=6*2n=c2g(n)

f(n)= (2n)

1 n n (n)1/2 n3/4 n nn n2 2n n! 2n2


4099142


4099142

  • nA[]x

    x=A[j],0<=j<=n-1,j,-1

    1.int linear_search(int A[], int n, int x)

    2.{

    3. int j=0;

  • while(j<n && x!=A[j])

  • j++;

  • if(x==A[j])

  • return j;

  • else

  • return -1;

    10.}


4099142

nA[]x

x=A[j],0<=j<=n-1,j,-1

1.int binary_search(int A[], int n, int x)

2.{

  • int mid, low=0, high=n-1, j=-1;

  • while(low<=high && j<0){

  • mid=(low+high)/2;

  • if(x==A[mid]) j=mid;

  • else if(x<A[mid]) high=mid-1;

  • else low=mid+1;

  • }

  • return j;

    11.}


4099142

  • xx4n(n)

  • xxx[n/2],[n/4],i[n/2j-1].1xjj

    [n/2j-1]=1j-1nj,j= [n]+1,[n]+1(n),[n]+1(n)


4099142

  • e.g.A

    nA[],n

    A[]

    1. void insert_sort(int A[], int n)

    2. {

  • int a, i, j;

  • for(i=1; i<n; i++){

  • a=A[i];

  • j=i-1;

  • while(j>=0 && A[j] > a){

  • A[j+1] = A[j];

  • j--;

  • }

  • A[j+1] = a;

  • }

    13.}


4099142

A{x1,x2,xn},xixj. nn!1/ii-1xii

ii-1

Ti=(i-1)/i+(i-j+1)/i=(i-1)/i +j/i = +i/2-1/i

j=2j=1

x2,x3,xnT

nnn n

T= Ti=(1/2+i/2-1/i)=(n-1)/2+1/2 i-1/i +1

i=2 i=2 n i=2 i=1

=(n2+3n)/4 - 1/i

i=1 n

(n+1) 1/i

i=1

T=(n2+3n)/4 - n

insert_sort (n2)


Search

Search

123


4099142


4099142

  • n

    ASL = Pi * Ci

    Pi i

    Cii


4099142

  • Key


4099142

C

int SequelSeach(elemtype s[], keytype Key, int n)

/*s[0]~s[n-1]Key*/

/*-1*/

{

int i=0;

while(i<n && s[i].Key!= Key) i++;

if(s[i].Key == Key) return i;

else return -1;

}


4099142

e.g. :

typedef int keytype;

typedef struct{

keytype Key;

}elemtype;

int SequelSearch(elemtype s[], keytype Key, int n);

int main(void)

{

elemtype Test[10]={710,342,45,686,6,

841,429,134,68,264};

int n=10, Key=686, i;

i = SequelSeach(Test, Key, n);

if(i!=-1)

printf(\n%di );

else printf(\n)

}


4099142

while

s[n]Keys[n].Key


4099142

int SequelSeach(elemtype s[], keytype Key, int n)

/*s[0]~s[n-1]Key*/

/*-1*/

{

int i=0;

s[n].Key = Key; /*s[n]*/

while( s[i].Key!= Key) i++;

if(i >= n) return -1;

else return i;

}


4099142

  • Pi=1/n,(i = 1,2,3,,n), Ci = i,

    ASLsq = (n+1)/2

    ASLsq = n

    p,

    q ASLsq = (n+1)(1- p/2)


4099142

  • s[0].Key <= s[1].Key <= <= s[n-1].Key

    low, high, mid,low=0,high=n-1,Key

    (1)mid = [(low+high)/2]

    (2)Keys[mid].Key

    1. s[mid].Key=Key,

    2. s[mid].Key<Key,s[mid] low=mid+1

    3. s[mid].Key>Key,s[mid] high=mid-1

    (3)lowhigh,low<=high,(1)(2);Key


4099142

1

  • 2

  • 1

e.g: key = 9

  • ST.elem

    • ST.elem ST.elem[i]. Key <= ST.elem[i+1]. Key; i= 1,2,n-1

    • low= 1; high= 7 n ;

    • mid = low+high/ 2 =4

mid=4 key=9 < 10,

key

4

8

9

10

11

13

19

0

1

2

3

4

5

6

7

high=7

low=1


4099142

1

  • 2

  • 1

e.g: key = 9

  • ST.elem

    • ST.elem ST.elem[i]. Key <= ST.elem[I+1]. Key; i= 1,2,n-1

    • low= 1; high= 3 ;

mid=4

key

4

8

9

10

11

13

19

0

1

2

3

4

5

6

7

low=1

high=3mid-1


4099142

1

  • 2

  • 1

e.g: key = 9

  • ST.elem

    • ST.elem ST.elem[i]. Key <= ST.elem[I+1]. Key; i= 1,2,n-1

    • low= 1; high= 3 ;

    • mid = low+high/ 2 =2

mid=2

key

4

8

9

10

11

13

19

0

1

2

3

4

5

6

7

low=1

high=3mid-1


4099142

1

  • 2

  • 1

e.g: key = 9

  • ST.elem

    • ST.elem ST.elem[i]. Key <= ST.elem[I+1]. Key; i= 1,2,n-1

    • low= 1; high= 3 ;

    • mid = low+high/ 2 =2

mid=2; key=9 > 8,

key

4

8

9

10

11

13

19

0

1

2

3

4

5

6

7

low=1

high=3mid-1


4099142

1

  • 2

  • 1

e.g: key = 9

  • ST.elem

    • ST.elem ST.elem[i]. Key <= ST.elem[I+1]. Key; i= 1,2,n-1

    • low= 3; high= 3 ;

mid=2

key

4

8

9

10

11

13

19

0

1

2

3

4

5

6

7

high=3

low=3mid+1


4099142

1

  • 2

  • 1

e.g: key = 9

  • ST.elem

    • ST.elem ST.elem[i]. Key <= ST.elem[I+1]. Key; i= 1,2,n-1

    • low= 3; high= 3 ;

    • mid = low+high/ 2 = 3

mid=3;

key

4

8

9

10

11

13

19

0

1

2

3

4

5

6

7

high=3

low=3


4099142

1

  • 2

  • 1

e.g: key = 9

  • ST.elem

    • ST.elem ST.elem[i]. Key <= ST.elem[I+1]. Key; i= 1,2,n-1

    • low= 1; high= 3 ;

    • mid = low+high/ 2 = 3

mid=3; key=9 9

key

4

8

9

10

11

13

19

0

1

2

3

4

5

6

7

high=3

low=3


4099142

1

  • 2

  • 1

e.g: key = 5

  • ST.elem

    • ST.elem ST.elem[i]. Key <= ST.elem[I+1]. Key; i= 1,2,n-1

    • low= 1; high= 7 n ;

    • mid = low+high/ 2

mid=4 key=5 < 10,

key

4

8

9

10

11

13

19

0

1

2

3

4

5

6

7

high=7

low=1


4099142

1

  • 2

  • 1

e.g: key = 5

  • ST.elem

    • ST.elem ST.elem[i]. Key <= ST.elem[I+1]. Key; i= 1,2,n-1

    • low= 1; high= 3 ;

mid=4

key

4

8

9

10

11

13

19

0

1

2

3

4

5

6

7

low=1

high=3mid-1


4099142

1

  • 2

  • 1

e.g: key = 5

  • ST.elem

    • ST.elem ST.elem[i]. Key <= ST.elem[I+1]. Key; i= 1,2,n-1

    • low= 1; high= 3 ;

    • mid = low+high/ 2 =2

mid=2

key

4

8

9

10

11

13

19

0

1

2

3

4

5

6

7

low=1

high=3


4099142

1

  • 2

  • 1

e.g: key = 5

  • ST.elem

    • ST.elem ST.elem[i]. Key <= ST.elem[I+1]. Key; i= 1,2,n-1

    • low= 1; high= 3 ;

    • mid = low+high/ 2 =2

mid=2; key=5 < 8,

key

4

8

9

10

11

13

19

0

1

2

3

4

5

6

7

low=1

high=3


4099142

1

  • 2

  • 1

e.g: key = 5

  • ST.elem

    • ST.elem ST.elem[i]. Key <= ST.elem[I+1]. Key; i= 1,2,n-1

    • low= 1; high= 1 ;

mid=2

key

4

8

9

10

11

13

19

0

1

2

3

4

5

6

7

low=1

high=1mid-1


4099142

1

  • 2

  • 1

e.g: key = 5

  • ST.elem

    • ST.elem ST.elem[i]. Key <= ST.elem[I+1]. Key; i= 1,2,n-1

    • low= 1; high= 1 ;

    • mid = low+high/ 2 = 1

mid=1

key

4

8

9

10

11

13

19

0

1

2

3

4

5

6

7

low=1

high=1


4099142

1

  • 2

  • 1

e.g: key = 5

  • ST.elem

    • ST.elem ST.elem[i]. Key <= ST.elem[I+1]. Key; i= 1,2,n-1

    • low= 1; high= 1 ;

    • mid = low+high/ 2 = 1

mid=1; key=5 > 4,

key

4

8

9

10

11

13

19

0

1

2

3

4

5

6

7

low=1

high=1


4099142

1

  • 2

  • 1

e.g: key = 5

  • ST.elem

    • ST.elem ST.elem[i]. Key <= ST.elem[I+1]. Key; i= 1,2,n-1

    • low= 1; high= 1 ;

    • mid = low+high/ 2 = 1

low > high;

mid=1; key=5 > 4,

key

4

8

9

10

11

13

19

0

1

2

3

4

5

6

7

low=2 mid+1

high=1


4099142

C

int BinarySeach(elemtype s[], keytype key,int n)

/*s[0]~s[n-1]Key*/

/*-1*/

{

int low=0, high=n-1, mid;

while(low<=high){

mid=(low+high)/2;

if(s[mid].key == key) return mid;

else if(s[mid].key<key) low = mid+1;

else high = mid-1;

}

return -1;

}


4099142

  • 3

2

ASL = ( 201+ 212+ 223+ + 2t-1 t) / n

t

= (i 2i-1 ) / n

i=1

= [(n + 1) ( log2(n + 1) - 1 ) + 1 ] / n

= (n + 1) log2(n + 1) / n - 1

= (n + 1) log2(n + 1) / n - 1

ASL = log2(n + 1) - 1 ASL = log2n - 1


4099142

simple


4099142

1.Breadth-First-Search,BFS

1

2 5 10

3 6 8

4 7 9


4099142

1.1.

{ 234 }

2.{ 234 }.

{ 567 }

3.{ 567 }{ 89 }

{ 8.9 }.

2 5 10

  • 3 6 8

    4 7 9

    1{1,2,3,4,5,6,7,8,9}


4099142

(Breadth-First Search,BFS)


4099142

//v

v;

Q,v;

While(Q)

{

w;

uw;

while(u)

{

if(u)

{

u;

u;

}

u = w;

}

}


4099142

v = 1.,2,3,4().,2,5;3,4, 67;58;6;79,89,.,19


4099142

2 5

1 3 6 8

4 7 9


4099142

NvN v(v).


4099142

2.Network

,,BFS,,,.,:

u = w;

.BFSNetwork,


4099142

Network

Class Network

{

public:

virtual int Begin(int i) = 0 ;

virtual int NextVertex(int i) = 0 ;

virtual void InitializePos() = 0 ;

virtual void DeactivatePos() = 0 ;

}


4099142

3.BFS:

void Network::BFS(int v , int reach[] , int label)

{

LinkedQueue<int>Q;

InitializePos();//

reach[v] = label;

Q.Add(v);

while(!Q.IsEmpty())

{

int w;

Q.Delete(w);//

int u = Begin(w);

while(u) //w

{

if(!reach[u])//

{

Q.Add(u);

reach[u] = label;//

}

u = NextVertex;//w

}

}

DeactivatePos();//

}


4099142

BSFreach[i] = 0 label != 0 . reach[i]label .


4099142

4.BSF

v,,,,.s,,O(sn),,O().i./.


4099142

,BFS.


4099142

BFS

Template<class T>

Void AdjacencyWDigraph<T>::BFS(int v , int reach[] , int label)

{//

LinkedQueue<int>Q;

reach[v] = label;

Q.Add(v);

while(!Q.IsEmpty())

{

int w;

Q.Delete(w);//

for(int u = 1 ; u <= n ; u ++)//,w

{

if(a[w][u] != NoEdge && !reach[u])

{

Q.Add(u);//u

reach[u] = label;

}

}

}

}


4099142

BFS

void LinkedDigraph::BFS(int v , int reach[] , int label)

{

LinkedQueue<int>Q;

reach[v] = label;

Q.Add;

While(!Q.IsEmpty())

{

int w ;

Q.Delete(w);

ChainNode<int>*p;

for(p = h[w],First() ; p ; p = p ->link)

{

int u = p -> data ;

if(!reach[u])

{

Q.Add(u);

reach[u] = label;

}

}

}

}


4099142

50Network::BFSAdjacencyWDigraph::BFS2.6.,4.5.

BeginNextVertex,.BFSNetworkBeginNextVertex,,BeginNextVertex,


4099142

5.

:v,DFS:

v,vu,u.u,uDFS.u,v,,.,DFS,


4099142

NetworkDFSdfs.DFS,uv

void Network::DFS(int v , int reach[] , int label)

{

initializePos();

dfs(v , reach , label);

DeactivatePos();

}

Void Network::dfs(int v , int reach[] , int label)

{

reach[v] = label;

int u = Begin(v);

while(u){

if(!reach[u])

dfs(u , reach , label);

u = NetVertex(v);}

}


4099142

DFS,v = 1,2,34u.u2,2(1,2),2DFS,2.u55(2,5).5DFS,5,(5,8)8,8.8,5,5u,2,1


4099142

:34.4,(1,4),4DFS,4.3,67u,6,u = 6 ,3,3(6,3),3DFS,3.3,4,4u=7DFS,9,9,1,1,.


4099142

  • N,.vN.v(v),DFS(v,reach,label),reached[i] = label .

    DFSBFS.,DFS()

    BFS(),BFSDFSDFSBFS.


4099142

1 2 3 n-1 n

DFS;BFS

1

2 3 4 n-1 n

DFS;BFS


4099142

1:1723 Finding Nemo

:

123 Marlin In

456

789

Nemo


4099142

2:(HOJ 1868)

3*3 8 1-8


Hewristic

(Hewristic)

  • :

    1.

    2.


4099142

  • :

    :

  • :

    While()

    {

    }


Hash table

Hash Table

()

Swai


4099142


Hash table1

Hash Table


4099142


4099142

template<class E,class K>

class Hash Table{

public:

Hast Table(int divisor = 11);

~Hash Table(){delete[]ht;delete[]empty};

bool Search(const K&k,E&e)const;

Hash Table<E,K>%Insert(const E&e);

private:

int hSearch()const K%k;

int D;//

E*ht;//

bool *empty;//

};


4099142


4099142

  • Tsize =sizoof(table)h(k)= k mod TsizeTsizeh(k)=(k mod p) mod Tsize

    p

    • key p H(key) = key MOD p

    • key 5 p 95 H(key) = key MOD p0 51015 904/5


4099142

  • (Shift Folding)

    1234567891234567891368Tsize

    XOR

  • (Boundary Folding)

    123456789

123

654

789

1566


4099142

  • key 2

  • K = 234D,423


4099142

  • (Vector)

  • (List)


Vector

(Vector)

h(K)

norm(h(K)+p(1)),h(K)+p(2), h(K)+p(i ),)

P(i)inormTsize


4099142

  • p(i)=i (h(K)+i) mod Tsize

    h(K)h(K) 1

    • On


4099142

    • m 357

    • 5 H(key) = ( key+5) MOD 11

    • H(key) = ( key+ 52) MOD 11

    • H(key) = ( key+ 53) MOD 11

    • 2743615

    • H(key) = ( key+2) MOD 11

    • H(key) = ( key+7) MOD 11

    • H(key) = ( key+ 4) MOD 11


4099142

  • p(i) = h(K)+(- 1) i-1 ( ( i + 1) /2)2

    Tsize = 4j +3 (Radke 1970)

    j = 4 Tsize = 19h(K)=9,


4099142

  • bDb = D(b)n(b)n

    UnSnVector: Un ~ [1+1/(1 - a)2]

    Sn ~ [1+1/(1 - a) ]

    a = n/b


4099142

  • D20

  • anb(20)

  • e.g 1000450.5a0.9a 6/7a 6/7b7n/6 = 1167

    b = D 37*37=1369

  • e.g Tsize530Dd23*23 = 529


4099142

20

55

14

27

79

68

01

19

10

23

11

68

0

1

2

3

4

5

6

7

8

9

10

11

12

(List)


4099142

  • template<class E,class K>

  • class ChainHash Table{

  • public:

  • ChainHast Table(int divisor = 11 )

  • {D = divisor;

  • ht = new SortedChain<E,K>[D];}

  • ~ChainHast Table(){delete[]ht;}

  • bool Search(const K&k,E&e ) const

  • {return ht[k%D].Search(k,e);}

  • ChainHash Table<E,K>&Insert(const E&e)

  • {ht[e%D].Distinclnsert(e);

  • return *this;}

  • ChainHash Table<E,K>&Delete(const K&k , E&e)

  • {ht[k%D].Deltet(k,e);

  • return *this;}

  • void Output()const;//

  • private:

  • int D;//

  • SortChain<>*ht;//


4099142

  • posInfoIndex


Vector list

VectorList

  • Sn = 1+ (n-1)/2b ~ 1+ a/2

  • Un ~ a+1/2 a>>1

  • a=0.90.91.4550.55.5


4099142


4099142

  • Cichelli

    h(word) = (length(word))+g(firstletter(word))+g(lastword(word)))modTsize

    g

When all else fails,

try brute force.


4099142

  • FHCD

    Thomas SagerCichelli

    h(word) = h0(word)+g(h1(word))+g(h2(word))

    g

    h0 = (T0(c1)++T0(cm))mod n

    h1 = (T1(c1)++T1(cm))mod r

    h2 = ((T2(c1)++T2(cm))mod r ) + r


4099142


4099142


4099142

http://acm.hit.edu.cn/forum


4099142

Thank You

2005.11.20


  • Login