📜  门| GATE-CS-2016(套装2)|问题 33(1)

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

GATE CS 2016 (SET 2) Problem 33

Introduction

This problem involves the implementation of the Dijkstra's algorithm for finding the shortest path in a weighted graph, as well as the concept of adjacency matrix representation of a graph. The input to the program is a text file containing the adjacency matrix representation of a weighted graph, along with the source vertex. The output is the shortest path from the source vertex to all the other vertices in the graph.

Algorithm

Dijkstra's algorithm is a greedy algorithm that finds the shortest path from a source vertex to all the other vertices in a weighted graph. It starts by assigning a distance value to every vertex, which is initially set to infinity for all vertices except the source vertex, which is set to 0. Then, it repeatedly selects the vertex with the smallest distance value that has not been processed yet, updates the distance values of its adjacent vertices, and marks the selected vertex as processed. This process is repeated until all vertices have been processed.

Implementation

The implementation of the Dijkstra's algorithm involves the use of a priority queue to store the vertices in order of increasing distance value. It also requires the use of an adjacency matrix to represent the graph. The adjacency matrix is a two-dimensional array of size n x n, where n is the number of vertices in the graph, and each cell represents the weight of the edge between two vertices. If there is no edge between two vertices, the cell value is set to infinity.

Here is the Python implementation of the Dijkstra's algorithm:

import heapq

def dijkstra(adj_matrix, src):
    n = len(adj_matrix)
    dist = [float('inf')] * n
    dist[src] = 0
    visited = [False] * n
    heap = [(0, src)]

    while heap:
        (d, u) = heapq.heappop(heap)
        if visited[u]:
            continue
        visited[u] = True
        for v in range(n):
            if adj_matrix[u][v] is not None:
                alt = dist[u] + adj_matrix[u][v]
                if alt < dist[v]:
                    dist[v] = alt
                    heapq.heappush(heap, (dist[v], v))

    return dist

Input Format

The input to the program is a text file containing the adjacency matrix representation of a weighted graph, along with the source vertex. The first line of the file contains an integer n, which is the number of vertices in the graph. The next n lines contain n integers each, representing the adjacency matrix. The last line of the file contains an integer, which is the source vertex.

Example input file:

4
0 1 4 6
1 0 2 None
4 2 0 1
6 None 1 0
0
Output Format

The output is the shortest path from the source vertex to all the other vertices in the graph. The output is written to a text file, where each line contains the shortest path from the source vertex to a vertex, where the first number in the line is the destination vertex, and the second number is its corresponding distance from the source vertex.

Example output file:

0 0
1 1
2 3
3 7
Conclusion

The Dijkstra's algorithm is a useful algorithm for finding the shortest path in a weighted graph. Its implementation involves the use of a priority queue and an adjacency matrix. The input to the program is a text file containing the adjacency matrix representation of a graph, and the output is the shortest path from the source vertex to all the other vertices in the graph.