static void RenderILS2D(SFNode *node, void *rs) { DrawableContext *ctx; M_IndexedLineSet2D *ils2D = (M_IndexedLineSet2D *)node; Drawable *cs = Node_GetPrivate(node); RenderEffect2D *eff = rs; if (!ils2D->coord) return; if (Node_GetDirty(node)) { drawable_reset_path(cs); build_graph(cs, ils2D); Node_ClearDirty(node); cs->node_changed = 1; } ctx = drawable_init_context(cs, eff); if (!ctx) return; /*ILS2D are NEVER filled*/ ctx->aspect.filled = 0; drawctx_store_original_bounds(ctx); drawable_finalize_render(ctx, eff); }
int main(int argc, char *argv[]) { if(argc!=2){ std::cerr<<"Must specify n.\n"; return 1; } int n=atoi(argv[1]); std::vector<node> graph=build_graph(n); dump_graph(graph); // The run-time can vary, depending on where you start from. How should you // take that into account when timing it? int start=rand()%n; // Note that it is only graph_distance that we care about std::vector<int> tmp=graph_distance(graph, start); for(int i=0;i<tmp.size();i++){ fprintf(stdout, "dist(%d->%d) = %d\n", start, i, tmp[i]); } return 0; }
static void RenderPointSet2D(SFNode *node, void *rs) { DrawableContext *ctx; M_PointSet2D *ps2D = (M_PointSet2D *)node; Drawable *cs = Node_GetPrivate(node); RenderEffect2D *eff = rs; if (!ps2D->coord) return; if (Node_GetDirty(node)) { drawable_reset_path(cs); build_graph(cs, &eff->transform, ps2D); Node_ClearDirty(node); cs->node_changed = 1; } ctx = drawable_init_context(cs, eff); if (!ctx) return; ctx->aspect.filled = 1; ctx->aspect.has_line = 0; drawctx_store_original_bounds(ctx); drawable_finalize_render(ctx, eff); }
int rcm(TMatrix_DCSR *matr, real threshold) { TWGraph gr; int *perm, *invp; int err = 0; #ifdef _DEBUG_LEVEL_0 printf("[debug(0)]{rcm}: graph_builder\n"); #endif err = build_graph(&gr, matr); if ( err != ERROR_NO_ERROR ) ERROR_MESSAGE("rcm: graph_builder failed", err); #ifdef _DEBUG_LEVEL_0 printf("[debug(0)]{rcm}: find_permutation\n"); #endif err = find_permutation(&gr,&perm, &invp, threshold); // !!!REORDERING!!! if ( err != ERROR_NO_ERROR ) ERROR_MESSAGE("rcm: find_permutation failed", err); #ifdef _DEBUG_LEVEL_0 printf("[debug(0)]{rcm}: graph_reoder\n"); #endif err = graph_reorder(&gr, perm, invp); if ( err != ERROR_NO_ERROR ) ERROR_MESSAGE("rcm: graph_reorder failed", err); if (perm) free(perm); if (invp) free(invp); #ifdef _DEBUG_LEVEL_0 printf("[debug(0)]{rcm}: matrix_builder\n"); #endif err = build_matrix(&gr, matr, 0); if ( err != ERROR_NO_ERROR ) ERROR_MESSAGE("rcm: matrix_builder failed", err); graph_destroy(&gr); return ERROR_NO_ERROR; }
int main() { const char out[4][32] = {"subordinate", "supervisor", "colleague", "unrelated"}; while (scanf("%d", &n) == 1) { for (int i = 0; i < n; i++) { scanf("%d", &A[i].id); readName(&A[i]); scanf("%d", &A[i].boss_id); } build_graph(); scanf("%d", &m); employee x, y; for (int i = 0; i < m; i++) { readName(&x); readName(&y); int ix = nameToIndex(&x), iy = nameToIndex(&y); if (ix == -1 || iy == -1) { puts(out[3]); } else { puts(out[relation(ix, iy)]); } } } return 0; }
int main(int argc, char **argv) { int n; cin >> n; for (int i = 0; i < n; ++i) { int size; cin >> size; // list<Edge> edges; // int vertex_u; // int vertex_v; // int edges_quantity = 0; // while(cin >> vertex_u >> vertex_v) // { // edges.push_back(make_pair(--vertex_u,--vertex_v)); // } // Graph graph = build_graph(edges,size); // Graph graph_transposed = transpose_of(graph,size); int **matrix = new int*[size]; for(int i = 0; i < size; i++) { matrix[i] = new int[size]; for (int j = 0; j < size; ++j) { cin >> matrix[i][j]; } } Graph graph = build_graph(matrix,size); print_graph(graph,size); cout << "\n==================\n"; } // print_graph(graph_transposed,size); // cout << "\n"; // run_dfs(graph,size); // cout << "Degrees" << endl; // for (int i = 0; i < size; ++i) // { // cout << i << " - IN: " << in_degree(graph,size,i) << " | OUT: " << out_degree(graph,size,i) << endl; // } // cout << "Is this graph connected? "; // if(is_connected(graph,size)){ // cout << "Yes"; // } else { // cout << "No"; // } // cout << endl; // cout << "Is there any cycle in this graph? "; // if(has_cycle(graph,size)){ // cout << "Yes"; // } else { // cout << "No"; // } // cout << endl; // cout << "Topological ordering (degree strategy) for this graph\n"; // list<int> order = topological_sort_degree_strategy(graph,size); // for(list<int>::iterator it = order.begin(); it != order.end(); it++){ // cout << *it << " "; // } // cout << endl; // cout << "Topological ordering (dfs strategy) for this graph\n"; // order = topological_sort_dfs_strategy(graph,size); // for(list<int>::iterator it = order.begin(); it != order.end(); it++){ // cout << *it << " "; // } // cout << endl; // cout << "Reachability Matrix" << endl; // int **matrix = simple_reachability_matrix(graph,size); // for (int i = 0; i < size; ++i) // { // for (int j = 0; j < size; ++j) // { // cout << matrix[i][j] << " "; // } // cout << endl; // } // cout << "Transitive Closure" << endl; // matrix = transitive_closure(graph,size); // for (int i = 0; i < size; ++i) // { // for (int j = 0; j < size; ++j) // { // cout << matrix[i][j] << " "; // } // cout << endl; // } // cout << "Mutual Reachability Matrix" << endl; // matrix = mutual_reachability_matrix(matrix,size); // for (int i = 0; i < size; ++i) // { // for (int j = 0; j < size; ++j) // { // cout << matrix[i][j] << " "; // } // cout << endl; // } // Connected_Components components; // /*cout << "Connected Components (UNDIRECTED GRAPH)" << endl; // components = connected_components_undirected_graph(graph,size); // list<int>* elements; // int counter = 0; // for (Connected_Components::iterator i = components.begin(); i != components.end(); ++i) // { // counter++; // elements = *i; // for (list<int>::iterator elem = elements->begin(); elem != elements->end(); ++elem) // { // cout << *elem << " "; // } // cout << endl; // } // cout << counter << " " << " elements" << endl;*/ // cout << "Connected Components (BRUTE FORCE)" << endl; // components = connected_components_brute_force(graph,size); // list<int>* elements; // int counter = 0; // for (Connected_Components::iterator i = components.begin(); i != components.end(); ++i) // { // counter++; // elements = *i; // for (list<int>::iterator elem = elements->begin(); elem != elements->end(); ++elem) // { // cout << *elem << " "; // } // cout << endl; // } // cout << counter << " " << " elements" << endl; // cout << "Connected Components (KOSARAJU AND SHARIR)" << endl; // components = connected_components_kosaraju_sharir(graph,size); // counter = 0; // for (Connected_Components::iterator i = components.begin(); i != components.end(); ++i) // { // counter++; // elements = *i; // for (list<int>::iterator elem = elements->begin(); elem != elements->end(); ++elem) // { // cout << *elem << " "; // } // cout << endl; // } // cout << counter << " " << " elements" << endl; return 0; }
graph_t build_graph(mstack_t instructions) { graph_t new_graph = (graph_t)calloc(1, sizeof(graph)); graph_t expr_graph; mstack_t aux_node_stack = (mstack_t)calloc(1, sizeof(stack)); mstack_t expr_stack; node_t new_node; node_t ending_graph_node; node_t aux_cond_node; stack_node aux_node; stack_node ending_node; instruction_t new_instruction; int new_node_type; int aux_node_type; if(instructions == NULL || is_empty(instructions)){ return NULL; } while (!is_empty(instructions)) { new_node = (node_t)calloc(1, sizeof(graph_node)); aux_node = pop(instructions); new_instruction = (instruction_t)aux_node->info; new_node_type = new_instruction->instruction_type->type; new_node->instruction_process = new_instruction; new_node->cond_executed = 0; if (new_graph->first) new_node->true_node = new_graph->first; if (new_node_type == ENDWHILE || new_node_type == ENDIF) push(aux_node_stack, new_node); else if (new_node_type == WHILE || new_node_type == IF) { ending_node = pop(aux_node_stack); ending_graph_node = (node_t)ending_node->info; aux_node_type = ending_graph_node-> instruction_process->instruction_type->type; if (ending_graph_node->instruction_process->param != new_node->instruction_process->param || aux_node_type == ENDIF && new_node_type != IF || aux_node_type == ENDWHILE && new_node_type != WHILE) return NULL; new_node->false_node = ending_graph_node->true_node; if (new_node_type == WHILE) ending_graph_node->true_node = new_node; expr_stack = create_stack(); if (parse_string(new_instruction->expr, expr_stack) == -1) return NULL; expr_graph = build_graph(expr_stack); if (expr_graph == NULL) return NULL; new_node->conditional_expr = expr_graph->first; aux_cond_node = expr_graph->first; while (aux_cond_node->true_node != NULL) aux_cond_node = aux_cond_node->true_node; aux_cond_node->true_node = new_node; } new_graph->first = new_node; free(aux_node); } if (!is_empty(aux_node_stack)) return NULL; return new_graph; }
int teste(int n) { build_graph(n); if(dfs(0, 1)) return 1; return 0; }
vector<vector<string> > findLadders(const string& start, const string& end, const unordered_set<string> &dict) { queue<string> q; unordered_map<string, int> visited; // 判重 unordered_map<string, vector<string> > father; // DAG // only used by state_extend() const unordered_map<string, unordered_set<string> >& g = build_graph(dict); auto state_is_valid = [&](const string& s) { return dict.find(s) != dict.end() || s == end; }; auto state_is_target = [&](const string &s) {return s == end; }; auto state_extend = [&](const string &s) { vector<string> result; const int new_depth = visited[s] + 1; auto iter = g.find(s); if (iter == g.end()) return result; const auto& list = iter->second; for (const auto& new_state : list) { if (state_is_valid(new_state)) { auto visited_iter = visited.find(new_state); if (visited_iter != visited.end()) { const int depth = visited_iter->second; if (depth < new_depth) { // do nothing } else if (depth == new_depth) { result.push_back(new_state); } else { // not possible throw std::logic_error("not possible to get here"); } } else { result.push_back(new_state); } } } return result; }; vector<vector<string>> result; q.push(start); visited[start] = 0; while (!q.empty()) { // 千万不能用 const auto&,pop() 会删除元素, // 引用就变成了悬空引用 const auto state = q.front(); q.pop(); // 如果当前路径长度已经超过当前最短路径长度, // 可以中止对该路径的处理,因为我们要找的是最短路径 if (!result.empty() && visited[state] + 1 > result[0].size()) break; if (state_is_target(state)) { vector<string> path; gen_path(father, start, state, path, result); continue; } // 必须挪到下面,比如同一层A和B两个节点均指向了目标节点, // 那么目标节点就会在q中出现两次,输出路径就会翻倍 // visited.insert(state); // 扩展节点 const auto& new_states = state_extend(state); for (const auto& new_state : new_states) { if (visited.find(new_state) == visited.end()) { q.push(new_state); visited[new_state] = visited[state] + 1; } father[new_state].push_back(state); } } return result; }
int main(int argc, char *argv[]) { /* hi */ double secs; clock_t ticks; unsigned long int *vertex_addresses; int start, target, file_size, file_pos, level, sum, found = 0; char *file_buffer, file[] = "Graphs/graph4.txt"; Graph *graph; Result *result, *rhead; /* yeah, dangerous, since never re-allocated - f**k that and save time :~D */ vertex_addresses = (unsigned long int *)malloc(2500000 * sizeof(unsigned long int)); result = new_result(); rhead = result; /* run clock */ ticks = clock(); /* input */ switch(argc) { case 2: file_buffer = load_file(argv[1], &file_size); break; case 1: file_buffer = load_file(file, &file_size); break; default: printf("\nFehlerhafte Eingabe\n\n"); exit(1); } /* organize */ start = get_start(file_buffer); target = get_target(file_buffer); graph = build_graph(file_buffer, file_size, vertex_addresses); /* search */ for(level = SEARCH_INIT_LEVEL, found = 0; found != 1 && level < 1000; level++) found = iterative_deepening(graph->vertices->next_vertex, target, level, &result, 0); /* format + output */ for(sum = 0; rhead->next; rhead = rhead->next) sum += rhead->next->cost; if(sum) { printf("\n%d\n", sum); while(result->prev && result->prev->state) { if(result->prev->state != result->state) printf("Z%d ", result->state); result = result->prev; } printf("Z%d\n\n", result->state); } else printf("\nZiel nicht erreichbar\n\n"); /* stop clock */ ticks = clock() - ticks; secs = ((double)ticks) / CLOCKS_PER_SEC; printf("\n\n(Duration %.3lf seconds)\n", secs); /* bye */ free(vertex_addresses); free(file_buffer); return(0); }