`{"id":"02a9370d-d3c8-4f80-8bb2-c70ff9a576ba","name":"Is Graph Cyclic","description":"1. You are given a graph.\r\n2. You are required to find and print if the graph is cyclic.","inputFormat":"Input has been managed for you","outputFormat":"true if the graph is cyclic, false otherwise","constraints":"None","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\n#include <queue>\n#include<string>\nusing namespace std;\n\nclass Edge\n{\npublic:\n int src = 0;\n int nbr = 0;\n\n Edge(int src, int nbr)\n {\n this->src = src; \n this->nbr = nbr;\n }\n};\n \n \nint main() { \n int vtces;\n cin >> vtces;\n vector<vector<Edge>> graph(vtces, vector<Edge>());\n \n\n int edges;\n cin >> edges;\n\n for (int i = 0; i < edges; i++ ) {\n int u, v, w; \n cin >> u >> v >> w;\n \n graph[u].push_back(Edge(u, v));\n graph[v].push_back(Edge(v, u));\n\n } \n \n // write your code here \n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n static class Edge {\r\n int src;\r\n int nbr;\r\n int wt;\r\n\r\n Edge(int src, int nbr, int wt) {\r\n this.src = src;\r\n this.nbr = nbr;\r\n this.wt = wt;\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int vtces = Integer.parseInt(br.readLine());\r\n ArrayList<Edge>[] graph = new ArrayList[vtces];\r\n for (int i = 0; i < vtces; i++) {\r\n graph[i] = new ArrayList<>();\r\n }\r\n\r\n int edges = Integer.parseInt(br.readLine());\r\n for (int i = 0; i < edges; i++) {\r\n String[] parts = br.readLine().split(\" \");\r\n int v1 = Integer.parseInt(parts);\r\n int v2 = Integer.parseInt(parts);\r\n int wt = Integer.parseInt(parts);\r\n graph[v1].add(new Edge(v1, v2, wt));\r\n graph[v2].add(new Edge(v2, v1, wt));\r\n }\r\n\r\n // write your code here\r\n }\r\n}"},"python":{"code":"class Edge:\n def __init__(self,src,nbr):\n self.src = src\n self.nbr = nbr\n\ndef main():\n vtces = int(input())\n edges = int(input())\n graph = {}\n for i in range(vtces):\n graph[i] = []\n \n for i in range(edges):\n lines = input().split(\" \")\n v1=int(lines)\n v2=int(lines)\n e1 = Edge(v1 ,v2 )\n e2 = Edge(v2 ,v1 )\n graph[e1.src].append(e1)\n graph[e2.src].append(e2)\n \n #write your code here\n \nif __name__ == \"__main__\":\n main()"}},"points":10,"difficulty":"easy","sampleInput":"7\r\n6\r\n0 1 10\r\n1 2 10\r\n2 3 10\r\n3 4 10\r\n4 5 10\r\n5 6 10","sampleOutput":"false","questionVideo":"https://www.youtube.com/embed/qbQq-k75bcY?end=150","hints":[],"associated":[{"id":"2f37c1f7-615e-43ec-b80c-0f502b1724ad","name":"Which type of array we are using for checking visits? (Is Graph Cyclic)","slug":"which-type-of-array-we-are-using-for-checking-visits-is-graph-cyclic","type":4},{"id":"b876e75e-e394-4c7f-8843-8b94f7e89b1e","name":"Type of Isocyclic function is? (Is Graph Cyclic)","slug":"type-of-isocyclic-function-is-is-graph-cyclic","type":4},{"id":"ce36b2c1-099d-454e-b1c4-c87d63fb0fa6","name":"Which approach helps you to find out Graph is cyclic?","slug":"which-approach-helps-you-to-find-out-graph-is-cyclic","type":4},{"id":"ce87b4d8-211c-48ee-b536-a546153d58ce","name":"How do we report graph is cyclic using BFS?","slug":"how-do-we-report-graph-is-cyclic-using-bfs","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"02ab8fe3-cddd-43ad-b241-b8b1a2fcd52e","name":"Graphs For Beginners","slug":"graphs-for-beginners","type":0},{"id":"3e730334-01e0-4891-84e3-c13f7007fc9c","name":"Is Graph Cyclic","slug":"is-graph-cyclic","type":1}],"next":{"id":"e0a724ab-189e-41ba-9c66-c743f0f3ade5","name":"Is Graph Cyclic\"","type":3,"slug":"is-graph-cyclic"},"prev":{"id":"d503ed5e-2def-4f47-91b3-fdd8774dfc09","name":"BREADTH FIRST TRAVERSAL","type":3,"slug":"breadth-first-traversal"}}}`

# Is Graph Cyclic

1. You are given a graph. 2. You are required to find and print if the graph is cyclic.

