Projects Inventory

Dijkstra’s Algorithm Gaming Project in C++

Explanation:

  1. Graph Representation:
    • The graph is represented as an adjacency list using a vector of vectors. Each inner vector contains pairs representing a connected vertex and the weight of the edge to that vertex.
    • Advertisement
  2. Node Structure:
    • The Node structure is used to store the current vertex and the distance from the source vertex. It also includes a custom comparison operator to allow the priority queue to prioritize nodes with the smallest distance.
    • Advertisement
  3. Dijkstra’s Algorithm (dijkstra):
    • The function takes the starting vertex and the graph as input and returns a vector of the shortest distances from the start vertex to all other vertices.
    • It initializes a vector distances to store the shortest known distance to each vertex, setting all distances to infinity (INF) initially except the start vertex, which is set to 0.
    • A priority queue is used to always expand the least costly node. The algorithm then iterates through each node’s neighbors, updating their distances if a shorter path is found.
  4. Main Function (main):
    • The main function creates a sample graph with 6 vertices and various weighted edges.
    • Advertisement
    • It runs Dijkstra’s algorithm from the starting vertex (vertex 0) and prints out the shortest distances to all other vertices.

Possible Enhancements:

Exit mobile version