//The function that implements Bellman-Ford algorithm. void BellmanFord(struct Graph* graph, int src) { int V = graph->V; int E = graph->E; int dist[V]; //STEP 1: Initialize distances from source to all other vertices as INFINITE for (int i = 0; i < V; i++) dist[i] = INT_MAX; dist[src] = 0; //STEP 2: Relax all edges |V| - 1 times. A simple shortest path from source to any other vertex can have maximum |V| - 1 edges for (int i = 1; i <= V-1; i++) { for (int j = 0; j < E; j++) { int u = graph->edge[j].source; int v = graph->edge[j].dest; int weight = graph->edge[j].weight; if (dist[u] + weight < dist[v]) dist[v] = dist[u] + weight; } } //STEP 3: Check for negative-weight cycles. for (int i = 0; i < E; i++) { int u = graph->edge[i].source; int v = graph->edge[i].dest; int weight = graph->edge[i].weight; if (dist[u] + weight < dist[v]) cout<<"\nGraph contains negative weight cycle\n"; } printsoln(dist, V); return; }
static int search(struct dlx *solver, int k) { /* if (k == 21) */ /* return 1; */ int solutions = 0; /* printf("k = %d\n", k); */ struct node *r, *j; if (solver->h.next == &solver->h) { printsoln(solver, k); return 1; } struct column *c = choosecol(solver); if (c->len == 0) { printf("X\n"); uncover(solver, c); return 0; } /* printf("CHOOSE %s\n", c->name); */ int xx = cover(solver, c); if (xx) { printf("Got a zero\n"); return 0; } for (r = c->node.d; r != &c->node; r = r->d) { solver->solnodes[k] = r; for (j = r->r; j != r; j = j->r) { cover(solver, j->c); } int rc = search(solver, k+1); solutions += search(solver, k+1); if (k == 0) { printf("Solutions increased by %d to %d\n", rc, solutions); } for (j = r->l; j != r; j = j->l) { uncover(solver, j->c); } if (solutions >= 1) break; } uncover(solver, c); return solutions; }
int main (void) { int peg_A[8]; int peg_B[8]; int peg_C[8]; int disks; int choice; int index_from; char choice_from; char choice_to; int n_discs; peg my_pegs[3]; printf("Instructions on playing TOWERS OF HANOI:\nThere are\nthree pegs, labeled A, B, and C.\n\nInitially, peg A contains a certain\namount of disks, each one with a different size.\nThe disks are \nstacked\nin increasing size so that a disk is always on top of a larger one,\nforming a tower.\nThe goal of the game is to move all the disks from peg\nA to peg C.\nYou may move only one disk at a time.\n\nYou may move the\ntop disk from any peg to the top of the stack at another peg.\nThe\nonly\nlimitation is that you may not place a disk on top of one which is\nsmaller.\nYOU MAY STOP THE GAME ANYTIME BY ENTERING Q (THEN PRESSING \nTHE\nENTER KEY)"); get_number_of_disks(&disks); initialize(disks, peg_A, peg_B, peg_C); print_status(disks, peg_A, peg_B, peg_C); while (peg_C[disks-1]!=3) { validate_move_from(&choice_from, peg_A, peg_B, peg_C); if(choice_from=='Q' || choice_from=='q') { printf("1.print sol'n\n"); //sleep(10); printf("2.Display sol'n\n"); printf("3.Exit\n"); scanf("%d",&choice); switch(choice) { case 1: printsoln(disks); break; case 2: initscr(); /* Start curses mode */ cbreak(); /* Line buffering disabled. Pass on every thing */ keypad(stdscr, TRUE); curs_set(FALSE); print_in_middle(0, LINES / 2, COLS, welcome_string, NULL); scanw("%d", &n_discs); timeout(TIME_OUT); noecho(); store_n_discs = n_discs; init_pegs(my_pegs, n_discs); show_pegs(stdscr, my_pegs, n_discs); solve_hanoi(my_pegs, n_discs, 0, 1, 2); free_pegs(my_pegs, n_discs); endwin(); /* End curses mode */ break; case 3: break; } break; } validate_move_to(disks, choice_from, &choice_to,&index_from, peg_A, peg_B, peg_C); if(choice_to=='Q' || choice_to=='q') { printf("1.print sol'n\n"); //sleep(10); printf("2.Display sol'n\n"); printf("3.Exit\n"); scanf("%d",&choice); switch(choice) { case 1: printsoln(disks); break; case 2: initscr(); /* Start curses mode */ cbreak(); /* Line buffering disabled. Pass on every thing */ keypad(stdscr, TRUE); curs_set(FALSE); print_in_middle(0, LINES / 2, COLS, welcome_string, NULL); scanw("%d", &n_discs); timeout(TIME_OUT); noecho(); store_n_discs = n_discs; init_pegs(my_pegs, n_discs); show_pegs(stdscr, my_pegs, n_discs); solve_hanoi(my_pegs, n_discs, 0, 1, 2); free_pegs(my_pegs, n_discs); endwin(); /* End curses mode */ break; case 3: printf("\nExiting"); break; } break; } move_disk(disks, choice_from, choice_to, index_from,peg_A, peg_B, peg_C); print_status(disks, peg_A, peg_B, peg_C); } if(peg_C[disks-1]==3) printf("Congratulations! You won!\n"); return 0; }