mirror of
https://github.com/guilhermewerner/paa
synced 2025-06-16 13:34:18 +00:00
Add Floyd Warshall Algorithm
This commit is contained in:
@ -96,3 +96,63 @@ void Graph::Print()
|
|||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Graph::TransitiveClosure()
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -36,6 +36,7 @@ public:
|
|||||||
void InsertD(int u, int v);
|
void InsertD(int u, int v);
|
||||||
void Remove(int u, int v);
|
void Remove(int u, int v);
|
||||||
void RemoveD(int u, int v);
|
void RemoveD(int u, int v);
|
||||||
|
void TransitiveClosure();
|
||||||
Graph *Clone();
|
Graph *Clone();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -2,27 +2,17 @@
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int length = 9;
|
int length = 4;
|
||||||
Graph *g1 = new Graph(length);
|
Graph *g1 = new Graph(length);
|
||||||
|
|
||||||
g1->Insert(0, 1);
|
g1->InsertD(0, 1);
|
||||||
g1->Insert(0, 3);
|
g1->InsertD(0, 2);
|
||||||
g1->Insert(0, 4);
|
g1->InsertD(1, 2);
|
||||||
g1->Insert(1, 2);
|
g1->InsertD(2, 0);
|
||||||
g1->Insert(1, 3);
|
g1->InsertD(2, 3);
|
||||||
g1->Insert(1, 4);
|
|
||||||
g1->Insert(2, 3);
|
|
||||||
g1->Insert(2, 4);
|
|
||||||
g1->Insert(2, 5);
|
|
||||||
g1->Insert(3, 5);
|
|
||||||
g1->Insert(4, 5);
|
|
||||||
g1->Insert(5, 6);
|
|
||||||
g1->Insert(6, 7);
|
|
||||||
g1->Insert(6, 8);
|
|
||||||
g1->Insert(7, 8);
|
|
||||||
|
|
||||||
cout << "-- GRAFO --\n\n";
|
cout << "-- TRANSITIVE CLOSURE --\n\n";
|
||||||
g1->Print();
|
g1->TransitiveClosure();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user