int no_path(int a[], int n, int start) { static int count =0; if(start == n-1) { count++; return count; } if(start+1<n) no_path(a,n,start+1); if(start+2<n) no_path(a,n,start+2); return count; }
struct path *find_shortest_ (int **weights, struct path **cache, struct node **graph, int id1, int id2) { printf ("id1: %d, id2: %d\n", id1, id2); if (id1 == id2) { struct path *stub = new_path (id1); cache[id1] = stub; return stub; } // If the path passed to us is a cycl //struct node *start = get_node (graph, id1); struct node *end = get_node (graph, id2); // Get all of the shortest paths for the predecessors // Also, the shortest path to us is us plus the shortest of the paths to our // predecessors int i; int min = 65535; int minpath = 0; for (i = 0; i < end->num_back; i++) { struct path *cur = cache[end->back[i]]; // Only do work if we have to if (cur == NULL) { cur = find_shortest_ (weights, cache, graph, id1, end->back[i]); } // Now we definitely have a value if (cur->numelems > 0 && cur->length + weights[end->back[i]][id2] < min) { min = cur->length + weights[end->back[i]][id2]; minpath = end->back[i]; } } // If there's no path, return that // We also get here if there's nobody behind us if (min == 65535) return no_path (); // Add us to the cache and return struct path *our_shortest = add_stop (weights, cache[minpath], minpath, id2); // If we've cycled (only happens if there's a negative cycle) then print out // that we have a negative cycle and the path that has it // Might not terminate if we do... Hmm... if (has_cycle (our_shortest)) { printf ("======== We have a cycle =======\n"); print_path (our_shortest); printf("================\n"); } // Add us to the cache cache[id2] = our_shortest; return our_shortest; }