Question

In: Computer Science

You are given an undirected graph A. The problem P asks, if there exists a cycle...

You are given an undirected graph A. The problem P asks, if there exists a cycle that contains more than half of the nodes in graph A? Is this problem in NP(nondeterministic polynomial time)? How is it related to the Hamiltonian cycle problem?

Construct your example in python.

Input: Graph A as either an edge list or adjacent list.
Output: Yes or No. If the output is yes, display the cycle as a sequence of nodes.

Solutions

Expert Solution

Please give positive ratings for my effort. Thanks.

PROGRAM

from collections import defaultdict 

class Graph: 

        def __init__(self,vert): 
                self.V = vert
                self.graph = defaultdict(list) 

        def addEdge(self,a,b): 
                self.graph[a].append(b) 
                self.graph[b].append(a) 


        def check(self,a,vis,parent): 
                vis[a] = True

                for i in self.graph[a]: 
                        if vis[i]==False : 
                                if(self.check(i,vis,a)): 
                                        return True
                        elif parent!=i: 
                                return True
                
                return False

        def iscycle(self): 
                
                vis =[False]*(self.V) 
                for i in range(self.V): 
                        if vis[i] ==False: 
                                if(self.check(i,vis,-1)) == True: 
                                        return True
                
                return False

test = Graph(5) 
test.addEdge(1, 0) 
test.addEdge(1, 2) 
test.addEdge(2, 0) 
test.addEdge(0, 3) 
test.addEdge(3, 4) 

if test.iscycle(): 
        print ("Given Graph contains cycle")
else : 
        print("Given Graph does not contain cycle ")

Related Solutions

Give an algorithm to detect whether a given undirected graph contains a cycle. If the graph...
Give an algorithm to detect whether a given undirected graph contains a cycle. If the graph contains a cycle, then your algorithm should output one. (It should not output all cycles in the graph, just one of them.) The running time of your algorithm should be O(m + n) for a graph with n nodes and m edges.
Which graph search method will be better for finding a cycle in an undirected graph: a...
Which graph search method will be better for finding a cycle in an undirected graph: a BTS or a DFS? In detail, describe (create) an algorithm for finding a cycle in any undirected graph, explaining why the algorithm assures to find a cycle if there exists one.
Write a program in java that detects if there is a cycle in an undirected graph...
Write a program in java that detects if there is a cycle in an undirected graph using DFS Two lines of input: 1. Two integers V for the number of vertices and E for the number of edges respectively. 2. List of integers that shows how the graph is connected. Ex input: 4 4 01020323 Output: Graph contains cycle Ex input: 5 4 01020314 Output: Graph doesn't contains cycle
Given a directed graph, prove that there exists an Eulerian cycle that is also a hamiltonian...
Given a directed graph, prove that there exists an Eulerian cycle that is also a hamiltonian cycle if and only if the graph is a single cycle.
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.
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...
Draw a graph having the given properties stated below, or explain why no such graph exists:...
Draw a graph having the given properties stated below, or explain why no such graph exists: In each case assume simple graphs (no self loops and no parallel edges) a. Six vertices each with degree 3 b. Five vertices each with degree 3. c. Four vertices each with degree 1. d. Six vertices and four edges. e. Four edges; four vertices having degrees 1, 2, 3, and 4.
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