Exercises (not part of formal assignment, not graded)

Similar documents
CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Social Media Mining. Graph Essentials

Solutions to Homework 6

IE 680 Special Topics in Production Systems: Networks, Routing and Logistics*

Approximation Algorithms

Handout #Ch7 San Skulrattanakulchai Gustavus Adolphus College Dec 6, Chapter 7: Digraphs

Network (Tree) Topology Inference Based on Prüfer Sequence

Data Structures and Algorithms Written Examination

Warshall s Algorithm: Transitive Closure

Outline. NP-completeness. When is a problem easy? When is a problem hard? Today. Euler Circuits

Part 2: Community Detection

Cpt S 223. School of EECS, WSU

Network File Storage with Graceful Performance Degradation

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

International Journal of Advanced Research in Computer Science and Software Engineering

Chapter 6: Graph Theory

Solutions to Math 51 First Exam January 29, 2015

Any two nodes which are connected by an edge in a graph are called adjacent node.

6.3 Conditional Probability and Independence

136 CHAPTER 4. INDUCTION, GRAPHS AND TREES

Euclidean Minimum Spanning Trees Based on Well Separated Pair Decompositions Chaojun Li. Advised by: Dave Mount. May 22, 2014

CMPSCI611: Approximating MAX-CUT Lecture 20

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

GRAPH THEORY LECTURE 4: TREES

5.1 Bipartite Matching

International Journal of Software and Web Sciences (IJSWS)

Zachary Monaco Georgia College Olympic Coloring: Go For The Gold

Linear Programming. March 14, 2014

Graph Theory Problems and Solutions

WRITING PROOFS. Christopher Heil Georgia Institute of Technology

Scheduling Shop Scheduling. Tim Nieberg

Examination paper for MA0301 Elementær diskret matematikk

Analysis of Algorithms, I

Distributed Computing over Communication Networks: Maximal Independent Set

Chapter 3. if 2 a i then location: = i. Page 40

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

Applied Algorithm Design Lecture 5

Midterm Practice Problems

Chapter 3. Cartesian Products and Relations. 3.1 Cartesian Products

Problem Set 7 Solutions

OPTIMAL DESIGN OF DISTRIBUTED SENSOR NETWORKS FOR FIELD RECONSTRUCTION

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Arrangements And Duality

CompSci-61B, Data Structures Final Exam

Mining Social Network Graphs

SHARP BOUNDS FOR THE SUM OF THE SQUARES OF THE DEGREES OF A GRAPH

On Integer Additive Set-Indexers of Graphs

DATA ANALYSIS II. Matrix Algorithms

Introduction to Logic in Computer Science: Autumn 2006

Topology-based network security

Dynamic Programming. Lecture Overview Introduction

Diversity Coloring for Distributed Data Storage in Networks 1

A linear combination is a sum of scalars times quantities. Such expressions arise quite frequently and have the form

Sample Questions Csci 1112 A. Bellaachia

CS 598CSC: Combinatorial Optimization Lecture date: 2/4/2010

Institut für Informatik Lehrstuhl Theoretische Informatik I / Komplexitätstheorie. An Iterative Compression Algorithm for Vertex Cover

One last point: we started off this book by introducing another famously hard search problem:

Lecture 1: Systems of Linear Equations

SEMITOTAL AND TOTAL BLOCK-CUTVERTEX GRAPH

1 Solving LPs: The Simplex Algorithm of George Dantzig

On the independence number of graphs with maximum degree 3

Approximating Minimum Bounded Degree Spanning Trees to within One of Optimal

COMBINATORIAL PROPERTIES OF THE HIGMAN-SIMS GRAPH. 1. Introduction

Lecture 1: Course overview, circuits, and formulas

Mining Social-Network Graphs

The number of marks is given in brackets [ ] at the end of each question or part question. The total number of marks for this paper is 72.

Lecture 17 : Equivalence and Order Relations DRAFT

A Non-Linear Schema Theorem for Genetic Algorithms

Protein Protein Interaction Networks

Session 6 Number Theory

Discrete Mathematics Problems

Data Structures. Chapter 8

THE NUMBER OF GRAPHS AND A RANDOM GRAPH WITH A GIVEN DEGREE SEQUENCE. Alexander Barvinok

3. Mathematical Induction

The Goldberg Rao Algorithm for the Maximum Flow Problem

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

Data Structures Fibonacci Heaps, Amortized Analysis

UPPER BOUNDS ON THE L(2, 1)-LABELING NUMBER OF GRAPHS WITH MAXIMUM DEGREE

