a)
b)
We change weight of every edge of given graph to its negation and initialize distances to all vertices as infinite and distance to source as 0, then we find a topological sorting of the graph which represents a linear ordering of the graph. When we consider a vertex u in topological order, it is guaranteed that we have considered every incoming edge to it. i.e. We have already found shortest path to that vertex and we can use that info to update shorter path of all its adjacent vertices. Once we have topological order, we one by one process all vertices in topological order. For every vertex being processed, we update distances of its adjacent vertex using shortest distance of current vertex from source vertex and its edge weight. i.e.
for every adjacent vertex v of every vertex u in topological order if (dist[v] > dist[u] + weight(u, v)) dist[v] = dist[u] + weight(u, v)
Once we have found all shortest paths from the source vertex, longest paths will be just negation of shortest paths.
Time Complexity: Time complexity of topological sorting is O(V + E). After finding topological order, the algorithm process all vertices and for every vertex, it runs a loop for all adjacent vertices. As total adjacent vertices in a graph is O(E), the inner loop runs O(V + E) times. Therefore, overall time complexity of this algorithm is O(V + E).
(Q4 - 30 pts: 15, 15) a) Give an O (n) time algorithm for finding the...
True or false for each, and explain why (4 pts) The height of a binary tree is bounded by O(n2), where n is the size of the C. tree. d. (4 pts) dynamic array and O(1) time if L is a linked list. Given a list L of n > 2 elements, the following code takes O(n) time if L is a iterator i = L. iterator () i.next); i.next); i.remove ); binary tree T that has size n and...
Assume that N=NP . Give a polynomial-time algorithm for finding a satisfying assignment for a boolean formula φ, if one exists.
Please show your work 3. Give an efficient algorithm that takes as input a directed graph G-(V,E) with edges labeled with either 0 or 1, and vertices s and t that ouputs TRUE if and only if there is a path (not necessarily simple) that goes from s to t such that the binary sequence of edges in the path avoids the substring "11" and outputs FALSE otherwise. (For example, the string 10100010 avoids 11 but the string 00101101110 does...
Hi, I'm confused about how to do this, any help is appreciated thanks! Give the specifications of a pseudo-polynomial time algorithm for Galactic Shortest Path. Hint: Given an instance of GSP such that G has n vertices and m edges, you may want to use a 5 dimensional m by n by n by L by R array. Give the specifications of a pseudo-polynomial time algorithm for Galactic Shortest Path. Hint: Given an instance of GSP such that G has...
Q5 Match the following operations to their corresponding worst case time complexities Operations Finding the nert larger item in a Hash Table Time Complexities од) O (log n) O(n) O(n log n) O(n2) o(n3) O(n + m) O(m logn) O((n +m) log n) O(n2+nm) Trying to remove a non-eristing item from a Hash Table 2 3Finding the previous smaller item in a possibly unbalanced BST Updating a previous value into a new value in an AVL Tree Sorting m edges...
Give a dynamic programming algorithm that runs within the time complexity. Also give the space complexity of the algorithm. Please Given a directed graph with non-negative integer edge weights, a pair of vertices s and t, and integers K and W, describe a dynamic-programming algorithm for deciding whether there exists a path from s to t that has total weight W and uses exactly K edges. Your algorithm should run in time O(nm)WK). Analyze the time- and space-complexity of your...
Packets in Ethermet LANs are routed according to the uni que path in a tree whose vertices correspond to clients and edges correspond to physical connections between the clients. In this problem, we want to design an algorithm for finding the "worst-case route, i.e., the two clients that are furthest apart. Let Tbe a tree, where each edge is labeled with distance 1 Figure (2a) Figure (2b) Define the diam eter of Tto be the length of a longest path...
Computer Algorithm question 8) Give an algorithm for building a heap in O(n) 9) Prove the algorithm given in 8) runs in O(n) time. 10) What is the asymptotic runtime of an algorithm represented by the following recurrence equation? 11) Suppose you have the following priority queue implemented as a (max) heap. What will the heap look like when the max node is removed and the heap is readjusted? Assume on each heapify operation the largest child node is selected...
For Dijkstra’s shortest path algorithm: a. Give the Big-O time for Dijkstra’s shortest path algorithm and explain your answer. b. Does the answer to (a) depend on whether we use an adjacency matrix or list? Explain your answer.
Here is a recursive algorithm that answers the same question as posed on Group HW3, finding the number of people who are taller than everyone before them in line. NumCanSeeRec(a1,... , an : list of n 2 1 distinct heights) (a) ifn -1 then (b return 1 (c) c= ŅumCanSeeRee(a1, , an-1) d) for i:- 1 ton- 1 (e) if a, an then return c (g) return c+1 Answer the following questions about this algorithm. Please show your work. (a)...