. (++)

: 1- , 121,

. ..

2005

, , . , , , " " , , . , , .

.

(1707-1783). .

. . 1 ( ), , . , , , . (, ) 1736 .

[image]

. 1

. , - . , (. 2). (, ) 1930 .

. 2

. . , . , (. 3). , . 1976 , . , , . , , ( 2000) , . . . : ? , . . , , .

1

2

2

3

4

. 3

G(V,E) V( ) E V(E ).

, [image] - (x,y), x , y . (x, y) [image]. , [image] x y, y x.[image]

E ( ) V, E , ( ).

E , , , , .

E , V, E , .

F : V > M / F : E > M, M , ( ). . F , () , .

G?(V?,E?), [image]/ [image].

V? = V, G? G.

[image], G? G.

G?(V?,E?) G(V,E), G? G.

() , ( ).

[image], .

[image], , .

, .

( , ) , .

.

.

.

, .

V1 V2

V3

V4 V5

. 4. , ,

, .4:

v1, v3, v1, v4 , ;

v1, v3, v5, v2, v3, v4 , ;

v1, v4, v3, v2, v5 ;

v1, v3, v5, v2, v3, v4, v1 , ;

v1, v3, v4, v1 .

( ), , .

, ( ), .

.

, .

, .

, .

, .

, , .

, , .

( ).

u v [image], .

G .

v G(V,E) v G.

G .

v , .

.

3

2 2

3 3

3

  1. 3

4 4

2

3 3

. 5 ().

, , .

1. .

. 1, 2, 3, ..., An , a p(A1), p(2), ..., p(An) . , , . . ( ).

: p(A1)+p(2)+ ... +p(An)=0,5N, 2(p(A1)+p(2)+ ... +p(An))=N , N .

2. .

. a1, a2, a3, , ak , b1, b2, b3, , bm . a1+a2+a3++ak+b1+b2+b3++bm . a1+a2+a3++ak ( ), b1+b2+b3++bm . , m , . .

.

1. .

2. , , .

3. , - , , .

3. n , n 2, .

. n , 0, 1, 2, ..., (n-1). , , , , . , ()=0, , , ()=n-1. , (n-1) , , . , , n , 0 (n-1). , n , .

4. n (n 2) , , , .

. . : , n , . , . , , .

5. , .

, ( , ) , .

6. , , , .

7. , , , .

. ( , 3.6). (, ); . 3.6, ?. (, ), .

, 7.

8. 2k , k , .

9. n nn-2.

. , , . (18211895). - . , , , ( 6).

10. .

. : -+=2, , , . , .

. . , . . , , . ?1 , ?2=0. , : , . , . =5, =10, =2-+=2-5+10=7.

: =?1+?2+?3+, ?3 , , ?4 , . .

, , 2, 2=20=3?3+4?4+... . =7=?3+ ?4 + ?5 + , =21=3( ?3 + ?4 + ?5 + ).

, (3?3+3?4+3?5+) < (3?3+4?4+ 5?5+) 3<2, , 2=20, =21; , ( ), . , .

11. ( -) , .

, , , . .

. . . , , , , . , .

, . , . n(p,q) . p , q .

M, , ,

[image]

n(p,q) = O(p2).

, ( ) .

H, , ,

[image]

[image]

n(p,q) = O(pq).

, ,

N : record v : 1..p; n :^ N end record,

. n(p,q) = O(p+2q), n(p,q) = O(p+q).

E : array [1..q] of record b,e : 1..p end record,

, (, , ). ( ) n(p,q) = O(2q).

. -, .

: , , , , , , , .. - , , , .

:

-

-

-

-

-

-

-

-

-

, . . , NP-, .. . ++, , .

., ., . : . : , 2001

. . : , , 1978.

.. . , , 2001.

.. . , , 1999.

1. , . , .

#include <iostream.h>

#include <stdio.h>

FILE* fi = fopen("g_graph.txt","r"); //

FILE* fo = fopen("g_cycle.txt","w"); //

bool **graph; //

int n; //

const int vertex = 1; //

int *St; //

int *Nnew; // :

void out_way(int n){ //

for(int i=0;i<n;i++)

fprintf(fo,"%d ",St[i]);

fprintf(fo,"%d ",vertex);

}

void gamilton_path(int k){

int v = St[k-1]; //

for(int j = 0;j < n;j++)

if(graph[v][j]==1) // v j

if(k==n && j==vertex)out_way(n); //

else

if(Nnew[j]){ //

St[k]=j; //

Nnew[j]=false; //

gamilton_path(k+1); //

Nnew[j]=true; //

}

}

int main(){

fscanf(fi,"%d",&n); //

St = new int[n];

Nnew = new int[n];

for(int i = 0;i < n;i++)Nnew[i]=1; //

graph = new bool*[n];

for(int i=0;i<n;i++){

graph[i] = new bool[n]; //

for(int j=0;j<n;j++){

fscanf(fi,"%d",&graph[i][j]);

}

}

Nnew[vertex]=false; //

St[0]=vertex; //

gamilton_path(1);//

fcloseall();

return(0);

}

