#pragma once

#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<unordered_set>
using namespace std;

class directedGraph
{
private:
	class vertex
	{
	public:
		string data;
		vector<vertex*> neighbors;

		//BFS variables
		vertex* breadCrumb;
		bool marked;

		//maybe some variables for the BFS algorithm
		//such as, bool marked, vertex * breadcrumb;

		vertex(string x)
		{
			data = x;
			breadCrumb = nullptr;
			marked = false;
		}
	};

	//Use a balanced binarySearch Tree: the map from STL
	map<string,vertex*> vertexMap;

	void cleanUpVariables()
	{
		for (auto x : vertexMap)
		{
			x.second->breadCrumb = nullptr;
			x.second->marked = false;
		}
	}

	void breadthFirstSearch(vertex* S)
	{
		//step -1:
		queue<vertex*> Q;
		cleanUpVariables();

		//step 0:
		S->marked = true;
		Q.push(S);

		//step 1: BFS loop
		while (!Q.empty())
		{
			//step 1.1: get item from Q
			vertex* x = Q.front();
			Q.pop();

			//step 1.2: visit all x's
			//unvisited/marked neighbors
			for (auto y : x->neighbors)
			{
				if (!y->marked)
				{
					y->marked = true;
					Q.push(y);

					//augmentation
					y->breadCrumb = x;
				}
			}
		}
	}

public:
	//add given vertex item to graph
	void addVertex(string s)
	{
		vertex * babyVertex = new vertex(s);
		vertexMap[s] = babyVertex; //O(log n), inserting (s,babyVertex) in the bst
	}

	//add an edge going from a to b
	void addDirectedEdge(string a, string b)
	{
		vertex* aptr = vertexMap[a]; //O(log n) bst search
		vertex* bptr = vertexMap[b]; //O(log n) bst search

		aptr->neighbors.push_back(bptr);
	}

	void addBasicEdge(string a, string b)
	{
		addDirectedEdge(a, b);
		addDirectedEdge(b, a);
	}

	string shortestPath(string start, string dest)
	{
		//step 1:Get the start and dest vertices, O(log n) 
		vertex* Svert = vertexMap.at(start);
		vertex* Dvert = vertexMap.at(dest);

		//step 2: do a BFS on source Svert
		breadthFirstSearch(Svert);

		//step 3: Trace breadcrumb trail from Dvert back to Svert
		//write down visited vertices, return that path.
		string path;
		vertex* current = Dvert;
		while (current != Svert)
		{
			path = current->data + ", " + path;
			current = current->breadCrumb;
		}
		path = Svert->data + ", " + path;

		return path;
	}

	void testDisplay()
	{
		for (auto x : vertexMap)
		{
			cout << x.first << ": ";
			for (auto y : x.second->neighbors)
				cout << y->data << ", ";
			cout << endl;
		}
	}
};
