My aim is to find all the cycles and their respective weights in an weighted undirected graph. The weight of a cycle is defined as sum of the weights of the paths that constitute the cycle. My preset algorithm does the following:
dfs(int start, int now,int val)
{
if(visited[now])
return;
if(now==start)
{
v.push_back(val);// v is the vector of all weights
return;
}
dfs through all nodes neighbouring to now;
}
I call dfs()
from each start point:
for(int i=0;i<V;++i)
{
initialise visited[];
for(int j=0;j<adj[i].size();++j)// adj is the adjacency matrix
dfs(i,adj[i][j].first,adj[i][j].second);
// adj is a vector of vector of pairs
// The first element of the pair is the neighbour index and the second element is the weight
}
So the overall complexity of this algorithm is O(V*E)
(I think so). Can anyone suggest a better approach?
Since not everyone defines it the same way, I assume...
Following steps can determine if (at least) one odd-weighted cycle exists:
Complexity O(V + E)
(this time really, instead of a exponential thing or not-working solutions).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.