(基于堆排序的Dijkstra算法 C++实现) Coursera 斯坦福大学 算法课程 Programming Assignment #6

本文介绍了如何使用C++实现Dijkstra最短路径算法,结合Coursera斯坦福大学算法课程的编程作业进行讲解。文章详细讨论了在处理邻接列表表示的加权无向图时遇到的问题,特别是深浅拷贝的注意事项,以及STL堆的使用技巧。读者将了解到如何从头开始构建Dijkstra算法,并解决在实际编程中可能遇到的挑战。
摘要由CSDN通过智能技术生成

Graph Search, Shortest Paths, and Data Structures

题目

第 1 个问题

In this programming problem you’ll code up Dijkstra’s shortest-path algorithm.

Download the following text file:
dijkstraData.txt

The file contains an adjacency list representation of an undirected weighted graph with 200 vertices labeled 1 to 200. Each row consists of the node tuples that are adjacent to that particular vertex along with the length of that edge. For example, the 6th row has 6 as the first entry indicating that this row corresponds to the vertex labeled 6. The next entry of this row “141,8200” indicates that there is an edge between vertex 6 and vertex 141 that has length 8200. The rest of the pairs of this row indicate the other vertices adjacent to vertex 6 and the lengths of the corresponding edges.

Your task is to run Dijkstra’s shortest-path algorithm on this graph, using 1 (the first vertex) as the source vertex, and to compute the shortest-path distances between 1 and every other vertex of the graph. If there is no path between a vertex vvv and vertex 1, we’ll define the shortest-path distance between 1 and vvv to be 1000000.

You should report the shortest-path distances to the following ten vertices, in order: 7,37,59,82,99,115,133,165,188,197. You should encode the distances as a comma-separated string of integers. So if you find that all ten of these vertices except 115 are at distance 1000 away from vertex 1 and 115 is 2000 distance away, then your answer should be 1000,1000,1000,1000,1000,2000,1000,1000,1000,1000. Remember the order of reporting DOES MATTER, and the string should be in the same order in which the above ten vertices are given. The string should not contain any spaces. Please type your answer in the space provided.

IMPLEMENTATION NOTES: This graph is small enough that the straightforward O(mn)O(mn)O(mn) time implementation of Dijkstra’s algorithm should work fine. OPTIONAL: For those of you seeking an additional challenge, try implementing the heap-based version. Note this requires a heap that supports deletions, and you’ll probably need to maintain some kind of mapping between vertices and their positions in the heap.

程序

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <regex>

#include <algorithm>

using namespace std;



class vertex {
   
private:
public:
    bool visited = false;
    int A = 0;
    //bool operator < (const vertex rhs) {
   
    //    return key < rhs.key;
    //}
    //bool operator > (const vertex rhs) {
   
    //    return key > rhs.key;
    //}
    int key = INT32_MAX;
    struct edge {
   
        vertex* neighbor;
        int distance;
        edge(vertex* neighbor_, int distance_) {
   
            neighbor = neighbor_;
            distance = distance_;
        }
    };
    int vertexName = -1;
    vector<edge> neighbors;
    void printVertex() {
   
        cout << "Vertex " << vertexName << ": ";
        for (auto& nb : neighbors) {
   
            cout << nb.neighbor->vertexName << "," << nb.distance << " ";
        }
    }
};


bool cmp(const vertex* a, const vertex* b)
{
   
    return a->key < b->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值