Changed graph struct, partially added BFS
This commit is contained in:
parent
e4016b54e6
commit
ce424df021
@ -3,6 +3,7 @@
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <queue>
|
||||
|
||||
Graph ReadInGraph(std::string fileLocation) {
|
||||
std::cout << fileLocation << std::endl;
|
||||
@ -13,7 +14,7 @@ Graph ReadInGraph(std::string fileLocation) {
|
||||
}
|
||||
Graph newGraph;
|
||||
graphFile >> newGraph.nodeCount;
|
||||
newGraph.nodes.resize(newGraph.nodeCount + 1);
|
||||
newGraph.vertices.resize(newGraph.nodeCount + 1);
|
||||
std::string line;
|
||||
int source, destination, weight;
|
||||
while (std::getline(graphFile, line)) {
|
||||
@ -21,32 +22,87 @@ Graph ReadInGraph(std::string fileLocation) {
|
||||
iss >> source;
|
||||
do {
|
||||
iss >> destination >> weight;
|
||||
newGraph.AddChild(source, destination, weight);
|
||||
newGraph.vertices.at(source).nodeNumber = source;
|
||||
newGraph.AddEdge(source, destination, weight);
|
||||
} while (!iss.eof());
|
||||
}
|
||||
graphFile.close();
|
||||
return newGraph;
|
||||
}
|
||||
|
||||
/* Old Node
|
||||
Node::Node(int destination, int weight) {
|
||||
this->destination = destination;
|
||||
this->weight = weight;
|
||||
}
|
||||
*/
|
||||
|
||||
Graph::Graph(void) {
|
||||
nodeCount = -1;
|
||||
|
||||
}
|
||||
|
||||
void Graph::AddChild(int source, int destination, int weight) {
|
||||
nodes.at(source).push_back(Node(destination, weight));
|
||||
Edge::Edge(Vertex* destination, int weight) {
|
||||
this->destination = destination;
|
||||
this->weight = weight;
|
||||
}
|
||||
|
||||
void Graph::AddEdge(int source, int destination, int weight) {
|
||||
vertices.at(source).edges.emplace_back(&(vertices.at(destination)), weight);
|
||||
}
|
||||
|
||||
void Graph::PrintGraph(void) {
|
||||
for (int i = 1; i < nodes.size(); i++) {
|
||||
std::cout << "Source " << i << " ";
|
||||
for (auto j : nodes.at(i)) { std::cout << "-> " << j.destination; }
|
||||
for (auto i : vertices) {
|
||||
std::cout << "Source " << i.nodeNumber << " has ";
|
||||
for (auto j : i.edges) { std::cout << " " << j.destination->nodeNumber; }
|
||||
std::cout << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
void PrintSolution(std::vector<Vertex*> solution) {
|
||||
std::cout << "Path: ";
|
||||
for (auto i : solution) { std::cout << i->nodeNumber << ' '; }
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
std::vector<Vertex*> UCBFS(Graph graph, int start, int end) {
|
||||
std::vector<Vertex*> solution;
|
||||
|
||||
|
||||
|
||||
return solution;
|
||||
}
|
||||
|
||||
std::vector<Vertex*> BFS(Graph graph, int start, int end) {
|
||||
// Setup
|
||||
std::vector<Vertex*> solution;
|
||||
std::queue<int> nodeQueue;
|
||||
nodeQueue.push(start);
|
||||
|
||||
// Search
|
||||
while (!nodeQueue.empty()) {
|
||||
int currentNode = nodeQueue.front();
|
||||
solution.push_back(&graph.vertices.at(currentNode));
|
||||
nodeQueue.pop();
|
||||
for (auto i : graph.vertices.at(currentNode).edges) {
|
||||
nodeQueue.push(i.destination->nodeNumber);
|
||||
}
|
||||
if (currentNode == end) { return solution; } // Solution found
|
||||
if (solution.size() > graph.nodeCount) {
|
||||
std::cout << "Path too long" << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
solution.clear();
|
||||
solution.push_back(&graph.vertices.at(0));
|
||||
return solution;
|
||||
}
|
||||
|
||||
std::vector<Vertex*> DFS(Graph graph, int start, int end) {
|
||||
std::vector<Vertex*> solution;
|
||||
|
||||
|
||||
|
||||
return solution;
|
||||
}
|
||||
|
@ -4,6 +4,30 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
typedef struct Edge Edge;
|
||||
typedef struct Vertex Vertex;
|
||||
|
||||
struct Edge {
|
||||
Vertex* destination;
|
||||
int weight;
|
||||
Edge(Vertex* destination, int weight);
|
||||
};
|
||||
|
||||
struct Vertex {
|
||||
int nodeNumber;
|
||||
std::vector<Edge> edges;
|
||||
void AddEdge(Vertex* destination, int weight);
|
||||
};
|
||||
|
||||
struct Graph {
|
||||
int nodeCount;
|
||||
std::vector<Vertex> vertices;
|
||||
Graph(void);
|
||||
void AddEdge(int source, int destination, int weight);
|
||||
void PrintGraph(void);
|
||||
};
|
||||
|
||||
/* OLD IMPLEMENTATION
|
||||
struct Node {
|
||||
int destination;
|
||||
int weight;
|
||||
@ -14,10 +38,16 @@ struct Graph {
|
||||
int nodeCount;
|
||||
std::vector<std::vector<Node>> nodes;
|
||||
Graph(void);
|
||||
void AddChild(int source, int destination, int weight);
|
||||
void AddEdge(int source, int destination, int weight);
|
||||
void PrintGraph(void);
|
||||
};
|
||||
*/
|
||||
|
||||
Graph ReadInGraph(std::string fileLocation);
|
||||
void PrintSolution(std::vector<Vertex*> solution);
|
||||
|
||||
std::vector<Vertex*> UCBFS(Graph graph, int start, int end);
|
||||
std::vector<Vertex*> BFS(Graph graph, int start, int end);
|
||||
std::vector<Vertex*> DFS(Graph graph, int start, int end);
|
||||
|
||||
#endif
|
||||
|
@ -8,5 +8,8 @@ int main(int argc, char* argv[]) {
|
||||
}
|
||||
Graph newGraph = ReadInGraph(argv[1]);
|
||||
newGraph.PrintGraph();
|
||||
for (int i = 1; i < newGraph.nodeCount; i++) {
|
||||
PrintSolution(BFS(newGraph, 1, i));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user