Skip to content

Added Prims Algorithm #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 211 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
211 commits
Select commit Hold shift + click to select a range
e0da40d
Kruskal's algorithm added
Dips2705 Dec 1, 2019
c5772cc
Tree Sort, Infix Converter
adiXcodr Dec 2, 2019
5b4af29
Merge pull request #13 from abhishekchopra13/master
chanmol1999 Dec 2, 2019
46fb3ce
Update README.md
chanmol1999 Dec 2, 2019
09cf0df
some Sorting and searching algorithms added
Dips2705 Dec 2, 2019
3d8949a
Folder structure created
Dips2705 Dec 2, 2019
25a04cf
Removed instructions for implementation in other languages
Lord-of-Codes Dec 2, 2019
cce76c5
added merge_sort algorithm under sorting algorithms
mohitmo Dec 3, 2019
f058860
added merge_sort algorithm under sorting algorithms
mohitmo Dec 3, 2019
c6d82a5
added merge_sort algorithm under sorting algorithms
mohitmo Dec 3, 2019
9e6a07e
Added Graphs algorithms in new graph folder inside Algorithms
namanvats Dec 3, 2019
493d36d
Strassen’s Matrix Multiplication Algorithm
sgpritam Dec 3, 2019
8cf8e5e
Merge pull request #27 from namanvats/master
abhishekchopra13 Dec 3, 2019
b7f3c34
balance paranthesis optimized
sgpritam Dec 4, 2019
764ad8d
Create large_factorial.c
ghostmk Dec 5, 2019
e6fefbb
Added Dynamic Programming algorithms in new Dynamic Programming folde…
vishal-bhola Dec 5, 2019
e513c62
added binary search implementation
promaroy Dec 5, 2019
6b0d5c0
Create Interpolation-Search.cpp
Dec 6, 2019
b4638aa
Merge pull request #1 from walter-ind21/patch-1
Dec 6, 2019
aeffab6
Added cycle detection algorithm
janish1999 Dec 7, 2019
b29501e
Fixed Typo
janish1999 Dec 7, 2019
f2f2804
Added BFS & DFS
akashksinghal Dec 8, 2019
a4b7ce4
Added Circular Queue & Stack in Arrays
akashksinghal Dec 8, 2019
3e89b02
Added DFS in nwoc_algorithms
namanvats Dec 9, 2019
5dc0502
Merge pull request #5 from Dips2705/master
abhishekchopra13 Dec 9, 2019
c76ad5b
Create Substitution_Cipher.cpp
nikitashaw13 Dec 10, 2019
cf97a48
created readme file
nikitashaw13 Dec 10, 2019
8edc61d
Update Substitution_Cipher.cpp
nikitashaw13 Dec 10, 2019
72d9d28
Update README.md
nikitashaw13 Dec 10, 2019
7f1a6b4
Update README.md
nikitashaw13 Dec 10, 2019
a44a9b6
Create last_men.cpp
nikitashaw13 Dec 11, 2019
6147a57
created readme.md for last men standing program
nikitashaw13 Dec 11, 2019
4cdcede
Update README.md
nikitashaw13 Dec 11, 2019
2f95476
Update README.md
nikitashaw13 Dec 11, 2019
97f6837
Update README.md
nikitashaw13 Dec 11, 2019
c47f45f
Added Bellman Ford algorithm by Tanmay
Tanmay211 Dec 11, 2019
183761d
Update README.md
Tanmay211 Dec 11, 2019
ac80d9c
Create implement_z-algorithm
Jainajagad Dec 11, 2019
221e087
Merge pull request #1 from Jainajagad/implement_z-algorithm
Jainajagad Dec 11, 2019
741e33e
Merge pull request #15 from Dips2705/master
chanmol1999 Dec 12, 2019
a3f67c2
Merge pull request #26 from mohitmo/master
chanmol1999 Dec 12, 2019
b24a51c
Added Mathematical Algorithms
yashagarwal1999 Dec 12, 2019
151850d
Added depth first search algorithm
Dec 12, 2019
2e9242b
z-algorithm
Jainajagad Dec 12, 2019
5178975
Update README.md
Jainajagad Dec 12, 2019
6bfec63
Delete implement_z-algorithm
Jainajagad Dec 12, 2019
d8a96ec
Merge branch 'master' into Tanmay
Tanmay211 Dec 13, 2019
aaae19b
Update README.md
sarthakeddy Dec 13, 2019
83c7029
Added Rabin Karp Algorithm in Search folder
sarthakeddy Dec 13, 2019
33cae9d
Added Heap Sort Algorithm in Sorting Algorithms folder
sarthakeddy Dec 13, 2019
753bcf6
Update README.md
sarthakeddy Dec 13, 2019
b84cb5c
Update README.md
sarthakeddy Dec 13, 2019
19c8e29
Added Rabin Karp and Count of substring
sarthakeddy Dec 13, 2019
631a82a
Merge pull request #59 from Jainajagad/z-algorithm
chanmol1999 Dec 13, 2019
8603d0e
Merge pull request #49 from Tanmay211/Tanmay
chanmol1999 Dec 13, 2019
142b1f5
Merge branch 'master' into floyd_cycle_detection
chanmol1999 Dec 13, 2019
2328243
Merge pull request #41 from janish1999/floyd_cycle_detection
chanmol1999 Dec 13, 2019
1e26ff9
Resolved merge conflict and modified folders
sarthakeddy Dec 14, 2019
f13c7a9
Resolved merge conflict and modified folders
sarthakeddy Dec 14, 2019
d585ca1
Added Manacher Algorithm
sarthakeddy Dec 14, 2019
b2c64dd
Added Manacher Algorithm
sarthakeddy Dec 14, 2019
257eddb
Merge pull request #60 from sarthakeddy/master
Bhavnaharitsa Dec 14, 2019
63097f5
Update Kadane's Algorithm.cpp
vishal-bhola Dec 14, 2019
0ddc3b1
Merge pull request #40 from VersatileVishal/master
Bhavnaharitsa Dec 14, 2019
3cece1c
InfixToPostfix Conversion Algorithm is added in Stack folder
vishal-bhola Dec 14, 2019
cbed802
Update InfixToPostfix.java
vishal-bhola Dec 14, 2019
58b2ba2
done
snehalkhandve Dec 14, 2019
65beab9
Merge pull request #62 from snehalkhandve/master
Bhavnaharitsa Dec 14, 2019
5964bf7
Update InfixToPostfix.java
vishal-bhola Dec 14, 2019
0ba57b4
first
adi1307 Dec 14, 2019
5d0cc4e
Update InfixToPostfix.java
vishal-bhola Dec 14, 2019
0cbde41
Merge pull request #61 from VersatileVishal/master
Bhavnaharitsa Dec 14, 2019
d462e35
Merge branch 'master' into master
susanka068 Dec 14, 2019
f8cbb79
Merge pull request #18 from Lord-of-Codes/master
Bhavnaharitsa Dec 15, 2019
e203fa7
Add files via upload
vanshita25 Dec 15, 2019
7a4323f
Merge pull request #1 from vanshita25/vanshita25-patch-1
vanshita25 Dec 15, 2019
054e221
Update CircularQueue.cpp
akashksinghal Dec 15, 2019
5581855
Update Stack in Array.cpp
akashksinghal Dec 15, 2019
0d064a6
Create Backtracking Algorithms
SubhradeepSS Dec 15, 2019
c4fc3ae
Updated README.md with Backtracking Algorithms
SubhradeepSS Dec 15, 2019
f13e7fa
Merge pull request #42 from akashksinghal/master
Bhavnaharitsa Dec 16, 2019
6e138a9
Merge branch 'master' of https://github.com/NJACKWinterOfCode/nwoc_al…
SubhradeepSS Dec 17, 2019
d554d49
Broken links fixed on README.md
Rodriq Dec 18, 2019
4ffde3f
Merge pull request #70 from Rodriq/fix-broken-links-on-README
Bhavnaharitsa Dec 18, 2019
9e5dadf
Merge branch 'master' of https://github.com/NJACKWinterOfCode/nwoc_al…
SubhradeepSS Dec 18, 2019
1d8f614
Add files via upload
vanshita25 Dec 19, 2019
13dfc6c
Delete towerofhanoi.c
vanshita25 Dec 19, 2019
4278dcc
Add files via upload
vanshita25 Dec 19, 2019
db80c73
Merge pull request #64 from vanshita25/master
Bhavnaharitsa Dec 19, 2019
cd3c542
Removed redundant content README.md
SubhradeepSS Dec 19, 2019
c3d17e3
EvalutePostixExpression added in Stack
vishal-bhola Dec 20, 2019
80eef6b
Update EvaluatePostfixExpression.txt
vishal-bhola Dec 20, 2019
70ea400
Create last_men.cpp
nikitashaw13 Dec 11, 2019
44c5f52
Merge branch 'master' of https://github.com/nikitashaw13/nwoc_algorithms
nikitashaw13 Dec 20, 2019
ecba70d
Added sample Input/Output with the algorithm Name.
yashagarwal1999 Dec 21, 2019
0505db2
Added Sample Input/Output with the algorithm Name.
yashagarwal1999 Dec 21, 2019
c457f40
Added Sample Input/Output with Algorithm Name
yashagarwal1999 Dec 21, 2019
0732775
Explanation Shifted To Top
yashagarwal1999 Dec 21, 2019
e567c25
Explanation shifted to top
yashagarwal1999 Dec 21, 2019
fa95087
updated readme.md
ashiscs Dec 21, 2019
169699e
Added Prims Algorithm
Subhojit-Kumar-Saha Dec 21, 2019
b10f8b9
Merge pull request #78 from ashiscs/patch-1
Bhavnaharitsa Dec 21, 2019
1970bb4
Merge branch 'master' into master
SubhradeepSS Dec 21, 2019
e1fd373
Merge pull request #67 from SubhradeepSS/master
Bhavnaharitsa Dec 21, 2019
8631818
Changes according to question requirement is done.
vishal-bhola Dec 21, 2019
88c5db7
Merge pull request #77 from VersatileVishal/master
Bhavnaharitsa Dec 21, 2019
0424a40
Added Kruskal's Algorithm and Dijkstra's Algorithm in Graphs folder
SubhradeepSS Dec 21, 2019
b7b810b
Removed single variables.
yashagarwal1999 Dec 21, 2019
b0bedee
Removed Single Variables
yashagarwal1999 Dec 21, 2019
18797eb
Updated Prims Algorithm
Subhojit-Kumar-Saha Dec 22, 2019
c4d9567
Merge pull request #81 from SubhradeepSS/master
Bhavnaharitsa Dec 22, 2019
c1e9282
Update prims_algorithm.cpp
Subhojit-Kumar-Saha Dec 22, 2019
d7a1b19
Prims Algorithm
Subhojit-Kumar-Saha Dec 22, 2019
72f5b33
Merge branch 'master' of https://github.com/Subhojit-Kumar-Saha/nwoc_…
Subhojit-Kumar-Saha Dec 22, 2019
90683c1
Merge branch 'master' into master
nikitashaw13 Dec 22, 2019
7f811aa
Shell Sort Algorithm in C
Abhishek-kumar09 Dec 23, 2019
8a7c783
a
Abhishek-kumar09 Dec 23, 2019
a983f6d
Shell Sort in C
Abhishek-kumar09 Dec 23, 2019
fac6a97
Update readme
Abhishek-kumar09 Dec 23, 2019
c83574f
a
Abhishek-kumar09 Dec 23, 2019
3a0d7bb
Finding NextLarger is added in Stack
vishal-bhola Dec 23, 2019
97cc3e9
Updated version of prims algorithm
Subhojit-Kumar-Saha Dec 24, 2019
3b3ed0a
Added jump search in Searching Algorithms
Sumangrewal Dec 24, 2019
35e0e67
Merge pull request #48 from nikitashaw13/master
chanmol1999 Dec 24, 2019
edd6e57
Update prims_algorithm.cpp
Subhojit-Kumar-Saha Dec 25, 2019
4c5ff8b
Merge pull request #83 from Subhojit-Kumar-Saha/master
Bhavnaharitsa Dec 25, 2019
3afdbdc
Updated addition of Jump Search Algo
Sumangrewal Dec 25, 2019
8bb6a77
Added sample input output on top
Sumangrewal Dec 25, 2019
5e24c93
Merge pull request #86 from Sumangrewal/master
Bhavnaharitsa Dec 25, 2019
4a449aa
Created Linear_Search
ashiscs Dec 25, 2019
65e14b8
Merge pull request #90 from ashiscs/patch-5
Bhavnaharitsa Dec 26, 2019
7088110
Adding radix sort algorithm
Sumangrewal Dec 26, 2019
d1712f3
fix broken link and add a working example
MahmoudMabrok Dec 28, 2019
f7e508e
Merge pull request #98 from MahmoudMabrok/patch-1
Bhavnaharitsa Dec 28, 2019
8a3cf02
proper path structure
axshivam Dec 28, 2019
a451138
mathematical
axshivam Dec 28, 2019
816a6c9
added
axshivam Dec 28, 2019
61e5352
broken link
axshivam Dec 28, 2019
31119a4
Added Kadane Algo
Dec 29, 2019
6494f70
Update kadane.cpp
ivaibhavjindal Dec 30, 2019
223d117
update
axshivam Dec 30, 2019
744ccc2
update1
axshivam Dec 30, 2019
4cbed87
update2
axshivam Dec 30, 2019
4e18744
update3
axshivam Dec 30, 2019
b8d6980
Euler's Totient function
axshivam Dec 30, 2019
21734af
Factorization
axshivam Dec 30, 2019
474db1a
GCD by Euclidean algorithm
axshivam Dec 30, 2019
3c641bd
LCM
axshivam Dec 30, 2019
b527a53
Matrix Exponentiation
axshivam Dec 30, 2019
ac97e0d
Pigeonhole Algorithm
axshivam Dec 30, 2019
570a8c2
Sieve of Eratosthenes
axshivam Dec 30, 2019
79ecf2b
SIEVE OF ERATOSTHENES
axshivam Dec 30, 2019
e02d8d3
PIGEONHOLE ALGORITHM
axshivam Dec 30, 2019
c36fd92
MATRIX EXPONENTIATION
axshivam Dec 30, 2019
43f0dfd
LCM
axshivam Dec 30, 2019
fee5e29
GCD BY EUCLIDEAN ALGORITHM
axshivam Dec 30, 2019
467fedd
FACTORIZATION
axshivam Dec 30, 2019
d40c890
EULER'S TOTIENT FUNCTION
axshivam Dec 30, 2019
1d8fb54
Merge pull request #103 from ivaibhavjindal/one
Bhavnaharitsa Dec 30, 2019
0d73997
Changed input from static to dynamic
Sumangrewal Dec 30, 2019
a3cc0a1
Added Detect Cycle in Undirected Graph
akashksinghal Dec 30, 2019
796626a
Merge pull request #94 from Sumangrewal/master
Bhavnaharitsa Dec 31, 2019
2a54d9b
Update Detect_Cycle_In_Undirected_Graph.cpp
akashksinghal Dec 31, 2019
5ba12d8
Update Detect_Cycle_In_Undirected_Graph.cpp
akashksinghal Dec 31, 2019
1cf7567
Update Detect_Cycle_In_Undirected_Graph.cpp
akashksinghal Dec 31, 2019
cad5bbf
Merge pull request #106 from akashksinghal/master
Bhavnaharitsa Dec 31, 2019
31119c0
Update README.md
akashksinghal Dec 31, 2019
232a745
Update README.md
akashksinghal Dec 31, 2019
e605299
Egyptian Fraction Algorithm added
parjanyaacoder Jan 1, 2020
6f058e5
Update README.md
alpha74 Jan 1, 2020
ba9df0d
Update README.md
alpha74 Jan 1, 2020
dd9b746
Update README.md
alpha74 Jan 1, 2020
9362889
Merge pull request #120 from alpha74/sort-titles
Bhavnaharitsa Jan 1, 2020
a660bb5
Updated EgyptianFractions.cpp
parjanyaacoder Jan 1, 2020
3c602cd
Updated ReadMe.md file
parjanyaacoder Jan 1, 2020
b66c441
Merge branch 'master' into Greedy
parjanyaacoder Jan 1, 2020
ddffc72
Update readme
Jan 1, 2020
c7b7307
Update README.md
akashksinghal Jan 1, 2020
a215dc9
Merge branch 'master' into patch-1
akashksinghal Jan 1, 2020
a0d8aee
Infix To Prefix, Postfix, Value
adiXcodr Jan 1, 2020
9fd698e
Binary Search Tree Sort
adiXcodr Jan 1, 2020
9855812
Merge pull request #110 from akashksinghal/patch-1
Bhavnaharitsa Jan 2, 2020
589a1d3
Merge branch 'master' into up
ivaibhavjindal Jan 2, 2020
4c51a1f
Merge pull request #121 from ivaibhavjindal/up
Bhavnaharitsa Jan 2, 2020
8531f5e
Create quick_sort.cpp
dheerajkotwani Jan 2, 2020
4bc0f4a
Infix Convertor
adiXcodr Jan 2, 2020
c8aadf7
Merge branch 'master' of https://github.com/NJACKWinterOfCode/nwoc_al…
adiXcodr Jan 2, 2020
fb46726
Merge branch 'master' into Greedy
parjanyaacoder Jan 2, 2020
84faf26
Tree Sort, Infix Converter
adiXcodr Jan 3, 2020
1a27af9
Merge branch 'master' of https://github.com/adiXcodr/nwoc_algorithms
adiXcodr Dec 2, 2019
9a99fa6
Update quick_sort.cpp
dheerajkotwani Jan 3, 2020
66c3c1c
Merge pull request #123 from dheerajkotwani/patch-1
Bhavnaharitsa Jan 4, 2020
50f6fa5
Update EgyptianFractions.cpp
parjanyaacoder Jan 4, 2020
633c174
Merge pull request #126 from adiXcodr/master
Bhavnaharitsa Jan 5, 2020
e7f7ec7
Update README.md
parjanyaacoder Jan 5, 2020
8f748c7
Update README.md
parjanyaacoder Jan 5, 2020
4624074
Merge pull request #116 from parjanyaacoder/Greedy
Bhavnaharitsa Jan 6, 2020
962f2f5
Merge pull request #29 from sgpritam/balance
chanmol1999 Jan 6, 2020
721851d
Merge pull request #32 from ghostmk/master
chanmol1999 Jan 6, 2020
bce9f87
Merge pull request #34 from promaroy/mybranch
chanmol1999 Jan 6, 2020
8506258
Merge pull request #37 from breaking-let/master
chanmol1999 Jan 6, 2020
26b56b5
Merge pull request #44 from namanvats/master
chanmol1999 Jan 6, 2020
8a9e84f
Merge pull request #54 from susanka068/master
chanmol1999 Jan 6, 2020
fcf9a1e
Merge pull request #55 from yashagarwal1999/master
chanmol1999 Jan 6, 2020
029e2b4
Merge branch 'master' into first
chanmol1999 Jan 6, 2020
7e3defb
Merge pull request #63 from adi1307/first
chanmol1999 Jan 6, 2020
ef5147d
Merge branch 'master' into master
chanmol1999 Jan 6, 2020
f22a359
Merge pull request #84 from Abhishek-kumar09/master
chanmol1999 Jan 6, 2020
5332ece
Merge pull request #85 from VersatileVishal/master
chanmol1999 Jan 6, 2020
63f2b6f
Merge branch 'master' into master
axshivam Jan 9, 2020
dc98222
Merge pull request #99 from Shiv-sharma-111/master
Bhavnaharitsa Jan 9, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 157 additions & 0 deletions Algorithms/Backtracking/Hamiltonian Cycle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// This program detects any Hamiltonian Cycle in a graph