2. , . , .

#include <iostream.h>

#include <stdio.h>

struct Node{

int inf;

Node *next;

};

//============================Stack==============================

Node *init(){ //

return NULL;

}

void push(Node *&st,int dat){ //

Node *el = new Node;

el->inf = dat;

el->next = st;

st=el;

}

int pop(Node *&st){ //

int value = st->inf;

Node *temp = st;

st = st->next;

delete temp;

return value;

}

int peek(Node *st){ //

return st->inf;

}

//==============================================================

Node **graph; //

const int vertex = 1; //

FILE* fi = fopen("e_graph.txt","r"); //

FILE* fo = fopen("e_cycle.txt","w"); //

void add(Node*& list,int data){ //

if(!list){list=new Node;list->inf=data;list->next=0;return;}

Node *temp=list;

while(temp->next)temp=temp->next;

Node *elem=new Node;

elem->inf=data;

elem->next=NULL;

temp->next=elem;

}

void del(Node* &l,int key){ // key

if(l->inf==key){Node *tmp=l; l=l->next; delete tmp;}

else{

Node *tmp=l;

while(tmp){

if(tmp->next) //

if(tmp->next->inf==key){ //

Node *tmp2=tmp->next;

tmp->next=tmp->next->next;

delete tmp2;

}

tmp=tmp->next;

}

}

}

bool eiler(Node **gr,int num){ //

int count;

for(int i=0;i<num;i++){ //

count=0;

Node *tmp=gr[i];

while(tmp){ //

count++;

tmp=tmp->next;

}

if(count%2==1)return 0; //

}

return 1; //

}

void eiler_path(Node **gr){ //

Node *S = init();//

int v=vertex;// 1 ()

int u;

push(S,v); //

while(S){ //

v = peek(S); //

if(!gr[v]){ //

v=pop(S); fprintf(fo,"%d ",v); //

}else {

u=gr[v]->inf; push(S,u); //

del(gr[v],u); del(gr[u],v); //

}

}

}

int main(){

int n; //

int zn;//

fscanf(fi,"%d",&n); graph=new Node*[n];

for(int i=0;i<n;i++)graph[i]=NULL;

for(int i=0;i<n;i++) //

for(int j=0;j<n;j++){

fscanf(fi,"%d",&zn);

if(zn) add(graph[i],j);

}

if(eiler(graph,n))eiler_path(graph);

else fprintf(fo," .");

return(0);

}

3. , .

#include <iostream.h>

#include <stdio.h>

#include <values.h>

FILE* fi = fopen("p_graph.txt","r"); //

FILE* fo = fopen("p_ostov.txt","w"); //

struct edge{ //

int b,e;

int weigh;

};

int **graph; //

int p; //

edge *SST; //

int num_ver=0; //

int *S; // ,

int num_s=0; //

int calc_ver(){ //

int i=0,j,res=0;

while(i<p){j=i+1; while(j++<p)if(graph[i][j])res+=1; i++;}

return res;

}

bool added(int v){ // : v

for(int i=0;i<num_s;i++)if(v==S[i])return 1;

return 0;

}

void prim(){ //

S = new int[calc_ver()];

int *alf = new int[p]; // ,

int *bet = new int[p]; // ,

int u = 0; //

S[num_s] = u; num_s++; //

for(int v=0;v<p;v++)

if(graph[v][u]){alf[v] = u; bet[v] = graph[u][v];}//u -

else {alf[v] = -1; bet[v] = MAXINT;}

int w,x;

for(int i=0;i<p-1;i++){

x = MAXINT;

for(int v=0;v<p;v++){ //

if(bet[v]<x && !added(v)){w = v; x = bet[v];}

}

S[num_s] = w; num_s++; //

//

SST[num_ver].b=alf[w];SST[num_ver].e=w;SST[num_ver].weigh=graph[alf[w]][w];

num_ver++;

for(int v=0;v<p;v++){

if(graph[v][w] && !added(v))//

if(bet[v]>graph[v][w]){alf[v]=w; bet[v]=graph[v][w];}

}

}

}

void out(){

int weight=0;

fprintf(fo,"%d ",num_ver);

for(int i=0;i<num_ver;i++){

fprintf(fo,"%2d %2d %2d ",SST[i].b,SST[i].e,SST[i].weigh);

weight+=SST[i].weigh;

}

fprintf(fo,"%s%d"," : ",weight);

}

int main(){

fscanf(fi,"%d",&p); //

graph = new int*[p];

for(int i=0;i<p;i++){

graph[i] = new int[p]; //

for(int j=0;j<p;j++){

fscanf(fi,"%d",&graph[i][j]);

}

}

SST = new edge[calc_ver()];

prim();

out();

fcloseall();

return 0;

}

4. , .

#include <iostream.h>

#include <stdio.h>

#include <conio.h>

FILE* fi = fopen("k_graph.txt","r"); //

