Skip to content

Commit

Permalink
Merge pull request #1 from darpanjbora/master
Browse files Browse the repository at this point in the history
Sync last forked
  • Loading branch information
naresh1406 authored Oct 27, 2019
2 parents 8add2f8 + e1a2b4b commit 1c452be
Show file tree
Hide file tree
Showing 28 changed files with 2,362 additions and 1 deletion.
235 changes: 235 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"code"
]
},
{
"login": "ADVAITH18",
"name": "ADVAITH U",
"avatar_url": "https://avatars0.githubusercontent.com/u/45172876?v=4",
Expand Down Expand Up @@ -48,6 +49,240 @@
"contributions": [
"code"
]
},
{
"login": "NayakNavin",
"name": "Navin Nayak",
"avatar_url": "https://avatars1.githubusercontent.com/u/37023798?v=4",
"profile": "https://github.com/NayakNavin",
"contributions": [
"code"
]
},
{
"login": "insidetheradar",
"name": "Vivek Pandey",
"avatar_url": "https://avatars2.githubusercontent.com/u/52068103?v=4",
"profile": "http://www.insidetheradar.com",
"contributions": [
"code"
]
},
{
"login": "Naivedh",
"name": "Naivedh",
"avatar_url": "https://avatars0.githubusercontent.com/u/47849344?v=4",
"profile": "https://github.com/Naivedh",
"contributions": [
"code"
]
},
{
"login": "augustojaba",
"name": "Augusto Alves",
"avatar_url": "https://avatars3.githubusercontent.com/u/6476574?v=4",
"profile": "https://github.com/augustojaba",
"contributions": [
"code"
]
},
{
"login": "Sematre",
"name": "Sematre",
"avatar_url": "https://avatars1.githubusercontent.com/u/7118368?v=4",
"profile": "https://sematre.de",
"contributions": [
"code"
]
},
{
"login": "yashasvi-goel",
"name": "Yashasvi Goel",
"avatar_url": "https://avatars0.githubusercontent.com/u/31849052?v=4",
"profile": "http://yashasvigoel.me",
"contributions": [
"code"
]
},
{
"login": "dshah2012",
"name": "Darshan Shah",
"avatar_url": "https://avatars0.githubusercontent.com/u/13819619?v=4",
"profile": "https://github.com/dshah2012",
"contributions": [
"code"
]
},
{
"login": "ltosh9802",
"name": "Toshik Langade",
"avatar_url": "https://avatars1.githubusercontent.com/u/28157232?v=4",
"profile": "https://www.linkedin.com/in/ltosh9802/",
"contributions": [
"code"
]
},
{
"login": "pb-10",
"name": "Pranit Brahmbhatt",
"avatar_url": "https://avatars2.githubusercontent.com/u/43945352?v=4",
"profile": "https://pb-10.github.io/my-portfolio/. ",
"contributions": [
"code"
]
},
{
"login": "vasugoel062",
"name": "vasugoel062",
"avatar_url": "https://avatars0.githubusercontent.com/u/56226687?v=4",
"profile": "https://github.com/vasugoel062",
"contributions": [
"code"
]
},
{
"login": "SMN321",
"name": "simon",
"avatar_url": "https://avatars1.githubusercontent.com/u/33412628?v=4",
"profile": "https://github.com/SMN321",
"contributions": [
"code"
]
},
{
"login": "lunodesouza",
"name": "Luno",
"avatar_url": "https://avatars3.githubusercontent.com/u/3415379?v=4",
"profile": "https://github.com/lunodesouza",
"contributions": [
"code"
]
},
{
"login": "yukta12",
"name": "Peswani Yukta",
"avatar_url": "https://avatars2.githubusercontent.com/u/36343835?v=4",
"profile": "http://www.yuktapeswani.tk/",
"contributions": [
"code"
]
},
{
"login": "sonwanigaurav",
"name": "sonwanigaurav",
"avatar_url": "https://avatars3.githubusercontent.com/u/33836074?v=4",
"profile": "https://github.com/sonwanigaurav",
"contributions": [
"code"
]
},
{
"login": "letscodedev",
"name": "letscodedev",
"avatar_url": "https://avatars0.githubusercontent.com/u/42699930?v=4",
"profile": "https://github.com/letscodedev",
"contributions": [
"code"
]
},
{
"login": "Amasha007",
"name": " Sewmini Amasha",
"avatar_url": "https://avatars3.githubusercontent.com/u/43165391?v=4",
"profile": "https://github.com/Amasha007",
"contributions": [
"code"
]
},
{
"login": "kavindugunathilaka",
"name": "kavindugunathilaka",
"avatar_url": "https://avatars3.githubusercontent.com/u/55224312?v=4",
"profile": "https://github.com/kavindugunathilaka",
"contributions": [
"code"
]
},
{
"login": "Tharakadhanushka",
"name": "Tharakadhanushka",
"avatar_url": "https://avatars3.githubusercontent.com/u/55351120?v=4",
"profile": "https://github.com/Tharakadhanushka",
"contributions": [
"code"
]
},
{
"login": "Mownika25",
"name": "Mownika",
"avatar_url": "https://avatars3.githubusercontent.com/u/41233359?v=4",
"profile": "https://github.com/Mownika25",
"contributions": [
"code"
]
},
{
"login": "AurumnPegasus",
"name": "AurumnPegasus",
"avatar_url": "https://avatars1.githubusercontent.com/u/54315149?v=4",
"profile": "https://github.com/AurumnPegasus",
"contributions": [
"code"
]
},
{
"login": "Suchith-kumar",
"name": "SuchithKumar",
"avatar_url": "https://avatars3.githubusercontent.com/u/13486451?v=4",
"profile": "https://github.com/Suchith-kumar",
"contributions": [
"code"
]
},
{
"login": "PratilipiAich",
"name": "PratilipiAich",
"avatar_url": "https://avatars0.githubusercontent.com/u/47985146?v=4",
"profile": "https://github.com/PratilipiAich",
"contributions": [
"code"
]
},
{
"login": "kushverma86",
"name": "Kush Verma",
"avatar_url": "https://avatars3.githubusercontent.com/u/25582970?v=4",
"profile": "https://github.com/kushverma86",
"contributions": [
"code"
]
},
{
"login": "Pika1998",
"name": "Prafulla Raichurkar",
"avatar_url": "https://avatars3.githubusercontent.com/u/36433104?v=4",
"profile": "https://github.com/Pika1998",
"contributions": [
"code"
]
},
{
"login": "timothycdykes",
"name": "Timothy",
"avatar_url": "https://avatars3.githubusercontent.com/u/22033127?v=4",
"profile": "https://timothycdykes.me/",
"contributions": [
"code"
]
},
{
"login": "tesla2101",
"name": "Saurabh Chandra",
"avatar_url": "https://avatars3.githubusercontent.com/u/22328756?v=4",
"profile": "https://github.com/tesla2101",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7,
Expand Down
115 changes: 115 additions & 0 deletions Backtracking/GraphColoring.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
* Given an undirected graph and an integer M.
* The task is to determine if the graph can be colored
* with at most M colors such that no two adjacent vertices of the graph
* are colored with the same color.
* Here coloring of a graph means assignment of colors to all vertices.
* Print 1 if it is possible to colour vertices and 0 otherwise.
*
*
* Vertex are 1-based (vertext number starts with 1, not 0).
*
* Input:
* The first line of input contains an integer T denoting the number of test cases.
* Then T test cases follow. Each test case consists of four lines.
* The first line of each test case contains an integer N
* denoting the number of vertices.
* The second line of each test case contains an integer M
* denoting the number of colors available.
* The third line of each test case contains an integer E denoting the number of edges
* available. The fourth line of each test case contains E pairs
* of space separated integers denoting the edges between vertices.
*
* Output:
* Print the desired output.
*
* Constraints:
* 1 <= T <= 30
* 1 <= N <= 50
* 1 <= E <= N*(N-1)
* 1 <= M <= N
*
* Example:
* Input :
* 2
* 4
* 3
* 5
* 1 2 2 3 3 4 4 1 1 3
* 3
* 2
* 3
* 1 2 2 3 1 3
*
* Output:
* 1
* 0
*/

class GraphColoring
{

public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
int tc = scan.nextInt();

while(tc-->0){
int V = scan.nextInt();
int C = scan.nextInt();
int E = scan.nextInt();
List<Integer>[] G = new ArrayList[V];
for(int i = 0 ; i < V ; i++){
G[i] = new ArrayList<>();
}
for(int i = 0 ; i < E ; i++){
int u = scan.nextInt() - 1;
int v = scan.nextInt() - 1;
G[u].add(v);
G[v].add(u);
}
int[] color = new int[V];

System.out.println(kColorable(G, color, 0 , C) ? 1 : 0);
}
}
public static boolean kColorable(List<Integer>[] G , int[] color, int i, int C){

if(i == G.length)
return solved(G,color);

for(int c = 1; c <= C; c++){
if(legal(G, color, i, c)){
color[i] = c;
if(kColorable(G, color, i + 1, C))
return true;
color[i] = 0;
}
}

return false;
}

public static boolean solved(List<Integer>[] G , int[] color){
for(int i = 0 ; i < G.length ; i++){
for(int nei : G[i]){
if(color[i] == 0 || color[i] == color[nei])
return false;
}
}
return true;
}

public static boolean legal(List<Integer>[] G , int[] color , int i, int c){
for(int nei : G[i]){
if(color[nei] == c)
return false;
}
return true;
}


}
Loading

0 comments on commit 1c452be

Please sign in to comment.