-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 753b1ad
Showing
209 changed files
with
11,520 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <stdio.h> | ||
#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;i<N;i++) | ||
if(answer[i]){ | ||
myvalue+=value[i]; | ||
myweight+=weight[i]; | ||
} | ||
if(myvalue>maxvalue){ | ||
maxvalue=myvalue; | ||
weighted=myweight; | ||
for(int i=0;i<N;i++) | ||
printf("%d",answer[i]); | ||
printf("\n"); | ||
} | ||
} | ||
|
||
int condition(int *weight,int *answer,int step,int select){ | ||
int i; | ||
int myweight=0; | ||
for(i=0;i<step;i++) | ||
if(answer[i]) | ||
myweight+=weight[i]; | ||
if(select) | ||
myweight+=weight[step]; | ||
return myweight<=M; | ||
} | ||
|
||
void backtrack(int *weight,int *value,int *answer,int step){ | ||
if(step==N) | ||
display_outcome(answer,weight,value); | ||
else{ | ||
int i; | ||
for(i=0;i<2;i++) | ||
if(condition(weight,answer,step,i)){ | ||
answer[step]=i; | ||
backtrack(weight,value,answer,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}; | ||
int answer[N]; | ||
backtrack(weight,value,answer,0); | ||
printf("maxvalue=%d\n",maxvalue); | ||
printf("weighted=%d\n",weighted); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include <stdio.h> | ||
#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;i<N;i++) | ||
if(answer[i]){ | ||
myvalue+=value[i]; | ||
myweight+=weight[i]; | ||
} | ||
if(myvalue>maxvalue){ | ||
maxvalue=myvalue; | ||
weighted=myweight; | ||
for(int i=0;i<N;i++) | ||
printf("%d",answer[i]); | ||
printf("\n"); | ||
} | ||
} | ||
|
||
int condition(int *weight,int *answer,int step,int select){ | ||
int i; | ||
int myweight=0; | ||
for(i=0;i<step;i++) | ||
if(answer[i]) | ||
myweight+=weight[i]; | ||
if(select) | ||
myweight+=weight[step]; | ||
return myweight<=M; | ||
} | ||
|
||
void backtrack(int *weight,int *value){ | ||
int answer[N]; | ||
int record[N]; | ||
int step=0; | ||
record[step]=-1; | ||
while(step>=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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#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;i<NODESIZE;i++) | ||
if(map[node][i]) | ||
neibors[counter++]=i; | ||
*size=counter; | ||
} | ||
|
||
void displayoutcome(int *answer){ | ||
int i; | ||
for(i=0;i<NODESIZE;i++) | ||
printf("%d ", answer[i]); | ||
printf("%d\n",STARTNODE ); | ||
} | ||
|
||
int inme(struct Node *node,int who){ | ||
int ok=0; | ||
while(node && !ok){ | ||
if(node->vertex==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;i<neiborsize;i++) | ||
if(!inme(currentnode,neibors[i])){ | ||
node=malloc(sizeof(struct Node)); | ||
node->height=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;i<neiborsize;i++) | ||
if(!inme(currentnode,neibors[i])){ | ||
node=malloc(sizeof(struct Node)); | ||
node->vertex=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<neiborsize;i++) | ||
if(!visited[myneibor[i]]){ | ||
answer[step]=myneibor[i]; | ||
visited[myneibor[i]]=1; | ||
backtrace(map,answer,step+1,visited); | ||
visited[myneibor[i]]=0; | ||
} | ||
} | ||
|
||
int main(){ | ||
int map[NODESIZE][NODESIZE]={ | ||
{0,1,0,0,0,1,1}, | ||
{1,0,0,0,0,1,0}, | ||
{0,0,0,1,1,0,0}, | ||
{0,0,1,0,1,0,1}, | ||
{0,0,1,1,0,1,0}, | ||
{1,1,0,0,1,0,0}, | ||
{1,0,0,1,0,0,0} | ||
}; | ||
int answer[NODESIZE]; | ||
answer[0]=STARTNODE; | ||
int visited[NODESIZE]={}; | ||
visited[STARTNODE]=1; | ||
printf("use backtrace:\n"); | ||
backtrace(map,answer,1,visited); | ||
printf("use DFS:\n"); | ||
dfs_search(map); | ||
printf("use BFS:\n"); | ||
bfs_search(map); | ||
} |
Oops, something went wrong.