FILE* fo = fopen("k_ostov.txt","w"); //

struct edge{ //

int beg,end;

int weigh;

};

edge *E; //

edge *SST; //

int num_edge; //

int n; //

bool *nov; //

void sort(edge *arr){ //

edge tmp;

for(int i=0;i<n-1;i++)

for(int j=n-1;j>i;j--)

if(arr[j].weigh<arr[j-1].weigh){

tmp = arr[j-1]; arr[j-1] = arr[j]; arr[j] = tmp;

}

}

int smezh(int v1,edge v2){

// v1 v2

if(v1==v2.beg)return(v2.end);

else if(v1==v2.end)return(v2.beg);

else return -1; // v1 v2

}

bool cycle(int v,int v0){ //

nov[v] = 0; // v

for(int i=0;i<num_edge-1;i++)

if(smezh(v,SST[i])!=-1) //

if(smezh(v,SST[i])==v0)return 1; //

else if(nov[smezh(v,SST[i])]) //

if(cycle(smezh(v,SST[i]),v0))return 1;

nov[v] = 1; //

return 0;

}

void kruskal(){

num_edge = 0; //

sort(E); // E

for(int i=0;i<n;i++){

SST[num_edge] = E[i]; num_edge++; //

// -

if(cycle(SST[num_edge-1].end,SST[num_edge-1].beg))num_edge--;

for(int i=0;i<n;i++)nov[i]=1;

}

}

void out(edge *mas,int num){ //

int sum=0;

fprintf(fo,"%d ",num); //

for(int i=0;i<num;i++){

fprintf(fo,"%d %d %3d ",mas[i].beg,mas[i].end,mas[i].weigh);

sum+=mas[i].weigh;

}

fprintf(fo,"%s%d"," : ",sum);

}

int main(){

fscanf(fi,"%d",&n); //

E = new edge[n];

SST = new edge[n];

nov = new bool[n+1];

for(int i=0;i<n;i++)nov[i] = 1;

for(int i=0;i<n;i++) //

fscanf(fi,"%d%d%d3",&E[i].beg,&E[i].end,&E[i].weigh);

kruskal(); //

out(SST,num_edge); //

return 0;

}

5. .

#include <iostream.h>

#include <stdio.h>

#include <conio.h>

FILE* fi = fopen("m_graph.txt","r");

FILE* fo = fopen("m_par.txt","w");

struct edge{ //

int b,e;

};

int n; //

edge *graph; //

edge *matching; //

int num_mat; //

bool smezh(edge q1,edge q2){ // 1 - q1 q2 , -0

return q1.b==q2.b||q1.b==q2.e||q1.e==q2.b||q1.e==q2.e;

}

void out(edge *m,int num){

fprintf(fo,"%d ",num); //

for(int i=0;i<num;i++)

fprintf(fo,"%d %d ",m[i].b,m[i].e);

}

bool bad(){// 1,

for(int i=0;i<num_mat-1;i++)

if(smezh(matching[i],matching[num_mat-1]))return 1;

return 0;

}

void solve(){ //

num_mat = 0;

for(int i=0;i<n;i++){

matching[num_mat]=graph[i];num_mat++; //

if(bad())num_mat--; // -

}

}

int main(){

fscanf(fi,"%d",&n);

graph = new edge[n];

matching = new edge[n];

for(int i=0;i<n;i++)

fscanf(fi,"%d%d",&graph[i].b,&graph[i].e);

solve();

out(matching,num_mat);

fcloseall();

return 0;

}

1.

"g_graph.txt":

5

0 0 1 1 0

0 0 0 1 1

1 0 0 0 1

1 1 0 0 0

0 1 1 0 0

"g_cycle.txt":

0 2 4 1 3 0

0 3 1 4 2 0

2.

" e_graph.txt ":

5

0 1 1 1 1

1 0 1 1 1

1 1 0 1 1

1 1 1 0 1

1 1 1 1 0

"e_cycle.txt":

1 4 3 2 4 0 3 1 2 0 1

3.

" p_graph.txt ":

9

0 4 5 2 0 0 0 0 0

4 0 7 0 0 0 6 0 0

5 7 0 3 0 0 0 0 0

2 0 3 0 0 0 0 0 0

0 0 0 0 0 7 8 0 0

0 0 0 0 7 0 1 0 0

0 6 0 0 8 1 0 3 1

0 0 0 0 0 0 3 0 2

0 0 0 0 0 0 1 2 0

"p_ostov.txt":

8

0 3 2

3 2 3

0 1 4

1 6 6

6 5 1

6 8 1

8 7 2

5 4 7

: 26

4.

" k_graph.txt ":

5

0 1 1

1 2 2

2 3 1

0 3 4

1 3 2

"k_ostov.txt":

3

0 1 1

2 3 1

1 2 2

: 4

5.

" m_graph.txt ":

7

0 1

1 4

0 3

3 2

1 3

2 0

2 5

"m_par.txt":

2

0 1

3 2





1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.

15.

,