2023-01-19 15:36:33 +00:00
|
|
|
#include "Graph.h"
|
|
|
|
|
2023-03-29 16:19:16 +00:00
|
|
|
Graph::Graph(int n, list< pair<int, int> > & edges):
|
2023-01-19 15:36:33 +00:00
|
|
|
n(n),
|
|
|
|
m(edges.size()),
|
2023-02-05 00:43:57 +00:00
|
|
|
adjMat(n * n),
|
2023-01-19 15:36:33 +00:00
|
|
|
adjList(n),
|
|
|
|
edges(),
|
2023-03-29 16:19:16 +00:00
|
|
|
edgeIndex()
|
2023-01-19 15:36:33 +00:00
|
|
|
{
|
|
|
|
for(list< pair<int, int> >::const_iterator it = edges.begin(); it != edges.end(); it++)
|
|
|
|
{
|
|
|
|
int u = (*it).first;
|
|
|
|
int v = (*it).second;
|
2023-09-09 10:22:14 +00:00
|
|
|
|
|
|
|
if (v < n && u < n) {
|
|
|
|
AddEdge(u, v);
|
|
|
|
}
|
2023-01-19 15:36:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 16:19:16 +00:00
|
|
|
pair<int, int> Graph::GetEdge(int e)
|
2023-01-19 15:36:33 +00:00
|
|
|
{
|
|
|
|
return edges[e];
|
|
|
|
}
|
|
|
|
|
2023-03-29 16:19:16 +00:00
|
|
|
int Graph::GetEdgeIndex(int u, int v) {
|
2023-03-29 17:02:23 +00:00
|
|
|
return edgeIndex[u * n + v];
|
2023-01-19 15:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Graph::AddEdge(int u, int v)
|
|
|
|
{
|
2023-02-05 00:43:57 +00:00
|
|
|
if(adjMat[u * n + v]) return;
|
2023-01-19 15:36:33 +00:00
|
|
|
|
2023-02-05 00:43:57 +00:00
|
|
|
adjMat[u * n + v] = adjMat[v * n + u] = true;
|
2023-01-19 15:36:33 +00:00
|
|
|
adjList[u].push_back(v);
|
|
|
|
adjList[v].push_back(u);
|
|
|
|
|
|
|
|
edges.push_back(pair<int, int>(u, v));
|
2023-03-29 17:02:23 +00:00
|
|
|
edgeIndex[u * n + v] = edgeIndex[v * n + u] = m++;
|
2023-01-19 15:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 16:19:16 +00:00
|
|
|
const vector<int>& Graph::AdjList(int v)
|
2023-01-19 15:36:33 +00:00
|
|
|
{
|
|
|
|
if(v > n)
|
|
|
|
throw "Error: vertex does not exist";
|
|
|
|
|
|
|
|
return adjList[v];
|
|
|
|
}
|
|
|
|
|
2023-03-29 16:19:16 +00:00
|
|
|
const vector<bool> & Graph::AdjMat()
|
2023-01-19 15:36:33 +00:00
|
|
|
{
|
|
|
|
return adjMat;
|
|
|
|
}
|
|
|
|
|