#include <bits/stdc++.h>
using namespace std;

// Number of vertices in the graph
#define V 5

void printSolution(int path[]);

/* A utility function to check if
the vertex v can be added at index 'pos'
in the Hamiltonian Cycle constructed
so far (stored in 'path[]') */
bool isSafe(int v, bool graph[V][V],
int path[], int pos)
{
/* Check if this vertex is an adjacent
vertex of the previously added vertex. */
if (graph [path[pos - 1]][ v ] == 0)
return false;

/* Check if the vertex has already been included.
This step can be optimized by creating
an array of size V */
for (int i = 0; i < pos; i++)
if (path[i] == v)
return false;

return true;
}

/* A recursive utility function
to solve hamiltonian cycle problem */
bool hamCycleUtil(bool graph[V][V],
int path[], int pos)
{
/* base case: If all vertices are
included in Hamiltonian Cycle */
if (pos == V)
{
// And if there is an edge from the
// last included vertex to the first vertex
if (graph[path[pos - 1]][path[0]] == 1)
return true;
else
return false;
}

// Try different vertices as a next candidate
// in Hamiltonian Cycle. We don't try for 0 as
// we included 0 as starting point in hamCycle()
for (int v = 1; v < V; v++)
{
/* Check if this vertex can be added
// to Hamiltonian Cycle */
if (isSafe(v, graph, path, pos))
{
path[pos] = v;

/* recur to construct rest of the path */
if (hamCycleUtil (graph, path, pos + 1) == true)
return true;

/* If adding vertex v doesn't lead to a solution,
then remove it */
path[pos] = -1;
}
}

/* If no vertex can be added to
Hamiltonian Cycle constructed so far,
then return false */
return false;
}

