Homeok Solution Unit : Chapte CMPSC 65 Diclaime: Thi i a daft of olution that ha been pepaed b the TA and the intucto make no guaantee that olution peented hee contain the leel of detail that ould be expected on an exam. An eo o explanation ou find unclea hould be epoted to eithe of the TA fo coection fit. Execie.- Gien an adjacenc-lit epeentation of a diected gaph, ho long doe it take to compute the out-degee of ee etex? Ho long doe it take to compute the in-degee? Gien an adjacenc-lit epeentation Adj of a diected gaph, the out-degee of a etex u i equal to the length of Adj[u], and the um of the length of all the adjacenc lit in Adj i E. Thu the time to compute the out-degee of ee etex i Θ ( V + E ). The in-degee of a etex u i equal to the numbe of time it appea in all the lit in Adj. If e each all the lit fo each etex, the time to compute the in-degee of ee etex i Θ ( V E ). (Altenatiel, e can allocate an aa T of ize V and initialize it entie to zeo. Then e onl need to can the lit in Adj once, incementing T [u] hen e ee u in the lit. The alue in T ill be the in-degee of ee etex. Thi can be done in Θ ( V + E ) time ith Θ ( V ) additional toage.) Page of 8 PSU CMPSC 65 Sping
Execie.- Gie an adjacenc-lit epeentation fo a complete bina tee on 7 etice. Gie an equialent adjacenc-matix epeentation. Aume that etice ae numbeed fom to 7 a in a bina heap. A complete bina tee look like: 5 6 7 An adjacenc-lit epeentation of thi tee i hon belo: 5 6 7 5 6 7 An equialent adjacenc-matix epeentation of thi tee i hon belo: 5 6 7 5 6 7 Page of 8 PSU CMPSC 65 Sping
Execie.- Sho the dit and ped alue that eult fom unning beadth-fit each on the diected gaph belo, uing etex a the ouce. 5 6.ped 5 6 (a).ped 5.ped (d).ped :5 6.ped 5.ped (b).ped 6.ped 5 6.ped.ped : 5.ped.ped :5 (e) 6.ped.ped 5.ped.ped :5 6.ped 6.ped.ped : 5.ped.ped :5 6.ped (c) (f) The pocedue of the beadth-fit each i hon aboe. Fom the eult, e can ee that: Node Ped dit NIL 5 6 5 NIL Page of 8 PSU CMPSC 65 Sping
Execie.- Sho the dit and ped alue that eult fom unning beadth-fit each on the undiected gaph belo, uing etex u a the ouce. t u x (a) t (f).ped : x u.ped (b) (g).ped :.ped : t x.ped (c) (h).ped :.ped :.ped x 5.ped.ped : 5 (d) (i).ped :.ped :.ped 5.ped.ped : (e).ped Page of 8 PSU CMPSC 65 Sping
The pocedue of the beadth-fit each i hon aboe. Fom the eult, e can ee that: Node Ped dit u NIL t u x u u t 5 Execie.- Make a -b- chat ith o and column label WHITE, GRAY, and BLACK. In each cell (i, j), indicate hethe, at an point duing a depth-fit each of diected gaph, thee can be an edge fom a etex of colo i to a etex of colo j. Fo each poible edge, indicate hat edge tpe it can be. Make a econd uch chat fo depth-fit each of an undiected gaph. Diected gaph: WHITE GRAY BLACK WHITE tee, back, foad, and co back and co co GRAY tee and foad tee, foad, back tee, foad, and co BLACK back and co tee, foad, back and co Undiected gaph: WHITE GRAY BLACK WHITE tee and back tee and back GRAY tee and back tee and back tee and back BLACK tee and back tee and back Execie.- Sho ho depth-fit each ok on the gaph of Figue.6. Aume that the fo loop of line 5 7 of the DFS pocedue conide the etice in alphabetical ode, and aume that each adjacenc lit i odeed alphabeticall. Sho the dicoe and finihing time fo each etex, and ho the claification of each edge. Page 5 of 8 PSU CMPSC 65 Sping
The dicoe and finihing time fo each etex ae hon in the figue belo:,6 7,,7 8,5 8,9,6,5 9,,, Tee edge: (q, ), (, ), (, ), (q, t), (t, x), (x, z), (t, ), (, u) Back edge: (, ), (z, x), (, q) Foad edge: (q, ) Co edge: (, ), (u, ) Execie.- Sho the paenthei tuctue of the depth-fit each of Figue.. Figue. (p) Execie.-7 Reite the pocedue DFS, uing a tack to eliminate ecuion. Aume that the tack ha folloing opeation: PUSH(S, ) puhe into the tack; POP(S) etun the top of the tack and emoe it; TOP(S) etun the top of the tack ithout emoing it. Page 6 of 8 PSU CMPSC 65 Sping
Denote an empt tack b EMPTY. Then the peudocode of thi algoithm become a follo: DFS_STACK (G, ) fo each etex u V(G) {} { u.colo = WHITE = NIL } time = S = EMPTY time = time +.t d = time.colo = GRAY PUSH(S, ) hile S!= EMPTY { t = TOP(S) if V(G).Adj[t].t..colo == WHITE // adjacenc lit han t been full examined {.ped = t time = time +.t d = time.colo = GRAY PUSH(S, ) } ele // adjacenc lit ha been full examined } { } t = POP(S) time = time +.t f = time.colo = BLACK Execie.- Sho the odeing of etice poduced b TOPOLGICAL-SORT hen it i un on the dag of Figue.8, unde the aumption of Execie.-. Accoding to the aumption of Execie.-, the fo loop of line 5 7 of the DFS pocedue Page 7 of 8 PSU CMPSC 65 Sping
conide the etice in alphabetical ode, and that each adjacenc lit i odeed alphabeticall. DFS on Figue.8: / /6 /5 6/9 /5 7/8 / / 7/8 /7 / 5/6 9/8 / Odeing of etice: Execie.- Gie a linea-time algoithm that take a input a diected acclic gaph G = (V, E) and to etice and t, and etun the numbe of imple path fom to t in G. Fo example, the diected acclic gaph of Figue.8 contain exactl fou imple path fom etex p to etex : po, po, po, and p. (You algoithm need onl to count the imple path, not lit them.) Add a field to the etex epeentation to hold an intege count. Initiall, et etex t count to and othe etice count to. Stat unning DFS ith a the tat etex. When t i dicoeed, it hould be immediatel maked a finihed (BLACK), ithout futhe poceing tating fom it. Subequentl, each time DFS finihe a etex, et count to the um of the count of all etice adjacent to. When DFS finihe etex, top and etun the count computed fo. Page 8 of 8 PSU CMPSC 65 Sping