finsolgg

A dijkstras algorithm begins by relaxing the edges a

Info iconThis preview shows pages 7–10. Sign up to view the full content.

View Full Document Right Arrow Icon
(a) Dijkstra’s algorithm begins by relaxing the edges A-B, A-C and A-D . After these relaxations, what are the distances computed so far from A to B and to C ? Answer: Distance to B is 3. Distance to C is 1. 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
(b) Which edges does Dijkstra’s algorithm relax in the second round? Answer: C-B, C-D, C-E, C-F . (c) After the second round, what is the distance computed from A to E ? Answer: 4. (d) What is the last edge that needs to be relaxed for the algorithm to finally compute the correct shortest distance from A to E ? Answer: B-E . Grading guidelines: 5pts for each of the 4 parts. For part (b) you lose 1pt for each incorrect edge. 10. (20pts) We define a node r in a digraph G to be “potential root” if for any other node v in G there exists a directed path from r to v . (a) Draw a digraph in which every node is a potential root. Answer: Any directed cycle would do. (b) Draw a digraph without any potential roots. Answer: u-->v<--w (c) Give the pseudocode of an efficient algorithm that takes a digraph as input and finds out whether it has any potential roots. (The more efficient your algorithm, the more points you get.) Answer: Here is a first solution. For each node v we can perform directed DFS starting from v and then count the marked nodes. If there are | V | of them then v is a potential root. This takes time O ( | V | ( | E | + | V | )). But we can do better. Here is a second solution. Consider first DAGs. In a DAG there is at least one node of in-degree 0. If the DAG has two or more nodes of indegree 0 then it does not have a potential root. Given a general digraph G first determine all the strongly connected (SC) components. If the nodes in an SC component are collapsed together and the edges between them are erased then we get a DAG. Now G has a potential root iff this DAG has only one node of indegree 0. All this can be tested in time O ( | E | + | V | ). PSEUDOCODE: 1) Call a method implementing Kosaraju’s algo that finds all SC components. [[Assume the result is an array sc of size | V | such that sc[v] is an integer between 0 and K - 1 denoting the SC component to which v belongs. Here K is the number of SC components and we know that 1 K ≤ | V | .]] 2) Create an array of integers compINdeg of size K and initialize it to all 0’s. 3) For each edge v-->w if sc[v] is different from sc[w] then increment compINdeg[sc[w]] 8
Background image of page 8
[[Now compINdeg[k] is going to be the number of edges of G that begin at a node outside SC component k and end inside SC component k .]] 4) If exactly one of entries in compINdeg[k] is 0 return true. Otherwise return false. ANALYSIS: Step 1: O ( | E | + | V | ) Step 2: O ( K ) Step 3: O ( | E | ) Step 4: O ( K ) Since K ≤ | V | the whole algo runs in time O ( | E | + | V | ). Grading guidelines: 4pts each for parts (a) and (b). 12pts for a solution to part (c) that runs in linear time, O ( | E | + | V | ). 6pts for a solution to part (c) that runs in quadratic time, O ( | V | ( | E | + | V | )). 11. (10 pts) Consider a basic
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page7 / 11

a Dijkstras algorithm begins by relaxing the edges A B A C...

This preview shows document pages 7 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online