the one that is alphabetically first. Classify each edge as a tree edge or back edge, and give the pre and post number of each vertex. A B C Basic Algorithms — Spring 2017 — Problem Set 3 Due: F E 19 D March 1. DFS mechanics. For each graph, show the “DFS forest” resulting from an execution of DFS. Whenever there is a choice of vertices, choose the one that is alphabetically first. Identify the cross, forward, and back H edges, and label each vertex with its discovery Gand finishing time.I 3.2. Perform depth-first search on each of the following graphs; whenever there’s a choice of vertices, A is alphabetically first. Classify each edge pick the one that (i) (ii)as a tree edge, forward edge, back edge, or cross edge, and give the pre and post number of each vertex. D C B (a) E A G A (b) B E C H F D J I A B B C H D E FC G G H ID In each case answer questions. H G F the following F E 2. DFS mechanics. Run the DFS-based topological sort algorithm on the following graph. Whenever there is (a) In order are the components (SCCs) a choice of what vertices, choose the strongly one that connected is alphabetically first. Show thefound? “DFS forest”, including discovery 3.3. Run the DFS-based topological ordering algorithm on the following Which are source SCCs and which are sink SCCs?of the vertices. graph. Whenever you have and (b) finishing times. Give the resulting topological ordering a choice of vertices to explore, always pick the one that is alphabetically first. (c) Draw the “metagraph” (each meta-node is an SCC of G). (d) What is the minimum Anumber of edges you G graph to make it strongly D must add to this connected? C F 3.5. The reverse of a directed graph G = (V, E) is another directed graph GR = (V, E R ) on the same H E}. R vertex set, but with all edgesBreversed; that is, E E = {(v, u) : (u, v) ∈ Give a linear-time algorithm for computing the reverse of a graph in adjacency list format. (a) Indicate the pre and post numbers of the nodes. (i) B G 3.6. In an undirectedRun graph, the degree d(u) of a vertex u isalgorithm the number of neighbors u has, equiv3. DFS mechanics. the strongly-connected components on this graph. Show the or “DFS forest”, (b) number What areofthe sources and sinks of the graph? alently,discovery the edges incident Inofa the directed graph, we Draw distinguish between the including and finishing times, forupon both it. runs DFS algorithm. the resulting component What topological is of found by the indegree d(c) which is with theordering number edges intoalgorithm? u, and the outdegree dout (u), the number of in (u), graph. As usual, when faced a choice A (ii) among vertices, pick the one that is alphabetically first. edges leaving u. many topological orderings does this graph have? (d) How D C ! A B = 2|E|.C (a)3.4. Show an undirected d(u) u∈Valgorithm Runthat the in strongly connectedgraph, components on the following directed graphs G. When R (b) Use part (a) to show that in an undirected graph, there be an evenpick number of that vertices doing DFS on G : whenever there is a choice of vertices tomust explore, always the one is E alphabetically first. whose degree is odd. F F E (c) Does a similar statement hold forDthe number of vertices with odd indegree in a directed 101 graph? H J 3.7. A bipartite graph is a graph G = (V, E) whose vertices can be H G I partitioned into two sets (V = V 1 ∪V2 I and V1 ∩ V2 = ∅) such that there are no edges between vertices in the same set (for instance, if u, v ∈ V1 , then there is no edge between u and v). each case answer the following questions. 4. Dijkstra mechanics. Run Dijkstra’s algorithm on the following graph, starting from vertex A. Whenever (a) Give aconnected linear-time algorithm toone determine whether an undirected is bipartite. a) In what order are the strongly components (SCCs) found? there is a choice of vertices, choose the that is alphabetically first. Show graph the state of the algorithm just (b) There are many other ways to formulate this property. For instance, an undirected graph after each vertex is removed from the queue. b) Which are source SCCs and which are sink SCCs? is bipartite if and only if it can be colored with just two colors. c) Draw the “metagraph” (each meta-node is an SCC of G). Prove the following an undirected graph bipartite if and only if it contains d) What is the minimum number of edges you formulation: must add to this graph to make it is strongly no cycles of odd length. connected? (c) At most how many colors are needed to color in an undirected graph with exactly one odde reverse of a directed graph G = (V, E) is another directed graph GR = (V, E R ) on the same length cycle? R rtex set, but with all edges reversed; that is, E = {(v, u) : (u, v) ∈ E}. ve a linear-time algorithm for computing thehave reverse of acontainers graph in adjacency list format. 3.8. Pouring water. We three whose sizes are 10 pints, 7 pints, and 4 pints, re- Theof7-pint andu 4-pint containers start outu full an undirected graph, spectively. the degree d(u) a vertex is the number of neighbors has,oforwater, equiv- but the 10-pint container is initially empty. We are allowed one type of operation: pouring thethe contents of one container into ently, the number of edges incident upon it. In a directed graph, we distinguish between another, stopping only when the source container is empty or the degree din (u), which is the number of edges into u, and the outdegree dout (u), the number ofdestination container is full. We want to know if there is a sequence of pourings that leaves exactly 2 pints in the 7- or 4-pint ges leaving u. 1 container. ! a) Show that in an undirected graph, u∈V d(u) = 2|E|. b) Use part (a) to show that in an undirected graph, there must be an even number of vertices 5. Carrying stones. You are given a directed acyclic graph G = (V, E) with n vertices and m edges, along with two distinguished vertices s, t ∈ V . At each vertex v ∈ V , there are a number of stones q(v) (so q(v) is a nonnegative integer). Your goal is to find a path from s to t, picking up stones along the way, and arrive at t carrying as many stones as possible. At each node v along the path, you are allowed to pick up at most q(v) stones (and for simplicity, assume that q(t) = 0). However, there is a complication: each edge e ∈ E has a capacity c(e) (which is a positive integer), and you are not allowed to carry more than c(e) stones across that edge. If necessary, you can drop stones before crossing an edge. Show how to solve this problem in time O(n + m). The output should be an optimal path from s to t, and for every vertex along the path, your output should include the number of stones to be picked up or dropped at that vertex. Assume the the graph is given in adjacency list form, and that you can fetch the q and c values in constant time. 6. Gotham City. The mayor of Gotham City has made all the streets of the city one way. We can model the street layout as a directed graph G = (V, E), where the vertices V of the graph correspond to street intersections (locations), and the edges E correspond the the roads connecting intersections. So for every pair of distinct vertices u, v ∈ V , if there is an edge from u to v, then there is no edge from v to u. Assume a sparse representation for G. (a) The mayor claims that one can legally drive from any one location in the city to any other. Show how to verify the mayor’s claim using a linear-time algorithm that takes as input the graph G. (b) Suppose the mayor’s claim is not necessarily true: that it may not be possible to legally drive from any one location to any other. Let us call a location v safe if wherever you legally drive starting at v, you can always legally drive back to v. Said another way, v is safe if the following property holds: for every location w, if one can legally drive from v to w, then one can legally drive back from w to v. Give a linear-time algorithm that outputs all of the safe locations, given the graph G as input. (c) Suppose again that the mayor’s claim is not necessarily true. Nevertheless, an emergency arises, and you must drive from location s to location t, even though there may not be a legal route from s to t. You want to plan a route that traverses some edges in the graph in the reverse (i.e., illegal) direction. In order to minimize the chance of getting caught by the police, your planned route should minimize the number of illegal edges. The number of legal edges in the route is irrelevant. Give a linear-time algorithm that computes a route from s to t that minimizes the number of illegal edges (or reports that no route exists), given the graph G and locations s and t as input. 7. Help! You are given a directed graph G = (V, E), along with vertices s, t ∈ V . Some edges are labeled with a letter ‘a’–‘z’, while other edges have no label. Your goal is to find a path from s to t that spells out the word “help”. So the path should consist of edges labeled ‘h’, ‘e’, ‘l’, ‘p’, in that order, along with any number of other edges. These other edges may be either labeled or unlabeled, but unlabeled edges are preferred. So among all such paths, you should find one with the minimum number of labeled edges. Design a linear time algorithm to solve this problem. 8. Invasion. Two armies simultaneously invade a country. Let’s call them the “red army,” and the “blue army.” The red army starts out occupying city a, and the blue army starts out occupying city b. Both armies fan out simultaneously in all directions, and whichever army arrives at a city first, occupies that city, and blocks the other army from either occupying or transiting through that city. The occupying army leaves a small occupation force at that city, but the remainder of the army continues to fan out to all neighboring cities. In case of a tie, the city is occupied by neither army, and neither army may transit the city. The army that occupies the most cities wins the war. The question is: which army wins? Let’s model this problem as a directed graph with positive edge weights. The nodes in the graph represent the cities, and the edges represent roads between cities. The weight of an edge (u, v) represents the amount of time required for either army to travel from city u to city v. Design and analyze an efficient algorithm to solve this problem. The input is a directed, weighted graph, along with distinct nodes a and b. The output is “red wins,” “blue wins,” or “tie.” Observations and hints: You might think that you could just run Dijkstra twice, once starting at a and once starting at b. However, the tie-breaking rule means that this won’t work. Why? You might want to come up with an example graph that shows why this does not work. 2 To solve this, you will have to modify Dijkstra’s algorithm. The idea is to associate with each city two pieces of information: a running estimate for red’s best time to reach that city, a running estimate for blue’s best time to reach that city. In every step of the algorithm, we greedily choose one city whose status is “undecided” and move it into the “decided” category, assigning it to either red, blue, or neither, and then update the estimates for the other undecided cities accordingly. Fill in the details of the above idea, and try to carefully prove the correctness of it using a loop invariant similar to what was done in class for Dijkstra. In your proof, you should identify where we used the fact that the edge weights are positive. You can use a priority queue in your algorithm, without worrying about how that is implemented. 9. Subway madness. The city of Brombus has n subway stations v1 , . . . vn and k subway systems B1 , . . . Bk . Each system Bs has the following structure: there is a fixed entrance fee ps to enter Bs (from any station vi ), and there is a set of fares wts (vi , vj ) to go from station vi to station vj on system Bs . We assume that each entrance fee ps is a positive number, and that each fare wts (vi , vj ) is a positive number or ∞. When traveling from vi to vj from the street, one can enter any subway system Bs , pay admission ps , travel to some intermediate city v` by paying the fare wts (vi , v` ), and then (if necessary) repeat the same process, using either a different subway system (and paying its entrance fee), or using the same subway system (without needing to pay the entrance fee again), until one reaches the desired destination vj . Design an algorithm that takes as input the entrance fee and fare data as above, and outputs a table of values Mij for i, j = 1, . . . , n, where Mij is the minimum cost required to get from vi to vj . Your algorithm does not need to compute the corresponding routes. State its complexity as a function of n and k. For full credit, your algorithm should run in time O(n3 k). Hint: First, run an all-pairs shotest path algorithm on each subway system. 3
© Copyright 2024 Paperzz