/*** Name: walk2 - Second pre-order walk ** ** Description: ** This function is the second pre-order walk. It is a recursive ** implementation of the second part of algorithm 3 in the reference. ** ** Inputs: ** pnode Pointer to the tree to walk ** control Pointer to control structure ** ** Outputs: ** pnode This function does something to the ** tree. Why was it never documented ** in-line? ** control Various things done to this ** Returns: ** None ** Exceptions: ** none ** ** Side Effects: ** none ** ** History: ** 06-may-86 (jeff) ** Adapted from walk2() in jutil!fmttree.c in 4.0 */ static VOID walk2( PARATREE *pnode, register ULD_CONTROL *control ) { register PARATREE *p = pnode; if (p == (PARATREE *) NULL) return; p->x += control->modfsum; control->modfsum += p->modifier; walk2(p->lf, control); walk2(p->rt, control); control->modfsum -= p->modifier; }
void walk2(int t0, int t1, int x0, int dx0, int x1, int dx1, int y0, int dy0, int y1, int dy1) { /* if (firsttime) { printf ("calling t0 = %d, t1 = %d, x0 = %d, y0 = %d, x1 = %d, y1 = %d \n", t0, t1, x0, y0, x1, y1); }/**/ int lt = t1 - t0; //if (lt==0) printf ("lt is zero\n"); if (lt == 1) { int x, y; //hist[x1-x0][y1-y0]++; U(t1,hx,hy)=5; for (x = x0; x < x1; x++) for (y = y0; y < y1; y++) kernel(t0, x, y); } else if (lt > 1) { //parallelize it if (2 * (x1 - x0) + (dx1 - dx0) * lt >= 4 * ds * lt) { int xm = (2 * (x0 + x1) + (2 * ds + dx0 + dx1) * lt) / 4; walk2(t0, t1, x0, dx0, xm, -ds, y0, dy0, y1, dy1); walk2(t0, t1, xm, -ds, x1, dx1, y0, dy0, y1, dy1); } else if (2 * (y1 - y0) + (dy1 - dy0) * lt >= 4 * ds * lt) { int ym = (2 * (y0 + y1) + (2 * ds + dy0 + dy1) * lt) / 4; walk2(t0, t1, x0, dx0, x1, dx1, y0, dy0, ym, -ds); walk2(t0, t1, x0, dx0, x1, dx1, ym, -ds, y1, dy1); } else { //if lt > THRESH ... if (lt > ltThresh) { int halflt = lt / 2; walk2(t0, t0 + halflt, x0, dx0, x1, dx1, y0, dy0, y1, dy1); walk2(t0 + halflt, t1, x0 + dx0 * halflt, dx0, x1 + dx1 * halflt, dx1, y0 + dy0 * halflt, dy0, y1 + dy1 * halflt, dy1); } else { int t,x,y; for (t=0; t<lt; ++t) { /* walk2(t+t0,t+t0+1, x0 + dx0 * t, dx0, x1 + dx1 * t, dx1, y0 + dy0 * t, dy0, y1 + dy1 * t, dy1); */ //hist[x1-x0][y1-y0]++; U(t1,hx,hy)=5; for (x = x0; x < x1; x++) for (y = y0; y < y1; y++) kernel(t0+t, x, y); x0 += dx0; y0 += dy0; x1 += dx1; y1 += dy1; } } } } }
void LineGraph::GenrateGraph(sint32 &infoXCount, sint32 &infoYCount, double *** infoGraphData, sint32 category) { infoYCount = 0; infoXCount = 0; AUI_ERRCODE errcode = AUI_ERRCODE_OK; std::auto_ptr<aui_StringTable> stringTable (new aui_StringTable(&errcode, "InfoStrings")); SetXAxisName(stringTable->GetString(6)); SetYAxisName("Power"); double minRound = s_minRound; double curRound = g_turn->GetRound(); double minPower = 0; double maxPower = 10; SetGraphBounds(minRound, curRound, minPower, maxPower); HasIndicator(false); sint32 i; for ( i = 0 ; i < k_MAX_PLAYERS ; i++ ) { if (g_player[i] && (i != PLAYER_INDEX_VANDALS)) { infoYCount++; } } sint32* color = new sint32[infoYCount + g_deadPlayer->GetCount()]; infoYCount = 0; for ( i = 0 ; i < k_MAX_PLAYERS ; i++ ) { if (g_player[i] && (i != PLAYER_INDEX_VANDALS)) { color[infoYCount++] = g_colorSet->ComputePlayerColor(i); } } for ( PointerList<Player>::Walker walk(g_deadPlayer); walk.IsValid(); walk.Next() ) { color[infoYCount++] = g_colorSet->ComputePlayerColor(walk.GetObj()->GetOwner()); } infoXCount = static_cast<sint32>(curRound) - static_cast<sint32>(minRound); if (infoXCount == 0) { RenderGraph(); return; } infoXCount = std::max<sint32>(1, infoXCount); infoYCount = std::max<sint32>(1, infoYCount); Assert(!*infoGraphData); *infoGraphData = new double *[infoYCount]; for (i = 0 ; i < infoYCount; i++) { (*infoGraphData)[i] = new double[infoXCount]; std::fill((*infoGraphData)[i], (*infoGraphData)[i] + infoXCount, 0.0); } sint32 playerCount = 0; for ( i = 0 ; i < k_MAX_PLAYERS ; i++ ) { if (g_player[i] && (i != PLAYER_INDEX_VANDALS)) { for (sint32 round = 0 ; round < infoXCount ; ++round) { sint32 strValue = GetCombinedStrength(*g_player[i]->m_strengths, round, category); (*infoGraphData)[playerCount][round] = strValue; while (strValue > maxPower) maxPower += 10.0; } playerCount++; } } for ( PointerList<Player>::Walker walk2(g_deadPlayer); walk2.IsValid(); walk2.Next() ) { for (sint32 round = 0 ; round < infoXCount ; ++round) { sint32 strValue = GetCombinedStrength(*walk2.GetObj()->m_strengths, round, category); (*infoGraphData)[playerCount][round] = strValue; while (strValue > maxPower) maxPower += 10.0; } playerCount++; } Assert(playerCount == infoYCount); SetLineData(infoYCount, infoXCount, (*infoGraphData), color); SetGraphBounds(minRound, curRound, minPower, maxPower); RenderGraph(); delete color; }
/*{ ** Name: uld_prtree - Format and print a tree ** ** Description: ** This function formats and prints a tree, with help from caller- ** supplied functions. The algorithm was adapted by Bob Kooi from ** Wetherell, C. and Shannon, A., "Tidy Drawings of Trees," ** IEEE Transactions on Software Engineering, Vol. SE-5, No. 5 ** September, 1979 ** ** Later mods made it re-entrant so it could run as part of a server. ** ** EXAMPLE: ** suppose you have a tree made out of nodes of type tnode: ** ** struct tnode ** { ** i4 data1; ** i4 data2; ** struct tnode *left; ** struct tnode *right; ** } ** ** where Root is a pointer to the root. you must provide ** three routines, call them leftson, rightson and printnode: ** ** PTR ** leftson(t) ** PTR t; ** { ** return ((PTR) ((struct tnode *) t)->left); ** } ** ** PTR ** rightson(t) ** PTR t; ** { ** return ((PTR) ((struct tnode *) t)->right); ** } ** ** VOID ** printnode(t, control) ** PTR t; ** PTR control; ** { ** struct tnode *node = (struct tnode *) t; ** ** uld_prnode(control, "*****"); ** TRformat(uld_tr_callback, (i4 *)0, ** global_buffer, sizeof(global_buffer), ** "* %d *", ** node->data1); ** uld_prnode(control, global_buffer); ** TRformat(uld_tr_callback, (i4 *)0, ** global_buffer, sizeof(global_buffer), ** %d*", ** node->data2, node->data1); ** uld_prnode(control, global_buffer); ** uld_prnode(control, "*****"); ** } ** ** ** then the call: ** ** uld_prtree(0, (PTR) Root, printnode, leftson, rightson, 8, 5); ** ** would print a tree where each node is 8 characters wide ** by 5 characters long and would contain data1 on the first ** line, data2 and data1 on the second line with a border of ** stars. ** ** a sample output might be: ***** * 7 * *3 7* ***** / \ ***** ***** * 4 * * 3 * *3 4* *3 3* ***** ***** / \ / \ ***** ***** ***** ***** * 3 * * 1 * * 2 * * 1 * *3 3* *3 1* *3 2* *3 1* ***** ***** ***** ***** / \ / \ ***** ***** ***** ***** * 2 * * 1 * * 1 * * 1 * *3 2* *3 1* *3 1* *3 1* ***** ***** ***** ***** / \ ***** ***** * 1 * * 1 * *3 1* *3 1* ***** ***** ** ** Parts of a tree that exceed the manifest constants are not printed ** but should not otherwise cause difficulty (supposedly). ** ** ** the main difference between this and the reference is that here a ** "parallel" tree is built to hold the info (x,modifier) that, in the ** reference, is held in the nodes of the tree being printed. ** this means that the user need not plan for this routine but can ** easily add it in later. this is almost imperative when the user ** is working with variable size nodes in his tree. ** ** Inputs: ** root Pointer to the root of the tree ** printnode Pointer to function to format a node ** leftson Pointer to function to find left son ** rightson Pointer to function to find right son ** indent Indent scale factor (number of spaces ** to indent for each horizontal level) ** lbl Lines between levels (should at least ** equal the maximum number of lines that ** the user would ever print for a node ** to ensure an evenly spaced tree) ** facility facility ID of caller ** ** Outputs: ** None ** Returns: ** None ** Exceptions: ** none ** ** Side Effects: ** none ** ** History: ** 06-may-86 (jeff) ** Adapted from fmttree() in jutil!fmttree.c in 4.0 ** 24-mar-87 (daved) ** set exclusive semaphore so that large static structures ** for the control struct are single threaded. ** 18-Jun-87 (DaveS) ** Modified to get memory from ULM instead of using statics. ** SCF semaphore removed since we no longer need it. ** 04-nov-87 (thurston) ** On ulm_startup() call, I made the block size almost as large as the ** pool since all that this routine does is create a pool, start a ** stream, and allocate one L*A*R*G*E piece out of it. (Wouldn't this ** be better served by SCU_MALLOC directly??? ** 08-feb-89 (jrb) ** Fixed calls to ule_format which made no sense at all. ** 21-may-89 (jrb) ** Updated for new ule_format interface. ** 22-sep-1992 (bryanp) ** Pass an err_code argument to ule_format. It demands one. ** 7-oct-2004 (thaju02) ** Change memleft to SIZE_TYPE. ** 19-Aug-2009 (kibro01) b122509 ** Add new uld_prtree_x function which allows an extra sc930-tracing ** parameter to uld_prtree ** 17-Aug-2010 (horda03) b124274 ** Allow Trees to be printed in connected segments to aid readability. */ VOID uld_prtree_x( i4 flags, PTR root, VOID (*printnode)(), PTR (*leftson)(), PTR (*rightson)(), i4 indent, i4 lbl, i4 facility, PTR sc930_trace ) { register i4 i; register PARATREE *pnode; ULD_CONTROL control; ULD_STORAGE *storage = 0; ULM_RCB ulm_rcb; SIZE_TYPE memleft; STATUS status; i4 err_code; i4 max_nodes; ULD_PARAM *uld_param = 0; control.facility = facility; ulm_rcb.ulm_facility = DB_ULF_ID; ulm_rcb.ulm_sizepool = sizeof(ULD_STORAGE) + 1024; ulm_rcb.ulm_blocksize = sizeof(ULD_STORAGE) + 128; memleft = sizeof(ULD_STORAGE) + 1024; ulm_rcb.ulm_memleft = &memleft; /* create a memory pool */ status = ulm_startup(&ulm_rcb); if (status != E_DB_OK) { (VOID) ule_format(ulm_rcb.ulm_error.err_code, (CL_ERR_DESC *)NULL, ULE_LOG, NULL, (char *)NULL, (i4)0, (i4 *)NULL, &err_code, 0); (VOID) ule_format(E_UL0201_ULM_FAILED, (CL_ERR_DESC *)NULL, ULE_LOG, NULL, (char *)NULL, (i4)0, (i4 *)NULL, &err_code, 0); return; } /* open the private memory stream and allocate ULD_STORAGE */ ulm_rcb.ulm_flags = ULM_PRIVATE_STREAM | ULM_OPEN_AND_PALLOC; ulm_rcb.ulm_psize = sizeof (ULD_STORAGE); ulm_rcb.ulm_streamid_p = NULL; if ((status = ulm_openstream(&ulm_rcb)) != E_DB_OK) { (VOID) ule_format(ulm_rcb.ulm_error.err_code, (CL_ERR_DESC *)NULL, ULE_LOG, NULL, (char *)NULL, (i4)0, (i4 *)NULL, &err_code, 0); (VOID) ule_format(E_UL0201_ULM_FAILED, (CL_ERR_DESC *)NULL, ULE_LOG, NULL, (char *)NULL, (i4)0, (i4 *)NULL, &err_code, 0); return; } storage = (ULD_STORAGE *)ulm_rcb.ulm_pptr; control.lbl = lbl + 1; control.isf = indent; control.pti = 0; control.ptp = storage->pts; control.ncmx = indent * 2; control.first = 0; if (flags & ULD_FLAG_OUT_SEG) { uld_param = (ULD_PARAM *) root; root = uld_param->root; control.max_level = uld_param->control->max_level; control.segments = uld_param->control->segments; control.num_segments = uld_param->control->num_segments; } else if (flags & ULD_FLAG_SEGMENTS) { control.segments = storage->segments; control.num_segments = &storage->num_segments; storage->num_segments = 0; max_nodes = 132 / control.ncmx; for (i = 1; i < sizeof(nodes_per_level)/sizeof(i4); i++) { if (max_nodes < nodes_per_level [i]) break; } control.max_level = i - 1; } else control.max_level = 0; for (i = PMX + 1; i--; ) control.pbuf[i] = &(storage->pbf[i][0]); control.modifier = storage->mod; control.next_pos = storage->ned; /* Initialize line buffers */ control.bmax = PMX; prflsh(1, &control, NULL); /* Initialize arrays */ for (i = MAXH; i--; ) { control.next_pos[i] = '\1'; control.modifier[i] = '\0'; } /* Initialize the function pointers */ control.lson = leftson; control.rson = rightson; control.pnod = printnode; /* Do the first post-order walk */ control.maxh = -1; pnode = walk1(root, 0, &control); if (uld_param) { pnode->new_root = uld_param->root_num; } /* Do the second pre-order walk */ control.modfsum = 0; walk2(pnode, &control); /* For each level, print out the nodes at that level */ control.type = ROOT; for (i = 0; i <= control.maxh; i++) { walk3(root, pnode, 0, i, &control); prflsh(0, &control, sc930_trace); } if ( !uld_param && control.max_level) { ULD_PARAM u_root; u_root.control = &control; for (i = 0; i < *control.num_segments; i++ ) { u_root.root = control.segments [i]; u_root.root_num = i + 1; uld_prtree_x( ULD_FLAG_OUT_SEG, (PTR) &u_root, printnode, leftson, rightson, indent, -lbl, facility, sc930_trace ); } } /* if we got any memory, return it */ if (storage) ulm_shutdown(&ulm_rcb); }
void rect(int t0, int t1, int x0, int x1, int y0, int y1) { printf ("using cache oblivious version\n"); walk2(t0, t1, x0, 0, x1, 0, y0, 0, y1, 0); }
/********************************* * * Main() * *********************************/ int main(int argc, char ** argv){ int ret, i, j; if( (ret=loadFile(argv[1])) < 0){ fprintf(stderr, "input file format error '%s' (ret=%d).\n", argv[1], ret); return -1; } #if DEBUG_LOADFILE fprintf(stderr, "loadFile: %d\n", ret); fprintf(stderr, "nNodes: %d\n", nNodes); fprintf(stderr, "nEdges: %d\n", nEdges); fprintf(stderr, "graphWeight: %d\n", graphWeight); printEdgeAdj(); printEdgeNames(); printEdgeWeights();//desc order #endif if (nEdges == nNodes){ //must be hamiltonian cycle if it is strongly connected //and problem statement said that we could assume it was printAnswer(); goto done; } maxOutDegree = 0; ret = 0; for (i=0; i<nNodes; i++) { for(j=0; j<nNodes; j++) if (A[i][j].m != NULL) ret ++; if (ret > maxOutDegree) maxOutDegree = ret; } //if the ratio of edges to nodes is < 2n use megbb //for(i=0; i<nNodes; i++) // dijkstra_single_source_shortest_paths(i); //printMinPaths(); /*for(i=0; i<nNodes; i++)*/{ uniqueVisited = 0; //reset visited list for(j = 0;j<nNodes; j++) visited[j] = 0; for(j = 0;j<nEdges; j++) visitedLastCross[j] = 0; //best = graphWeight;//reset this startNode = 0; walk2(startNode, 0); //printAnswer(); } printAnswer(); done: //cleanup for good form freeEdges(EdgesAdjHead); EdgesAdjHead = EdgesNameHead = EdgesCostHead = NULL; return (0); }
void walk2(int node, float totCost){ machine_t * ptr; if (!visited[node]) uniqueVisited++; visited[node]++; //base case if ( (node == startNode) && (uniqueVisited == nNodes)){ //this is a possible solution if (totCost < best) updateOptimumSoln(totCost); visited[node]--; if (!visited[node]) uniqueVisited--; return; } //first try to go to nodes you haven't visisted yet ptr = rowMin[node]; while(ptr != NULL){ if(!visited[ptr->cj]){ if( (ptr->crossed == 0) && ((totCost + ptr->edgecost) < best)){ ptr->crossed++; visitedLastCross[CALCK(ptr->ci, ptr->cj)] = uniqueVisited; walk2(ptr->cj, totCost + ptr->edgecost); ptr->crossed--; } else if ( (ptr->crossed > 0) && (ptr->crossed < maxOutDegree) ){ if(visitedLastCross[CALCK(ptr->ci,ptr->cj)] != uniqueVisited){ ptr->crossed++; visitedLastCross[CALCK(ptr->ci, ptr->cj)] = uniqueVisited; walk2(ptr->cj, totCost); ptr->crossed--; } } } ptr = ptr->nextRowMin; } //next try revisiting nodes that have already been visited ptr = rowMin[node]; while(ptr != NULL){ if (visited[ptr->cj]){ if( (ptr->crossed == 0) && ((totCost + ptr->edgecost) < best)){ ptr->crossed ++; visitedLastCross[CALCK(ptr->ci, ptr->cj)] = uniqueVisited; walk2(ptr->cj, totCost + ptr->edgecost); ptr->crossed --; } else if ( (ptr->crossed > 0) && (ptr->crossed < maxOutDegree) ){ if(visitedLastCross[CALCK(ptr->ci,ptr->cj)] != uniqueVisited){ ptr->crossed ++; visitedLastCross[CALCK(ptr->ci, ptr->cj)] = uniqueVisited; walk2(ptr->cj, totCost); ptr->crossed --; } } } ptr = ptr->nextRowMin; } done: visited[node] --; if(visited[node] == 0) uniqueVisited --; return; }