cs180_hw2-final

pdf

School

University of California, Los Angeles *

*We aren’t endorsed by this school

Course

180

Subject

Computer Science

Date

Oct 30, 2023

Type

pdf

Pages

7

Report

Uploaded by GrandLeopardPerson487

CS 180 HW2 Arjun Raj Loomba 605809574 Problem 1 (a) We define a graph G ⇒ ∃ tree, back/forward, and cross edges. DFS cannot result in cross-edges and only in back/forward edges, and tree edges, which we will analyse. As- sume V is the set of all nodes, and E is a set of all edges in G .We establish that u v and v , v u ( u, v ) V . To prove that if removing an edge e E that connects the vertices u and v will disconnect the graph, we first prove that removing a back-edge will not disconnect the graph. Then, we prove that removing a tree-edge will disconnect the graph. In a situation where there are multiple subtrees of v that are represented by the set S where v S and multiple back-edges from S to u or any other ancestor of u, then even if edge e , which in this case is a back-edge is removed, there still remains other back-edges which will connect the graph. This proves by contradiction that it can’t be a back-edge. The same would hold for a case with forward edges. Since this is DFS on an undirected graph any cross-edge, because it would aldready be traversed, therefore we only traverse through descendants and ancestors of u . This leaves only tree-edges, where if we remove a tree edge e , and forward/backward edges from u k v , then DFS will not traverse through all ancestors, and descendants. In this case, the above statement of removing edge e disconnecting the graph if it’s a tree edge is True (b) Assuming we have an undirected connected Graph ( V , E ) G , we prove that removing 2 nodes from any G wouldn’t disconnect G . We divide all undirected graphs into 2 categories: Cyclic and Acyclic. Trees: All acyclic connected graphs : In this case, an undirected graph G forms a tree, which is defined as a connected, acyclic, undirected graph. This also includes other seemingly different acyclic graphs, such as a complete bipartite graph or a star. In all such cases, more than 1 node with degree equal to 1. If both such nodes are removed then the graph’s connectivity is unaffected. These nodes will either have indegree = 0 or outdegree = 0. Either, there will be 2 nodes that don’t have any descendants (like a conventional binary tree or a star) or a single node with no ancestors and another node with no descendants. This proves that no acyclic, undirected graph G is disconnected if 2 specific nodes are removed. All other graphs such as Cyclic, connected graphs: In this case, an undirected graph has a cycle if a path that starts and ends at the same node ( v 1 , v 2 , ..., v n 1 , v n ) : v n = v 1 Using BFS (Breath First Search), we can convert the cyclic graph into a BFS tree. This BFS tree structure can now employee above case for Trees: All acyclic connected graphs . There’s also another proof if each node in our cycle has degree 2. Removing the 2 nodes that are adjacent in the cycle G will not disconnect the graph. In the above example, this implies removing node v n 1 and v 1 = v n , will still leave the graph connected. Since proven for all cases of possible undirected graphs, the statement is True 1
CS 180 HW2 Arjun Raj Loomba 605809574 (c) Assume that a graph G is not connected, for a solution with proof by contradiction, to prove that v V (set of all Nodes in the graph), if deg ( n ) n 2 , then G is connected. Assuming that G is not connected and it consists of more than 1 connected component C i : i (1 , 2 , ..., n 1 , n ): C = { C 1 , C 2 , C 3 , ..., C n } For our proof, we can consider only 2 connected components, C 1 , C 2 : ( C 1 , C 2 ) G and | C 1 | ≤ | C 2 | . This implies that C 1 is the smallest connected component ⇒ | C 1 | ≤ n 2 . Let’s assume that we have a node u : u C 1 . Then even in the best case, where all other nodes in C 1 are connected to u , deg ( u ) is | C 1 |− 1. In all cases: | C 1 | − 1 < n 2 | C 1 | − 1 n 2 1 n 2 1 < n 2 Since u can’t possibly have a degree of n 2 , then every node doesn’t have a degree n 2 , if the graph G is not connected. Therefore, by contradiction, if deg ( u ) n 2 then it has to be connected. Therefore, the given statement is True 2
CS 180 HW2 Arjun Raj Loomba 605809574 Problem 2 (a) A min heap is typically used to discern the minimum value in a given set of num- bers. In this case, however, we’re tasked to find the K th smallest value without removing it. Assuming that our algorithm is defined as a class, then upon initializing this class into an object, the value of k is defined to start with. We assume that the set S with values from 0 , 1 , ..., n is aldready in sorted order. Data Structure Construction : Based on the value of K , our data-structure, is a max- heap and an array. The max-heap will contain all values from (1 st , 2 nd , ..., ( k 1) st ) < k th , and will be heapified when the class is initiated based on the value of k (i.e. the root node is the k th node). All remaining values can be stored in an array with values from k + 1 , k + 2 , ..., n 1 , n . The K th smallest value becomes the largest value, i.e. the head-node of the heap. Therefore, there may be operations on the heap in O ( logK ) time. Since the K th smallest element is the root node of the heap, the function Find Kmin will return the head/root node of the heap, which is the K th smallest value in the set. Since O (1), which is the time complexity of accessing the root node, as we point to the root node to begin with: O (1) = O ( logK ). If we wish to insert a value into our new data-structure in log ( k ) time, then there are two different cases. Let’s assume, we wish to insert a value: v . First, we must check whether v k min or is v < k min : v K min , then our new value v is pushed onto the end of of our array in O (1) = O ( logK ) time. v < K min , then v is added, as a leaf node to the leftmost empty position on the heap. Then the max-heap is heapified up, where key ( w ), which is assumed as the value associated with the parent node must be key ( v ) Since there are k elements to deal with, this algorithm must be completed in O ( logK ) time. It’s important to note that because the total number of nodes must equal k in the heap to preserve O ( logK ) time. Therefore, the current K th smallest value node will be popped in O (1) time and pushed onto the array, and a new k t h smallest value will be determined. O ( logK + 1) = O ( logK ), which is also valid. (b) In part (a) , a max-heap and an array is employed, with an array. However, to support pop() functionality, the data structure implementation must be partially different. Data Structure Construction: A max-heap like (a), that contains all values from (1 st , 2 nd , ..., ( k 1) st ) < k th . For the remaining values there’s a min-heap ( k + 1) st < ( k + 2) nd , ..., ( n 1) st , n th The min-heap is essential for the pop() operation which is explained in the list below. And replaces the array in (a) 3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
CS 180 HW2 Arjun Raj Loomba 605809574 Find Kmin same as (a), where the head of the max-heap is taken in O (1) = O ( logk ) Since, k n , then O ( logk ) = O ( logn ), which is satisfied. In the push operation, like (a) there are 2 cases. For the first case, if the new node to be inserted is v , then if v < K min v is added as a leaf node in the max-heap and follows the same explanation as (a) such that the max-heap is heapified up, and follows O ( logk ) time. However if v K min , the new node is inserted into the max-heap, and instead of popping K min into the array, we insert it into the min-heap, which is reheapified in O ( log ( n k )) = O ( logn ) since, k n Therefore, the overall operation is completed in O ( logn ) time. Now for the new pop() operation on the K th smallest value, this value is removed and the max-heap is reheapified. However the size of the max-heap has to preserved as size k . Therefore, the min-heap head is removed in O (1), then the min-heap is reheapified in O ( log ( n k ) = O ( logn ). This value is added into the max-heap which is heapified up in O ( logk ) = O ( logn ). Overall the time complexity will remain O ( logn ) for popping the K min value. All operations are completed in O ( logn ) in the worst case, which is the parameter of the question. 4
CS 180 HW2 Arjun Raj Loomba 605809574 Problem 3 Assume that there is a Directed Graph G where each node is defined as b i , d i i (1 , 2 , ..., n ) b i B and d i D : B , D are birth and death dates respectively P i : i (1 , 2 , ..., n ) Based on these definitions, we can construct a Graph G . The standard base case that’s followed P i an edge ( b i , d i ) : b i d i . This means that P i was born before P j died. We know that each fact has one of two forms. Based on this we can create two more cases for edges connecting between nodes in G : Edge ( d i , b j ) if P i died before P j ’s birth Edge ( b i , d j ) and Edge ( b i , d j ) if overlapped at-least partially In these cases, there may be certain inconsistencies, which is what we have to identify. The inconsistencies can be listed as: If P i died before P j ’s birth and P j died before P i ’s birth (which is a clear internal flaw) [fig 1.0] If there’s partial overlap (2 nd case the above list), and at the same time P i died before P j ’s birth Or P j died before P i ’s birth. [fig 1.1] and [fig 1.2] If P i dies before P j is born and there’s partial overlap between P i and P j (also the opposite case where P j dies before P i ’s birth For a clear death and birth date P i , we need know which date precedes the next date. However, it’s not clear here because of the cycle between the nodes. This asserts that if there’s a cycle in our Directed Graph then internal inconsistency. If not, then a topological order, which yields the correct birth and death dates timeline To detect whether the Directed Graph is acyclic or not, we can devise a DFS based algorithm. While traversing the graph, if we identify a back-edge an edge from the descendants of node n to the ancestors of node n . The following is a recursive, DFS algorithm where a Global variable, Inconsisent becomes True, if there is a back-edge indeed, and the graph is a cycle. 5
CS 180 HW2 Arjun Raj Loomba 605809574 The function findInconsistency() keeps calling DFS for each node that hasn’t been visited, with a new stack, and then DFS keeps performing this recursion. //Assume that G is a Directed Graph, V is a set of vertices inconsistent = False Def findInconsistency(): visited = [ ’X’ for each node in G] For v in |V|: if visited[v] == ’X’ : //Not visited stack = [] //values will be pushed in for each descendant stack.push(v) visited[v] = ’A’ //mark visited DFS(stack, visited) //analyse all the neighbours of the node //Printing out the results If Not inconsistent: For i in visited: print(visited[i].UID, visited[i].birthOrDeath, visited[i].date) Else: print( "Inconsistent" ) Def DFS(stack, visited): For v in stack.top().neighbours: if visited[v] == ’A’ : inconsistent = True // A back edge exists else : stack.push(v) visited[v] = ’A’ DFS(stack, visited) findInconsistency() The time-complexity will be equivalent to the time-complexity of a DFS algorithm, which is proportional to the number of edges E , which represents the number of facts m . This results in O ( m ). In the worst case, printing out values will result in a time complexity of O ( n ). Therefore, together the time complexity is O ( m + n ) for this DFS algorithm to identify whether a cycle. 6
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
CS 180 HW2 Arjun Raj Loomba 605809574 Problem 4 This problem resembles the extraction of a subsequence from a larger sequence. Based on the conditions given in the problem, we have a sequence S i S : i (0 , 1 , ..., n ) and S j S : j (0 , 1 , ..., m ). This problem can be solved through a simple greedy algorithm, where there are 2 pointers i, j across S , and j across S . i is iterated over in each instance, and j is iterated whenever S j = S i If j reaches the last element of S , then it’s a subsequence, otherwise S isn’t a subsequence. In the following pseudocode, assume that S = S As an edge case, if | S | > | S | then the subsequence can never be true. Therefore, this case can be auto-rejected at the beginning, without needing to run through the entire algorithm until n if (m > n): print( "S_ is not a subsequence!" ) return i = 0, j = 0 while (i < n) and (j < m): if (S[i] == S_[j]): // Check for same val in sequence and subsequence i += 1, j+= 1 // both pointers are iterated else : i+=1 if (j = m): print( "S_ is a subsequence of the form: \n" ) for (k = 0 to m): print(S_[k] + " " ) else : print( "S_ is not a subsequence!" ) return The algorithm runs in linear time O ( n ), since there’s one while loop till a maximum of n The total number of iterations needed to print the complete subsequence at the end is m . Therefore, the Big-O of this algorithm is O ( m + n ) 7

Browse Popular Homework Q&A

Q: 800 9001000 min = Q₁ = Q₂ Q3 (b) Approximately what percenta Approximately% (c) Approximately what…
Q: The incomplete ques
Q: The graph of f(x) is shown. वि | 45 6 7 X fl=o at x = ? DIY'S_
Q: Questions 1. What are two advantages of online ordering in a fast-food restaurant such as Domino's…
Q: A horizontal rifle is fired at a bull's-eye. The muzzle speed of the bullet is 650 m/s. The gun is…
Q: Ginny currently earns a    (a. nominal; b. real)    wage of $12.00 per hour; in other words, the…
Q: Match the graph to the correct exponential functions. Select all that apply. -10 -6 □y=-2(3)^…
Q: A row of long cylindrical power cables that are spaced 2 cm evenly apart is placed in parallel with…
Q: Required information Use the following information for the Quick Studies below. (Algo) Rafner…
Q: QUESTION 1 You and 3 friends go bowling. You bowl for 2.5 hours. You play 3 games and the total cost…
Q: 1.12 Choose the equation below that can be used to determine the value of "P" for a known interest…
Q: The first part of the assignment is to open Excel and in column A starting in row 1 and down to row…
Q: Which type of performance evaluation you thinks works best, a criteria based or a competency based…
Q: Below are listed molecules with different chemical characteristics. Knowing that all molecules will…
Q: An ampule of naphthalene in hexane contains 1.00×10-4 naphthalene. The naphthalene is excited with a…
Q: this was completely wrong, and it took me more than hour
Q: Br H3C CH3 a b с ionization d 1-propanol e Prelab Question #4 Homework Unanswered H H 8+ With…
Q: (a) State the null hypothesis H and the alternate hypothesis H₁. H₁:0 H₁:0 (b) Determine the type of…
Q: O ATOMS, IONS AND MOLECULES Counting protons and electrons in atoms and atomic ions Fill in the…
Q: Evaluate √ √R 6xy5 dydx R where the region is bounded by the curves y = = x² 1 and y = 1-². Sketch…
Q: A travel magazine conducts an annual survey where readers rate their favorite cruise ship. Ships are…
Q: MEAN=50 STANDARD DEVIATION =7 P(3440)= *Round to 4 decimal as needed*