Lecture 15 An Arithmetic Circuit Lowerbound and Flows in Graphs

1 if 1 x 0 1 if 0 x 1

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Computer Science Department. Technion - IIT, Haifa, Israel. Itai and Rodeh [IR] have proved that for any 2-connected graph G and any vertex s G there

AN ALGORITHM FOR DETERMINING WHETHER A GIVEN BINARY MATROID IS GRAPHIC

Exam study sheet for CS2711. List of topics

Pigeonhole Principle Solutions

Practical Graph Mining with R. 5. Link Analysis

DETERMINANTS IN THE KRONECKER PRODUCT OF MATRICES: THE INCIDENCE MATRIX OF A COMPLETE GRAPH

The last three chapters introduced three major proof techniques: direct,

Network Flow I. Lecture Overview The Network Flow Problem

1. Prove that the empty set is a subset of every set.

Vector and Matrix Norms

Lecture 3: Finding integer solutions to systems of linear equations

The Graphical Method: An Example

Graphs without proper subgraphs of minimum degree 3 and short cycles

A Fast Algorithm For Finding Hamilton Cycles

Practical Guide to the Simplex Method of Linear Programming

A permutation can also be represented by describing its cycles. What do you suppose is meant by this?

Discuss the size of the instance for the minimum spanning tree problem.

Transcription:

CSE 101: Design and Analysis of Algorithms Homework 3 Winter 2016 Due: January 28, 2016 Exercises (not part of formal assignment, not graded) 1. DPV 5.2 (Modified). Suppose we want to find the minimum spanning tree of the following graph. (a) Run Prim s algorithm; whenever there is a choice of nodes, always use alphabetic ordering (e.g., start from node A). Draw a table showing the intermediate values of the cost array. Show the final spanning tree. (b) Suppose Kruskal s algorithm is run on this graph. In what order are the edges added to the MST? Show the final spanning tree. Solution: (a) The table below shows the cost/prev values. Set S A B C D E F G H {} 0 A 1/A 4/A 8/A A, B 2/B 4/A 6/B 6/B A, B, C 3/C 4/A 6/B 2/C A, B, C, G 1/G 4/A 1/G 1/G A, B, C, G, D 4/A 1/G 1/G A, B, C, G, D, F 4/A 1/G A, B, C, G, D, F, H 4/A A, B, C, G, D, F, H, E Final spanning tree : A B C D 4 1 1 E F G H 1 1 (b) Order in which edges are added : A-B, D-G, F -G, G-H, B-C, C-G, A-E.

Final spanning tree : A B C D 4 2 1 E F G H 1 1 2. Prove or disprove: Let G = (V, E) be a weighted graph and let T be a minimum spanning tree of G. The path in T between any pair of vertices u and v must be a shortest path in G. Solution: Disprove : Please find attached graph G = (V, E): 2 3 s v t T = MST(G): 4 2 3 s v t Shortest path between s and t in G = edge (s, t), distance = 4 3. For each of the following statements, decide whether it is true or false. If it is true, give a short explanation. If it is false, give a counterexample. (a) Suppose we are given an instance of the MST Problem on a graph G, with edge costs that are positive and distinct. Let T be a minimum spanning tree for this instance. Now suppose we replace each edge cost c e by its square c 2 e, thereby creating a new instance of the problem with the same graph but with different costs. True or False? T must be a minimum spanning tree for this new instance. (b) Suppose we are given an instance of the Shortest Path Problem on a directed graph G. We assume that all edge costs are positive and distinct. Let P be a minimum cost s t path for this instance. Now suppose we replace each edge cost c e by its square c 2 e, thereby creating a new instance of the problem with the same graph but with different costs. True or False? P must still be a minimum-cost s t path for this new instance. Solution: (a) True. If we feed costs c 2 e into Kruskal s Algorithm, it will sort them in the same order, and thus put the same subset of edges in the MST. It is crucial here to notice that Krushkal s Algorithm only cares about the relative order of the costs, not the actual value.

(b) False. Consider the following graph. s 3 t 2 2 v shortest path between s and t is single edge (s, t) with value 3. After squaring the costs the shortest path would be s v t with value 8 rather than s t with value 9. 4. DPV 5.14. Suppose the symbols a, b, c, d, e occur with frequencies 1/2, 1/4, 1/8, 1/16, 1/16 respectively. (a) What is the Huffman encoding of the alphabet? (b) If this encoding is applied to a file consisting of 1, 000, 000 characters with the given frequencies, what is the length of the encoded file in bits? Solution: (a) Symbol Encoding a 0 b 10 c 110 d 1110 e 1111 (b) Length = (1/2 1 + 1/4 2 + 1/8 3 + 1/16 4 + 1/16 4) 1, 000, 000 = 1, 875, 000 (or n i=1 mp ilog(1/p i )).

