C data structures and algorithms - graph problem

In summary, the algorithm tries to find shortest cyclic path for each vertex. After the first source vertex is processed, next vertex becomes the source. The algorithm needs to keep track of [I], where [I] is the number of nodes found along the shortest cyclic path.
  • #1
username_unknown
2
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
  • #2
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.
 
  • #3
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:

Related to C data structures and algorithms - graph problem

1. What is a graph data structure?

A graph data structure is a non-linear data structure that consists of nodes (vertices) connected by edges. It is used to represent relationships and connections between different entities or objects. Graphs are commonly used in computer science and can be directed (edges have a specific direction) or undirected (edges have no direction).

2. What are the different types of graphs?

There are several types of graphs, including:

  • Undirected Graph: A graph where edges have no direction.
  • Directed Graph: A graph where edges have a specific direction.
  • Weighted Graph: A graph where edges have a weight or cost associated with them.
  • Cyclic Graph: A graph that contains a cycle (a path that starts and ends at the same node).
  • Acyclic Graph: A graph that does not contain any cycles.

3. What are some common graph algorithms?

Some common graph algorithms include:

  • Breadth-First Search (BFS): A graph traversal algorithm that starts at a given node and explores all its neighboring nodes before moving on to the next level of nodes.
  • Depth-First Search (DFS): A graph traversal algorithm that explores as far as possible along each branch before backtracking.
  • Dijkstra's Algorithm: A shortest path algorithm that finds the shortest path between two nodes in a weighted graph.
  • Prim's Algorithm: A minimum spanning tree algorithm that finds the minimum cost tree that connects all the nodes in a weighted graph.
  • Kruskal's Algorithm: Another minimum spanning tree algorithm that finds the minimum cost tree by adding edges in ascending order of their weights.

4. How do you represent a graph in C?

A graph can be represented in C using an adjacency matrix or an adjacency list. An adjacency matrix is a 2-dimensional array where the value at index [i][j] represents the weight of the edge between nodes i and j. An adjacency list is a collection of linked lists where each node has a list of its neighboring nodes.

5. What are some common applications of graph data structures?

Graph data structures have many applications, including:

  • Social Networks: Graphs can be used to represent connections between users in social networks.
  • Maps and Navigation: Graphs can be used to represent roads and intersections for navigation purposes.
  • Webpage Ranking: Graphs can be used to represent links between webpages and help determine their ranking in search engines.
  • Recommendation Systems: Graphs can be used to represent relationships between items and make recommendations based on these relationships.
  • Network Routing: Graphs can be used to represent networks and determine the most efficient path for data transmission.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • STEM Academic Advising
Replies
3
Views
949
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
885
  • Set Theory, Logic, Probability, Statistics
Replies
22
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Science and Math Textbooks
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
Back
Top