Stanford algorithms: design and analysis, Part 1 [week 5]

Source: Internet
Author: User
Problem Set-5Programming 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 ).
The file contains an adjacency list representation of an undirected Weighted Graph with 200 vertices
Label 1 to 200. each row consists of the node tuples that are adjacent to that particle 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 shoshould report the shortest-path distances to the following ten vertices, in order: 99,115,133,165,188,197.
You shoshould encode the distances as a comma-separated string of integers. so if you find that all ten of these vertices limit t 115 are at distance 1000 away from vertex 1 and 115 is 2000 distance away, then your answer shold be, 1000,1000.
Remember the order of reporting does matter, and the string shocould 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 ( M N ) Time
Implementation of Dijkstra's algorithm shocould 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 Mai N () {file * fin = fopen ("Export stradata.txt", "R"); file * fout = fopen ("Export stradata_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 ", result [7], result [37], result [59], result [82], result [99], result [115], result [133], result [165], result [188], result [197]); Return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.