Problems (must be written up and turned in) 1. Given an edge-weighted undirected connected chain-graph G = (V, E), all vertices having degree 2, except two endpoints which have degree 1 (there is no cycle). Design an algorithm that preprocesses the graph in linear time and can return the distance of the shortest path between any two vertices in constant time (i.e., the O( V ) preprocessing enables return of the shortest-path distance between any two vertices in O(1) time). Give an English description of your algorithm along with pseudocode, and give an analysis of runtime complexity. Solution: Given that this is a linear chain, we can run DFS from the source vertex and record the distance from the source to each vertex in this chain (shortest path to each vertex). To get the distance between any two vertices u and v, we would simply do dist(u) dist(v). Pseudocode v is vertex with v.degree = 1 Preprocess(Graph g, Vertex v) visited(v) = true v.dist = 0 Stack s s.push(v) while(s.size()>0) v = s.top() flag = 0 for each edge(v, u) in E if not visited(u): visited(u) = true u.dist = v.dist + edge(v,u) s.push(u) flag = 1 break if flag==0 // all neighbors visited stack.pop() Dist(Vertex u, Vertex v, Graph preprocessed) return u.dist v.dist Preprocess has run time of DFS, O( V + E ). Preprocess must iterate through every vertex and edge, thus the best run time must be linear. Dist has a runtime of O(1) since preprocess will have labeled each vertex with a distance from source, we only need one operation to get the distance between two vertices.

2. Recall interval scheduling problem from class (the interval scheduling problem is to find a largest compatible set - a set of non-overlapping intervals of maximum size). Suppose that instead of always selecting the first activity to finish, we select the last activity to start that is compatible with all previously selected activities. Describe how this approach is a greedy algorithm, and prove that it yields an optimal solution. Solution: How Algorithm is Greedy: Greedy algorithm makes a locally optimal choice in the hope that this choice will lead to a globally optimal solution. The algorithm proposed here chooses jobs that start last as the locally optimal choice. Proof of optimality: We are given a set S = a 1, a 2,, a n of activities, where a i = [s i, f i ), and the algorithm proposed finds an optimal solution by selecting the last activity to start that is compatible with all previously selected activities. Instead, let us create a set S = a 1, a 2,, a n, where a i = [f i, s i ). That is, a i is a i in reverse. Clearly, a subset of a i1, a i2,, a ik S is mutually compatible if and only if the corresponding subset a i1, a i2,, a ik S is also mutually compatible. Thus, an optimal solution for S maps directly to an optimal solution for S and vice versa. The algorithm proposed selects the last activity to start (in S) that is compatible with all previously selected activities in S, this algorithm is equivalent to selecting the first activity to finish (in S ) that is compatible with all previously selected activities in S. Since an optimal solution for S maps directly to an optimal solution for S, the algorithm proposed yields an optimal solution. 3. Let T be an MST of a weighted, undirected graph G. Given a connected subgraph H of G, show that T H is contained in some MST of H. Solution: Let T H = {e 1, e 2, e k }. We use the cut property repeatedly to show that there exists an MST of H containing T H. Cut property states that, for a graph G, let R be any subset of nodes, and let e be the min cost edge with exactly one endpoint in R. Then the MST T contains e. Suppose for i 0, X = {e 1, e 2, e i } is contained in some MST of H. Removing the edge e i+1 from T divides T in two parts giving a cut (S, G\S) and a corresponding cut (S 1, H\S 1 ) of H with S 1 = S H. Now, e i+1 must be the lightest edge in G (and hence also in H) crossing the cut, else we can include the lightest and remove e i+1 to get a better tree. Also, no other edges in T, and hence in X, cross the cut. We can then apply the cut property to get that X e i+1 must be contained in some MST of H. Continuing in this manner we will get that T H = {e 1, e 2, e k } must be contained in some MST of H.

