|
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | +#include <queue> |
| 4 | + |
| 5 | +#define MAX_VERTICES 6 // Maximum number of vertices in the graph |
| 6 | + |
| 7 | +// Structure that defines each Vertex of the Graph |
| 8 | +struct Vertex |
| 9 | +{ |
| 10 | + char id; |
| 11 | + std::vector<Vertex *> neighbors; // List of neighbors |
| 12 | + bool visited; |
| 13 | + |
| 14 | + Vertex(char id) : id(id), visited(false) {} |
| 15 | +}; |
| 16 | + |
| 17 | +// Creates a vertex and returns it |
| 18 | +Vertex *createVertex(char id) |
| 19 | +{ |
| 20 | + return new Vertex(id); |
| 21 | +} |
| 22 | + |
| 23 | +// Links two vertices (makes them neighbors) |
| 24 | +void linkVertices(Vertex *v1, Vertex *v2) |
| 25 | +{ |
| 26 | + v1->neighbors.push_back(v2); // Add v2 as a neighbor of v1 |
| 27 | + v2->neighbors.push_back(v1); // Add v1 as a neighbor of v2 |
| 28 | +} |
| 29 | + |
| 30 | +/* |
| 31 | + * Depth First Search (DFS) |
| 32 | + * Recursively visits neighbors of the starting vertex |
| 33 | + */ |
| 34 | +int depthFirstSearch(Vertex *start, Vertex *destination, int steps) |
| 35 | +{ |
| 36 | + start->visited = true; // Mark the current vertex as visited |
| 37 | + if (start == destination) |
| 38 | + return steps; // If found, return the distance |
| 39 | + |
| 40 | + for (Vertex *neighbor : start->neighbors) |
| 41 | + { // Visit all neighbors |
| 42 | + if (!neighbor->visited) |
| 43 | + { // If neighbor hasn't been visited |
| 44 | + int result = depthFirstSearch(neighbor, destination, steps + 1); |
| 45 | + if (result != -1) |
| 46 | + return result; // If destination found, return result |
| 47 | + } |
| 48 | + } |
| 49 | + return -1; // Destination not found |
| 50 | +} |
| 51 | + |
| 52 | +/* |
| 53 | + * Breadth First Search (BFS) |
| 54 | + * Uses a queue to traverse level by level |
| 55 | + */ |
| 56 | +int breadthFirstSearch(Vertex *start, Vertex *destination) |
| 57 | +{ |
| 58 | + std::queue<Vertex *> q; |
| 59 | + q.push(start); // Enqueue starting vertex |
| 60 | + start->visited = true; |
| 61 | + |
| 62 | + int steps = 0; |
| 63 | + |
| 64 | + while (!q.empty()) |
| 65 | + { |
| 66 | + int qSize = q.size(); // Current queue size (level size) |
| 67 | + |
| 68 | + // Process all vertices at the current level |
| 69 | + for (int i = 0; i < qSize; i++) |
| 70 | + { |
| 71 | + Vertex *current = q.front(); |
| 72 | + q.pop(); |
| 73 | + |
| 74 | + if (current == destination) |
| 75 | + return steps; // If destination found, return steps |
| 76 | + |
| 77 | + // Enqueue all unvisited neighbors |
| 78 | + for (Vertex *neighbor : current->neighbors) |
| 79 | + { |
| 80 | + if (!neighbor->visited) |
| 81 | + { |
| 82 | + neighbor->visited = true; |
| 83 | + q.push(neighbor); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + steps++; // Increment the level |
| 88 | + } |
| 89 | + return -1; // Destination not found |
| 90 | +} |
| 91 | + |
| 92 | +int main() |
| 93 | +{ |
| 94 | + // Create vertices |
| 95 | + Vertex *A = createVertex('A'); |
| 96 | + Vertex *B = createVertex('B'); |
| 97 | + Vertex *C = createVertex('C'); |
| 98 | + Vertex *D = createVertex('D'); |
| 99 | + Vertex *E = createVertex('E'); |
| 100 | + Vertex *F = createVertex('F'); |
| 101 | + |
| 102 | + // Link vertices as per the graph structure |
| 103 | + linkVertices(A, B); |
| 104 | + linkVertices(A, C); |
| 105 | + linkVertices(B, D); |
| 106 | + linkVertices(C, D); |
| 107 | + linkVertices(B, E); |
| 108 | + linkVertices(D, E); |
| 109 | + linkVertices(E, F); |
| 110 | + linkVertices(D, F); |
| 111 | + |
| 112 | + // Perform Depth First Search |
| 113 | + int result = depthFirstSearch(A, F, 0); |
| 114 | + if (result != -1) |
| 115 | + std::cout << "DFS - Found. Distance: " << result << std::endl; |
| 116 | + else |
| 117 | + std::cout << "DFS - Not Found." << std::endl; |
| 118 | + |
| 119 | + // Reset visited status for all vertices |
| 120 | + A->visited = false; |
| 121 | + B->visited = false; |
| 122 | + C->visited = false; |
| 123 | + D->visited = false; |
| 124 | + E->visited = false; |
| 125 | + F->visited = false; |
| 126 | + |
| 127 | + // Perform Breadth First Search |
| 128 | + result = breadthFirstSearch(A, F); |
| 129 | + if (result != -1) |
| 130 | + std::cout << "BFS - Found. Distance: " << result << std::endl; |
| 131 | + else |
| 132 | + std::cout << "BFS - Not Found." << std::endl; |
| 133 | + |
| 134 | + return 0; |
| 135 | +} |
0 commit comments