diff --git "a/Backtrack/01\350\203\214\345\214\205\351\227\256\351\242\230_\351\200\222\345\275\222.c" "b/Backtrack/01\350\203\214\345\214\205\351\227\256\351\242\230_\351\200\222\345\275\222.c" new file mode 100644 index 0000000..daa553d --- /dev/null +++ "b/Backtrack/01\350\203\214\345\214\205\351\227\256\351\242\230_\351\200\222\345\275\222.c" @@ -0,0 +1,58 @@ +#include +#define N 5 +#define M 20 + +int maxvalue; +int weighted; + +void display_outcome(int *answer,int *weight,int *value){ + int i; + int myvalue=0,myweight=0; + for(i=0;imaxvalue){ + maxvalue=myvalue; + weighted=myweight; + for(int i=0;i +#define N 5 +#define M 20 + +int maxvalue; +int weighted; + +void display_outcome(int *answer,int *weight,int *value){ + int i; + int myvalue=0,myweight=0; + for(i=0;imaxvalue){ + maxvalue=myvalue; + weighted=myweight; + for(int i=0;i=0){ + record[step]++; + while(record[step]<2 && !condition(weight,answer,step,record[step])) + record[step]++; + if(record[step]>=2) + step--; + else if(step==N-1){ + answer[step]=record[step]; + display_outcome(answer,weight,value); + }else{ + answer[step]=record[step]; + step++; + record[step]=-1; + } + } +} + +int main(int argc, char const *argv[]) +{ + int weight[N]={3,4,5,6,7}; + int value[N]={5,4,3,2,10}; + backtrack(weight,value); + printf("maxvalue=%d\n",maxvalue); + printf("weighted=%d\n",weighted); + return 0; +} \ No newline at end of file diff --git a/Backtrack/ch7code/7.1.c b/Backtrack/ch7code/7.1.c new file mode 100755 index 0000000..b798d2b --- /dev/null +++ b/Backtrack/ch7code/7.1.c @@ -0,0 +1,167 @@ +#include +#include +#define NODESIZE 7 +#define STARTNODE 0 +#define QSIZE 1000 + +struct Node{ + int vertex; + int height; + struct Node *prev; +}; + +struct Node *queue[QSIZE]; +int head,rear; +int qempty(void){ + return head==rear; +} +int qfull(void){ + return head==(rear+1)%QSIZE; +} +void enqueue(struct Node *item){ + queue[rear]=item; + rear=(rear+1)%QSIZE; +} +struct Node *dequeue(void){ + struct Node *retval=queue[head]; + head=(head+1)%QSIZE; + return retval; +} + +int isneibor(int (*map)[NODESIZE],int node1,int node2){ + return map[node1][node2]; +} + +void getneibors(int (*map)[NODESIZE],int node,int *neibors,int *size){ + int counter=0,i; + for(i=0;ivertex==who) + ok=1; + node=node->prev; + } + return ok; +} + +void displayout_come(struct Node *node){ + printf("%d ",STARTNODE ); + while(node){ + printf("%d ",node->vertex ); + node=node->prev; + } + printf("\n"); +} + +void bfs_search(int (*map)[NODESIZE]){ + int i,step; + struct Node *currentnode=malloc(sizeof(struct Node)); + currentnode->vertex=STARTNODE,currentnode->prev=NULL,currentnode->height=0; + enqueue(currentnode); + int neibors[NODESIZE],neiborsize; + struct Node *node; + while(!qempty()){ + currentnode=dequeue(); + step=currentnode->height; + if(step==NODESIZE-1 && isneibor(map,currentnode->vertex,STARTNODE)){ + displayout_come(currentnode); + //break; + } + getneibors(map,currentnode->vertex,neibors,&neiborsize); + for(i=0;iheight=step+1,node->prev=currentnode,node->vertex=neibors[i]; + enqueue(node); + } + } +} +#define STACKSIZE 100 +struct Node *stack[STACKSIZE]; +int sp=-1; +int sempty(void){ + return sp==-1; +} +void push(struct Node *item){ + stack[++sp]=item; +} +struct Node *pop(void){ + return stack[sp--]; +} +struct Node *top(void){ + return stack[sp]; +} + +void dfs_search(int (*map)[NODESIZE]){ + struct Node *currentnode=malloc(sizeof(struct Node)); + currentnode->vertex=STARTNODE,currentnode->height=0,currentnode->prev=NULL; + push(currentnode); + int neiborsize,neibors[NODESIZE],i; + struct Node *node; + while(!sempty()){ + currentnode=top(); + if(currentnode->height==NODESIZE-1 && isneibor(map,currentnode->vertex,STARTNODE)){ + displayout_come(currentnode); + //break; + } + pop(); + getneibors(map,currentnode->vertex,neibors,&neiborsize); + for(i=0;ivertex=neibors[i],node->height=currentnode->height+1,node->prev=currentnode; + push(node); + } + } +} + +void backtrace(int (*map)[NODESIZE],int *answer,int step,int *visited){ + if(step==NODESIZE && isneibor(map,answer[step-1],STARTNODE)){ + displayoutcome(answer); + return; + } + int myneibor[NODESIZE],neiborsize,currentnode=answer[step-1],i; + getneibors(map,currentnode,myneibor,&neiborsize); + for(i=0;i +#include +#define CUBE 3 +#define HEAPSIZE 100 + + +struct Node{ + int g; + int f; + int x; + int y; + int cube[CUBE][CUBE]; + struct Node *prev; +}; + +#define HEAPSIZE 100 +struct Heap{ + int size; + int capacity; + struct Node **data; +}; +int heap_empty(struct Heap *heap){ + return heap->size==0; +} +void heap_swap(struct Heap *heap,int pos1,int pos2){ + struct Node *tmp=heap->data[pos1]; + heap->data[pos1]=heap->data[pos2]; + heap->data[pos2]=tmp; +} +void heap_hold(struct Heap *heap,int pos){ + int left=2*pos+1,right=2*pos+2; + int minpos=pos; + if(rightsize && heap->data[right]->f < heap->data[minpos]->f) + minpos=right; + if(leftsize && heap->data[left]->f < heap->data[minpos]->f) + minpos=left; + if(minpos!=pos){ + heap_swap(heap,minpos,pos); + heap_hold(heap,minpos); + } +} +struct Heap *init_heap(void){ + struct Heap *heap=malloc(sizeof(struct Heap)); + heap->size=0,heap->capacity=HEAPSIZE; + heap->data=malloc(sizeof(struct Node*)*heap->capacity); + return heap; +} + +void insert_heap(struct Heap *heap,struct Node *new){ + int currentpos=heap->size,parent; + heap->data[heap->size++]=new; + while((parent=(currentpos-1)/2)>=0){ + if(heap->data[currentpos]->f >= heap->data[parent]->f) + break; + heap_swap(heap,currentpos,parent); + } +} + +struct Node *delete_min(struct Heap *heap){ + if(heap_empty(heap)) + return NULL; + struct Node *retval=heap->data[0]; + heap->data[0]=heap->data[--(heap->size)]; + heap_hold(heap,0); + return retval; +} + +int cube_equal(int (*m1)[CUBE],int (*m2)[CUBE]){ + int i,j,ok=1; + for(i=0;icube); + printf("^\n"); + printf("^\n"); + printf("^\n"); + node=node->prev; + } +} + +int inme(struct Node *node,int (*m)[CUBE]){ + int ok=0; + while(node && !ok){ + if(cube_equal(node->cube,m)) + ok=1; + node=node->prev; + } + return ok; +} + +void astar(int (*cube)[CUBE],int x,int y,int (*target)[CUBE]){ + struct Node *currentnode=malloc(sizeof(struct Node)),*node; + currentnode->x=x,currentnode->y=y,currentnode->prev=NULL; + currentnode->g=0,currentnode->f=0; + cube_copy(cube,currentnode->cube); + struct Heap *heap=init_heap(); + insert_heap(heap,currentnode); + + int tmpcube[CUBE][CUBE]; + int counter=0,h; + while(!heap_empty(heap)){ + counter+=1; + currentnode=delete_min(heap); + + if(cube_equal(currentnode->cube,target)){ + displayoutcome(currentnode); + break; + } + //up + cube_copy(currentnode->cube,tmpcube); + if(currentnode->x > 0){ + move_up(tmpcube,currentnode->x,currentnode->y); + if(!inme(currentnode,tmpcube)){ + // printf("UP!\n"); + node=malloc(sizeof(struct Node)); + cube_copy(tmpcube,node->cube); + node->x=currentnode->x-1,node->y=currentnode->y; + node->prev=currentnode; + node->g=currentnode->g+1; + h=calc_weight(target,node->cube); + node->f=node->g+h; + insert_heap(heap,node); + } + } + //down + cube_copy(currentnode->cube,tmpcube); + if(currentnode->x x,currentnode->y); + if(!inme(currentnode,tmpcube)){ + //printf("DOWN!\n"); + node=malloc(sizeof(struct Node)); + cube_copy(tmpcube,node->cube); + node->x=currentnode->x+1,node->y=currentnode->y; + node->prev=currentnode; + node->g=currentnode->g+1; + h=calc_weight(target,node->cube); + node->f=node->g+h; + insert_heap(heap,node); + } + } + //left + cube_copy(currentnode->cube,tmpcube); + if(currentnode->y > 0){ + move_left(tmpcube,currentnode->x,currentnode->y); + if(!inme(currentnode,tmpcube)){ + node=malloc(sizeof(struct Node)); + cube_copy(tmpcube,node->cube); + node->x=currentnode->x,node->y=currentnode->y-1; + node->prev=currentnode; + node->g=currentnode->g+1; + h=calc_weight(target,node->cube); + node->f=node->g+h; + insert_heap(heap,node); + } + } + //right + cube_copy(currentnode->cube,tmpcube); + if(currentnode->y < CUBE-1){ + move_right(tmpcube,currentnode->x,currentnode->y); + if(!inme(currentnode,tmpcube)){ + // printf("RIGHT\n"); + node=malloc(sizeof(struct Node)); + cube_copy(tmpcube,node->cube); + node->x=currentnode->x,node->y=currentnode->y+1; + node->prev=currentnode; + node->g=currentnode->g+1; + h=calc_weight(target,node->cube); + node->f=node->g+h; + insert_heap(heap,node); + } + } + } +} + +int main(int argc, char const *argv[]){ + int origin[CUBE][CUBE]={{2,3,-1},{1,8,5},{7,4,6}}; + int x=0,y=2; + int target[CUBE][CUBE]={{1,2,3},{8,-1,4},{7,6,5}}; + astar(origin,x,y,target); + return 0; +} \ No newline at end of file diff --git a/Backtrack/ch7code/7.2/best.c b/Backtrack/ch7code/7.2/best.c new file mode 100755 index 0000000..355279b --- /dev/null +++ b/Backtrack/ch7code/7.2/best.c @@ -0,0 +1,230 @@ +#include +#include +#define CUBE 3 +#define HEAPSIZE 100 + +struct Node{ + int cube[CUBE][CUBE]; + int x; + int y; + int weight; + struct Node *prev; +}; + +struct Heap{ + int size; + int capacity; + struct Node **data; +}; + +int heap_empty(struct Heap *heap){ + return heap->size==0; +} + +void heap_swap(struct Heap *heap,int pos1,int pos2){ + struct Node *tmp=heap->data[pos1]; + heap->data[pos1]=heap->data[pos2]; + heap->data[pos2]=tmp; +} + +void heap_hold(struct Heap *heap,int pos){ + int left=2*pos+1,right=2*pos+2; + int minpos=pos; + if(rightsize && heap->data[right]->weight < heap->data[minpos]->weight) + minpos=right; + if(leftsize && heap->data[left]->weight < heap->data[minpos]->weight) + minpos=left; + if(minpos!=pos){ + heap_swap(heap,minpos,pos); + heap_hold(heap,minpos); + } +} +struct Heap *init_heap(void){ + struct Heap *heap=malloc(sizeof(struct Heap)); + heap->size=0,heap->capacity=HEAPSIZE; + heap->data=malloc(sizeof(struct Node*)*heap->capacity); + return heap; +} + +void insert_heap(struct Heap *heap,struct Node *new){ + int currentpos=heap->size,parent; + heap->data[heap->size++]=new; + while((parent=(currentpos-1)/2)>=0){ + if(heap->data[currentpos]->weight >= heap->data[parent]->weight) + break; + heap_swap(heap,currentpos,parent); + } +} + +struct Node *delete_min(struct Heap *heap){ + if(heap_empty(heap)) + return NULL; + struct Node *retval=heap->data[0]; + heap->data[0]=heap->data[--(heap->size)]; + heap_hold(heap,0); + return retval; +} +int cube_equal(int (*m1)[CUBE],int (*m2)[CUBE]){ + int i,j,ok=1; + for(i=0;icube); + printf("^\n"); + printf("^\n"); + printf("^\n"); + node=node->prev; + } +} + +int inme(struct Node *node,int (*m)[CUBE]){ + int ok=0; + while(node && !ok){ + if(cube_equal(node->cube,m)) + ok=1; + node=node->prev; + } + return ok; +} + +void insertsort(struct Node **data,int size){ + int i,j; + struct Node *pivot; + for(i=1;i=0 && data[j]->weight < pivot->weight;j--) + data[j+1]=data[j]; + data[j+1]=pivot; + } +} + +void hillclimbing(int (*cube)[CUBE],int x,int y,int (*target)[CUBE]){ + struct Node *currentnode=malloc(sizeof(struct Node)),*node; + currentnode->x=x,currentnode->y=y,currentnode->prev=NULL; + currentnode->weight=calc_weight(target,cube); + cube_copy(cube,currentnode->cube); + struct Heap *heap=init_heap(); + insert_heap(heap,currentnode); + int tmpcube[CUBE][CUBE]; + while(!heap_empty(heap)){ + currentnode=delete_min(heap); + + if(cube_equal(currentnode->cube,target)){ + displayoutcome(currentnode); + break; + } + //up + cube_copy(currentnode->cube,tmpcube); + if(currentnode->x > 0){ + move_up(tmpcube,currentnode->x,currentnode->y); + if(!inme(currentnode,tmpcube)){ + // printf("UP!\n"); + node=malloc(sizeof(struct Node)); + cube_copy(tmpcube,node->cube); + node->x=currentnode->x-1,node->y=currentnode->y; + node->prev=currentnode; + node->weight=calc_weight(target,node->cube); + insert_heap(heap,node); + } + } + //down + cube_copy(currentnode->cube,tmpcube); + if(currentnode->x x,currentnode->y); + if(!inme(currentnode,tmpcube)){ + //printf("DOWN!\n"); + node=malloc(sizeof(struct Node)); + cube_copy(tmpcube,node->cube); + node->x=currentnode->x+1,node->y=currentnode->y; + node->prev=currentnode; + node->weight=calc_weight(target,node->cube); + insert_heap(heap,node); + } + } + //left + cube_copy(currentnode->cube,tmpcube); + if(currentnode->y > 0){ + move_left(tmpcube,currentnode->x,currentnode->y); + if(!inme(currentnode,tmpcube)){ + node=malloc(sizeof(struct Node)); + cube_copy(tmpcube,node->cube); + node->x=currentnode->x,node->y=currentnode->y-1; + node->prev=currentnode; + node->weight=calc_weight(target,node->cube); + insert_heap(heap,node); + } + } + //right + cube_copy(currentnode->cube,tmpcube); + if(currentnode->y < CUBE-1){ + move_right(tmpcube,currentnode->x,currentnode->y); + if(!inme(currentnode,tmpcube)){ + // printf("RIGHT\n"); + node=malloc(sizeof(struct Node)); + cube_copy(tmpcube,node->cube); + node->x=currentnode->x,node->y=currentnode->y+1; + node->prev=currentnode; + node->weight=calc_weight(target,node->cube); + insert_heap(heap,node); + } + } + } +} + +int main(int argc, char const *argv[]){ + int origin[CUBE][CUBE]={{2,3,-1},{1,8,5},{7,4,6}}; + int x=0,y=2; + int target[CUBE][CUBE]={{1,2,3},{8,-1,4},{7,6,5}}; + hillclimbing(origin,x,y,target); + return 0; +} \ No newline at end of file diff --git a/Backtrack/ch7code/7.2/bfs.c b/Backtrack/ch7code/7.2/bfs.c new file mode 100755 index 0000000..b1114e5 --- /dev/null +++ b/Backtrack/ch7code/7.2/bfs.c @@ -0,0 +1,174 @@ +#include +#include +#define CUBE 3 + +int cube_equal(int (*m1)[CUBE],int (*m2)[CUBE]){ + int i,j,ok=1; + for(i=0;icube); + printf("^\n"); + printf("^\n"); + printf("^\n"); + node=node->prev; + } +} + +int inme(struct Node *node,int (*m)[CUBE]){ + int ok=0; + while(node && !ok){ + if(cube_equal(node->cube,m)) + ok=1; + node=node->prev; + } + return ok; +} + +#define QSIZE 10000 +struct Node *queue[QSIZE]; +int head,rear; +int queue_empty(void){ + return head==rear; +} +int full(void){ + return head==(rear+1)%QSIZE; +} +void enqueue(struct Node *item){ + queue[rear]=item; + rear=(rear+1)%QSIZE; +} +struct Node *dequeue(void){ + struct Node *retval=queue[head]; + head=(head+1)%QSIZE; + return retval; +} +struct Node *queue_top(void){ + return queue[head]; +} + +void bfs_search(int (*cube)[CUBE],int x,int y,int (*target)[CUBE]){ + struct Node *currentnode=malloc(sizeof(struct Node)); + currentnode->x=x,currentnode->y=y,currentnode->prev=NULL; + cube_copy(cube,currentnode->cube); + enqueue(currentnode); + struct Node *node; + int tmpcube[CUBE][CUBE]; + int counter=0; + while(!queue_empty()){ + currentnode=queue_top(); + if(cube_equal(currentnode->cube,target)){ + printf("One method .....\n"); + displayoutcome(currentnode); + counter+=1; + //break; + } + dequeue(); + //up + cube_copy(currentnode->cube,tmpcube); + if(currentnode->x > 0){ + move_up(tmpcube,currentnode->x,currentnode->y); + if(!inme(currentnode,tmpcube)){ + // printf("UP!\n"); + node=malloc(sizeof(struct Node)); + cube_copy(tmpcube,node->cube); + node->x=currentnode->x-1,node->y=currentnode->y; + node->prev=currentnode; + enqueue(node); + } + } + //down + cube_copy(currentnode->cube,tmpcube); + if(currentnode->x x,currentnode->y); + if(!inme(currentnode,tmpcube)){ + //printf("DOWN!\n"); + node=malloc(sizeof(struct Node)); + cube_copy(tmpcube,node->cube); + node->x=currentnode->x+1,node->y=currentnode->y; + node->prev=currentnode; + enqueue(node); + } + } + //left + cube_copy(currentnode->cube,tmpcube); + if(currentnode->y > 0){ + move_left(tmpcube,currentnode->x,currentnode->y); + if(!inme(currentnode,tmpcube)){ + // printf("LEFT\n"); + node=malloc(sizeof(struct Node)); + cube_copy(tmpcube,node->cube); + node->x=currentnode->x,node->y=currentnode->y-1; + node->prev=currentnode; + enqueue(node); + } + } + //right + cube_copy(currentnode->cube,tmpcube); + if(currentnode->y < CUBE-1){ + move_right(tmpcube,currentnode->x,currentnode->y); + if(!inme(currentnode,tmpcube)){ + // printf("RIGHT\n"); + node=malloc(sizeof(struct Node)); + cube_copy(tmpcube,node->cube); + node->x=currentnode->x,node->y=currentnode->y+1; + node->prev=currentnode; + enqueue(node); + } + } + } + printf("Total %d methods\n",counter); +} + +int main(int argc, char const *argv[]){ + int origin[CUBE][CUBE]={{2,3,-1},{1,8,5},{7,4,6}}; + int x=0,y=2; + int target[CUBE][CUBE]={{1,2,3},{8,-1,4},{7,6,5}}; + bfs_search(origin,x,y,target); + return 0; +} \ No newline at end of file diff --git a/Backtrack/ch7code/7.2/dfs.c b/Backtrack/ch7code/7.2/dfs.c new file mode 100755 index 0000000..417b196 --- /dev/null +++ b/Backtrack/ch7code/7.2/dfs.c @@ -0,0 +1,173 @@ +#include +#include +#define CUBE 3 + +int cube_equal(int (*m1)[CUBE],int (*m2)[CUBE]){ + int i,j,ok=1; + for(i=0;icube); + printf("^\n"); + printf("^\n"); + printf("^\n"); + node=node->prev; + } +} + +int inme(struct Node *node,int (*m)[CUBE]){ + int ok=0; + while(node && !ok){ + if(cube_equal(node->cube,m)) + ok=1; + node=node->prev; + } + return ok; +} + +void hillclimbing(int (*cube)[CUBE],int x,int y,int (*target)[CUBE]){ + struct Node *currentnode=malloc(sizeof(struct Node)); + currentnode->x=x,currentnode->y=y,currentnode->prev=NULL; + cube_copy(cube,currentnode->cube); + push(currentnode); + struct Node *node; + int tmpcube[CUBE][CUBE]; + while(!empty()){ + currentnode=top(); + if(cube_equal(currentnode->cube,target)){ + displayoutcome(currentnode); + break; + } + pop(); + nodesszie=0; + //up + cube_copy(currentnode->cube,tmpcube); + if(currentnode->x > 0){ + move_up(tmpcube,currentnode->x,currentnode->y); + if(!inme(currentnode,tmpcube)){ + node=malloc(sizeof(struct Node)); + cube_copy(tmpcube,node->cube); + node->x=currentnode->x-1,node->y=currentnode->y; + node->prev=currentnode; + push(node); + } + } + //down + cube_copy(currentnode->cube,tmpcube); + if(currentnode->x x,currentnode->y); + if(!inme(currentnode,tmpcube)){ + node=malloc(sizeof(struct Node)); + cube_copy(tmpcube,node->cube); + node->x=currentnode->x+1,node->y=currentnode->y; + node->prev=currentnode; + push(node); + } + } + //left + cube_copy(currentnode->cube,tmpcube); + if(currentnode->y > 0){ + move_left(tmpcube,currentnode->x,currentnode->y); + if(!inme(currentnode,tmpcube)){ + node=malloc(sizeof(struct Node)); + cube_copy(tmpcube,node->cube); + node->x=currentnode->x,node->y=currentnode->y-1; + node->prev=currentnode; + push(node); + } + } + //right + cube_copy(currentnode->cube,tmpcube); + if(currentnode->y < CUBE-1){ + move_right(tmpcube,currentnode->x,currentnode->y); + if(!inme(currentnode,tmpcube)){ + node=malloc(sizeof(struct Node)); + cube_copy(tmpcube,node->cube); + node->x=currentnode->x,node->y=currentnode->y+1; + node->prev=currentnode; + push(node); + } + } + } +} + +int main(int argc, char const *argv[]){ + int origin[CUBE][CUBE]={{2,3,-1},{1,8,5},{7,4,6}}; + int x=0,y=2; + int target[CUBE][CUBE]={{1,2,3},{8,-1,4},{7,6,5}}; + hillclimbing(origin,x,y,target); + return 0; +} diff --git a/Backtrack/ch7code/7.2/hillclimb.c b/Backtrack/ch7code/7.2/hillclimb.c new file mode 100755 index 0000000..eb93384 --- /dev/null +++ b/Backtrack/ch7code/7.2/hillclimb.c @@ -0,0 +1,201 @@ +#include +#include +#define CUBE 3 + +int cube_equal(int (*m1)[CUBE],int (*m2)[CUBE]){ + int i,j,ok=1; + for(i=0;icube); + printf("^\n"); + printf("^\n"); + printf("^\n"); + node=node->prev; + } +} + +int inme(struct Node *node,int (*m)[CUBE]){ + int ok=0; + while(node && !ok){ + if(cube_equal(node->cube,m)) + ok=1; + node=node->prev; + } + return ok; +} + +void insertsort(struct Node **data,int size){ + int i,j; + struct Node *pivot; + for(i=1;i=0 && data[j]->weight < pivot->weight;j--) + data[j+1]=data[j]; + data[j+1]=pivot; + } +} + +void hillclimbing(int (*cube)[CUBE],int x,int y,int (*target)[CUBE]){ + struct Node *currentnode=malloc(sizeof(struct Node)); + currentnode->x=x,currentnode->y=y,currentnode->prev=NULL; + currentnode->weight=calc_weight(target,cube); + cube_copy(cube,currentnode->cube); + push(currentnode); + struct Node *node; + int tmpcube[CUBE][CUBE]; + int counter=0; + struct Node *nodes[4]; + int nodesszie,i; + while(!empty()){ + counter+=1; + currentnode=top(); + cube_display(currentnode->cube); + printf("************\n"); + if(cube_equal(currentnode->cube,target)){ + displayoutcome(currentnode); + break; + } + pop(); + nodesszie=0; + //up + cube_copy(currentnode->cube,tmpcube); + if(currentnode->x > 0){ + move_up(tmpcube,currentnode->x,currentnode->y); + if(!inme(currentnode,tmpcube)){ + // printf("UP!\n"); + node=malloc(sizeof(struct Node)); + cube_copy(tmpcube,node->cube); + node->x=currentnode->x-1,node->y=currentnode->y; + node->prev=currentnode; + node->weight=calc_weight(target,node->cube); + nodes[nodesszie++]=node; + } + } + //down + cube_copy(currentnode->cube,tmpcube); + if(currentnode->x x,currentnode->y); + if(!inme(currentnode,tmpcube)){ + //printf("DOWN!\n"); + node=malloc(sizeof(struct Node)); + cube_copy(tmpcube,node->cube); + node->x=currentnode->x+1,node->y=currentnode->y; + node->prev=currentnode; + node->weight=calc_weight(target,node->cube); + nodes[nodesszie++]=node; + } + } + //left + cube_copy(currentnode->cube,tmpcube); + if(currentnode->y > 0){ + move_left(tmpcube,currentnode->x,currentnode->y); + if(!inme(currentnode,tmpcube)){ + // printf("LEFT\n"); + node=malloc(sizeof(struct Node)); + cube_copy(tmpcube,node->cube); + node->x=currentnode->x,node->y=currentnode->y-1; + node->prev=currentnode; + node->weight=calc_weight(target,node->cube); + nodes[nodesszie++]=node; + } + } + //right + cube_copy(currentnode->cube,tmpcube); + if(currentnode->y < CUBE-1){ + move_right(tmpcube,currentnode->x,currentnode->y); + if(!inme(currentnode,tmpcube)){ + // printf("RIGHT\n"); + node=malloc(sizeof(struct Node)); + cube_copy(tmpcube,node->cube); + node->x=currentnode->x,node->y=currentnode->y+1; + node->prev=currentnode; + node->weight=calc_weight(target,node->cube); + nodes[nodesszie++]=node; + } + } + for(i=0;i +#include +#include + +#define NODESIZE 13 +#define SOURCE 0 +#define DEST 12 +#define QSIZE 100 + +struct Node{ + int id; + int vertex; + struct Node *next; +}; + +struct TNode{ + int g; + int f; + int id; + struct TNode *prev; +}; + +#define HEAPSIZE 100 +struct Heap{ + int size; + int capacity; + struct TNode **data; +}; +int heap_empty(struct Heap *heap){ + return heap->size==0; +} +void heap_swap(struct Heap *heap,int pos1,int pos2){ + struct TNode *tmp=heap->data[pos1]; + heap->data[pos1]=heap->data[pos2]; + heap->data[pos2]=tmp; +} +void heap_hold(struct Heap *heap,int pos){ + int left=2*pos+1,right=2*pos+2; + int minpos=pos; + if(rightsize && heap->data[right]->f < heap->data[minpos]->f) + minpos=right; + if(leftsize && heap->data[left]->f < heap->data[minpos]->f) + minpos=left; + if(minpos!=pos){ + heap_swap(heap,minpos,pos); + heap_hold(heap,minpos); + } +} +struct Heap *init_heap(void){ + struct Heap *heap=malloc(sizeof(struct Heap)); + heap->size=0,heap->capacity=HEAPSIZE; + heap->data=malloc(sizeof(struct TNode*)*heap->capacity); + return heap; +} + +void insert_heap(struct Heap *heap,struct TNode *new){ + int currentpos=heap->size,parent; + heap->data[heap->size++]=new; + while((parent=(currentpos-1)/2)>=0){ + if(heap->data[currentpos]->f >= heap->data[parent]->f) + break; + heap_swap(heap,currentpos,parent); + } +} + +struct TNode *delete_min(struct Heap *heap){ + if(heap_empty(heap)) + return NULL; + struct TNode *retval=heap->data[0]; + heap->data[0]=heap->data[--(heap->size)]; + heap_hold(heap,0); + return retval; +} + +void insertsort(struct TNode **data,int size){ + struct TNode *pivot; + int i,j; + for(i=1;i=0 && data[j]->f > pivot->f;j--) + data[j+1]=data[j]; + data[j+1]=pivot; + } +} + +void insertedge(struct Node **map,int node1,int node2,int vertex){ + struct Node *cur=map[node1]; + struct Node *new=malloc(sizeof(struct Node)); + new->id=node2,new->vertex=vertex,new->next=cur; + map[node1]=new; +} + +int find_min_h(struct Node **map,int nodeid){ + struct Node *cur=map[nodeid]; + int h=INT_MAX; + while(cur){ + if(cur->vertex < h) + h=cur->vertex; + cur=cur->next; + } + if(h==INT_MAX) + h=0; + return h; +} + +void displayoutcome(struct TNode *node){ + printf("cost=%d\n",node->g ); + while(node){ + printf("%d <-",node->id ); + node=node->prev; + } + printf("\n"); +} + +void astar(struct Node **map){ + struct Heap *heap=init_heap(); + struct TNode *currentnode=malloc(sizeof(struct TNode)); + currentnode->id=SOURCE,currentnode->g=0,currentnode->prev=NULL,currentnode->f=0; + insert_heap(heap,currentnode); + struct Node *neibor; + int i,htable[NODESIZE]; + struct TNode *node; + for(i=0;iid==DEST){ + displayoutcome(currentnode); + break; + } + neibor=map[currentnode->id]; + while(neibor){ + node=malloc(sizeof(struct TNode)); + node->id=neibor->id; + node->g=currentnode->g+neibor->vertex; + node->f=node->g+htable[node->id]; + node->prev=currentnode; + insert_heap(heap,node); + neibor=neibor->next; + } + } +} + +int main(int argc, char const *argv[]){ + struct Node *map[NODESIZE]={}; + insertedge(map,SOURCE,1,2); + insertedge(map,SOURCE,2,5); + insertedge(map,SOURCE,3,1); + insertedge(map,SOURCE,4,6); + insertedge(map,1,5,1); + insertedge(map,1,5,4); + insertedge(map,2,5,9); + insertedge(map,2,7,7); + insertedge(map,3,5,3); + insertedge(map,3,7,4); + insertedge(map,4,6,7); + insertedge(map,4,7,4); + insertedge(map,5,8,6); + insertedge(map,5,10,7); + insertedge(map,6,8,4); + insertedge(map,6,9,3); + insertedge(map,6,11,5); + insertedge(map,7,9,1); + insertedge(map,7,10,4); + insertedge(map,7,11,5); + insertedge(map,8,DEST,3); + insertedge(map,9,DEST,1); + insertedge(map,10,DEST,2); + insertedge(map,11,DEST,5); + astar(map); + return 0; +} \ No newline at end of file diff --git a/Backtrack/ch7code/7.3_branchbound.c b/Backtrack/ch7code/7.3_branchbound.c new file mode 100755 index 0000000..b581eaa --- /dev/null +++ b/Backtrack/ch7code/7.3_branchbound.c @@ -0,0 +1,123 @@ +#include +#include +#include + +#define NODESIZE 13 +#define SOURCE 0 +#define DEST 12 +#define QSIZE 100 + +struct Node{ + int id; + int vertex; + struct Node *next; +}; + +struct TNode{ + int cost; + int id; + struct TNode *prev; +}; + +struct TNode *stack[QSIZE]; +int sp=-1; +void push(struct TNode *item){ + stack[++sp]=item; +} +struct TNode *pop(void){ + return stack[sp--]; +} +struct TNode *top(void){ + return stack[sp]; +} +int empty(void){ + return sp==-1; +} + +void insertsort(struct TNode **data,int size){ + struct TNode *pivot; + int i,j; + for(i=1;i=0 && data[j]->cost < pivot->cost;j--) + data[j+1]=data[j]; + data[j+1]=pivot; + } +} + +int min=INT_MAX; + +void displayoutcome(struct TNode *node){ + min=node->cost; + while(node){ + printf("%d<==",node->id); + node=node->prev; + } + printf("cost=%d\n",min ); +} + +void branchbound(struct Node **map){ + struct TNode *currentnode=malloc(sizeof(struct TNode)); + currentnode->id=SOURCE,currentnode->cost=0,currentnode->prev=NULL; + push(currentnode); + struct Node *neibor; + struct TNode *nextnode, * neibors[NODESIZE]; + int neiborsize,i; + while(!empty()){ + currentnode=top(); + if(currentnode->id==DEST && currentnode->costcost>min) + continue; + neibor=map[currentnode->id]; + neiborsize=0; + while(neibor){ + nextnode=malloc(sizeof(struct TNode)); + nextnode->id=neibor->id,nextnode->prev=currentnode; + nextnode->cost=currentnode->cost+neibor->vertex; + neibors[neiborsize++]=nextnode; + neibor=neibor->next; + } + insertsort(neibors,neiborsize); + for(i=0;iid=node2,new->vertex=vertex,new->next=cur; + map[node1]=new; +} + +int main(int argc, char const *argv[]){ + struct Node *map[NODESIZE]={}; + insertedge(map,SOURCE,1,2); + insertedge(map,SOURCE,2,5); + insertedge(map,SOURCE,3,1); + insertedge(map,SOURCE,4,6); + insertedge(map,1,5,1); + insertedge(map,1,5,4); + insertedge(map,2,5,9); + insertedge(map,2,7,7); + insertedge(map,3,5,3); + insertedge(map,3,7,4); + insertedge(map,4,6,7); + insertedge(map,4,7,4); + insertedge(map,5,8,6); + insertedge(map,5,10,7); + insertedge(map,6,8,4); + insertedge(map,6,9,3); + insertedge(map,6,11,5); + insertedge(map,7,9,1); + insertedge(map,7,10,4); + insertedge(map,7,11,5); + insertedge(map,8,DEST,3); + insertedge(map,9,DEST,1); + insertedge(map,10,DEST,2); + insertedge(map,11,DEST,5); + branchbound(map); + return 0; +} diff --git "a/Backtrack/ch7code/7.4\346\234\211\347\202\271\347\274\272\346\206\276.c" "b/Backtrack/ch7code/7.4\346\234\211\347\202\271\347\274\272\346\206\276.c" new file mode 100755 index 0000000..2b3cc9d --- /dev/null +++ "b/Backtrack/ch7code/7.4\346\234\211\347\202\271\347\274\272\346\206\276.c" @@ -0,0 +1,113 @@ +#include +#include +#include + +#define NODESIZE 5 +#define STARTNODE 0 + +struct Node{ + int id; + int cost; + int height; + struct Node *prev; +}; +#define QSIZE 100 +struct Node *stack[QSIZE]; +int sp=-1; +int empty(void){ + return sp==-1; +} +void push(struct Node *item){ + stack[++sp]=item; +} +struct Node *pop(void){ + return stack[--sp]; +} +struct Node *top(void){ + return stack[sp]; +} + +void insertsort(struct Node **nodes,int size){ + int i,j; + struct Node *pivot; + for(i=1;i=0 && nodes[j]->cost < pivot->cost;j--) + nodes[j+1]=nodes[j]; + nodes[j+1]=pivot; + } +} + +int min=INT_MAX; + +int sum(struct Node *node){ + int summary=0; + while(node){ + summary+=node->cost; + node=node->prev; + } + return summary; +} + +int inme(struct Node *node,int who){ + int ok=0; + while(node && !ok){ + if(node->id==who) + ok=1; + node=node->prev; + } + return ok; +} + +void display_outcome(struct Node *node,int newmin){ + min=newmin; + printf("%d",STARTNODE); + while(node){ + printf("<===%d ",node->id); + node=node->prev; + } + printf(".And cost=%d\n",min ); +} + +void branchbound(int (*map)[NODESIZE]){ + struct Node *currentnode=malloc(sizeof(struct Node)); + currentnode->id=STARTNODE,currentnode->height=0,currentnode->prev=NULL,currentnode->cost=0; + push(currentnode); + struct Node *node; + struct Node *neibors[NODESIZE]; + int neiborsize,i; + while(!empty()){ + currentnode=top(); + if(currentnode->height==NODESIZE-1 && currentnode->cost+map[currentnode->id][STARTNODE]cost+map[currentnode->id][STARTNODE]); + pop(); + if(currentnode->cost > min){ + printf("drop one.\n"); + continue; + } + neiborsize=0; + for(i=0;iid && !inme(currentnode,i)){ + node=malloc(sizeof(struct Node)); + node->id=i,node->height=currentnode->height+1,node->prev=currentnode; + node->cost=currentnode->cost+map[currentnode->id][i]; + neibors[neiborsize++]=node; + } + insertsort(neibors,neiborsize); + for(i=0;i +#include +#include + +#define SIZE 6 +#define K 18 + +void displayoutcome(int *set,int *answer,int size){ + int i; + for(i=0;iK){ + printf("Drop branch.\n"); + return ; + } + if(step==SIZE){ + if(summary==K) + displayoutcome(set,answer,step); + return ; + } + int sel; + for(sel=0;sel<2;sel++){ + answer[step]=sel; + branchbound(set,answer,step+1); + } +} + +int main(int argc, char const *argv[]) +{ + int set[SIZE]={7,4,6,13,20,8}; + int answer[SIZE]; + printf("use DFS:\n"); + BFS_search(set,answer,0); + printf("use Branch and Bound:\n"); + branchbound(set,answer,0); + return 0; +} \ No newline at end of file diff --git a/Backtrack/ch7code/ch7.pdf b/Backtrack/ch7code/ch7.pdf new file mode 100755 index 0000000..c56378f Binary files /dev/null and b/Backtrack/ch7code/ch7.pdf differ diff --git "a/Backtrack/ch7code/ch7\344\271\240\351\242\230.pdf" "b/Backtrack/ch7code/ch7\344\271\240\351\242\230.pdf" new file mode 100755 index 0000000..c92b7f6 Binary files /dev/null and "b/Backtrack/ch7code/ch7\344\271\240\351\242\230.pdf" differ diff --git "a/Backtrack/\345\205\250\346\216\222\345\210\227_\351\200\222\345\275\222.c" "b/Backtrack/\345\205\250\346\216\222\345\210\227_\351\200\222\345\275\222.c" new file mode 100644 index 0000000..48ff78d --- /dev/null +++ "b/Backtrack/\345\205\250\346\216\222\345\210\227_\351\200\222\345\275\222.c" @@ -0,0 +1,47 @@ +#include +#define SIZE 5 + +void print_answer(int *answer,int size){ + static int count; + int i; + for(i=0;i +#define SIZE 5 + +void print_answer(int *answer){ + static int count; + int i; + for(i=0;i=0){ + record[step]++; + while(record[step]SIZE-1){ + step--; + }else if(step==SIZE-1){ + answer[step]=record[step]+1; + print_answer(answer); + }else{ + answer[step]=record[step]+1; + step++; + record[step]=-1; + } + } +} + +int main(int argc, char const *argv[]) +{ + int set[]={1,2,3,4,5}; + backtrack(set); + return 0; +} \ No newline at end of file diff --git "a/Backtrack/\345\205\253\347\232\207\345\220\216_\351\200\222\345\275\222.c" "b/Backtrack/\345\205\253\347\232\207\345\220\216_\351\200\222\345\275\222.c" new file mode 100644 index 0000000..490e93f --- /dev/null +++ "b/Backtrack/\345\205\253\347\232\207\345\220\216_\351\200\222\345\275\222.c" @@ -0,0 +1,58 @@ +#include +#define SIZE 8 + +int count; + +void display_outcome(int *answer){ + int i; + for(i=0;i +#define SIZE 8 + +int count; + +void display_outcome(int *answer){ + int i; + for(i=0;i=0){ + record[step]++; + while(record[step]SIZE-1) + step--; + else if(step==SIZE-1){ + answer[step]=record[step]; + display_outcome(answer); + }else{ + answer[step]=record[step]; + step++; + record[step]=-1; + } + } +} + +int main(int argc, char const *argv[]) +{ + backtrack(); + printf("count=%d\n", count); + return 0; +} \ No newline at end of file diff --git "a/Backtrack/\345\217\267\347\240\201\345\257\271\345\272\224\347\232\204\345\205\250\351\203\250\345\255\227\347\254\246\344\270\262_\351\200\222\345\275\222.c" "b/Backtrack/\345\217\267\347\240\201\345\257\271\345\272\224\347\232\204\345\205\250\351\203\250\345\255\227\347\254\246\344\270\262_\351\200\222\345\275\222.c" new file mode 100644 index 0000000..dac09bb --- /dev/null +++ "b/Backtrack/\345\217\267\347\240\201\345\257\271\345\272\224\347\232\204\345\205\250\351\203\250\345\255\227\347\254\246\344\270\262_\351\200\222\345\275\222.c" @@ -0,0 +1,44 @@ +#include +#define N 10 + +static char ch[10][4] = +{ + "", + "", + "ABC", + "DEF", + "GHI", + "JKL", + "MNO", + "PQRS", + "TUV", + "WXYZ", +}; +int size[10]={0,0,3,3,3,3,3,4,3,4}; + +void display_outcome(int *number,int *answer){ + int i; + for(i=0;i +#define N 10 + +static char ch[10][4] = +{ + "", + "", + "ABC", + "DEF", + "GHI", + "JKL", + "MNO", + "PQRS", + "TUV", + "WXYZ", +}; +int size[10]={0,0,3,3,3,3,3,4,3,4}; + +void display_outcome(int *number,int *answer){ + int i; + for(i=0;i=0){ + record[step]++; + if(record[step]>=size[number[step]]) + step--; + else if(step==N-1){ + answer[step]=record[step]; + display_outcome(number,answer); + }else{ + answer[step]=record[step]; + step++; + record[step]=-1; + } + } +} + +int main(int argc, char const *argv[]) +{ + int number[10]={4,8,6,9,4,6,8,6,6,4}; + backtrack(number); + return 0; +} \ No newline at end of file diff --git "a/Backtrack/\345\223\210\345\257\206\351\241\277\347\216\257/main.c" "b/Backtrack/\345\223\210\345\257\206\351\241\277\347\216\257/main.c" new file mode 100644 index 0000000..98ad65f --- /dev/null +++ "b/Backtrack/\345\223\210\345\257\206\351\241\277\347\216\257/main.c" @@ -0,0 +1,78 @@ +#include + +#define STARTNODE 0 + +int isneibor(int (*map)[6],int node1,int node2){ + return map[node1][node2]; +} + +void displayoutcome(int *answer,int size){ + for(int i=0;i=1){ + record[step]++; + curnode=answer[step-1]; + while(record[step] +#define N 8 + +int count; + +void display_outcome(char *answer){ + int i; + for(i=0;i +#define N 8 + +int count; + +void display_outcome(char *answer){ + int i; + for(i=0;i=0){ + record[step]++; + while(record[step]<26 && !condition(string,answer,step,record[step])) + record[step]++; + if(record[step]>=26) + step--; + else if(step==N-1){ + answer[step]=record[step]+'a'; + display_outcome(answer); + }else{ + answer[step]=record[step]+'a'; + step++; + record[step]=-1; + } + } +} + +int main(int argc, char const *argv[]) +{ + char *string="leechanx"; + backtrack(string); + printf("%d\n",count); + return 0; +} \ No newline at end of file diff --git "a/Backtrack/\346\211\276sum=K\347\232\204\346\211\200\346\234\211\345\255\220\351\233\206/main_nr.c" "b/Backtrack/\346\211\276sum=K\347\232\204\346\211\200\346\234\211\345\255\220\351\233\206/main_nr.c" new file mode 100755 index 0000000..0c4a388 --- /dev/null +++ "b/Backtrack/\346\211\276sum=K\347\232\204\346\211\200\346\234\211\345\255\220\351\233\206/main_nr.c" @@ -0,0 +1,49 @@ +#include +#define ARRAYSIZE(array) sizeof array/sizeof *array + +void displayoutcome(int *answer,int *set,int step){ + int i; + for(i=0;i=0){ + summary=sum(answer,step,set); + record[step]++; + if(record[step]) + summary+=set[step]; + if(record[step]>=N || summary>9) + step--; + else if(summary==9){ + answer[step]=record[step]; + displayoutcome(answer,set,step+1); + } + else if(step!=size-1){ + answer[step]=record[step]; + step++; + record[step]=-1; + } + } +} + +int main(){ + int set[]={7,5,1,2,8,4,3,10}; + int size=ARRAYSIZE(set); + backtrace(set,size); +} diff --git "a/Backtrack/\346\211\276sum=K\347\232\204\346\211\200\346\234\211\345\255\220\351\233\206/main_r.c" "b/Backtrack/\346\211\276sum=K\347\232\204\346\211\200\346\234\211\345\255\220\351\233\206/main_r.c" new file mode 100755 index 0000000..9dd83c3 --- /dev/null +++ "b/Backtrack/\346\211\276sum=K\347\232\204\346\211\200\346\234\211\345\255\220\351\233\206/main_r.c" @@ -0,0 +1,40 @@ +#include +#define ARRAYSIZE(array) sizeof array/sizeof *array + +void displayoutcome(int *answer,int *set,int step){ + int i; + for(i=0;i9) + return ; + int i; + for(i=0;i<2;i++){ + answer[step]=i; + backtrace(answer,step+1,set,size); + } +} + +int main(){ + int set[]={7,5,1,2,8,4,3,10}; + int size=ARRAYSIZE(set); + int answer[size]; + backtrace(answer,0,set,size); +} diff --git "a/Backtrack/\346\211\276\351\222\261\351\227\256\351\242\230_\351\200\222\345\275\222.c" "b/Backtrack/\346\211\276\351\222\261\351\227\256\351\242\230_\351\200\222\345\275\222.c" new file mode 100644 index 0000000..c174efe --- /dev/null +++ "b/Backtrack/\346\211\276\351\222\261\351\227\256\351\242\230_\351\200\222\345\275\222.c" @@ -0,0 +1,45 @@ +#include +#define PEOPLE 6 +#define YIYUAN 3 +#define WUMAO 3 + +void display_outcome(int *answer){ + int i; + for(i=0;i +#define PEOPLE 6 +#define YIYUAN 3 +#define WUMAO 3 + +void display_outcome(int *answer){ + int i; + for(i=0;i=0){ + record[step]++; + while(record[step]<2 && !condition(answer,step,record[step])) + record[step]++; + if(record[step]>=2) + step--; + else if(step==PEOPLE-1){ + answer[step]=record[step]; + display_outcome(answer); + }else{ + answer[step]=record[step]; + step++; + record[step]=-1; + } + } +} + +int main(int argc, char const *argv[]) +{ + backtrack(); + return 0; +} \ No newline at end of file diff --git "a/Backtrack/\351\233\206\345\220\210\347\232\204K\345\205\203\345\255\220\351\233\206_\351\200\222\345\275\222.c" "b/Backtrack/\351\233\206\345\220\210\347\232\204K\345\205\203\345\255\220\351\233\206_\351\200\222\345\275\222.c" new file mode 100644 index 0000000..24a14f9 --- /dev/null +++ "b/Backtrack/\351\233\206\345\220\210\347\232\204K\345\205\203\345\255\220\351\233\206_\351\200\222\345\275\222.c" @@ -0,0 +1,59 @@ +#include +#define N 5 + +int num; + +void display_outcome(int *answer,int *set){ + int i; + num++; + int count=0; + for(i=0;i +#define N 5 + +int num; + +void display_outcome(int *answer,int *set){ + int i; + num++; + int count=0; + for(i=0;i=0){ + record[step]++; + while(record[step]<2 && condition(answer,target,step,record[step])>target) + record[step]++; + if(record[step]>=2) + step--; + else if(step==N-1){ + answer[step]=record[step]; + if(getsizeok(answer,target)) + display_outcome(answer,set); + }else{ + answer[step]=record[step]; + step++; + record[step]=-1; + } + } +} + +int main(int argc, char const *argv[]) +{ + int set[5]={93,12,43,9,87}; + backtrack(set,3); + printf("subset size=%d\n", num); + return 0; +} \ No newline at end of file diff --git "a/Backtrack/\351\233\206\345\220\210\347\232\204\346\211\200\346\234\211\345\255\220\351\233\206_\351\200\222\345\275\222.c" "b/Backtrack/\351\233\206\345\220\210\347\232\204\346\211\200\346\234\211\345\255\220\351\233\206_\351\200\222\345\275\222.c" new file mode 100644 index 0000000..ed3817c --- /dev/null +++ "b/Backtrack/\351\233\206\345\220\210\347\232\204\346\211\200\346\234\211\345\255\220\351\233\206_\351\200\222\345\275\222.c" @@ -0,0 +1,38 @@ +#include +#define N 5 + +int num; + +void display_outcome(int *answer,int *set){ + int i; + num++; + int count=0; + for(i=0;i +#define N 5 + +int num; + +void display_outcome(int *answer,int *set){ + int i; + num++; + int count=0; + for(i=0;i=0){ + record[step]++; + if(record[step]>=2) + step--; + else if(step==N-1){ + answer[step]=record[step]; + display_outcome(answer,set); + }else{ + answer[step]=record[step]; + step++; + record[step]=-1; + } + } +} + +int main(int argc, char const *argv[]) +{ + /* code */ + int set[5]={93,12,43,9,87}; + subset(set); + printf("subset size=%d\n", num); + return 0; +} \ No newline at end of file diff --git a/DictOrderFindFullArray/DictOrderFindFullArray.c b/DictOrderFindFullArray/DictOrderFindFullArray.c new file mode 100644 index 0000000..ea567d1 --- /dev/null +++ b/DictOrderFindFullArray/DictOrderFindFullArray.c @@ -0,0 +1,54 @@ +#include + +int findj(int *set,int size){ + int i; + for(i=size-2;i>=0;i--) + if(*(set+i)<*(set+i+1)) + break; + return i; +} + +int minestmax_inright(int *set,int j,int size){ + int i; + for(i=size-1;i>j;i--) + if(set[i]>set[j]) + break; + return i; +} + +void reverse(int *set,int head,int size){ + int copy[10],sp=-1; + int i; + for(i=head;i + +int set_find(int *parent,int k){ + int parentnode=parent[k-1]; + if(parentnode==k)//is root + return k; + parent[k-1]=set_find(parent,parentnode); + return parent[k-1]; +} + +void set_union(int *parent,int *rank,int i,int j){ + if(rank[i-1] +#include + +struct SetNode{ + int vertex; + int rank; + struct SetNode *parent; +}; +typedef struct SetNode *snlink; + +snlink create_set(int vertex){ + snlink node=malloc(sizeof(struct SetNode)); + if(!node) + return node; + node->vertex=vertex; + node->rank=0; + node->parent=node; + return node; +} + +snlink find_set(snlink node){ + if(node==node->parent) + return node->parent; + node->parent=find_set(node->parent); + return node->parent; +} + +void union_set(snlink node1,snlink node2){ + printf("Union now.\n"); + if(node1->rankrank) + node1->parent=node2; + else{ + if(node1->rank==node2->rank) + node1->rank++; + node2->parent=node1; + } +} + +int main(int argc, char const *argv[]) +{ + int set[]={1,2,3,4,5,6,7,8,9,10}; + int size=sizeof(set)/sizeof(int); + int i; + snlink disjointset[size]; + for(i=0;i +#include +#define ARRAYSIZE(array) (sizeof array/sizeof *array) +struct Node{ + int x; + int y; +}; + +int available(struct Node *node1,struct Node *node2){ + return (node1->x>=node2->x && node1->y>=node2->y); +} + +void showresult(int *match,struct Node *pset,int qsize,struct Node *qset){ + for(int i=0;i(%d,%d)\n", + pset[match[i]].x,pset[match[i]].y, + qset[i].x,qset[i].y); +} +#define TRUE 1 +#define FALSE 0 +int findout(int *match, + struct Node *pset,struct Node *qset, + int qsize,int *used,int who,int head){ + int new; + for(int j=head;j +#include "list.h" + +int if_has_ring(llink list){ + llink cur_one=list,cur_two=list; + int retval=0; + while(cur_two){ + cur_one=cur_one->next; + cur_two=cur_two->next; + if(cur_two) + cur_two=cur_two->next; + if(cur_one==cur_two){ + retval=1; + break; + } + } + return retval; +} + +void make_ring(llink list,llink that){ + llink cur=list; + if(!cur) + return; + while(cur->next) + cur=cur->next; + cur->next=that; +} + +llink find_item(llink list,int vertex){ + llink cur=list; + while(cur){ + if(cur->vertex==vertex) + return cur; + cur=cur->next; + } + return NULL; +} + +int main(int argc, char const *argv[]) +{ + int data[]={1,2,3,4,5,6,7}; + int size=sizeof(data)/sizeof(int); + llink list=NULL; + int i; + for(i=0;i +#include +#include "list.h" + +void insert_list(llink *list,int item){ + llink new=malloc(sizeof(struct List)); + if(!new) + return; + new->vertex=item; + new->next=NULL; + if(!*list){ + *list=new; + return; + } + llink cur=*list; + while(cur->next) + cur=cur->next; + cur->next=new; +} + +void display_list(llink list){ + while(list){ + printf("%d ",list->vertex); + list=list->next; + } + printf("\n"); +} + +llink insert_list_recursive(llink list,int item){ + if(!list){ + llink new=malloc(sizeof(struct List)); + if(!new) + return new; + new->vertex=item; + new->next=NULL; + return new; + } + list->next=insert_list_recursive(list->next,item); + return list; +} + +llink search_list(llink list,int item){ + llink retval=NULL; + while(list && !retval){ + if(list->vertex==item) + retval=list; + list=list->next; + } + return retval; +} + +int list_length(llink list){ + int length=0; + while(list){ + length++; + list=list->next; + } + return length; +} \ No newline at end of file diff --git a/List/list.h b/List/list.h new file mode 100644 index 0000000..39fb062 --- /dev/null +++ b/List/list.h @@ -0,0 +1,16 @@ +#ifndef LIST_H +#define LIST_H + +struct List{ + int vertex; + struct List *next; +}; +typedef struct List *llink; + +void insert_list(llink *,int); +void display_list(llink); +llink insert_list_recursive(llink,int); +llink search_list(llink,int); +int list_length(llink); + +#endif \ No newline at end of file diff --git a/List/listdelete.c b/List/listdelete.c new file mode 100644 index 0000000..47fd100 --- /dev/null +++ b/List/listdelete.c @@ -0,0 +1,41 @@ +#include +#include +#include "list.h" + +llink find_item(llink list,int vertex){ + while(list){ + if(list->vertex==vertex) + return list; + list=list->next; + } + return NULL; +} + +void delete_item_advanced(llink node){ + llink next=node->next; + if(next){ + node->vertex=next->vertex; + node->next=next->next; + free(next); + }else + printf("cannot delete %d for its the last item\n",node->vertex); +} + +int main(int argc, char const *argv[]) +{ + int data[]={1,2,3,4,5,6,7}; + int size=sizeof(data)/sizeof(int); + llink list=NULL; + int i; + for(i=0;i +#include "list.h" + +llink get_middle_item(llink list){ + llink one_p=list,two_p=list; + while(two_p){ + two_p=two_p->next; + if(two_p) + two_p=two_p->next; + if(two_p) + one_p=one_p->next; + } + return one_p; +} + +llink get_klast_item(llink list,int k){ + int i=1; + llink cur=list,curk=list; + while(i++<=k && cur) + curk=curk->next; + if(i>k && !curk) + return cur; + if(!curk) + return NULL; + while(curk){ + cur=cur->next; + curk=curk->next; + } + return cur; +} + +int main(int argc, char const *argv[]) +{ + int data[]={1,2,3,4,5,6,7}; + int size=sizeof(data)/sizeof(int); + llink list=NULL; + int i; + for(i=0;ivertex); + llink k; + for(i=1;i<=size;i++){ + k=get_klast_item(list,i); + if(k) + printf("%dlast=%d\n",i,k->vertex ); + } + return 0; +} \ No newline at end of file diff --git a/List/mergeList.c b/List/mergeList.c new file mode 100644 index 0000000..8d6a0cf --- /dev/null +++ b/List/mergeList.c @@ -0,0 +1,73 @@ +#include +#include "list.h" + +llink merge_list(llink list1,llink list2){ + llink cur1=list1,cur2=list2; + llink selected,newlist=NULL,newlist_cur=NULL; + while(list1 && list2){ + if(list1->vertexvertex){ + selected=list1; + list1=list1->next; + }else{ + selected=list2; + list2=list2->next; + } + if(!newlist){ + newlist=selected; + newlist_cur=newlist; + }else{ + newlist_cur->next=selected; + newlist_cur=newlist_cur->next; + } + } + if(list1){ + if(!newlist_cur) + newlist=list1; + else + newlist_cur->next=list1; + } + if(list2){ + if(!newlist_cur) + newlist=list2; + else + newlist_cur->next=list2; + } + return newlist; +} + +llink merge_list_recursive(llink list1,llink list2){ + if(list1 && list2){ + if(list1->vertexvertex){ + list1->next=merge_list_recursive(list1->next,list2); + return list1; + }else{ + list2->next=merge_list_recursive(list1,list2->next); + return list2; + } + + } + else if(list1) + return list1; + else + return list2; +} + +int main(int argc, char const *argv[]) +{ + int data1[]={1,3,3,8,9,10}; + int size1=sizeof(data1)/sizeof(int); + int data2[]={2,3,6,7,8,9,12,13}; + int size2=sizeof(data2)/sizeof(int); + + llink list1=NULL,list2=NULL; + int i; + for(i=0;i +#include "list.h" + +void reverse_non_recursive(llink *list){ + llink prev=NULL,cur=*list,next; + while(cur){ + next=cur->next; + cur->next=prev; + prev=cur; + cur=next; + } + *list=prev; +} + +llink reverse_recursive(llink *list,llink cur){ + if(!cur) + return NULL; + llink prev=reverse_recursive(list,cur->next); + cur->next=NULL; + if(prev) + prev->next=cur; + else + *list=cur; + return cur; +} + +int main(int argc, char const *argv[]) +{ + int data[]={1,2,3,4,5,6,7}; + int size=sizeof(data)/sizeof(int); + llink list=NULL; + int i; + for(i=0;i +#include "map.h" + +#define EDGE 20 + +void display_answer(int *answer,int size){ + for(int i=0;i%d",answer[i]); + printf("\n"); +} + +void allpath(struct Graph *map,int *answer,int endnode,int step){ + //now handle step + int prevnode=answer[step-1]; + if(prevnode==endnode) + display_answer(answer,step); + struct Node *cur=map->nodes[prevnode]->next; + while(cur){ + answer[step]=cur->vertex; + allpath(map,answer,endnode,step+1); + cur=cur->next; + } +} + +int isneibor(struct Graph *map,int node,int neibor){ + struct Node *cur=map->nodes[node]->next; + int retval=0; + while(cur && !retval){ + if(cur->vertex==neibor) + retval=1; + cur=cur->next; + } + return retval; +} + +void allpath_nor(struct Graph *map,int startnode,int endnode){ + int answer[EDGE]; + int record[EDGE]; + answer[0]=startnode; + record[0]=startnode; + int step=1,prevnode; + record[step]=-1; + while(step>=1){ + prevnode=answer[step-1]; + record[step]++; + while(record[step]nodesize && !isneibor(map,prevnode,record[step])) + record[step]++; + if(record[step]==map->nodesize) + step--; + else if(record[step]==endnode){ + answer[step]=record[step]; + display_answer(answer,step+1); + }else{ + answer[step]=record[step]; + step+=1; + record[step]=-1; + } + } +} + +int main(int argc, char const *argv[]) +{ + if(argc==1){ + printf("argv NULL!\n"); + return (1); + } + struct Graph *map=read_map(argv[1]); + //display_debug(map); + int startnode,endnode; + int answer[EDGE]; + startnode=0,endnode=7; + //recursive + answer[0]=startnode; + allpath(map,answer,endnode,1); + printf("*************\n"); + //no recursive + allpath_nor(map,startnode,endnode); + return 0; +} \ No newline at end of file diff --git a/Map/All_Pathes/map.c b/Map/All_Pathes/map.c new file mode 100644 index 0000000..4089546 --- /dev/null +++ b/Map/All_Pathes/map.c @@ -0,0 +1,73 @@ +#include +#include +#include + +#include "map.h" + +struct Graph *graph_init(int nodesize){ + struct Graph *map=malloc(sizeof(struct Graph)); + if(!map) + return map; + map->sp=0; + map->nodesize=nodesize; + map->nodes=malloc(sizeof(struct Node *)*nodesize); + return map; +} + +void graph_insertnode(struct Graph *map,int nodename){ + struct Node *newnode=malloc(sizeof(struct Node)); + newnode->next=NULL; + newnode->vertex=nodename; + map->nodes[map->sp++]=newnode; +} + +void graph_insertedge(struct Graph *map,int nodename1,int nodename2){ + struct Node *node=map->nodes[nodename1]; + struct Node *new=malloc(sizeof(struct Node)); + new->vertex=nodename2; + new->next=node->next; + node->next=new; +} + +void display_debug(struct Graph *map){ + int i; + struct Node *cur; + for(i=0;inodesize;i++){ + cur=map->nodes[i]; + printf("%d:",cur->vertex); + cur=cur->next; + while(cur){ + printf(" %d",cur->vertex); + cur=cur->next; + } + printf("\n"); + } +} + +struct Graph *read_map(char const *filename){ + FILE *map_file=fopen(filename,"r"); + int directed,nodesize; + fscanf(map_file,"%d %d\n",&directed,&nodesize); + struct Graph *map=graph_init(nodesize); + char buff[1024]; + char *node_line=fgets(buff,sizeof(buff),map_file); + char *node=strtok(node_line," \n"); + while(node){ + graph_insertnode(map,atoi(node)); + node=strtok(NULL," \n"); + } + char *edge_line; + int node1,node2; + while((edge_line=fgets(buff,sizeof(buff),map_file))){ + node=strtok(edge_line,"\t\n"); + node1=atoi(node); + node=strtok(NULL,"\t\n"); + node2=atoi(node); + graph_insertedge(map,node1,node2); + if(!directed) + graph_insertedge(map,node2,node1); + } + fflush(map_file); + fclose(map_file); + return map; +} \ No newline at end of file diff --git a/Map/All_Pathes/map.h b/Map/All_Pathes/map.h new file mode 100644 index 0000000..3bcaa25 --- /dev/null +++ b/Map/All_Pathes/map.h @@ -0,0 +1,20 @@ +#ifndef MAP_H +#define MAP_H + +struct Node{ + int vertex; + struct Node *next; +}; + +struct Graph{ + struct Node **nodes; + int nodesize; + int sp; +}; + +struct Graph *graph_init(int); +void graph_insertedge(struct Graph *,int,int); +void display_debug(struct Graph *); +struct Graph *read_map(char const *); + +#endif \ No newline at end of file diff --git a/Map/All_Pathes/originmap.in b/Map/All_Pathes/originmap.in new file mode 100644 index 0000000..74cdbc9 --- /dev/null +++ b/Map/All_Pathes/originmap.in @@ -0,0 +1,15 @@ +1 8 +0 1 2 3 4 5 6 7 +0 1 +0 2 +0 3 +1 2 +1 6 +2 4 +3 2 +3 5 +4 6 +4 7 +5 4 +5 7 +6 7 diff --git a/Map/BFS&DFS/matrix/bfs.c b/Map/BFS&DFS/matrix/bfs.c new file mode 100644 index 0000000..f8b754e --- /dev/null +++ b/Map/BFS&DFS/matrix/bfs.c @@ -0,0 +1,59 @@ +#include +#include "map.h" +#define QSIZE 100 +#define N 100 + +int head,rear; +int queue[QSIZE]; +int empty(void){ + return head==rear; +} +int full(void){ + return head==(rear+1)%rear; +} +void enqueue(int new){ + queue[rear]=new; + rear=(rear+1)%QSIZE; +} +int dequeue(void){ + int retval=queue[head]; + head=(head+1)%QSIZE; + return retval; +} + +void breath_first_search_connected(int **map,int nodesize,int startnode,int *visited){ + enqueue(startnode); + int cur,i; + while(!empty()){ + cur=dequeue(); + if(!visited[cur]){ + visited[cur]=1; + printf("%d ",cur); + } + for(i=0;i +#include "map.h" +#define STACKSIZE 100 +#define N 100 + +void depth_first_search_conneted_recursive(int **map,int startnode,int nodesize,int *visited){ + if(visited[startnode]) + return; + visited[startnode]=1; + printf("%d ",startnode); + int i; + for(i=0;i +#include +#include + +#include "map.h" + +int **graph_init(int nodesize){ + int **map=malloc(sizeof(int *)*nodesize); + int i; + for(i=0;i +#include "map.h" +#define QSIZE 100 +#define N 100 + +int head,rear; +struct Node * queue[QSIZE]; +int empty(void){ + return head==rear; +} +int full(void){ + return head=(rear+1)%QSIZE; +} +void enqueue(struct Node *item){ + queue[rear]=item; + rear=(rear+1)%QSIZE; +} +struct Node *dequeue(void){ + struct Node *retval=queue[head]; + head=(head+1)%QSIZE; + return retval; +} + +void breath_first_search_conneted(struct Graph *map,int start,int *visited){ + struct Node *startnode=map->nodes[start]; + enqueue(startnode); + struct Node *cur; + while(!empty()){ + cur=dequeue(); + if(!visited[cur->vertex]){ + cur=map->nodes[cur->vertex]; + printf("%d ",cur->vertex); + visited[cur->vertex]=1; + cur=cur->next; + while(cur){ + if(!visited[cur->vertex]) + enqueue(map->nodes[cur->vertex]); + cur=cur->next; + } + } + } + printf("\n"); +} + +void breath_first_search(struct Graph *map){ + int visited[N]={}; + int i; + for(i=0;inodesize;i++) + if(!visited[i]) + breath_first_search_conneted(map,i,visited); +} + +int main(int argc, char const *argv[]) +{ + struct Graph *map=read_map(argv[1]); + display_debug(map); + breath_first_search(map); + return 0; +} \ No newline at end of file diff --git a/Map/BFS&DFS/neibortable/dfs.c b/Map/BFS&DFS/neibortable/dfs.c new file mode 100644 index 0000000..daacba1 --- /dev/null +++ b/Map/BFS&DFS/neibortable/dfs.c @@ -0,0 +1,105 @@ +#include +#include "map.h" +#define STACKSIZE 100 +#define N 100 + +void depth_first_search_conneted_recursive(struct Graph *map,int i,int *visited){ + if(visited[i]) + return; + struct Node *node=map->nodes[i]; + printf("%d ",node->vertex); + visited[i]=1; + struct Node *cur=node->next; + while(cur){ + depth_first_search_conneted_recursive(map,cur->vertex,visited); + cur=cur->next; + } +} + +struct Node * stack[STACKSIZE]; +int sp=-1; +int empty(void){ + return sp==-1; +} +void push(struct Node *item){ + stack[++sp]=item; +} +struct Node * pop(void){ + return stack[sp--]; +} +struct Node * top(void){ + return stack[sp]; +} + +struct Node *unvisited_neibor(struct Graph *map,int i,int *visited){ + struct Node *cur=map->nodes[i]->next,*retval=NULL; + while(cur){ + if(!visited[cur->vertex]){ + retval=cur; + break; + } + cur=cur->next; + } + return retval; +} + +void depth_first_search_conneted_nor_recursive(struct Graph *map,int startnode,int *visited){ + struct Node *cur=map->nodes[startnode]; + printf("%d ",cur->vertex); + visited[cur->vertex]=1; + push(cur); + while(!empty()){ + cur=top(); + if((cur=unvisited_neibor(map,cur->vertex,visited))){ + printf("%d ",cur->vertex); + visited[cur->vertex]=1; + push(cur); + }else{ + pop(); + } + } + printf("\n"); +} + +void depth_first_search_conneted_nor_recursive_cool(struct Graph *map,int startnode,int *visited){ + struct Node *cur=map->nodes[startnode]; + push(cur); + while(!empty()){ + cur=pop(); + if(visited[cur->vertex]) + continue; + visited[cur->vertex]=1; + printf("%d ",cur->vertex ); + cur=map->nodes[cur->vertex]->next; + while(cur){ + if(!visited[cur->vertex]) + push(cur); + cur=cur->next; + } + //now... + } + printf("\n"); +} + +void depth_first_search(struct Graph *map){ + int visited[N]={}; + int i; + for(i=0;inodesize;i++) + if(!visited[i]) + //depth_first_search_conneted_recursive(map,i,visited); + //depth_first_search_conneted_nor_recursive(map,i,visited); + depth_first_search_conneted_nor_recursive_cool(map,i,visited); + printf("\n"); +} + +int main(int argc, char const *argv[]) +{ + if(argc==1){ + printf("argv NULL!\n"); + return (1); + } + struct Graph *map=read_map(argv[1]); + display_debug(map); + depth_first_search(map); + return 0; +} \ No newline at end of file diff --git a/Map/BFS&DFS/neibortable/map.c b/Map/BFS&DFS/neibortable/map.c new file mode 100644 index 0000000..4089546 --- /dev/null +++ b/Map/BFS&DFS/neibortable/map.c @@ -0,0 +1,73 @@ +#include +#include +#include + +#include "map.h" + +struct Graph *graph_init(int nodesize){ + struct Graph *map=malloc(sizeof(struct Graph)); + if(!map) + return map; + map->sp=0; + map->nodesize=nodesize; + map->nodes=malloc(sizeof(struct Node *)*nodesize); + return map; +} + +void graph_insertnode(struct Graph *map,int nodename){ + struct Node *newnode=malloc(sizeof(struct Node)); + newnode->next=NULL; + newnode->vertex=nodename; + map->nodes[map->sp++]=newnode; +} + +void graph_insertedge(struct Graph *map,int nodename1,int nodename2){ + struct Node *node=map->nodes[nodename1]; + struct Node *new=malloc(sizeof(struct Node)); + new->vertex=nodename2; + new->next=node->next; + node->next=new; +} + +void display_debug(struct Graph *map){ + int i; + struct Node *cur; + for(i=0;inodesize;i++){ + cur=map->nodes[i]; + printf("%d:",cur->vertex); + cur=cur->next; + while(cur){ + printf(" %d",cur->vertex); + cur=cur->next; + } + printf("\n"); + } +} + +struct Graph *read_map(char const *filename){ + FILE *map_file=fopen(filename,"r"); + int directed,nodesize; + fscanf(map_file,"%d %d\n",&directed,&nodesize); + struct Graph *map=graph_init(nodesize); + char buff[1024]; + char *node_line=fgets(buff,sizeof(buff),map_file); + char *node=strtok(node_line," \n"); + while(node){ + graph_insertnode(map,atoi(node)); + node=strtok(NULL," \n"); + } + char *edge_line; + int node1,node2; + while((edge_line=fgets(buff,sizeof(buff),map_file))){ + node=strtok(edge_line,"\t\n"); + node1=atoi(node); + node=strtok(NULL,"\t\n"); + node2=atoi(node); + graph_insertedge(map,node1,node2); + if(!directed) + graph_insertedge(map,node2,node1); + } + fflush(map_file); + fclose(map_file); + return map; +} \ No newline at end of file diff --git a/Map/BFS&DFS/neibortable/map.h b/Map/BFS&DFS/neibortable/map.h new file mode 100644 index 0000000..3bcaa25 --- /dev/null +++ b/Map/BFS&DFS/neibortable/map.h @@ -0,0 +1,20 @@ +#ifndef MAP_H +#define MAP_H + +struct Node{ + int vertex; + struct Node *next; +}; + +struct Graph{ + struct Node **nodes; + int nodesize; + int sp; +}; + +struct Graph *graph_init(int); +void graph_insertedge(struct Graph *,int,int); +void display_debug(struct Graph *); +struct Graph *read_map(char const *); + +#endif \ No newline at end of file diff --git a/Map/BFS&DFS/neibortable/originmap.in b/Map/BFS&DFS/neibortable/originmap.in new file mode 100644 index 0000000..bf658f0 --- /dev/null +++ b/Map/BFS&DFS/neibortable/originmap.in @@ -0,0 +1,10 @@ +1 6 +0 1 2 3 4 5 +0 1 +0 3 +1 4 +2 4 +2 5 +3 1 +4 3 +5 5 diff --git "a/Map/BFS_App/bfs_\346\211\200\346\234\211\346\234\200\347\237\255\350\267\257\345\276\204/allshortestpath.c" "b/Map/BFS_App/bfs_\346\211\200\346\234\211\346\234\200\347\237\255\350\267\257\345\276\204/allshortestpath.c" new file mode 100644 index 0000000..a97718e --- /dev/null +++ "b/Map/BFS_App/bfs_\346\211\200\346\234\211\346\234\200\347\237\255\350\267\257\345\276\204/allshortestpath.c" @@ -0,0 +1,31 @@ +#include "map.h" +#define QSIZE 100 + +int head,rear; +int queue[QSIZE]; +int empty(void){ + return head==rear; +} +void enqueue(int item){ + queue[rear]=item; + rear=(rear+1)%QSIZE; +} +int dequeue(void){ + int retval=queue[head]; + head=(head+1)%QSIZE; + return retval; +} + +void allshortestpath(struct Graph *map,int start,int end){ + int +} + +int main(int argc, char const *argv[]) +{ + if(argc<2) + return 1; + struct Graph *map=read_map(argv[1]); + display_debug(map); + + return 0; +} \ No newline at end of file diff --git "a/Map/BFS_App/bfs_\346\211\200\346\234\211\346\234\200\347\237\255\350\267\257\345\276\204/map.c" "b/Map/BFS_App/bfs_\346\211\200\346\234\211\346\234\200\347\237\255\350\267\257\345\276\204/map.c" new file mode 100644 index 0000000..4089546 --- /dev/null +++ "b/Map/BFS_App/bfs_\346\211\200\346\234\211\346\234\200\347\237\255\350\267\257\345\276\204/map.c" @@ -0,0 +1,73 @@ +#include +#include +#include + +#include "map.h" + +struct Graph *graph_init(int nodesize){ + struct Graph *map=malloc(sizeof(struct Graph)); + if(!map) + return map; + map->sp=0; + map->nodesize=nodesize; + map->nodes=malloc(sizeof(struct Node *)*nodesize); + return map; +} + +void graph_insertnode(struct Graph *map,int nodename){ + struct Node *newnode=malloc(sizeof(struct Node)); + newnode->next=NULL; + newnode->vertex=nodename; + map->nodes[map->sp++]=newnode; +} + +void graph_insertedge(struct Graph *map,int nodename1,int nodename2){ + struct Node *node=map->nodes[nodename1]; + struct Node *new=malloc(sizeof(struct Node)); + new->vertex=nodename2; + new->next=node->next; + node->next=new; +} + +void display_debug(struct Graph *map){ + int i; + struct Node *cur; + for(i=0;inodesize;i++){ + cur=map->nodes[i]; + printf("%d:",cur->vertex); + cur=cur->next; + while(cur){ + printf(" %d",cur->vertex); + cur=cur->next; + } + printf("\n"); + } +} + +struct Graph *read_map(char const *filename){ + FILE *map_file=fopen(filename,"r"); + int directed,nodesize; + fscanf(map_file,"%d %d\n",&directed,&nodesize); + struct Graph *map=graph_init(nodesize); + char buff[1024]; + char *node_line=fgets(buff,sizeof(buff),map_file); + char *node=strtok(node_line," \n"); + while(node){ + graph_insertnode(map,atoi(node)); + node=strtok(NULL," \n"); + } + char *edge_line; + int node1,node2; + while((edge_line=fgets(buff,sizeof(buff),map_file))){ + node=strtok(edge_line,"\t\n"); + node1=atoi(node); + node=strtok(NULL,"\t\n"); + node2=atoi(node); + graph_insertedge(map,node1,node2); + if(!directed) + graph_insertedge(map,node2,node1); + } + fflush(map_file); + fclose(map_file); + return map; +} \ No newline at end of file diff --git "a/Map/BFS_App/bfs_\346\211\200\346\234\211\346\234\200\347\237\255\350\267\257\345\276\204/map.h" "b/Map/BFS_App/bfs_\346\211\200\346\234\211\346\234\200\347\237\255\350\267\257\345\276\204/map.h" new file mode 100644 index 0000000..3bcaa25 --- /dev/null +++ "b/Map/BFS_App/bfs_\346\211\200\346\234\211\346\234\200\347\237\255\350\267\257\345\276\204/map.h" @@ -0,0 +1,20 @@ +#ifndef MAP_H +#define MAP_H + +struct Node{ + int vertex; + struct Node *next; +}; + +struct Graph{ + struct Node **nodes; + int nodesize; + int sp; +}; + +struct Graph *graph_init(int); +void graph_insertedge(struct Graph *,int,int); +void display_debug(struct Graph *); +struct Graph *read_map(char const *); + +#endif \ No newline at end of file diff --git "a/Map/BFS_App/bfs_\346\211\200\346\234\211\346\234\200\347\237\255\350\267\257\345\276\204/originmap.in" "b/Map/BFS_App/bfs_\346\211\200\346\234\211\346\234\200\347\237\255\350\267\257\345\276\204/originmap.in" new file mode 100644 index 0000000..3d776e9 --- /dev/null +++ "b/Map/BFS_App/bfs_\346\211\200\346\234\211\346\234\200\347\237\255\350\267\257\345\276\204/originmap.in" @@ -0,0 +1,15 @@ +0 8 +0 1 2 3 4 5 6 7 +0 1 +0 2 +0 3 +1 2 +1 6 +2 3 +2 4 +3 5 +4 5 +4 6 +4 7 +5 7 +6 7 diff --git a/Map/DFS_App/Bi_connected/biconnect.py b/Map/DFS_App/Bi_connected/biconnect.py new file mode 100644 index 0000000..c2e34fd --- /dev/null +++ b/Map/DFS_App/Bi_connected/biconnect.py @@ -0,0 +1,95 @@ +#!/usr/bin/env python +#-*- encoding=utf-8 -*- +import sys + +def readmap(filename): + graph={} + with open(filename,'r') as mapfile: + line=mapfile.readline() + directed=int(line[:-1]) + line=mapfile.readline() + nodes=line[:-1].split(' ') + for node in nodes: + graph[int(node)]=[] + for line in mapfile: + node1,node2=line[:-1].split('\t') + graph[int(node1)].append(int(node2)) + if not directed: + graph[int(node2)].append(int(node1)) + return graph + +def dfs(map,prevnode,startnode,nexttable,prevtable,visited,record,tag): + if visited[startnode]: + #prevnode->startnode 1 + record[(prevnode,startnode)]=1 + if (startnode,prevnode) in record: + if startnode in prevtable: + prevtable[startnode].append(prevnode) + else: + prevtable[startnode]=[] + prevtable[startnode].append(prevnode) + return + #print startnode,'!' + tag[startnode]=len(tag)+1 + visited[startnode]=1 + if prevnode!=-1: + if prevnode in nexttable: + nexttable[prevnode].append(startnode) + else: + nexttable[prevnode]=[] + nexttable[prevnode].append(startnode) + for neibor in map[startnode]: + dfs(map,startnode,neibor,nexttable,prevtable,visited,record,tag) + +def suffix(node,prevtable,nexttable,value,tag): + if node in nexttable: + for inode in nexttable[node]: + suffix(inode,prevtable,nexttable,value,tag) + numv=tag[node] + numw,low=100,100 + if node in prevtable: + for prev in prevtable[node]: + if tag[prev]1: + cutnodes.append(node) + else: + for neibor in nexttable[node]: + if value[neibor][1]>=value[node][0]: + cutnodes.append(node) + return cutnodes + +if __name__ == '__main__': + map=readmap(sys.argv[1]) + nexttable,value=dfs_tree(map) + cutnodes=check(nexttable,value) + print cutnodes \ No newline at end of file diff --git a/Map/DFS_App/Bi_connected/biconneted.c b/Map/DFS_App/Bi_connected/biconneted.c new file mode 100644 index 0000000..00698c3 --- /dev/null +++ b/Map/DFS_App/Bi_connected/biconneted.c @@ -0,0 +1,14 @@ +#include +#include +#include "map.h" + +void create_dfstree(struct Graph *map,){} + +int main(int argc, char const *argv[]){ + if(argc<2) + return 1; + struct Graph *map=read_map(argv[1]); + //display_debug(map); + //draw(map); + return 0; +} \ No newline at end of file diff --git a/Map/DFS_App/Bi_connected/map.c b/Map/DFS_App/Bi_connected/map.c new file mode 100644 index 0000000..4089546 --- /dev/null +++ b/Map/DFS_App/Bi_connected/map.c @@ -0,0 +1,73 @@ +#include +#include +#include + +#include "map.h" + +struct Graph *graph_init(int nodesize){ + struct Graph *map=malloc(sizeof(struct Graph)); + if(!map) + return map; + map->sp=0; + map->nodesize=nodesize; + map->nodes=malloc(sizeof(struct Node *)*nodesize); + return map; +} + +void graph_insertnode(struct Graph *map,int nodename){ + struct Node *newnode=malloc(sizeof(struct Node)); + newnode->next=NULL; + newnode->vertex=nodename; + map->nodes[map->sp++]=newnode; +} + +void graph_insertedge(struct Graph *map,int nodename1,int nodename2){ + struct Node *node=map->nodes[nodename1]; + struct Node *new=malloc(sizeof(struct Node)); + new->vertex=nodename2; + new->next=node->next; + node->next=new; +} + +void display_debug(struct Graph *map){ + int i; + struct Node *cur; + for(i=0;inodesize;i++){ + cur=map->nodes[i]; + printf("%d:",cur->vertex); + cur=cur->next; + while(cur){ + printf(" %d",cur->vertex); + cur=cur->next; + } + printf("\n"); + } +} + +struct Graph *read_map(char const *filename){ + FILE *map_file=fopen(filename,"r"); + int directed,nodesize; + fscanf(map_file,"%d %d\n",&directed,&nodesize); + struct Graph *map=graph_init(nodesize); + char buff[1024]; + char *node_line=fgets(buff,sizeof(buff),map_file); + char *node=strtok(node_line," \n"); + while(node){ + graph_insertnode(map,atoi(node)); + node=strtok(NULL," \n"); + } + char *edge_line; + int node1,node2; + while((edge_line=fgets(buff,sizeof(buff),map_file))){ + node=strtok(edge_line,"\t\n"); + node1=atoi(node); + node=strtok(NULL,"\t\n"); + node2=atoi(node); + graph_insertedge(map,node1,node2); + if(!directed) + graph_insertedge(map,node2,node1); + } + fflush(map_file); + fclose(map_file); + return map; +} \ No newline at end of file diff --git a/Map/DFS_App/Bi_connected/map.h b/Map/DFS_App/Bi_connected/map.h new file mode 100644 index 0000000..3bcaa25 --- /dev/null +++ b/Map/DFS_App/Bi_connected/map.h @@ -0,0 +1,20 @@ +#ifndef MAP_H +#define MAP_H + +struct Node{ + int vertex; + struct Node *next; +}; + +struct Graph{ + struct Node **nodes; + int nodesize; + int sp; +}; + +struct Graph *graph_init(int); +void graph_insertedge(struct Graph *,int,int); +void display_debug(struct Graph *); +struct Graph *read_map(char const *); + +#endif \ No newline at end of file diff --git a/Map/DFS_App/Bi_connected/originmap.in b/Map/DFS_App/Bi_connected/originmap.in new file mode 100644 index 0000000..d908e79 --- /dev/null +++ b/Map/DFS_App/Bi_connected/originmap.in @@ -0,0 +1,10 @@ +0 +0 1 2 3 4 5 6 +0 1 +0 3 +1 2 +2 3 +2 6 +3 4 +3 5 +4 5 diff --git a/Map/DFS_App/EulerMap/euler.c b/Map/DFS_App/EulerMap/euler.c new file mode 100644 index 0000000..f85de08 --- /dev/null +++ b/Map/DFS_App/EulerMap/euler.c @@ -0,0 +1,65 @@ +#include +#include +#include "map.h" + +#define START 4 + +int path[1000]; +int sp; + +void graph_deleteedge(struct Graph *map,int node,int neibor){ + struct Node *cur,*prev; + prev=map->nodes[node]; + cur=prev->next; + while(cur && cur->vertex!=neibor){ + prev=cur; + cur=cur->next; + } + prev->next=cur->next; + free(cur); + prev=map->nodes[neibor]; + cur=prev->next; + while(cur && cur->vertex!=node){ + prev=cur; + cur=cur->next; + } + prev->next=cur->next; + free(cur); +} + +void DFS(struct Graph *map,int prev,int startnode){ + printf("%d\n",startnode); + path[sp++]=startnode; + if(prev!=-1) + graph_deleteedge(map,startnode,prev); + struct Node *cur=map->nodes[startnode]->next; + if(cur){ + DFS(map,startnode,cur->vertex); + } +} + +int find_new(struct Graph *map){ + for(int i=0;inodes[path[i]]->next) + return i; + return -1; +} + +void draw(struct Graph *map){ + DFS(map,-1,START); + int new; + while((new=find_new(map))!=-1){ + printf("========\n"); + DFS(map,-1,path[new]); + } + printf("End\n"); +} + +int main(int argc, char const *argv[]){ + if(argc<2) + return 1; + struct Graph *map=read_map(argv[1]); + //display_debug(map); + draw(map); + return 0; +} \ No newline at end of file diff --git a/Map/DFS_App/EulerMap/map.c b/Map/DFS_App/EulerMap/map.c new file mode 100644 index 0000000..4089546 --- /dev/null +++ b/Map/DFS_App/EulerMap/map.c @@ -0,0 +1,73 @@ +#include +#include +#include + +#include "map.h" + +struct Graph *graph_init(int nodesize){ + struct Graph *map=malloc(sizeof(struct Graph)); + if(!map) + return map; + map->sp=0; + map->nodesize=nodesize; + map->nodes=malloc(sizeof(struct Node *)*nodesize); + return map; +} + +void graph_insertnode(struct Graph *map,int nodename){ + struct Node *newnode=malloc(sizeof(struct Node)); + newnode->next=NULL; + newnode->vertex=nodename; + map->nodes[map->sp++]=newnode; +} + +void graph_insertedge(struct Graph *map,int nodename1,int nodename2){ + struct Node *node=map->nodes[nodename1]; + struct Node *new=malloc(sizeof(struct Node)); + new->vertex=nodename2; + new->next=node->next; + node->next=new; +} + +void display_debug(struct Graph *map){ + int i; + struct Node *cur; + for(i=0;inodesize;i++){ + cur=map->nodes[i]; + printf("%d:",cur->vertex); + cur=cur->next; + while(cur){ + printf(" %d",cur->vertex); + cur=cur->next; + } + printf("\n"); + } +} + +struct Graph *read_map(char const *filename){ + FILE *map_file=fopen(filename,"r"); + int directed,nodesize; + fscanf(map_file,"%d %d\n",&directed,&nodesize); + struct Graph *map=graph_init(nodesize); + char buff[1024]; + char *node_line=fgets(buff,sizeof(buff),map_file); + char *node=strtok(node_line," \n"); + while(node){ + graph_insertnode(map,atoi(node)); + node=strtok(NULL," \n"); + } + char *edge_line; + int node1,node2; + while((edge_line=fgets(buff,sizeof(buff),map_file))){ + node=strtok(edge_line,"\t\n"); + node1=atoi(node); + node=strtok(NULL,"\t\n"); + node2=atoi(node); + graph_insertedge(map,node1,node2); + if(!directed) + graph_insertedge(map,node2,node1); + } + fflush(map_file); + fclose(map_file); + return map; +} \ No newline at end of file diff --git a/Map/DFS_App/EulerMap/map.h b/Map/DFS_App/EulerMap/map.h new file mode 100644 index 0000000..3bcaa25 --- /dev/null +++ b/Map/DFS_App/EulerMap/map.h @@ -0,0 +1,20 @@ +#ifndef MAP_H +#define MAP_H + +struct Node{ + int vertex; + struct Node *next; +}; + +struct Graph{ + struct Node **nodes; + int nodesize; + int sp; +}; + +struct Graph *graph_init(int); +void graph_insertedge(struct Graph *,int,int); +void display_debug(struct Graph *); +struct Graph *read_map(char const *); + +#endif \ No newline at end of file diff --git a/Map/DFS_App/EulerMap/originmap.in b/Map/DFS_App/EulerMap/originmap.in new file mode 100644 index 0000000..32cd2ee --- /dev/null +++ b/Map/DFS_App/EulerMap/originmap.in @@ -0,0 +1,23 @@ +0 12 +0 1 2 3 4 5 6 7 8 9 10 11 +0 2 +0 3 +1 2 +1 7 +2 3 +2 5 +2 6 +2 8 +3 4 +3 6 +3 9 +3 10 +4 9 +5 8 +6 8 +6 9 +7 8 +8 9 +8 11 +9 10 +9 11 diff --git a/Map/DFS_App/Strongly_connected_comp/map.c b/Map/DFS_App/Strongly_connected_comp/map.c new file mode 100644 index 0000000..4089546 --- /dev/null +++ b/Map/DFS_App/Strongly_connected_comp/map.c @@ -0,0 +1,73 @@ +#include +#include +#include + +#include "map.h" + +struct Graph *graph_init(int nodesize){ + struct Graph *map=malloc(sizeof(struct Graph)); + if(!map) + return map; + map->sp=0; + map->nodesize=nodesize; + map->nodes=malloc(sizeof(struct Node *)*nodesize); + return map; +} + +void graph_insertnode(struct Graph *map,int nodename){ + struct Node *newnode=malloc(sizeof(struct Node)); + newnode->next=NULL; + newnode->vertex=nodename; + map->nodes[map->sp++]=newnode; +} + +void graph_insertedge(struct Graph *map,int nodename1,int nodename2){ + struct Node *node=map->nodes[nodename1]; + struct Node *new=malloc(sizeof(struct Node)); + new->vertex=nodename2; + new->next=node->next; + node->next=new; +} + +void display_debug(struct Graph *map){ + int i; + struct Node *cur; + for(i=0;inodesize;i++){ + cur=map->nodes[i]; + printf("%d:",cur->vertex); + cur=cur->next; + while(cur){ + printf(" %d",cur->vertex); + cur=cur->next; + } + printf("\n"); + } +} + +struct Graph *read_map(char const *filename){ + FILE *map_file=fopen(filename,"r"); + int directed,nodesize; + fscanf(map_file,"%d %d\n",&directed,&nodesize); + struct Graph *map=graph_init(nodesize); + char buff[1024]; + char *node_line=fgets(buff,sizeof(buff),map_file); + char *node=strtok(node_line," \n"); + while(node){ + graph_insertnode(map,atoi(node)); + node=strtok(NULL," \n"); + } + char *edge_line; + int node1,node2; + while((edge_line=fgets(buff,sizeof(buff),map_file))){ + node=strtok(edge_line,"\t\n"); + node1=atoi(node); + node=strtok(NULL,"\t\n"); + node2=atoi(node); + graph_insertedge(map,node1,node2); + if(!directed) + graph_insertedge(map,node2,node1); + } + fflush(map_file); + fclose(map_file); + return map; +} \ No newline at end of file diff --git a/Map/DFS_App/Strongly_connected_comp/map.h b/Map/DFS_App/Strongly_connected_comp/map.h new file mode 100644 index 0000000..5731cae --- /dev/null +++ b/Map/DFS_App/Strongly_connected_comp/map.h @@ -0,0 +1,21 @@ +#ifndef MAP_H +#define MAP_H + +struct Node{ + int vertex; + struct Node *next; +}; + +struct Graph{ + struct Node **nodes; + int nodesize; + int sp; +}; + +struct Graph *graph_init(int); +void graph_insertnode(struct Graph *,int); +void graph_insertedge(struct Graph *,int,int); +void display_debug(struct Graph *); +struct Graph *read_map(char const *); + +#endif \ No newline at end of file diff --git a/Map/DFS_App/Strongly_connected_comp/originmap.in b/Map/DFS_App/Strongly_connected_comp/originmap.in new file mode 100644 index 0000000..de8fabd --- /dev/null +++ b/Map/DFS_App/Strongly_connected_comp/originmap.in @@ -0,0 +1,16 @@ +1 8 +0 1 2 3 4 5 6 7 +0 1 +1 2 +1 4 +1 5 +2 3 +2 6 +3 2 +3 7 +4 0 +4 5 +5 6 +6 5 +6 7 +7 7 diff --git a/Map/DFS_App/Strongly_connected_comp/strongconnect.c b/Map/DFS_App/Strongly_connected_comp/strongconnect.c new file mode 100644 index 0000000..5ef1a81 --- /dev/null +++ b/Map/DFS_App/Strongly_connected_comp/strongconnect.c @@ -0,0 +1,80 @@ +#include +#include +#include "map.h" + +#define STACKSIZE 100 + +int sp=-1; +int stack[STACKSIZE]; +int empty(void){ + return sp==-1; +} +void push(int new){ + stack[++sp]=new; +} +int pop(void){ + return stack[--sp]; +} + +void DFS_connect(struct Graph *map,int startnode,int *visited){ + if(visited[startnode]) + return; + struct Node *cur=map->nodes[startnode]->next; + printf("%d ",startnode ); + visited[startnode]=1; + while(cur){ + DFS_connect(map,cur->vertex,visited); + cur=cur->next; + } + push(startnode); +} + +void DFS(struct Graph *map){ + int visited[map->nodesize]; + for(int i=0;inodesize;i++) + visited[i]=0; + for(int i=0;inodesize;i++) + if(!visited[i]) + DFS_connect(map,i,visited); +} + +struct Graph *reversegraph(struct Graph *map){ + struct Graph *rmap=graph_init(map->nodesize); + struct Node *cur; + for(int i=0;inodesize;i++) + graph_insertnode(rmap,i); + for(int i=0;inodesize;i++){ + cur=map->nodes[i]->next; + while(cur){//i --> cur->vertex + graph_insertedge(rmap,cur->vertex,i); + cur=cur->next; + } + } + return rmap; +} + +void strongly_connect_comp(struct Graph *map){ + struct Graph *rmap=reversegraph(map); + DFS(map); + int curno; + int rvisited[rmap->nodesize]; + for(int i=0;inodesize;i++) + rvisited[i]=0; + while(!empty()){ + curno=pop(); + if(!rvisited[curno]){ + printf("======\n"); + DFS_connect(rmap,curno,rvisited); + printf("\n"); + } + + } +} + +int main(int argc, char const *argv[]){ + if(argc<2) + return 1; + struct Graph *map=read_map(argv[1]); + strongly_connect_comp(map); + return 0; +} \ No newline at end of file diff --git a/Map/DFS_App/ifRing/map.c b/Map/DFS_App/ifRing/map.c new file mode 100644 index 0000000..7e5efb7 --- /dev/null +++ b/Map/DFS_App/ifRing/map.c @@ -0,0 +1,73 @@ +#include +#include +#include + +#include "map.h" + +struct Graph *graph_init(int nodesize){ + struct Graph *map=malloc(sizeof(struct Graph)); + if(!map) + return map; + map->sp=0; + map->nodesize=nodesize; + map->nodes=malloc(sizeof(struct Node *)*nodesize); + return map; +} + +void graph_insertnode(struct Graph *map,int nodename){ + struct Node *newnode=malloc(sizeof(struct Node)); + newnode->next=NULL; + newnode->vertex=nodename; + map->nodes[map->sp++]=newnode; +} + +void graph_insertedge(struct Graph *map,int nodename1,int nodename2){ + struct Node *node=map->nodes[nodename1]; + struct Node *new=malloc(sizeof(struct Node)); + new->vertex=nodename2; + new->next=node->next; + node->next=new; +} + +void display_debug(struct Graph *map){ + int i; + struct Node *cur; + for(i=0;inodesize;i++){ + cur=map->nodes[i]; + printf("%d:",cur->vertex); + cur=cur->next; + while(cur){ + printf(" %d",cur->vertex); + cur=cur->next; + } + printf("\n"); + } +} + +struct Graph *read_map(char const *filename,int *directed){ + FILE *map_file=fopen(filename,"r"); + int nodesize; + fscanf(map_file,"%d %d\n",directed,&nodesize); + struct Graph *map=graph_init(nodesize); + char buff[1024]; + char *node_line=fgets(buff,sizeof(buff),map_file); + char *node=strtok(node_line," \n"); + while(node){ + graph_insertnode(map,atoi(node)); + node=strtok(NULL," \n"); + } + char *edge_line; + int node1,node2; + while((edge_line=fgets(buff,sizeof(buff),map_file))){ + node=strtok(edge_line,"\t\n"); + node1=atoi(node); + node=strtok(NULL,"\t\n"); + node2=atoi(node); + graph_insertedge(map,node1,node2); + if(!*directed) + graph_insertedge(map,node2,node1); + } + fflush(map_file); + fclose(map_file); + return map; +} \ No newline at end of file diff --git a/Map/DFS_App/ifRing/map.h b/Map/DFS_App/ifRing/map.h new file mode 100644 index 0000000..b0028b7 --- /dev/null +++ b/Map/DFS_App/ifRing/map.h @@ -0,0 +1,20 @@ +#ifndef MAP_H +#define MAP_H + +struct Node{ + int vertex; + struct Node *next; +}; + +struct Graph{ + struct Node **nodes; + int nodesize; + int sp; +}; + +struct Graph *graph_init(int); +void graph_insertedge(struct Graph *,int,int); +void display_debug(struct Graph *); +struct Graph *read_map(char const *,int *); + +#endif \ No newline at end of file diff --git a/Map/DFS_App/ifRing/originmap.in b/Map/DFS_App/ifRing/originmap.in new file mode 100644 index 0000000..97d7d3a --- /dev/null +++ b/Map/DFS_App/ifRing/originmap.in @@ -0,0 +1,5 @@ +1 3 +0 1 2 +0 1 +1 2 +2 0 diff --git a/Map/DFS_App/ifRing/ring.c b/Map/DFS_App/ifRing/ring.c new file mode 100644 index 0000000..f77eeef --- /dev/null +++ b/Map/DFS_App/ifRing/ring.c @@ -0,0 +1,83 @@ +#include +#include +#include "map.h" + +int findneinor_unvisited(struct Graph *map,int node,int *visited){ + struct Node *cur=map->nodes[node]->next; + int retval=-1; + while(cur){ + if(!visited[cur->vertex]){ + retval=cur->vertex; + break; + } + cur=cur->next; + } + return retval; +} + +void DFS_dg(struct Graph *map,int startnode,int *visited,int *flag){ + if(visited[startnode]==-1){ + *flag=1; + return; + } + if(*flag==1 || visited[startnode]) + return; + struct Node *cur=map->nodes[startnode]->next; + visited[startnode]=-1; + while(cur){ + DFS_dg(map,cur->vertex,visited,flag); + cur=cur->next; + } + visited[startnode]=1; +} + +int ifRing_dg(struct Graph *map){ + int visited[map->nodesize]; + for(int i=0;inodesize;i++) + visited[i]=0; + int flag=0; + for(int i=0;inodesize && !flag;i++) + DFS_dg(map,i,visited,&flag); + return flag; +} + +void DFS_udg(struct Graph *map,int startnode,int prev,int *visited,int *flag){ + if(visited[startnode]==-1){ + *flag=1; + return; + } + if(*flag==1 || visited[startnode]) + return; + struct Node *cur=map->nodes[startnode]->next; + visited[startnode]=-1; + while(cur){ + if(cur->vertex!=prev) + DFS_udg(map,cur->vertex,startnode,visited,flag); + cur=cur->next; + } + visited[startnode]=1; +} + +int ifRing_udg(struct Graph *map){ + int visited[map->nodesize]; + for(int i=0;inodesize;i++) + visited[i]=0; + int flag=0; + for(int i=0;inodesize && !flag;i++) + DFS_udg(map,i,-1,visited,&flag); + return flag; +} + +int main(int argc, char const *argv[]){ + if(argc<2) + return 1; + int directed; + struct Graph *map=read_map(argv[1],&directed); + int status; + status=directed?ifRing_dg(map):ifRing_udg(map); + if(status) + printf("有环\n"); + else + printf("无环\n"); + return 0; +} \ No newline at end of file diff --git "a/Map/KeyPath/AOE\347\275\221\344\270\216\345\205\263\351\224\256\350\267\257\345\276\204 - Learn as if you were to live forever - \345\215\232\345\256\242\351\242\221\351\201\223 - CSDN.NET.pdf" "b/Map/KeyPath/AOE\347\275\221\344\270\216\345\205\263\351\224\256\350\267\257\345\276\204 - Learn as if you were to live forever - \345\215\232\345\256\242\351\242\221\351\201\223 - CSDN.NET.pdf" new file mode 100644 index 0000000..a82627b Binary files /dev/null and "b/Map/KeyPath/AOE\347\275\221\344\270\216\345\205\263\351\224\256\350\267\257\345\276\204 - Learn as if you were to live forever - \345\215\232\345\256\242\351\242\221\351\201\223 - CSDN.NET.pdf" differ diff --git a/Map/KeyPath/keypath.c b/Map/KeyPath/keypath.c new file mode 100644 index 0000000..ce61ddf --- /dev/null +++ b/Map/KeyPath/keypath.c @@ -0,0 +1,134 @@ +#include +#include +#include + +#include "map.h" + +#define STACKSIZE 100 +#define PRINT(x,size) printf("" #x ":");\ +for(int i=0;inodes[i]->next; + while(cur && !retval){ + if(!visited[cur->vertex]) + retval=cur; + cur=cur->next; + } + return retval; +} + +void DFS_connect(struct Graph *map,int startnode,int *visited){ + push(startnode); + visited[startnode]=1; + //printf("%d ",startnode ); + int curno; + struct Node *cur; + while(!empty()){ + curno=top(); + if((cur=ifneiborOK(map,curno,visited))){ + push(cur->vertex); + //printf("%d ",cur->vertex); + visited[cur->vertex]=1; + }else{ + pop(); + topo_r[++tsp]=curno; + } + } +} + +void DFS(struct Graph *map){ + int visited[map->nodesize]; + for(int i=0;inodesize;i++) + visited[i]=0; + for(int i=0;inodesize;i++) + if(!visited[i]) + DFS_connect(map,i,visited); + //printf("\n"); +} + +int *count_ve(struct Graph *map,int *topo_r){ + int *ve=malloc(sizeof(int)*map->nodesize); + for(int i=0;inodesize;i++) + ve[i]=0; + int nodeid; + struct Node *cur=NULL; + for(int i=tsp;i>=0;i--){ + nodeid=topo_r[i]; + cur=map->nodes[nodeid]->next; + while(cur){ + //nodeid===>cur->vertex + if(ve[nodeid]+cur->weight>ve[cur->vertex]) + ve[cur->vertex]=ve[nodeid]+cur->weight; + cur=cur->next; + } + } + return ve; +} + +int *count_vl(struct Graph *map,int *topo_r,int *ve){ + int *vl=malloc(sizeof(int)*map->nodesize); + int nodeid; + struct Node *cur=NULL; + for(int i=0;i<=tsp;i++) + vl[i]=INT_MAX; + vl[topo_r[0]]=ve[topo_r[0]]; + for(int i=0;i<=tsp;i++){ + nodeid=topo_r[i]; + cur=map->nodes[nodeid]->next; + while(cur){ + // nodeid ===> cur->vertex + if(vl[nodeid]>vl[cur->vertex]-cur->weight) + vl[nodeid]=vl[cur->vertex]-cur->weight; + cur=cur->next; + } + } + return vl; +} + +void count_keypath(struct Graph *map,int *ve,int *vl){ + struct Node *cur; + for(int i=0;inodesize;i++){ + cur=map->nodes[i]->next; + while(cur){ + //i===>cur->vertex + if(ve[i]==vl[cur->vertex]-cur->weight) + printf("%d==>%d\n",i,cur->vertex); + cur=cur->next; + } + } +} + +int main(int argc, char const *argv[]) +{ + if(argc<2) + return 1; + struct Graph *map=read_map(argv[1]); + display_debug(map); + DFS(map); + int *ve=count_ve(map,topo_r); + int *vl=count_vl(map,topo_r,ve); + //PRINT(ve,map->nodesize); + //PRINT(vl,map->nodesize); + count_keypath(map,ve,vl); + return 0; +} \ No newline at end of file diff --git a/Map/KeyPath/map.c b/Map/KeyPath/map.c new file mode 100644 index 0000000..447b98d --- /dev/null +++ b/Map/KeyPath/map.c @@ -0,0 +1,76 @@ +#include +#include +#include +#include "map.h" + +struct Graph *graph_init(int nodesize){ + struct Graph *map=malloc(sizeof(struct Graph)); + if(!map) + return map; + map->sp=0; + map->nodesize=nodesize; + map->nodes=malloc(sizeof(struct Node *)*nodesize); + return map; +} + +void graph_insertnode(struct Graph *map,int nodename){ + struct Node *newnode=malloc(sizeof(struct Node)); + newnode->next=NULL; + newnode->vertex=nodename; + map->nodes[map->sp++]=newnode; +} + +void graph_insertedge(struct Graph *map,int nodename1,int nodename2,int weight){ + struct Node *node=map->nodes[nodename1]; + struct Node *new=malloc(sizeof(struct Node)); + new->vertex=nodename2; + new->weight=weight; + new->next=node->next; + node->next=new; +} + +void display_debug(struct Graph *map){ + int i; + struct Node *cur; + for(i=0;inodesize;i++){ + cur=map->nodes[i]; + printf("%d:",cur->vertex); + cur=cur->next; + while(cur){ + printf(" %d(%d)",cur->vertex,cur->weight); + cur=cur->next; + } + printf("\n"); + } +} + +struct Graph *read_map(char const *filename){ + FILE *map_file=fopen(filename,"r"); + int directed,nodesize; + fscanf(map_file,"%d %d\n",&directed,&nodesize); + struct Graph *map=graph_init(nodesize); + + char buff[1024]; + char *node_line=fgets(buff,sizeof(buff),map_file); + char *node=strtok(node_line," \n"); + while(node){ + graph_insertnode(map,atoi(node)); + node=strtok(NULL," \n"); + } + char *edge_line; + int node1,node2,weight; + while((edge_line=fgets(buff,sizeof(buff),map_file))){ + node=strtok(edge_line,"\t\n"); + node1=atoi(node); + node=strtok(NULL,"\t\n"); + node2=atoi(node); + node=strtok(NULL,"\t\n"); + weight=atoi(node); + graph_insertedge(map,node1,node2,weight); + if(!directed) + graph_insertedge(map,node2,node1,weight); + } + fflush(map_file); + fclose(map_file); + return map; +} \ No newline at end of file diff --git a/Map/KeyPath/map.h b/Map/KeyPath/map.h new file mode 100644 index 0000000..21e0737 --- /dev/null +++ b/Map/KeyPath/map.h @@ -0,0 +1,22 @@ +#ifndef MAP_H +#define MAP_H + +struct Node{ + int vertex; + struct Node *next; + int weight; +}; + +struct Graph{ + struct Node **nodes; + int nodesize; + int sp; +}; + +struct Graph *graph_init(int); +void graph_insertnode(struct Graph *,int); +void graph_insertedge(struct Graph *,int,int,int); +void display_debug(struct Graph *); +struct Graph *read_map(char const *); + +#endif \ No newline at end of file diff --git a/Map/KeyPath/map.jpg b/Map/KeyPath/map.jpg new file mode 100644 index 0000000..b828cdb Binary files /dev/null and b/Map/KeyPath/map.jpg differ diff --git a/Map/KeyPath/originmap.in b/Map/KeyPath/originmap.in new file mode 100644 index 0000000..99c4c6a --- /dev/null +++ b/Map/KeyPath/originmap.in @@ -0,0 +1,12 @@ +1 7 +0 1 2 3 4 5 6 +0 1 3 +0 2 2 +0 3 6 +1 3 2 +1 4 4 +2 3 1 +2 5 3 +3 4 1 +4 6 3 +5 6 4 diff --git "a/Map/KeyPath/\346\225\231\344\275\240\350\275\273\346\235\276\350\256\241\347\256\227AOE\347\275\221\345\205\263\351\224\256\350\267\257\345\276\204.pdf" "b/Map/KeyPath/\346\225\231\344\275\240\350\275\273\346\235\276\350\256\241\347\256\227AOE\347\275\221\345\205\263\351\224\256\350\267\257\345\276\204.pdf" new file mode 100644 index 0000000..cc93fb4 Binary files /dev/null and "b/Map/KeyPath/\346\225\231\344\275\240\350\275\273\346\235\276\350\256\241\347\256\227AOE\347\275\221\345\205\263\351\224\256\350\267\257\345\276\204.pdf" differ diff --git "a/Map/KeyPath/\346\234\252\345\221\275\345\220\215.tiff" "b/Map/KeyPath/\346\234\252\345\221\275\345\220\215.tiff" new file mode 100644 index 0000000..e50f0e4 Binary files /dev/null and "b/Map/KeyPath/\346\234\252\345\221\275\345\220\215.tiff" differ diff --git a/Map/Maximum two matches/erbutu.in b/Map/Maximum two matches/erbutu.in new file mode 100644 index 0000000..4799e50 --- /dev/null +++ b/Map/Maximum two matches/erbutu.in @@ -0,0 +1,11 @@ +5 4 +m1 m2 m3 m4 m5 +t1 t2 t3 t4 +m1 t1 +m2 t1 +m2 t3 +m3 t2 +m3 t3 +m3 t4 +m4 t3 +m5 t3 diff --git a/Map/Maximum two matches/twomatch.cpp b/Map/Maximum two matches/twomatch.cpp new file mode 100644 index 0000000..5410b85 --- /dev/null +++ b/Map/Maximum two matches/twomatch.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include + +using namespace std; + +int **init_map(int nodesize){ + int **graph=(int **)malloc(sizeof(int *)*nodesize); + for(int i=0;i"< string2int; + map machines; + map tasks; + char buff[1024]; + char *machine_line=fgets(buff,sizeof(buff),fp); + char *machine_name=strtok(machine_line,"\t\n"); + int i=1; + while(machine_name){ + string2int[machine_name]=i; + graph[0][i]=1; + machines[i++]=machine_name; + machine_name=strtok(NULL,"\t\n"); + } + char *task_line=fgets(buff,sizeof(buff),fp); + char *task_name=strtok(task_line,"\t\n"); + while(task_name){ + string2int[task_name]=i; + graph[i][machinesize+tasksize+1]=1; + tasks[i++]=task_name; + task_name=strtok(NULL,"\t\n"); + } + string node1,node2; + while(fgets(buff,sizeof(buff),fp)!=NULL){ + node1=strtok(buff,"\t\n"); + node2=strtok(NULL,"\t\n"); + graph[string2int[node1]][string2int[node2]]=1; + } + fflush(fp); + fclose(fp); + + int **copy_graph=copy_matrix(graph,machinesize+tasksize+2); + + int flag; + while((flag=find_path(graph,0,machinesize+tasksize+1,machinesize+tasksize+2))) + ; + show_match(copy_graph,graph,machinesize+tasksize+2,machines,tasks); + + return 0; +} \ No newline at end of file diff --git "a/Map/Maximum two matches/\345\214\210\347\211\231\345\210\251\347\256\227\346\263\225.c" "b/Map/Maximum two matches/\345\214\210\347\211\231\345\210\251\347\256\227\346\263\225.c" new file mode 100755 index 0000000..42d2d1d --- /dev/null +++ "b/Map/Maximum two matches/\345\214\210\347\211\231\345\210\251\347\256\227\346\263\225.c" @@ -0,0 +1,51 @@ +#include +#include +#define BOYSIZE 4 +#define BOY 0 +#define GIRLSIZE 4 +#define GIRL 4 +#define TRUE 1 +#define FALSE 0 + +int match[BOYSIZE][GIRLSIZE]={ + {1,1,0,0},{0,1,1,0},{1,1,0,0},{0,0,1,0} +}; + +int findout(int *mygirl,int *myboy,int boy,int *used){ + int j;// + for(j=0;j%d\n",i,mygirl[i]); +} + +int xyl(int *mygirl,int *myboy){ + int i,counter=0; + int used[GIRLSIZE]={}; + for(i=0;i +#include +#include + +#include "map.h" + +int **graph_init(int nodesize){ + int **map=malloc(sizeof(int *)*nodesize); + int i; + for(i=0;i +#include +#include + +#include "map.h" +#define QSIZE 100 + +int **init_flow(int nodesize){ + int **flow=malloc(sizeof(int *)*nodesize); + int i,j; + for(i=0;i0) + complegraph[i][j]=capacity[i][j]-flow[i][j]; + else + complegraph[i][j]=0; + } + } + return complegraph; +} + +void destory_matrix(int **matrix,int size){ + int i; + for(i=0;i%d\n",parent,cur); + flow[parent][cur]+=minflow; + flow[cur][parent]-=minflow; + cur=parent; + } + return 1; + }else{ + return 0; + } +} + +int main(int argc, char const *argv[]){ + if(argc<2) + return 1; + int nodesize; + int **capacity=read_map(argv[1],&nodesize); + int **flow=init_flow(nodesize); + display_debug(capacity,nodesize); + + int status=0; + int **complegraph; + + do{ + complegraph=create_complegraph(capacity,nodesize,flow); + status=find_path(complegraph,nodesize,flow); + destory_matrix(complegraph,nodesize); + }while(status); + + printf("maximum flow:\n"); + display_debug(flow,nodesize); + + return 0; +} \ No newline at end of file diff --git a/Map/MaximumFlow/originmap.in b/Map/MaximumFlow/originmap.in new file mode 100644 index 0000000..521966c --- /dev/null +++ b/Map/MaximumFlow/originmap.in @@ -0,0 +1,12 @@ +1 6 +0 1 2 3 4 5 +0 1 16 +0 2 13 +1 2 10 +1 3 12 +2 1 4 +2 4 14 +3 2 9 +3 5 20 +4 3 7 +4 5 4 diff --git a/Map/MinestTree/Kruskal/disjointset.c b/Map/MinestTree/Kruskal/disjointset.c new file mode 100644 index 0000000..af08cba --- /dev/null +++ b/Map/MinestTree/Kruskal/disjointset.c @@ -0,0 +1,32 @@ +#include +#include "disjointset.h" + +int rank[MAXSIZE]; + +struct Set **init_set(int size){ + int i; + struct Set **set=malloc(sizeof(struct Set *)*size); + for(i=0;ivertex=i; + set[i]->parent=set[i]; + } + return set; +} + +struct Set *find_set(struct Set *item){ + if(item->parent==item) + return item; + item->parent=find_set(item->parent); + return item->parent; +} + +void union_set(struct Set *one,struct Set *other){ + if(rank[one->vertex]vertex]) + one->parent=other; + else{ + if(rank[one->vertex]==rank[other->vertex]) + rank[one->vertex]++; + other->parent=one; + } +} \ No newline at end of file diff --git a/Map/MinestTree/Kruskal/disjointset.h b/Map/MinestTree/Kruskal/disjointset.h new file mode 100644 index 0000000..4914b44 --- /dev/null +++ b/Map/MinestTree/Kruskal/disjointset.h @@ -0,0 +1,15 @@ +#ifndef DISJOINTSET_H +#define DISJOINTSET_H +#define MAXSIZE 100 +struct Set{ + int vertex; + struct Set *parent; +}; + +extern int rank[]; + +struct Set **init_set(int size); +struct Set *find_set(struct Set *); +void union_set(struct Set *,struct Set *); + +#endif \ No newline at end of file diff --git a/Map/MinestTree/Kruskal/heap.c b/Map/MinestTree/Kruskal/heap.c new file mode 100644 index 0000000..fb0dfdd --- /dev/null +++ b/Map/MinestTree/Kruskal/heap.c @@ -0,0 +1,56 @@ +#include +#include +#include "heap.h" + +struct Heap *init_heap(int size){ + struct Heap *heap=malloc(sizeof(struct Heap)); + heap->size=0; + heap->data=malloc(sizeof(struct Edge *)*size); + return heap; +} + +void heap_swap(struct Heap *heap,int pos1,int pos2){ + struct Edge *tmp=heap->data[pos1]; + heap->data[pos1]=heap->data[pos2]; + heap->data[pos2]=tmp; +} + +void heap_hold(struct Heap *heap,int pos){ + int minpos=pos; + int left=2*pos+1,right=2*pos+2; + if(leftsize && heap->data[left]->weightdata[minpos]->weight) + minpos=left; + if(rightsize && heap->data[right]->weightdata[minpos]->weight) + minpos=right; + if(minpos!=pos){ + heap_swap(heap,pos,minpos); + heap_hold(heap,minpos); + } +} + +void heap_insert(struct Heap *heap,int node1,int node2,int weight){ + struct Edge *edge=malloc(sizeof(struct Edge)); + edge->node1=node1,edge->node2=node2,edge->weight=weight; + heap->data[heap->size]=edge; + heap->size++; + if(heap->size==1) + return; + int current=heap->size-1,parentpos=(current+1)/2-1; + while(parentpos>=0){ + if(heap->data[parentpos]->weight<=heap->data[current]->weight) + break; + heap_swap(heap,current,parentpos); + current=parentpos; + parentpos=(current+1)/2-1; + } +} + +struct Edge *delete_min(struct Heap *heap){ + if(!heap->size) + return NULL; + struct Edge *retval=heap->data[0]; + heap->data[0]=heap->data[heap->size-1]; + heap->size--; + heap_hold(heap,0); + return retval; +} \ No newline at end of file diff --git a/Map/MinestTree/Kruskal/heap.h b/Map/MinestTree/Kruskal/heap.h new file mode 100644 index 0000000..9826bf2 --- /dev/null +++ b/Map/MinestTree/Kruskal/heap.h @@ -0,0 +1,21 @@ +#ifndef HEAP_H +#define HEAP_H + +struct Heap{ + int size; + struct Edge **data; +}; + +struct Edge{ + int node1; + int node2; + int weight; +}; + +struct Heap *init_heap(int size); +void heap_swap(struct Heap *heap,int pos1,int pos2); +void heap_hold(struct Heap *heap,int pos); +void heap_insert(struct Heap *heap,int node1,int node2,int weight); +struct Edge *delete_min(struct Heap *heap); + +#endif \ No newline at end of file diff --git a/Map/MinestTree/Kruskal/main.c b/Map/MinestTree/Kruskal/main.c new file mode 100644 index 0000000..473015f --- /dev/null +++ b/Map/MinestTree/Kruskal/main.c @@ -0,0 +1,75 @@ +#include +#include +#include + +#include "map.h" +#include "disjointset.h" +#include "heap.h" + +void kruskal(struct Graph *map,struct Heap *heap,struct Set **origin_set){ + struct Graph *treemap=graph_init(map->nodesize); + for(int i=0;inodesize;i++) + graph_insertnode(treemap,i); + + struct Edge *cur; + int node1,node2,weight; + struct Set *par1,*par2; + while((cur=delete_min(heap))){ + node1=cur->node1,node2=cur->node2,weight=cur->weight; + par1=find_set(origin_set[node1]),par2=find_set(origin_set[node2]); + + if(par1!=par2){ + union_set(par1,par2); + graph_insertedge(treemap,node1,node2,weight); + graph_insertedge(treemap,node2,node1,weight); + } + } + printf("Done.\n"); + printf("Minest Tree:\n"); + display_debug(treemap); +} + +int main(int argc, char const *argv[]) +{ + if(argc<2) + return 1; + + FILE *map_file=fopen(argv[1],"r"); + int directed,nodesize,edgesize; + fscanf(map_file,"%d %d %d\n",&directed,&nodesize,&edgesize); + struct Graph *map=graph_init(nodesize); + + struct Heap *heap=init_heap(edgesize); + + char buff[1024]; + char *node_line=fgets(buff,sizeof(buff),map_file); + char *node=strtok(node_line," \n"); + while(node){ + graph_insertnode(map,atoi(node)); + node=strtok(NULL," \n"); + } + char *edge_line; + int node1,node2,weight; + while((edge_line=fgets(buff,sizeof(buff),map_file))){ + node=strtok(edge_line,"\t\n"); + node1=atoi(node); + node=strtok(NULL,"\t\n"); + node2=atoi(node); + node=strtok(NULL,"\t\n"); + weight=atoi(node); + graph_insertedge(map,node1,node2,weight); + + heap_insert(heap,node1,node2,weight); + if(!directed) + graph_insertedge(map,node2,node1,weight); + } + fflush(map_file); + fclose(map_file); + + display_debug(map); + struct Set **origin_set=init_set(map->nodesize); + + kruskal(map,heap,origin_set); + + return 0; +} \ No newline at end of file diff --git a/Map/MinestTree/Kruskal/makefile b/Map/MinestTree/Kruskal/makefile new file mode 100644 index 0000000..39ca898 --- /dev/null +++ b/Map/MinestTree/Kruskal/makefile @@ -0,0 +1,15 @@ +kruskal:main.o map.o heap.o disjointset.o + gcc -o kruskal main.o map.o heap.o disjointset.o +main.o:main.c map.h disjointset.h heap.h + gcc -c main.c +map.o:map.c map.h + gcc -c map.c +heap.o:heap.c heap.h + gcc -c heap.c +disjointset.o:disjointset.c disjointset.h + gcc -c disjointset.c +.PHONY:clean run +clean: + rm main.o map.o heap.o disjointset.o kruskal +run: + ./kruskal $(M) diff --git a/Map/MinestTree/Kruskal/map.c b/Map/MinestTree/Kruskal/map.c new file mode 100644 index 0000000..c2fc67c --- /dev/null +++ b/Map/MinestTree/Kruskal/map.c @@ -0,0 +1,47 @@ +#include +#include + +#include "map.h" +#include "heap.h" + +struct Graph *graph_init(int nodesize){ + struct Graph *map=malloc(sizeof(struct Graph)); + if(!map) + return map; + map->sp=0; + map->nodesize=nodesize; + map->nodes=malloc(sizeof(struct Node *)*nodesize); + return map; +} + +void graph_insertnode(struct Graph *map,int nodename){ + struct Node *newnode=malloc(sizeof(struct Node)); + newnode->next=NULL; + newnode->vertex=nodename; + map->nodes[map->sp++]=newnode; +} + +void graph_insertedge(struct Graph *map,int nodename1,int nodename2,int weight){ + struct Node *node=map->nodes[nodename1]; + struct Node *new=malloc(sizeof(struct Node)); + new->vertex=nodename2; + new->weight=weight; + new->next=node->next; + node->next=new; +} + +void display_debug(struct Graph *map){ + int i; + struct Node *cur; + for(i=0;inodesize;i++){ + cur=map->nodes[i]; + printf("%d:",cur->vertex); + cur=cur->next; + while(cur){ + printf(" %d(%d)",cur->vertex,cur->weight); + cur=cur->next; + } + printf("\n"); + } +} + diff --git a/Map/MinestTree/Kruskal/map.h b/Map/MinestTree/Kruskal/map.h new file mode 100644 index 0000000..21e0737 --- /dev/null +++ b/Map/MinestTree/Kruskal/map.h @@ -0,0 +1,22 @@ +#ifndef MAP_H +#define MAP_H + +struct Node{ + int vertex; + struct Node *next; + int weight; +}; + +struct Graph{ + struct Node **nodes; + int nodesize; + int sp; +}; + +struct Graph *graph_init(int); +void graph_insertnode(struct Graph *,int); +void graph_insertedge(struct Graph *,int,int,int); +void display_debug(struct Graph *); +struct Graph *read_map(char const *); + +#endif \ No newline at end of file diff --git a/Map/MinestTree/Kruskal/originmap.in b/Map/MinestTree/Kruskal/originmap.in new file mode 100644 index 0000000..db6b6c7 --- /dev/null +++ b/Map/MinestTree/Kruskal/originmap.in @@ -0,0 +1,14 @@ +0 7 12 +0 1 2 3 4 5 6 +0 1 2 +0 2 4 +0 3 1 +1 3 3 +1 4 10 +2 3 2 +2 5 5 +3 4 7 +3 5 8 +3 6 4 +4 6 6 +5 6 1 diff --git a/Map/MinestTree/Prim/main.c b/Map/MinestTree/Prim/main.c new file mode 100644 index 0000000..82fcd8d --- /dev/null +++ b/Map/MinestTree/Prim/main.c @@ -0,0 +1,105 @@ +#include +#include +#include +#include + +#include "map.h" +#define SOURCENODE 0 + +struct Table{ + char known; + int distance; + int prevnode; +}; + +struct Table **create_table(struct Graph *map){ + struct Table **table=malloc(sizeof(struct Table *)*map->nodesize); + int i; + for(i=0;inodesize;i++){ + table[i]=malloc(sizeof(struct Table)); + table[i]->known=0; + table[i]->distance=INT_MAX; + table[i]->prevnode=-1; + } + table[SOURCENODE]->distance=0; + return table; +} + +int find_startnode(struct Table **table,int size){ + int i; + int retpos=-1,minvalue=INT_MAX; + for(i=0;iknown && table[i]->distancedistance; + retpos=i; + } + return retpos; +} + + +void prim(struct Graph *map){ + struct Table **table=create_table(map); + int startnode; + struct Node *cur; + while((startnode=find_startnode(table,map->nodesize))!=-1){ + table[startnode]->known=1; + cur=map->nodes[startnode]->next; + while(cur){ + if(!table[cur->vertex]->known && cur->weightvertex]->distance){ + table[cur->vertex]->distance=cur->weight; + table[cur->vertex]->prevnode=startnode; + } + cur=cur->next; + } + } + struct Graph *treemap=graph_init(map->nodesize); + for(int i=0;inodesize;i++) + graph_insertnode(treemap,i); + for(int i=1;inodesize;i++){ + graph_insertedge(treemap,table[i]->prevnode,i,table[i]->distance); + graph_insertedge(treemap,i,table[i]->prevnode,table[i]->distance); + } + printf("Done.\n"); + printf("Minest Tree:\n"); + display_debug(treemap); +} + +int main(int argc, char const *argv[]) +{ + if(argc<2) + return 1; + + FILE *map_file=fopen(argv[1],"r"); + int directed,nodesize,edgesize; + fscanf(map_file,"%d %d %d\n",&directed,&nodesize,&edgesize); + struct Graph *map=graph_init(nodesize); + + char buff[1024]; + char *node_line=fgets(buff,sizeof(buff),map_file); + char *node=strtok(node_line," \n"); + while(node){ + graph_insertnode(map,atoi(node)); + node=strtok(NULL," \n"); + } + char *edge_line; + int node1,node2,weight; + while((edge_line=fgets(buff,sizeof(buff),map_file))){ + node=strtok(edge_line,"\t\n"); + node1=atoi(node); + node=strtok(NULL,"\t\n"); + node2=atoi(node); + node=strtok(NULL,"\t\n"); + weight=atoi(node); + graph_insertedge(map,node1,node2,weight); + if(!directed) + graph_insertedge(map,node2,node1,weight); + } + fflush(map_file); + fclose(map_file); + + display_debug(map); + + prim(map); + + return 0; +} \ No newline at end of file diff --git a/Map/MinestTree/Prim/map.c b/Map/MinestTree/Prim/map.c new file mode 100644 index 0000000..d7d63c5 --- /dev/null +++ b/Map/MinestTree/Prim/map.c @@ -0,0 +1,46 @@ +#include +#include + +#include "map.h" + +struct Graph *graph_init(int nodesize){ + struct Graph *map=malloc(sizeof(struct Graph)); + if(!map) + return map; + map->sp=0; + map->nodesize=nodesize; + map->nodes=malloc(sizeof(struct Node *)*nodesize); + return map; +} + +void graph_insertnode(struct Graph *map,int nodename){ + struct Node *newnode=malloc(sizeof(struct Node)); + newnode->next=NULL; + newnode->vertex=nodename; + map->nodes[map->sp++]=newnode; +} + +void graph_insertedge(struct Graph *map,int nodename1,int nodename2,int weight){ + struct Node *node=map->nodes[nodename1]; + struct Node *new=malloc(sizeof(struct Node)); + new->vertex=nodename2; + new->weight=weight; + new->next=node->next; + node->next=new; +} + +void display_debug(struct Graph *map){ + int i; + struct Node *cur; + for(i=0;inodesize;i++){ + cur=map->nodes[i]; + printf("%d:",cur->vertex); + cur=cur->next; + while(cur){ + printf(" %d(%d)",cur->vertex,cur->weight); + cur=cur->next; + } + printf("\n"); + } +} + diff --git a/Map/MinestTree/Prim/map.h b/Map/MinestTree/Prim/map.h new file mode 100644 index 0000000..aa02cf6 --- /dev/null +++ b/Map/MinestTree/Prim/map.h @@ -0,0 +1,21 @@ +#ifndef MAP_H +#define MAP_H + +struct Node{ + int vertex; + struct Node *next; + int weight; +}; + +struct Graph{ + struct Node **nodes; + int nodesize; + int sp; +}; + +struct Graph *graph_init(int); +void graph_insertnode(struct Graph *,int); +void graph_insertedge(struct Graph *,int,int,int); +void display_debug(struct Graph *); + +#endif \ No newline at end of file diff --git a/Map/MinestTree/Prim/originmap.in b/Map/MinestTree/Prim/originmap.in new file mode 100644 index 0000000..db6b6c7 --- /dev/null +++ b/Map/MinestTree/Prim/originmap.in @@ -0,0 +1,14 @@ +0 7 12 +0 1 2 3 4 5 6 +0 1 2 +0 2 4 +0 3 1 +1 3 3 +1 4 10 +2 3 2 +2 5 5 +3 4 7 +3 5 8 +3 6 4 +4 6 6 +5 6 1 diff --git "a/Map/MinestTree/Prim/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/main.c" "b/Map/MinestTree/Prim/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/main.c" new file mode 100644 index 0000000..3e9d0df --- /dev/null +++ "b/Map/MinestTree/Prim/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/main.c" @@ -0,0 +1,151 @@ +#include +#include +#include +#include + +#include "map.h" +#define SOURCENODE 0 + +struct HeapNode{ + int vertex; + int distance; + int prev; +}; + +struct Heap{ + int size; + struct HeapNode *data; +}; + +struct Heap *create_heap(struct Graph *map,int *distance){ + struct Heap *heap=malloc(sizeof(struct Heap)); + if(!heap) + return heap; + heap->size=map->nodesize; + heap->data=malloc(sizeof(struct HeapNode)*map->nodesize); + for(int i=0;inodesize;i++){ + heap->data[i].vertex=i; + heap->data[i].distance=distance[i]; + heap->data[i].prev=-1; + } + return heap; +} + +void swap_heap(struct Heap *heap,int pos1,int pos2){ + struct HeapNode tmp=heap->data[pos1]; + heap->data[pos1]=heap->data[pos2]; + heap->data[pos2]=tmp; +} + +void hold_heap(struct Heap *heap,int pos){ + int left=2*pos+1,right=2*pos+2; + int minpos=pos; + if(leftsize && heap->data[left].distancedata[minpos].distance) + minpos=left; + if(rightsize && heap->data[right].distancedata[minpos].distance) + minpos=right; + if(minpos!=pos){ + swap_heap(heap,minpos,pos); + hold_heap(heap,minpos); + } +} + +int empty_heap(struct Heap *heap){ + return heap->size==0; +} + +struct HeapNode delete_heap(struct Heap *heap){ + struct HeapNode retval=heap->data[0]; + //heap->data[0]=heap->data[heap->size-1]; + swap_heap(heap,0,heap->size-1); + heap->size--; + hold_heap(heap,0); + return retval; +} + +void build_heap(struct Heap *heap){ + int lastparent=(heap->size-1)/2; + for(int i=lastparent;i>=0;i--) + hold_heap(heap,i); +} + +void decrease_heap(struct Heap *heap,int vertex,int newdistance,int newprev){ + int pos=-1; + for(int i=0;isize;i++) + if(heap->data[i].vertex==vertex){ + pos=i; + break; + } + if(pos==-1 || newdistance>=heap->data[pos].distance) + return ; + heap->data[pos].distance=newdistance; + heap->data[pos].prev=newprev; + int current=pos,parent=(current-1)/2; + while(parent>=0){ + if(heap->data[parent].distance<=heap->data[current].distance) + break; + swap_heap(heap,parent,current); + current=parent; + parent=(current-1)/2; + } +} + +void prim(struct Graph *map){ + int distance[map->nodesize]; + for(int i=0;inodesize;i++) + distance[i]=INT_MAX; + distance[SOURCENODE]=0; + struct Heap *heap=create_heap(map,distance); + build_heap(heap); + struct HeapNode node; + struct Node *cur; + while(!empty_heap(heap)){ + node=delete_heap(heap); + cur=map->nodes[node.vertex]->next; + while(cur){ + decrease_heap(heap,cur->vertex,cur->weight,node.vertex); + cur=cur->next; + } + } + //now print minest tree + for(int i=0;inodesize;i++) + printf("%d,distance=%d,prev=%d\n",heap->data[i].vertex,heap->data[i].distance,heap->data[i].prev); +} + +int main(int argc, char const *argv[]){ + if(argc<2) + return 1; + FILE *map_file=fopen(argv[1],"r"); + int directed,nodesize,edgesize; + fscanf(map_file,"%d %d %d\n",&directed,&nodesize,&edgesize); + struct Graph *map=graph_init(nodesize); + + char buff[1024]; + char *node_line=fgets(buff,sizeof(buff),map_file); + char *node=strtok(node_line," \n"); + while(node){ + graph_insertnode(map,atoi(node)); + node=strtok(NULL," \n"); + } + char *edge_line; + int node1,node2,weight; + while((edge_line=fgets(buff,sizeof(buff),map_file))){ + node=strtok(edge_line,"\t\n"); + node1=atoi(node); + node=strtok(NULL,"\t\n"); + node2=atoi(node); + node=strtok(NULL,"\t\n"); + weight=atoi(node); + graph_insertedge(map,node1,node2,weight); + if(!directed) + graph_insertedge(map,node2,node1,weight); + } + fflush(map_file); + fclose(map_file); + + display_debug(map); + + prim(map); + + return 0; +} \ No newline at end of file diff --git "a/Map/MinestTree/Prim/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/map.c" "b/Map/MinestTree/Prim/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/map.c" new file mode 100644 index 0000000..d7d63c5 --- /dev/null +++ "b/Map/MinestTree/Prim/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/map.c" @@ -0,0 +1,46 @@ +#include +#include + +#include "map.h" + +struct Graph *graph_init(int nodesize){ + struct Graph *map=malloc(sizeof(struct Graph)); + if(!map) + return map; + map->sp=0; + map->nodesize=nodesize; + map->nodes=malloc(sizeof(struct Node *)*nodesize); + return map; +} + +void graph_insertnode(struct Graph *map,int nodename){ + struct Node *newnode=malloc(sizeof(struct Node)); + newnode->next=NULL; + newnode->vertex=nodename; + map->nodes[map->sp++]=newnode; +} + +void graph_insertedge(struct Graph *map,int nodename1,int nodename2,int weight){ + struct Node *node=map->nodes[nodename1]; + struct Node *new=malloc(sizeof(struct Node)); + new->vertex=nodename2; + new->weight=weight; + new->next=node->next; + node->next=new; +} + +void display_debug(struct Graph *map){ + int i; + struct Node *cur; + for(i=0;inodesize;i++){ + cur=map->nodes[i]; + printf("%d:",cur->vertex); + cur=cur->next; + while(cur){ + printf(" %d(%d)",cur->vertex,cur->weight); + cur=cur->next; + } + printf("\n"); + } +} + diff --git "a/Map/MinestTree/Prim/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/map.h" "b/Map/MinestTree/Prim/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/map.h" new file mode 100644 index 0000000..aa02cf6 --- /dev/null +++ "b/Map/MinestTree/Prim/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/map.h" @@ -0,0 +1,21 @@ +#ifndef MAP_H +#define MAP_H + +struct Node{ + int vertex; + struct Node *next; + int weight; +}; + +struct Graph{ + struct Node **nodes; + int nodesize; + int sp; +}; + +struct Graph *graph_init(int); +void graph_insertnode(struct Graph *,int); +void graph_insertedge(struct Graph *,int,int,int); +void display_debug(struct Graph *); + +#endif \ No newline at end of file diff --git "a/Map/MinestTree/Prim/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/originmap.in" "b/Map/MinestTree/Prim/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/originmap.in" new file mode 100644 index 0000000..db6b6c7 --- /dev/null +++ "b/Map/MinestTree/Prim/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/originmap.in" @@ -0,0 +1,14 @@ +0 7 12 +0 1 2 3 4 5 6 +0 1 2 +0 2 4 +0 3 1 +1 3 3 +1 4 10 +2 3 2 +2 5 5 +3 4 7 +3 5 8 +3 6 4 +4 6 6 +5 6 1 diff --git a/Map/P2P shortest path/Base/main.c b/Map/P2P shortest path/Base/main.c new file mode 100644 index 0000000..f0696e6 --- /dev/null +++ b/Map/P2P shortest path/Base/main.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include "map.h" + +int **getnextmatrix(int **matrix,int nodesize,int **map){ + int **nextmatrix=malloc(sizeof(int *)*nodesize); + for(int i=0;imatrix[i][k]+map[k][j]) + nextmatrix[i][j]=matrix[i][k]+map[k][j]; + } + } + return nextmatrix; +} + +void basemethod(int **map,int nodesize){ + for(int i=0;i +#include +#include + +#include "map.h" + +int **graph_init(int nodesize){ + int **map=malloc(sizeof(int *)*nodesize); + int i; + for(i=0;i +#include +#include +#include "map.h" + +void floyed(int **map,int nodesize,int **distance,int **prev){ + for(int k=0;k%d",pop()); + printf("\n"); +} + +int main(int argc, char const *argv[]) +{ + if(argc<2) + return 1; + int nodesize; + int **map=read_map(argv[1],&nodesize); + + int **distance=malloc(sizeof(int *)*nodesize); + int **prev=malloc(sizeof(int *)*nodesize); + for(int i=0;i%d shortest path length=:%d\n",test1,test2,distance[test1][test2]); + print_path(prev,nodesize,test1,test2); + } + return 0; +} \ No newline at end of file diff --git a/Map/P2P shortest path/Floyed/map.c b/Map/P2P shortest path/Floyed/map.c new file mode 100644 index 0000000..ea5e3e1 --- /dev/null +++ b/Map/P2P shortest path/Floyed/map.c @@ -0,0 +1,56 @@ +#include +#include +#include + +#include "map.h" + +int **graph_init(int nodesize){ + int **map=malloc(sizeof(int *)*nodesize); + int i; + for(i=0;i +#include +#include +#include +#include "map.h" + +int bellman_ford(struct Graph *map,int *distance,int source){ + struct Node *cur; + for(int i=0;inodesize-1;i++) + for(int j=0;jnodesize;j++){ + cur=map->nodes[j]->next; + while(cur){ + //i=>cur->vertex + if(distance[j]+cur->weightvertex]) + distance[cur->vertex]=distance[j]+cur->weight; + cur=cur->next; + } + } + int ok=1; + for(int i=0;inodesize && ok;i++){ + cur=map->nodes[i]->next; + while(cur && ok){ + //i=>cur->vertex + if(distance[i]+cur->weightvertex]) + ok=0; + cur=cur->next; + } + } + return ok; +} + +void change2positive(struct Graph *map,int *distance){ + map->nodes=realloc(map->nodes,sizeof(struct Node *)*(map->nodesize+1)); + int source=map->nodesize; + graph_insertnode(map,source); + map->nodesize++; + for(int i=0;inodesize;i++){ + cur=map->nodes[i]->next; + while(cur){ + //i => cur->vertex + cur->weight+=distance[i]-distance[cur->vertex]; + cur=cur->next; + } + } + display_debug(map); + map->nodesize--; +} + +int find_min_d_dij(int *distance,int size,int *known){ + int retval=-1,min=SHRT_MAX; + for(int i=0;inodesize]; + for(int i=0;inodesize;i++) + known[i]=0; + int find; + struct Node *cur; + while((find=find_min_d_dij(distance,map->nodesize,known))!=-1){ + known[find]=1; + cur=map->nodes[find]->next; + while(cur){ + //find=>cur->vertex + if(distance[find]+cur->weightvertex]) + distance[cur->vertex]=distance[find]+cur->weight; + cur=cur->next; + } + } +} + +void johnson(struct Graph *map,int *h){ + int **retval=malloc(sizeof(int *)*map->nodesize); + for(int i=0;inodesize;i++){ + retval[i]=malloc(sizeof(int)*map->nodesize); + for(int j=0;jnodesize;j++) + retval[i][j]=SHRT_MAX; + } + printf("source"); + for(int i=0;inodesize;i++) + printf("\t%d",i); + printf("\n"); + for(int i=0;inodesize;i++){ + dijkstra(map,i,h,retval[i]); + printf("%d",i); + for(int j=0;jnodesize;j++){ + //node=j,source=i + retval[i][j]+=h[j]-h[i]; + printf("\t%d",retval[i][j]); + } + printf("\n"); + } +} + +int main(int argc, char const *argv[]) +{ + if(argc<2) + return 1; + int hasnegativeweight=0; + FILE *map_file=fopen(argv[1],"r"); + int directed,nodesize; + fscanf(map_file,"%d %d\n",&directed,&nodesize); + struct Graph *map=graph_init(nodesize); + + char buff[1024]; + char *node_line=fgets(buff,sizeof(buff),map_file); + char *node=strtok(node_line," \n"); + while(node){ + graph_insertnode(map,atoi(node)); + node=strtok(NULL," \n"); + } + char *edge_line; + int node1,node2,weight; + while((edge_line=fgets(buff,sizeof(buff),map_file))){ + node=strtok(edge_line,"\t\n"); + node1=atoi(node); + node=strtok(NULL,"\t\n"); + node2=atoi(node); + node=strtok(NULL,"\t\n"); + weight=atoi(node); + if(!hasnegativeweight && weight<0) + hasnegativeweight=1; + graph_insertedge(map,node1,node2,weight); + if(!directed) + graph_insertedge(map,node2,node1,weight); + } + fflush(map_file); + fclose(map_file); + + //display_debug(map); + int *distance=malloc(sizeof(int)*(map->nodesize+1)); + for(int i=0;inodesize;i++) + distance[i]=SHRT_MAX; + if(hasnegativeweight) + change2positive(map,distance); + johnson(map,distance); + return 0; +} \ No newline at end of file diff --git a/Map/P2P shortest path/Johnson/map.c b/Map/P2P shortest path/Johnson/map.c new file mode 100644 index 0000000..7731663 --- /dev/null +++ b/Map/P2P shortest path/Johnson/map.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include "map.h" + +struct Graph *graph_init(int nodesize){ + struct Graph *map=malloc(sizeof(struct Graph)); + if(!map) + return map; + map->sp=0; + map->nodesize=nodesize; + map->nodes=malloc(sizeof(struct Node *)*nodesize); + return map; +} + +void graph_insertnode(struct Graph *map,int nodename){ + struct Node *newnode=malloc(sizeof(struct Node)); + newnode->next=NULL; + newnode->vertex=nodename; + map->nodes[map->sp++]=newnode; +} + +void graph_insertedge(struct Graph *map,int nodename1,int nodename2,int weight){ + struct Node *node=map->nodes[nodename1]; + struct Node *new=malloc(sizeof(struct Node)); + new->vertex=nodename2; + new->weight=weight; + new->next=node->next; + node->next=new; +} + +void display_debug(struct Graph *map){ + int i; + struct Node *cur; + for(i=0;inodesize;i++){ + cur=map->nodes[i]; + printf("%d:",cur->vertex); + cur=cur->next; + while(cur){ + printf(" %d(%d)",cur->vertex,cur->weight); + cur=cur->next; + } + printf("\n"); + } +} + diff --git a/Map/P2P shortest path/Johnson/map.h b/Map/P2P shortest path/Johnson/map.h new file mode 100644 index 0000000..aa02cf6 --- /dev/null +++ b/Map/P2P shortest path/Johnson/map.h @@ -0,0 +1,21 @@ +#ifndef MAP_H +#define MAP_H + +struct Node{ + int vertex; + struct Node *next; + int weight; +}; + +struct Graph{ + struct Node **nodes; + int nodesize; + int sp; +}; + +struct Graph *graph_init(int); +void graph_insertnode(struct Graph *,int); +void graph_insertedge(struct Graph *,int,int,int); +void display_debug(struct Graph *); + +#endif \ No newline at end of file diff --git a/Map/P2P shortest path/Johnson/originmap.in b/Map/P2P shortest path/Johnson/originmap.in new file mode 100644 index 0000000..9eab319 --- /dev/null +++ b/Map/P2P shortest path/Johnson/originmap.in @@ -0,0 +1,11 @@ +1 5 +0 1 2 3 4 +0 1 3 +0 2 8 +0 4 -4 +1 3 1 +1 4 7 +2 1 4 +3 0 2 +3 2 -5 +4 3 6 diff --git a/Map/Single-source shortest path/Bellman-Ford/bellmanford.c b/Map/Single-source shortest path/Bellman-Ford/bellmanford.c new file mode 100644 index 0000000..a9f498c --- /dev/null +++ b/Map/Single-source shortest path/Bellman-Ford/bellmanford.c @@ -0,0 +1,51 @@ +#include +#include +#include + +#define START 0 + +#include "map.h" + + +int bellmanford(struct Graph *map){ + int shortestpath[map->nodesize]; + for(int i=0;inodesize;i++) + shortestpath[i]=SHRT_MAX; + shortestpath[START]=0; + struct Node *cur; + for(int i=0;inodesize-1;i++) + for(int j=0;jnodesize;j++){ + cur=map->nodes[j]->next; + while(cur){ + //j ---> cur->vertex + if(shortestpath[cur->vertex]>shortestpath[j]+cur->weight) + shortestpath[cur->vertex]=shortestpath[j]+cur->weight; + cur=cur->next; + } + } + + for(int i=0;inodesize;i++){ + cur=map->nodes[i]->next; + while(cur){ + //j ---> cur->vertex + if(shortestpath[cur->vertex]>shortestpath[i]+cur->weight) + return 0; + cur=cur->next; + } + } + + for(int i=0;inodesize;i++) + printf("%d:%d\n",i,shortestpath[i]); + return 1; +} + +int main(int argc, char const *argv[]){ + if(argc<2) + return 1; + + struct Graph *map=read_map(argv[1]); + + bellmanford(map); + + return 0; +} \ No newline at end of file diff --git a/Map/Single-source shortest path/Bellman-Ford/map.c b/Map/Single-source shortest path/Bellman-Ford/map.c new file mode 100644 index 0000000..447b98d --- /dev/null +++ b/Map/Single-source shortest path/Bellman-Ford/map.c @@ -0,0 +1,76 @@ +#include +#include +#include +#include "map.h" + +struct Graph *graph_init(int nodesize){ + struct Graph *map=malloc(sizeof(struct Graph)); + if(!map) + return map; + map->sp=0; + map->nodesize=nodesize; + map->nodes=malloc(sizeof(struct Node *)*nodesize); + return map; +} + +void graph_insertnode(struct Graph *map,int nodename){ + struct Node *newnode=malloc(sizeof(struct Node)); + newnode->next=NULL; + newnode->vertex=nodename; + map->nodes[map->sp++]=newnode; +} + +void graph_insertedge(struct Graph *map,int nodename1,int nodename2,int weight){ + struct Node *node=map->nodes[nodename1]; + struct Node *new=malloc(sizeof(struct Node)); + new->vertex=nodename2; + new->weight=weight; + new->next=node->next; + node->next=new; +} + +void display_debug(struct Graph *map){ + int i; + struct Node *cur; + for(i=0;inodesize;i++){ + cur=map->nodes[i]; + printf("%d:",cur->vertex); + cur=cur->next; + while(cur){ + printf(" %d(%d)",cur->vertex,cur->weight); + cur=cur->next; + } + printf("\n"); + } +} + +struct Graph *read_map(char const *filename){ + FILE *map_file=fopen(filename,"r"); + int directed,nodesize; + fscanf(map_file,"%d %d\n",&directed,&nodesize); + struct Graph *map=graph_init(nodesize); + + char buff[1024]; + char *node_line=fgets(buff,sizeof(buff),map_file); + char *node=strtok(node_line," \n"); + while(node){ + graph_insertnode(map,atoi(node)); + node=strtok(NULL," \n"); + } + char *edge_line; + int node1,node2,weight; + while((edge_line=fgets(buff,sizeof(buff),map_file))){ + node=strtok(edge_line,"\t\n"); + node1=atoi(node); + node=strtok(NULL,"\t\n"); + node2=atoi(node); + node=strtok(NULL,"\t\n"); + weight=atoi(node); + graph_insertedge(map,node1,node2,weight); + if(!directed) + graph_insertedge(map,node2,node1,weight); + } + fflush(map_file); + fclose(map_file); + return map; +} \ No newline at end of file diff --git a/Map/Single-source shortest path/Bellman-Ford/map.h b/Map/Single-source shortest path/Bellman-Ford/map.h new file mode 100644 index 0000000..21e0737 --- /dev/null +++ b/Map/Single-source shortest path/Bellman-Ford/map.h @@ -0,0 +1,22 @@ +#ifndef MAP_H +#define MAP_H + +struct Node{ + int vertex; + struct Node *next; + int weight; +}; + +struct Graph{ + struct Node **nodes; + int nodesize; + int sp; +}; + +struct Graph *graph_init(int); +void graph_insertnode(struct Graph *,int); +void graph_insertedge(struct Graph *,int,int,int); +void display_debug(struct Graph *); +struct Graph *read_map(char const *); + +#endif \ No newline at end of file diff --git a/Map/Single-source shortest path/Bellman-Ford/originmap.in b/Map/Single-source shortest path/Bellman-Ford/originmap.in new file mode 100644 index 0000000..373d931 --- /dev/null +++ b/Map/Single-source shortest path/Bellman-Ford/originmap.in @@ -0,0 +1,12 @@ +1 5 +0 1 2 3 4 +0 1 6 +0 3 7 +1 2 5 +1 3 8 +1 4 -4 +2 1 -2 +3 2 -3 +3 4 9 +4 0 2 +4 2 7 diff --git a/Map/Single-source shortest path/Dijkstra/dijkstra.c b/Map/Single-source shortest path/Dijkstra/dijkstra.c new file mode 100644 index 0000000..36bfb60 --- /dev/null +++ b/Map/Single-source shortest path/Dijkstra/dijkstra.c @@ -0,0 +1,80 @@ +#include +#include +#include + +#include "map.h" + +#define START 0 + +struct Table{ + int prev; + int distance; + char known; +}; + +struct Table **init_table(int size){ + struct Table **table=malloc(sizeof(struct Table *)*size); + for(int i=0;iknown=0; + table[i]->prev=-1; + table[i]->distance=SHRT_MAX; + } + table[START]->distance=0; + return table; +} + +int find_entry(struct Table **table,int size){ + int pos=-1; + int min=SHRT_MAX; + for(int i=0;iknown && table[i]->distancedistance; + pos=i; + } + return pos; +} + +void dijkstra(struct Graph *map,struct Table **table){ + int pos; + struct Node *cur; + while((pos=find_entry(table,map->nodesize))!=-1){ + cur=map->nodes[pos]->next; + while(cur){ + //pos --> cur->vertex + if(table[cur->vertex]->distance>table[pos]->distance+cur->weight){ + table[cur->vertex]->distance=table[pos]->distance+cur->weight; + table[cur->vertex]->prev=pos; + } + cur=cur->next; + } + table[pos]->known=1; + } +} + +void show_path(struct Table **table,int dest){ + int prev; + while((prev=table[dest]->prev)!=-1){ + printf("%d<==",dest); + dest=prev; + } + printf("%d\n",START); +} + +int main(int argc, char const *argv[]) +{ + if(argc<2) + return 1; + struct Graph *map=read_map(argv[1]); + struct Table **table=init_table(map->nodesize); + dijkstra(map,table); + for(int i=0;inodesize;i++) + printf("%d,%d,%d\n",i,table[i]->distance,table[i]->prev); + + int source=START,dest; + while(scanf("%d",&dest)==1){ + printf("%d到%d最短路径长为%d:\n",source,dest,table[dest]->distance); + show_path(table,dest); + } + return 0; +} \ No newline at end of file diff --git a/Map/Single-source shortest path/Dijkstra/map.c b/Map/Single-source shortest path/Dijkstra/map.c new file mode 100644 index 0000000..447b98d --- /dev/null +++ b/Map/Single-source shortest path/Dijkstra/map.c @@ -0,0 +1,76 @@ +#include +#include +#include +#include "map.h" + +struct Graph *graph_init(int nodesize){ + struct Graph *map=malloc(sizeof(struct Graph)); + if(!map) + return map; + map->sp=0; + map->nodesize=nodesize; + map->nodes=malloc(sizeof(struct Node *)*nodesize); + return map; +} + +void graph_insertnode(struct Graph *map,int nodename){ + struct Node *newnode=malloc(sizeof(struct Node)); + newnode->next=NULL; + newnode->vertex=nodename; + map->nodes[map->sp++]=newnode; +} + +void graph_insertedge(struct Graph *map,int nodename1,int nodename2,int weight){ + struct Node *node=map->nodes[nodename1]; + struct Node *new=malloc(sizeof(struct Node)); + new->vertex=nodename2; + new->weight=weight; + new->next=node->next; + node->next=new; +} + +void display_debug(struct Graph *map){ + int i; + struct Node *cur; + for(i=0;inodesize;i++){ + cur=map->nodes[i]; + printf("%d:",cur->vertex); + cur=cur->next; + while(cur){ + printf(" %d(%d)",cur->vertex,cur->weight); + cur=cur->next; + } + printf("\n"); + } +} + +struct Graph *read_map(char const *filename){ + FILE *map_file=fopen(filename,"r"); + int directed,nodesize; + fscanf(map_file,"%d %d\n",&directed,&nodesize); + struct Graph *map=graph_init(nodesize); + + char buff[1024]; + char *node_line=fgets(buff,sizeof(buff),map_file); + char *node=strtok(node_line," \n"); + while(node){ + graph_insertnode(map,atoi(node)); + node=strtok(NULL," \n"); + } + char *edge_line; + int node1,node2,weight; + while((edge_line=fgets(buff,sizeof(buff),map_file))){ + node=strtok(edge_line,"\t\n"); + node1=atoi(node); + node=strtok(NULL,"\t\n"); + node2=atoi(node); + node=strtok(NULL,"\t\n"); + weight=atoi(node); + graph_insertedge(map,node1,node2,weight); + if(!directed) + graph_insertedge(map,node2,node1,weight); + } + fflush(map_file); + fclose(map_file); + return map; +} \ No newline at end of file diff --git a/Map/Single-source shortest path/Dijkstra/map.h b/Map/Single-source shortest path/Dijkstra/map.h new file mode 100644 index 0000000..21e0737 --- /dev/null +++ b/Map/Single-source shortest path/Dijkstra/map.h @@ -0,0 +1,22 @@ +#ifndef MAP_H +#define MAP_H + +struct Node{ + int vertex; + struct Node *next; + int weight; +}; + +struct Graph{ + struct Node **nodes; + int nodesize; + int sp; +}; + +struct Graph *graph_init(int); +void graph_insertnode(struct Graph *,int); +void graph_insertedge(struct Graph *,int,int,int); +void display_debug(struct Graph *); +struct Graph *read_map(char const *); + +#endif \ No newline at end of file diff --git a/Map/Single-source shortest path/Dijkstra/originmap.in b/Map/Single-source shortest path/Dijkstra/originmap.in new file mode 100644 index 0000000..8e3a82b --- /dev/null +++ b/Map/Single-source shortest path/Dijkstra/originmap.in @@ -0,0 +1,14 @@ +1 7 +0 1 2 3 4 5 6 +0 1 2 +0 3 1 +1 3 3 +1 4 10 +2 0 4 +2 5 5 +3 2 2 +3 4 2 +3 5 8 +3 6 4 +4 6 6 +6 5 1 diff --git "a/Map/Single-source shortest path/Dijkstra/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/dijkstra.c" "b/Map/Single-source shortest path/Dijkstra/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/dijkstra.c" new file mode 100644 index 0000000..360fd1b --- /dev/null +++ "b/Map/Single-source shortest path/Dijkstra/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/dijkstra.c" @@ -0,0 +1,151 @@ +#include +#include +#include +#include + +#include "map.h" +#define SOURCENODE 0 + +struct HeapNode{ + int vertex; + int distance; + int prev; +}; + +struct Heap{ + int size; + struct HeapNode *data; +}; + +struct Heap *create_heap(struct Graph *map,int *distance){ + struct Heap *heap=malloc(sizeof(struct Heap)); + if(!heap) + return heap; + heap->size=map->nodesize; + heap->data=malloc(sizeof(struct HeapNode)*map->nodesize); + for(int i=0;inodesize;i++){ + heap->data[i].vertex=i; + heap->data[i].distance=distance[i]; + heap->data[i].prev=-1; + } + return heap; +} + +void swap_heap(struct Heap *heap,int pos1,int pos2){ + struct HeapNode tmp=heap->data[pos1]; + heap->data[pos1]=heap->data[pos2]; + heap->data[pos2]=tmp; +} + +void hold_heap(struct Heap *heap,int pos){ + int left=2*pos+1,right=2*pos+2; + int minpos=pos; + if(leftsize && heap->data[left].distancedata[minpos].distance) + minpos=left; + if(rightsize && heap->data[right].distancedata[minpos].distance) + minpos=right; + if(minpos!=pos){ + swap_heap(heap,minpos,pos); + hold_heap(heap,minpos); + } +} + +int empty_heap(struct Heap *heap){ + return heap->size==0; +} + +struct HeapNode delete_heap(struct Heap *heap){ + struct HeapNode retval=heap->data[0]; + //heap->data[0]=heap->data[heap->size-1]; + swap_heap(heap,0,heap->size-1); + heap->size--; + hold_heap(heap,0); + return retval; +} + +void build_heap(struct Heap *heap){ + int lastparent=(heap->size-1)/2; + for(int i=lastparent;i>=0;i--) + hold_heap(heap,i); +} + +void decrease_heap(struct Heap *heap,int vertex,int newdistance,int newprev){ + int pos=-1; + for(int i=0;isize;i++) + if(heap->data[i].vertex==vertex){ + pos=i; + break; + } + if(pos==-1 || newdistance>=heap->data[pos].distance) + return ; + heap->data[pos].distance=newdistance; + heap->data[pos].prev=newprev; + int current=pos,parent=(current-1)/2; + while(parent>=0){ + if(heap->data[parent].distance<=heap->data[current].distance) + break; + swap_heap(heap,parent,current); + current=parent; + parent=(current-1)/2; + } +} + +void dijkstra(struct Graph *map){ + int distance[map->nodesize]; + for(int i=0;inodesize;i++) + distance[i]=INT_MAX; + distance[SOURCENODE]=0; + struct Heap *heap=create_heap(map,distance); + build_heap(heap); + struct HeapNode node; + struct Node *cur; + while(!empty_heap(heap)){ + node=delete_heap(heap); + cur=map->nodes[node.vertex]->next; + while(cur){ + decrease_heap(heap,cur->vertex,node.distance+cur->weight,node.vertex); + cur=cur->next; + } + } + //now print minest tree + for(int i=0;inodesize;i++) + printf("%d,distance=%d,prev=%d\n",heap->data[i].vertex,heap->data[i].distance,heap->data[i].prev); +} + +int main(int argc, char const *argv[]){ + if(argc<2) + return 1; + FILE *map_file=fopen(argv[1],"r"); + int directed,nodesize; + fscanf(map_file,"%d %d\n",&directed,&nodesize); + struct Graph *map=graph_init(nodesize); + + char buff[1024]; + char *node_line=fgets(buff,sizeof(buff),map_file); + char *node=strtok(node_line," \n"); + while(node){ + graph_insertnode(map,atoi(node)); + node=strtok(NULL," \n"); + } + char *edge_line; + int node1,node2,weight; + while((edge_line=fgets(buff,sizeof(buff),map_file))){ + node=strtok(edge_line,"\t\n"); + node1=atoi(node); + node=strtok(NULL,"\t\n"); + node2=atoi(node); + node=strtok(NULL,"\t\n"); + weight=atoi(node); + graph_insertedge(map,node1,node2,weight); + if(!directed) + graph_insertedge(map,node2,node1,weight); + } + fflush(map_file); + fclose(map_file); + + display_debug(map); + + dijkstra(map); + + return 0; +} \ No newline at end of file diff --git "a/Map/Single-source shortest path/Dijkstra/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/map.c" "b/Map/Single-source shortest path/Dijkstra/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/map.c" new file mode 100644 index 0000000..d7d63c5 --- /dev/null +++ "b/Map/Single-source shortest path/Dijkstra/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/map.c" @@ -0,0 +1,46 @@ +#include +#include + +#include "map.h" + +struct Graph *graph_init(int nodesize){ + struct Graph *map=malloc(sizeof(struct Graph)); + if(!map) + return map; + map->sp=0; + map->nodesize=nodesize; + map->nodes=malloc(sizeof(struct Node *)*nodesize); + return map; +} + +void graph_insertnode(struct Graph *map,int nodename){ + struct Node *newnode=malloc(sizeof(struct Node)); + newnode->next=NULL; + newnode->vertex=nodename; + map->nodes[map->sp++]=newnode; +} + +void graph_insertedge(struct Graph *map,int nodename1,int nodename2,int weight){ + struct Node *node=map->nodes[nodename1]; + struct Node *new=malloc(sizeof(struct Node)); + new->vertex=nodename2; + new->weight=weight; + new->next=node->next; + node->next=new; +} + +void display_debug(struct Graph *map){ + int i; + struct Node *cur; + for(i=0;inodesize;i++){ + cur=map->nodes[i]; + printf("%d:",cur->vertex); + cur=cur->next; + while(cur){ + printf(" %d(%d)",cur->vertex,cur->weight); + cur=cur->next; + } + printf("\n"); + } +} + diff --git "a/Map/Single-source shortest path/Dijkstra/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/map.h" "b/Map/Single-source shortest path/Dijkstra/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/map.h" new file mode 100644 index 0000000..aa02cf6 --- /dev/null +++ "b/Map/Single-source shortest path/Dijkstra/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/map.h" @@ -0,0 +1,21 @@ +#ifndef MAP_H +#define MAP_H + +struct Node{ + int vertex; + struct Node *next; + int weight; +}; + +struct Graph{ + struct Node **nodes; + int nodesize; + int sp; +}; + +struct Graph *graph_init(int); +void graph_insertnode(struct Graph *,int); +void graph_insertedge(struct Graph *,int,int,int); +void display_debug(struct Graph *); + +#endif \ No newline at end of file diff --git "a/Map/Single-source shortest path/Dijkstra/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/originmap.in" "b/Map/Single-source shortest path/Dijkstra/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/originmap.in" new file mode 100644 index 0000000..8e3a82b --- /dev/null +++ "b/Map/Single-source shortest path/Dijkstra/\344\270\215\345\273\272\347\253\213\350\241\250\346\226\271\346\263\225/originmap.in" @@ -0,0 +1,14 @@ +1 7 +0 1 2 3 4 5 6 +0 1 2 +0 3 1 +1 3 3 +1 4 10 +2 0 4 +2 5 5 +3 2 2 +3 4 2 +3 5 8 +3 6 4 +4 6 6 +6 5 1 diff --git a/Map/Single-source shortest path/Topo/map.c b/Map/Single-source shortest path/Topo/map.c new file mode 100644 index 0000000..447b98d --- /dev/null +++ b/Map/Single-source shortest path/Topo/map.c @@ -0,0 +1,76 @@ +#include +#include +#include +#include "map.h" + +struct Graph *graph_init(int nodesize){ + struct Graph *map=malloc(sizeof(struct Graph)); + if(!map) + return map; + map->sp=0; + map->nodesize=nodesize; + map->nodes=malloc(sizeof(struct Node *)*nodesize); + return map; +} + +void graph_insertnode(struct Graph *map,int nodename){ + struct Node *newnode=malloc(sizeof(struct Node)); + newnode->next=NULL; + newnode->vertex=nodename; + map->nodes[map->sp++]=newnode; +} + +void graph_insertedge(struct Graph *map,int nodename1,int nodename2,int weight){ + struct Node *node=map->nodes[nodename1]; + struct Node *new=malloc(sizeof(struct Node)); + new->vertex=nodename2; + new->weight=weight; + new->next=node->next; + node->next=new; +} + +void display_debug(struct Graph *map){ + int i; + struct Node *cur; + for(i=0;inodesize;i++){ + cur=map->nodes[i]; + printf("%d:",cur->vertex); + cur=cur->next; + while(cur){ + printf(" %d(%d)",cur->vertex,cur->weight); + cur=cur->next; + } + printf("\n"); + } +} + +struct Graph *read_map(char const *filename){ + FILE *map_file=fopen(filename,"r"); + int directed,nodesize; + fscanf(map_file,"%d %d\n",&directed,&nodesize); + struct Graph *map=graph_init(nodesize); + + char buff[1024]; + char *node_line=fgets(buff,sizeof(buff),map_file); + char *node=strtok(node_line," \n"); + while(node){ + graph_insertnode(map,atoi(node)); + node=strtok(NULL," \n"); + } + char *edge_line; + int node1,node2,weight; + while((edge_line=fgets(buff,sizeof(buff),map_file))){ + node=strtok(edge_line,"\t\n"); + node1=atoi(node); + node=strtok(NULL,"\t\n"); + node2=atoi(node); + node=strtok(NULL,"\t\n"); + weight=atoi(node); + graph_insertedge(map,node1,node2,weight); + if(!directed) + graph_insertedge(map,node2,node1,weight); + } + fflush(map_file); + fclose(map_file); + return map; +} \ No newline at end of file diff --git a/Map/Single-source shortest path/Topo/map.h b/Map/Single-source shortest path/Topo/map.h new file mode 100644 index 0000000..21e0737 --- /dev/null +++ b/Map/Single-source shortest path/Topo/map.h @@ -0,0 +1,22 @@ +#ifndef MAP_H +#define MAP_H + +struct Node{ + int vertex; + struct Node *next; + int weight; +}; + +struct Graph{ + struct Node **nodes; + int nodesize; + int sp; +}; + +struct Graph *graph_init(int); +void graph_insertnode(struct Graph *,int); +void graph_insertedge(struct Graph *,int,int,int); +void display_debug(struct Graph *); +struct Graph *read_map(char const *); + +#endif \ No newline at end of file diff --git a/Map/Single-source shortest path/Topo/originmap.in b/Map/Single-source shortest path/Topo/originmap.in new file mode 100644 index 0000000..783dd7b --- /dev/null +++ b/Map/Single-source shortest path/Topo/originmap.in @@ -0,0 +1,12 @@ +1 6 +0 1 2 3 4 5 +0 1 5 +0 2 3 +1 2 2 +1 3 6 +2 3 7 +2 4 4 +2 5 2 +3 4 -1 +3 5 1 +4 5 -2 diff --git a/Map/Single-source shortest path/Topo/topo.c b/Map/Single-source shortest path/Topo/topo.c new file mode 100644 index 0000000..92b1274 --- /dev/null +++ b/Map/Single-source shortest path/Topo/topo.c @@ -0,0 +1,75 @@ +#include +#include +#include +#define STACKSIZE 100 + +#include "map.h" + +int sp=-1; +int stack[STACKSIZE]; +int empty(void){ + return sp==-1; +} +void push(int new){ + stack[++sp]=new; +} +int pop(void){ + return stack[sp--]; +} +int top(void){ + return stack[sp]; +} + +void DFS_connect(struct Graph *map,int startnode,int *visited){ + if(visited[startnode]) + return; + struct Node *cur=map->nodes[startnode]->next; + while(cur){ + if(!visited[cur->vertex]) + DFS_connect(map,cur->vertex,visited); + cur=cur->next; + } + visited[startnode]=1; + push(startnode); +} + +void DFS(struct Graph *map){ + int visited[map->nodesize]; + for(int i=0;inodesize;i++) + visited[i]=0; + for(int i=0;inodesize;i++) + if(!visited[i]) + DFS_connect(map,i,visited); +} + +void topo(struct Graph *map){ + #define START 1 + DFS(map); + int curno; + int shortestpath[map->nodesize]; + for(int i=0;inodesize;i++) + shortestpath[i]=SHRT_MAX; + shortestpath[START]=0; + struct Node *cur; + while(!empty() && top()!=START) + pop(); + while(!empty()){ + curno=pop(); + cur=map->nodes[curno]->next; + while(cur){ + if(shortestpath[cur->vertex]>shortestpath[curno]+cur->weight) + shortestpath[cur->vertex]=shortestpath[curno]+cur->weight; + cur=cur->next; + } + } + for(int i=0;inodesize;i++) + printf("%d,%d\n",i,shortestpath[i]); +} + +int main(int argc, char const *argv[]){ + if(argc<2) + return 1; + struct Graph *map=read_map(argv[1]); + topo(map); + return 0; +} \ No newline at end of file diff --git a/Map/TopoSort/map.cpp b/Map/TopoSort/map.cpp new file mode 100644 index 0000000..9288811 --- /dev/null +++ b/Map/TopoSort/map.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include + +#include "map.h" + +struct Graph *graph_init(int nodesize){ + struct Graph *map=(struct Graph *)malloc(sizeof(struct Graph)); + if(!map) + return map; + map->sp=0; + map->nodesize=nodesize; + map->nodes=(struct Node **)malloc(sizeof(struct Node *)*nodesize); + return map; +} + +void graph_insertnode(struct Graph *map,int nodename){ + struct Node *newnode=(struct Node *)malloc(sizeof(struct Node)); + newnode->next=NULL; + newnode->vertex=nodename; + map->nodes[map->sp++]=newnode; +} + +void graph_insertedge(struct Graph *map,int nodename1,int nodename2){ + struct Node *node=map->nodes[nodename1]; + struct Node *newone=(struct Node *)malloc(sizeof(struct Node)); + newone->vertex=nodename2; + newone->next=node->next; + node->next=newone; +} + +void display_debug(struct Graph *map){ + int i; + struct Node *cur; + for(i=0;inodesize;i++){ + cur=map->nodes[i]; + printf("%d:",cur->vertex); + cur=cur->next; + while(cur){ + printf(" %d",cur->vertex); + cur=cur->next; + } + printf("\n"); + } +} + +struct Graph *read_map(char const *filename,map *node_hash){ + FILE *map_file=fopen(filename,"r"); + int directed,nodesize; + fscanf(map_file,"%d %d\n",&directed,&nodesize); + struct Graph *graph=graph_init(nodesize); + char buff[1024]; + char *node_line=fgets(buff,sizeof(buff),map_file); + char *node=strtok(node_line," \n"); + + int i=0; + while(node){ + (*node_hash)[i]=node; + graph_insertnode(graph,i); + node=strtok(NULL," \n"); + i++; + } + + map name2no; + for(i=0;i *); + +#endif \ No newline at end of file diff --git a/Map/TopoSort/originmap.in b/Map/TopoSort/originmap.in new file mode 100644 index 0000000..c531ad0 --- /dev/null +++ b/Map/TopoSort/originmap.in @@ -0,0 +1,14 @@ +1 7 +clanguage linux net os apue expert kernel +clanguage linux +clanguage net +clanguage os +linux os +linux apue +net expert +os net +os expert +os kernel +apue os +apue kernel +kernel expert diff --git a/Map/TopoSort/topo.cpp b/Map/TopoSort/topo.cpp new file mode 100644 index 0000000..77a8263 --- /dev/null +++ b/Map/TopoSort/topo.cpp @@ -0,0 +1,112 @@ +#include +#include +#include +#include "map.h" +#define QSIZE 100 +#define N 100 +using namespace std; + +int head,rear; +int queue[QSIZE]; +int empty(void){ + return head==rear; +} +int full(void){ + return head=(rear+1)%QSIZE; +} +void enqueue(int item){ + queue[rear]=item; + rear=(rear+1)%QSIZE; +} +int dequeue(void){ + int retval=queue[head]; + head=(head+1)%QSIZE; + return retval; +} + +void topo_base_method(struct Graph *graph,map node_hash){ + map indgrees; + int i; + struct Node *cur; + for(i=0;inodesize;i++){ + cur=graph->nodes[i]->next; + while(cur){ + if(indgrees.count(cur->vertex)) + indgrees[cur->vertex]++; + else + indgrees[cur->vertex]=1; + cur=cur->next; + } + } + + for(i=0;inodesize;i++) + if(!indgrees[i]) + enqueue(i); + + int indgree0; + while(!empty()){ + indgree0=dequeue(); + cout<nodes[indgree0]->next; + while(cur){ + indgrees[cur->vertex]--; + if(!indgrees[cur->vertex]) + enqueue(cur->vertex); + cur=cur->next; + } + } + cout<nodesize;i++){ + if(indgrees[i]){ + cout<<"有环。"< mystack; +void dfs(struct Graph *graph,int startnode,int *visited,map node_hash){ + if(visited[startnode]==2) + return; + if(visited[startnode]==1){ + cout<<"有环。"<nodes[startnode]->next; + //cout<vertex,visited,node_hash); + cur=cur->next; + } + visited[startnode]=2; + mystack.push(node_hash[startnode]); +} + +void topo_use_dfs(struct Graph *graph,map node_hash){ + int visited[N]={}; + int i; + for(i=0;inodesize;i++) + if(!visited[i]) + dfs(graph,i,visited,node_hash); + while(!mystack.empty()){ + cout< node_hash; + struct Graph *graph=read_map(argv[1],&node_hash); + display_debug(graph); + topo_base_method(graph,node_hash); + topo_use_dfs(graph,node_hash); + return 0; +} \ No newline at end of file diff --git a/PriorityQueue/PriorityQueue.c b/PriorityQueue/PriorityQueue.c new file mode 100644 index 0000000..54cb09f --- /dev/null +++ b/PriorityQueue/PriorityQueue.c @@ -0,0 +1,101 @@ +#include +#include "PriorityQueue.h" + +struct Task *create_task(int taskid,int priority){ + struct Task *task=malloc(sizeof(struct Task)); + if(task) + task->taskid=taskid,task->priority=priority; + return task; +} + +void swap_heap(heaptype heap,int pos1,int pos2){ + struct Task *tmp=heap->data[pos1]; + heap->data[pos1]=heap->data[pos2]; + heap->data[pos2]=tmp; +} + +heaptype create_heap(int capacity){ + heaptype heap=malloc(sizeof(struct Heap)); + if(!heap) + return heap; + heap->size=0,heap->capacity=capacity; + heap->data=malloc(sizeof(struct Task *)*capacity); + return heap; +} + +int empty_heap(heaptype heap){ + return heap->size==0; +} + +int full_heap(heaptype heap){ + return heap->size==heap->capacity; +} + +int gettaskbyid(heaptype heap,int id){ + int pos=-1; + for(int i=0;isize && pos==-1;i++) + if(heap->data[i]->taskid==id) + pos=i; + return pos; +} + +void hold_heap(heaptype heap,int pos){ + int left=(pos<<1)+1,right=(pos<<1)+2,minpos=pos; + if(rightsize && heap->data[right]->priority > heap->data[minpos]->priority) + minpos=right; + if(leftsize && heap->data[left]->priority > heap->data[minpos]->priority) + minpos=left; + if(minpos!=pos){ + swap_heap(heap,minpos,pos); + hold_heap(heap,minpos); + } +} + +void insert_heap(heaptype heap,struct Task *newtask){ + if(full_heap(heap)) + return ; + heap->data[heap->size]=newtask; + int currentpos=heap->size,parentpos; + heap->size+=1; + while((parentpos=(currentpos-1)>>1)>=0 && + heap->data[parentpos]->priority < heap->data[currentpos]->priority){ + swap_heap(heap,parentpos,currentpos); + currentpos=parentpos; + } +} + +void decrease_priority(heaptype heap,int taskid,int priority){ + int where=gettaskbyid(heap,taskid); + if(where==-1 || where>=heap->size || heap->data[where]->priority <= priority) + return ; + heap->data[where]->priority=priority; + hold_heap(heap,where); +} + +void increase_priority(heaptype heap,int taskid,int priority){ + int where=gettaskbyid(heap,taskid); + if(where==-1 || where>=heap->size || heap->data[where]->priority >= priority) + return ; + int currentpos=where,parentpos; + heap->data[where]->priority=priority; + while((parentpos=(currentpos-1)>>1)>=0 && + heap->data[parentpos]->priority < heap->data[currentpos]->priority){ + swap_heap(heap,parentpos,currentpos); + currentpos=parentpos; + } +} + +struct Task *delete_min(heaptype heap){ + if(empty_heap(heap)) + return NULL; + struct Task *top=heap->data[0]; + swap_heap(heap,0,heap->size-1); + heap->size-=1; + hold_heap(heap,0); + return top; +} + +void destroy_heap(heaptype heap){ + free(heap->data); + free(heap); +} diff --git a/PriorityQueue/PriorityQueue.h b/PriorityQueue/PriorityQueue.h new file mode 100644 index 0000000..d6f6751 --- /dev/null +++ b/PriorityQueue/PriorityQueue.h @@ -0,0 +1,43 @@ +#ifndef _PRIORITY_QUEUE_H +#define _PRIORITY_QUEUE_H + +#include + +struct Task{ + int taskid; + int priority; +}; + +struct Heap{ + int capacity; + int size; + struct Task **data; +}; + +typedef struct Heap *heaptype; + +void swap_heap(heaptype heap,int pos1,int pos2); + +heaptype create_heap(int capacity); + +int empty_heap(heaptype heap); + +int full_heap(heaptype heap); + +void hold_heap(heaptype heap,int pos); + +void insert_heap(heaptype heap,struct Task *newtask); + +void decrease_priority(heaptype heap,int where,int priority); + +void increase_priority(heaptype heap,int where,int priority); + +struct Task *delete_min(heaptype heap); + +void destroy_heap(heaptype heap); + +struct Task *create_task(int taskid,int priority); + +int gettaskbyid(heaptype heap,int id); + +#endif \ No newline at end of file diff --git a/PriorityQueue/main.c b/PriorityQueue/main.c new file mode 100644 index 0000000..abec911 --- /dev/null +++ b/PriorityQueue/main.c @@ -0,0 +1,27 @@ +#include +#include +#include +#include "PriorityQueue.h" +#define HEAPSIZE 100 + +int main(){ + struct Task *task; + int priority,i; + srand(time(NULL)); + heaptype heap=create_heap(HEAPSIZE); + for(i=0;i<10;i++){ + priority=i*i;//rand()%100; + task=create_task(i,priority); + insert_heap(heap,task); + } + increase_priority(heap,3,1000); + if((task=delete_min(heap))){ + printf("taskid=%d,task priority=%d\n",task->taskid,task->priority); + decrease_priority(heap,4,3); + increase_priority(heap,0,999); + } + increase_priority(heap,1,28); + while((task=delete_min(heap))) + printf("taskid=%d,task priority=%d\n",task->taskid,task->priority); + destroy_heap(heap); +} diff --git a/PriorityQueue/makefile b/PriorityQueue/makefile new file mode 100644 index 0000000..81022ab --- /dev/null +++ b/PriorityQueue/makefile @@ -0,0 +1,12 @@ +target=pg +$(target):main.o PriorityQueue.o PriorityQueue.h + gcc -o pg main.o PriorityQueue.o +main.o:main.c PriorityQueue.h + gcc -c main.c +PriorityQueue.o:PriorityQueue.c PriorityQueue.h + gcc -c PriorityQueue.c +.PHONY:clean run +clean: + -rm main.o PriorityQueue.o pg +run: + ./$(target) diff --git a/Queue/queue_twostack.c b/Queue/queue_twostack.c new file mode 100644 index 0000000..ba72ecc --- /dev/null +++ b/Queue/queue_twostack.c @@ -0,0 +1,86 @@ +#include +#define STACKSIZE 100 + +int stack_one[STACKSIZE]; +int sp_one=-1; +void push_one(int item){ + stack_one[++sp_one]=item; +} +int pop_one(void){ + return stack_one[sp_one--]; +} +int empty_one(void){ + return sp_one==-1; +} +int full_one(void){ + return sp_one==STACKSIZE-1; +} + +int stack_two[STACKSIZE]; +int sp_two=-1; +void push_two(int item){ + stack_two[++sp_two]=item; +} +int pop_two(void){ + return stack_two[sp_two--]; +} +int empty_two(void){ + return sp_two==-1; +} +int full_two(void){ + return sp_two==STACKSIZE-1; +} + +void enqueue(int item){ + if(full_queue()){ + printf("Full!\n"); + return; + } + if(!full_one()) + push_one(item); + else{ + while(!full_two() && !empty_one()) + push_two(pop_one()); + push_one(item); + } +} + +int dequeue(void){ + int retval; + if(empty_two()){ + while(!empty_one()){ + retval=pop_one(); + push_two(retval); + } + } + if(empty_two()){ + printf("Queue empty.\n"); + return -1; + }else + retval=pop_two(); + return retval; +} + +int empty_queue(){ + return empty_one() && empty_two(); +} + +int full_queue(){ + return full_one() && full_two(); +} + +int main(int argc, char const *argv[]) +{ + int data[]={1,2,3,4,5,6,7,8,9,10}; + int size=sizeof(data)/sizeof(int); + int i=0; + while(i<6) + enqueue(data[i++]); + while(!empty_queue()) + printf("%d\n",dequeue()); + while(i +#include + +struct Queue{ + int capibility; + int *data; + int head; + int rear; +}; +typedef struct Queue *qlink; + +qlink create_queue(int capibility){ + qlink queue=malloc(sizeof(struct Queue)); + if(!queue) + return queue; + queue->capibility=capibility; + queue->data=malloc(sizeof(int)*capibility); + queue->head=queue->rear=0; + return queue; +} + +int full(qlink queue){ + return queue->head==(queue->rear+1)%queue->capibility; +} + +int empty(qlink queue){ + return queue->head==queue->rear; +} + +void enqueue(qlink queue,int item){ + if(full(queue)) + return; + queue->data[queue->rear]=item; + queue->rear=(queue->rear+1)%queue->capibility; +} + +int dequeue(qlink queue){ + if(empty(queue)) + return -1; + int retval=queue->data[queue->head]; + queue->head=(queue->head+1)%queue->capibility; + return retval; +} + +int main(){ + int data[]={1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18}; + int i=0; + qlink queue=create_queue(8); + while(i<5) + enqueue(queue,data[i++]); + while(!empty(queue)) + printf("%d\n",dequeue(queue) ); + while(i<18 && !full(queue)) + enqueue(queue,data[i++]); + while(!empty(queue)) + printf("%d\n",dequeue(queue) ); + return 0; +} \ No newline at end of file diff --git a/Queue/queue_usinglist.c b/Queue/queue_usinglist.c new file mode 100644 index 0000000..b300fd3 --- /dev/null +++ b/Queue/queue_usinglist.c @@ -0,0 +1,69 @@ +#include +#include + +struct List{ + int item; + struct List *next; +}; + +struct Queue{ + int size; + struct List *head; + struct List *rear; +}; +typedef struct Queue *qlink; + +qlink create_queue(void){ + qlink queue=malloc(sizeof(struct Queue)); + if(!queue) + return queue; + queue->size=0; + queue->head=queue->rear=NULL; + return queue; +} + +int empty(qlink queue){ + return queue->size==0; +} + +void enqueue(qlink queue,int item){ + struct List *new=malloc(sizeof(struct List)); + if(!new) + return; + new->item=item; + new->next=NULL; + if(queue->head) + queue->rear->next=new; + else + queue->head=new; + queue->rear=new; + queue->size++; +} + +int dequeue(qlink queue){ + if(empty(queue)) + return -1; + int retval=queue->head->item; + struct List *torm=queue->head; + if(queue->head==queue->rear) + queue->rear=NULL; + queue->head=queue->head->next; + free(torm); + queue->size--; + return retval; +} + +int main(){ + int data[]={1,2,3,4,5,6,7,8,9,10}; + int i=0; + qlink queue=create_queue(); + while(i<5) + enqueue(queue,data[i++]); + while(!empty(queue)) + printf("%d\n",dequeue(queue) ); + while(i<10) + enqueue(queue,data[i++]); + while(!empty(queue)) + printf("%d\n",dequeue(queue) ); + return 0; +} \ No newline at end of file diff --git "a/Random/\347\264\240\346\200\247\346\265\213\350\257\225/primtest.py" "b/Random/\347\264\240\346\200\247\346\265\213\350\257\225/primtest.py" new file mode 100755 index 0000000..0a50358 --- /dev/null +++ "b/Random/\347\264\240\346\200\247\346\265\213\350\257\225/primtest.py" @@ -0,0 +1,49 @@ +#!/usr/bin/env python +#-*- encoding=utf-8 -*- + +from random import randint + +limits=2**16 + +def primtest(a,b,c):#return a**b%c + #print c + result=1 + while b>0: + #print b + if b&1: + result*=a + if a*a%c==1 and a!=1 and a!=c-1: + return 0 + a=a*a%c + b>>=1 + return result%c==1 + +def basemethod(): + primtable={}.fromkeys(range(1,limits),0) + primtable[1]=1 + for i in primtable.keys()[1:]: + if not primtable[i]: + current=i + while current+inextsibling=NULL; + tlink nodeb=(tlink)malloc(sizeof(struct Tree)); + nodeb->vertex='B'; + nodeb->firstchild=NULL; + tlink nodec=(tlink)malloc(sizeof(struct Tree)); + nodec->vertex='C'; + nodec->firstchild=NULL; + tlink noded=(tlink)malloc(sizeof(struct Tree)); + noded->vertex='D'; + noded->nextsibling=NULL; + noded->firstchild=NULL; + tree->firstchild=nodeb; + nodeb->nextsibling=nodec; + nodec->nextsibling=noded; + + tlink tree2=(tlink)malloc(sizeof(struct Tree)); + tree2->vertex='E'; + tree2->nextsibling=NULL; + tlink nodef=(tlink)malloc(sizeof(struct Tree)); + nodef->vertex='F'; + nodef->firstchild=NULL; + nodef->nextsibling=NULL; + tree2->firstchild=nodef; + + tlink tree3=(tlink)malloc(sizeof(struct Tree)); + tree3->vertex='G'; + tree3->nextsibling=NULL; + tlink nodeh=(tlink)malloc(sizeof(struct Tree)); + nodeh->firstchild=NULL; + tlink nodei=(tlink)malloc(sizeof(struct Tree)); + nodei->nextsibling=NULL; + nodeh->nextsibling=nodei; + tlink nodej=(tlink)malloc(sizeof(struct Tree)); + nodej->firstchild=nodej->nextsibling=NULL; + nodei->firstchild=nodej; + tree3->firstchild=nodeh; + nodeh->vertex='H'; + nodei->vertex='I'; + nodej->vertex='J'; + + tlink forest[3]={tree,tree2,tree3}; + printf("forest to binarytree:\n"); + btlink btree=forestconvert2btree(forest,3); + prevfix(btree); + printf("\n"); + printf("binarytree to forest:\n"); + int forestsize; + tlink *rforest=binarytree2forest(btree,&forestsize); + forestprevfix(rforest,forestsize); + return 0; +} \ No newline at end of file diff --git a/Tree/BinaryTree&Forest/makefile b/Tree/BinaryTree&Forest/makefile new file mode 100644 index 0000000..88ad64e --- /dev/null +++ b/Tree/BinaryTree&Forest/makefile @@ -0,0 +1,11 @@ +treeconvert:main.o treeconvert.o + gcc -o treeconvert main.o treeconvert.o +main.o:main.c treeconvert.h + gcc -c main.c +treeconvert.o:treeconvert.c treeconvert.h + gcc -c treeconvert.c +.PHONY:clean run +clean: + -rm treeconvert main.o treeconvert.o +run: + ./treeconvert \ No newline at end of file diff --git a/Tree/BinaryTree&Forest/treeconvert.c b/Tree/BinaryTree&Forest/treeconvert.c new file mode 100644 index 0000000..03da74c --- /dev/null +++ b/Tree/BinaryTree&Forest/treeconvert.c @@ -0,0 +1,76 @@ +#include +#include +#include "treeconvert.h" + +void prevfix(btlink root){ + if(root){ + printf("%c ", root->vertex); + prevfix(root->left); + prevfix(root->right); + } +} +void treeprevfix(tlink root){ + if(root){ + printf("%c ", root->vertex); + treeprevfix(root->firstchild); + treeprevfix(root->nextsibling); + } +} + +void forestprevfix(tlink *forest,int size){ + int i; + for(i=0;ivertex=btree->vertex; + tree->firstchild=binarytree2tree(btree->left); + tree->nextsibling=binarytree2tree(btree->right); + return tree; +} + +tlink *binarytree2forest(btlink tree,int *newforestsize){ + int sp=0; + tlink *forest=malloc(sizeof(tlink)*10); + btlink current=tree,rmright; + while(current){ + rmright=current; + current=current->right; + rmright->right=NULL; + forest[sp++]=binarytree2tree(rmright); + } + *newforestsize=sp; + return forest; +} + +btlink treeconvert2btree(tlink tree){ + if(!tree) + return NULL; + btlink newbtreenode=malloc(sizeof(struct BinaryTree)); + newbtreenode->vertex=tree->vertex; + newbtreenode->left=treeconvert2btree(tree->firstchild); + newbtreenode->right=treeconvert2btree(tree->nextsibling); + return newbtreenode; +} + +btlink forestconvert2btree(tlink *forest,int forestsize){ + if(!forest) + return NULL; + int i; + btlink btree=treeconvert2btree(forest[0]); + btlink prevsubbtree=btree; + for(i=1;iright=treeconvert2btree(forest[i]); + prevsubbtree=prevsubbtree->right; + } + return btree; +} + diff --git a/Tree/BinaryTree&Forest/treeconvert.h b/Tree/BinaryTree&Forest/treeconvert.h new file mode 100644 index 0000000..79fa23f --- /dev/null +++ b/Tree/BinaryTree&Forest/treeconvert.h @@ -0,0 +1,29 @@ +#ifndef TREECONVERT_H +#define TREECONVERT_H + +struct Tree{ + char vertex; + struct Tree *firstchild; + struct Tree *nextsibling; +}; + +struct BinaryTree{ + char vertex; + struct BinaryTree *left; + struct BinaryTree *right; +}; + +typedef struct Tree *tlink; +typedef struct BinaryTree *btlink; + +void prevfix(btlink); +void treeprevfix(tlink); +void forestprevfix(tlink *,int); + +tlink binarytree2tree(btlink); +tlink *binarytree2forest(btlink,int *); + +btlink treeconvert2btree(tlink); +btlink forestconvert2btree(tlink *,int); + +#endif \ No newline at end of file diff --git a/Tree/BinaryTree/binarytree.c b/Tree/BinaryTree/binarytree.c new file mode 100644 index 0000000..47b9879 --- /dev/null +++ b/Tree/BinaryTree/binarytree.c @@ -0,0 +1,290 @@ +#include +#include +#include "binarytree.h" +//nodestack define +static btlink stack[STACKSIZE]; +static int sp=-1; +static int empty(void){ + return sp==-1; +} +static void push(btlink item){ + stack[++sp]=item; +} +static btlink pop(void){ + return stack[sp--]; +} +static btlink top(void){ + return stack[sp]; +} +//nodestack define OK + +//statusstack define +static int stack2[STACKSIZE]; +static int sp2=-1; +static int empty2(void){ + return sp2==-1; +} +static void push2(int status){ + stack2[++sp2]=status; +} +static int pop2(void){ + return stack2[sp2--]; +} +static int top2(void){ + return stack2[sp2]; +} +static void change2(int newstatus){ + stack2[sp]=newstatus; +} +//statusstack define OK + +void insert_btree(btlink *btree,int vertex){ + btlink cur=*btree,parent=NULL; + btlink new=malloc(sizeof(struct BTree)); + new->vertex=vertex; + new->left=new->right=NULL; + if(!cur){ + *btree=new; + return; + } + while(cur){ + parent=cur; + if(cur->vertex>=vertex) + cur=cur->left; + else + cur=cur->right; + } + if(parent->vertex>=vertex) + parent->left=new; + else + parent->right=new; +} + +btlink insert_btree_recursive(btlink cur,int vertex){ + if(!cur){ + btlink new=malloc(sizeof(struct BTree)); + new->vertex=vertex; + new->left=new->right=NULL; + return new; + } + if(vertex<=cur->vertex) + cur->left=insert_btree_recursive(cur->left,vertex); + else + cur->right=insert_btree_recursive(cur->right,vertex); + return cur; +} + +btlink find_rightleftmostnode(btlink node){ + btlink cur=node->right; + btlink parent=node; + while(cur->left){ + parent=cur; + cur=cur->left; + } + if(parent->vertex>=cur->vertex) + parent->left=cur->right; + else + parent->right=cur->right; + return cur; +} + +void delete_btree(btlink *btree,int vertex){ + btlink cur=*btree,parent=NULL,target; + if(!cur) + return; + while(cur && cur->vertex!=vertex){ + parent=cur; + cur=(cur->vertex > vertex)?(cur->left):(cur->right); + } + target=cur; + if(!target) + return; + if(target->right && target->left){ + btlink leftmost=find_rightleftmostnode(target); + target->vertex=leftmost->vertex; + target=leftmost; + }else if(target->left){ + if(parent){ + if(parent->left==target) + parent->left=target->left; + else + parent->right=target->left; + }else + *btree=target->left; + }else if(target->right){ + if(parent){ + if(parent->left==target) + parent->left=target->right; + else + parent->right=target->right; + }else + *btree=target->right; + }else{ + if(parent){ + if(parent->left==target) + parent->left=NULL; + else + parent->right=NULL; + }else + *btree=NULL; + } + free(target); +} + +void prefix_recursive(btlink root){ + if(root){ + printf("%d ",root->vertex ); + prefix_recursive(root->left); + prefix_recursive(root->right); + } +} + +void infix_recursive(btlink root){ + if(root){ + infix_recursive(root->left); + printf("%d ",root->vertex ); + infix_recursive(root->right); + } +} + +void suffix_recursive(btlink root){ + if(root){ + suffix_recursive(root->left); + suffix_recursive(root->right); + printf("%d ",root->vertex ); + } +} + +void prefix_nonrecursive(btlink btree){ + printf("prefix_nonrecursive:\n"); + btlink cur=btree; + while(cur || !empty()){ + while(cur){ + printf("%d ",cur->vertex); + push(cur); + cur=cur->left; + } + if(!cur){ + cur=pop(); + cur=cur->right; + } + } + printf("\n"); +} + +void infix_nonrecursive(btlink btree){ + printf("infix_nonrecursive:\n"); + btlink cur=btree; + while(cur || !empty()){ + while(cur){ + push(cur); + cur=cur->left; + } + if(!cur){ + cur=pop(); + printf("%d ",cur->vertex ); + cur=cur->right; + } + } + printf("\n"); +} + +void suffix_nonrecursive(btlink btree){ + printf("suffix_nonrecursive:\n"); + btlink cur=btree; + int status; + push(cur);push2(0); + while(!empty()){ + status=top2(); + switch(status){ + case 0: + cur=top(); + change2(1); + if(cur->left){ + push(cur->left); + push2(0); + } + break; + case 1: + cur=top(); + change2(2); + if(cur->right){ + push(cur->right); + push2(0); + } + break; + default: + cur=pop();pop2(); + printf("%d ",cur->vertex); + } + } + printf("\n"); +} + +void destroy_tree(btlink btree){ + if(btree){ + destroy_tree(btree->left); + destroy_tree(btree->right); + free(btree); + } +} + +btlink search_btree_recursive(btlink btree,int target){ + if(!btree || btree->vertex==target) + return btree; + if(btree->vertex>target) + return search_btree_recursive(btree->left,target); + else + return search_btree_recursive(btree->right,target); +} + +btlink search_btree_nonrecursive(btlink btree,int target){ + btlink cur=btree; + btlink retval=NULL; + while(cur){ + if(cur->vertex==target){ + retval=cur; + break; + }else + cur=cur->vertex>target?cur->left:cur->right; + } + return retval; +} + +int head,rear; +#define QUEUESIZE 100 +btlink queue[QUEUESIZE]; + +int empty_queue(){ + return head==rear; +} + +int full_queue(){ + return head=(rear+1)%QUEUESIZE; +} + +void enqueue(btlink item){ + queue[rear]=item; + rear=(rear+1)%QUEUESIZE; +} + +btlink dequeue(){ + btlink retval=queue[head]; + head=(head+1)%QUEUESIZE; + return retval; +} + +void bfs(btlink tree){ + enqueue(tree); + btlink cur; + while(!empty_queue()){ + cur=dequeue(); + printf("%d ", cur->vertex); + if(cur->left) + enqueue(cur->left); + if(cur->right) + enqueue(cur->right); + } + printf("\n"); +} + diff --git a/Tree/BinaryTree/binarytree.h b/Tree/BinaryTree/binarytree.h new file mode 100644 index 0000000..df25cc8 --- /dev/null +++ b/Tree/BinaryTree/binarytree.h @@ -0,0 +1,26 @@ +#ifndef BINARYTREE_H +#define BINARYTREE_H +#define STACKSIZE 300 + +struct BTree{ + int vertex; + struct BTree *left; + struct BTree *right; +}; +typedef struct BTree *btlink; + +void insert_btree(btlink *,int); +btlink insert_btree_recursive(btlink,int); +void delete_btree(btlink *,int); +btlink search_btree_recursive(btlink,int); +btlink search_btree_nonrecursive(btlink,int); +void prefix_recursive(btlink); +void infix_recursive(btlink); +void suffix_recursive(btlink); +void prefix_nonrecursive(btlink); +void infix_nonrecursive(btlink); +void suffix_nonrecursive(btlink); +void destroy_tree(btlink); +void bfs(btlink); + +#endif \ No newline at end of file diff --git a/Tree/BinaryTree/copybinarytree.c b/Tree/BinaryTree/copybinarytree.c new file mode 100644 index 0000000..55183d7 --- /dev/null +++ b/Tree/BinaryTree/copybinarytree.c @@ -0,0 +1,62 @@ +#include +#include "binarytree.h" +#include "copybinarytree.h" +//nodestack define +static btlink stack[STACKSIZE]; +static int sp=-1; +static int empty(void){ + return sp==-1; +} +static void push(btlink item){ + stack[++sp]=item; +} +static btlink pop(void){ + return stack[sp--]; +} +static btlink top(void){ + return stack[sp]; +} +//nodestack define OK + +btlink copy_btree_recursive(btlink btree){ + if(!btree) + return NULL; + btlink root=malloc(sizeof(struct BTree)); + if(!root) + exit(1); + root->vertex=btree->vertex; + root->left=copy_btree_recursive(btree->left); + root->right=copy_btree_recursive(btree->right); + return root; +} + +btlink copy_btree_nonrecursive(btlink btree){ + btlink cur=btree; + btlink newbtree,newnode,parent=NULL; + while(cur || !empty()){ + while(cur){ + newnode=malloc(sizeof(struct BTree)); + newnode->vertex=cur->vertex; + newnode->left=newnode->right=NULL; + push(parent);push(newnode);push(cur); + parent=newnode; + cur=cur->left; + } + if(!cur){ + cur=pop(); + newnode=pop(); + parent=pop(); + if(parent){ + if(parent->vertex>=newnode->vertex) + parent->left=newnode; + else + parent->right=newnode; + }else{ + newbtree=newnode; + } + parent=newnode; + cur=cur->right; + } + } + return newbtree; +} \ No newline at end of file diff --git a/Tree/BinaryTree/copybinarytree.h b/Tree/BinaryTree/copybinarytree.h new file mode 100644 index 0000000..774560b --- /dev/null +++ b/Tree/BinaryTree/copybinarytree.h @@ -0,0 +1,7 @@ +#ifndef COPYBINARYTREE_H +#define COPYBINARYTREE_H + +btlink copy_btree_recursive(btlink btree); +btlink copy_btree_nonrecursive(btlink btree); + +#endif \ No newline at end of file diff --git a/Tree/BinaryTree/main.c b/Tree/BinaryTree/main.c new file mode 100644 index 0000000..aca2db5 --- /dev/null +++ b/Tree/BinaryTree/main.c @@ -0,0 +1,104 @@ +#include +#include "binarytree.h" +#include "copybinarytree.h" +#include "reverseisotree.h" + +void search(btlink btree,int target){ + btlink node=search_btree_recursive(btree,target); + printf("use nonrecursive:\n"); + node=search_btree_nonrecursive(btree,target); + if(!node){ + printf("No found!\n"); + return; + }else + printf("%d!\n",node->vertex ); + printf("use recursive:\n"); + node=search_btree_recursive(btree,target); + if(!node){ + printf("No found!\n"); + return; + }else + printf("find %d.\n",node->vertex ); +} + +int main(int argc, char const *argv[]){ + int data[]={8,1,19,4,3,2,5,7,6,14,3,1,16,18,12,11,14,1,5}; + int size=sizeof(data)/sizeof(int); + btlink btree=NULL; + int i=0; + while(i=0;i--){ + delete_btree(&btree,data[i]); + infix_recursive(btree);printf("\n"); + } + /* + printf("====Traversal=======\n"); + printf("prefix:\n"); + prefix_recursive(btree); + printf("\n"); + printf("infix:\n"); + infix_recursive(btree); + printf("\n"); + printf("suffix:\n"); + suffix_recursive(btree); + printf("\n"); + printf("layerfix:\n"); + bfs(btree); + + printf("=====Search======\n"); + search(btree,5); + search(btree,16); + search(btree,20); + + printf("=====CopyTree======\n"); + printf("use recursive:\n"); + btlink newbtree=copy_btree_recursive(btree); + prefix_nonrecursive(newbtree); + infix_nonrecursive(newbtree); + suffix_nonrecursive(newbtree); + printf("use nonrecursive:\n"); + btlink newbtree2=copy_btree_nonrecursive(btree); + prefix_nonrecursive(newbtree2); + infix_nonrecursive(newbtree2); + suffix_nonrecursive(newbtree2); + + printf("=====Delete======\n"); + delete_btree(&btree,6); + infix_nonrecursive(btree); + delete_btree(&btree,19); + infix_nonrecursive(btree); + delete_btree(&btree,4); + infix_nonrecursive(btree); + delete_btree(&btree,8); + infix_nonrecursive(btree); + + printf("=====DestroyTree======\n"); + destroy_tree(btree); + destroy_tree(newbtree); + destroy_tree(newbtree2); + */ + /* + btree=reverse_btree_iso(btree); + printf("====Traversal=======\n"); + printf("prefix:\n"); + prefix_recursive(btree); + printf("\n"); + printf("infix:\n"); + infix_recursive(btree); + printf("\n"); + printf("suffix:\n"); + suffix_recursive(btree); + printf("\n"); + prefix_nonrecursive(btree); + infix_nonrecursive(btree); + suffix_nonrecursive(btree); + + printf("layerfix:\n"); + bfs(btree); + */ + return 0; +} \ No newline at end of file diff --git a/Tree/BinaryTree/makefile b/Tree/BinaryTree/makefile new file mode 100644 index 0000000..48671d0 --- /dev/null +++ b/Tree/BinaryTree/makefile @@ -0,0 +1,15 @@ +tree:main.o binarytree.o copybinarytree.o reverseisotree.o + gcc -o tree main.o binarytree.o copybinarytree.o reverseisotree.o +main.o:main.c binarytree.h copybinarytree.h reverseisotree.h + gcc -c main.c +binarytree.o:binarytree.h binarytree.c + gcc -c binarytree.c +copybinarytree.o:copybinarytree.c copybinarytree.h binarytree.h + gcc -c copybinarytree.c +reverseisotree.o:reverseisotree.c reverseisotree.h binarytree.h + gcc -c reverseisotree.c +.PHONY:clean run +clean: + -rm tree main.o binarytree.o copybinarytree.o reverseisotree.o +run: + ./tree \ No newline at end of file diff --git a/Tree/BinaryTree/reverseisotree.c b/Tree/BinaryTree/reverseisotree.c new file mode 100644 index 0000000..4d9077c --- /dev/null +++ b/Tree/BinaryTree/reverseisotree.c @@ -0,0 +1,12 @@ +#include +#include "binarytree.h" +#include "reverseisotree.h" + +btlink reverse_btree_iso(btlink btree){ + if(!btree) + return NULL; + btlink tmpleft=btree->left,tmpright=btree->right; + btree->left=reverse_btree_iso(tmpright); + btree->right=reverse_btree_iso(tmpleft); + return btree; +} \ No newline at end of file diff --git a/Tree/BinaryTree/reverseisotree.h b/Tree/BinaryTree/reverseisotree.h new file mode 100644 index 0000000..6558124 --- /dev/null +++ b/Tree/BinaryTree/reverseisotree.h @@ -0,0 +1,6 @@ +#ifndef REVERSEISOTREE_H +#define REVERSEISOTREE_H + +btlink reverse_btree_iso(btlink); + +#endif \ No newline at end of file diff --git a/Tree/ExpressTree/expresstree.c b/Tree/ExpressTree/expresstree.c new file mode 100644 index 0000000..0e9118f --- /dev/null +++ b/Tree/ExpressTree/expresstree.c @@ -0,0 +1,128 @@ +#include +#include +#include + +struct ETree{ + char ch; + struct ETree *left; + struct ETree *right; +}; +typedef struct ETree *etlink; + +char stack[100]; +int sp=-1; +void push_ch(char item){ + stack[++sp]=item; +} +char pop_ch(void){ + return stack[sp--]; +} +char top_ch(void){ + return stack[sp]; +} +int empty(void){ + return sp==-1; +} +etlink stack_node[30]; +void push_node(etlink item){ + stack_node[++sp]=item; +} +etlink pop_node(void){ + return stack_node[sp--]; +} + +void display_infix(etlink root){ + if(root){ + printf("("); + display_infix(root->left); + printf("%c",root->ch); + display_infix(root->right); + printf(")"); + } +} + +void display_suffix(etlink root){ + if(root){ + display_suffix(root->left); + display_suffix(root->right); + printf("%c",root->ch); + } +} + +etlink create_expresstree(char *suffix,int sufsize){ + int i; + char ch; + etlink cur=NULL; + for(i=0;ich=ch; + cur->left=cur->right=NULL; + if(ch=='+' || ch=='-' || ch=='*' || ch=='/'){ + cur->right=pop_node(); + cur->left=pop_node(); + } + push_node(cur); + } + if(!empty()) + cur=pop_node(); + return cur; +} + +void infix2suffix(char *infix,int size,char *suffix,int *sufsize){ + int i,cp=0; + char ch; + for(i=0;i +#include +#include "binarytree.h" +//nodestack define +static btlink stack[STACKSIZE]; +static int sp=-1; +static int empty(void){ + return sp==-1; +} +static void push(btlink item){ + stack[++sp]=item; +} +static btlink pop(void){ + return stack[sp--]; +} +static btlink top(void){ + return stack[sp]; +} +//nodestack define OK + +//statusstack define +static int stack2[STACKSIZE]; +static int sp2=-1; +static int empty2(void){ + return sp2==-1; +} +static void push2(int status){ + stack2[++sp2]=status; +} +static int pop2(void){ + return stack2[sp2--]; +} +static int top2(void){ + return stack2[sp2]; +} +static void change2(int newstatus){ + stack2[sp]=newstatus; +} +//statusstack define OK + +void insert_btree(btlink *btree,int vertex){ + btlink cur=*btree,parent=NULL; + btlink new=malloc(sizeof(struct BTree)); + new->vertex=vertex; + new->left=new->right=NULL; + if(!cur){ + *btree=new; + return; + } + while(cur){ + parent=cur; + if(cur->vertex>=vertex) + cur=cur->left; + else + cur=cur->right; + } + if(parent->vertex>=vertex) + parent->left=new; + else + parent->right=new; +} + +btlink insert_btree_recursive(btlink cur,int vertex){ + if(!cur){ + btlink new=malloc(sizeof(struct BTree)); + new->vertex=vertex; + new->left=new->right=NULL; + return new; + } + if(vertex<=cur->vertex) + cur->left=insert_btree_recursive(cur->left,vertex); + else + cur->right=insert_btree_recursive(cur->right,vertex); + return cur; +} + +btlink find_rightleftmostnode(btlink node){ + btlink cur=node->right; + btlink parent=node; + while(cur->left){ + parent=cur; + cur=cur->left; + } + if(parent->vertex>=cur->vertex) + parent->left=NULL; + else + parent->right=NULL; + return cur; +} + +void delete_btree(btlink *btree,int vertex){ + btlink cur=*btree,parent=NULL,target=NULL; + if(!cur) + return; + while(cur){ + if(cur->vertex==vertex){ + target=cur; + break; + }else{ + parent=cur; + cur=(cur->vertex>vertex)?(cur->left):(cur->right); + } + } + if(!target){ + printf("No such a node!\n"); + return; + } + if(!target->left && !target->right){ + if(parent){ + if(parent->left==target){ + parent->left=NULL; + }else{ + parent->right=NULL; + } + }else{ + *btree=NULL; + } + }else if(target->right){ + btlink leftmost=find_rightleftmostnode(target); + target->vertex=leftmost->vertex; + target=leftmost; + }else{ + if(parent){ + if(parent->left==target){ + parent->left=target->left; + }else{ + parent->right=target->left; + } + }else{ + *btree=target->left; + } + } + free(target); +} + +void prefix_recursive(btlink root){ + if(root){ + printf("%d ",root->vertex ); + prefix_recursive(root->left); + prefix_recursive(root->right); + } +} + +void infix_recursive(btlink root){ + if(root){ + infix_recursive(root->left); + printf("%d ",root->vertex ); + infix_recursive(root->right); + } +} + +void suffix_recursive(btlink root){ + if(root){ + suffix_recursive(root->left); + suffix_recursive(root->right); + printf("%d ",root->vertex ); + } +} + +void prefix_nonrecursive(btlink btree){ + printf("prefix_nonrecursive:\n"); + btlink cur=btree; + while(cur || !empty()){ + while(cur){ + printf("%d ",cur->vertex); + push(cur); + cur=cur->left; + } + if(!cur){ + cur=pop(); + cur=cur->right; + } + } + printf("\n"); +} + +void infix_nonrecursive(btlink btree){ + printf("infix_nonrecursive:\n"); + btlink cur=btree; + while(cur || !empty()){ + while(cur){ + push(cur); + cur=cur->left; + } + if(!cur){ + cur=pop(); + printf("%d ",cur->vertex ); + cur=cur->right; + } + } + printf("\n"); +} + +void suffix_nonrecursive(btlink btree){ + printf("suffix_nonrecursive:\n"); + btlink cur=btree; + int status; + push(cur);push2(0); + while(!empty()){ + status=top2(); + switch(status){ + case 0: + cur=top(); + change2(1); + if(cur->left){ + push(cur->left); + push2(0); + } + break; + case 1: + cur=top(); + change2(2); + if(cur->right){ + push(cur->right); + push2(0); + } + break; + default: + cur=pop();pop2(); + printf("%d ",cur->vertex); + } + } + printf("\n"); +} + +void destroy_tree(btlink btree){ + if(btree){ + destroy_tree(btree->left); + destroy_tree(btree->right); + free(btree); + } +} + +btlink search_btree_recursive(btlink btree,int target){ + if(!btree || btree->vertex==target) + return btree; + if(btree->vertex>target) + return search_btree_recursive(btree->left,target); + else + return search_btree_recursive(btree->right,target); +} + +btlink search_btree_nonrecursive(btlink btree,int target){ + btlink cur=btree; + btlink retval=NULL; + while(cur){ + if(cur->vertex==target){ + retval=cur; + break; + }else + cur=cur->vertex>target?cur->left:cur->right; + } + return retval; +} + +int head,rear; +#define QUEUESIZE 100 +btlink queue[QUEUESIZE]; + +int empty_queue(){ + return head==rear; +} + +int full_queue(){ + return head=(rear+1)%QUEUESIZE; +} + +void enqueue(btlink item){ + queue[rear]=item; + rear=(rear+1)%QUEUESIZE; +} + +btlink dequeue(){ + btlink retval=queue[head]; + head=(head+1)%QUEUESIZE; + return retval; +} + +void bfs(btlink tree){ + enqueue(tree); + btlink cur; + while(!empty_queue()){ + cur=dequeue(); + printf("%d ", cur->vertex); + if(cur->left) + enqueue(cur->left); + if(cur->right) + enqueue(cur->right); + } + printf("\n"); +} + diff --git a/Tree/LCA/binarytree.h b/Tree/LCA/binarytree.h new file mode 100644 index 0000000..df25cc8 --- /dev/null +++ b/Tree/LCA/binarytree.h @@ -0,0 +1,26 @@ +#ifndef BINARYTREE_H +#define BINARYTREE_H +#define STACKSIZE 300 + +struct BTree{ + int vertex; + struct BTree *left; + struct BTree *right; +}; +typedef struct BTree *btlink; + +void insert_btree(btlink *,int); +btlink insert_btree_recursive(btlink,int); +void delete_btree(btlink *,int); +btlink search_btree_recursive(btlink,int); +btlink search_btree_nonrecursive(btlink,int); +void prefix_recursive(btlink); +void infix_recursive(btlink); +void suffix_recursive(btlink); +void prefix_nonrecursive(btlink); +void infix_nonrecursive(btlink); +void suffix_nonrecursive(btlink); +void destroy_tree(btlink); +void bfs(btlink); + +#endif \ No newline at end of file diff --git a/Tree/LCA/tarjan.c b/Tree/LCA/tarjan.c new file mode 100644 index 0000000..f5e7f59 --- /dev/null +++ b/Tree/LCA/tarjan.c @@ -0,0 +1,151 @@ +#include +#include +#include "binarytree.h" + +struct Set{ + int vertex; + struct Set *parent; +}; +typedef struct Set *slink; + +struct Query{ + int test1; + int test2; + char ok; +}; + +void search(btlink btree,int target){ + btlink node=search_btree_recursive(btree,target); + printf("use nonrecursive:\n"); + node=search_btree_nonrecursive(btree,target); + if(!node){ + printf("No found!\n"); + return; + }else + printf("%d!\n",node->vertex ); + printf("use recursive:\n"); + node=search_btree_recursive(btree,target); + if(!node){ + printf("No found!\n"); + return; + }else + printf("find %d.\n",node->vertex ); +} + +int find_set(slink *sets,int member){ + slink current=sets[member],parent=current->parent; + while(current!=parent){ + current=parent; + parent=current->parent; + } + return parent->vertex; +} + +void union_set(slink *sets,int member1,int member2){ + int set1=find_set(sets,member1),set2=find_set(sets,member2); + sets[set2]->parent=sets[set1]; +} + +void tartjan_dfs(btlink btree,int *visited,slink *sets,int (*query)[2]){ + if(!btree) + return; + tartjan_dfs(btree->left,visited,sets,query); + if(btree->left){ + union_set(sets,btree->vertex,btree->left->vertex); + sets[find_set(sets,btree->vertex)]->parent=sets[btree->vertex]; + } + tartjan_dfs(btree->right,visited,sets,query); + if(btree->right){ + union_set(sets,btree->vertex,btree->right->vertex); + sets[find_set(sets,btree->vertex)]->parent=sets[btree->vertex]; + } + visited[btree->vertex]=1; + int query_obj; + for(int i=0;i<8;i++){ + if(query[i][0]==btree->vertex && visited[query[i][1]]) + printf("%d,%d LCA=%d\n",btree->vertex,query[i][1],find_set(sets,query[i][1])); + else if(query[i][1]==btree->vertex && visited[query[i][0]]) + printf("%d,%d LCA=%d\n",btree->vertex,query[i][0],find_set(sets,query[i][0])); + } +} + +int lca_binarysearchtree_only(btlink tree,int one,int other){ + btlink cur=tree; + int retval=-1; + if(one>other){ + int tmp=one; + one=other; + other=tmp; + } + while(cur && retval==-1){ + if(onevertex && othervertex) + cur=cur->left; + else if(one>cur->vertex && other>cur->vertex) + cur=cur->right; + else if(one<=cur->vertex && other>=cur->vertex) + retval=cur->vertex; + } + return retval; +} + +void method_only_binarysearchtree(btlink tree,int (*query)[2]){ + for(int i=0;i<8;i++) + printf("%d,%d LCA=%d\n",query[i][0],query[i][1],lca_binarysearchtree_only(tree,query[i][0],query[i][1])); +} + +int lca_fool(btlink tree,int one,int other){ + btlink cur=tree; + int visited[20]={}; + while(cur){ + visited[cur->vertex]=1; + if(cur->vertexright; + else if(cur->vertex>one) + cur=cur->left; + else + break; + } + cur=tree; + int retval=-1; + while(cur){ + if(visited[cur->vertex]) + retval=cur->vertex; + if(cur->vertexright; + else if(cur->vertex>other) + cur=cur->left; + else + break; + } + return retval; +} + + +void method_fool(btlink tree,int (*query)[2]){ + for(int i=0;i<8;i++) + printf("%d,%d LCA=%d\n",query[i][0],query[i][1],lca_fool(tree,query[i][0],query[i][1])); +} + +int main(int argc, char const *argv[]){ + int data[]={8,1,19,4,2,7,6,3,16,18,12,11,14,5}; + int size=sizeof(data)/sizeof(int); + btlink btree=NULL; + int i=0; + slink sets[20]; + int visited[20]; + while(ivertex=data[i]; + sets[data[i]]->parent=sets[data[i]]; + visited[data[i]]=0; + btree=insert_btree_recursive(btree,data[i++]); + } + int query[8][2]={{1,3},{2,5},{3,8},{11,14},{11,16},{14,18},{5,19},{4,14}}; + printf("cool way:\n"); + tartjan_dfs(btree,visited,sets,query); + printf("only binarysearchtree:\n"); + method_only_binarysearchtree(btree,query); + printf("fool:\n"); + method_fool(btree,query); + return 0; +} \ No newline at end of file diff --git "a/Tree/LCA/\343\200\220POJ1330\343\200\221\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210\357\274\210LCA\357\274\211\357\274\232\345\271\266\346\237\245\351\233\206+\346\267\261\346\220\234 - cxllyg\347\232\204\344\270\223\346\240\217 - \345\215\232\345\256\242\351\242\221\351\201\223 - CSDN.NET.pdf" "b/Tree/LCA/\343\200\220POJ1330\343\200\221\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210\357\274\210LCA\357\274\211\357\274\232\345\271\266\346\237\245\351\233\206+\346\267\261\346\220\234 - cxllyg\347\232\204\344\270\223\346\240\217 - \345\215\232\345\256\242\351\242\221\351\201\223 - CSDN.NET.pdf" new file mode 100644 index 0000000..90462cb Binary files /dev/null and "b/Tree/LCA/\343\200\220POJ1330\343\200\221\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210\357\274\210LCA\357\274\211\357\274\232\345\271\266\346\237\245\351\233\206+\346\267\261\346\220\234 - cxllyg\347\232\204\344\270\223\346\240\217 - \345\215\232\345\256\242\351\242\221\351\201\223 - CSDN.NET.pdf" differ diff --git a/Tree/TrieTree/trieTree.c b/Tree/TrieTree/trieTree.c new file mode 100644 index 0000000..ce76fc1 --- /dev/null +++ b/Tree/TrieTree/trieTree.c @@ -0,0 +1,135 @@ +#include +#include +#include +#define N 23 + +struct Tnode{ + int value; + struct Tnode *next[26]; +}; + +typedef struct Tnode *tlink; + +void init_pointer_array(tlink node){ + int i; + for(i=0;i<26;i++) + node->next[i]=NULL; +} + +tlink create_tree(){ + tlink root=(tlink)malloc(sizeof(struct Tnode)); + if(!root) + return NULL; + root->value=-1; + init_pointer_array(root); + return root; +} + +void insert_node(tlink root,char *name,int value){ + tlink now=root; + tlink new; + int sp=0; + int size=strlen(name); + while(spnext[name[sp]-'a'])){ + new=(tlink)malloc(sizeof(struct Tnode)); + if(!new) + return; + new->value=-1; + init_pointer_array(new); + now->next[name[sp]-'a']=new; + } + now=now->next[name[sp]-'a']; + sp++; + } + now->value=value; +} + +void insert_node_r(tlink root,char *name,int step,int size,int value){ + if(step==size) + return; + char ch=name[step]; + tlink cur=root->next[ch-'a']; + if(!cur){ + tlink new=(tlink)malloc(sizeof(struct Tnode)); + if(!new) + return; + root->next[ch-'a']=new; + init_pointer_array(new); + new->value=-1; + } + if(step==size-1) + root->next[ch-'a']->value=value; + insert_node_r(root->next[ch-'a'],name,step+1,size,value); +} + +int search_tree(tlink root,char *name){ + int size=strlen(name); + char ch; + int retval=-1; + tlink now=root; + for(int i=0;inext[ch-'a']; + if(!now) + return -1; + } + retval=now->value; + return retval; +} + +int search_tree_r(tlink root,char *name,int step,int size){ + if(step>size-1) + return -1; + char ch=name[step]; + tlink cur=root->next[ch-'a']; + if(!cur) + return -1; + if(step==size-1) + return cur->value; + return search_tree_r(root->next[ch-'a'],name,step+1,size); +} + +void destory_tree(tlink root){ + int i; + if(!root) + return; + for(i=0;i<26;i++) + destory_tree(root->next[i]); + free(root); +} + +int main(int argc, char const *argv[]) +{ + char *string[]={ + "leechanx","lichenxuan","leechany","leechlian", + "cong","congx","congxiaol","leechanxuan","liche", + "zhangyumao","zhangyuzhou","zhanghongli","zhangweizhe", + "zhai","alibaba","tencent","baidu","wangyi", + "souhu","macos","windows","unix","linux", + }; + int value[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23}; + tlink trietree=create_tree(); + for(int i=0;i +#include + +int sp; + +int findposition_infix(int *seq,int head,int rear,int target){ + int i; + for(i=rear-1;i>=0;i--)//一定要逆序查找,因为有权值相同的结点,如根节点=1,在中序里,这个1应该是是最后一个1 + if(seq[i]==target) + return i; + return -1; +} + +void create_prefix(int *infix,int *suffix,int infixhead, + int infixrear,int suffixhead,int suffixrear, + int *prefix){ + if(suffixrear-suffixhead<1) + return; + int root_v=suffix[suffixrear-1]; + prefix[sp++]=root_v; + + int root_position_infix=findposition_infix(infix,infixhead,infixrear,root_v); + int split_suffix=root_position_infix-infixhead+suffixhead; + + //leftsubtree: + create_prefix(infix,suffix,infixhead,root_position_infix,suffixhead,split_suffix,prefix); + //rightsubtree: + create_prefix(infix,suffix,root_position_infix+1,infixrear,split_suffix,suffixrear-1,prefix); +} + +int main(int argc, char const *argv[]) +{ + int infix[]={1,1,1,2,3,3,4,5,5,6,7,8,11,12,14,14,16,18,19}; + int suffix[]={1,1,3,2,3,5,6,7,5,4,1,11,14,12,18,16,14,19,8}; + int size=sizeof(suffix)/sizeof(int); + int *prefix=malloc(sizeof(int)*size); + create_prefix(infix,suffix,0,size,0,size,prefix); + printf("prefix:\n"); + int i; + for(i=0;i +#include + +struct BTree{ + int vertex; + struct BTree *left; + struct BTree *right; +}; +typedef struct BTree *btlink; + +int findposition_infix(int *infix,int head,int rear,int target){ + int i; + for(i=rear-1;i>=0;i--)//一定要逆序查找,因为有权值相同的结点,如根节点=1,在中序里,这个1应该是是最后一个1 + if(infix[i]==target) + return i; + return -1; +} + +btlink create_binarytree(int *infix,int *suffix, + int infixhead,int infixrear,int suffixhead,int suffixrear){ + if(infixrear-infixhead<1) + return NULL; + btlink new=malloc(sizeof(struct BTree)); + int root_v=suffix[suffixrear-1]; + new->vertex=root_v; + int root_position_infix=findposition_infix(infix,infixhead,infixrear,root_v); + + int split_suffix=suffixhead+root_position_infix-infixhead; + + new->left=create_binarytree(infix,suffix,infixhead,root_position_infix,suffixhead,split_suffix); + new->right=create_binarytree(infix,suffix,root_position_infix+1,infixrear,split_suffix,suffixrear-1); + return new; +} + +void prefix(btlink root){ + if(root){ + printf("%d ",root->vertex ); + prefix(root->left); + prefix(root->right); + } +} + +int main(int argc, char const *argv[]) +{ + int infix[]={1,1,1,2,3,3,4,5,5,6,7,8,11,12,14,14,16,18,19}; + int suffix[]={1,1,3,2,3,5,6,7,5,4,1,11,14,12,18,16,14,19,8}; + int size=sizeof(suffix)/sizeof(int); + btlink btree=create_binarytree(infix,suffix,0,size,0,size); + printf("prefix:\n"); + prefix(btree); + printf("\n"); + return 0; +} \ No newline at end of file diff --git "a/Tree/\345\211\215\347\274\200\344\270\255\347\274\200\346\261\202\345\220\216\347\274\200/evalsuffix_byarray.c" "b/Tree/\345\211\215\347\274\200\344\270\255\347\274\200\346\261\202\345\220\216\347\274\200/evalsuffix_byarray.c" new file mode 100644 index 0000000..ca9fae0 --- /dev/null +++ "b/Tree/\345\211\215\347\274\200\344\270\255\347\274\200\346\261\202\345\220\216\347\274\200/evalsuffix_byarray.c" @@ -0,0 +1,45 @@ +#include +#include + +int sp; + +int findposition_infix(int *seq,int head,int rear,int target){ + int i; + for(i=rear-1;i>=0;i--)//一定要逆序查找,因为有权值相同的结点,如根节点=1,在中序里,这个1应该是是最后一个1 + if(seq[i]==target) + return i; + return -1; +} + +void create_suffix(int *prefix,int *infix,int prefixhead, + int prefixrear,int infixhead,int infixrear, + int *suffix){ + if(prefixrear-prefixhead<1) + return; + int root_v=prefix[prefixhead]; + suffix[sp--]=root_v; + + int root_position_infix=findposition_infix(infix,infixhead,infixrear,root_v); + int split_prefix=root_position_infix-infixhead+prefixhead+1; + + //rightsubtree: + create_suffix(prefix,infix,split_prefix,prefixrear,root_position_infix+1,infixrear,suffix); + //leftsubtree: + create_suffix(prefix,infix,prefixhead+1,split_prefix,infixhead,root_position_infix,suffix); +} + +int main(int argc, char const *argv[]) +{ + int prefix[]={8,1,1,1,4,3,2,3,5,5,7,6,19,14,12,11,14,16,18}; + int infix[]={1,1,1,2,3,3,4,5,5,6,7,8,11,12,14,14,16,18,19}; + int size=sizeof(prefix)/sizeof(int); + int *suffix=malloc(sizeof(int)*size); + sp=size-1; + create_suffix(prefix,infix,0,size,0,size,suffix); + printf("suffix:\n"); + int i; + for(i=0;i +#include + +struct BTree{ + int vertex; + struct BTree *left; + struct BTree *right; +}; +typedef struct BTree *btlink; + +int findposition_infix(int *seq,int head,int rear,int target){ + int i; + for(i=rear-1;i>=0;i--)//一定要逆序查找,因为有权值相同的结点,如根节点=1,在中序里,这个1应该是是最后一个1 + if(seq[i]==target) + return i; + return -1; +} + +btlink create_binarytree(int *prefix,int *infix,int prefixhead, + int prefixrear,int infixhead,int infixrear){ + if(prefixrear-prefixhead<1) + return NULL; + btlink new=malloc(sizeof(struct BTree)); + int root_v=prefix[prefixhead]; + new->vertex=root_v; + int root_position_infix=findposition_infix(infix,infixhead,infixrear,root_v); + //int leftsubtree_size=root_position_infix-infixhead; + int split_prefix=root_position_infix-infixhead+prefixhead+1; + new->left=create_binarytree(prefix,infix,prefixhead+1,split_prefix,infixhead,root_position_infix); + new->right=create_binarytree(prefix,infix,split_prefix,prefixrear,root_position_infix+1,infixrear); + return new; +} + +void suffix(btlink root){ + if(root){ + suffix(root->left); + suffix(root->right); + printf("%d ",root->vertex ); + } +} + +int main(int argc, char const *argv[]) +{ + int prefix[]={8,1,1,1,4,3,2,3,5,5,7,6,19,14,12,11,14,16,18}; + int infix[]={1,1,1,2,3,3,4,5,5,6,7,8,11,12,14,14,16,18,19}; + int size=sizeof(prefix)/sizeof(int); + btlink btree=create_binarytree(prefix,infix,0,size,0,size); + printf("suffix:\n"); + suffix(btree); + printf("\n"); + return 0; +} \ No newline at end of file diff --git "a/Tree/\346\261\202\346\240\221\351\253\230\346\267\261/main.c" "b/Tree/\346\261\202\346\240\221\351\253\230\346\267\261/main.c" new file mode 100644 index 0000000..74fba39 --- /dev/null +++ "b/Tree/\346\261\202\346\240\221\351\253\230\346\267\261/main.c" @@ -0,0 +1,134 @@ +#include +#include + +struct Node{ + int vertex; + int depth; + struct Node *left; + struct Node *right; +}; + +void insertnode(struct Node **treep,int vertex){ + struct Node *new=malloc(sizeof(struct Node)); + if(!new) + exit(1); + new->vertex=vertex; + new->depth=0; + new->left=new->right=NULL; + if(!*treep){ + *treep=new; + return ; + } + struct Node *tree=*treep,*parent=NULL; + while(tree){ + parent=tree; + tree=tree->vertex>=vertex?tree->left:tree->right; + //稳定版本应该是tree=tree->vertex>vertex?tree->left:tree->right; + } + if(parent->vertex>=vertex) + parent->left=new; + else + parent->right=new; +} + +void prefix(struct Node *tree){ + if(tree){ + printf("%d ",tree->vertex); + prefix(tree->left); + prefix(tree->right); + } +} + +struct Node *stack[100]; +int sp=-1; +void push(struct Node *item){ + stack[++sp]=item; +} +struct Node *pop(void){ + return stack[sp--]; +} +int empty(void){ + return sp==-1; +} + +int getdepth(struct Node *tree){ + struct Node *cur=tree; + int curdepth=0,depth=0; + while(cur || !empty()){ + while(cur){ + cur->depth=curdepth++; + push(cur); + if(cur->depth>depth) + depth=cur->depth; + cur=cur->left; + } + if(!cur){ + cur=pop(); + curdepth=cur->depth+1; + cur=cur->right; + } + } + return depth; +} + +#define QSIZE 100 +struct Node *queue[QSIZE]; +int head,rear; +int qempty(void){ + return head==rear; +} +int qfull(void){ + return (rear+1)%QSIZE==head; +} +void enqueue(struct Node *item){ + if(qfull()){ + printf("Error:almost full.\n"); + return ; + } + queue[rear]=item; + rear=(rear+1)%QSIZE; +} +struct Node *dequeue(void){ + if(qempty()) + return NULL; + struct Node *ret=queue[head]; + head=(head+1)%QSIZE; + return ret; +} + +int getdepth_cuo(struct Node *tree){ + int depth=0; + if(!tree) + return depth; + tree->depth=0; + enqueue(tree); + struct Node *cur; + while(!qempty()){ + cur=dequeue(); + if(cur->depth>depth) + depth=cur->depth; + if(cur->left){ + cur->left->depth=cur->depth+1; + enqueue(cur->left); + } + if(cur->right){ + cur->right->depth=cur->depth+1; + enqueue(cur->right); + } + } + return depth; +} + +int main(int argc, char const *argv[]) +{ + int data[]={7,10,2,8,4,8,6,1,9,11,3,5}; + int size=sizeof data/sizeof *data; + struct Node *tree=NULL; + for(int i=0;i +#include + +void method1(){ + int a=1; + char *p=&a; + if(*p) + printf("小端\n"); + else + printf("大端\n"); +} + +union Test{ + short a; + char b; +}; + +void method2(){ + union Test test; + test.a=1; + if(test.b) + printf("小端\n"); + else + printf("大端\n"); +} + +void method3(){ + unsigned short limit=(USHRT_MAX+1)/2; + unsigned short a=0; + char name[1]; + printf("输入2字节字符串!\n"); + gets(name); + if(a>=limit) + printf("大端\n"); + else + printf("小端\n"); +} + +main(){ + method1(); + method2(); + method3(); +} diff --git "a/\345\244\247\346\225\260\346\215\256\351\207\217\357\274\214\346\265\267\351\207\217\346\225\260\346\215\256 \345\244\204\347\220\206\346\226\271\346\263\225\346\200\273\347\273\223-\346\227\245\345\277\227\345\210\206\344\272\253.pdf" "b/\345\244\247\346\225\260\346\215\256\351\207\217\357\274\214\346\265\267\351\207\217\346\225\260\346\215\256 \345\244\204\347\220\206\346\226\271\346\263\225\346\200\273\347\273\223-\346\227\245\345\277\227\345\210\206\344\272\253.pdf" new file mode 100644 index 0000000..f005f0a Binary files /dev/null and "b/\345\244\247\346\225\260\346\215\256\351\207\217\357\274\214\346\265\267\351\207\217\346\225\260\346\215\256 \345\244\204\347\220\206\346\226\271\346\263\225\346\200\273\347\273\223-\346\227\245\345\277\227\345\210\206\344\272\253.pdf" differ