C data structures and algorithms - graph problem

Click For Summary
The discussion revolves around finding the shortest cyclic path for each vertex in a connected, directed, and weighted graph using C. Participants seek clarification on implementing depth-first search (DFS) for cycle detection and whether additional data structures are needed to track cycle weights. Suggestions include using the Floyd-Warshall or Dijkstra's algorithms, noting that modifications are necessary to adapt these algorithms for cycle detection. The complexity of the proposed solutions is highlighted as O(V^3), where V is the number of vertices. Efficient data structure selection is emphasized as crucial for successful implementation.
username_unknown
Messages
2
Reaction score
0

Homework Statement


Given connected, directed and weighted (positive weights) graph. Find the shortest cyclic path for each vertex. Cycles have back edges and can also be self loops.

2. The attempt at a solution
I need some clarifications for this problem related to implementation in C.

After depth first search is finished for the first source vertex, we can detect cycles from that source.
Do we need an additional queue or stack to store total weights for each cycle from one source, and return the largest total sum?
After one source is processed (depth first search traversal is finished for the current source), next vertex becomes the source.
How to reset a vector visited (this is a variable-array) for the next depth first search iteration (with the next source)?

Is there an algorithm that finds shortest cyclic path for each vertex directly, without doing modifications to depth first search algorithm?
 
Physics news on Phys.org
username_unknown said:
Do we need an additional queue or stack to store total weights for each cycle from one source, and return the largest total sum?
What do you mean by "each cycle"? Do you have more than one? Where and why would you want to have the largest total sum of anything?
username_unknown said:
How to reset a vector visited (this is a variable-array) for the next depth first search iteration (with the next source)?
You can start the whole algorithm from scratch again. This is not necessarily the optimal approach but it works.
 
username_unknown said:

Homework Statement


Given connected, directed and weighted (positive weights) graph. Find the shortest cyclic path for each vertex. Cycles have back edges and can also be self loops.

2. The attempt at a solution
I need some clarifications for this problem related to implementation in C.

After depth first search is finished for the first source vertex, we can detect cycles from that source.
Do we need an additional queue or stack to store total weights for each cycle from one source, and return the largest total sum?
After one source is processed (depth first search traversal is finished for the current source), next vertex becomes the source.
How to reset a vector visited (this is a variable-array) for the next depth first search iteration (with the next source)?

Is there an algorithm that finds shortest cyclic path for each vertex directly, without doing modifications to depth first search algorithm?

I think that utilizing DFS in this case, may lead to exponential times.

You can use Floyd - Warshall algorithm https://en.wikipedia.org/wiki/Floyd–Warshall_algorithm or Dijkstra's algorithm https://en.wikipedia.org/wiki/Dijkstra's_algorithm, for shortest (even cyclic paths), but you inevitably have to modify whichever you choose. One such modification is setting
Code:
path[i][i] = infinity
. You must also keep track of where the algorithm finds cycle, in order to find which nodes lead to cycles. Keep in mind that whichever one of the two algorithms you use, the complexity will be O(V3), with V denoting the number of nodes.

For the implementation, you have to choose whichever data structure does your job efficiently. Due to the modifications required, you have to think about it.
 
Last edited:

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
86
Views
2K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 22 ·
Replies
22
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K