BSF Algorithm

An undirected graph has 24 vertices denoted by A’, B,’ C’, D’, E’, F’, G’, H’, I’, J’, K’, L’, M’, N’, P’, Q’, R’, S’, T’, U’, V’, W’, Y’, and Z’ respectively. {M’, Z’}, {M’, Y’}, {L’, W’}, {K’, V’}, {J’, U’}, {J’, T’}, {I’, S’}, {H’, R’}, {G’, Q’}, {F’, P’}, {F’, N’}, {E’, M’}, {E’, L’}, {D’, K’}, {D’, J’}, {D’, I’}, {C’, H’}, {C’, G’}, {B’, F’}, {B’, E’}, {A’, D’}, {A’, C’}, and {A’, B’} are the edges in this graph. Assume that breadth-first search does not revisit any vertex along any path. Each edge is equivalent to two operators, e.g., the operators corresponding to {A’, B’} are A’ --> B’ and B’ --> A’. How many vertices other than D’ and V’ is breadth-first search guaranteed to visit if D’ is the initial state and V’ is the goal state?
I have written the following code but I am confused about one thing. should I consider immediate neighbors of D including A,I,J and K and take into account all possibilities? let's say

from D, I,J, and K can be visited, then from I "S" is visited, from J, "T" and "U" and from "K", "V" is visited. so far {E,J,K,S,T,U} are visited in this side. Now my question is should I consider all paths through A as well? Like B and C can be visited through A, then E and F can be visited through B and so on so far. Your help is strongly appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
#include <string>

using namespace std;

unordered_map<char, vector<char>> graph = {
    {'A', {'B', 'C', 'D'}},
    {'B', {'A', 'F', 'E'}},
    {'C', {'A', 'H', 'G'}},
    {'D', {'A', 'K', 'J', 'I'}},
    {'E', {'B', 'M', 'L'}},
    {'F', {'B', 'P', 'N'}},
    {'G', {'C', 'Q'}},
    {'H', {'C', 'R'}},
    {'I', {'D', 'S'}},
    {'J', {'D', 'T', 'U'}},
    {'K', {'D', 'V'}},
    {'L', {'E', 'W'}},
    {'M', {'E', 'Z', 'Y'}},
    {'N', {'F'}},
    {'P', {'F'}},
    {'Q', {'G'}},
    {'R', {'H'}},
    {'S', {'I'}},
    {'T', {'J'}},
    {'U', {'J'}},
    {'V', {'K'}},
    {'W', {'L'}},
    {'Y', {'M'}},
    {'Z', {'M'}}
};

int bfs(char start, char goal) {
    queue<vector<char>> q;
    unordered_map<char, bool> visited;
    q.push({ start });
    visited[start] = true;

    while (!q.empty()) {
        vector<char> path = q.front();
        q.pop();
        char node = path.back();

        if (node == goal) {
            cout << "Shortest path: ";
            for (char vertex : path) {
                cout << vertex << " -> ";
            }
            cout << "\b\b\b   \n"; 
            return path.size() - 1;
        }

        for (char adjacent : graph[node]) {
            if (!visited[adjacent]) {
                visited[adjacent] = true;
                vector<char> new_path = path;
                new_path.push_back(adjacent);
                q.push(new_path);
            }
        }
    }

    return -1; 
}

int main() {
    char start = 'Z';
    char goal = 'C';

    int operators = bfs(start, goal);
    if (operators != -1) {
        cout << "Number of operators on the path found by BFS: " << operators << endl;
    }
    else {
        cout << "Goal state not reachable from the initial state!" << endl;
    }

    return 0;
}
Last edited on
{'D', {'A', 'K', 'J', 'I'}}

From D you can reach four vertices.
1
2
3
4
{'A', {'B', 'C', 'D'}},
{'K', {'V', 'D'}},
{'J', {'T', 'U', 'D'}},
{'I', {'S', 'D'}}

From 'A', 'K', 'J', and 'I' you can reach six new vertices.
@Keskiverto Thanks for the answer.
Topic archived. No new replies allowed.