Stanford Algorithms: Design and Analysis, Part 1[week 5]

Problem Set-5

Stanford Algorithms: Design and Analysis, Part 1[week 5]_第1张图片 Stanford Algorithms: Design and Analysis, Part 1[week 5]_第2张图片 Stanford Algorithms: Design and Analysis, Part 1[week 5]_第3张图片 Stanford Algorithms: Design and Analysis, Part 1[week 5]_第4张图片

Programming Question-5

Question 1

In this programming problem you'll code up Dijkstra's shortest-path algorithm. 
Download the text file here. (Right click and save link as). 
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  v  and vertex 1, we'll define the shortest-path distance between 1 and  v  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. Please type your answer in the space provided.

IMPLEMENTATION NOTES: This graph is small enough that the straightforward  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 <stdio.h>
#include <vector>
#include <queue>
using namespace std;

#define UNEXPANDED 99999999
#define MAX_NODE_ID 200

struct ID_DIS
{
        int id,distance;
        bool operator < (const ID_DIS & other) const
        {
                return distance > other.distance;
        }
        ID_DIS(int p_id,int p_dis):
        id(p_id),distance(p_dis){};
};

vector<ID_DIS> adj[MAX_NODE_ID + 1];
int result[MAX_NODE_ID + 1];
priority_queue<ID_DIS> distance_heap;

int main () {
        FILE *fin  = fopen ("dijkstraData.txt", "r");
        FILE *fout = fopen ("dijkstraData_out2.txt", "w");

        int in_node,in_distance,lead_node;
        char in_char;
        while(fscanf(fin,"%d",&in_node) > 0)
        {
                fscanf(fin,"%c",&in_char);
                if(in_char != ',')
                        lead_node = in_node;
                else
                {
                        fscanf(fin,"%d",&in_distance);
                        adj[lead_node].push_back(ID_DIS(in_node,in_distance));
                }
        }

        fill(result,result + MAX_NODE_ID,UNEXPANDED);
        distance_heap.push(ID_DIS(1,0));
        while(!distance_heap.empty())
        {
                ID_DIS expanding = distance_heap.top();
                distance_heap.pop();
                if(result[expanding.id] != UNEXPANDED) continue;
                result[expanding.id] = expanding.distance;
                for(int i = 0 ; i < adj[expanding.id].size() ; i ++)
                {
                        distance_heap.push(ID_DIS(adj[expanding.id][i].id,
                                expanding.distance + adj[expanding.id][i].distance));
                }
        }

        fprintf(fout,"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
                result[7],result[37],result[59],result[82],result[99],
                result[115],result[133],result[165],result[188],result[197]);
       return 0;
}



你可能感兴趣的:(排序,算法,图论,dijkstra)