MHB Find the greatest of the shortest paths

  • Thread starter Thread starter evinda
  • Start date Start date
AI Thread Summary
The discussion revolves around an algorithm designed to compute the diameter of a graph using the Floyd-Warshall algorithm. The diameter is defined as the greatest of the shortest paths within the graph. The proposed algorithm initializes a weight matrix and applies Floyd-Warshall to find shortest-path weights, then iterates through the resulting matrix to determine the maximum value, which represents the diameter.Participants highlight the importance of clarity in presenting algorithms, suggesting that a descriptive explanation may be more effective than code alone. The time complexity of the proposed algorithm is noted to be Θ(n^3), primarily due to the Floyd-Warshall algorithm's complexity. There is a debate about the time complexity of the loops involved, with some clarification provided on how to interpret loop execution times. Overall, the discussion emphasizes the correctness of the approach and the need for clear communication in algorithm design.
evinda
Gold Member
MHB
Messages
3,741
Reaction score
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
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.
 
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)?
 
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?
 
You are right. But I agree with caffeinemachine that a word description like this is easier to understand then a code.
 
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)
 
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.
 
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)
 

Similar threads

Back
Top