C data structures and algorithms - graph problem

Click For Summary
SUMMARY

The discussion focuses on finding the shortest cyclic path for each vertex in a connected, directed, and weighted graph using C programming. Participants suggest utilizing Depth First Search (DFS) for cycle detection but highlight its potential exponential time complexity. Alternatives such as the Floyd-Warshall algorithm and Dijkstra's algorithm are recommended, noting that modifications are necessary to accommodate cycle detection, which increases the complexity to O(V³). Key implementation considerations include managing a visited vector and selecting appropriate data structures for efficiency.

PREREQUISITES
  • Understanding of Depth First Search (DFS) in graph theory
  • Familiarity with Floyd-Warshall algorithm for shortest paths
  • Knowledge of Dijkstra's algorithm and its modifications
  • Proficiency in C programming and data structure management
NEXT STEPS
  • Research the implementation of Floyd-Warshall algorithm in C
  • Learn about cycle detection techniques in directed graphs
  • Explore modifications to Dijkstra's algorithm for cyclic paths
  • Investigate efficient data structures for graph representation in C
USEFUL FOR

Students and developers working on graph algorithms, particularly those focusing on cycle detection and pathfinding in directed graphs using C programming.

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
3K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 22 ·
Replies
22
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K