4. Given an edge-weighted undirected graph G = (V, E), such that E > V and all edge-weights are distinct. We define a second-best minimum spanning tree as follows. Let T be the set of all spanning trees of G, and let T be a MST of G. Then a second-best minimum spanning tree is a spanning tree T such that W (T ) = min T T {T }(W (T )), W ( ) denotes weight of spanning tree. (a) Show that the minimum spanning tree is unique, but that the second-best minimum spanning tree need not be unique. (b) Let T be the minimum spanning tree of G. Prove that G contains edges (u, v) T and (x, y) / T such that T - {(u, v)} {(x, y)} is a second-best minimum spanning tree of G. (c) Let T be a spanning tree of G and for any two vertices u, v V, let max(u, v) denote an edge of maximum weight on the unique simple path between u and v in T. Design an O( V 2 ) time algorithm that, given T, computes max(u, v); for all u, v V. Give an English description of your algorithm along with pseudocode, and give an analysis of runtime complexity. (d) Design an efficient algorithm to compute the second-best minimum spanning tree of G. Give an English description of your algorithm along with pseudocode, and give an analysis of runtime complexity. Solution: (a) Since the graph is connected and all edge weights are distinct, for each cut (S, S ), there is a unique light edge (i.e., of smallest cost c e ) crossing the cut (call it e S ). By the cut property, each such edge e S must be part of every minimum spanning tree. Define E = {e S (S, S ) is a cut}, where E itself is already connected, because it contains an edge for every cut. Hence, E itself is the MST of G which would be unique. The second-best minimum spanning tree need not be unique, below is a weighted, undirected graph with a unique minimum spanning tree of weight 18 and two second-best minimum spanning trees of weight 19. Graph and MST: A B C A B C 7 6 5 4 3 7 5 3 D E F D E F Second Best minimum spanning trees: A B C A B C 7 5 4 7 6 3 D E F D E F

(b) Since any spanning tree has exactly V 1 edges, any second-best minimum spanning tree must have at least one edge that is not in the (best) minimum spanning tree. If a secondbest minimum spanning tree has exactly one edge, say (x, y), that is not in the minimum spanning tree, then it has the same set of edges as the minimum spanning tree, except that (x, y) replaces some edge, say (u, v), of the minimum spanning tree. In this case, T = T {(u, v)} {(x, y)}, as we wished to show. Thus, all we need to show is that by replacing two or more edges of the minimum spanning tree, we cannot obtain a second-best minimum spanning tree. Let T be the minimum spanning tree of G, and suppose that there exists a second-best minimum spanning tree T that differs from T by two or more edges. There are at least two edges in T T, and let (u, v) be the edge in T T with minimum weight. If we were to add (u, v) to T, we would get a cycle c. This cycle contains some edge (x, y) in T T (since otherwise, T would contain a cycle). Since the edges (u, v) and (x, y) would be on a common cycle c, the set of edges T {(x, y)} {(u, v)} is a spanning tree, and its weight is less than w(t ). Moreover, it differs from T (because it differs from T by only one edge). Thus, we have formed a spanning tree whose weight is less than w(t ) but is not T. Hence, T was not a second-best minimum spanning tree. Figure below indicates edge (u, v) and (x, y) for example taken in part (a). A B C 7 5 = (u, v) D 6 = (x, y) E F 3 (c) Algorithm: We will do breadth first search from vertex u, having restricted the edges visited to those of the spanning tree T. This will give us max(u, v) for all vertex v u since we are visiting via edges in a spanning tree of an undirected graph, we are guaranteed that the search from each vertex u will visit all vertices. This procedure is done for all the vertices u V to get max(u, v) for all vertices u and v Pseudocode: max[v,v] = NULL //Initialization Bfs-Max(G,T) for each vertex u V Q.enqueue(u) while Q is not empty x = Q.dequeue() for each v Adj[x] if max[u, v] == NULL and v u if x == u or w(x, v) > max[u, x] max[u, v] = (x, v) else max[u, v] = max[u, x] Q.enqueue(v) Runtime Complexity: We are doing BFS from each vertex u V. Hence runtime complexity of above algorithm is O V ( V + E ) = O( V 2 ). E = V 1 for spanning tree. (d) Algorithm to find second best minimum spanning tree is as follows:

i. Compute the minimum spanning tree T using Prim s or Kruskal s Algorithm. ii. Given the minimum spanning tree T, compute the max table, as in part (c). iii. Find an edge (u, v) / T that minimizes w(max[u, v]) w(u, v). iv. Having found an edge (u, v) in step (iii), return T = T {max[u, v]} {(u, v)} as a second best minimum spanning tree. Since adding (u, v) completes a cycle, and we remove an MST edge max[u, v] to regain a spanning tree topology, minimizing the cost increase w(u, v) w(max[u, v]). Pseudocode Second-best-mst(G = (V,E)) T = (V, E ) Prim salgorithm(g= (V,E)) max[v,v] Bfs-Max(G,T) max val = 0 for all edges (u,v) / E if w(max[u,v]) - w(u,v) > max val edge picked = (u, v) return T - {max[edge picked]} {edge picked} Runtime complexity Time complexity for Prim s algorithm is O(E + V log(v )) which is O( V 2 ), time complexity to fill matrix max[v,v] is O( V 2 ) (from part (c)). To find out edge that minimizes w(max[u, v]) w(u, v) will take time O( E ) which is again O( V 2 ), hence overall time complexity of the algorithm is O( V 2 ).

