mirror of
https://github.com/guilhermewerner/paa
synced 2025-06-16 13:34:18 +00:00
Incidence matrix
This commit is contained in:
269
Source/Graph.cpp
269
Source/Graph.cpp
@ -1,194 +1,125 @@
|
||||
#include "Graph.h"
|
||||
|
||||
void Graph::Insert(int u, int v)
|
||||
void Graph::PrintIncidenceMatrix()
|
||||
{
|
||||
InsertD(u, v);
|
||||
InsertD(v, u);
|
||||
}
|
||||
cout << " ";
|
||||
|
||||
void Graph::InsertD(int u, int v)
|
||||
{
|
||||
adj[u].push_back(v);
|
||||
}
|
||||
|
||||
void Graph::Remove(int u, int v)
|
||||
{
|
||||
RemoveD(u, v);
|
||||
RemoveD(v, u);
|
||||
}
|
||||
|
||||
void Graph::RemoveD(int u, int v)
|
||||
{
|
||||
auto iv = find(adj[u].begin(), adj[u].end(), v);
|
||||
*iv = -1;
|
||||
}
|
||||
|
||||
bool Graph::HasEdge(int u, int v)
|
||||
{
|
||||
auto iv = find(adj[u].begin(), adj[u].end(), v);
|
||||
return (iv != adj[u].end());
|
||||
}
|
||||
|
||||
Graph *Graph::Clone()
|
||||
{
|
||||
Graph *graph = new Graph(length);
|
||||
|
||||
for (auto i = 0; i < length; i++)
|
||||
for (int j = 0; j < edges; j++)
|
||||
{
|
||||
for (auto j = adj[i].begin(); j != adj[i].end(); j++)
|
||||
{
|
||||
graph->InsertD(i, *j);
|
||||
}
|
||||
cout << j << " ";
|
||||
}
|
||||
|
||||
return graph;
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
void Graph::DFS(int u, vector<int> &disc, vector<int> &low, vector<int> &parent, vector<pair<int, int>> &bridge)
|
||||
{
|
||||
static int time = 0;
|
||||
disc[u] = low[u] = time;
|
||||
time += 1;
|
||||
|
||||
for (auto v : adj[u])
|
||||
for (int i = 0; i < nodes; i++)
|
||||
{
|
||||
// vertice não visitado
|
||||
if (disc[v] == -1)
|
||||
{
|
||||
parent[v] = u;
|
||||
DFS(v, disc, low, parent, bridge);
|
||||
low[u] = min(low[u], low[v]);
|
||||
cout << i << " ";
|
||||
|
||||
if (low[v] > disc[u])
|
||||
for (int j = 0; j < edges; j++)
|
||||
{
|
||||
int val = this->matrix[i][j];
|
||||
|
||||
if (val >= 0)
|
||||
{
|
||||
bridge.push_back({u, v});
|
||||
cout << " " << val << " ";
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << val << " ";
|
||||
}
|
||||
}
|
||||
else if (v != parent[u])
|
||||
{
|
||||
low[u] = min(low[u], disc[v]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int Graph::DFSCount(int v, bool visited[])
|
||||
{
|
||||
// marcar vertice como visitado
|
||||
visited[v] = true;
|
||||
int count = 1;
|
||||
|
||||
for (auto i = adj[v].begin(); i != adj[v].end(); ++i)
|
||||
{
|
||||
if (*i != -1 && !visited[*i])
|
||||
{
|
||||
count += DFSCount(*i, visited);
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
void Graph::Print()
|
||||
{
|
||||
for (auto i = 0; i < length; i++)
|
||||
{
|
||||
cout << "[" << i << "]: ";
|
||||
|
||||
for (auto j = adj[i].begin(); j != adj[i].end(); j++)
|
||||
{
|
||||
cout << *j << " ";
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void Graph::TransitiveClosure2()
|
||||
void Graph::AddEdge(int u, int v)
|
||||
{
|
||||
// Populate matrix
|
||||
|
||||
int reach[length][length];
|
||||
|
||||
for (auto i = 0; i < length; i++)
|
||||
{
|
||||
for (auto j = 0; j < length; j++)
|
||||
{
|
||||
reach[i][j] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto i = 0; i < length; i++)
|
||||
{
|
||||
for (auto j = adj[i].begin(); j != adj[i].end(); j++)
|
||||
{
|
||||
reach[i][*j] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Transitive closure
|
||||
|
||||
for (auto k = 0; k < length; k++)
|
||||
{
|
||||
for (auto i = 0; i < length; i++)
|
||||
{
|
||||
for (auto j = 0; j < length; j++)
|
||||
{
|
||||
reach[i][j] = reach[i][j] || (reach[i][k] && reach[k][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Print matrix
|
||||
|
||||
cout << " ";
|
||||
|
||||
for (int j = 0; j < length; j++)
|
||||
{
|
||||
cout << j << " ";
|
||||
}
|
||||
|
||||
cout << "\n";
|
||||
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
cout << i << " ";
|
||||
|
||||
for (int j = 0; j < length; j++)
|
||||
{
|
||||
if (i == j)
|
||||
{
|
||||
cout << "1 ";
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << reach[i][j] << " ";
|
||||
}
|
||||
}
|
||||
|
||||
cout << "\n";
|
||||
}
|
||||
this->matrix[u][this->edges] = 1;
|
||||
this->matrix[v][this->edges] = -1;
|
||||
this->edges++;
|
||||
}
|
||||
|
||||
void Graph::TransitiveReduce()
|
||||
vector<vector<int>> Graph::GetAdjacencyMatrix()
|
||||
{
|
||||
for (auto a = 0; a < length; a++)
|
||||
vector<vector<int>> adjacency(this->nodes, vector<int>(this->nodes, 0));
|
||||
|
||||
// Converte uma matriz de incidência para de adjacência.
|
||||
for (int j = 0; j < this->edges; j++)
|
||||
{
|
||||
for (auto b = 0; b < length; b++)
|
||||
int u = -1, v = -1;
|
||||
|
||||
for (int i = 0; i < this->nodes; i++)
|
||||
{
|
||||
if (HasEdge(a, b))
|
||||
if (this->matrix[i][j] == 1)
|
||||
{
|
||||
for (auto c = 0; c < length; c++)
|
||||
if (u == -1)
|
||||
{
|
||||
if (HasEdge(b, c))
|
||||
{
|
||||
// this->RemoveD();
|
||||
}
|
||||
u = i;
|
||||
}
|
||||
}
|
||||
else if (this->matrix[i][j] == -1)
|
||||
{
|
||||
if (v == -1)
|
||||
{
|
||||
v = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (u != -1 && v != -1)
|
||||
{
|
||||
adjacency[u][v] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return adjacency;
|
||||
}
|
||||
|
||||
vector<vector<int>> Graph::GetIncidenceMatrix()
|
||||
{
|
||||
vector<vector<int>> incidence(this->nodes);
|
||||
|
||||
for (int i = 0; i < this->nodes; i++)
|
||||
{
|
||||
for (int j = 0; j < this->edges; j++)
|
||||
{
|
||||
incidence[i].push_back(this->matrix[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
return incidence;
|
||||
}
|
||||
|
||||
Graph *Graph::ToTransitiveClosure()
|
||||
{
|
||||
auto closure = this->GetAdjacencyMatrix();
|
||||
|
||||
// Calcula o fecho transitivo do grafo.
|
||||
for (auto k = 0; k < this->nodes; k++)
|
||||
{
|
||||
for (auto i = 0; i < this->nodes; i++)
|
||||
{
|
||||
for (auto j = 0; j < this->nodes; j++)
|
||||
{
|
||||
if (i == j)
|
||||
{
|
||||
closure[i][j] = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
closure[i][j] = closure[i][j] || (closure[i][k] && closure[k][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new Graph(closure, closure.size());
|
||||
}
|
||||
|
||||
/*
|
||||
void Graph::TransitiveClosure()
|
||||
{
|
||||
int m1[length][length];
|
||||
@ -236,7 +167,14 @@ void Graph::TransitiveClosure()
|
||||
{
|
||||
for (auto j = 0; j < length; j++)
|
||||
{
|
||||
m2[i][j] = m2[i][j] || (m2[i][k] && m2[k][j]);
|
||||
if (i == j)
|
||||
{
|
||||
m2[i][j] = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
m2[i][j] = m2[i][j] || (m2[i][k] && m2[k][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -286,8 +224,14 @@ void Graph::TransitiveClosure()
|
||||
{
|
||||
for (auto j = 0; j < length; j++)
|
||||
{
|
||||
int temp = m1[i][j] - m3[i][j];
|
||||
m3[i][j] = temp > 0 ? temp : 0;
|
||||
if (m1[i][j] >= 1 && m3[i][j] >= 1)
|
||||
{
|
||||
m3[i][j] = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
m3[i][j] = m1[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -303,3 +247,4 @@ void Graph::TransitiveClosure()
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
Reference in New Issue
Block a user