Skip to content

Commit efb5e08

Browse files
Petersen Graph Algorithm
1 parent 9206436 commit efb5e08

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

Graph/PetersenGraph.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
char S[100005];
5+
6+
bool adj[10][10];
7+
8+
char result[100005];
9+
10+
bool findthepath(char* S, int v)
11+
{
12+
result[0] = v + '0';
13+
for (int i = 1; S[i]; i++) {
14+
15+
if (adj[v][S[i] - 'A'] || adj[S[i] -
16+
'A'][v]) {
17+
v = S[i] - 'A';
18+
}
19+
20+
else if (adj[v][S[i] - 'A' + 5] ||
21+
adj[S[i] - 'A' + 5][v]) {
22+
v = S[i] - 'A' + 5;
23+
}
24+
25+
else
26+
return false;
27+
28+
result[i] = v + '0';
29+
}
30+
31+
return true;
32+
}
33+
34+
int main()
35+
{
36+
adj[0][1] = adj[1][2] = adj[2][3] =
37+
adj[3][4] = adj[4][0] = adj[0][5] =
38+
adj[1][6] = adj[2][7] = adj[3][8] =
39+
adj[4][9] = adj[5][7] = adj[7][9] =
40+
adj[9][6] = adj[6][8] = adj[8][5] = true;
41+
42+
char S[] = "ABB";
43+
44+
if (findthepath(S, S[0] - 'A') ||
45+
findthepath(S, S[0] - 'A' + 5)) {
46+
cout << result;
47+
}
48+
49+
else {
50+
cout << "-1";
51+
}
52+
return 0;
53+
}

0 commit comments

Comments
 (0)