Find the greatest of the shortest paths

  • MHB
  • Thread starter evinda
  • Start date
In summary, the conversation discusses the definition of diameter in a graph and the goal of finding the diameter of a given graph using the Floyd-Warshall algorithm. The algorithm is provided in both code and word description form, and there is a discussion about its time complexity, which is determined to be $\Theta(n^3)$. The conversation ends with a thank you and acknowledgement of the well-known fact that the Floyd-Warshall algorithm takes $O(n^3)$ time.
  • #1
evinda
Gold Member
MHB
3,836
0
Hello! (Wave)We define as diameter the greatest of the shortest paths of a graph.

We are given a graph $G=(V,E)$.

I want to write an algorithm that finds the diameter of the graph $G$.

I have tried the following:
Code:
    Diam(G) 
    // Let W be a  nxn array with the weight edges of G
    1. FLOYD-WARSHALL(W)
    2. max=d_{11}
    3. for i=1 to n 
    4.     for j=1 to n 
    5.         if (d_(ij)>max) 
    6.             max=d_(ij) 
    7. return max
    
    
    
    FLOYD-WARSHALL(W)
    1.   n=W.rows
    2.   D^(0)=W
    3.   for k=1 to n
    4.       let D^(k)=(d_(ij)^(k)) be a new  nxn matrix
    5.       for i=1 to n
    6.           for j=1 to n
    7.               d_(ij)^(k)=min(d_(ij)^((k-1)), d_(ik)^((k-1))+d_(kj)^(k-1))
    8. return D^(n)

Could you tell me if my idea is right? (Thinking)
 
Technology news on Phys.org
  • #2
evinda said:
Hello! (Wave)We define as diameter the greatest of the shortest paths of a graph.

We are given a graph $G=(V,E)$.

I want to write an algorithm that finds the diameter of the graph $G$.

I have tried the following:
Code:
    Diam(G) 
    // Let W be a  nxn array with the weight edges of G
    1. FLOYD-WARSHALL(W)
    2. max=d_{11}
    3. for i=1 to n 
    4.     for j=1 to n 
    5.         if (d_(ij)>max) 
    6.             max=d_(ij) 
    7. return max
    
    
    
    FLOYD-WARSHALL(W)
    1.   n=W.rows
    2.   D^(0)=W
    3.   for k=1 to n
    4.       let D^(k)=(d_(ij)^(k)) be a new  nxn matrix
    5.       for i=1 to n
    6.           for j=1 to n
    7.               d_(ij)^(k)=min(d_(ij)^((k-1)), d_(ik)^((k-1))+d_(kj)^(k-1))
    8. return D^(n)

Could you tell me if my idea is right? (Thinking)

Hello evinda.

I apologize in advance that my post is not helpful in answering your question.

I just want to suggest that if you want your algorithm checked then you should actually put your algorithm rather than the code. Checking someones code is an arduous task.
 
  • #3
I have an opinion about your code, but could you explain why you think it computes the graph diameter (without explaining the details of the Floyd–Warshall algorithm)?
 
  • #4
Evgeny.Makarov said:
I have an opinion about your code, but could you explain why you think it computes the graph diameter (without explaining the details of the Floyd–Warshall algorithm)?

The Floyd-Warshall algorithm returns the matrix $D^{(n)}=(d_{ij}^{(n)})$ of shortest-path weights.
Isn't the diameter the maximum of the shortest-path weights?
Or am I wrong?
 
  • #5
You are right. But I agree with caffeinemachine that a word description like this is easier to understand then a code.
 
  • #6
Evgeny.Makarov said:
You are right. But I agree with caffeinemachine that a word description like this is easier to understand then a code.

Nice! (Happy)