/* It mainly uses hamCycleUtil() to
solve the problem. It returns false if there is no
Hamiltonian Cycle possible, otherwise return true
and prints the path.*/
bool hamCycle(bool graph[V][V])
{
int *path = new int[V];
for (int i = 0; i < V; i++)
path[i] = -1;

/* Let us put vertex 0 as the first vertex in the path.
If there is a Hamiltonian Cycle, then the path can be
started from any point of the cycle as the graph is undirected */
path[0] = 0;
if (hamCycleUtil(graph, path, 1) == false )
{
cout << "\nSolution does not exist";
return false;
}

printSolution(path);
return true;
}

/* A utility function to print solution */
void printSolution(int path[])
{
cout << "Solution Exists:"
" Following is one Hamiltonian Cycle \n";
for (int i = 0; i < V; i++)
cout << path[i] << " ";

// Let us print the first vertex again
// to show the complete cycle
cout << path[0] << " ";
cout << endl;
}

// Driver Code
int main()
{
/* Let us create the following graph
(0)--(1)--(2)
| / \ |
| / \ |
| / \ |
(3)-------(4) */
bool graph1[V][V] = {{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 1},
{0, 1, 1, 1, 0}};

// Print the solution
hamCycle(graph1);

/* Let us create the following graph
(0)--(1)--(2)
| / \ |
| / \ |
| / \ |
(3) (4) */
bool graph2[V][V] = {{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 0},
{0, 1, 1, 0, 0}};

// Print the solution
hamCycle(graph2);

return 0;
}