5. Given a graph G = (V, E), a subset S V of vertices is said to be a vertex cover of G if for every edge (u, v) E, at least one of u, v belongs to the subset S. A minimum vertex cover of G is a vertex cover with minimum cardinality (i.e., smallest vertex cover). Design an algorithm to find a minimum vertex cover of a given tree T = (V, E). Justify why your algorithm works, give pseudocode, and give an analysis of runtime complexity. Solution: The following claim will lead to an algorithm. Claim: Let v be any vertex in G that has only one incident edge and let this edge connect v to another vertex u. Let G be the graph obtained from G by removing u and its incident edges from G. Let S be a minimum vertex cover of the graph G. Then S {u} is a minimum vertex cover of G. Proof: First, we argue that there is a minimum vertex cover of G that includes the vertex u. Suppose for the sake of contradiction that no minimum vertex cover of the graph includes u. Consider a minimum vertex cover R. Since u / R, this means that v R. Consider the set R = R {v} {u}. Note that R is also a vertex cover which is a contradiction. Suppose for the sake of contradiction assume that S {u} is not a minimum vertex cover of G. Let Q be a minimum vertex cover of G that includes u (from the argument above we know there exists such a set). So, we can write Q = Q {u} such that u / Q. Note that Q is a vertex cover of G (since u does not cover any edges in G ). This is a contradiction, since from our assumption Q < S. The correctness of the following algorithm easily follows from the above claim. Note that tree (or the forest of trees) will always have a vertex v that has only one incident edge (leaf vertex) unless there are no edges in the forest of trees. Pseudocode: TreeVertexCover(G = (V, E)) S = {} while there are no edges in G Let v be a vertex with exactly one incident edge (u, v) S = S {u} Remove u and its incident edges from G to obtain G G = G return S Runtime complexity: We need to maintain the degrees of vertices in the current graph and vertices with degree 1. Updating the degrees after every iteration will cost time proportional to the degree of the vertex being removed. So, the total time will be proportional to the sum of degrees which in this case would be O( V ).

6. Given an edge-weighted directed graph G = (V, E) such that all the edge-weights are positive. Let s and t be two vertices in G and k V be an integer. Design an algorithm to find the shortest path from s to t that contains exactly k edges. Give an English description of your algorithm along with pseudocode, and give an analysis of runtime complexity. Note that the path need not be simple, and is permitted to visit vertices and edges multiple times. Solution: For this, we will use dynamic programming. Our base condition is that for k = 0 all distances will be set to infinity except s = 0. At any given m, from m = 1 to k, we can find the distance to any vertex u using exactly m edges as: D[m, u] = min{d[m 1, x] + w[x, u]} for all edges (x, u) where x is a predecessor of u with an edge from x to u. If there are no incoming edges, D[m, u] =. Using this formula, we can build a k by V matrix, which will keep track of all shortest path weights using exactly m edges from s at iteration m. We will find the weight of the shortest path in row k, column vertex t (which will give shortest path weight from s to t using exactly k edges.), from there we must backtrack to reconstruct the shortest path. Pseudocode: Shortest-path-k-edges(G, k, s, t) D[k,V], P[k,V] -Initialization D[0,s] = 0; for all vertices u except s for m = 0 to k: D[m,u] = for m = 1 to k: for every edge(x,u): if (D[m-1,x] + w[x,u]) < D[m, u] D[m, u] = D[m-1,x] + w[x,u] P[m, u] = x Path = emtpy list if D[k,t] = Return no path last = t for m = k down to 0: Path.append(P[m,last]) last = P[m,last] Path = Path.reverse() Return Path Runtime Complexity Initializing matrix has complexity O(k V ) and for each iteration, we are going through each edge E to update Distance matrix D and Path matrix P. In total there will be k iterations to fill matrices. Hence time complexity in building matrices is O(k E ). Traversing through the path has time complexity k. Hence, overall time complexity is O(k( E + V )).