#include <iostream>
using namespace std;
typedef unsigned int uint;
class Graph {
public:
Graph(uint NoOfNodes);
void addEdge(uint Node1, uint Node2);
bool hasEdge(uint Node1, uint Node2);
uint noOfEdges();
uint noOfNodes();
void printGraph();
//int maxGrad();
//int minGrad();
//bool reachable(uint Node1, uint Node2);
~Graph();
private:
uint mNoOfNodes;
uint **AdjacencyMatrix;
};
Graph :: Graph (uint NoOfNodes ) {
mNoOfNodes = NoOfNodes ;
AdjacencyMatrix = new uint *[ NoOfNodes ];
for ( uint i =0; i < NoOfNodes ; i ++)
AdjacencyMatrix [ i ] = new uint [ NoOfNodes ];
}
Graph::~Graph() {
if (mNoOfNodes > 0) delete[] AdjacencyMatrix;
}
void Graph::addEdge(uint Node1, uint Node2)
{
AdjacencyMatrix[Node1][Node2]=1;
AdjacencyMatrix[Node2][Node1]=1;
}
bool Graph::hasEdge(uint Node1, uint Node2) {
if (mNoOfNodes < 1) return false;
return AdjacencyMatrix[Node1][Node2]==1;
}
uint Graph::noOfEdges() {
uint cnt = 0;
for (uint i = 0; i < mNoOfNodes; i++){
for(uint e=0; e<mNoOfNodes; e++){
if(AdjacencyMatrix[i][e]==1) cnt++;
}
}
return cnt / 2;
}
uint Graph::noOfNodes() {
return mNoOfNodes;
}
/*int Graph::maxGrad() {
}
int Graph::minGrad(){
}
bool Graph::reachable(uint Node1, uint Node2){
}*/
void Graph::printGraph(){
if (noOfNodes() == 0) return;
cout<<"Anzahl der Knoten: "<<noOfNodes()<<endl;
cout<<"Anzahl der Kanten: "<<noOfNodes()<<endl;
//cout<<"Maxgrad: "<<maxGrad()<<endl;
//cout<<"Mingrad: "<<minGrad()<<endl;
for(uint z=0; AdjacencyMatrix[z]!=0; z++){
cout<<"Knoten "<<z+1<<" ist verbunden mit den/dem Knoten: ";
for(uint e=0; AdjacencyMatrix[e]!=0; e++){
if(AdjacencyMatrix[z][e]==1) cout<<e+1<<" ";
}
cout<<endl;
}
cout<<endl;
cout<<endl;
}
int main() {
//Statt a, b, c... wird 1, 2, 3... verwendet
Graph b(8);
cout << "Beispiel b):" << endl;
uint n = b.noOfNodes();
for (uint i = 0; i < n; i++)
b.addEdge(i, (i+1) % n);
if (b.hasEdge(3,4))
cout << "Kante (3,4) existiert!" << endl;
else cout << "Kante (3,4) existiert nicht!" << endl;
//b.maxGrad();
//b.minGrad();
b.printGraph();
Graph a(7);
cout << "Beispiel a):" << endl;
uint f = a.noOfNodes();
a.addEdge(0, 2);
a.addEdge(2, 1);
a.addEdge(1, 6);
a.addEdge(2, 4);
a.addEdge(6, 5);
a.addEdge(5, 4);
a.addEdge(4, 3);
a.addEdge(5, 3);
if (a.hasEdge(0,5))
cout << "Kante (0,5) existiert!" << endl;
else cout << "Kante (0,5) existiert nicht!" << endl;
//a.maxGrad();
//a.minGrad();
a.printGraph();
return 0;
}