Projects Inventory

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).
    • Advertisement
    • #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.
      • Advertisement
    • 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.
        • Advertisement
        • 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.

 

Exit mobile version