Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "graph.h"
- #include "node.h"
- //[1p] Creaza un graph gol, fara noduri, fara muchii
- Graph::Graph()
- {
- }
- //[1p] adauga un nod in graf
- void Graph::AddNode(int x, int y, const int& value){
- Node n1;
- n1.x = x;
- n1.y = y;
- n1.Value = value;
- this->n.push_back(n1);
- }
- //[5p] adaugat o muchie in graf. Returneaza true daca indecsii sunt valizi, diferiti intre ei si muchia nu exista deja, false altfel
- bool Graph::AddEdge(int firstNodeIndex, int secondNodeIndex){
- if (firstNodeIndex != secondNodeIndex)
- return false;
- else
- {
- for (vector<Edge>::iterator it = this->e.begin(); it != e.end()-1; ++it)
- for (vector<Edge>::iterator it2 = this->e.begin()+1; it != e.end() ; ++it)
- if (it.operator==(it2))
- return false;
- else
- {
- Edge e1;
- e1.IndexNode1 = firstNodeIndex;
- e1.IndexNode2 = secondNodeIndex;
- this->e.push_back(e1);
- return true;
- }
- }
- }
- int Graph::GetNodesCount(){
- return n.size();
- }
- //[1p] returneaza numarul de muchii
- int Graph::GetEdgesCount(){
- return e.size();
- }
- //[1p] returneaza nodul corespunzator pentru un index, sau NULL daca indexul nu e valid
- Node* Graph::GetNode(int index){
- if (index < n.size)
- return &n.at(index);
- return NULL;
- }
- //[3p] sterge o muchie
- bool Graph::DeleteEdge(int index){
- if (index < e.size())
- {
- e.erase(e.begin()+index);
- return true;
- }
- return false;
- }
- //[9p] returneaza true daca graful este conex, false altfel
- bool Graph::IsConex()
- {
- for (int i = 0; i < n.size(); i++)
- n.at(i).colour = -1;
- Node aux = n.at(0);
- aux.colour = 0;
- for (int j = 0; j<e.size();j++)
- for (int i = 0; i < n.size(); i++)
- {
- if (e.at(j).IndexNode1 == n.at(i).x || e.at(j).IndexNode2 == n.at(i).x)
- if (n.at(i).colour == -1;)
- n.at(i).colour = 0;
- }
- for (int i = 0; i < n.size(); i++)
- {
- if (n.at(i).colour == -1)
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement