Changed graph struct, partially added BFS
This commit is contained in:
parent
e4016b54e6
commit
ce424df021
@ -3,6 +3,7 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <queue>
|
||||||
|
|
||||||
Graph ReadInGraph(std::string fileLocation) {
|
Graph ReadInGraph(std::string fileLocation) {
|
||||||
std::cout << fileLocation << std::endl;
|
std::cout << fileLocation << std::endl;
|
||||||
@ -13,7 +14,7 @@ Graph ReadInGraph(std::string fileLocation) {
|
|||||||
}
|
}
|
||||||
Graph newGraph;
|
Graph newGraph;
|
||||||
graphFile >> newGraph.nodeCount;
|
graphFile >> newGraph.nodeCount;
|
||||||
newGraph.nodes.resize(newGraph.nodeCount + 1);
|
newGraph.vertices.resize(newGraph.nodeCount + 1);
|
||||||
std::string line;
|
std::string line;
|
||||||
int source, destination, weight;
|
int source, destination, weight;
|
||||||
while (std::getline(graphFile, line)) {
|
while (std::getline(graphFile, line)) {
|
||||||
@ -21,32 +22,87 @@ Graph ReadInGraph(std::string fileLocation) {
|
|||||||
iss >> source;
|
iss >> source;
|
||||||
do {
|
do {
|
||||||
iss >> destination >> weight;
|
iss >> destination >> weight;
|
||||||
newGraph.AddChild(source, destination, weight);
|
newGraph.vertices.at(source).nodeNumber = source;
|
||||||
|
newGraph.AddEdge(source, destination, weight);
|
||||||
} while (!iss.eof());
|
} while (!iss.eof());
|
||||||
}
|
}
|
||||||
graphFile.close();
|
graphFile.close();
|
||||||
return newGraph;
|
return newGraph;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Old Node
|
||||||
Node::Node(int destination, int weight) {
|
Node::Node(int destination, int weight) {
|
||||||
this->destination = destination;
|
this->destination = destination;
|
||||||
this->weight = weight;
|
this->weight = weight;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
Graph::Graph(void) {
|
Graph::Graph(void) {
|
||||||
nodeCount = -1;
|
nodeCount = -1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Graph::AddChild(int source, int destination, int weight) {
|
Edge::Edge(Vertex* destination, int weight) {
|
||||||
nodes.at(source).push_back(Node(destination, 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) {
|
void Graph::PrintGraph(void) {
|
||||||
for (int i = 1; i < nodes.size(); i++) {
|
for (auto i : vertices) {
|
||||||
std::cout << "Source " << i << " ";
|
std::cout << "Source " << i.nodeNumber << " has ";
|
||||||
for (auto j : nodes.at(i)) { std::cout << "-> " << j.destination; }
|
for (auto j : i.edges) { std::cout << " " << j.destination->nodeNumber; }
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
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 <string>
|
||||||
#include <vector>
|
#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 {
|
struct Node {
|
||||||
int destination;
|
int destination;
|
||||||
int weight;
|
int weight;
|
||||||
@ -14,10 +38,16 @@ struct Graph {
|
|||||||
int nodeCount;
|
int nodeCount;
|
||||||
std::vector<std::vector<Node>> nodes;
|
std::vector<std::vector<Node>> nodes;
|
||||||
Graph(void);
|
Graph(void);
|
||||||
void AddChild(int source, int destination, int weight);
|
void AddEdge(int source, int destination, int weight);
|
||||||
void PrintGraph(void);
|
void PrintGraph(void);
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
Graph ReadInGraph(std::string fileLocation);
|
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
|
#endif
|
||||||
|
@ -8,5 +8,8 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
Graph newGraph = ReadInGraph(argv[1]);
|
Graph newGraph = ReadInGraph(argv[1]);
|
||||||
newGraph.PrintGraph();
|
newGraph.PrintGraph();
|
||||||
|
for (int i = 1; i < newGraph.nodeCount; i++) {
|
||||||
|
PrintSolution(BFS(newGraph, 1, i));
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user