CSOR W4231.002 – Spring, 2019 hw4solns.pdf - CSOR W4231.002 – Spring 2019 hw4 solutions 1 Solution to problem 1 G has a cycle that contains e =(u v

CSOR W4231.002 – Spring, 2019 hw4solns.pdf - CSOR...

This preview shows page 1 - 3 out of 4 pages.

CSOR W4231.002 – Spring, 2019 hw4 solutions 1. Solution to problem 1 G has a cycle that contains e = ( u, v ) if and only if there is another path between v and u . Remove e = ( u, v ) from G . Run BFS( G , v ). If u appears in the BFS tree, then G has a cycle that contains e . Running time: O ( n + m ). 2. Solution to problem 2 For clarity, we’ll rename the special nodes v and w as s and t , respectively. Then the algorithm is a modified BFS, starting at node s : for each node u , we also store the number of shortest s - u paths in u.num . Initialize s.num = 1. Return t.num . . . . while Q 6 = do u = dequeue ( Q ) for each ( u, v ) E do if discovered [ v ] = 0 then . . . v.num = u.num else if dist [ v ] = dist [ u ] + 1 then v.num = v.num + u.num end if end for end while return t.num 3. Solution to problem 3 Run BFS ( G, s ). Then s appears in layer 0 and t in some layer L d with d > n/ 2. Note that distances are integers in undirected graphs, hence d ≥ d n/ 2 e . Then some layer between 1 and d - 1, say layer i , must contain exactly 1 node. Otherwise, layers 1 to d - 1 contain at least 2( d - 1) nodes, which is at least 2( d n/ 2 e - 1) = 2 · b n/ 2 c nodes. Note that s and t don’t appear in these layers but 2 · b n/ 2 c + 2 > n , the number of nodes in G ! Removing the node in layer i disconnects s from t since all graph edges that are not tree edges span at most 1 layer of the BFS tree. Running time: O ( m + n )
Image of page 1

Subscribe to view the full document.

4. Solution to problem 4 Modify procedure Update in Dijkstra’s algorithm to maintain in dist [ v ] the length of the longest edge on any s - v path. Update ( u, v ) if dist [ v ] > max { dist [ u ] , w uv } then dist [ v ] = max { dist [ u ] , w uv } prev [ v ] = u end if Now run Dijkstra’s algorithm from the origin s . Correctness and running time follow from Dijkstra’s algorithm. 5. Solution to problem 5 Note that the resulting graph is undirected, hence xy = yx . Run Dijkstra’s algorithm twice, once from s and once from t , thus computing the distances d s ( x ) between s and every other node x and the distances d t ( y ) between t and every other node y .
Image of page 2
Image of page 3
  • Fall '11
  • Berger

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes