Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assignment 03 Algorithms & Examples.

Similar presentations


Presentation on theme: "Assignment 03 Algorithms & Examples."— Presentation transcript:

1 Assignment 03 Algorithms & Examples

2 DFS Recall DFS algorithm: DFS { for each v in V do // Initialize
visit[v] = false; for each v in V do if (visit[v] == false) RDFS(v); } DFS(v) visit[v]=true; for each w in Adj(v) do if (visit[w] == false) RDFS(w);

3 Additional Info Needed
The discovery time: A counter that is incremented each time a node is discovered in the DFS. This information is needed for each node The parent: the parent of vertex v is the one that discovered v. Tree edges: any edges in the DFS. Back edges: any edges that remain and are NOT in the DFS.

4 Easy Solution The “easiest” solution would be to remove each vertex (and its corresponding edges) one by one and see whether the resulting graph is connected. However, this solution would have a O(N * (N + E)), where N is the number of vertices and E is the number of edges. Not great performance. The process we are going to look at has a big O of O(N + E).

5 Better Solution Perform DFS, recording the discovery time for each vertex and initialize low vertex value to discovery time. Find the low vertex value using these rules…. If the vertex has no children, it is the same as the discovery time If the vertex has children, then the low vertex value is equal to the lowest numbered vertex that you can reach while following the DFS tree or back edges You can use at most 1 back edge, and must stop immediately after using the back edge. Once this is complete, you can determine if removing a vertex would disconnect the graph through the following two rules: If the vertex is the root (starts the DFS) and has more than one child (in the DFS). If the vertex is NOT the root, but its discovery time is less than or equal to its child’s low vertex value.

6 1. Perform DFS & record discovery time

7 1. Perform DFS & record discovery time
DT = 1 Low = 1 DT = 0 Low = 0

8 1. Perform DFS & record discovery time
DT = 1 Low = 1 DT = 2 Low = 2 DT = 0 Low = 0

9 1. Perform DFS & record discovery time
DT = 3 Low = 3 DT = 1 Low = 1 DT = 2 Low = 2 DT = 0 Low = 0

10 1. Perform DFS & record discovery time
B is already visited, not part of DFS, back track to e, then to b, then to a. DT = 3 Low = 3 DT = 1 Low = 1 DT = 2 Low = 2 DT = 0 Low = 0

11 1. Perform DFS & record discovery time
DT = 3 Low = 3 DT = 1 Low = 1 DT = 2 Low = 2 DT = 0 Low = 0 DT = 4 Low = 4

12 1. Perform DFS & record discovery time
DT = 3 Low = 3 DT = 1 Low = 1 DT = 2 Low = 2 DT = 0 Low = 0 DT = 5 Low = 5 DT = 4 Low = 4

13 1. Perform DFS & record discovery time
DT = 3 Low = 3 DT = 1 Low = 1 DT = 2 Low = 2 DT = 0 Low = 0 DT = 5 Low = 5 DT = 6 Low = 6 DT = 4 Low = 4

14 2. Determine low vertex value
DT = 3 Low = 3 Cannot get lower than 0, low value stays DT = 1 Low = 1 DT = 2 Low = 2 DT = 0 Low = 0 DT = 5 Low = 5 DT = 6 Low = 6 DT = 4 Low = 4

15 2. Determine low vertex value
B can get to e, but 1 < 2, so leave low value. DT = 3 Low = 3 DT = 1 Low = 1 DT = 2 Low = 2 DT = 0 Low = 0 DT = 5 Low = 5 DT = 6 Low = 6 DT = 4 Low = 4

16 2. Determine low vertex value
E can get to G, but 2 < 3, so don’t update. E can also get to B (through G), Update low value to 1. DT = 3 Low = 3 DT = 1 Low = 1 DT = 2 Low = 1 DT = 0 Low = 0 DT = 5 Low = 5 DT = 6 Low = 6 DT = 4 Low = 4

17 2. Determine low vertex value
G can get to B which has a lower low value, update to 1. DT = 3 Low = 1 DT = 1 Low = 1 DT = 2 Low = 1 DT = 0 Low = 0 DT = 5 Low = 5 DT = 6 Low = 6 DT = 4 Low = 4

