March 3
homework
Discuss; see where we are.
See attached python (improved and completed graphs.py) for 3.2a and 3.2b.
chapter 4 topics :
1. Discuss how to implement a breadth-first search,
and the use of stacks and queues in non-recursive
tree searches.
2. Discuss the idea and method behind Dijkstra's minimum path algorithm. Mention the "greedy" approach.
3. Discuss the notion of a "heap", and simple vs sophisticated implementations.
What is the O() runtime of Dijkstra's with a good heap? What is the O() of the heap operations?
midterm check-in
languages methods analysis structures classic problems
--------- ------------------ --------- ----------- -----------------
python brute force O() linear array sorting
C divide-and-conquer linked lists etc FFT
greedy queue, stack graph properties
heap
Midterm project? Spring break timing? Discuss.
system("dot ...") issue
Looks like I was doing some unclean stuff in my binary_tree.c from last week.
I put in an unintentional "race condition":
since I didn't close the "tree.dot" file explicitly, and fired up the "/usr/local/bin/dot"
immediately, it was possible that the .dot file wasn't in a finished writing state before
the dot program tried to use it. Trying to replicate a problem Ned was getting gave me
situations where the .png file was empty ... without getting any errors.
So here's the improved code.
// Write 'tree.dot' graphviz file,
// and use that to create a .png of the graph.
void make_dotfile(node tree){
FILE* dotfile;
int status;
if ((dotfile = fopen("tree.dot", "w"))==NULL){
printf("Oops - error opening tree.dot\n");
exit(1);
}
fprintf(dotfile, "digraph tree {\n");
write_dot_node(dotfile, tree);
fprintf(dotfile, "}\n");
status = fclose(dotfile); // Here's the bugfix: close tree.dot before using it.
printf(" fclose(dotfile) returned %i\n", status);
printf(" -- making dotfile ... \n");
status = system("dot -v -Tpng < tree.dot > tree.png 2> tree.stderr "); // -v is "verbose"
printf(" system(dotfile...) finished; status=%i \n", status);
}
That does several things more thorougly:
- It prints the status code (0 for success, something else for various errors) after each file or system operation,
- It closes the .dot file.
- The system("dot ...") command reads from tree.dot, writes to tree.png, and sends error messages (and diagnostics) to tree.stderr.
- Putting in -v before -Tpng makes dot run "verbose", sending out more details of what it's doing to stderr.