📜  门| GATE-CS-2005 |问题2(1)

📅  最后修改于: 2023-12-03 15:12:41.323000             🧑  作者: Mango

GATE-CS-2005 Problem 2

Introduction

This is a programming problem from the GATE Computer Science 2005 exam. It tests the ability of the programmer to write efficient algorithms to solve a given problem.

Problem Description

The problem involves finding the shortest path between two vertices in a directed acyclic graph (DAG). The graph is represented using adjacency lists and each edge has a weight. The program should take as input the number of vertices, the number of edges, the adjacency lists, the start vertex and the destination vertex. It should output the length of the shortest path and the path itself.

Solution Approach

The solution to this problem involves using a modified version of the topological sort algorithm. We first run a topological sort on the graph to get a linear ordering of the vertices. We then initialize the distance of the start vertex to 0 and all other vertices to infinity. We maintain a priority queue of vertices to be visited next. We iterate through the vertices in the linear order and for each vertex, we iterate through its neighbors and update their distances. We also update the priority queue with these neighbors. We continue this process until the destination vertex is reached. The distance of the destination vertex gives us the length of the shortest path and the path can be obtained using backtracking.

Code Implementation
// Function to find the shortest path in a DAG
void shortestPath(int n, int m, vector<vector<pair<int, int>>> adjList, int start, int dest) {
    // Topological sort
    vector<int> topoOrder;
    vector<int> inDegree(n, 0);
    queue<int> q;
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < adjList[i].size(); j++) {
            int v = adjList[i][j].first;
            inDegree[v]++;
        }
    }
    
    for (int i = 0; i < n; i++) {
        if (inDegree[i] == 0) q.push(i);
    }
    
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        topoOrder.push_back(u);
        for (int i = 0; i < adjList[u].size(); i++) {
            int v = adjList[u][i].first;
            inDegree[v]--;
            if (inDegree[v] == 0) q.push(v);
        }
    }
    
    // Shortest path
    vector<int> dist(n, INT_MAX);
    dist[start] = 0;
    vector<int> prev(n, -1);
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    pq.push(make_pair(0, start));
    
    while (!pq.empty()) {
        int u = pq.top().second;
        pq.pop();
        for (int i = 0; i < adjList[u].size(); i++) {
            int v = adjList[u][i].first;
            int w = adjList[u][i].second;
            if (dist[v] > dist[u] + w) {
                dist[v] = dist[u] + w;
                prev[v] = u;
                pq.push(make_pair(dist[v], v));
            }
        }
    }
    
    // Output shortest path
    vector<int> path;
    int curr = dest;
    while (curr != -1) {
        path.push_back(curr);
        curr = prev[curr];
    }
    reverse(path.begin(), path.end());
    cout << "Length of shortest path: " << dist[dest] << endl;
    cout << "Shortest path: ";
    for (int i = 0; i < path.size(); i++) {
        cout << path[i] << " ";
    }
    cout << endl;
}
Conclusion

In conclusion, this problem tests the ability of the programmer to write efficient algorithms for finding the shortest path in a directed acyclic graph. The solution involves using a modified topological sort algorithm to obtain a linear ordering of vertices and then using dynamic programming to update the shortest distances to each vertex.