Basic Graph Algorithms (DFS, BFS) Gaming Project in C++

Explanation

  1. Include Libraries:
    • #include <iostream>: For input and output operations.
    • #include <vector>: For using dynamic arrays (vectors).
    • #include <queue>: For the BFS implementation.
    • #include <stack>: For the DFS implementation.
  2. Graph Class:
    • Private Data Members:
      • V: Number of vertices.
      • adj: Adjacency list representation of the graph, where each vertex has a list of its neighbors.
    • Public Methods:
      • Graph(int vertices): Constructor to initialize the graph with a given number of vertices.
      • void addEdge(int u, int v): Adds an undirected edge between vertices u and v.
      • void DFS(int start) const: Performs Depth-First Search starting from vertex start.
        • Uses a stack to manage the nodes.
        • Marks nodes as visited and prints them.
        • Pushes unvisited neighbors onto the stack.
      • void BFS(int start) const: Performs Breadth-First Search starting from vertex start.
        • Uses a queue to manage the nodes.
        • Marks nodes as visited and prints them.
        • Enqueues unvisited neighbors.
  3. Main Function (main):
    • Creates a Graph object with a specified number of vertices.
    • Adds edges to the graph using the addEdge method.
    • Calls DFS and BFS methods to demonstrate the traversal of the graph starting from vertex 0.

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top