void test_negative() { // generate int n = 2+rand()%(MAX_GRAPH_SIZE-1); Graph g(n); vector<int> arc_count(n+1, 0); bool expected = true; int x, y; int m = MIN_EDGES + rand()%(MAX_EDGES-MIN_EDGES); for (int i=0; i<m; ++i) { x = 1+rand()%n; y = 1+rand()%n; g.addArc(x, y); ++arc_count[x]; --arc_count[y]; } for (int i=1; i<=n; ++i) { if (arc_count[i]) { expected = false; } } // run vector<int> cycle = find_cycle(g); // verify if (expected == false) { assert(cycle.size() == 1); assert(cycle[0] == -1); } }
void solve() { while(m_strm >> m_from >> m_to) { long long from = m_from, to = m_to; if(m_from > m_to) std::swap(from, to); for(;from <= to; ++from) { std::map<long long, long long>::iterator it = m_cash.find(from); if(it != m_cash.end()) { m_res = std::max(m_res, it->second); } else { long long res = find_cycle(from); m_cash[from] = res; m_res = std::max(m_res, res); } } std::cout << m_from << ' ' << m_to << ' ' << m_res << std::endl; m_res=-1; } }
void find_cycle(GraphFrame *gf, struct pt *v, int level, int cycle_len) { LNode_type *poed; if(level > cycle_len) return; v->mark = True; v->pick = True; v->level = level; Insert_linked_list(gf->list_visited_vertex,create_new_node_llist((char*)v)); /*printf(" level : %d , vertex %s \n", level, v->label);*/ for(poed = v->ledges_out; poed->next != v->ledges_out; poed = poed->next) { struct edge *e = (struct edge *)poed->next->info; struct pt * tv = e->to; if(tv->pick) save_new_cycle(gf,tv,v,cycle_len); else if(!(tv->mark)) find_cycle(gf,tv, level+1, cycle_len); } for(poed = v->ledges_in; poed->next != v->ledges_in; poed = poed->next) { struct edge *e = (struct edge *)poed->next->info; struct pt * tv = e->from; if(tv == v) tv = e->to; if(tv->pick) save_new_cycle(gf,tv,v,cycle_len); else if(!(tv->mark)) find_cycle(gf,tv, level+1, cycle_len); } v->pick = False; v->mark = False; Delete_linked_list(Search_Sequential_list(gf->list_visited_vertex, (char *)v)); }
bool MSConnectivityRestraint::ExperimentalTree::find_cycle( unsigned int node_index) { Node &node = nodes_[node_index]; if (node.visited_) return true; node.visited_ = true; bool cycle_found = false; for (unsigned int i = 0; i < node.get_number_of_children(); ++i) if (find_cycle(node.get_child(i))) { cycle_found = true; break; } node.visited_ = false; return cycle_found; }
int max_cycle (int lim) { int n = lim; int i = lim; int res = 0; while (res < i) { int tmp = find_cycle (i); if (tmp > res) { res = tmp; n = i; } i--; } return n; }
virtual void solve() { int pos, rides, money, reach_rides, reach_money; std::clog << "Finding cycle" << std::endl; find_cycle(pos, rides, money, reach_rides, reach_money); std::clog << "Cycle found: pos = " << pos << ", rides = " << rides << ", money = " << money << ", reach_rides = " << reach_rides << ", reach_money = " << reach_money << std::endl; m_solution = reach_money; if (m_rides > reach_rides) { int repeats = (m_rides - reach_rides) / rides; int remainder = (m_rides - reach_rides) % rides; m_solution += repeats*money; while (remainder--) { m_solution += m_group_next_sum[pos]; pos = m_group_next_idx[pos]; } } }
void test_positive() { // generate int n = 2+rand()%(MAX_GRAPH_SIZE-1); Graph g(n); int arc_count = 0; { int m = MIN_EDGES + rand()%(MAX_EDGES-MIN_EDGES); int current = 1; int next; for (int i=0; i<m; ++i) { if (i == m-1) { if (current != 1) { next = 1; g.addArc(current, next); ++arc_count; } break; } next = 1+rand()%(n-1); if (next == current) ++next; g.addArc(current, next); ++arc_count; if (next == 1 && i >= MIN_EDGES) break; current = next; } } // run vector<int> cycle = find_cycle(g); // verify assert(cycle.size() == size_t(arc_count+1)); assert(cycle[0] == cycle[cycle.size()-1]); int current = cycle[cycle.size()-1]; int next; for (int i=cycle.size()-2; i>=0; --i) { next = cycle[i]; list<size_t>::iterator edge = find(g.arcs_[current].begin(), g.arcs_[current].end(), next); assert(edge != g.arcs_[current].end()); g.arcs_[current].erase(edge); current = next; } }
/* look for the longest? cycle from node from to node to. */ static int find_cycle(NODE *from, NODE *to, int longest_len, int depth) { NODE **np; int i, len; /* * avoid infinite loops and ignore portions of the graph known * to be acyclic */ if (from->n_flags & (NF_NODEST|NF_MARK|NF_ACYCLIC)) return (0); from->n_flags |= NF_MARK; for (np = from->n_arcs, i = from->n_narcs; --i >= 0; np++) { cycle_buf[depth] = *np; if (*np == to) { if (depth + 1 > longest_len) { longest_len = depth + 1; (void)memcpy(longest_cycle, cycle_buf, longest_len * sizeof(NODE *)); } } else { if ((*np)->n_flags & (NF_MARK|NF_ACYCLIC|NF_NODEST)) continue; len = find_cycle(*np, to, longest_len, depth + 1); if (debug) (void)printf("%*s %s->%s %d\n", depth, "", from->n_name, to->n_name, len); if (len == 0) (*np)->n_flags |= NF_NODEST; if (len > longest_len) longest_len = len; if (len > 0 && !longest) break; } } from->n_flags &= ~NF_MARK; return (longest_len); }
void get_cycle(GraphFrame *gf, int cycle_len) { int i; struct pt *v; if(cycle_len > gf->count_vertex) return; for(i = 1 ; i <= gf->count_vertex; i++) { v = get_vertex(i,gf); /*printf("first node %s \n", v->label);*/ if(!v->mark) find_cycle(gf, v, 0, cycle_len); if(!is_empty_list(gf->the_cycle)) break; } }
int main (void) { int max = 0; int cycle_count; int max_cycle_number = 0; int d; for (d = 2; d < 1000; ++d) { cycle_count = find_cycle(d); if (cycle_count > max) { max_cycle_number = d; max = cycle_count; } } printf("Highest Number: %d, Cycle Count: %d\n", max_cycle_number, max); return 0; }
void MSConnectivityRestraint::ExperimentalTree::finalize() { if (finalized_) return; for (unsigned int i = 0; i < nodes_.size(); ++i) { if (nodes_[i].is_root()) { if (root_ == static_cast<unsigned int>(-1)) root_ = i; else IMP_THROW("Experimental tree has multiple roots", IMP::ValueException); } } if (find_cycle(root_)) IMP_THROW("Experimental tree has a cycle", IMP::ValueException); for (unsigned int i = 0; i < nodes_.size(); ++i) if (!is_consistent(i)) { IMP_THROW( "Experimental tree is inconsistent: a child has to " "have fewer proteins than its parent", IMP::ValueException); } finalized_ = true; }
int main() { node_t head = NULL; node_t first = NULL; node_t second = NULL; node_t merged = NULL; int choice = 0; int ele; int pos; node_t node_pointer = NULL; int n; int is_cycle = 0; node_t list_cycle = NULL; int isPalindrome = 0; int intersection = 0; node_t resultant_of_addition = NULL; int k = 0; int m; while(1) { printf("--------------------------------------------------------------------------------------------------------\n"); printf("1. Add node in the front end \n"); printf("2. Display the list \n"); printf("3. Exit \n"); printf("4. Add node at the rear end of the list \n"); printf("5. Delete a node at the front end of the list \n"); printf("6. Delete a node from the rear end of the list \n"); printf("7. Insert a node in order to the list \n"); printf("8. Merge two ordered linked lists \n"); printf("9. Search for an item in the list \n"); printf("10. Delete a node whose value is specified \n"); printf("11. Delete a node at the specified position \n"); printf("12. Reverse list wihtout creating extra nodes \n"); printf("13. Delete a node given only a pointer to the node \n"); printf("14. Print middle element of the list \n"); printf("15. Print the nth last element in the list \n"); printf("16. Delete the entire list \n"); printf("17. Detect a loop in the list \n"); printf("18. Check whether a list is a palindrome \n"); printf("19. Find the intersection of two lists \n"); printf("20. Print reverse recursively \n"); printf("21. Remove duplicates in a sorted linked list \n"); printf("22. Move the last node in the list to the first node \n"); printf("23. Reverse the list pairwise \n"); printf("24. Find the intersection of two lists recursively \n"); printf("25. Delete alternate nodes in the list \n"); printf("26. Alternating split of the list \n"); printf("27. Delete nodes whose neighbours value is greater \n"); printf("28. Sepearate into even and odd in that order \n"); printf("29. Add two lists and give the resultant list \n"); printf("30. Rotate the list by k elements \n"); printf("31. Separate into 0s and 1s \n"); printf("32. Delete n nodes after the first m nodes \n"); printf("-------------------------------------------------------------------------------------------------------\n"); printf("Enter the choice\n"); scanf("%d", &choice); switch(choice) { case 1: printf("Enter the element to enter to the front end of the list \n"); scanf("%d", &ele); head = add_front(head, ele); break; case 2: display(head); break; case 3: exit(0); case 4: printf("Enter an element to be added to the end of the list \n"); scanf("%d", &ele); head = add_end(head, ele); break; case 5: head = delete_front(head); break; case 6: head = delete_rear(head); break; case 7: printf("Enter the element to be inserted \n"); scanf("%d", &ele); head = insert_in_order(head, ele); break; case 8: first = insert_in_order(first, 92); first = insert_in_order(first, 42); first = insert_in_order(first, 35); second = insert_in_order(second, 100); second = insert_in_order(second, 432); second = insert_in_order(second, 90); second = insert_in_order(second, 10); printf("The elements of the first list are: \n"); display(first); printf("The elements of the second list are \n"); display(second); merged = merge_ordered_lists(first, second); printf("The ordered list is: \n"); display(merged); case 9: printf("Enter the element of the list to be searched for \n"); scanf("%d", &ele); int pos = search(head, ele); if(pos != -1) { printf("The element is found at %d: \n", pos); } else { printf("The element is not found in the list \n"); } break; case 10: printf("Enter the element of the list to be deleted: \n"); scanf("%d", &ele); head = delete_with_info(head, ele); if(head == (node_t)NULL) { printf("The list is empty or the element u specified is not found: \n"); } break; case 11: printf("Enter the position with the first node as position 1 \n"); scanf("%d", &ele); head = delete_with_pos(head, ele); if(head == (node_t)NULL) { printf("Either the list is empty or the position specified is not appropriate \n"); } break; case 12: head = reverse(head); break; case 13: node_pointer = head -> link -> link; delete_node_given_pointer(node_pointer); break; case 14: print_middle(head); break; case 15: printf("Enter the value of n \n"); scanf("%d",&n); print_nth_last(head, n); break; case 16: head = delete_list(head); break; case 17: list_cycle = add_end(list_cycle,1); list_cycle = add_end(list_cycle,2); list_cycle = add_end(list_cycle,3); list_cycle = add_end(list_cycle,4); list_cycle = add_end(list_cycle,5); list_cycle -> link -> link -> link -> link -> link = list_cycle -> link; is_cycle = find_cycle(list_cycle); if(is_cycle) { printf("There is a cycle in the list \n"); } else { printf("There is no cycle in the list \n"); } break; case 18: isPalindrome = is_palindrome(&head, head); if(isPalindrome) { printf("The list is a palindrome \n"); } else { printf("The list is not a palindrome \n"); } break; case 19: first = add_end(first,10); first = add_end(first,20); second = add_end(second,43); second = add_end(second,3); second = add_end(second,34); second = add_end(second,44); first -> link -> link = second -> link; intersection = find_intersection(first, second); printf("The intersection point of the two lists are %d \n", intersection); break; case 20: print_reverse_recursively(head); printf("\n"); break; case 21: remove_duplicates(head); break; case 22: head = move_last_to_first(head); break; case 23: head = pairwise_reverse(head); break; case 24: first = add_end(first, 10); first = add_end(first, 30); first = add_end(first, 40); first = add_end(first, 50); first = add_end(first, 60); second = add_end(second, 10); second = add_end(second, 20); second = add_end(second, 30); find_common_recursively(first, second); break; case 25: head = delete_alternate(head); break; case 26: alternating_split_v2(head); break; case 27: head = delete_node_when_neigbour_higher(head); break; case 28: head = separate_into_even_odd_v2(head); break; case 29: first = add_front(first, 2); first = add_front(first, 4); first = add_front(first, 8); second = add_front(second,2); second = add_front(second,4); second = add_front(second,5); second = add_front(second,3); resultant_of_addition = add_two_lists(first, second); printf("The resultant list is as follows \n"); display(resultant_of_addition); break; case 30: printf("Enter the value of k \n"); scanf("%d",&k); head = rotate_by_k(head, k); break; case 31: head = separate_into_zeroes_ones(head); break; case 32: printf("Enter the value of m \n"); scanf("%d", &m); printf("Enter the value of n \n"); scanf("%d", &n); head = retain_m_delte_n(head, m , n); break; default: printf("Invalid choice... Please try again \n"); break; } } }
int optimize_transport(BASIS *basis) { /** Declare |optimize_transport| scalars */ int i_enter,j_enter; int arcindex; int halfnodes; int subroot; int prev; int orient; int status; double deltadual; /** Declare |optimize_transport| arrays */ int *family; int *pred; int *brother; int *son; status=init_basis(basis); /* construct initial basis */ if (status) return status; /** Define simplifications for |optimize_transport| */ family = basis->family; pred = basis->pred; brother = basis->brother; son = basis->son; halfnodes=basis->no_nodes/2; deltadual=find_entering_arc(basis,&i_enter,&j_enter,&arcindex); while(i_enter>=0){ /* there is an entering arc */ basis->pivot++; #ifdef PRINT printf("+-----------------------------------+\n"); printf("| pivot %3d |\n",basis->pivot); printf("+-----------------------------------+\n"); #endif subroot = find_cycle(basis,i_enter,j_enter,arcindex,&orient); #ifdef PRINT printf("\nentering arc : %d --> %d \n",i_enter,j_enter); printf(" arcindex : %d\n",arcindex); printf(" redcost : %8.4lf",deltadual); #endif /* update duals in the smaller subtrees */ if(family[subroot]<=halfnodes) update_dual(basis,subroot,(orient>0)?deltadual:-deltadual); else { /* separate trees, update duals, then reconnect */ basis->rerooted++; prev = pred[subroot]; /* remove root from successors of prev */ son[prev]= brother[subroot]; update_dual(basis,0,(orient>0)?-deltadual:deltadual); brother[subroot]=son[prev]; /* insert root to successors of prev */ son[prev]=subroot; } #ifdef PRINT print_basis(basis); #endif deltadual=find_entering_arc(basis,&i_enter,&j_enter,&arcindex); } status=check_solution(basis); return status; }
/* do topological sort on graph */ static void tsort(void) { NODE *n, *next; int cnt, i; while (graph != NULL) { /* * Keep getting rid of simple cases until there are none left, * if there are any nodes still in the graph, then there is * a cycle in it. */ do { for (cnt = 0, n = graph; n != NULL; n = next) { next = n->n_next; if (n->n_refcnt == 0) { remove_node(n); ++cnt; } } } while (graph != NULL && cnt); if (graph == NULL) break; if (!cycle_buf) { /* * Allocate space for two cycle logs - one to be used * as scratch space, the other to save the longest * cycle. */ for (cnt = 0, n = graph; n != NULL; n = n->n_next) ++cnt; cycle_buf = malloc((u_int)sizeof(NODE *) * cnt); longest_cycle = malloc((u_int)sizeof(NODE *) * cnt); if (cycle_buf == NULL || longest_cycle == NULL) err(1, "malloc"); } for (n = graph; n != NULL; n = n->n_next) { if (!(n->n_flags & NF_ACYCLIC)) { if ((cnt = find_cycle(n, n, 0, 0)) != 0) { if (!quiet) { warnx("cycle in data"); for (i = 0; i < cnt; i++) warnx("%s", longest_cycle[i]->n_name); } remove_node(n); clear_cycle(); break; } else { /* to avoid further checks */ n->n_flags |= NF_ACYCLIC; clear_cycle(); } } } if (n == NULL) errx(1, "internal error -- could not find cycle"); } }