Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
A Peterson Graph Problem in C Program?
The Peterson Graph is a specific undirected graph with 10 vertices and 15 edges. In this problem, we need to find a walk through the Peterson Graph that realizes a given string pattern. Each vertex is labeled with a letter (A-E), and we must find a path where the sequence of vertex labels matches our target string.
Syntax
bool petersonGraphWalk(char* S, int startVertex);
Algorithm
The algorithm checks if we can traverse from one vertex to another following the graph's edge connections. For each character in the string, we try to find a valid adjacent vertex with the same label −
petersonGraphWalk(S, v) ?
begin
res := starting vertex
for each character c in S except the first one, do
if there is an edge between v and c in outer graph, then
v := c
else if there is an edge between v and c+5 in inner graph, then
v := c + 5
else
return false
end if
put v into res
done
return true
end
Example
Let's implement the Peterson Graph walk to find a path for the string "ABBECCD" −
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int adj_mat[10][10] = {
{0, 1, 0, 0, 1, 1, 0, 0, 0, 0},
{1, 0, 1, 0, 0, 0, 1, 0, 0, 0},
{0, 1, 0, 1, 0, 0, 0, 1, 0, 0},
{0, 0, 1, 0, 1, 0, 0, 0, 1, 0},
{1, 0, 0, 1, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1, 1, 0},
{0, 1, 0, 0, 0, 0, 0, 0, 1, 1},
{0, 0, 1, 0, 0, 1, 0, 0, 0, 1},
{0, 0, 0, 1, 0, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 1, 1, 0, 0}
};
char result[100];
bool petersonGraphWalk(char* S, int v) {
result[0] = v + '0';
for (int i = 1; S[i]; i++) {
// Check outer graph (vertices 0-4)
if (adj_mat[v][S[i] - 'A'] || adj_mat[S[i] - 'A'][v]) {
v = S[i] - 'A';
}
// Check inner graph (vertices 5-9)
else if (adj_mat[v][S[i] - 'A' + 5] || adj_mat[S[i] - 'A' + 5][v]) {
v = S[i] - 'A' + 5;
} else {
return false;
}
result[i] = v + '0';
}
result[strlen(S)] = '\0';
return true;
}
int main() {
char str[] = "ABBECCD";
printf("Input string: %s<br>", str);
// Try starting from outer vertex (A=0) or inner vertex (A=5)
if (petersonGraphWalk(str, str[0] - 'A') || petersonGraphWalk(str, str[0] - 'A' + 5)) {
printf("Walk found: %s<br>", result);
} else {
printf("No valid walk found: -1<br>");
}
return 0;
}
Input string: ABBECCD Walk found: 0169723
How It Works
The Peterson Graph has 10 vertices (0-9) where vertices 0-4 form the outer pentagon and 5-9 form the inner pentagon. Each vertex is labeled with letters A-E. The algorithm tries to find a path where:
- We start from vertex corresponding to the first character
- For each subsequent character, we move to an adjacent vertex with the same label
- If no such vertex exists, the walk is impossible
Conclusion
The Peterson Graph walk problem demonstrates graph traversal with constraints. The adjacency matrix approach efficiently checks valid moves, and the algorithm returns the lexicographically smallest valid path or -1 if no path exists.