The time complexity of the algorithm [m]Diam(G)[/m] is $\Theta(n^3)$, since:

  • the line 1, where we call FLOYD-WARSHALL(W), requires $\Theta(n^3)$ time,
  • the line 2 requires $O(1)$ time,
  • the lines 3-6 require $O(n^2)$ time, since each for-loop requires $O(n)$ time and the lines $5,6$ require constant time and finally,
  • the line 7 requires contant time.

Does this suffice or do I have to show also that the Floyd-Warshall algorithm requires $O(n^3)$ time?

Code:
    Diam(G) 
    // Let W be a  nxn array with the weight edges of G
    1. FLOYD-WARSHALL(W)
    2. max=d_{11}
    3. for i=1 to n 
    4.     for j=1 to n 
    5.         if (d_(ij)>max) 
    6.             max=d_(ij) 
    7. return max
    
    
    
    FLOYD-WARSHALL(W)
    1.   n=W.rows
    2.   D^(0)=W
    3.   for k=1 to n
    4.       let D^(k)=(d_(ij)^(k)) be a new  nxn matrix
    5.       for i=1 to n
    6.           for j=1 to n
    7.               d_(ij)^(k)=min(d_(ij)^((k-1)), d_(ik)^((k-1))+d_(kj)^(k-1))
    8. return D^(n)
 
  • #7
evinda said:
the lines 3-6 require $O(n^2)$ time, since each for-loop requires $O(n)$ time and the lines $5,6$ require constant time
I wouldn't say that the outer loop requires $O(n)$ time. When I am talking about a loop, I include its body, and by saying that a loop takes a certain time I mean that it takes that time for the whole loop to finish. So the outer loop takes time $O(n^2)$ because its body (the inner loop) executes $n$ times and each execution takes time $O(n)$.

evinda said:
Does this suffice or do I have to show also that the Floyd-Warshall algorithm requires $O(n^3)$ time?
This is a well-known fact, but it depends on the requirements of your course.
 
  • #8
Evgeny.Makarov said:
I wouldn't say that the outer loop requires $O(n)$ time. When I am talking about a loop, I include its body, and by saying that a loop takes a certain time I mean that it takes that time for the whole loop to finish. So the outer loop takes time $O(n^2)$ because its body (the inner loop) executes $n$ times and each execution takes time $O(n)$.

This is a well-known fact, but it depends on the requirements of your course.

I see... Thanks a lot! (Nod)
 

FAQ: Find the greatest of the shortest paths

What does "find the greatest of the shortest paths" mean?

"Find the greatest of the shortest paths" refers to finding the longest possible path between two points while still maintaining the shortest distance possible. This is commonly used in graph theory and optimization problems.

How is "find the greatest of the shortest paths" different from finding the shortest path?

While finding the shortest path focuses on minimizing the distance between two points, "find the greatest of the shortest paths" aims to find the longest possible path while still keeping the distance as short as possible. This requires a different approach and algorithms compared to finding the shortest path.

What are some real-world applications of "find the greatest of the shortest paths"?

"Find the greatest of the shortest paths" can be applied in various fields such as transportation, logistics, and network routing. For example, it can be used to optimize flight paths to reduce travel time while still maintaining the shortest distance possible.

What are some common algorithms used for "find the greatest of the shortest paths"?

Some commonly used algorithms for "find the greatest of the shortest paths" include Dijkstra's algorithm, Bellman-Ford algorithm, and Floyd-Warshall algorithm. These algorithms are designed to find the longest possible path while still keeping the distance as short as possible.

What are some challenges in solving "find the greatest of the shortest paths"?

One of the main challenges in solving "find the greatest of the shortest paths" is the complexity of the problem. As the number of nodes and edges in a graph increases, the time and computational resources required to find the greatest of the shortest paths also increase. Additionally, there may be multiple paths with the same length, making it difficult to determine the greatest path.

Similar threads

Replies
19
Views
2K
Replies
1
Views
2K
Replies
97
Views
8K
Replies
2
Views
2K
Replies
2
Views
2K
Replies
1
Views
1K
Replies
17
Views
1K
Back
Top