/*Output:
Solution Exists: Following is one Hamiltonian Cycle
0 1 2 4 3 0

Solution does not exist
*/
109 changes: 109 additions & 0 deletions Algorithms/Backtracking/M Coloring Problem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/* To determine if an undirected graph can be colored with atmost m colors
such that no two adjacent vertices are colored with the same color */

#include<bits/stdc++.h>

// Number of vertices in the graph
#define V 4

void printSolution(int color[]);

/* A utility function to check whether the edge exists or not
(i.e, graph[v][i]==1). If exist then checks whether the color to
be filled in the new vertex(c is sent in the parameter) is already
used by its adjacent vertices(i-->adj vertices) or not (i.e, color[i]==c) */

bool isSafe (int v, bool graph[V][V], int color[], int c)
{
for (int i = 0; i < V; i++)
if (graph[v][i] && c == color[i])
return false;
return true;
}

/* A recursive utility function to solve m coloring problem */
bool graphColoringUtil(bool graph[V][V], int m, int color[], int v)
{
/* base case: If all vertices are assigned a color then
return true */
if (v == V)
return true;

/* Consider this vertex v and try different colors */
for (int c = 1; c <= m; c++)
{
/* Check if assignment of color c to v is fine*/
if (isSafe(v, graph, color, c))
{
color[v] = c;

/* recur to assign colors to rest of the vertices */
if (graphColoringUtil (graph, m, color, v+1) == true)
return true;

/* If assigning color c doesn't lead to a solution
then remove it */
color[v] = 0;
}
}

/* If no color can be assigned to this vertex then return false */
return false;
}

