问题
i have a directed graph, which has 0 or more circles. I would like to know if the largest product of the weights inside the circle exceeds a threshold.
Example:
--------->
^ |1
0.5 | <------v
1 -----------> 2
^ |
|4 |1
| 2 v
4<------------3
This Graph has 4 Edges and 2 circles. The first circle (2 -> 2) has a product of 1. The second circle (1 -> 2 -> 3 -> 4 -> 1) has a product of 4. The algorithm outputs true, if the product is greater than 1, otherwise it will output false. The output for this graph is true.
My approach:
- I am using a graph with a adjacency list
- I am using this algorithm, which is based on DFS, to detect cycles
- unlike the algorithm from GeeksForGeeks, I do not stop, after the first cycle, since I would like to find the cycle with the biggest products of weights
- After finding a cycle, I remove all nodes not part of the cycle from the recursion stack
- I use the nodes left over on the stack to calculate the product
Can you help me find the error in the following code?
My Code:
#include <iostream>
#include <list>
#include <limits.h>
#include <vector>
using namespace std;
class Graph
{
int V; // No. of vertices
list<pair<int, double>> *adj; // Pointer to an array containing adjacency lists
vector<double> isCyclicUtil(int v, bool visited[], bool *rs); // used by isCyclic()
public:
Graph(int V); // Constructor
void addEdge(int v, int w, double rate); // to add an edge to graph
bool isCyclic(); // returns true if there is a cycle in this graph
};
Graph::Graph(int V)
{
this->V = V;
adj = new list<pair<int, double>>[V];
}
void Graph::addEdge(int v, int w, double rate)
{
adj[v].push_back(make_pair(w, rate)); // Add w to v’s list.
}
vector<double> Graph::isCyclicUtil(int v, bool visited[], bool *recStack)
{
if (visited[v] == false)
{
// Mark the current node as visited and part of recursion stack
visited[v] = true;
recStack[v] = true;
// Recur for all the vertices adjacent to this vertex
list<pair<int, double>>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
{
if (!visited[(*i).first])
{
vector<double> tmp = isCyclicUtil((*i).first, visited, recStack);
if (tmp[0] == 1)
{
// is cycle
double newValue = tmp[2];
if ((*i).first != tmp[1])
{
newValue = tmp[2] * (*i).second;
}
return vector<double>{1, tmp[1], newValue};
}
}
else if (recStack[(*i).first])
{
// found cycle, with at node first and weight second
return vector<double>{1, (double)(*i).first, (*i).second};
}
}
}
// remove the vertex from recursion stack
recStack[v] = false;
return vector<double>{0, -1, -1};
}
// Returns true if the graph contains a cycle, else false.
// This function is a variation of DFS() in https://www.geeksforgeeks.org/archives/18212
bool Graph::isCyclic()
{
// Mark all the vertices as not visited and not part of recursion
// stack
bool *visited = new bool[V];
bool *recStack = new bool[V];
for (int i = 0; i < V; i++)
{
visited[i] = false;
recStack[i] = false;
}
// Call the recursive helper function to detect cycle in different
// DFS trees
for (int i = 0; i < V; i++)
{
vector<double> tmp = isCyclicUtil(i, visited, recStack);
if (tmp[2] > 1)
{
return true;
}
}
return false;
}
int main()
{
Graph g();
// add edges to graph
if (g.isCyclic())
{
cout << "true";
}
else {
cout << "false";
}
}
回答1:
Here is a partial answer to the question. It is working, whenever the threshold is equal to 1.
Using Bellman Ford to detect cycles with product exceeding threshold
来源:https://stackoverflow.com/questions/50394471/calculate-product-of-weights-in-circle-graph