typedef struct vertex * refvertex type refvertex = ^ vertex; typedef struct item * refitem refitem = ^ item; struct vertex { vertex = record int marks; marks: integer; refitem list; list: refitem; }; end; struct item { item = record refitem next; next: refitem; refvertex edge; edge: refvertex; }; end;Assume that the marks field of each vertex is initially zero!
Part a: Give readable pseudocode for an algorithm to traverse a graph with the above represenation searching for a cycle, as would be required for deadlock detection. Your algorithm will most likely be recursively formulated, and while it would be better if the algorithm was modestly efficient and terminated with the marks all set back to zero, this is not a requirement of this problem!
Part b: Give readable pseudocode for an algorithm to traverse a graph with the above represenation and mark each reachable vertexs, as would be required for garbage collection. Your algorithm will most likely be recursively formulated, and while it would be better if the algorithm was modestly efficient and terminated with the marks all set back to zero, this is not a requirement of this problem!
Assume that there is some pointer called root pointing to a block in the heap. Assume that each block in the heap may contain pointers to other blocks, with the constraint that the pointers are either valid pointers to non-free blocks or nil.
Assume that all blocks begin with an integer field called count, a count of the number of pointers in the block, and that all pointers in a block come immediately after count and before any other non-pointer data in the block.
The Problem: Write a mark-sweep garbage collector for this machine, so that a user program could use the following routine:
char * gc_malloc(s) { char * p = malloc(s); /* first try */ if (p != NIL) return p; mark(root); sweep(); return malloc(s); /* second try */ }