/* This function returns false if the m colors cannot be assigned, otherwise return true and
prints assignments of colors to all vertices. */
bool graphColoring(bool graph[V][V], int m)
{
// Initialize all color values as 0. This initialization is needed
// correct functioning of isSafe()
int color[V];
for (int i = 0; i < V; i++)
color[i] = 0;

// Call graphColoringUtil() for vertex 0
if (graphColoringUtil(graph, m, color, 0) == false)
{
cout<<"Solution does not exist";
return false;
}

// Print the solution
printSolution(color);
return true;
}

/* A utility function to print solution */
void printSolution(int color[])
{
cout<<"Solution Exists:Following are the assigned colors \n";
for (int i = 0; i < V; i++)
cout<<" "<<color[i]<<" ";
cout<<endl;
}

// driver program to test above function
int main()
{
/* Create following graph and test whether it is 3 colorable
(3)---(2)
| / |
| / |
| / |
(0)---(1)
*/
bool graph[V][V] = {{0, 1, 1, 1},
{1, 0, 1, 0},
{1, 1, 0, 1},
{1, 0, 1, 0},
};
int m = 3; // Number of colors
graphColoring (graph, m);
return 0;
}


/*Output:
Solution Exists: Following are the assigned colors
1 2 3 2
*/
107 changes: 107 additions & 0 deletions Algorithms/Backtracking/N Queen Problem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/* To place N Queens on a NXN chessboard so that no two queens attack each other */

