Question

In: Computer Science

You are given a directed graph G(V,E) with n vertices and m edges. Let S be...

You are given a directed graph G(V,E) with n vertices and m edges. Let S be the subset of vertices in G that are able to reach some cycle in G. Design an O(n + m) time algorithm to compute the set S. You can assume that G is given to you in the adjacency-list representation.

Solutions

Expert Solution

import java.util.*;

class DirectedGraph { // A Java Program to detect cycle in a graph

    private final int V;

    private final List<List<Integer>> adj;

    public Graph(int V)

    {

        this.V = V;

        adj = new ArrayList<>(V);

        for (int i = 0; i < V; i++)

            adj.add(new LinkedList<>());

    }

    // This function is a variation of DFSUtil()

    private boolean isCyclicUtil(int i, boolean[] visited,boolean[] recStack)

    {

        // Mark the current node as visited and

        // part of recursion stack

        if (recStack[i])

            return true;

        if (visited[i])

            return false;

        visited[i] = true;

        recStack[i] = true;

        List<Integer> children = adj.get(i);

        for (Integer c: children)

            if (isCyclicUtil(c, visited, recStack))

                return true;

        recStack[i] = false;

        return false;

    }

    private void addEdge(int source, int dest) {

        adj.get(source).add(dest);

}

    // Returns true if the graph contains a

    // cycle, else false.

    // This function is a variation of DFS()

    private boolean isCyclic() //Time Complexity of this method is same as time complexity of DFS traversal which is O(V+E).

    {

        // Mark all the vertices as not visited and

        // not part of recursion stack

        boolean[] visited = new boolean[V];

        boolean[] recStack = new boolean[V];

        // Call the recursive helper function to

        // detect cycle in different DFS trees

        for (int i = 0; i < V; i++)

            if (isCyclicUtil(i, visited, recStack))

                return true;

        return false;

    }

    // Driver code

    public static void main(String[] args)

    {

        Graph graph = new Graph(4);

        graph.addEdge(0, 1);

        graph.addEdge(0, 2);

        graph.addEdge(1, 2);

        graph.addEdge(2, 0);

        graph.addEdge(2, 3);

        graph.addEdge(3, 3);

        if(graph.isCyclic())

            System.out.println("Graph contains cycle");

        else

            System.out.println("Graph doesn't "+ "contain cycle");

}

}

}


Related Solutions

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.
Let G = (V, E) be a directed graph, with source s ∈ V, sink t...
Let G = (V, E) be a directed graph, with source s ∈ V, sink t ∈ V, and nonnegative edge capacities {ce}. Give a polynomial-time algorithm to decide whether G has a unique minimum s-t cut (i.e., an s-t of capacity strictly less than that of all other s-t cuts).
. Provide a weighted directed graph G = (V, E, c) that includes three vertices a,...
. Provide a weighted directed graph G = (V, E, c) that includes three vertices a, b, and c, and for which the maximum-cost simple path P from a to b includes vertex c, but the subpath from a to c is not the maximum-cost path from a to c
If graph G has n edges and k component and m vertices, so m ≥ n-k....
If graph G has n edges and k component and m vertices, so m ≥ n-k. Prove it!
If graph g has n vertices and k component and m edges, so m ≥ n-k....
If graph g has n vertices and k component and m edges, so m ≥ n-k. Prove it ! Thank you...
Given a graph G = (V,E), the source-sink pair (s,t) and capacity of edges {C_e ≥...
Given a graph G = (V,E), the source-sink pair (s,t) and capacity of edges {C_e ≥ 0 | e ∈ E}, design a polynomial-time algorithm to find a set of edges S, such that for every edge e ∈ S, increasing C_e will lead to an increase of max-flow value between s and t. Show the correctness of your algorithm.
If G = (V, E) is a graph and x ∈ V , let G \...
If G = (V, E) is a graph and x ∈ V , let G \ x be the graph whose vertex set is V \ {x} and whose edges are those edges of G that don’t contain x. Show that every connected finite graph G = (V, E) with at least two vertices has at least two vertices x1, x2 ∈ V such that G \ xi is connected.
A DAG is a directed graph that contains no directed cycles. Define G = (V, E)...
A DAG is a directed graph that contains no directed cycles. Define G = (V, E) in which V is the set of all nodes as {v1, v2, ..., vi , ...vn} and E is the set of edges E = {ei,j = (vi , vj ) | vi , vj ∈ V} . A topological order of a directed graph G = (V, E) is an ordering of its nodes as {v1, v2, ..., vi , ...vn} so that...
Let G = (V, E) be a directed acyclic graph modeling a communication network. Each link...
Let G = (V, E) be a directed acyclic graph modeling a communication network. Each link e in E is associated with two parameters, w(e) and d(e), where w(e) is a non-negative number representing the cost of sending a unit-sized packet through e, and d(e) is an integer between 1 and D representing the time (or delay) needed for transmitting a packet through e. Design an algorithm to find a route for sending a packet between a given pair of...
Determine, for a given graph G =V,E and a positive integer m ≤ |V |, whether...
Determine, for a given graph G =V,E and a positive integer m ≤ |V |, whether G contains a clique of size m or more. (A clique of size k in a graph is its complete subgraph of k vertices.) Determine, for a given graph G = V,E and a positive integer m ≤ |V |, whether there is a vertex cover of size m or less for G. (A vertex cover of size k for a graph G =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT