forked from greedythib/CME212-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortest_path.cpp
107 lines (91 loc) · 3.24 KB
/
shortest_path.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* @file shortest_path.cpp
* Test script for using our templated Graph to determine shortest paths.
*
* @brief Reads in two files specified on the command line.
* First file: 3D Points (one per line) defined by three doubles
* Second file: Tetrahedra (one per line) defined by 4 indices into the point
* list
*/
#include <vector>
#include <fstream>
#include "CME212/SFML_Viewer.hpp"
#include "CME212/Util.hpp"
#include "CME212/Color.hpp"
#include "Graph.hpp"
// Define our types
using GraphType = Graph<int>;
using NodeType = typename GraphType::node_type;
using NodeIter = typename GraphType::node_iterator;
/** Find the node with the minimum euclidean distance to a point.
* @param g The graph of nodes to search.
* @param point The point to use as the query.
* @return An iterator to the node of @a g with the minimun Eucliean
* distance to @a point.
* graph.node_end() if graph.num_nodes() == 0.
*
* @post For all i, 0 <= i < graph.num_nodes(),
* norm(point - *result) <= norm(point - g.node(i).position())
*/
NodeIter nearest_node(const GraphType& g, const Point& point)
{
// HW1 #3: YOUR CODE HERE
(void) g, (void) point; // Quiet compiler warning
return g.node_end();
}
/** Update a graph with the shortest path lengths from a root node.
* @param[in,out] g Input graph
* @param[in,out] root Root node to start the search.
* @return The maximum path length found.
*
* @post root.value() == 0
* @post Graph has modified node values indicating the minimum path length
* to the root.
* @post Graph nodes that are unreachable from the root have value() == -1.
*
* This sets all nodes' value() to the length of the shortest path to
* the root node. The root's value() is 0. Nodes unreachable from
* the root have value() -1.
*/
int shortest_path_lengths(GraphType& g, NodeType& root)
{
// HW1 #3: YOUR CODE HERE
(void) g, (void) root; // Quiet compiler warnings
return 0;
}
int main(int argc, char** argv)
{
// Check arguments
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " NODES_FILE TETS_FILE\n";
exit(1);
}
// Construct a Graph
GraphType graph;
std::vector<GraphType::node_type> nodes;
// Create a nodes_file from the first input argument
std::ifstream nodes_file(argv[1]);
// Interpret each line of the nodes_file as a 3D Point and add to the Graph
Point p;
while (CME212::getline_parsed(nodes_file, p))
nodes.push_back(graph.add_node(p));
// Create a tets_file from the second input argument
std::ifstream tets_file(argv[2]);
// Interpret each line of the tets_file as four ints which refer to nodes
std::array<int,4> t;
while (CME212::getline_parsed(tets_file, t))
for (unsigned i = 1; i < t.size(); ++i)
for (unsigned j = 0; j < i; ++j)
graph.add_edge(nodes[t[i]], nodes[t[j]]);
// Print out the stats
std::cout << graph.num_nodes() << " " << graph.num_edges() << std::endl;
// Launch the SFML_Viewer
CME212::SFML_Viewer viewer;
// HW1 #3: YOUR CODE HERE
// Use nearest_node and shortest_path_lengths to set the node values
// Construct a Color functor and view with the SFML_Viewer
// Center the view and enter the event loop for interactivity
viewer.center_view();
viewer.event_loop();
return 0;
}