原文地址: Dijkstra算法的實現
實現Dijkstra算法。node
In this assignment, you'll actually implement Dijkstra's. But first, you'll play around with bits in C.c++
The data files for this assignment are in the CS 5007 resources repo. Check it out (clone/pull into it's own folder/local repo), and copy the a5 folder over to your own personal repo.git
When starting to work with bit manipulation, it's helpful to have a way to print out the bits of a value.算法
Side note: unsigned means the bits represent an positive integer between 0 and 2n, where n is the number of bits. Typically, a 16-bit integer is stored in 2's-complement form, which gives it a range that is positive and negative, centered around 0.bash
Make sure the following code makes sense to you.app
void displayBits(unsigned value) { unsigned c; // Take 1, shift it all the way to the fileft. unsigned displayMask = 1 << 15; printf("%3c%7u = ", value, value); for (c=1; c<=16; c++) { // putchar prints a character on the screen // 'value & displayMask' gets the relevant bit putchar(value & displayMask ? '1' : '0'); // Shift value over one value <<= 1; // Put a space in for easy reading if (c % 8 == 0) { putchar(' '); } } putchar('\n'); }
To print out the bit values, we're defining a bit mask (displayMask) that determines if the bit in the first spot on the fileft is 1 or not. When it's AND'd with the original value, the output will be 1 if the fileft most bit is 1, or 0 otherwise.less
After a bit is printed, the value is bit-shifted fileft, meaning all bits are shifted over one, and the right-most bit is filled in with a 0.ide
The table below shows the mask that never changes throughout the call to the function, and how the value changes throughout the loop, when the original value is 'B' (or 173) (as shown in the first line).oop
Implement the following functionsui
The fileft shift operator (<<
) can be used to pack two character values into a 2-byte unsigned integer variable. Write a function packCharacters that takes two characters as parameters, and returns a 16 bit integer that is holding the packed characters.
A Hint: To pack two characters into an unsigned integer variable, assign the first character to the to the unsigned variable, shift the unsigned variable fileft by 8 bit positions, and combine the unsigned variable with the second character using the bitwise inclusive OR operator. The function should print the characters in their bit format before and after they are packed into the unsigned integer to prove that the characters are in fact packed correctly in the unsigned variable.
Example: Printing the output of packCharacters('B', 'i') should result in:
01000010 01101001
Using the right shift operator, the bitwise AND (&&
) operator, and a mask, write function unpackCharacters that takes the unsigned integer from above and unpacks it into two characters. This is the reverse operation of the packCharacters function above. As a note: 65280 = 11111111 00000000. This might be helpful as a mask for this function. Print the input integer and output characters.
Left shifting an unsigned integer by 1 bit is equivalent to multiplying the value by 2. Write function power^2 that takes two integer arguments, number and pow and calculates (number * 2^pow). Use the shift operator to calculate the result. The program should print the values as integers and as bits.
In this part of the assignment, you'll implement Dijkstra's algorithm. There are two parts to this: first, you need to read in the graph. Then, you need to design your own data structures and implement Dijkstra's to answer some questions.
We're providing you with three data files to use:
Please note: Dijkstra's *is* a common algorithm, and a classic intro algorithm to implement. You can surely find implementations and information online. However, we're assigning this because implementing it is valuable to *YOU*, particularly when you give it a good shot on your own before relying on looking at other implementations.
In that vein, we encourage you to focus on trying to do this part all your own. If you do refer to resources online, please note that in your source file (in the notes at the top).
Now, you haven't read in files yet. Here's a snippet of code that reads a file in character by character:
#include void readFile() { FILE *cfPtr; if ((cfPtr = fopen("sample.txt", "r")) == NULL) { printf("File could not be opened\n"); } else { char c; while (!feof(cfPtr)) { c = fgetc(cfPtr); printf("%c\n", c); } } } int main() { readFile(); return 0; }
Other functions that might be helpful:
You'll be able to answer questions about the distance between two cities by using the miles.csv file. It contains the distance between two cities as a matrix: read in this file as a matrix (two-dimensional array), and implement Dijkstra's using this matrix.
No example of the file because it's very large :)
A value of -1 means there is no edge between those cities.
Answer the questions about time using the nodes.csv and edges.csv files. Implement your graph as an adjacency list, and implement Dijkstra's using this kind of graph representation.
The nodes represent locations. The file looks something like:
0001,Aberdeen 0002,Abilene 0003,Akron 0004,Albany 0005,Albuquerque 0006,Alexandria
The edges represent the time between cities. It looks something like this:
Abilene_TX Akron_OH 12 52 59 Abilene_TX Albany_GA 9 25 59 Abilene_TX Albany_OR 25 21 43 Abilene_TX Alexandria_VA 15 34 14 Abilene_TX Alexandria_LA 2 52 50 Abilene_TX Alhambra_AZ 5 19 53 Abilene_TX Alhambra_CA 11 17 29 Abilene_TX Alief_TX 2 07 42 Abilene_TX Aliso_Viejo_CA 11 18 08 Abilene_TX Allentown_PA 17 16 12
The columns are: source city, dest city, hours, mins, seconds to get from source city to dest city.
Seattle to Boston: 3,742.2 miles Seattle -> Minneapolis -> Ann Arbor -> Boston (this is not the path; it's just representative)
Please be sure to follow the Google C++ Style Guide for your C code.
Since you used clint for the last assignment, it is still available for you to use this assignment. If you followed the directions last time, you DO NOT need to copy clint.py to your a5 directory this time. Run clint as such:
bash% clint.py a5.c
No style-checking tool is perfect, but you should try to clean up any problems that clint detects unless clint ags something that is definitely not a problem in this particular context (but be sure you have very good reasons to ignore any warnings).
In your class git repo, we want to see a directory called "a5". In that folder, we want to see:
We do NOT want to see:
Make sure your repo is tagged! It must be tagged a5-final, and pushed before the assignment deadline.
Makefile: For this assignment, create your own Makefile.
Please make notes about other resources you have used for this assignment.
It might be helpful to have a README.md that describes any resources you've used and why. It could be a good place to show the output of your code as well (we will run your code, but it's usually good to have a note about how to run it and what to expect as the output. Think of this as a practice of writing up what you did, why, and what you found out).
(本文出自csprojectedu.com,轉載請註明出處)