|
| 1 | +package problems.ongraph; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +public class DeadlockDetection { |
| 7 | + public static enum Color {WHITE, GRAY, BLACK}; |
| 8 | + |
| 9 | + public static class GraphVertex { |
| 10 | + public List<GraphVertex> edges; |
| 11 | + Color color; |
| 12 | + |
| 13 | + public GraphVertex() { |
| 14 | + edges = new ArrayList<>(); |
| 15 | + this.color = Color.WHITE; |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + public static boolean isDeadlocked(List<GraphVertex> graph) { |
| 20 | + |
| 21 | + for (GraphVertex g : graph) { |
| 22 | + if (g.color == Color.WHITE && hasCycle(g)) { |
| 23 | + return true; |
| 24 | + } |
| 25 | + } |
| 26 | + return false; |
| 27 | + } |
| 28 | + |
| 29 | + private static boolean hasCycle(GraphVertex g) { |
| 30 | + if (g.color == Color.GRAY) |
| 31 | + return true; |
| 32 | + |
| 33 | + g.color = Color.GRAY; |
| 34 | + for (GraphVertex path : g.edges) { |
| 35 | + if (path.color != Color.BLACK) |
| 36 | + if (hasCycle(path)) return true; |
| 37 | + } |
| 38 | + g.color = Color.BLACK; |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * High performance database systems use multiple processes and resource locking. |
| 44 | + * These systems may not provide mechanisms to avoid or prevent deadlock: a situation |
| 45 | + * in which two or more competing actions are each waiting for the other to finish, |
| 46 | + * which precludes all these actions from progressing. Such systems must support a |
| 47 | + * mechanism to detect deadlocks, as well as an algorithm for recovering from them. |
| 48 | + * One deadlock detection algorithm makes use of a "wait-for " graph to track which |
| 49 | + * other processes a process is currently blocking on. In a wait-for graph, processes |
| 50 | + * are represented as nodes, and an edge from process P to 0 implies 0 is holding a |
| 51 | + * resource that P needs and thus P is waiting for 0 to release its lock on that resource. A |
| 52 | + * cycle in this graph implies the possibility of a deadlock. This motivates the following |
| 53 | + * problem. |
| 54 | + * Write a program that takes as input a directed graph and checks if the graph contains |
| 55 | + * a cycle. |
| 56 | + * |
| 57 | + * We can check for the existence of a cycle in G by running DFS on G. Recall |
| 58 | + * DFS maintains a color for each vertex. Initially, all vertices are white. When a vertex |
| 59 | + * is first discovered, it is colored gray. When DFS finishes processing a vertex, that |
| 60 | + * vertex is colored black. |
| 61 | + * As soon as we discover an edge from a gray vertex back to a gray vertex, a cycle |
| 62 | + * exists in G and we can stop. Conversely, if there exists a cycle, once we first reach |
| 63 | + * vertex in the cycle (call it v), we will visit its predecessor in the cycle (call it u) before |
| 64 | + * finishing processing v, i.e., we will find an edge from a gray to a gray vertex. In |
| 65 | + * summary, a cycle exists if and only if DFS discovers an edge from a gray vertex to a |
| 66 | + * gray vertex. Since the graph may not be strongly connected, we must examine each |
| 67 | + * vertex, and run DFS from it if it has not already been explored. |
| 68 | + * |
| 69 | + * Variant: Solve the same problem for an undirected graph. |
| 70 | + * Ans : locic will remain same |
| 71 | + * |
| 72 | + * |
| 73 | + * Variant: Write a program that takes as input an undirected graph, which you can |
| 74 | + * assume to be connected, and checks if the graph remains connected if any one edge |
| 75 | + * is removed. |
| 76 | + * Ans : this about connected component |
| 77 | + */ |
| 78 | +} |
0 commit comments