Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include <vector>
- #include <list>
- #include <queue>
- #include <immintrin.h>
- class Vertex
- {
- public:
- static int numberOfVertices;
- std::list<Vertex*> neighbors;
- int vertexNumber;
- Vertex()
- {
- this->vertexNumber = numberOfVertices;
- numberOfVertices++;
- }
- Vertex(int vertexNumber)
- {
- this->vertexNumber = vertexNumber;
- }
- bool isPresent(int index)
- {
- for (auto neighbor : neighbors)
- if (neighbor->getVertexNumber() == index)
- return true;
- return false;
- }
- void addNeighbors(Vertex* index)
- {
- if (isPresent(index->getVertexNumber()))
- return;
- this->neighbors.push_back(index);
- }
- void printNeighbors()
- {
- for (auto neighbor : neighbors)
- {
- printf("%d ", neighbor->getVertexNumber());
- }
- printf("\n");
- }
- const int getVertexNumber()
- {
- return vertexNumber;
- }
- };
- int Vertex::numberOfVertices = 1;
- class Graph
- {
- std::vector<Vertex*> vertices;
- public:
- Graph(int nodeCount)
- {
- vertices.resize(nodeCount);
- }
- bool hasBeenCreated(int index)
- {
- for (auto vertex : vertices)
- {
- if (vertex != nullptr)
- if (vertex->getVertexNumber() == index)
- return true;
- }
- return false;
- }
- void formConnection(int vertexNumber, int neighbor)
- {
- const int vertexIndex = vertexNumber - 1;
- Vertex* newVertex = new Vertex(neighbor);
- if (hasBeenCreated(vertexNumber))
- {
- vertices[vertexIndex]->addNeighbors(newVertex);
- }
- else
- {
- vertices[vertexIndex] = new Vertex();
- vertices[vertexIndex]->addNeighbors(newVertex);
- }
- }
- void printGraph()
- {
- for (auto vertex : vertices)
- {
- if (vertex != nullptr)
- {
- printf("Vertex %d: ", vertex->getVertexNumber());
- vertex->printNeighbors();
- }
- }
- }
- void BFS(int from, std::vector<bool>& visited)
- {
- std::queue<Vertex* > nextToVisit;
- visited[from-1] = true;
- nextToVisit.push(vertices[from-1]);
- while (!nextToVisit.empty())
- {
- Vertex* nextVertex = nextToVisit.front();
- nextToVisit.pop();
- printf("%d ", nextVertex->getVertexNumber());
- for (auto vertex : vertices[nextVertex->getVertexNumber()-1]->neighbors)
- {
- if (!visited[vertex->getVertexNumber()-1])
- {
- nextToVisit.push(vertex);
- visited[vertex->getVertexNumber()-1] = true;
- }
- }
- }
- printf("\n");
- }
- void DFS(int from, std::vector<bool>& visited)
- {
- Vertex* startNode = vertices[from - 1];
- visited[from - 1] = true;
- printf("%d,", startNode->getVertexNumber());
- for(auto neighbor: vertices[from-1]->neighbors)
- {
- if(neighbor !=nullptr)
- if (!visited[neighbor->getVertexNumber() - 1])
- DFS(neighbor->getVertexNumber(), visited);
- }
- }
- };
- int main()
- {
- // allocated 4 nodes but with null
- Graph* myGraph = new Graph(4);
- //TODO Make DFS AND TOPOLOGICAL
- myGraph->formConnection(1, 2);
- myGraph->formConnection(1, 3);
- myGraph->formConnection(2, 1);
- myGraph->formConnection(2, 4);
- myGraph->formConnection(3, 1);
- myGraph->formConnection(4, 3);
- std::vector<bool> visited(4,false);
- //myGraph->BFS(4, visited);
- myGraph->DFS(4, visited);
- printf("\n");
- myGraph->printGraph();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement