Read the questions carefully. This assignment is on material from weeks 12,13,14.
(a) 8 points Let G be an undirected graph. The graph G may are may not be connected. Graphs that are not connected are made up of several connected components. The graph below consists of three connected components. Graphs that are connected consist of only one connected component.
In class we showed that DFS-visit(u) ran depth-fifirst-search starting from vertex u. Update the pseudocode for DFS(G) to a new algorithm called PrintCC(G) so that it prints out both the number of connected components, and also the vertices in each component. For example, the result of running the algorithm on the above graph should be :
Component 1: A B C D E
Component 2: F G H
Component 3: J
Total number of connected components: 3
(b) 6 points In class we designed an algorithm that determines if an undirected graph G contains a cycle. The solution was based on DFS. Your job is to design an algorithm that performs the same task,but this time using BFS. Call your algorithm Find-Cycle-BFS(G). Your algorithm should return TRUE if the graph contains a cycle, and FALSE otherwise. Ensure that your solution works even if the graph G is not connected. Provide the pseudo-code and justify the runtime of O(V + E).
(c) 5 points Give an example of an input graph that demonstrates that your solution above may fifind difffferent cycles on the same graph, depending on the order of the edges in the implementation.Next, provide an algorithm that prints the length of the shortest cycle in the graph. Provide the pseudo-code and justify the runtime of O(V E + V 2 ).
(d) 8 points Suppose a directed graph G is not strongly connected. However, when we run the algorithm to fifind the strongly connected components, there are exactly k SCC’s. Write a new algorithm that adds at most 2k directed edges to G so that G is strongly connected. Your algorithm should run in time O(V + E). As input, it only receives the graph G (not the SCC’s). Provide the pseudo-code and justify the runtime.
(e) 8 points In practice problem 8 from week 12 we designed an algorithm for fifinding a Topological sort of a DAG. Your job is to update this algorithm so that it prints out all possible topological orderings. You may assume that the input to the problem is a graph G where the vertices have attributes v.indegree. You may also assume that the input graph already has the v.indegree attributes correctly assigned. Call your algorithm PrintSorts(G). The simplest approach is to use a global list variable, L which stores the vertices in the topological sort. Vertices are added and removed to the list at difffferent states of the recursion.
When the list contains all vertices, the list is printed.
(a) 8 points A student in Algorithms class proposed the following method for fifinding the MST of a connected undirected graph G:
Step 1:. Sort the edges of the graph by decreasing order of weight. Store the edges in a list L.
Step 2: For each edge e in L:
If removing edge e does NOT disconnect the graph, then remove it from G.
Otherwise, leave it in G.
Demonstrate the execution of this algorithm on the graph below. Do you get the same MST with Kruskal’s and/or Prim’s?
Next, provide the pseudo-code that implements this algorithm. Note that you cannot simply write “if e disconnects the graph ” in your pseudo-code. You may use algorithms from class, like sorting, searching,etc. Justify that the runtime of your algorithm is O(E2)
(b) 8 points Clustering is the task of grouping a set of points in such a way that points in the same group (or cluster) are similar to each other. There are many difffferent types of cluster analysis, depending on how we defifine the similarity of points. In the example below left, instinctively most of us would identify three clusters. But clusters are not always in “clumps”. In the example below right, we also see three main clusters, although one of them is shaped more like a chain.
Suppose P consists of a set of n points, where each point has an x and a y coordinate in the 2D plane.The point objects are contained in the array p where p[i].x and p[i].y are the coordinates of the ith point.Your job is to create a graph G using the points as vertices, and the distance between points as weighted edges. Assume the vertex objects have attributes v.x and v.y which must be set correctly. Next, you fifind fifind exactly k clusters, where the defifinition of a cluster is as follows: the shortest distance between points in two difffferent clusters is maximized. You must provide the pseudo-code for your algorithm called FindClusters(G, k), which takes as input the graph previously created graph G. The algorithm print out the cluster number of each point. For example, in the case where k = 3 an example output is:
Point: Cluster number:
Justify the runtime of your algorithm as O(n2 log n).
(c) 8 points A city consists of a set of n bus stops. There are two main bus lines in the city: the red bus line and the blue bus line. Buses travel between pairs of bus stops. Two bus stops may be connected by either the red bus, the blue bus, both buses, or neither bus. You would like to travel from bus stop X to bus stop Y. You have enough money for one bus ticket, and the bus ticket includes exactly one transfer between bus lines. That means you can leave X on the blue line, and transfer at some point to the red line until you reach Y , or you could travel the entire route on the same bus line, or you can start on the red bus line and then transfer to blue. Your job is to design an algorithm that determines if there is a way for you to get from X to Y using your one ticket. Justify the runtime of O(n2 ).
(d) 5 points Suppose we alter Dijkstra’s algorithm so that instead of the < sign, we replace it with the > sign. Let G be a directed graph that contains no cycles. Does this new version fifind the longest path from the source vertex to every other vertex in the graph? Justify your answer.
(e) 8 points Suppose G is a graph that models a set of n points in the plane. The vertices are the points, and every pair of points has an edge between them. The weight of the edge corresponds to the distance between the points. One of the points is marked s, which is the source point. We wish to fifind the shortest path from s to all other vertices in the graph.