Skip to content

Commit 2f0a27e

Browse files
Add files via upload
1 parent f8b5cb7 commit 2f0a27e

File tree

5 files changed

+315
-0
lines changed

5 files changed

+315
-0
lines changed

GRAPH 1/CODING NINJAS.txt

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
public class Solution {
2+
3+
int solve(String[] board , int n, int m)
4+
{
5+
// Write your code here.
6+
char [][] graph = new char[n][m];
7+
boolean [][] visited = new boolean[n][m];
8+
9+
10+
for(int i=0;i<n;i++)
11+
{
12+
for(int j=0;j<m;j++)
13+
{
14+
graph[i][j] =board[i].charAt(j);
15+
//System.out.print(graph[i][j]);
16+
}
17+
//System.out.println();
18+
}
19+
20+
for(int i=0;i<n;i++)
21+
{
22+
for(int j=0;j<m;j++)
23+
{
24+
char c = graph[i][j];
25+
26+
if(c=='C')
27+
{
28+
visited[i][j] = true;
29+
boolean ans = findPath(graph,visited,i,j,1);
30+
31+
if(ans == true)
32+
return 1;
33+
34+
}
35+
}
36+
}
37+
return 0;
38+
39+
}
40+
public static boolean findPath(char[][]graph, boolean [][]visited,int row, int col, int start)
41+
{
42+
String str = "CODINGNINJA";
43+
if(start==str.length())
44+
return true;
45+
int n =graph.length;
46+
int m = graph[0].length;
47+
//LinkedList <String> side =new LinkedList <String>();
48+
int [] side = new int[16];
49+
side = getNeighbour(row,col);
50+
51+
for(int i=0;i<16;i=i+2)
52+
{
53+
int cur_row = side[i];
54+
int cur_col = side[i+1];
55+
if(cur_row<0 || cur_col <0 || cur_row>=n || cur_col>=m)
56+
{
57+
continue;
58+
}
59+
else
60+
{
61+
if(visited[cur_row][cur_col]==false && graph[cur_row][cur_col]==str.charAt(start))
62+
{
63+
visited[cur_row][cur_col] =true;
64+
boolean ans = findPath(graph,visited,cur_row,cur_col,start+1);
65+
if(ans == true)
66+
return true;
67+
else
68+
visited[cur_row][cur_col] = false;
69+
70+
}
71+
}
72+
}
73+
return false;
74+
75+
76+
}
77+
public static int [] getNeighbour(int row,int col)
78+
{
79+
int ans [] = new int[16];
80+
//right
81+
ans[0]= row;
82+
ans[1]= col+1;
83+
//down
84+
ans[2]= row+1;
85+
ans[3]= col;
86+
//left
87+
ans[4]= row;
88+
ans[5]= col-1;
89+
//up
90+
ans[6]= row-1;
91+
ans[7]= col;
92+
//diagona;
93+
ans[8]= row+1;
94+
ans[9]= col+1;
95+
//diagonal
96+
ans[10]= row-1;
97+
ans[11]= col-1;
98+
99+
//right diagonal
100+
ans[12]= row-1;
101+
ans[13]= col+1;
102+
103+
//right dia
104+
ans[14]= row+1;
105+
ans[15]= col-1;
106+
107+
return ans;
108+
}
109+
110+
}

GRAPH 1/GET PATH-BFS.txt

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import java.util.Scanner;
2+
import java.util.*;
3+
public class Solution {
4+
5+
public static void main(String[] args) {
6+
Scanner s = new Scanner(System.in);
7+
int V = s.nextInt();
8+
int E = s.nextInt();
9+
int edges[][]=new int[V][V];
10+
for(int i=0;i<E;i++){
11+
int sv=s.nextInt();
12+
int ev=s.nextInt();
13+
edges[sv][ev]=1;
14+
edges[ev][sv]=1;
15+
}
16+
int V1=s.nextInt();
17+
int V2=s.nextInt();
18+
boolean visited[]=new boolean[V];
19+
ArrayList<Integer> ans=getPathBFS(edges,visited,V1,V2);
20+
if(ans!=null){
21+
for(int elem:ans)
22+
{
23+
System.out.print(elem+" ");
24+
}
25+
}
26+
}
27+
public static ArrayList<Integer> getPathBFS(int[][] edges,boolean[] visited,int V1,int V2){
28+
if(V1==V2)
29+
{
30+
ArrayList<Integer> ans=new ArrayList<Integer>();
31+
ans.add(V1);
32+
visited[V1]=true;
33+
return ans;
34+
}
35+
Queue<Integer> q=new LinkedList<Integer>();
36+
HashMap<Integer,Integer> h=new HashMap<>();
37+
ArrayList<Integer> ans=new ArrayList<>();
38+
q.add(V1);
39+
visited[V1]=true;
40+
while(!q.isEmpty() ){
41+
int first=q.remove();
42+
for(int i=0;i<edges.length;i++){
43+
if(edges[first][i]==1 && !visited[i]){
44+
visited[i]=true;
45+
q.add(i);
46+
h.put(i,first);
47+
if(i==V2)
48+
{ ans.add(i);
49+
while(!ans.contains(V1)){
50+
51+
int b=h.get(i);
52+
ans.add(b);
53+
i=b;
54+
}
55+
return ans;
56+
}
57+
}
58+
}
59+
60+
61+
62+
}
63+
64+
65+
return null;
66+
}
67+
68+
69+
}