18 2. Determine low vertex value
DT = 3 Low = 1 DT = 1 Low = 1 DT = 2 Low = 1 DT = 0 Low = 0 DT = 5 Low = 5 DT = 6 Low = 6 DT = 4 Low = 4 D can get to C and F, but have higher low values. Don’t update.

19 2. Determine low vertex value
DT = 3 Low = 1 DT = 1 Low = 1 DT = 2 Low = 1 DT = 0 Low = 0 DT = 5 Low = 5 Can’t get anywhere else, leave at 5. DT = 6 Low = 6 DT = 4 Low = 4

20 2. Determine low vertex value
DT = 3 Low = 1 DT = 1 Low = 1 DT = 2 Low = 1 DT = 0 Low = 0 DT = 5 Low = 5 DT = 6 Low = 6 DT = 4 Low = 4 Can’t get anywhere else, leave at 6.

21 3. Determine if you can remove vertex
Root vertex and has more than one child (b and d). You cannot remove this node without disconnecting the graph! DT = 3 Low = 1 DT = 1 Low = 1 DT = 2 Low = 1 DT = 0 Low = 0 DT = 5 Low = 5 DT = 6 Low = 6 DT = 4 Low = 4

22 3. Determine if you can remove vertex
Not root, but DT is <= child’s (vertex e) low vertex value. Cannot remove w/o disconnecting graph! DT = 3 Low = 1 DT = 1 Low = 1 DT = 2 Low = 1 DT = 0 Low = 0 DT = 5 Low = 5 DT = 6 Low = 6 DT = 4 Low = 4

23 3. Determine if you can remove vertex
Not root, and DT is NOT <= child’s (vertex g) low vertex value. This vertex COULD be removed w/o disconnecting graph. DT = 3 Low = 1 DT = 1 Low = 1 DT = 2 Low = 1 DT = 0 Low = 0 DT = 5 Low = 5 DT = 6 Low = 6 DT = 4 Low = 4

24 3. Determine if you can remove vertex
Not root, and has no children. This vertex COULD be removed w/o disconnecting graph. DT = 3 Low = 1 DT = 1 Low = 1 DT = 2 Low = 1 DT = 0 Low = 0 DT = 5 Low = 5 DT = 6 Low = 6 DT = 4 Low = 4

25 3. Determine if you can remove vertex
DT = 3 Low = 1 DT = 1 Low = 1 DT = 2 Low = 1 DT = 0 Low = 0 DT = 5 Low = 5 Not root, but DT <= child’s (both c and f) low vertex value. This cannot be removed w/o disconnecting the graph. DT = 6 Low = 6 DT = 4 Low = 4

26 3. Determine if you can remove vertex
DT = 3 Low = 1 DT = 1 Low = 1 DT = 2 Low = 1 DT = 0 Low = 0 DT = 5 Low = 5 Not root, and no children. This CAN be removed w/o disconnecting the graph. DT = 6 Low = 6 DT = 4 Low = 4

27 3. Determine if you can remove vertex
DT = 3 Low = 1 DT = 1 Low = 1 DT = 2 Low = 1 DT = 0 Low = 0 DT = 5 Low = 5 Not root, and no children. This CAN be removed w/o disconnecting the graph. DT = 6 Low = 6 DT = 4 Low = 4

28 3. Determine if you can remove vertex
Nodes A, B, and D are the vertices you CANNOT remove without disconnecting the graph.

29 Pseudo Code DFS() { for every vertex v if (!visited(v)) DFS(v) } DFS(v) //Note: you can add parameters as needed visit[v]=true discoveryTime++ discoveryTime[v] = lowVertexTime[v] = discoveryTime for every adj vertex i of v if i is not visited parent[i] = v DFS(i) low[v] = min of low[v] and low[i] if v is root and children > 1 //v cannot be removed if v is not root and discoveryTime[v] <= low[i] //v cannot be removed!! else if it’s a back edge low[v] = min of low[v] and discoveryTime[i]


Download ppt "Assignment 03 Algorithms & Examples."

Similar presentations


Ads by Google