#define N 4
#include <stdbool.h>
#include <stdio.h>

/* A utility function to print solution */
void printSolution(int board[N][N])
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
printf(" %d ", board[i][j]);
printf("\n");
}
}

/* A utility function to check if a queen can
be placed on board[row][col]. */
bool isSafe(int board[N][N], int row, int col)
{
int i, j;

/* Check this row on left side */
for (i = 0; i < col; i++)
if (board[row][i])
return false;

/* Check upper diagonal on left side */
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
if (board[i][j])
return false;

/* Check lower diagonal on left side */
for (i = row, j = col; j >= 0 && i < N; i++, j--)
if (board[i][j])
return false;

return true;
}

/* A recursive utility function to solve N
Queen problem */
bool solveNQUtil(int board[N][N], int col)
{
/* base case: If all queens are placed
then return true */
if (col >= N)
return true;

/* Consider this column and try placing
this queen in all rows one by one */
for (int i = 0; i < N; i++) {
/* Check if the queen can be placed on
board[i][col] */
if (isSafe(board, i, col)) {
/* Place this queen in board[i][col] */
board[i][col] = 1;

/* recur to place rest of the queens */
if (solveNQUtil(board, col + 1))
return true;

/* If placing queen in board[i][col]
doesn't lead to a solution, then
remove queen from board[i][col] */
board[i][col] = 0; // BACKTRACK
}
}

/* If the queen cannot be placed in any row in
this colum col then return false */
return false;
}

/* This function returns false if queens
cannot be placed, otherwise, return true and
prints placement of queens in the form of 1s.*/
bool solveNQ()
{
int board[N][N] = { { 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };

if (solveNQUtil(board, 0) == false) {
printf("Solution does not exist");
return false;
}

printSolution(board);
return true;
}

// driver program to test above function
int main()
{
solveNQ();
return 0;
}


/*Output:
0 0 1 0
1 0 0 0
0 0 0 1
0 1 0 0
*/
Loading