GRAPH 1/GET PATH-DFS.txt

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.util.Scanner;
2+
import java.util.*;
3+
public class Solution {
4+
public static void main(String[] args) {
5+
Scanner s = new Scanner(System.in);
6+
int V = s.nextInt();
7+
int E = s.nextInt();
8+
int edges[][]=new int[V][V];
9+
for(int i=0;i<E;i++){
10+
int sv=s.nextInt();
11+
int ev=s.nextInt();
12+
edges[sv][ev]=1;
13+
edges[ev][sv]=1;
14+
}
15+
int V1=s.nextInt();
16+
int V2=s.nextInt();
17+
boolean visited[]=new boolean[V];
18+
ArrayList<Integer> ans=getPathDFS(edges,visited,V1,V2);
19+
if(ans!=null){
20+
for(int elem:ans)
21+
{
22+
System.out.print(elem+" ");
23+
}
24+
}
25+
}
26+
public static ArrayList<Integer> getPathDFS(int[][] edges,boolean[] visited,int V1,int V2){
27+
if(V1==V2)
28+
{
29+
ArrayList<Integer> ans=new ArrayList<>();
30+
visited[V1]=true;
31+
ans.add(V1);
32+
return ans;
33+
}
34+
visited[V1]=true;
35+
for(int i=0;i<edges.length;i++)
36+
{
37+
if(edges[V1][i]==1 && !visited[i])
38+
{
39+
ArrayList<Integer> arr=getPathDFS(edges,visited,i,V2);
40+
if(arr!=null)
41+
{
42+
arr.add(V1);
43+
44+
return arr;
45+
}
46+
}
47+
}
48+
return null;
49+
}
50+
}

GRAPH 1/HAS PATH.txt

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.util.Scanner;
2+
import java.util.*;
3+
public class Solution {
4+
5+
public static void main(String[] args) {
6+
Scanner s = new Scanner(System.in);
7+
int V = s.nextInt();
8+
int E = s.nextInt();
9+
int edges[][]=new int[V][V];
10+
for(int i=0;i<E;i++){
11+
int sv=s.nextInt();
12+
int ev=s.nextInt();
13+
edges[sv][ev]=1;
14+
edges[ev][sv]=1;
15+
}
16+
int V1=s.nextInt();
17+
int V2=s.nextInt();
18+
boolean visited[]=new boolean[V];
19+
boolean ans=hasPath(edges,V1,V2,visited);
20+
System.out.println(ans);
21+
}
22+
public static boolean hasPath(int[][] edges,int V1, int V2,boolean visited[]){
23+
if(edges[V1][V2]==1)
24+
return true;
25+
Queue<Integer> q=new LinkedList<>();
26+
q.add(V1);
27+
visited[V1]=true;
28+
while(!q.isEmpty()){
29+
int n=q.remove();
30+
for(int i=0;i<edges.length;i++)
31+
{
32+
if(edges[n][i]==1 && !visited[i])
33+
{
34+
q.add(i);
35+
visited[i]=true;
36+
}
37+
}
38+
}
39+
if(visited[V2]==true)
40+
return true;
41+
else
42+
return false;
43+
}
44+
}

GRAPH 1/IS CONNECTED.txt

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.util.Scanner;
2+
import java.util.*;
3+
public class Solution {
4+
public static void main(String[] args) {
5+
Scanner s = new Scanner(System.in);
6+
int V = s.nextInt();
7+
int E = s.nextInt();
8+
int edges[][]=new int[V][V];
9+
for(int i=0;i<E;i++){
10+
int sv=s.nextInt();
11+
int ev=s.nextInt();
12+
edges[sv][ev]=1;
13+
edges[ev][sv]=1;
14+
}
15+
boolean ans=isConnected(edges);
16+
System.out.println(ans);
17+
}
18+
public static boolean isConnected(int[][] edges){
19+
if(edges.length==0){
20+
return true;
21+
}
22+
boolean[] visited=new boolean[edges.length];
23+
DFS(edges,0,visited);
24+
for(boolean elem:visited)
25+
{
26+
if(elem==false)
27+
return false;
28+
}
29+
return true;
30+
}
31+
public static void DFS(int[][] edges,int startver,boolean[] visited){
32+
visited[startver]=true;
33+
for(int i=0;i<edges.length;i++){
34+
if(edges[startver][i]==1 && !visited[i]){
35+
visited[i]=true;
36+
DFS(edges,i,visited);
37+
38+
}
39+
}
40+
return ;
41+
}
42+
}

0 commit comments

Comments
 (0)