`{"id":"02a9370d-d3c8-4f80-8bb2-c70ff9a576ba","name":"Is Graph Cyclic","description":"1. You are given a graph.\r\n2. You are required to find and print if the graph is cyclic.","inputFormat":"Input has been managed for you","outputFormat":"true if the graph is cyclic, false otherwise","constraints":"None","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\n#include <queue>\n#include<string>\nusing namespace std;\n\nclass Edge\n{\npublic:\n int src = 0;\n int nbr = 0;\n\n Edge(int src, int nbr)\n {\n this->src = src; \n this->nbr = nbr;\n }\n};\n \n \nint main() { \n int vtces;\n cin >> vtces;\n vector<vector<Edge>> graph(vtces, vector<Edge>());\n \n\n int edges;\n cin >> edges;\n\n for (int i = 0; i < edges; i++ ) {\n int u, v, w; \n cin >> u >> v >> w;\n \n graph[u].push_back(Edge(u, v));\n graph[v].push_back(Edge(v, u));\n\n } \n \n // write your code here \n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n static class Edge {\r\n int src;\r\n int nbr;\r\n int wt;\r\n\r\n Edge(int src, int nbr, int wt) {\r\n this.src = src;\r\n this.nbr = nbr;\r\n this.wt = wt;\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int vtces = Integer.parseInt(br.readLine());\r\n ArrayList<Edge>[] graph = new ArrayList[vtces];\r\n for (int i = 0; i < vtces; i++) {\r\n graph[i] = new ArrayList<>();\r\n }\r\n\r\n int edges = Integer.parseInt(br.readLine());\r\n for (int i = 0; i < edges; i++) {\r\n String[] parts = br.readLine().split(\" \");\r\n int v1 = Integer.parseInt(parts);\r\n int v2 = Integer.parseInt(parts);\r\n int wt = Integer.parseInt(parts);\r\n graph[v1].add(new Edge(v1, v2, wt));\r\n graph[v2].add(new Edge(v2, v1, wt));\r\n }\r\n\r\n // write your code here\r\n }\r\n}"},"python":{"code":"class Edge:\n def __init__(self,src,nbr):\n self.src = src\n self.nbr = nbr\n\ndef main():\n vtces = int(input())\n edges = int(input())\n graph = {}\n for i in range(vtces):\n graph[i] = []\n \n for i in range(edges):\n lines = input().split(\" \")\n v1=int(lines)\n v2=int(lines)\n e1 = Edge(v1 ,v2 )\n e2 = Edge(v2 ,v1 )\n graph[e1.src].append(e1)\n graph[e2.src].append(e2)\n \n #write your code here\n \nif __name__ == \"__main__\":\n main()"}},"points":10,"difficulty":"easy","sampleInput":"7\r\n6\r\n0 1 10\r\n1 2 10\r\n2 3 10\r\n3 4 10\r\n4 5 10\r\n5 6 10","sampleOutput":"false","questionVideo":"https://www.youtube.com/embed/qbQq-k75bcY?end=150","hints":[],"associated":[{"id":"2f37c1f7-615e-43ec-b80c-0f502b1724ad","name":"Which type of array we are using for checking visits? (Is Graph Cyclic)","slug":"which-type-of-array-we-are-using-for-checking-visits-is-graph-cyclic","type":4},{"id":"b876e75e-e394-4c7f-8843-8b94f7e89b1e","name":"Type of Isocyclic function is? (Is Graph Cyclic)","slug":"type-of-isocyclic-function-is-is-graph-cyclic","type":4},{"id":"ce36b2c1-099d-454e-b1c4-c87d63fb0fa6","name":"Which approach helps you to find out Graph is cyclic?","slug":"which-approach-helps-you-to-find-out-graph-is-cyclic","type":4},{"id":"ce87b4d8-211c-48ee-b536-a546153d58ce","name":"How do we report graph is cyclic using BFS?","slug":"how-do-we-report-graph-is-cyclic-using-bfs","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"02ab8fe3-cddd-43ad-b241-b8b1a2fcd52e","name":"Graphs For Beginners","slug":"graphs-for-beginners","type":0},{"id":"3e730334-01e0-4891-84e3-c13f7007fc9c","name":"Is Graph Cyclic","slug":"is-graph-cyclic","type":1}],"next":{"id":"e0a724ab-189e-41ba-9c66-c743f0f3ade5","name":"Is Graph Cyclic\"","type":3,"slug":"is-graph-cyclic"},"prev":{"id":"d503ed5e-2def-4f47-91b3-fdd8774dfc09","name":"BREADTH FIRST TRAVERSAL","type":3,"slug":"breadth-first-traversal"}}}` Editor

# Is Graph Cyclic

easy

1. You are given a graph. 2. You are required to find and print if the graph is cyclic.

None

## Format

### Input

Input has been managed for you

### Output

true if the graph is cyclic, false otherwise

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}7 6 0 1 10 1 2 10 2 3 10 3 4 10 4 5 10 5 6 10```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}false`

Question Video

Discussions

Show Discussion

Related Resources 