Question

In: Computer Science

Consider a modification to the DFS procedure such that it returns for an undirected graph G,...

Consider a modification to the DFS procedure such that it returns for an undirected graph
G, the connected components of G. To do this, you will modify the DFS procedure to
• maintain a counter i, initially set in 1, that counts how many connected components we found so
far; and
• on each vertex v ∈ G.V maintain an attribute component indicating to which connected component
number v belong to. That is, when DFS terminates, v.component indicates the component number
1, 2, . . . i that v belongs to.
Answer the following questions:
(a) Write the pseudocode for this slightly modified version of the DFS algorithm
(b) Explain why your algorithm is correct and its running time. You do not need to provide a formal
argument for correctness, but be sure to explain in clear terms.

Solutions

Expert Solution

we will have variable count=1 which would count the number of connected components

apart from count we also create an array component[] of size |V|, where component[i] would store the component in which node i lies.

Algorithm:

procedure DFS_modified(G) is

count =1;

component[i]=1 for each i

    let S be a stack and v be a non-discovered vertex
    S.push(v)
    component[v]=count               
    while S is not empty do
        v = S.pop()
        if v is not discovered yet then
            mark v as discovered
            for all each neighbour w of v do
                S.push(w)

                              component[w]=count;             // assign each w the same component number as its parent

        count++;                                                       //increase the component count

The running time is same as regular DFS i.e. O(m+n)

The correctness follows from th efact that child of each node should be in same component. and that is what algorithm does, it asigns the same component number to child as node's


Related Solutions

Consider an unweighted, undirected graph G = <V, E>. The neighbourhood of a node v ∈...
Consider an unweighted, undirected graph G = <V, E>. The neighbourhood of a node v ∈ V in the graph is the set of all nodes that are adjacent (or directly connected) to v. Subsequently, we can define the neighbourhood degree of the node v as the sum of the degrees of all its neighbours (those nodes that are directly connects to v). (a) Design an algorithm that returns a list containing the neighbourhood degree for each node v ∈...
Consider the Minimum Spanning Tree Problem on an undirected graph G=(V,E), with a cost ce ≥0...
Consider the Minimum Spanning Tree Problem on an undirected graph G=(V,E), with a cost ce ≥0 on each edge, where the costs may not all be different. If the costs are not all distinct, there can in general be many distinct minimum-cost solutions. Suppose we are given a spanning tree T ⊆ E with the guarantee that for every e ∈ T, e belongs to some minimum-cost spanning tree in G. Can we conclude that T itself must be a...
You are given an undirected graph G = ( V, E ) in which the edge...
You are given an undirected graph G = ( V, E ) in which the edge weights are highly restricted. In particular, each edge has a positive integer weight from 1 to W, where W is a constant (independent of the number of edges or vertices). Show that it is possible to compute the single-source shortest paths in such a graph in O(E+V) time.
Given an undirected graph G=(V, E) with weights and a vertex , we ask for a...
Given an undirected graph G=(V, E) with weights and a vertex , we ask for a minimum weight spanning tree in G where is not a leaf (a leaf node has degree one). Can you solve this problem in polynomial time? Please write the proof.
Consider an undirected graph G that has n distinct vertices. Assume n≥3. How many distinct edges...
Consider an undirected graph G that has n distinct vertices. Assume n≥3. How many distinct edges will there be in any circuit for G that contains all the vertices in G? What is the maximum degree that any vertex in G can have? What is the maximum number of distinct edges G can have? What is the maximum number of distinct edges that G can have if G is disconnected?
Let G be a simple undirected graph with n vertices where n is an even number....
Let G be a simple undirected graph with n vertices where n is an even number. Prove that G contains a triangle if it has at least (n^2 / 4) + 1 edges using mathematical induction.
Prove or disprove: If G = (V; E) is an undirected graph where every vertex has...
Prove or disprove: If G = (V; E) is an undirected graph where every vertex has degree at least 4 and u is in V , then there are at least 64 distinct paths in G that start at u.
5. Suppose we are given both an undirected graph G with weighted edges and a minimum...
5. Suppose we are given both an undirected graph G with weighted edges and a minimum spanning tree T of G . (a) Describe an algorithm to update the minimum spanning tree when the weight of a single edge e is decreased. (b) Describe an algorithm to update the minimum spanning tree when the weight of a single edge e is increased. In both cases, the input to your algorithm is the edge e and its new weight; your algorithms...
Design a linear-time algorithm which, given an undirected graph G and a particular edge e in...
Design a linear-time algorithm which, given an undirected graph G and a particular edge e in it, determines whether G has a cycle containing e. Your algorithm should also return the length (number of edges) of the shortest cycle containing e, if one exists. Just give the algorithm, no proofs are necessary. Hint: you can use BFS to solve this.
Given an undirected graph G = (V,E), consisting of n vertices and m edges, with each...
Given an undirected graph G = (V,E), consisting of n vertices and m edges, with each edge labeled from the set {0,1}. Describe and analyze the worst-case time complexity of an efficient algorithm to find any cycle consisting of edges whose labels alternate 0,1.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT