Created
November 15, 2018 14:24
-
-
Save dermesser/a519e514e05ae60416ead8109a818325 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <climits> | |
struct inner { | |
std::string bezeichnung_; | |
int value_; | |
}; | |
struct node { | |
inner inner_; | |
std::vector<int> ptrs_; | |
bool visited_ = false; | |
}; | |
std::vector<node> CreateGraph(const std::vector<inner>& in) { | |
std::vector<node> graph{in.size()}; | |
for (unsigned int i = 0; i < in.size(); i++) { | |
node newnode; | |
newnode.inner_ = in[i]; | |
newnode.ptrs_.resize(in.size()-1); | |
for (unsigned int j = 0, k = 0; j < in.size(); j++) { | |
// Erzeuge Verweise auf alle Knoten, außer den aktuellen Knoten (Knoten i). | |
if (j == i) continue; | |
newnode.ptrs_[k] = j; | |
k++; | |
} | |
graph[i] = newnode; | |
} | |
return graph; | |
} | |
void Walk(std::vector<node>& graph, unsigned int current) { | |
if (graph[current].visited_) return; | |
graph[current].visited_ = true; | |
std::cout << graph[current].inner_.bezeichnung_ << " "; | |
int cv = graph[current].inner_.value_; | |
int diff = INT_MAX, next_elem = -1; | |
for (unsigned int i = 0; i < graph.size(); i++) { | |
int current_diff = graph[i].inner_.value_ - cv; | |
if ( current_diff > 0 && current_diff < diff && !graph[i].visited_) { | |
diff = current_diff; | |
next_elem = i; | |
} | |
} | |
if (next_elem < 0) return; | |
// Jetzt: next_elem ist Index des nächstgrößeren Elements. | |
Walk(graph, next_elem); | |
} | |
int main(void) { | |
std::vector<inner> initial{ | |
{",", 42}, | |
{"\n", 137}, | |
{"dieser", -5}, | |
{"Satz", 2}, | |
{"ergibt", 22}, | |
{"Sinn", 17}, | |
{"ist", 61}, | |
{"Lösung", 67}, | |
{"Wenn", -49}, | |
{"die", 65}, | |
{".", 99}, | |
{"korrekt", 81}, | |
}; | |
std::vector<node> graph = CreateGraph(initial); | |
Walk(graph, 8); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment