Skip to content

Commit 3de7cad

Browse files
committed
backtracking (Array permutation)
1 parent f635623 commit 3de7cad

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
2+
<CodeBlocks_project_file>
3+
<FileVersion major="1" minor="6" />
4+
<Project>
5+
<Option title="Backtracking (Array Permutation)" />
6+
<Option pch_mode="2" />
7+
<Option compiler="gcc" />
8+
<Build>
9+
<Target title="Debug">
10+
<Option output="bin/Debug/Backtracking (Array Permutation)" prefix_auto="1" extension_auto="1" />
11+
<Option object_output="obj/Debug/" />
12+
<Option type="1" />
13+
<Option compiler="gcc" />
14+
<Compiler>
15+
<Add option="-g" />
16+
</Compiler>
17+
</Target>
18+
<Target title="Release">
19+
<Option output="bin/Release/Backtracking (Array Permutation)" prefix_auto="1" extension_auto="1" />
20+
<Option object_output="obj/Release/" />
21+
<Option type="1" />
22+
<Option compiler="gcc" />
23+
<Compiler>
24+
<Add option="-O2" />
25+
</Compiler>
26+
<Linker>
27+
<Add option="-s" />
28+
</Linker>
29+
</Target>
30+
</Build>
31+
<Compiler>
32+
<Add option="-Wall" />
33+
<Add option="-fexceptions" />
34+
</Compiler>
35+
<Unit filename="main.cpp" />
36+
<Extensions>
37+
<code_completion />
38+
<envvars />
39+
<debugger />
40+
<lib_finder disable_auto="1" />
41+
</Extensions>
42+
</Project>
43+
</CodeBlocks_project_file>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
2+
<CodeBlocks_layout_file>
3+
<FileVersion major="1" minor="0" />
4+
<ActiveTarget name="Debug" />
5+
<File name="main.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
6+
<Cursor>
7+
<Cursor1 position="3164" topLine="66" />
8+
</Cursor>
9+
</File>
10+
</CodeBlocks_layout_file>
Binary file not shown.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
/*
7+
Permutation of an array using the backtracking technique which
8+
finds all possible solutions without repeating.
9+
*/
10+
11+
ostream& operator << (ostream& stream, const vector<int>& v) {
12+
for (int i = 0; i < (int)v.size(); i++) {
13+
stream << v[i] << " ";
14+
}
15+
return stream;
16+
}
17+
18+
void permutation(vector<int>& v, int index, int arraySize, string space = "") {
19+
cout << space << "permutation(v, " << index << ", " << arraySize << ")" << endl;
20+
/*
21+
permutation([1,2,3], 0, 3, "")
22+
i == 0
23+
swap(array[0], array[0]);
24+
permutation([1,2,3], 1, 3, "") -> index + 1
25+
i = 1
26+
swap(array[1], array[1]);
27+
permutation([1,2,3], 2, 3, "") -> index + 1
28+
base case ->> print([1,2,3])
29+
swap(array[1], array[1]); -> ->> [1,2,3] again (backtracking)
30+
i = 2
31+
swap(array[1], array[2]);
32+
permutation([1,3,2], 2, 3, "") -> index + 1
33+
base case ->> print([1,3,2])
34+
swap(array[1], array[2]); ->> [1,2,3] again (backtracking)
35+
i = 3
36+
break loop;
37+
i == 1
38+
swap(array[0], array[1]);
39+
permutation([2,1,3], 1, 3, "") -> index + 1
40+
i = 1
41+
swap(array[1], array[1]);
42+
permutation([2,1,3], 2, 3, "") -> index + 1
43+
base case ->> print([2,1,3])
44+
swap(array[1], array[1]); ->> [2,1,3] again (backtracking)
45+
i = 2
46+
swap(array[1], array[2]);
47+
permutation([2,3,1], 2, 3, "") -> index + 1
48+
base case ->> print([2,3,1])
49+
swap(array[1], array[2]); ->> [2,1,3] again (backtracking)
50+
i = 3
51+
break loop;
52+
i == 2
53+
swap(array[0], array[2]);
54+
permutation([3,1,2], 1, 3, "") -> index + 1
55+
i = 1
56+
swap(array[1], array[1]);
57+
permutation([3,1,2], 2, 3, "") -> index + 1
58+
base case ->> print([3,1,2])
59+
swap(array[1], array[1]); ->> [3,1,2] again (backtracking)
60+
i = 2
61+
swap(array[1], array[2]);
62+
permutation([3,2,1], 2, 3, "") -> index + 1
63+
base case ->> print([3,2,1])
64+
swap(array[1], array[2]); ->> [3,1,2] again (backtracking)
65+
i = 3
66+
break loop;
67+
i == 3
68+
break loop;
69+
finish function
70+
*/
71+
// Base case
72+
if(index == arraySize - 1) {
73+
cout << "->>>> " << v << endl;
74+
} else {
75+
for (int i = index; i < arraySize; i++) {
76+
swap(v[index], v[i]);
77+
permutation(v, index + 1, arraySize, space + " ");
78+
swap(v[index], v[i]); // Backtrack phase (reordering array)
79+
}
80+
}
81+
}
82+
83+
int main()
84+
{
85+
vector<int> v = {1,2,3};
86+
permutation(v, 0, 3);
87+
return 0;
88+
}
150 KB
Binary file not shown.

0 commit comments

Comments
 (0)