1、计算机算法分析与设计课程综合实验算法分析与设计课程综合实验Design and Analysis of Algorithms1 Map Routing要求:Mandatory. 实验目的:Implement the classic Dijkstras shortest path algorithm and optimize it for maps. Such algorithms are widely used in geographic information systems (GIS) including MapQuest and GPS-based car navigation syst
2、ems. 实验内容及要求: Maps. For this assignment we will be working with maps, or graphs whose vertices are points in the plane and are connected by edges whose weights are Euclidean distances. Think of the vertices as cities and the edges as roads connected to them. To represent a map in a file, we list the
3、 number of vertices and edges, then list the vertices (index followed by its x and y coordinates), then list the edges (pairs of vertices), and finally the source and sink vertices. For example, Input6 represents the map below:Dijkstras algorithm. Dijkstras algorithm is a classic solution to the sho
4、rtest path problem. It is described in section 24.3 in CLRS. The basic idea is not difficult to understand. We maintain, for every vertex in the graph, the length of the shortest known path from the source to that vertex, and we maintain these lengths in a priority queue. Initially, we put all the v
5、ertices on the queue with an artificially high priority and then assign priority 0.0 to the source. The algorithm proceeds by taking the lowest-priority vertex off the PQ, then checking all the vertices that can be reached from that vertex by one edge to see whether that edge gives a shorter path to
6、 the vertex from the source than the shortest previously-known path. If so, it lowers the priority to reflect this new information. Here is a step-by-step description that shows how Dijkstras algorithm finds the shortest path 0-1-2-5 from 0 to 5 in the example above. process 0 (0.0) lower 3 to 3841.
7、9 lower 1 to 1897.4process 1 (1897.4) lower 4 to 3776.2 lower 2 to 2537.7process 2 (2537.7) lower 5 to 6274.0process 4 (3776.2)process 3 (3841.9)process 5 (6274.0)This method computes the length of the shortest path. To keep track of the path, we also maintain for each vertex, its predecessor on the
8、 shortest path from the source to that vertex. The files EuclideanGraph.java, Point.java, IndexPQ.java, IntIterator.java, and Dijkstra.java provide a bare bones implementation of Dijkstras algorithm for maps, and you should use this as a starting point. The client program ShortestPath.java solves a
9、single shortest path problem and plots the results using turtle graphics. The client program Paths.java solves many shortest path problems and prints the shortest paths to standard output. The client program Distances.java solves many shortest path problems and prints only the distances to standard
10、output. Your goal. Optimize Dijkstras algorithm so that it can process thousands of shortest path queries for a given map. Once you read in (and optionally preprocess) the map, your program should solve shortest path problems in sublinear time. One method would be to precompute the shortest path for
11、 all pairs of vertices; however you cannot afford the quadratic space required to store all of this information. Your goal is to reduce the amount of work involved per shortest path computation, without using excessive space. We suggest a number of potential ideas below which you may choose to imple
12、ment. Or you can develop and implement your own ideas. Idea 1. The naive implementation of Dijkstras algorithm examines all V vertices in the graph. An obvious strategy to reduce the number of vertices examined is to stop the search as soon as you discover the shortest path to the destination. With
13、this approach, you can make the running time per shortest path query proportional to E log V where E and V are the number of edges and vertices examined by Dijkstras algorithm. However, this requires some care because just re-initializing all of the distances to would take time proportional to V. Si
14、nce you are doing repeated queries, you can speed things up dramatically by only re-initializing those values that changed in the previous query. Idea 2. You can cut down on the search time further by exploiting the Euclidean geometry of the problem, as described in section 21.5 in the book of Algor
15、ithm in C Part V. For general graphs, Dijkstras relaxes edge v-w by updating dw to the sum of dv plus the distance from v to w. For maps, we instead update dw to be the sum of dv plus the distance from v to w plus the Euclidean distance from w to d minus the Euclidean distance from v to d. This is k
16、nown as the A* algorithm. This heuristics affects performance, but not correctness. Idea 3. Use a faster priority queue. There is some room for optimization in the supplied priority queue. You could also consider using a multiway heap as in Sedgewick Program 20.10. Testing. The file usa.txt contains
17、 87,575 intersections and 121,961 roads in the continental United States. The graph is very sparse - the average degree is 2.8. Your main goal should be to answer shortest path queries quickly for pairs of vertices on this network. Your algorithm will likely perform differently depending on whether
18、the two vertices are nearby or far apart. We provide input files that test both cases. You may assume that all of the x and y coordinates are integers between 0 and 10,000.实验类型:Verification.适用对象:Undergraduate for Computer School2 Document Distance Problem要求:Mandatory. 实验目的:Design and implement the d
19、ocument distance problem and optimize it for data. 实验内容及要求: Let D be a text document (e.g. the complete works of William Shakespeare). A word is a consecutive sequence of alphanumeric characters, such as Hamlet or 2007. Well treat all upper-case letters as if they are lower-case, so that Hamlet and
20、hamlet are the same word. Words end at a non-alphanumeric character, so cant contains two words: can and t. The word frequency distribution of a document D is a mapping from words w to their frequency count, which well denote as D(w). We can view the frequency distribution D as vector, with one comp
21、onent per possible word. Each component will be a non-negative integer (possibly zero). The norm of this vector is defined in the usual way:. The inner-product between two vectors D and D is defined as usual. . Finally, the angle between two vectors D and D is defined: This angle (in radians) will b
22、e a number between 0 and since the vectors are non-negative in each component. Clearly, angle(D,D) = 0.0 for all vectors D, and angle(D,D) = / 2 if D and D have no words in common. Example: The angle between the documents To be or not to be and Doubt truth to be a liar is We define the distance betw
23、een two documents to be the angle between their word frequency vectors. The document distance problem is thus the problem of computing the distance between two given text documents. An instance of the document distance problem is the pair of input text documents. 3 Edit Distance要求:Mandatory.实验目的: Ma
24、ny word processors and keyword search engines have a spelling correction feature. If you type in a misspelled word x, the word processor or search engine can suggest a correction y. The correction y should be a word that is close to x. One way to measure the similarity in spelling between two text s
25、trings is by “edit distance”. The notion of edit distance is useful in other fields as well. For example, biologists use edit distance to characterize the similarity of DNA or protein sequences. 实验内容及要求:The edit distance d(x, y) of two strings of text, x1.m and y1.n, is defined to be the minimum pos
26、sible cost of a sequence of “transformation operations”(defined below) that transforms string x1.m into string y1.n. To define the effect of the transformation operations, we use an auxiliary string z1.s that holds the intermediate results. At the beginning of the transformation sequence s = m and z
27、1.s = x1.m (i.e., we start with string x1.m). At the end of the transformation sequence, we should have s = n and z1.s = y1.n(i.e., our goal is to transform into string y1.n). Throughout the transformation, we maintain the current length s of string z, as well as a cursor position i, i.e., an index
28、into string z. The invariant 1 i s +1 holds at all times during the transformation. (Notice that the cursor can move one space beyond the end of the string z in order to allow insertion at the end of the string.)Each transformation operation may alter the string z, the size s, and the cursor positio
29、n i. Each transformation operation also has an associated cost. The cost of a sequence of transformation operations is the sum of the costs of the individual operations on the sequence. The goal of the edit-distance problem is to find a sequence of transformation operation of minimum cost that trans
30、forms x1.m into y1.n.There are five transformation operations:Operation Cost Effectleft 0 If i = 1 then do nothing. Otherwise, set i i-1right 0 If i = s +1 then do nothing. Otherwise, set i i-1.replace 4 If i = s +1 then do nothing. Otherwise, replace the character under the cursor by another charac
31、ter c by setting zi c, and then incrementing i.delete 2 If i = s +1 then do nothing. Otherwise, delete the character c under the cursor by setting zi.s zi+1.s+1 and decrementing s. The cursor position i does not change. insert 3 Insert the character c into string z by incrementing s, setting zi +1.s
32、 zi.s-1, setting zi c, and then incrementing index i.As an example, one way to transform the source string algorithm to the target string analysis is to use the sequence of operations shown in Table 1, where the position of the underlined character represents the cursor position i. Many other sequences of transformation operations also transform algorithm to analysis the solution in Table 1 is not unique and some other solutions
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1